Merge pull request #5191 from solgenomics/topic/quality_control
[sgn.git] / lib / CXGN / BrAPI / v2 / References.pm
blobda7519c2d8b39a1ba6dfd1777fe8882cda820af9
1 package CXGN::BrAPI::v2::References;
3 use Moose;
4 use Data::Dumper;
5 use CXGN::BrAPI::Pagination;
6 use CXGN::BrAPI::JSONResponse;
7 use JSON;
9 extends 'CXGN::BrAPI::v2::Common';
11 sub search {
12 my $self = shift;
13 my $inputs = shift;
14 my $schema = $self->bcs_schema();
15 my $status = $self->status;
16 my $page_size = $self->page_size;
17 my $page = $self->page;
19 my $referenceset_ids = $inputs->{referenceSetDbId} || ($inputs->{referenceSetDbIds} || ());
20 my $reference_ids = $inputs->{referenceDbId} || ($inputs->{referenceDbIds} || ());
21 my $accession = $inputs->{accession} || ($inputs->{accessions} || ());
22 my $is_derived = $inputs->{isDerived} || ($inputs->{isDerived} || ());
23 my $md5checksum = $inputs->{md5checksum} || ($inputs->{md5checksums} || ());
24 my $max_length = $inputs->{maxLength} || ($inputs->{maxLength} || ());
25 my $min_length = $inputs->{minLength} || ($inputs->{minLength} || ());
27 if ($md5checksum || $accession || $is_derived ){
28 push @$status, { 'error' => 'The following parameters search are not implemented: md5checksum, accession, isDerived' };
31 my @data;
32 my $counter = 0;
33 my $where_clause = "";
34 my %reference_sets;
36 my $vcf_map_details_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'vcf_map_details', 'protocol_property')->cvterm_id();
38 if ($referenceset_ids && scalar(@$referenceset_ids)>0) {
39 my $protocol_sql = join ("," , @$referenceset_ids);
40 $where_clause = "AND nd_protocolprop.nd_protocol_id in ($protocol_sql)";
43 my $subquery = "SELECT distinct value->'reference_genome_name', value->'species_name', nd_protocol_id, value->'header_information_lines' from nd_protocolprop where type_id = $vcf_map_details_cvterm_id $where_clause";
45 my $h = $schema->storage->dbh()->prepare($subquery);
46 $h->execute();
48 my $start_index = $page*$page_size;
49 my $end_index = $page*$page_size + $page_size - 1;
51 while (my ($referenceset, $species, $protocol, $header ) = $h->fetchrow_array()) {
52 $referenceset =~ s/"//g;
53 $species =~ s/"//g;
54 $protocol =~ s/"//g;
55 my $source;
57 my $head = decode_json $header;
59 foreach (@{$head}){
60 $source = $1 if ($_ =~ /##source=(\w+)/);
63 foreach (@{$head}){
64 my $referenceName = $1 if ($_ =~ /##contig=<ID=(\w+)/) ;
65 my $length = $1 if ($_ =~ /,length=(\d+)/) ;
66 my $md5 = $1 if ($_ =~ /,md5=(\w+)/) ;
67 if ( $md5checksum && $md5 && ! grep { $_ eq $md5 } @{$md5checksum} ) { next; }
68 if ( $referenceName && $reference_ids && ! grep { $_ eq $referenceName } @{$reference_ids} ) { next; }
69 if ( $max_length && $length && $max_length->[0] < $length + 0 ) { next; }
70 if ( $min_length && $length && $min_length->[0] > $length + 0 ) { next; }
72 if($referenceName){
73 if ($counter >= $start_index && $counter <= $end_index) {
74 push @data, {
75 additionalInfo => {},
76 isDerived => JSON::false,
77 length => $length + 0,
78 md5checksum => $md5,
79 referenceSetDbId => qq|$protocol|,
80 referenceSetName => $referenceset,
81 referenceDbId => qq|$referenceName|,
82 referenceName => qq|$referenceName|,
83 sourceAccessions => undef,
84 sourceDivergence => undef,
85 sourceURI => undef,
86 species => {
87 term => $species,
88 termURI => undef
92 $counter++;
97 my %result = (data=>\@data);
98 my @data_files;
99 my $pagination = CXGN::BrAPI::Pagination->pagination_response($counter,$page_size,$page);
100 return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'References result constructed');
103 sub detail {
104 my $self = shift;
105 my $reference_ids = shift;
106 my $schema = $self->bcs_schema();
107 my $page_size = $self->page_size;
108 my $page = $self->page;
110 my @data;
111 my $counter = 0;
112 my $where_clause;
113 my %reference_sets;
115 my $vcf_map_details_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'vcf_map_details', 'protocol_property')->cvterm_id();
117 my $subquery = "SELECT distinct value->'reference_genome_name', value->'species_name', nd_protocol_id, value->'header_information_lines' from nd_protocolprop where type_id = $vcf_map_details_cvterm_id $where_clause";
119 my $h = $schema->storage->dbh()->prepare($subquery);
120 $h->execute();
122 while (my ($referenceset, $species, $protocol, $header ) = $h->fetchrow_array()) {
123 $referenceset =~ s/"//g;
124 $species =~ s/"//g;
125 $protocol =~ s/"//g;
126 my $source;
128 my $head = decode_json $header;
130 foreach (@{$head}){
131 $source = $1 if ($_ =~ /##source=(\w+)/);
134 foreach (@{$head}){
135 my $referenceName = $1 if ($_ =~ /##contig=<ID=(\w+)/) ;
136 my $length = $1 if ($_ =~ /,length=(\d+)/) ;
137 my $md5 = $1 if ($_ =~ /,md5=(\w+)/) ;
139 if($referenceName && $reference_ids eq $referenceName ){
140 push @data, {
141 additionalInfo => {},
142 isDerived => JSON::false,
143 length => $length + 0,
144 md5checksum => $md5,
145 referenceSetDbId => qq|$protocol|,
146 referenceSetName => $referenceset,
147 referenceDbId => qq|$referenceName|,
148 referenceName => qq|$referenceName|,
149 sourceAccessions => undef,
150 sourceDivergence => undef,
151 sourceURI => undef,
152 species => {
153 term => $species,
154 termURI => undef
157 $counter++;
163 my $status = $self->status;
165 my %result = (data=>\@data);
166 my @data_files;
167 my $pagination = CXGN::BrAPI::Pagination->pagination_response($counter,$page_size,$page);
168 return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'References result constructed');