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