SeqReader/Fasta.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 56 CC 5 SCAR 23.9
CC
5
Cov
0%
next
20.0
new
6.0
BEGIN
2.0
CRAP 10.9
1 #! /usr/bin/perl
2  
3 package Bio::SeqReader::Fasta;
4  
5 =head1 NAME
6  
7 Bio::SeqReader::Fasta - Class providing a reader for files in FASTA format.
8  
9 =head1 SYNOPSIS
10  
11 use Bio::SeqReader::Fasta;
12 my $in1 = new Bio::SeqReader::Fasta(); # from stdin
13 my $fh = ...
14 my $in2 = new Bio::SeqReader::Fasta( fh => $fh ); # from filehandle
15  
16 =head1 DESCRIPTION
17  
18 Bio::SeqReader::Fasta provides a method for reading a file or stream in FASTA format.
19  
20 =head1 CLASS METHODS
21  
22 Bio::SeqReader::Fasta provides no class methods.
23  
24 =head1 INSTANCE METHODS
25  
26 Bio::SeqReader::Fasta provides the following instance methods.
27  
28 =cut
29  
30 use strict;
31 use IO::Handle;
32 use Bio::SeqReader::FastaRecord;
33  
34 =over 12
35  
36 =item B<new()>
37  
38 Returns a new Bio::SeqReader::Fasta object associated with stdin or with a filehandle.
39  
40 # From an IO::File filehandle
41 my $fh1 = new IO::File( 'in.fq' );
42 my $in1 = new Bio::SeqReader::Fasta( fh => $fh1);
43  
44 # From an IO::Uncompress::AnyUncompress filehandle
45 my $fh2 = new IO::File( 'in.fq.gz' );
46 my $in2 = new Bio::SeqReader::Fasta( fh => $fh2);
47  
48 # From stdin
49 my $in3 = new Bio::SeqReader::Fasta();
50  
51 A specified filehandle must be compatible with those produced by IO::File filehandle; for example,
52  
53 $fh1 = new IO::File( 'in.fasta' )
54 $fh2 = new IO::Uncompress::AnyUncompress( 'in.fasta.gz' )
55 $fh3 = new IO::Uncompress::AnyUncompress( 'in.fasta' ).
56  
57 =back
58  
59 =cut
60  
61 sub new {
62 my ( $class, %parms ) = @_;
63  
64 my $self = {};
65 bless( $self, $class );
66  
67 if ( exists $parms{ fh } ) {
68 $self->{ _FH } = $parms{ fh };
69 }
70  
71 else {
72 $self->{ _FH } = \*STDIN;
73 }
74  
75 $self->{ _READER_STATE } = 'start';
76  
77 return $self;
78 }
79  
80 =over 12
81  
82 =item B<next()>
83  
84 Returns the next sequence as a Bio::SeqReader::FastaRecord object.
85  
86 while ( my $so = $in->next() ) {
87 ... work with $so here ...
88 }
89  
90 =back
91  
92 =cut
93  
94 sub next {
95 my $self = shift;
96  
97 # undef $self->{ _CURRENT_HEADER };
98  
99 my $seq; # undefined to support the "while (my $so = $in->next_seq())" idiom
100  
101 my $seqtext;
102  
103 while ( my $line = $self->{ _FH }->getline() ) {
104  
105 if ( $line =~ /^\s*[>;]/ ) {
106  
107  
108 $seq = new Bio::SeqReader::FastaRecord();
109  
110 $seq->seq( $seqtext );
111  
112 $self->{ _CURRENT_HEADER } =~ s/^\s*[>;]\s*//;
113  
114 my ( $id, $description ) = split( /\s+/, $self->{ _CURRENT_HEADER }, 2 );
115  
116 $seq->display_id( $id );
117 $seq->desc( $description );
118  
119 $self->{ _CURRENT_HEADER } = $line;
120  
121 next if $seqtext =~ /^\s*$/;
122 return $seq;
123 }
124  
125 else {
126 $seqtext .= $line;
127 }
128 }
129  
130 if ( $seqtext !~ /^\s*$/ ) {
131 $seq = new Bio::SeqReader::FastaRecord();
132  
133 $seq->seq( $seqtext );
134  
135 $self->{ _CURRENT_HEADER } =~ s/^\s*[>;]\s*//;
136  
137 my ( $id, $description ) = split( /\s+/, $self->{ _CURRENT_HEADER }, 2 );
138 $seq->display_id( $id );
139 $seq->desc( $description );
140 }
141  
142 return $seq;
143 }
144  
145 1;
146  
147 =head1 EXTERNAL DEPENDENCIES
148  
149 Perl core.
150  
151 =head1 EXAMPLES
152  
153 # Open and read a file in FASTA format
154 my $fh = new IO::File( 'foo.fasta' );
155 my $in = new Bio::SeqReader( fh => $fh );
156 while ( my $so = $in->next() ) {
157 my $s = $so->seq(); # $so is a Bio::SeqReader::FastaRecord
158 . . .
159 }
160  
161 # Open and read a gzipped file in FASTA format
162 my $fh = new IO::Uncompress::AnyUncompress( 'foo.fasta.gz' );
163 my $in = new Bio::SeqReader( fh => $fh );
164 while ( my $so = $in->next() ) {
165 my $s = $so->seq(); # $so is a Bio::SeqReader::FastaRecord
166 . . .
167 }
168  
169 =head1 BUGS
170  
171 None reported yet, but let me know.
172  
173 =head1 SEE ALSO
174  
175 Bio::SeqReader::FastaRecord, Bio::SeqReader.
176  
177 =head1 AUTHOR
178  
179 John A. Crow E<lt>jac_at_cpan_dot_orgE<gt>
180  
181 =head1 COPYRIGHT AND LICENSE
182  
183 This library is free software; you can redistribute it and/or modify
184 it under the same terms as Perl itself, either Perl version 5.10.0 or,
185 at your option, any later version of Perl 5 you may have available.
186  
187 Copyright (C) 2012 by John A. Crow
188 Copyright (C) 2012 by National Center for Genome Resources
189  
190 =cut
191