Merge pull request #5248 from solgenomics/topic/batch_update_trials
[sgn.git] / lib / SGN / Controller / solGS / Download.pm
blob9783bcb2767017a5ef605ad396edd070b1760611
1 package SGN::Controller::solGS::Download;
4 use Moose;
5 use namespace::autoclean;
8 BEGIN { extends 'Catalyst::Controller::REST' }
11 __PACKAGE__->config(
12 default => 'application/json',
13 stash_key => 'rest',
14 map => { 'application/json' => 'JSON' },
18 sub download_training_pop_data :Path('/solgs/download/training/pop/data') Args(0) {
19 my ($self, $c) = @_;
21 my $args = $c->req->param('arguments');
22 $c->controller('solGS::Utils')->stash_json_args($c, $args);
24 $c->stash->{rest}{training_pop_raw_geno_file} = $self->download_raw_geno_data_file($c);
25 $c->stash->{rest}{training_pop_raw_pheno_file} = $self->download_raw_pheno_data_file($c);
26 $c->stash->{rest}{traits_acronym_file} = $self->download_traits_acronym_file($c);
31 sub download_selection_pop_data :Path('/solgs/download/selection/pop/data') Args(0) {
32 my ($self, $c) = @_;
34 my $args = $c->req->param('arguments');
35 $c->controller('solGS::Utils')->stash_json_args($c, $args);
37 my $geno_file = $self->download_selection_pop_filtered_geno_data_file($c);
38 my $log_file = $self->download_selection_prediction_report_file($c);
40 $c->stash->{rest}{selection_pop_filtered_geno_file} = $geno_file;
41 $c->stash->{rest}{selection_prediction_report_file} = $log_file;
44 sub download_model_input_data :Path('/solgs/download/model/input/data') Args(0) {
45 my ($self, $c) = @_;
47 my $args = $c->req->param('arguments');
48 $c->controller('solGS::Utils')->stash_json_args($c, $args);
50 my $geno_file = $self->download_model_geno_data_file($c);
51 my $pheno_file = $self->download_model_pheno_data_file($c);
52 my $log_file = $self->download_model_analysis_report_file($c);
54 $c->stash->{rest}{model_geno_data_file} = $geno_file;
55 $c->stash->{rest}{model_pheno_data_file} = $pheno_file;
56 $c->stash->{rest}{model_analysis_report_file} = $log_file;
61 sub download_gebvs :Path('/solgs/download/gebvs/pop') Args(0) {
62 my ($self, $c) = @_;
64 my $args = $c->req->param('arguments');
65 $c->controller('solGS::Utils')->stash_json_args($c, $args);
67 my $gebvs_file;
68 my $type;
69 if ($c->stash->{selection_pop_id})
71 $type = 'selection';
72 $gebvs_file = $self->download_selection_gebvs_file($c);
74 else
76 $type = 'training';
77 $gebvs_file = $self->download_training_gebvs_file($c);
80 my $file_id = $c->controller('solGS::Files')->gebvs_file_id($c, $type);
82 $c->stash->{rest}{gebvs_file} = $gebvs_file;
83 $c->stash->{rest}{gebvs_file_id} = $file_id;
88 sub download_marker_effects :Path('/solgs/download/model/marker/effects') Args(0) {
89 my ($self, $c) = @_;
91 my $args = $c->req->param('arguments');
92 $c->controller('solGS::Utils')->stash_json_args($c, $args);
94 my $marker_effects_file = $self->download_marker_effects_file($c);
95 $c->stash->{rest}{marker_effects_file} = $marker_effects_file;
99 sub download_traits_acronym :Path('/solgs/download/traits/acronym') Args(0) {
100 my ($self, $c) = @_;
102 my $args = $c->req->param('arguments');
103 $c->controller('solGS::Utils')->stash_json_args($c, $args);
105 my $acronyms_file = $self->download_traits_acronym_file($c);
106 $c->stash->{rest}{traits_acronym_file} = $acronyms_file;
111 sub selection_prediction_download_urls {
112 my ($self, $c, $training_pop_id, $selection_pop_id) = @_;
114 my $selected_model_traits = $c->stash->{training_traits_ids} || [$c->stash->{trait_id}];
115 my $protocol_id = $c->stash->{genotyping_protocol_id};
117 no warnings 'uninitialized';
119 my $url_args = {
120 'training_pop_id' => $training_pop_id,
121 'selection_pop_id' => $selection_pop_id,
122 'genotyping_protocol_id' => $protocol_id,
125 my $selection_traits_ids;
127 if ($selection_pop_id)
129 $c->controller('solGS::Gebvs')->selection_pop_analyzed_traits($c, $training_pop_id, $selection_pop_id);
130 $selection_traits_ids = $c->stash->{selection_pop_analyzed_traits_ids};
133 my @selection_traits_ids = sort(@$selection_traits_ids) if $selection_traits_ids->[0];
134 my @selected_model_traits = sort(@$selected_model_traits) if $selected_model_traits->[0];
136 my $page = $c->req->referer;
137 my $data_set_type = $page =~ /combined/ ? 'combined_populations' : 'single_population';
138 $url_args->{data_set_type} = $data_set_type;
140 my $sel_pop_page;
141 my $download_url;
143 if (@selected_model_traits ~~ @selection_traits_ids)
145 foreach my $trait_id (@selection_traits_ids)
148 $url_args->{trait_id} = $trait_id;
150 $c->controller('solGS::Trait')->get_trait_details($c, $trait_id);
151 my $trait_abbr = $c->stash->{trait_abbr};
153 $sel_pop_page = $c->controller('solGS::Path')->selection_page_url($url_args);
155 if ($page =~ /solgs\/traits\/all\/|solgs\/models\/combined\//)
157 $download_url .= " | " if $download_url;
160 $download_url .= qq |<a href="$sel_pop_page">$trait_abbr</a> |;
164 if (!$download_url)
166 my $trait_id = $selected_model_traits[0];
167 $url_args->{trait_id} = $trait_id;
169 $sel_pop_page = $c->controller('solGS::Path')->selection_page_url($url_args);
170 $download_url = qq | <a href ="$sel_pop_page" onclick="solGS.waitPage(this.href); return false;">[ Predict ]</a>|;
173 $c->stash->{selection_prediction_download} = $download_url;
177 sub download_raw_pheno_data_file {
178 my ($self, $c) = @_;
180 my $pop_id = $c->stash->{training_pop_id};
181 my $data_set_type = $c->stash->{data_set_type};
183 my @trials_ids;
184 if ($data_set_type =~ /combined_populations/)
186 my $trials_ids = $c->controller('solGS::combinedTrials')->get_combined_pops_list($c, $pop_id);
187 @trials_ids = @$trials_ids;
189 else
191 push @trials_ids, $pop_id;
194 my @files;
196 foreach my $trial_id (@trials_ids)
198 my $file = $c->controller('solGS::Files')->phenotype_file_name($c, $trial_id);
199 $file = $c->controller('solGS::Files')->copy_to_tempfiles_subdir($c, $file, 'solgs');
201 push @files, $file;
204 return \@files;
209 sub download_raw_geno_data_file {
210 my ($self, $c) = @_;
212 my $pop_id = $c->stash->{training_pop_id};
213 my $protocol_id = $c->stash->{genotyping_protocol_id};
214 my $data_set_type = $c->stash->{data_set_type};
216 my @trials_ids;
217 if ($data_set_type =~ /combined_populations/)
219 my $trials_ids = $c->controller('solGS::combinedTrials')->get_combined_pops_list($c, $pop_id);
220 @trials_ids = @$trials_ids;
222 else
224 push @trials_ids, $pop_id;
227 my @files;
229 foreach my $trial_id (@trials_ids)
231 my $file = $c->controller('solGS::Files')->genotype_file_name($c, $trial_id, $protocol_id);
232 $file = $c->controller('solGS::Files')->copy_to_tempfiles_subdir($c, $file, 'solgs');
234 push @files, $file;
237 return \@files;
241 sub download_selection_pop_filtered_geno_data_file {
242 my ($self, $c) = @_;
244 $c->controller('solGS::Files')->filtered_selection_genotype_file($c);
245 my $file = $c->stash->{filtered_selection_genotype_file};
246 $file = $c->controller('solGS::Files')->copy_to_tempfiles_subdir($c, $file, 'solgs');
248 return $file;
253 sub download_model_geno_data_file {
254 my ($self, $c) = @_;
256 $c->controller('solGS::Trait')->get_trait_details($c, $c->stash->{trait_id});
258 my $file = $c->controller('solGS::Files')->model_genodata_file($c);
259 $file = $c->controller('solGS::Files')->copy_to_tempfiles_subdir($c, $file, 'solgs');
261 return $file;
266 sub download_model_pheno_data_file {
267 my ($self, $c) = @_;
269 $c->controller('solGS::Trait')->get_trait_details($c, $c->stash->{trait_id});
271 my $file = $c->controller('solGS::Files')->model_phenodata_file($c);
272 $file = $c->controller('solGS::Files')->copy_to_tempfiles_subdir($c, $file, 'solgs');
274 return $file;
278 sub download_model_analysis_report_file {
279 my ($self, $c) = @_;
281 $c->controller('solGS::Trait')->get_trait_details($c, $c->stash->{trait_id});
283 my $page = $c->controller('solGS::Path')->page_type($c, $c->req->referer);
285 if ($page =~ /training_model/)
287 $c->stash->{analysis_type} = 'training_model';
290 my $file = $c->controller('solGS::Files')->analysis_report_file($c);
291 #$file = $c->controller('solGS::Files')->convert_txt_pdf($file);
293 $file = $c->controller('solGS::Files')->copy_to_tempfiles_subdir($c, $file, 'solgs');
295 return $file;
299 sub download_selection_prediction_report_file {
300 my ($self, $c) = @_;
302 $c->controller('solGS::Trait')->get_trait_details($c, $c->stash->{trait_id});
304 my $referer = $c->req->referer;
305 $c->stash->{analysis_type} = $c->controller('solGS::Path')->page_type($c, $referer);
307 my $file = $c->controller('solGS::Files')->analysis_report_file($c);
308 $file = $c->controller('solGS::Files')->copy_to_tempfiles_subdir($c, $file, 'solgs');
310 return $file;
314 sub download_training_gebvs_file {
315 my ($self, $c) = @_;
317 my $training_pop_id = $c->stash->{training_pop_id};
318 my $trait_id = $c->stash->{trait_id};
319 my $protocol_id = $c->stash->{genotyping_protocol_id};
321 $c->controller('solGS::Files')->rrblup_training_gebvs_file($c, $training_pop_id, $trait_id, $protocol_id);
322 my $gebvs_file = $c->stash->{rrblup_training_gebvs_file};
324 $gebvs_file = $c->controller('solGS::Files')->copy_to_tempfiles_subdir($c, $gebvs_file, 'solgs');
326 return $gebvs_file;
331 sub download_selection_gebvs_file {
332 my ($self, $c) = @_;
334 my $training_pop_id = $c->stash->{training_pop_id};
335 my $selection_pop_id = $c->stash->{selection_pop_id};
336 my $trait_id = $c->stash->{trait_id};
337 my $protocol_id = $c->stash->{genotyping_protocol_id};
339 $c->controller('solGS::Files')->rrblup_selection_gebvs_file($c, $training_pop_id, $selection_pop_id, $trait_id, $protocol_id);
340 my $gebvs_file = $c->stash->{rrblup_selection_gebvs_file};
342 $gebvs_file = $c->controller('solGS::Files')->copy_to_tempfiles_subdir($c, $gebvs_file, 'solgs');
344 return $gebvs_file;
349 sub download_marker_effects_file {
350 my ($self, $c) = @_;
352 $c->controller('solGS::Trait')->get_trait_details($c, $c->stash->{trait_id});
354 my $file = $c->controller('solGS::Files')->marker_effects_file($c);
355 $file = $c->controller('solGS::Files')->copy_to_tempfiles_subdir($c, $file, 'solgs');
357 return $file;
360 sub download_traits_acronym_file {
361 my ($self, $c) = @_;
364 $c->controller('solGS::Files')->traits_acronym_file($c, $c->stash->{training_pop_id});
365 my $file = $c->stash->{traits_acronym_file};
367 $file = $c->controller('solGS::Files')->copy_to_tempfiles_subdir($c, $file, 'solgs');
369 return $file;
372 sub begin : Private {
373 my ($self, $c) = @_;
375 $c->controller('solGS::Files')->get_solgs_dirs($c);
379 #####
381 #####