예제 #1
0
    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))
예제 #2
0
    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))
예제 #3
0
    def getVmax(self, request, context):
        v_max = np.zeros(3)
        max_v_max = np.zeros(3)

        for n, feature in enumerate(request.feature):
            f_v_max = 0
            f_max_v_max = 0
            if feature != "":
                for loomFilePath in request.loomFilePath:
                    l_v_max = 0
                    l_max_v_max = 0
                    loom = self.lfh.get_loom(loom_file_path=Path(loomFilePath))
                    if request.featureType[n] == "gene":
                        vals, _ = loom.get_gene_expression(
                            gene_symbol=feature,
                            log_transform=request.hasLogTransform,
                            cpm_normalise=request.hasCpmTransform,
                        )
                        l_v_max, l_max_v_max = data.get_99_and_100_percentiles(
                            vals)
                    if request.featureType[n] == "regulon":
                        vals, _ = loom.get_auc_values(regulon=feature)
                        l_v_max, l_max_v_max = data.get_99_and_100_percentiles(
                            vals)
                    if request.featureType[n] == "metric":
                        vals, _ = loom.get_metric(
                            metric_name=feature,
                            log_transform=request.hasLogTransform,
                            cpm_normalise=request.hasCpmTransform,
                        )
                        l_v_max, l_max_v_max = data.get_99_and_100_percentiles(
                            vals)
                    if l_v_max > f_v_max:
                        f_v_max = l_v_max
                if l_max_v_max > f_max_v_max:
                    f_max_v_max = l_max_v_max
            v_max[n] = f_v_max
            max_v_max[n] = f_max_v_max
        return s_pb2.VmaxReply(vmax=v_max, maxVmax=max_v_max)
예제 #4
0
 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),
         )
예제 #5
0
def test_vmax(values):
    expected_100 = np.amax(values)
    expected_99 = np.clip(np.percentile(values, 99), 0.01, expected_100)
    assert data.get_99_and_100_percentiles(values) == (expected_99,
                                                       expected_100)