Skip to content

Instantly share code, notes, and snippets.

@dwinter
Created May 30, 2012 05:27
Show Gist options
  • Save dwinter/2833941 to your computer and use it in GitHub Desktop.
Save dwinter/2833941 to your computer and use it in GitHub Desktop.
Summarise Beast output from the command line
"""
Summarise a variable in a BEAST logfile
arguments:
-h, --help show this help message and exit
-f or --file string whats the name of the BEAST file
-v or --variable string value you want to summarise
-b or --burnin int number of states to ignore
-g export graphs to summarise the sample?
-l export a logfile for just this variable
If you don't provide a variable name, you'll get a list of names to choose form
Usage (summarise 'clock.rate' in a file called 'combined.log'):
$python parseBeast.py -f combined.log -v likelihood -b 100 -g
number variable
0 state
1 posterior
.
.
.
11 clock.rate
.
.
what is the number of the variable you want to summarise? 11
mean value: 1.0, sd: 0.0
making graphs...
...saved to trace.png and hist.png
"""
import os
import argparse
import sys
import numpy as np
from matplotlib import pyplot
class SummariseBeast:
def __init__(self, filename, variable, burnin, graph, log):
self.filename = filename
self.handle = open(filename, 'r')
self.variable = variable
self.burnin = burnin
self.graph = graph
self.log = log
self.header = self._get_header()
def _get_header(self):
"""Get variable names from header"""
for line in self.handle:
#first non-comment line has header
if not line.strip().startswith("#"):
return line.split()
def _interact(self):
"""Let user pick variable to sumarise from list"""
print "number\tvariable"
for tup in enumerate(self.header):
print "%i\t%s" % tup
self.col = input(
'what is the number of the variable you want to summarise? ')
self.variable = self.header[self.col]
def _find_col(self):
"""Get the column number for a sepcified value"""
self.col = self.header.index(self.variable)
def summarise(self, write=False, graph=False):
"""
Summarise the variable
returns mean, sd and (optionally) graphs and a log
"""
for i in range(self.burnin):
#throw out the burn-in
_ = self.handle.next()
array = np.array([ float(line.split()[self.col]) for line in self.handle ])
mean = np.mean(array)
sd = np.std(array)
print 'mean value: %s, sd: %s' % (mean, sd)
if self.graph:
print "making graphs..."
pyplot.hist(array, 100)
pyplot.savefig('hist.png', format='png')
pyplot.plot(array, lw=0.25)
pyplot.savefig('trace.png', format='png')
print "...saved to trace.png and hist.png"
if self.log:
print 'writting log file...'
outfile = '%s_%s.log' % (self.filename.split('.')[0], self.variable)
out = open(outfile, 'w')
for value in array:
out.write(str(value)+'\n')
print 'wrote to ' + outfile
def run(self):
""""Uses functions above to do whole analysis """
if self.variable:
self._find_col()
else:
self._interact()
self.summarise()
def main():
parser = argparse.ArgumentParser(description='Summarise a variable in a BEAST logfile')
parser.add_argument('-f', '--file', metavar='string',
help='whats the name of the BEAST file')
parser.add_argument('-v', '--variable', metavar='string',
help= 'value you want to summarise', default=None)
parser.add_argument('-b','--burnin', metavar='int', type=int,
help = 'number of states to ignore')
parser.add_argument('-g', action='store_true', dest='graph', default = False,
help = 'export graphs to summarise the sample?')
parser.add_argument('-l', action='store_true', dest='log', default = False,
help = 'export a logfile for just this variable')
args = parser.parse_args()
#OK arguments dealt with... run it!
SummariseBeast(
args.file, args.variable, args.burnin, args.graph, args.log).run()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment