import anndata as ad import pyarrow as pa import pandas as pd import datasets # parameters per dataset CITATION = """ Simone Webb, Muzlifah Haniffa & Emily Stephenson (2022). Human fetal yolk sac scRNA-seq data (sample ID: F158 for Haniffa Lab; 16099 for HDBR). BioStudies, E-MTAB-11673. Retrieved from https://www.ebi.ac.uk/biostudies/arrayexpress/studies/E-MTAB-11673 """ URL = "https://www.ebi.ac.uk/biostudies/arrayexpress/studies/E-MTAB-11673" DESCRIPTION = """Investigating the blood, immune and stromal cells present in a human fetal embryo in a world first single cell transcriptomic atlas. The embryo was dissected into 12 coronal sections, yolk sac, and yolk sac stalk. Live single cells sorted, with cell suspension then undergoing 10x chromium 5 prime scRNA-seq. This accession contains the yolk sac and yolk sac stalk data from this embryo. A matched accession contains the coronal section data. Lane "WS_wEMB12142156" (from yolk sac) was excluded from downstream analysis due to low fraction reads in cells post-CellRanger QC. Termination procedure for this embryo was medical. The F158_[features...barcodes...matrix].[tsv...mtx].gz files attached to this accession represent raw count data from all the 10x lanes in this accession combined, and as output from CellRanger filtered matrices (CellRanger version 6.0.1 using human reference genome GRCh38-2020-A). One set of count matrices relates to the yolk sac data, and one set of count matrices relates to the yolk sac stalk data.""" RAW_COUNTS = "X" DATA_URL = "./data/17_04_24_YolkSacRaw_F158_WE_annots.h5ad" FEATURES_TO_INCLUDE = ['LVL1', 'LVL2', 'LVL3'] class RNAExp(datasets.ArrowBasedBuilder): """RNA Expression Baseclass.""" def _info(self): self.batch = 1000 # 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