Skip to content

Instantly share code, notes, and snippets.

View davidaknowles's full-sized avatar

David A Knowles davidaknowles

View GitHub Profile
@davidaknowles
davidaknowles / splitseq_convert.m
Created March 27, 2023 02:47
Convert SPLIT-seq Matlab sparse matrices to something useable
function mmwrite(filename,A)
mmfile = fopen(filename,'w');
[M,N] = size(A);
[I,J,V] = find(A);
NZ = length(V);
@davidaknowles
davidaknowles / make_het.sh
Created June 17, 2022 17:28
Make a dummy vcf with all SNPs being heterozygous for one individual. SOME_SAMPLE needs to be set to one of the sample names in the input.vcf.
#!/bin/bash
bcftools view -H input.vcf.gz | \
awk '{print $1 "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 "\t" $7 "\t" $8 "\t" $9 "\t" "0/1"}' | \
cat <(bcftools view -h -s SOME_SAMPLE input.vcf.gz) - | \
bgzip > \
output.vcf.gz
bcftools view -H output.vcf.gz | awk '{ print $1 "\t" $2 - 1 "\t" $2 }' | bgzip > output.bed.gz
import pyro
from pyro.infer import SVI, Trace_ELBO, TraceEnum_ELBO, config_enumerate
from torch.distributions import constraints
data = 2. * torch.Tensor( [-1., -0.5, -0.5, .5, .8, 1.] )
def model(data):
guide_efficacy = pyro.sample('guide_efficacy', dist.Beta(1., 1.).expand([len(data)]).to_event(1) )
gene_essentiality = pyro.sample("gene_essentiality", dist.Normal(0., 5.))
mean = gene_essentiality * guide_efficacy
@davidaknowles
davidaknowles / get_cis_geno.R
Created June 19, 2021 17:46
Read genotype data for given range from VCF and convert to dosage matrix
require(VariantAnnotation)
require(GenomicRanges)
get_cis_geno = function(tab, chrom, left, right, genome_build = "GRCh38") {
gr = GRanges(chrom, IRanges(left, right))
sp = ScanVcfParam(which = gr)
vcf = readVcf(tab, genome_build, param = sp)
gt = geno(vcf)$GT
if (nrow(gt) == 0) return(NULL)
allele1 = substr(gt, 1, 1)
@davidaknowles
davidaknowles / easy_impute.R
Created June 19, 2021 17:42
SVD based imputation of missing genotypes
#' Consistent implementation of diag
fix_diag=function(x) {
if(length(x)==1) matrix(x) else diag(x)
}
unscale = function(x) {
x = sweep(x, 1, attr(x, "scaled:scale"), "*")
sweep(x, 1, attr(x, "scaled:center"), "+")
}
@davidaknowles
davidaknowles / torch_pixel_shuffle1d.py
Created August 20, 2019 19:23
torch currently only supports 2d pixel shuffle from https://arxiv.org/abs/1609.05158. This is the 1d version.
def pixel_shuffle_1d(x, upscale_factor):
batch_size, channels, steps = x.size()
channels //= upscale_factor
input_view = x.contiguous().view(batch_size, channels, upscale_factor, steps)
shuffle_out = input_view.permute(0, 1, 3, 2).contiguous()
return shuffle_out.view(batch_size, channels, steps * upscale_factor)
@davidaknowles
davidaknowles / ghostscript_pdf_1p7_to_1p5.sh
Last active December 8, 2017 04:08
Use ghostscript to convert PDFs to version 1.5 (better for pdflatex)
#!/bin/bash
set -e
outfile=$2
if [ "$#" -eq 1 ]; then
outfile=$1
fi
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dCompatibilityLevel=1.5 -sOutputFile=temp.pdf $1
# so you can run on the same file
mv temp.pdf $outfile
@davidaknowles
davidaknowles / images_as_xaxis_labels_updated.R
Last active September 19, 2017 18:26 — forked from jonocarroll/images_as_xaxis_labels_updated.R
Implements @baptiste's much better method for making this work
library(ggplot2) ## devtools::install_github("hadley/ggplot2)
library(grid) ## rasterGrob
library(EBImage) ## readImage
library(ggthemes) ## theme_minimal
## ##########
## INDEPENDENT CODE TO BE SOURCED:
## ##########
# user-level interface to the element grob