This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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:] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ]) |
OlderNewer