-
Notifications
You must be signed in to change notification settings - Fork 1
/
grabsprot.pl
executable file
·294 lines (254 loc) · 7.65 KB
/
grabsprot.pl
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
#!/usr/bin/perl -s
#*************************************************************************
#
# Program: grabsprot
# File: grabsprot.pl
#
# Version: V1.1
# Date: 05.02.14
# Function: Grab a UniProt file from the UniProt website
#
# Copyright: (c) Dr. Andrew C. R. Martin, UCL, 2013-2014
# Author: Dr. Andrew C. R. Martin
# Address: Institute of Structural and Molecular Biology
# Division of Biosciences
# University College
# Gower Street
# London
# WC1E 6BT
# EMail: andrew@bioinf.org.uk
#
#*************************************************************************
#
# This program is not in the public domain, but it may be copied
# according to the conditions laid out in the accompanying file
# COPYING.DOC
#
# The code may be modified as required, but any modifications must be
# documented so that the person responsible can be identified. If
# someone else breaks this code, I don't want to be blamed for code
# that does not work!
#
# The code may not be sold commercially or included as part of a
# commercial product except as described in the file COPYING.DOC.
#
#*************************************************************************
#
# Description:
# ============
#
#*************************************************************************
#
# Usage:
# ======
#
#*************************************************************************
#
# Revision History:
# =================
# V1.0 20.12.13 Original
# V1.1 05.02.14 Changed -d option to use for DNA downloads
# and -debug for debugging
#
#*************************************************************************
use LWP::UserAgent;
use strict;
#*************************************************************************
$::URLUPtemplate = "http://www.uniprot.org/uniprot/%s.%s";
$::URLDNAtemplate = "http://www.ebi.ac.uk/ena/data/view/%s&display=%s";
$::OutTemplate = "%s.%s";
#*************************************************************************
UsageDie() if(defined($::h) || (scalar(@ARGV) == 0));
# Grab accession code from command line
my $ac = shift @ARGV;
my $outFile = "";
my $data;
if(defined($::d))
{
# First grab the SwissProt entry in txt format
my $url = CreateURL($::URLUPtemplate, $ac, "txt");
# Grab the SwissProt file
print "Grabbing: $url\n" if(defined($::v) || defined($::debug));
my $ok;
($ok, $data) = GetFile($url);
if(!$ok)
{
print STDERR "$data\n";
print STDERR "URL: $url\n";
exit 1;
}
# Find the DNA cross reference
my $dnaAccession = FindDNAAccession($data);
if($dnaAccession eq "")
{
print STDERR "Unable to find EMBL cross-reference\n";
exit 1;
}
if(defined($::v))
{
print "Identified DNA accession as: $dnaAccession\n";
}
# Build the URL to obtain the DNA data
my $type = ((defined($::f))?"fasta":"text");
$url = CreateURL($::URLDNAtemplate, $dnaAccession, $type);
# Build the output filename
my $outType = ((defined($::f))?"dna.fasta":"dna.txt");
$outFile = CreateFilename($ac, $outType, @ARGV);
# Grab the EMBL file
print "Grabbing: $url\n" if(defined($::v) || defined($::debug));
($ok, $data) = GetFile($url);
if(!$ok)
{
print STDERR "$data\n";
print STDERR "URL: $url\n";
exit 1;
}
}
else
{
my $type = ((defined($::f))?"fasta":"txt");
# Create the URL and output filename
my $url = CreateURL($::URLUPtemplate, $ac, $type);
$outFile = CreateFilename($ac, $type, @ARGV);
# Grab the file
print "Grabbing: $url\n" if(defined($::v) || defined($::debug));
my $ok;
($ok, $data) = GetFile($url);
if(!$ok)
{
print STDERR "$data\n";
print STDERR "URL: $url\n";
exit 1;
}
}
if(!WriteData($data, $outFile))
{
print STDERR "Can't write file: $outFile\n";
exit 1;
}
#*************************************************************************
sub FindDNAAccession
{
my($data) = @_;
my @lines = split(/\n/, $data);
# Look for an EMBL mRNA entry
foreach my $line (@lines)
{
if($line =~ /^DR\s+EMBL;\s+(.*?);.*mRNA/)
{
return($1);
}
}
# Look for an EMBL mRNA entry
foreach my $line (@lines)
{
if($line =~ /^DR\s+EMBL;\s+(.*?);\s+(.*?);.*Genomic_DNA/)
{
my $ac = $2;
$ac =~ s/\..*//;
return($ac);
}
}
return("");
}
#*************************************************************************
# Writes the data to the specified file. If the filename is '-' or
# 'stdout', then it writes to standard output
#
# 28.02.13 Original By: ACRM
sub WriteData
{
my($data, $file) = @_;
print STDERR "Writing data file: $file\n" if(defined($::debug));
if(($file eq "-") || ($file eq "stdout"))
{
print $data;
}
else
{
if(open(FILE, ">$file"))
{
print FILE $data;
close FILE;
}
else
{
return 0;
}
}
return(1);
}
#*************************************************************************
# Generates a filename from the command line if one was specified,
# otherwise builds a filename from the accession and type using the global
# template.
#
# 20.12.13 Original By: ACRM
sub CreateFilename
{
my($ac, $type, @args) = @_;
if(scalar(@args))
{
return($args[0]);
}
my $fileName = sprintf($::OutTemplate, $ac, $type);
print STDERR "Output filename: $fileName\n" if(defined($::debug));
return($fileName);
}
#*************************************************************************
# Creates a URL from the accession code and the global URL template
#
# 20.12.13 Original By: ACRM
# 05.02.14 Takes $template as a parameter
sub CreateURL
{
my($template, $ac, $type) = @_;
$ac = "\U$ac";
my $url = sprintf($template, $ac, $type);
return($url);
}
#*************************************************************************
# Grabs a file using the LWP package
# Returns two values: success (TRUE/FALSE) and the data (content if all
# was OK, otherwise the error message)
#
# 28.02.13 Original By: ACRM
sub GetFile
{
my ($url) = @_;
my $ua = LWP::UserAgent->new;
$ua->agent("grabpdb/0.1 ");
my $req = HTTP::Request->new(GET => $url);
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success)
{
return(1, $res->content);
}
return(0, $res->status_line);
}
#*************************************************************************
# Prints a usage message and exits
#
# 20.12.13 Original By: ACRM
sub UsageDie
{
my $autoname = sprintf($::OutTemplate, "XXXX", "type");
print <<__EOF;
grabsprot V1.1 (c) 2013-2014, Dr. Andrew C.R. Martin, UCL
Usage: grabsprot [-f][-d][-v][-debug] accode [filename|-|stdout]
-f Grab FASTA format rather than full data file
-d Grab DNA data from EMBL-ENA
-v Verbose
-debug Debug mode (prints more information)
accode Accession (or identifier) code to grab
(upper or lower case)
filename Output file to write (default to $autoname)
- (or stdout) Output to standard output
Grabs a UniProt file from the UniProt web site and writes it to a local file
or to standard output. If the -d option is given, then the program
initially grabs the UniProt entry, looks up the cross-reference to EMBL-ENA
and grabs the first appropriate (spliced) DNA for the sequence.
__EOF
exit 0;
}