%matplotlib inline
%load_ext autoreload
%autoreload 2
%load_ext line_profiler
import scanpy as sc
import random
from unicoord import scu
from unicoord.visualization import draw_loss_curves
import torch
from line_profiler import LineProfiler
sc.settings.verbosity = 3 # verbosity: errors (0), warnings (1), info (2), hints (3)
sc.logging.print_header()
# sc.settings.set_figure_params(dpi=80, facecolor='white')
sc.settings.set_figure_params(vector_friendly=False)
adata = sc.read_h5ad(r'F:\h5ad\tabularSapiens\obs_and_var.h5ad')
genes = pd.read_table('./protein_coding_genes.txt', header=None)[0]
g = list(set(genes) & set(adata.var_names))
adata
scu.model_unicoord_in_adata(adata, genes_used=g,
n_cont=50, n_diff=0, n_clus = [],
obs_fitting=['cell_ontology_class','anatomical_information',
'method','organ_tissue','donor','gender','compartment'],
min_obs = 2000)
unc_stuffs = adata.uns['unc_stuffs']
for idx in range(3):
bdata = sc.read_h5ad(r'F:\h5ad\tabularSapiens\TBSP_%s.h5ad'%(str(idx)))
scu.train_unicoord_in_adata(bdata, unc_stuffs=unc_stuffs,
epochs=10, chunk_size=20000, slot = "cur")
torch.cuda.empty_cache()
fig = draw_loss_curves(unc_stuffs['loss'])
# if save_figs:
# fig.savefig(os.path.join(savePath, 'img', 'fig1_lossCurves.png'))
fig.show()
scu.write_scu_h5ad(adata, './pretrained_models/TBSP.h5ad', only_model=True)
cdata = scu.load_scu_h5ad('./pretrained_models/TBSP.h5ad')
cdata
ddata = sc.read_h5ad(r'D:\hECA\Liver_cancer.pp.h5ad')
ddata = ddata.raw.to_adata()
sc.pp.normalize_total(ddata, target_sum=1e4 ,exclude_highly_expressed= True)
sc.pp.log1p(ddata)
ddata
scu.predcit_unicoord_in_adata(ddata,adata_ref=cdata,chunk_size=1000)
sc.pl.embedding(ddata, 'X_umap',legend_loc='on data', legend_fontsize=10,
color= ['Type']+[s+'_unc_infered' for s in \
['cell_ontology_class','anatomical_information',
'method','organ_tissue','donor','gender','compartment']], ncols=2)
ddata.obs.groupby(['cell_ontology_class_unc_infered','Type']).size().unstack().to_csv('TBSP_HCC.csv')
cdata = scu.load_scu_h5ad('./pretrained_models/TBSP.h5ad')
cdata
ddata = sc.read_h5ad(r'D:\hECA\Lung_cancer_nLung.pp.h5ad')
ddata = ddata.raw.to_adata()
sc.pp.normalize_total(ddata, target_sum=1e4 ,exclude_highly_expressed= True)
sc.pp.log1p(ddata)
ddata
scu.predcit_unicoord_in_adata(ddata,adata_ref=cdata,chunk_size=1000)
sc.pl.embedding(ddata, 'X_umap',legend_loc='on data', legend_fontsize=10,
color= ['Cell_type.refined','Cell_subtype']+[s+'_unc_infered' for s in \
['cell_ontology_class','anatomical_information',
'method','organ_tissue','donor','gender','compartment']], ncols=2)
ddata.obs.groupby(['cell_ontology_class_unc_infered','Cell_subtype']).size().unstack().to_csv('TBSP_LUAD.csv')
RP_genes = [g for g in cdata.uns['unc_stuffs']['genes_used'] if g.startswith("RPL") or g.startswith("RPS")]
pd.Series(RP_genes).to_csv('./RP_genes.csv')
ddata.obs.columns
ddata.obs['Cell_subtype'][ddata.obs.organ_tissue_unc_infered == 'Muscle'].value_counts()
ddata.obs.organ_tissue_unc_infered.value_counts()
adata.obs.organ_tissue.value_counts()