2 package SGN
::Controller
::BreedersToolbox
::Trial
;
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
;
19 BEGIN { extends
'Catalyst::Controller'; }
22 sub trial_init
: Chained
('/') PathPart
('breeders/trial') CaptureArgs
(1) {
27 $c->stash->{trial_id
} = $trial_id;
28 print STDERR
"TRIAL ID = $trial_id\n";
30 my $schema = $c->dbic_schema("Bio::Chado::Schema");
31 $c->stash->{schema
} = $schema;
34 $trial = CXGN
::Trial
->new( { bcs_schema
=> $schema, trial_id
=> $trial_id });
37 $c->stash->{template
} = 'system_message.txt';
38 $c->stash->{message
} = "The requested trial ($trial_id) does not exist";
41 $c->stash->{trial
} = $trial;
44 sub old_trial_url
: Path
('/breeders_toolbox/trial') Args
(1) {
48 my $format = $c->req->param("format");
49 $c->res->redirect('/breeders/trial/'.$args[0].'?format='.$format);
52 sub trial_info
: Chained
('trial_init') PathPart
('') Args
(0) {
53 #print STDERR "Check 1: ".localtime()."\n";
54 print STDERR
"TRIAL INIT...\n\n";
57 my $format = $c->req->param("format");
58 #print STDERR $format;
59 my $user = $c->user();
61 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
65 $c->stash->{user_can_modify
} = ($user->check_roles("submitter") || $user->check_roles("curator")) ;
67 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
68 my $trial = $c->stash->{trial
};
69 my $program_object = CXGN
::BreedersToolbox
::Projects
->new( { schema
=> $schema });
71 if (!$program_object->trial_exists($c->stash->{trial_id
})) {
72 $c->stash->{message
} = "The requested trial does not exist or has been deleted.";
73 $c->stash->{template
} = 'generic_message.mas';
77 $c->stash->{trial_name
} = $trial->get_name();
79 my $trial_type_data = $trial->get_project_type();
80 $c->stash->{trial_type
} = $trial_type_data->[1];
81 $c->stash->{trial_type_id
} = $trial_type_data->[0];
83 $c->stash->{planting_date
} = $trial->get_planting_date();
85 $c->stash->{harvest_date
} = $trial->get_harvest_date();
87 $c->stash->{trial_description
} = $trial->get_description();
88 $c->stash->{trial_phenotype_files
} = $trial->get_phenotype_metadata();
89 $c->stash->{assayed_traits
} = $trial->get_traits_assayed();
91 my $location_data = $trial->get_location();
92 $c->stash->{location_id
} = $location_data->[0];
93 $c->stash->{location_name
} = $location_data->[1];
95 my $breeding_program_data = $program_object->get_breeding_programs_by_trial($c->stash->{trial_id
});
96 $c->stash->{breeding_program_id
} = $breeding_program_data->[0]->[0];
97 $c->stash->{breeding_program_name
} = $breeding_program_data->[0]->[1];
99 $c->stash->{year
} = $trial->get_year();
101 $c->stash->{trial_id
} = $c->stash->{trial_id
};
103 $c->stash->{has_col_and_row_numbers
} = $trial->has_col_and_row_numbers();
104 $c->stash->{has_plant_entries
} = $trial->has_plant_entries();
105 $c->stash->{has_subplot_entries
} = $trial->has_subplot_entries();
106 $c->stash->{has_tissue_sample_entries
} = $trial->has_tissue_sample_entries();
107 $c->stash->{phenotypes_fully_uploaded
} = $trial->get_phenotypes_fully_uploaded();
109 $c->stash->{hidap_enabled
} = $c->config->{hidap_enabled
};
110 $c->stash->{has_expression_atlas
} = $c->config->{has_expression_atlas
};
111 $c->stash->{expression_atlas_url
} = $c->config->{expression_atlas_url
};
112 $c->stash->{main_production_site_url
} = $c->config->{main_production_site_url
};
113 $c->stash->{site_project_name
} = $c->config->{project_name
};
114 $c->stash->{sgn_session_id
} = $c->req->cookie('sgn_session_id');
115 $c->stash->{user_name
} = $c->user->get_object->get_username;
117 if ($trial->get_folder) {
118 $c->stash->{folder_id
} = $trial->get_folder()->project_id();
119 $c->stash->{folder_name
} = $trial->get_folder()->name();
122 my $design_type = $trial->get_design_type();
123 $c->stash->{design_name
} = $design_type;
125 # print STDERR "TRIAL TYPE DATA = $trial_type_data->[1]\n\n";
127 if ($design_type eq "genotyping_plate") {
128 $c->stash->{plate_id
} = $c->stash->{trial_id
};
129 $c->stash->{genotyping_facility
} = $trial->get_genotyping_facility;
130 $c->stash->{genotyping_facility_submitted
} = $trial->get_genotyping_facility_submitted;
131 $c->stash->{genotyping_facility_status
} = $trial->get_genotyping_facility_status;
132 $c->stash->{genotyping_plate_sample_type
} = $trial->get_genotyping_plate_sample_type;
133 if ($trial->get_genotyping_plate_format){
134 $c->stash->{genotyping_plate_format
} = $trial->get_genotyping_plate_format;
136 if ($format eq "as_table") {
137 $c->stash->{template
} = '/breeders_toolbox/genotyping_trials/format/as_table.mas';
140 $c->stash->{template
} = '/breeders_toolbox/genotyping_trials/detail.mas';
143 elsif ($design_type eq "treatment"){
144 $c->stash->{template
} = '/breeders_toolbox/treatment.mas';
147 $c->stash->{template
} = '/breeders_toolbox/trial.mas';
150 if ($trial_type_data->[1] eq "crossing_trial"){
151 print STDERR
"It's a crossing trial!\n\n";
152 $c->stash->{template
} = '/breeders_toolbox//cross/crossing_trial.mas';
155 print STDERR
"End Load Trial Detail Page: ".localtime()."\n";
160 sub trait_info
:Path
('/breeders/trial') Args
(3) {
161 my ($self, $c, $trial_id, $trait_txt, $trait_id) = @_;
163 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
165 my $trial_name = $schema->resultset("Project::Project")
166 ->search( {'me.project_id' => $trial_id})
170 $c->stash->{trial_name
} = $trial_name;
172 my $trait_name = $schema->resultset("Cv::Cvterm")
173 ->search({'me.cvterm_id' => $trait_id})
177 $c->stash->{trial_id
} = $trial_id;
178 $c->stash->{trial_name
} = $trial_name;
180 $c->stash->{trait_id
} = $trait_id;
181 $c->stash->{trait_name
} = $trait_name;
183 $c->stash->{template
} = '/breeders_toolbox/trial_trait.mas';
186 ##DEPRECATED by /breeders/trials
187 sub trial_tree
: Path
('/breeders/trialtree') Args
(0) {
192 $c->stash->{template
} = '/breeders_toolbox/trialtree.mas';
196 #For downloading trial layout in CSV and Excel, for downloading trial phenotypes in CSV and Excel, and for downloading trial phenotyping spreadsheets in Excel.
197 #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.
198 sub trial_download
: Chained
('trial_init') PathPart
('download') Args
(1) {
202 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
204 my $user = $c->user();
206 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
210 my $format = $c->req->param("format") || "xls";
211 my $data_level = $c->req->param("dataLevel") || "plot";
212 my $timestamp_option = $c->req->param("timestamp") || 0;
213 my $trait_list = $c->req->param("trait_list");
214 my $search_type = $c->req->param("search_type") || 'fast';
216 my $trial = $c->stash->{trial
};
217 if ($data_level eq 'plants') {
218 if (!$trial->has_plant_entries()) {
219 $c->stash->{template
} = 'generic_message.mas';
220 $c->stash->{message
} = "The requested trial (".$trial->get_name().") does not have plant entries. Please create the plant entries first.";
224 if ($data_level eq 'subplots' || $data_level eq 'plants_subplots') {
225 if (!$trial->has_subplot_entries()) {
226 $c->stash->{template
} = 'generic_message.mas';
227 $c->stash->{message
} = "The requested trial (".$trial->get_name().") does not have subplot entries.";
232 my $selected_cols = $c->req->param('selected_columns') ? decode_json
$c->req->param('selected_columns') : {};
233 if ($data_level eq 'plate'){
234 $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, 'dna_person'=>1, 'notes'=>1, 'tissue_type'=>1, 'extraction'=>1, 'concentration'=>1, 'volume'=>1, 'is_blank'=>1};
236 my $selected_trait_list_id = $c->req->param('trait_list_id');
237 my @selected_trait_names;
239 if ($selected_trait_list_id){
240 my $list = CXGN
::List
->new({ dbh
=> $c->dbc->dbh, list_id
=> $selected_trait_list_id });
241 @selected_trait_names = @
{$list->elements()};
242 my $validator = CXGN
::List
::Validate
->new();
243 my @absent_traits = @
{$validator->validate($schema, 'traits', \
@selected_trait_names)->{'missing'}};
244 if (scalar(@absent_traits)>0){
245 $c->stash->{template
} = 'generic_message.mas';
246 $c->stash->{message
} = "Trait list is not valid because of these terms: ".join ',',@absent_traits;
249 my $lt = CXGN
::List
::Transform
->new();
250 @trait_list = @
{$lt->transform($schema, "traits_2_trait_ids", \
@selected_trait_names)->{transform
}};
253 my @treatment_project_ids;
254 my $treatments = $trial->get_treatments();
255 foreach (@
$treatments){
256 push @treatment_project_ids, $_->[0];
259 if ($trait_list && $trait_list ne 'null') {
260 @trait_list = @
{_parse_list_from_json
($trait_list)};
264 if ( ($format eq "xls") && ($what eq "layout")) {
265 $plugin = "TrialLayoutExcel";
267 if (($format eq "csv") && ($what eq "layout")) {
268 $plugin = "TrialLayoutCSV";
270 if (($format eq "xls") && ($what =~ /phenotype/)) {
271 $plugin = "TrialPhenotypeExcel";
273 if (($format eq "csv") && ($what =~ /phenotype/)) {
274 $plugin = "TrialPhenotypeCSV";
276 if (($format eq "xls") && ($what eq "basic_trial_excel")) {
277 $plugin = "BasicExcel";
279 if ( ($format eq "intertekxls") && ($what eq "layout")) {
280 $plugin = "GenotypingTrialLayoutIntertekXLS";
283 my $trial_name = $trial->get_name();
284 my $trial_id = $trial->get_trial_id();
285 my $dir = $c->tempfiles_subdir('download');
286 my $temp_file_name = $trial_id . "_" . "$what" . "XXXX";
287 my $rel_file = $c->tempfile( TEMPLATE
=> "download/$temp_file_name");
288 $rel_file = $rel_file . ".$format";
289 my $tempfile = $c->config->{basepath
}."/".$rel_file;
291 print STDERR
"TEMPFILE : $tempfile\n";
293 my $download = CXGN
::Trial
::Download
->new({
294 bcs_schema
=> $schema,
295 trial_id
=> $c->stash->{trial_id
},
296 trait_list
=> \
@trait_list,
297 filename
=> $tempfile,
299 data_level
=> $data_level,
300 search_type
=> $search_type,
301 include_timestamp
=> $timestamp_option,
302 treatment_project_ids
=> \
@treatment_project_ids,
303 selected_columns
=> $selected_cols,
304 selected_trait_names
=> \
@selected_trait_names,
307 my $error = $download->download();
309 if ($format eq 'intertekxls'){
313 my $file_name = $trial_id . "_" . "$what" . ".$format";
314 $c->res->content_type('Application/'.$format);
315 $c->res->header('Content-Disposition', qq[attachment
; filename
="$file_name"]);
317 my $output = read_file
($tempfile);
319 $c->res->body($output);
323 sub _parse_list_from_json
{
324 my $list_json = shift;
327 my $decoded_list = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($list_json);
328 #my $decoded_list = decode_json($list_json);
329 my @array_of_list_items = @
{$decoded_list};
330 return \
@array_of_list_items;