Skip to content

Instantly share code, notes, and snippets.

#! /usr/bin/env python
import dendropy
def get_tree(r):
tree_str = "((C:1.3,D:4.)34:0.034,(A:1.1,(B:1.2,X:1.6)26:0.026)12:0.0126,E:1.5);"
tree = dendropy.Tree.get(
data=tree_str,
schema="newick",
rooting=r,
@mtholder
mtholder / bootstrap-propinquity.sh
Created March 29, 2016 01:36
checkout the open tree of life repos used to build the synthetic tree of life - assumes that you have python 2.7, g++ and devtools for each language
#!/bin/bash
echo 'This script installs the tools needed for propinquity as subdirectories'
echo 'of the working directory. This assumes that you have a C++ dev environment (g++, boost, automake, libtool)'
echo 'installed. See README files for propinquity and otcetera if those builds fail'
echo 'Also assumes virtualenv for python'
CONTEXT_DIR="${PWD}"
virtualenv propinquity-env
source propinquity-env/bin/activate
@mtholder
mtholder / normalize-and-partition-by-num-states-obs.py
Last active October 26, 2015 20:53
Takes a NEXUS file and writes SETS block with CharSet statements that partition the characters by the number of states
#!/usr/bin/env python
from collections import defaultdict
import dendropy
import sys
import os
_SCRIPT_NAME = os.path.split(sys.argv[0])[-1]
def error(msg):
sys.stderr.write('{}: Error: {}\n'.format(_SCRIPT_NAME, msg))
OUT = sys.stdout
@mtholder
mtholder / oti_responses_to_dict.py
Created September 21, 2015 16:34
after filling a directory from running a lot of curl calls generated by https://gist.github.com/mtholder/ddae56851edd81180767 run this giving it the path to the dir and an output filename
#!/usr/bin/env python
import sys
import os
import json
import codecs
dn = sys.argv[1]
outfn = sys.argv[2]
d = {}
for s in os.listdir(dn):
if s.endswith('.out'):
@mtholder
mtholder / gen-curl-oti.py
Created September 21, 2015 16:27
pull study info from OTI
#!/usr/bin/env
import sys
'''
python gen-curl-oti.py template.txt studies.txt
where studies.txt is the list of studies used in synthesis.
and template is a cURL call with XYZ inplace of the study id (and no other XYZ)
cURL
'''
@mtholder
mtholder / paste-in-js-css.py
Created September 20, 2015 18:43
copy opentree static CSS and JS files into a file - but forget to deal with relative paths changing...
#!/usr/bin/env python
import sys
import re
inp = sys.stdin
outp = sys.stdout
JS_PAT = re.compile(r'^<script src="/opentree/static/(.+)" type="text/javascript"></script>\s+$')
CSS_PAT = re.compile(r'^<link href="/opentree/static/(.+)" rel="stylesheet" type="text/css" />\s+$')
@mtholder
mtholder / tree-covers-taxa.py
Last active August 29, 2015 14:22
used dendropy4 to assure tree and matrix have the same taxon set.
#!/usr/bin/env python
from dendropy import DnaCharacterMatrix, Tree
import sys
d = DnaCharacterMatrix.get(path=sys.argv[1],
schema='fasta')
# make the taxon_namespace immutable, so the tree does not add
# new labels...
d.taxon_namespace.is_mutable = False
tree = Tree.get(path=sys.argv[2],
schema='newick',
@mtholder
mtholder / indent-newick-frag.py
Last active August 29, 2015 14:16
indent a fragment of a newick to make it easier to grab a subtree - does not correctly parse labels (so quoted parens and commas cause problems)
#!/usr/bin/env python
from StringIO import StringIO
import sys
out = sys.stdout
def get_indented(x, indent):
start_indent = indent
written_something = False
out = StringIO()
for letter in x:
@mtholder
mtholder / acc-gi-organism.c
Last active August 29, 2015 14:13
a GBParsy-based program that takes a GenBank flat file and writes to standard out one line per record containing: Accession.Version TAB gi TAB Organism
/* Most of this code is from GBParsy which available from https://code.google.com/p/gbfp/
T.-H. Lee, Y.-K. Kim and B.H. Nahm (2008) GBParsy: A GenBank flatfile parser library with high speed. BMC Bioinformatics, 9:321.
That code is released under the GPL (see bottom of file).
Mark T. Holder only wrote the slight modification to parsing of the GI and a simpliefied main
function (based on the example seqext.c from gbfpy)
@mtholder
mtholder / chars_in_file.py
Created November 28, 2014 12:48
chars_in_file.py takes a list of filepaths (to files encoded using utf-8). prints a list of the characters encountered.
#!/usr/bin/env python
import codecs
import sys
chars = set()
for filepath in sys.argv[1:]:
with codecs.open(filepath, 'r', encoding='utf-8') as fo:
for line in fo:
chars.update(iter(line))
c = list(chars)
c.sort()