File size: 16,632 Bytes
a48f0ae |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.16.1
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# # 3 LINCS SCIPLEX GENE MATCHING
#
# **Requires**
# * `'lincs_full_smiles.h5ad'`
# * `'sciplex_raw_chunk_{i}.h5ad'` with $i \in \{0,1,2,3,4\}$
#
# **Output**
# * `'sciplex3_matched_genes_lincs.h5ad'`
# * `lincs`: `'sciplex3_lincs_genes.h5ad'`
# * `sciplex`: `'lincs_full_smiles_sciplex_genes.h5ad'`
#
# ## Description
#
# The goal of this notebook is to match and merge genes between the LINCS and SciPlex datasets, resulting in the creation of three new datasets:
#
# ### Created datasets
#
# - **`sciplex3_matched_genes_lincs.h5ad`**: Contains **SciPlex observations**. **Genes are limited to the intersection** of the genes found in both LINCS and SciPlex datasets.
#
# - **`sciplex3_lincs_genes.h5ad`**: Contains **SciPlex data**, but filtered to include **only the genes that are shared with the LINCS dataset**.
#
# - **`lincs_full_smiles_sciplex_genes.h5ad`**: Contains **LINCS data**, but filtered to include **only the genes that are shared with the SciPlex dataset**.
#
# To create these datasets, we need to match the genes between the two datasets, which is done as follows:
#
# ### Gene Matching
#
# 1. **Gene ID Assignment**: SciPlex gene names are standardized to Ensembl gene IDs by extracting the primary identifier and using either **sfaira** or a predefined mapping (`symbols_dict.json`). The LINCS dataset is already standardized.
#
# 2. **Identifying Shared Genes**: We then compute the intersection of the gene IDs (`gene_id`) inside LINCS and SciPlex. Both datasets are then filtered to ggvG$retain only these shared genes.
#
# 3. **Reindexing**: The LINCS dataset is reindexed to match the order of genes in the SciPlex dataset.
#
#
# +
import os
import sys
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sfaira
import warnings
os.getcwd()
from chemCPA.paths import DATA_DIR, PROJECT_DIR
pd.set_option('display.max_columns', 100)
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(root_dir)
import raw_data.datasets as datasets
import logging
logging.basicConfig(level=logging.INFO)
from notebook_utils import suppress_output
import scanpy as sc
with suppress_output():
sc.set_figure_params(dpi=80, frameon=False)
sc.logging.print_header()
warnings.filterwarnings('ignore')
# -
# %load_ext autoreload
# %autoreload 2
# ## Load data
# Load lincs
adata_lincs = sc.read(DATA_DIR/'lincs_full_smiles.h5ad' )
# Load sciplex
# +
from tqdm import tqdm
from chemCPA.paths import DATA_DIR, PROJECT_DIR
from raw_data.datasets import sciplex
# Load and concatenate chunks
adatas_sciplex = []
logging.info("Starting to load in sciplex data")
# Get paths to all sciplex chunks
chunk_paths = sciplex()
# Load chunks with progress bar
for chunk_path in tqdm(chunk_paths, desc="Loading sciplex chunks"):
tqdm.write(f"Loading {os.path.basename(chunk_path)}")
adatas_sciplex.append(sc.read(chunk_path))
adata_sciplex = adatas_sciplex[0].concatenate(adatas_sciplex[1:])
logging.info("Sciplex data loaded")
# -
# Add gene_id to sciplex
adata_sciplex.var['gene_id'] = adata_sciplex.var.id.str.split('.').str[0]
adata_sciplex.var['gene_id'].head()
# ### Get gene ids from symbols via sfaira
# Load genome container with sfaira
try:
# load json file with symbol to id mapping
import json
with open(DATA_DIR/ 'symbols_dict.json') as json_file:
symbols_dict = json.load(json_file)
except:
logging.info("No symbols_dict.json found, falling back to sfaira")
genome_container = sfaira.versions.genomes.GenomeContainer(organism="homo_sapiens", release="82")
symbols_dict = genome_container.symbol_to_id_dict
# Extend symbols dict with unknown symbol
symbols_dict.update({'PLSCR3':'ENSG00000187838'})
# Identify genes that are shared between lincs and trapnell
# For lincs
adata_lincs.var['gene_id'] = adata_lincs.var_names.map(symbols_dict)
adata_lincs.var['in_sciplex'] = adata_lincs.var.gene_id.isin(adata_sciplex.var.gene_id)
# For trapnell
adata_sciplex.var['in_lincs'] = adata_sciplex.var.gene_id.isin(adata_lincs.var.gene_id)
# Print gene matching statistics
print("\nGene matching statistics:")
print(f"Number of genes in LINCS: {adata_lincs.shape[1]}")
print(f"Number of genes in sciplex: {adata_sciplex.shape[1]}")
print(f"Number of shared genes: {sum(adata_sciplex.var.in_lincs)}")
# ## Preprocess sciplex dataset
# See `sciplex3.ipynb`
# The original CPA implementation required to subset the data due to scaling limitations.
# In this version we expect to be able to handle the full sciplex dataset.
# +
SUBSET = False
if SUBSET:
sc.pp.subsample(adata_sciplex, fraction=0.5, random_state=42)
# -
sc.pp.normalize_per_cell(adata_sciplex)
sc.pp.log1p(adata_sciplex)
sc.pp.highly_variable_genes(adata_sciplex, n_top_genes=1032, subset=False)
# ### Combine HVG with lincs genes
#
# Union of genes that are considered highly variable and those that are shared with lincs
((adata_sciplex.var.in_lincs) | (adata_sciplex.var.highly_variable)).sum()
# Subset to that union of genes
adata_sciplex = adata_sciplex[:, (adata_sciplex.var.in_lincs) | (adata_sciplex.var.highly_variable)].copy()
# ### Create additional meta data
# Normalise dose values
adata_sciplex.obs['dose_val'] = adata_sciplex.obs.dose.astype(float) / np.max(adata_sciplex.obs.dose.astype(float))
adata_sciplex.obs.loc[adata_sciplex.obs['product_name'].str.contains('Vehicle'), 'dose_val'] = 1.0
adata_sciplex.obs['dose_val'].value_counts()
# Change `product_name`
adata_sciplex.obs['product_name'] = [x.split(' ')[0] for x in adata_sciplex.obs['product_name']]
adata_sciplex.obs.loc[adata_sciplex.obs['product_name'].str.contains('Vehicle'), 'product_name'] = 'control'
# Create copy of `product_name` with column name `control`
adata_sciplex.obs['condition'] = adata_sciplex.obs.product_name.copy()
# Add combinations of drug (`condition`), dose (`dose_val`), and cell_type (`cell_type`)
# make column of dataframe to categorical
adata_sciplex.obs["condition"] = adata_sciplex.obs["condition"].astype('category').cat.rename_categories({"(+)-JQ1": "JQ1"})
adata_sciplex.obs['drug_dose_name'] = adata_sciplex.obs.condition.astype(str) + '_' + adata_sciplex.obs.dose_val.astype(str)
adata_sciplex.obs['cov_drug_dose_name'] = adata_sciplex.obs.cell_type.astype(str) + '_' + adata_sciplex.obs.drug_dose_name.astype(str)
adata_sciplex.obs['cov_drug'] = adata_sciplex.obs.cell_type.astype(str) + '_' + adata_sciplex.obs.condition.astype(str)
# Add `control` columns with vale `1` where only the vehicle was used
adata_sciplex.obs['control'] = [1 if x == 'control_1.0' else 0 for x in adata_sciplex.obs.drug_dose_name.values]
# ## Compute DE genes
# +
from chemCPA.helper import rank_genes_groups_by_cov
rank_genes_groups_by_cov(adata_sciplex, groupby='cov_drug', covariate='cell_type', control_group='control', key_added='all_DEGs')
# -
adata_subset = adata_sciplex[:, adata_sciplex.var.in_lincs].copy()
rank_genes_groups_by_cov(adata_subset, groupby='cov_drug', covariate='cell_type', control_group='control', key_added='lincs_DEGs')
adata_sciplex.uns['lincs_DEGs'] = adata_subset.uns['lincs_DEGs']
# ### Map all unique `cov_drug_dose_name` to the computed DEGs, independent of the dose value
#
# Create mapping between names with dose and without dose
cov_drug_dose_unique = adata_sciplex.obs.cov_drug_dose_name.unique()
remove_dose = lambda s: '_'.join(s.split('_')[:-1])
cov_drug = pd.Series(cov_drug_dose_unique).apply(remove_dose)
dose_no_dose_dict = dict(zip(cov_drug_dose_unique, cov_drug))
# ### Compute new dicts for DEGs
uns_keys = ['all_DEGs', 'lincs_DEGs']
for uns_key in uns_keys:
new_DEGs_dict = {}
df_DEGs = pd.Series(adata_sciplex.uns[uns_key])
for key, value in dose_no_dose_dict.items():
if 'control' in key:
continue
new_DEGs_dict[key] = df_DEGs.loc[value]
adata_sciplex.uns[uns_key] = new_DEGs_dict
adata_sciplex
# ## Create sciplex splits
#
# This is not the right configuration fot the experiments we want but for the moment this is okay
# ### OOD in Pathways
# +
adata_sciplex.obs['split_ho_pathway'] = 'train' # reset
ho_drugs = [
# selection of drugs from various pathways
"Azacitidine",
"Carmofur",
"Pracinostat",
"Cediranib",
"Luminespib",
"Crizotinib",
"SNS-314",
"Obatoclax",
"Momelotinib",
"AG-14361",
"Entacapone",
"Fulvestrant",
"Mesna",
"Zileuton",
"Enzastaurin",
"IOX2",
"Alvespimycin",
"XAV-939",
"Fasudil",
]
ho_drug_pathway = adata_sciplex.obs['condition'].isin(ho_drugs)
adata_sciplex.obs.loc[ho_drug_pathway, 'pathway_level_1'].value_counts()
# -
ho_drug_pathway.sum()
# +
adata_sciplex.obs.loc[ho_drug_pathway & (adata_sciplex.obs['dose_val'] == 1.0), 'split_ho_pathway'] = 'ood'
test_idx = sc.pp.subsample(adata_sciplex[adata_sciplex.obs['split_ho_pathway'] != 'ood'], .15, copy=True).obs.index
adata_sciplex.obs.loc[test_idx, 'split_ho_pathway'] = 'test'
# -
pd.crosstab(adata_sciplex.obs.pathway_level_1, adata_sciplex.obs['condition'][adata_sciplex.obs.condition.isin(ho_drugs)])
adata_sciplex.obs['split_ho_pathway'].value_counts()
adata_sciplex[adata_sciplex.obs.split_ho_pathway == 'ood'].obs.condition.value_counts()
adata_sciplex[adata_sciplex.obs.split_ho_pathway == 'test'].obs.condition.value_counts()
# ### OOD drugs in epigenetic regulation, Tyrosine kinase signaling, cell cycle regulation
adata_sciplex.obs['pathway_level_1'].value_counts()
# ___
#
# #### Tyrosine signaling
adata_sciplex.obs.loc[adata_sciplex.obs.pathway_level_1.isin(["Tyrosine kinase signaling"]),'condition'].value_counts()
tyrosine_drugs = adata_sciplex.obs.loc[adata_sciplex.obs.pathway_level_1.isin(["Tyrosine kinase signaling"]),'condition'].unique()
# +
adata_sciplex.obs['split_tyrosine_ood'] = 'train'
test_idx = sc.pp.subsample(adata_sciplex[adata_sciplex.obs.pathway_level_1.isin(["Tyrosine kinase signaling"])], .20, copy=True).obs.index
adata_sciplex.obs.loc[test_idx, 'split_tyrosine_ood'] = 'test'
adata_sciplex.obs.loc[adata_sciplex.obs.condition.isin(["Cediranib", "Crizotinib", "Motesanib", "BMS-754807", "Nintedanib"]), 'split_tyrosine_ood'] = 'ood'
# -
adata_sciplex.obs.split_tyrosine_ood.value_counts()
pd.crosstab(adata_sciplex.obs.split_tyrosine_ood, adata_sciplex.obs['condition'][adata_sciplex.obs.condition.isin(tyrosine_drugs)])
pd.crosstab(adata_sciplex.obs.split_tyrosine_ood, adata_sciplex.obs.dose_val)
# ____
#
# #### Epigenetic regulation
adata_sciplex.obs.loc[adata_sciplex.obs.pathway_level_1.isin(["Epigenetic regulation"]),'condition'].value_counts()
epigenetic_drugs = adata_sciplex.obs.loc[adata_sciplex.obs.pathway_level_1.isin(["Epigenetic regulation"]),'condition'].unique()
# +
adata_sciplex.obs['split_epigenetic_ood'] = 'train'
test_idx = sc.pp.subsample(adata_sciplex[adata_sciplex.obs.pathway_level_1.isin(["Epigenetic regulation"])], .20, copy=True).obs.index
adata_sciplex.obs.loc[test_idx, 'split_epigenetic_ood'] = 'test'
adata_sciplex.obs.loc[adata_sciplex.obs.condition.isin(["Azacitidine", "Pracinostat", "Trichostatin", "Quisinostat", "Tazemetostat"]), 'split_epigenetic_ood'] = 'ood'
# -
adata_sciplex.obs.split_epigenetic_ood.value_counts()
pd.crosstab(adata_sciplex.obs.split_epigenetic_ood, adata_sciplex.obs['condition'][adata_sciplex.obs.condition.isin(epigenetic_drugs)])
pd.crosstab(adata_sciplex.obs.split_tyrosine_ood, adata_sciplex.obs.dose_val)
# + [markdown] jp-MarkdownHeadingCollapsed=true
# __________
#
# #### Cell cycle regulation
# -
adata_sciplex.obs.loc[adata_sciplex.obs.pathway_level_1.isin(["Cell cycle regulation"]),'condition'].value_counts()
cell_cycle_drugs = adata_sciplex.obs.loc[adata_sciplex.obs.pathway_level_1.isin(["Cell cycle regulation"]),'condition'].unique()
# +
adata_sciplex.obs['split_cellcycle_ood'] = 'train'
test_idx = sc.pp.subsample(adata_sciplex[adata_sciplex.obs.pathway_level_1.isin(["Cell cycle regulation"])], .20, copy=True).obs.index
adata_sciplex.obs.loc[test_idx, 'split_cellcycle_ood'] = 'test'
adata_sciplex.obs.loc[adata_sciplex.obs.condition.isin(["SNS-314", "Flavopiridol", "Roscovitine"]), 'split_cellcycle_ood'] = 'ood'
# -
adata_sciplex.obs.split_cellcycle_ood.value_counts()
pd.crosstab(adata_sciplex.obs.split_cellcycle_ood, adata_sciplex.obs['condition'][adata_sciplex.obs.condition.isin(cell_cycle_drugs)])
pd.crosstab(adata_sciplex.obs.split_cellcycle_ood, adata_sciplex.obs.dose_val)
[c for c in adata_sciplex.obs.columns if 'split' in c]
# + [markdown] jp-MarkdownHeadingCollapsed=true
# ### Further splits
#
# **We omit these split as we design our own splits - for referece this is commented out for the moment**
#
# Also a split which sees all data:
# +
# adata.obs['split_all'] = 'train'
# test_idx = sc.pp.subsample(adata, .10, copy=True).obs.index
# adata.obs.loc[test_idx, 'split_all'] = 'test'
# +
# adata.obs['ct_dose'] = adata.obs.cell_type.astype('str') + '_' + adata.obs.dose_val.astype('str')
# -
# Round robin splits: dose and cell line combinations will be held out in turn.
# +
# i = 0
# split_dict = {}
# +
# # single ct holdout
# for ct in adata.obs.cell_type.unique():
# for dose in adata.obs.dose_val.unique():
# i += 1
# split_name = f'split{i}'
# split_dict[split_name] = f'{ct}_{dose}'
# adata.obs[split_name] = 'train'
# adata.obs.loc[adata.obs.ct_dose == f'{ct}_{dose}', split_name] = 'ood'
# test_idx = sc.pp.subsample(adata[adata.obs[split_name] != 'ood'], .16, copy=True).obs.index
# adata.obs.loc[test_idx, split_name] = 'test'
# display(adata.obs[split_name].value_counts())
# +
# # double ct holdout
# for cts in [('A549', 'MCF7'), ('A549', 'K562'), ('MCF7', 'K562')]:
# for dose in adata.obs.dose_val.unique():
# i += 1
# split_name = f'split{i}'
# split_dict[split_name] = f'{cts[0]}+{cts[1]}_{dose}'
# adata.obs[split_name] = 'train'
# adata.obs.loc[adata.obs.ct_dose == f'{cts[0]}_{dose}', split_name] = 'ood'
# adata.obs.loc[adata.obs.ct_dose == f'{cts[1]}_{dose}', split_name] = 'ood'
# test_idx = sc.pp.subsample(adata[adata.obs[split_name] != 'ood'], .16, copy=True).obs.index
# adata.obs.loc[test_idx, split_name] = 'test'
# display(adata.obs[split_name].value_counts())
# +
# # triple ct holdout
# for dose in adata.obs.dose_val.unique():
# i += 1
# split_name = f'split{i}'
# split_dict[split_name] = f'all_{dose}'
# adata.obs[split_name] = 'train'
# adata.obs.loc[adata.obs.dose_val == dose, split_name] = 'ood'
# test_idx = sc.pp.subsample(adata[adata.obs[split_name] != 'ood'], .16, copy=True).obs.index
# adata.obs.loc[test_idx, split_name] = 'test'
# display(adata.obs[split_name].value_counts())
# +
# adata.uns['all_DEGs']
# -
# ## Save adata
# Reindex the lincs dataset
# +
sciplex_ids = pd.Index(adata_sciplex.var.gene_id)
lincs_idx = [sciplex_ids.get_loc(_id) for _id in adata_lincs.var.gene_id[adata_lincs.var.in_sciplex]]
# +
non_lincs_idx = [sciplex_ids.get_loc(_id) for _id in adata_sciplex.var.gene_id if not adata_lincs.var.gene_id.isin([_id]).any()]
lincs_idx.extend(non_lincs_idx)
# -
adata_sciplex = adata_sciplex[:, lincs_idx].copy()
# +
fname = PROJECT_DIR/'datasets'/'sciplex3_matched_genes_lincs.h5ad'
sc.write(fname, adata_sciplex)
# -
# Check that it worked
sc.read(fname)
# ## Subselect to shared only shared genes
# Subset to shared genes
adata_lincs = adata_lincs[:, adata_lincs.var.in_sciplex].copy()
adata_sciplex = adata_sciplex[:, adata_sciplex.var.in_lincs].copy()
adata_lincs.var_names
adata_sciplex.var_names
# Print some stats about the gene matching
print("\nGene matching statistics:")
print(f"Number of genes in LINCS: {adata_lincs.shape[1]}")
print(f"Number of genes in combinatorial sciplex: {adata_sciplex.shape[1]}")
print(f"Number of shared genes: {sum(adata_sciplex.var.in_lincs)}")
# ## Save adata objects with shared genes only
# Index of lincs has also been reordered accordingly
# +
fname = PROJECT_DIR/'datasets'/'sciplex3_lincs_genes.h5ad'
sc.write(fname, adata_sciplex)
# -
# ____
# +
fname_lincs = PROJECT_DIR/'datasets'/'lincs_full_smiles_sciplex_genes.h5ad'
sc.write(fname_lincs, adata_lincs)
# -
|