I'm trying to do simple filter for single data that is stored in h5ad
file format using this
import scanpy as sc
sc.pp.filter_cells(adata, min_genes=500)
sc.pp.filter_genes(adata, min_cells=10)
print(adata.n_obs, adata.n_vars)
when I saved the filtered data and see the size of my original data compare it, the size of the filtered data is more, but the dimension etc are reduced due to filtering.
So is it something wrong am I doing?
UPDATE
adata = sc.read_h5ad('/Single_cell/cerebellar_development/508cf892-174c-45ab-b2dc-05f54f1ee7ed/aldinger20.processed.h5ad')
adata
AnnData object with n_obs × n_vars = 69174 × 49166
obs: 'orig.ident', 'nCount_RNA', 'nFeature_RNA', 'sample_id', 'percent.mito', 'S.Score', 'G2M.Score', 'Phase', 'CC.Difference', 'nCount_SCT', 'nFeature_SCT', 'age', 'figure_clusters', 'sex', 'type', 'experiment', 'fig_cell_type'
var: 'name'
obsm: 'X_pca', 'X_umap', 'X_tsne'
sc.pp.filter_cells(adata, min_genes=500)
sc.pp.filter_genes(adata, min_cells=10)
print(adata.n_obs, adata.n_vars)
results_file = 'adata.h5ad'
adata.write(results_file)
FULL CODE
updated version: h5ad filter, added layers to make the filtered file usable for seurat to read and make a correct seurat object
#https://github.com/satijalab/seurat/issues/2485
import scanpy as sc
import numpy as np
import pandas as pd
import time
from ipywidgets import interact, interactive, widgets
from IPython.display import display, HTML
def filter_and_save_h5ad(h5ad_file, min_genes, min_cells):
# h5ad file read
adata = sc.read_h5ad(h5ad_file)
# cells and genes filter parameter from user
sc.pp.filter_cells(adata, min_genes=min_genes)
sc.pp.filter_genes(adata, min_cells=min_cells)
# Add 'counts' layer
adata.layers['counts'] = adata.X.copy()
# add 'winsorization' layer
adata.layers['winsorized'] = adata.X.copy()
# filtered dataset save
filtered_file = h5ad_file.replace('.h5ad', '_filtered.h5ad')
adata.write(filtered_file)
print(f"Filtered dataset saved to {filtered_file}")
# input widgets
h5ad_file_widget = widgets.Text(description='File_add:')
min_genes_widget = widgets.IntText(description='Min_Genes:')
min_cells_widget = widgets.IntText(description='Min_Cells:')
button = widgets.Button(description='Filter_Save')
# timer widget
time_widget = widgets.HTML(value="<p>Time passed away: 0 seconds</p>")
def on_button_clicked(click_me):
h5ad_file = h5ad_file_widget.value.strip()
min_genes = min_genes_widget.value
min_cells = min_cells_widget.value
# timer
start_time = time.time()
filter_and_save_h5ad(h5ad_file, min_genes, min_cells)
# Calculate and update the elapsed time
elapsed_time = time.time() - start_time
time_widget.value = f"<p>Time passed away: {elapsed_time} seconds</p>"
button.on_click(on_button_clicked)
# Display the widgets and button
display(h5ad_file_widget, min_genes_widget, min_cells_widget, button, time_widget)