Phylo/Parsers/Abstract.pm

filter: Statementstmt 66.6% 84 / 126 Branchbran 55.0% 33 / 60 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 88.8% 24 / 27 Podpod n/a totaltotal 62.0% 144 / 232 CC 38 SCAR 26.0
CC
38
Cov
61%
_guess_format
44.6
_open_url
30.0
_process
8.8
CRAP 13.4
« Phylo/Unparsers/Taxlist.pm ↑ Summary Phylo/Project.pm »
1 package Bio::Phylo::Parsers::Abstract;
2 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use base 'Bio::Phylo::IO';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use Bio::Phylo::Util::Exceptions 'throw';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use Bio::Phylo::Util::CONSTANT '/looks_like/';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use Bio::Phylo::Util::Logger ':simple';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use Bio::Phylo::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Bio::Phylo::Parsers::Abstract - Superclass for parsers used by Bio::Phylo::IO
14  
15 =head1 DESCRIPTION
16  
17 This package is subclassed by all other packages within Bio::Phylo::Parsers::.*.
18 There is no direct usage.
19  
20 =cut
21  
22 33 my $factory = Bio::Phylo::Factory->new;
23 33 my $logger = Bio::Phylo::Util::Logger->new;
24  
25 # argument is a file name, which we open
26 1 ▶sub _open_file {
Subroutine _open_file: called CC=3 SCAR=12.5
27 1 my $file_name = shift;
28 1 ▶ my $encoding = shift || '';
Condition50%1/2
shift() || ''
l!l
01
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
29 1 ▶ open my $handle, "<${encoding}", $file_name or throw 'FileError' => $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<$encoding", $file_name or &throw("FileError", $!) 0 1 1
30 0 return $handle;
31 }
32  
33 # argument is a string, which, at perl version >5.8,
34 # we can treat as a handle by opening it by reference
35 108 ▶sub _open_string {
Subroutine _open_string: called CC=3 SCAR=11.4
36 108 my $string_value = shift;
37 108 ▶ my $encoding = shift || '';
Condition50%1/2
shift() || ''
l!l
0108
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
38 108 ▶ open my $handle, "<${encoding}", \$string_value or throw 'FileError' => $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<$encoding", \$string_value or &throw("FileError", $!) 108 0 108
39 108 return $handle;
40 }
41  
42 # argument is a url,
43 0 ▶sub _open_url {
Subroutine _open_url: not called CC=5 SCAR=34.0
44 0 my $url = shift;
45 0 ▶ my $encoding = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
46 0 my $handle;
47  
48 # we need to use LWP::UserAgent to fetch the resource, but
49 # we don't "use" it at the top of the module because that
50 # would make it a required dependency
51 0 eval { require LWP::UserAgent };
52 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
53 0 throw 'ExtensionError' =>
54 "Providing a -url argument requires\nsuccesful loading "
55 . "of LWP::UserAgent.\nHowever, there was an error when "
56 . "I\ntried that:\n"
57 . $@;
58 }
59  
60 # apparently it's installed, so let's instantiate a client
61 0 my $ua = LWP::UserAgent->new;
62 0 $ua->timeout(10);
63 0 $ua->env_proxy;
64  
65 # fetch the resource, get an HTTP::Response object
66 0 my $response = $ua->get($url);
67  
68 # i.e. 200, or 304 (unchanged cache)
69 0 ▶ if ( $response->is_success or $response->status_line =~ /304/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success or $response->status_line =~ /304/) { } 0 0 0
Condition0%0/3
$response->is_success or $response->status_line =~ /304/
l!l&&r!l&&!r
000
MC/DC0%0/2
$response->is_success or $response->status_line =~ /304/
$response->is_success $response->status_line =~ /304/
Truth table0%0/3
A or B
A= $response->is_successB= $response->status_line =~ /304/
ABresult
1X1
011
000
70  
71 # content is a string, so we create a handle in the same way
72 # as when the argument was a string
73 0 $handle = _open_string( $response->content, $encoding );
74 }
75 else {
76 0 throw 'NetworkError' => $response->status_line;
77 }
78 0 return $handle;
79 }
80  
81 # deal with all possible data sources, return
82 # a handle to whatever it is or throw an exception
83 116 ▶sub _open_handle {
Subroutine _open_handle: called CC=5 SCAR=17.1
84 116 my %args = @_;
85 116 my $handle;
86 116 ▶ if ( $args{'-handle'} ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($args{'-handle'}) { } 7 109 116
elsif ($args{'-file'}) { } 1 108 109
elsif ($args{'-string'}) { } 108 0 108
elsif ($args{'-url'}) { } 0 0 0
87 7 binmode $args{'-handle'}, ":utf8";
88 7 $handle = $args{'-handle'};
89 }
90 elsif ( $args{'-file'} ) {
91 1 $handle = _open_file( $args{'-file'}, $args{'-encoding'} );
92 }
93 elsif ( $args{'-string'} ) {
94 108 $handle = _open_string( $args{'-string'}, $args{'-encoding'} );
95 }
96 elsif ( $args{'-url'} ) {
97 0 $handle = _open_url( $args{'-url'}, $args{'-encoding'} );
98 }
99 else {
100 0 throw 'BadArgs' => 'No data source provided!';
101 }
102
103 # check to see if the data source contains anything
104 #if ( eof $handle ) {
105 # throw 'NoData' => "Source is empty!";
106 #}
107 115 return $handle;
108 }
109  
110 # open a Bio::Phylo::Project if asked (if the -as_project flag
111 # was provided.) If the user has supplied one (the -project flag)
112 # simply return that or undefined otherwise.
113 113 ▶sub _open_project {
Subroutine _open_project: called CC=3 SCAR=11.3
114 113 my ( $fac, %args ) = @_;
115 113 ▶ if ( $args{'-project'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'-project'}) { } 0 113 113
elsif ($args{'-as_project'}) { } 24 89 113
116 0 return $args{'-project'};
117 }
118 elsif ( $args{'-as_project'} ) {
119 24 return $fac->create_project;
120 }
121 else {
122 89 return undef;
123 }
124 }
125  
126 # this constructor is called by the Bio::Phylo::IO::parse
127 # subroutine
128 116 ▶sub _new {
Subroutine _new: called CC=4 SCAR=13.9
129 116 my $class = shift;
130 116 my %args = looks_like_hash @_;
131
132 # we need to guess the format
133 116 ▶ if ( $class eq __PACKAGE__ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq __PACKAGE__) 2 114 116
134 2 ▶ if ( my $format = _guess_format(_open_handle(%args)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $format = _guess_format(_open_handle(%args))) { } 1 1 2
135 1 $class = 'Bio::Phylo::Parsers::' . ucfirst($format);
136 1 return looks_like_class($class)->_new(%args);
137 }
138 else {
139 1 throw 'BadArgs' => "No format specified and unable to guess!";
140 }
141 }
142  
143 # factory is either user supplied or a private static
144 114 ▶ my $fac = $args{'-factory'} || $factory;
Condition33%1/3
$args{'-factory'} || $factory
l!l&&r!l&&!r
01140
MC/DC0%0/2
$args{'-factory'} || $factory
$args{'-factory'} $factory
Truth table33%1/3
A || B
A= $args{'-factory'}B= $factory
ABresult
1X1
011
000
145  
146 # values of these object fields will be accessed
147 # by child classes through the appropriate protected
148 # getters
149 return bless {
150 '_fac' => $fac,
151 '_handle' => _open_handle(%args),
152 '_proj' => _open_project( $fac, %args ),
153 '_args' => \%args, # for child-specific arguments
154 '_encoding' => $args{'-encoding'},
155 '_handlers' => $args{'-handlers'},
156 114 '_flush' => $args{'-flush'},
157 }, $class;
158 }
159  
160 # child classes can override this to specify
161 # that their return value is a single scalar
162 # (e.g. a tree block, as is the case for newick),
163 # instead of an array of blocks
164 29 ▶sub _return_is_scalar { 0 }
Subroutine _return_is_scalar: called CC=1 SCAR=0.0
165  
166 # this is called by Bio::Phylo::IO::parse, and
167 # in turn it calls the _parse method of whatever
168 # the concrete child instance is.
169 113 ▶sub _process {
Subroutine _process: called CC=8 SCAR=21.7
170 113 my $self = shift;
171 113 ▶ if ( $self->_return_is_scalar ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_return_is_scalar) { } 84 29 113
172 84 my $result = $self->_parse;
173 84 ▶ if ( my $p = $self->_project ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $self->_project) { } 10 74 84
174 10 ▶ if ( my $meta = $self->_project_meta ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $meta = $self->_project_meta) 0 10 10
175 0 $p->add_meta($_) for @{ $meta };
176 }
177 10 return $p->insert($result);
178 }
179 else {
180 74 return $result;
181 }
182 }
183 else {
184 29 my @result = $self->_parse;
185 27 ▶ if ( my $p = $self->_project ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $self->_project) { } 14 13 27
186 14 ▶ if ( my $meta = $self->_project_meta ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $meta = $self->_project_meta) 0 14 14
187 0 $p->add_meta($_) for @{ $meta };
188 }
189 14 return $p->insert(@result);
190 }
191 else {
192 13 return [@result];
193 }
194 }
195 }
196  
197 # once this is called, the handle will have read to
198 # the end of the stream, so it needs to be rewound
199 # if we want to read from the top
200 0 ▶sub _string {
Subroutine _string: not called CC=1 SCAR=6.9
201 0 my $self = shift;
202 0 my $handle = $self->_handle;
203 0 my $string = do { local $/; <$handle> };
204 0 return $string;
205 }
206 24 ▶sub _project_meta {};
Subroutine _project_meta: called CC=1 SCAR=0.0
207 160354 ▶sub _logger { $logger }
Subroutine _logger: called CC=1 SCAR=0.0
208 111 ▶sub _project { shift->{'_proj'} }
Subroutine _project: called CC=1 SCAR=0.0
209 113 ▶sub _handle { shift->{'_handle'} }
Subroutine _handle: called CC=1 SCAR=0.0
210 5391 ▶sub _factory { shift->{'_fac'} }
Subroutine _factory: called CC=1 SCAR=0.0
211 19146 ▶sub _args { shift->{'_args'} }
Subroutine _args: called CC=1 SCAR=0.0
212 0 ▶sub _encoding { shift->{'_encoding'} }
Subroutine _encoding: not called CC=1 SCAR=6.9
213 586 ▶sub _flush { shift->{'_flush'} }
Subroutine _flush: called CC=1 SCAR=0.0
214 584 ▶sub _handlers {
Subroutine _handlers: called CC=3 SCAR=11.7
215 584 my ( $self, $type ) = @_;
216 584 ▶ if ( my $h = $self->{'_handlers'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = $self->{"_handlers"}) 584 0 584
217 584 ▶ return defined $type ? $h->{$type} : $h;
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 584 0 584
218 }
219 }
220  
221 2 ▶sub _guess_format {
Subroutine _guess_format: called CC=12 SCAR=38.0
222 2 my $handle = shift;
223 2 my $line = $handle->getline;
224 2 my $format;
225 2 ▶ if ( $line =~ /^#nexus/i ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($line =~ /^#nexus/i) { } 1 1 2
elsif ($line =~ /^<[^>]*nexml/) { } 0 1 1
elsif ($line =~ /^<[^>]*phyloxml/) { } 0 1 1
elsif ($line =~ /^\s*\d+\s+\d+\s*$/) { } 0 1 1
elsif ($line =~ /^>/) { } 0 1 1
elsif ($line =~ /^\@/) { } 0 1 1
elsif ($line =~ /^\s*\(/) { } 0 1 1
elsif ($line =~ /<\? xml/) { } 0 1 1
226 1 $format = 'nexus';
227 }
228 elsif ( $line =~ /^<[^>]*nexml/ ) {
229 0 $format = 'nexml';
230 }
231 elsif ( $line =~ /^<[^>]*phyloxml/ ) {
232 0 $format = 'phyloxml';
233 }
234 elsif ( $line =~ /^\s*\d+\s+\d+\s*$/ ) {
235 0 $format = 'phylip';
236 }
237 elsif ( $line =~ /^>/ ) {
238 0 $format = 'fasta';
239 }
240 elsif ( $line =~ /^\@/ ) {
241 0 $format = 'fastq';
242 }
243 elsif ( $line =~ /^\s*\(/ ) {
244 0 $format = 'newick';
245 0 ▶ if ( $line =~ /{/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /{/) 0 0 0
246 0 $format = 'figtree';
247 }
248 }
249 elsif ( $line =~ /<\? xml/ ) {
250 0 $line = $handle;
251 0 ▶ if ( $line =~ /^<[^>]*nexml/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^<[^>]*nexml/) { } 0 0 0
elsif ($line =~ /^<[^>]*phyloxml/) { } 0 0 0
252 0 $format = 'nexml';
253 }
254 elsif ( $line =~ /^<[^>]*phyloxml/ ) {
255 0 $format = 'phyloxml';
256 }
257 }
258 2 seek( $handle, 0, 0 );
259 2 return $format;
260 }
261  
262 # podinherit_insert_token
263  
264 =head1 SEE ALSO
265  
266 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
267 for any user or developer questions and discussions.
268  
269 =over
270  
271 =item L<Bio::Phylo::IO>
272  
273 The parsers are called by the L<Bio::Phylo::IO> object.
274 Look there for examples.
275  
276 =item L<Bio::Phylo::Manual>
277  
278 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
279  
280 =back
281  
282 =head1 CITATION
283  
284 If you use Bio::Phylo in published research, please cite it:
285  
286 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
287 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
288 I<BMC Bioinformatics> B<12>:63.
289 L<http://dx.doi.org/10.1186/1471-2105-12-63>
290  
291 =cut
292  
293 33 1;
« Phylo/Unparsers/Taxlist.pm ↑ Summary Phylo/Project.pm »