default plant material
[sgn.git] / lib / SGN / Controller / BreedersToolbox / Trial.pm
blob205b2654af83a633a78903d2b85a6cc44e49e482
2 package SGN::Controller::BreedersToolbox::Trial;
4 use Moose;
6 use File::Basename;
7 use File::Slurp qw | read_file |;
8 use URI::FromHash 'uri';
10 use CXGN::Trial::TrialLayout;
11 use CXGN::BreedersToolbox::Projects;
12 use SGN::View::Trial qw/design_layout_view design_info_view trial_detail_design_view/;
13 use CXGN::Trial::Download;
14 use CXGN::List::Transform;
15 use CXGN::List::Validate;
16 use CXGN::List;
17 use JSON::XS;
18 use Data::Dumper;
19 use CXGN::TrialStatus;
20 use SGN::Model::Cvterm;
21 use CXGN::Genotype::GenotypingProject;
22 use CXGN::Stock::TissueSample::Search;
23 use CXGN::Genotype::Protocol;
24 use CXGN::TrackingActivity::ActivityProject;
25 use CXGN::Transformation::Transformation;
27 BEGIN { extends 'Catalyst::Controller'; }
30 sub trial_init : Chained('/') PathPart('breeders/trial') CaptureArgs(1) {
31 my $self = shift;
32 my $c = shift;
33 my $trial_id = shift;
35 $c->stash->{trial_id} = $trial_id;
36 # print STDERR "TRIAL ID = $trial_id\n";
38 my $schema = $c->dbic_schema("Bio::Chado::Schema");
39 $c->stash->{schema} = $schema;
40 my $trial;
41 eval {
42 $trial = CXGN::Trial->new( { bcs_schema => $schema, trial_id => $trial_id });
44 if ($@) {
45 $c->stash->{template} = 'system_message.txt';
46 $c->stash->{message} = "The requested trial ($trial_id) does not exist";
47 return;
49 $c->stash->{trial} = $trial;
52 sub old_trial_url : Path('/breeders_toolbox/trial') Args(1) {
53 my $self = shift;
54 my $c = shift;
55 my @args = @_;
56 my $format = $c->req->param("format");
57 $c->res->redirect('/breeders/trial/'.$args[0].'?format='.$format);
60 sub trial_info : Chained('trial_init') PathPart('') Args(0) {
61 #print STDERR "Check 1: ".localtime()."\n";
62 #print STDERR "TRIAL INIT...\n\n";
63 my $self = shift;
64 my $c = shift;
65 my $format = $c->req->param("format");
66 #print STDERR $format;
67 my $user = $c->user();
68 if (!$user) {
69 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
70 return;
73 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
74 my $trial = $c->stash->{trial};
75 my $program_object = CXGN::BreedersToolbox::Projects->new( { schema => $schema });
77 if (!$program_object->trial_exists($c->stash->{trial_id})) {
78 $c->stash->{message} = "The requested trial does not exist or has been deleted.";
79 $c->stash->{template} = 'generic_message.mas';
80 return;
83 $c->stash->{trial_name} = $trial->get_name();
84 $c->stash->{trial_owner} = $trial->get_owner_link();
86 my $trial_type_data = $trial->get_project_type();
87 my $trial_type_name = $trial_type_data ? $trial_type_data->[1] : '';
88 $c->stash->{trial_type} = $trial_type_name;
89 $c->stash->{trial_type_id} = $trial_type_data->[0];
91 $c->stash->{planting_date} = $trial->get_planting_date();
92 $c->stash->{show_transplanting_date} = $c->config->{show_transplanting_date};
93 $c->stash->{transplanting_date} = $trial->get_transplanting_date();
94 warn "Transplanting Date: ".$c->stash->{transplanting_date};
95 $c->stash->{harvest_date} = $trial->get_harvest_date();
97 $c->stash->{plot_width} = $trial->get_plot_width();
98 $c->stash->{plot_length} = $trial->get_plot_length();
99 $c->stash->{field_size} = $trial->get_field_size();
101 $c->stash->{field_trial_is_planned_to_be_genotyped} = $trial->get_field_trial_is_planned_to_be_genotyped();
102 $c->stash->{field_trial_is_planned_to_cross} = $trial->get_field_trial_is_planned_to_cross();
104 $c->stash->{trial_description} = $trial->get_description();
106 my $activities = $c->config->{'trial_activities'};
107 my @activity_list = split ',', $activities;
108 my $trial_status = CXGN::TrialStatus->new({ bcs_schema => $schema, parent_id => $c->stash->{trial_id}, activity_list => \@activity_list });
109 $c->stash->{latest_trial_activity} = $trial_status->get_latest_activity();
111 my $location_data = $trial->get_location();
112 $c->stash->{location_id} = $location_data->[0];
113 $c->stash->{location_name} = $location_data->[1];
114 $c->stash->{country_name} = $trial->get_location_country_name();
116 my $breeding_program_data = $program_object->get_breeding_programs_by_trial($c->stash->{trial_id});
117 $c->stash->{breeding_program_id} = $breeding_program_data->[0]->[0];
118 $c->stash->{breeding_program_name} = $breeding_program_data->[0]->[1];
120 $c->stash->{user_can_modify} = ($user->check_roles("submitter") && $user->check_roles($c->stash->{breeding_program_name})) || $user->check_roles("curator") ;
124 $c->stash->{year} = $trial->get_year();
126 $c->stash->{trial_id} = $c->stash->{trial_id};
128 $c->stash->{has_col_and_row_numbers} = $trial->has_col_and_row_numbers();
129 $c->stash->{has_plant_entries} = $trial->has_plant_entries();
130 $c->stash->{has_subplot_entries} = $trial->has_subplot_entries();
131 $c->stash->{has_tissue_sample_entries} = $trial->has_tissue_sample_entries();
132 $c->stash->{phenotypes_fully_uploaded} = $trial->get_phenotypes_fully_uploaded();
134 $c->stash->{hidap_enabled} = $c->config->{hidap_enabled};
135 $c->stash->{has_expression_atlas} = $c->config->{has_expression_atlas};
136 $c->stash->{expression_atlas_url} = $c->config->{expression_atlas_url};
137 $c->stash->{main_production_site_url} = $c->config->{main_production_site_url};
138 $c->stash->{site_project_name} = $c->config->{project_name};
139 $c->stash->{sgn_session_id} = $c->req->cookie('sgn_session_id');
140 $c->stash->{user_name} = $c->user->get_object->get_username;
142 if ($trial->get_folder) {
143 $c->stash->{folder_id} = $trial->get_folder()->project_id();
144 $c->stash->{folder_name} = $trial->get_folder()->name();
147 my $design_type = $trial->get_design_type();
148 $c->stash->{design_name} = $design_type;
149 $c->stash->{genotyping_facility} = $trial->get_genotyping_facility;
151 my $activity_project = CXGN::TrackingActivity::ActivityProject->new( { bcs_schema => $schema, trial_id => $c->stash->{trial_id} });
153 # print STDERR "TRIAL TYPE DATA = $trial_type_data->[1]\n\n";
155 if ($design_type eq "genotyping_plate") {
156 my $plate_id = $c->stash->{trial_id};
157 $c->stash->{plate_id} = $plate_id;
158 $c->stash->{raw_data_link} = $trial->get_raw_data_link;
159 $c->stash->{genotyping_facility} = $trial->get_genotyping_facility;
160 $c->stash->{genotyping_facility_submitted} = $trial->get_genotyping_facility_submitted;
161 $c->stash->{genotyping_facility_status} = $trial->get_genotyping_facility_status;
162 $c->stash->{genotyping_vendor_order_id} = $trial->get_genotyping_vendor_order_id;
163 $c->stash->{genotyping_vendor_submission_id} = $trial->get_genotyping_vendor_submission_id;
164 $c->stash->{genotyping_plate_sample_type} = $trial->get_genotyping_plate_sample_type;
166 my $sample_data_search = CXGN::Stock::TissueSample::Search->new({
167 bcs_schema=>$schema,
168 plate_db_id_list => [$plate_id],
171 my $data = $sample_data_search->get_sample_data();
172 $c->stash->{number_of_samples} = $data->{number_of_samples};
173 $c->stash->{number_of_samples_with_data} = $data->{number_of_samples_with_data};
175 my $genotyping_project_relationship_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'genotyping_project_and_plate_relationship', 'project_relationship');
176 my $genotyping_project_plate_relationship = $schema->resultset("Project::ProjectRelationship")->find ({
177 subject_project_id => $plate_id,
178 type_id => $genotyping_project_relationship_cvterm->cvterm_id()
180 if ($genotyping_project_plate_relationship) {
181 my $genotyping_project_id = $genotyping_project_plate_relationship->object_project_id();
182 my $genotyping_project = $schema->resultset("Project::Project")->find ({
183 project_id => $genotyping_project_id
185 my $genotyping_project_name = $genotyping_project->name();
186 my $genotyping_project_link = '<a href="/breeders/trial/'.$genotyping_project_id.'">'.$genotyping_project_name.'</a>';
187 $c->stash->{genotyping_project_link} = $genotyping_project_link;
190 if ($trial->get_genotyping_plate_format){
191 $c->stash->{genotyping_plate_format} = $trial->get_genotyping_plate_format;
193 if ($format eq "as_table") {
194 $c->stash->{template} = '/breeders_toolbox/genotyping_trials/format/as_table.mas';
196 else {
197 $c->stash->{template} = '/breeders_toolbox/genotyping_trials/detail.mas';
200 elsif ($design_type eq "sampling_trial"){
201 $c->stash->{template} = '/breeders_toolbox/sampling_trials/detail.mas';
203 elsif ($design_type eq "treatment"){
204 $c->stash->{management_factor_type} = $trial->get_management_factor_type;
205 $c->stash->{management_factor_date} = $trial->get_management_factor_date;
206 $c->stash->{template} = '/breeders_toolbox/management_factor.mas';
208 elsif (($design_type eq "genotype_data_project") || ($design_type eq "pcr_genotype_data_project")){
209 my $project_id = $c->stash->{trial_id};
210 if ($design_type eq "pcr_genotype_data_project") {
211 $c->stash->{genotype_data_type} = 'SSR'
212 } else {
213 $c->stash->{genotype_data_type} = 'SNP'
216 my $project_info = CXGN::Genotype::GenotypingProject->new({
217 bcs_schema => $schema,
218 project_id => $project_id
220 my ($data, $tc) = $project_info->get_plate_info();
221 my $has_plate;
222 if (!$data) {
223 $has_plate = 'none';
225 $c->stash->{has_plate} = $has_plate;
227 my $associated_protocol = $project_info->get_associated_protocol();
229 if ( defined $associated_protocol && scalar(@$associated_protocol)>0) {
230 my $protocol_id = $associated_protocol->[0]->[0];
231 my $protocol_info = CXGN::Genotype::Protocol->new({
232 bcs_schema => $schema,
233 nd_protocol_id => $protocol_id
236 my $marker_names = $protocol_info->{marker_names};
237 my $assay_type = $protocol_info->{assay_type};
238 $c->stash->{marker_names} = $marker_names;
239 $c->stash->{assay_type} = $assay_type;
241 my $marker_info_keys = $protocol_info->marker_info_keys;
242 my @marker_info_headers = ();
243 if (defined $marker_info_keys) {
244 foreach my $info_key (@$marker_info_keys) {
245 if ($info_key eq 'name') {
246 push @marker_info_headers, 'Marker Name';
247 } elsif (($info_key eq 'intertek_name') || ($info_key eq 'facility_name')) {
248 push @marker_info_headers, 'Facility Marker Name';
249 } elsif ($info_key eq 'chrom') {
250 push @marker_info_headers, 'Chromosome';
251 } elsif ($info_key eq 'pos') {
252 push @marker_info_headers, 'Position';
253 } elsif ($info_key eq 'alt') {
254 if ($assay_type eq 'KASP') {
255 push @marker_info_headers, 'Y-allele';
256 } else {
257 push @marker_info_headers, 'Alternate';
259 } elsif ($info_key eq 'ref') {
260 if ($assay_type eq 'KASP') {
261 push @marker_info_headers, 'X-allele';
262 } else {
263 push @marker_info_headers, 'Reference';
265 } elsif ($info_key eq 'qual') {
266 push @marker_info_headers, 'Quality';
267 } elsif ($info_key eq 'filter') {
268 push @marker_info_headers, 'Filter';
269 } elsif ($info_key eq 'info') {
270 push @marker_info_headers, 'Info';
271 } elsif ($info_key eq 'format') {
272 push @marker_info_headers, 'Format';
273 } elsif ($info_key eq 'sequence') {
274 push @marker_info_headers, 'Sequence';
277 } else {
278 @marker_info_headers = ('Marker Name','Chromosome','Position','Alternate','Reference','Quality','Filter','Info','Format');
280 $c->stash->{marker_info_headers} = \@marker_info_headers;
283 $c->stash->{template} = '/breeders_toolbox/genotype_data_project.mas';
285 elsif ($design_type eq "drone_run"){
286 $c->stash->{drone_run_date} = $trial->get_drone_run_date;
287 $c->stash->{template} = '/breeders_toolbox/drone_run_project.mas';
289 elsif ($trial_type_name eq "crossing_trial"){
290 print STDERR "It's a crossing trial!\n\n";
291 my $program_name = $breeding_program_data->[0]->[1];
292 my $locations = $program_object->get_all_locations_by_breeding_program();
293 my @locations_by_program;
294 foreach my $location_hashref (@$locations) {
295 my $properties = $location_hashref->{'properties'};
296 my $program = $properties->{'Program'};
297 my $name = $properties->{'Name'};
298 if ($program eq $program_name) {
299 push @locations_by_program, $name;
302 my $locations_by_program_json = encode_json(\@locations_by_program);
303 $c->stash->{locations_by_program_json} = $locations_by_program_json;
304 $c->stash->{template} = '/breeders_toolbox/cross/crossing_trial.mas';
305 } elsif ($trial_type_name eq 'activity_record') {
306 my $project_id = $c->stash->{trial_id};
307 my $project_vendor_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'project_vendor', 'project_property')->cvterm_id();
308 my $activity_type_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'activity_type', 'project_property')->cvterm_id();
310 my $activity_type = $schema->resultset("Project::Projectprop")->find ({ project_id => $project_id, type_id => $activity_type_cvterm_id })->value();
311 $c->stash->{activity_type} = $activity_type;
313 my $input_field_headers;
314 if ($activity_type eq 'tissue_culture') {
315 $input_field_headers = $c->config->{tracking_tissue_culture_info_header};
316 } elsif ($activity_type eq 'transformation') {
317 $input_field_headers = $c->config->{tracking_transformation_info_header};
319 my @field_headers = split ',',$input_field_headers;
320 $c->stash->{field_headers} = \@field_headers;
322 my $project_vendor_rs = $schema->resultset("Project::Projectprop")->find ({
323 project_id => $project_id,
324 type_id => $project_vendor_cvterm_id
326 my $vendor_id;
327 if ($project_vendor_rs) {
328 $vendor_id = $project_vendor_rs->value();
331 $c->stash->{vendor_id} = $vendor_id;
333 my $progress_of_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema,'progress_of', 'project_relationship')->cvterm_id();
334 my $project_rel_row = $schema->resultset('Project::ProjectRelationship')->find({subject_project_id => $project_id, type_id => $progress_of_cvterm_id });
335 if ($project_rel_row) {
336 my $parent_project_id = $project_rel_row->object_project_id;
337 $c->stash->{parent_project_id} = $parent_project_id;
340 $c->stash->{template} = '/tracking_activities/activity_project.mas';
342 elsif ($trial_type_name eq "transformation_project"){
343 my $transformation_project_id = $c->stash->{trial_id};
344 my $program_name = $breeding_program_data->[0]->[1];
345 my $locations = $program_object->get_all_locations_by_breeding_program();
346 my @locations_by_program;
347 foreach my $location_hashref (@$locations) {
348 my $properties = $location_hashref->{'properties'};
349 my $program = $properties->{'Program'};
350 my $name = $properties->{'Name'};
351 if ($program eq $program_name) {
352 push @locations_by_program, $name;
355 my $locations_by_program_json = encode_json(\@locations_by_program);
356 $c->stash->{locations_by_program_json} = $locations_by_program_json;
358 my $progress_of_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema,'progress_of', 'project_relationship')->cvterm_id();
359 my $project_rel_row = $schema->resultset('Project::ProjectRelationship')->find({object_project_id => $transformation_project_id, type_id => $progress_of_cvterm_id });
360 if ($project_rel_row) {
361 my $tracking_project_id = $project_rel_row->subject_project_id;
362 $c->stash->{tracking_project_id} = $tracking_project_id;
365 my $name_metadata_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'autogenerated_name_metadata', 'project_property')->cvterm_id();
366 my $name_metadata_rs = $schema->resultset("Project::Projectprop")->find ({
367 project_id => $breeding_program_data->[0]->[0],
368 type_id => $name_metadata_cvterm_id
370 if($name_metadata_rs) {
371 my $name_metadata = $name_metadata_rs->value;
372 my $name_metadata_hash = decode_json $name_metadata;
373 my @prefixes = keys %{$name_metadata_hash};
374 $c->stash->{autogenerated_name_prefixes} = \@prefixes;
377 my $transformation_project = CXGN::Transformation::Transformation->new({schema=>$schema, dbh => $c->dbc->dbh, project_id=>$transformation_project_id});
378 my $prefix = $transformation_project->get_autogenerated_name_prefix();
379 my $default_plant_material = $transformation_project->get_default_plant_material();
380 my $default_plant_material_id = $default_plant_material->[0];
381 my $default_plant_material_name = $default_plant_material->[1];
383 $c->stash->{prefix} = $prefix;
384 $c->stash->{default_plant_material_id} = $default_plant_material_id;
385 $c->stash->{default_plant_material_name} = $default_plant_material_name;
386 $c->stash->{template} = '/transformation/transformation_project.mas';
388 else {
389 my $field_management_factors = $c->config->{management_factor_types};
390 my @management_factor_types = split ',',$field_management_factors;
391 $c->stash->{management_factor_types} = \@management_factor_types;
392 $c->stash->{trial_stock_type} = $trial->get_trial_stock_type();
393 $c->stash->{trial_stock_count} = $trial->get_trial_stock_count();
394 $c->stash->{template} = '/breeders_toolbox/trial.mas';
397 print STDERR "End Load Trial Detail Page: ".localtime()."\n";
402 =head2 view_by_name
404 Public Path: /breeders/trial/view_by_name/$name
405 Path Params:
406 name = trial unique name
408 Search for the trial that matches the provided trial name.
409 If 1 match is found, display the trial detail page. Display an
410 error message if no matches are found.
412 =cut
414 sub view_trial_by_name :Path('/breeders/trial/view_by_name') CaptureArgs(1) {
415 my ($self, $c, $trial_query) = @_;
416 $self->search_trial($c, $trial_query);
420 sub trait_info :Path('/breeders/trial') Args(3) {
421 my ($self, $c, $trial_id, $trait_txt, $trait_id) = @_;
423 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
425 my $trial_name = $schema->resultset("Project::Project")
426 ->search( {'me.project_id' => $trial_id})
427 ->single
428 ->name;
430 $c->stash->{trial_name} = $trial_name;
432 my $trait_name = $schema->resultset("Cv::Cvterm")
433 ->search({'me.cvterm_id' => $trait_id})
434 ->single
435 ->name;
437 $c->stash->{trial_id} = $trial_id;
438 $c->stash->{trial_name} = $trial_name;
440 $c->stash->{trait_id} = $trait_id;
441 $c->stash->{trait_name} = $trait_name;
443 $c->stash->{template} = '/breeders_toolbox/trial_trait.mas';
446 ##DEPRECATED by /breeders/trials
447 sub trial_tree : Path('/breeders/trialtree') Args(0) {
448 my $self = shift;
449 my $c = shift;
452 $c->stash->{template} = '/breeders_toolbox/trialtree.mas';
456 #For downloading trial layout in CSV and Excel, for downloading trial phenotypes in CSV and Excel, and for downloading trial phenotyping spreadsheets in Excel.
457 #For phenotype download, better to use SGN::Controller::BreedersToolbox::Download->download_phenotypes_action and provide a single trial_id in the trial_list argument, as that is how the phenotype download works from the wizard page, the trial tree page, and the trial detail page for phenotype download.
458 sub trial_download : Chained('trial_init') PathPart('download') Args(1) {
459 my $self = shift;
460 my $c = shift;
461 my $what = shift;
462 #print STDERR "trial_download: WHAT =".Dumper($what)."\n";
463 #print STDERR Dumper $c->req->params();
464 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
465 my $user = $c->user();
466 if (!$user) {
467 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
468 return;
471 my $format = $c->req->param("format") || "xls";
472 my $data_level = $c->req->param("dataLevel") || "plot";
473 my $timestamp_option = $c->req->param("timestamp") || 0;
474 my $trait_list = $c->req->param("trait_list");
475 my $include_measured = $c->req->param('include_measured') || '';
476 my $search_type = $c->req->param("search_type") || 'fast';
478 my $trial = $c->stash->{trial};
479 if ($data_level eq 'plants') {
480 if (!$trial->has_plant_entries()) {
481 $c->stash->{template} = 'generic_message.mas';
482 $c->stash->{message} = "The requested trial (".$trial->get_name().") does not have plant entries. Please create the plant entries first.";
483 return;
486 if ($data_level eq 'subplots' || $data_level eq 'plants_subplots') {
487 if (!$trial->has_subplot_entries()) {
488 $c->stash->{template} = 'generic_message.mas';
489 $c->stash->{message} = "The requested trial (".$trial->get_name().") does not have subplot entries.";
490 return;
494 my $selected_cols = $c->req->param('selected_columns') ? JSON::XS->new()->decode( $c->req->param('selected_columns') ) : {};
495 if ($data_level eq 'plate'){
496 $selected_cols = {'trial_name'=>1, 'acquisition_date'=>1, 'plot_name'=>1, 'plot_number'=>1, 'row_number'=>1, 'col_number'=>1, 'source_observation_unit_name'=>1, 'accession_name'=>1, 'synonyms'=>1, 'dna_person'=>1, 'notes'=>1, 'tissue_type'=>1, 'extraction'=>1, 'concentration'=>1, 'volume'=>1, 'is_blank'=>1, 'facility_identifier'=>1};
498 if ($data_level eq 'samplingtrial'){
499 $selected_cols = {'trial_name'=>1, 'year'=>1, 'location'=>1, 'sampling_facility'=>1, 'sampling_trial_sample_type'=>1, 'acquisition_date'=>1, 'tissue_sample_name'=>1, 'plot_number'=>1, 'rep_number'=>1, 'source_observation_unit_name'=>1, 'accession_name'=>1, 'synonyms'=>1, 'dna_person'=>1, 'notes'=>1, 'tissue_type'=>1, 'extraction'=>1, 'concentration'=>1, 'volume'=>1 };
501 my $selected_trait_list_id = $c->req->param('trait_list_id');
502 my @trait_list;
503 if ($selected_trait_list_id){
504 my $list = CXGN::List->new({ dbh => $c->dbc->dbh, list_id => $selected_trait_list_id });
505 my @selected_trait_names = @{$list->elements()};
506 my $validator = CXGN::List::Validate->new();
507 my @absent_traits = @{$validator->validate($schema, 'traits', \@selected_trait_names)->{'missing'}};
508 if (scalar(@absent_traits)>0){
509 $c->stash->{template} = 'generic_message.mas';
510 $c->stash->{message} = "Trait list is not valid because of these terms: ".join ',',@absent_traits;
511 return;
513 my $lt = CXGN::List::Transform->new();
514 @trait_list = @{$lt->transform($schema, "traits_2_trait_ids", \@selected_trait_names)->{transform}};
517 my @treatment_project_ids;
518 my $treatments = $trial->get_treatments();
519 foreach (@$treatments){
520 push @treatment_project_ids, $_->[0];
523 if ($trait_list && $trait_list ne 'null') {
524 @trait_list = @{_parse_list_from_json($trait_list)};
527 my $plugin = "";
528 if ( ($format eq "xls") && ($what eq "layout")) {
529 $plugin = "TrialLayoutExcel";
531 if (($format eq "csv") && ($what eq "layout")) {
532 $plugin = "TrialLayoutCSV";
534 if (($format eq "xls") && ($what =~ /phenotype/)) {
535 $plugin = "TrialPhenotypeExcel";
537 if (($format eq "csv") && ($what =~ /phenotype/)) {
538 $plugin = "TrialPhenotypeCSV";
540 if (($format eq "xls") && ($what eq "basic_trial_excel")) {
541 $plugin = "BasicExcel";
543 if ( ($format eq "intertekxls") && ($what eq "layout")) {
544 $plugin = "GenotypingTrialLayoutIntertekXLS";
546 if ( ($format eq "dartseqcsv") && ($what eq "layout")) {
547 $plugin = "GenotypingTrialLayoutDartSeqCSV";
550 my @field_crossing_data_order;
551 if ($format eq "crossing_experiment_xls") {
552 $plugin = "CrossingExperimentXLS";
553 $what = "crosses";
554 $format = "xlsx";
555 my $cross_properties = $c->config->{cross_properties};
556 @field_crossing_data_order = split ',',$cross_properties;
559 my $prop_id;
560 if ($format eq "soil_data_xls") {
561 $plugin = "SoilDataXLS";
562 $what = "soil_data";
563 $format = "xlsx";
564 $prop_id = $c->req->param("prop_id");
567 my $trial_name = $trial->get_name();
568 $trial_name =~ s/ /\_/g;
569 my $trial_id = $trial->get_trial_id();
570 my $dir = $c->tempfiles_subdir('download');
571 my $temp_file_name = $trial_name . "_" . "$what" . "XXXX";
572 my $rel_file = $c->tempfile( TEMPLATE => "download/$temp_file_name");
573 $rel_file = $rel_file . ".$format";
574 my $tempfile = $c->config->{basepath}."/".$rel_file;
577 my $download = CXGN::Trial::Download->new({
578 bcs_schema => $schema,
579 trial_id => $c->stash->{trial_id},
580 trait_list => \@trait_list,
581 filename => $tempfile,
582 format => $plugin,
583 data_level => $data_level,
584 search_type => $search_type,
585 include_timestamp => $timestamp_option,
586 treatment_project_ids => \@treatment_project_ids,
587 selected_columns => $selected_cols,
588 include_measured => $include_measured,
589 field_crossing_data_order => \@field_crossing_data_order,
590 prop_id => $prop_id
593 my $error = $download->download();
595 if ($format eq 'intertekxls'){
596 $format = 'xls';
598 if ($format eq 'dartseqcsv'){
599 $format = 'csv';
602 #my $file_name = $trial_id . "_" . "$what" . ".$format";
603 my $file_name = $trial_name . "_" . "layout" . ".$format";
604 $c->res->content_type('Application/'.$format);
605 $c->res->header('Content-Disposition', qq[attachment; filename="$file_name"]);
607 my $output = read_file($tempfile);
609 $c->res->body($output);
612 sub trials_download_layouts : Path('/breeders/trials/download/layout') Args(0) {
613 my $self = shift;
614 my $c = shift;
615 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
616 my $user = $c->user();
617 if (!$user) {
618 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
619 return;
622 my $format = $c->req->param("format") || "xls";
623 my $data_level = $c->req->param("dataLevel") || "plot";
624 my $genotyping_trial_id = $c->req->param("genotyping_trial_id");
625 my @genotyping_trial_id_list = $c->req->param("genotyping_trial_id_list") ? split ',', $c->req->param("genotyping_trial_id_list") : ();
627 my $selected_cols = $c->req->param('selected_columns') ? JSON::XS->new()->decode( $c->req->param('selected_columns') ) : {};
628 if ($data_level eq 'plate'){
629 $selected_cols = {'trial_name'=>1, 'acquisition_date'=>1, 'plot_name'=>1, 'plot_number'=>1, 'row_number'=>1, 'col_number'=>1, 'source_observation_unit_name'=>1,
630 'accession_name'=>1, 'synonyms'=>1, 'dna_person'=>1, 'notes'=>1, 'tissue_type'=>1, 'extraction'=>1, 'concentration'=>1, 'volume'=>1, 'is_blank'=>1, 'facility_identifier' =>1};
633 my $plugin = "";
634 if ($format eq "intertekxls") {
635 $plugin = "GenotypingTrialLayoutIntertekXLS";
637 if ($format eq "dartseqcsv") {
638 $plugin = "GenotypingTrialLayoutDartSeqCSV";
640 if ($format eq "csv") {
641 $plugin = "TrialLayoutCSV";
644 my $dir = $c->tempfiles_subdir('download');
645 my $temp_file_name = "genotyping_plate_layouts"."XXXX";
646 my $rel_file = $c->tempfile( TEMPLATE => "download/$temp_file_name");
647 $rel_file = $rel_file . ".$format";
648 my $tempfile = $c->config->{basepath}."/".$rel_file;
650 #print STDERR "TEMPFILE : $tempfile\n";
652 my $trial_download_args = {
653 bcs_schema => $schema,
654 trial_list => \@genotyping_trial_id_list,
655 filename => $tempfile,
656 format => $plugin,
657 data_level => $data_level,
658 selected_columns => $selected_cols
660 if ($genotyping_trial_id) {
661 $trial_download_args->{trial_id} = $genotyping_trial_id;
663 my $download = CXGN::Trial::Download->new($trial_download_args);
664 my $error = $download->download();
666 if ($format eq 'intertekxls'){
667 $format = 'xls';
669 if ($format eq 'dartseqcsv'){
670 $format = 'csv';
673 my $file_name = "genotyping_plate_layouts.$format";
674 $c->res->content_type('Application/'.$format);
675 $c->res->header('Content-Disposition', qq[attachment; filename="$file_name"]);
676 my $output = read_file($tempfile);
677 $c->res->body($output);
680 sub _parse_list_from_json {
681 my $list_json = shift;
683 if ($list_json) {
684 #my $decoded_list = $json->allow_nonref->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($list_json);
685 my $decoded_list = JSON::XS->new()->decode($list_json);
686 my @array_of_list_items = @{$decoded_list};
687 return \@array_of_list_items;
689 else {
690 return;
695 # Search for trial by trial name
696 # Display trial detail page for 1 match, error messages for no matches
697 sub search_trial : Private {
698 my ( $self, $c, $trial_query ) = @_;
699 my $schema = $c->dbic_schema("Bio::Chado::Schema");
700 my $rs = $schema->resultset('Project::Project');
702 my $matches;
703 my $count = 0;
705 # Search by name
706 if ( defined($trial_query) ) {
707 $matches = $rs->search({
708 'UPPER(name)' => uc($trial_query)
711 $count = $matches->count;
714 # NO MATCH FOUND
715 if ( $count != 1 ) {
716 $c->stash->{template} = "generic_message.mas";
717 $c->stash->{message} = "<strong>No Matching Trial Found</strong> ($trial_query)<br />You can view and search for trials from the <a href='/search/trials'>Trial Search Page</a>";
720 # 1 MATCH FOUND - FORWARD TO VIEW TRIAL
721 else {
722 my $trial_id = $matches->first->project_id;
723 $c->stash->{trial_id} = $trial_id;
725 my $schema = $c->dbic_schema("Bio::Chado::Schema");
726 $c->stash->{schema} = $schema;
728 my $trial = CXGN::Trial->new( { bcs_schema => $schema, trial_id => $trial_id });
729 $c->stash->{trial} = $trial;
731 $c->forward('trial_info');