Skip to content

Instantly share code, notes, and snippets.

@mtholder
mtholder / balanced_tree.py
Created November 2, 2010 19:11
produces a balanced tree with 2^{$1} leaves when $1 is the first command line argument
#!/usr/bin/env python
def balanced_tree(n, level):
if level == 0:
return n + 1, 't%d' % n
n, leftsub = balanced_tree(n, level - 1)
n, rightsub = balanced_tree(n, level - 1)
return n, '(%s,%s)' % (leftsub, rightsub)
if __name__ == '__main__':
import sys
@mtholder
mtholder / alter_edge_lengths.py
Created June 6, 2011 15:52
Uses dendropy to traverse a tree and modifies the length of each edge
#!/usr/bin/env python
import sys
import dendropy
if __name__ == '__main__':
from optparse import OptionParser
from optparse import OptionGroup
import sys, os
_script_name = os.path.split(sys.argv[0])[1]
@mtholder
mtholder / conflict_viz.py
Created June 7, 2011 20:26
Uses dendropy to calculate a similarity score for the trees in analyses based on split support
import sys
import dendropy
from copy import copy
threshold = float(sys.argv[1])
filename_list = sys.argv[2:]
SCRIPT_NAME = 'conflict_viz'
def debug(*valist):
msg = ' '.join([str(i) for i in valist])
sys.stderr.write(SCRIPT_NAME + ': ' + msg + '\n')
@mtholder
mtholder / terminal_edges_to_dot_p.py
Created June 15, 2011 15:58
Takes a MrBayes .t file and writes (to standard out) a .p file with the parameters being the edge lengths for each of the Terminal edges.
#!/usr/bin/env python
'''Takes a MrBayes .t file and writes (to standard out) a .p file with
the parameters being the edge lengths for each of the Terminal edges.
This .p file can be used with Tracer http://tree.bio.ed.ac.uk/software/tracer/
or other MCMC diagnostics files.
Example invocation with redirection to a file called term_edges_myrun.p :
python terminal_edges_to_dot_p.py myrun.t > term_edges_myrun.p
@mtholder
mtholder / download-and-run-geodispersal.sh
Created April 14, 2012 23:10
Install of Dendropy with test run of modified BPA using geodispersal-analysis.py
#!/bin/sh
set -x
wget http://pypi.python.org/packages/source/D/DendroPy/DendroPy-3.11.0.tar.gz
tar xfvz DendroPy-3.11.0.tar.gz
cd DendroPy-3.11.0
python setup.py install
cd extras/geodispersal/
python geodispersal-analysis.py LiebermanEBasidechenella.nex LiebermanEBasidechenella.nex --labels=labels.txt --paup --vicariance=vic --dispersal=disp
cat Readme.txt
@mtholder
mtholder / log_of_mean_exp.py
Created May 29, 2012 16:19
Log of the mean of the exp of values from standard input
#!/usr/bin/env python
import sys, math
v_list = [float(value) for value in sys.stdin]
offset = max(v_list)
n = len(v_list)
sum_exps = sum([math.exp(v - offset) for v in v_list])
mean_exps = sum_exps/n
sys.stdout.write('Log of mean of the exp of %d values is:\n' % n)
sys.stdout.write('%8.7f\n' % (math.log(mean_exps) + offset))
@mtholder
mtholder / build_gcc_4.7_on_mac.sh
Created June 10, 2012 19:04
Script to download and build gcc 4.7 and its dependencies on mac - it took about 40 minutes on my machine (and the "make check" in the gcc build failed for lack of an "autogen" script"), but the compiler seems to work.
#!/bin/sh
# You might want to modify the first line to specify your own install location.
# In theory the rest should not need tweaking...
export GCC_PREFIX="$HOME/gcc4.7"
# Hopefully, you can tweak these as they get out of date, but the download URL's
# may not be stable to text substitution.
GMP_DOWNLOAD_VERSION=gmp-5.0.5
MPFR_DOWNLOAD_VERSION=mpfr-3.1.0
MPC_DOWNLOAD_VERSION=mpc-0.8.2
@mtholder
mtholder / phylotastic_tnrs_client.py
Created June 21, 2012 18:49
Example of a client of the demo of the TNRS API described at: http://www.evoio.org/wiki/Phylotastic/TNRS
#!/usr/bin/env python
'''
Example of a client of the demo of the TNRS API described at:
http://www.evoio.org/wiki/Phylotastic/TNRS
Reads names (separated by newline characters) from file names passed in as
command-line arguments (or reads name from standard input if no arguments
are given).
Outputs tab-delimited summary of the matches for each query sorted by score of
@mtholder
mtholder / range_of_treebase2newick.sh
Created August 25, 2012 06:47
shell script that uses NCL to convert a series of sequentially numbered TreeBase NEXUS files to newick. NCL is available from http://sourceforge.net/projects/ncl/
#!/bin/sh
# Script to use NCLconverter to convert TreeBase NEXUS files to newick
# Invocation:
# sh range_of_treebase2newick.sh 10000 10100
# in a directory with filenames like S10001.nex to convert each file
# name like S10000.nex up to file S10100.nex to newick.
# you should get files like:
# outS####.tre newick tree for the first trees block
# 2outS####.tre , 3outS####.tre , etc if the file has multiple output blocks
# outS####.NameTranslationFile.txt is an ad hoc xml description of the name mapping
@mtholder
mtholder / newick2taxalist.cpp
Created August 26, 2012 04:25
Reads a phylogenetic tree or data file and prints out the taxa labels one per line
// Copyright (C) 2012 Mark T. Holder
//
// Based on example/splitsinfile/splitsinfile.cpp in NEXUS class library
//
// After NCL has been installed at ${NCL_PREFIX}
//
// g++ newick2taxalist.cpp -o newick2taxalist -I "${NCL_PREFIX}/include" -lncl -L "${NCL_PREFIX}/lib/ncl"
//
// Takes a newick file and prints out the taxa labels, one per line.
//