|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""The RNA Expression Baseclass.""" |
|
|
|
|
|
import json |
|
import os |
|
import anndata as ad |
|
import pyarrow as pa |
|
import pandas as pd |
|
import numpy as np |
|
|
|
import datasets |
|
CITATION = """ |
|
Test |
|
""" |
|
|
|
DESCRIPTION = """ |
|
Test |
|
""" |
|
|
|
class RNAExpConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for RNAExpConfig.""" |
|
|
|
def __init__(self, features, data_url, citation, url, raw_counts="X", **kwargs): |
|
"""BuilderConfig for RNAExpConfig. |
|
Args: |
|
features: `list[string]`, list of the features that will appear in the |
|
feature dict. Should not include "label". |
|
data_url: `string`, url to download the zip file from. |
|
citation: `string`, citation for the data set. |
|
url: `string`, url for information about the data set. |
|
|
|
**kwargs: keyword arguments forwarded to super. |
|
""" |
|
|
|
|
|
super(RNAExpConfig, self).__init__(version=datasets.Version("0.0.1"), **kwargs) |
|
self.features = features |
|
self.data_url = data_url |
|
self.citation = citation |
|
self.url = url |
|
self.raw_counts = raw_counts |
|
self.batch = 1000 |
|
self.species = None |
|
|
|
|
|
|
|
|
|
|
|
class RNAExp(datasets.ArrowBasedBuilder): |
|
"""RNA Expression Baseclass.""" |
|
|
|
def _info(self): |
|
self.config = RNAExpConfig( |
|
name="human_yolk_sac", |
|
description = DESCRIPTION, |
|
features=["raw_counts",'LVL1', 'LVL2', 'LVL3'], |
|
raw_counts = "X", |
|
data_url="./data/17_04_24_YolkSacRaw_F158_WE_annots.h5ad", |
|
citation=CITATION, |
|
url="https://www.ebi.ac.uk/biostudies/arrayexpress/studies/E-MTAB-11673") |
|
|
|
features = {"raw_counts": datasets.features.Sequence(feature=datasets.Value("int32"))} |
|
|
|
|
|
|
|
for feature in self.config.features: |
|
if features.get(feature,None) is None: |
|
features[feature] = datasets.Value("string") |
|
|
|
|
|
|
|
return datasets.DatasetInfo( |
|
description= self.config.description, |
|
features=None, |
|
homepage=self.config.url, |
|
citation=self.config.citation, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
self.anndata_file = dl_manager.download_and_extract(self.config.data_url) |
|
|
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={"split": "train","expression_file": self.anndata_file,"batch_size":self.config.batch}, |
|
) |
|
] |
|
|
|
def _generate_examples(self, expression_file, split): |
|
|
|
|
|
adata = ad.read_h5ad(expression_file) |
|
self.genes_list = adata.var.index.str.lower().tolist() |
|
|
|
|
|
if self.config.raw_counts =="X": |
|
X = adata.X |
|
else: |
|
X = adata.var[raw_counts] |
|
num_cells = X.shape[0] |
|
for _id,cell in enumerate(X): |
|
example = {"raw_counts": cell.toarray().flatten()} |
|
|
|
for feature in self.config.features: |
|
if example.get(feature,None) is None: |
|
example[feature] = adata.obs[feature][_id] |
|
|
|
yield _id,example |
|
|
|
def _generate_tables(self, expression_file,batch_size,split): |
|
idx = 0 |
|
adata = ad.read_h5ad(expression_file,backed='r') |
|
genes = adata.var_names.str.lower().to_list() |
|
|
|
features = {"raw_counts": datasets.features.Sequence(datasets.features.Value("int32"),id = ",".join(adata.var.index.str.lower().tolist()))} |
|
for feature in self.config.features: |
|
if features.get(feature,None) is None: |
|
features[feature] = datasets.Value("string") |
|
|
|
self.info.features = datasets.Features(features) |
|
|
|
|
|
|
|
|
|
|
|
for batch in range(0,adata.shape[0],batch_size): |
|
chunk = adata.X[batch:batch+batch_size].todense().astype('int32') |
|
df = pd.DataFrame(chunk,columns=adata.var.index.str.lower()) |
|
df["raw_counts"] = [x for x in df.to_numpy()] |
|
df = df[["raw_counts"]] |
|
|
|
|
|
|
|
|
|
|
|
for feature in self.config.features: |
|
if feature != "raw_counts": |
|
df[feature] = adata.obs[feature][batch:batch+batch_size].tolist() |
|
|
|
|
|
|
|
pa_table = pa.Table.from_pandas(df) |
|
yield idx, pa_table |
|
idx += 1 |
|
|