示例#1
0
    def setAnnotationFeature(self, request, feature):
        md_annotation_values = self.loom.get_meta_data_annotation_by_name(
            name=feature)["values"]
        ca_annotation = self.loom.get_ca_attr_by_name(name=feature)
        ca_annotation_as_int = list(
            map(lambda x: md_annotation_values.index(str(x)), ca_annotation))
        num_annotations = max(ca_annotation_as_int)
        if num_annotations <= len(constant.BIG_COLOR_LIST):
            self.hex_vec = list(
                map(lambda x: constant.BIG_COLOR_LIST[x],
                    ca_annotation_as_int))
        else:
            raise ValueError(
                "The annotation {0} has too many unique values.".format(
                    feature))
        # Set the reply

        if len(request.annotation) > 0:
            cellIndices = self.loom.get_anno_cells(
                annotations=request.annotation, logic=request.logic)
            self.hex_vec = np.array(self.hex_vec)[cellIndices]
        reply = s_pb2.CellColorByFeaturesReply(
            color=self.hex_vec,
            vmax=self.v_max,
            legend=s_pb2.ColorLegend(
                values=md_annotation_values,
                colors=constant.BIG_COLOR_LIST[:len(md_annotation_values)]),
        )
        self.setReply(reply=reply)
示例#2
0
    def setAnnotationFeature(self,
                             feature: str,
                             annotations: Optional[List[Annotation]] = None,
                             logic: str = "OR"):
        md_annotation = self.loom.get_meta_data_annotation_by_name(
            name=feature)
        md_annotation_values = md_annotation["values"]
        ca_annotation = self.loom.get_ca_attr_by_name(name=feature)
        ca_annotation_as_int = list(
            map(lambda x: md_annotation_values.index(str(x)), ca_annotation))
        self.hex_vec = to_colours(ca_annotation_as_int,
                                  color_list=md_annotation["colors"]
                                  if "colors" in md_annotation else None)

        if annotations is not None:
            cellIndices = self.loom.get_anno_cells(annotations=annotations,
                                                   logic=logic)
            self.hex_vec = np.array(self.hex_vec)[cellIndices]

        reply = s_pb2.CellColorByFeaturesReply(
            color=self.hex_vec,
            vmax=self.v_max,
            legend=s_pb2.ColorLegend(
                values=md_annotation_values,
                colors=to_colours(
                    range(len(md_annotation_values)),
                    color_list=md_annotation["colors"]
                    if "colors" in md_annotation else None,
                ),
            ),
        )
        self.setReply(reply=reply)
示例#3
0
    def setClusteringFeature(self, request, feature, n, secret=None):
        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))
                    if numClusters <= 245:
                        self.legend = set()
                        clustering_meta = self.loom.get_meta_data_clustering_by_id(
                            int(clusteringID), secret=secret)
                        cluster_dict = {
                            int(x["id"]): x["description"]
                            for x in clustering_meta["clusters"]
                        }
                        for i in self.loom.get_clustering_by_id(clusteringID):
                            self.hex_vec.append(constant.BIG_COLOR_LIST[i])
                            self.legend.add(
                                (cluster_dict[i], constant.BIG_COLOR_LIST[i]))
                        self.legend = s_pb2.ColorLegend(
                            values=[x[0] for x in self.legend],
                            colors=[x[1] for x in self.legend])
                    else:
                        interval = int(RGB_COLORS / numClusters)
                        self.hex_vec = [
                            hex(i)[2:].zfill(6)
                            for i in range(0, numClusters, interval)
                        ]
                    if len(request.annotation) > 0:
                        cellIndices = self.loom.get_anno_cells(
                            annotations=request.annotation,
                            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:
                cellIndices = self.loom.get_anno_cells(
                    annotations=request.annotation, logic=request.logic)
                clusterCol = clusterCol[cellIndices]
            self.features.append(clusterCol)
示例#4
0
    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)