Bio/AlignIO/metafasta.pm

filter: Statementstmt 86.3% 76 / 88 Branchbran 50.0% 16 / 32 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 73.3% 110 / 150 CC 25 SCAR 23.8
CC
25
Cov
74%
next_aln
17.3
write_aln
14.5
width
3.8
CRAP 10.8
« Bio/Annotation/Target.pm ↑ Summary Bio/SearchIO/blast_pull.pm »
1 #
2 # BioPerl module for Bio::AlignIO::metafasta
3 #
4 # Copyright Heikki Lehvaslaiho
5 #
6 # You may distribute this module under the same terms as perl itself
7 #
8 # POD documentation - main docs before the code
9  
10 =head1 NAME
11  
12 Bio::AlignIO::metafasta - Metafasta MSA Sequence input/output stream
13  
14 =head1 SYNOPSIS
15  
16 Do not use this module directly. Use it via the L<Bio::AlignIO> class.
17  
18 =head1 DESCRIPTION
19  
20 This object can transform L<Bio::SimpleAlign> objects to and from
21 metafasta flat file databases.
22  
23 The format of a metafasta file is
24  
25 >test/1-25
26 ABCDEFHIJKLMNOPQRSTUVWXYZ
27 &charge
28 NBNAANCNJCNNNONNCNNUNNXNZ
29 &chemical
30 LBSAARCLJCLSMOIMCHHULRXRZ
31  
32 where the sequence block is followed by one or several meta blocks.
33 Each meta block starts with the ampersand character '&' in the first
34 column and is immediately followed by the name of the meta data which
35 continues until the new line. The meta data follows it. All
36 characters, except new line, are important in meta data.
37  
38 =head1 SEE ALSO
39  
40 L<Bio::SeqIO::metafasta>
41  
42 =head1 FEEDBACK
43  
44 =head2 Support
45  
46 Please direct usage questions or support issues to the mailing list:
47  
48 I<bioperl-l@bioperl.org>
49  
50 rather than to the module maintainer directly. Many experienced and
51 reponsive experts will be able look at the problem and quickly
52 address it. Please include a thorough description of the problem
53 with code and data examples if at all possible.
54  
55 =head2 Reporting Bugs
56  
57 Report bugs to the Bioperl bug tracking system to help us keep track
58 the bugs and their resolution. Bug reports can be submitted via the
59 web:
60  
61 https://github.com/bioperl/bioperl-live/issues
62  
63 =head1 AUTHOR - Heikki Lehvaslaiho
64  
65 Email heikki-at-bioperl-dot-org
66  
67 =head1 APPENDIX
68  
69 The rest of the documentation details each of the object
70 methods. Internal methods are usually preceded with a _
71  
72 =cut
73  
74 # Let the code begin...
75  
76 package Bio::AlignIO::metafasta;
77 4 $Bio::AlignIO::metafasta::VERSION = '1.7.8';
78 4 ▶use vars qw($WIDTH);
Subroutine BEGIN: called CC=1 SCAR=0.0
79 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 4 ▶use Bio::SimpleAlign;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 4 ▶use Bio::Seq::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 4 ▶use Bio::Seq::SeqFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 4 ▶use Bio::Seq::SeqFastaSpeedFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 4 ▶use base qw(Bio::AlignIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 4 ▶BEGIN { $WIDTH = 60}
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 10 ▶sub _initialize {
Subroutine _initialize: called CC=2 SCAR=7.0
91 10 my($self,@args) = @_;
92 10 $self->SUPER::_initialize(@args);
93 10 my ($width) = $self->_rearrange([qw(WIDTH)], @args);
94 10 ▶ $width && $self->width($width);
Branch: 1 branch
BranchTrueFalseTotal
$width and $self->width($width) 0 10 10
95 }
96  
97 =head2 next_aln
98  
99 Title : next_aln
100 Usage : $aln = $stream->next_aln()
101 Function: returns the next alignment in the stream.
102 Returns : L<Bio::Align::AlignI> object - returns 0 on end of file
103 or on error
104 Args : NONE
105  
106 =cut
107  
108 8 ▶sub next_aln {
Subroutine next_aln: called CC=12 SCAR=28.5
Pod documented
109 8 my( $self ) = @_;
110 8 my $seq;
111 my $alphabet;
112 8 local $/ = "\n>";
113  
114 8 my $aln = Bio::SimpleAlign->new();
115  
116 8 ▶ while(defined (my $entry = $self->_readline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = $self->_readline)) 16 8 24
117 16 chomp($entry);
118 16 ▶ if ($entry =~ m/\A\s*\Z/s) { # very first one
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /\A\s*\Z/s) 0 16 16
119 0 ▶ return unless $entry = $self->_readline;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry = $self->_readline 0 0 0
120 0 chomp($entry);
121 }
122 16 $entry =~ s/^>//;
123  
124 16 my ($top,$sequence) = split(/\n/,$entry,2);
125 16 ▶ defined $sequence && $sequence =~ s/>//g;
Branch: 1 branch
BranchTrueFalseTotal
defined $sequence and $sequence =~ s/>//g 16 0 16
126  
127 16 my @metas;
128 16 ($sequence, @metas) = split /\n&/, $sequence;
129  
130 16 my ($id, $start, $end);
131 16 ▶ if ( $top =~ /(\S+)\/(\d+)-(\d+)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($top =~ m[(\S+)/(\d+)-(\d+)]) { } 16 0 16
elsif ($top =~ /(\S+)/) { } 0 0 0
132 16 $id = $1;
133 16 $start = $2;
134 16 $end = $3;
135 }
136 elsif ($top =~ /(\S+)/) {
137 0 $id = $1;
138 0 $start = 1;
139 0 $end = length($sequence);
140 }
141  
142 16 ▶ defined $sequence && $sequence =~ s/\s//g; # Remove whitespace
Branch: 1 branch
BranchTrueFalseTotal
defined $sequence and $sequence =~ s/\s//g 16 0 16
143  
144 16 $seq = Bio::Seq::Meta->new('-seq' => $sequence,
145 '-display_id' => $id,
146 '-start' => $start,
147 '-end' => $end,
148 '-alphabet' => $self->alphabet,
149 );
150  
151 16 foreach my $meta (@metas) {
152 16 my ($name,$string) = split /\n/, $meta;
153 16 $string =~ s/\n//g; # Remove newlines, spaces are important
154 16 $seq->named_meta($name, $string);
155 }
156  
157 16 $aln->add_seq($seq);
158
159 # alignment needs seqs all the same length, pad with gaps
160 16 my $alnlen = $aln->length;
161 16 foreach my $seq ( $aln->each_seq ) {
162 24 ▶ if ( $seq->length < $alnlen ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq->length < $alnlen) 0 24 24
163 0 my ($diff) = ($alnlen - $seq->length);
164 0 $seq->seq( $seq->seq() . "-" x $diff);
165 }
166 }
167 }
168 8 ▶ return $aln if $aln->num_sequences;
Branch: 1 branch
BranchTrueFalseTotal
if $aln->num_sequences 8 0 8
169 0 return;
170 }
171  
172 =head2 write_aln
173  
174 Title : write_aln
175 Usage : $stream->write_aln(@aln)
176 Function: writes the $aln object into the stream in fasta format
177 Returns : 1 for success and 0 for error
178 Args : L<Bio::Align::AlignI> object
179  
180 =cut
181  
182 2 ▶sub write_aln {
Subroutine write_aln: called CC=11 SCAR=26.7
Pod documented
183 2 my ($self,@aln) = @_;
184 2 my $width = $self->width;
185  
186 2 foreach my $aln (@aln) {
187 2 ▶ if( ! $aln || ! $aln->isa('Bio::Align::AlignI') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($aln and $aln->isa("Bio::Align::AlignI")) 0 2 2
Condition33%1/3
$aln and $aln->isa("Bio::Align::AlignI")
!ll&&!rl&&r
002
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
188 0 $self->warn("Must provide a Bio::Align::AlignI object when calling write_aln");
189 0 next;
190 }
191 2 foreach my $seq ( $aln->each_seq() ) {
192 8 my $name = $aln->displayname($seq->get_nse);
193  
194 8 my $str = $seq->seq();
195 8 ▶ if(length($str) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str > 0) { } 8 0 8
196 8 $str =~ s/(.{1,$width})/$1\n/g;
197 } else {
198 0 $str = "\n";
199 }
200 8 ▶ $self->_print (">",$name,"\n",$str) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_print(">", $name, "\n", $str) or return 8 0 8
201 8 ▶ if ($seq->isa('Bio::Seq::MetaI')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq->isa("Bio::Seq::MetaI")) 2 6 8
202 2 foreach my $meta ($seq->meta_names) {
203 2 my $str = $seq->named_meta($meta);
204 2 $str =~ s/(.{1,$width})/$1\n/g;
205 2 $self->_print ("&",$meta,"\n",$str);
206 }
207 }
208 }
209 }
210 2 ▶ $self->flush if $self->_flush_on_write && defined $self->_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_flush_on_write and defined $self->_fh 2 0 2
Condition33%1/3
$self->_flush_on_write and defined $self->_fh
!ll&&!rl&&r
002
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
211 2 return 1;
212 }
213  
214  
215 =head2 width
216  
217 Title : width
218 Usage : $obj->width($newval)
219 Function: Get/Set the line width for METAFASTA output
220 Returns : value of width
221 Args : newvalue (optional)
222  
223  
224 =cut
225  
226 2 ▶sub width{
Subroutine width: called CC=3 SCAR=13.3
Pod documented
227 2 my ($self,$value) = @_;
228 2 ▶ if( defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 2 2
229 0 $self->{'width'} = $value;
230 }
231 2 ▶ return $self->{'width'} || $WIDTH;
Condition33%1/3
$self->{'width'} || $Bio::AlignIO::metafasta::WIDTH
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{'width'} || $Bio::AlignIO::metafasta::WIDTH
$self->{'width'} $Bio::AlignIO::metafasta::WIDTH
Truth table33%1/3
A || B
A= $self->{'width'}B= $Bio::AlignIO::metafasta::WIDTH
ABresult
1X1
011
000
232 }
233  
234 4 1;
« Bio/Annotation/Target.pm ↑ Summary Bio/SearchIO/blast_pull.pm »