can download plant phenotype data in the same way as plot phenotype data
[sgn.git] / lib / SGN / Controller / BreedersToolbox / Trial.pm
blobe4d44e58e7f4859183b8a3775101f4b2fcc686df
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;
15 BEGIN { extends 'Catalyst::Controller'; }
18 sub trial_init : Chained('/') PathPart('breeders/trial') CaptureArgs(1) {
19 my $self = shift;
20 my $c = shift;
21 my $trial_id = shift;
23 $c->stash->{trial_id} = $trial_id;
24 print STDERR "TRIAL ID = $trial_id\n";
26 my $schema = $c->dbic_schema("Bio::Chado::Schema");
27 $c->stash->{schema} = $schema;
28 my $trial;
29 eval {
30 $trial = CXGN::Trial->new( { bcs_schema => $schema, trial_id => $trial_id });
32 if ($@) {
33 $c->stash->{template} = 'system_message.txt';
34 $c->stash->{message} = "The requested trial ($trial_id) does not exist";
35 return;
37 $c->stash->{trial} = $trial;
40 sub old_trial_url : Path('/breeders_toolbox/trial') Args(1) {
41 my $self = shift;
42 my $c = shift;
43 my @args = @_;
44 my $format = $c->req->param("format");
45 $c->res->redirect('/breeders/trial/'.$args[0].'?format='.$format);
48 sub trial_info : Chained('trial_init') PathPart('') Args(0) {
49 #print STDERR "Check 1: ".localtime()."\n";
50 my $self = shift;
51 my $c = shift;
52 my $format = $c->req->param("format");
53 #print STDERR $format;
54 my $user = $c->user();
55 if (!$user) {
56 $c->res->redirect( uri( path => '/solpeople/login.pl', query => { goto_url => $c->req->uri->path_query } ) );
57 return;
60 $c->stash->{user_can_modify} = ($user->check_roles("submitter") || $user->check_roles("curator")) ;
62 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
63 my $trial = $c->stash->{trial};
64 my $program_object = CXGN::BreedersToolbox::Projects->new( { schema => $schema });
66 if (!$program_object->trial_exists($c->stash->{trial_id})) {
67 $c->stash->{message} = "The requested trial does not exist or has been deleted.";
68 $c->stash->{template} = 'generic_message.mas';
69 return;
72 $c->stash->{trial_name} = $trial->get_name();
74 my $trial_type_data = $trial->get_project_type();
75 $c->stash->{trial_type} = $trial_type_data->[1];
77 $c->stash->{planting_date} = $trial->get_planting_date();
79 $c->stash->{harvest_date} = $trial->get_harvest_date();
81 $c->stash->{trial_description} = $trial->get_description();
83 my $location_data = $trial->get_location();
84 $c->stash->{location_id} = $location_data->[0];
85 $c->stash->{location_name} = $location_data->[1];
87 my $breeding_program_data = $program_object->get_breeding_programs_by_trial($c->stash->{trial_id});
88 $c->stash->{breeding_program_id} = $breeding_program_data->[0]->[0];
89 $c->stash->{breeding_program_name} = $breeding_program_data->[0]->[1];
91 $c->stash->{year} = $trial->get_year();
93 $c->stash->{trial_id} = $c->stash->{trial_id};
95 $c->stash->{has_plant_entries} = $trial->has_plant_entries();
97 $c->stash->{hidap_enabled} = $c->config->{hidap_enabled};
99 if ($trial->get_folder) {
100 $c->stash->{folder_id} = $trial->get_folder()->project_id();
101 $c->stash->{folder_name} = $trial->get_folder()->name();
104 my $design_type = $trial->get_design_type();
106 if ($design_type eq "genotyping_plate") {
107 if ($format eq "as_table") {
108 $c->stash->{template} = '/breeders_toolbox/genotyping_trials/format/as_table.mas';
110 else {
111 $c->stash->{template} = '/breeders_toolbox/genotyping_trials/detail.mas';
115 else {
116 $c->stash->{template} = '/breeders_toolbox/trial.mas';
119 print STDERR "End Load Trial Detail Page: ".localtime()."\n";
124 sub trait_info :Path('/breeders/trial') Args(3) {
125 my ($self, $c, $trial_id, $trait_txt, $trait_id) = @_;
127 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
129 my $trial_name = $schema->resultset("Project::Project")
130 ->search( {'me.project_id' => $trial_id})
131 ->single
132 ->name;
134 $c->stash->{trial_name} = $trial_name;
136 my $trait_name = $schema->resultset("Cv::Cvterm")
137 ->search({'me.cvterm_id' => $trait_id})
138 ->single
139 ->name;
141 $c->stash->{trial_id} = $trial_id;
142 $c->stash->{trial_name} = $trial_name;
144 $c->stash->{trait_id} = $trait_id;
145 $c->stash->{trait_name} = $trait_name;
147 $c->stash->{template} = '/breeders_toolbox/trial_trait.mas';
151 sub trial_tree : Path('/breeders/trialtree') Args(0) {
152 my $self = shift;
153 my $c = shift;
156 $c->stash->{template} = '/breeders_toolbox/trialtree.mas';
160 sub trial_download : Chained('trial_init') PathPart('download') Args(1) {
161 my $self = shift;
162 my $c = shift;
163 my $what = shift;
166 my $user = $c->user();
167 if (!$user) {
168 $c->res->redirect( uri( path => '/solpeople/login.pl', query => { goto_url => $c->req->uri->path_query } ) );
169 return;
172 my $format = $c->req->param("format") || "xls";
173 my $data_level = $c->req->param("dataLevel") || "plots";
174 my $timestamp_option = $c->req->param("timestamp") || 0;
175 my $trait_list = $c->req->param("trait_list") || "";
177 if ($data_level eq 'plants') {
178 my $trial = $c->stash->{trial};
179 if (!$trial->has_plant_entries()) {
180 $c->stash->{template} = 'generic_message.mas';
181 $c->stash->{message} = "The requested trial (".$trial->get_name().") does not have plant entries. Please create the plant entries first.";
182 return;
186 my @trait_list;
187 if ($trait_list) {
188 @trait_list = @{_parse_list_from_json($trait_list)};
191 my $plugin = "";
192 if ( ($format eq "xls") && ($what eq "layout")) {
193 $plugin = "TrialLayoutExcel";
195 if (($format eq "csv") && ($what eq "layout")) {
196 $plugin = "TrialLayoutCSV";
198 if (($format eq "xls") && ($what =~ /phenotype/)) {
199 $plugin = "TrialPhenotypeExcel";
201 if (($format eq "csv") && ($what =~ /phenotype/)) {
202 $plugin = "TrialPhenotypeCSV";
204 if (($format eq "xls") && ($what eq "basic_trial_excel")) {
205 $plugin = "BasicExcel";
208 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
209 my $trial = CXGN::Trial->new({bcs_schema => $schema, trial_id => $c->stash->{trial_id} });
210 my $trial_name = $trial->get_name();
211 my $trial_id = $trial->get_trial_id();
212 my $dir = $c->tempfiles_subdir('download');
213 my $temp_file_name = $trial_id . "_" . "$what" . "XXXX";
214 my $rel_file = $c->tempfile( TEMPLATE => "download/$temp_file_name");
215 $rel_file = $rel_file . ".$format";
216 my $tempfile = $c->config->{basepath}."/".$rel_file;
218 print STDERR "TEMPFILE : $tempfile\n";
220 my $download = CXGN::Trial::Download->new(
222 bcs_schema => $c->stash->{schema},
223 trial_id => $c->stash->{trial_id},
224 trait_list => \@trait_list,
225 filename => $tempfile,
226 format => $plugin,
227 data_level => $data_level,
228 include_timestamp => $timestamp_option,
231 my $error = $download->download();
233 my $file_name = $trial_id . "_" . "$what" . ".$format";
234 $c->res->content_type('Application/'.$format);
235 $c->res->header('Content-Disposition', qq[attachment; filename="$file_name"]);
237 my $output = read_file($tempfile);
239 $c->res->body($output);
243 sub _parse_list_from_json {
244 my $list_json = shift;
245 my $json = new JSON;
246 if ($list_json) {
247 my $decoded_list = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($list_json);
248 #my $decoded_list = decode_json($list_json);
249 my @array_of_list_items = @{$decoded_list};
250 return \@array_of_list_items;
252 else {
253 return;