Browse the source: DartPerl:PhyloGram.pm, DartPerl:DartSexpr.pm
The PhyloGram.pm perl module is a Perl class for constructing, parsing and accessing
phylo grammars expressed in Dart's xgram format.
PhyloGram.pm is a subclass of DartSexpr.pm,
a Perl module for parsing S-expressions that uses AUTOLOAD'ing for smooth access
and is loosely based on Chris Mungall's Data::Stag.
Examples
There are several one-line examples at the beginning of the source file.
Here is one of those examples, broken down:
use PhyloGram;
$g=PhyloGram->from_file(shift); # load file
foreach$chain($g->all_chains){ # loop over chains, using PhyloGram method "all_chains" (which returns a list of objects of type PhyloGram::Chain)
print$chain->terminal->to_string,"\n"; # print out the terminals, using DartSexpr's AUTOLOADed methods to find the child node of "chain" named "terminal" ("to_string" is a built-in DartSexpr method)
$mutateHash=$chain->mutate_hash; # for fast indexing, it's necessary to make a lookup table of the rate matrix. The PhyloGram::Chain method "mutate_hash" does this
@states=map(join("",@{$_->state->value}),$chain->find_all("initial")); # get a list of states by extracting the "state" field of all child nodes named "initial" ("find_all" and "value" are DartSexpr methods)
print"* @states\n";
foreach$i(@states){
print$i;
foreach$j(@states){
print" ",$i eq$j?"*":$chain->mutate($i,$j,$mutateHash)->rate->value # note how $mutateHash is passed into "mutate", which is a special method of PhyloGram::Chain
}
print"\n"
}
print"-"x80,"\n"
}
-- IanHolmes - 13 Apr 2006
Some more examples...
This one prints out the expected substitution rate of every chain in a grammar:
perl -e 'use PhyloGram;$g=PhyloGram->from_file(shift);foreach$chain($g->all_chains){print$chain->terminal->to_string,"\n";
$mutateHash=$chain->mutate_hash;@states=map(join("",@{$_->state->value}),$chain->find_all("initial"));print"States: @states\n";
$total=0;foreach$i(@states){foreach$j(@states){if($i ne $j){$m=$chain->mutate($i,$j,$mutateHash);if(defined $m){
$total+=$chain->initial($i)->prob->value * $m->rate->value}}}}print"Expected mutation rate: $total\n","-"x80,"\n"}' tr02.eg
This one re-scales all the rates in a grammar (in this case "01/tr01.eg") by a constant multiplicative factor (in this case 100):
perl -e 'use PhyloGram;$g=PhyloGram->from_file(shift);$mul=shift;foreach$chain($g->all_chains){
$mutateHash=$chain->mutate_hash;@states=map(join("",@{$_->state->value}),$chain->find_all("initial"));
foreach$i(@states){foreach$j(@states){if($i ne $j){$m=$chain->mutate($i,$j,$mutateHash);if(defined $m){
$m->rate->value ($m->rate->value * $mul)}}}}}print $g->to_string' 01/tr01.eg 100
|