1 package CXGN
::Phenotypes
::HighDimensionalPhenotypesSearch
;
5 CXGN::Phenotypes::HighDimensionalPhenotypesSearch - an object to handle searching high dim phenotypes (NIRS, Trnascriptomics, Metabolomics) across database.
9 my $phenotypes_search = CXGN::Phenotypes::HighDimensionalPhenotypesSearch->new({
11 nd_protocol_id=>$nd_protocol_id,
12 high_dimensional_phenotype_type=>$high_dimensional_phenotype_type, #NIRS, Transcriptomics, or Metabolomics
13 high_dimensional_phenotype_identifier_list=>\@high_dimensional_phenotype_identifier_list,
14 query_associated_stocks=>$query_associated_stocks, #Query associated plots, plants, tissue samples, etc for accessions that are given
15 accession_list=>$accession_ids,
17 plant_list=>$plant_ids,
19 my (\%data, \%identifier_metadata, \@identifier_names) = $phenotypes_search->search();
34 use SGN
::Model
::Cvterm
;
35 use CXGN
::Stock
::StockLookup
;
36 use CXGN
::Trial
::TrialLayout
;
39 use CXGN
::Phenotypes
::HighDimensionalPhenotypeProtocol
;
42 isa
=> 'Bio::Chado::Schema',
47 has
'high_dimensional_phenotype_type' => (
53 has
'nd_protocol_id' => (
59 has
'query_associated_stocks' => (
65 has
'high_dimensional_phenotype_identifier_list' => (
66 isa
=> 'ArrayRef[Str]|Undef',
70 has
'accession_list' => (
71 isa
=> 'ArrayRef[Int]|Undef',
76 isa
=> 'ArrayRef[Int]|Undef',
81 isa
=> 'ArrayRef[Int]|Undef',
87 my $schema = $self->bcs_schema();
88 my $nd_protocol_id = $self->nd_protocol_id();
89 my $high_dimensional_phenotype_type = $self->high_dimensional_phenotype_type();
90 my $high_dimensional_phenotype_identifier_list = $self->high_dimensional_phenotype_identifier_list();
91 my $accession_ids = $self->accession_list();
92 my $plot_ids = $self->plot_list();
93 my $plant_ids = $self->plant_list();
94 my $query_associated_stocks = $self->query_associated_stocks();
95 my $dbh = $schema->storage->dbh();
97 if (!$accession_ids && !$plot_ids && !$plant_ids) {
98 return { error
=> "No accessions or plots or plants in your selected dataset!" };
102 if ($query_associated_stocks) {
103 if ($accession_ids && scalar(@
$accession_ids) > 0) {
104 push @all_stock_ids, @
$accession_ids;
106 if ($plot_ids && scalar(@
$plot_ids) > 0) {
107 push @all_stock_ids, @
$plot_ids;
109 if ($plant_ids && scalar(@
$plant_ids) > 0) {
110 push @all_stock_ids, @
$plant_ids;
113 my $accession_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'accession', 'stock_type')->cvterm_id();
114 my $plot_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'plot', 'stock_type')->cvterm_id();
115 my $plant_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'plant', 'stock_type')->cvterm_id();
116 my $tissue_sample_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'tissue_sample', 'stock_type')->cvterm_id();
118 my $plot_of_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'plot_of', 'stock_relationship')->cvterm_id();
119 my $plant_of_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'plant_of', 'stock_relationship')->cvterm_id();
120 my $tissue_sample_of_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'tissue_sample_of', 'stock_relationship')->cvterm_id();
122 if ($accession_ids && scalar(@
$accession_ids) > 0) {
123 my $accession_ids_sql = join ',', @
$accession_ids;
124 my $accession_q = "SELECT subject_id FROM stock_relationship WHERE object_id IN ($accession_ids_sql) AND type_id IN ($plot_of_cvterm_id, $plant_of_cvterm_id, $tissue_sample_of_cvterm_id);";
125 my $accession_h = $dbh->prepare($accession_q);
126 $accession_h->execute();
127 while (my ($stock_id) = $accession_h->fetchrow_array()) {
128 push @all_stock_ids, $stock_id;
131 if ( ($plot_ids && scalar(@
$plot_ids) > 0) || ($plant_ids && scalar(@
$plant_ids) > 0) ) {
133 if ($plot_ids && scalar(@
$plot_ids) > 0) {
134 push @plot_plant_ids, @
$plot_ids;
136 if ($plant_ids && scalar(@
$plant_ids) > 0) {
137 push @plot_plant_ids, @
$plant_ids;
139 my $plot_ids_sql = join ',', @plot_plant_ids;
140 my $plot_q = "SELECT object_id FROM stock_relationship WHERE subject_id IN ($plot_ids_sql) AND type_id IN ($plot_of_cvterm_id, $plant_of_cvterm_id, $tissue_sample_of_cvterm_id);";
141 my $plot_h = $dbh->prepare($plot_q);
143 while (my ($stock_id) = $plot_h->fetchrow_array()) {
144 push @all_stock_ids, $stock_id;
147 my $accession_ids_sql = join ',', @all_stock_ids;
148 my $accession_q = "SELECT subject_id FROM stock_relationship WHERE object_id IN ($accession_ids_sql) AND type_id IN ($plot_of_cvterm_id, $plant_of_cvterm_id, $tissue_sample_of_cvterm_id);";
149 my $accession_h = $dbh->prepare($accession_q);
150 $accession_h->execute();
151 while (my ($stock_id) = $accession_h->fetchrow_array()) {
152 push @all_stock_ids, $stock_id;
157 if ($accession_ids && scalar(@
$accession_ids) > 0) {
158 push @all_stock_ids, @
$accession_ids;
160 if ($plot_ids && scalar(@
$plot_ids) > 0) {
161 push @all_stock_ids, @
$plot_ids;
163 if ($plant_ids && scalar(@
$plant_ids) > 0) {
164 push @all_stock_ids, @
$plant_ids;
168 # print STDERR Dumper \@all_stock_ids;
169 my $stock_ids_sql = join ',', @all_stock_ids;
172 my $protocol_type_cvterm_id;
174 if ($high_dimensional_phenotype_type eq 'NIRS') {
175 my $q = "SELECT stock.uniquename, stock.stock_id, metadata.md_json.json->>'spectra', metadata.md_json.json->>'device_type'
177 JOIN nd_experiment_stock USING(stock_id)
178 JOIN nd_experiment USING(nd_experiment_id)
179 JOIN nd_experiment_protocol USING(nd_experiment_id)
180 JOIN phenome.nd_experiment_md_json USING(nd_experiment_id)
181 JOIN metadata.md_json USING(json_id)
182 WHERE stock.stock_id IN ($stock_ids_sql) AND nd_experiment_protocol.nd_protocol_id = ? AND metadata.md_json.json_type = 'nirs_spectra';";
183 print STDERR Dumper
$q;
184 my $h = $dbh->prepare($q);
185 $h->execute($nd_protocol_id);
186 while (my ($stock_uniquename, $stock_id, $spectra, $device_type) = $h->fetchrow_array()) {
188 $spectra = decode_json
$spectra;
189 $data_matrix{$stock_id}->{spectra
} = $spectra;
190 $data_matrix{$stock_id}->{device_type
} = $device_type;
194 $protocol_type_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'high_dimensional_phenotype_nirs_protocol', 'protocol_type')->cvterm_id();
196 elsif ($high_dimensional_phenotype_type eq 'Transcriptomics') {
197 my $q = "SELECT stock.uniquename, stock.stock_id, metadata.md_json.json
199 JOIN nd_experiment_stock USING(stock_id)
200 JOIN nd_experiment USING(nd_experiment_id)
201 JOIN nd_experiment_protocol USING(nd_experiment_id)
202 JOIN phenome.nd_experiment_md_json USING(nd_experiment_id)
203 JOIN metadata.md_json USING(json_id)
204 WHERE stock.stock_id IN ($stock_ids_sql) AND nd_experiment_protocol.nd_protocol_id = ? AND metadata.md_json.json_type = 'transcriptomics';";
205 print STDERR Dumper
$q;
206 my $h = $dbh->prepare($q);
207 $h->execute($nd_protocol_id);
208 while (my ($stock_uniquename, $stock_id, $transcriptomics) = $h->fetchrow_array()) {
210 $transcriptomics = decode_json
$transcriptomics;
211 $data_matrix{$stock_id}->{transcriptomics
} = $transcriptomics;
215 $protocol_type_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'high_dimensional_phenotype_transcriptomics_protocol', 'protocol_type')->cvterm_id();
217 elsif ($high_dimensional_phenotype_type eq 'Metabolomics') {
218 my $q = "SELECT stock.uniquename, stock.stock_id, metadata.md_json.json
220 JOIN nd_experiment_stock USING(stock_id)
221 JOIN nd_experiment USING(nd_experiment_id)
222 JOIN nd_experiment_protocol USING(nd_experiment_id)
223 JOIN phenome.nd_experiment_md_json USING(nd_experiment_id)
224 JOIN metadata.md_json USING(json_id)
225 WHERE stock.stock_id IN ($stock_ids_sql) AND nd_experiment_protocol.nd_protocol_id = ? AND metadata.md_json.json_type = 'metabolomics';";
226 print STDERR Dumper
$q;
227 my $h = $dbh->prepare($q);
228 $h->execute($nd_protocol_id);
229 while (my ($stock_uniquename, $stock_id, $metabolomics) = $h->fetchrow_array()) {
231 $metabolomics = decode_json
$metabolomics;
232 $data_matrix{$stock_id}->{metabolomics
} = $metabolomics;
236 $protocol_type_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'high_dimensional_phenotype_metabolomics_protocol', 'protocol_type')->cvterm_id();
239 die "NOT A VALID HIGHDIMENSIONAL PHENOTYPE TYPE $high_dimensional_phenotype_type\n";
242 my $protocol = CXGN
::Phenotypes
::HighDimensionalPhenotypeProtocol
->new({
243 bcs_schema
=> $schema,
244 nd_protocol_id
=> $nd_protocol_id,
245 nd_protocol_type_id
=> $protocol_type_cvterm_id
247 my $identifier_metadata = $protocol->header_column_details;
248 my $identifier_names = $protocol->header_column_names;
250 # print STDERR Dumper \%data_matrix;
251 # print STDERR Dumper $identifier_metadata;
252 # print STDERR Dumper $identifier_names;
253 return (\
%data_matrix, $identifier_metadata, $identifier_names);