fix accidental deletion..
[sgn.git] / lib / SGN / Controller / solGS / CachedResult.pm
blobf1c9f883fc4745ff50c0f83382095c48788360b4
1 =head1 AUTHOR
3 Isaak Y Tecle <iyt2@cornell.edu>
5 =head1 Name
7 SGN::Controller::solGS::CachedResult - a controller related to cached result.
9 =cut
12 package SGN::Controller::solGS::CachedResult;
14 use Moose;
15 use namespace::autoclean;
16 use JSON;
19 BEGIN { extends 'Catalyst::Controller::REST' }
22 __PACKAGE__->config(
23 default => 'application/json',
24 stash_key => 'rest',
25 map => { 'application/json' => 'JSON',
26 'text/html' => 'JSON' },
30 sub check_cached_result :Path('/solgs/check/cached/result') Args(0) {
31 my ($self, $c) = @_;
33 my $req_page = $c->req->param('page');
34 my $args = $c->req->param('args');
35 my $json = JSON->new();
36 $args = $json->decode($args);
38 $self->_check_cached_output($c, $req_page, $args);
43 sub _check_cached_output {
44 my ($self, $c, $req_page, $args) = @_;
46 $c->stash->{rest}{cached} = undef;
48 if ($req_page =~ /solgs\/population\//)
50 my $pop_id = $args->{training_pop_id}[0];
52 if ($pop_id !~ /uploaded/)
54 $self->_check_single_trial_training_data($c, $pop_id);
57 elsif ($req_page =~ /solgs\/populations\/combined\//)
59 my $pop_id = $args->{training_pops_id}[0] || $args->{combo_pops_id}[0];
60 $c->stash->{data_set_type} = $args->{data_set_type};
62 $self->_check_combined_trials_data($c, $pop_id);
64 elsif ($req_page =~ /solgs\/trait\//)
66 my $pop_id = $args->{training_pop_id}[0];
67 my $trait_id = $args->{trait_id}[0];
69 $self->_check_single_trial_model_output($c, $pop_id, $trait_id);
71 elsif ($req_page =~ /solgs\/model\/combined\/trials\//)
73 my $pop_id = $args->{training_pop_id}[0];
74 my $trait_id = $args->{trait_id}[0];
76 $c->stash->{data_set_type} = $args->{data_set_type};
78 $self->_check_combined_trials_model_output($c, $pop_id, $trait_id);
80 elsif ($req_page =~ /solgs\/model\/\d+\/prediction\//)
82 my $tr_pop_id = $args->{training_pop_id}[0];
83 my $sel_pop_id = $args->{selection_pop_id}[0];
84 my $trait_id = $args->{trait_id}[0];
86 $c->stash->{data_set_type} = $args->{data_set_type};
88 my $referer = $c->req->referer;
90 if ($referer =~ /solgs\/traits\/all\//)
92 $self->_check_selection_pop_all_traits_output($c, $tr_pop_id, $sel_pop_id);
94 elsif ($referer =~ /solgs\/models\/combined\/trials\//)
96 $self->_check_selection_pop_all_traits_output($c, $tr_pop_id, $sel_pop_id);
98 else
100 $self->_check_selection_pop_output($c, $tr_pop_id, $sel_pop_id, $trait_id);
103 elsif ($req_page =~ /solgs\/analyze\/traits\/population\//)
105 my $tr_pop_id = $args->{training_pop_id}[0];
106 my $sel_pop_id = $args->{selection_pop_id}[0];
107 my $traits_ids = $args->{trait_id};
109 $self->_check_single_trial_model_all_traits_output($c, $tr_pop_id, $traits_ids);
111 elsif ($req_page =~ /solgs\/models\/combined\/trials\//)
113 my $tr_pop_id = $args->{training_pop_id}[0];
114 my $sel_pop_id = $args->{selection_pop_id}[0];
115 my $traits = $args->{trait_id};
117 $c->stash->{data_set_type} = $args->{data_set_type};
119 $self->_check_combined_trials_model_all_traits_output($c, $tr_pop_id, $traits);
125 sub _check_single_trial_training_data {
126 my ($self, $c, $pop_id) = @_;
128 $c->stash->{rest}{cached} = $self->check_single_trial_training_data($c, $pop_id);
133 sub _check_single_trial_model_output {
134 my ($self, $c, $pop_id, $trait_id) =@_;
136 my $cached_pop_data = $self->check_single_trial_training_data($c, $pop_id);
138 if ($cached_pop_data)
140 $c->stash->{rest}{cached} = $self->check_single_trial_model_output($c, $pop_id, $trait_id);
145 sub _check_single_trial_model_all_traits_output {
146 my ($self, $c, $pop_id, $traits_ids) =@_;
148 my $cached_pop_data = $self->check_single_trial_training_data($c, $pop_id);
150 $self->check_single_trial_model_all_traits_output($c, $pop_id, $traits_ids);
152 foreach my $tr (@$traits_ids)
154 my $tr_cache = $c->stash->{$tr}{cached};
156 if (!$tr_cache)
158 $c->stash->{rest}{cached} = undef;
159 last;
161 else
163 $c->stash->{rest}{cached} = 1;
169 sub _check_combined_trials_data {
170 my ($self, $c, $pop_id) =@_;
172 $c->stash->{combo_pops_id} = $pop_id;
173 $c->controller('solGS::combinedTrials')->get_combined_pops_arrayref($c);
174 my $trials = $c->stash->{arrayref_combined_pops_ids};
176 foreach my $trial (@$trials)
178 $self->_check_single_trial_training_data($c, $trial);
179 my $cached = $c->stash->{rest}{cached};
181 last if !$c->stash->{rest}{cached};
186 sub _check_combined_trials_model_output {
187 my ($self, $c, $pop_id, $trait_id) =@_;
189 my $cached_pop_data = $self->check_combined_trials_training_data($c, $pop_id, $trait_id);
191 if ($cached_pop_data)
193 $c->stash->{rest}{cached} = $self->check_single_trial_model_output($c, $pop_id, $trait_id);
199 sub _check_combined_trials_model_all_traits_output {
200 my ($self, $c, $pop_id, $traits) = @_;
202 $self->check_combined_trials_model_all_traits_output($c, $pop_id, $traits);
204 foreach my $tr (@$traits)
206 my $tr_cache = $c->stash->{$tr}{cached};
208 if (!$tr_cache)
210 $c->stash->{rest}{cached} = undef;
211 last;
213 else
215 $c->stash->{rest}{cached} = 1;
222 sub _check_selection_pop_all_traits_output {
223 my ($self, $c, $tr_pop_id, $sel_pop_id) =@_;
225 #$self->check_selection_pop_all_traits_output($c, $tr_pop_id, $sel_pop_id);
227 $c->controller('solGS::solGS')->prediction_pop_analyzed_traits($c, $tr_pop_id, $sel_pop_id);
228 my $sel_traits_ids = $c->stash->{prediction_pop_analyzed_traits_ids};
230 $c->stash->{training_pop_id} = $tr_pop_id;
231 $c->controller("solGS::solGS")->traits_with_valid_models($c);
232 my $training_models_traits = $c->stash->{traits_ids_with_valid_models};
234 if (sort(@$sel_traits_ids) ~~ sort(@$training_models_traits))
236 $c->stash->{rest}{cached} = 1;
242 sub _check_selection_pop_output {
243 my ($self, $c, $tr_pop_id, $sel_pop_id, $trait_id) = @_;
245 my $data_set_type = $c->stash->{data_set_type};
247 if ($data_set_type =~ 'combined populations')
249 $self->_check_combined_trials_model_selection_output($c, $tr_pop_id, $sel_pop_id, $trait_id);
251 else
253 $self->_check_single_trial_model_selection_output($c, $tr_pop_id, $sel_pop_id, $trait_id);
259 sub _check_single_trial_model_selection_output {
260 my ($self, $c, $tr_pop_id, $sel_pop_id, $trait_id) = @_;
262 my $cached_pop_data = $self->check_single_trial_training_data($c, $tr_pop_id);
264 if ($cached_pop_data)
266 my $cached_model_out = $self->check_single_trial_model_output($c, $tr_pop_id, $trait_id);
268 if ($cached_model_out)
270 $c->stash->{rest}{cached} = $self->check_selection_pop_output($c, $tr_pop_id, $sel_pop_id, $trait_id);
277 sub _check_combined_trials_model_selection_output {
278 my ($self, $c, $tr_pop_id, $sel_pop_id, $trait_id) = @_;
280 my $cached_tr_data = $self->check_combined_trials_training_data($c, $tr_pop_id, $trait_id);
282 if ($cached_tr_data)
284 my $cached_model_out = $self->_check_combined_trials_model_output($c, $tr_pop_id, $trait_id);
286 if ($cached_model_out)
288 $c->stash->{rest}{cached} = $self->check_selection_pop_output($c, $tr_pop_id, $sel_pop_id, $trait_id);
295 sub check_single_trial_training_data {
296 my ($self, $c, $pop_id) = @_;
298 $c->controller('solGS::solGS')->phenotype_file_name($c, $pop_id);
299 my $cached_pheno = -s $c->stash->{phenotype_file_name};
301 $c->controller('solGS::solGS')->genotype_file_name($c, $pop_id);
302 my $cached_geno = -s $c->stash->{genotype_file_name};
304 if ($cached_pheno && $cached_geno)
306 return 1;
308 else
310 return 0;
316 sub check_single_trial_model_output {
317 my ($self, $c, $pop_id, $trait_id) = @_;
319 $c->controller('solGS::solGS')->get_trait_details($c, $trait_id);
320 my $trait_abbr = $c->stash->{trait_abbr};
322 $c->stash->{trait_abbr} = $trait_abbr;
323 $c->stash->{pop_id} = $pop_id;
325 $c->controller('solGS::solGS')->gebv_kinship_file($c);
326 my $cached_gebv = -s $c->stash->{gebv_kinship_file};
328 if ($cached_gebv)
330 return 1;
332 else
334 return 0;
340 sub check_single_trial_model_all_traits_output {
341 my ($self, $c, $pop_id, $traits_ids) =@_;
343 my $cached_pop_data = $self->check_single_trial_training_data($c, $pop_id);
345 if ($cached_pop_data)
347 foreach my $tr (@$traits_ids)
349 $c->stash->{$tr}{cached} = $self->check_single_trial_model_output($c, $pop_id, $tr);
356 sub check_combined_trials_model_all_traits_output {
357 my ($self, $c, $pop_id, $traits_ids) =@_;
359 foreach my $tr (@$traits_ids)
361 my $cached_tr_data = $self->check_combined_trials_training_data($c, $pop_id, $tr);
363 if ($cached_tr_data)
365 $c->stash->{$tr}{cached} = $self->check_single_trial_model_output($c, $pop_id, $tr);
372 sub check_selection_pop_output {
373 my ($self, $c, $tr_pop_id, $sel_pop_id, $trait_id) = @_;
375 my $identifier = $tr_pop_id . '_' . $sel_pop_id;
376 $c->controller('solGS::solGS')->prediction_pop_gebvs_file($c, $identifier, $trait_id);
378 my $cached_gebv = -s $c->stash->{prediction_pop_gebvs_file};
380 if ($cached_gebv)
382 return 1;
384 else
386 return 0;
392 sub check_selection_pop_all_traits_output {
393 my ($self, $c, $tr_pop_id, $sel_pop_id) =@_;
395 $c->controller('solGS::solGS')->prediction_pop_analyzed_traits($c, $tr_pop_id, $sel_pop_id);
396 my $traits_ids = $c->stash->{prediction_pop_analyzed_traits_ids};
398 foreach my $tr (@$traits_ids)
400 $c->stash->{$tr}{cached} = $self->check_selection_pop_output($c, $tr_pop_id, $sel_pop_id, $tr);
406 sub check_combined_trials_training_data {
407 my ($self, $c, $combo_pops_id, $trait_id) = @_;
409 $c->controller('solGS::solGS')->get_trait_details($c, $trait_id);
410 $c->stash->{combo_pops_id} = $combo_pops_id;
412 $c->controller('solGS::solGS')->cache_combined_pops_data($c);
414 my $cached_pheno = -s $c->stash->{trait_combined_pheno_file};
415 my $cached_geno = -s $c->stash->{trait_combined_geno_file};
417 if ($cached_pheno && $cached_geno)
419 return 1;
421 else
423 return 0;
429 sub begin : Private {
430 my ($self, $c) = @_;
432 $c->controller("solGS::solGS")->get_solgs_dirs($c);
437 #####
438 1;###
439 ####