Merge pull request #5248 from solgenomics/topic/batch_update_trials
[sgn.git] / lib / CXGN / BrAPI / v1 / ObservationUnits.pm
blob8b732f7930a895e9f4c8d8424874da2395b8c080
1 package CXGN::BrAPI::v1::ObservationUnits;
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;
11 use Try::Tiny;
12 use CXGN::Phenotypes::PhenotypeMatrix;
13 use CXGN::List::Transform;
15 extends 'CXGN::BrAPI::v1::Common';
17 sub search {
18 my $self = shift;
19 my $params = shift;
20 my $page_size = $self->page_size;
21 my $page = $self->page;
22 my $status = $self->status;
23 my @data_files;
25 my $data_level = $params->{observationLevel}->[0] || 'all';
26 my $years_arrayref = $params->{seasonDbId} || ($params->{seasonDbIds} || ());
27 my $location_ids_arrayref = $params->{locationDbId} || ($params->{locationDbIds} || ());
28 my $study_ids_arrayref = $params->{studyDbId} || ($params->{studyDbIds} || ());
29 my $accession_ids_arrayref = $params->{germplasmDbId} || ($params->{germplasmDbIds} || ());
30 my $observation_unit_names_list = $params->{observationUnitName} || ($params->{observationUnitNames} || ());
31 my $trait_list_arrayref = $params->{observationVariableDbId} || ($params->{observationVariableDbIds} || ());
32 my $program_ids_arrayref = $params->{programDbId} || ($params->{programDbIds} || ());
33 my $folder_ids_arrayref = $params->{trialDbId} || ($params->{trialDbIds} || ());
34 my $start_time = $params->{observationTimeStampRangeStart}->[0] || undef;
35 my $end_time = $params->{observationTimeStampRangeEnd}->[0] || undef;
37 # not part of brapi standard yet
38 # my $phenotype_min_value = $params->{phenotype_min_value};
39 # my $phenotype_max_value = $params->{phenotype_max_value};
40 # my $exclude_phenotype_outlier = $params->{exclude_phenotype_outlier} || 0;
41 # my $search_type = $params->{search_type}->[0] || 'MaterializedViewTable';
43 my $lt = CXGN::List::Transform->new();
44 my $trait_ids_arrayref = $lt->transform($self->bcs_schema, "traits_2_trait_ids", $trait_list_arrayref)->{transform};
46 my $limit = $page_size*($page+1)-1;
47 my $offset = $page_size*$page;
49 my $phenotypes_search = CXGN::Phenotypes::SearchFactory->instantiate(
50 'MaterializedViewTable',
52 bcs_schema=>$self->bcs_schema,
53 data_level=>$data_level,
54 trial_list=>$study_ids_arrayref,
55 trait_list=>$trait_ids_arrayref,
56 include_timestamp=>1,
57 year_list=>$years_arrayref,
58 location_list=>$location_ids_arrayref,
59 accession_list=>$accession_ids_arrayref,
60 folder_list=>$folder_ids_arrayref,
61 program_list=>$program_ids_arrayref,
62 observation_unit_names_list=>$observation_unit_names_list,
63 limit=>$limit,
64 offset=>$offset,
65 # # Order by plot_number, account for non-numeric plot numbers
66 # order_by=>'NULLIF(regexp_replace(plot_number, \'\D\', \'\', \'g\'), \'\')::int',
67 # phenotype_min_value=>$phenotype_min_value,
68 # phenotype_max_value=>$phenotype_max_value,
69 # exclude_phenotype_outlier=>$exclude_phenotype_outlier
72 my ($data, $unique_traits) = $phenotypes_search->search();
73 # print STDERR Dumper $data;
75 my @data_window;
76 my $total_count = 0;
77 foreach my $obs_unit (@$data){
78 my @brapi_observations;
79 my $observations = $obs_unit->{observations};
80 foreach (@$observations){
81 my $obs_timestamp = $_->{collect_date} ? $_->{collect_date} : $_->{timestamp};
82 if ( $start_time && $obs_timestamp < $start_time ) { next; } #skip observations before date range
83 if ( $end_time && $obs_timestamp > $end_time ) { next; } #skip observations after date range
84 push @brapi_observations, {
85 observationDbId => qq|$_->{phenotype_id}|,
86 observationVariableDbId => qq|$_->{trait_id}|,
87 observationVariableName => $_->{trait_name},
88 observationTimeStamp => $obs_timestamp,
89 season => $obs_unit->{year},
90 collector => $_->{operator},
91 value => qq|$_->{value}|,
94 my @brapi_treatments;
95 my $treatments = $obs_unit->{treatments};
96 while (my ($factor, $modality) = each %$treatments){
97 my $modality = $modality ? $modality : '';
98 push @brapi_treatments, {
99 factor => $factor,
100 modality => $modality,
103 my $entry_type = $obs_unit->{obsunit_is_a_control} ? 'check' : 'test';
104 push @data_window, {
105 observationUnitDbId => qq|$obs_unit->{observationunit_stock_id}|,
106 observationLevel => $obs_unit->{observationunit_type_name},
107 observationLevels => $obs_unit->{observationunit_type_name},
108 plotNumber => $obs_unit->{obsunit_plot_number},
109 plantNumber => $obs_unit->{obsunit_plant_number},
110 blockNumber => $obs_unit->{obsunit_block},
111 replicate => $obs_unit->{obsunit_rep},
112 observationUnitName => $obs_unit->{observationunit_uniquename},
113 germplasmDbId => qq|$obs_unit->{germplasm_stock_id}|,
114 germplasmName => $obs_unit->{germplasm_uniquename},
115 studyDbId => qq|$obs_unit->{trial_id}|,
116 studyName => $obs_unit->{trial_name},
117 studyLocationDbId => qq|$obs_unit->{trial_location_id}|,
118 studyLocation => $obs_unit->{trial_location_name},
119 programName => $obs_unit->{breeding_program_name},
120 X => $obs_unit->{obsunit_col_number},
121 Y => $obs_unit->{obsunit_row_number},
122 entryType => $entry_type,
123 entryNumber => '',
124 treatments => \@brapi_treatments,
125 observations => \@brapi_observations,
126 observationUnitXref => [],
127 pedigree => undef
129 $total_count = $obs_unit->{full_count};
132 my %result = (data=>\@data_window);
133 my $pagination = CXGN::BrAPI::Pagination->pagination_response($total_count,$page_size,$page);
134 return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'Phenotype search result constructed');