Add missing dependency in FileRequest module and fix variable scopes
[sgn.git] / lib / CXGN / BrAPI / v1 / Observations.pm
blob8dd3a4d20954b4790523ef647830922905506d48
1 package CXGN::BrAPI::v1::Observations;
3 use Moose;
4 use Data::Dumper;
5 use SGN::Model::Cvterm;
6 use CXGN::Stock::Search;
7 use CXGN::Stock;
8 use CXGN::Chado::Organism;
9 use CXGN::BrAPI::Pagination;
10 use CXGN::BrAPI::FileRequest;
11 use utf8;
12 use JSON;
14 has 'bcs_schema' => (
15 isa => 'Bio::Chado::Schema',
16 is => 'rw',
17 required => 1,
20 has 'metadata_schema' => (
21 isa => 'CXGN::Metadata::Schema',
22 is => 'rw',
23 required => 1,
26 has 'phenome_schema' => (
27 isa => 'CXGN::Phenome::Schema',
28 is => 'rw',
29 required => 1,
32 has 'people_schema' => (
33 isa => 'CXGN::People::Schema',
34 is => 'rw',
35 required => 1,
38 has 'page_size' => (
39 isa => 'Int',
40 is => 'rw',
41 required => 1,
44 has 'page' => (
45 isa => 'Int',
46 is => 'rw',
47 required => 1,
50 has 'status' => (
51 isa => 'ArrayRef[Maybe[HashRef]]',
52 is => 'rw',
53 required => 1,
56 sub observations_store {
57 my $self = shift;
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.";
96 } else {
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.";
113 } else {
114 if ($parsed_request->{'error'}) {
115 print STDERR $parsed_request->{'error'};
116 push @error_status, $parsed_request->{'error'};
117 return (\@success_status, \@error_status);
121 my %parsed_data;
122 my @units;
123 my @variables;
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.";
133 #archive in file
135 my $archived_request = CXGN::BrAPI::FileRequest->new({
136 schema=>$schema,
137 user_id => $user_id,
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
150 ## Set metadata
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(
160 bcs_schema=>$schema,
161 metadata_schema=>$metadata_schema,
162 phenome_schema=>$phenome_schema,
163 user_id=>$user_id,
164 unit_list=>\@units,
165 variable_list=>\@variables,
166 data=>\%parsed_data,
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});
194 my $total_count = 1;
195 my @data;
196 my %result = (data => \@data);
197 my @data_files;
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');
203 =comment
204 sub observations_search {
205 my $self = shift;
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
219 my @data;
220 my %result = (data => \@data);
221 my @data_files;
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');
225 =cut