oops, the key was preferred_species, not default_species.
[sgn.git] / lib / SGN / Controller / BreedersToolbox.pm
blob0395221ba2fb6a112e73fe67571a6c30a9e69fd6
2 package SGN::Controller::BreedersToolbox;
4 use Moose;
6 use Data::Dumper;
8 use CXGN::Trial::TrialLayout;
9 use URI::FromHash 'uri';
11 use CXGN::BreederSearch;
12 use SGN::Controller::AJAX::List;
13 use CXGN::List::Transform;
14 use CXGN::BreedersToolbox::Projects;
15 use CXGN::BreedersToolbox::Accessions;
17 BEGIN { extends 'Catalyst::Controller'; }
19 sub manage_breeding_programs : Path("/breeders/manage_programs") :Args(0) {
20 my $self = shift;
21 my $c = shift;
23 if (!$c->user()) {
25 # redirect to login page
26 $c->res->redirect( uri( path => '/solpeople/login.pl', query => { goto_url => $c->req->uri->path_query } ) );
27 return;
30 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
32 my $projects = CXGN::BreedersToolbox::Projects->new( { schema=> $schema } );
34 my $breeding_programs = $projects->get_breeding_programs();
36 $c->stash->{breeding_programs} = $breeding_programs;
37 $c->stash->{user} = $c->user();
39 $c->stash->{template} = '/breeders_toolbox/breeding_programs.mas';
44 sub manage_trials : Path("/breeders/trials") Args(0) {
45 my $self = shift;
46 my $c = shift;
48 if (!$c->user()) {
50 # redirect to login page
51 $c->res->redirect( uri( path => '/solpeople/login.pl', query => { goto_url => $c->req->uri->path_query } ) );
52 return;
55 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
57 my $projects = CXGN::BreedersToolbox::Projects->new( { schema=> $schema } );
59 my $breeding_programs = $projects->get_breeding_programs();
61 my %trials_by_breeding_project = ();
63 foreach my $bp (@$breeding_programs) {
64 print STDERR "RETRIEVING TRIALS FOR $bp->[1]\n";
65 $trials_by_breeding_project{$bp->[1]}= $projects->get_trials_by_breeding_program($bp->[0]);
68 print STDERR Dumper(\%trials_by_breeding_project);
70 $trials_by_breeding_project{'Other'} = $projects->get_trials_by_breeding_program();
72 # use get_all_locations, as other calls for locations can be slow
74 $c->stash->{locations} = $projects->get_all_locations();
76 $c->stash->{trials_by_breeding_project} = \%trials_by_breeding_project;
78 $c->stash->{breeding_programs} = $breeding_programs;
80 $c->stash->{template} = '/breeders_toolbox/manage_projects.mas';
83 sub manage_accessions : Path("/breeders/accessions") Args(0) {
84 my $self = shift;
85 my $c = shift;
86 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
88 if (!$c->user()) {
89 # redirect to login page
91 $c->res->redirect( uri( path => '/solpeople/login.pl', query => { goto_url => $c->req->uri->path_query } ) );
92 return;
95 my $ac = CXGN::BreedersToolbox::Accessions->new( { schema=>$schema });
97 my $accessions = $ac->get_all_accessions($c);
98 my $populations = $ac->get_all_populations($c);
100 $c->stash->{accessions} = $accessions;
101 $c->stash->{population_groups} = $populations;
102 $c->stash->{preferred_species} = $c->config->{preferred_species};
103 $c->stash->{template} = '/breeders_toolbox/manage_accessions.mas';
108 sub manage_locations : Path("/breeders/locations") Args(0) {
109 my $self = shift;
110 my $c = shift;
112 if (!$c->user()) {
114 # redirect to login page
116 $c->res->redirect( uri( path => '/solpeople/login.pl', query => { goto_url => $c->req->uri->path_query } ) );
117 return;
120 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
121 my $bp = CXGN::BreedersToolbox::Projects->new( { schema=>$schema });
122 my $breeding_programs = $bp->get_breeding_programs();
123 my $locations = {};
124 foreach my $b (@$breeding_programs) {
125 $locations->{$b->[1]} = $bp->get_locations_by_breeding_program($b->[0]);
127 $locations->{'Other'} = $bp->get_locations_by_breeding_program();
129 $c->stash->{user_id} = $c->user()->get_object()->get_sp_person_id();
131 $c->stash->{locations} = $locations;
133 $c->stash->{template} = '/breeders_toolbox/manage_locations.mas';
136 sub manage_crosses : Path("/breeders/crosses") Args(0) {
137 my $self = shift;
138 my $c = shift;
140 if (!$c->user()) {
142 # redirect to login page
144 $c->res->redirect( uri( path => '/solpeople/login.pl', query => { goto_url => $c->req->uri->path_query } ) );
145 return;
147 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
148 my $bp = CXGN::BreedersToolbox::Projects->new( { schema=>$schema });
149 my $breeding_programs = $bp->get_breeding_programs();
151 $c->stash->{user_id} = $c->user()->get_object()->get_sp_person_id();
153 $c->stash->{locations} = $bp->get_all_locations($c);
155 #$c->stash->{projects} = $self->get_projects($c);
157 $c->stash->{programs} = $breeding_programs;
159 $c->stash->{roles} = $c->user()->roles();
161 $c->stash->{cross_populations} = $self->get_crosses($c);
163 $c->stash->{template} = '/breeders_toolbox/manage_crosses.mas';
167 sub manage_phenotyping :Path("/breeders/phenotyping") Args(0) {
168 my $self =shift;
169 my $c = shift;
171 if (!$c->user()) {
172 $c->res->redirect( uri( path => '/solpeople/login.pl', query => { goto_url => $c->req->uri->path_query } ) );
173 return;
176 my $data = $self->get_phenotyping_data($c);
178 $c->stash->{phenotype_files} = $data->{file_info};
179 $c->stash->{deleted_phenotype_files} = $data->{deleted_file_info};
181 $c->stash->{template} = '/breeders_toolbox/manage_phenotyping.mas';
187 sub make_cross_form :Path("/stock/cross/new") :Args(0) {
188 my ($self, $c) = @_;
189 $c->stash->{template} = '/breeders_toolbox/new_cross.mas';
190 if ($c->user()) {
191 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
192 # get projects
193 my @rows = $schema->resultset('Project::Project')->all();
194 my @projects = ();
195 foreach my $row (@rows) {
196 push @projects, [ $row->project_id, $row->name, $row->description ];
198 $c->stash->{project_list} = \@projects;
199 @rows = $schema->resultset('NaturalDiversity::NdGeolocation')->all();
200 my @locations = ();
201 foreach my $row (@rows) {
202 push @locations, [ $row->nd_geolocation_id,$row->description ];
204 $c->stash->{locations} = \@locations;
207 else {
208 $c->res->redirect( uri( path => '/solpeople/login.pl', query => { goto_url => $c->req->uri->path_query } ) );
209 return;
214 sub make_cross :Path("/stock/cross/generate") :Args(0) {
215 my ($self, $c) = @_;
216 $c->stash->{template} = '/breeders_toolbox/progeny_from_crosses.mas';
217 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
218 my $cross_name = $c->req->param('cross_name');
219 $c->stash->{cross_name} = $cross_name;
220 my $trial_id = $c->req->param('trial_id');
221 $c->stash->{trial_id} = $trial_id;
222 #my $location = $c->req->param('location_id');
223 my $maternal = $c->req->param('maternal');
224 my $paternal = $c->req->param('paternal');
225 my $prefix = $c->req->param('prefix');
226 my $suffix = $c->req->param('suffix');
227 my $progeny_number = $c->req->param('progeny_number');
228 my $visible_to_role = $c->req->param('visible_to_role');
230 if (! $c->user()) { # redirect
231 $c->res->redirect( uri( path => '/solpeople/login.pl', query => { goto_url => $c->req->uri->path_query } ) );
232 return;
236 #check that progeny number is an integer less than maximum allowed
237 my $maximum_progeny_number = 1000;
238 if ((! $progeny_number =~ m/^\d+$/) or ($progeny_number > $maximum_progeny_number)){
239 #redirect to error page?
240 return;
243 #check that parent names are not blank
244 if ($maternal eq "" or $paternal eq "") {
245 return;
248 #check that parents exist in the database
249 if (! $schema->resultset("Stock::Stock")->find({name=>$maternal,})){
250 return;
252 if (! $schema->resultset("Stock::Stock")->find({name=>$paternal,})){
253 return;
256 #check that cross name does not already exist
257 if ($schema->resultset("Stock::Stock")->find({name=>$cross_name})){
258 return;
261 #check that progeny do not already exist
262 if ($schema->resultset("Stock::Stock")->find({name=>$prefix.$cross_name.$suffix."-1",})){
263 return;
266 my $organism = $schema->resultset("Organism::Organism")->find_or_create(
268 genus => 'Manihot',
269 species => 'Manihot esculenta',
270 } );
271 my $organism_id = $organism->organism_id();
273 my $accession_cvterm = $schema->resultset("Cv::Cvterm")->create_with(
274 { name => 'accession',
275 cv => 'stock type',
276 db => 'null',
277 dbxref => 'accession',
280 # my $population_cvterm = $schema->resultset("Cv::Cvterm")->create_with(
281 # { name => 'member',
282 # cv => 'stock type',
283 # db => 'null',
284 # dbxref => 'member',
285 # });
287 my $population_cvterm = $schema->resultset("Cv::Cvterm")->find(
288 { name => 'population',
291 # my $cross_cvterm = $schema->resultset("Cv::Cvterm")->create_with(
292 # { name => 'cross',
293 # cv => 'stock relationship',
294 # db => 'null',
295 # dbxref => 'cross',
296 # });
298 my $female_parent_stock = $schema->resultset("Stock::Stock")->find(
299 { name => $maternal,
300 } );
302 my $male_parent_stock = $schema->resultset("Stock::Stock")->find(
303 { name => $paternal,
304 } );
306 my $population_stock = $schema->resultset("Stock::Stock")->find_or_create(
307 { organism_id => $organism_id,
308 name => $cross_name,
309 uniquename => $cross_name,
310 type_id => $population_cvterm->cvterm_id,
311 } );
312 my $female_parent = $schema->resultset("Cv::Cvterm")->create_with(
313 { name => 'female_parent',
314 cv => 'stock relationship',
315 db => 'null',
316 dbxref => 'female_parent',
319 my $male_parent = $schema->resultset("Cv::Cvterm")->create_with(
320 { name => 'male_parent',
321 cv => 'stock relationship',
322 db => 'null',
323 dbxref => 'male_parent',
326 my $population_members = $schema->resultset("Cv::Cvterm")->create_with(
327 { name => 'cross_name',
328 cv => 'stock relationship',
329 db => 'null',
330 dbxref => 'cross_name',
333 my $visible_to_role_cvterm = $schema->resultset("Cv::Cvterm")->create_with(
334 { name => 'visible_to_role',
335 cv => 'local',
336 db => 'null',
339 my $increment = 1;
340 while ($increment < $progeny_number + 1) {
341 $increment = sprintf "%03d", $increment;
342 my $stock_name = $prefix.$cross_name."_".$increment.$suffix;
343 my $accession_stock = $schema->resultset("Stock::Stock")->create(
344 { organism_id => $organism_id,
345 name => $stock_name,
346 uniquename => $stock_name,
347 type_id => $accession_cvterm->cvterm_id,
348 } );
349 $accession_stock->find_or_create_related('stock_relationship_objects', {
350 type_id => $female_parent->cvterm_id(),
351 object_id => $accession_stock->stock_id(),
352 subject_id => $female_parent_stock->stock_id(),
353 } );
354 $accession_stock->find_or_create_related('stock_relationship_objects', {
355 type_id => $male_parent->cvterm_id(),
356 object_id => $accession_stock->stock_id(),
357 subject_id => $male_parent_stock->stock_id(),
358 } );
359 $accession_stock->find_or_create_related('stock_relationship_objects', {
360 type_id => $population_members->cvterm_id(),
361 object_id => $accession_stock->stock_id(),
362 subject_id => $population_stock->stock_id(),
363 } );
364 if ($visible_to_role ne "") {
365 my $accession_stock_prop = $schema->resultset("Stock::Stockprop")->find_or_create(
366 { type_id =>$visible_to_role_cvterm->cvterm_id(),
367 value => $visible_to_role,
368 stock_id => $accession_stock->stock_id()
371 $increment++;
374 if ($@) {
381 sub breeder_home :Path("/breeders/home") Args(0) {
382 my ($self , $c) = @_;
385 if (!$c->user()) {
387 # redirect to login page
388 $c->res->redirect( uri( path => '/solpeople/login.pl', query => { goto_url => $c->req->uri->path_query } ) );
389 return;
392 # my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
393 # my $bp = CXGN::BreedersToolbox::Projects->new( { schema=>$schema });
394 # my $breeding_programs = $bp->get_breeding_programs();
396 # $c->stash->{programs} = $breeding_programs;
397 # $c->stash->{breeding_programs} = $breeding_programs;
399 # # my $locations_by_breeding_program;
400 # # foreach my $b (@$breeding_programs) {
401 # # $locations_by_breeding_program->{$b->[1]} = $bp->get_locations_by_breeding_program($b->[0]);
402 # # }
403 # # $locations_by_breeding_program->{'Other'} = $bp->get_locations_by_breeding_program();
405 # $c->stash->{locations_by_breeding_program} = ""; #$locations_by_breeding_program;
407 # # get roles
409 # my @roles = $c->user->roles();
410 # $c->stash->{roles}=\@roles;
412 # $c->stash->{cross_populations} = $self->get_crosses($c);
414 # $c->stash->{stockrelationships} = $self->get_stock_relationships($c);
416 # my $locations = $bp->get_locations($c);
418 # $c->stash->{locations} = $locations;
419 # # get uploaded phenotype files
422 # my $data = $self->get_phenotyping_data($c);
424 # $c->stash->{phenotype_files} = $data->{file_info};
425 # $c->stash->{deleted_phenotype_files} = $data->{deleted_file_info};
428 $c->stash->{template} = '/breeders_toolbox/home.mas';
432 sub breeder_search : Path('/breeders/search/') :Args(0) {
433 my ($self, $c) = @_;
435 $c->stash->{template} = '/breeders_toolbox/breeder_search_page.mas';
439 # next two functions moved to CXGN::BreedersToolbox::Project
441 # sub get_locations : Private {
442 # my $self = shift;
443 # my $c= shift;
445 # my $schema = $c->dbic_schema("Bio::Chado::Schema");
447 # my @rows = $schema->resultset('NaturalDiversity::NdGeolocation')->all();
449 # my $type_id = $schema->resultset('Cv::Cvterm')->search( { 'name'=>'plot' })->first->cvterm_id;
452 # my @locations = ();
453 # foreach my $row (@rows) {
454 # my $plot_count = "SELECT count(*) from stock join cvterm on(type_id=cvterm_id) join nd_experiment_stock using(stock_id) join nd_experiment using(nd_experiment_id) where cvterm.name='plot' and nd_geolocation_id=?"; # and sp_person_id=?";
455 # my $sh = $c->dbc->dbh->prepare($plot_count);
456 # $sh->execute($row->nd_geolocation_id); #, $c->user->get_object->get_sp_person_id);
458 # my ($count) = $sh->fetchrow_array();
460 # #if ($count > 0) {
462 # push @locations, [ $row->nd_geolocation_id,
463 # $row->description,
464 # $row->latitude,
465 # $row->longitude,
466 # $row->altitude,
467 # $count, # number of experiments TBD
469 # ];
471 # return \@locations;
475 # sub get_all_locations {
476 # my $self = shift;
477 # my $c = shift;
479 # my $schema = $c->dbic_schema("Bio::Chado::Schema");
480 # my $rs = $schema -> resultset("NaturalDiversity::NdGeolocation")->search( {} );
482 # my @locations = ();
483 # foreach my $loc ($rs->all()) {
484 # push @locations, [ $loc->nd_geolocation_id(), $loc->description() ];
486 # return \@locations;
490 # sub get_projects : Private {
491 # my $self = shift;
492 # my $c = shift;
494 # my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
497 # # get breeding programs
500 # my $bp_rows = ();
501 # # get projects
503 # my @projects = ();
504 # foreach my $bp (@bp_rows) {
505 # my @project_rows = $schema->resultset('Project::Project')->search( { }, { join => 'project_relationship', { join => 'project' }}) ;
508 # foreach my $row (@project_rows) {
509 # push @projects, [ $row->project_id, $row->name, $row->description ];
513 # return \@projects;
516 sub get_crosses : Private {
517 my $self = shift;
518 my $c = shift;
520 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
522 # get crosses
524 my $stock_type_cv = $schema->resultset("Cv::Cv")->find( {name=>'stock type'});
525 my $cross_cvterm = $schema->resultset("Cv::Cvterm")->find(
526 { name => 'cross',
527 cv_id => $stock_type_cv->cv_id(),
529 my @cross_populations = ();
531 if ($cross_cvterm) {
533 my @cross_population_stocks = $schema->resultset("Stock::Stock")->search(
534 { type_id => $cross_cvterm->cvterm_id, is_obsolete => 'f'
535 } );
536 foreach my $cross_pop (@cross_population_stocks) {
537 push @cross_populations, [$cross_pop->name,$cross_pop->stock_id];
540 return \@cross_populations;
544 sub get_stock_relationships : Private {
545 my $self = shift;
546 my $c = shift;
548 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
550 my $stockrel = $schema->resultset("Cv::Cvterm")->create_with(
551 { name => 'cross',
552 cv => 'stock relationship',
553 db => 'null',
554 dbxref => 'cross',
559 #my $stockrel_type_id = $schema->resultset('Cv::Cvterm')->search( { 'name'=>'cross' })->first->cvterm_id;
561 my @rows = $schema->resultset('Stock::StockRelationship')->search( {type_id => $stockrel->cvterm_id });
563 my @stockrelationships = ();
565 foreach my $row (@rows) {
566 push @stockrelationships, [$row->type_id];
569 push @stockrelationships, ["example"];
571 return \@stockrelationships;
575 sub get_phenotyping_data : Private {
576 my $self = shift;
577 my $c = shift;
579 my $metadata_schema = $c->dbic_schema("CXGN::Metadata::Schema");
581 my $file_info = [];
582 my $deleted_file_info = [];
584 my $metadata_rs = $metadata_schema->resultset("MdMetadata")->search( { create_person_id => $c->user()->get_object->get_sp_person_id(), obsolete => 0 }, { order_by => 'create_date' } );
586 print STDERR "RETRIEVED ".$metadata_rs->count()." METADATA ENTRIES...\n";
588 while (my $md_row = ($metadata_rs->next())) {
589 my $file_rs = $metadata_schema->resultset("MdFiles")->search( { metadata_id => $md_row->metadata_id() } );
591 if (!$md_row->obsolete) {
592 while (my $file_row = $file_rs->next()) {
593 push @$file_info, { file_id => $file_row->file_id(),
594 basename => $file_row->basename,
595 dirname => $file_row->dirname,
596 file_type => $file_row->filetype,
597 md5checksum => $file_row->md5checksum,
598 create_date => $md_row->create_date,
602 else {
603 while (my $file_row = $file_rs->next()) {
604 push @$deleted_file_info, { file_id => $file_row->file_id(),
605 basename => $file_row->basename,
606 dirname => $file_row->dirname,
607 file_type => $file_row->filetype,
608 md5checksum => $file_row->md5checksum,
609 create_date => $md_row->create_date,
615 my $data = { phenotype_files => $file_info,
616 deleted_phenotype_files => $deleted_file_info,
618 return $data;
623 sub manage_genotyping : Path("/breeders/genotyping") Args(0) {
624 my $self = shift;
625 my $c = shift;
627 if (!$c->user()) {
628 # redirect to login page
629 $c->res->redirect( uri( path => '/solpeople/login.pl', query => { goto_url => $c->req->uri->path_query } ) );
630 return;
633 my $schema = $c->dbic_schema('Bio::Chado::Schema');
635 my $projects = CXGN::BreedersToolbox::Projects->new( { schema=> $schema } );
637 my $breeding_programs = $projects->get_breeding_programs();
639 my %genotyping_trials_by_breeding_project = ();
641 foreach my $bp (@$breeding_programs) {
642 $genotyping_trials_by_breeding_project{$bp->[1]}= $projects->get_genotyping_trials_by_breeding_program($bp->[0]);
645 $genotyping_trials_by_breeding_project{'Other'} = $projects->get_genotyping_trials_by_breeding_program();
647 $c->stash->{locations} = $projects->get_all_locations($c);
649 $c->stash->{genotyping_trials_by_breeding_project} = \%genotyping_trials_by_breeding_project;
651 $c->stash->{breeding_programs} = $breeding_programs;
654 $c->stash->{template} = '/breeders_toolbox/manage_genotyping.mas';