-
Notifications
You must be signed in to change notification settings - Fork 182
/
dbfetch
executable file
·618 lines (501 loc) · 18.1 KB
/
dbfetch
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#!/usr/local/bin/perl -- # -*-Perl-*-
=head1 NAME
dbfetch - generic CGI program to retrieve biological database entries
in various formats and styles (using SRS)
=head1 SYNOPSIS
# URL examples:
# prints the interactive page with the HTML form
http://www.ebi.ac.uk/cgi-bin/dbfetch
# for backward compatibility, implements <ISINDEX>
# single entry queries defaulting to EMBL sequence database
http://www.ebi.ac.uk/cgi-bin/dbfetch?J00231
# retrieves one or more entries in default format
# and default style (html)
# returns nothing for IDs which are not valid
http://www.ebi.ac.uk/cgi-bin/dbfetch?id=J00231.1,hsfos,bum
# retrieve entries in fasta format without html tags
http://www.ebi.ac.uk/cgi-bin/dbfetch?format=fasta&style=raw&id=J00231,hsfos,bum
# retrieve a raw Ensembl entry
http://www.ebi.ac.uk/cgi-bin/dbfetch?db=ensembl&style=raw&id=AL122059
=head1 DESCRIPTION
This program generates a page allowing a web user to retrieve database
entries from a local SRS in two styles: html and raw. Other
database engines can be used to implement te same interfase.
At this stage, on unique identifier queries are supported. Free text
searches returning more than one entry per query term are not in these
specs.
In its default setup, type one or more EMBL accession numbers
(e.g. J00231), entry name (e.g. BUM) or sequence version into the
seach dialog to retieve hypertext linked enties.
Note that for practical reasons only the first 50 identifiers
submitted are processed.
Additional input is needed to change the sequence format or suppress
the HTML tags. The styles are html and raw. In future there might be
additional styles (e.g. xml). Currently XML is a 'raw' format used by
Medline. Each style is implemented as a separate subroutine.
=head1 MAINTANENCE
A new database can be added simply by adding a new entry in the global hash
%IDS. Additionally, if the database defines new formats add an entry for
each of them into the hash %IDMATCH. After modifying the hash, run this
script from command line for some sanity checks with parameter debug set to
true (e.g. dbfetch debug=1 ).
Finally, the user interface needs to be updated in the L<print_prompt>
subroutine.
=head1 VERSIONS
Version 3 uses EBI SRS server 6.1.3. That server is able to merge release
and update libraries automatically which makes this script simpler. The
other significant change is the way sequence versions are indexed. They
used to be indexed together with the string accession
(e.g. 'J00231.1'). Now they are indexed as integers (e.g. '1').
Version 3.1 changes the command line interface. To get the debug
information use attribute 'debug' set to true. Also, it uses File::Temp
module to create temporary files securely.
Version 3.2 fixes fasta format parsing to get the entry id.
Version 3.3. Adds RefSeq to the database list.
Version 3.4. Make this compliant to BioFetch specs.
=head1 AUTHOR - Heikki Lehvaslaiho
Email: heikki-at-bioperl-dot-org
=cut
# Let the code begin...
$VERSION = '3.4';
$DATE = '28 Jan 2002';
use CGI "standard";
#use POSIX;
use CGI::Carp qw/ fatalsToBrowser /;
use File::Temp qw/ tempfile tempdir /;
use strict;
no strict "refs";
use constant MAXIDS => 50;
use constant TMPDIR => '/usr/tmp';
use vars qw( $VERSION $DATE %DBS %STYLES $RWGETZ $RGETZ %IDMATCH %IDLIST $XEMBL $FH );
BEGIN {
# paths to SRS binaries
$RWGETZ = '/ebi/srs/srs/bin/osf_5/wgetz -e';
$RGETZ = '/ebi/srs/srs/bin/osf_5/getz -e';
$XEMBL = "cd /ebi/www/pages/cgi-bin/xembl/; ./XEMBL.pl";
#$EMBOSSDIR = '/ebi/services/pkgs/emboss/bin';
# RE matching the unique ID in the db entry
# - key is the
# - put the id string in parenthesis
%IDMATCH = ( # 123
embl => 'ID (\w+)',
fasta => '>\w+.(\w+)',
medlinefull => '[\n><]MedlineID. ?(\w+)',
swissprot => 'ID (\w+)',
pdb => '.{62}(\w+)',
bsml => 'DUMMY',
agave => 'DUMMY',
refseq => 'LOCUS ([\w_]+)'
);
%DBS = (
embl => {
fields => ['id', 'acc'],
version => 'sv', # name of the SRS field
format => {
default => 'embl',
embl => 1,
fasta => 'FastaSeqs',
bsml => 1,
agave => 1
}
},
medline => {
fields => ['id'],
format => {
default => 'medlinefull',
# medlineref => 'MedlineRef',
medlinefull => 'MedlineFull'
}
},
ensembl => {
fields => ['id'],
format => {
default => 'embl',
embl => 1,
fasta => 'FastaSeqs'
}
},
swall => {
fields => ['id', 'acc'],
format => {
default => 'swissprot',
swissprot => 1,
fasta => 'FastaSeqs'
}
},
pdb => {
fields => ['id'],
format => {
default => 'pdb',
pdb => '1'
}
},
refseq => {
fields => ['id', 'acc'],
format => {
default => 'refseq',
refseq => 1,
fasta => 'FastaSeqs'
}
}
#add more databases here...
);
%STYLES = (
html => 1,
raw => 1
);
%IDLIST = (); #redundancy check list built during the execution
}
my $q = new CGI;
# sanity checks if the script is running from command line
# and debug parameter is set.
my $debug = protect($q->param('debug')) if $q->param('debug');
&debugging if not $q->user_agent and $debug;
if ( $q->param('id') or $q->param('keywords') ) {
# pacify input strings
my $value;
$value = protect($q->param('id')) if $q->param('id');
$value = protect($q->param('keywords')) if $q->param('keywords');
my $db = lc protect($q->param('db')); # let's keep the case lower
my $format = lc protect($q->param('format'));
my $style = lc protect($q->param('style'));
# check input and set defaults
$style ||= 'html'; # default style
input_error($q, $style, "2 Unknown style [$style].") unless $STYLES{$style};
$db ||= 'embl'; # default db
input_error($q, $style, "1 Unknown database [$db].") unless $DBS{$db};
$format ||= $DBS{$db}{format}{default}; # default format
input_error($q, $style, "3 Format [$format] not known for database [$db]")
unless $DBS{$db}{format}{$format};
$format = $DBS{$db}{format}{default} if $format eq 'default';
# If people choose Bsml or AGAVE, DB can only be 'embl'
input_error($q, $style, "1 Unknown database [$db].")
if ($format eq 'bsml' or $format eq 'agave') and $db ne 'embl';
# If people choose Bsml or AGAVE, internal style has to be xml . Make it so.
$style = ($format =~ /(bsml|agave)/i) ? 'xml' : $style;
if ($style eq 'html') {
print $q->header(-type => 'text/html', -charset => 'UTF-8');
}
elsif ($style eq 'raw') {
print "Content-Type: text/plain; charset=UTF-8\n\n";
}
$FH = tempfile('dbfetchXXXXXX', DIR => TMPDIR, UNLINK => 1 ); #automatic unlinking
# Check the number of IDs
my @ids = split (/ /, $value);
input_error($q, $style, "6 Too many IDs [". scalar @ids. "]. Max [". MAXIDS. "] allowed.")
if scalar @ids > MAXIDS;
# XEMBL cannot 'glue' single entries due to XML setup
#- we need to send things in one go.
if ($style eq 'xml') {
&xml($format, @ids);
} else {
my $counter;
foreach my $id (@ids) {
&$style($db, $id, $format);
}
no_entries($q, $value) if $style eq 'html' and tell($FH) == 0;
}
seek $FH, 0, 0;
print '<pre>' if $style eq 'html';
print $_ while <$FH>;
} else {
print_prompt($q);
}
=head2 print_prompt
Title : print_prompt
Usage :
Function: Prints the default page with the query form
to STDOUT (Web page)
Args :
Returns :
=cut
sub print_prompt {
print $q->header(),
$q->start_html(-title => 'DB Entry Retrieval',
-bgcolor => 'white',
-author => 'heikki-at-bioperl-dot-org'
),
'<IMG align=middle SRC="/icons/ebibanner.gif">',
$q->h1('Generic DB Entry Retrieval'),
$q->p("This page allows you to retrieve up to ". MAXIDS .
" entries at the time from various up-to-date biological databases."),
$q->p("For EMBL, enter an accession number (e.g. J00231) or entry name (e.g.
BUM) or a sequence version (e.g. J00231.1), or any combination of them
separated by a non-word character into your browser's search dialog.
SWALL examples are: fos_human, p53_human.
For short Ensembl entries, try : AL122059, AL031002, AL031030 .
'Random' Medline entry examples are: 20063307, 98276153.
PDB entry examples are: 100D, 1FOS. Try NM_006732 for RefSeq.
Only one copy of the latest version of the entry is returned."),
$q->hr,
$q->startform,
$q->popup_menu(-name => 'db',
-values => ['EMBL',
'SWALL',
'PDB',
'Medline',
'Ensembl',
'RefSeq'
]),
$q->textfield(-name => 'id',
-size => 40,
-maxlength => 1000),
$q->popup_menu(-name => 'format',
-values => ['default','Fasta','bsml','agave']),
$q->popup_menu(-name => 'style',
-values => ['html','raw']),
$q->submit('Retrieve'),
$q->endform,
$q->hr,
$q->h2('Direct access'),
$q->p('For backward compatibility, the script defaults to EMBL:'),
$q->code('<A href="http://www.ebi.ac.uk/cgi-bin/dbfetch?J00231">
http://www.ebi.ac.uk/cgi-bin/dbfetch?J00231</a>'),
$q->p('but the preferred way of calling it is:'),
$q->code('<A href="http://www.ebi.ac.uk/cgi-bin/dbfetch?id=J00231.1,hsfos,bum">
http://www.ebi.ac.uk/cgi-bin/dbfetch?id=J00231.1,hsfos,bum</a>'),
$q->p('which can be extended to retrieve entries in alternative sequence formats
and other databases:'),
$q->code('<A href="http://www.ebi.ac.uk/cgi-bin/dbfetch?db=swall&format=fasta&id=fos_human">
http://www.ebi.ac.uk/cgi-bin/dbfetch?db=swall&format=fasta&id=fos_human</a>'),
$q->p('Set style to <code>raw</code> to retrieve plain text entries for computational purposes
and saving to disk:'),
$q->code('<A href="http://www.ebi.ac.uk/cgi-bin/dbfetch?db=medline&style=raw&id=21131735">
http://www.ebi.ac.uk/cgi-bin/dbfetch?db=medline&style=raw&id=21131735</a>'),
$q->p('There is now the possibility to retrieve EMBL sequences formatterd into two XML standards:
Bsml (Bioinformatic Sequence Markup Language - from
Labbook, Inc.) or as AGAVE (Architecture for Genomic Annotation,
Visualisation, and Exchange - from Labbook, Inc.). To do this, use the
formats \'bsml\' or \'agave\', as follows:'),
$q->code('<A href="http://www.ebi.ac.uk/cgi-bin/dbfetch?format=bsml&id=J00231">
http://www.ebi.ac.uk/cgi-bin/dbfetch?format=bsml&id=J00231</a><br>'),
$q->code('<A href="http://www.ebi.ac.uk/cgi-bin/dbfetch?format=agave&id=J00231">
http://www.ebi.ac.uk/cgi-bin/dbfetch?format=agave&id=J00231</a>'),
$q->p("Version numbers are not supported with the XML retrieval."),
$q->hr,
$q->address("Version $VERSION, $DATE, <a href=\"mailto:support\@ebi.ac.uk\">support\@ebi.ac.uk</a>"),
$q->end_html, "\n" ;
}
=head2 protect
Title : protect
Usage : $value = protect($q->param('id'));
Function:
Removes potentially dangerous characters from the input
string. At the same time, converts word separators into a
single space character.
Args : scalar, string with one or more IDs or accession numbers
Returns : scalar
=cut
sub protect {
my ($s) = @_;
$s =~ s![^\w\.\_]+! !g; # allow version numbers with '.' & RefSeq IDs with '_'
$s =~ s|^\W+||;
$s =~ s|\W+$||;
return $s;
}
=head2 input_error
Title : input_error
Usage : input_error($q, 'html', "Error message");
Function: Standard error message behaviour
Args : reference to the CGI object
scalar, string to display on input error.
Returns : scalar
=cut
sub input_error {
my ($q, $style, $s) = @_;
if ($style eq 'html' ) {
print $q->header,
$q->start_html(-title => 'DB Entry Retrieval: Input error',
-bgcolor => 'white'
),
"<h2>ERROR in input:<h2>$s\n",
$q->end_html, "\n";
} else {
print "Content-type: text/plain\n\n", "ERROR $s\n";
}
exit 0;
}
=head2 no_entries
Title : no_entries
Usage : no_entries($q, "Message");
Function: Standard behaviour when no entries found
Args : reference to the CGI object
scalar, string to display on input error.
Returns : scalar
=cut
sub no_entries {
my ($q, $value) = @_;
print $q->start_html(-title => 'DB Entry Retrieval: Input warning',
-bgcolor => 'white'
),
"<h2>Sorry, your query retrieved no entries.</h2>",
"Entries with [$value] where not found.",
"Please go back or press <a href=\"dbfetch\"><b>here</b></a> to try again",
$q->end_html, "\n";
exit 0;
}
=head2 raw
Title : raw
Usage :
Function: Retrieves a single database entry in plain text
Args : scalar, an ID
scaler, format
Returns : scalar
=cut
sub raw {
my ($db, $value, $format) = @_;
my ($srsq, $qdb, $entry, $id);
my ($seqformat) = '';
$seqformat = '-view '. $DBS{$db}{format}{$format}
if $format ne $DBS{$db}{format}{default};
my $version = '';
$value =~ /(.+)\.(.+)/;
$version = $2 if $2;
$value = $1 if $1;
# main db
$qdb = $db;
$srsq = '';
foreach my $field (@{$DBS{$db}{fields}}) {
$srsq .= " [$qdb-$field:$value] |";
}
chop $srsq;
# if database supports versions (EMBL, GenBank, RefSeq...)
if ($version) {
my $vfname = $DBS{$db}{version};
$srsq = "[$qdb-$vfname:$version] & (". $srsq. ")"
}
# print "rsh srs $RGETZ $seqformat $srsq\n";
$entry = `rsh srs "$RGETZ $seqformat '$srsq'"`;
$entry =~ s|EMBL[^\n]+\n||;
$entry =~ s|^\s+||g;
$entry =~ s|\s+$|\n|g;
my $idmatch = $IDMATCH{$format};
($id) = $entry =~ /$idmatch/;
# die if ID not found
input_error(' ', 'raw', "5 ID [$value] not found in database [$db].")
unless $id;
# my $tmp = substr($entry, 0, 20);
# print "Entry:$tmp\n";
# print "-----id=$id---\$1=$1----idmatch=$idmatch=format=$format=\n";
#
print $FH $entry unless $IDLIST{$id};
$IDLIST{$id} = 1;
}
=head2 html
Title : html
Usage :
Function: Retrieves a single database entry with HTML
hypertext links in place. Limits retieved enties to
ones with correct version if the string has '.' in it.
Args : scalar, a UID
scalar, format
Returns : scalar
=cut
sub html {
my ($db, $value, $format) = @_;
my ($srsq, $qdb, $entry, $id, $idmatch);
my ($seqformat) = '';
$seqformat = '-view '. $DBS{$db}{format}{$format}
if $format ne $DBS{$db}{format}{default};
my $version = '';
$value =~ /(.+)\.(.+)/;
$version = $2 if $2;
$value = $1 if $1;
# SWALL plain format at EBI
$seqformat .= ' -vn 2 ' if $db eq 'swall' or $db eq 'refseq';
$qdb = $db;
$srsq = '';
foreach my $field (@{$DBS{$db}{fields}}) {
$srsq .= " [$qdb-$field:$value] |";
}
chop $srsq;
# if database supports versions (EMBL...)
if ($version) {
my $vfname = $DBS{$db}{version};
$srsq = "[$qdb-$vfname:$version] & (". $srsq. ")"
}
# print "rsh srs $RWGETZ $seqformat $srsq\n";
### '-id EBISRS' is (hopefully) a temporary addtion until SRS HTML output is fixed
$entry = `rsh srs "$RWGETZ $seqformat '$srsq'"`;
return if $entry =~ /SRS error/;
$entry =~ s|^Content-type:[^\n]+\n||;
$entry =~ s|\n<A HREF[^\n]+\n||;
$entry =~ s|<A +HREF=\"?wgetz|<A HREF=http://srs6.ebi.ac.uk/srs6bin/cgi-bin/wgetz|g; #"\
$entry =~ s/\+-e\"/\+-e/g; #"
$entry =~ s|<BR>||g;
$entry =~ s|</?pre>||g;
$entry =~ s|\n+|\n|g;
$entry =~ s|^\n+||g;
$idmatch = $IDMATCH{$format};
($id) = $entry =~ /$idmatch/;
# my $tmp = substr($entry, 0, 20);
# print "Entry:$tmp\n";
# print "-----id=$id---\$1=$1----idmatch=$idmatch=format=$format=\n";
print $FH $entry unless $IDLIST{$id};
$IDLIST{$id} = 1;
}
=head2 xml
Title : xml
Usage :
Function: Retrieves an entry formatted as XML
Args : array, UID
scalar, format
Returns : scalar
=cut
sub xml {
my ($format, @ids) = @_;
my ($entry, $id, $content, $counter, $reg);
$content = ($ENV{'HTTP_USER_AGENT'} =~ /MSIE/) ? "Content-type: text/xml\n\n" :
"Content-type: text/plain\n\n";
$entry = "--format ".(($format eq "bsml") ? "Bsml" : "sciobj") .
" " . join (" ", @ids);
$entry = `rsh mercury "$XEMBL $entry"`;
$reg = (($format eq "bsml") ? '<Sequence id=' : '<contig length-');
$counter++ while $entry =~ /($reg)/g;
foreach my $idl (@ids) {
input_error($q, " ", "5 ID [$idl] not found in database [embl].")
if ($format eq "bsml" && $entry =~ "NOT EXIST: $idl") ||
($format eq "agave" && $entry =~ "NOT FOUND: $idl")
}
print $FH ($content . $entry);
}
=head2 debugging
Title : debugging
Usage : 'perl dbfetch'
Function:
Performs sanity checks on global hash %IDS when this script
is run from command line. %IDS holds the description of
formats and other crusial info for each database accessible
through the program.
Note that hash key 'version' is not tested as it should
only be in sequence databases.
Args : none
Returns : error messages to STDOUT
=cut
sub debugging {
foreach my $db (keys %DBS) {
my $status = 1;
# field
print "ERROR: [$db]: no SRS fields defined.".
" Give an array of field names?\n" and $status = 0
unless $DBS{$db}{fields};
print "ERROR: [$db]: SRS fields are not defined as an array.\n" and $status = 0
unless ref $DBS{$db}{fields} eq 'ARRAY';
# format
print "ERROR: [$db]: no formats defined.\n" and $status = 0
unless $DBS{$db}{format};
print "ERROR: [$db]: no default format defined.\n" and $status = 0
unless $DBS{$db}{format}{default};
my $format = $DBS{$db}{format}{default};
print "ERROR: [$db]: no format [$format] defined.".
" You declared it as a default and only.\n" and $status = 0
unless $DBS{$db}{format}{$format};
foreach my $dbformat (keys %{$DBS{$db}{format}}) {
print "ERROR: [$db]: format [$format] not defined in %IDMATCH.\n"
and $status = 0
unless $IDMATCH{$dbformat} or $dbformat eq 'default';
}
printf "%-12s%s", "[$db]", "OK\n" if $status;
}
exit;
}