Skip to content

Instantly share code, notes, and snippets.

@egafni
egafni / plot_karyotype_ideogram.py
Last active October 13, 2015 19:14
Plot Karyotype Ideogram using matplotlib
"""
import os
import urllib
url = 'http://pastebin.com/raw.php?i=6nBX6sdE'
fn = 'karyotype.txt'
if not os.path.exists(fn):
print 'Downloading %s to local file: %s' % (url, fn)
with open(fn, 'w') as k_file:
f = urllib.urlopen(url)
@egafni
egafni / vcf_genotype_ordering.py
Last active September 2, 2016 16:33
VCF allele ordering and indexing
def vcf_genotype_ordering_iter(ploidy, num_alt_alleles, suffix=None):
"""
Generator version of :func:`geno_ordering`
"""
for a in range(num_alt_alleles+1):
if ploidy == 1:
yield (a,) + suffix
elif ploidy > 1:
for alleles in vcf_genotype_ordering_iter(ploidy-1, a, (a,) + (suffix or tuple())):
yield alleles
@egafni
egafni / segment.py
Last active April 4, 2018 07:25
Groupby a pairwise comparison function
from more_itertools import peekable
def segment(itrbl, is_group):
"""
Similar to itertools.groupby but allows you to directly compare elements pairwise with a function rather than only being able to use a key.
>>> f = lambda x, y: y - x < 50
>>> list(segment([1, 2, 100, 101, 500, 505, 510, 700], f))
[[1, 2], [100, 101], [500, 505, 510], [700]]
>>> list(segment[1], f)
@egafni
egafni / gist:a53cc2120f5702c7f1928c38c9305437
Created September 19, 2017 22:11
Persist local environment to a shelve db
import shelve
from contextlib import closing
def shelve_locals(filename, locals_):
"""
Intended to be called as shelve_locals(filename, locals()) from a debug session
and then loaded into another environment like an ipython notebook.
ex:
@egafni
egafni / gitx
Created September 20, 2017 16:14
auto add branch name to commit message
#!/usr/bin/env python
import sys
import subprocess as sp
GIT = '/usr/local/bin/git'
for line in sp.check_output('%s branch' % GIT, shell=True).strip().split("\n"):
if line.startswith('* '):
branch = line[2:]
def beta_binom_credible_interval(k, n, credible_interval_size=0.95, a=1, b=1):
"""
Returns the credible interval of the posterior of a beta binomial. This is the Bayesian
interpretation of the 95% confidence interval.
a, b are alpha/beta parameters of a beta distribution.
Default of a=1, b=1 is a uniform distribution over [0, 1]
>>> np.round(beta_binom_credible_interval(1, 2), 2)
array([0.09, 0.91])
def get_segment_ranges(arr):
"""
Get a list of (start, stop) ranges for segments of repeating elements within an array
>>> get_segment_ranges([0,1,0,0,0,1,1,1,0,1])
[(0, 1), (1, 2), (2, 5), (5, 8), (8, 9), (9, 10)]
>>> get_segment_ranges([])
[]
>>> get_segment_ranges([1,1,2])
[(0, 2), (2, 3)]
@egafni
egafni / percentile_mask.py
Created July 24, 2018 17:14
percentile mask
def percentile_mask(arr, lower=None, upper=None):
"""
>>> arr = np.array([1,2,3,4])
array([False, True, True, False])
>>> percentile_mask(arr, 25)
array([False, True, True, True])
>>> percentile_mask(arr, None, 25)
array([ True, False, False, False])
"""
if lower is not None:
@egafni
egafni / get_adjacent_cells.py
Created April 3, 2019 07:39
Get Adjacent Cells
def get_adjacent_cells(arr, selected_idxs):
"""
>>> arr = np.ones((3,))
>>> get_adjacent_cells(arr, {(1,)})
{(0,), (1,), (2,)}
>>> arr = np.ones((3,2))
>>> get_adjacent_cells(arr, {(1,1)})
{(0, 1), (1, 0), (1, 1), (2, 1)}
>>> arr = np.ones((3,2,3))
>>> {(0, 1, 0), (1, 0, 0), (1, 1, 0), (1, 1, 1), (2, 1, 0)}
@egafni
egafni / credible_coordinates
Last active April 3, 2019 08:18
credible_coordinates
def credible_coordinates(arr, min_percentile=.99):
"""
Calculates a type of credible "interval" by a simple greedy approach of the most likely coordinates of a joint
density.
>>> X = np.array([.1,.1,.5,.3])
>>> Y = np.array([.2,.8])
>>> Z = np.array([.5,.5])
>>> R = np.array([[[x*y*z for z in Z] for y in Y] for x in X ])