lib/Bio/NEXUS/Tools/NexModifier.pm

filter: Statementstmt 3.6% 17 / 461 Branchbran 0.0% 0 / 208 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 12.5% 5 / 40 Podpod 100% 35 / 35 totaltotal 7.2% 57 / 782 CC 131 SCAR 58.9
CC
131
Cov
2%
modify
1482.0
setsbycharstate
110.0
removesets
110.0
CRAP 360.2
« lib/Bio/NEXUS/Tools/NexPlotter.pm ↑ Summary lib/Bio/NEXUS/DistancesBlock.pm »
1 #!/usr/bin/perl -w
2 package Bio::NEXUS::Tools::NexModifier;
3 #######################################################################
4 # Bio::NEXUS::Tools::NexModifier.pm
5 #######################################################################
6 # Author: Chengzhi Liang, Weigang Qiu, Eugene Melamud, Peter Yang
7 # $Id: NexModifier.pm,v 1.2 2008/06/16 19:53:41 astoltzfus Exp $
8  
9 # This script provides a set of functions for manipulating NEXUS files
10 # eg, select/exclude/rename OTUs, select character columns in OTUs
11 # select/exclude subtree, select/reroot tree
12  
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Bio::NEXUS; our $VERSION = $Bio::NEXUS::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 $Data::Dumper::Maxdepth=3;
20  
21 =head2 new
22  
23 Title : new
24 Usage : NA
25 Function: create a new modifier object
26 Returns : NA
27 Args : NA
28  
29 =cut
30 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
31 0 my $self = shift;
32 0 my $inp_data = $_[0];
33 0 ▶ $inp_data = [@_] if (ref($inp_data) ne 'ARRAY') ;
Branch: 1 branch
BranchTrueFalseTotal
if ref $inp_data ne "ARRAY" 0 0 0
34  
35 #print Dumper $inp_data;
36 0 my $object_data = {
37 'parameters' => [
38 'version' => 1.0,
39 'help' => undef,
40 'input_file' => 'test.nex',
41 'output_file' => 'temp.nex',
42 'rename_otus' => undef,
43 'rename_otus_file' => undef,
44 'restrict_blocks' => undef,
45 'restrict_otus' => undef,
46 'restrict_subtree' => undef,
47 'restrict_tree' => undef,
48 'restrict_column' => undef,
49 'restrict_set' => undef,
50 'exclude_blocks' => undef,
51 'exclude_otus' => undef,
52 'exclude_subtree' => undef,
53 'exclude_column' => undef,
54 'exclude_set' => undef,
55 'root' => undef,
56 'addtree' => undef,
57 'make_sets_by_file' => undef,
58 'make_sets_by_inode' => undef,
59 'make_sets_by_otu' => undef,
60 'make_sets_by_clade' => undef,
61 'make_sets_by_union' => undef,
62 'make_sets_by_difference' => undef,
63 'make_sets_by_charstate' => undef,
64 'make_sets_by_cladeconsensus' => undef,
65 'make_sets_by_difference' => undef,
66 'remove_sets' => undef,
67 'list_sets' => undef,
68 'rewrite' => undef,
69 'return_nexus_obj' => 1,
70 ],
71 'data'=> {
72  
73 },
74 };
75  
76 0 ▶ for (my $i=0; $i < scalar @$inp_data; $i = $i+2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$inp_data) 0 0 0
77 0 $inp_data->[$i] =~s/^-//;
78 }
79
80 0 $object_data->{'parameters'} = $inp_data;
81
82 0 bless($object_data,$self);
83 0 return $object_data;
84  
85 }
86  
87 =head2 modify
88  
89 Title : modify
90 Usage : NA
91 Function: alter object
92 Returns : NA
93 Args : NA
94  
95 =cut
96  
97 0 ▶sub modify {
Subroutine modify: not called CC=38 SCAR=73.0
Pod documented
98 0 my $self = shift;
99 0 my $options = $self->{parameters};
100 0 my %options_as_hash = @{$options};
101  
102 0 my $infile = $options_as_hash{input_file};
103  
104 0 ▶ unless ($infile) {die "Usage: nextool.pl <infile> [outfile] [command [options]]\n";}
Branch: 1 branch
BranchTrueFalseTotal
unless ($infile) 0 0 0
105  
106 0 ▶ if ($infile eq "-v") {
Branch: 1 branch
BranchTrueFalseTotal
if ($infile eq "-v") 0 0 0
107 0 die "nextool.pl \$Revision: 1.2 $\n";
108 }
109 0 ▶ if ($infile eq "-h" || $infile eq '--help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($infile eq "-h" or $infile eq "--help") 0 0 0
Condition0%0/3
$infile eq "-h" or $infile eq "--help"
l!l&&r!l&&!r
000
MC/DC0%0/2
$infile eq "-h" or $infile eq "--help"
$infile eq "-h" $infile eq "--help"
Truth table0%0/3
A or B
A= $infile eq "-h"B= $infile eq "--help"
ABresult
1X1
011
000
110 0 pod2usage(-exitval => 0, -verbose => 1);
111 }
112  
113 0 my $outfile = $options_as_hash{output_file};
114  
115 ###if ($outfile eq "-") {$outfile = \*STDOUT;}
116 0 ▶ if ($outfile =~ /(^-$|^STDOUT$)/) {$outfile = \*STDOUT;}
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile =~ /(^-$|^STDOUT$)/) 0 0 0
117 0 my $command;
118 my $value;
119  
120 0 my @test = @{$options};
121  
122 0 my $nexus = new Bio::NEXUS($infile);
123  
124 0 ▶ if (! $outfile || $options_as_hash{"rewrite"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $outfile or $options_as_hash{"rewrite"}) 0 0 0
Condition0%0/3
not $outfile or $options_as_hash{"rewrite"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $outfile or $options_as_hash{"rewrite"}
not $outfile $options_as_hash{"rewrite"}
Truth table0%0/3
not(A) or B
A= not $outfileB= $options_as_hash{"rewrite"}
ABresult
1X1
011
000
125 0 $nexus = new Bio::NEXUS($infile, 1);
126 0 ▶ $nexus->write($outfile||"temp.nex", 1);
Condition0%0/2
$outfile || 'temp.nex'
l!l
00
MC/DC0%0/1
$outfile || 'temp.nex'
$outfile
Truth table0%0/2
A
A= $outfile
Aresult
11
00
127 0 return $nexus;
128 }
129 #print Dumper $nexus;
130  
131 0 ▶ for(my $i=0; $i < scalar @test ; $i = $i+2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @test) 0 0 0
132 0 my $runtime_options = { $test[$i] => $test[$i+1] };
133 #print "$test[$i], i=$i\n";
134  
135 ### Rename options
136 0 ▶ $nexus = rename_otus_file($nexus,$runtime_options->{rename_otus_file} ) if $runtime_options->{rename_otus_file};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"rename_otus_file"} 0 0 0
137 0 ▶ $nexus = $nexus->rename_otus($runtime_options->{rename_otus}) if $runtime_options->{rename_otus};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"rename_otus"} 0 0 0
138  
139 ### Restrict options
140 0 ▶ $nexus = selectbyblock($nexus, $runtime_options->{restrict_blocks}) if $runtime_options->{restrict_blocks};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"restrict_blocks"} 0 0 0
141 0 ▶ $nexus = $nexus->select_otus($runtime_options->{restrict_otus}) if $runtime_options->{restrict_otus};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"restrict_otus"} 0 0 0
142 0 ▶ $nexus = selectbyinode($nexus, $runtime_options->{restrict_subtree}) if $runtime_options->{restrict_subtree};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"restrict_subtree"} 0 0 0
143 0 ▶ $nexus = selectbytree($nexus, $runtime_options->{restrict_tree}) if $runtime_options->{restrict_tree};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"restrict_tree"} 0 0 0
144 0 ▶ $nexus = selectbycolumn($nexus,1, $runtime_options->{restrict_column}) if $runtime_options->{restrict_column};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"restrict_column"} 0 0 0
145 0 ▶ $nexus = selectbysets($nexus,1, $runtime_options->{restrict_set}) if $runtime_options->{restrict_set};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"restrict_set"} 0 0 0
146  
147 ### Exclude options
148 0 ▶ $nexus = exclude_blocks($nexus, $runtime_options->{exclude_blocks}) if $runtime_options->{exclude_blocks};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"exclude_blocks"} 0 0 0
149 0 ▶ $nexus = $nexus->exclude_otus($runtime_options->{exclude_otus}) if $runtime_options->{exclude_otus};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"exclude_otus"} 0 0 0
150 0 ▶ $nexus = exclude_subtree($nexus, $runtime_options->{exclude_subtree}) if $runtime_options->{exclude_subtree};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"exclude_subtree"} 0 0 0
151 0 ▶ $nexus = selectbycolumn($nexus,0, $runtime_options->{exclude_column}) if $runtime_options->{exclude_column};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"exclude_column"} 0 0 0
152 0 ▶ $nexus = selectbysets($nexus,0, $runtime_options->{exclude_set}) if $runtime_options->{exclude_set};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"exclude_set"} 0 0 0
153  
154 ### Reroot options
155 0 ▶ $nexus = reroottree($nexus, $runtime_options->{reroottree}) if $runtime_options->{reroottree};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"reroottree"} 0 0 0
156  
157 ### Addtree options
158 0 ▶ &addtree($nexus,$runtime_options->{add_tree}) if $runtime_options->{addtree};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"addtree"} 0 0 0
159  
160 ### Sets options
161 0 ▶ $nexus = setsbyfile($nexus, $runtime_options->{make_sets_by_file}) if $runtime_options->{make_sets_by_file};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"make_sets_by_file"} 0 0 0
162 0 ▶ $nexus = setsbyinode($nexus,$runtime_options->{make_sets_by_inode} ) if $runtime_options->{make_sets_by_inode};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"make_sets_by_inode"} 0 0 0
163 0 ▶ $nexus = setsbyotus($nexus, $runtime_options->{make_sets_by_otu}) if $runtime_options->{make_sets_by_otu};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"make_sets_by_otu"} 0 0 0
164 0 ▶ $nexus = setsbyclade($nexus, $runtime_options->{make_sets_by_clade}) if $runtime_options->{make_sets_by_clade};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"make_sets_by_clade"} 0 0 0
165 0 ▶ $nexus = setsbyunion($nexus, $runtime_options->{make_sets_by_union}) if $runtime_options->{make_sets_by_union};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"make_sets_by_union"} 0 0 0
166 0 ▶ $nexus = setsbydifference($nexus, $runtime_options->{make_sets_by_difference}) if $runtime_options->{make_sets_by_difference};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"make_sets_by_difference"} 0 0 0
167 0 ▶ $nexus = setsbycharstate($nexus,$runtime_options->{make_sets_by_charstate}) if $runtime_options->{make_sets_by_charstate};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"make_sets_by_charstate"} 0 0 0
168 0 ▶ $nexus = setsbycladeconsensus($nexus,$runtime_options->{make_sets_by_cladeconsensus} ) if $runtime_options->{make_sets_by_cladeconsensus};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"make_sets_by_cladeconsensus"} 0 0 0
169 #die "Expecting command that looks like 'makesets <bymode> <arguments>', where bymode is one of: file, inode, otu, clade, union, difference, charstate, cladeconsensus\n";
170 0 ▶ $nexus = removesets($nexus,$runtime_options->{remove_sets}) if $runtime_options->{remove_sets};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"remove_sets"} 0 0 0
171 0 ▶ $nexus = renamesets($nexus,$runtime_options->{rename_sets}) if $runtime_options->{rename_sets};
Branch: 1 branch
BranchTrueFalseTotal
if $runtime_options->{"rename_sets"} 0 0 0
172 0 ▶ if ($runtime_options->{listsets}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($runtime_options->{"listsets"}) 0 0 0
173 0 file_overwrite_warning($infile, $outfile, 'listsets', 0);
174 0 listsets($nexus, $outfile, $runtime_options->{listsets});
175 }
176 0 ▶ if ($runtime_options->{listsetnames}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($runtime_options->{"listsetnames"}) 0 0 0
177 0 file_overwrite_warning($infile, $outfile,'listsetnames' , 0);
178 0 listsetnames($nexus, $outfile);
179 }
180 }
181  
182 0 $nexus->write($outfile);
183 0 ▶ return $nexus if $options_as_hash{return_nexus_obj};
Branch: 1 branch
BranchTrueFalseTotal
if $options_as_hash{"return_nexus_obj"} 0 0 0
184 }
185  
186  
187 ######### SUBROUTINES #########################
188  
189  
190 =head2 rename_otus
191  
192 Title : rename_otus
193 Usage : NA
194 Function: assigns new labels to OTUs
195 Returns : NA
196 Args : NA
197  
198 =cut
199  
200 0 ▶sub rename_otus {
Subroutine rename_otus: not called CC=2 SCAR=17.9
Pod documented
201 0 my ($nexus, $transfile) = @_;
202 0 ▶ open(FILE, "<$transfile") or die "ERROR: Can\'t open $transfile\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<$transfile" or die "ERROR: Can't open $transfile\n" 0 0 0
203 0 my @lines = <FILE>;
204 0 my $lines = "@lines";
205 0 my %translation = (split(/\s+/, $lines));
206 0 return $nexus->rename_otus(\%translation);
207 }
208  
209 =head2 selectbyblock
210  
211 Title : selectbyblock
212 Usage : NA
213 Function: select a subset of blocks
214 Returns : NA
215 Args : NA
216  
217 =cut
218  
219 0 ▶sub selectbyblock {
Subroutine selectbyblock: not called CC=1 SCAR=6.9
Pod documented
220 0 my ($nexus, @blocks) = @_;
221 0 return $nexus->select_blocks(\@blocks);
222 }
223  
224 =head2 selectbyotu
225  
226 Title : selectbyotu
227 Usage : NA
228 Function: select subset of OTUs by labels
229 Returns : NA
230 Args : NA
231  
232 =cut
233 #
234 0 ▶sub selectbyotu {
Subroutine selectbyotu: not called CC=5 SCAR=34.0
Pod documented
235 0 my ($nexus, $mode, @args) = @_;
236 0 ▶ die "need otu names" unless (@args);
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 0 0
237 0 my @otus;
238 0 ▶ if ($args[0] eq '-f') { # input from file
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] eq '-f') { } 0 0 0
239 0 my $file = $args[1];
240 0 ▶ open(FILE, $file) or carp "File $file not found\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $file or carp("File $file not found\n") 0 0 0
241 0 my @lines = <FILE>;
242 0 @otus = split /\s+/, "@lines";
243 0 close(FILE);
244 }
245 else { # input from command line separated by space
246 0 my $list = join( " ", @args );
247 0 $list = unquote( $list );
248 0 $list =~ s/^\s+|\s+$//g;
249 0 @otus = split( /[,\s]\s*/, $list );
250 }
251 0 ▶ if ( $mode == 1 ) { # select mode
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == 1) { } 0 0 0
252 0 return $nexus->select_otus(\@otus);
253 }
254 else { # exclude mode
255 0 return $nexus->exclude_otus(\@otus);
256 }
257 }
258  
259 =head2 selectbytree
260  
261 Title : selectbytree
262 Usage : NA
263 Function: select a tree
264 Returns : NA
265 Args : NA
266  
267 =cut
268  
269 0 ▶sub selectbytree {
Subroutine selectbytree: not called CC=2 SCAR=17.9
Pod documented
270 0 my ($nexus, $treename) = @_;
271 0 ▶ ($treename) or die "ERROR: Need to specify a tree to be selected\n";
Branch: 1 branch
BranchTrueFalseTotal
$treename or die "ERROR: Need to specify a tree to be selected\n" 0 0 0
272 0 return $nexus->select_tree($treename);
273 }
274  
275 =head2 selectbyinode
276  
277 Title : selectbyinode
278 Usage : NA
279 Function: select a subtree by specifying its root internal node
280 Returns : NA
281 Args : NA
282  
283 =cut
284 #
285 0 ▶sub selectbyinode {
Subroutine selectbyinode: not called CC=2 SCAR=17.9
Pod documented
286 0 my ($nexus, $nodename, $treename) = @_;
287 0 ▶ $nodename or die "ERROR: Need to specify an internal node for subtree\n";
Branch: 1 branch
BranchTrueFalseTotal
$nodename or die "ERROR: Need to specify an internal node for subtree\n" 0 0 0
288  
289 0 return $nexus->select_subtree($nodename, $treename);
290 }
291  
292 =head2 selectbycolumn
293  
294 Title : selectbycolumn
295 Usage : NA
296 Function: select ($command=1) or exclude ($command=0) specified column list or file with list
297 Returns : NA
298 Args : NA
299  
300 =cut
301 # arguments (column numbers) can be of form:
302 # 1) 1-3 4 5 6-10 or 1-3, 4 5, 6-10
303 # 2) -f <file name> contains numbers in format as examplified in 1)
304 0 ▶sub selectbycolumn {
Subroutine selectbycolumn: not called CC=4 SCAR=30.0
Pod documented
305 0 my ($nexus, $command, @args) = @_;
306 0 my $args = "@args";
307 0 $args =~ s/title\s*=\s*(\w+)//i;
308 0 my $title = $1;
309 # my $block = $nexus->get_block("characters", $title);
310  
311 0 ▶ die "need column numbers" unless $args;
Branch: 1 branch
BranchTrueFalseTotal
unless $args 0 0 0
312  
313 0 my $columns;
314 0 ▶ if ($args =~ /-f (\S+)/) { # input from file
Branch: 1 branch
BranchTrueFalseTotal
if ($args =~ /-f (\S+)/) { } 0 0 0
315 0 my $file = $1;
316 0 $columns = do{ local(@ARGV, $/) = $file; <>};
317 0 $columns =~ s/\n/ /g;
318 }else { # input from command line separated by comma or space
319 0 $columns = $args;
320 }
321  
322 0 my @columns = @{ &parse_number($columns) };
323 0 ▶ if ($command) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command) { } 0 0 0
324 0 return $nexus->select_chars(\@columns, $title);
325 }else {
326 0 return $nexus->exclude_chars(\@columns, $title);
327 }
328 }
329  
330 =head2 selectbysets
331  
332 Title : selectbysets
333 Usage : NA
334 Function: NA
335 Returns : NA
336 Args : NA
337  
338 =cut
339 0 ▶sub selectbysets {
Subroutine selectbysets: not called CC=4 SCAR=30.0
Pod documented
340 0 my ($nexus, $mode, @setnames) = @_;
341 0 my @otus;
342 0 ▶ die "Provide set names as arguments\n" unless (@setnames);
Branch: 1 branch
BranchTrueFalseTotal
unless @setnames 0 0 0
343 0 for my $setname (@setnames) {
344 0 push(@otus, @{ $nexus->get_block('sets')->get_taxset($setname) } );
345 }
346  
347 0 ▶ if ( $mode == 1 ) { # "select" mode
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == 1) { } 0 0 0
348 0 return $nexus->select_otus(\@otus);
349 } else { # "exclude" mode
350 0 return $nexus->exclude_otus(\@otus);
351 }
352 }
353  
354 =head2 parse_number
355  
356 Title : parse_number
357 Usage : NA
358 Function: parse numbers in format "1-3, 4 6 8-10"
359 Returns : NA
360 Args : NA
361  
362 =cut
363 #
364 0 ▶sub parse_number {
Subroutine parse_number: not called CC=7 SCAR=40.3
Pod documented
365 0 my $s = unquote( shift );
366  
367 0 ▶ if (! $s =~ /^\s*(\d+(-\d+)?)([,\s]\s*\d+(-\d+)?)*\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (!$s =~ /^\s*(\d+(-\d+)?)([,\s]\s*\d+(-\d+)?)*\s*$/) 0 0 0
368 0 die "Invalid number format. Use 1 or 1-3 or 1, 3, 5-8 or 1 3 5 6-10.\n";
369 }
370 0 $s =~ s/^\s+|\s+$//g;
371 0 $s =~ s/,?\s+/,/g; # use ',' as separator
372 0 my @cols = split(/,/, $s);
373  
374 0 my @arr;
375 0 foreach my $item (@cols) {
376 0 ▶ if ($item =~ /-/) { # eg 1-3
Branch: 3 branches
BranchTrueFalseTotal
if ($item =~ /-/) { } 0 0 0
elsif ($item =~ /^\d+$/) { } 0 0 0
elsif ($item) { } 0 0 0
377 0 $item =~ /([0-9]+)\s*-\s*([0-9]+)/;
378 0 ▶ for (my $i = $1; $i <= $2; $i++) { push ( @arr, $i-1 ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $2) 0 0 0
379 } elsif ($item =~ /^\d+$/) { # eg 4
380 0 push ( @arr, $item-1 );
381 } elsif ($item) {
382 0 die "non-number was used for column number\n";
383 }
384 }
385  
386 0 @arr = sort {$a<=>$b} @arr;
387 0 return \@arr;
388 }
389  
390 =head2 unquote
391  
392 Title : unquote
393 Usage : NA
394 Function: NA
395 Returns : NA
396 Args : NA
397  
398 =cut
399 0 ▶sub unquote {
Subroutine unquote: not called CC=1 SCAR=6.9
Pod documented
400 0 my $string = shift;
401 0 $string =~ s/^ *'(.*)' *$/$1/;
402 0 $string =~ s/^ *"(.*)" *$/$1/;
403 0 return( $string );
404 }
405  
406 =head2 exclude_blocks
407  
408 Title : exclude_blocks
409 Usage : NA
410 Function: NA
411 Returns : NA
412 Args : NA
413  
414 =cut
415 0 ▶sub exclude_blocks {
Subroutine exclude_blocks: not called CC=1 SCAR=6.9
Pod documented
416 0 my ($nexus, @blocks) = @_;
417 0 return $nexus->exclude_blocks(\@blocks);
418 }
419  
420 =head2 exclude_subtree
421  
422 Title : exclude_subtree
423 Usage : NA
424 Function: NA
425 Returns : NA
426 Args : NA
427  
428 =cut
429 0 ▶sub exclude_subtree {
Subroutine exclude_subtree: not called CC=1 SCAR=6.9
Pod documented
430 0 my ($nexus, $subtree) = @_;
431 0 return $nexus->exclude_subtree($subtree);
432 }
433  
434 =head2 reroottree
435  
436 Title : reroottree
437 Usage : NA
438 Function: NA
439 Returns : NA
440 Args : NA
441  
442 =cut
443 0 ▶sub reroottree {
Subroutine reroottree: not called CC=2 SCAR=17.9
Pod documented
444 0 my ($nexus, $outgroup, $root_position, $treename) = @_;
445 0 ▶ if ( $nexus->get_block('trees')->get_tree('$root_position') ) { #in case no root position is supplied, but a tree name is
Branch: 1 branch
BranchTrueFalseTotal
if ($nexus->get_block("trees")->get_tree("\$root_position")) 0 0 0
446 0 ($root_position, $treename) = (undef, $root_position);
447 }
448 0 return $nexus->reroot($outgroup, $root_position, $treename);
449 }
450  
451 =head2 addtree
452  
453 Title : addtree
454 Usage : NA
455 Function: NA
456 Returns : NA
457 Args : NA
458  
459 =cut
460 0 ▶sub addtree {
Subroutine addtree: not called CC=6 SCAR=37.4
Pod documented
461 0 my ($nexus,$treeFile,$treeName) = @_;
462  
463 0 ▶ if ( !$treeName ) { ($treeName = (split("/",$treeFile))[-1]) =~ s/(.*)\..*/$1/; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($treeName) 0 0 0
464  
465 0 ▶ if ( -f $treeFile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $treeFile) { } 0 0 0
466 0 ▶ open(TF,"<$treeFile") || warn("ERROR: could not open $treeFile. No tree data will appear in NEXUS file.");
Branch: 1 branch
BranchTrueFalseTotal
open TF, "<$treeFile" or warn "ERROR: could not open $treeFile. No tree data will appear in NEXUS file." 0 0 0
467  
468 0 my ($treeString);
469 0 ▶ while(<TF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline TF)) 0 0 0
470 0 s/\s*//go;
471 0 $treeString .= $_;
472 }
473  
474 0 close(TF);
475  
476 0 $treeString =~ s/(.*);$/$1/; #remove semicolon from end of $treeString, if there is one.
477  
478 #$treeString must be formatted like this in order to work w/ Bio::NEXUS::TreesBlock::new -
479 # tree $treeName = $treeString
480 #the following manipulations ensure we end up w/ such a string to pass to create_block
481 0 $treeString =~ s/^tree(.*)$/$1/;
482 0 ▶ unless ( $treeString =~ m/^$treeName=/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($treeString =~ /^$treeName=/) 0 0 0
483 0 $treeString = $treeName . " = " . $treeString;
484 }
485 0 $treeString = "tree " . $treeString;
486  
487 0 print STDERR "tree string:\n$treeString\n";
488 0 $nexus->add_block( $nexus->create_block('trees',$treeString, 1) );
489 } else {
490 0 die("Tree file: \"$treeFile\" not found or could not be read");
491 }
492 }
493  
494 =head2 setsbyfile
495  
496 Title : setsbyfile
497 Usage : NA
498 Function: NA
499 Returns : NA
500 Args : NA
501  
502 =cut
503 0 ▶sub setsbyfile {
Subroutine setsbyfile: not called CC=4 SCAR=30.0
Pod documented
504 0 my ($nexus, @setfiles) = @_;
505 0 my $sets;
506  
507 0 for my $setfile (@setfiles) {
508 0 ▶ open (SF, "<$setfile") || die ("Could not open set file $setfile.\n\n");
Branch: 1 branch
BranchTrueFalseTotal
open SF, "<$setfile" or die "Could not open set file $setfile.\n\n" 0 0 0
509 0 ▶ while (<SF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline SF)) 0 0 0
510 0 chomp;
511 0 push (@{$$sets{$setfile}}, $_);
512 }
513 0 close (SF);
514 }
515 0 &add_or_create_set ($nexus, $sets);
516 0 return $nexus;
517 }
518  
519 =head2 setsbyinode
520  
521 Title : setsbyinode
522 Usage : NA
523 Function: NA
524 Returns : NA
525 Args : NA
526  
527 =cut
528 0 ▶sub setsbyinode {
Subroutine setsbyinode: not called CC=3 SCAR=24.8
Pod documented
529 0 my ($nexus, @inodenames) = @_;
530 0 my $sets;
531  
532 0 for my $inodename (@inodenames) {
533 0 ▶ unless ($nexus->get_block('trees')->get_tree()->find($inodename)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nexus->get_block("trees")->get_tree->find($inodename)) 0 0 0
534 0 die "User-provided inode <$inodename> is not the name of an inode in this NEXUS file\nCommand aborted\n\n";
535 }
536 0 my $subtree = $nexus->select_subtree($inodename);
537 0 my $otus = $subtree->get_otus();
538 0 $$sets{$inodename} = $otus;
539 }
540 0 &add_or_create_set ($nexus, $sets);
541 0 return $nexus;
542 }
543  
544 =head2 setsbyclade
545  
546 Title : setsbyclade
547 Usage : NA
548 Function: NA
549 Returns : NA
550 Args : NA
551  
552 =cut
553 0 ▶sub setsbyclade {
Subroutine setsbyclade: not called CC=4 SCAR=30.0
Pod documented
554 0 my ($nexus, @clade_elements) = @_;
555 0 my ($sets, $treename);# $treename is currently a dummy variable, but we should implement it
556  
557 0 ▶ unless (validate_otus($nexus, [@clade_elements])) {die "User-provided OTUs failed validation against TAXA Block\n\n"}
Branch: 1 branch
BranchTrueFalseTotal
unless (validate_otus($nexus, [@clade_elements])) 0 0 0
558  
559 0 ▶ while (scalar(@clade_elements) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @clade_elements > 0) 0 0 0
560 0 my ($otu1, $otu2) = splice(@clade_elements, 0, 2);
561 0 ▶ unless (defined $otu2) {die "You have supplied an odd number of OTU's. This mode requires pairs.\n\n"}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $otu2) 0 0 0
562 0 $otu1 = $nexus->get_block('trees')->get_tree($treename)->find($otu1);
563 0 $otu2 = $nexus->get_block('trees')->get_tree($treename)->find($otu2);
564 0 my $inode_name = $otu1 -> mrca($otu2, $treename) -> name();
565 0 my $otus = $nexus->select_subtree($inode_name)->get_otus();
566 0 $$sets{$inode_name} = $otus;
567 }
568  
569 0 &add_or_create_set ($nexus, $sets);
570 0 return $nexus;
571 }
572  
573 =head2 setsbyotus
574  
575 Title : setsbyotus
576 Usage : NA
577 Function: NA
578 Returns : NA
579 Args : NA
580  
581 =cut
582 0 ▶sub setsbyotus {
Subroutine setsbyotus: not called CC=4 SCAR=30.0
Pod documented
583 0 my ($nexus, @otuargs) = @_;
584 0 my $otuargs = join(" ", @otuargs);
585 0 my $sets;
586  
587 0 my @setsandelements = $otuargs =~ /\s*([^=]+)\s*=\s*\[\s*([^\]]+)\s*\]/g;
588  
589 0 ▶ while (@setsandelements) {
Branch: 1 branch
BranchTrueFalseTotal
if (@setsandelements) 0 0 0
590 0 $$sets{shift(@setsandelements)} = [split(/\s/, splice(@setsandelements, 1, 1))];
591 }
592  
593 0 my $validation = 1;
594 0 for my $users_otus (values %$sets) {
595 0 $validation = validate_otus($nexus, $users_otus);
596 }
597  
598 0 ▶ unless ($validation == 1) {die "User-defined sets failed validation against TAXA Block\n\n"}
Branch: 1 branch
BranchTrueFalseTotal
unless ($validation == 1) 0 0 0
599  
600 0 &add_or_create_set ($nexus, $sets);
601 0 return $nexus;
602 }
603  
604 =head2 setsbyunion
605  
606 Title : setsbyunion
607 Usage : NA
608 Function: NA
609 Returns : NA
610 Args : NA
611  
612 =cut
613 0 ▶sub setsbyunion {
Subroutine setsbyunion: not called CC=3 SCAR=24.8
Pod documented
614 0 my ($nexus, @setargs) = @_;
615 0 my $setargs = join(" ", @setargs);
616 0 my $sets = $nexus -> get_block('sets') -> get_taxsets();
617  
618 0 my @setsandelements = $setargs =~ /\s*(\b[^=]+\b)\s*=\s*\[\s*([^\]]+)\s*\]/g;
619 0 ▶ while (@setsandelements) {
Branch: 1 branch
BranchTrueFalseTotal
if (@setsandelements) 0 0 0
620 0 my ($newsetname, @subsets) = (shift(@setsandelements), split(/(?:\s+|\s*\+\s*)/, shift(@setsandelements)));
621 0 my @otus;
622 0 for my $subset (@subsets) {
623 0 push(@otus, @{$nexus -> get_block('sets') -> get_taxset($subset)});
624 }
625 0 $$sets{$newsetname} = [@otus];
626 }
627  
628 0 &add_or_create_set ($nexus, $sets);
629 0 return $nexus;
630 }
631  
632 =head2 setsbydifference
633  
634 Title : setsbydifference
635 Usage : NA
636 Function: NA
637 Returns : NA
638 Args : NA
639  
640 =cut
641 0 ▶sub setsbydifference {
Subroutine setsbydifference: not called CC=6 SCAR=37.4
Pod documented
642 0 my ($nexus, @setargs) = @_;
643 0 my $setargs = join(" ", @setargs);
644 0 my $sets = $nexus -> get_block('sets') -> get_taxsets();
645 # print Dumper ($sets);
646  
647 0 @setargs = $setargs =~ /\s*([^=\s]+)\s*=\s*\[\s*([^-\s\]]+)\s*-\s*([^-\s\]]+)\s*\]/g;
648 0 my (@difference_set_names, @minuend_set_names, @subtrahend_set_names);
649 0 ▶ while (@setargs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@setargs) 0 0 0
650 0 push(@difference_set_names, shift (@setargs));
651 0 push(@minuend_set_names, shift (@setargs));
652 0 push(@subtrahend_set_names, shift (@setargs));
653 }
654  
655 0 ▶ for (my $i = 0; $i < @difference_set_names; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @difference_set_names) 0 0 0
656 0 $$sets{$difference_set_names[$i]} = [@{$$sets{$minuend_set_names[$i]}}];
657 0 ▶ for (my $j = 0; $j < @{$$sets{ $difference_set_names[$i] }}; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @{$$sets{$difference_set_names[$i]};}) 0 0 0
658 0 for my $subtrahendOTU ( @{$$sets{ $subtrahend_set_names[$i] }} ) {
659 0 ▶ if ( ${$$sets{ $difference_set_names[$i] }}[$j] eq $subtrahendOTU) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { $$sets{$difference_set_names[$i]} }->[$j] eq $subtrahendOTU) 0 0 0
660 0 splice ( @{$$sets{ $difference_set_names[$i] }}, $j, 1);
661 }
662 }
663 }
664 }
665  
666 0 &add_or_create_set ($nexus, $sets);
667 0 return $nexus;
668 }
669  
670 =head2 setsbycharstate
671  
672 Title : setsbycharstate
673 Usage : NA
674 Function: NA
675 Returns : NA
676 Args : NA
677  
678 =cut
679 0 ▶sub setsbycharstate {
Subroutine setsbycharstate: not called CC=10 SCAR=47.0
Pod documented
680 0 my ($nexus, @char_args) = @_;
681 0 my ($proposed_sets, $title) = &parse_charstate_args(@char_args);
682 0 my ($sets, @column_labels);
683 0 my $block = $nexus->get_block("characters", $title);
684  
685 0 my $character_labels = $block->get_charlabels;
686 0 my $seq_length = $block->get_nchar;
687 0 my %char_matrix = %{$block->get_otuset->get_otu_sequences};
688 0 ▶ if ($character_labels && @$character_labels) {
Branch: 2 branches
BranchTrueFalseTotal
if ($character_labels and @$character_labels) { } 0 0 0
elsif ($seq_length) { } 0 0 0
Condition0%0/3
$character_labels and @$character_labels
!ll&&!rl&&r
000
MC/DC0%0/2
$character_labels and @$character_labels
$character_labels @$character_labels
Truth table0%0/3
A and B
A= $character_labelsB= @$character_labels
ABresult
0X0
100
111
689 0 @column_labels = @$character_labels;
690 } elsif ($seq_length) {
691 0 for (1 .. $seq_length) {push @column_labels, $_;}
692 }
693 0 my %column_indices;
694 0 ▶ for (my $i = 0; $i < @column_labels; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @column_labels) 0 0 0
695 0 $column_indices{$column_labels[$i]} = $i;
696 }
697 0 foreach my $proposed_setname (keys %{$proposed_sets}){
698 0 ▶ while (@{$$proposed_sets{$proposed_setname}} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$proposed_sets->{$proposed_setname};} > 0) 0 0 0
699 0 my ($column_label, $query_value) = splice(@{$$proposed_sets{$proposed_setname}}, 0, 2);
700 0 foreach my $otu (keys %char_matrix) {
701 0 my $locus_value = substr($char_matrix{$otu},$column_indices{$column_label},1);
702 0 ▶ if ($query_value =~ m/$locus_value/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query_value =~ /$locus_value/) 0 0 0
703 0 push (@{$$sets{$proposed_setname}}, $otu);
704 }
705 }
706 }
707 }
708  
709 0 &add_or_create_set ($nexus, $sets);
710 0 return $nexus;
711 }
712  
713 =head2 parse_charstate_args
714  
715 Title : parse_charstate_args
716 Usage : NA
717 Function: NA
718 Returns : NA
719 Args : NA
720  
721 =cut
722 0 ▶sub parse_charstate_args {
Subroutine parse_charstate_args: not called CC=5 SCAR=34.0
Pod documented
723 0 my $char_args = join (" ", @_);
724 0 $char_args =~ m/title\s*=\s*(\w+)/i;
725 0 my $title = $1; # store the captured title name, if one was provided
726 0 my $num_title_matches = $char_args =~ s/title\s*=\s*\w+//g; # make sure the user only referred to one title (to prevent ambiguity)
727 0 ▶ if ($num_title_matches > 1 ) {die ("Too many 'title' arguments--please reference only one Characters Block\n\n")}
Branch: 1 branch
BranchTrueFalseTotal
if ($num_title_matches > 1) 0 0 0
728 # the following line matches each set as defined by the user and stores it in the @proposed_sets array
729 # basically, it looks for bracket pairs that look like foo[bar],
730 # which may or may not be preceded by a set name declaration ('set_name = ')
731 0 my @proposed_sets = $char_args =~ m/(?:[-\w]+\s*=\s*)?\s*[-\w]+\s*\[[^\]]+\]\s*/g;
732 0 my %proposed_sets;
733 0 for my $proposed_set (@proposed_sets) { # for each of the matches made above
734 0 my $set_name;
735 0 ▶ if ($proposed_set =~ m/^\s*([^\s\[\]]+)\s*=/) { # if a name declaration was matched
Branch: 1 branch
BranchTrueFalseTotal
if ($proposed_set =~ /^\s*([^\s\[\]]+)\s*=/) { } 0 0 0
736 0 $set_name = $1; # use that as the set name
737 0 $proposed_set =~ s/^\s*[^\s\[\]]+\s*=\s*//; # then, get rid of that portion of the array element
738 } else { # otherwise, make up a unique set name by joining the column label and locus value(s)
739 0 $set_name = join("", ($proposed_set =~ m/[^"\s\[\]]/g));
740 }
741 0 my ($column_label, $locus_value);
742 0 ▶ while ($proposed_set =~ m/[^"\s]/) { # as long as $proposed_set contains more than spaces and quotes
Branch: 1 branch
BranchTrueFalseTotal
if ($proposed_set =~ /[^"\s]/) 0 0 0
743 0 ($column_label) = $proposed_set =~ m/[^"\s\[]+/; # match the column label
744 0 $proposed_set =~ m/\[([^\]]+)\]/; # and match the locus value(s)
745 0 $locus_value = $1;
746 0 $locus_value =~ s/[\s,]//g; # get rid of any spaces or commas the user may have separated the locus values with
747 0 push (@{$proposed_sets{$set_name}}, $column_label, $locus_value); # push the column label and locus value(s) onto this sets array in the set hash
748 0 $proposed_set =~ s/^[^\]]*\]//; # destroy the evidence
749 }
750 }
751 0 return (\%proposed_sets, $title);
752 }
753  
754 =head2 setsbycladeconsensus
755  
756 Title : setsbycladeconsensus
757 Usage : NA
758 Function: NA
759 Returns : NA
760 Args : NA
761  
762 =cut
763 0 ▶sub setsbycladeconsensus {
Subroutine setsbycladeconsensus: not called CC=3 SCAR=24.8
Pod documented
764 0 my ($nexus, @args) = @_;
765 0 my $consensus_sets = {};
766 0 my $treename;
767 0 my $args = join(' ', @args);
768 0 $args =~ m/title\s*=\s*(\w+)/i;
769 0 my $title = $1;
770 0 ▶ if ($title) {$args =~ s/\s*title\s*=\s*\w+\s*//i;}
Branch: 1 branch
BranchTrueFalseTotal
if ($title) 0 0 0
771 0 ▶ if ($args =~ /(\w+)/) {$treename = $1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($args =~ /(\w+)/) 0 0 0
772 0 my $char_block = $nexus->get_block("characters", $title);
773 0 my %otunames_sequences = %{$char_block->get_otuset->get_otu_sequences};
774 0 my $tree = $nexus->get_block('trees')->get_tree($treename)->clone();
775 0 my $rootnode = $tree->get_rootnode();
776 0 &set_consensus_seqs($rootnode, \%otunames_sequences);
777 0 ($rootnode, $consensus_sets) = &simplify_tree($rootnode, $consensus_sets);
778 0 $tree->set_name("nonredundant" . $tree->name());
779 0 $nexus->get_block('trees')->add_tree($tree);
780 0 &add_or_create_set ($nexus, $consensus_sets);
781 0 return $nexus;
782 }
783  
784 =head2 set_consensus_seqs
785  
786 Title : set_consensus_seqs
787 Usage : NA
788 Function: NA
789 Returns : NA
790 Args : NA
791  
792 =cut
793 0 ▶sub set_consensus_seqs {
Subroutine set_consensus_seqs: not called CC=7 SCAR=40.3
Pod documented
794 0 my ($node, $otus_seqs) = @_;
795 0 my @children = @{ $node->children() };
796 0 for my $child (@children) {
797 0 ▶ if ( $child->is_otu() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->is_otu) { } 0 0 0
798 0 $child->set_seq(${$otus_seqs}{$child->name()});
799 0 ▶ if ( ! $node->get_seq() || ( $node->get_seq() eq $child->get_seq() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $node->get_seq or $node->get_seq eq $child->get_seq) { } 0 0 0
Condition0%0/3
not $node->get_seq or $node->get_seq eq $child->get_seq
l!l&&r!l&&!r
000
MC/DC0%0/2
not $node->get_seq or $node->get_seq eq $child->get_seq
not $node->get_seq $node->get_seq eq $child->get_seq
Truth table0%0/3
not(A) or B
A= not $node->get_seqB= $node->get_seq eq $child->get_seq
ABresult
1X1
011
000
800 0 $node->set_seq($child->get_seq());
801 } else {
802 0 $node->set_seq('NO_CONSENSUS');
803 }
804 } else {
805 0 &set_consensus_seqs($child, $otus_seqs);
806 0 ▶ if ( ! $node->get_seq() || ( $node->get_seq() eq $child->get_seq() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $node->get_seq or $node->get_seq eq $child->get_seq) { } 0 0 0
Condition0%0/3
not $node->get_seq or $node->get_seq eq $child->get_seq
l!l&&r!l&&!r
000
MC/DC0%0/2
not $node->get_seq or $node->get_seq eq $child->get_seq
not $node->get_seq $node->get_seq eq $child->get_seq
Truth table0%0/3
not(A) or B
A= not $node->get_seqB= $node->get_seq eq $child->get_seq
ABresult
1X1
011
000
807 0 $node->set_seq($child->get_seq());
808 } else {
809 0 $node->set_seq('NO_CONSENSUS');
810 }
811 }
812 }
813 }
814  
815 =head2 simplify_tree
816  
817 Title : simplify_tree
818 Usage : NA
819 Function: NA
820 Returns : NA
821 Args : NA
822  
823 =cut
824 0 ▶sub simplify_tree {
Subroutine simplify_tree: not called CC=5 SCAR=34.0
Pod documented
825 0 my ($node, $consensus_sets) = @_;
826 0 my $setname;
827 0 ▶ if ($node->get_seq() ne 'NO_CONSENSUS' && ! $node->is_otu() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->get_seq ne 'NO_CONSENSUS' and not $node->is_otu) { } 0 0 0
Condition0%0/3
$node->get_seq ne "NO_CONSENSUS" and not $node->is_otu
!ll&&!rl&&r
000
MC/DC0%0/2
$node->get_seq ne "NO_CONSENSUS" and not $node->is_otu
$node->get_seq ne "NO_CONSENSUS" not $node->is_otu
Truth table0%0/3
A and not(B)
A= $node->get_seq ne "NO_CONSENSUS"B= not $node->is_otu
ABresult
0X0
100
111
828 0 my @otu_descendents = @{ $node->get_otus() };
829 0 $setname = $otu_descendents[0]->name() . "_GROUP";
830 0 ${ $consensus_sets }{$setname} = [];
831 0 for my $descendent (@otu_descendents) {
832 0 push(@{${$consensus_sets }{$setname}}, $descendent->name());
833 }
834 0 my $newlength = $node->distance($otu_descendents[0]) + $node->length();
835 0 my $parent = $node->get_parent();
836 0 my $siblings = $node->get_siblings();
837 0 $parent->set_children($siblings);
838 0 $parent->adopt($otu_descendents[0],0);
839 0 $otu_descendents[0]->set_length($newlength);
840 } else {
841 0 for my $child (@{ $node->children() }) {
842 0 simplify_tree($child, $consensus_sets);
843 }
844 }
845 0 return ($node, $consensus_sets);
846 }
847  
848 =head2 validate_otus
849  
850 Title : validate_otus
851 Usage : NA
852 Function: NA
853 Returns : NA
854 Args : NA
855  
856 =cut
857 0 ▶sub validate_otus { #Verifies that user-specified OTU names are taxlabels in the TAXA block
Subroutine validate_otus: not called CC=3 SCAR=24.8
Pod documented
858 0 my ($nexus, $users_otus) = @_;
859 0 my $validation = 1;
860 0 for my $users_otu (@$users_otus) {
861 0 ▶ unless (defined ($nexus->get_block('taxa')->is_taxon($users_otu, 1))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nexus->get_block("taxa")->is_taxon($users_otu, 1)) 0 0 0
862 0 $validation = 0;
863 }
864 }
865 0 return $validation;
866 }
867  
868 =head2 add_or_create_set
869  
870 Title : add_or_create_set
871 Usage : NA
872 Function: NA
873 Returns : NA
874 Args : NA
875  
876 =cut
877 0 ▶sub add_or_create_set {
Subroutine add_or_create_set: not called CC=2 SCAR=17.9
Pod documented
878 0 my ($nexus, $sets) = @_;
879 0 my $setsblock;
880 0 ▶ if ($nexus -> get_block('Sets')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nexus->get_block('Sets')) { } 0 0 0
881 0 $nexus -> get_block('Sets') -> add_taxsets($sets);
882 } else {
883 0 $setsblock = Bio::NEXUS::SetsBlock -> new('Sets');
884 0 $setsblock -> set_taxsets($sets);
885 0 $nexus -> add_block($setsblock);
886 }
887 0 return $nexus;
888 }
889  
890 =head2 removesets
891  
892 Title : removesets
893 Usage : NA
894 Function: NA
895 Returns : NA
896 Args : NA
897  
898 =cut
899 0 ▶sub removesets {
Subroutine removesets: not called CC=10 SCAR=47.0
Pod documented
900 0 my ($nexus, @usersets) = @_;
901 0 my $failure = 0;
902 0 my $sets_to_remove;
903 0 my $setsblock = get_setsblock_if_setsblock($nexus);
904 0 my $taxsets = $setsblock->get_taxsets();
905 0 ▶ unless (scalar(keys %$taxsets) > 0) {die "The SETS Block is already empty\n\n";}
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %$taxsets > 0) 0 0 0
906  
907 0 ▶ if ($usersets[0] =~ /^-p$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usersets[0] =~ /^-p$/i) { } 0 0 0
908 0 shift @usersets;
909 0 for my $userset (@usersets) {
910 0 my $match = 0;
911 0 TAXSET: for my $taxset (keys %$taxsets) {
912 0 ▶ if ($taxset =~ /^$userset$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($taxset =~ /^$userset$/) 0 0 0
913 0 push(@$sets_to_remove, $taxset);
914 0 $match = 1;
915 0 next TAXSET;
916 }
917 }
918 0 ▶ if ($match == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match == 0) 0 0 0
919 0 warn "<$userset> does not match any sets in this NEXUS file\n";
920 0 $failure = 1;
921 }
922 }
923 } else {
924 0 ▶ for (my $i=0; $i < scalar(@usersets); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @usersets) 0 0 0
925 0 ▶ if ($$taxsets{$usersets[$i]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$taxsets{$usersets[$i]}) { } 0 0 0
926 0 push (@$sets_to_remove, $usersets[$i]);
927 } else {
928 0 warn "<$usersets[$i]> is not the name of a set in this NEXUS file.\n";
929 0 $failure = 1;
930 }
931 }
932 }
933 0 ▶ die "Command aborted\n\n" if $failure == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $failure == 1 0 0 0
934 0 $nexus->get_block('sets')->delete_taxsets(@$sets_to_remove);
935 0 return $nexus;
936 }
937  
938 =head2 swap_children
939  
940 Title : swap_children
941 Usage : NA
942 Function: NA
943 Returns : NA
944 Args : NA
945  
946 =cut
947 0 ▶sub renamesets {
Subroutine renamesets: not called CC=1 SCAR=6.9
Pod documented
948 0 my ($nexus, @old_and_new) = @_;
949 0 my $setsblock = get_setsblock_if_setsblock($nexus);
950 0 $setsblock->rename_taxsets(@old_and_new);
951 0 return $nexus;
952 }
953  
954 =head2 listsets
955  
956 Title : listsets
957 Usage : NA
958 Function: NA
959 Returns : NA
960 Args : NA
961  
962 =cut
963 0 ▶sub listsets {
Subroutine listsets: not called CC=5 SCAR=34.0
Pod documented
964 0 my ($nexus, $outfile, @sets_to_list) = @_;
965 0 my $setsblock = get_setsblock_if_setsblock($nexus);
966 0 ▶ if (@sets_to_list > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sets_to_list > 0) { } 0 0 0
967 0 my $fh = open_filehandle($outfile);
968 0 for (@sets_to_list) {
969 0 my @otus = @{ $setsblock->get_taxset($_) };
970 0 ▶ if (@otus == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@otus == 0) { } 0 0 0
elsif (@otus > 0) { } 0 0 0
971 0 warn("$_ = [] Possible error: Set is empty or does not exist\n\n");
972 } elsif (@otus > 0) {
973 0 print $fh "$_ = [@otus]\n\n";
974 }
975 }
976 } else {
977 0 $setsblock->print_all_taxsets($outfile);
978 }
979 }
980  
981 =head2 listsetnames
982  
983 Title : listsetnames
984 Usage : NA
985 Function: NA
986 Returns : NA
987 Args : NA
988  
989 =cut
990 0 ▶sub listsetnames {
Subroutine listsetnames: not called CC=1 SCAR=6.9
Pod documented
991 0 my ($nexus, $outfile) = @_;
992 0 my $setsblock = get_setsblock_if_setsblock($nexus);
993 0 my @setnames = @{ $setsblock->get_taxset_names() };
994 0 my $fh = open_filehandle($outfile);
995 0 print $fh "@setnames\n";
996 }
997  
998 =head2 open_filehandle
999  
1000 Title : open_filehandle
1001 Usage : NA
1002 Function: NA
1003 Returns : NA
1004 Args : NA
1005  
1006 =cut
1007 0 ▶sub open_filehandle {
Subroutine open_filehandle: not called CC=4 SCAR=30.0
Pod documented
1008 0 my ($fh) = @_;
1009 0 ▶ if ($fh eq "-" || $fh eq \*STDOUT) {return \*STDOUT}
Branch: 1 branch
BranchTrueFalseTotal
if ($fh eq '-' or $fh eq \*STDOUT) { } 0 0 0
Condition0%0/3
$fh eq "-" or $fh eq \*STDOUT
l!l&&r!l&&!r
000
MC/DC0%0/2
$fh eq "-" or $fh eq \*STDOUT
$fh eq "-" $fh eq \*STDOUT
Truth table0%0/3
A or B
A= $fh eq "-"B= $fh eq \*STDOUT
ABresult
1X1
011
000
1010 0 ▶ else {open(FH, ">$fh") || die "Could not open $fh for writing\n\n"}
Branch: 1 branch
BranchTrueFalseTotal
open FH, ">$fh" or die "Could not open $fh for writing\n\n" 0 0 0
1011 0 return \*FH;
1012 }
1013  
1014 =head2 get_setsblock_if_setsblock
1015  
1016 Title : get_setsblock_if_setsblock
1017 Usage : NA
1018 Function: NA
1019 Returns : NA
1020 Args : NA
1021  
1022 =cut
1023 0 ▶sub get_setsblock_if_setsblock {
Subroutine get_setsblock_if_setsblock: not called CC=2 SCAR=17.9
Pod documented
1024 0 my ($nexus) = @_;
1025 0 my $setsblock;
1026 0 ▶ unless ($setsblock = $nexus->get_block('sets')){die "This NEXUS file does not contain a SETS Block\n\n"}
Branch: 1 branch
BranchTrueFalseTotal
unless ($setsblock = $nexus->get_block("sets")) 0 0 0
1027 0 return $setsblock;
1028 }
1029  
1030 =head2 file_overwrite_warning
1031  
1032 Title : file_overwrite_warning
1033 Usage : NA
1034 Function: NA
1035 Returns : NA
1036 Args : NA
1037  
1038 =cut
1039 0 ▶sub file_overwrite_warning {
Subroutine file_overwrite_warning: not called CC=4 SCAR=30.0
Pod documented
1040 0 my ($infile, $outfile, $command, $silentmode) = @_;
1041 0 ▶ if ($silentmode == 1) {return 1}
Branch: 1 branch
BranchTrueFalseTotal
if ($silentmode == 1) 0 0 0
1042 0 ▶ if ($infile eq $outfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($infile eq $outfile) { } 0 0 0
1043 0 warn("Do you really want to overwrite NEXUS file $infile with the $command output?\n");
1044 0 ▶ if (<STDIN> =~ /y/) {
Branch: 1 branch
BranchTrueFalseTotal
if (readline(STDIN) =~ /y/) { } 0 0 0
1045 0 return 1;
1046 0 } else { die("Command aborted\n\n")}
1047 0 } else { return 1;}
1048 }
1049  
1050 ################# POD Documentation ##################
1051  
1052 __END__
« lib/Bio/NEXUS/Tools/NexPlotter.pm ↑ Summary lib/Bio/NEXUS/DistancesBlock.pm »