can download plant phenotype data in the same way as plot phenotype data
[sgn.git] / lib / SGN / Controller / solGS / Heritability.pm
blob627083f4109775103b19f4ff19eff8a3be220afe
1 package SGN::Controller::solGS::Heritability;
3 use Moose;
4 use namespace::autoclean;
6 use File::Slurp qw /write_file read_file/;
7 use JSON;
8 use Math::Round::Var;
9 use Statistics::Descriptive;
12 BEGIN { extends 'Catalyst::Controller' }
15 sub check_regression_data :Path('/heritability/check/data/') Args(0) {
16 my ($self, $c) = @_;
18 my $pop_id = $c->req->param('population_id');
19 $c->stash->{pop_id} = $pop_id;
21 my $solgs_controller = $c->controller('solGS::solGS');
23 my $trait_id = $c->req->param('trait_id');
24 $solgs_controller->get_trait_details($c, $trait_id);
26 $self->get_regression_data_files($c);
28 my $ret->{exists} = undef;
30 my $gebv_file = $c->stash->{regression_gebv_file};
31 my $pheno_file = $c->stash->{regression_pheno_file};
33 if(-s $gebv_file && -s $pheno_file)
35 $ret->{exists} = 'yes';
38 $ret = to_json($ret);
40 $c->res->content_type('application/json');
41 $c->res->body($ret);
46 sub get_regression_data_files {
47 my ($self, $c) = @_;
49 my $pop_id = $c->stash->{pop_id};
50 my $trait_abbr = $c->stash->{trait_abbr};
51 my $cache_dir = $c->stash->{solgs_cache_dir};
53 my $solgs_controller = $c->controller('solGS::solGS');
55 my $phenotype_file = "phenotype_trait_${trait_abbr}_${pop_id}";
56 $phenotype_file = $solgs_controller->grep_file($cache_dir, $phenotype_file);
58 my $gebv_file = "gebv_kinship_${trait_abbr}_${pop_id}";
59 $gebv_file = $solgs_controller->grep_file($cache_dir, $gebv_file);
61 $c->stash->{regression_gebv_file} = $gebv_file;
62 $c->stash->{regression_pheno_file} = $phenotype_file;
67 sub get_heritability {
68 my ($self, $c) = @_;
70 my $trait_abbr = $c->stash->{trait_abbr};
71 my $pop_id = $c->stash->{pop_id};
73 my $solgs_controller = $c->controller('solGS::solGS');
74 my $cache_dir = $c->stash->{solgs_cache_dir};
76 $solgs_controller->variance_components_file($c);
77 my $var_comp_file = $c->stash->{variance_components_file};
79 my ($txt, $value) = map { split(/\t/) }
80 grep {/Heritability/}
81 read_file($var_comp_file);
83 $c->stash->{heritability} = $value;
87 sub heritability_regeression_data :Path('/heritability/regression/data/') Args(0) {
88 my ($self, $c) = @_;
90 my $pop_id = $c->req->param('population_id');
91 $c->stash->{pop_id} = $pop_id;
93 my $trait_id = $c->req->param('trait_id');
94 my $solgs_controller = $c->controller('solGS::solGS');
95 $solgs_controller->get_trait_details($c, $trait_id);
97 $self->get_regression_data_files($c);
99 my $gebv_file = $c->stash->{regression_gebv_file};
100 my $pheno_file = $c->stash->{regression_pheno_file};
102 my @gebv_data = map { $_ =~ s/\n//; $_ } read_file($gebv_file);
103 my @pheno_data = map { $_ =~ s/\n//; $_ } read_file($pheno_file);
105 @gebv_data = map { [ split(/\t/) ] } @gebv_data;
106 @pheno_data = map { [ split(/\t/) ] } @pheno_data;
108 my @pheno_values = map { $_->[1] } @pheno_data;
109 shift(@pheno_values);
110 shift(@gebv_data);
111 shift(@pheno_data);
113 my $stat = Statistics::Descriptive::Full->new();
114 $stat->add_data(@pheno_values);
115 my $pheno_mean = $stat->mean();
117 my $round = Math::Round::Var->new(0.01);
119 my @pheno_deviations = map { [$_->[0], $round->round(( $_->[1] - $pheno_mean ))] } @pheno_data;
121 $self->get_heritability($c);
122 my $heritability = $c->stash->{heritability};
124 my $ret->{status} = 'failed';
126 if (@gebv_data && @pheno_data)
128 $ret->{status} = 'success';
129 $ret->{gebv_data} = \@gebv_data;
130 $ret->{pheno_deviations} = \@pheno_deviations;
131 $ret->{pheno_data} = \@pheno_data;
132 $ret->{heritability} = $heritability;
136 $ret = to_json($ret);
138 $c->res->content_type('application/json');
139 $c->res->body($ret);
144 sub begin : Private {
145 my ($self, $c) = @_;
147 $c->controller("solGS::solGS")->get_solgs_dirs($c);
153 ####
155 ####