forked from Ensembl/ensembl-webcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExaLead.pm
331 lines (304 loc) · 10.1 KB
/
ExaLead.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
=head1 LICENSE
Copyright [1999-2014] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=cut
package ExaLead;
use strict;
### The Exalead class heirarchy is a Perl wrapper around the Exalead one
### XML search engine output
## packages used to grab content of XML
use XML::Simple;
use LWP;
use CGI;
use ExaLead::SpellingSuggestion;
use ExaLead::Keyword;
use ExaLead::Query;
use ExaLead::QueryParameter;
use ExaLead::QueryTerm;
use ExaLead::TextSeg;
use ExaLead::Value;
use ExaLead::Group;
use ExaLead::Category;
use ExaLead::Link;
use ExaLead::Hit;
use ExaLead::HitGroup;
use ExaLead::HitCategory;
sub new {
### c
### Store information about hits
my( $class ) = @_;
my $self = {
'engineURL' => '',
'rootURL' => '',
'nmatches' => 0,
'nhits' => 0,
'start' => 0,
'end' => 0,
'last' => 0,
'estimated' => 0,
'keywords' => [],
'spellings' => [],
'groups' => [],
'hits' => [],
'query_string' => '',
'hidden_fields' => [],
'__status' => 'no_search',
'__error' => undef,
'__timeout' => 30
};
bless $self, $class;
return $self;
}
sub __timeout :lvalue {
### a
### Sets the timeout period for XML retrival
$_[0]->{'__timeout'};
}
sub __status :lvalue {
### a
### either 'no_search', 'search' or 'failure'
$_[0]->{'__status'};
}
sub __error :lvalue {
### a
### error string if failure occurs
$_[0]->{'__error'};
}
sub engineURL :lvalue {
### a
### URL for search engine itself - that generates the XML hits
$_[0]->{'engineURL'};
}
sub rootURL :lvalue {
### a
### URL for the wrapper script
$_[0]->{'rootURL'};
}
sub nmatches :lvalue {
### a
### Taken from <Hits> element of the XML returned
$_[0]->{'nmatches'};
}
sub nhits :lvalue {
### a
### Taken from <Hits> element of the XML returned
$_[0]->{'nhits'};
}
sub start :lvalue {
### a
### Taken from <Hits> element of the XML returned
$_[0]->{'start'};
}
sub end :lvalue {
### a
### Taken from <Hits> element of the XML returned
$_[0]->{'end'};
}
sub last :lvalue {
### a
### Taken from <Hits> element of the XML returned
$_[0]->{'last'};
}
sub estimated :lvalue {
### a
### Taken from <Hits> element of the XML returned
$_[0]->{'estimated'};
}
sub query :lvalue {
### a
### get/set {{Exalead::Query}} object, which summarises the
### query that was sentto the sarch engine
$_[0]->{'query'};
}
sub addGroup {
### Add a {{Exalead::Query}} object - these are used to hold the different
### categorisation of matches (in Ensembl's case these
### are Feature type and Species)
push @{$_[0]{'groups'}}, $_[1];
}
sub addSpellingSuggestion {
### Add a {{Exalead::SpellingSuggestion}} object - exalead comes back with
### alternative spellings if it cannot find the requested ID
push @{$_[0]{'spellings'}}, $_[1];
}
sub addKeyword {
### Add a {{Exalead::Keyword}} object
push @{$_[0]{'keywords'}}, $_[1];
}
sub addHit {
### Add a {{Exalead::Hit}} object - these are the actual URL responses
### for "pages" which match the request.
push @{$_[0]{'hits'}}, $_[1];
}
sub groups {
### Returns array of {{Exalead::Group}} objects previously added
return @{$_[0]{'groups'}};
}
sub spellingsuggestions {
### Returns array of {{Exalead::SpellingSuggestion}} objects previously added
return @{$_[0]{'spellings'}};
}
sub keywords {
### Returns array of {{Exalead::Keywords}} objects previously added
return @{$_[0]{'keywords'}};
}
sub hits {
### Returns array of {{Exalead::Hits}} objects previously added
return @{$_[0]{'hits'}};
}
## Parser and associated functions...
sub parse {
### Main function in the Exalead module, constructs the approprate request URL,
### and retrieves the XML, which then calls the inner function _parse, which
### actually parses the XML created the Exalead::* objects
my( $self, $q, $flag ) = @_;
my $search_URL = $self->engineURL;
# my $host = `hostname`; chomp $host;
# $host.=':'.gmtime();
if( $flag > 20 ) {
#warn "[EXALEAD:$host:$$] $search_URL :FAIL: Exalead search engine failure\n";
$self->__status = 'failure';
$self->__error = 'Exalead search engine failure';
return;
}
# $search_URL =~ s/1/2/;
my $join = '?';
foreach my $VAR ( $q->param() ) {
$search_URL .= $join. join( '&', map { "$VAR=".CGI::escape($_) } $q->param( $VAR ) );
$join = '&';
}
my $ua = LWP::UserAgent->new();
$ua->timeout( $self->__timeout ); ## Allow 30 seconds for a response!!
my $res = $ua->get( $search_URL );
$self->{'search_URL'} = $search_URL;
if( $res->is_success ) {
#warn "[EXALEAD:$host:$$] $search_URL ".length($res->content)." bytes\n";
if( length( $res->content ) < 100 ) {
$flag++;
#warn "[EXALEAD:$host:$$] rerunning $flag due to null response\n";
sleep 1;
return $self->parse( $q, $flag );
}
$self->_parse( $res->content );
} else {
#warn "[EXALEAD:$host:$$] $search_URL :FAIL: ",$res->message,"\n";
$self->__status = 'failure';
$self->__error = $res->message eq 'read timeout' ? 'Exalead search engine timed out after '.$self->__timeout.' seconds' : $res->message;
}
}
sub _parse {
### The guts of the parser, takes an XML string, and converts it into a nested hash/array
### data structure using XML::Simple, which is then traversed to store the results
### returned by the search engine for later use
my( $self, $XML ) = @_;
## Convert XML to object hash....
my $xml = eval { XMLin( $XML, ForceArray=>1, KeyAttr=>[] ) };
if( $@ ) {
$self->__status = 'failure';
(my $error = $@) =~ s/ at \/.*/./sm;
$self->__error = $error;
return;
}
$self->{'raw_XML'} = $XML;
$self->{'XML'} = $xml;
$self->__status = 'search';
## Parse Query....
my $Q = $xml->{'Query'}[0];
my $query = new ExaLead::Query( $Q->{'query'}, $Q->{'context'} );
foreach my $qt_xml ( @{$Q->{'QueryTerm'}} ) {
$query->addTerm( new ExaLead::QueryTerm( $qt_xml->{'regexp'}, $qt_xml->{'level'} ) );
}
foreach my $qp_xml ( @{$Q->{'QueryParameter'}} ) {
$query->addParameter( new ExaLead::QueryParameter( $qp_xml->{'name'}, $qp_xml->{'value'} ) );
}
$self->query = $query;
## Parse Groups....
foreach my $T ( @{$xml->{'Groups'}[0]{'Group'}||[]} ) {
my $group = new ExaLead::Group( $T->{'title'}, $T->{'count'} );
$group->link( 'reset' ) = new ExaLead::Link( $T->{'resetHref'}, $self ) if exists $T->{'resetHref'};
$group->addChildren( $self->_parse_category_tree( $T->{'Category'} ) );
$self->addGroup( $group );
}
## Parse Hits....
$self->nmatches = $xml->{'Hits'}[0]{'nmatches'};
$self->nhits = $xml->{'Hits'}[0]{'nhits'};
$self->start = $xml->{'Hits'}[0]{'start'};
$self->end = $xml->{'Hits'}[0]{'end'};
$self->estimated = $xml->{'Hits'}[0]{'estimated'};
$self->last = $xml->{'Hits'}[0]{'last'};
foreach my $hit_xml ( @{$xml->{'Hits'}[0]{'Hit'}||[]} ) {
my $hit = new ExaLead::Hit( $hit_xml->{'url'}, $hit_xml->{'score'} );
foreach my $hit_field_xml ( @{$hit_xml->{'HitField'}||[]} ) {
my $name = $hit_field_xml->{'name'};
if( exists($hit_field_xml->{'TextSeg'} ) ) {
my $TS = new ExaLead::TextSeg();
foreach my $ts_xml ( @{$hit_field_xml->{'TextSeg'}} ) {
$TS->addPart($ts_xml->{'content'}||' ', $ts_xml->{'highlighted'});
}
$hit->addField( $name, $TS );
} elsif( exists($hit_field_xml->{'value'} ) ) {
my $TS = new ExaLead::Value( $hit_field_xml->{'value'}, $query );
$hit->addField( $name, $TS );
}
}
foreach my $hit_group_xml( @{$hit_xml->{'HitGroup'}||[]} ) {
my $hitgroup = new ExaLead::HitGroup( $hit_group_xml->{'title'} );
$hitgroup->addChildren( $self->_parse_hit_category_tree( $hit_group_xml->{'HitCategory'} ) );
$hit->addGroup( $hitgroup );
}
$self->addHit( $hit );
}
## Parse spelling suggestions....
foreach my $spelling_xml ( @{$xml->{'SpellingSuggestions'}[0]{'SpellingSuggestion'}||[]} ) {
foreach my $T ( @{$spelling_xml->{'SpellingSuggestionVariant'}||[]} ) {
$self->addSpellingSuggestion( new ExaLead::SpellingSuggestion(
$T->{'query'}, $T->{'display'}
));
}
}
## Parse relevant keywords....
foreach my $k_xml ( @{$xml->{'Keywords'}[0]{'Keyword'}||[]} ) {
my $K = new ExaLead::Keyword( $k_xml->{'display'}, $k_xml->{'count'} );
my @links = qw( exclude reset refine );
foreach my $n ( @links ) {
$K->link( $n ) = new ExaLead::Link( $k_xml->{ $n."Href" }, $self ) if exists( $k_xml->{$n."Href"} );
}
$self->addKeyword( $K );
}
}
sub _parse_category_tree {
### Recursive parser for the main category trees (stored in {{Exalead::Group}} object)
my( $self, $category ) = @_;
my @links = qw( exclude reset refine );
my $cat_array = [];
foreach my $cat (@$category) {
my $entry = new ExaLead::Category( $cat->{'name'}, $cat->{'count'}, $cat->{'gcount'} );
foreach my $n ( @links ) {
$entry->link( $n ) = new ExaLead::Link( $cat->{ $n."Href" }, $self ) if exists( $cat->{$n."Href"} );
}
$entry->addChildren( $self->_parse_category_tree( $cat->{'Category'} ) ) if exists $cat->{'Category'};
push @$cat_array, $entry;
}
return $cat_array;
}
sub _parse_hit_category_tree {
### Recursive parser for the hit category trees (stored in {{Exalead::Hit}} object)
my( $self, $category ) = @_;
my $cat_array = [];
foreach my $cat (@$category) {
my $entry = new ExaLead::HitCategory( $cat->{'name'} );
$entry->link( 'browse' ) = new ExaLead::Link( $cat->{ "browseHref" }, $self ) if exists $cat->{"browseHref"};
$entry->addChildren( $self->_parse_hit_category_tree( $cat->{'HitCategory'} ) ) if exists $cat->{'HitCategory'};
push @$cat_array, $entry;
}
return $cat_array;
}
1;