def main():
    gn = Granatum()

    adata = gn.ann_data_from_assay(gn.get_import("assay"))
    num_cells_to_sample = gn.get_arg("num_cells_to_sample")
    random_seed = gn.get_arg("random_seed")

    np.random.seed(random_seed)

    num_cells_before = adata.shape[0]
    num_genes_before = adata.shape[1]

    if num_cells_to_sample > 0 and num_cells_to_sample < 1:
        num_cells_to_sample = round(num_cells_before * num_cells_to_sample)
    else:

        num_cells_to_sample = round(num_cells_to_sample)

    if num_cells_to_sample > num_cells_before:
        num_cells_to_sample = num_cells_before

    if num_cells_to_sample < 1:
        num_cells_to_sample = 1

    sampled_cells_idxs = np.sort(np.random.choice(num_cells_before, num_cells_to_sample, replace=False))

    adata = adata[sampled_cells_idxs, :]

    gn.add_result(
        "\n".join(
            [
                "The assay before down-sampling has **{}** cells and {} genes.".format(
                    num_cells_before, num_genes_before
                ),
                "",
                "The assay after down-sampling has **{}** cells and {} genes.".format(adata.shape[0], adata.shape[1]),
            ]
        ),
        type="markdown",
    )

    gn.export(gn.assay_from_ann_data(adata), "Down-sampled Assay", dynamic=False)

    gn.commit()
Example #2
0
def main():
    gn = Granatum()

    adata = gn.ann_data_from_assay(gn.get_import("assay"))
    min_cells_expressed = gn.get_arg("min_cells_expressed")
    min_mean = gn.get_arg("min_mean")
    max_mean = gn.get_arg("max_mean")
    min_disp = gn.get_arg("min_disp")
    max_disp = gn.get_arg("max_disp")

    num_genes_before = adata.shape[1]

    sc.pp.filter_genes(adata, min_cells=min_cells_expressed)

    filter_result = sc.pp.filter_genes_dispersion(
        adata.X, flavor='seurat', min_mean=math.log(min_mean), max_mean=math.log(max_mean), min_disp=min_disp, max_disp=max_disp,
    )
    adata = adata[:, filter_result.gene_subset]

    sc.pl.filter_genes_dispersion(filter_result)
    gn.add_current_figure_to_results(
        "Each dot represent a gene. The gray dots are the removed genes. The x-axis is log-transformed.",
        zoom=3,
        dpi=50,
        height=400,
    )

    gn.add_result(
        "\n".join(
            [
                "Number of genes before filtering: **{}**".format(num_genes_before),
                "",
                "Number of genes after filtering: **{}**".format(adata.shape[1]),
            ]
        ),
        type="markdown",
    )

    gn.export(gn.assay_from_ann_data(adata), "Filtered Assay", dynamic=False)

    gn.commit()
def main():
  gn = Granatum()

  adata = gn.ann_data_from_assay(gn.get_import('assay'))
  outliers = gn.get_arg('outliers')

  num_cells_before = adata.shape[0]

  kept_cell_ids = adata.obs_names.drop(outliers, errors='ignore').values

  adata = adata[kept_cell_ids, :]

  gn.export_statically(gn.assay_from_ann_data(adata), 'Outlier removed assay')
  gn.add_result(
    'You removed {} outliers from {} cells, the result assay has {} cells (and {} genes).'.format(
      len(outliers), num_cells_before, adata.shape[0], adata.shape[1]
    ),
    type='markdown'
  )

  gn.commit()