can download plant phenotype data in the same way as plot phenotype data
[sgn.git] / lib / SGN / Controller / solGS / Histogram.pm
blobc8af9b4dbdbd817c1211b44b36735c9ec93f9184
1 package SGN::Controller::solGS::Histogram;
3 use Moose;
4 use namespace::autoclean;
5 use CXGN::Tools::Run;
6 use File::Spec::Functions qw / catfile catdir/;
7 use File::Path qw / mkpath /;
8 use File::Copy;
9 use File::Basename;
10 use File::Temp qw / tempfile tempdir /;
11 use JSON;
12 use Try::Tiny;
14 BEGIN { extends 'Catalyst::Controller' }
17 sub histogram_phenotype_data :Path('/histogram/phenotype/data/') Args(0) {
18 my ($self, $c) = @_;
20 my $pop_id = $c->req->param('population_id');
21 my $trait_id = $c->req->param('trait_id');
22 my $referer = $c->req->referer;
24 if ($referer =~ /combined/)
26 $c->stash->{data_set_type} = 'combined populations';
27 $c->stash->{combo_pops_id} = $pop_id;
30 $c->stash->{pop_id} = $pop_id;
32 $c->controller('solGS::solGS')->get_trait_details($c, $trait_id);
33 my $trait_abbr = $c->stash->{trait_abbr};
35 $c->controller('solGS::solGS')->trait_phenodata_file($c);
36 my $trait_pheno_file = $c->stash->{trait_phenodata_file};
37 $c->stash->{histogram_trait_file} = $c->stash->{trait_phenodata_file};
39 if (!$trait_pheno_file || -z $trait_pheno_file)
41 $self->create_population_phenotype_data($c);
44 unless (!$c->stash->{phenotype_file} || -s $trait_pheno_file)
46 $self->create_trait_phenodata($c);
49 my $data = $self->format_plot_data($c);
51 $c->controller('solGS::solGS')->trait_phenotype_stat($c);
52 my $stat = $c->stash->{descriptive_stat};
54 my $ret->{status} = 'failed';
56 if (@$data)
58 $ret->{data} = $data;
59 $ret->{stat} = $stat;
60 $ret->{status} = 'success';
63 $ret = to_json($ret);
65 $c->res->content_type('application/json');
66 $c->res->body($ret);
71 sub format_plot_data {
72 my ($self, $c) = @_;
74 my $file = $c->stash->{histogram_trait_file};
75 my $data = $c->controller('solGS::solGS')->convert_to_arrayref_of_arrays($c, $file);
77 return $data;
82 sub create_population_phenotype_data {
83 my ($self, $c) = @_;
85 $c->controller("solGS::solGS")->phenotype_file($c);
90 sub create_histogram_dir {
91 my ($self, $c) = @_;
93 $c->controller("solGS::solGS")->get_solgs_dirs($c);
97 sub create_trait_phenodata {
98 my ($self, $c) = @_;
100 my $combo_id = $c->stash->{combo_pops_id};
102 my $pop_id = $c->stash->{pop_id} ? $c->stash->{pop_id} : $c->stash->{combo_pops_id};
104 $self->create_histogram_dir($c);
105 my $histogram_dir = $c->stash->{histogram_dir};
107 my $pheno_file = $c->stash->{phenotype_file};
108 my $trait_file = $c->controller("solGS::solGS")->trait_phenodata_file($c);
109 my $trait_abbr = $c->stash->{trait_abbr};
111 if (-s $pheno_file)
113 CXGN::Tools::Run->temp_base($histogram_dir);
115 my ( $histogram_commands_temp, $histogram_output_temp ) =
118 my (undef, $filename ) =
119 tempfile(
120 catfile(
121 CXGN::Tools::Run->temp_base(),
122 "histogram_analysis_${pop_id}_${trait_abbr}-$_-XXXXXX",
125 $filename
126 } qw / in out /;
129 my $histogram_commands_file = $c->path_to('/R/histogram.r');
130 copy( $histogram_commands_file, $histogram_commands_temp )
131 or die "could not copy '$histogram_commands_file' to '$histogram_commands_temp'";
133 try
135 print STDERR "\nsubmitting histogram job to the cluster..\n";
136 my $r_process = CXGN::Tools::Run->run_cluster(
137 'R', 'CMD', 'BATCH',
138 '--slave',
139 "--args input_file=$pheno_file trait_name=$trait_abbr output_file=$trait_file",
140 $histogram_commands_temp,
141 $histogram_output_temp,
143 working_dir => $histogram_dir,
144 max_cluster_jobs => 1_000_000_000,
148 $r_process->wait;
149 print STDERR "\ndone with histogram analysis..\n";
151 catch
153 my $err = $_;
154 $err =~ s/\n at .+//s; #< remove any additional backtrace
155 # # try to append the R output
158 $err .= "\n=== R output ===\n".file($histogram_output_temp)->slurp."\n=== end R output ===\n"
161 $c->stash->{script_error} = "There is a problem running the histogram r script on this dataset.";
165 $c->stash->{histogram_trait_file} = $trait_file;
167 else
169 $c->stash->{script_error} = "There is no phenotype for this trait.";
175 sub begin : Private {
176 my ($self, $c) = @_;
178 $c->controller("solGS::solGS")->get_solgs_dirs($c);
181 ####
183 ####