Skip to content

Instantly share code, notes, and snippets.

@nhoffman
nhoffman / test_cast.py
Last active August 29, 2015 13:57
Relative speed of various methods of coercion from int to real in sqlite3
#!/usr/bin/env python
import sqlite3
from random import choice
from timeit import timeit
from itertools import chain
con = sqlite3.connect('test_cast.db', isolation_level=None) # autocommit
cur = con.cursor()
@nhoffman
nhoffman / build_wheels.py
Last active August 29, 2015 13:59
Create python wheels
#!/bin/bash
set -e
REQFILE="${1-requirements.txt}"
WHEELSTREET="${2-wheelstreet}" # base dir for wheel dirs
if [[ ! -f "$REQFILE" ]]; then
echo "Cannot find requirements file named $REQFILE"
echo "Usage: $(basename $0) [requirements.txt] [wheelstreet]"
@nhoffman
nhoffman / script.R
Created April 24, 2014 19:11
An R script template
#!/usr/bin/env Rscript
suppressPackageStartupMessages(library(lattice, quietly = TRUE))
suppressPackageStartupMessages(library(latticeExtra, quietly = TRUE))
suppressPackageStartupMessages(library(argparse, quietly = TRUE))
main <- function(arguments){
parser <- ArgumentParser()
parser$add_argument('infile')
@nhoffman
nhoffman / sshmount
Last active August 29, 2015 14:10
bash function for mounting a directory via sshfs
sshmount () {
# Usage: sshmount host directory
host=$1
pth=$2
mountpoint=~/mnt/${host}+$(basename $pth)
mkdir -p $mountpoint
sshfs $host:$pth $mountpoint -oauto_cache,reconnect,defer_permissions,negative_vncache,volname=${host}+$(basename $pth)
cd $mountpoint
echo "unmount using: umount $mountpoint"
}
@nhoffman
nhoffman / wang_fig1_extended.R
Created January 20, 2011 18:19
Extends Wang, et al, 2007 Fig 1 (pmid 17600086) to show detection threshold at higher coverage
## Extends Wang, et al, 2007 Fig 1 (pmid 17600086) to show detection threshold at higher coverage
##
library(lattice)
## mu = error rate
mu_hp <- 0.0044
mu_nhp <- 0.0007
thresh <- function(N, mu, p=0.001){
@nhoffman
nhoffman / mutinfo.R
Created June 29, 2011 05:13
Use mutual information to find a cutoff separating two distributions
## Use mutual information to define a value separating two
## distributions.
entropy <- function(x,y){
## shannon entropy of x or joint entropy of x and y
if(missing(y)){
freqs <- table(x)/length(x)
}else{
stopifnot(length(x) == length(y))
freqs <- table(paste(x,y))/length(x)
;; gist.el
;; https://github.com/defunkt/gist.el
;; added as a submodule:
;; % git submodule add https://github.com/defunkt/gist.el.git
;; now, to clone .emacs.d elsewhere:
;; % git clone git@github.com:nhoffman/.emacs.d.git
;; % cd .emacs.d
;; % git submodule init && git submodule update
(condition-case nil
(require 'gist "~/.emacs.d/gist.el/gist.el")
import os
import sys
import ConfigParser
from os import path, environ
import glob
import itertools
venv = environ.get('VIRTUAL_ENV')
if not venv:
sys.exit('--> an active virtualenv is required'.format(venv))
@nhoffman
nhoffman / parallel.sh
Created July 24, 2012 19:11
Demonstrate parallel processes using xargs
#!/bin/bash
# try me out:
# two processors
# $ echo {1..9} | xargs -n1 -P2 ./ps.sh
# four processors
# $ echo {1..9} | xargs -n1 -P4 ./ps.sh
# sleeptime set to a random integer between 2 and 5
sleeptime=$(shuf -i 2-5 -n 1)
@nhoffman
nhoffman / unclassified.py
Created November 16, 2012 20:54
More patterns for matching unclassified sequences
#!/usr/bin/env python
import re
import sys
rexp = re.compile(r'|'.join([
r'\bactinomycete\b',
r'\bcrenarchaeote\b',
r'\bculture\b',
r'\bchimeric\b',