create and upload fieldbook phenotypes with treatments
[sgn.git] / lib / CXGN / Genotype / Search.pm
blob83783fb7dd948dbb53b2f6a9d701b49bc7d45fd3
1 package CXGN::Genotype::Search;
3 =head1 NAME
5 CXGN::Genotype::Search - an object to handle searching genotypes for stocks
7 =head1 USAGE
9 my $genotypes_search = CXGN::Genotype::Search->new({
10 bcs_schema=>$schema,
11 accession_list=>$accession_list,
12 trial_list=>$trial_list,
13 protocol_id=>$protocol_id
14 });
15 my $resultset = $genotypes_search->get_genotype_info();
16 my $genotypes = $resultset->{genotypes};
18 =head1 DESCRIPTION
21 =head1 AUTHORS
23 Nicolas Morales <nm529@cornell.edu>
24 With code moved from CXGN::BreederSearch
25 Lukas Mueller <lam87@cornell.edu>
26 Aimin Yan <ay247@cornell.edu>
28 =cut
30 use strict;
31 use warnings;
32 use Moose;
33 use Try::Tiny;
34 use Data::Dumper;
35 use SGN::Model::Cvterm;
36 use CXGN::Trial;
38 has 'bcs_schema' => ( isa => 'Bio::Chado::Schema',
39 is => 'rw',
40 required => 1,
43 has 'accession_list' => (
44 isa => 'ArrayRef[Int]|Undef',
45 is => 'ro',
48 has 'trial_list' => (
49 isa => 'ArrayRef[Int]|Undef',
50 is => 'ro',
53 has 'protocol_id' => (
54 isa => 'Int',
55 is => 'rw',
56 required => 1,
59 has 'limit' => (
60 isa => 'Int',
61 is => 'rw',
64 has 'offset' => (
65 isa => 'Int',
66 is => 'rw',
69 =head2 get_genotype_info
71 returns: an array with genotype information
73 =cut
75 sub get_genotype_info {
76 my $self = shift;
77 my $schema = $self->bcs_schema;
78 my $trial_list = $self->trial_list;
79 my $protocol_id = $self->protocol_id;
80 my $accession_list = $self->accession_list;
81 my $limit = $self->limit;
82 my $offset = $self->offset;
83 my @data;
84 my %search_params;
86 my $snp_genotyping_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'snp genotyping', 'genotype_property')->cvterm_id();
88 my @trials_accessions;
89 foreach (@$trial_list){
90 my $trial = CXGN::Trial->new({bcs_schema=>$schema, trial_id=>$_});
91 my $accessions = $trial->get_accessions();
92 foreach (@$accessions){
93 push @trials_accessions, $_->{stock_id};
97 #If accessions are explicitly given, then accessions found from trials will not be added to the search.
98 if (!$accession_list || scalar(@$accession_list)==0) {
99 push @$accession_list, @trials_accessions;
102 #For projects inserted into database during the addition of genotypes and genotypeprops
103 if (scalar(@trials_accessions)==0){
104 if ($trial_list && scalar(@$trial_list)>0) {
105 $search_params{'nd_experiment_projects.project_id'} = { -in => $trial_list };
109 $search_params{'genotypeprops.type_id'} = $snp_genotyping_cvterm_id;
110 $search_params{'nd_protocol.nd_protocol_id'} = $protocol_id;
111 if ($accession_list && scalar(@$accession_list)>0) {
112 $search_params{'stock.stock_id'} = { -in => $accession_list };
115 my @select_list = ('genotypeprops.genotypeprop_id', 'genotypeprops.value', 'nd_protocol.name', 'stock.stock_id', 'stock.uniquename', 'genotype.uniquename');
116 my @select_as_list = ('genotypeprop_id', 'value', 'protocol_name', 'stock_id', 'uniquename', 'genotype_uniquename');
117 #$self->bcs_schema->storage->debug(1);
118 my $rs = $self->bcs_schema->resultset('NaturalDiversity::NdExperiment')->search(
119 \%search_params,
120 {join=> [{'nd_experiment_genotypes' => {'genotype' => 'genotypeprops'} }, {'nd_experiment_protocols' => 'nd_protocol' }, 'nd_experiment_projects', {'nd_experiment_stocks' => 'stock'} ],
121 select=> \@select_list,
122 as=> \@select_as_list,
123 order_by=>{ -asc=>'genotypeprops.genotypeprop_id' }
127 if ($rs) {
128 if ($limit && defined($offset)){
129 my $rs_slice = $rs->slice($offset, $limit);
130 $rs = $rs_slice;
132 while (my $row = $rs->next()) {
133 my $genotype_json = $row->get_column('value');
134 my $genotype = JSON::Any->decode($genotype_json);
136 push @data, {
137 markerProfileDbId => $row->get_column('genotypeprop_id'),
138 germplasmDbId => $row->get_column('stock_id'),
139 germplasmName => $row->get_column('uniquename'),
140 genotypeUniquename => $row->get_column('genotype_uniquename'),
141 analysisMethod => $row->get_column('protocol_name'),
142 genotype_hash => $genotype,
143 resultCount => scalar(keys(%$genotype))
147 #print STDERR Dumper \@data;
149 my $total_count = $rs->count();
151 return ($total_count, \@data);