def __init__(self, a_signature, another_signature): self.a = a_signature self.another = another_signature self.name = "%s <-> %s" % (a_signature.link, another_signature.link) self.up = a_signature.up & another_signature.up self.down = a_signature.down & another_signature.down self.diff = (a_signature.up | a_signature.down) \ & (another_signature.up | another_signature.down) self.diff_pvalue = hyperg(len(a_signature.diff), len(another_signature.diff), a_signature.transcripts.count(), len(self.diff)) self.down_pvalue = hyperg(len(a_signature.down), len(another_signature.down), a_signature.transcripts.count(), len(self.down)) self.up_pvalue = hyperg(len(a_signature.up), len(another_signature.up), a_signature.transcripts.count(), len(self.up)) self.anti_a = a_signature.up & another_signature.down self.anti_b = a_signature.down & another_signature.up self.anti_a_pvalue = hyperg(len(a_signature.up), len(another_signature.down), a_signature.transcripts.count(), len(self.anti_a)) self.anti_b_pvalue = hyperg(len(a_signature.down), len(another_signature.up), a_signature.transcripts.count(), len(self.anti_b))
def __init__(self, a_signature, another_signature): self.a = a_signature self.another = another_signature self.name = "%s <-> %s" % (a_signature.link, another_signature.link) self.up = a_signature.up & another_signature.up self.down = a_signature.down & another_signature.down self.diff = (a_signature.up | a_signature.down) & (another_signature.up | another_signature.down) self.diff_pvalue = hyperg( len(a_signature.diff), len(another_signature.diff), a_signature.transcripts.count(), len(self.diff) ) self.down_pvalue = hyperg( len(a_signature.down), len(another_signature.down), a_signature.transcripts.count(), len(self.down) ) self.up_pvalue = hyperg( len(a_signature.up), len(another_signature.up), a_signature.transcripts.count(), len(self.up) ) self.anti_a = a_signature.up & another_signature.down self.anti_b = a_signature.down & another_signature.up self.anti_a_pvalue = hyperg( len(a_signature.up), len(another_signature.down), a_signature.transcripts.count(), len(self.anti_a) ) self.anti_b_pvalue = hyperg( len(a_signature.down), len(another_signature.up), a_signature.transcripts.count(), len(self.anti_b) )
def compara(self, S1, S2): """Compares two signatures for significant overlap.""" if isinstance(S1, str): S1 = self[S1] # if only the name is given if isinstance(S2, str): S2 = self[S2] # the signature will be identfied if len(S1) > len(S2): total = len(S1) else: total = len(S2) S1_ranked_list = [] S2_ranked_list = [] for gene in S1: S1_ranked_list.append((S1[gene].ratio, S1[gene])) S1_ranked_list.sort() # S1_ranked_list.reverse() for gene in S2: S2_ranked_list.append((S2[gene].ratio, S2[gene])) S2_ranked_list.sort() # S2_ranked_list.reverse() print len(S1_ranked_list), len(S2_ranked_list) for threshold in xrange(100, 5000, 100): S1.up = {} S2.up = {} for ratio, s1 in S1_ranked_list[:threshold]: S1.up[s1.id] = s1 for ratio, s2 in S2_ranked_list[:threshold]: S2.up[s2.id] = s2 Intersection = set(S1.up) & set(S2.up) pvalue = hyperg(len(S1.up), len(S2.up), total, len(Intersection)) print threshold, "\t", len(S1.up), "\t", len(S2.up), "\t", len( Intersection ), "\t", pvalue # len(IntersectionUp), pvalueUp, len(S1.down), len(S2.down), len(IntersectionDown), pvalueDown, len(IntersectionComb), pvalueComb
def compara(self, S1, S2): """Compares two signatures for significant overlap.""" if isinstance(S1, str): S1 = self[S1] # if only the name is given if isinstance(S2, str): S2 = self[S2] # the signature will be identfied if len(S1) > len(S2): total = len(S1) else: total = len(S2) S1_ranked_list = [] S2_ranked_list = [] for gene in S1: S1_ranked_list.append((S1[gene].ratio, S1[gene])) S1_ranked_list.sort() #S1_ranked_list.reverse() for gene in S2: S2_ranked_list.append((S2[gene].ratio, S2[gene])) S2_ranked_list.sort() #S2_ranked_list.reverse() print len(S1_ranked_list), len(S2_ranked_list) for threshold in xrange(100, 5000, 100): S1.up = {} S2.up = {} for ratio, s1 in S1_ranked_list[:threshold]: S1.up[s1.id] = s1 for ratio, s2 in S2_ranked_list[:threshold]: S2.up[s2.id] = s2 Intersection = set(S1.up) & set(S2.up) pvalue = hyperg(len(S1.up), len(S2.up), total, len(Intersection)) print threshold, '\t', len(S1.up), '\t', len(S2.up), '\t', len( Intersection ), '\t', pvalue # len(IntersectionUp), pvalueUp, len(S1.down), len(S2.down), len(IntersectionDown), pvalueDown, len(IntersectionComb), pvalueComb
def enriched(self, S1, S2, ratio=1.0, upper=None, step=0.1, pvalue=None): """Compares two signatures for significant overlap.""" if isinstance(S1, str): S1 = self[S1] # if only the name is given if isinstance(S2, str): S2 = self[S2] # the signature will be identfied if len(S1) > len(S2): total = len(S1) else: total = len(S2) if not upper: upper = 99 Range = arange(ratio, upper, step).tolist() for upper in Range: lower = 1 / upper S1.delta, S1.up, S1.down = {}, {}, {} S2.delta, S2.up, S2.down = {}, {}, {} for gene in S1: if S1[gene].ratio > upper: #or not S1[gene].ratio: if pvalue: if S1[gene].pvalue < pvalue: S1.up[gene] = S1[gene] else: S1.up[gene] = S1[gene] for gene in S2: if S2[gene].ratio > upper or not S2[gene].ratio: if pvalue: if S2[gene].pvalue < pvalue: S2.up[gene] = S2[gene] else: S2.up[gene] = S2[gene] Intersection = set(S1.up) & set(S2.up) if not S1.up or not S2.up or not Intersection: break pvalue = hyperg(len(S1.up), len(S2.up), total, len(Intersection)) print upper, len(S1.up), len( S2.up), len(Intersection), pvalue, ', '.join(Intersection)
def enriched(self, S1, S2, ratio=1.0, upper=None, step=0.1, pvalue=None): """Compares two signatures for significant overlap.""" if isinstance(S1, str): S1 = self[S1] # if only the name is given if isinstance(S2, str): S2 = self[S2] # the signature will be identfied if len(S1) > len(S2): total = len(S1) else: total = len(S2) if not upper: upper = 99 Range = arange(ratio, upper, step).tolist() for upper in Range: lower = 1 / upper S1.delta, S1.up, S1.down = {}, {}, {} S2.delta, S2.up, S2.down = {}, {}, {} for gene in S1: if S1[gene].ratio > upper: # or not S1[gene].ratio: if pvalue: if S1[gene].pvalue < pvalue: S1.up[gene] = S1[gene] else: S1.up[gene] = S1[gene] for gene in S2: if S2[gene].ratio > upper or not S2[gene].ratio: if pvalue: if S2[gene].pvalue < pvalue: S2.up[gene] = S2[gene] else: S2.up[gene] = S2[gene] Intersection = set(S1.up) & set(S2.up) if not S1.up or not S2.up or not Intersection: break pvalue = hyperg(len(S1.up), len(S2.up), total, len(Intersection)) print upper, len(S1.up), len(S2.up), len(Intersection), pvalue, ", ".join(Intersection)
def compare(self, S1, S2, lower=1, upper=99, step=0.1): """Compares two signatures for significant overlap.""" if isinstance(S1, str): S1 = self[S1] # if only the name is given if isinstance(S2, str): S2 = self[S2] # the signature will be identfied ## O = Organisms[S.taxid].number_genes # if S1.type != 'profile' and S2.type != profile: if len(S1) > len(S2): total = len(S1) else: total = len(S2) # total = 6600 Range = arange(lower, upper, step).tolist() for upper in Range: lower = 1 / upper ## if S1.type == 'profile': S1.delta = {} S1.up = {} S1.down = {} for s1 in S1: if S1[s1].ratio > upper or not S1[s1].ratio: # or S1[s1].ratio < lower: S1.delta[s1] = S1[s1] ## if S1.genes[s1].fold_change > upper: ## S1.up[s1] = S1.genes[s1] ## else: ## S1.down[s1] = S1.genes[s1] ## else:S1.delta = S1.genes ## if S2.type == 'profile': S2.delta = {} S2.up = {} S2.down = {} for s2 in S2: # print s2, type(s2), S2[s2].fold_change, hasattr(S2[s2], 'fold_change') if S2[s2].ratio > upper or not S2[s2].ratio: # S2[s2].ratio < lower: S2.delta[s2] = S2[s2] ## if S2.genes[s2].fold_change > upper: ## S2.up[s2] = S2.genes[s2] ## else: ## S2.down[s2] = S2.genes[s2] ## else: S2.delta = S2.genes # print len(S1.delta), len(S2.delta) Intersection = set(S1.delta) & set(S2.delta) ## IntersectionUp = set(S1.up) & set(S2.up) ## IntersectionDown = set(S1.down) & set(S2.down) ## IntersectionComb = set(IntersectionUp) | set(IntersectionDown) if not len(S1.delta) or not len(S2.delta) or not Intersection: return None pvalue = hyperg(len(S1.delta), len(S2.delta), total, len(Intersection)) ## pvalueComb = hyperg(len(S1.delta), len(S2.delta), total, len(IntersectionComb)) ## pvalueUp = hyperg(len(S1.up), len(S2.up), total, len(IntersectionUp)) ## pvalueDown = hyperg(len(S1.down), len(S2.down), total, len(IntersectionDown)) print upper, "\t", len(S1.delta), "\t", len(S2.delta), "\t", len( Intersection ), "\t", pvalue # len(IntersectionUp), pvalueUp, len(S1.down), len(S2.down), len(IntersectionDown), pvalueDown, len(IntersectionComb), pvalueComb ## if pvalue < 0.05 and pvalue != 0: ## if len(Intersection) == 4: ## for i in Intersection: print S1.genes[i].symbol, S1.genes[i].fold_change ## stop ## print S1.genes[Result[0]].symbol, S2.genes[Result[0]].fold_change return len(S1.delta.keys()), len(S2.delta.keys()), len(Intersection)
def compare(self, S1, S2, lower=1, upper=99, step=0.1): """Compares two signatures for significant overlap.""" if isinstance(S1, str): S1 = self[S1] # if only the name is given if isinstance(S2, str): S2 = self[S2] # the signature will be identfied ## O = Organisms[S.taxid].number_genes #if S1.type != 'profile' and S2.type != profile: if len(S1) > len(S2): total = len(S1) else: total = len(S2) #total = 6600 Range = arange(lower, upper, step).tolist() for upper in Range: lower = 1 / upper ## if S1.type == 'profile': S1.delta = {} S1.up = {} S1.down = {} for s1 in S1: if S1[s1].ratio > upper or not S1[ s1].ratio: #or S1[s1].ratio < lower: S1.delta[s1] = S1[s1] ## if S1.genes[s1].fold_change > upper: ## S1.up[s1] = S1.genes[s1] ## else: ## S1.down[s1] = S1.genes[s1] ## else:S1.delta = S1.genes ## if S2.type == 'profile': S2.delta = {} S2.up = {} S2.down = {} for s2 in S2: #print s2, type(s2), S2[s2].fold_change, hasattr(S2[s2], 'fold_change') if S2[s2].ratio > upper or not S2[ s2].ratio: #S2[s2].ratio < lower: S2.delta[s2] = S2[s2] ## if S2.genes[s2].fold_change > upper: ## S2.up[s2] = S2.genes[s2] ## else: ## S2.down[s2] = S2.genes[s2] ## else: S2.delta = S2.genes #print len(S1.delta), len(S2.delta) Intersection = set(S1.delta) & set(S2.delta) ## IntersectionUp = set(S1.up) & set(S2.up) ## IntersectionDown = set(S1.down) & set(S2.down) ## IntersectionComb = set(IntersectionUp) | set(IntersectionDown) if not len(S1.delta) or not len(S2.delta) or not Intersection: return None pvalue = hyperg(len(S1.delta), len(S2.delta), total, len(Intersection)) ## pvalueComb = hyperg(len(S1.delta), len(S2.delta), total, len(IntersectionComb)) ## pvalueUp = hyperg(len(S1.up), len(S2.up), total, len(IntersectionUp)) ## pvalueDown = hyperg(len(S1.down), len(S2.down), total, len(IntersectionDown)) print upper, '\t', len(S1.delta), '\t', len(S2.delta), '\t', len( Intersection ), '\t', pvalue # len(IntersectionUp), pvalueUp, len(S1.down), len(S2.down), len(IntersectionDown), pvalueDown, len(IntersectionComb), pvalueComb ## if pvalue < 0.05 and pvalue != 0: ## if len(Intersection) == 4: ## for i in Intersection: print S1.genes[i].symbol, S1.genes[i].fold_change ## stop ## print S1.genes[Result[0]].symbol, S2.genes[Result[0]].fold_change return len(S1.delta.keys()), len(S2.delta.keys()), len(Intersection)