-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths6_cnv.py
136 lines (107 loc) · 3.88 KB
/
s6_cnv.py
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
# reference: https://infercnvpy.readthedocs.io/en/latest/notebooks/tutorial_3k.html
import argparse
# deal with input
parser = argparse.ArgumentParser(description='infer cnv')
parser.add_argument('-i', '--input', help='input file name (h5ad)', type=str, required=True)
args = parser.parse_args()
import os
import sys
import traceback
import warnings
from pathlib import Path
import scanpy as sc
import seaborn as sns
import infercnvpy as cnv
import matplotlib.pyplot as plt
warnings.filterwarnings("ignore")
sc.settings.set_figure_params(figsize=(5, 5))
sc.settings.verbosity = 3
input_file = args.input
GSE_code = input_file.split('_')[1] # d6_GSExxxx_xxx.h5ad
output_file = f'd7_{GSE_code}_cnv.h5ad'
outputdir = Path('cnv')
print(f'''
-------------------------
Input file:
{input_file}
Do:
infer cnv
Output file:
{output_file} (h5ad)
{str(outputdir.absolute())} (dir)
-------------------------
''')
def my_chromosome_heatmap(adata, groupby, vmax, **kwargs):
# Save the original function
original_heatmap = sc.pl.heatmap
# Create a new function that calls the original function without a norm parameter
def new_heatmap(*args, **kwargs):
kwargs.pop('norm', None) # Remove the norm parameter if it exists
return original_heatmap(*args, vmax=vmax, **kwargs)
# Replace the original function with our new function
sc.pl.heatmap = new_heatmap
# Call chromosome_heatmap, which will now use our new function
result = cnv.pl.chromosome_heatmap(adata, groupby=groupby, **kwargs)
# Restore the original function
sc.pl.heatmap = original_heatmap
return result
if not outputdir.exists():
outputdir.mkdir(parents=True)
adata = sc.read_h5ad(input_file)
cnv.tl.infercnv(
adata,
reference_key="manual_ann",
reference_cat=[
"Endothelial",
],
window_size=250,
)
adata.write_h5ad(output_file) # cache
cnv.pl.chromosome_heatmap(adata, groupby="manual_ann", dendrogram=True)
plt.savefig(outputdir / 'f1_celltype_cnv_heatmap.png')
my_chromosome_heatmap(adata, groupby="manual_ann", vmax=1, dendrogram=True)
plt.savefig(outputdir / 'f2_celltype_cnv_heatmap_revised.png')
cnv.tl.pca(adata)
cnv.pp.neighbors(adata)
cnv.tl.leiden(adata)
cnv.pl.chromosome_heatmap(adata, groupby="cnv_leiden", dendrogram=True)
plt.savefig(outputdir / 'f3_leiden_cnv_heatmap.png')
my_chromosome_heatmap(adata, groupby="cnv_leiden", vmax=1, dendrogram=True)
plt.savefig(outputdir / 'f4_leiden_cnv_heatmap_revised.png')
adata.write_h5ad(output_file) # cache
cnv.tl.umap(adata)
cnv.tl.cnv_score(adata)
adata.write_h5ad(output_file) # cache
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(11, 11))
ax4.axis("off")
cnv.pl.umap(
adata,
color="cnv_leiden",
legend_loc="on data",
legend_fontoutline=2,
ax=ax1,
show=False,
)
cnv.pl.umap(adata, color="cnv_score", ax=ax2, show=False)
cnv.pl.umap(adata, color="manual_ann", ax=ax3)
plt.savefig(outputdir / 'f5_cnv_umap.png')
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(
2, 2, figsize=(12, 11), gridspec_kw=dict(wspace=0.5)
)
ax4.axis("off")
sc.pl.umap(adata, color="cnv_leiden", ax=ax1, show=False)
sc.pl.umap(adata, color="cnv_score", ax=ax2, show=False)
sc.pl.umap(adata, color="manual_ann", ax=ax3)
plt.savefig(outputdir / 'f6_cnv_umap2.png')
adata.obs["cnv_status"] = "normal"
adata.obs.loc[
adata.obs['cnv_leiden'].isin(["13", "17", "18", "19", "22", "14", "25", "26", "11", "20"]), "cnv_status"
] = "tumor"
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5), gridspec_kw=dict(wspace=0.5))
cnv.pl.umap(adata, color="cnv_status", ax=ax1, show=False)
sc.pl.umap(adata, color="cnv_status", ax=ax2)
plt.savefig(outputdir / 'f7_status_assigned.png')
cnv.pl.chromosome_heatmap(adata[adata.obs["cnv_status"] == "tumor", :])
plt.savefig(outputdir / 'f8_tumor_cnv_heatmap.png')
cnv.pl.chromosome_heatmap(adata[adata.obs["cnv_status"] == "normal", :])
plt.savefig(outputdir / 'f9_normal_cnv_heatmap.png')