AlignIO/stockholm.pm

filter: Statementstmt 84.6% 160 / 189 Branchbran 63.0% 82 / 130 Conditioncond 77.0% 37 / 48 MC/DCmcdc 66.6% 18 / 27 Subroutinesub 86.6% 13 / 15 Podpod 100% 5 / 5 totaltotal 76.0% 315 / 414 CC 94 SCAR 42.6
CC
94
Cov
76%
write_aln
153.7
next_aln
38.2
_print_seqs
11.0
CRAP 70.5
↑ Summary Index/Stockholm.pm »
1 #
2 # BioPerl module for Bio::AlignIO::stockholm
3 #
4 # Based on the Bio::SeqIO::stockholm module
5 # by Ewan Birney <birney@ebi.ac.uk>
6 # and Lincoln Stein <lstein@cshl.org>
7 #
8 # and the SimpleAlign.pm module of Ewan Birney
9 #
10 # Copyright Peter Schattner, Chris Fields
11 #
12 # You may distribute this module under the same terms as perl itself
13 # _history
14 # September 5, 2000
15 # November 6, 2006 - completely refactor read_aln(), add write_aln()
16 # POD documentation - main docs before the code
17  
18 =head1 NAME
19  
20 Bio::AlignIO::stockholm - stockholm sequence input/output stream
21  
22 =head1 SYNOPSIS
23  
24 # Do not use this module directly. Use it via the L<Bio::AlignIO> class.
25  
26 use Bio::AlignIO;
27 use strict;
28  
29 my $in = Bio::AlignIO->new(-format => 'stockholm',
30 -file => 't/data/testaln.stockholm');
31 while( my $aln = $in->next_aln ) {
32  
33 }
34  
35 =head1 DESCRIPTION
36  
37 This object can transform L<Bio::Align::AlignI> objects to and from
38 stockholm flat file databases. This has been completely refactored
39 from the original stockholm parser to handle annotation data and now
40 includes a write_aln() method for (almost) complete stockholm
41 format output.
42  
43 Stockholm alignment records normally contain additional sequence-based
44 and alignment-based annotation
45  
46 GF Lines (alignment feature/annotation):
47 #=GF <featurename> <Generic per-file annotation, free text>
48 Placed above the alignment
49  
50 GC Lines (Alignment consensus)
51 #=GC <featurename> <Generic per-column annotation, exactly 1
52 character per column>
53 Placed below the alignment
54  
55 GS Lines (Sequence annotations)
56 #=GS <seqname> <featurename> <Generic per-sequence annotation, free
57 text>
58  
59 GR Lines (Sequence meta data)
60 #=GR <seqname> <featurename> <Generic per-sequence AND per-column
61 mark up, exactly 1 character per column>
62  
63 Currently, sequence annotations (those designated with GS tags) are
64 parsed only for accession numbers and descriptions. It is intended that
65 full parsing will be added at some point in the near future along with
66 a builder option for optionally parsing alignment annotation and meta data.
67  
68 The following methods/tags are currently used for storing and writing
69 the alignment annotation data.
70  
71 Tag SimpleAlign
72 Method
73 ----------------------------------------------------------------------
74 AC accession
75 ID id
76 DE description
77 ----------------------------------------------------------------------
78  
79 Tag Bio::Annotation TagName Parameters
80 Class
81 ----------------------------------------------------------------------
82 AU SimpleValue record_authors value
83 SE SimpleValue seed_source value
84 GA SimpleValue gathering_threshold value
85 NC SimpleValue noise_cutoff value
86 TC SimpleValue trusted_cutoff value
87 TP SimpleValue entry_type value
88 SQ SimpleValue num_sequences value
89 PI SimpleValue previous_ids value
90 DC Comment database_comment comment
91 CC Comment alignment_comment comment
92 DR Target dblink database
93 primary_id
94 comment
95 AM SimpleValue build_method value
96 NE SimpleValue pfam_family_accession value
97 NL SimpleValue sequence_start_stop value
98 SS SimpleValue sec_structure_source value
99 BM SimpleValue build_model value
100 RN Reference reference *
101 RC Reference reference comment
102 RM Reference reference pubmed
103 RT Reference reference title
104 RA Reference reference authors
105 RL Reference reference location
106 ----------------------------------------------------------------------
107 * RN is generated based on the number of Bio::Annotation::Reference objects
108  
109 =head2 Custom annotation
110  
111 Some users may want to add custom annotation beyond those mapped above.
112 Currently there are two methods to do so; however, the methods used for adding
113 such annotation may change in the future, particularly if alignment Writer
114 classes are introduced. In particular, do not rely on changing the global
115 variables @WRITEORDER or %WRITEMAP as these may be made private at some point.
116  
117 1) Use (and abuse) the 'custom' tag. The tagname for the object can differ
118 from the tagname used to store the object in the AnnotationCollection.
119  
120 # AnnotationCollection from the SimpleAlign object
121 my $coll = $aln->annotation;
122 my $factory = Bio::Annotation::AnnotationFactory->new(-type =>
123 Bio::Annotation::SimpleValue');
124 my $rfann = $factory->create_object(-value => $str,
125 -tagname => 'mytag');
126 $coll->add_Annotation('custom', $rfann);
127 $rfann = $factory->create_object(-value => 'foo',
128 -tagname => 'bar');
129 $coll->add_Annotation('custom', $rfann);
130  
131 OUTPUT:
132  
133 # STOCKHOLM 1.0
134  
135 #=GF ID myID12345
136 #=GF mytag katnayygqelggvnhdyddlakfyfgaglealdffnnkeaaakiinwvaEDTTRGKIQDLV??
137 #=GF mytag TPtd~????LDPETQALLV???????????????????????NAIYFKGRWE?????????~??
138 #=GF mytag ??HEF?A?EMDTKPY??DFQH?TNen?????GRI??????V???KVAM??MF?????????N??
139 #=GF mytag ???DD?VFGYAEL????DE???????L??D??????A??TALELAY??????????????????
140 #=GF mytag ?????????????KG??????Sa???TSMLILLP???????????????D??????????????
141 #=GF mytag ???????????EGTr?????AGLGKLLQ??QL????????SREef??DLNK??L???AH????R
142 #=GF mytag ????????????L????????????????????????????????????????R?????????R
143 #=GF mytag ??QQ???????V???????AVRLPKFSFefefdlkeplknlgmhqafdpnsdvfklmdqavlvi
144 #=GF mytag gdlqhayafkvd????????????????????????????????????????????????????
145 #=GF mytag ????????????????????????????????????????????????????????????????
146 #=GF mytag ????????????????????????????????????????????????????????????????
147 #=GF mytag ????????????????????????????????????????????????????????????????
148 #=GF mytag ?????????????INVDEAG?TEAAAATAAKFVPLSLppkt??????????????????PIEFV
149 #=GF mytag ADRPFAFAIR??????E?PAT?G????SILFIGHVEDPTP?msv?
150 #=GF bar foo
151 ...
152  
153 2) Modify the global @WRITEORDER and %WRITEMAP.
154  
155 # AnnotationCollection from the SimpleAlign object
156 my $coll = $aln->annotation;
157  
158 # add to WRITEORDER
159 my @order = @Bio::AlignIO::stockholm::WRITEORDER;
160 push @order, 'my_stuff';
161 @Bio::AlignIO::stockholm::WRITEORDER = @order;
162  
163 # make sure new tag maps to something
164 $Bio::AlignIO::stockholm::WRITEMAP{my_stuff} = 'Hobbit/SimpleValue';
165  
166 my $rfann = $factory->create_object(-value => 'Frodo',
167 -tagname => 'Hobbit');
168 $coll->add_Annotation('my_stuff', $rfann);
169 $rfann = $factory->create_object(-value => 'Bilbo',
170 -tagname => 'Hobbit');
171 $coll->add_Annotation('my_stuff', $rfann);
172  
173 OUTPUT:
174  
175 # STOCKHOLM 1.0
176  
177 #=GF ID myID12345
178 #=GF Hobbit Frodo
179 #=GF Hobbit Bilbo
180 ....
181  
182 =head1 FEEDBACK
183  
184 =head2 Support
185  
186 Please direct usage questions or support issues to the mailing list:
187  
188 I<bioperl-l@bioperl.org>
189  
190 rather than to the module maintainer directly. Many experienced and
191 reponsive experts will be able look at the problem and quickly
192 address it. Please include a thorough description of the problem
193 with code and data examples if at all possible.
194  
195 =head2 Reporting Bugs
196  
197 Report bugs to the Bioperl bug tracking system to help us keep track
198 the bugs and their resolution. Bug reports can be submitted via the
199 web:
200  
201 https://github.com/bioperl/bioperl-live/issues
202  
203 =head1 AUTHORS - Chris Fields, Peter Schattner
204  
205 Email: cjfields-at-uiuc-dot-edu, schattner@alum.mit.edu
206  
207 =head1 CONTRIBUTORS
208  
209 Andreas Kahari, ak-at-ebi.ac.uk
210 Jason Stajich, jason-at-bioperl.org
211  
212 =head1 APPENDIX
213  
214 The rest of the documentation details each of the object
215 methods. Internal methods are usually preceded with a _
216  
217 =cut
218  
219 # Let the code begin...
220  
221 package Bio::AlignIO::stockholm;
222 3 $Bio::AlignIO::stockholm::VERSION = '1.7.3';
223 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
224  
225 3 ▶use Bio::Seq::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
226 3 ▶use Bio::AlignIO::Handler::GenericAlignHandler;
Subroutine BEGIN: called CC=1 SCAR=0.0
227 3 ▶use Text::Wrap qw(wrap);
Subroutine BEGIN: called CC=1 SCAR=0.0
228  
229 3 ▶use base qw(Bio::AlignIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
230  
231 3 my $STKVERSION = 'STOCKHOLM 1.0';
232  
233 # This maps the two-letter annotation key to a Annotation/parameter/tagname
234 # combination. Some data is stored using get/set methods ('Methods') The rest
235 # is mapped to Annotation objects using the parameter for the parsed data
236 # and the tagname for, well, the Annotation tagname. A few are treated differently
237 # based on the type of data stored (Reference data in particular).
238  
239 3 my %MAPPING = (
240 'AC' => 'ACCESSION',
241 'ID' => 'ID',
242 'DE' => ['DESCRIPTION' => 'DESCRIPTION'],
243 'AU' => ['RECORD_AUTHORS' => 'RECORD_AUTHORS'],
244 'SE' => 'SEED_SOURCE',
245 'BM' => 'BUILD_COMMAND',
246 'GA' => 'GATHERING_THRESHOLD',
247 'NC' => 'NOISE_CUTOFF',
248 'TC' => 'TRUSTED_CUTOFF',
249 'TP' => 'ENTRY_TYPE',
250 'SQ' => 'NUM_SEQUENCES',
251 'PI' => 'PREVIOUS_IDS',
252 'DC' => ['DATABASE_COMMENT' => 'DATABASE_COMMENT'],
253 'DR' => 'DBLINK',
254 'RN' => ['REFERENCE' => 'REFERENCE'],
255 'RC' => ['REFERENCE' => 'COMMENT'],
256 'RM' => ['REFERENCE' => 'PUBMED'],
257 'RT' => ['REFERENCE' => 'TITLE'],
258 'RA' => ['REFERENCE' => 'AUTHORS'],
259 'RL' => ['REFERENCE' => 'JOURNAL'],
260 'CC' => ['ALIGNMENT_COMMENT' => 'ALIGNMENT_COMMENT'],
261 #Pfam-specific
262 'AM' => 'BUILD_METHOD',
263 'NE' => 'PFAM_FAMILY_ACCESSION',
264 'NL' => 'SEQ_START_STOP',
265 # Rfam-specific GF lines
266 #'SS' => 'SEC_STRUCTURE_SOURCE',
267 'SEQUENCE' => 'SEQUENCE'
268 );
269  
270 # this is the order that annotations are written
271 3 our @WRITEORDER = qw(accession
272 id
273 description
274 previous_ids
275 record_authors
276 seed_source
277 sec_structure_source
278 gathering_threshold
279 trusted_cutoff
280 noise_cutoff
281 entry_type
282 build_command
283 build_method
284 pfam_family_accession
285 seq_start_stop
286 reference
287 database_comment
288 custom
289 dblink
290 alignment_comment
291 num_sequences
292 seq_annotation
293 );
294  
295 # This maps the tagname back to a tagname-annotation value combination.
296 # Some data is stored using get/set methods ('Methods'), others
297 # are mapped b/c of more complex annotation types.
298  
299 3 our %WRITEMAP = (
300 'accession' => 'AC/Method',
301 'id' => 'ID/Method',
302 'description' => 'DE/Method',
303 'record_authors' => 'AU/SimpleValue',
304 'seed_source' => 'SE/SimpleValue',
305 'build_command' => 'BM/SimpleValue',
306 'gathering_threshold' => 'GA/SimpleValue',
307 'noise_cutoff' => 'NC/SimpleValue',
308 'trusted_cutoff' => 'TC/SimpleValue',
309 'entry_type' => 'TP/SimpleValue',
310 'num_sequences' => 'SQ/SimpleValue',
311 'previous_ids' => 'PI/SimpleValue',
312 'database_comment' => 'DC/SimpleValue',
313 'dblink' => 'DR/DBLink',
314 'reference' => 'RX/Reference',
315 'ref_number' => 'RN/number',
316 'ref_comment' => 'RC/comment',
317 'ref_pubmed' => 'RM/pubmed',
318 'ref_title' => 'RT/title',
319 'ref_authors' => 'RA/authors',
320 'ref_location' => 'RL/location',
321 'alignment_comment' => 'CC/Comment',
322 'seq_annotation' => 'DR/Collection',
323 #Pfam-specific
324 'build_method' => 'AM/SimpleValue',
325 'pfam_family_accession' => 'NE/SimpleValue',
326 'seq_start_stop' => 'NL/SimpleValue',
327 # Rfam-specific GF lines
328 'sec_structure_source' => 'SS/SimpleValue',
329 # custom; this is used to carry over anything from the input alignment
330 # not mapped to LocatableSeqs or SimpleAlign in a meaningful way
331 'custom' => 'XX/SimpleValue'
332 );
333  
334 # This maps the tagname back to a tagname-annotation value combination.
335 # Some data is stored using get/set methods ('Methods'), others
336 # are mapped b/c of more complex annotation types.
337  
338 =head2 new
339  
340 Title : new
341 Usage : my $alignio = Bio::AlignIO->new(-format => 'stockholm'
342 -file => '>file');
343 Function: Initialize a new L<Bio::AlignIO::stockholm> reader or writer
344 Returns : L<Bio::AlignIO> object
345 Args : -line_length : length of the line for the alignment block
346 -alphabet : symbol alphabet to set the sequences to. If not set,
347 the parser will try to guess based on the alignment
348 accession (if present), defaulting to 'dna'.
349 -spaces : (optional, def = 1) boolean to add a space in between
350 the "# STOCKHOLM 1.0" header and the annotation and
351 the annotation and the alignment.
352  
353 =cut
354  
355 5 ▶sub _initialize {
Subroutine _initialize: called CC=4 SCAR=14.3
356 5 my ( $self, @args ) = @_;
357 5 $self->SUPER::_initialize(@args);
358 5 my ($handler, $linelength, $spaces) = $self->_rearrange([qw(HANDLER LINE_LENGTH SPACES)],@args);
359 5 ▶ $spaces = defined $spaces ? $spaces : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $spaces ? : 0 5 5
360 5 $self->spaces($spaces);
361 # hash for functions for decoding keys.
362 5 ▶ $handler ? $self->alignhandler($handler) :
Branch: 1 branch
BranchTrueFalseTotal
$handler ? : 0 5 5
363 $self->alignhandler(Bio::AlignIO::Handler::GenericAlignHandler->new(
364 -format => 'stockholm',
365 -verbose => $self->verbose,
366 ));
367 5 ▶ $linelength && $self->line_length($linelength);
Branch: 1 branch
BranchTrueFalseTotal
$linelength and $self->line_length($linelength) 0 5 5
368 }
369  
370 =head2 next_aln
371  
372 Title : next_aln
373 Usage : $aln = $stream->next_aln()
374 Function: returns the next alignment in the stream.
375 Returns : L<Bio::Align::AlignI> object
376 Args : NONE
377  
378 =cut
379  
380 8 ▶sub next_aln {
Subroutine next_aln: called CC=38 SCAR=36.4
Pod documented
381 8 my $self = shift;
382  
383 8 my $handler = $self->alignhandler;
384 # advance to alignment header
385 8 ▶ while( defined(my $line = $self->_readline) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $self->_readline)) 8 0 8
386 8 ▶ if ($line =~ m{^\#\s*STOCKHOLM\s+}xmso) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\#\s*STOCKHOLM\s+/mosx) 8 0 8
387 8 last;
388 }
389 }
390  
391 8 $self->{block_line} = 0;
392 # go into main body of alignment
393 8 my ($data_chunk, $isa_primary, $name, $alphabet);
394 8 my $last_feat = '';
395 8 ▶ while( defined(my $line = $self->_readline) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $self->_readline)) 666 0 666
396 # only blank lines are in between blocks, so reset block line
397 666 my ($primary_tag, $secondary_tag, $data, $nse, $feat, $align, $concat);
398 666 ▶ if ($line =~ m{^\s*$}xmso) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*$/mosx) 13 653 666
399 13 ▶ $self->{block_line} &&= 0;
Condition100%2/2
$self->{"block_line"} &&= 0
!ll
76
MC/DC100%1/1
$self->{"block_line"} &&= 0
$self->{"block_line"}
Truth table100%2/2
A
A= $self->{"block_line"}
Aresult
00
11
400 13 next;
401 }
402  
403 # End of Record
404 653 ▶ if (index($line, '//') == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (index($line, '//') == 0) { } 8 645 653
elsif ($line =~ /^\#=([A-Z]{2})\s+([^\n]+?)\s*$/mosx) { } 361 284 645
elsif ($line =~ /^(\S+)\s+([^\s]+)\s*/) { } 284 0 284
405 # fencepost
406 8 $handler->data_handler($data_chunk);
407 8 undef $data_chunk;
408 8 ▶ $handler->data_handler({ALIGNMENT => 1,
Branch: 1 branch
BranchTrueFalseTotal
if $self->alphabet 7 1 8
409 NAME => 'ALPHABET',
410 DATA => $self->alphabet})
411 if $self->alphabet;
412 8 last;
413 }
414 elsif ($line =~ m{^\#=([A-Z]{2})\s+([^\n]+?)\s*$}xmso) {
415 361 ($primary_tag, $data) = ($1, $2);
416 361 ▶ if ($primary_tag eq 'GS' || $primary_tag eq 'GR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($primary_tag eq 'GS' or $primary_tag eq 'GR') { } 61 300 361
Condition100%3/3
$primary_tag eq "GS" or $primary_tag eq "GR"
l!l&&r!l&&!r
592300
MC/DC100%2/2
$primary_tag eq "GS" or $primary_tag eq "GR"
$primary_tag eq "GS" $primary_tag eq "GR"
Truth table100%3/3
A or B
A= $primary_tag eq "GS"B= $primary_tag eq "GR"
ABresult
1X1
011
000
417 61 ($nse, $feat, $data) = split(/\s+/, $data, 3);
418 } else {
419 300 ($feat, $data) = split(/\s+/, $data, 2);
420 }
421 361 ▶ $align = ($primary_tag eq 'GF' || $primary_tag eq 'GR') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$primary_tag eq 'GF' || $primary_tag eq 'GR' ? : 287 74 361
Condition100%3/3
$primary_tag eq "GF" or $primary_tag eq "GR"
l!l&&r!l&&!r
285274
MC/DC100%2/2
$primary_tag eq "GF" or $primary_tag eq "GR"
$primary_tag eq "GF" $primary_tag eq "GR"
Truth table100%3/3
A or B
A= $primary_tag eq "GF"B= $primary_tag eq "GR"
ABresult
1X1
011
000
422 }
423 elsif ($line =~ m{^(\S+)\s+([^\s]+)\s*}) {
424 284 $self->{block_line}++;
425 284 ($feat, $nse, $data) = ('SEQUENCE', $1, $2);
426 }
427 else {
428 0 $self->debug("Missed line : $line\n");
429 }
430 645 ▶ $primary_tag ||= ''; # when no #= line is present
Condition100%2/2
$primary_tag ||= ""
l!l
361284
MC/DC100%1/1
$primary_tag ||= ""
$primary_tag
Truth table100%2/2
A
A= $primary_tag
Aresult
11
00
431 645 ▶ $align ||= 0;
Condition100%2/2
$align ||= 0
l!l
287358
MC/DC100%1/1
$align ||= 0
$align
Truth table100%2/2
A
A= $align
Aresult
11
00
432  
433 # array refs where the two values are equal indicate the start of a
434 # primary chunk of data, otherwise it is to be folded into the last
435 # data chunk under a secondary tag. These are also concatenated
436 # to previous values if the
437  
438 645 ▶ if (exists($MAPPING{$feat}) && ref $MAPPING{$feat} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $MAPPING{$feat} and ref $MAPPING{$feat} eq 'ARRAY') { } 183 462 645
elsif (exists $MAPPING{$feat}) { } 441 21 462
Condition100%3/3
exists $MAPPING{$feat} and ref $MAPPING{$feat} eq "ARRAY"
!ll&&!rl&&r
21441183
MC/DC100%2/2
exists $MAPPING{$feat} and ref $MAPPING{$feat} eq "ARRAY"
exists $MAPPING{$feat} ref $MAPPING{$feat} eq "ARRAY"
Truth table100%3/3
A and B
A= exists $MAPPING{$feat}B= ref $MAPPING{$feat} eq "ARRAY"
ABresult
0X0
100
111
439 ($name, $secondary_tag, $isa_primary) = ( $MAPPING{$feat}->[0] eq $MAPPING{$feat}->[1] ) ?
440 ($MAPPING{$feat}->[0], 'DATA', 1) :
441 183 ▶ (@{ $MAPPING{$feat} }, 0) ;
Branch: 1 branch
BranchTrueFalseTotal
$MAPPING{$feat}[0] eq $MAPPING{$feat}[1] ? : 75 108 183
442 183 ▶ $concat = $last_feat eq $feat ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$last_feat eq $feat ? : 57 126 183
443 } elsif (exists($MAPPING{$feat})) {
444 441 ($name, $secondary_tag, $isa_primary) = ($MAPPING{$feat}, 'DATA', 1);
445 # catch alphabet here if possible
446 441 ▶ if ($align && $name eq 'ACCESSION' && !$self->alphabet) {
Branch: 1 branch
BranchTrueFalseTotal
if ($align and $name eq "ACCESSION" and not $self->alphabet) 3 438 441
Condition100%6/6
$align and $name eq "ACCESSION" and not $self->alphabet
!ll&&!rl&&r
43443
$align and $name eq "ACCESSION"
!ll&&!rl&&r
343917
MC/DC100%3/3
$align and $name eq "ACCESSION" and not $self->alphabet
$align $name eq "ACCESSION" not $self->alphabet
Truth table100%4/4
A and B and not(C)
A= $alignB= $name eq "ACCESSION"C= not $self->alphabet
ABCresult
0XX0
10X0
1100
1111
447 3 ▶ if ($data =~ m{^(P|R)F}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^(P|R)F/) 3 0 3
448 3 ▶ $self->alphabet($1 eq 'R' ? 'rna' : $1 eq 'P' ? 'protein' : undef );
Branch: 2 branches
BranchTrueFalseTotal
$1 eq 'R' ? : 1 2 3
$1 eq 'P' ? : 2 0 2
449 }
450 }
451 } else {
452 21 ▶ $name = ($primary_tag eq 'GR') ? 'NAMED_META' :
Branch: 2 branches
BranchTrueFalseTotal
$primary_tag eq 'GR' ? : 2 19 21
$primary_tag eq 'GC' ? : 15 4 19
453 ($primary_tag eq 'GC') ? 'CONSENSUS_META' :
454 'CUSTOM';
455 21 ($secondary_tag, $isa_primary) = ('DATA', 1);
456 }
457  
458 # Since we can't determine whether data should be passed into the
459 # Handler until the next round (due to concatenation and combining
460 # data), we always check for the presence of the last chunk when the
461 # occasion calls for it (i.e. when the current data string needs to go
462 # into a new data chunk). If the data needs to be concatenated it is
463 # flagged above and checked below (and passed by if the conditions
464 # warrant it).
465  
466 # We run into a bit of a fencepost problem, (one chunk left over at
467 # the end); that is taken care of above when the end of the record is
468 # found.
469  
470 645 ▶ if ($isa_primary && defined $data_chunk && !$concat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isa_primary and defined $data_chunk and not $concat) 495 150 645
Condition100%6/6
$isa_primary and defined $data_chunk and not $concat
!ll&&!rl&&r
11634495
$isa_primary and defined $data_chunk
!ll&&!rl&&r
1088529
MC/DC100%3/3
$isa_primary and defined $data_chunk and not $concat
$isa_primary defined $data_chunk not $concat
Truth table100%4/4
A and B and not(C)
A= $isa_primaryB= defined $data_chunkC= not $concat
ABCresult
0XX0
10X0
1100
1111
471 495 $handler->data_handler($data_chunk);
472 495 undef $data_chunk;
473 }
474 645 $data_chunk->{NAME} = $name; # used for the handler
475 645 $data_chunk->{ALIGNMENT} = $align; # flag that determines chunk destination
476 645 ▶ $data_chunk->{$secondary_tag} .= (defined($data_chunk->{$secondary_tag})) ?
Branch: 1 branch
BranchTrueFalseTotal
defined $data_chunk->{$secondary_tag} ? : 57 588 645
477 ' '.$data : $data;
478 645 ▶ $data_chunk->{NSE} = $nse if $nse;
Branch: 1 branch
BranchTrueFalseTotal
if $nse 345 300 645
479 645 ▶ if ($name eq 'SEQUENCE' || $name eq 'NAMED_META' || $name eq 'CONSENSUS_META') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "SEQUENCE" or $name eq "NAMED_META" or $name eq "CONSENSUS_META") 301 344 645
Condition100%6/6
$name eq "SEQUENCE" or $name eq "NAMED_META" or $name eq "CONSENSUS_META"
l!l&&r!l&&!r
28615344
$name eq "SEQUENCE" or $name eq "NAMED_META"
l!l&&r!l&&!r
2842359
MC/DC100%3/3
$name eq "SEQUENCE" or $name eq "NAMED_META" or $name eq "CONSENSUS_META"
$name eq "SEQUENCE" $name eq "NAMED_META" $name eq "CONSENSUS_META"
Truth table100%4/4
A or B or C
A= $name eq "SEQUENCE"B= $name eq "NAMED_META"C= $name eq "CONSENSUS_META"
ABCresult
1XX1
01X1
0011
0000
480 301 $data_chunk->{BLOCK_LINE} = $self->{block_line};
481 301 ▶ $data_chunk->{META_TAG} = $feat if ($name ne 'SEQUENCE');
Branch: 1 branch
BranchTrueFalseTotal
if $name ne "SEQUENCE" 17 284 301
482 }
483 645 $last_feat = $feat;
484 }
485  
486 8 my $aln = $handler->build_alignment;
487 8 $handler->reset_parameters;
488 8 return $aln;
489 }
490  
491 =head2 write_aln
492  
493 Title : write_aln
494 Usage : $stream->write_aln(@aln)
495 Function: writes the $aln object into the stream in stockholm format
496 Returns : 1 for success and 0 for error
497 Args : L<Bio::Align::AlignI> object
498  
499 =cut
500  
501 {
502 my %LINK_CB = (
503 0 ▶ 'PDB' => sub {join('; ',($_[0]->database,
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[0]->optional_id || ''
l!l
00
MC/DC0%0/1
$_[0]->optional_id || ''
$_[0]->optional_id
Truth table0%0/2
A
A= $_[0]->optional_id
Aresult
11
00
504 $_[0]->primary_id.' '.
505 ($_[0]->optional_id || ''),
506 $_[0]->start,
507 $_[0]->end)).';'},
508 0 ▶ 'SCOP' => sub {join('; ',($_[0]->database,
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[0]->primary_id || ''
l!l
00
MC/DC0%0/1
$_[0]->primary_id || ''
$_[0]->primary_id
Truth table0%0/2
A
A= $_[0]->primary_id
Aresult
11
00
509 $_[0]->primary_id || '',
510 $_[0]->optional_id)).';'},
511 1 ▶ '_DEFAULT_' => sub {join('; ',($_[0]->database,
Subroutine __ANON__: called CC=1 SCAR=0.0
512 $_[0]->primary_id)).';'},
513 3 );
514  
515 1 ▶sub write_aln {
Subroutine write_aln: called CC=39 SCAR=50.4
Pod documented
516 # enable array of SimpleAlign objects as well (see clustalw write_aln())
517 1 my ($self, @aln) = @_;
518 1 for my $aln (@aln) {
519 1 ▶ $self->throw('Need Bio::Align::AlignI object')
Branch: 1 branch
BranchTrueFalseTotal
unless $aln and $aln->isa("Bio::Align::AlignI") 0 1 1
Condition33%1/3
$aln and $aln->isa("Bio::Align::AlignI")
!ll&&!rl&&r
001
MC/DC0%0/2
$aln and $aln->isa("Bio::Align::AlignI")
$aln $aln->isa("Bio::Align::AlignI")
Truth table33%1/3
A and B
A= $alnB= $aln->isa("Bio::Align::AlignI")
ABresult
0X0
100
111
520 if (!$aln || !($aln->isa('Bio::Align::AlignI')));
521  
522 1 my $coll = $aln->annotation;
523 1 my ($aln_ann, $seq_ann) =
524 ('#=GF ', '#=GS ');
525 1 ▶ $self->_print("# $STKVERSION\n") || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_print("# $STKVERSION\n") or return 0 1 0 1
526 1 ▶ $self->spaces && $self->_print("\n");
Branch: 1 branch
BranchTrueFalseTotal
$self->spaces and $self->_print("\n") 1 0 1
527 # annotations first
528  
529 #=GF XX ....
530 1 for my $param (@WRITEORDER) {
531 22 my @anns;
532 # no point in going through this if there is no annotation!
533 22 ▶ last if !$coll;
Branch: 1 branch
BranchTrueFalseTotal
unless $coll 0 22 22
534 # alignment annotations
535 22 my $ct = 1;
536 22 ▶ $self->throw("Bad parameter: $param") if !exists $WRITEMAP{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Bio::AlignIO::stockholm::WRITEMAP{$param} 0 22 22
537 # get the data, act on it based on the tag
538 22 my ($tag, $key) = split q(/), $WRITEMAP{$param};
539 22 ▶ if ($key eq 'Method') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'Method') { } 3 19 22
540 3 push @anns, $aln->$param;
541 } else {
542 19 @anns = $coll->get_Annotations($param);
543 }
544 22 my $rn = 1;
545 ANNOTATIONS:
546 22 for my $ann (@anns) {
547 # using Text::Wrap::wrap() for word wrap
548 15 my ($text, $alntag, $data);
549 15 ▶ if ($tag eq 'RX') {
Branch: 4 branches
BranchTrueFalseTotal
if ($tag eq 'RX') { } 0 15 15
elsif ($tag eq 'XX') { } 1 14 15
elsif ($tag eq 'SQ') { } 1 13 14
elsif ($tag eq 'DR') { } 1 12 13
550 REFS:
551 0 for my $rkey (qw(ref_comment ref_number ref_pubmed
552 ref_title ref_authors ref_location)) {
553 0 my ($newtag, $method) = split q(/), $WRITEMAP{$rkey};
554 0 $alntag = sprintf('%-10s',$aln_ann.$newtag);
555 0 ▶ if ($rkey eq 'ref_number') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rkey eq 'ref_number') { } 0 0 0
556 0 $data = "[$rn]";
557 } else {
558 0 $data = $ann->$method;
559 }
560 0 ▶ next REFS unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
561 0 $text = wrap($alntag, $alntag, $data);
562 0 ▶ $self->_print("$text\n") or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_print("$text\n") or return 0 0 0 0
563 }
564 0 $rn++;
565 0 next ANNOTATIONS;
566 }
567 elsif ($tag eq 'XX') { # custom
568 1 my $newtag = $ann->tagname;
569 1 my $tmp = $aln_ann.$newtag;
570 1 $alntag = sprintf('%-*s',length($tmp) + 1, $tmp);
571 1 $data = $ann->display_text;
572 }
573 elsif ($tag eq 'SQ') {
574 # use the actual number, not the stored Annotation data
575 1 my $tmp = $aln_ann.$tag;
576 1 $alntag = sprintf('%-*s',length($tmp) + 1, $tmp);
577 1 $data = $aln->num_sequences;
578 }
579 elsif ($tag eq 'DR') {
580 1 my $tmp = $aln_ann.$tag;
581 1 $alntag = sprintf('%-*s',length($tmp) + 1, $tmp);
582 1 my $db = uc $ann->database;
583 1 ▶ my $cb = exists $LINK_CB{$db} ? $LINK_CB{$db} : $LINK_CB{_DEFAULT_};
Branch: 1 branch
BranchTrueFalseTotal
exists $LINK_CB{$db} ? : 0 1 1
584 1 $data = $ann->display_text($cb);
585 }
586 else {
587 12 my $tmp = $aln_ann.$tag;
588 12 $alntag = sprintf('%-*s',length($tmp) + 1, $tmp);
589 12 ▶ $data = ref $ann ? $ann->display_text : $ann;
Branch: 1 branch
BranchTrueFalseTotal
ref $ann ? : 9 3 12
590 }
591 15 ▶ next unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 15 15
592 15 $text = wrap($alntag, $alntag, $data);
593 15 ▶ $self->_print("$text\n") || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_print("$text\n") or return 0 15 0 15
594 }
595 }
596  
597 #=GS <seq-id> AC xxxxxx
598 1 my $tag = 'AC';
599 1 for my $seq ($aln->each_seq) {
600 11 ▶ if (my $acc = $seq->accession_number) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $acc = $seq->accession_number) 11 0 11
601 11 my $text = sprintf("%-4s%-22s %-3s%s\n",$seq_ann,
602 $aln->displayname($seq->get_nse), $tag, $acc);
603 11 ▶ $self->_print($text) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_print($text) or return 0 11 0 11
604 }
605 }
606  
607 #=GS <seq-id> DR xxxxxx
608 1 $tag = 'DR';
609 1 for my $sf ($aln->get_SeqFeatures) {
610 0 ▶ if (my @links = $sf->annotation->get_Annotations('dblink')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@links) = $sf->annotation->get_Annotations("dblink")) 0 0 0
611 0 for my $link (@links) {
612 0 my $db = uc $link->database;
613 0 ▶ my $cb = exists $LINK_CB{$db} ? $LINK_CB{$db} : $LINK_CB{_DEFAULT_};
Branch: 1 branch
BranchTrueFalseTotal
exists $LINK_CB{$db} ? : 0 0 0
614 0 my $text = sprintf("%-4s%-22s%-3s%s\n",$seq_ann,
615 $aln->displayname($sf->entire_seq->get_nse),
616 $tag,
617 $link->display_text($cb));
618 0 ▶ $self->_print($text) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_print($text) or return 0 0 0 0
619 }
620 }
621 }
622  
623 1 ▶ $self->spaces && $self->_print("\n");
Branch: 1 branch
BranchTrueFalseTotal
$self->spaces and $self->_print("\n") 1 0 1
624 # now the sequences...
625  
626 1 my $blocklen = $self->line_length;
627 1 my $maxlen = $aln->maxdisplayname_length() + 3;
628 1 ▶ my $metalen = $aln->max_metaname_length() || 0;
Condition50%1/2
$aln->max_metaname_length || 0
l!l
10
MC/DC0%0/1
$aln->max_metaname_length || 0
$aln->max_metaname_length
Truth table50%1/2
A
A= $aln->max_metaname_length
Aresult
11
00
629 1 ▶ if ($blocklen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blocklen) { } 0 1 1
630 0 my $blockstart = 1;
631 0 my $alnlen = $aln->length;
632 0 ▶ while ($blockstart < $alnlen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blockstart < $alnlen) 0 0 0
633 0 my $subaln = $aln->slice($blockstart, $blockstart+$blocklen-1 ,1);
634 0 $self->_print_seqs($subaln,$maxlen,$metalen);
635 0 $blockstart += $blocklen;
636 0 ▶ $self->_print("\n") unless $blockstart >= $alnlen;
Branch: 1 branch
BranchTrueFalseTotal
unless $blockstart >= $alnlen 0 0 0
637 }
638 } else {
639 1 $self->_print_seqs($aln,$maxlen,$metalen);
640 }
641  
642 1 ▶ $self->_print("//\n") || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_print("//\n") or return 0 1 0 1
643 }
644 1 ▶ $self->flush() if $self->_flush_on_write && defined $self->_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_flush_on_write and defined $self->_fh 1 0 1
Condition33%1/3
$self->_flush_on_write and defined $self->_fh
!ll&&!rl&&r
001
MC/DC0%0/2
$self->_flush_on_write and defined $self->_fh
$self->_flush_on_write defined $self->_fh
Truth table33%1/3
A and B
A= $self->_flush_on_writeB= defined $self->_fh
ABresult
0X0
100
111
645  
646 1 return 1;
647 }
648  
649 }
650  
651 =head2 line_length
652  
653 Title : line_length
654 Usage : $obj->line_length($newval)
655 Function: Set the alignment output line length
656 Returns : value of line_length
657 Args : newvalue (optional)
658  
659 =cut
660  
661 3 ▶sub line_length {
Subroutine line_length: called CC=2 SCAR=7.4
Pod documented
662 1 my ( $self, $value ) = @_;
663 1 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 1 1
664 0 $self->{'_line_length'} = $value;
665 }
666 1 return $self->{'_line_length'};
667 }
668  
669 =head2 spaces
670  
671 Title : spaces
672 Usage : $obj->spaces(1)
673 Function: Set the 'spaces' flag, which prints extra newlines between the
674 header and the annotation and the annotation and the alignment
675 Returns : sequence data type
676 Args : newvalue (optional)
677  
678 =cut
679  
680 7 ▶sub spaces {
Subroutine spaces: called CC=2 SCAR=6.9
Pod documented
681 7 my $self = shift;
682 7 ▶ return $self->{'_spaces'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 5 2 7
683 2 return $self->{'_spaces'};
684 };
685  
686 =head2 alignhandler
687  
688 Title : alignhandler
689 Usage : $stream->alignhandler($handler)
690 Function: Get/Set the Bio::HandlerBaseI object
691 Returns : Bio::HandlerBaseI
692 Args : Bio::HandlerBaseI
693  
694 =cut
695  
696 13 ▶sub alignhandler {
Subroutine alignhandler: called CC=4 SCAR=14.5
Pod documented
697 13 my ($self, $handler) = @_;
698 13 ▶ if ($handler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler) 5 8 13
699 5 ▶ $self->throw("Not a Bio::HandlerBaseI") unless
Branch: 1 branch
BranchTrueFalseTotal
unless ref $handler and $handler->isa("Bio::HandlerBaseI") 0 5 5
Condition33%1/3
ref $handler and $handler->isa("Bio::HandlerBaseI")
!ll&&!rl&&r
005
MC/DC0%0/2
ref $handler and $handler->isa("Bio::HandlerBaseI")
ref $handler $handler->isa("Bio::HandlerBaseI")
Truth table33%1/3
A and B
A= ref $handlerB= $handler->isa("Bio::HandlerBaseI")
ABresult
0X0
100
111
700 ref($handler) && $handler->isa("Bio::HandlerBaseI");
701 5 $self->{'_alignhandler'} = $handler;
702 }
703 13 return $self->{'_alignhandler'};
704 }
705  
706 ############# PRIVATE INIT/HANDLER METHODS #############
707  
708 1 ▶sub _print_seqs {
Subroutine _print_seqs: called CC=9 SCAR=24.0
709 1 my ($self, $aln, $maxlen, $metalen) = @_;
710  
711 1 my ($seq_meta, $aln_meta) = ('#=GR','#=GC');
712 # modified (significantly) from AlignIO::pfam
713  
714 1 my ($namestr,$seq,$add);
715  
716 # pad extra for meta lines
717  
718 1 for $seq ( $aln->each_seq() ) {
719 11 my ($s, $e, $str) = ($seq->start, $seq->end, $seq->strand);
720 11 $namestr = $aln->displayname($seq->get_nse());
721 11 ▶ $self->_print(sprintf("%-*s%s\n",$maxlen+$metalen,
Branch: 1 branch
BranchTrueFalseTotal
$self->_print(sprintf("%-*s%s\n", $maxlen + $metalen, $namestr, $seq->seq)) or return 0 11 0 11
722 $namestr,
723 $seq->seq())) || return 0;
724 11 ▶ if ($seq->isa('Bio::Seq::MetaI')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq->isa("Bio::Seq::MetaI")) 11 0 11
725 11 for my $mname ($seq->meta_names) {
726 0 ▶ $self->_print(sprintf("%-*s%s\n",$maxlen+$metalen,
Branch: 1 branch
BranchTrueFalseTotal
$self->_print(sprintf("%-*s%s\n", $maxlen + $metalen, $seq_meta . ' ' . $namestr . ' ' . $mname, $seq->named_meta($mname))) or return 0 0 0 0
727 $seq_meta.' '.$namestr.' '.$mname,
728 $seq->named_meta($mname))) || return 0;
729 }
730 }
731 }
732 # alignment consensus
733 1 my $ameta = $aln->consensus_meta;
734 1 ▶ if ($ameta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ameta) 1 0 1
735 1 for my $mname ($ameta->meta_names) {
736 1 ▶ $self->_print(sprintf("%-*s%s\n",$maxlen+$metalen,
Branch: 1 branch
BranchTrueFalseTotal
$self->_print(sprintf("%-*s%s\n", $maxlen + $metalen, $aln_meta . ' ' . $mname, $ameta->named_meta($mname))) or return 0 1 0 1
737 $aln_meta.' '.$mname,
738 $ameta->named_meta($mname))) || return 0;
739 }
740 }
741 }
742  
743 3 1;
↑ Summary Index/Stockholm.pm »