1 package SGN
::Controller
::solGS
::Files
;
4 use namespace
::autoclean
;
8 use File
::Path qw
/ mkpath /;
9 use File
::Temp qw
/ tempfile tempdir /;
10 use File
::Spec
::Functions qw
/ catfile catdir/;
11 use File
::Slurp qw
/write_file read_file/;
13 use CXGN
::People
::Person
;
17 BEGIN { extends
'Catalyst::Controller' }
20 sub marker_effects_file
{
23 my $pop_id = $c->stash->{pop_id
} || $c->stash->{training_pop_id
};
24 my $trait = $c->stash->{trait_abbr
};
26 my $protocol_id = $c->stash->{genotyping_protocol_id
};
27 my $file_id = "${pop_id}-${trait}-GP-${protocol_id}";
29 no warnings
'uninitialized';
31 my $data_set_type = $c->stash->{data_set_type
};
33 my $cache_data = {key
=> 'marker_effects_' . $file_id,
34 file
=> 'marker_effects_' . $file_id,
35 stash_key
=> 'marker_effects_file',
36 cache_dir
=> $c->stash->{solgs_cache_dir
}
39 $self->cache_file($c, $cache_data);
44 sub variance_components_file
{
47 my $pop_id = $c->stash->{training_pop_id
} || $c->stash->{pop_id
};
48 my $trait = $c->stash->{trait_abbr
};
50 my $data_set_type = $c->stash->{data_set_type
};
51 my $protocol_id = $c->stash->{genotyping_protocol_id
};
53 my $file_id = "${pop_id}-${trait}-GP-${protocol_id}";
55 no warnings
'uninitialized';
58 my $cache_data = {key
=> 'variance_components_' . $file_id,
59 file
=> 'variance_components_' . $file_id,
60 stash_key
=> 'variance_components_file',
61 cache_dir
=> $c->stash->{solgs_cache_dir
}
64 $self->cache_file($c, $cache_data);
68 sub model_phenodata_file
{
71 my $pop_id = $c->stash->{training_pop_id
} || $c->stash->{combo_pops_id
} ;
72 my $trait_abbr = $c->stash->{trait_abbr
};
73 my $protocol_id = $c->stash->{genotyping_protocol_id
};
75 my $id = "${pop_id}-${trait_abbr}-GP-${protocol_id}";
78 no warnings
'uninitialized';
80 my $cache_data = {key
=> 'model_phenodata_' . $id,
81 file
=> 'model_phenodata_' . $id,
82 stash_key
=> 'model_phenodata_file',
83 cache_dir
=> $c->stash->{solgs_cache_dir
}
86 $self->cache_file($c, $cache_data);
91 sub model_genodata_file
{
94 my $pop_id = $c->stash->{training_pop_id
} || $c->stash->{combo_pops_id
} ;
95 my $trait_abbr = $c->stash->{trait_abbr
};
96 my $protocol_id = $c->stash->{genotyping_protocol_id
};
98 my $id = "${pop_id}-${trait_abbr}-GP-${protocol_id}";
101 no warnings
'uninitialized';
103 my $cache_data = {key
=> 'model_genodata_' . $id,
104 file
=> 'model_genodata_' . $id,
105 stash_key
=> 'model_genodata_file',
106 cache_dir
=> $c->stash->{solgs_cache_dir
}
109 $self->cache_file($c, $cache_data);
114 sub trait_raw_phenodata_file
{
117 my $pop_id = $c->stash->{training_pop_id
} || $c->stash->{combo_pops_id
} ;
118 my $trait_abbr = $c->stash->{trait_abbr
};
120 my $id = "${pop_id}-${trait_abbr}";
123 no warnings
'uninitialized';
125 my $cache_data = {key
=> 'trait_raw_phenodata_' . $id,
126 file
=> 'trait_raw_phenodata_' . $id,
127 stash_key
=> 'trait_raw_phenodata_file',
128 cache_dir
=> $c->stash->{solgs_cache_dir
}
131 $self->cache_file($c, $cache_data);
138 sub model_info_file
{
141 my $pop_id = $c->stash->{training_pop_id
} || $c->stash->{combo_pops_id
};
142 my $trait_id = $c->stash->{trait_id
};
143 my $trait_abbr = $c->stash->{trait_abbr
};
144 my $protocol_id = $c->stash->{genotyping_protocol_id
};
146 my $file_id = "${trait_id}-${pop_id}-GP-${protocol_id}";
148 my $cache_data = { key
=> 'model_info_file_' . $file_id,
149 file
=> 'model_info_file_' . $file_id,
150 stash_key
=> 'model_info_file',
151 cache_dir
=> $c->stash->{solgs_cache_dir
}
154 $self->cache_file($c, $cache_data);
161 sub filtered_training_genotype_file
{
162 my ($self, $c, $pop_id, $protocol_id) = @_;
164 $pop_id = $c->stash->{training_pop_id
} || $c->stash->{pop_id
} || $c->{stash
}->{combo_pops_id
} if !$pop_id;
166 $protocol_id = $c->stash->{genotyping_protocol_id
} if !$protocol_id;
167 my $file_id = "${pop_id}-GP-${protocol_id}";
169 my $cache_data = { key
=> 'filtered_genotype_data_' . $file_id,
170 file
=> 'filtered_training_genotype_data_' . $file_id,
171 stash_key
=> 'filtered_training_genotype_file',
172 cache_dir
=> $c->stash->{solgs_cache_dir
}
175 $self->cache_file($c, $cache_data);
178 sub genotype_filtering_log_file
{
181 my $tr_pop_id = $c->stash->{training_pop_id
};
182 my $sel_pop_id = $c->stash->{selection_pop_id
};
183 my $protocol_id = $c->stash->{genotyping_protocol_id
};
185 my $file_id = "${tr_pop_id}";
186 $file_id .= "-${sel_pop_id}" if $sel_pop_id;
187 $file_id .= "-GP-${protocol_id}";
189 my $cache_data = { key
=> 'genotype_filtering_log' . $file_id,
190 file
=> 'genotype_filtering_log_' . $file_id,
191 stash_key
=> 'genotype_filtering_log_file',
192 cache_dir
=> $c->stash->{solgs_cache_dir
}
195 $self->cache_file($c, $cache_data);
199 sub filtered_selection_genotype_file
{
202 my $tr_pop_id = $c->stash->{training_pop_id
};
203 my $sel_pop_id = $c->stash->{selection_pop_id
};
204 my $protocol_id = $c->stash->{genotyping_protocol_id
};
205 my $file_id = "${tr_pop_id}-${sel_pop_id}-GP-${protocol_id}";
207 my $cache_data = { key
=> 'filtered_genotype_data_' . $file_id,
208 file
=> 'filtered_selection_genotype_data_' . $file_id,
209 stash_key
=> 'filtered_selection_genotype_file',
210 cache_dir
=> $c->stash->{solgs_cache_dir
}
213 $self->cache_file($c, $cache_data);
217 sub formatted_phenotype_file
{
218 my ($self, $c, $pop_id) = @_;
220 $pop_id = $c->stash->{pop_id
} if $pop_id;
221 $pop_id = $c->{stash
}->{combo_pops_id
} if !$pop_id;
223 my $cache_data = { key
=> 'formatted_phenotype_data_' . $pop_id,
224 file
=> 'formatted_phenotype_data_' . $pop_id,
225 stash_key
=> 'formatted_phenotype_file',
226 cache_dir
=> $c->stash->{solgs_cache_dir
}
229 $self->cache_file($c, $cache_data);
233 sub phenotype_file_name
{
234 my ($self, $c, $pop_id) = @_;
236 $pop_id = $c->stash->{training_pop_id
} || $c->{stash
}->{combo_pops_id
} if !$pop_id;
239 if ($pop_id =~ /list/)
241 $dir = $c->stash->{solgs_lists_dir
};
243 elsif ($pop_id =~ /dataset/)
245 $dir = $c->stash->{solgs_datasets_dir
};
249 $dir = $c->stash->{solgs_cache_dir
};
252 my $cache_data = { key
=> 'phenotype_data_' . $pop_id,
253 file
=> 'phenotype_data_' . $pop_id,
254 stash_key
=> 'phenotype_file_name',
258 $self->cache_file($c, $cache_data);
260 return $c->stash->{phenotype_file_name
};
265 sub analysis_error_file
{
268 my $type = $c->stash->{analysis_type
};
269 my $cache_dir = $c->stash->{cache_dir
} || $c->stash->{solgs_cache_dir
};
270 my $file_id = $c->stash->{file_id
};
272 my $name = "${type}_error_${file_id}";
274 my $cache_data = { key
=> $name,
276 cache_dir
=> $cache_dir,
277 stash_key
=> "${type}_error_file",
280 $self->cache_file($c, $cache_data);
285 sub analysis_report_file
{
288 my $page_type = $c->controller('solGS::Path')->page_type($c, $c->req->referer);
289 my $analysis_type = $c->stash->{analysis_type
} || $page_type;
290 $analysis_type =~ s/\s+/_/g;
292 my $cache_dir = $c->stash->{cache_dir
} || $c->stash->{solgs_cache_dir
};
293 my $file_id = $c->stash->{file_id
};
297 my $pop_id = $c->stash->{training_pop_id
} || $c->stash->{combo_pops_id
};
298 my $trait_abbr = $c->stash->{trait_abbr
};
299 my $protocol_id = $c->stash->{genotyping_protocol_id
};
301 if ($analysis_type =~ /selection_prediction/)
303 $pop_id .= "_" . $c->stash->{selection_pop_id
};
306 $file_id = "${pop_id}-${trait_abbr}-GP-${protocol_id}";
309 my $name = "${analysis_type}_report_${file_id}";
311 my $cache_data = { key
=> $name,
313 cache_dir
=> $cache_dir,
314 stash_key
=> "${analysis_type}_report_file",
317 $self->cache_file($c, $cache_data);
322 sub genotype_file_name
{
323 my ($self, $c, $pop_id, $protocol_id) = @_;
325 $protocol_id = $c->stash->{genotyping_protocol_id
} if !$protocol_id;
327 $c->controller('solGS::genotypingProtocol')->stash_protocol_id($c, $protocol_id);
328 $protocol_id = $c->stash->{genotyping_protocol_id
};
331 if ($pop_id =~ /list/)
333 $dir = $c->stash->{solgs_lists_dir
};
335 elsif ($pop_id =~ /dataset/)
337 $dir = $c->stash->{solgs_datasets_dir
};
341 $dir = $c->stash->{solgs_cache_dir
};
344 my $file_id = $pop_id . '-GP-' . $protocol_id;
346 my $cache_data = { key
=> 'genotype_data_' . $file_id,
347 file
=> 'genotype_data_' . $file_id,
348 stash_key
=> 'genotype_file_name',
352 $self->cache_file($c, $cache_data);
357 sub relationship_matrix_file
{
360 my $file_id = $self->kinship_file_id($c);
361 no warnings
'uninitialized';
363 my $cache_data = {key
=> 'relationship_matrix_table_' . $file_id ,
364 file
=> 'relationship_matrix_table_' . $file_id,
365 stash_key
=> 'relationship_matrix_table_file',
366 cache_dir
=> $c->stash->{kinship_cache_dir
}
369 $self->cache_file($c, $cache_data);
371 $cache_data = {key
=> 'relationship_matrix_json_' . $file_id ,
372 file
=> 'relationship_matrix_json_' . $file_id,
373 stash_key
=> 'relationship_matrix_json_file',
374 cache_dir
=> $c->stash->{kinship_cache_dir
}
377 $self->cache_file($c, $cache_data);
382 sub relationship_matrix_adjusted_file
{
385 my $file_id = $self->kinship_file_id($c);
387 my $cache_data = {key
=> 'relationship_matrix_adjusted_table_' . $file_id ,
388 file
=> 'relationship_matrix_adjusted_table_' . $file_id,
389 stash_key
=> 'relationship_matrix_adjusted_table_file',
390 cache_dir
=> $c->stash->{kinship_cache_dir
}
393 $self->cache_file($c, $cache_data);
395 $cache_data = {key
=> 'relationship_matrix_adjusted_json_' . $file_id ,
396 file
=> 'relationship_matrix_adjusted_json_' . $file_id,
397 stash_key
=> 'relationship_matrix_adjusted_json_file',
398 cache_dir
=> $c->stash->{kinship_cache_dir
}
401 $self->cache_file($c, $cache_data);
405 sub kinship_file_id
{
408 my $pop_id = $c->stash->{kinship_pop_id
} || $c->stash->{training_pop_id
};
409 my $protocol_id = $c->stash->{genotyping_protocol_id
};
410 my $trait_abbr = $c->stash->{trait_abbr
};
412 my $file_id = $trait_abbr ?
413 "${pop_id}_${trait_abbr}_GP_${protocol_id}" :
414 "${pop_id}_GP_${protocol_id}";
419 sub average_kinship_file
{
422 my $file_id = $self->kinship_file_id($c);
424 my $cache_data = {key
=> 'average_kinship_file' . $file_id ,
425 file
=> 'average_kinship_file_' . $file_id,
426 stash_key
=> 'average_kinship_file',
427 cache_dir
=> $c->stash->{kinship_cache_dir
}
430 $self->cache_file($c, $cache_data);
435 sub inbreeding_coefficients_file
{
438 my $file_id = $self->kinship_file_id($c);
440 no warnings
'uninitialized';
442 my $cache_data = {key
=> 'inbreeding_coefficients' . $file_id ,
443 file
=> 'inbreeding_coefficients_' . $file_id,
444 stash_key
=> 'inbreeding_coefficients_file',
445 cache_dir
=> $c->stash->{kinship_cache_dir
}
449 $self->cache_file($c, $cache_data);
454 sub validation_file
{
457 my $pop_id = $c->stash->{training_pop_id
} || $c->stash->{pop_id
};
458 my $trait = $c->stash->{trait_abbr
};
460 my $protocol_id = $c->stash->{genotyping_protocol_id
};
461 my $file_id = $pop_id . '-' . $trait . '-GP-' . $protocol_id;
464 my $data_set_type = $c->stash->{data_set_type
};
465 no warnings
'uninitialized';
469 key
=> 'cross_validation_' . $file_id,
470 file
=> 'cross_validation_' . $file_id,
471 stash_key
=> 'validation_file',
472 cache_dir
=> $c->stash->{solgs_cache_dir
}
475 $self->cache_file($c, $cache_data);
479 sub combined_gebvs_file
{
480 my ($self, $c, $identifier) = @_;
482 my $pop_id = $c->stash->{pop_id
};
485 key
=> 'selected_traits_gebv_' . $pop_id . '_' . $identifier,
486 file
=> 'selected_traits_gebv_' . $pop_id . '_' . $identifier,
487 stash_key
=> 'selected_traits_gebv_file',
488 cache_dir
=> $c->stash->{solgs_cache_dir
}
491 $self->cache_file($c, $cache_data);
496 sub trait_phenotype_file
{
497 my ($self, $c, $pop_id, $trait) = @_;
499 my $protocol_id = $c->stash->{genotyping_protocol_id
};
501 my $dir = $c->stash->{solgs_cache_dir
};
502 my $exp = "phenotype_data_${trait}_${pop_id}";
503 my $file = $self->grep_file($dir, $exp);
505 $c->stash->{trait_phenotype_file
} = $file;
510 sub all_traits_file
{
511 my ($self, $c, $pop_id) = @_;
513 $pop_id = $c->stash->{pop_id
} || $c->stash->{training_pop_id
} if !$pop_id;
515 my $cache_data = {key
=> 'all_traits_pop' . $pop_id,
516 file
=> 'all_traits_pop_' . $pop_id,
517 stash_key
=> 'all_traits_file',
518 cache_dir
=> $c->stash->{solgs_cache_dir
}
521 $self->cache_file($c, $cache_data);
526 sub traits_list_file
{
527 my ($self, $c, $pop_id) = @_;
529 $pop_id = $c->stash->{pop_id
} || $c->stash->{training_pop_id
} if !$pop_id;
531 my $cache_data = {key
=> 'traits_list_pop' . $pop_id,
532 file
=> 'traits_list_pop_' . $pop_id,
533 stash_key
=> 'traits_list_file',
534 cache_dir
=> $c->stash->{solgs_cache_dir
}
537 $self->cache_file($c, $cache_data);
542 sub population_metadata_file
{
543 my ($self, $c, $dir, $file_id) = @_;
548 if ($c->stash->{list_id
})
550 my $list = CXGN
::List
->new({ dbh
=> $c->dbc()->dbh(),
551 list_id
=> $c->stash->{list_id
}
554 $owner_id = $list->owner;
557 elsif ($c->stash->{dataset_id
})
559 $owner_id = $c->controller('solGS::Search')->model($c)->get_dataset_owner($c->stash->{dataset_id
});
562 my $person = CXGN
::People
::Person
->new($c->dbc()->dbh(), $owner_id);
563 $user_id = $person->get_username();
565 my $cache_data = {key
=> "metadata_${user_id}_${file_id}",
566 file
=> "metadata_${user_id}_${file_id}",
567 stash_key
=> 'population_metadata_file',
571 $self->cache_file($c, $cache_data);
576 sub phenotype_metadata_file
{
579 my $cache_data = {key
=> 'phenotype_metadata',
580 file
=> 'phenotype_metadata',
581 stash_key
=> 'phenotype_metadata_file',
582 cache_dir
=> $c->stash->{solgs_cache_dir
}
585 $self->cache_file($c, $cache_data);
590 sub rrblup_training_gebvs_file
{
591 my ($self, $c, $training_pop_id, $trait_id, $protocol_id) = @_;
593 my $type = 'training';
594 my $file_id = $self->gebvs_file_id($c, $type);
596 my $cache_data = {key
=> 'rrblup_training_gebvs_' . $file_id,
597 file
=> 'rrblup_training_gebvs_' . $file_id,
598 stash_key
=> 'rrblup_training_gebvs_file',
599 cache_dir
=> $c->stash->{solgs_cache_dir
}
602 $self->cache_file($c, $cache_data);
608 my ($self, $c, $type) = @_;
610 my $training_pop_id = $c->stash->{training_pop_id
};
611 my $selection_pop_id = $c->stash->{selection_pop_id
};
612 my $trait_id = $c->stash->{trait_id
};
613 my $protocol_id = $c->stash->{genotyping_protocol_id
};
615 my $identifier = $training_pop_id;
617 if ($type =~ /selection/)
619 $identifier = "${identifier}-${selection_pop_id}";
622 $c->controller('solGS::Trait')->get_trait_details($c, $trait_id);
623 my $trait_abbr = $c->stash->{trait_abbr
};
625 my $file_id = "${identifier}-${trait_abbr}-GP-${protocol_id}";
630 sub rrblup_selection_gebvs_file
{
631 my ($self, $c, $training_pop_id, $selection_pop_id, $trait_id, $protocol_id) = @_;
633 my $type = 'selection';
634 $c->stash->{selection_pop_id
} = $selection_pop_id if $selection_pop_id;
635 $c->stash->{training_pop_id
} = $training_pop_id if $training_pop_id;
637 my $file_id = $self->gebvs_file_id($c, $type);
639 my $cache_data = {key
=> 'rrblup_selection_gebvs_' . $file_id,
640 file
=> 'rrblup_selection_gebvs_' . $file_id,
641 stash_key
=> 'rrblup_selection_gebvs_file',
642 cache_dir
=> $c->stash->{solgs_cache_dir
}
645 $self->cache_file($c, $cache_data);
650 sub list_of_prediction_pops_file
{
651 my ($self, $c, $training_pop_id)= @_;
653 my $protocol_id = $c->stash->{genotyping_protocol_id
};
654 my $file_id = $training_pop_id . '-GP-' . $protocol_id;
656 my $cache_data = {key
=> 'list_of_prediction_pops_' . $file_id,
657 file
=> 'list_of_prediction_pops_' . $file_id,
658 stash_key
=> 'list_of_prediction_pops_file',
659 cache_dir
=> $c->stash->{solgs_cache_dir
}
662 $self->cache_file($c, $cache_data);
667 sub first_stock_genotype_file
{
668 my ($self, $c, $pop_id, $protocol_id) = @_;
670 $protocol_id = $c->stash->{genotyping_protocol_id
} if !$protocol_id;
672 my $file_id = $pop_id . '-GP-' . $protocol_id;
674 my $cache_data = {key
=> 'first_stock_genotype_file_'. $file_id,
675 file
=> 'first_stock_genotype_file_' . $file_id,
676 stash_key
=> 'first_stock_genotype_file',
677 cache_dir
=> $c->stash->{solgs_cache_dir
}
680 $self->cache_file($c, $cache_data);
685 sub selection_population_file
{
686 my ($self, $c, $selection_pop_id) = @_;
688 my $tmp_dir = $c->stash->{solgs_tempfiles_dir
};
690 my $file = "selection_population_file_${selection_pop_id}";
691 my $tempfile = $self->create_tempfile($tmp_dir, $file);
693 $c->stash->{selection_pop_id
} = $selection_pop_id;
694 $self->filtered_selection_genotype_file($c);
695 my $filtered_geno_file = $c->stash->{filtered_selection_genotype_file
};
697 my $geno_files = $filtered_geno_file;
699 $self->genotype_file_name($c, $selection_pop_id);
700 $geno_files .= "\t" . $c->stash->{genotype_file_name
};
702 write_file
($tempfile, {binmode => ':utf8'}, $geno_files);
704 $c->stash->{selection_population_file
} = $tempfile;
709 sub traits_acronym_file
{
710 my ($self, $c, $pop_id) = @_;
712 my $cache_data = {key
=> 'traits_acronym_pop' . $pop_id,
713 file
=> 'traits_acronym_pop_' . $pop_id,
714 stash_key
=> 'traits_acronym_file',
715 cache_dir
=> $c->stash->{solgs_cache_dir
}
718 $self->cache_file($c, $cache_data);
724 my ($self, $file) = @_;
729 return catfile
($dir, $file);
735 my ($self, $c, $cache_data) = @_;
737 my $cache_dir = $cache_data->{cache_dir
} || $c->stash->{cache_dir
} || $c->stash->{solgs_cache_dir
};
739 my $file_cache = Cache
::File
->new(cache_root
=> $cache_dir,
740 lock_level
=> Cache
::File
::LOCK_NFS
()
743 $file_cache->purge();
745 my $file = $file_cache->get($cache_data->{key
});
747 no warnings
'uninitialized';
751 $file = catfile
($cache_dir, $cache_data->{file
});
753 if ($file !~ /(\.\w+)/)
755 my $ext = $cache_data->{ext
};
758 if ($ext !~ /^\./) { $ext = '.' . $ext}
764 $file = $file . $ext;
766 write_file
($file, {binmode => ':utf8'});
767 $file_cache->set($cache_data->{key
}, $file, '30 days');
770 $c->stash->{$cache_data->{stash_key
}} = $file;
774 sub copy_to_tempfiles_subdir
{
775 my ($self, $c, $file, $dir_name) = @_;
777 my $tmp_dir = catfile
($c->config->{tempfiles_subdir
}, $dir_name);
778 my $base_tmp_dir = catfile
($c->config->{basepath
}, $tmp_dir);
780 mkpath
([$base_tmp_dir], 0, 0755);
782 $self->copy_file($file, $base_tmp_dir);
783 $file = catfile
($tmp_dir, basename
($file));
789 sub convert_txt_pdf
{
790 my ($self, $file, $title) = @_;
792 my $content = read_file
($file, {binmode => ':utf8'});
793 my $pdf_file = $file;
794 $pdf_file =~ s/txt/pdf/;
796 my $pdf = PDF
::Create
->new(
797 'filename' => $pdf_file,
798 'Author' => 'solGS M Tool',
799 'Title' => $title || 'Analysis log',
800 'CreationDate' => [localtime]
803 my $root = $pdf->new_page('MediaBox' => $pdf->get_page_size('A4'));
804 my $page = $root->new_page;
805 my $font = $pdf->font('BaseFont' => 'Courier');
807 $page->printnl($content, $font);
817 my $training_pop_id = $c->stash->{training_pop_id
};
818 my $selection_pop_id = $c->stash->{selection_pop_id
};
819 my $data_structure = $c->stash->{data_structure
};
820 my $list_id = $c->stash->{list_id
};
821 my $list_type = $c->stash->{list_type
};
822 my $dataset_id = $c->stash->{dataset_id
};
823 my $cluster_type = $c->stash->{cluster_type
};
824 my $combo_pops_id = $c->stash->{combo_pops_id
};
825 my $data_type = $c->stash->{data_type
};
826 my $k_number = $c->stash->{k_number
};
827 my $sindex_name = $c->stash->{sindex_weigths
} || $c->stash->{sindex_name
};
828 my $sel_prop = $c->stash->{selection_proportion
};
829 my $protocol_id = $c->stash->{genotyping_protocol_id
};
830 my $cluster_pop_id = $c->stash->{cluster_pop_id
};
831 my $pca_pop_id = $c->stash->{pca_pop_id
};
832 my $training_traits_code = $c->stash->{training_traits_code
};
834 $c->controller('solGS::genotypingProtocol')->stash_protocol_id($c, $protocol_id);
835 $protocol_id = $c->stash->{genotyping_protocol_id
};
837 my $traits_ids = $c->stash->{training_traits_ids
};
838 my @traits_ids = ref($traits_ids) eq 'ARRAY' ? @
{$traits_ids} : ($c->stash->{trait_id
});
841 if (scalar(@traits_ids == 1))
843 $trait_id = $traits_ids[0];
847 my $referer = $c->req->referer;
849 my $selection_pages = 'solgs\/selection\/'
850 . '|solgs\/combined\/model\/\d+\/selection\/'
851 . '|/solgs\/traits\/all\/population\/'
852 . '|solgs\/models\/combined\/trials\/';
855 if ($referer =~ /cluster\/analysis\
/|\/solgs\
/model\/combined\
/populations\// && $combo_pops_id)
857 $c->controller('solGS::combinedTrials')->get_combined_pops_list($c, $combo_pops_id);
858 $c->stash->{pops_ids_list
} = $c->stash->{combined_pops_list
};
859 $file_id = $combo_pops_id;
860 $c->stash->{data_set_type
} = 'combined_populations';
862 elsif ($referer =~ /$selection_pages/)
864 if ($selection_pop_id)
866 $file_id = $selection_pop_id && $selection_pop_id !~ /^$training_pop_id$/ ?
867 $training_pop_id . '-' . $selection_pop_id :
872 $file_id = $cluster_pop_id && $cluster_pop_id != $training_pop_id ?
873 $training_pop_id . '-' . $cluster_pop_id :
879 $file_id = $training_pop_id || $cluster_pop_id || $pca_pop_id;
882 if ($c->req->referer =~ /cluster|pca|kinship/)
884 if ($data_structure =~ /list/)
886 $file_id = "list_${list_id}" if $list_id;
888 elsif ($data_structure =~ /dataset/)
890 $file_id = "dataset_${dataset_id}" if $dataset_id;
896 if ($sindex_name ne $selection_pop_id)
898 $file_id .= $sindex_name ?
'-' . $sindex_name : "";
902 if (!$sindex_name && $training_traits_code)
904 $file_id .= '-traits-' . $training_traits_code;
907 if (!$training_traits_code && $trait_id)
909 $file_id = $file_id . '-' . $trait_id;
912 $file_id = $data_type ?
$file_id . '-' . lc($data_type) : $file_id;
913 if ($cluster_type !~ /hierarchical/i)
915 $file_id = $k_number ?
$file_id . '-k-' . $k_number : $file_id;
917 $file_id = $protocol_id && $data_type =~ /genotype/i ?
$file_id . '-gp-' . $protocol_id : $file_id;
921 $file_id = $sel_prop ?
$file_id . '-sp-' . $sel_prop : $file_id;
929 sub format_cluster_output_url
{
930 my ($self, $c, $path) = @_;
932 my $pop_id = $c->stash->{pop_id
};
934 my $host = $c->req->base;
936 if ( $host !~ /localhost/)
939 $host =~ s/http\w?/https/;
942 my $end = substr($path, -1, 1);
943 my $front = substr($path, 0, 1);
945 $path = $path . '/' if $end !~ /\
//;
946 $path =~ s/\/// if $front =~ /\
//;
948 my $output_link = $host . $path . $pop_id;
954 sub create_tempfile
{
955 my ($self, $dir, $name, $ext) = @_;
957 $ext = '.' . $ext if $ext;
959 my ($fh, $file) = tempfile
($name . "-XXXXX",
972 my ($self, $file, $dir) = @_;
974 mkpath
($dir, 0, 755);
977 or die "could not copy $file to $dir";
979 return catfile
($dir, basename
($file));
984 my ($self, $dir, $exp) = @_;
987 or die "can't open $dir: $!\n";
989 my ($file) = grep { /^$exp/ && -f
"$dir/$_" } readdir($dh);
994 $file = catfile
($dir, $file);
1001 sub get_solgs_dirs
{
1002 my ($self, $c) = @_;
1004 my $geno_version = $c->config->{default_genotyping_protocol
};
1005 $geno_version = 'analysis-data' if ($geno_version =~ /undefined/) || !$geno_version;
1006 $geno_version =~ s/\s+//g;
1007 my $tmp_dir = $c->site_cluster_shared_dir;
1008 $tmp_dir = catdir
($tmp_dir, $geno_version);
1009 my $solgs_dir = catdir
($tmp_dir, "solgs");
1010 my $solgs_cache = catdir
($tmp_dir, 'solgs', 'cache');
1011 my $solgs_tempfiles = catdir
($tmp_dir, 'solgs', 'tempfiles');
1012 my $solqtl_cache = catdir
($tmp_dir, 'solqtl', 'cache');
1013 my $solqtl_tempfiles = catdir
($tmp_dir, 'solqtl', 'tempfiles');
1014 my $solgs_lists = catdir
($tmp_dir, 'solgs', 'tempfiles', 'lists');
1015 my $solgs_datasets = catdir
($tmp_dir, 'solgs', 'tempfiles', 'datasets');
1016 my $histogram_cache = catdir
($tmp_dir, 'histogram', 'cache');
1017 my $histogram_temp = catdir
($tmp_dir, 'histogram', 'tempfiles');
1018 my $log_dir = catdir
($tmp_dir, 'log', 'cache');
1019 my $anova_cache = catdir
($tmp_dir, 'anova', 'cache');
1020 my $anova_temp = catdir
($tmp_dir, 'anova', 'tempfiles');
1021 my $corre_cache = catdir
($tmp_dir, 'correlation', 'cache');
1022 my $corre_temp = catdir
($tmp_dir, 'correlation', 'tempfiles');
1023 my $h2_cache = catdir
($tmp_dir, 'heritability', 'cache');
1024 my $h2_temp = catdir
($tmp_dir, 'heritability', 'tempfiles');
1025 my $qc_cache = catdir
($tmp_dir, 'qualityControl', 'cache');
1026 my $qc_temp = catdir
($tmp_dir, 'qualityControl', 'tempfiles');
1027 my $pca_cache = catdir
($tmp_dir, 'pca', 'cache');
1028 my $pca_temp = catdir
($tmp_dir, 'pca', 'tempfiles');
1029 my $cluster_cache = catdir
($tmp_dir, 'cluster', 'cache');
1030 my $cluster_temp = catdir
($tmp_dir, 'cluster', 'tempfiles');
1031 my $sel_index_cache = catdir
($tmp_dir, 'selectionIndex', 'cache');
1032 my $sel_index_temp = catdir
($tmp_dir, 'selectionIndex', 'tempfiles');
1033 my $kinship_cache = catdir
($tmp_dir, 'kinship', 'cache');
1034 my $kinship_temp = catdir
($tmp_dir, 'kinship', 'tempfiles');
1038 $solgs_dir, $solgs_cache, $solgs_tempfiles, $solgs_lists, $solgs_datasets,
1039 $pca_cache, $pca_temp, $histogram_cache, $histogram_temp, $log_dir, $corre_cache, $corre_temp,
1040 $h2_temp, $h2_cache, $qc_cache, $qc_temp, $anova_temp,$anova_cache, $solqtl_cache, $solqtl_tempfiles,
1041 $cluster_cache, $cluster_temp, $sel_index_cache, $sel_index_temp, $kinship_cache, $kinship_temp
1046 $c->stash(solgs_dir
=> $solgs_dir,
1047 solgs_cache_dir
=> $solgs_cache,
1048 solgs_tempfiles_dir
=> $solgs_tempfiles,
1049 solgs_lists_dir
=> $solgs_lists,
1050 solgs_datasets_dir
=> $solgs_datasets,
1051 pca_cache_dir
=> $pca_cache,
1052 pca_temp_dir
=> $pca_temp,
1053 cluster_cache_dir
=> $cluster_cache,
1054 cluster_temp_dir
=> $cluster_temp,
1055 correlation_cache_dir
=> $corre_cache,
1056 correlation_temp_dir
=> $corre_temp,
1057 heritability_cache_dir
=> $h2_cache,
1058 heritability_temp_dir
=> $h2_temp,
1059 qualityControl_cache_dir
=> $qc_cache,
1060 qualityControl_temp_dir
=> $qc_temp,
1061 histogram_cache_dir
=> $histogram_cache,
1062 histogram_temp_dir
=> $histogram_temp,
1063 analysis_log_dir
=> $log_dir,
1064 anova_cache_dir
=> $anova_cache,
1065 anova_temp_dir
=> $anova_temp,
1066 solqtl_cache_dir
=> $solqtl_cache,
1067 solqtl_tempfiles_dir
=> $solqtl_tempfiles,
1068 cache_dir
=> $solgs_cache,
1069 selection_index_cache_dir
=> $sel_index_cache,
1070 selection_index_temp_dir
=> $sel_index_temp,
1071 kinship_cache_dir
=> $kinship_cache,
1072 kinship_temp_dir
=> $kinship_temp