SwiftCNV on Ovarian cancer dataset#
This tutorial demonstrates how to run SwiftCNV from a Jupyter notebook and perform downstream analysis using the resulting copy number variation (CNV) matrix. It also shows how to load the results generated with the command-line interface into an AnnData object.
Load packages#
import sys
import scanpy as sc
import matplotlib.pyplot as plt
import warnings
import logging
import requests
import swiftcnv as cnv
from swiftcnv.datasets import Qian2020_Ovarian
logging.basicConfig(level=logging.INFO, datefmt='%H:%M:%S',
format='%(asctime)s [%(levelname)s]: %(message)s')
warnings.simplefilter('ignore')
sc.settings.set_figure_params(figsize=(4, 4))
gtf_filename = 'gencode.v44.basic.annotation.gtf.gz'
output_dir = 'example_output'
Load example dataset#
The example dataset includes four samples with 4000 cells and 12000 genes. The data was adapted from an ovarian cancer study available through the Curated Cancer Cell Atlas.
adata = Qian2020_Ovarian()
adata
AnnData object with n_obs × n_vars = 4000 × 12000
obs: 'sample', 'cell_type'
obsm: 'X_umap'
layers: None (.X)
We can first inspect the UMAP plot based on gene expression:
The dataset contains the representative cells from the tumor microenvironment as well as several epithelial cells from 4 different patients.
Get Gene Annotations#
To run SwiftCNV a GTF file with gene coordinates is needed. If not available, it can be downloaded from Gencode database using one of the latest releases:
gtf_url = 'https://ftp.ebi.ac.uk/pub/databases/gencode/Gencode_human/release_44/gencode.v44.basic.annotation.gtf.gz'
print('Downloading file...')
with requests.get(gtf_url, stream=True) as r:
r.raise_for_status()
with open(gtf_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
print('Download finished!')
Downloading file...
Download finished!
Running SwiftCNV#
We will use all non-epithelial cells as reference cells (T cells, Macrophages, Fibroblasts, Endothelial and B_cells). In general, we recommend including all non-cell-of-origin cell types in the reference to minimize cell type-specific signals. The exclude_immune parameter excludes immune-associated genes, such as HLA and immunoglobulins, that could confound CNV inference. We use read_X to specify that raw counts should be taken from the main matrix. The cutoff parameter filters out genes whose average expression across the reference cells falls below the specified value. When running the analysis from a script, output_dir can also be omitted and the results will be returned.
adata, cnv_states, subclusters_df = cnv.run_from_adata(
adata,
output_dir=output_dir,
gtf_file=gtf_filename,
reference_col='cell_type',
reference_vals=['T_cell', 'Macrophage', 'Fibroblast', 'Endothelial', 'B_cell'],
sample_col='sample',
exclude_immune=True,
read_X=True,
plot=True,
run_hmm=True,
hmm_by='sample',
cutoff=0.1
)
The resulting cnv matrix with the genes considered for the analysis is added to the obsm slot.
print(adata)
print('\n', adata.obsm['cnv_mat'].shape)
AnnData object with n_obs × n_vars = 4000 × 12000
obs: 'sample', 'cell_type', 'reference'
var: 'chr', 'arm', 'chr_arm', 'start', 'end', 'has_cnv'
uns: 'cell_type_colors', 'sample_colors'
obsm: 'X_umap', 'cnv_mat'
layers: None (.X)
(4000, 7198)
Load SwiftCNV results from an output directory#
If SwiftCNV was run from the command line, the output files are saved in the specified directory. These results can be added into a preloaded AnnData object using the load_output function by providing the path to the output directory.
cnv.load_output(output_dir, adata, inplace=True)
Plotting the CNV heatmap#
The CNV heatmap can be generated with plot_cnv_from_adata. We can add cell variables from obs (like sample or cell type).
Smoothing CNV matrix by chromosome arm#
The CNV signal can be further smoothed by chromosome arms to reduce the noise. In this case, a new cnv_mat_arms is added to the obsm slot.
cnv.summarise_by_var(adata, obsm_key='cnv_mat', by='chr_arm', key_added='cnv_mat_arms', inplace=True)
adata.obsm['cnv_mat_arms'].shape
(4000, 39)
Additionally, we can also plot the CNV heatmap by chromosome arms. In this case we will add a second grouping variable with the cell type.
cnv.plot_cnv_from_adata(adata, obsm_key='cnv_mat_arms', var_key=None, figsize=(20, 10), **{'Sample': 'sample', 'Cell type': 'cell_type'})
With this matrix we can generate a UMAP base only on CNV values using the scanpy workflow.
sc.pp.neighbors(adata, use_rep='cnv_mat_arms', key_added='cnv_neighbors')
sc.tl.leiden(adata, neighbors_key='cnv_neighbors', resolution=0.2, key_added='cnv_leiden')
sc.tl.umap(adata, neighbors_key='cnv_neighbors', min_dist=0.5, spread=1, key_added='cnv_umap', random_state=42)
Additionally, a CNV score per cell can be obtained from the Mean Squared Error of the smoothed values.
cnv.cnv_score(adata, obsm_key='cnv_mat_arms', inplace=True)
sc.pl.embedding(adata, basis='cnv_umap', color=['cell_type', 'cnv_score'], wspace=0.4, frameon=False, vmax='p99')
The generated clusters can also be displayed in the heatmap alongside the sample and cell type annotations.
cnv.plot_cnv_from_adata(adata, obsm_key='cnv_mat_arms', figsize=(20, 10), **{'Sample': 'sample', 'Cell type': 'cell_type', 'CNV leiden': 'cnv_leiden'})
To focus on a specific sample, we can subset adata.
cnv.plot_cnv_from_adata(adata[adata.obs['sample'] == '13'], obsm_key='cnv_mat_arms', group_cells=False, figsize=(20, 10), **{'Cell type': 'cell_type', 'CNV score': 'cnv_score', 'CNV leiden': 'cnv_leiden'})
A heatmap with the mean expression per sample can be obtained using the function plot_cnv_summary
cnv.plot_cnv_summary(adata[~adata.obs['reference']], by='sample', obsm_key='cnv_mat_arms', figsize=(20, 3))
Finally, the genes used on each chromosome arm can be retrieved as dataframe using the get_genes_chr_arm function
genes_1p = cnv.get_genes_chr_arm(adata, obsm_key='cnv_mat', chr_arms='1p')
genes_1p.head()
| gene | MTCO2P12 | NOC2L | HES4 | ISG15 | AGRN | TNFRSF18 | TNFRSF4 | SDF4 | UBE2J2 | INTS11 | ... | ATP1A1 | CD58 | CD2 | PTGFRN | MAN1A2 | TENT5C | PHGDH | NOTCH2 | SEC22B | SRGAP2C |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| BT1303_TGATTTCAGATGGGTC | 0.003178 | 0.002340 | 0.001732 | 0.001292 | 0.000922 | 0.001829 | 0.000843 | 0.004028 | 0.021919 | 0.014610 | ... | -0.000302 | -0.000226 | -0.000046 | -0.000376 | -0.000286 | -0.000175 | -0.000140 | -0.000019 | 0.000005 | 0.000134 |
| BT1303_TCTGGAACACAAGACG | -0.000174 | -0.000253 | -0.000346 | -0.000453 | -0.000606 | -0.000015 | -0.000133 | 0.000037 | 0.000572 | 0.000292 | ... | -0.001265 | -0.001032 | -0.001890 | -0.000847 | -0.000670 | -0.000450 | -0.000376 | -0.001005 | -0.000799 | -0.004841 |
| BT1303_GGGCACTAGTATGACA | 0.151988 | 0.139090 | 0.125665 | 0.111800 | 0.096482 | 0.110470 | 0.084753 | 0.119680 | 0.131748 | 0.114032 | ... | -0.008070 | -0.006594 | -0.003246 | -0.013190 | -0.011003 | -0.008264 | -0.007160 | -0.015833 | -0.013268 | -0.016196 |
| BT1303_CCTATTACACAAGTAA | -0.000422 | -0.000406 | -0.000392 | -0.000375 | -0.000381 | -0.001304 | -0.001983 | -0.000076 | -0.000183 | -0.000270 | ... | 0.000376 | 0.000415 | 0.000884 | 0.001785 | 0.001979 | 0.002451 | 0.002515 | 0.004231 | 0.004714 | 0.000086 |
| BT1303_TTCTCAATCCTACAGA | -0.004046 | -0.004504 | -0.004973 | -0.005432 | -0.006112 | -0.015428 | -0.022275 | -0.003286 | -0.000015 | -0.000080 | ... | -0.080258 | -0.079538 | -0.109285 | -0.095015 | -0.094832 | -0.091765 | -0.093795 | -0.129399 | -0.130352 | -0.160076 |
5 rows × 428 columns