Debian Codegraph @

scripts/ann_pfam30_tmptbl.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 # ann_pfam.pl gets an annotation file from fasta36 -V with a line of the form:
21
22 # gi|62822551|sp|P00502|GSTA1_RAT Glutathione S-transfer\n (at least from pir1.lseg)
23 #
24 # it must:
25 # (1) read in the line
26 # (2) parse it to get the up_acc
27 # (3) return the tab delimited features
28 #
29
30 # this is the first version that works with the new Pfam strategy of
31 # separating Uniprot reference sequences from the rest of uniprot. as
32 # a result, it is possible that 2 SQL queries will be required, one to
33 # pfamA_reg_full_significant and a second to uniprot_reg_full.
34
35 # modified 15-Jan-2017 to reduce the number of calls when the same
36 # accession is present multiple times. Accessions are saved in a hash
37 # than ensures uniqueness.
38 #
39 # Uses tmp_annot.temporary table for more rapid joins. $user must have
40 # create temporary tables/select permissions for tmp_annot
41 #
42
43 use warnings;
44 use strict;
45
46 use DBI;
47 use Getopt::Long;
48 use Pod::Usage;
49 use File::Temp qw/tempfile/;
50
51 use vars qw($host $db $port $user $pass);
52
53 my $hostname = `/bin/hostname`;
54
55 ($host, $db, $port, $user, $pass) = ("wrpxdb.its.virginia.edu", "pfam30", 0, "web_user", "fasta_www");
56 #$host = 'xdb';
57 #$host = 'localhost';
58 #$db = 'RPD2_pfam28u';
59
60 my ($auto_reg,$rpd2_fams, $neg_doms, $vdoms, $lav, $no_doms, $no_clans, $pf_acc, $acc_comment, $bound_comment, $shelp, $help) =
61 (0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0,);
62 my ($no_over, $split_over, $over_fract) = (0, 0, 3.0);
63
64 my ($color_sep_str, $show_color) = (" :",1);
65 $color_sep_str = '~';
66
67 my ($min_nodom, $min_vdom) = (10,10);
68
69 GetOptions(
70 "host=s" => \$host,
71 "db=s" => \$db,
72 "user=s" => \$user,
73 "password=s" => \$pass,
74 "port=i" => \$port,
75 "lav" => \$lav,
76 "acc_comment" => \$acc_comment,
77 "bound_comment" => \$bound_comment,
78 "color!" => \$show_color,
79 "no-over" => \$no_over,
80 "no_over" => \$no_over,
81 "split-over" => \$split_over,
82 "split_over" => \$split_over,
83 "over_fract" => \$over_fract,
84 "over-fract" => \$over_fract,
85 "no-clans" => \$no_clans,
86 "no_clans" => \$no_clans,
87 "neg" => \$neg_doms,
88 "neg_doms" => \$neg_doms,
89 "neg-doms" => \$neg_doms,
90 "min_nodom=i" => \$min_nodom,
91 "vdoms" => \$vdoms,
92 "v_doms" => \$vdoms,
93 "pfacc" => \$pf_acc,
94 "RPD2" => \$rpd2_fams,
95 "auto_reg" => \$auto_reg,
96 "h|?" => \$shelp,
97 "help" => \$help,
98 );
99
100 pod2usage(1) if $shelp;
101 pod2usage(exitstatus => 0, verbose => 2) if $help;
102 pod2usage(1) unless (@ARGV || -p STDIN || -f STDIN);
103
104 my $connect = "dbi:mysql(AutoCommit=>1,RaiseError=>1):database=$db";
105 $connect .= ";host=$host" if $host;
106 $connect .= ";port=$port" if $port;
107
108 my $dbh = DBI->connect($connect,
109 $user,
110 $pass
111 ) or die $DBI::errstr;
112
113 my %annot_types = ();
114 my %domains = (NODOM=>0);
115 my %domain_clan = (NODOM => {clan_id => 'NODOM', clan_acc=>0, domain_cnt=>0});
116 my @domain_list = (0);
117 my $domain_cnt = 0;
118
119 my $pfamA_reg_full = 'pfamA_reg_full_significant';
120 my $uniprot_reg_full = 'uniprot_reg_full';
121
122 my @pfam_fields = qw(seq_start seq_end model_start model_end model_length pfamA_acc pfamA_id auto_pfamA_reg_full evalue length);
123 my @upfam_fields = qw(seq_start seq_end model_start model_end model_length pfamA_acc pfamA_id auto_uniprot_reg_full length);
124
125 my $get_pfam_acc = $dbh->prepare(<<EOSQL);
126 SELECT t_acc, seq_start, seq_end, model_start, model_end, model_length, pfamA_acc, pfamA_id, auto_pfamA_reg_full, domain_evalue_score as evalue, length
127 FROM pfamseq
128 JOIN pfamA_reg_full_significant using(pfamseq_acc)
129 JOIN pfamA USING (pfamA_acc)
130 JOIN tmp_annot.targets on(pfamseq_acc=t_acc)
131 WHERE in_full = 1
132 ORDER BY t_acc,seq_start
133
134 EOSQL
135
136 my $get_upfam_acc = $dbh->prepare(<<EOSQL);
137 SELECT t_acc, seq_start, seq_end, model_start, model_end, model_length, pfamA_acc, pfamA_id, auto_uniprot_reg_full as auto_pfamA_reg_full, domain_evalue_score as evalue, length
138 FROM uniprot
139 JOIN uniprot_reg_full using(uniprot_acc)
140 JOIN pfamA USING (pfamA_acc)
141 JOIN tmp_annot.targets on(uniprot_acc=t_acc)
142 WHERE in_full = 1
143 ORDER BY t_acc, seq_start
144
145 EOSQL
146
147 my $get_pfam_refacc = $dbh->prepare(<<EOSQL);
148 SELECT t_acc, seq_start, seq_end, model_start, model_end, model_length, pfamA_acc, pfamA_id, auto_pfamA_reg_full, domain_evalue_score as evalue, length
149 FROM $pfamA_reg_full
150 JOIN pfamseq using(pfamseq_acc)
151 JOIN pfamA USING (pfamA_acc)
152 JOIN uniprot.refseq2up as rf2up on(rf2up.up_acc=pfamseq_acc)
153 JOIN tmp_annot.targets on(rf2up.refseq_acc=t_acc)
154 WHERE in_full = 1
155 ORDER BY t_acc, seq_start
156
157 EOSQL
158
159 my $get_upfam_refacc = $dbh->prepare(<<EOSQL);
160 SELECT t_acc,seq_start, seq_end, model_start, model_end, model_length, pfamA_acc, pfamA_id, auto_uniprot_reg_full as auto_pfamA_reg_full, domain_evalue_score as evalue, length
161 FROM uniprot
162 JOIN uniprot_reg_full using(uniprot_acc)
163 JOIN pfamA USING (pfamA_acc)
164 JOIN uniprot.refseq2up as rf2up on(rf2up.up_acc=uniprot_acc)
165 JOIN tmp_annot.targets on(rf2up.refseq_acc=t_acc)
166 WHERE in_full = 1
167 ORDER BY t_acc, seq_start
168
169 EOSQL
170
171 my $get_annots_sql = $get_pfam_acc;
172 my $get_annots_sql_u = $get_upfam_acc;
173
174 my $get_pfam_id = $dbh->prepare(<<EOSQL);
175 SELECT t_acc, seq_start, seq_end, model_start, model_end, model_length, pfamA_acc, pfamA_id, auto_pfamA_reg_full, domain_evalue_score as evalue, length
176 FROM pfamseq
177 JOIN $pfamA_reg_full using(pfamseq_acc)
178 JOIN pfamA USING (pfamA_acc)
179 JOIN tmp_annot.targets on(pfamseq_id=t_acc)
180 WHERE in_full=1
181 ORDER BY t_acc, seq_start
182
183 EOSQL
184
185 my $get_upfam_id = $dbh->prepare(<<EOSQL);
186 SELECT t_acc, seq_start, seq_end, model_start, model_end, model_length, pfamA_acc, pfamA_id, auto_uniprot_reg_full as auto_pfamA_reg_full, domain_evalue_score as evalue, length
187 FROM uniprot
188 JOIN uniprot_reg_full using(pfamseq_acc)
189 JOIN pfamA USING (pfamA_acc)
190 JOIN tmp_annot.targets on(uniprot_id=t_acc)
191 WHERE in_full=1
192 ORDER BY t_acc, seq_start
193
194 EOSQL
195
196 my $get_pfam_clan = $dbh->prepare(<<EOSQL);
197
198 SELECT clan_acc, clan_id
199 FROM clan
200 JOIN clan_membership using(clan_acc)
201 WHERE pfamA_acc=?
202
203 EOSQL
204
205 my $get_rpd2_clans = $dbh->prepare(<<EOSQL);
206
207 SELECT auto_pfamA, clan
208 FROM ljm_db.RPD2_final_fams
209 WHERE clan is not NULL
210
211 EOSQL
212
213 $dbh->do(<<EOSQL);
214 create temporary table tmp_annot.targets (t_acc char(10) primary key)
215 EOSQL
216
217 # -- LEFT JOIN clan_membership USING (auto_pfamA)
218 # -- LEFT JOIN clans using(auto_clan)
219
220 my ($tmp, $gi, $sdb, $acc, $id, $use_acc);
221
222 my @lav_list = qw(seq_start seq_end info);
223 my @no_lav_list = qw(seq_start dash seq_end info);
224 my $out_list_r = \@no_lav_list;
225 if ($lav) {
226 $show_color = 0;
227 $out_list_r = \@lav_list;
228 }
229
230 # get the query
231 my ($query, $seq_len) = @ARGV;
232 $seq_len = 0 unless defined($seq_len);
233
234 $query =~ s/^>// if ($query);
235
236 my @annots = ();
237 my %annot_set = ();
238
239 my %rpd2_clan_fams = ();
240
241 if ($rpd2_fams) {
242 $get_rpd2_clans->execute();
243 my ($auto_pfam, $auto_clan);
244 while (($auto_pfam, $auto_clan)=$get_rpd2_clans->fetchrow_array()) {
245 $rpd2_clan_fams{$auto_pfam} = $auto_clan;
246 }
247 }
248
249 #if it's a file I can open, read and parse it
250 #unless ($query && $query =~ m/[\|:]/) {
251
252 if (! $query || -r $query) {
253 while (my $a_line = <>) {
254 $a_line =~ s/^>//;
255 chomp $a_line;
256 push @annots, show_annots($a_line);
257 }
258 }
259 else {
260 push @annots, show_annots("$query\t$seq_len");
261 }
262
263 # @annots has a list of id's or annotations
264 # write to temporary file, load data local infile, join to get results
265
266 my ($fh, $temp_file) = tempfile(TEMPLATE=>'accannXXXXX');
267 my @u_annots = keys %annot_set;
268 print $fh join("\n",@u_annots);
269 close($fh);
270
271 $dbh->do("load data local infile '$temp_file' into table tmp_annot.targets");
272
273 unlink($temp_file);
274
275 # $get_annots_sql->execute();
276 # while (my $annot_ar = $get_annots_sql->fetchrow_arrayref()) {
277 # my %annot_data = ();
278 # @annot_data{@pfam_fields} = @{$annot_ar}[1..10];
279 # if (!defined($annot_set{$annot_ar->[0]}->{list})) {
280 # $annot_set{$annot_ar->[0]}->{list} = [\%annot_data];
281 # }
282 # else {
283 # push @{$annot_set{$annot_ar->[0]}->{list}}, \%annot_data;
284 # }
285 # }
286
287 $get_annots_sql_u->execute();
288 while (my $annot_hr = $get_annots_sql_u->fetchrow_hashref()) {
289 if (!defined($annot_set{$annot_hr->{t_acc}}->{list})) {
290 $annot_set{$annot_hr->{t_acc}}->{list} = [$annot_hr];
291 }
292 else {
293 push @{$annot_set{$annot_hr->{t_acc}}->{list}}, $annot_hr;
294 }
295 }
296
297 for my $u_acc (@u_annots) {
298 map_pfam_annots($annot_set{$u_acc});
299 }
300
301 for my $seq_annot (@annots) {
302 next unless $seq_annot;
303 my $annot_r = $annot_set{$seq_annot};
304 print ">",$annot_r->{seq_info},"\n";
305 for my $annot (@{$annot_r->{list}}) {
306 $annot->{dash} = '-';
307 if (defined($domains{$annot->{info}})) {
308 my ($a_name, $a_num) = domain_num($annot->{info},$domains{$annot->{info}});
309 $annot->{info} = $a_name;
310 my $tmp_a_num = $a_num;
311 $tmp_a_num =~ s/v$//;
312 if ($acc_comment) {
313 $annot->{info} .= "{$domain_list[$tmp_a_num]}";
314 }
315 if ($bound_comment) {
316 $annot->{info} .= $color_sep_str.$annot->{seq_start}.":".$annot->{seq_end};
317 }
318 elsif ($show_color) {
319 $annot->{info} .= $color_sep_str.$a_num;
320 }
321 }
322 print join("\t",@{$annot}{@{$out_list_r}}),"\n";
323 }
324 }
325
326 exit(0);
327
328 sub show_annots {
329 my ($query_len) = @_;
330
331 my ($annot_line, $seq_len) = split(/\t/,$query_len);
332
333 my $pfamA_acc;
334
335 $use_acc = 1;
336 $get_annots_sql = $get_pfam_acc;
337 $get_annots_sql_u = $get_upfam_acc;
338
339 if ($annot_line =~ m/^pf\d+\|/) {
340 ($sdb, $gi, $pfamA_acc, $acc, $id) = split(/\|/,$annot_line);
341 # $dbh->do("use RPD2_pfam");
342 }
343 elsif ($annot_line =~ m/^gi\|/) {
344 ($tmp, $gi, $sdb, $acc, $id) = split(/\|/,$annot_line);
345 if ($sdb =~ m/ref/) {
346 $get_annots_sql = $get_pfam_refacc;
347 $get_annots_sql_u = $get_upfam_refacc;
348 }
349 }
350 elsif ($annot_line =~ m/^(sp|tr)\|/) {
351 ($sdb, $acc, $id) = split(/\|/,$annot_line);
352 }
353 elsif ($annot_line =~ m/^ref\|/) {
354 ($sdb, $acc) = split(/\|/,$annot_line);
355 $get_annots_sql = $get_pfam_refacc;
356 $get_annots_sql_u = $get_upfam_refacc;
357 }
358 elsif ($annot_line =~ m/^(SP|TR):/i) {
359 ($sdb, $id) = split(/:/,$annot_line);
360 $use_acc = 0;
361 }
362 elsif ($annot_line !~ m/\|/ && $annot_line !~ m/:/) {
363 $use_acc = 1;
364 ($acc) = split(/\s+/,$annot_line);
365 }
366 # deal with no-database SwissProt/NR
367 else {
368 ($acc)=($annot_line =~ /^(\S+)/);
369 }
370
371 # here we have an $acc or an $id: check to see if we have the data
372
373 my %annot_data = (seq_info=>$annot_line, length=>$seq_len);
374 my $annot_key = '';
375 unless ($use_acc) {
376 next if ($annot_set{$id});
377 $annot_set{$id} = \%annot_data;
378 $annot_key = $id;
379
380 $get_annots_sql = $get_pfam_id;
381 }
382 else {
383 unless ($acc) {
384 warn "missing acc in $annot_line";
385 return "";
386 }
387 else {
388 $acc =~ s/\.\d+$//;
389
390 next if ($annot_set{$acc});
391 $annot_set{$acc} = \%annot_data;
392 $annot_key = $acc;
393 }
394 }
395
396 return $annot_key;
397 }
398
399 sub map_pfam_annots {
400 my ($annot_ref) = @_;
401
402 my $seq_length = $annot_ref->{length};
403 my $pf_domains_r = $annot_ref->{list};
404
405 my $row_href=$annot_ref->{list}[0];
406 if ($row_href->{length} && $row_href->{length} > $seq_length && $seq_length == 0) {
407 $annot_ref->{length} = $seq_length = $row_href->{length};
408 }
409
410 # fill in {info} field
411 for my $pf_dom (@$pf_domains_r) {
412 if ($auto_reg) {
413 $pf_dom->{info} = $pf_dom->{auto_pfamA_reg_full};
414 } elsif ($pf_acc) {
415 $pf_dom->{info} = $pf_dom->{pfamA_acc};
416 } else {
417 $pf_dom->{info} = $pf_dom->{pfamA_id};
418 }
419 }
420
421 # before checking for domain overlap, check for "split-domains"
422 # (self-unbound) by looking for runs of the same domain that are
423 # ordered by model_start
424
425 if (scalar(@{$pf_domains_r}) > 1) {
426 my @j_domains; #joined domains
427 my @tmp_domains = @{$pf_domains_r};
428
429 my $prev_dom = shift(@tmp_domains);
430
431 for my $curr_dom (@tmp_domains) {
432 # to join domains:
433 # (1) the domains must be in order by model_start/end coordinates
434 # (3) joining the domains cannot make the total combination too long
435
436 # check for model and sequence consistency
437 if (($prev_dom->{pfamA_acc} eq $curr_dom->{pfamA_acc}) # same family
438 && $prev_dom->{model_start} < $curr_dom->{model_start} # model check
439 && $prev_dom->{model_end} < $curr_dom->{model_end}
440
441 && ($curr_dom->{model_start} > $prev_dom->{model_end} * 0.80 # limit overlap
442 || $curr_dom->{model_start} < $prev_dom->{model_end} * 1.25)
443 && ((($curr_dom->{model_end} - $curr_dom->{model_start}+1)/$curr_dom->{model_length} +
444 ($prev_dom->{model_end} - $prev_dom->{model_start}+1)/$prev_dom->{model_length}) < 1.33)
445 ) { # join them by updating $prev_dom
446 $prev_dom->{seq_end} = $curr_dom->{seq_end};
447 $prev_dom->{model_end} = $curr_dom->{model_end};
448 $prev_dom->{auto_pfamA_reg_full} = $prev_dom->{auto_pfamA_reg_full} . ";". $curr_dom->{auto_pfamA_reg_full};
449 $prev_dom->{evalue} = ($prev_dom->{evalue} < $curr_dom->{evalue} ? $prev_dom->{evalue} : $curr_dom->{evalue});
450 } else {
451 push @j_domains, $prev_dom;
452 $prev_dom = $curr_dom;
453 }
454 }
455 push @j_domains, $prev_dom;
456 @{$pf_domains_r} = @j_domains;
457
458
459 if ($no_over) { # for either $no_over or $split_over, check for overlapping domains and edit/split them
460
461 my @tmp_domains = @{$pf_domains_r}; # allow shifts from copy of @pf_domains
462 my @save_domains = (); # where the new domains go
463
464 my $prev_dom = shift @tmp_domains;
465
466 while (my $curr_dom = shift @tmp_domains) {
467
468 my @overlap_domains = ($prev_dom);
469
470 my $diff = $prev_dom->{seq_end} - $curr_dom->{seq_start};
471
472 my ($prev_len, $cur_len) = ($prev_dom->{seq_end}-$prev_dom->{seq_start}+1,
473 $curr_dom->{seq_end}-$curr_dom->{seq_start}+1);
474
475 my $inclusion = ((($curr_dom->{seq_start} >= $prev_dom->{seq_start}) # start is right && end is left
476 && ($curr_dom->{seq_end} <= $prev_dom->{seq_end})) || # -- curr inside prev
477 (($curr_dom->{seq_start} <= $prev_dom->{seq_start}) # start is left && end is right
478 && ($curr_dom->{seq_end} >= $prev_dom->{seq_end}))); # -- prev is inside curr
479
480 my $longer_len = ($prev_len > $cur_len) ? $prev_len : $cur_len;
481
482 # check for overlap > domain_length/$over_fract
483 while ($inclusion || ($diff > 0 && $diff > $longer_len/$over_fract)) {
484 push @overlap_domains, $curr_dom;
485 $curr_dom = shift @tmp_domains;
486 last unless $curr_dom;
487 $diff = $prev_dom->{seq_end} - $curr_dom->{seq_start};
488 ($prev_len, $cur_len) = ($prev_dom->{seq_end}-$prev_dom->{seq_start}+1, $curr_dom->{seq_end}-$curr_dom->{seq_start}+1);
489 $longer_len = ($prev_len > $cur_len) ? $prev_len : $cur_len;
490 $inclusion = ((($curr_dom->{seq_start} >= $prev_dom->{seq_start}) && ($curr_dom->{seq_end} <= $prev_dom->{seq_end})) ||
491 (($curr_dom->{seq_start} <= $prev_dom->{seq_start}) && ($curr_dom->{seq_end} >= $prev_dom->{seq_end})));
492 }
493
494 # check for overlapping domains; >1 because $prev_dom is always there
495 if (scalar(@overlap_domains) > 1 ) {
496 # if $rpd2_fams, check for a chosen one
497
498 for my $dom ( @overlap_domains) {
499 $dom->{evalue} = 1.0 unless defined($dom->{evalue});
500 }
501
502 @overlap_domains = sort { $a->{evalue} <=> $b->{evalue} } @overlap_domains;
503 $prev_dom = $overlap_domains[0];
504 }
505
506 # $prev_dom should be the best of the overlaps, and we are no longer overlapping > dom_length/3
507 push @save_domains, $prev_dom;
508 $prev_dom = $curr_dom;
509 }
510
511 if ($prev_dom) {
512 push @save_domains, $prev_dom;
513 }
514
515 @{$pf_domains_r} = @save_domains;
516
517 # now check for smaller overlaps
518 for (my $i=1; $i < scalar(@{$pf_domains_r}); $i++) {
519 if ($pf_domains_r->[$i-1]->{seq_end} >= $pf_domains_r->[$i]->{seq_start}) {
520 my $overlap = $pf_domains_r->[$i-1]->{seq_end} - $pf_domains_r->[$i]->{seq_start};
521 $pf_domains_r->[$i-1]->{seq_end} -= int($overlap/2);
522 $pf_domains_r->[$i]->{seq_start} = $pf_domains_r->[$i-1]->{seq_end}+1;
523 }
524 }
525 }
526 elsif ($split_over) { # here, everything that overlaps by > $min_vdom should be split into a separate domain
527 my @save_domains = (); # where the new domains go
528
529 # check to see if one domain is included (or overlapping) more
530 # than xx% of the other. If so, pick the longer one
531
532 my ($prev_dom, $curr_dom) = ($pf_domains_r->[0],0) ;
533 for (my $i=1; $i < scalar(@{$pf_domains_r}); $i++) {
534 $curr_dom = $pf_domains_r->[$i];
535
536 my ($prev_len, $cur_len) = ($prev_dom->{seq_end}-$prev_dom->{seq_start}+1, $curr_dom->{seq_end}-$curr_dom->{seq_start}+1);
537 my $longer_len = ($prev_len > $cur_len) ? $prev_len : $cur_len;
538
539 if (($curr_dom->{seq_start} >= $prev_dom->{seq_start}) && ($curr_dom->{seq_end} <= $prev_dom->{seq_end})
540 && $cur_len / $prev_len > 0.80) {
541 # $prev_dom stays the same, $curr_dom deleted
542 next;
543 }
544 elsif (($curr_dom->{seq_start} <= $prev_dom->{seq_start}) && ($curr_dom->{seq_end} >= $prev_dom->{seq_end})
545 && $prev_len / $cur_len > 0.80) {
546 $prev_dom = $curr_dom; # this should delete $prev_dom
547 next;
548 }
549
550 if ($prev_dom->{seq_end} >= $curr_dom->{seq_start} + $min_vdom) {
551 my ($l_seq_end, $r_seq_start) = ($curr_dom->{seq_start}-1, $prev_dom->{seq_end}+1);
552
553 $prev_dom->{seq_end} = $l_seq_end;
554 push @save_domains, $prev_dom;
555 my $new_dom = {seq_start => $l_seq_end+1, seq_end=>$r_seq_start-1,
556 model_length => -1,
557 pfamA_acc=>$prev_dom->{pfamA_acc}."/".$curr_dom->{pfamA_acc},
558 pfamA_id=>$prev_dom->{pfamA_id}."/".$curr_dom->{pfamA_id},
559 };
560
561 if ($pf_acc) {
562 $new_dom->{info} = $new_dom->{pfamA_acc};
563 }
564 else {
565 $new_dom->{info} = $new_dom->{pfamA_id};
566 }
567
568 push @save_domains, $new_dom;
569 $curr_dom->{seq_start} = $r_seq_start;
570 $prev_dom = $curr_dom;
571 }
572 else {
573 push @save_domains, $prev_dom;
574 $prev_dom = $curr_dom;
575 }
576 }
577 push @save_domains, $prev_dom;
578 @{$pf_domains_r} = @save_domains;
579 }
580 }
581
582 # $vdoms -- virtual Pfam domains -- the equivalent of $neg_doms,
583 # but covering parts of a Pfam model that are not annotated. split
584 # domains have been joined, so simply check beginning and end of
585 # each domain (but must also check for bounded-ness)
586 # only add when 10% or more is missing and missing length > $min_nodom
587
588 if ($vdoms && scalar(@{$pf_domains_r})) {
589 my @vpf_domains;
590
591 my $curr_dom = $pf_domains_r->[0];
592 my $length = $curr_dom->{length};
593
594 my $prev_dom={seq_end=>0, pfamA_acc=>''};
595 my $prev_dom_end = 0;
596 my $next_dom_start = $length+1;
597
598 for (my $dom_ix=0; $dom_ix < scalar(@{$pf_domains_r}); $dom_ix++ ) {
599 $curr_dom = $pf_domains_r->[$dom_ix];
600
601 my $pfamA = $curr_dom->{pfamA_acc};
602
603 # first, look left, is there a domain there (if there is,
604 # it should be updated right
605
606 # my $min_vdom = $curr_dom->{model_length} / 10;
607
608 if ($curr_dom->{model_length} < $min_vdom) {
609 push @vpf_domains, $curr_dom;
610 next;
611 }
612 if ($prev_dom->{pfamA_acc}) { # look for previous domain
613 $prev_dom_end = $prev_dom->{seq_end};
614 }
615
616 # there is a domain to the left, how much room is available?
617 my $left_dom_len = min($curr_dom->{seq_start}-$prev_dom_end-1, $curr_dom->{model_start}-1);
618 if ( $left_dom_len > $min_vdom) {
619 # there is room for a virtual domain
620 my %new_dom = (seq_start=> $curr_dom->{seq_start}-$left_dom_len,
621 seq_end => $curr_dom->{seq_start}-1,
622 info=>'@'.$curr_dom->{info},
623 model_length=>$curr_dom->{model_length},
624 model_end => $curr_dom->{model_start}-1,
625 model_start => $left_dom_len,
626 pfamA_acc=>$pfamA,
627 );
628 push @vpf_domains, \%new_dom;
629 }
630
631 # save the current domain
632 push @vpf_domains, $curr_dom;
633 $prev_dom = $curr_dom;
634
635 if ($dom_ix < scalar(@$pf_domains_r)-1) { # there is a domain to the right
636 # first, give all the extra space to the first domain (no splitting)
637 $next_dom_start = $pf_domains_r->[$dom_ix+1]->{seq_start};
638 }
639 else {
640 $next_dom_start = $length;
641 }
642
643 # is there room for a virtual domain right
644
645 my $right_dom_len = min($next_dom_start-$curr_dom->{seq_end}-1, # space available
646 $curr_dom->{model_length}-$curr_dom->{model_end} # space needed
647 );
648 if ( $right_dom_len > $min_vdom) {
649 my %new_dom = (seq_start=> $curr_dom->{seq_end}+1,
650 seq_end=> $curr_dom->{seq_end}+$right_dom_len,
651 info=>'@'.$curr_dom->{info},
652 model_length => $curr_dom->{model_length},
653 pfamA_acc=> $pfamA,
654 );
655 push @vpf_domains, \%new_dom;
656 $prev_dom = \%new_dom;
657 }
658 } # all done, check for last one
659
660 # $curr_dom=$pf_domains_r->[-1];
661 # # my $min_vdom = $curr_dom->{model_length}/10;
662
663 # my $right_dom_len = min($length - $curr_dom->{seq_end}+1, # space available
664 # $curr_dom->{model_length}-$curr_dom->{model_end} # space needed
665 # );
666 # if ($right_dom_len > $min_vdom) {
667 # my %new_dom = (seq_start=> $curr_dom->{seq_end}+1,
668 # seq_end => $curr_dom->{seq_end}+$right_dom_len,
669 # info=>'@'.$curr_dom->{pfamA_acc},
670 # model_len=> $curr_dom->{model_len},
671 # pfamA_acc => $curr_dom->{pfamA_acc},
672 # model_start => $curr_dom->{model_end}+1,
673 # model_end => $curr_dom->{model_len},
674 # );
675
676 # push @vpf_domains, \%new_dom;
677 # }
678
679 # @vpf_domains has both old @{$pf_domains_r} and new neg-domains
680 @{$pf_domains_r} = @vpf_domains;
681 }
682
683 if ($neg_doms) {
684 my @npf_domains;
685 my $prev_dom={seq_end=>0};
686 for my $curr_dom ( @{$pf_domains_r}) {
687 if ($curr_dom->{seq_start} - $prev_dom->{seq_end} > $min_nodom) {
688 my %new_dom = (seq_start=>$prev_dom->{seq_end}+1, seq_end => $curr_dom->{seq_start}-1, info=>'NODOM');
689 push @npf_domains, \%new_dom;
690 }
691 push @npf_domains, $curr_dom;
692 $prev_dom = $curr_dom;
693 }
694 if ($seq_length - $prev_dom->{seq_end} > $min_nodom) {
695 my %new_dom = (seq_start=>$prev_dom->{seq_end}+1, seq_end=>$seq_length, info=>'NODOM');
696 if ($new_dom{seq_end} > $new_dom{seq_start}) {
697 push @npf_domains, \%new_dom;
698 }
699 }
700
701 # @npf_domains has both old @pf_domains and new neg-domains
702 @{$pf_domains_r} = @npf_domains;
703 }
704
705 # now make sure we have useful names: colors
706
707 for my $pf (@{$pf_domains_r}) {
708 $pf->{info} = domain_name($pf->{info}, $pf->{pfamA_acc});
709 }
710 }
711
712 sub min {
713 my ($arg1, $arg2) = @_;
714
715 return ($arg1 <= $arg2 ? $arg1 : $arg2);
716 }
717
718 sub max {
719 my ($arg1, $arg2) = @_;
720
721 return ($arg1 >= $arg2 ? $arg1 : $arg2);
722 }
723
724 # domain name takes a uniprot domain label, removes comments ( ;
725 # truncated) and numbers and returns a canonical form. Thus:
726 # Cortactin 6.
727 # Cortactin 7; truncated.
728 # becomes "Cortactin"
729 #
730
731 sub domain_name {
732
733 my ($value, $pfamA_acc) = @_;
734 my $is_virtual = 0;
735
736 if ($value =~ m/^@/) {
737 $is_virtual = 1;
738 $value =~ s/^@//;
739 }
740
741 # check for clan:
742 if ($no_clans) {
743 if (! defined($domains{$value})) {
744 $domain_clan{$value} = 0;
745 $domains{$value} = ++$domain_cnt;
746 push @domain_list, $pfamA_acc;
747 }
748 }
749 elsif (!defined($domain_clan{$value})) {
750 ## only do this for new domains, old domains have known mappings
751
752 ## ways to highlight the same domain:
753 # (1) for clans, substitute clan name for family name
754 # (2) for clans, use the same color for the same clan, but don't change the name
755 # (3) for clans, combine family name with clan name, but use colors based on clan
756
757 # check to see if it's a clan
758 $get_pfam_clan->execute($pfamA_acc);
759
760 my $pfam_clan_href=0;
761
762 if ($pfam_clan_href=$get_pfam_clan->fetchrow_hashref()) { # is a clan
763 my ($clan_id, $clan_acc) = @{$pfam_clan_href}{qw(clan_id clan_acc)};
764
765 # now check to see if we have seen this clan before (if so, do not increment $domain_cnt)
766 my $c_value = "C." . $clan_id;
767 if ($pf_acc) {$c_value = $clan_acc;}
768
769 $domain_clan{$value} = {clan_id => $clan_id,
770 clan_acc => $clan_acc};
771
772 if ($domains{$c_value}) {
773 $domain_clan{$value}->{domain_cnt} = $domains{$c_value};
774 $value = $c_value;
775 }
776 else {
777 $domain_clan{$value}->{domain_cnt} = ++ $domain_cnt;
778 $value = $c_value;
779 $domains{$value} = $domain_cnt;
780 push @domain_list, $pfamA_acc;
781 }
782 }
783 else { # not a clan
784 $domain_clan{$value} = 0;
785 $domains{$value} = ++$domain_cnt;
786 push @domain_list, $pfamA_acc;
787 }
788 }
789 elsif ($domain_clan{$value} && $domain_clan{$value}->{clan_acc}) {
790 if ($pf_acc) {$value = $domain_clan{$value}->{clan_acc};}
791 else { $value = "C." . $domain_clan{$value}->{clan_id}; }
792 }
793
794 if ($is_virtual) {
795 $domains{'@'.$value} = $domains{$value};
796 $value = '@'.$value;
797 }
798 return $value;
799 }
800
801 sub domain_num {
802 my ($value, $number) = @_;
803 if ($value =~ m/^@/) {
804 $value =~ s/^@/v/;
805 $number = $number."v";
806 }
807 return ($value, $number);
808 }
809
810
811 __END__
812
813 =pod
814
815 =head1 NAME
816
817 ann_pfam30_tmptbl.pl
818
819 =head1 SYNOPSIS
820
821 ann_pfam30_tmptbl.pl --neg-doms --vdoms 'sp|P09488|GSTM1_NUMAN' | accession.file
822
823 =head1 OPTIONS
824
825 -h short help
826 --help include description
827 --no-over : generate non-overlapping domains (equivalent to ann_pfam.pl)
828 --split-over : overlaps of two domains generate a new hybrid domain
829 --no-clans : do not use clans with multiple families from same clan
830 --neg-doms : report domains between annotated domains as NODOM
831 (also --neg, --neg_doms)
832 --vdoms : produce "virtual domains" using model_start,
833 model_end for partial pfam domains
834 --min_nodom=10 : minimum length between domains for NODOM
835
836 --host, --user, --password, --port --db : info for mysql database
837
838 =head1 DESCRIPTION
839
840 C<ann_pfam30_tmptbl.pl> extracts domain information from the pfam msyql
841 database. Currently, the program works with database
842 sequence descriptions in several formats:
843
844 >gi|1705556|sp|P54670.1|CAF1_DICDI
845 >sp|P09488|GSTM1_HUMAN
846 >sp:CALM_HUMAN
847
848 C<ann_pfam30_tmptbl.pl> uses the C<pfamA_reg_full_significant>, C<pfamseq>,
849 and C<pfamA> tables of the C<pfam> database to extract domain
850 information on a protein.
851
852 If the C<--no-over> option is set, overlapping domains are selected and
853 edited to remove overlaps. For proteins with multiple overlapping
854 domains (domains overlap by more than 1/3 of the domain length),
855 C<auto_pfam28.pl> selects the domain annotation with the best
856 C<domain_evalue_score>. When domains overlap by less than 1/3 of the
857 domain length, they are shortened to remove the overlap.
858
859 If the C<--split-over> option is set, if two domains overlap, the
860 overlapping region is split out of the domains and labeled as a new,
861 virtual-lie, domain. If one domain is internal to another and spans
862 80% of the domain, the shorter domain is removed.
863
864 C<ann_pfam30_tmptbl.pl> is designed to be used by the B<FASTA> programs with
865 the C<-V \!ann_pfam30_tmptbl.pl> or C<-V "\!ann_pfam30_tmptbl.pl --neg"> option.
866
867 C<ann_pfam30_tmptbl.pl> requires an additional database, C<tmp_annot>,
868 with C<create temporary tables>, C<insert>, and C<select> privileges
869 for the default user.
870
871 =head1 AUTHOR
872
873 William R. Pearson, [email protected]
874
875 =cut