Added eval; site now shows clean dataset missing message instead of server error...
[sgn.git] / lib / SGN / Controller / solGS / CachedResult.pm
blob8d9d5f6f272d82dadd5487156c7ae40042df2e50
2 =head1 AUTHOR
4 Isaak Y Tecle <iyt2@cornell.edu>
6 =head1 Name
8 SGN::Controller::solGS::CachedResult - checks for cached output.
10 =cut
12 package SGN::Controller::solGS::CachedResult;
14 use Moose;
15 use namespace::autoclean;
17 use File::Slurp qw /write_file read_file/;
18 use JSON;
20 #use Scalar::Util qw /weaken reftype/;
21 use Data::Dumper;
23 BEGIN { extends 'Catalyst::Controller::REST' }
25 __PACKAGE__->config(
26 default => 'application/json',
27 stash_key => 'rest',
28 map => {
29 'application/json' => 'JSON',
30 'text/html' => 'JSON'
34 sub check_cached_result : Path('/solgs/check/cached/result') Args(0) {
35 my ( $self, $c ) = @_;
37 my $req_page = $c->req->param('page');
38 my $args = $c->req->param('arguments');
40 $c->controller('solGS::Utils')->stash_json_args( $c, $args );
42 $c->stash->{rest}{arguments} = $args;
44 my $json = JSON->new();
45 $args = $json->decode($args);
47 $self->_check_cached_output( $c, $req_page, $args );
51 sub _check_cached_output {
52 my ( $self, $c, $req_page, $args ) = @_;
54 $c->stash->{training_traits_ids} =
55 $args->{training_traits_ids} || $args->{trait_id};
56 my $training_pop_id = $args->{training_pop_id};
58 $c->stash->{training_pop_id} =
59 ref($training_pop_id) eq 'ARRAY'
60 ? $training_pop_id->[0]
61 : $training_pop_id;
63 $c->stash->{rest}{cached} = undef;
65 if ( $req_page =~ /solgs\/population\// ) {
66 my $pop_id = $args->{training_pop_id}[0];
67 $self->_check_single_trial_training_data( $c, $pop_id );
69 elsif ( $req_page =~ /solgs\/populations\/combined\// ) {
70 my $pop_id = $args->{training_pops_id}[0] || $args->{combo_pops_id}[0];
71 $c->stash->{data_set_type} = $args->{data_set_type};
73 $self->_check_combined_trials_data( $c, $pop_id );
75 elsif ( $req_page =~ /solgs\/trait\// ) {
76 my $pop_id = $args->{training_pop_id}[0];
77 my $trait_id = $args->{trait_id}[0];
79 $self->_check_single_trial_model_output( $c, $pop_id, $trait_id );
81 elsif ( $req_page =~ /solgs\/model\/combined\/trials\// ) {
82 my $pop_id = $args->{training_pop_id}[0];
83 my $trait_id = $args->{trait_id}[0];
85 $c->stash->{data_set_type} = $args->{data_set_type};
87 $self->_check_combined_trials_model_output( $c, $pop_id, $trait_id );
89 elsif ( $req_page =~ /solgs\/selection\/(\d+|\w+_\d+)\/model\// ) {
90 my $tr_pop_id = $args->{training_pop_id}[0];
91 my $sel_pop_id = $args->{selection_pop_id}[0];
92 my $trait_id = $args->{trait_id}[0];
94 $c->stash->{data_set_type} = $args->{data_set_type};
96 my $referer = $c->req->referer;
98 if ( $referer =~ /solgs\/traits\/all\// ) {
99 $self->_check_selection_pop_all_traits_output( $c, $tr_pop_id,
100 $sel_pop_id );
102 elsif ( $referer =~ /solgs\/models\/combined\/trials\// ) {
103 $self->_check_selection_pop_all_traits_output( $c, $tr_pop_id,
104 $sel_pop_id );
106 else {
107 $self->_check_selection_pop_output( $c, $tr_pop_id, $sel_pop_id,
108 $trait_id );
111 elsif ( $req_page =~ /solgs\/traits\/all\/population\// ) {
112 my $tr_pop_id = $args->{training_pop_id}[0];
113 my $sel_pop_id = $args->{selection_pop_id}[0];
114 my $traits_ids = $args->{training_traits_ids};
116 $self->_check_single_trial_model_all_traits_output( $c, $tr_pop_id,
117 $traits_ids );
119 elsif ( $req_page =~ /solgs\/models\/combined\/trials\// ) {
120 my $tr_pop_id = $args->{training_pop_id}[0];
121 my $sel_pop_id = $args->{selection_pop_id}[0];
122 my $traits = $args->{training_traits_ids};
124 $self->_check_combined_trials_model_all_traits_output( $c, $tr_pop_id,
125 $traits );
127 elsif ( $req_page =~ /kinship\/analysis/ ) {
128 $c->controller('solGS::Kinship')
129 ->stash_kinship_pop_id( $c, $args->{kinship_pop_id} );
130 my $kinship_pop_id = $c->stash->{kinship_pop_id};
131 my $protocol_id = $args->{genotyping_protocol_id};
132 my $trait_id = $args->{trait_id};
134 $self->_check_kinship_output( $c, $kinship_pop_id, $protocol_id,
135 $trait_id );
137 elsif ( $req_page =~ /pca\/analysis/ ) {
138 my $pca_pop_id = $args->{pca_pop_id};
139 my $data_str = $args->{data_structure};
141 if ( $data_str =~ /dataset|list/ && $pca_pop_id !~ /dataset|list/ ) {
142 $pca_pop_id = $data_str . '_' . $pca_pop_id;
145 my $file_id = $c->controller('solGS::Files')->create_file_id($c);
146 $self->_check_pca_output( $c, $file_id );
148 elsif ( $req_page =~ /cluster\/analysis/ ) {
149 my $cluster_pop_id = $args->{cluster_pop_id};
150 my $protocol_id = $args->{genotyping_protocol_id};
152 # my
153 # my $trait_id = $args->{trait_id};
154 my $data_str = $args->{data_structure};
156 if ( $data_str =~ /dataset|list/ && $cluster_pop_id !~ /dataset|list/ )
158 $cluster_pop_id = $data_str . '_' . $cluster_pop_id;
161 my $file_id = $c->controller('solGS::Files')->create_file_id($c);
162 $self->_check_cluster_output( $c, $file_id );
167 sub _check_single_trial_training_data {
168 my ( $self, $c, $pop_id ) = @_;
170 $c->stash->{rest}{cached} =
171 $self->check_single_trial_training_data( $c, $pop_id );
175 sub _check_single_trial_model_output {
176 my ( $self, $c, $pop_id, $trait_id ) = @_;
178 my $cached_pop_data =
179 $self->check_single_trial_training_data( $c, $pop_id );
181 if ($cached_pop_data) {
182 $c->stash->{rest}{cached} =
183 $self->check_single_trial_model_output( $c, $pop_id, $trait_id );
187 sub _check_single_trial_model_all_traits_output {
188 my ( $self, $c, $pop_id, $traits_ids ) = @_;
190 my $cached_pop_data =
191 $self->check_single_trial_training_data( $c, $pop_id );
193 $self->check_single_trial_model_all_traits_output( $c, $pop_id,
194 $traits_ids );
196 foreach my $tr (@$traits_ids) {
197 my $tr_cache = $c->stash->{$tr}{cached};
199 if ( !$tr_cache ) {
200 $c->stash->{rest}{cached} = undef;
201 last;
203 else {
204 $c->stash->{rest}{cached} = 1;
209 sub _check_combined_trials_data {
210 my ( $self, $c, $pop_id ) = @_;
212 $c->stash->{combo_pops_id} = $pop_id;
213 $c->controller('solGS::combinedTrials')->get_combined_pops_list($c);
214 my $trials = $c->stash->{combined_pops_list};
216 foreach my $trial (@$trials) {
217 $self->_check_single_trial_training_data( $c, $trial );
218 my $cached = $c->stash->{rest}{cached};
220 last if !$c->stash->{rest}{cached};
224 sub _check_combined_trials_model_output {
225 my ( $self, $c, $pop_id, $trait_id ) = @_;
227 my $cached_pop_data =
228 $self->check_combined_trials_training_data( $c, $pop_id, $trait_id );
230 if ($cached_pop_data) {
231 $c->stash->{rest}{cached} =
232 $self->check_single_trial_model_output( $c, $pop_id, $trait_id );
237 sub _check_combined_trials_model_all_traits_output {
238 my ( $self, $c, $pop_id, $traits ) = @_;
240 $self->check_combined_trials_model_all_traits_output( $c, $pop_id,
241 $traits );
243 foreach my $tr (@$traits) {
244 my $tr_cache = $c->stash->{$tr}{cached};
246 if ( !$tr_cache ) {
247 $c->stash->{rest}{cached} = undef;
248 last;
250 else {
251 $c->stash->{rest}{cached} = 1;
257 sub _check_selection_pop_all_traits_output {
258 my ( $self, $c, $tr_pop_id, $sel_pop_id ) = @_;
260 $c->controller('solGS::Gebvs')
261 ->selection_pop_analyzed_traits( $c, $tr_pop_id, $sel_pop_id );
262 my $sel_traits_ids = $c->stash->{selection_pop_analyzed_traits_ids};
264 $c->stash->{training_pop_id} = $tr_pop_id;
265 $c->controller("solGS::solGS")->traits_with_valid_models($c);
266 my $training_models_traits = $c->stash->{traits_ids_with_valid_models};
268 $c->stash->{rest}{cached} = 0;
269 if ( $sel_traits_ids->[0] ) {
270 if ( scalar(@$sel_traits_ids) == scalar(@$training_models_traits) ) {
271 if ( sort(@$sel_traits_ids) ~~ sort(@$training_models_traits) ) {
272 $c->stash->{rest}{cached} = 1;
279 sub _check_selection_pop_output {
280 my ( $self, $c, $tr_pop_id, $sel_pop_id, $trait_id ) = @_;
282 if ( $c->stash->{data_set_type} =~ 'combined_populations' ) {
283 $self->_check_combined_trials_model_selection_output( $c, $tr_pop_id,
284 $sel_pop_id, $trait_id );
286 else {
287 $self->_check_single_trial_model_selection_output( $c, $tr_pop_id,
288 $sel_pop_id, $trait_id );
293 sub _check_single_trial_model_selection_output {
294 my ( $self, $c, $tr_pop_id, $sel_pop_id, $trait_id ) = @_;
296 my $cached_pop_data =
297 $self->check_single_trial_training_data( $c, $tr_pop_id );
299 if ($cached_pop_data) {
300 my $cached_model_out =
301 $self->check_single_trial_model_output( $c, $tr_pop_id, $trait_id );
303 if ($cached_model_out) {
304 $c->stash->{rest}{cached} =
305 $self->check_selection_pop_output( $c, $tr_pop_id, $sel_pop_id,
306 $trait_id );
312 sub _check_combined_trials_model_selection_output {
313 my ( $self, $c, $tr_pop_id, $sel_pop_id, $trait_id ) = @_;
315 my $cached_tr_data =
316 $self->check_combined_trials_training_data( $c, $tr_pop_id, $trait_id );
318 if ($cached_tr_data) {
319 my $cached_model_out =
320 $self->_check_combined_trials_model_output( $c, $tr_pop_id,
321 $trait_id );
323 if ($cached_model_out) {
324 $c->stash->{rest}{cached} =
325 $self->check_selection_pop_output( $c, $tr_pop_id, $sel_pop_id,
326 $trait_id );
332 sub _check_kinship_output {
333 my ( $self, $c, $kinship_pop_id, $protocol_id, $trait_id ) = @_;
335 $c->stash->{rest} =
336 $self->check_kinship_output( $c, $kinship_pop_id, $protocol_id,
337 $trait_id );
340 sub _check_pca_output {
341 my ( $self, $c, $file_id ) = @_;
343 $c->stash->{rest} = $self->check_pca_output( $c, $file_id );
346 sub _check_cluster_output {
347 my ( $self, $c, $file_id ) = @_;
348 my $cached = $self->check_cluster_output( $c, $file_id );
349 $c->stash->{rest} = $self->check_cluster_output( $c, $file_id );
352 sub check_single_trial_training_data {
353 my ( $self, $c, $pop_id, $protocol_id ) = @_;
355 $protocol_id = $c->stash->{genotyping_protocol_id} if !$protocol_id;
356 $c->controller('solGS::genotypingProtocol')
357 ->stash_protocol_id( $c, $protocol_id );
358 $protocol_id = $c->stash->{genotyping_protocol_id};
360 my $cached_pheno = $self->check_cached_phenotype_data( $c, $pop_id );
361 my $cached_geno = $self->check_cached_genotype_data( $c, $pop_id );
363 if ( $cached_pheno && $cached_geno ) {
364 return 1;
366 else {
367 return 0;
372 sub check_cached_genotype_data {
373 my ( $self, $c, $pop_id ) = @_;
375 $c->controller('solGS::Files')->genotype_file_name( $c, $pop_id );
376 my $file = $c->stash->{genotype_file_name};
378 my $count = $c->controller('solGS::Utils')->count_data_rows($file);
379 return $count;
383 sub check_cached_phenotype_data {
384 my ( $self, $c, $pop_id ) = @_;
386 $c->controller('solGS::Files')->phenotype_file_name( $c, $pop_id );
387 my $file = $c->stash->{phenotype_file_name};
389 my $count = $c->controller('solGS::Utils')->count_data_rows($file);
390 return $count;
394 sub check_single_trial_model_output {
395 my ( $self, $c, $pop_id, $trait_id, $protocol_id ) = @_;
397 $c->stash->{trait_id} = $trait_id;
398 $c->stash->{training_pop_id} = $pop_id;
400 $c->controller('solGS::Files')
401 ->rrblup_training_gebvs_file( $c, $pop_id, $trait_id, $protocol_id );
402 my $cached_gebv = -s $c->stash->{rrblup_training_gebvs_file};
404 if ($cached_gebv) {
405 return 1;
407 else {
408 return 0;
413 sub check_single_trial_model_all_traits_output {
414 my ( $self, $c, $pop_id, $traits_ids ) = @_;
416 my $cached_pop_data =
417 $self->check_single_trial_training_data( $c, $pop_id );
419 if ($cached_pop_data) {
420 foreach my $tr (@$traits_ids) {
421 $c->stash->{$tr}{cached} =
422 $self->check_single_trial_model_output( $c, $pop_id, $tr );
428 sub check_combined_trials_model_all_traits_output {
429 my ( $self, $c, $pop_id, $traits_ids ) = @_;
431 foreach my $tr (@$traits_ids) {
432 my $cached_tr_data =
433 $self->check_combined_trials_training_data( $c, $pop_id, $tr );
435 if ($cached_tr_data) {
436 $c->stash->{$tr}{cached} =
437 $self->check_single_trial_model_output( $c, $pop_id, $tr );
443 sub check_selection_pop_output {
444 my ( $self, $c, $tr_pop_id, $sel_pop_id, $trait_id ) = @_;
446 $c->stash->{trait_id} = $trait_id;
447 $c->stash->{training_pop_id} = $tr_pop_id;
448 $c->stash->{selection_pop_id} = $sel_pop_id;
450 $c->controller('solGS::Files')
451 ->rrblup_selection_gebvs_file( $c, $tr_pop_id, $sel_pop_id, $trait_id );
452 my $cached_gebv = -s $c->stash->{rrblup_selection_gebvs_file};
454 if ($cached_gebv) {
455 return 1;
457 else {
458 return 0;
463 sub check_selection_pop_all_traits_output {
464 my ( $self, $c, $tr_pop_id, $sel_pop_id ) = @_;
466 $c->controller('solGS::Gebvs')
467 ->selection_pop_analyzed_traits( $c, $tr_pop_id, $sel_pop_id );
468 my $traits_ids = $c->stash->{selection_pop_analyzed_traits_ids};
470 foreach my $tr (@$traits_ids) {
471 $c->stash->{$tr}{cached} =
472 $self->check_selection_pop_output( $c, $tr_pop_id, $sel_pop_id, $tr );
477 sub check_combined_trials_training_data {
478 my ( $self, $c, $combo_pops_id, $trait_id ) = @_;
480 $c->controller('solGS::Trait')->get_trait_details( $c, $trait_id );
481 $c->stash->{combo_pops_id} = $combo_pops_id;
483 $c->controller('solGS::combinedTrials')->cache_combined_pops_data($c);
485 my $cached_pheno = -s $c->stash->{trait_combined_pheno_file};
486 my $cached_geno = -s $c->stash->{trait_combined_geno_file};
488 if ( $cached_pheno && $cached_geno ) {
489 return 1;
491 else {
492 return 0;
497 sub check_multi_trials_training_data {
498 my ( $self, $c, $trials_ids, $protocol_id ) = @_;
500 my $cached;
502 foreach my $trial_id (@$trials_ids) {
503 my $exists = $self->check_single_trial_training_data( $c, $trial_id,
504 $protocol_id );
506 if ( !$exists ) {
507 last;
509 else {
510 $cached = $exists;
514 return $cached;
518 sub check_kinship_output {
519 my ( $self, $c, $pop_id, $protocol_id, $trait_id ) = @_;
521 my $files = $c->controller('solGS::Kinship')
522 ->get_kinship_coef_files( $c, $pop_id, $protocol_id, $trait_id );
524 if ( -s $files->{'json_file_adj'} && -s $files->{'matrix_file_adj'} ) {
525 my $res =
526 $c->controller('solGS::Kinship')->structure_kinship_response($c);
527 return $res;
529 else {
530 return { cached => 0 };
535 sub check_pca_output {
536 my ( $self, $c, $file_id ) = @_;
538 if ($file_id) {
539 $c->stash->{file_id} = $file_id;
540 $c->controller('solGS::pca')->pca_scores_file($c);
541 my $scores_file = $c->stash->{pca_scores_file};
543 if ( -s $scores_file ) {
544 $c->controller('solGS::pca')->prepare_pca_output_response($c);
545 my $ret = $c->stash->{pca_output_response};
546 return $ret;
548 else {
549 my $ret = { scores_file => 0 };
555 sub check_cluster_output {
556 my ( $self, $c, $file_id ) = @_;
558 if ($file_id) {
559 $c->stash->{file_id} = $file_id;
561 my $cluster_type = $c->stash->{cluster_type};
562 my $cached_file;
563 if ( $cluster_type =~ /k-means/i ) {
564 $c->controller('solGS::Cluster')->cluster_result_file($c);
565 $cached_file = $c->stash->{"${cluster_type}_result_file"};
567 else {
568 $c->controller('solGS::Cluster')->cluster_result_file($c);
569 $cached_file = $c->stash->{"${cluster_type}_result_newick_file"};
571 if ( -s $cached_file ) {
572 my $res = $c->controller('solGS::Cluster')->prepare_response($c);
573 return $res;
575 else {
576 return { cached => 0 };
582 sub begin : Private {
583 my ( $self, $c ) = @_;
585 $c->controller('solGS::Files')->get_solgs_dirs($c);
589 #####
590 1; ###
591 ####