Nexml/Factory.pm

filter: Statementstmt 12.0% 39 / 323 Branchbran 0.7% 1 / 140 Conditioncond 0.0% 0 / 66 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 50.0% 13 / 26 Podpod 90.9% 10 / 11 totaltotal 10.4% 63 / 602 CC 109 SCAR 52.8
CC
109
Cov
8%
create_bperl_tree
420.0
create_bphylo_datum
380.0
create_bperl_seq
306.0
CRAP 197.1
↑ Summary NexmlIO.pm »
1 # $Id: Util.pm 15875 2009-07-21 19:20:00Z chmille4 $
2 #
3 # BioPerl module for Bio::Nexml::Factory
4 #
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 #
7 # Cared for by Chase Miller <chmille4@gmail.com>
8 #
9 # Copyright Chase Miller
10 #
11 # You may distribute this module under the same terms as perl itself
12  
13 # POD documentation - main docs before the code
14  
15 =head1 NAME
16  
17 Bio::Nexml::Factory - A factory module for creating BioPerl and Bio::Phylo objects from/to nexml documents
18  
19 =head1 SYNOPSIS
20  
21 Do not use this module directly. It shoulde be used through
22 Bio::NexmlIO, Bio::SeqIO::nexml, Bio::AlignIO::nexml, or
23 Bio::TreeIO::nexml
24
25  
26 =head1 DESCRIPTION
27  
28 This is a factory/utility module in the Nexml namespace. It contains
29 methods that are needed by multiple modules.
30  
31 This module handles the creation of BioPerl objects from Bio::Phylo
32 objects and vice versa, which is used to read and write nexml
33 documents to and from BioPerl objects.
34  
35 =head1 FEEDBACK
36  
37 =head2 Mailing Lists
38  
39 User feedback is an integral part of the evolution of this and other
40 Bioperl modules. Send your comments and suggestions preferably to
41 the Bioperl mailing list. Your participation is much appreciated.
42  
43 bioperl-l@bioperl.org - General discussion
44 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
45  
46 =head2 Support
47  
48 Please direct usage questions or support issues to the mailing list:
49  
50 I<bioperl-l@bioperl.org>
51  
52 rather than to the module maintainer directly. Many experienced and
53 reponsive experts will be able look at the problem and quickly
54 address it. Please include a thorough description of the problem
55 with code and data examples if at all possible.
56  
57 =head2 Reporting Bugs
58  
59 Report bugs to the Bioperl bug tracking system to help us keep track
60 of the bugs and their resolution. Bug reports can be submitted via
61 the web:
62  
63 https://github.com/bioperl/bioperl-live/issues
64  
65 =head1 AUTHOR - Chase Miller
66  
67 Email chmille4@gmail.com
68  
69 =head1 APPENDIX
70  
71 The rest of the documentation details each of the object methods.
72 Internal methods are usually preceded with a _
73  
74 =cut
75  
76  
77 #Let the code begin
78  
79 package Bio::Nexml::Factory;
80 8 $Bio::Nexml::Factory::VERSION = '1.7.3';
81  
82 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 8 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.4
85 8 ▶ use Bio::Root::Root;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 8 ▶ unless (eval "require Bio::Phylo; 1") {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "require Bio::Phylo; 1") 0 8 8
87 0 Bio::Root::Root->throw("Bio::Phylo package required; see http://www.nexml.org for download details");
88 }
89 }
90
91 8 ▶use Bio::Phylo::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 8 ▶use Bio::Phylo::Matrices;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 8 ▶use Bio::Phylo::Matrices::Matrix;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 8 ▶use Bio::Phylo::Matrices::Datum;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 8 ▶use Bio::Phylo::Forest::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 8 ▶use Bio::Phylo::Matrices;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 8 ▶use Bio::Phylo::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
98  
99 8 ▶use Bio::SeqFeature::Generic;
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101  
102 8 ▶use base qw(Bio::Root::Root);
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 8 my $fac = Bio::Phylo::Factory->new();
105  
106  
107 =head2 new
108  
109 Title : new
110 Usage : my $obj = Bio::Nexml::Factory->new();
111 Function: Builds a new L<Bio::Nexml::Factory> object
112 Returns : L<Bio::Nexml::Factory> object
113 Args : none
114  
115 =cut
116  
117 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
118 2 my($class,@args) = @_;
119 2 my $self = $class->SUPER::new(@args);
120 }
121  
122 #should all these creates be private methods?
123 # naah./maj
124  
125 =head2 create_bperl_aln
126  
127 Title : create_bperl_aln
128 Usage : my @alns = $factory->create_bperl_aln($objIO);
129 Function: Converts Bio::Phylo::Matrices::Matrix objects into L<Bio::SimpleAlign> objects
130 Returns : an array of L<Bio::SimpleAlign> objects
131 Args : Bio::NexmlIO, Bio::SeqIO, Bio::AlignIO, or Bio::TreeIO
132  
133 see [http://search.cpan.org/~rvosa/Bio-Phylo/lib/Bio/Phylo/Project.pm Bio::Phylo::Project]
134  
135 =cut
136  
137 0 ▶sub create_bperl_aln {
Subroutine create_bperl_aln: not called CC=13 SCAR=52.0
Pod documented
138 0 my ($self, $caller) = @_;
139 0 my ($start, $end, $seq, $desc);
140 0 my $matrices = $caller->doc->get_matrices();
141 0 my @alns;
142
143 0 foreach my $matrix (@$matrices)
144 {
145 #check if mol_type is something that makes sense to be an aln
146 0 my $mol_type = lc($matrix->get_type());
147 0 ▶ unless ($mol_type eq 'dna' || $mol_type eq 'rna' || $mol_type eq 'protein')
Branch: 1 branch
BranchTrueFalseTotal
unless ($mol_type eq "dna" or $mol_type eq "rna" or $mol_type eq "protein") 0 0 0
Condition0%0/6
$mol_type eq "dna" or $mol_type eq "rna" or $mol_type eq "protein"
l!l&&r!l&&!r
000
$mol_type eq "dna" or $mol_type eq "rna"
l!l&&r!l&&!r
000
MC/DC0%0/3
$mol_type eq "dna" or $mol_type eq "rna" or $mol_type eq "protein"
$mol_type eq "dna" $mol_type eq "rna" $mol_type eq "protein"
Truth table0%0/4
A or B or C
A= $mol_type eq "dna"B= $mol_type eq "rna"C= $mol_type eq "protein"
ABCresult
1XX1
01X1
0011
0000
148 {
149 0 next;
150 # something for the back-burner: BioPerl has objects
151 # to handle arbitrary genotypes; might be cool to
152 # be able to create something besides alignments
153 # here .../maj
154 }
155
156 #continue creating an aln
157 0 my $aln = Bio::SimpleAlign->new();
158 0 my $taxa = $matrix->get_taxa();
159
160 # TODO: should $caller->{_ID} always be defined?
161 # ATM, this is a Bio::AlignIO::nexml stream...
162 0 ▶ $aln->{_Nexml_ID} = $caller->{_ID}? $caller->{_ID} . $taxa->get_xml_id : $taxa->get_xml_id;
Branch: 1 branch
BranchTrueFalseTotal
$caller->{'_ID'} ? : 0 0 0
163
164 0 my $aln_feats = Bio::SeqFeature::Generic->new();
165 0 $aln_feats->add_tag_value('NexmlIO_ID', $caller->{_ID});
166 #check if there is a taxa associated with this alignment
167 0 ▶ if ($taxa) {
Branch: 1 branch
BranchTrueFalseTotal
if ($taxa) 0 0 0
168 0 $aln_feats->add_tag_value('taxa_id', $taxa->get_xml_id());
169 0 ▶ $aln_feats->add_tag_value('taxa_label', $taxa->get_name()) if $taxa->get_name();
Branch: 1 branch
BranchTrueFalseTotal
if $taxa->get_name 0 0 0
170
171 0 my $taxon = $taxa->first;
172 0 ▶ while ($taxon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($taxon) 0 0 0
173 0 $aln_feats->add_tag_value('taxon', $taxon->get_name);
174 0 $taxon = $taxa->next;
175 }
176 }
177 0 $aln->add_SeqFeature($aln_feats);
178
179 0 my $basename = $matrix->get_name();
180 0 $aln->id($basename);
181 0 my $seqNum = 0;
182 0 my$row = $matrix->first;
183 0 ▶ while ($row)
Branch: 1 branch
BranchTrueFalseTotal
if ($row) 0 0 0
184 {
185 0 my $newSeq = $row->get_char();
186 0 my $rowlabel;
187 0 $seqNum++;
188
189 #constuct seqID based on matrix label and row id
190 0 my $seqID = "$basename.row_$seqNum";
191
192 #Check if theres a row label and if not default to seqID
193 0 ▶ if( !defined($rowlabel = $row->get_name())) {$rowlabel = $seqID;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($rowlabel = $row->get_name)) 0 0 0
194  
195 0 $seq = Bio::LocatableSeq->new(
196 -seq => $newSeq,
197 -display_id => "$rowlabel",
198 #-description => $desc,
199 -alphabet => $mol_type,
200 );
201 0 my $seq_feats;
202 #check if there is a taxa associated w/ this alignment
203 0 ▶ if($taxa)
Branch: 1 branch
BranchTrueFalseTotal
if ($taxa) 0 0 0
204 {
205 0 ▶ if (my $taxon = $taxa->get_by_name($row->get_taxon->get_name())) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $taxon = $taxa->get_by_name($row->get_taxon->get_name)) 0 0 0
206 #attach taxon to each sequence by using the sequenceID because
207 #LocatableSeq does not support features
208 0 my $taxon_name = $taxon->get_name();
209 0 $seq_feats = Bio::SeqFeature::Generic->new();
210 0 $seq_feats->add_tag_value('taxon', "$taxon_name");
211 0 $seq_feats->add_tag_value('id', "$rowlabel");
212 }
213 }
214 0 $aln->add_seq($seq);
215 0 $aln->add_SeqFeature($seq_feats);
216 0 $self->debug("Reading r$rowlabel\n");
217
218 0 $row = $matrix->next();
219 }
220 0 push (@alns, $aln);
221 }
222 0 return \@alns;
223 }
224  
225  
226 =head2 create_bperl_tree
227  
228 Title : create_bperl_tree
229 Usage : my @trees = $factory->create_bperl_seq($objIO);
230 Function: Converts Bio::Phylo::Forest::Tree objects into L<Bio::Tree::Tree> objects
231 Returns : an array of L<Bio::Tree::Tree> objects
232 Args : Bio::NexmlIO, Bio::SeqIO, Bio::AlignIO, or Bio::TreeIO
233  
234 see [http://search.cpan.org/~rvosa/Bio-Phylo/lib/Bio/Phylo/Project.pm Bio::Phylo::Project]
235  
236 =cut
237  
238 0 ▶sub create_bperl_tree {
Subroutine create_bperl_tree: not called CC=20 SCAR=60.4
Pod documented
239 0 my($self, $caller) = @_;
240 0 my @trees;
241
242 0 my $forests = $caller->doc->get_forests();
243
244 0 foreach my $forest (@$forests)
245 {
246 0 ▶ my $basename = $forest->get_name() || '';
Condition0%0/2
$forest->get_name || ''
l!l
00
MC/DC0%0/1
$forest->get_name || ''
$forest->get_name
Truth table0%0/2
A
A= $forest->get_name
Aresult
11
00
247 0 my $taxa = $forest->get_taxa();
248 0 my $taxa_label = $taxa->get_name();
249 0 my $taxa_id = $taxa->get_xml_id();
250
251 0 my $t = $forest->first();
252
253 0 ▶ while ($t)
Branch: 1 branch
BranchTrueFalseTotal
if ($t) 0 0 0
254 {
255 0 my %created_nodes;
256 0 my $tree_id = $t->get_name();
257 0 my $tree = Bio::Tree::Tree->new(-id => "$basename.$tree_id");
258  
259 #set the taxa info of the tree
260 0 ▶ $tree->add_tag_value('taxa_label', $taxa_label) if defined($taxa_label);
Branch: 1 branch
BranchTrueFalseTotal
if defined $taxa_label 0 0 0
261 0 ▶ $tree->add_tag_value('taxa_id', $taxa_id) if defined($taxa_id);
Branch: 1 branch
BranchTrueFalseTotal
if defined $taxa_id 0 0 0
262  
263 # TODO: should $caller->{_ID} always be defined?
264 # ATM, this is a Bio::TreeIO::nexml stream...
265 0 ▶ $tree->add_tag_value('_NexmlIO_ID', $caller->{_ID}) if $caller->{_ID};
Branch: 1 branch
BranchTrueFalseTotal
if $caller->{"_ID"} 0 0 0
266
267 0 my $taxon = $taxa->first;
268 0 ▶ while($taxon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($taxon) 0 0 0
269 0 ▶ $tree->add_tag_value('taxon', $taxon->get_name()) if defined($taxon->get_name);
Branch: 1 branch
BranchTrueFalseTotal
if defined $taxon->get_name 0 0 0
270 0 $taxon = $taxa->next;
271 }
272
273 #process terminals only, removing terminals as they get processed
274 #which inturn creates new terminals to process until the entire tree has been processed
275 0 my $terminals = $t->get_terminals();
276 # for(my $i=0; $i<@$terminals; $i++)
277 0 ▶ while (my $terminal = shift @$terminals)
Branch: 1 branch
BranchTrueFalseTotal
if (my $terminal = shift @$terminals) 0 0 0
278 {
279 # my $terminal = $$terminals[$i];
280 0 my $new_node_id = $terminal->get_name();
281 0 my $newNode;
282  
283 0 ▶ if(exists $created_nodes{$new_node_id})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $created_nodes{$new_node_id}) { } 0 0 0
284 {
285 0 $newNode = $created_nodes{$new_node_id};
286 }
287 else
288 {
289 0 $newNode = Bio::Tree::Node->new();
290 0 ▶ $new_node_id ||= 'internal_'.$newNode->_creation_id;
Condition0%0/2
$new_node_id ||= 'internal_' . $newNode->_creation_id
l!l
00
MC/DC0%0/1
$new_node_id ||= 'internal_' . $newNode->_creation_id
$new_node_id
Truth table0%0/2
A
A= $new_node_id
Aresult
11
00
291 0 $newNode->id($new_node_id);
292  
293 0 $created_nodes{$new_node_id} = $newNode;
294 }
295
296 #check if taxa data exists for the current node ($terminal)
297 0 ▶ if($taxa) {
Branch: 1 branch
BranchTrueFalseTotal
if ($taxa) 0 0 0
298 0 my $taxon = $terminal->get_taxon();
299 0 ▶ $newNode->add_tag_value("taxon", $taxon->get_name()) if $taxon && $taxon->get_name;
Branch: 1 branch
BranchTrueFalseTotal
if $taxon and $taxon->get_name 0 0 0
Condition0%0/3
$taxon and $taxon->get_name
!ll&&!rl&&r
000
MC/DC0%0/2
$taxon and $taxon->get_name
$taxon $taxon->get_name
Truth table0%0/3
A and B
A= $taxonB= $taxon->get_name
ABresult
0X0
100
111
300 }
301
302 #check if you've reached the root of the tree and if so, stop.
303 0 ▶ if($terminal->is_root()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($terminal->is_root) 0 0 0
304 0 $tree->set_root_node($newNode);
305 0 last;
306 }
307
308 #transfer attributes that apply to non-root only nodes
309 0 $newNode->branch_length($terminal->get_branch_length());
310
311 0 my $parent = $terminal->get_parent();
312 0 my $parentID = $parent->get_name();
313 0 ▶ if(exists $created_nodes{$parentID})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $created_nodes{$parentID}) { } 0 0 0
314 {
315  
316 0 $created_nodes{$parentID}->add_Descendent($newNode);
317 }
318 else
319 {
320 0 my $parent_node = Bio::Tree::Node->new();
321 0 ▶ $parentID ||= 'internal_'.$parent_node->_creation_id;
Condition0%0/2
$parentID ||= 'internal_' . $parent_node->_creation_id
l!l
00
MC/DC0%0/1
$parentID ||= 'internal_' . $parent_node->_creation_id
$parentID
Truth table0%0/2
A
A= $parentID
Aresult
11
00
322 0 $parent_node->id($parentID);
323 0 $parent_node->add_Descendent($newNode);
324 0 $created_nodes{$parentID} = $parent_node;
325 }
326 #remove processed node from tree
327 0 $parent->prune_child($terminal);
328
329 #check if the parent of the removed node is now a terminal node and should be added for processing
330 0 ▶ if($parent->is_terminal())
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->is_terminal) 0 0 0
331 {
332 0 ▶ push(@$terminals, $terminal->get_parent()) if $terminal->get_parent;
Branch: 1 branch
BranchTrueFalseTotal
if $terminal->get_parent 0 0 0
333 }
334 }
335 0 push @trees, $tree;
336 0 $t = $forest->next();
337 }
338 }
339 0 return \@trees;
340 }
341  
342 =head2 create_bperl_seq
343  
344 Title : create_bperl_seq
345 Usage : my @seqs = $factory->create_bperl_seq($objIO);
346 Function: Converts Bio::Phylo::Matrices::Datum objects into L<Bio::Seq> objects
347 Returns : an array of L<Bio::Seq> objects
348 Args : Bio::NexmlIO, Bio::SeqIO, Bio::AlignIO, or Bio::TreeIO
349  
350 see [http://search.cpan.org/~rvosa/Bio-Phylo/lib/Bio/Phylo/Project.pm Bio::Phylo::Project]
351  
352 =cut
353  
354 0 ▶sub create_bperl_seq {
Subroutine create_bperl_seq: not called CC=17 SCAR=57.2
Pod documented
355 0 my($self, $caller) = @_;
356 0 my $matrices = $caller->doc->get_matrices();
357 0 my @seqs;
358
359 0 foreach my $matrix (@$matrices)
360 {
361 #check if mol_type is something that makes sense to be a seq
362 0 my $mol_type = lc($matrix->get_type());
363 0 ▶ unless ($mol_type eq 'dna' || $mol_type eq 'rna' || $mol_type eq 'protein')
Branch: 1 branch
BranchTrueFalseTotal
unless ($mol_type eq "dna" or $mol_type eq "rna" or $mol_type eq "protein") 0 0 0
Condition0%0/6
$mol_type eq "dna" or $mol_type eq "rna" or $mol_type eq "protein"
l!l&&r!l&&!r
000
$mol_type eq "dna" or $mol_type eq "rna"
l!l&&r!l&&!r
000
MC/DC0%0/3
$mol_type eq "dna" or $mol_type eq "rna" or $mol_type eq "protein"
$mol_type eq "dna" $mol_type eq "rna" $mol_type eq "protein"
Truth table0%0/4
A or B or C
A= $mol_type eq "dna"B= $mol_type eq "rna"C= $mol_type eq "protein"
ABCresult
1XX1
01X1
0011
0000
364 {
365 0 next;
366 }
367
368 0 my $taxa = $matrix->get_taxa();
369 0 my $seqnum = 0;
370 0 my $taxa_id = $taxa->get_xml_id();
371 0 my $taxa_label = $taxa->get_name();
372 0 my $basename = $matrix->get_name();
373 0 my $row = $matrix->first;
374 0 ▶ while ($row)
Branch: 1 branch
BranchTrueFalseTotal
if ($row) 0 0 0
375 {
376 0 my $newSeq = $row->get_char();
377 0 my $feat = Bio::SeqFeature::Generic->new();
378 0 ▶ $feat->add_tag_value('matrix_label', $matrix->get_name()) if defined($matrix->get_name);
Branch: 1 branch
BranchTrueFalseTotal
if defined $matrix->get_name 0 0 0
379 0 $feat->add_tag_value('matrix_id', $matrix->get_xml_id());
380 0 $feat->add_tag_value('NexmlIO_ID', $caller->{_ID});
381 0 ▶ $feat->add_tag_value('taxa_id', $taxa_id) if defined($taxa_id);
Branch: 1 branch
BranchTrueFalseTotal
if defined $taxa_id 0 0 0
382 0 ▶ $feat->add_tag_value('taxa_label', $taxa_label) if defined($taxa_label);
Branch: 1 branch
BranchTrueFalseTotal
if defined $taxa_label 0 0 0
383
384 0 $seqnum++;
385 #construct full sequence id by using bio::phylo "matrix label" and "row id"
386 0 my $seqID = "$basename.seq_$seqnum";
387 0 my $rowlabel;
388 #check if there is a label for the row, if not default to seqID
389 0 ▶ if (!defined ($rowlabel = $row->get_name())) {$rowlabel = $seqID;}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined($rowlabel = $row->get_name)) { } 0 0 0
390 0 else {$seqID = $rowlabel;}
391
392 #build the seq object using the factory create method
393 0 my $seqbuilder = Bio::Seq::SeqFactory->new('-type' => 'Bio::Seq');
394 0 my $seq = $seqbuilder->create(
395 -seq => $newSeq,
396 -id => $rowlabel,
397 -primary_id => $seqID,
398 #-desc => $fulldesc,
399 -alphabet => $mol_type,
400 -direct => 1,
401 );
402 # TODO: should $caller->{_ID} always be defined?
403 # ATM, this is a Bio::SeqIO::nexml stream...
404 0 ▶ $seq->{_Nexml_ID} = $caller->{_ID} ? $caller->{_ID} . $taxa_id : $taxa_id;
Branch: 1 branch
BranchTrueFalseTotal
$caller->{'_ID'} ? : 0 0 0
405 0 ▶ $seq->{_Nexml_matrix_ID} = $caller->{_ID} ? $caller->{_ID} . $matrix->get_xml_id() : $matrix->get_xml_id();
Branch: 1 branch
BranchTrueFalseTotal
$caller->{'_ID'} ? : 0 0 0
406
407 #check if taxon linked to sequence if so create feature to attach to alignment
408 0 ▶ if ($taxa) {
Branch: 1 branch
BranchTrueFalseTotal
if ($taxa) 0 0 0
409 0 my $taxon = $taxa->first;
410 0 ▶ while ($taxon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($taxon) 0 0 0
411 0 ▶ $feat->add_tag_value('taxon', $taxon->get_name) if defined($taxon->get_name);
Branch: 1 branch
BranchTrueFalseTotal
if defined $taxon->get_name 0 0 0
412 0 ▶ if($taxon eq $row->get_taxon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($taxon eq $row->get_taxon) 0 0 0
413 0 my $taxon_name = $taxon->get_name();
414
415 0 ▶ $feat->add_tag_value('my_taxon', "$taxon_name") if defined($taxon_name);
Branch: 1 branch
BranchTrueFalseTotal
if defined $taxon_name 0 0 0
416 0 $feat->add_tag_value('id', $rowlabel);
417 }
418 0 $taxon = $taxa->next;
419 }
420 }
421 0 $seq->add_SeqFeature($feat);
422 0 push (@seqs, $seq);
423
424 0 $row = $matrix->next;
425 }
426 }
427 0 return \@seqs;
428 }
429  
430 =head2 create_bphylo_tree
431  
432 Title : create_bphylo_tree
433 Usage : my $bphylo_tree = $factory->create_bphylo_tree($bperl_tree);
434 Function: Converts a L<Bio::Tree::Tree> object into Bio::Phylo::Forest::Tree object
435 Returns : a Bio::Phylo::Forest::Tree object
436 Args : Bio::Tree::Tree object
437  
438 =cut
439  
440 0 ▶sub create_bphylo_tree {
Subroutine create_bphylo_tree: not called CC=5 SCAR=34.0
Pod documented
441 0 my ($self, $bptree, $taxa) = @_;
442 #most of the code below ripped form Bio::Phylo::Forest::Tree::new_from_bioperl()d
443
444 0 my $tree = $fac->create_tree;
445 0 my $class = 'Bio::Phylo::Forest::Tree';
446
447 0 ▶ if ( ref $bptree && $bptree->isa('Bio::Tree::TreeI') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $bptree and $bptree->isa('Bio::Tree::TreeI')) { } 0 0 0
Condition0%0/3
ref $bptree and $bptree->isa("Bio::Tree::TreeI")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $bptree and $bptree->isa("Bio::Tree::TreeI")
ref $bptree $bptree->isa("Bio::Tree::TreeI")
Truth table0%0/3
A and B
A= ref $bptreeB= $bptree->isa("Bio::Tree::TreeI")
ABresult
0X0
100
111
448 0 bless $tree, $class;
449 0 ($tree) = _copy_tree( $tree, $bptree->get_root_node, "", $taxa);
450
451 # copy name
452 0 my $name = $bptree->id;
453 0 ▶ $tree->set_name( $name ) if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 0 0 0
454
455 # copy score
456 0 my $score = $bptree->score;
457 0 ▶ $tree->set_score( $score ) if defined $score;
Branch: 1 branch
BranchTrueFalseTotal
if defined $score 0 0 0
458 }
459 else {
460 0 $self->throw('Not a bioperl tree!');
461 }
462 0 return $tree;
463 }
464  
465  
466 0 ▶sub _copy_tree {
Subroutine _copy_tree: not called CC=4 SCAR=30.0
467 0 my ( $tree, $bpnode, $parent, $taxa ) = @_;
468 0 my $node = create_bphylo_node($bpnode);
469 0 my $taxon;
470 0 ▶ if ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 0 0 0
471 0 $parent->set_child($node);
472 }
473 0 ▶ if (my $bptaxon_name = $bpnode->get_tag_values('taxon'))
Branch: 1 branch
BranchTrueFalseTotal
if (my $bptaxon_name = $bpnode->get_tag_values("taxon")) 0 0 0
474 {
475 0 $node->set_taxon($taxa->get_by_name($bptaxon_name));
476 }
477 0 $tree->insert($node);
478 0 foreach my $bpchild ( $bpnode->each_Descendent ) {
479 0 _copy_tree( $tree, $bpchild, $node, $taxa );
480 }
481 0 return $tree;
482 }
483  
484 =head2 create_bphylo_node
485  
486 Title : create_bphylo_node
487 Usage : my $bphylo_node = $factory->create_bphylo_node($bperl_node);
488 Function: Converts a L<Bio::Tree::Node> object into Bio::Phylo::Forest::Node object
489 Returns : a Bio::Phylo::Forest::Node object
490 Args : L<Bio::Tree::Node> object
491  
492 =cut
493  
494 0 ▶sub create_bphylo_node {
Subroutine create_bphylo_node: not called CC=7 SCAR=40.3
Pod documented
495 0 my ($bpnode) = @_;
496 0 my $node = Bio::Phylo::Forest::Node->new();
497
498 #mostly ripped from Bio::Phylo::Forest::Node->new_from_bioperl()
499 # copy name
500 0 my $name = $bpnode->id;
501 0 ▶ $node->set_name( $name ) if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 0 0 0
502
503 # copy branch length
504 0 my $branch_length = $bpnode->branch_length;
505 0 ▶ $node->set_branch_length( $branch_length ) if defined $branch_length;
Branch: 1 branch
BranchTrueFalseTotal
if defined $branch_length 0 0 0
506
507 # copy description
508 0 my $desc = $bpnode->description;
509 0 ▶ $node->set_desc( $desc ) if defined $desc;
Branch: 1 branch
BranchTrueFalseTotal
if defined $desc 0 0 0
510
511 # copy bootstrap
512 0 my $bootstrap = $bpnode->bootstrap;
513 0 ▶ $node->set_score( $bootstrap ) if defined $bootstrap and looks_like_number $bootstrap;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bootstrap and $bootstrap->looks_like_number 0 0 0
Condition0%0/3
defined $bootstrap and $bootstrap->looks_like_number
!ll&&!rl&&r
000
MC/DC0%0/2
defined $bootstrap and $bootstrap->looks_like_number
defined $bootstrap $bootstrap->looks_like_number
Truth table0%0/3
A and B
A= defined $bootstrapB= $bootstrap->looks_like_number
ABresult
0X0
100
111
514
515 # copy other tags
516 0 for my $tag ( $bpnode->get_all_tags ) {
517 0 my @values = $bpnode->get_tag_values( $tag );
518 0 $node->set_generic( $tag => \@values );
519 }
520 0 return $node;
521 }
522
523  
524 =head2 create_bphylo_aln
525  
526 Title : create_bphylo_aln
527 Usage : my $bphylo_aln = $factory->create_bphylo_aln($bperl_aln);
528 Function: Converts a L<Bio::SimpleAlign> object into Bio::Phylo::Matrices::Matrix object
529 Returns : a Bio::Phylo::Matrices::Matrix object
530 Args : Bio::SimpleAlign object
531  
532 =cut
533  
534 0 ▶sub create_bphylo_aln {
Subroutine create_bphylo_aln: not called CC=10 SCAR=47.0
Pod documented
535
536 0 my ($self, $aln, $taxa, @args) = @_;
537
538 #most of the code below ripped from Bio::Phylo::Matrices::Matrix::new_from_bioperl()
539 0 ▶ if ( $aln->isa('Bio::Align::AlignI') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aln->isa('Bio::Align::AlignI')) { } 0 0 0
540 0 $aln->unmatch;
541 0 $aln->map_chars('\.','-');
542 0 my @seqs = $aln->each_seq;
543 0 my ( $type, $missing, $gap, $matchchar );
544 0 ▶ if ( $seqs[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seqs[0]) { } 0 0 0
545 0 ▶ $type = $seqs[0]->alphabet || $seqs[0]->_guess_alphabet || 'dna';
Condition0%0/5
$seqs[0]->alphabet || $seqs[0]->_guess_alphabet || 'dna'
l!l
00
$seqs[0]->alphabet or $seqs[0]->_guess_alphabet
l!l&&r!l&&!r
000
MC/DC0%0/2
$seqs[0]->alphabet || $seqs[0]->_guess_alphabet || 'dna'
$seqs[0]->alphabet $seqs[0]->_guess_alphabet
Truth table0%0/3
A or B
A= $seqs[0]->alphabetB= $seqs[0]->_guess_alphabet
ABresult
1X1
011
000
546 }
547 else {
548 0 $type = 'dna';
549 }
550
551 0 ▶ my $matrix = $fac->create_matrix(
Condition0%0/6
$aln->missing_char || '?'
l!l
00
$aln->match_char || '.'
l!l
00
$aln->gap_char || '-'
l!l
00
MC/DC0%0/1
$aln->missing_char || '?'
$aln->missing_char
MC/DC0%0/1
$aln->match_char || '.'
$aln->match_char
MC/DC0%0/1
$aln->gap_char || '-'
$aln->gap_char
Truth table0%0/2
A
A= $aln->missing_char
Aresult
11
00
Truth table0%0/2
A
A= $aln->match_char
Aresult
11
00
Truth table0%0/2
A
A= $aln->gap_char
Aresult
11
00
552 '-type' => $type,
553 '-special_symbols' => {
554 '-missing' => $aln->missing_char || '?',
555 '-matchchar' => $aln->match_char || '.',
556 '-gap' => $aln->gap_char || '-',
557 },
558 @args
559 );
560 # XXX create raw getter/setter pairs for annotation, accession, consensus_meta source
561 0 for my $field ( qw(description accession id annotation consensus_meta score source) ) {
562 0 $matrix->$field( $aln->$field );
563 }
564 0 my $to = $matrix->get_type_object;
565 0 my @feats = $aln->get_all_SeqFeatures();
566
567 0 for my $seq ( @seqs ) {
568 #create datum linked to taxa
569 0 my $datum = create_bphylo_datum($seq, $taxa, \@feats, '-type_object' => $to);
570 0 $matrix->insert($datum);
571 }
572 0 return $matrix;
573 }
574 else {
575 0 $self->throw('Not a bioperl alignment!');
576 }
577 }
578  
579  
580  
581 =head2 create_bphylo_seq
582  
583 Title : create_bphylo_seq
584 Usage : my $bphylo_seq = $factory->create_bphylo_seq($bperl_seq);
585 Function: Converts a L<Bio::Seq> object into Bio::Phylo::Matrices::Matrix object
586 Returns : a Bio::Phylo::Matrices::Matrix object
587 Args : Bio::Seq object
588  
589 =cut
590  
591 0 ▶sub create_bphylo_seq {
Subroutine create_bphylo_seq: not called CC=10 SCAR=47.0
Pod documented
592 0 my ($self, $seq, $taxa, @args) = @_;
593 0 ▶ my $type = $seq->alphabet || $seq->_guess_alphabet || 'dna';
Condition0%0/5
$seq->alphabet || $seq->_guess_alphabet || 'dna'
l!l
00
$seq->alphabet or $seq->_guess_alphabet
l!l&&r!l&&!r
000
MC/DC0%0/2
$seq->alphabet || $seq->_guess_alphabet || 'dna'
$seq->alphabet $seq->_guess_alphabet
Truth table0%0/3
A or B
A= $seq->alphabetB= $seq->_guess_alphabet
ABresult
1X1
011
000
594 0 $type = uc($type);
595
596 0 my $dat = create_bphylo_datum($seq, $taxa, '-type' => $type);
597
598 # copy seq string
599 0 my $seqstring = $seq->seq;
600 0 ▶ if ( $seqstring and $seqstring =~ /\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seqstring and $seqstring =~ /\S/) 0 0 0
Condition0%0/3
$seqstring and $seqstring =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
$seqstring and $seqstring =~ /\S/
$seqstring $seqstring =~ /\S/
Truth table0%0/3
A and B
A= $seqstringB= $seqstring =~ /\S/
ABresult
0X0
100
111
601 0 eval { $dat->set_char( $seqstring ) };
602 0 ▶ if ( $@ and UNIVERSAL::isa($@,'Bio::Phylo::Util::Exceptions::InvalidData') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and UNIVERSAL::isa($@, "Bio::Phylo::Util::Exceptions::InvalidData")) 0 0 0
Condition0%0/3
$@ and UNIVERSAL::isa($@, "Bio::Phylo::Util::Exceptions::InvalidData")
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and UNIVERSAL::isa($@, "Bio::Phylo::Util::Exceptions::InvalidData")
$@ UNIVERSAL::isa($@, "Bio::Phylo::Util::Exceptions::InvalidData")
Truth table0%0/3
A and B
A= $@B= UNIVERSAL::isa($@, "Bio::Phylo::Util::Exceptions::InvalidData")
ABresult
0X0
100
111
603 0 $self->throw("\n\nThe BioPerl sequence object contains invalid data ($seqstring)\n");
604 }
605 }
606
607 # copy name
608 0 my $name = $seq->display_id;
609 #$dat->set_name( $name ) if defined $name;
610
611 # copy desc
612 0 my $desc = $seq->desc;
613 0 ▶ $dat->set_desc( $desc ) if defined $desc;
Branch: 1 branch
BranchTrueFalseTotal
if defined $desc 0 0 0
614
615 #get features from SeqFeatureI
616 0 for my $field ( qw(start end strand) ) {
617 0 ▶ $dat->$field( $seq->$field ) if $seq->can($field);
Branch: 1 branch
BranchTrueFalseTotal
if $seq->can($field) 0 0 0
618 }
619 0 return $dat;
620 }
621  
622 =head2 create_bphylo_taxa
623  
624 Title : create_bphylo_seq
625 Usage : my $taxa = $factory->create_bphylo_taxa($bperl_obj);
626 Function: creates a taxa object from the data attached to a bioperl object
627 Returns : a Bio::Phylo::Taxa object
628 Args : L<Bio::Seq> object, or L<Bio::SimpleAlign> object, or L<Bio::Tree::Tree> object
629  
630 =cut
631  
632 0 ▶sub create_bphylo_taxa {
Subroutine create_bphylo_taxa: not called CC=4 SCAR=30.0
Pod documented
633 0 my $self = shift @_;
634 0 my ($obj) = @_;
635
636 #check if tree or aln object
637 0 ▶ if ( UNIVERSAL::isa( $obj, 'Bio::Align::AlignI' ) || UNIVERSAL::isa( $obj, 'Bio::Seq')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($obj, 'Bio::Align::AlignI') or UNIVERSAL::isa($obj, 'Bio::Seq')) { } 0 0 0
elsif (UNIVERSAL::isa($obj, 'Bio::Tree::TreeI')) { } 0 0 0
Condition0%0/3
UNIVERSAL::isa($obj, "Bio::Align::AlignI") or UNIVERSAL::isa($obj, "Bio::Seq")
l!l&&r!l&&!r
000
MC/DC0%0/2
UNIVERSAL::isa($obj, "Bio::Align::AlignI") or UNIVERSAL::isa($obj, "Bio::Seq")
UNIVERSAL::isa($obj, "Bio::Align::AlignI") UNIVERSAL::isa($obj, "Bio::Seq")
Truth table0%0/3
A or B
A= UNIVERSAL::isa($obj, "Bio::Align::AlignI")B= UNIVERSAL::isa($obj, "Bio::Seq")
ABresult
1X1
011
000
638 0 return $self->_create_bphylo_matrix_taxa(@_);
639 }
640 elsif ( UNIVERSAL::isa( $obj, 'Bio::Tree::TreeI' ) ) {
641 0 return $self->_create_bphylo_tree_taxa(@_);
642 }
643 }
644  
645 0 ▶sub _create_bphylo_tree_taxa {
Subroutine _create_bphylo_tree_taxa: not called CC=3 SCAR=24.8
646 0 my ($self, $tree) = @_;
647
648 0 my $taxa = $fac->create_taxa();
649 0 my $taxon;
650
651 #check if taxa exists
652 0 ▶ unless ($tree->has_tag('taxa_id')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tree->has_tag("taxa_id")) 0 0 0
653 0 return 0;
654 }
655
656 #copy taxa details
657 0 $taxa->set_xml_id(($tree->get_tag_values('taxa_id'))[0]);
658 0 $taxa->set_name(($tree->get_tag_values('taxa_label'))[0]);
659
660 0 foreach my $taxon_name ($tree->get_tag_values('taxon')) {
661
662 0 $taxon = $fac->create_taxon(-name => $taxon_name);
663 0 $taxa->insert($taxon);
664 }
665 0 return $taxa;
666 }
667  
668 0 ▶sub _create_bphylo_matrix_taxa {
Subroutine _create_bphylo_matrix_taxa: not called CC=6 SCAR=37.4
669 0 my ($self, $aln) = @_;
670
671 0 my $taxa = $fac->create_taxa();
672 0 my $taxon;
673 0 my @feats = $aln->get_all_SeqFeatures();
674
675 0 foreach my $feat (@feats) {
676 0 ▶ if (my $taxa_id = ($feat->get_tag_values('taxa_id'))[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $taxa_id = ($feat->get_tag_values("taxa_id"))[0]) 0 0 0
677 0 my $taxa_label = ($feat->get_tag_values('taxa_label'))[0];
678
679 0 ▶ $taxa->set_name($taxa_label) if defined $taxa_label;
Branch: 1 branch
BranchTrueFalseTotal
if defined $taxa_label 0 0 0
680 0 ▶ $taxa->set_xml_id($taxa_id) if defined $taxa_label;
Branch: 1 branch
BranchTrueFalseTotal
if defined $taxa_label 0 0 0
681 0 my @taxa_bp = $feat->get_tag_values('taxon');
682 0 foreach my $taxon_name (@taxa_bp) {
683 0 $taxon = $fac->create_taxon(-name => $taxon_name);
684 0 $taxa->insert($taxon);
685 }
686 0 last;
687 }
688 }
689 0 return $taxa
690 }
691  
692 =head2 create_bphylo_datum
693  
694 Title : create_bphylo_datum
695 Usage : my $bphylo_datum = $factory->create_bphylo_datum($bperl_datum);
696 Function: Converts a L<Bio::Seq> object into Bio::Phylo::Matrices::datum object
697 Returns : a Bio::Phylo::Matrices::datum object
698 Args : Bio::Seq object, Bio::Phylo::Taxa object,
699 [optional] arrayref to SeqFeatures,
700 [optional] key => value pairs to pass to Bio::Phylo constructor
701  
702 =cut
703  
704 0 ▶sub create_bphylo_datum {
Subroutine create_bphylo_datum: not called CC=19 SCAR=59.4
Pod documented
705 #mostly ripped from Bio::Phylo::Matrices::Datum::new_from_bioperl()
706 0 my ( $seq, $taxa, @args ) = @_;
707 0 my $class = 'Bio::Phylo::Matrices::Datum';
708 0 my $feats;
709 # want $seq type-check here? Allowable: is-a Bio::PrimarySeq,
710 # Bio::LocatableSeq /maj
711 0 ▶ if (@args % 2) { # odd
Branch: 1 branch
BranchTrueFalseTotal
if (@args % 2) 0 0 0
712 0 $feats = shift @args;
713 0 ▶ unless (ref($feats) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $feats eq "ARRAY") 0 0 0
714 0 Bio::Root::Root->throw("Third argument must be array of SeqFeatures");
715 }
716 }
717 0 ▶ my $type = $seq->alphabet || $seq->_guess_alphabet || 'dna';
Condition0%0/5
$seq->alphabet || $seq->_guess_alphabet || 'dna'
l!l
00
$seq->alphabet or $seq->_guess_alphabet
l!l&&r!l&&!r
000
MC/DC0%0/2
$seq->alphabet || $seq->_guess_alphabet || 'dna'
$seq->alphabet $seq->_guess_alphabet
Truth table0%0/3
A or B
A= $seq->alphabetB= $seq->_guess_alphabet
ABresult
1X1
011
000
718 0 my $self = $class->new( '-type' => $type, @args );
719 # copy seq string
720 0 my $seqstring = $seq->seq;
721 0 ▶ if ( $seqstring and $seqstring =~ /\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seqstring and $seqstring =~ /\S/) 0 0 0
Condition0%0/3
$seqstring and $seqstring =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
$seqstring and $seqstring =~ /\S/
$seqstring $seqstring =~ /\S/
Truth table0%0/3
A and B
A= $seqstringB= $seqstring =~ /\S/
ABresult
0X0
100
111
722 0 eval { $self->set_char( $seqstring ) };
723 0 ▶ if ( $@ and UNIVERSAL::isa($@,'Bio::Phylo::Util::Exceptions::InvalidData') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and UNIVERSAL::isa($@, "Bio::Phylo::Util::Exceptions::InvalidData")) 0 0 0
Condition0%0/3
$@ and UNIVERSAL::isa($@, "Bio::Phylo::Util::Exceptions::InvalidData")
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and UNIVERSAL::isa($@, "Bio::Phylo::Util::Exceptions::InvalidData")
$@ UNIVERSAL::isa($@, "Bio::Phylo::Util::Exceptions::InvalidData")
Truth table0%0/3
A and B
A= $@B= UNIVERSAL::isa($@, "Bio::Phylo::Util::Exceptions::InvalidData")
ABresult
0X0
100
111
724 0 $self->throw("\n\nThe BioPerl sequence object contains invalid data ($seqstring)\n");
725 }
726 }
727
728 # copy name
729 0 my $name = $seq->display_id;
730 0 ▶ $self->set_name( $name ) if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 0 0 0
731 0 my $taxon;
732 0 ▶ my @feats = (defined $feats ? @$feats : $seq->get_all_SeqFeatures);
Branch: 1 branch
BranchTrueFalseTotal
defined $feats ? : 0 0 0
733 # convert taxa
734 0 foreach my $feat (@feats)
735 {
736 #get sequence id associated with taxa to compare
737 0 ▶ my $taxa_id = ($feat->get_tag_values('id'))[0] if $feat->has_tag('id');
Branch: 1 branch
BranchTrueFalseTotal
if $feat->has_tag("id") 0 0 0
738 0 ▶ if ($taxa_id && $name eq $taxa_id)
Branch: 1 branch
BranchTrueFalseTotal
if ($taxa_id and $name eq $taxa_id) 0 0 0
Condition0%0/3
$taxa_id and $name eq $taxa_id
!ll&&!rl&&r
000
MC/DC0%0/2
$taxa_id and $name eq $taxa_id
$taxa_id $name eq $taxa_id
Truth table0%0/3
A and B
A= $taxa_idB= $name eq $taxa_id
ABresult
0X0
100
111
739 {
740 0 my $taxon_name;
741 0 ▶ if($feat->has_tag('my_taxon')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($feat->has_tag('my_taxon')) { } 0 0 0
742 0 $taxon_name = ($feat->get_tag_values('my_taxon'))[0]
743 }
744 else {
745 0 $taxon_name = ($feat->get_tag_values('taxon'))[0];
746 }
747 0 $self->set_taxon($taxa->get_by_name($taxon_name));
748 }
749 }
750
751 # copy desc
752 0 my $desc = $seq->desc;
753 0 ▶ $self->set_desc( $desc ) if defined $desc;
Branch: 1 branch
BranchTrueFalseTotal
if defined $desc 0 0 0
754  
755 # only Bio::LocatableSeq objs have these fields...
756 0 for my $field ( qw(start end strand) ) {
757 0 ▶ $self->$field( $seq->$field ) if $seq->can($field);
Branch: 1 branch
BranchTrueFalseTotal
if $seq->can($field) 0 0 0
758 }
759 0 return $self;
760 }
761  
762 =head2 CREATOR
763  
764 =cut
765  
766 =head1 bioperl_create
767  
768 Title : bioperl_create
769 Usage : $bioperl_obj = $fac->bioperl_create($obj_type, $biophylo_proj);
770 Function: Create a specified bioperl object using a Bio::Phylo project
771 Args : scalar string ('aln', 'tree', 'seq') type designator
772 Bio::Phylo::Project object
773 Returns : Appropriate BioPerl object
774  
775 =cut
776  
777 0 ▶sub bioperl_create {
Subroutine bioperl_create: not called CC=2 SCAR=17.9
Pod undocumented
778 0 my $self = shift;
779 0 my ($type, @args) = @_;
780 0 ▶ unless (grep /^type/,qw( seq aln tree )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^type/, ("seq", "aln", "tree")) 0 0 0
781 0 $self->throw("Unrecognized type for argument 1");
782 }
783 0 my $call = 'create_bioperl_'.$type;
784 0 return $self->$call(@args);
785 }
786  
787 8 1;
788  
↑ Summary NexmlIO.pm »