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