fix t/unit_mech/AJAX/BreedersToolbox/Locations.t test. Refactor two location related...
[sgn.git] / lib / SGN / Controller / BreedersToolbox.pm
blobc2dc0f609ade4384c0655ae92833ac15c52753a0
2 package SGN::Controller::BreedersToolbox;
4 use Moose;
6 use Data::Dumper;
7 use SGN::Controller::AJAX::List;
8 use CXGN::List::Transform;
9 use CXGN::BreedersToolbox::Projects;
10 use CXGN::BreedersToolbox::Accessions;
11 use SGN::Model::Cvterm;
12 use URI::FromHash 'uri';
13 use Spreadsheet::WriteExcel;
14 use Spreadsheet::Read;
15 use File::Slurp qw | read_file |;
16 use File::Temp;
17 use CXGN::Trial::TrialLayout;
18 use Try::Tiny;
19 use File::Basename qw | basename dirname|;
20 use File::Spec::Functions;
21 use CXGN::People::Roles;
22 use CXGN::Trial::TrialLayout;
23 use CXGN::Genotype::Search;
24 use CXGN::Trial;
27 BEGIN { extends 'Catalyst::Controller'; }
29 sub manage_breeding_programs : Path("/breeders/manage_programs") :Args(0) {
30 my $self = shift;
31 my $c = shift;
33 if (!$c->user()) {
35 # redirect to login page
36 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
37 return;
40 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
42 my $projects = CXGN::BreedersToolbox::Projects->new( { schema=> $schema } );
44 my $breeding_programs = $projects->get_breeding_programs();
46 $c->stash->{breeding_programs} = $breeding_programs;
47 $c->stash->{user} = $c->user();
49 $c->stash->{template} = '/breeders_toolbox/breeding_programs.mas';
54 sub manage_trials : Path("/breeders/trials") Args(0) {
55 my $self = shift;
56 my $c = shift;
58 if (!$c->user()) {
60 # redirect to login page
61 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
62 return;
65 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
67 my $projects = CXGN::BreedersToolbox::Projects->new( { schema=> $schema } );
69 my @editable_stock_props = split ',', $c->config->{editable_stock_props};
70 my %editable_stock_props = map { $_=>1 } @editable_stock_props;
72 my @editable_stock_props_definitions = split ',', $c->config->{editable_stock_props_definitions};
73 my %def_hash;
74 foreach (@editable_stock_props_definitions) {
75 my @term_def = split ':', $_;
76 $def_hash{$term_def[0]} = $term_def[1];
79 my $breeding_programs = $projects->get_breeding_programs();
80 my @breeding_programs = @$breeding_programs;
81 my @roles = $c->user->roles();
83 #Add true false field to breeding program array indicating whether program is linked to current user
84 foreach my $role (@roles) {
85 for (my $i=0; $i < scalar @breeding_programs; $i++) {
86 if ($role eq $breeding_programs[$i][1]){
87 $breeding_programs[$i][3] = 1;
88 } else {
89 $breeding_programs[$i][3] = 0;
94 #print STDERR "Breeding programs are ".Dumper(@breeding_programs);
95 my $field_management_factors = $c->config->{management_factor_types};
96 my @management_factor_types = split ',',$field_management_factors;
98 my $design_type_string = $c->config->{design_types};
99 my @design_types = split ',',$design_type_string;
101 $c->stash->{design_types} = \@design_types;
102 $c->stash->{management_factor_types} = \@management_factor_types;
103 $c->stash->{editable_stock_props} = \%editable_stock_props;
104 $c->stash->{editable_stock_props_definitions} = \%def_hash;
105 $c->stash->{preferred_species} = $c->config->{preferred_species};
106 $c->stash->{timestamp} = localtime;
108 my $locations = $projects->get_all_locations_by_breeding_program();
110 #print STDERR "Locations are ".Dumper($locations)."\n";
112 $c->stash->{locations} = $locations;
114 $c->stash->{breeding_programs} = \@breeding_programs;
116 $c->stash->{template} = '/breeders_toolbox/manage_projects.mas';
119 sub manage_accessions : Path("/breeders/accessions") Args(0) {
120 my $self = shift;
121 my $c = shift;
122 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
123 my $list_id = $c->req->param('list_id') || ''; #If a list_id is given in the URL, then the add accessions process will automatically begin with that list.
125 if (!$c->user()) {
126 # redirect to login page
128 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
129 return;
132 my $ac = CXGN::BreedersToolbox::Accessions->new( { schema=>$schema });
134 my $accessions = $ac->get_all_accessions($c);
135 # my $populations = $ac->get_all_populations($c);
137 my @editable_stock_props = split ',', $c->config->{editable_stock_props};
138 my %editable_stock_props = map { $_=>1 } @editable_stock_props;
140 my @editable_stock_props_definitions = split ',', $c->config->{editable_stock_props_definitions};
141 my %def_hash;
142 foreach (@editable_stock_props_definitions) {
143 my @term_def = split ':', $_;
144 $def_hash{$term_def[0]} = $term_def[1];
147 $c->stash->{accessions} = $accessions;
148 $c->stash->{list_id} = $list_id;
149 #$c->stash->{population_groups} = $populations;
150 $c->stash->{preferred_species} = $c->config->{preferred_species};
151 $c->stash->{editable_stock_props} = \%editable_stock_props;
152 $c->stash->{editable_stock_props_definitions} = \%def_hash;
153 $c->stash->{template} = '/breeders_toolbox/manage_accessions.mas';
156 sub manage_roles : Path("/breeders/manage_roles") Args(0) {
157 my $self = shift;
158 my $c = shift;
160 if (!$c->user()) {
161 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
162 return;
165 $c->stash->{is_curator} = $c->user->check_roles("curator");
167 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
168 my $person_roles = CXGN::People::Roles->new({ bcs_schema=>$schema });
169 my $ascii_chars = 1;
170 my $breeding_programs = $person_roles->get_breeding_program_roles($ascii_chars);
172 $c->stash->{roles} = $breeding_programs;
173 $c->stash->{template} = '/breeders_toolbox/manage_roles.mas';
176 sub manage_tissue_samples : Path("/breeders/samples") Args(0) {
177 my $self = shift;
178 my $c = shift;
180 if (!$c->user()) {
181 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
182 return;
184 my $genotyping_facilities = $c->config->{genotyping_facilities};
185 my @facilities = split ',',$genotyping_facilities;
187 my $sampling_facilities = $c->config->{sampling_facilities};
188 my @sampling_facilities = split ',',$sampling_facilities;
190 $c->stash->{facilities} = \@facilities;
191 $c->stash->{sampling_facilities} = \@sampling_facilities;
192 $c->stash->{user_id} = $c->user()->get_object()->get_sp_person_id();
193 $c->stash->{template} = '/breeders_toolbox/manage_samples.mas';
197 sub manage_locations : Path("/breeders/locations") Args(0) {
198 my $self = shift;
199 my $c = shift;
201 if (!$c->user()) {
203 # redirect to login page
205 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
206 return;
209 $c->stash->{user_id} = $c->user()->get_object()->get_sp_person_id();
211 $c->stash->{template} = '/breeders_toolbox/manage_locations.mas';
214 sub manage_nurseries : Path("/breeders/nurseries") Args(0) {
215 my $self = shift;
216 my $c = shift;
218 if (!$c->user()) {
220 # redirect to login page
222 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
223 return;
225 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
226 my $bp = CXGN::BreedersToolbox::Projects->new( { schema=>$schema });
227 my $breeding_programs = $bp->get_breeding_programs();
229 $c->stash->{user_id} = $c->user()->get_object()->get_sp_person_id();
231 $c->stash->{locations} = $bp->get_all_locations($c);
233 #$c->stash->{projects} = $self->get_projects($c);
235 $c->stash->{programs} = $breeding_programs;
237 $c->stash->{roles} = $c->user()->roles();
239 $c->stash->{nurseries} = $self->get_nurseries($c);
241 $c->stash->{template} = '/breeders_toolbox/manage_nurseries.mas';
245 sub manage_crosses : Path("/breeders/crosses") Args(0) {
246 my $self = shift;
247 my $c = shift;
249 if (!$c->user()) {
251 # redirect to login page
253 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
254 return;
256 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
257 my $bp = CXGN::BreedersToolbox::Projects->new( { schema=>$schema });
258 my $breeding_programs = $bp->get_breeding_programs();
260 my $crossingtrial = CXGN::BreedersToolbox::Projects->new( { schema=>$schema });
261 my $crossing_trials = $crossingtrial->get_crossing_trials();
263 $c->stash->{user_id} = $c->user()->get_object()->get_sp_person_id();
266 my @breeding_programs = @$breeding_programs;
267 my @roles = $c->user->roles();
269 foreach my $role (@roles) {
270 for (my $i=0; $i < scalar @breeding_programs; $i++) {
271 if ($role eq $breeding_programs[$i][1]){
272 $breeding_programs[$i][3] = 1;
273 } else {
274 $breeding_programs[$i][3] = 0;
279 my $locations = $crossingtrial->get_all_locations_by_breeding_program();
281 $c->stash->{locations} = $locations;
283 $c->stash->{programs} = \@breeding_programs;
285 #$c->stash->{locations} = $bp->get_all_locations($c);
287 #$c->stash->{programs} = $breeding_programs;
289 $c->stash->{crossing_trials} = $crossing_trials;
291 $c->stash->{roles} = $c->user()->roles();
293 $c->stash->{template} = '/breeders_toolbox/manage_crosses.mas';
297 sub manage_phenotyping :Path("/breeders/phenotyping") Args(0) {
298 my $self =shift;
299 my $c = shift;
301 if (!$c->user()) {
302 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
303 return;
306 my @file_types = ( 'spreadsheet phenotype file', 'direct phenotyping', 'trial_additional_file_upload', 'brapi observations', 'tablet phenotype file' );
307 my $data = $self->get_file_data($c, \@file_types);
309 $c->stash->{phenotype_files} = $data->{files};
310 $c->stash->{deleted_phenotype_files} = $data->{deleted_files};
312 $c->stash->{template} = '/breeders_toolbox/manage_phenotyping.mas';
316 sub manage_nirs :Path("/breeders/nirs") Args(0) {
317 my $self =shift;
318 my $c = shift;
320 if (!$c->user()) {
321 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
322 return;
325 my @file_types = ( 'nirs spreadsheet' );
326 my $all_data = $self->get_file_data($c, \@file_types, 1);
327 my $data = $self->get_file_data($c, \@file_types, 0);
329 my $sampling_facilities = $c->config->{sampling_facilities};
330 my @sampling_facilities = split ',',$sampling_facilities;
332 $c->stash->{sampling_facilities} = \@sampling_facilities;
333 $c->stash->{nirs_files} = $data->{files};
334 $c->stash->{deleted_nirs_files} = $data->{deleted_files};
335 $c->stash->{all_nirs_files} = $all_data->{files};
336 $c->stash->{all_deleted_nirs_files} = $all_data->{deleted_files};
338 $c->stash->{template} = '/breeders_toolbox/manage_nirs.mas';
342 sub manage_upload :Path("/breeders/upload") Args(0) {
343 my $self =shift;
344 my $c = shift;
345 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
347 if (!$c->user()) {
348 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
349 return;
352 my @editable_stock_props = split ',', $c->config->{editable_stock_props};
353 my %editable_stock_props = map { $_=>1 } @editable_stock_props;
355 my @editable_stock_props_definitions = split ',', $c->config->{editable_stock_props_definitions};
356 my %def_hash;
357 foreach (@editable_stock_props_definitions) {
358 my @term_def = split ':', $_;
359 $def_hash{$term_def[0]} = $term_def[1];
362 my $projects = CXGN::BreedersToolbox::Projects->new( { schema=> $schema } );
363 my $breeding_programs = $projects->get_breeding_programs();
365 my $genotyping_facilities = $c->config->{genotyping_facilities};
366 my @facilities = split ',',$genotyping_facilities;
368 my $field_management_factors = $c->config->{management_factor_types};
369 my @management_factor_types = split ',',$field_management_factors;
371 my $design_type_string = $c->config->{design_types};
372 my @design_types = split ',',$design_type_string;
374 $c->stash->{editable_stock_props} = \%editable_stock_props;
375 $c->stash->{editable_stock_props_definitions} = \%def_hash;
376 $c->stash->{design_types} = \@design_types;
377 $c->stash->{management_factor_types} = \@management_factor_types;
378 $c->stash->{facilities} = \@facilities;
379 $c->stash->{geojson_locations} = $projects->get_all_locations_by_breeding_program();
380 $c->stash->{locations} = $projects->get_all_locations();
381 $c->stash->{breeding_programs} = $breeding_programs;
382 $c->stash->{timestamp} = localtime;
383 $c->stash->{preferred_species} = $c->config->{preferred_species};
384 $c->stash->{template} = '/breeders_toolbox/manage_upload.mas';
387 sub manage_file_share_dump :Path("/breeders/file_share_dump") Args(0) {
388 my $self =shift;
389 my $c = shift;
390 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
392 if (!$c->user()) {
393 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
394 return;
397 $c->stash->{template} = '/breeders_toolbox/file_share/manage_file_share_dump.mas';
400 sub manage_plot_phenotyping :Path("/breeders/plot_phenotyping") Args(0) {
401 my $self =shift;
402 my $c = shift;
403 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
404 my $stock_id = $c->req->param('stock_id');
406 if (!$c->user()) {
407 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
408 return;
410 my $stock = $schema->resultset("Stock::Stock")->find( { stock_id=>$stock_id })->uniquename();
412 $c->stash->{plot_name} = $stock;
413 $c->stash->{stock_id} = $stock_id;
414 $c->stash->{template} = '/breeders_toolbox/manage_plot_phenotyping.mas';
418 sub manage_trial_phenotyping :Path("/breeders/trial_phenotyping") Args(0) {
419 my $self =shift;
420 my $c = shift;
421 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
422 my $trial_id = $c->req->param('trial_id');
424 if (!$c->user()) {
425 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
426 return;
428 my $project_name = $schema->resultset("Project::Project")->find( { project_id=>$trial_id })->name();
430 my $trial = CXGN::Trial->new({ bcs_schema => $schema, trial_id => $trial_id });
432 $c->stash->{trial_stock_type} = $trial->get_trial_stock_type();
433 $c->stash->{trial_name} = $project_name;
434 $c->stash->{trial_id} = $trial_id;
435 $c->stash->{template} = '/breeders_toolbox/manage_trial_phenotyping.mas';
438 sub manage_odk_data_collection :Path("/breeders/odk") Args(0) {
439 my $self =shift;
440 my $c = shift;
441 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
443 if (!$c->user()) {
444 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
445 return;
447 $c->stash->{odk_crossing_data_service_name} = $c->config->{odk_crossing_data_service_name};
448 $c->stash->{odk_crossing_data_service_url} = $c->config->{odk_crossing_data_service_url};
449 $c->stash->{odk_crossing_data_test_form_name} = $c->config->{odk_crossing_data_test_form_name};
450 $c->stash->{odk_phenotyping_data_service_name} = $c->config->{odk_phenotyping_data_service_name};
451 $c->stash->{odk_phenotyping_data_service_url} = $c->config->{odk_phenotyping_data_service_url};
452 $c->stash->{template} = '/breeders_toolbox/manage_odk_data_collection.mas';
456 sub manage_identifier_generation :Path("/breeders/identifier_generation") Args(0) {
457 my $self =shift;
458 my $c = shift;
459 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
461 if (!$c->user()) {
462 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
463 return;
465 $c->stash->{template} = '/breeders_toolbox/identifier_generation/manage_identifier_generation.mas';
469 sub manage_phenotyping_download : Path("/breeders/phenotyping/download") Args(1) {
470 my $self =shift;
471 my $c = shift;
472 my $file_id = shift;
474 my $metadata_schema = $c->dbic_schema('CXGN::Metadata::Schema');
475 my $file_row = $metadata_schema->resultset("MdFiles")->find({file_id => $file_id});
476 my $file_destination = catfile($file_row->dirname, $file_row->basename);
477 #print STDERR "\n\n\nfile name:".$file_row->basename."\n";
478 my $contents = read_file($file_destination);
479 my $file_name = $file_row->basename;
480 $c->res->content_type('Application/trt');
481 $c->res->header('Content-Disposition', qq[attachment; filename="$file_name"]);
482 $c->res->body($contents);
485 sub manage_phenotyping_view : Path("/breeders/phenotyping/view") Args(1) {
486 my $self =shift;
487 my $c = shift;
488 my $file_id = shift;
490 my $metadata_schema = $c->dbic_schema('CXGN::Metadata::Schema');
491 my $file_row = $metadata_schema->resultset("MdFiles")->find({file_id => $file_id});
492 my $file_destination = catfile($file_row->dirname, $file_row->basename);
493 #print STDERR "\n\n\nfile name:".$file_row->basename."\n";
494 my @contents = ReadData ($file_destination);
495 #print STDERR Dumper \@contents;
496 my $file_name = $file_row->basename;
497 $c->stash->{file_content} = \@contents;
498 $c->stash->{filename} = $file_name;
499 $c->stash->{template} = '/breeders_toolbox/view_file.mas';
502 sub make_cross_form :Path("/stock/cross/new") :Args(0) {
503 my ($self, $c) = @_;
504 $c->stash->{template} = '/breeders_toolbox/new_cross.mas';
505 if ($c->user()) {
506 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
507 # get projects
508 my @rows = $schema->resultset('Project::Project')->all();
509 my @projects = ();
510 foreach my $row (@rows) {
511 push @projects, [ $row->project_id, $row->name, $row->description ];
513 $c->stash->{project_list} = \@projects;
514 @rows = $schema->resultset('NaturalDiversity::NdGeolocation')->all();
515 my @locations = ();
516 foreach my $row (@rows) {
517 push @locations, [ $row->nd_geolocation_id,$row->description ];
519 $c->stash->{locations} = \@locations;
522 else {
523 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
524 return;
529 sub make_cross :Path("/stock/cross/generate") :Args(0) {
530 my ($self, $c) = @_;
531 $c->stash->{template} = '/breeders_toolbox/progeny_from_crosses.mas';
532 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
533 my $cross_name = $c->req->param('cross_name');
534 $c->stash->{cross_name} = $cross_name;
535 my $trial_id = $c->req->param('trial_id');
536 $c->stash->{trial_id} = $trial_id;
537 #my $location = $c->req->param('location_id');
538 my $maternal = $c->req->param('maternal');
539 my $paternal = $c->req->param('paternal');
540 my $prefix = $c->req->param('prefix');
541 my $suffix = $c->req->param('suffix');
542 my $progeny_number = $c->req->param('progeny_number');
543 my $visible_to_role = $c->req->param('visible_to_role');
545 if (! $c->user()) { # redirect
546 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
547 return;
551 #check that progeny number is an integer less than maximum allowed
552 my $maximum_progeny_number = 1000;
553 if ((! $progeny_number =~ m/^\d+$/) or ($progeny_number > $maximum_progeny_number)){
554 #redirect to error page?
555 return;
558 #check that parent names are not blank
559 if ($maternal eq "" or $paternal eq "") {
560 return;
563 #check that parents exist in the database
564 if (! $schema->resultset("Stock::Stock")->find({name=>$maternal,})){
565 return;
567 if (! $schema->resultset("Stock::Stock")->find({name=>$paternal,})){
568 return;
571 #check that cross name does not already exist
572 if ($schema->resultset("Stock::Stock")->find({name=>$cross_name})){
573 return;
576 #check that progeny do not already exist
577 if ($schema->resultset("Stock::Stock")->find({name=>$prefix.$cross_name.$suffix."-1",})){
578 return;
581 my $organism = $schema->resultset("Organism::Organism")->find_or_create(
583 genus => 'Manihot',
584 species => 'Manihot esculenta',
585 } );
586 my $organism_id = $organism->organism_id();
588 my $accession_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'accession', 'stock_type');
590 my $population_cvterm = $schema->resultset("Cv::Cvterm")->find(
591 { name => 'population',
595 my $female_parent_stock = $schema->resultset("Stock::Stock")->find(
596 { name => $maternal,
597 } );
599 my $male_parent_stock = $schema->resultset("Stock::Stock")->find(
600 { name => $paternal,
601 } );
603 my $population_stock = $schema->resultset("Stock::Stock")->find_or_create(
604 { organism_id => $organism_id,
605 name => $cross_name,
606 uniquename => $cross_name,
607 type_id => $population_cvterm->cvterm_id,
608 } );
609 my $female_parent = SGN::Model::Cvterm->get_cvterm_row($schema, 'female_parent', 'stock_relationship');
611 my $male_parent = SGN::Model::Cvterm->get_cvterm_row($schema, 'male_parent', 'stock_relationship');
614 my $population_members = SGN::Model::Cvterm->get_cvterm_row($schema, 'cross_relationship','stock_relationship');
617 my $visible_to_role_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'visible_to_role', 'local');
619 my $increment = 1;
620 while ($increment < $progeny_number + 1) {
621 $increment = sprintf "%03d", $increment;
622 my $stock_name = $prefix.$cross_name."_".$increment.$suffix;
623 my $accession_stock = $schema->resultset("Stock::Stock")->create(
624 { organism_id => $organism_id,
625 name => $stock_name,
626 uniquename => $stock_name,
627 type_id => $accession_cvterm->cvterm_id,
628 } );
629 $accession_stock->find_or_create_related('stock_relationship_objects', {
630 type_id => $female_parent->cvterm_id(),
631 object_id => $accession_stock->stock_id(),
632 subject_id => $female_parent_stock->stock_id(),
633 } );
634 $accession_stock->find_or_create_related('stock_relationship_objects', {
635 type_id => $male_parent->cvterm_id(),
636 object_id => $accession_stock->stock_id(),
637 subject_id => $male_parent_stock->stock_id(),
638 } );
639 $accession_stock->find_or_create_related('stock_relationship_objects', {
640 type_id => $population_members->cvterm_id(),
641 object_id => $accession_stock->stock_id(),
642 subject_id => $population_stock->stock_id(),
643 } );
644 if ($visible_to_role ne "") {
645 my $accession_stock_prop = $schema->resultset("Stock::Stockprop")->find_or_create(
646 { type_id =>$visible_to_role_cvterm->cvterm_id(),
647 value => $visible_to_role,
648 stock_id => $accession_stock->stock_id()
651 $increment++;
654 if ($@) {
658 sub selection_index : Path("/selection/index") :Args(0) {
659 my $self = shift;
660 my $c = shift;
662 if (!$c->user()) {
664 # redirect to login page
665 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
666 return;
669 # my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
671 # my $projects = CXGN::BreedersToolbox::Projects->new( { schema=> $schema } );
673 # my $breeding_programs = $projects->get_breeding_programs();
675 # $c->stash->{breeding_programs} = $breeding_programs;
676 $c->stash->{user} = $c->user();
678 $c->stash->{template} = '/breeders_toolbox/selection_index.mas';
684 sub breeder_home :Path("/breeders/home") Args(0) {
685 my ($self , $c) = @_;
688 if (!$c->user()) {
690 # redirect to login page
691 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
692 return;
695 # my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
696 # my $bp = CXGN::BreedersToolbox::Projects->new( { schema=>$schema });
697 # my $breeding_programs = $bp->get_breeding_programs();
699 # $c->stash->{programs} = $breeding_programs;
700 # $c->stash->{breeding_programs} = $breeding_programs;
702 # # my $locations_by_breeding_program;
703 # # foreach my $b (@$breeding_programs) {
704 # # $locations_by_breeding_program->{$b->[1]} = $bp->get_locations_by_breeding_program($b->[0]);
705 # # }
706 # # $locations_by_breeding_program->{'Other'} = $bp->get_locations_by_breeding_program();
708 # $c->stash->{locations_by_breeding_program} = ""; #$locations_by_breeding_program;
710 # # get roles
712 # my @roles = $c->user->roles();
713 # $c->stash->{roles}=\@roles;
715 # $c->stash->{stockrelationships} = $self->get_stock_relationships($c);
717 # my $locations = $bp->get_locations($c);
719 # $c->stash->{locations} = $locations;
720 # # get uploaded phenotype files
723 # my $data = $self->get_file_data($c, \@file_types);
725 # $c->stash->{phenotype_files} = $data->{file_info};
726 # $c->stash->{deleted_phenotype_files} = $data->{deleted_file_info};
729 $c->stash->{template} = '/breeders_toolbox/home.mas';
732 sub breeder_search : Path('/breeders/search/') :Args(0) {
733 my ($self, $c) = @_;
735 if (!$c->user()) {
736 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
737 return;
740 $c->stash->{dataset_id} = $c->req->param('dataset_id');
741 $c->stash->{template} = '/breeders_toolbox/breeder_search_page.mas';
744 sub get_file_data : Private {
745 my $self = shift;
746 my $c = shift;
747 my $file_types = shift;
748 my $get_files_for_all_users = shift;
749 my $file_type_string = "'".join("','", @$file_types)."'";
751 my $metadata_schema = $c->dbic_schema("CXGN::Metadata::Schema");
753 my $file_info = [];
754 my $deleted_file_info = [];
756 my $where_string = '';
757 if (!$get_files_for_all_users) {
758 $where_string = ' AND md.create_person_id = '.$c->user()->get_object->get_sp_person_id();
761 my $q = "SELECT mdf.file_id, mdf.basename, mdf.dirname, mdf.filetype, mdf.md5checksum, md.create_date, md.obsolete
762 FROM metadata.md_files AS mdf
763 JOIN metadata.md_metadata AS md ON (mdf.metadata_id = md.metadata_id)
764 WHERE mdf.filetype IN ($file_type_string) $where_string;";
765 print STDERR $q."\n";
766 my $h = $c->dbc->dbh->prepare($q);
767 $h->execute();
768 while (my ($file_id, $basename, $dirname, $filetype, $md5, $create_date, $obsolete) = $h->fetchrow_array()) {
769 if (!$obsolete) {
770 push @$file_info, {
771 file_id => $file_id,
772 basename => $basename,
773 dirname => $dirname,
774 file_type => $filetype,
775 md5checksum => $md5,
776 create_date => $create_date
779 else {
780 push @$deleted_file_info, {
781 file_id => $file_id,
782 basename => $basename,
783 dirname => $dirname,
784 file_type => $filetype,
785 md5checksum => $md5,
786 create_date => $create_date
791 my $data = {
792 files => $file_info,
793 deleted_files => $deleted_file_info
795 return $data;
799 sub manage_genotyping : Path("/breeders/genotyping") Args(0) {
800 my $self = shift;
801 my $c = shift;
803 if (!$c->user()) {
804 # redirect to login page
805 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
806 return;
809 my $schema = $c->dbic_schema('Bio::Chado::Schema');
811 my $projects = CXGN::BreedersToolbox::Projects->new( { schema=> $schema } );
813 my $breeding_programs = $projects->get_breeding_programs();
815 my %genotyping_trials_by_breeding_project = ();
817 foreach my $bp (@$breeding_programs) {
818 $genotyping_trials_by_breeding_project{$bp->[1]}= $projects->get_genotyping_trials_by_breeding_program($bp->[0]);
821 $genotyping_trials_by_breeding_project{'Other'} = $projects->get_genotyping_trials_by_breeding_program();
823 my $genotyping_facilities = $c->config->{genotyping_facilities};
824 my @facilities = split ',',$genotyping_facilities;
826 $c->stash->{locations} = $projects->get_all_locations($c);
828 $c->stash->{genotyping_trials_by_breeding_project} = \%genotyping_trials_by_breeding_project;
830 $c->stash->{breeding_programs} = $breeding_programs;
832 $c->stash->{facilities} = \@facilities;
834 $c->stash->{template} = '/breeders_toolbox/manage_genotyping.mas';
837 sub manage_genotype_qc : Path("/breeders/genotype_qc") :Args(0) {
838 my $self = shift;
839 my $c = shift;
841 if (!$c->user()) {
842 # redirect to login page
843 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
844 return;
847 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
849 $c->stash->{template} = '/breeders_toolbox/manage_genotype_qc.mas';
853 sub manage_markers : Path("/breeders/markers") Args(0) {
854 my $self = shift;
855 my $c = shift;
857 $c->stash->{template} = '/breeders_toolbox/markers/manage_markers.mas';
860 sub manage_drone_imagery : Path("/breeders/drone_imagery") Args(0) {
861 my $self = shift;
862 my $c = shift;
864 if (!$c->user()) {
865 # redirect to login page
866 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
867 return;
870 my $schema = $c->dbic_schema('Bio::Chado::Schema');
871 $c->stash->{template} = '/breeders_toolbox/manage_drone_imagery.mas';