def run(self) -> None: logging = cg.logging(self, True) with self.output().temporary_path() as out_dir: logging.info("Exporting cluster data") if not os.path.exists(out_dir): os.mkdir(out_dir) with loompy.connect(self.input()[0].fn) as dsagg: dsagg.export( os.path.join(out_dir, f"L6_R{self.rank}_expression.tab")) dsagg.export(os.path.join(out_dir, f"L6_R{self.rank}_enrichment.tab"), layer="enrichment") dsagg.export(os.path.join(out_dir, f"L6_R{self.rank}_enrichment_q.tab"), layer="enrichment_q") dsagg.export(os.path.join(out_dir, f"L6_R{self.rank}_trinaries.tab"), layer="trinaries") logging.info("Plotting manifold graph with auto-annotation") with loompy.connect(self.input()[1].fn) as ds: cg.plot_graph( ds, os.path.join(out_dir, f"L6_R{self.rank}_manifold.aa.png"), list(dsagg.ca.AutoAnnotation)) logging.info( "Plotting manifold graph with auto-auto-annotation") cg.plot_graph( ds, os.path.join(out_dir, f"L6_R{self.rank}_manifold.aaa.png"), list(dsagg.ca.MarkerGenes)) logging.info("Plotting manifold graph with taxon names") cg.plot_graph( ds, os.path.join(out_dir, f"L6_R{self.rank}_manifold.names.png"), list(dsagg.ca[f"TaxonomyRank{self.rank}"])) logging.info("Plotting marker heatmap") cg.plot_markerheatmap(ds, dsagg, n_markers_per_cluster=self.n_markers, out_file=os.path.join( out_dir, f"L6_R{self.rank}_heatmap.pdf"))
def run(self) -> None: logging = cg.logging(self, True) logging.info("Exporting cluster data") with self.output().temporary_path() as out_dir: if not os.path.exists(out_dir): os.mkdir(out_dir) with loompy.connect(self.input()[0].fn) as dsagg: logging.info("Computing auto-annotation") aa = cg.AutoAnnotator(root=am.paths().autoannotation) aa.annotate_loom(dsagg) aa.save_in_loom(dsagg) dsagg.export(os.path.join(out_dir, "L1_" + self.tissue + "_expression.tab")) dsagg.export(os.path.join(out_dir, "L1_" + self.tissue + "_enrichment.tab"), layer="enrichment") dsagg.export(os.path.join(out_dir, "L1_" + self.tissue + "_enrichment_q.tab"), layer="enrichment_q") dsagg.export(os.path.join(out_dir, "L1_" + self.tissue + "_trinaries.tab"), layer="trinaries") ds = loompy.connect(self.input()[1].fn) logging.info("Plotting MKNN graph") cg.plot_knn(ds, os.path.join(out_dir, "L1_" + self.tissue + "_manifold.mknn.png")) # logging.info("Plotting Louvain resolution") # cg.plot_louvain(ds, os.path.join(out_dir, "L1_" + self.tissue + "_manifold.louvain.png")) try: logging.info("Plotting manifold graph with classes") cg.plot_classes(ds, os.path.join(out_dir, "L1_" + self.tissue + "_manifold.classes.png")) except Exception: pass logging.info("Plotting manifold graph with auto-annotation") tags = list(dsagg.col_attrs["AutoAnnotation"]) cg.plot_graph(ds, os.path.join(out_dir, "L1_" + self.tissue + "_manifold.aa.png"), tags) logging.info("Plotting manifold graph with auto-auto-annotation") tags = list(dsagg.col_attrs["MarkerGenes"]) cg.plot_graph(ds, os.path.join(out_dir, "L1_" + self.tissue + "_manifold.aaa.png"), tags) logging.info("Plotting marker heatmap") cg.plot_markerheatmap(ds, dsagg, n_markers_per_cluster=self.n_markers, out_file=os.path.join(out_dir, "L1_" + self.tissue + "_heatmap.pdf"))
def run(self) -> None: logging = cg.logging(self) with self.output().temporary_path() as out_dir: logging.info("Exporting cluster data") if not os.path.exists(out_dir): os.mkdir(out_dir) dsagg = loompy.connect(self.input()[0].fn) logging.info("Computing auto-annotation") aa = cg.AutoAnnotator(root=am.paths().autoannotation) aa.annotate_loom(dsagg) aa.save_in_loom(dsagg) dsagg.export( os.path.join(out_dir, "L3_" + self.target + "_expression.tab")) dsagg.export(os.path.join(out_dir, "L3_" + self.target + "_enrichment.tab"), layer="enrichment") dsagg.export(os.path.join( out_dir, "L3_" + self.target + "_enrichment_q.tab"), layer="enrichment_q") dsagg.export(os.path.join(out_dir, "L3_" + self.target + "_trinaries.tab"), layer="trinaries") logging.info("Plotting manifold graph with auto-annotation") tags = list(dsagg.col_attrs["AutoAnnotation"][np.argsort( dsagg.col_attrs["Clusters"])]) ds = loompy.connect(self.input()[1].fn) cg.plot_graph( ds, os.path.join(out_dir, "L3_" + self.target + "_manifold.aa.png"), tags) logging.info("Plotting manifold graph with auto-auto-annotation") tags = list(dsagg.col_attrs["MarkerGenes"][np.argsort( dsagg.col_attrs["Clusters"])]) cg.plot_graph( ds, os.path.join(out_dir, "L3_" + self.target + "_manifold.aaa.png"), tags) logging.info("Plotting marker heatmap") cg.plot_markerheatmap(ds, dsagg, n_markers_per_cluster=self.n_markers, out_file=os.path.join( out_dir, "L3_" + self.target + "_heatmap.pdf")) logging.info("Computing discordance distances") pep = 0.05 n_labels = dsagg.shape[1] def discordance_distance(a: np.ndarray, b: np.ndarray) -> float: """ Number of genes that are discordant with given PEP, divided by number of clusters """ return np.sum((1 - a) * b + a * (1 - b) > 1 - pep) / n_labels data = dsagg.layer["trinaries"][:n_labels * 10, :].T D = squareform(pdist(data, discordance_distance)) with open( os.path.join(out_dir, "L3_" + self.target + "_distances.txt"), "w") as f: f.write(str(np.diag(D, k=1)))
def run(self) -> None: logging = cg.logging(self, True) with self.output().temporary_path() as out_dir: logging.info("Exporting cluster data") if not os.path.exists(out_dir): os.mkdir(out_dir) with loompy.connect(self.input()[0].fn) as dsagg: logging.info("Exporting tab files") dsagg.export( os.path.join( out_dir, "L2_" + self.major_class + "_" + self.tissue + "_expression.tab")) dsagg.export(os.path.join( out_dir, "L2_" + self.major_class + "_" + self.tissue + "_enrichment.tab"), layer="enrichment") dsagg.export(os.path.join( out_dir, "L2_" + self.major_class + "_" + self.tissue + "_enrichment_q.tab"), layer="enrichment_q") dsagg.export(os.path.join( out_dir, "L2_" + self.major_class + "_" + self.tissue + "_trinaries.tab"), layer="trinaries") logging.info("Plotting manifold graph with auto-annotation") tags = list(dsagg.col_attrs["AutoAnnotation"]) with loompy.connect(self.input()[1].fn) as ds: cg.plot_graph( ds, os.path.join( out_dir, "L2_" + self.major_class + "_" + self.tissue + "_manifold.aa.png"), tags) logging.info( "Plotting manifold graph with auto-auto-annotation") tags = list(dsagg.col_attrs["MarkerGenes"][np.argsort( dsagg.col_attrs["Clusters"])]) cg.plot_graph( ds, os.path.join( out_dir, "L2_" + self.major_class + "_" + self.tissue + "_manifold.aaa.png"), tags) logging.info("Plotting manifold graph with classes") cg.plot_classes( ds, os.path.join( out_dir, "L2_" + self.major_class + "_" + self.tissue + "_manifold.classes.png")) logging.info("Plotting marker heatmap") cg.plot_markerheatmap(ds, dsagg, n_markers_per_cluster=10, out_file=os.path.join( out_dir, "L2_" + self.major_class + "_" + self.tissue + "_heatmap.pdf")) logging.info("Plotting latent factors") cg.plot_factors(ds, base_name=os.path.join( out_dir, "L2_" + self.major_class + "_" + self.tissue + "_factors"))
def run(self) -> None: logging = cg.logging(self, True) with self.output().temporary_path() as out_dir: logging.info("Exporting cluster data") if not os.path.exists(out_dir): os.mkdir(out_dir) with loompy.connect(self.input()[0].fn) as dsagg: dsagg.export( os.path.join( out_dir, f"L6_R{self.rank}_({self.taxon})_expression.tab")) dsagg.export(os.path.join( out_dir, f"L6_R{self.rank}_({self.taxon})_enrichment.tab"), layer="enrichment") dsagg.export(os.path.join( out_dir, f"L6_R{self.rank}_({self.taxon})_enrichment_q.tab"), layer="enrichment_q") dsagg.export(os.path.join( out_dir, f"L6_R{self.rank}_({self.taxon})_trinaries.tab"), layer="trinaries") logging.info("Plotting manifold graph with auto-annotation") with loompy.connect(self.input()[1].fn) as ds: cg.plot_graph( ds, os.path.join( out_dir, f"L6_R{self.rank}_({self.taxon})_manifold.aa.png"), list(dsagg.ca.AutoAnnotation)) logging.info( "Plotting manifold graph with auto-auto-annotation") cg.plot_graph( ds, os.path.join( out_dir, f"L6_R{self.rank}_({self.taxon})_manifold.aaa.png" ), list(dsagg.ca.MarkerGenes)) logging.info("Plotting manifold graph with cluster names") cg.plot_graph( ds, os.path.join( out_dir, f"L6_R{self.rank}_({self.taxon})_manifold.names.png" ), list(dsagg.ca.ClusterName)) logging.info("Plotting marker heatmap") cg.plot_markerheatmap( ds, dsagg, n_markers_per_cluster=self.n_markers, out_file=os.path.join( out_dir, f"L6_R{self.rank}_({self.taxon})_heatmap.pdf")) size = 200000 / ds.shape[1] fig = plt.figure(figsize=(3, 3)) ax = fig.add_axes([0, 0, 1, 1]) ax.axis('off') ix = 0 if self.rank == 3: colors = cg.colorize(np.unique(ds.ca.ClusterName)) for cluster in np.unique(ds.ca.ClusterName): cells = ds.ca.ClusterName == cluster plt.scatter(x=ds.ca._X[cells], y=ds.ca._Y[cells], s=size, c=colors[ix, :], marker='.', label=cluster, alpha=0.5, lw=0) ix += 1 else: colors = cg.colorize(np.unique(ds.ca.TaxonomyRank4)) for taxon4 in np.unique(ds.ca.TaxonomyRank4): cells = ds.ca.TaxonomyRank4 == taxon4 plt.scatter(x=ds.ca._X[cells], y=ds.ca._Y[cells], s=size, c=colors[ix, :], marker='.', label=taxon4, alpha=0.5, lw=0) ix += 1 lgnd = ax.legend(fontsize=10, labelspacing=0.2, loc="upper left", bbox_to_anchor=(1, 1), frameon=False) for handle in lgnd.legendHandles: handle.set_sizes([250]) handle.set_alpha(1) plt.savefig(os.path.join( out_dir, f"L6_R{self.rank}_({self.taxon})_manifold.pretty.png"), dpi=600, transparent=True, bbox_extra_artists=(lgnd, ), bbox_inches='tight') plt.close()