Example #1
0
    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 update_state(self, step, status_code, status_message, values):
     state = GeneSetEnrichment.State(step=step,
                                     status_code=status_code,
                                     status_message=status_message,
                                     values=values)
     logger.debug("Status: {0}".format(state.get_status_message()))
     if state.get_values() is None:
         return s_pb2.GeneSetEnrichmentReply(
             progress=s_pb2.Progress(value=state.get_step(),
                                     status=state.get_status_message()),
             isDone=False,
             cellValues=s_pb2.CellColorByFeaturesReply(color=[],
                                                       vmax=[],
                                                       maxVmax=[],
                                                       cellIndices=[]),
         )
     else:
         vmax = np.zeros(3)
         max_vmax = np.zeros(3)
         aucs = state.get_values()
         _vmax = data.get_99_and_100_percentiles(values=aucs)
         vmax[0] = _vmax[0]
         max_vmax[0] = _vmax[1]
         vals = aucs / vmax[0]
         vals = (((constant.UPPER_LIMIT_RGB - constant.LOWER_LIMIT_RGB) *
                  (vals - min(vals))) /
                 (1 - min(vals))) + constant.LOWER_LIMIT_RGB
         hex_vec = [
             "null" if r == g == b == 0 else "{0:02x}{1:02x}{2:02x}".format(
                 int(r), int(g), int(b)) for r, g, b in zip(
                     vals, np.zeros(len(aucs)), np.zeros(len(aucs)))
         ]
         cell_indices = list(range(self.loom.get_nb_cells()))
         return s_pb2.GeneSetEnrichmentReply(
             progress=s_pb2.Progress(value=state.get_step(),
                                     status=state.get_status_message()),
             isDone=True,
             cellValues=s_pb2.CellColorByFeaturesReply(
                 color=hex_vec,
                 vmax=vmax,
                 maxVmax=max_vmax,
                 cellIndices=cell_indices),
         )
Example #3
0
    def getCellColorByFeatures(self, request, context):
        start_time = time.time()
        try:
            loom = self.lfh.get_loom(loom_file_path=request.loomFilePath)
        except ValueError:
            return

        cell_color_by_features = ccbf.CellColorByFeatures(loom=loom)

        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)
                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()

        print("Debug: %s seconds elapsed ---" % (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())
Example #4
0
 def setAnnotationFeature(self, 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
     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)
Example #5
0
    def setAnnotationFeature(self,
                             feature: str,
                             annotations: Optional[List[Annotation]] = None,
                             logic: str = "OR"):
        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))
        self.hex_vec = to_colours(ca_annotation_as_int)

        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)))),
        )
        self.setReply(reply=reply)
Example #6
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)
Example #7
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)