1 package CXGN
::BrAPI
::v1
::Observations
;
5 use SGN
::Model
::Cvterm
;
6 use CXGN
::Stock
::Search
;
8 use CXGN
::Chado
::Organism
;
9 use CXGN
::BrAPI
::Pagination
;
10 use CXGN
::BrAPI
::FileRequest
;
15 isa
=> 'Bio::Chado::Schema',
20 has
'metadata_schema' => (
21 isa
=> 'CXGN::Metadata::Schema',
26 has
'phenome_schema' => (
27 isa
=> 'CXGN::Phenome::Schema',
32 has
'people_schema' => (
33 isa
=> 'CXGN::People::Schema',
51 isa
=> 'ArrayRef[Maybe[HashRef]]',
56 sub observations_store
{
58 my $search_params = shift;
59 my $observations = $search_params->{observations
} ?
$search_params->{observations
} : ();
61 print STDERR
"Observations are ". Dumper
($observations) . "\n";
63 my $schema = $self->bcs_schema;
64 my $metadata_schema = $self->metadata_schema;
65 my $phenome_schema = $self->phenome_schema;
66 my $page_size = $self->page_size;
67 my $page = $self->page;
68 my $status = $self->status;
69 my $user_id = $search_params->{user_id
};
70 my $username = $search_params->{username
};
71 my $user_type = $search_params->{user_type
};
72 my $archive_path = $search_params->{archive_path
};
73 my @error_status = [];
74 my @success_status = [];
76 print STDERR
"OBSERVATIONS_MODULE: User id is $user_id and type is $user_type\n";
78 if ($user_type ne 'submitter' && $user_type ne 'sequencer' && $user_type ne 'curator') {
79 push @error_status, 'Must have submitter privileges to upload phenotypes! Please contact us!';
80 return (\
@success_status, \
@error_status);
83 #validate request structure and parse data
84 my $timestamp_included = 1;
85 my $data_level = 'stocks';
87 my $parser = CXGN
::Phenotypes
::ParseUpload
->new();
88 my $validate_request = $parser->validate('brapi observations', $observations, $timestamp_included, $data_level, $schema);
89 if (!$validate_request) {
90 print STDERR
"Error parsing request structure.";
91 push @error_status, "Error parsing request structure.";
92 return (\
@success_status, \
@error_status);
94 if ($validate_request == 1){
95 push @success_status, "Request structure is valid.";
97 if ($validate_request->{'error'}) {
98 print STDERR
$validate_request->{'error'};
99 push @error_status, $validate_request->{'error'};
101 return (\
@success_status, \
@error_status);
104 my $parsed_request = $parser->parse('brapi observations', $observations, $timestamp_included, $data_level, $schema);
106 if (!$parsed_request) {
107 print STDERR
"Error parsing request data.";
108 push @error_status, "Error parsing request data.";
109 return (\
@success_status, \
@error_status);
111 if ($parsed_request == 1){
112 push @success_status, "Request data is valid.";
114 if ($parsed_request->{'error'}) {
115 print STDERR
$parsed_request->{'error'};
116 push @error_status, $parsed_request->{'error'};
117 return (\
@success_status, \
@error_status);
125 print STDERR
"Defining stocks (observationUnits) and traits (observationVariables) from parsed request";
126 if ($parsed_request && !$parsed_request->{'error'}) {
127 %parsed_data = %{$parsed_request->{'data'}};
128 @units = @
{$parsed_request->{'units'}};
129 @variables = @
{$parsed_request->{'variables'}};
130 push @success_status, "Request data is valid.";
135 my $archived_request = CXGN
::BrAPI
::FileRequest
->new({
138 user_type
=> $user_type,
139 archive_path
=> $archive_path,
140 format
=> 'observations',
141 data
=> $observations
144 my $file = $archived_request->get_path();
146 print STDERR
"Archived Request is in $file\n";
148 ## Store observations and return details for response
151 my %phenotype_metadata;
152 my $time = DateTime
->now();
153 my $timestamp = $time->ymd()."_".$time->hms();
154 $phenotype_metadata{'archived_file'} = $file;
155 $phenotype_metadata{'archived_file_type'} = 'brapi observations';
156 # $phenotype_metadata{'operator'} = $username;
157 $phenotype_metadata{'date'} = $timestamp;
159 my $store_observations = CXGN
::Phenotypes
::StoreObservations
->new(
161 metadata_schema
=>$metadata_schema,
162 phenome_schema
=>$phenome_schema,
165 variable_list
=>\
@variables,
167 # has_timestamps=>$timestamp_included,
168 # overwrite_values=>0,
169 metadata_hash
=>\
%phenotype_metadata
172 # my ($verified_warning, $verified_error) = $store_phenotypes->verify();
174 # if ($verified_error) {
175 # print STDERR "Error: $verified_error\n";
177 # if ($verified_warning) {
178 # print STDERR "Warning: $verified_warning\n";
181 my ($stored_phenotype_error, $stored_phenotype_success) = $store_observations->store();
183 if ($stored_phenotype_error) {
184 print STDERR
"Error: $stored_phenotype_error\n";
186 if ($stored_phenotype_success) {
187 print STDERR
"Success: $stored_phenotype_success\n";
190 # will need to initiate refresh matviews in controller instead
191 # my $bs = CXGN::BreederSearch->new( { dbh=>$c->dbc->dbh, dbname=>$c->config->{dbname}, } );
192 # my $refresh = $bs->refresh_matviews($c->config->{dbhost}, $c->config->{dbname}, $c->config->{dbuser}, $c->config->{dbpass}, 'fullview', 'concurrent', $c->config->{basepath});
196 my %result = (data
=> \
@data);
198 my $pagination = CXGN
::BrAPI
::Pagination
->pagination_response($total_count,$page_size,$page);
199 return CXGN
::BrAPI
::JSONResponse
->return_success(\
%result, $pagination, \
@data_files, $status, 'Observations-search result constructed');
204 sub observations_search
{
206 my $search_params = shift;
208 my $page_size = $self->page_size;
209 my $page = $self->page;
210 my $status = $self->status;
212 my @collectors = $search_params->{collectors
} ? @
{$search_params->{collectors
}} : ();
213 my @observation_db_ids = $search_params->{observationDbIds
} ? @
{$search_params->{observationDbIds
}} : ();
214 my @observation_unit_db_ids = $search_params->{observationUnitDbIds
} ? @
{$search_params->{observationUnitDbIds
}} : ();
215 my @observation_variable_db_ids = $search_params->{observationVariableDbIds
} ? @
{$search_params->{observationVariableDbIds
}} : ();
217 #implement observation search here using stock search
220 my %result = (data
=> \
@data);
222 my $pagination = CXGN
::BrAPI
::Pagination
->pagination_response($total_count,$page_size,$page);
223 return CXGN
::BrAPI
::JSONResponse
->return_success(\
%result, $pagination, \
@data_files, $status, 'Observations-search result constructed');