Merge pull request #5230 from solgenomics/topic/open_pollinated
[sgn.git] / lib / SGN / Controller / solGS / Histogram.pm
blob0077161a3858b45a54804ca69d00cff27f1320ce
1 package SGN::Controller::solGS::Histogram;
3 use Moose;
4 use namespace::autoclean;
6 use Carp qw/ carp confess croak /;
7 use File::Spec::Functions qw / catfile catdir/;
8 use File::Path qw / mkpath /;
9 use File::Copy;
10 use File::Basename;
11 use File::Temp qw / tempfile tempdir /;
12 use JSON;
13 use Storable qw/ nstore retrieve /;
15 BEGIN { extends 'Catalyst::Controller' }
18 sub trait_pheno_means_data :Path('/trait/pheno/means/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 my $trait_id = $c->stash->{'trait_id'};
25 $c->controller('solGS::Trait')->get_trait_details($c, $c->stash->{trait_id});
27 my $data = $self->get_trait_pheno_means_data($c);
28 my $raw_data = $self->get_trait_pheno_raw_data($c);
29 $c->controller('solGS::solGS')->model_phenotype_stat($c);
30 my $stat = $c->stash->{descriptive_stat};
32 my $ret->{status} = 'failed';
34 if (@$data)
36 $ret->{data} = $data;
37 $ret->{stat} = $stat;
38 $ret->{status} = 'success';
41 $ret = to_json($ret);
43 $c->res->content_type('application/json');
44 $c->res->body($ret);
48 sub trait_pheno_raw_data :Path('/trait/pheno/raw/data/') Args(0) {
49 my ($self, $c) = @_;
51 my $args = $c->req->param('arguments');
52 $c->controller('solGS::Utils')->stash_json_args($c, $args);
54 my $trait_id = $c->stash->{'trait_id'};
55 $c->controller('solGS::Trait')->get_trait_details($c, $c->stash->{trait_id});
57 my $data = $self->get_trait_pheno_raw_data($c);
59 $c->controller('solGS::solGS')->model_phenotype_stat($c);
60 my $stat = $c->stash->{descriptive_stat};
62 my $ret->{status} = 'failed';
64 if (@$data)
66 $ret->{data} = $data;
67 $ret->{stat} = $stat;
68 $ret->{status} = 'success';
71 $ret = to_json($ret);
73 $c->res->content_type('application/json');
74 $c->res->body($ret);
79 sub get_trait_pheno_means_data {
80 my ($self, $c) = @_;
82 my $trait_id = $c->stash->{trait_id};
83 $c->controller('solGS::Trait')->get_trait_details($c, $trait_id);
84 $c->controller('solGS::Files')->model_phenodata_file($c);
85 my $model_pheno_file = $c->stash->{model_phenodata_file};
87 my $data = $c->controller('solGS::Utils')->read_file_data($model_pheno_file);
89 return $data;
94 sub get_trait_pheno_raw_data {
95 my ($self, $c) = @_;
97 my $trait_id = $c->stash->{trait_id};
98 $c->controller('solGS::Trait')->get_trait_details($c, $trait_id);
99 $c->controller('solGS::Files')->trait_raw_phenodata_file($c);
100 my $trait_raw_pheno_file = $c->stash->{trait_raw_phenodata_file};
101 my $trait_abbr = $c->stash->{trait_abbr};
102 my @cols = ('observationUnitName', $trait_abbr);
103 my $data = $c->controller('solGS::Utils')->read_file_data_cols($trait_raw_pheno_file, \@cols);
105 return $data;
111 sub run_histogram {
112 my ($self, $c) = @_;
114 $self->histogram_r_jobs_file($c);
115 $c->stash->{dependent_jobs} = $c->stash->{histogram_r_jobs_file};
117 $c->controller('solGS::AsyncJob')->run_async($c);
122 sub histogram_r_jobs {
123 my ($self, $c) = @_;
125 my $pop_id = $c->stash->{pop_id} ? $c->stash->{pop_id} : $c->stash->{combo_pops_id};
126 my $trait_abbr = $c->stash->{trait_abbr};
128 $c->stash->{analysis_tempfiles_dir} = $c->stash->{histogram_temp_dir};
130 my $input_file = $self->histogram_input_files($c);
131 my $output_file = $self->histogram_output_files($c);
132 #my $trait_file = $c->controller('solGS::Files')->model_phenodata_file($c);
134 $c->stash->{r_temp_file} = "histogram-data-${pop_id}-${trait_abbr}";
135 $c->stash->{r_script} = 'R/solGS/histogram.r';
136 $c->stash->{input_file} = $input_file;
137 $c->stash->{output_file} = $output_file;
139 $c->controller('solGS::AsyncJob')->get_cluster_r_job_args($c);
140 my $jobs = $c->stash->{cluster_r_job_args};
142 if (reftype $jobs ne 'ARRAY')
144 $jobs = [$jobs];
147 $c->stash->{histogram_r_jobs} = $jobs;
152 sub histogram_r_jobs_file {
153 my ($self, $c) = @_;
155 $self->histogram_r_jobs($c);
156 my $jobs = $c->stash->{histogram_r_jobs};
158 my $temp_dir = $c->stash->{histogram_temp_dir};
159 my $jobs_file = $c->controller('solGS::Files')->create_tempfile($temp_dir, 'histo-r-jobs-file');
161 nstore $jobs, $jobs_file
162 or croak "histogram r jobs : $! serializing histogram r jobs to $jobs_file";
164 $c->stash->{histogram_r_jobs_file} = $jobs_file;
169 sub histogram_input_files {
170 my ($self, $c) = @_;
172 my $pop_id = $c->stash->{pop_id} || $c->stash->{combo_pops_id};
173 my $trait_id = $c->stash->{trait_id};
175 $c->controller('solGS::Files')->phenotype_file_name($c);
176 my $pheno_file = $c->stash->{phenotype_file_name};
178 $self->histogram_traits_file($c);
179 my $traits_file = $c->stash->{histogram_traits_file};
181 $c->controller("solGS::Files")->phenotype_metadata_file($c);
182 my $metadata_file = $c->stash->{phenotype_metadata_file};
184 my $file_list = join ("\t",
185 $pheno_file,
186 $traits_file,
187 $metadata_file
190 my $tmp_dir = $c->stash->{histogram_temp_dir};
191 my $name = "histogram_input_files_${pop_id}_${trait_id}";
192 my $tempfile = $c->controller('solGS::Files')->create_tempfile($tmp_dir, $name);
193 write_file($tempfile, {binmode => ':utf8'}, $file_list);
195 $c->stash->{histogram_input_files} = $tempfile;
200 sub histogram_output_files {
201 my ($self, $c) = @_;
203 my $pop_id = $c->stash->{training_pop_id} || $c->stash->{combo_pops_id};
204 my $trait_id = $c->stash->{trait_id};
206 $c->controller('solGS::Trait')->get_trait_details($c, $trait_id);
208 $c->controller('solGS::Files')->trait_raw_phenodata_file($c);
209 my $raw_pheno_file = $c->stash->{trait_raw_phenodata_file};
211 my $means_pheno_file = $c->controller('solGS::Files')->model_phenodata_file($c);
213 my $file_list = join ("\t",
214 $raw_pheno_file,
215 $means_pheno_file,
218 my $tmp_dir = $c->stash->{histogram_temp_dir};
219 my $name = "histogram_output_files_${pop_id}_${trait_id}";
220 my $tempfile = $c->controller('solGS::Files')->create_tempfile($tmp_dir, $name);
221 write_file($tempfile, {binmode => ':utf8'}, $file_list);
223 $c->stash->{histogram_output_files} = $tempfile;
228 sub histogram_traits_file {
229 my ($self, $c) = @_;
231 my $pop_id = $c->stash->{pop_id} || $c->stash->{combo_pops_id};
233 my $traits = $c->stash->{trait_abbr};
235 my $tmp_dir = $c->stash->{histogram_temp_dir};
236 my $name = "histogram_traits_file_${pop_id}";
237 my $traits_file = $c->controller('solGS::Files')->create_tempfile($tmp_dir, $name);
238 write_file($traits_file, {binmode => ':utf8'}, $traits);
240 $c->stash->{histogram_traits_file} = $traits_file;
246 sub begin : Private {
247 my ($self, $c) = @_;
249 $c->controller('solGS::Files')->get_solgs_dirs($c);
252 ####
254 ####