2 package SGN
::Controller
::BreedersToolbox
;
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) {
25 # redirect to login page
26 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
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) {
50 # redirect to login page
51 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
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) {
86 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
89 # redirect to login page
91 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
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) {
114 # redirect to login page
116 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
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();
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) {
142 # redirect to login page
144 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
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) {
172 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
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) {
189 $c->stash->{template
} = '/breeders_toolbox/new_cross.mas';
191 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
193 my @rows = $schema->resultset('Project::Project')->all();
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();
201 foreach my $row (@rows) {
202 push @locations, [ $row->nd_geolocation_id,$row->description ];
204 $c->stash->{locations
} = \
@locations;
208 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
214 sub make_cross
:Path
("/stock/cross/generate") :Args
(0) {
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 } ) );
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?
243 #check that parent names are not blank
244 if ($maternal eq "" or $paternal eq "") {
248 #check that parents exist in the database
249 if (! $schema->resultset("Stock::Stock")->find({name
=>$maternal,})){
252 if (! $schema->resultset("Stock::Stock")->find({name
=>$paternal,})){
256 #check that cross name does not already exist
257 if ($schema->resultset("Stock::Stock")->find({name
=>$cross_name})){
261 #check that progeny do not already exist
262 if ($schema->resultset("Stock::Stock")->find({name
=>$prefix.$cross_name.$suffix."-1",})){
266 my $organism = $schema->resultset("Organism::Organism")->find_or_create(
269 species
=> 'Manihot esculenta',
271 my $organism_id = $organism->organism_id();
273 my $accession_cvterm = $schema->resultset("Cv::Cvterm")->create_with(
274 { name
=> 'accession',
277 dbxref
=> 'accession',
280 # my $population_cvterm = $schema->resultset("Cv::Cvterm")->create_with(
281 # { name => 'member',
282 # cv => 'stock type',
284 # dbxref => 'member',
287 my $population_cvterm = $schema->resultset("Cv::Cvterm")->find(
288 { name
=> 'population',
291 # my $cross_cvterm = $schema->resultset("Cv::Cvterm")->create_with(
293 # cv => 'stock relationship',
298 my $female_parent_stock = $schema->resultset("Stock::Stock")->find(
302 my $male_parent_stock = $schema->resultset("Stock::Stock")->find(
306 my $population_stock = $schema->resultset("Stock::Stock")->find_or_create(
307 { organism_id
=> $organism_id,
309 uniquename
=> $cross_name,
310 type_id
=> $population_cvterm->cvterm_id,
312 my $female_parent = $schema->resultset("Cv::Cvterm")->create_with(
313 { name
=> 'female_parent',
314 cv
=> 'stock relationship',
316 dbxref
=> 'female_parent',
319 my $male_parent = $schema->resultset("Cv::Cvterm")->create_with(
320 { name
=> 'male_parent',
321 cv
=> 'stock relationship',
323 dbxref
=> 'male_parent',
326 my $population_members = $schema->resultset("Cv::Cvterm")->create_with(
327 { name
=> 'cross_name',
328 cv
=> 'stock relationship',
330 dbxref
=> 'cross_name',
333 my $visible_to_role_cvterm = $schema->resultset("Cv::Cvterm")->create_with(
334 { name
=> 'visible_to_role',
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,
346 uniquename
=> $stock_name,
347 type_id
=> $accession_cvterm->cvterm_id,
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(),
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(),
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(),
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()
381 sub breeder_home
:Path
("/breeders/home") Args
(0) {
382 my ($self , $c) = @_;
387 # redirect to login page
388 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
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]);
403 # # $locations_by_breeding_program->{'Other'} = $bp->get_locations_by_breeding_program();
405 # $c->stash->{locations_by_breeding_program} = ""; #$locations_by_breeding_program;
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) {
435 $c->stash->{template
} = '/breeders_toolbox/breeder_search_page.mas';
439 # next two functions moved to CXGN::BreedersToolbox::Project
441 # sub get_locations : Private {
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();
462 # push @locations, [ $row->nd_geolocation_id,
467 # $count, # number of experiments TBD
471 # return \@locations;
475 # sub get_all_locations {
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 {
494 # my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
497 # # get breeding programs
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 ];
516 sub get_crosses
: Private
{
520 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
524 my $stock_type_cv = $schema->resultset("Cv::Cv")->find( {name
=>'stock type'});
525 my $cross_cvterm = $schema->resultset("Cv::Cvterm")->find(
527 cv_id
=> $stock_type_cv->cv_id(),
529 my @cross_populations = ();
533 my @cross_population_stocks = $schema->resultset("Stock::Stock")->search(
534 { type_id
=> $cross_cvterm->cvterm_id, is_obsolete
=> 'f'
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
{
548 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
550 my $stockrel = $schema->resultset("Cv::Cvterm")->create_with(
552 cv
=> 'stock relationship',
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
{
579 my $metadata_schema = $c->dbic_schema("CXGN::Metadata::Schema");
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,
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,
623 sub manage_genotyping
: Path
("/breeders/genotyping") Args
(0) {
628 # redirect to login page
629 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
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';