Skip to content

Instantly share code, notes, and snippets.

@songqiang
songqiang / pyfreq.py
Last active June 9, 2017 07:46
python imlementation of SAS proc freq 1D & 2D tables
from pandas import crosstab
def pyfreq(v1, v2 = None, weight = None, order = ''):
if v2 is not None:
if weight is not None:
cnt = crosstab(v1, v2, weight, aggfunc = sum, margins = True, dropna = False)
else:
cnt = crosstab(v1, v2, margins = True, dropna = False)
if order == 'freq':
cnt.iloc[:-1, :] = cnt.iloc[:-1, :].sort_values('All', ascending = False).values
@songqiang
songqiang / fastLiftOver-example.sh
Last active August 29, 2015 14:02
Code snippets to map mouse methcounts output file to the human reference genome using the fastLiftOver program
fromFile=Mouse_ESC.meth
indexFile=CpGs-mm9Tohg19.bed
toFile=Mouse_ESC-hg19.meth
$fastLiftOver -f $fromFile -i $indexFile -t $toFile -v
# sort output and merge sites mapped to the same location
export LC_ALL=C;
tmpfile=$(mktemp);
wc -l $toFile
promoters=~qiangson/panfs/data/human/hg18/gene/promoter-nr.bed
CpGs=~qiangson/panfs/co-methylation/methylomes/Berman-Human-2012-Human_ColonCancer-results-Human_ColonCancer_Meth.bed
~qiangson/app/bedtools/bin/intersect $promoters $CpGs \
|cut -f4-6|uniq -c \
|awk '{print $2,$3,$4,2*$1/($4-$3);}' > CpG-densities.bed