Phylo Gram Perl Module

From Biowiki
Revision as of 23:43, 1 January 2017 by Move page script (talk | contribs) (Move page script moved page PhyloGramPerlModule to Phylo Gram Perl Module: Rename from TWiki to MediaWiki style)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

PhyloGram perl module

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 [[Phylo Gram]];
$g=PhyloGram->from_file(shift);  # load file
foreach$chain($g->all_chains){  # loop over chains, using [[Phylo Gram]] method "all_chains" (which returns a list of objects of type [[Phylo Gram]]::Chain)
 print$chain->terminal->to_string,"\n";  # print out the terminals, using [[Dart Sexpr]]'s AUTOLOADed methods to find the child node of "chain" named "terminal" ("to_string" is a built-in [[Dart Sexpr]] method)
 $mutateHash=$chain->mutate_hash;  # for fast indexing, it's necessary to make a lookup table of the rate matrix. The [[Phylo Gram]]::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 [[Dart Sexpr]] 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 [[Phylo Gram]]::Chain
  }
  print"\n"
 }
 print"-"x80,"\n"
}

-- Ian Holmes - 13 Apr 2006

Some more examples...

This one prints out the expected substitution rate of every chain in a grammar:

perl -e 'use [[Phylo Gram]];$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 [[Phylo Gram]];$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