1 package SGN
::Controller
::solGS
::Histogram
;
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 /;
11 use File
::Temp qw
/ tempfile tempdir /;
13 use Storable qw
/ nstore retrieve /;
15 BEGIN { extends
'Catalyst::Controller' }
18 sub trait_pheno_means_data
:Path
('/trait/pheno/means/data/') Args
(0) {
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';
38 $ret->{status
} = 'success';
43 $c->res->content_type('application/json');
48 sub trait_pheno_raw_data
:Path
('/trait/pheno/raw/data/') Args
(0) {
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';
68 $ret->{status
} = 'success';
73 $c->res->content_type('application/json');
79 sub get_trait_pheno_means_data
{
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);
94 sub get_trait_pheno_raw_data
{
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);
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
{
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')
147 $c->stash->{histogram_r_jobs
} = $jobs;
152 sub histogram_r_jobs_file
{
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
{
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",
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
{
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",
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
{
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
{
249 $c->controller('solGS::Files')->get_solgs_dirs($c);