Skip to content

Instantly share code, notes, and snippets.

Avatar

Kevin Gori kgori

View GitHub Profile
@kgori
kgori / Makefile
Last active Jul 12, 2019
bcftools make consensus with insertion
View Makefile
# Replace these with the full path to the executables, if necessary
BWA=bwa
SAMTOOLS=samtools
BCFTOOLS=bcftools
# Makefile targets
all: ref.fa reads.fq aln.bam aln.bam.bai pileup.vcf filtered_calls.bcf consensus.fa
.PHONY: all
simulation: ref.fa reads.fq
@kgori
kgori / standardised_variables.R
Created Nov 27, 2018
Convert regression line coefficient and intercept of standardised variables back to the original scale
View standardised_variables.R
# Standardisation formula
z <- function(x) (x - mean(x)) / sd(x)
#' Convert regression parameters obtained from standardised variables
#' back to the scale of the original data.
#'
#' For a regression y ~ x*, where x* denotes the standardised form of x:
#' Result: y = m(x*) + c
#' Transformed: y = (m / sd(x))(x) + (c - (m*mean(x)/sd(x))
#'
@kgori
kgori / revcomp.py
Created Apr 6, 2018
DNA reverse complement
View revcomp.py
def revcomp(s):
complements = dict(
A='T', C='G', G='C', T='A',
M='K', R='Y', W='W', S='S',
Y='R', K='M', V='B', H='D',
D='H', B='V', N='N'
)
return ''.join(complements[char] for char in s.upper()[::-1])
@kgori
kgori / progress.py
Created Nov 17, 2017
Simple progress bar
View progress.py
import sys
class Progress():
def __init__(self, capacity, label = '', width=80, fill='#', blank=' '):
self.capacity = capacity
self.label = label
self.width = width
self.fill = fill
self.blank = blank
self.completed = 0
@kgori
kgori / insert_size.py
Last active Mar 23, 2018
Estimate insert size distribution of proper pairs in bam file
View insert_size.py
import pysam
import math
from itertools import islice
def filterfn(read):
return (read.is_proper_pair and
read.is_paired and
read.tlen > 0 and
not read.is_supplementary and
not read.is_duplicate and
@kgori
kgori / sigfit_pymc3.py
Last active Jan 23, 2017
PYMC3 model code to do signature fitting using NUTS
View sigfit_pymc3.py
"""
PYMC3 model code to do signature fitting using NUTS
"""
import pymc3
from theano import tensor
mytest = pymc3.Model()
counts = data
with mytest:
mixing_proportions = pymc3.Dirichlet('mixing_proportions', a = np.ones(30), shape = 30)
@kgori
kgori / discretize.py
Last active Oct 11, 2020
How to discretize a gamma or lognormal distribution, for modelling rates over sites in a phylogenetic model
View discretize.py
import scipy
import scipy.stats as ss
import numpy as np
def discretize(alpha, ncat, dist=ss.gamma):
if dist == ss.gamma:
dist = dist(alpha, scale=1 / alpha)
elif dist == ss.lognorm:
dist = dist(s=alpha, scale=np.exp(0.5 * alpha**2))
quantiles = dist.ppf(np.arange(0, ncat) / ncat)
View zoo.tasks.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kgori
kgori / pairdist.py
Last active Sep 10, 2015
Maximum likelihood distance between pairs of sequences
View pairdist.py
import numpy as np
def setup_logger():
import logging
logger = logging.getLogger()
for handler in logger.handlers:
logger.removeHandler(handler)
ch=logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
@kgori
kgori / discrete_gamma.c
Created Jul 6, 2015
Cython wrapper of PAML discrete gamma rates function
View discrete_gamma.c
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "discrete_gamma.h"
// PAML code for discrete gamma
// Forward decs
double QuantileChi2(double prob, double v);
double LnGammaFunction(double alpha);
double IncompleteGamma(double x, double alpha, double ln_gamma_alpha);
You can’t perform that action at this time.