Debian Codegraph @

scripts/ann_feats_up_www2.pl

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/env perl
2
3 ################################################################
4 # copyright (c) 2014,2015 by William R. Pearson and The Rector &
5 # Visitors of the University of Virginia */
6 ################################################################
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing,
14 # software distributed under this License is distributed on an "AS
15 # IS" BASIS, WITHOUT WRRANTIES OR CONDITIONS OF ANY KIND, either
16 # express or implied. See the License for the specific language
17 # governing permissions and limitations under the License.
18 ################################################################
19
20 ## modified 29-Sept-2016 to use EBI/proteins JSON URL:
21 ## https://www.ebi.ac.uk/proteins/api/features/p12345
22
23 # ann_feats_up_www2.pl gets an annotation file from fasta36 -V with a line of the form:
24
25 # SP:GSTM1_HUMAN P09488 218
26 #
27 # it must:
28 # (1) read in the line
29 # (2) parse it to get the up_acc
30 # (3) return the tab delimited features
31 #
32
33 # this version can read feature2 uniprot features (acc/pos/end/label/value), but returns sorted start/end domains
34
35 use warnings;
36 use strict;
37
38 use Getopt::Long;
39 use Pod::Usage;
40 use LWP::Simple;
41 use LWP::UserAgent;
42 use JSON qw(decode_json);
43
44 ## use IO::String;
45
46 my $ua = LWP::UserAgent->new(ssl_opts=>{verify_hostname => 0});
47 my $up_base = 'https://www.ebi.ac.uk/proteins/api/features';
48 my $uniprot_suff = ".json";
49
50 my %domains = ();
51 my $domain_cnt = 0;
52
53 my $hostname = `/bin/hostname`;
54
55 my ($sstr, $lav, $neg_doms, $no_doms, $no_feats, $no_vars, $no_over, $data_file, $shelp, $help) = (0,0,0,0,0,0,0,0,0,0);
56 my ($min_nodom) = (10);
57
58 my ($show_color) = (1);
59 my $color_sep_str = " :";
60 $color_sep_str = '~';
61
62 GetOptions(
63 "lav" => \$lav,
64 "no-over" => \$no_over,
65 "no_doms" => \$no_doms,
66 "no-doms" => \$no_doms,
67 "nodoms" => \$no_doms,
68 "no_vars" => \$no_vars,
69 "no-vars" => \$no_vars,
70 "novars" => \$no_vars,
71 "neg" => \$neg_doms,
72 "neg_doms" => \$neg_doms,
73 "neg-doms" => \$neg_doms,
74 "negdoms" => \$neg_doms,
75 "min_nodom=i" => \$min_nodom,
76 "no_feats" => \$no_feats,
77 "no-feats" => \$no_feats,
78 "nofeats" => \$no_feats,
79 "data:s" => \$data_file,
80 "color!" => \$show_color,
81 "sstr" => \$sstr,
82 "h|?" => \$shelp,
83 "help" => \$help,
84 );
85
86 pod2usage(1) if $shelp;
87 pod2usage(exitstatus => 0, verbose => 2) if $help;
88 pod2usage(1) unless @ARGV || $data_file || -p STDIN || -f STDIN;
89
90 my @feat_keys = qw( ACT_SITE MOD_RES BINDING METAL SITE );
91 my @feat_vals = ( '=','*','#','^','@');
92 my @feat_names = ('Active site', 'Modified', 'Binding', 'Metal binding', 'Site');
93
94 unless ($no_vars) {
95 push @feat_keys, qw(MUTAGEN VARIANT);
96 push @feat_vals, ('V','V',);
97 push @feat_names, ("","",);
98 }
99
100 my %feats_text = ();
101 @feats_text{@feat_keys} = @feat_names;
102
103 my %feats_label;
104 @feats_label{@feat_keys} = @feat_names;
105
106 my @dom_keys = qw( DOMAIN REPEAT );
107 my @dom_vals = ( [ '[', ']'],[ '[', ']']);
108
109 my @ssr_keys = qw(STRAND HELIX);
110 my @ssr_vals = ( [ '[', ']'], [ '[', ']']);
111
112 my %annot_types = ();
113
114 my $get_annot_sub = \&json_annots;
115 if ($lav) {
116 $no_feats = 1;
117 }
118
119 if ($sstr) {
120 @annot_types{@ssr_keys} = @ssr_vals;
121 } else {
122 @annot_types{@feat_keys} = @feat_vals unless ($no_feats);
123 @annot_types{@dom_keys} = @dom_vals unless ($no_doms);
124 }
125
126 if ($neg_doms) {
127 $domains{'NODOM'}=0;
128 }
129
130 my ($tmp, $gi, $sdb, $acc, $id, $use_acc);
131
132 unless ($no_feats || $sstr) {
133 for my $i ( 0 .. $#feat_keys) {
134 next unless $feats_label{$feat_keys[$i]};
135 print "=",$feat_vals[$i],":",$feats_label{$feat_keys[$i]},"\n";
136 }
137 }
138
139 # get the query
140 my ($query, $seq_len) = @ARGV;
141 $seq_len = 0 unless defined($seq_len);
142
143 $query =~ s/^>// if ($query);
144
145 my @annots = ();
146
147 #if it's a file I can open, read and parse it
148
149 unless ($data_file) {
150 # unless ($query && ($query =~ m/[\|:]/
151 # || $query =~ m/^[OPQ][0-9][A-Z0-9]{3}[0-9]|[A-NR-Z][0-9]([A-Z][A-Z0-9]{2}[0-9]){1,2}\s/
152 # || $query =~ m/^(XN)(MP)_\d+/)) {
153 if (! $query || -r $query) {
154 while (my $a_line = <>) {
155 $a_line =~ s/^>//;
156 chomp $a_line;
157 push @annots, lwp_annots($a_line, $get_annot_sub);
158 }
159 } else {
160 push @annots, lwp_annots("$query\t$seq_len", $get_annot_sub);
161 }
162 } else { # just read the data from a file, give to $get_annot_sub().
163 my %annot_data = (seq_info => ">$data_file DATA");
164
165 open(DATA_IN, $data_file) || die "Cannot read $data_file";
166
167 my $lwp_data = "";
168 while (<DATA_IN>) {
169 $lwp_data .= $_;
170 }
171
172 $annot_data{list} = $get_annot_sub->(\%annot_types, $lwp_data,0);
173
174 push @annots, \%annot_data;
175 }
176
177
178 for my $seq_annot (@annots) {
179 print ">",$seq_annot->{seq_info},"\n";
180 for my $annot (@{$seq_annot->{list}}) {
181 if (!$lav && $show_color && defined($domains{$annot->[-1]})) {
182 $annot->[-1] .= $color_sep_str.$domains{$annot->[-1]};
183 }
184 print join("\t",@$annot),"\n";
185 }
186 }
187
188 exit(0);
189
190 sub lwp_annots {
191 my ($query_len, $get_annot_sub) = @_;
192
193 my ($annot_line, $seq_len) = split(/\t/,$query_len);
194
195 my %annot_data = (seq_info=>$annot_line);
196
197 if ($annot_line =~ m/^gi\|/) {
198 ($tmp, $gi, $sdb, $acc, $id) = split(/\|/,$annot_line);
199 } elsif ($annot_line =~ m/^(SP|TR):(\w+)\s(\w+)/) {
200 ($sdb, $id, $acc) = (lc($1), $2, $3);
201 } elsif ($annot_line =~ m/^(SP|TR):(\w+)/) {
202 ($sdb, $id, $acc) = (lc($1), $2, "");
203 warn("*** $0 accession required: $annot_line\n");
204 } elsif ($annot_line =~ m/^(UR\d{3}:UniRef\d{2})_(\w+)/) {
205 $sdb = lc($1);
206 $id = $2;
207 # $acc = $2;
208 } elsif ($annot_line =~ m/\|/) {
209 ($sdb, $acc, $id) = split(/\|/,$annot_line);
210 }
211 else {
212 ($acc) = ($annot_line =~ m/^(\S+)/);
213 }
214
215 $acc =~ s/\.\d+// if ($acc);
216
217 $annot_data{list} = [];
218 my $lwp_features = "";
219
220 if ($acc && ($acc =~ m/^[A-Z][0-9][A-Z0-9]{3}[0-9]/)) {
221 $lwp_features = get_https("$up_base/$acc.json");
222 }
223 # elsif ($id && ($id =~ m/^\w+$/)) {
224 # $lwp_features = get("$up_base/$id/$gff_post");
225 # }
226
227 if ($lwp_features && ($lwp_features !~ /ERROR/)) {
228 my $annot_json = decode_json($lwp_features);
229 $annot_data{list} = $get_annot_sub->(\%annot_types, $annot_json, $seq_len);
230 }
231
232 return \%annot_data;
233 }
234
235 ####
236 # parses www.ebi.ac.uk/uniprot/api json
237 #
238 sub json_annots {
239 my ($annot_types, $json_ref, $seq_len) = @_;
240
241 my ($acc, $pos, $end, $label, $value, $comment, $len);
242
243 $seq_len = 0;
244
245 my @feats2 = (); # features with start/stop, for checking overlap, adding negative
246 my @sites = (); # sites with one position
247
248 my ($seq_str, $seq_acc, $seq_id) = @{$json_ref}{qw(sequence accession entryName)};
249 $seq_len = length($seq_str);
250
251 for my $feat ( @{$json_ref->{features}} ) {
252 if ($annot_types->{$feat->{type}}) {
253
254 my ($label, $pos, $end, $value) = @{$feat}{qw(type begin end description)};
255
256 $pos =~ s/[<>]//g;
257 $end =~ s/[<>]//g;
258
259 if ($label =~ m/DOMAIN/ || $label =~ m/REPEAT/) {
260 $value = domain_name($label,$value);
261 push @feats2, [$pos, "-", $end, $value];
262 } elsif ($label =~ m/HELIX/) {
263 push @feats2, [$pos, "-", $end, $label];
264 } elsif ($label =~ m/STRAND/) {
265 push @feats2, [$pos, "-", $end, $label];
266 } elsif ($label =~ m/VARIANT/ || $label =~ m/MUTAGEN/) {
267 my $alt = $feat->{alternativeSequence};
268 if ($alt eq '') { $alt = 'X';}
269 elsif (length($alt) > 1) { $alt=substr($alt,0,1);}
270 push @sites, [$pos, $annot_types->{$label}, $alt, $value];
271 }
272 else {
273 next unless ($pos == $end);
274 if ($feats_text{$label}) {
275 my $info = $feats_text{$label};
276 if ($value) {
277 $info .= ": $value";
278 }
279 push @sites, [$pos, $annot_types->{$label}, "-", $info];
280 } else {
281 push @sites, [$pos, $annot_types->{$label}, "-", $value];
282 }
283 }
284 }
285 }
286
287 @feats2 = sort { $a->[0] <=> $b->[0] } @feats2;
288
289 if ($no_over) {
290 # check for containment
291 my $have_contained = 0;
292 my $last_container = 0;
293 for (my $i=1; $i < scalar(@feats2); $i++) {
294 if ($feats2[$i]->[0] >= $feats2[$last_container]->[0] && $feats2[$i]->[2] <= $feats2[$last_container]->[2]) {
295 $feats2[$i]->[1] = 'Delete';
296 $have_contained = 1;
297 } else {
298 $last_container=$i;
299 }
300 }
301
302 if ($have_contained) {
303 @feats2 = grep { $_->[1] !~ /Delete/ } @feats2;
304 }
305
306 # ensure that domains do not overlap
307 for (my $i=1; $i < scalar(@feats2); $i++) {
308 my $diff = $feats2[$i-1]->[2] - $feats2[$i]->[0];
309 if ($diff >= 0) {
310 $feats2[$i-1]->[2] = $feats2[$i]->[0]+ int($diff/2);
311 $feats2[$i]->[0] = $feats2[$i-1]->[2] + 1;
312 }
313 }
314 }
315
316 my @n_feats2 = ();
317
318 if ($neg_doms) {
319 my $last_end = 0;
320 for my $feat ( @feats2 ) {
321 if ($feat->[0] - $last_end > $min_nodom) {
322 push @n_feats2, [$last_end+1, "-", $feat->[0]-1, "NODOM"];
323 }
324 $last_end = $feat->[2];
325 }
326 if ($seq_len - $last_end > $min_nodom) {
327 push @n_feats2, [$last_end+1, "-", $seq_len, "NODOM"];
328 }
329 }
330
331 my @feats = ();
332 for my $feat (@feats2, @n_feats2) {
333 if (!$lav) {
334 push @feats, [$feat->[0], '-', $feat->[2], $feat->[-1] ];
335 # push @feats, [$feat->[2], ']', '-', ""];
336 }
337 else {
338 push @feats, [$feat->[0], $feat->[2], $feat->[-1]];
339 }
340 }
341
342 @feats = sort { $a->[0] <=> $b->[0] } (@sites, @feats);
343
344 return \@feats;
345 }
346
347 # domain name takes a uniprot domain label, removes comments ( ;
348 # truncated) and numbers and returns a canonical form. Thus:
349 # Cortactin 6.
350 # Cortactin 7; truncated.
351 # becomes "Cortactin"
352 #
353
354 sub domain_name {
355
356 my ($label, $value) = @_;
357
358 $value = 'UnDef' unless $value;
359
360 $value =~ s/ /_/g;
361
362 if ($label =~ /Domain|Repeat/i) {
363 $value =~ s/;.*$//;
364 $value =~ s/\.\s*$//;
365 $value =~ s/\s+\d+$//;
366 if (!defined($domains{$value})) {
367 $domain_cnt++;
368 $domains{$value} = $domain_cnt;
369 }
370 return $value;
371 }
372 else {
373 return $value;
374 }
375 }
376
377 sub get_https {
378 my ($url) = @_;
379
380 my $result = "";
381 my $response = $ua->get($url);
382
383 if ($response->is_success) {
384 $result = $response->decoded_content;
385 } else {
386 $result = '';
387 }
388 return $result;
389 }
390
391
392 __END__
393
394 =pod
395
396 =head1 NAME
397
398 ann_feats_up_www2.pl
399
400 =head1 SYNOPSIS
401
402 ann_feats_up_www2.pl --no_doms --no_feats --lav 'sp|P09488|GSTM1_NUMAN' | accession.file
403
404 =head1 OPTIONS
405
406 -h short help
407 --help include description
408 --no-doms do not show domain boundaries (domains are always shown with --lav)
409 --no-feats do not show feature (variants, active sites, phospho-sites)
410 --lav produce lav2plt.pl annotation format, only show domains/repeats
411
412 --neg-doms, -- report domains between annotated domains as NODOM
413 (also --neg, --neg_doms)
414 --min_nodom=10 -- minimum length between domains for NODOM
415
416 --host, --user, --password, --port --db -- info for mysql database
417
418 =head1 DESCRIPTION
419
420 C<ann_feats_up_www2.pl> extracts feature, domain, and repeat
421 information from the Uniprot DAS server through an XSLT transation
422 provided by https://www.ebi.ac.uk/Tools/dbfetch/dbfetch/uniprotkb.
423 This server provides GFF descriptions of Uniprot entries, with most of
424 the information provided in UniProt feature tables.
425
426 C<ann_feats_up_www2.pl> is an alternative to C<ann_pfam.pl> and
427 C<ann_pfam.pl> that does not require a local MySQL copy of Pfam.
428
429 Given a command line argument that contains a sequence accession
430 (P09488), the program looks up the features available for that
431 sequence and returns them in a tab-delimited format:
432
433 >sp|P09488|GSTM1_HUMAN
434 2 [ - GST N-terminal :1
435 7 V F Mutagen: Reduces catalytic activity 100- fold.
436 23 * - MOD_RES: Phosphotyrosine (By similarity).
437 33 * - MOD_RES: Phosphotyrosine (By similarity).
438 34 * - MOD_RES: Phosphothreonine (By similarity).
439 88 ] -
440 90 [ - GST C-terminal :2
441 108 V Q Mutagen: Reduces catalytic activity by half.
442 108 V S Mutagen: Changes the properties of the enzyme toward some substrates.
443 109 V I Mutagen: Reduces catalytic activity by half.
444 116 # - BINDING: Substrate.
445 116 V A Mutagen: Reduces catalytic activity 10-fold.
446 116 V F Mutagen: Slight increase of catalytic activity.
447 173 V N in allele GSTM1B; dbSNP:rs1065411.
448 208 ] -
449 210 V T in dbSNP:rs449856.
450
451 If features are provided, then a legend of feature symbols is provided
452 as well:
453
454 =*:phosphorylation
455 ==:active site
456 =@:site
457 =^:binding
458 =!:metal binding
459
460 If the C<--lav> option is specified, domain and repeat features are
461 presented in a different format for the C<lav2plt.pl> program:
462
463 >sp|P09488|GSTM1_HUMAN
464 2 88 GST N-terminal.
465 90 208 GST C-terminal.
466
467 C<ann_feats_up_www2.pl> is designed to be used by the B<FASTA> programs with
468 the C<-V \!ann_feats_up_www2.pl> option. It can also be used with the lav2plt.pl
469 program with the C<--xA "\!ann_feats_up_www2.pl --lav"> or C<--yA "\!ann_feats_up_www2.pl --lav"> options.
470
471 =head1 AUTHOR
472
473 William R. Pearson, [email protected]
474
475 =cut