gastrulation_mmusculus / gastrulation_mmusculus.py
bputzeys's picture
Upload folder using huggingface_hub
a9aea9f verified
import anndata as ad
import pyarrow as pa
import pandas as pd
import datasets
# parameters per dataset
CITATION = """
Blanca Pijuan-Sala & Jonathan Griffiths (2018).
Timecourse single-cell RNAseq of whole mouse embryos harvested between days 6.5 and 8.5 of development.
BioStudies, E-MTAB-6967.
Retrieved from https://www.ebi.ac.uk/biostudies/arrayexpress/studies/E-MTAB-6967"""
DESCRIPTION = """
We have captured 100,000 single cells for single-cell RNAseq from whole mouse embryos during gastrulation and organogenesis,
spanning days 6.5 to 8.5 of development, including embryonic and extraembryonic tissues.
Cells were sampled every six hours, providing a continuous molecular characterisation of these processes.
Cell libraries were prepared using the 10X Genomics Chromium platform."""
URL = "https://www.ebi.ac.uk/biostudies/arrayexpress/studies/E-MTAB-6967"
RAW_COUNTS = "X"
DATA_URL = "./data/mmusculus_gastrulation.h5ad"
FEATURES_TO_INCLUDE = ["cell_type"]
class RNAExp(datasets.ArrowBasedBuilder):
"""RNA Expression Baseclass."""
def _info(self):
self.batch = 10000
# create a dictionary of features where raw_counts are ints and the rest are strings
features = {"raw_counts": datasets.features.Sequence(datasets.features.Value("uint32")),"rows": datasets.features.Sequence(datasets.features.Value("uint32")),"size":datasets.Value("uint32")}
for feature in FEATURES_TO_INCLUDE:
if not features.get(feature):
features[feature] = datasets.Value("string")
return datasets.DatasetInfo(
description = DESCRIPTION,
features = datasets.Features(features),
homepage = URL,
citation = CITATION
)
def _split_generators(self, dl_manager):
self.anndata_file = dl_manager.download_and_extract(DATA_URL)
adata = ad.read_h5ad(self.anndata_file, backed = "r")
demarcation = int(len(adata)*80/100)
return [
datasets.SplitGenerator(
name = datasets.Split.TRAIN,
gen_kwargs = {"split": "train", "adata": adata[:demarcation], "batch_size":self.batch},
),
datasets.SplitGenerator(
name = datasets.Split.TEST,
gen_kwargs = {"split": "test", "adata": adata[demarcation:], "batch_size":self.batch},
)
]
def _generate_tables(self, adata, batch_size, split):
idx = 0
# save the gene names as the id for the raw_counts feature
self.info.features["raw_counts"].id = f"{','.join(adata.var.index.tolist())}"
# iterate over the data in batches
for batch in range(0, adata.shape[0], batch_size):
# raw counts
if RAW_COUNTS == "X":
chunk = adata.X[batch:batch+batch_size].tolil().astype('uint32')
elif RAW_COUNTS == "raw.X":
chunk = adata.raw.X[batch:batch+batch_size].tolil().astype('uint32')
else:
raise("Not valid raw_counts")
df = pd.DataFrame([chunk.data,chunk.rows]).T
df.columns = ['raw_counts','rows']
df['size'] = chunk.shape[1]
# other features are all mapped to a list of strings
for feature in FEATURES_TO_INCLUDE:
df[feature] = list(map(str, adata.obs[feature][batch:batch+batch_size].tolist()))
pa_table = pa.Table.from_pandas(df)
yield idx, pa_table
idx += 1