def run(self): llhs = [] events = [] for sam in os.listdir(self.indir): jfile = os.path.join(self.indir, sam, self.j) if os.path.exists(jfile): jclones = pickle.load(gzip.open(jfile, 'rb')) for c in jclones: if not rcommon.visited_event(events, c): events.append(c) clonellh = rcommon.ntclone_likelihood(c, self.model) llhs.append(clonellh) sumllh = sum([10 ** llh for llh in llhs]) pickle.dump(sumllh, gzip.open(self.outfile, 'wb'))
def aaclones_likelihood(clone2sams, model, db_dir, sam2total, group2sams, outfile, ingroup, outgroup): f = open(outfile, 'w') f.write("sample\tnum_ntclones\tprob_observed\n") for clone, (insams, outsams) in clone2sams.iteritems(): f.write("#%s\n" % clone) events = [] event_llhs = [] for i, sams in enumerate([insams, outsams]): if not sams: continue sam2ntclones = get_ntclones(clone, sams, db_dir) f.write("#Group_%d\n" % (i + 1)) for sam, ntclones in sam2ntclones.iteritems(): total = sam2total[sam] llhoods = [] for ntclone in ntclones: clonellhood = rcommon.ntclone_likelihood(ntclone, model) #prob_observed = clonellhood + log10(total) logmu = log10(total) + clonellhood prob_observed = poisson.logsf(1, 10 ** logmu) # prob. observing >=1 ntclone llhoods.append(prob_observed) if not rcommon.visited_event(events, ntclone): events.append(ntclone) event_llhs.append(clonellhood) #if clonellhood != float(-inf): # event_llhs.append(clonellhood) llhoods_str = ",".join(["%f" % llh for llh in llhoods]) f.write("%s\t%d\t%s\n" % (sam, len(ntclones), llhoods_str)) # calc prob to observe the aa clones (sum of all nt events) if sum([10**llh for llh in event_llhs]) > 0: aa_llh = log10(sum([10**llh for llh in event_llhs])) avr_total = (sam2total[ingroup] + sam2total[outgroup]) / 2 avr_logmu = aa_llh + log10(avr_total) avr_aa_llh = poisson.logsf(1, 10 ** avr_logmu) f.write("#Clone_log_likelihood: %f, %f\n" % (aa_llh, avr_aa_llh)) ingroup_llh = get_group_likelihood(aa_llh, group2sams[ingroup], insams, sam2total) outgroup_llh = get_group_likelihood(aa_llh, group2sams[outgroup], outsams, sam2total) f.write("#Ingrp vs Outgrp: %f vs %f\n#\n" % (ingroup_llh, outgroup_llh)) f.close()