def setGeneFeature(self, request, feature, n): if feature != "": if len(request.annotation) > 0: annotations = [ Annotation(name=ann.name, values=ann.values) for ann in request.annotation ] else: annotations = None vals, self.cellIndices = self.loom.get_gene_expression( gene_symbol=feature, log_transform=request.hasLogTransform, cpm_normalise=request.hasCpmTransform, annotation=annotations, logic=request.logic, ) if request.vmax[n] != 0.0: self.v_max[n] = request.vmax[n] else: self.v_max[n], self.max_v_max[ n] = data.get_99_and_100_percentiles(vals) vals = CellColorByFeatures.normalise_vals(vals, self.v_max[n], request.vmin[n]) self.features.append(vals) else: self.features.append(np.zeros(self.n_cells))
def setRegulonFeature(self, request, feature, n): if feature != "": if len(request.annotation) > 0: annotations = [ Annotation(name=ann.name, values=ann.values) for ann in request.annotation ] else: annotations = None vals, self.cellIndices = self.loom.get_auc_values( regulon=feature, annotation=annotations, logic=request.logic) if request.vmax[n] != 0.0: self.v_max[n] = request.vmax[n] else: self.v_max[n], self.max_v_max[ n] = data.get_99_and_100_percentiles(vals) if request.scaleThresholded: vals = np.array([ auc if auc >= request.threshold[n] else 0 for auc in vals ]) vals = CellColorByFeatures.normalise_vals( vals, self.v_max[n], request.vmin[n]) self.features.append(vals) else: self.features.append([ constant.UPPER_LIMIT_RGB if auc >= request.threshold[n] else 0 for auc in vals ]) else: self.features.append(np.zeros(self.n_cells))
def getCellColorByFeatures(self, request, context): start_time = time.time() try: loom = self.lfh.get_loom(loom_file_path=Path(request.loomFilePath)) except ValueError: return cell_color_by_features = ccbf.CellColorByFeatures(loom=loom) if len(request.annotation) > 0: annotations = [ Annotation(name=ann.name, values=ann.values) for ann in request.annotation ] else: annotations = None for n, feature in enumerate(request.feature): if request.featureType[n] == "gene": cell_color_by_features.setGeneFeature(request=request, feature=feature, n=n) elif request.featureType[n] == "regulon": cell_color_by_features.setRegulonFeature(request=request, feature=feature, n=n) elif request.featureType[n] == "annotation": cell_color_by_features.setAnnotationFeature( feature=feature, annotations=annotations, logic=request.logic) return cell_color_by_features.getReply() elif request.featureType[n] == "metric": cell_color_by_features.setMetricFeature(request=request, feature=feature, n=n) elif request.featureType[n].startswith("Clustering: "): cell_color_by_features.setClusteringFeature(request=request, feature=feature, n=n) if cell_color_by_features.hasReply(): return cell_color_by_features.getReply() else: cell_color_by_features.addEmptyFeature() logger.debug( "{0:.5f} seconds elapsed getting colours ---".format(time.time() - start_time)) return s_pb2.CellColorByFeaturesReply( color=None, compressedColor=cell_color_by_features.get_compressed_hex_vec(), hasAddCompressionLayer=True, vmax=cell_color_by_features.get_v_max(), maxVmax=cell_color_by_features.get_max_v_max(), cellIndices=cell_color_by_features.get_cell_indices(), )
def labels() -> Generator[FeatureLabel, None, None]: for i, annotation in enumerate(values): coords = loom.get_coordinates( coordinatesID=embedding, annotation=[Annotation(name=feature, values=[annotation])] ) yield FeatureLabel( label=annotation, colour=colours[i], coordinate=Coordinate(x=np.mean(coords["x"]), y=np.mean(coords["y"])), )
def getCoordinates(self, request, context): # request content loom = self.lfh.get_loom(loom_file_path=Path(request.loomFilePath)) if len(request.annotation) > 0: annotations = [ Annotation(name=ann.name, values=ann.values) for ann in request.annotation ] else: annotations = None c = loom.get_coordinates(coordinatesID=request.coordinatesID, annotation=annotations, logic=request.logic) return s_pb2.CoordinatesReply(x=c["x"], y=c["y"], cellIndices=c["cellIndices"])
def setClusteringFeature(self, request, feature, n): clusteringID = None clusterID = None logger.debug("Getting clustering: {0}".format(request.feature[n])) for clustering in self.meta_data["clusterings"]: if clustering["name"] == re.sub("^Clustering: ", "", request.featureType[n]): clusteringID = str(clustering["id"]) if request.feature[n] == "All Clusters": numClusters = max( self.loom.get_clustering_by_id(clusteringID)) legend = set() cluster_names_dict = self.loom.get_cluster_names( int(clusteringID)) for i in self.loom.get_clustering_by_id(clusteringID): if i == -1: self.hex_vec.append("XX" * 3) continue colour = constant.BIG_COLOR_LIST[i % len( constant.BIG_COLOR_LIST)] self.hex_vec.append(colour) legend.add((cluster_names_dict[i], colour)) values, colors = zip(*legend) self.legend = s_pb2.ColorLegend(values=values, colors=colors) if len(request.annotation) > 0: annotations = [ Annotation(name=ann.name, values=ann.values) for ann in request.annotation ] cellIndices = self.loom.get_anno_cells( annotations=annotations, logic=request.logic) self.hex_vec = np.array(self.hex_vec)[cellIndices] # Set the reply and break the for loop reply = s_pb2.CellColorByFeaturesReply(color=self.hex_vec, vmax=self.v_max, legend=self.legend) self.setReply(reply=reply) break else: for cluster in clustering["clusters"]: if request.feature[n] == cluster["description"]: clusterID = int(cluster["id"]) if clusteringID is None and clusterID is None and len( request.feature[n]) > 0: error_message = "The cluster '{0}' does not exist in the current active .loom. Clear the query to continue with SCope.".format( request.feature[n]) self.setReply( s_pb2.CellColorByFeaturesReply(error=s_pb2.ErrorReply( type="Value Error", message=error_message))) if clusteringID is not None and clusterID is not None: clusterIndices = self.loom.get_clustering_by_id( clusteringID) == clusterID clusterCol = np.array( [constant.UPPER_LIMIT_RGB if x else 0 for x in clusterIndices]) if len(request.annotation) > 0: annotations = [ Annotation(name=ann.name, values=ann.values) for ann in request.annotation ] cellIndices = self.loom.get_anno_cells(annotations=annotations, logic=request.logic) clusterCol = clusterCol[cellIndices] self.features.append(clusterCol)