Bio/SeqIO/metafasta.pm

filter: Statementstmt 89.4% 76 / 85 Branchbran 44.7% 17 / 38 Conditioncond 33.3% 7 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 66.6% 114 / 171 CC 30 SCAR 27.7
CC
30
Cov
69%
write_seq
22.7
next_seq
22.4
width
3.8
CRAP 15.9
« Bio/DB/Failover.pm ↑ Summary Bio/Tools/Glimmer.pm »
1 # BioPerl module for Bio::SeqIO::metafasta
2 #
3 # Please direct questions and support issues to <bioperl-l@bioperl.org>
4 #
5 # Cared for by Heikki Lehvaslaiho
6 #
7 # Copyright Heikki Lehvaslaiho
8 #
9 # You may distribute this module under the same terms as perl itself
10  
11 # POD documentation - main docs before the code
12  
13 =head1 NAME
14  
15 Bio::SeqIO::metafasta - metafasta sequence input/output stream
16  
17 =head1 SYNOPSIS
18  
19 Do not use this module directly. Use it via the Bio::SeqIO class.
20  
21 use Bio::SeqIO;
22  
23 # read the metafasta file
24 $io = Bio::SeqIO->new(-file => "test.metafasta",
25 -format => "metafasta" );
26  
27 $seq = $io->next_seq;
28  
29 =head1 DESCRIPTION
30  
31 This object can transform Bio::Seq::Meta objects to and from metafasta
32 flat file databases.
33  
34 For sequence part the code is an exact copy of Bio::SeqIO::fasta
35 module. The only added bits deal with meta data IO.
36  
37 The format of a metafasta file is
38  
39 >test
40 ABCDEFHIJKLMNOPQRSTUVWXYZ
41 &charge
42 NBNAANCNJCNNNONNCNNUNNXNZ
43 &chemical
44 LBSAARCLJCLSMOIMCHHULRXRZ
45  
46 where the sequence block is followed by one or several meta blocks.
47 Each meta block starts with the ampersand character '&' in the first
48 column and is immediately followed by the name of the meta data which
49 continues until the new line. The meta data follows it. All
50 characters, except new line, are important in meta data.
51  
52 =head1 FEEDBACK
53  
54 =head2 Mailing Lists
55  
56 User feedback is an integral part of the evolution of this and other
57 Bioperl modules. Send your comments and suggestions preferably to one
58 of the Bioperl mailing lists. Your participation is much appreciated.
59  
60 bioperl-l@bioperl.org - General discussion
61 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
62  
63 =head2 Support
64  
65 Please direct usage questions or support issues to the mailing list:
66  
67 I<bioperl-l@bioperl.org>
68  
69 rather than to the module maintainer directly. Many experienced and
70 reponsive experts will be able look at the problem and quickly
71 address it. Please include a thorough description of the problem
72 with code and data examples if at all possible.
73  
74 =head2 Reporting Bugs
75  
76 Report bugs to the Bioperl bug tracking system to help us keep track
77 the bugs and their resolution. Bug reports can be submitted via the
78 web:
79  
80 https://github.com/bioperl/bioperl-live/issues
81  
82 =head1 AUTHOR - Heikki Lehvaslaiho
83  
84 Email heikki-at-bioperl-dot-org
85  
86 =head1 APPENDIX
87  
88 The rest of the documentation details each of the object
89 methods. Internal methods are usually preceded with a _
90  
91 =cut
92  
93 # Let the code begin...
94  
95 package Bio::SeqIO::metafasta;
96 3 $Bio::SeqIO::metafasta::VERSION = '1.7.8';
97 3 ▶use vars qw($WIDTH);
Subroutine BEGIN: called CC=1 SCAR=0.0
98 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 3 ▶use Bio::Seq::SeqFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 3 ▶use Bio::Seq::SeqFastaSpeedFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 3 ▶use Bio::Seq::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 3 ▶use base qw(Bio::SeqIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 3 ▶BEGIN { $WIDTH = 60}
Subroutine BEGIN: called CC=1 SCAR=0.0
107  
108 3 ▶sub _initialize {
Subroutine _initialize: called CC=3 SCAR=11.2
109 3 my($self,@args) = @_;
110 3 $self->SUPER::_initialize(@args);
111 3 my ($width) = $self->_rearrange([qw(WIDTH)], @args);
112 3 ▶ $width && $self->width($width);
Branch: 1 branch
BranchTrueFalseTotal
$width and $self->width($width) 0 3 3
113 3 ▶ unless ( defined $self->sequence_factory ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->sequence_factory) 3 0 3
114 3 $self->sequence_factory(Bio::Seq::SeqFastaSpeedFactory->new());
115 }
116 }
117  
118 =head2 next_seq
119  
120 Title : next_seq
121 Usage : $seq = $stream->next_seq()
122 Function: returns the next sequence in the stream
123 Returns : Bio::Seq object
124 Args : NONE
125  
126 =cut
127  
128 2 ▶sub next_seq {
Subroutine next_seq: called CC=14 SCAR=31.1
Pod documented
129 2 my( $self ) = @_;
130 2 my $seq;
131 my $alphabet;
132 2 local $/ = "\n>";
133 2 ▶ return unless my $entry = $self->_readline;
Branch: 1 branch
BranchTrueFalseTotal
unless my $entry = $self->_readline 0 2 2
134  
135 2 chomp($entry);
136 2 ▶ if ($entry =~ m/\A\s*\Z/s) { # very first one
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /\A\s*\Z/s) 0 2 2
137 0 ▶ return unless $entry = $self->_readline;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry = $self->_readline 0 0 0
138 0 chomp($entry);
139 }
140 2 $entry =~ s/^>//;
141  
142 2 my ($top,$sequence) = split(/\n/,$entry,2);
143 2 ▶ defined $sequence && $sequence =~ s/>//g;
Branch: 1 branch
BranchTrueFalseTotal
defined $sequence and $sequence =~ s/>//g 2 0 2
144  
145 2 my @metas;
146 2 ($sequence, @metas) = split /\n&/, $sequence;
147  
148 2 my ($id,$fulldesc);
149 2 ▶ if( $top =~ /^\s*(\S+)\s*(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top =~ /^\s*(\S+)\s*(.*)/) 2 0 2
150 2 ($id,$fulldesc) = ($1,$2);
151 }
152  
153 0 ▶ if (defined $id && $id eq '') {$id=$fulldesc;} # FIX incase no space
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id and $id eq "") 0 2 2
Condition33%1/3
defined $id and $id eq ""
!ll&&!rl&&r
020
MC/DC0%0/2
defined $id and $id eq ""
defined $id $id eq ""
Truth table33%1/3
A and B
A= defined $idB= $id eq ""
ABresult
0X0
100
111
154 # between > and name \AE
155 2 ▶ defined $sequence && $sequence =~ s/\s//g; # Remove whitespace
Branch: 1 branch
BranchTrueFalseTotal
defined $sequence and $sequence =~ s/\s//g 2 0 2
156  
157 # for empty sequences we need to know the mol.type
158 2 $alphabet = $self->alphabet();
159 2 ▶ if(defined $sequence && length($sequence) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sequence and length $sequence == 0) { } 0 2 2
Condition33%1/3
defined $sequence and length $sequence == 0
!ll&&!rl&&r
020
MC/DC0%0/2
defined $sequence and length $sequence == 0
defined $sequence length $sequence == 0
Truth table33%1/3
A and B
A= defined $sequenceB= length $sequence == 0
ABresult
0X0
100
111
160 0 ▶ if(! defined($alphabet)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $alphabet) 0 0 0
161 # let's default to dna
162 0 $alphabet = "dna";
163 }
164 } else {
165 # we don't need it really, so disable
166 2 $alphabet = undef;
167 }
168  
169 2 $seq = $self->sequence_factory->create(
170 -seq => $sequence,
171 -id => $id,
172 # Ewan's note - I don't think this healthy
173 # but obviously to taste.
174 #-primary_id => $id,
175 -desc => $fulldesc,
176 -alphabet => $alphabet,
177 -direct => 1,
178 );
179  
180 2 $seq = $seq->primary_seq;
181 2 bless $seq, 'Bio::Seq::Meta';
182  
183 2 foreach my $meta (@metas) {
184 10 my ($name,$string) = split /\n/, $meta;
185 # $split ||= '';
186 10 $string =~ s/\n//g; # Remove newlines, spaces are important
187 10 $seq->named_meta($name, $string);
188 }
189  
190 # if there wasn't one before, set the guessed type
191 2 ▶ unless ( defined $alphabet ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $alphabet) 2 0 2
192 2 $self->alphabet($seq->alphabet());
193 }
194 2 return $seq;
195 }
196  
197 =head2 write_seq
198  
199 Title : write_seq
200 Usage : $stream->write_seq(@seq)
201 Function: writes the $seq object into the stream
202 Returns : 1 for success and 0 for error
203 Args : array of 1 to n Bio::PrimarySeqI objects
204  
205 =cut
206  
207 1 ▶sub write_seq {
Subroutine write_seq: called CC=13 SCAR=31.2
Pod documented
208 1 my ($self,@seq) = @_;
209 1 my $width = $self->width;
210 1 foreach my $seq (@seq) {
211 1 ▶ $self->throw("Did not provide a valid Bio::PrimarySeqI object")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seq and ref $seq and $seq->isa("Bio::PrimarySeqI") 0 1 1
Condition33%2/6
defined $seq and ref $seq and $seq->isa("Bio::PrimarySeqI")
!ll&&!rl&&r
001
defined $seq and ref $seq
!ll&&!rl&&r
001
MC/DC0%0/3
defined $seq and ref $seq and $seq->isa("Bio::PrimarySeqI")
defined $seq ref $seq $seq->isa("Bio::PrimarySeqI")
Truth table25%1/4
A and B and C
A= defined $seqB= ref $seqC= $seq->isa("Bio::PrimarySeqI")
ABCresult
0XX0
10X0
1100
1111
212 unless defined $seq && ref($seq) && $seq->isa('Bio::PrimarySeqI');
213  
214 1 my $str = $seq->seq;
215 1 my $top = $seq->display_id();
216 1 ▶ if ($seq->can('desc') and my $desc = $seq->desc()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq->can("desc") and my $desc = $seq->desc) 0 1 1
Condition33%1/3
$seq->can("desc") and my $desc = $seq->desc
!ll&&!rl&&r
010
MC/DC0%0/2
$seq->can("desc") and my $desc = $seq->desc
$seq->can("desc") my $desc = $seq->desc
Truth table33%1/3
A and B
A= $seq->can("desc")B= my $desc = $seq->desc
ABresult
0X0
100
111
217 0 $desc =~ s/\n//g;
218 0 $top .= " $desc";
219 }
220 1 ▶ if(length($str) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str > 0) { } 1 0 1
221 1 $str =~ s/(.{1,$width})/$1\n/g;
222 } else {
223 0 $str = "\n";
224 }
225 1 ▶ $self->_print (">",$top,"\n",$str) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_print(">", $top, "\n", $str) or return 1 0 1
226 1 ▶ if ($seq->isa('Bio::Seq::MetaI')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq->isa("Bio::Seq::MetaI")) 1 0 1
227 1 foreach my $meta ($seq->meta_names) {
228 5 my $str = $seq->named_meta($meta);
229 5 $str =~ s/(.{1,$width})/$1\n/g;
230 5 $self->_print ("&",$meta,"\n",$str);
231 }
232 }
233 }
234  
235 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
236 1 return 1;
237 }
238  
239 =head2 width
240  
241 Title : width
242 Usage : $obj->width($newval)
243 Function: Get/Set the line width for METAFASTA output
244 Returns : value of width
245 Args : newvalue (optional)
246  
247  
248 =cut
249  
250 1 ▶sub width{
Subroutine width: called CC=3 SCAR=13.3
Pod documented
251 1 my ($self,$value) = @_;
252 1 ▶ if( defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 1 1
253 0 $self->{'width'} = $value;
254 }
255 1 ▶ return $self->{'width'} || $WIDTH;
Condition33%1/3
$self->{'width'} || $Bio::SeqIO::metafasta::WIDTH
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{'width'} || $Bio::SeqIO::metafasta::WIDTH
$self->{'width'} $Bio::SeqIO::metafasta::WIDTH
Truth table33%1/3
A || B
A= $self->{'width'}B= $Bio::SeqIO::metafasta::WIDTH
ABresult
1X1
011
000
256 }
257  
258 3 1;
« Bio/DB/Failover.pm ↑ Summary Bio/Tools/Glimmer.pm »