def computeTraitStatistics(genes, pfilter):

    
    traitSet = set([key for key in gwasDB.__studyByTrait if len(gwasDB.getGenesForTrait(key)) > 0])
    
    traitChi = {}
    pbar = ProgressBar()

    pbar.setMaximum(len(traitSet))
    pbar.updateProgress(0)
    i=0
    for trait in traitSet:
        if i % 5 == 0:
            pbar.updateProgress(0)

        i+=1
        traitGenes = gwasDB.getGenesForTrait(trait,pfilter)

        listA = traitGenes & genes
        listC = traitGenes - genes

        a = len(listA)
        b = len(genes - traitGenes)
        c = len(listC)
        d = len(geneDB.__approved_symbols - (traitGenes | genes))

        oddsratio = geneUtils.oddsRatio(a,b,c,d)
        kappa = geneUtils.kappaStatistic(a,b,c,d)

        fisher_exact = fisher.compute(a,b,c,d)
        fisher_p = fisher.significance(fisher_exact, a,b,c,d)

        traitChi[trait] = (a, oddsratio, kappa, len(traitGenes),
                fisher_exact, fisher_p, traitGenes)

    pbar.finalize()
    return traitChi
def computeTraitGeneLists(RE_genes, drug_genes, pfilter_cutoff):
    
    traitSet = set(gwasDB.__studyByTrait.keys())
    
    pbar = ProgressBar()
    pbar.setMaximum(len(traitSet))

    pbar.updateProgress(0)
    i = 0
    for trait in traitSet:
        if i % 5 == 0:
            pbar.updateProgress(i)
        i+=1
        traitGenes = gwasDB.getGenesForTrait(trait, pfilter_cutoff)
        
        if len(traitGenes) == 0: 
            continue
        
        __traitMetaAnalysis[trait] = {}
        
        
        RE = []
        for gene in traitGenes & RE_genes:
            
            count = len(gwasDB.getTraitsForGene(gene))
            RE.append((gene, count))
        
        __traitMetaAnalysis[trait]['RE'] = RE
        
        
        
        drug = []
        for gene in traitGenes & drug_genes:
            
            count = len(gwasDB.getTraitsForGene(gene))
            drug.append((gene, count))
        
        __traitMetaAnalysis[trait]['drugbank'] = drug
        
        
            
        other = []
        for gene in traitGenes - RE_genes - drug_genes:
            
            count = len(gwasDB.getTraitsForGene(gene))
            other.append((gene,count))
        
        __traitMetaAnalysis[trait]['other'] = other
        
        
        
        a = len(traitGenes & RE_genes)
        b = len(RE_genes - traitGenes)
        c = len(traitGenes - RE_genes)
        d = len(geneDB.__approved_symbols - (traitGenes | RE_genes))
        
        oddsratio = geneUtils.oddsRatio(a,b,c,d)
        kappa = geneUtils.kappaStatistic(a,b,c,d)
        fisher_exact = fisher.compute(a,b,c,d)
        fisher_p = fisher.significance(fisher_exact, a,b,c,d)
        
        __traitMetaAnalysis[trait]['RE_chi'] = (a, b, c, d,
                oddsratio, kappa, fisher_exact, fisher_p)
        
        a = len(traitGenes & drug_genes)
        b = len(drug_genes - traitGenes)
        c = len(traitGenes - drug_genes)
        d = len(geneDB.__approved_symbols - (traitGenes | drug_genes))
        
        oddsratio = geneUtils.oddsRatio(a,b,c,d)
        kappa = geneUtils.kappaStatistic(a,b,c,d)
        fisher_exact = fisher.compute(a,b,c,d)
        fisher_p = fisher.significance(fisher_exact, a,b,c,d)
        
        __traitMetaAnalysis[trait]['drugbank_chi'] = (a, b, c, d,
                oddsratio, kappa, fisher_exact, fisher_p)
        
        __traitMetaAnalysis[trait]['geneset_size'] = len(traitGenes)
    
    pbar.finalize()
def computeTraitDrugLists(RE_genes, drug_genes, pfilter_cutoff):
    
    traitSet = set(gwasDB.__studyByTrait.keys())
    
    for trait in traitSet:
        traitGenes = gwasDB.getGenesForTrait(trait, pfilter_cutoff)
        
        if len(traitGenes) == 0:
            continue
        
        RE_drugs = []
        other_drugs = []
        drug_counts_by_gene = {}
        
        drugs_targeting_RE = set([])
        allDrugs = set([])
        for gene in traitGenes & RE_genes:
            drug_counts_by_gene[gene] = 0
            if gene in drugDB.__drugDict:
                for drug in drugDB.__drugDict[gene]:
                    RE_drugs.append(drug)
                drug_counts_by_gene[gene] += len(drugDB.__drugDict[gene])
                drugs_targeting_RE |= drugDB.__drugDict[gene]

        drugs_targeting_disease = set([])

        for gene in ((traitGenes & drug_genes) - RE_genes):
            drug_counts_by_gene[gene] = 0
            if gene in drugDB.__drugDict:
                for drug in drugDB.__drugDict[gene]:
                    other_drugs.append(drug)
                drug_counts_by_gene[gene] += len(drugDB.__drugDict[gene])
                drugs_targeting_disease |= drugDB.__drugDict[gene]

        drugs_targeting_other_RE = set([])

        for gene in RE_genes:
            drug_counts_by_gene[gene] = 0
            if gene in drugDB.__drugDict:
                drugs_targeting_other_RE |= drugDB.__drugDict[gene]

        drugs_targeting_other_RE -= drugs_targeting_RE

        a = len(drugs_targeting_RE)
        b = len(drugs_targeting_disease)
        c = len(drugs_targeting_other_RE)
        d = len(set(drugDB.__drugs.keys()) - (drugs_targeting_RE |
                drugs_targeting_disease | drugs_targeting_other_RE))
        # print a, b, c, d

        if (a + b) == 0 or (a + c) == 0:
            odds, kappa = 0, 0
            fisher_exact = 1.0
            fisher_p = 1.0
        else:
            odds = geneUtils.oddsRatio(a,b,c,d)
            kappa = geneUtils.kappaStatistic(a,b,c,d)
            fisher_exact = fisher.compute(a,b,c,d)
            fisher_p = fisher.significance(fisher_exact, a,b,c,d)

        __traitMetaAnalysis[trait]['RE_drugs'] = RE_drugs
        __traitMetaAnalysis[trait]['other_drugs'] = other_drugs
        __traitMetaAnalysis[trait]['drug_counts'] = drug_counts_by_gene
        __traitMetaAnalysis[trait]['drugchi'] = (a,b,c,d,odds,kappa,fisher_exact,fisher_p)