Skip to content

Instantly share code, notes, and snippets.

@mtholder
mtholder / num-studies-in-phylesystem.bash
Created May 2, 2017 14:09
get the unix timestamps (seconds since start of epoch) for every commit in phylesystem-1 that changes the # of studies
#!/bin/bash
git rev-list HEAD > commits.txt
prev_ns=0
for sha in $(cat commits.txt)
do
unix_time=$(git show -s --format=%at "${sha}")
git checkout $sha || exit
num_studies=$(find study -name "*.json" | wc -l)
if test $num_studies -ne $prev_ns
then
#!/usr/bin/env python
import sys
import json
import datetime
synth_j, unix_ts_and_num_studies = sys.argv[1:]
ts_ns = []
with open(unix_ts_and_num_studies, 'r') as inp:
for line in inp:
ls = line.strip().split()
if ls:
d = read.table('times_num_studies.tsv', header=TRUE, sep="\t");
print(d);
dt = as.POSIXct(d$time, origin="1970-01-01");
print(dt);
pdf("ot-time-series.pdf");
plot(dt, d$num_phyle_studies,
xlab="Date", ylab="num studies",
ylim=c(0, max(d$num_phyle_studies)),
type="l",
@mtholder
mtholder / count_species_rank_binomen.sh
Created May 6, 2017 14:34
count_species_rank_binomen.sh from binomen
#!/bin/bash
OTT_DIR=$1
ROOT_TAXON=$2
echo "Extracting taxa that are descendants of ${ROOT_TAXON}"
otc-taxonomy-parser \
$OTT_DIR \
--format="%R | %N |" \
-r $ROOT_TAXON \
--cull-flags "major_rank_conflict,major_rank_conflict_inherited,environmental,viral,barren,not_otu,hidden,was_container,inconsistent,hybrid,merged" \
@mtholder
mtholder / count_species_rank_binomen.py
Created May 6, 2017 15:43
count species ranked taxa in OTT with two word names from the output of the otcetera command in https://gist.github.com/mtholder/0ab3ef34ac96d4eec38fcfbefd4e66cc
#!/usr/bin/env python
import sys
inp = sys.stdin
count = 0
for line in inp:
ls = [i.strip() for i in line.split(' |')]
if ls:
assert len(ls) == 3 and ls[2] == ''
rank = ls[0]
if rank == 'species':
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:
@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 "
@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 / 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 / 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