-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_datamonkey.pl
More file actions
executable file
·133 lines (119 loc) · 2.85 KB
/
Copy pathparse_datamonkey.pl
File metadata and controls
executable file
·133 lines (119 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Bio::Phylo::IO qw'parse parse_tree';
use Bio::Phylo::Util::Logger ':levels';
use Bio::Phylo::Util::CONSTANT ':objecttypes';
# process command line arguments
my $verbosity = WARN;
my ( $nexus, $csv );
GetOptions(
'verbose+' => \$verbosity,
'nexus=s' => \$nexus,
'csv=s' => \$csv,
);
# instantiate helper objects
my $ns = 'http://www.hyphy.org/terms#';
my $log = Bio::Phylo::Util::Logger->new(
'-level' => $verbosity,
'-class' => 'main',
);
# read CSV
my %csv;
{
my @header;
open my $fh, '<', $csv or die $!;
LINE: while(<$fh>) {
chomp;
# parse the header
if ( not @header ) {
@header = split /,/, $_;
s/[ \-]/_/g for @header;
shift @header;
next LINE;
}
# parse the record
my @record = split /,/, $_;
my $id = shift @record;
my %r;
$r{$header[$_]} = $record[$_] for 0 .. $#record;
$csv{$id} = \%r;
}
$log->info("parsed CSV file $csv");
}
# read hyphy block
my ( $mixtree, $nexusdata );
{
open my $fh, '<', $nexus or die $!;
LINE: while(<$fh>) {
s/'//g;
$nexusdata .= $_;
# parse the mixtureTree statement, which we need for the node labels
if ( /Tree mixtureTree=(.+)/ ) {
my $newick = $1;
$newick =~ s/{.+?}//g;
$log->info("found mixtureTree in HyPhy block");
$mixtree = parse_tree(
'-format' => 'newick',
'-string' => $newick,
);
last LINE;
}
}
}
# parse nexus data
my $project = parse(
'-format' => 'nexus',
'-string' => $nexusdata,
'-as_project' => 1,
);
$log->info("parsed nexus file $nexus");
# node indexer function
my %lookup;
sub indexer {
my $node = shift;
my @children = @{ $node->get_children };
if ( @children ) {
my @tips = sort { $a cmp $b } map { @{ $_->get_generic('tips') } } @children;
my $key = join ',', @tips;
$lookup{$key} = [] if not $lookup{$key};
push @{ $lookup{$key} }, $node;
$node->set_generic( 'tips' => \@tips );
$node->set_generic( 'key' => $key );
}
else {
$node->set_generic( 'tips' => [ $node->get_name ] );
}
}
# index the trees
my ($orgtree) = @{ $project->get_items(_TREE_) };
$orgtree->visit_depth_first( '-post' => \&indexer );
$mixtree->visit_depth_first( '-post' => \&indexer );
$log->info("done indexing nodes");
# apply csv annotations
$project->set_namespaces( 'hyphy' => $ns );
$orgtree->visit(sub{
my $node = shift;
my $name;
# copy node name over if internal
if ( $node->is_internal ) {
my $key = $node->get_generic('key');
my ( $org, $mix ) = @{ $lookup{$key} };
$name = $mix->get_name;
$node->set_name( $name );
}
else {
$name = $node->get_name;
}
# copy annotations
if ( $csv{$name} ) {
my %anno = %{ $csv{$name} };
for my $predicate ( keys %anno ) {
$node->set_meta_object( "hyphy:$predicate" => $anno{$predicate} );
}
}
$log->info("copied annotations for name $name");
});
# print output
print $project->to_xml( '-compact' => 1 );