def geneusage_table(attr, type, outfile, g2n, n2obj, genes): fullattr = "type2gene2%s" % attr f = open(outfile, 'w') f.write("Sample\t%s\n" % ("\t".join([g.lstrip("TRB") for g in genes]))) for g in sorted(g2n.keys()): names = sorted(g2n[g]) cumul_freqs = [0.0] * len(genes) for name in names: gene2freq = n2obj[name][fullattr][type] freqs = [] for i, gene in enumerate(genes): if gene in gene2freq: cumul_freqs[i] += gene2freq[gene] freqs.append(gene2freq[gene]) else: freqs.append(0.0) pretty_freqs = [libcommon.pretty_float(freq) for freq in freqs] f.write("%s\t%s\n" % (name, "\t".join(pretty_freqs))) groupsize = len(names) if groupsize > 0: avr = [libcommon.pretty_float(freq/groupsize) for freq in cumul_freqs] else: avr = ['0'] * groupsize f.write("%s_Avr\t%s\n" % (g, "\t".join(avr))) f.close()
def table_latex_tab(f, objs, colfields): for (name, obj) in objs: f.write("%s" % name.replace('_', '\_')) for attr in colfields: if not obj or attr not in obj.getitems(): f.write(" & NA") else: f.write(" & %s" % libcommon.pretty_float(obj[attr])) #f.write(" & %.3f" % obj[attr]) stdattr = "%s_std" % attr if stdattr in obj.getitems(): std = obj[stdattr] if std: f.write(" $\pm$ %s" % libcommon.pretty_float(std)) f.write("\\\\\n") f.write("\\hline\n")
def table_text_tab(f, objs, colfields): for (name, obj) in objs: f.write("%s" % name) for attr in colfields: if not obj or attr not in obj.getitems(): f.write("\tNA") else: f.write("\t%s" % libcommon.pretty_float(obj[attr])) #f.write("\t%.3f" % obj[attr]) stdattr = "%s_std" % attr if stdattr in obj.getitems(): std = obj[stdattr] if std: f.write(" +/- %s" % libcommon.pretty_float(std)) #f.write(" +/- %.3f" % std) f.write("\n")
def table_latex_tab_list(f, objs, listattr): for (name, obj) in objs: if obj and listattr in obj.getitems(): f.write("%s & " % name.replace('_', '\_')) vals = ["%s" % libcommon.pretty_float(v) for v in obj[listattr]] #vals = ["%.3f" % v for v in obj[listattr]] f.write(" & ".join(vals)) f.write("\\\\\n") f.write("\\hline\n")
def run(self): f = open(self.outfile, "w") for clone, (present1, absent1, inames) in self.mjclones.iteritems(): portion1 = float(present1) / (present1 + absent1) portion2, onames = clone_group_freq(self.c2n2s, self.names2, clone) if portion2 <= self.opts.outgroup: present2 = round(portion2 * len(self.names2)) absent2 = len(self.names2) - present2 tab = [[present1, present2], [absent1, absent2]] oddratio, pval = fisher_exact(tab, alternative="greater") if pval <= self.opts.pval: p1 = libcommon.pretty_float(portion1) p2 = libcommon.pretty_float(portion2) odd = libcommon.pretty_float(oddratio) p = libcommon.pretty_float(pval) f.write( "%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (clone, p1, p2, odd, p, ",".join(inames), ",".join(onames)) ) # f.write("%s\t%.2e\t%.2e\t%.2e\t%.2e\n" % (clone, present1, # present2, oddratio, pval)) f.close() # remove outfile if there is no significant results if os.stat(self.outfile).st_size <= 0: system("rm -f %s" % self.outfile)
def ttest_write(f, name, pair2tp, group2mean, pcutoff=1): for pair, (tval, pval) in pair2tp.iteritems(): if pval <= pcutoff: #f.write("%s\t%s\t%.2e\t%.2e" % (name, pair, tval, pval)) f.write("%s\t%s\t%s\t%s" % (name, pair, libcommon.pretty_float(tval), libcommon.pretty_float(pval))) groups = pair.split("_") (m1, std1) = group2mean[groups[0]] (m2, std2) = group2mean[groups[1]] #f.write("\t%.2e +/- %.2e\t%.2e +/- %.2e\n" % (m1, std1, m2, std2)) m1pretty = libcommon.pretty_float(m1) std1pretty = libcommon.pretty_float(std1) m2pretty = libcommon.pretty_float(m2) std2pretty = libcommon.pretty_float(std2) f.write("\t%s +/- %s\t%s +/- %s\n" % (m1pretty, std1pretty, m2pretty, std2pretty))
def diversity_ttest_text(index2obj, outfile, pcutoff): # obj: (pair2(tval, pval), group2(mean, std)) f = open(outfile, 'w') f.write("#Index\tSample_Pair\tt_value\tp_value\tMean1\tStd1\tMean2\tStd2\n") for index in sorted(index2obj.keys()): obj = index2obj[index] assert len(obj) == 2 pair2stat = obj[0] group2mean = obj[1] for pair in sorted(pair2stat.keys()): tval, pval = pair2stat[pair] if pval > pcutoff: # does not pass pcutoff continue groups = pair.split('_') assert len(groups) == 2 (mean1, std1) = group2mean[groups[0]] (mean2, std2) = group2mean[groups[1]] vals = [tval, pval, mean1, std1, mean2, std2] valstrs = [] for v in vals: valstrs.append(libcommon.pretty_float(v)) f.write("%s\t%s\t%s\n" % (index, pair, '\t'.join(valstrs))) f.close()
def table_text_tab_list(f, objs, listattr): for (name, obj) in objs: if obj and listattr in obj.getitems(): vals = ["%s" % libcommon.pretty_float(v) for v in obj[listattr]] f.write("%s\t%s\n" % (name, "\t".join(vals)))