do not attempt get the person object when no one is logged in...
[sgn.git] / lib / CXGN / Phenotypes / TrialPhenotype.pm
blobc9b7c5f14439f9a492a9207bed2cc3d9736921b2
2 package CXGN::Phenotypes::TrialPhenotype;
5 =head1 NAME
7 CXGN::Phenotypes::TrialPhenotype - an object to handle retrieving of trial phenotype and field information.
9 =head1 USAGE
11 my $phenotypes_heatmap = CXGN::Phenotypes::TrialPhenotype->new(
12 bcs_schema=>$schema,
13 trial_id=>$trial_id,
14 trait_id=>$trait_id
16 my @phenotype = $phenotypes_heatmap->get_trial_phenotypes_heatmap();
18 =head1 DESCRIPTION
21 =head1 AUTHORS
24 =cut
28 use strict;
29 use warnings;
30 use Moose;
31 use Data::Dumper;
32 use SGN::Model::Cvterm;
33 use List::MoreUtils qw | :all !before !after |;
34 use CXGN::Trial;
36 BEGIN { extends 'Catalyst::Controller'; }
38 has 'bcs_schema' => (
39 isa => 'Bio::Chado::Schema',
40 is => 'rw',
41 required => 1,
44 has 'trial_id' => (
45 isa => 'Int',
46 is => 'rw',
47 required => 1,
50 has 'trait_id' => (
51 isa => 'Int',
52 is => 'rw',
53 required => 1,
56 sub get_trial_phenotypes_heatmap {
57 my $self = shift;
58 my $schema = $self->bcs_schema;
59 my $trial_id = $self->trial_id;
60 my $trait_id = $self->trait_id;
61 my $rep_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'replicate', 'stock_property')->cvterm_id();
62 my $block_number_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'block', 'stock_property')->cvterm_id();
63 my $plot_number_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'plot number', 'stock_property')->cvterm_id();
64 my $row_number_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'row_number', 'stock_property')->cvterm_id();
65 my $col_number_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'col_number', 'stock_property')->cvterm_id();
66 my $plot_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'plot', 'stock_type')->cvterm_id();
67 my $accession_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'accession', 'stock_type')->cvterm_id();
69 my %columns = (
70 stock_id => 'plot.stock_id',
71 trial_id=> 'project.project_id',
72 trait_id=> 'cvterm.cvterm_id',
73 row_number=> 'row_number.value::int',
74 col_number=> 'col_number.value::int',
75 rep=> 'rep.value',
76 plot_number=> 'plot_number.value::INT',
77 block_number=> 'block_number.value',
78 phenotype_value=> 'phenotype.value',
79 phenotype_id=> 'phenotype.phenotype_id',
80 plot_name=> 'plot.uniquename AS plot_name',
81 accession_name=> 'accession.uniquename',
82 from_clause=> " FROM stock as plot JOIN stock_relationship ON (plot.stock_id=subject_id)
83 JOIN cvterm as plot_type ON (plot_type.cvterm_id = plot.type_id)
84 JOIN stock as accession ON (object_id=accession.stock_id AND accession.type_id = $accession_type_id)
85 LEFT JOIN stockprop AS rep ON (plot.stock_id=rep.stock_id AND rep.type_id = $rep_type_id)
86 LEFT JOIN stockprop AS block_number ON (plot.stock_id=block_number.stock_id AND block_number.type_id = $block_number_type_id)
87 LEFT JOIN stockprop AS col_number ON (plot.stock_id=col_number.stock_id AND col_number.type_id = $col_number_type_id)
88 LEFT JOIN stockprop AS row_number ON (plot.stock_id=row_number.stock_id AND row_number.type_id = $row_number_type_id)
89 LEFT JOIN stockprop AS plot_number ON (plot.stock_id=plot_number.stock_id AND plot_number.type_id = $plot_number_type_id)
90 JOIN nd_experiment_stock ON(nd_experiment_stock.stock_id=plot.stock_id)
91 JOIN nd_experiment ON (nd_experiment_stock.nd_experiment_id=nd_experiment.nd_experiment_id)
92 LEFT JOIN nd_experiment_phenotype ON (nd_experiment_phenotype.nd_experiment_id=nd_experiment.nd_experiment_id)
93 LEFT JOIN phenotype USING(phenotype_id)
94 LEFT JOIN cvterm ON (phenotype.cvalue_id=cvterm.cvterm_id)
95 LEFT JOIN dbxref ON (cvterm.dbxref_id = dbxref.dbxref_id)
96 LEFT JOIN db USING(db_id)
97 JOIN nd_experiment_project ON (nd_experiment_project.nd_experiment_id=nd_experiment.nd_experiment_id)
98 JOIN project USING(project_id)",
101 my $select_clause = "SELECT DISTINCT ".$columns{'stock_id'}.", ".$columns{'plot_name'}.", ".$columns{'accession_name'}.", ".$columns{'plot_number'}.", ".$columns{'block_number'}.", ".$columns{'rep'}.", ".$columns{'row_number'}.", ".$columns{'col_number'}.", ".$columns{'phenotype_value'}.", ".$columns{'phenotype_id'}."";
103 my $from_clause = $columns{'from_clause'};
105 my $order_clause = " ORDER BY 7, 8, 4 ASC";
106 my $numeric_regex = '^-?[0-9]+([,.][0-9]+)?$';
107 my $numeric_regex_2 = '/^\s*$/';
109 my @where_clause;
111 if ($trial_id && $trait_id){
112 push @where_clause, " (". $columns{'trait_id'}." in ($trait_id) OR ". $columns{'trait_id'}." is NULL )";
113 #push @where_clause, $columns{'trait_id'}." in ($trait_id)";
114 push @where_clause, "plot.type_id in ($plot_type_id)";
115 push @where_clause, $columns{'trial_id'}." in ($trial_id)";
116 #push @where_clause, " (". $columns{'phenotype_value'}." ~\'$numeric_regex\' OR ". $columns{'phenotype_value'}." is NULL )";
117 push @where_clause, $columns{'phenotype_value'}." ~\'$numeric_regex\' ";
120 my $where_clause = " WHERE " . (join (" AND " , @where_clause));
121 my $q = $select_clause . $from_clause . $where_clause . $order_clause;
123 my $h = $schema->storage->dbh()->prepare($q);
124 $h->execute();
125 my $result = [];
126 my (@col_No, @row_No, @pheno_val, @plot_Name, @stock_Name, @plot_No, @block_No, @rep_No, @msg, %results, @phenoID);
128 while (my ($id, $plot_name, $stock_name, $plot_number, $block_number, $rep, $row_number, $col_number, $value, $pheno_id) = $h->fetchrow_array()) {
129 if (!$row_number && !$col_number){
130 if ($block_number){
131 $row_number = $block_number;
132 }elsif ($rep && !$block_number ){
133 $row_number = $rep;
136 my $plot_popUp = $plot_name."\nplot_No:".$plot_number."\nblock_No:".$block_number."\nrep_No:".$rep."\nstock:".$stock_name."\nvalue:".$value;
137 push @$result, {plotname => $plot_name, stock => $stock_name, plotn => $plot_number, blkn=>$block_number, rep=>$rep, row=>$row_number, col=>$col_number, pheno=>$value, plot_msg=>$plot_popUp, pheno_id=>$pheno_id} ;
138 push @col_No, $col_number;
139 push @row_No, $row_number;
140 push @pheno_val, $value;
141 push @plot_Name, $plot_name;
142 push @stock_Name, $stock_name;
143 push @plot_No, $plot_number;
144 push @block_No, $block_number;
145 push @rep_No, $rep;
146 push @phenoID, $pheno_id;
147 push @msg, "plot_No:".$plot_number."\nblock_No:".$block_number."\nrep_No:".$rep."\nstock:".$stock_name."\nvalue:".$value;
150 # my ($min_col, $max_col) = minmax @col_No;
151 # my ($min_row, $max_row) = minmax @row_No;
152 # my (@unique_col,@unique_row);
153 # for my $x (1..$max_col){
154 # push @unique_col, $x;
156 # for my $y (1..$max_row){
157 # push @unique_row, $y;
160 my $false_coord;
161 if ($col_No[0] == ""){
162 @col_No = ();
163 $false_coord = 'false_coord';
164 my @row_instances = uniq @row_No;
165 my %unique_row_counts;
166 $unique_row_counts{$_}++ for @row_No;
167 my @col_number2;
168 for my $key (keys %unique_row_counts){
169 push @col_number2, (1..$unique_row_counts{$key});
171 for (my $i=0; $i < scalar(@$result); $i++){
172 @$result[$i]->{'col'} = $col_number2[$i];
173 push @col_No, $col_number2[$i];
177 my ($min_col, $max_col) = minmax @col_No;
178 my ($min_row, $max_row) = minmax @row_No;
179 my (@unique_col,@unique_row);
180 for my $x (1..$max_col){
181 push @unique_col, $x;
183 for my $y (1..$max_row){
184 push @unique_row, $y;
187 my $trial = CXGN::Trial->new({
188 bcs_schema => $schema,
189 trial_id => $trial_id
191 my $data = $trial->get_controls();
193 #print STDERR Dumper($data);
195 my @control_name;
196 foreach my $cntrl (@{$data}) {
197 push @control_name, $cntrl->{'accession_name'};
199 #print STDERR Dumper(\@$result);
200 #print STDERR Dumper(\@plot_No);
202 %results = (
203 col => \@col_No,
204 row => \@row_No,
205 pheno => \@pheno_val,
206 plotName => \@plot_Name,
207 stock => \@stock_Name,
208 plot => \@plot_No,
209 block => \@block_No,
210 rep => \@rep_No,
211 result => $result,
212 plot_msg => \@msg,
213 col_max => $max_col,
214 row_max => $max_row,
215 unique_col => \@unique_col,
216 unique_row => \@unique_row,
217 false_coord => $false_coord,
218 phenoID => \@phenoID,
219 controls => \@control_name
221 print STDERR "Search End:".localtime."\n";
222 #print STDERR Dumper($result);
223 return \%results;