Merge pull request #5248 from solgenomics/topic/batch_update_trials
[sgn.git] / lib / CXGN / BrAPI / v1 / ObservationTables.pm
blobbf0a2fde0392d59b10f382f30990cc9eca5dc062
1 package CXGN::BrAPI::v1::ObservationTables;
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 $trait_list_arrayref = $params->{observationVariableDbId} || ($params->{observationVariableDbIds} || ());
31 my $program_ids_arrayref = $params->{programDbId} || ($params->{programDbIds} || ());
32 my $folder_ids_arrayref = $params->{trialDbId} || ($params->{trialDbIds} || ());
33 my $start_time = $params->{observationTimeStampRangeStart}->[0] || undef;
34 my $end_time = $params->{observationTimeStampRangeEnd}->[0] || undef;
36 # not part of brapi standard yet
37 # my $phenotype_min_value = $params->{phenotype_min_value};
38 # my $phenotype_max_value = $params->{phenotype_max_value};
39 # my $exclude_phenotype_outlier = $params->{exclude_phenotype_outlier} || 0;
40 # my $search_type = $params->{search_type}->[0] || 'MaterializedViewTable';
42 my $lt = CXGN::List::Transform->new();
43 my $trait_ids_arrayref = $lt->transform($self->bcs_schema, "traits_2_trait_ids", $trait_list_arrayref)->{transform};
45 my $phenotypes_search = CXGN::Phenotypes::PhenotypeMatrix->new(
46 bcs_schema=>$self->bcs_schema,
47 data_level=>$data_level,
48 search_type=>'MaterializedViewTable',
49 trial_list=>$study_ids_arrayref,
50 trait_list=>$trait_ids_arrayref,
51 include_timestamp=>1,
52 year_list=>$years_arrayref,
53 location_list=>$location_ids_arrayref,
54 accession_list=>$accession_ids_arrayref,
55 folder_list=>$folder_ids_arrayref,
56 program_list=>$program_ids_arrayref,
57 # phenotype_min_value=>$phenotype_min_value,
58 # phenotype_max_value=>$phenotype_max_value,
59 # exclude_phenotype_outlier=>$exclude_phenotype_outlier
61 my @data;
62 try {
63 @data = $phenotypes_search->get_phenotype_matrix();
65 catch {
66 return CXGN::BrAPI::JSONResponse->return_error($status, 'An Error Occured During Phenotype Search Table');
69 my @data_files;
70 my $total_count = scalar(@data)-1;
71 my @header_names = @{$data[0]};
72 my @trait_names = @header_names[39 .. $#header_names];
73 my @header_ids;
74 foreach my $t (@trait_names) {
75 if ($t eq 'notes') { next; }
76 push @header_ids, SGN::Model::Cvterm->get_cvterm_row_from_trait_name($self->bcs_schema, $t)->cvterm_id();
79 my $start = $page_size*$page;
80 my $end = $page_size*($page+1)-1;
81 my @data_window;
82 for (my $line = $start; $line < $end; $line++) {
83 if ($data[$line]) {
84 my $columns = $data[$line];
85 push @data_window, $columns;
89 #print STDERR Dumper \@data_window;
91 my %result = (
92 headerRow => ['studyYear', 'programDbId', 'programName', 'programDescription', 'studyDbId', 'studyName', 'studyDescription', 'studyDesign', 'plotWidth', 'plotLength', 'fieldSize', 'fieldTrialIsPlannedToBeGenotyped', 'fieldTrialIsPlannedToCross', 'plantingDate', 'harvestDate', 'locationDbId', 'locationName', 'germplasmDbId', 'germplasmName', 'germplasmSynonyms', 'observationLevel', 'observationUnitDbId', 'observationUnitName', 'replicate', 'blockNumber', 'plotNumber', 'rowNumber', 'colNumber', 'entryType', 'plantNumber', 'plantedSeedlotStockDbId', 'plantedSeedlotStockUniquename', 'plantedSeedlotCurrentCount', 'plantedSeedlotCurrentWeightGram', 'plantedSeedlotBoxName', 'plantedSeedlotTransactionCount', 'plantedSeedlotTransactionWeight', 'plantedSeedlotTransactionDescription', 'availableGermplasmSeedlotUniquenames'],
93 observationVariableDbIds => \@header_ids,
94 observationVariableNames => \@trait_names,
95 data=>\@data_window
98 my $pagination = CXGN::BrAPI::Pagination->pagination_response($total_count,$page_size,$page);
99 return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'Phenotype-search table result constructed');