Skip to content

Commit

Permalink
Option to ignore leaves with missing trait value when doing add_trait()
Browse files Browse the repository at this point in the history
  • Loading branch information
fangly committed May 21, 2012
1 parent f290574 commit 8131205
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
33 changes: 18 additions & 15 deletions Bio/Tree/TreeFunctionsI.pm
Expand Up @@ -1133,12 +1133,14 @@ sub move_id_to_bootstrap{
are added to leaf nodes as a tag named $key using the add_tag_value()
method. This means that you can retrieve the trait values using the
get_tag_values() method (see the documentation for Bio::Tree::Node).
If a leaf node has no value in the trait file, an exception is thrown.
Returns : Trait name (a scalar) on success, undef on failure (for example, if
the column index requested was too large).
Args : Name of trait file (scalar string)
Index of trait file column (scalar int). Note that numbering starts
at 0. Default: 1 (second column)
Args : * Name of trait file (scalar string).
* Index of trait file column (scalar int). Note that numbering starts
at 0. Default: 1 (second column).
* Ignore missing values. Typically, if a leaf node has no value in
the trait file, an exception is thrown. If you set this option to
1, then no trait will be given to the node (no exception thrown).
=cut

Expand All @@ -1148,43 +1150,44 @@ sub _read_trait_file {
my $column = shift || 1;

my $trait_name;
my $traits;
my $trait_values;
open my $TRAIT, "<", $file or $self->("Could not open file $file: $!\n");

my $first_line = 1;
while (<$TRAIT>) {
chomp;
s/['"]//g;
my @line = split;
my @line = split /\t/;
if ($first_line) {
$first_line = 0;
$trait_name = $line[$column];
next;
}
last unless $line[0];
$traits->{$line[0]} = $line[$column];
$trait_values->{$line[0]} = $line[$column];
}

close $TRAIT;
return $trait_name, $traits;
return $trait_name, $trait_values;
}

sub add_trait {
my $self = shift;
my $file = shift;
my $column = shift;
my ($self, $file, $column, $ignore) = @_;
$ignore = 0 if not defined $ignore;

my ($trait_name, $traits) = $self->_read_trait_file($file, $column);
my ($trait_name, $trait_values) = $self->_read_trait_file($file, $column);

if (defined $trait_name) {
for my $node ($self->get_leaf_nodes) {
# strip quotes from the node id
$node->id($1) if $node->id =~ /^['"]+(.*)['"]+$/;
eval {
$node->verbose(2);
$node->add_tag_value($trait_name, $traits->{ $node->id } );
$node->add_tag_value($trait_name, $trait_values->{ $node->id } );
};
$self->throw("No trait for node [".$node->id."/".$node->internal_id."]")
if $@;
if ($@ && (not $ignore)) {
$self->throw("No trait for node [".$node->id."/".$node->internal_id."]")
}
}
}
return $trait_name;
Expand Down
5 changes: 4 additions & 1 deletion t/Tree/TreeStatistics.t
Expand Up @@ -7,7 +7,7 @@ BEGIN {
use lib '.';
use Bio::Root::Test;

test_begin(-tests => 41);
test_begin(-tests => 42);

use_ok('Bio::TreeIO');
use_ok('Bio::Tree::Statistics');
Expand All @@ -29,6 +29,9 @@ is $stats->cherries($tree, $node), 4, 'cherries';
my $key = $tree->add_trait(test_input_file('traits.tab'), 4);
is $key, undef, 'read traits'; # exceeded column number

$key = $tree->add_trait(test_input_file('traits.tab'), 2, 1);
is $key, 'disp'; # one leaf has a missing trait value, but ignore it

$key = $tree->add_trait(test_input_file('traits.tab'), 3);
is $key, 'intermediate';

Expand Down
34 changes: 17 additions & 17 deletions t/data/traits.tab
@@ -1,17 +1,17 @@
"id" "assoc" "disp" "intermediate"
1 "blue" "blue" "red"
2 "blue" "red" "blue"
3 "blue" "blue" "blue"
4 "blue" "red" "blue"
5 "blue" "blue" "red"
6 "blue" "red" "blue"
7 "blue" "blue" "blue"
8 "blue" "red" "blue"
9 "red" "blue" "red"
10 "red" "red" "red"
11 "red" "blue" "blue"
12 "red" "red" "blue"
13 "red" "blue" "red"
14 "red" "red" "red"
15 "red" "blue" "red"
16 "red" "red" "red"
"id" "assoc" disp "intermediate"
1 "blue" blue "red"
2 "blue" "blue"
3 "blue" blue "blue"
4 "blue" red "blue"
5 "blue" blue "red"
6 "blue" red "blue"
7 "blue" blue "blue"
8 "blue" red "blue"
9 "red" blue "red"
10 "red" red "red"
11 "red" blue "blue"
12 "red" red "blue"
13 "red" blue "red"
14 "red" red "red"
15 "red" blue "red"
16 "red" red "red"

0 comments on commit 8131205

Please sign in to comment.