# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Reyfman data

import scanpy as sc
import glob
import anndata
import scIB
import pandas as pd
import numpy as np

files = glob.glob('../../../data/HLCA_extended/extension_datasets/raw/Reyfman/*filtered*')

files

adatas = {}
for i in files:
    name = "_".join(i.split('/')[9].split('_')[1:3])
    sub_id = i.split('/')[9].split('_')[0]
    adatas[name] = sc.read_10x_h5(i)
    adatas[name].obs['condition']=name.split('_')[0]
    #adatas[name].obs['condition']=name.split('_')[0]
    adatas[name].obs['subject_ID']=name
    adatas[name].var_names_make_unique()
    #adatas[name].obs_names = adatas[name].obs_names+name

adata = anndata.concat(adatas)

adata

adata.obs_names_make_unique()

adata.obs['dataset']='MisharinBudinger2019_disease'
adata.obs['study']='MisharinBudinger2019'

adata.obs

scIB.pp.summarize_counts(adata)

scIB.pp.plot_QC(adata, gene_threshold=(800,5000), count_threshold=(10,40000))

sc.pp.filter_cells(adata, max_genes=7000)

sc.pp.filter_genes(adata, min_counts=1)

adata.obs=adata.obs.iloc[:,0:4]

adata.obs['preprocessing']='filter cells to max_genes=7000 and genes to min_counts=1'

adata.obs.condition.unique()



# # Assign further obs covariates

adata

adata.obs['sample'] = adata.obs['subject_ID']

# # Load annotations

idents = pd.read_csv('../../../data/HLCA_extended/extension_datasets/raw/Reyfman/Reyfman_2019_idents.csv')
samps = pd.read_csv('../../../data/HLCA_extended/extension_datasets/raw/Reyfman/Reyfman_samples.csv')

idents.index = idents['Unnamed: 0']
samps.index = samps['paper_ID']

# + tags=[]
samps
idents
adata.obs
# -

#Map obs names
adata.obs_names = [samps.loc[adata.obs['sample'][idx]]['library_ID']+'_'+idx.split('-')[0] for idx in adata.obs_names]

# + tags=[]
adata.obs
# -

adata.obs['original_celltype_ann'] = idents['<EMAIL>']

adata.obs

adata.obs['original_celltype_ann'].value_counts()

np.sum(adata.obs['original_celltype_ann'].isna())

# ### Filter out NA celltype cells

adata = adata[~adata.obs['original_celltype_ann'].isin([np.nan])].copy()

adata
np.sum(adata_nona.obs['original_celltype_ann'].isna())

# # Write full object

adata

adata.write('../../../data/HLCA_extended/extension_datasets/ready/full/reyfman_disease.h5ad')



# # Subset data

adata = sc.read('ready/full/reyfman_disease.h5ad')
gene_set = pd.read_csv('genes_for_mapping.csv')

# cd ../scripts

import preprocessing as pp

# cd ../query_datasets/

adata_sub = pp.subset_and_pad_adata(gene_set, adata)

# # Write output

adata_sub.write('../../../data/HLCA_extended/extension_datasets/ready/subsetted/reyfman_disease_sub.h5ad')

StarcoderdataJupyterScriptsDedupFiltered