Skip to content

Instantly share code, notes, and snippets.

@crazyhottommy
Last active March 8, 2019 07:12
Show Gist options
  • Select an option

  • Save crazyhottommy/67179a5f2925794a09d8 to your computer and use it in GitHub Desktop.

Select an option

Save crazyhottommy/67179a5f2925794a09d8 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
import sys
import scipy.stats as stats
#The result will be
# a p-value where by random chance number of genes with both condition A and B will be <= to your number with condition A and B
# a p-value where by random chance number of genes with both condition A and B will be >= to your number with condition A and B
# The second p-value is probably what you want.
if len(sys.argv) < 5:
print '''use it by: python gene_sets_hypergeomtric_test.py [total number of genes in the list] [total number of genes in the list with condition A] [total number of genes in the list with condition B] [number of genes with both condition A and B]'''
sys.exit()
print
print 'total number in population: ' + sys.argv[1]
print 'total number with condition in population: ' + sys.argv[2]
print 'number in subset: ' + sys.argv[3]
print 'number with condition in subset: ' + sys.argv[4]
print
print 'p-value <= ' + sys.argv[4] + ': ' + str(stats.hypergeom.cdf(int(sys.argv[4]) + 1,int(sys.argv[1]),int(sys.argv[2]),int(sys.argv[3])))
print
print 'p-value >= ' + sys.argv[4] + ': ' + str(stats.hypergeom.sf(int(sys.argv[4]) - 1,int(sys.argv[1]),int(sys.argv[2]),int(sys.argv[3])))
print
# phyper(overlap-1,list1,PopSize-list1,list2,lower.tail = FALSE, log.p = FALSE)
phyper(100-1, 3000, 15000-3000, 400,lower.tail = FALSE, log.p = FALSE)
#0.00784
@wrparker
Copy link
Copy Markdown

wrparker commented Mar 8, 2019

I foudn thsi extremely helpful for converting hypergeometric.r to python. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment