Merge pull request #2890 from solgenomics/topic/check_login_always
[sgn.git] / lib / CXGN / Marker / SearchBrAPI.pm
blob8c71e3b417d7425a4a462dc05c441a8653996a41
1 package CXGN::Marker::SearchBrAPI;
3 =head1 NAME
5 CXGN::Marker::SearchBrAPI - an object to handle searching for markers given criteria
7 =head1 USAGE
9 my $marker_search = CXGN::Marker::SearchBrAPI->new({
10 bcs_schema=>$schema,
11 marker_ids=>\@marker_ids,
12 marker_names=>\@marker_names,
13 get_synonyms=>$synonyms,
14 match_method=>$method,
15 types=>\@types,
16 offset=>$page_size*$page,
17 limit=>$page_size
18 });
19 my ($result, $total_count) = $marker_search->search();
21 =head1 DESCRIPTION
24 =head1 AUTHORS
27 =cut
29 use strict;
30 use warnings;
31 use Moose;
32 use Try::Tiny;
33 use Data::Dumper;
35 use CXGN::Marker;
36 use CXGN::Marker::LocMarker;
37 use CXGN::Marker::Tools qw(clean_marker_name);
38 use SGN::Model::Cvterm;
39 use CXGN::Trial;
40 use JSON;
42 has 'bcs_schema' => (
43 isa => 'Bio::Chado::Schema',
44 is => 'rw',
45 required => 1,
48 has 'nd_protocol_id' => (
49 isa => 'Int',
50 is => 'rw',
53 has 'marker_ids' => (
54 isa => 'ArrayRef[Int]|Undef',
55 is => 'rw',
58 has 'marker_names' => (
59 isa => 'ArrayRef[Int]|Undef',
60 is => 'rw'
63 has 'get_synonyms' => (
64 isa => 'Bool|Undef',
65 is => 'rw',
66 default => 0
69 has 'match_method' => (
70 isa => 'Str|Undef',
71 is => 'rw'
74 has 'types' => (
75 isa => 'ArrayRef[Int]|Undef',
76 is => 'rw'
79 has 'limit' => (
80 isa => 'Int|Undef',
81 is => 'rw'
84 has 'offset' => (
85 isa => 'Int|Undef',
86 is => 'rw'
90 sub search {
91 my $self = shift;
92 my $schema = $self->bcs_schema();
93 my $marker_ids = $self->marker_ids;
94 my $marker_names = $self->marker_names;
95 my $get_synonyms = $self->get_synonyms;
96 my $match_method = $self->match_method;
97 my $types = $self->types;
98 my $limit = $self->limit;
99 my $offset = $self->offset;
100 my @where_clause;
101 my $comparison;
103 if ($match_method eq 'exact'){ $comparison = 'in';}
104 elsif ($match_method eq 'case_insensitive'){ $comparison = 'ilike'; }
105 else { $comparison = 'like'; }
107 if ($marker_ids && scalar(@$marker_ids)>0) {
108 my $sql = join ("," , @$marker_ids);
109 push @where_clause, "marker.marker_id in ($sql)";
112 if ($marker_names && scalar(@$marker_names)>0) {
113 my $sql = join ("," , @$marker_names);
114 push @where_clause, "marker_names.name in ($sql)";
117 if ($types && scalar(@$types)>0) {
118 my $sql = join ("," , @$types);
119 push @where_clause, "protocol in ($sql)";
122 my $where_clause = scalar(@where_clause)>0 ? " WHERE " . (join (" AND " , @where_clause)) : '';
125 my $subquery = "SELECT distinct m2m.marker_id,name,alias,protocol,organism_name,common_name.common_name FROM sgn.marker
126 LEFT JOIN sgn.marker_to_map as m2m using(marker_id)
127 INNER JOIN sgn.accession ON(parent_1 = accession.accession_id OR parent_2 = accession.accession_id)
128 INNER JOIN sgn.organism using(organism_id)
129 INNER JOIN sgn.common_name USING(common_name_id)
130 INNER JOIN marker_names ON(m2m.marker_id=marker_names.marker_id)
131 INNER JOIN marker_alias ON(m2m.marker_id=marker_alias.marker_id) $where_clause";
133 my $h = $schema->storage->dbh()->prepare($subquery);
134 $h->execute();
136 my @result;
137 my $total_count = 0;
138 my $subtract_count = 0;
140 while (my ($marker_id, $marker_name, $reference, $alias, $protocol, $full_count) = $h->fetchrow_array()) {
141 push @result, {
142 marker_id => $marker_id,
143 marker_name => $marker_name,
144 method => $protocol,
145 references => $reference,
146 synonyms => $alias,
147 type => $protocol
149 $total_count = $full_count;
152 my @data_window;
153 if (($limit && defined($limit) || ($offset && defined($offset)))){
154 my $start = $offset;
155 my $end = $offset + $limit - 1;
156 for( my $i = $start; $i <= $end; $i++ ) {
157 if ($result[$i]) {
158 push @data_window, $result[$i];
161 } else {
162 @data_window = @result;
165 $total_count = $total_count-$subtract_count;
166 return (\@data_window, $total_count);