Skip to content

Instantly share code, notes, and snippets.

@hlapp
Created March 8, 2012 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hlapp/2003096 to your computer and use it in GitHub Desktop.
Save hlapp/2003096 to your computer and use it in GitHub Desktop.
Test-driven development example for BabelPhysh PhyloSoC project idea
#!/usr/bin/perl
use strict;
use warnings;
use Bio::NEXUS;
use Bio::TreeIO;
use Bio::AlignIO;
use Bio::Phylo::IO;
use Bio::Phylo::Factory;
use Bio::Phylo::Forest::Tree;
#################################################################################
# THIS CODE SHOULD WORK IF Bio::NEXUS IMPLEMENTS BioPerl INTERFACES
# read trees with Bio::NEXUS, write with BioPerl
my $treeio = Bio::TreeIO->new( '-format' => 'nhx', '-file' => 'outfile.dnd' );
my $nexus = Bio::NEXUS->new('filename.nex');
for my $treeblock ( $nexus->get_blocks('trees') ) {
for my $tree ( $treeblock->get_trees ) {
$treeio->write_tree($tree);
}
}
# read matrices with Bio::NEXUS, write with BioPerl
my $alignio = Bio::AlignIO->new( '-format' => 'fasta', '-file' => 'outfile.fa' );
for my $matrix ( $nexus->get_blocks('characters') ) {
$alignio->write_aln($matrix);
}
# populate Bio::Phylo project, print to XML
my $fac = Bio::Phylo::Factory->new;
my $proj = $fac->create_project;
my ( @taxa, @forests, @matrices );
for my $treeblock ( $nexus->get_blocks('trees') ) {
my $forest = $fac->create_forest;
for my $tree ( $treeblock->get_trees ) {
$forest->insert( Bio::Phylo::Forest::Tree->new_from_bioperl($tree) );
}
push @taxa, $forest->make_taxa;
push @forests, $forest;
$proj->insert($forest);
}
for my $characters ( $nexus->get_blocks('characters') ) {
my $matrix = Bio::Phylo::Matrices::Matrix->new_from_bioperl($characters);
push @taxa, $matrix->make_taxa;
push @matrices, $matrix;
$proj->insert($matrix);
}
for my $i ( 1 .. $#taxa ) {
$taxa[0]->merge_by_name($taxa[$i]);
}
for my $block ( @matrices, @forests ) {
$block->set_taxa($taxa[0]);
}
$proj->insert($taxa[0]);
print $proj->to_xml;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment