brapi phenotypes search refactor
[sgn.git] / lib / CXGN / BrAPI / v1 / Phenotypes.pm
blob42c0a2985e39e5796781e1e791e0a698946e051d
1 package CXGN::BrAPI::v1::Phenotypes;
3 use Moose;
4 use Data::Dumper;
5 use SGN::Model::Cvterm;
6 use CXGN::Trial;
7 use CXGN::Trait;
8 use CXGN::Phenotypes::SearchFactory;
9 use CXGN::BrAPI::Pagination;
11 has 'bcs_schema' => (
12 isa => 'Bio::Chado::Schema',
13 is => 'rw',
14 required => 1,
17 has 'metadata_schema' => (
18 isa => 'CXGN::Metadata::Schema',
19 is => 'rw',
20 required => 1,
23 has 'phenome_schema' => (
24 isa => 'CXGN::Phenome::Schema',
25 is => 'rw',
26 required => 1,
29 has 'page_size' => (
30 isa => 'Int',
31 is => 'rw',
32 required => 1,
35 has 'page' => (
36 isa => 'Int',
37 is => 'rw',
38 required => 1,
41 has 'status' => (
42 isa => 'ArrayRef[Maybe[HashRef]]',
43 is => 'rw',
44 required => 1,
48 sub search {
49 my $self = shift;
50 my $inputs = shift;
51 my $data_level = $inputs->{data_level} || 'plot';
52 my $search_type = $inputs->{search_type} || 'complete';
53 my @trait_ids_array = $inputs->{trait_ids} ? @{$inputs->{trait_ids}} : ();
54 my @accession_ids_array = $inputs->{accession_ids} ? @{$inputs->{accession_ids}} : ();
55 my @study_ids_array = $inputs->{study_ids} ? @{$inputs->{study_ids}} : ();
56 my @location_ids_array = $inputs->{location_ids} ? @{$inputs->{location_ids}} : ();
57 my @years_array = $inputs->{years} ? @{$inputs->{years}} : ();
58 my $page_size = $self->page_size;
59 my $page = $self->page;
60 my $status = $self->status;
62 my $factory_type;
63 if ($search_type eq 'complete'){
64 $factory_type = 'Native';
66 if ($search_type eq 'fast'){
67 $factory_type = 'MaterializedView';
69 my $phenotypes_search = CXGN::Phenotypes::SearchFactory->instantiate(
70 $factory_type, #can be either 'MaterializedView', or 'Native'
72 bcs_schema=>$self->bcs_schema,
73 data_level=>$data_level,
74 trial_list=>\@study_ids_array,
75 trait_list=>\@trait_ids_array,
76 include_timestamp=>1,
77 year_list=>\@years_array,
78 location_list=>\@location_ids_array,
79 accession_list=>\@accession_ids_array,
82 my $data = $phenotypes_search->search();
83 #print STDERR Dumper $data;
84 my @data_window;
85 my %obs_units;
86 foreach (@$data){
87 if (exists($obs_units{$_->[14]})){
88 my $observations = $obs_units{$_->[14]}->{observations};
89 push @$observations, {
90 observationDbId => $_->[19],
91 observationVariableDbId => $_->[10],
92 observationVariableName => $_->[4],
93 observationTimestamp => $_->[15],
94 season => $_->[0],
95 collector => '',
96 value => $_->[5],
98 $obs_units{$_->[14]}->{observations} = $observations;
99 } else {
100 $obs_units{$_->[14]} = {
101 observationUnitDbId => $_->[14],
102 observationLevel => $_->[18],
103 observationLevels => $_->[18],
104 plotNumber => $_->[9],
105 plantNumber => '',
106 blockNumber => $_->[8],
107 replicate => $_->[7],
108 observationUnitName => $_->[6],
109 germplasmDbId => $_->[13],
110 germplasmName => $_->[2],
111 studyDbId => $_->[11],
112 studyName => $_->[1],
113 studyLocationDbId => $_->[12],
114 studyLocation => $_->[3],
115 programName => '',
116 X => '',
117 Y => '',
118 entryType => '',
119 entryNumber => '',
120 treatments => [],
121 observations => [{
122 observationDbId => $_->[19],
123 observationVariableDbId => $_->[10],
124 observationVariableName => $_->[4],
125 observationTimestamp => $_->[15],
126 season => $_->[0],
127 collector => '',
128 value => $_->[5],
133 my $total_count = scalar(keys %obs_units);
134 my $count = 0;
135 my $offset = $page*$page_size;
136 my $limit = $page_size*($page+1)-1;
137 foreach my $obs_unit_id (sort keys %obs_units) {
138 if ($count >= $offset && $count <= ($offset+$limit)){
139 push @data_window, $obs_units{$obs_unit_id};
142 my %result = (data=>\@data_window);
143 push @$status, { 'success' => 'Studies observations result constructed' };
144 my $pagination = CXGN::BrAPI::Pagination->pagination_response($total_count,$page_size,$page);
145 my $response = {
146 'status' => $status,
147 'pagination' => $pagination,
148 'result' => \%result,
149 'datafiles' => []
151 return $response;