"""
Created on Jun 23, 2013

@author: agross
"""


def read_in_pathways(mSigDbFile):
    '''
    Reads in mSigDb pathways.
    File format should be that downloaded from website, tsv with format:
    pathway \t gene1 \t gene2 ... geneX \n
    input:
      mSigDbFile:        pathway file, can be downloaded from the web-site
    output:
      geneSets:          dict mapping pathway name to set of genes in the pathway
      geneLookup:        dict mapping genes to the pathways they occur in
    '''    
    f = open(mSigDbFile, 'r')
    geneSets = {}
    genes = []
    for line in f:
        line = line.replace('\"', '')
        tmp = line.strip().split("\t")
        setName = tmp[0]
        geneSets[setName] = set(tmp[2:])
        genes.extend(tmp[2:])
    f.close()
    genes = set(genes)

    geneLookup = dict([(gene, set()) for gene in genes])
    for pathway in geneSets: 
        for gene in geneSets[pathway]: 
            geneLookup[gene].add(pathway)
    return geneSets, geneLookup


@@ -43,16 +43,56 @@
 ss%0A%22%22%22%0A%0A
+from Stats.Scipy import chi2_cont_test%0A%0A
 %0Adef rea
@@ -122,19 +122,19 @@
 e):%0A    
-'''
+%22%22%22
 %0A    Rea
@@ -534,15 +534,11 @@
     
-'''    
+%22%22%22
 %0A   
@@ -1029,8 +1029,729 @@
 Lookup%0A%0A
+%0Adef filter_pathway_hits(hits, gs, cutoff=.00001):%0A    %22%22%22%0A    Returns a filtered list of p-values with redundant pathways%0A    removed.%0A%0A    hits:%0A        Series of p-values.%0A    gs:%0A        DataFrame of gene-set asignments encoded as a binary matrix.%0A    cutoff:%0A        p-value cutoff for overlab between gene-sets%0A    %22%22%22%0A    hits = hits.order()%0A    l = %5Bhits.index%5B0%5D%5D%0A    for gg in hits.index:%0A        flag = 0%0A        for g2 in l:%0A            if gg in l:%0A                flag = 1%0A                break%0A            elif chi2_cont_test(gs%5Bgg%5D, gs%5Bg2%5D)%5B'p'%5D %3C cutoff:%0A                flag = 1%0A                break%0A        if flag == 0:%0A            l.append(gg)%0A    hits_filtered = hits.ix%5Bl%5D%0A    return hits_filtered%0A

Add function to filter redundant pathways.