1 package SGN
::Controller
::solGS
::Histogram
;
4 use namespace
::autoclean
;
6 use File
::Spec
::Functions qw
/ catfile catdir/;
7 use File
::Path qw
/ mkpath /;
10 use File
::Temp qw
/ tempfile tempdir /;
14 BEGIN { extends
'Catalyst::Controller' }
17 sub histogram_phenotype_data
:Path
('/histogram/phenotype/data/') Args
(0) {
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';
60 $ret->{status
} = 'success';
65 $c->res->content_type('application/json');
71 sub format_plot_data
{
74 my $file = $c->stash->{histogram_trait_file
};
75 my $data = $c->controller('solGS::solGS')->convert_to_arrayref_of_arrays($c, $file);
82 sub create_population_phenotype_data
{
85 $c->controller("solGS::solGS")->phenotype_file($c);
90 sub create_histogram_dir
{
93 $c->controller("solGS::solGS")->get_solgs_dirs($c);
97 sub create_trait_phenodata
{
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
};
113 CXGN
::Tools
::Run
->temp_base($histogram_dir);
115 my ( $histogram_commands_temp, $histogram_output_temp ) =
118 my (undef, $filename ) =
121 CXGN
::Tools
::Run
->temp_base(),
122 "histogram_analysis_${pop_id}_${trait_abbr}-$_-XXXXXX",
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'";
135 print STDERR
"\nsubmitting histogram job to the cluster..\n";
136 my $r_process = CXGN
::Tools
::Run
->run_cluster(
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
,
149 print STDERR
"\ndone with histogram analysis..\n";
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;
169 $c->stash->{script_error
} = "There is no phenotype for this trait.";
175 sub begin
: Private
{
178 $c->controller("solGS::solGS")->get_solgs_dirs($c);