def venn(inp, names, title="venn", folder=''): """ Plots a venn diagram using the pyvenn package Args: ----- inp: list[set()] of sets of values (e.g. [(1,2,3,4),(2,3),(1,3,4,5)]) names: list[str] of the name of each leaf title: str the plot title folder: str of location where to save the plot, won't save if empty """ labels = pyvenn.get_labels(inp, fill=['number', 'logic']) if len(inp) == 2: fig, ax = pyvenn.venn2(labels, names=names) elif len(inp) == 3: fig, ax = pyvenn.venn3(labels, names=names) elif len(inp) == 4: fig, ax = pyvenn.venn4(labels, names=names) elif len(inp) == 5: fig, ax = pyvenn.venn5(labels, names=names) elif len(inp) == 6: fig, ax = pyvenn.venn6(labels, names=names) else: raise ValueError('need to be between 2 to 6') ax.set_title(title) if folder: fig.savefig(folder + title + '_venn.pdf') fig.show() plt.pause(0.1)
def generate_venn_diagram(nb_dim, files, names, sep, label): """Cette fonction fait l'appel du fichier venn.py (qui doit être présent dans le même dossier que generate_venn.py) pour générer les diagrammes de Venn. :param: nombre de dimensions présent dans le diagramme de Venn (1 < nb_dim <= 6) :param: files: liste des chemins d'accès aux fichiers contenant les résultats des modèles :param: names: liste des noms des modèles (doit être en adéquation avec la liste des modèles files) :param: sep: séparateur présent dans filename séparant les id-instance des label :label: label que l'on souhaite extraire """ if nb_dim < 2 or nb_dim > 6: print( 'Le diagramme de Venn ne peut être généré que pour un nombre de dimensions supérieur à un et inférieur ou égal à 6.' ) else: i = 0 models = [] while i < nb_dim: models.append( get_id_instances_title(files[i], sep=sep, label=label)) i += 1 labels = venn.get_labels(models) if nb_dim == 2: fig, ax = venn.venn2(labels, names=names) plt.show() elif nb_dim == 3: fig, ax = venn.venn3(labels, names=names) plt.show() elif nb_dim == 4: fig, ax = venn.venn4(labels, names=names) plt.show() elif nb_dim == 5: fig, ax = venn.venn5(labels, names=names) plt.show() elif nb_dim == 6: fig, ax = venn.venn6(labels, names=names) plt.show() else: print( 'Le diagramme de Venn ne peut être généré que pour un nombre de dimensions supérieur à un et inférieur ou égal à 6.' )
def uniq_share_cal(self, infile): for line in infile: s = line.rstrip() orths = list(s.split('\t')) ids = [] for x in xrange(len(orths)): item = orths[x].split('|')[0] if item in self.ids: ids.append(item) target = set(ids) for key in self.data: if set(key) == target: self.data[key] += 1 labels = {} for key in self.data: key2 = '' for x in xrange(len(self.ids)): if self.ids[x] in key: key2 += '1' else: key2 += '0' labels[key2] = self.data[key] if len(self.ids) == 3: fig, ax = venn.venn3(labels, names=self.ids) fig.savefig('gene_family_3taxa.pdf') elif len(self.ids) == 4: fig, ax = venn.venn4(labels, names=self.ids) fig.savefig('gene_family_4taxa.pdf') elif len(self.ids) == 5: fig, ax = venn.venn5(labels, names=self.ids) fig.savefig('gene_family_5taxa.pdf') elif len(self.ids) == 6: fig, ax = venn.venn6(labels, names=self.ids) fig.savefig('gene_family_6taxa.pdf')
def cohort_overlap(cohorts: dict): """ Helper method to intersect MRNs from different cohorts and generate a venn diagram based on that. Args: cohorts: dict of label and Cohort object Returns: dict with overlapping MRNs and figure """ cohort_list = list(cohorts.values()) overlapping_mrns = cohort_list[0].mrns.copy() for cohort in cohort_list: overlapping_mrns &= cohort.mrns labels = venn.get_labels([c.mrns for c in cohorts.values()]) figure, ax = venn.venn3(labels, names=cohorts.keys()) return { 'count': len(overlapping_mrns), 'mrns': overlapping_mrns, 'figure': figure }
n34 = len(intersection(tp[ids[2]], tp[ids[3]])) n35 = len(intersection(tp[ids[2]], tp[ids[4]])) n45 = len(intersection(tp[ids[3]], tp[ids[4]])) area5 = len(tp[ids[4]]) area4 = len(tp[ids[3]]) area3 = len(tp[ids[2]]) area2 = len(tp[ids[1]]) area1 = len(tp[ids[0]]) venn_diag_list = [area1, area2, area3, area4, area5, n12, n13, n14, n15,n23, n24, n25, n34, n35, n45, n123, n124, n125, n134, n135, n145, n234, n235, n245, n345, n1234, n1235, n1245, n1345, n2345, n12345] print('Overlap list needed for VennDiagram in R') print(venn_diag_list) out1.write('\n\n## Overlap list needed for VennDiagram in R:\n\n%s' % venn_diag_list) if plot.lower() == 't' or plot.lower() == 'true': if n_comparing == 5: labels = venn.get_labels([tp[ids[0]], tp[ids[1]], tp[ids[2]], tp[ids[3]], tp[ids[4]]], fill = ['number']) fig, ax = venn.venn5(labels, names = ids) elif n_comparing == 4: labels = venn.get_labels([tp[ids[0]], tp[ids[1]], tp[ids[2]], tp[ids[3]]], fill = ['number']) fig, ax = venn.venn4(labels, names = ids) elif n_comparing == 3: labels = venn.get_labels([tp[ids[0]], tp[ids[1]], tp[ids[2]]], fill = ['number']) fig, ax = venn.venn3(labels, names = ids) elif n_comparing == 2: labels = venn.get_labels([tp[ids[0]], tp[ids[1]]], fill = ['number']) fig, ax = venn.venn2(labels, names = ids) filename = save+'_pred_compared.pdf' fig.savefig(filename)
axes[1].tick_params(labelsize=14) plt.tight_layout(w_pad=2.5) plt.savefig("f1ab_new.png", dpi=600) plt.close() labels_A0101 = venn.get_labels(pred_labels['A0101'], fill=['number', 'logic']) labels_A0201 = venn.get_labels(pred_labels['A0201'], fill=['number', 'logic']) labels_B3501 = venn.get_labels(pred_labels['B3501'], fill=['number', 'logic']) # Format label a bit for key in labels_A0101: labels_A0101[key] = labels_A0101[key][4:] for key in labels_A0201: labels_A0201[key] = labels_A0201[key][4:] for key in labels_B3501: labels_B3501[key] = labels_B3501[key][4:] # Plot venn #matplotlib.rcParams.update({"font.size": 40}) sns.set_context("talk") ax3 = venn.venn3(labels_A0101, names=['NP', 'NetMHCpan', 'MS.True']) plt.savefig("A0101.venn_new.png", dpi=300, bbox_inches='tight') plt.close() ax4 = venn.venn3(labels_A0201, names=['NP', 'NetMHCpan', 'MS.True']) plt.savefig("A0201.venn_new.png", dpi=300, bbox_inches='tight') plt.close() ax5 = venn.venn3(labels_B3501, names=['NP', 'NetMHCpan', 'MS.True']) plt.savefig("B3501.venn_new.png", dpi=300, bbox_inches='tight') plt.close()
""" @author: tctianchi https://github.com/tctianchi/pyvenn """ import matplotlib.pyplot as plt import venn labels = venn.get_labels([range(10), range(5, 15)], fill=['number', 'logic']) fig, ax = venn.venn2(labels, names=['list 1', 'list 2']) fig.savefig('venn2.png', bbox_inches='tight') plt.close() labels = venn.get_labels( [range(10), range(5, 15), range(3, 8)], fill=['number', 'logic']) fig, ax = venn.venn3(labels, names=['list 1', 'list 2', 'list 3']) fig.savefig('venn3.png', bbox_inches='tight') plt.close() labels = venn.get_labels( [range(10), range(5, 15), range(3, 8), range(8, 17)], fill=['number', 'logic']) fig, ax = venn.venn4(labels, names=['list 1', 'list 2', 'list 3', 'list 4']) fig.savefig('venn4.png', bbox_inches='tight') plt.close() labels = venn.get_labels( [range(10), range(5, 15), range(3, 8),
n25 = len(intersection(comp[ids[1]], comp[ids[4]])) n34 = len(intersection(comp[ids[2]], comp[ids[3]])) n35 = len(intersection(comp[ids[2]], comp[ids[4]])) n45 = len(intersection(comp[ids[3]], comp[ids[4]])) area5 = len(comp[ids[4]]) area4 = len(comp[ids[3]]) area3 = len(comp[ids[2]]) area2 = len(comp[ids[1]]) area1 = len(comp[ids[0]]) venn_diag_list = [area1, area2, area3, area4, area5, n12, n13, n14, n15,n23, n24, n25, n34, n35, n45, n123, n124, n125, n134, n135, n145, n234, n235, n245, n345, n1234, n1235, n1245, n1345, n2345, n12345] print('Overlap list needed for VennDiagram in R') print(venn_diag_list) #out1.write('\n\n## Overlap list needed for VennDiagram in R:\n\n%s' % venn_diag_list) if n_comparing == 5: labels = venn.get_labels([comp[ids[0]], comp[ids[1]], comp[ids[2]], comp[ids[3]], comp[ids[4]]], fill = ['number']) fig, ax = venn.venn5(labels, names = ids) elif n_comparing == 4: labels = venn.get_labels([comp[ids[0]], comp[ids[1]], comp[ids[2]], comp[ids[3]]], fill = ['number']) fig, ax = venn.venn4(labels, names = ids) elif n_comparing == 3: labels = venn.get_labels([comp[ids[0]], comp[ids[1]], comp[ids[2]]], fill = ['number']) fig, ax = venn.venn3(labels, names = ids) elif n_comparing == 2: labels = venn.get_labels([comp[ids[0]], comp[ids[1]]], fill = ['number']) fig, ax = venn.venn2(labels, names = ids) filename = save+'_pred_compared.pdf' fig.savefig(filename)
# %matplotlib inline # python console requires this import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import venn labels = venn.get_labels([range(10), range(5, 15)], fill=['number', 'logic']) fig, ax = venn.venn2(labels, names=['list 1', 'list 2']) fig.savefig('venn2.png', bbox_inches='tight') plt.close() labels = venn.get_labels([range(10), range(5, 15), range(3, 8)], fill=['number', 'logic']) fig, ax = venn.venn3(labels, names=['list 1', 'list 2', 'list 3']) fig.savefig('venn3.png', bbox_inches='tight') plt.close() labels = venn.get_labels([range(10), range(5, 15), range(3, 8), range(8, 17)], fill=['number', 'logic']) fig, ax = venn.venn4(labels, names=['list 1', 'list 2', 'list 3', 'list 4']) fig.savefig('venn4.png', bbox_inches='tight') plt.close() labels = venn.get_labels([range(10), range(5, 15), range(3, 8), range(8, 17), range(10, 20)], fill=['number', 'logic']) fig, ax = venn.venn5(labels, names=['list 1', 'list 2', 'list 3', 'list 4', 'list 5']) fig.savefig('venn5.png', bbox_inches='tight') plt.close() labels = venn.get_labels([range(10), range(5, 15), range(3, 8), range(8, 17), range(10, 20), range(13, 25)], fill=['number', 'logic']) fig, ax = venn.venn6(labels, names=['list 1', 'list 2', 'list 3', 'list 4', 'list 5', 'list 6'])
# Remove '.' from sets tmp = [] for i in range(0, len(set_list)): tmpset = {x for x in set_list[i] if x==x} tmpset = set([x for x in tmpset if '.' not in x]) tmp.append(tmpset) del set_list set_list = tmp del tmp ### Venn diagram of clonal sequences for each sample pdf = PdfPages('outfile.pdf') labels = venn.get_labels([set_list[0], set_list[1], set_list[2]],fill=['number','number']) fig, ax = venn.venn3(labels, names = (argv[2], argv[3], argv[4])) plt.tight_layout() plt.savefig(pdf, format='pdf') plt.close() pdf.close() #Now print overlapped sequences cdr3dic = defaultdict(lambda: []) for i in range(0, len(set_list)): for cdr3 in set_list[i]: cdr3dic[cdr3].append(argv[i+2]) for cdr3 in sorted(cdr3dic.keys(), key=lambda x: len(cdr3dic[x]), reverse=True): if len(cdr3dic[cdr3]) > 1:
elif "full_model" in results_file: all_labels.append("Full model") for r_label in results: # print unique TP to this result tps = set(results[r_label]["all"]) & set(gold_pairs["all"]) for r_label2 in results: if r_label2 == r_label: continue tps = tps - set(results[r_label2]["all"]) print() print("unique to {}:".format(r_label)) print(len(tps)) print(tps) print() print(all_labels) # print(results) labels = venn.get_labels(all_pairs, fill=["number"]) if len(all_pairs) == 2: fig, ax = venn.venn2(labels, names=all_labels) elif len(all_pairs) == 3: fig, ax = venn.venn3(labels, names=all_labels) elif len(all_pairs) == 4: fig, ax = venn.venn4(labels, names=all_labels) elif len(all_pairs) == 5: fig, ax = venn.venn5(labels, names=all_labels) elif len(all_pairs) == 6: fig, ax = venn.venn6(labels, names=all_labels) fig.savefig("{}.png".format("_".join(all_labels)), bbox_inches="tight") plt.close()
def create_venn3(labels, names): fig, ax = venn.venn3(labels, names=names) #fig.savefig('venn4.png', bbox_inches='tight') plt.show()