Skip to content

Instantly share code, notes, and snippets.

@mtholder
mtholder / suppress-non-listed-ids-or-unnamed.py
Created December 28, 2019 19:14
suppresses nodes from an OT tree if they do not have an ID listed in a separate file. Takes newick-tree-filepath and file-with-ids-one-per-line as args
#!/bin/env python3
import sys
import dendropy
import re
id_extractor = re.compile('.*ott([0-9]+)$')
tree_filepath = sys.argv[1]
id_filepath = sys.argv[2]
tree = dendropy.Tree.get(path=tree_filepath, schema='newick', suppress_internal_node_taxa=False)
# Get the list of IDs to retain
@mtholder
mtholder / ott_ids_for_nexson.sh
Created December 12, 2019 00:08
takes a path to an Open Tree NexSON. writes a element of python dict mapping the filename to a frozenset of mapped OTT Ids
#!/bin/bash
fp=$1
fn=$(basename $fp)
if ! test -f $fp ; then
echo "$fp does not exist"
exit 1
fi
o=`grep '"^ot:ottId": ' "$fp" | sed 's/".*": //' | sort -g | sed ':a;N;$!ba;s/\n/ /g' | sed -E 's/ +/ /g'`
echo "\"${fn}\": frozenset([ ${o} ]),"
@mtholder
mtholder / construct_c_trie.py
Created June 7, 2019 22:55
start of generating a C-trie in python
#!/usr/bin/env python3
# -*- coding: <encoding name> -*-
from __future__ import print_function
from collections import defaultdict
from math import log
import codecs
from ctypes import c_uint64 as c_trie_node_type
import sys
""" Pseudocode from Maly, 1976 Appendix B
@mtholder
mtholder / crawl.py
Last active October 19, 2018 20:40 — forked from bredelings/crawl.py
Silly web crawler
#!/usr/bin/python3
import requests
import time
from threading import Thread
machine='http://localhost:1984'
# machine='https://ot39.opentreeoflife.org'
# machine='https://api.opentreeoflife.org'
@mtholder
mtholder / crontest.sh
Created August 9, 2018 18:22
cron-executable script (no output except in case of failure) for testing Open Tree services
#!/bin/bash
PAR_DIR=/home/mholder
TEST_OT_DIR="${PAR_DIR}/test-ot-ws"
TEST_LOG_DIR="${PAR_DIR}/testotlogs"
source "${PAR_DIR}/env/test-otc-ws/bin/activate" || exit
source "${TEST_OT_DIR}/dev/activate.sh" || exit
cd "${TEST_OT_DIR}" || exit
date > "${TEST_LOG_DIR}/latest.log"
nf=0
if ! git pull origin >> "${TEST_LOG_DIR}/latest.log" 2>&1 ; then
@mtholder
mtholder / brent_opt.py
Created April 21, 2018 13:40
simple demo of using bracket and brent to optimize a single-variable function
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from scipy.optimize import bracket, brent
from scipy.stats import binom
from math import log, isnan
import sys
_inf = float('inf')
def main():
n = 100
@mtholder
mtholder / scrape_rb_tutorial_download_as_archive.py
Created March 2, 2018 23:21
brittle scrape of download archive from rb tutorial
#!/usr/bin/env python
from __future__ import print_function
import sys
try:
from selenium import webdriver
except:
sys.exit('Need to run:\npip install selenium\n')
import tempfile
import time
import os
@mtholder
mtholder / buneman_four_point_condition_tree.py
Last active October 25, 2017 15:55
takes 6 (non-negative) distances as arguments (in the order [d12, d13, d14, d23, d24, d34]) and returns tree (with branch lengths if the four-point condition is met).
#!/usr/bin/env python
from __future__ import print_function
import sys
try:
farg = [float(i) for i in sys.argv[1:]]
for i in farg:
assert i >= 0.0
d_12, d_13, d_14, d_23, d_24, d_34 = farg
except:
m = 'd_12, d_13, d_14, d_23, d_24, d_34'
@mtholder
mtholder / tnt2nexus.py
Created July 6, 2017 02:47
brittle script for converting a TNT output to NEXUS trees block. I'm not sure what the TNT name quoting rules are, so it reads the first ws delim word in the xread block to get the names
#!/usr/bin/env python
import sys
import os
import re
tread_pat = re.compile('^tread')
xread_pat = re.compile('^xread')
treefilename = sys.argv[1]
ext_data_pref = "tread 'tree(s) from TNT, for data in "
import sys
fn = sys.argv[1]
ls_list = []
mapping = {}
with open(fn, 'r') as inp:
for n, line in enumerate(inp):
ls = line.split('\t|\t')
assert len(ls) == 5
assert ls[-1] == '\n'
if n > 0: