Bio/AlignIO/msf.pm

filter: Statementstmt 90.0% 90 / 100 Branchbran 75.0% 27 / 36 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 83.2% 134 / 161 CC 26 SCAR 25.1
CC
26
Cov
84%
write_aln
15.6
next_aln
13.9
BEGIN
1.0
CRAP 12.3
« Bio/Tools/EMBOSS/Palindrome.pm ↑ Summary Bio/TreeIO/lintree.pm »
1 #
2 # BioPerl module for Bio::AlignIO::msf
3 # based on the Bio::SeqIO::msf module
4 # by Ewan Birney <birney@ebi.ac.uk>
5 # and Lincoln Stein <lstein@cshl.org>
6 #
7 # and the SimpleAlign.pm module of Ewan Birney
8 #
9 # Copyright Peter Schattner
10 #
11 # You may distribute this module under the same terms as perl itself
12 # _history
13 # September 5, 2000
14 # POD documentation - main docs before the code
15  
16 =head1 NAME
17  
18 Bio::AlignIO::msf - msf sequence input/output stream
19  
20 =head1 SYNOPSIS
21  
22 Do not use this module directly. Use it via the L<Bio::AlignIO> class.
23  
24 =head1 DESCRIPTION
25  
26 This object can transform L<Bio::Align::AlignI> objects to and from msf
27 flat file databases.
28  
29 =head1 FEEDBACK
30  
31 =head2 Support
32  
33 Please direct usage questions or support issues to the mailing list:
34  
35 I<bioperl-l@bioperl.org>
36  
37 rather than to the module maintainer directly. Many experienced and
38 reponsive experts will be able look at the problem and quickly
39 address it. Please include a thorough description of the problem
40 with code and data examples if at all possible.
41  
42 =head2 Reporting Bugs
43  
44 Report bugs to the Bioperl bug tracking system to help us keep track
45 the bugs and their resolution. Bug reports can be submitted via the
46 web:
47  
48 https://github.com/bioperl/bioperl-live/issues
49  
50 =head1 AUTHORS - Peter Schattner
51  
52 Email: schattner@alum.mit.edu
53  
54  
55 =head1 APPENDIX
56  
57 The rest of the documentation details each of the object
58 methods. Internal methods are usually preceded with a _
59  
60 =cut
61  
62 # Let the code begin...
63  
64 package Bio::AlignIO::msf;
65 3 $Bio::AlignIO::msf::VERSION = '1.7.8';
66 3 ▶use vars qw(%valid_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
67 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 3 ▶use Bio::SeqIO::gcg; # for GCG_checksum()
Subroutine BEGIN: called CC=1 SCAR=0.0
70 3 ▶use Bio::SimpleAlign;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 3 ▶use base qw(Bio::AlignIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
75 3 %valid_type = qw( dna N rna N protein P );
76 }
77  
78 =head2 next_aln
79  
80 Title : next_aln
81 Usage : $aln = $stream->next_aln()
82 Function: returns the next alignment in the stream. Tries to read *all* MSF
83 It reads all non whitespace characters in the alignment
84 area. For MSFs with weird gaps (eg ~~~) map them by using
85 $aln->map_chars('~','-')
86 Returns : Bio::Align::AlignI object
87 Args : NONE
88  
89 =cut
90  
91 15 ▶sub next_aln {
Subroutine next_aln: called CC=12 SCAR=26.3
Pod documented
92 15 my $self = shift;
93 15 my $entry;
94 15 my (%hash,$name,$str,@names,$seqname,$start,$end,$count,$seq);
95  
96 15 my $aln = Bio::SimpleAlign->new(-source => 'gcg' );
97  
98 15 ▶ while( $entry = $self->_readline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry = $self->_readline) 315 0 315
99 315 ▶ $entry =~ m{//} && last; # move to alignment section
Branch: 1 branch
BranchTrueFalseTotal
$entry =~ m[//] and last 15 300 315
100 300 ▶ $entry =~ /Name:\s+(\S+)/ && do { $name = $1;
Branch: 1 branch
BranchTrueFalseTotal
$entry =~ /Name:\s+(\S+)/ and do { $name = $1; $hash{$name} = ""; push @names, $name } 240 60 300
101 240 $hash{$name} = ""; # blank line
102 240 push(@names,$name); # we need it ordered!
103 };
104 # otherwise - skip
105 }
106  
107 # alignment section
108  
109 15 ▶ while( $entry = $self->_readline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry = $self->_readline) 1380 15 1395
110 1380 ▶ next if ( $entry =~ /^\s+(\d+)/ ) ;
Branch: 1 branch
BranchTrueFalseTotal
if $entry =~ /^\s+(\d+)/ 0 1380 1380
111 1380 ▶ $entry =~ /^\s*(\S+)\s+(.*)$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
$entry =~ /^\s*(\S+)\s+(.*)$/ and do { $name = $1; $str = $2; unless (exists $hash{$name}) { $self->throw("$name exists as an alignment line but not in the header. Not confident of what is going on!"); } ; $str =~ s/\s//g; $str =~ s/~/-/g; $hash{$name} .= $str } 1200 180 1380
112 1200 $name = $1;
113 1200 $str = $2;
114 1200 ▶ if( ! exists $hash{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $hash{$name}) 0 1200 1200
115 0 $self->throw("$name exists as an alignment line but not in the header. Not confident of what is going on!");
116 }
117 1200 $str =~ s/\s//g;
118 1200 $str =~ s/~/-/g;
119 1200 $hash{$name} .= $str;
120 };
121 }
122  
123 15 ▶ return if @names < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @names < 1 0 15 15
124  
125 # now got this as a name - sequence hash. Let's make some sequences!
126  
127 15 for $name ( @names ) {
128 240 ▶ if( $name =~ m{(\S+)/(\d+)-(\d+)} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m[(\S+)/(\d+)-(\d+)]) { } 240 0 240
129 240 $seqname = $1;
130 240 $start = $2;
131 240 $end = $3;
132 } else {
133 0 $seqname = $name;
134 0 $start = 1;
135 0 $str = $hash{$name};
136 0 $str =~ s/[^0-9A-Za-z$Bio::LocatableSeq::OTHER_SYMBOLS]//g;
137  
138 0 $end = length($str);
139 }
140  
141 240 $seq = Bio::LocatableSeq->new('-seq' => $hash{$name},
142 '-display_id' => $seqname,
143 '-start' => $start,
144 '-end' => $end,
145 '-alphabet' => $self->alphabet,
146 );
147 240 $aln->add_seq($seq);
148  
149 # If $end <= 0, we have either reached the end of
150 # file in <> or we have encountered some other error
151  
152 }
153 15 ▶ return $aln if $aln->num_sequences;
Branch: 1 branch
BranchTrueFalseTotal
if $aln->num_sequences 15 0 15
154 0 return;
155 }
156  
157  
158 =head2 write_aln
159  
160 Title : write_aln
161 Usage : $stream->write_aln(@aln)
162 Function: writes the $aln object into the stream in MSF format
163 Sequence type of the alignment is determined by the first sequence.
164 Returns : 1 for success and 0 for error
165 Args : Bio::Align::AlignI object
166  
167  
168 =cut
169  
170 2 ▶sub write_aln {
Subroutine write_aln: called CC=15 SCAR=27.5
Pod documented
171 2 my ($self,@aln) = @_;
172 2 my $msftag;
173 my $type;
174 2 my $count = 0;
175 2 my $maxname;
176 2 my ($length,$date,$name,$seq,$miss,$pad,%hash,@arr,$tempcount,$index);
177 2 foreach my $aln (@aln) {
178 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
179 0 $self->warn("Must provide a Bio::Align::AlignI object when calling write_aln");
180 0 next;
181 }
182 2 $date = localtime(time);
183 2 $msftag = "MSF";
184 2 $type = $valid_type{$aln->get_seq_by_pos(1)->alphabet};
185 2 $maxname = $aln->maxdisplayname_length();
186 2 $length = $aln->length();
187 2 $name = $aln->id();
188 2 ▶ if( !defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 2 2
189 0 $name = "Align";
190 }
191  
192 2 $self->_print (sprintf("\n%s MSF: %d Type: %s %s Check: 00 ..\n\n",
193 $name, $aln->num_sequences, $type, $date));
194  
195 2 ▶ my $seqCountFormat = "%".($maxname > 20 ? $maxname + 2: 22)."s%-27d%27d\n";
Branch: 1 branch
BranchTrueFalseTotal
$maxname > 20 ? : 1 1 2
196 2 ▶ my $seqNameFormat = "%-".($maxname > 20 ? $maxname : 20)."s ";
Branch: 1 branch
BranchTrueFalseTotal
$maxname > 20 ? : 1 1 2
197
198 2 foreach $seq ( $aln->each_seq() ) {
199 22 $name = $aln->displayname($seq->get_nse());
200 22 $miss = $maxname - length ($name);
201 22 $miss += 2;
202 22 $pad = " " x $miss;
203  
204 22 $self->_print (sprintf(" Name: %s%sLen: %d Check: %d Weight: 1.00\n",$name,$pad,length $seq->seq(), Bio::SeqIO::gcg->GCG_checksum($seq)));
205  
206 22 $hash{$name} = $seq->seq();
207 22 push(@arr,$name);
208 }
209 # ok - heavy handed, but there you go.
210 #
211 2 $self->_print ("\n//\n\n\n");
212  
213 2 ▶ while( $count < $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < $length) 14 2 16
214 # there is another block to go!
215 14 $self->_print (sprintf($seqCountFormat,' ',$count+1,$count+50));
216 14 foreach $name ( @arr ) {
217 134 $self->_print (sprintf($seqNameFormat,$name));
218  
219 134 $tempcount = $count;
220 134 $index = 0;
221 134 ▶ while( ($tempcount + 10 < $length) && ($index < 5) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tempcount + 10 < $length and $index < 5) 636 134 770
Condition100%3/3
$tempcount + 10 < $length and $index < 5
!ll&&!rl&&r
22112636
MC/DC100%2/2
$tempcount + 10 < $length and $index < 5
$tempcount + 10 < $length $index < 5
Truth table100%3/3
A and B
A= $tempcount + 10 < $lengthB= $index < 5
ABresult
0X0
100
111
222  
223 636 $self->_print (sprintf("%s ",substr($hash{$name},
224 $tempcount,10)));
225  
226 636 $tempcount += 10;
227 636 $index++;
228 } #
229 # ok, could be the very last guy ;)
230 #
231 134 ▶ if( $index < 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < 5) 22 112 134
232 # space to print!
233 #
234 22 $self->_print (sprintf("%s ",substr($hash{$name},$tempcount)));
235 22 $tempcount += 10;
236 }
237 134 $self->_print ("\n");
238 }
239 14 $self->_print ("\n\n");
240 14 $count = $tempcount;
241 }
242 }
243 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
244 2 return 1;
245 }
246  
247 3 1;
« Bio/Tools/EMBOSS/Palindrome.pm ↑ Summary Bio/TreeIO/lintree.pm »