Merge pull request #5248 from solgenomics/topic/batch_update_trials
[sgn.git] / lib / CXGN / BrAPI / v1 / Markerprofiles.pm
blob45d4517fa28459352b67821cb58d69ee3abeeb02
1 package CXGN::BrAPI::v1::Markerprofiles;
3 use Moose;
4 use Data::Dumper;
5 use SGN::Model::Cvterm;
6 use CXGN::Genotype::Search;
7 use JSON;
8 use CXGN::BrAPI::FileResponse;
9 use CXGN::BrAPI::Pagination;
10 use CXGN::BrAPI::JSONResponse;
12 extends 'CXGN::BrAPI::v1::Common';
14 sub markerprofiles_search {
15 my $self = shift;
16 my $inputs = shift;
17 my $page_size = $self->page_size;
18 my $page = $self->page;
19 my $status = $self->status;
20 my $cache_file_path = $inputs->{cache_file_path};
21 my $shared_cluster_dir = $inputs->{shared_cluster_dir};
22 my @germplasm_ids = $inputs->{stock_ids} ? @{$inputs->{stock_ids}} : ();
23 my @study_ids = $inputs->{study_ids} ? @{$inputs->{study_ids}} : ();
24 my @extract_ids = $inputs->{extract_ids} ? @{$inputs->{extract_ids}} : ();
25 my @sample_ids = $inputs->{sample_ids} ? @{$inputs->{sample_ids}} : ();
26 my @methods = $inputs->{protocol_ids} ? @{$inputs->{protocol_ids}} : ();
28 if (scalar(@extract_ids)>0){
29 push @$status, { 'error' => 'Search parameter extractDbId not supported' };
31 if (scalar(@sample_ids)>0){
32 push @$status, { 'error' => 'Search parameter sampleDbId not supported' };
35 my $genotypes_search = CXGN::Genotype::Search->new({
36 bcs_schema=>$self->bcs_schema,
37 people_schema=>$self->people_schema,
38 cache_root=>$cache_file_path,
39 accession_list=>\@germplasm_ids,
40 trial_list=>\@study_ids,
41 protocol_id_list=>\@methods,
42 genotypeprop_hash_select=>['DS'],
43 protocolprop_top_key_select=>[],
44 protocolprop_marker_hash_select=>[],
45 # offset=>$page_size*$page,
46 # limit=>$page_size
47 });
48 my $file_handle = $genotypes_search->get_cached_file_search_json($shared_cluster_dir, 1); #Metadata only returned
49 my @data;
51 my $start_index = $page*$page_size;
52 my $end_index = $page*$page_size + $page_size - 1;
53 my $counter = 0;
55 open my $fh, "<&", $file_handle or die "Can't open output file: $!";
56 my $header_line = <$fh>;
57 if ($header_line) {
58 while( <$fh> ) {
59 if ($counter >= $start_index && $counter <= $end_index) {
60 my $gt = decode_json $_;
61 push @data, {
62 markerprofileDbId => qq|$gt->{markerProfileDbId}|,
63 germplasmDbId => qq|$gt->{germplasmDbId}|,
64 uniqueDisplayName => $gt->{genotypeUniquename},
65 extractDbId => qq|$gt->{stock_id}|,
66 sampleDbId => qq|$gt->{stock_id}|,
67 analysisMethod => $gt->{analysisMethod},
68 resultCount => $gt->{resultCount}
71 $counter++;
75 my %result = (data => \@data);
76 my @data_files;
77 my $pagination = CXGN::BrAPI::Pagination->pagination_response($counter,$page_size,$page);
78 return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'Markerprofiles-search result constructed');
81 sub markerprofiles_detail {
82 my $self = shift;
83 my $inputs = shift;
84 my $page_size = $self->page_size;
85 my $page = $self->page;
86 my $status = $self->status;
87 my $cache_file_path = $inputs->{cache_file_path};
88 my $shared_cluster_dir = $inputs->{shared_cluster_dir};
89 my $genotypeprop_id = $inputs->{markerprofile_id};
90 my $sep_phased = $inputs->{sep_phased};
91 my $sep_unphased = $inputs->{sep_unphased};
92 my $unknown_string = $inputs->{unknown_string};
93 my $expand_homozygotes = $inputs->{expand_homozygotes};
95 if ($sep_phased || $sep_unphased || $expand_homozygotes || $unknown_string){
96 push @$status, {'error' => 'The following parameters are not implemented: expandHomozygotes, unknownString, sepPhased, sepUnphased'};
99 my $genotypes_search = CXGN::Genotype::Search->new({
100 bcs_schema=>$self->bcs_schema,
101 people_schema=>$self->people_schema,
102 cache_root=>$cache_file_path,
103 markerprofile_id_list=>[$genotypeprop_id],
104 genotypeprop_hash_select=>['DS', 'GT', 'NT'],
105 protocolprop_top_key_select=>[],
106 protocolprop_marker_hash_select=>[],
108 my $file_handle = $genotypes_search->get_cached_file_search_json($shared_cluster_dir, 0);
110 my $start_index = $page*$page_size;
111 my $end_index = $page*$page_size + $page_size - 1;
112 my $counter = 0;
113 my @data;
114 my %result;
116 open my $fh, "<&", $file_handle or die "Can't open output file: $!";
117 my $header_line = <$fh>;
118 if ($header_line) {
119 my $marker_objects = decode_json $header_line;
120 my $gt_line = <$fh>;
121 my $gt = decode_json $gt_line;
122 my $genotype = $gt->{selected_genotype_hash};
124 foreach my $m_obj (@$marker_objects) {
125 my $m = $m_obj->{name};
126 if ($counter >= $start_index && $counter <= $end_index) {
127 my $geno = '';
128 if (exists($genotype->{$m}->{'NT'}) && defined($genotype->{$m}->{'NT'})){
129 $geno = $genotype->{$m}->{'NT'};
131 elsif (exists($genotype->{$m}->{'GT'}) && defined($genotype->{$m}->{'GT'})){
132 $geno = $genotype->{$m}->{'GT'};
134 elsif (exists($genotype->{$m}->{'DS'}) && defined($genotype->{$m}->{'DS'})){
135 $geno = $genotype->{$m}->{'DS'};
137 push @data, {$m => $geno};
139 $counter++;
142 %result = (
143 germplasmDbId=>qq|$gt->{germplasmDbId}|,
144 uniqueDisplayName=>$gt->{genotypeUniquename},
145 extractDbId=>qq|$gt->{stock_id}|,
146 sampleDbId=>qq|$gt->{stock_id}|,
147 markerprofileDbId=>qq|$gt->{markerProfileDbId}|,
148 analysisMethod=>$gt->{analysisMethod},
149 #encoding=>"AA,BB,AB",
150 data => \@data
154 my $pagination;
155 my @data_files;
156 return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'Markerprofiles detail result constructed');
159 sub markerprofiles_methods {
160 my $self = shift;
161 my $page_size = $self->page_size;
162 my $page = $self->page;
163 my $status = $self->status;
164 my $rs = $self->bcs_schema()->resultset("NaturalDiversity::NdProtocol")->search({});
165 my $total_count = $rs->count;
166 my $rs_slice= $rs->slice($page_size*$page, $page_size*($page+1)-1);
167 my @data;
168 while (my $row = $rs_slice->next()) {
169 push @data, {
170 'analysisMethodDbId' => $row->nd_protocol_id(),
171 'analysisMethod' => $row->name()
174 my %result = (data => \@data);
175 my @data_files;
176 my $pagination = CXGN::BrAPI::Pagination->pagination_response($total_count,$page_size,$page);
177 return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'Markerprofiles methods result constructed');
180 sub markerprofiles_allelematrix {
181 my $self = shift;
182 my $inputs = shift;
183 my $page_size = $self->page_size;
184 my $page = $self->page;
185 my $status = $self->status;
186 my $cache_file_path = $inputs->{cache_file_path};
187 my $shared_cluster_dir = $inputs->{shared_cluster_dir};
188 my @markerprofile_ids = $inputs->{markerprofile_ids} ? @{$inputs->{markerprofile_ids}} : ();
189 my @marker_ids = $inputs->{marker_ids} ? @{$inputs->{marker_ids}} : ();
190 my $sep_phased = $inputs->{sep_phased};
191 my $sep_unphased = $inputs->{sep_unphased};
192 my $unknown_string = $inputs->{unknown_string};
193 my $expand_homozygotes = $inputs->{expand_homozygotes};
194 my $data_format = $inputs->{format};
195 my $file_path = $inputs->{file_path};
196 my $uri = $inputs->{file_uri};
198 if ($sep_phased || $sep_unphased || $expand_homozygotes || $unknown_string){
199 push @$status, { 'error' => 'The following parameters are not implemented: expandHomozygotes, unknownString, sepPhased, sepUnphased' };
202 my @data_files;
203 my %result;
205 if ($data_format ne 'json' && $data_format ne 'tsv' && $data_format ne 'csv') {
206 push @$status, { 'error' => 'Unsupported Format Given. Supported values are: json, tsv, csv' };
209 my $genotypes_search = CXGN::Genotype::Search->new({
210 bcs_schema=>$self->bcs_schema,
211 people_schema=>$self->people_schema,
212 cache_root=>$cache_file_path,
213 markerprofile_id_list=>\@markerprofile_ids,
214 genotypeprop_hash_select=>['DS', 'GT', 'NT'],
215 protocolprop_top_key_select=>[],
216 protocolprop_marker_hash_select=>[],
218 my $file_handle = $genotypes_search->get_cached_file_search_json($shared_cluster_dir, 0);
220 my $start_index = $page*$page_size;
221 my $end_index = $page*$page_size + $page_size - 1;
222 my $counter = 0;
223 my @data;
224 my @scores;
226 open my $fh, "<&", $file_handle or die "Can't open output file: $!";
227 my $header_line = <$fh>;
228 if ($header_line) {
229 my $marker_objects = decode_json $header_line;
231 while (my $gt_line = <$fh>) {
232 my $gt = decode_json $gt_line;
233 my $genotype = $gt->{selected_genotype_hash};
234 my @ordered_refmarkers = sort keys(%$genotype);
235 my $genotypeprop_id = $gt->{markerProfileDbId};
237 foreach my $m (@ordered_refmarkers) {
238 if ($counter >= $start_index && $counter <= $end_index) {
239 my $geno = '';
240 if (exists($genotype->{$m}->{'NT'}) && defined($genotype->{$m}->{'NT'})){
241 $geno = $genotype->{$m}->{'NT'};
243 elsif (exists($genotype->{$m}->{'GT'}) && defined($genotype->{$m}->{'GT'})){
244 $geno = $genotype->{$m}->{'GT'};
246 elsif (exists($genotype->{$m}->{'DS'}) && defined($genotype->{$m}->{'DS'})){
247 $geno = $genotype->{$m}->{'DS'};
249 push @scores, [
250 qq|$m|,
251 qq|$genotypeprop_id|,
252 $geno
255 $counter++;
259 #print STDERR Dumper \@scores;
261 my @scores_seen;
262 if (!$data_format || $data_format eq 'json' ){
264 %result = (data=>\@scores);
266 } elsif ($data_format eq 'tsv' || $data_format eq 'csv' || $data_format eq 'xls') {
268 my @data = (['marker', 'markerprofileDbId', 'genotype'], @scores);
270 my $file_response = CXGN::BrAPI::FileResponse->new({
271 absolute_file_path => $file_path,
272 absolute_file_uri => $inputs->{main_production_site_url}.$uri,
273 format => $data_format,
274 data => \@data
276 @data_files = $file_response->get_datafiles();
279 my $pagination = CXGN::BrAPI::Pagination->pagination_response($counter,$page_size,$page);
280 return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'Markerprofiles allelematrix result constructed');