Bio/AlignIO/fasta.pm

filter: Statementstmt 94.3% 83 / 88 Branchbran 71.8% 23 / 32 Conditioncond 40.0% 8 / 20 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 76.6% 125 / 163 CC 27 SCAR 25.7
CC
27
Cov
81%
write_aln
18.9
next_aln
13.4
width
3.1
CRAP 13.1
« Bio/OntologyIO/obo.pm ↑ Summary Bio/Tools/QRNA.pm »
1 #
2 # BioPerl module for Bio::AlignIO::fasta
3 #
4 # Copyright Peter Schattner
5 #
6 # You may distribute this module under the same terms as perl itself
7 # POD documentation - main docs before the code
8  
9 =head1 NAME
10  
11 Bio::AlignIO::fasta - fasta MSA Sequence input/output stream
12  
13 =head1 SYNOPSIS
14  
15 Do not use this module directly. Use it via the L<Bio::AlignIO>
16 class.
17  
18 =head1 DESCRIPTION
19  
20 This object can transform L<Bio::SimpleAlign> objects to and from
21 fasta flat files. This is for the fasta alignment format, not
22 for the FastA sequence analysis program. To process the alignments from
23 FastA (FastX, FastN, FastP, tFastA, etc) use the Bio::SearchIO module.
24  
25 =head1 FEEDBACK
26  
27 =head2 Support
28  
29 Please direct usage questions or support issues to the mailing list:
30  
31 I<bioperl-l@bioperl.org>
32  
33 rather than to the module maintainer directly. Many experienced and
34 reponsive experts will be able look at the problem and quickly
35 address it. Please include a thorough description of the problem
36 with code and data examples if at all possible.
37  
38 =head2 Reporting Bugs
39  
40 Report bugs to the Bioperl bug tracking system to help us keep track
41 the bugs and their resolution. Bug reports can be submitted via the
42 web:
43  
44 https://github.com/bioperl/bioperl-live/issues
45  
46 =head1 AUTHORS
47  
48 Peter Schattner
49  
50 =head1 APPENDIX
51  
52 The rest of the documentation details each of the object
53 methods. Internal methods are usually preceded with a _
54  
55 =cut
56  
57 # Let the code begin...
58  
59 package Bio::AlignIO::fasta;
60 4 $Bio::AlignIO::fasta::VERSION = '1.7.6';
61 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 4 ▶use base qw(Bio::AlignIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
64 4 our $WIDTH = 60;
65 4 ▶use Bio::LocatableSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 =head2 next_aln
68  
69 Title : next_aln
70 Usage : $aln = $stream->next_aln
71 Function: returns the next alignment in the stream.
72 Returns : Bio::Align::AlignI object - returns 0 on end of file
73 or on error
74 Args : -width => optional argument to specify the width sequence
75 will be written (60 chars by default)
76  
77 See L<Bio::Align::AlignI>
78  
79 =cut
80  
81 sub next_aln {
82 6 ▶ my $self = shift;
Subroutine next_aln: called CC=13 SCAR=25.9
Pod documented
83 6 my ($width) = $self->_rearrange( [qw(WIDTH)], @_ );
84 6 ▶ $self->width( $width || $WIDTH );
Condition33%1/3
$width || $Bio::AlignIO::fasta::WIDTH
l!l&&r!l&&!r
060
MC/DC0%0/2
$width || $Bio::AlignIO::fasta::WIDTH
$width $Bio::AlignIO::fasta::WIDTH
Truth table33%1/3
A || B
A= $widthB= $Bio::AlignIO::fasta::WIDTH
ABresult
1X1
011
000
85  
86 6 my ($start, $end, $name, $seqname, $seq, $seqchar,
87 $entry, $tempname, $tempdesc, %align, $desc, $maxlen
88 );
89 6 my $aln = Bio::SimpleAlign->new();
90  
91 6 ▶ while ( defined( $entry = $self->_readline ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($entry = $self->_readline)) 367 6 373
92 367 chomp $entry;
93 367 ▶ if ( $entry =~ s/^>\s*(\S+)\s*// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ s/^>\s*(\S+)\s*//) 51 316 367
94 51 $tempname = $1;
95 51 chomp($entry);
96 51 $tempdesc = $entry;
97 51 ▶ if ( defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 45 6 51
98 45 $seqchar =~ s/\s//g;
99 45 $seqname = $name;
100 45 $start = 1;
101 45 $end = $self->_get_len($seqchar);
102 45 $seq = Bio::LocatableSeq->new(
103 -seq => $seqchar,
104 -display_id => $seqname,
105 -description => $desc,
106 -start => $start,
107 -end => $end,
108 -alphabet => $self->alphabet,
109 );
110 45 $aln->add_seq($seq);
111 45 $self->debug("Reading $seqname\n");
112 }
113 51 $desc = $tempdesc;
114 51 $name = $tempname;
115 51 $desc = $entry;
116 51 $seqchar = "";
117 51 next;
118 }
119  
120 # removed redundant symbol validation
121 # this is already done in Bio::PrimarySeq
122 316 $seqchar .= $entry;
123 }
124  
125 # Next two lines are to silence warnings that
126 # otherwise occur at EOF when using <$fh>
127 6 ▶ $name = "" if ( !defined $name );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 6 6
128 6 ▶ $seqchar = "" if ( !defined $seqchar );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seqchar 0 6 6
129 6 $seqchar =~ s/\s//g;
130  
131 # Put away last name and sequence
132 6 ▶ if ( $name =~ /(\S+\/(\d+)-(\d+))$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m[(\S+/(\d+)-(\d+))$]) { } 4 2 6
133 4 $seqname = $1;
134 4 $start = $2;
135 4 $end = $3;
136 }
137 else {
138 2 $seqname = $name;
139 2 $start = 1;
140 2 $end = $self->_get_len($seqchar);
141 }
142  
143 # This logic now also reads empty lines at the
144 # end of the file. Skip this is seqchar and seqname is null
145 6 ▶ unless ( length($seqchar) == 0 && length($seqname) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $seqchar == 0 and length $seqname == 0) 6 0 6
Condition33%1/3
length $seqchar == 0 and length $seqname == 0
!ll&&!rl&&r
600
MC/DC0%0/2
length $seqchar == 0 and length $seqname == 0
length $seqchar == 0 length $seqname == 0
Truth table33%1/3
A and B
A= length $seqchar == 0B= length $seqname == 0
ABresult
0X0
100
111
146 6 $seq = Bio::LocatableSeq->new(
147 -seq => $seqchar,
148 -display_id => $seqname,
149 -description => $desc,
150 -start => $start,
151 -end => $end,
152 -alphabet => $self->alphabet,
153 );
154 6 $aln->add_seq($seq);
155 6 $self->debug("Reading $seqname\n");
156 }
157 6 my $alnlen = $aln->length;
158 6 foreach my $seq ( $aln->each_seq ) {
159 51 ▶ if ( $seq->length < $alnlen ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq->length < $alnlen) 28 23 51
160 28 my ($diff) = ( $alnlen - $seq->length );
161 28 $seq->seq( $seq->seq() . "-" x $diff );
162 }
163 }
164  
165 # no sequences means empty alignment (possible EOF)
166 6 ▶ return $aln if $aln->num_sequences;
Branch: 1 branch
BranchTrueFalseTotal
if $aln->num_sequences 6 0 6
167 0 return;
168 }
169  
170  
171 =head2 write_aln
172  
173 Title : write_aln
174 Usage : $stream->write_aln(@aln)
175 Function: writes the $aln object into the stream in fasta format
176 Returns : 1 for success and 0 for error
177 Args : L<Bio::Align::AlignI> object
178  
179 See L<Bio::Align::AlignI>
180  
181 =cut
182  
183 sub write_aln {
184 2 ▶ my ($self,@aln) = @_;
Subroutine write_aln: called CC=13 SCAR=29.4
Pod documented
185 2 my $width = $self->width;
186 2 my ($seq,$desc,$rseq,$name,$count,$length,$seqsub);
187  
188 2 foreach my $aln (@aln) {
189 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
190 0 $self->warn("Must provide a Bio::Align::AlignI object when calling write_aln");
191 0 next;
192 }
193 2 ▶ if( $self->force_displayname_flat ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->force_displayname_flat) 0 2 2
194 0 $aln->set_displayname_flat(1);
195 }
196 2 foreach $rseq ( $aln->each_seq() ) {
197 17 $name = $aln->displayname($rseq->get_nse());
198 17 $seq = $rseq->seq();
199 17 ▶ $desc = $rseq->description || '';
Condition100%2/2
$rseq->description || ''
l!l
314
MC/DC100%1/1
$rseq->description || ''
$rseq->description
Truth table100%2/2
A
A= $rseq->description
Aresult
11
00
200 17 ▶ $desc = ' '.$desc if $desc;
Branch: 1 branch
BranchTrueFalseTotal
if $desc 3 14 17
201 17 ▶ $self->_print (">$name$desc\n") or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_print(">$name$desc\n") or return 0 17 17
202 17 $count = 0;
203 17 $length = length($seq);
204 17 ▶ if(defined $seq && $length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seq and $length > 0) { } 17 0 17
Condition33%1/3
defined $seq and $length > 0
!ll&&!rl&&r
0017
MC/DC0%0/2
defined $seq and $length > 0
defined $seq $length > 0
Truth table33%1/3
A and B
A= defined $seqB= $length > 0
ABresult
0X0
100
111
205 17 $seq =~ s/(.{1,$width})/$1\n/g;
206 } else {
207 0 $seq = "\n";
208 }
209 17 $self->_print($seq);
210 }
211 }
212 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
213 2 return 1;
214 }
215  
216 =head2 _get_len
217  
218 Title : _get_len
219 Usage :
220 Function: determine number of alphabetic chars
221 Returns : integer
222 Args : sequence string
223  
224 =cut
225  
226 sub _get_len {
227 47 ▶ my ($self,$seq) = @_;
Subroutine _get_len: called CC=1 SCAR=0.0
228 47 my $chars = $Bio::LocatableSeq::GAP_SYMBOLS.$Bio::LocatableSeq::FRAMESHIFT_SYMBOLS;
229 47 $seq =~ s{[$chars]+}{}gi;
230 47 return CORE::length($seq);
231 }
232  
233 =head2 width
234  
235 Title : width
236 Usage : $obj->width($newwidth)
237 $width = $obj->width;
238 Function: Get/set width of alignment
239 Returns : integer value of width
240 Args : on set, new value (a scalar or undef, optional)
241  
242  
243 =cut
244  
245 sub width{
246 8 ▶ my $self = shift;
Subroutine width: called CC=3 SCAR=11.4
Pod documented
247  
248 8 ▶ return $self->{'_width'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 6 2 8
249 2 ▶ return $self->{'_width'} || $WIDTH;
Condition33%1/3
$self->{'_width'} || $Bio::AlignIO::fasta::WIDTH
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{'_width'} || $Bio::AlignIO::fasta::WIDTH
$self->{'_width'} $Bio::AlignIO::fasta::WIDTH
Truth table33%1/3
A || B
A= $self->{'_width'}B= $Bio::AlignIO::fasta::WIDTH
ABresult
1X1
011
000
250 }
251  
252 4 1;
« Bio/OntologyIO/obo.pm ↑ Summary Bio/Tools/QRNA.pm »