├── stp1_gridid.R ├── stp1_sampleid.R ├── stp1_summary.R ├── stp2_epiArea.R ├── stp2_epiSpatial.R ├── stp2_epilinedist.R ├── stp2_exprSpatial.R ├── stp2_fovplot.R ├── stp2_programAnalysis.R ├── stp2_programSpatial.R ├── stp3_CN_kmeans.R ├── stp3_fib_fibepidist.R ├── stp3_immuneSpatial.R ├── stp3_mye_analysis.R └── stp3_tc_analysis.R /stp1_gridid.R: -------------------------------------------------------------------------------- 1 | source('~/xenium/header.R') 2 | source('~/xenium/xenium_function.R') 3 | 4 | ##### input ##### 5 | allmeta = read_rds('data/xenium_seurat_processed/allcell_meta.rds') 6 | crds = read_rds('stp1_summary/data/allsample_coords.rds') 7 | 8 | allmk = c('EPCAM','SFN','KRT4','KRT5', # epi 9 | 'FN1','DCN','COL1A1','COL1A2','COL3A1','COL6A1', # fibro 10 | 'PLN','MYH11', # muscle 11 | 'VWF','PECAM1','ENG','CDH5', # endo 12 | 'PTPRC', # immune 13 | 'CD2','CD3D','CD3E', # t cell 14 | 'CD1E','CD68','LYZ','CD14','FCGR3A', # myeloid 15 | 'MS4A2','KIT','CPA3', # mast 16 | 'CD19','CD79A','MS4A1', # B 17 | 'JCHAIN','IGHG2' # plasma 18 | ) 19 | 20 | epimarker = c('KRT15','MYC', # QP 21 | 'TOP2A','MKI67', # CY 22 | 'ANXA1','KRT13','KRT4','CSTA','S100A8','TRIM29', # MD 23 | 'SPRR3','SPRR2E','ECM1','EMP1', # TD 24 | 'HIF1A','SERPINE1','VEGFA', # HY 25 | 'KRT17', # RO 26 | 'SPP1','CES1', 'AKR1C3','AKR1C2','CBR1', # DO 27 | 'HLA-DRB1','HLA-DQB1'# AP 28 | ) 29 | 30 | myemarker = c('SFN','DCN','VWF','CD19','CD68','CD2', # pan marker 31 | 'GZMB','JCHAIN', # pDC 32 | 'CD1C','CD1E','FCER1A', # APC 33 | 'CCR7', # tDC 34 | 'FCN1','S100A9','S100A8','FCGR3A', # Mono 35 | 'IL1B', # Macro 36 | 'LYVE1','C1QC','C1QB','C1QA','SPP1','FN1' # Macro 37 | ) 38 | 39 | cd4marker = c('CD4','CD8A','CD8B', 40 | 'FOXP3','IL2RA','CTLA4', # reg 41 | 'TCF7','CCR7','SELL', # tn 42 | 'IL7R','CCL5', # mem 43 | 'CXCL13','CD200' # tfh 44 | ) 45 | 46 | cd8marker = c('CD4','CD8A','CD8B', 47 | 'TCF7','CCR7','SELL', # tn 48 | 'IL7R','CXCR4',#mem 49 | 'GNLY', 'NKG7', 'GZMB', 'GZMA', 'GZMH', 'GZMK', #Teff 50 | 'PDCD1','CXCL13','CTLA4','HAVCR2' # tex 51 | ) 52 | cytogene = c('GNLY', 'NKG7', 'GZMB', 'GZMA', 'GZMH', 'GZMK', 'KLRB1','TYROBP') 53 | exhgene = c('PDCD1','HAVCR2','CTLA4','TIGIT','LAG3','CXCL13') 54 | reggene = c('FOXP3','TIGIT','CD274','IL2RA') 55 | tmarker = c('CD4','CD8A','CD8B', 56 | 'GNLY', 'NKG7', 'GZMB', 'GZMA', 'GZMH', 'GZMK', 'KLRB1','TYROBP', 57 | 'PDCD1','HAVCR2','CTLA4','TIGIT','LAG3','CXCL13', 58 | 'FOXP3','CD274','IL2RA') 59 | 60 | fibmarker = c('PLA2G2A','GPX3','IGFBP6','PI16','ADH1B','TNXB','DCN','CXCL12', # NF 61 | 'IGF1','RGS5','HIF1A','CCND1','POSTN','STAT1','FAP','SERPINE1','DPT', # iCAF 62 | 'MMP11','MMP1','IL7R','STMN1','PDPN','RUNX1' # myCAF 63 | ) 64 | 65 | probelist = fread('data/235gene_label.txt',data.table = F) 66 | # probelist = fread('data/probe_list.txt',data.table = F) 67 | 68 | 69 | 70 | 71 | 72 | ##### grid id ##### 73 | gridlength = 100 74 | gridid=lapply(names(crds), function(x){ 75 | crd = crds[[x]] 76 | 77 | idbyspstg = lapply(unique(crd$sp_stg), function(y){ 78 | dt = crd[crd$sp_stg==y,] 79 | gridx = dt$x%/%gridlength + 1 80 | gridy = dt$y%/%gridlength + 1 81 | gridid = paste0(x,'-',gridx,'-',gridy) 82 | 83 | dt$gridid = gridid %>% factor(levels = sort(unique(gridid))) 84 | return(dt) 85 | }) %>% Reduce(rbind,.) 86 | 87 | idbyspstg$slideid = x 88 | return(idbyspstg) 89 | }) %>% Reduce(rbind,.) 90 | saveRDS(gridid,'stp1_summary/data/allsample_gridid.rds') 91 | 92 | 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /stp1_sampleid.R: -------------------------------------------------------------------------------- 1 | source('~/xenium/header.R') 2 | source('~/xenium/xenium_function.R') 3 | 4 | ##### input ##### 5 | job({ 6 | xe1_1 = read_rds('data/xenium_seurat_processed/xenium_t1_s1_seurat.rds') 7 | xe1_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s1_meta.rds') 8 | xe1_2 = read_rds('data/xenium_seurat_processed/xenium_t1_s2_seurat.rds') 9 | xe1_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s2_meta.rds') 10 | xe1_3 = read_rds('data/xenium_seurat_processed/xenium_t1_s3_seurat.rds') 11 | xe1_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s3_meta.rds') 12 | 13 | xe2_1 = read_rds('data/xenium_seurat_processed/xenium_t2_s1_seurat.rds') 14 | xe2_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s1_meta.rds') 15 | xe2_2 = read_rds('data/xenium_seurat_processed/xenium_t2_s2_seurat.rds') 16 | xe2_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s2_meta.rds') 17 | xe2_3 = read_rds('data/xenium_seurat_processed/xenium_t2_s3_seurat.rds') 18 | xe2_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s3_meta.rds') 19 | 20 | xe3_1 = read_rds('data/xenium_seurat_processed/xenium_t3_s1_seurat.rds') 21 | xe3_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t3_s1_meta.rds') 22 | xe3_2 = read_rds('data/xenium_seurat_processed/xenium_t3_s2_seurat.rds') 23 | xe3_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t3_s2_meta.rds') 24 | 25 | xe4_1 = read_rds('data/xenium_seurat_processed/xenium_t4_s1_seurat.rds') 26 | xe4_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t4_s1_meta.rds') 27 | xe4_2 = read_rds('data/xenium_seurat_processed/xenium_t4_s2_seurat.rds') 28 | xe4_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t4_s2_meta.rds') 29 | 30 | xe5_1 = read_rds('data/xenium_seurat_processed/xenium_t5_s1_seurat.rds') 31 | xe5_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s1_meta.rds') 32 | xe5_2 = read_rds('data/xenium_seurat_processed/xenium_t5_s2_seurat.rds') 33 | xe5_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s2_meta.rds') 34 | xe5_3 = read_rds('data/xenium_seurat_processed/xenium_t5_s3_seurat.rds') 35 | xe5_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s3_meta.rds') 36 | xe5_2_split = read_rds('data/xenium_seurat_processed/xenium_t5_s2_split_seurat.rds') 37 | xe5_2_split@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s2_split_meta.rds') 38 | xe5_3_split = read_rds('data/xenium_seurat_processed/xenium_t5_s3_split_seurat.rds') 39 | xe5_3_split@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s3_split_meta.rds') 40 | 41 | xe6_1 = read_rds('data/xenium_seurat_processed/xenium_t6_s1_seurat.rds') 42 | xe6_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s1_meta.rds') 43 | xe6_2 = read_rds('data/xenium_seurat_processed/xenium_t6_s2_seurat.rds') 44 | xe6_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s2_meta.rds') 45 | xe6_3 = read_rds('data/xenium_seurat_processed/xenium_t6_s3_seurat.rds') 46 | xe6_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s3_meta.rds') 47 | },import = 'auto') 48 | 49 | 50 | allmk = c('EPCAM','SFN','KRT4','KRT5', # epi 51 | 'FN1','DCN','COL1A1','COL1A2','COL3A1','COL6A1', # fibro 52 | 'PLN','MYH11', # muscle 53 | 'VWF','PECAM1','ENG','CDH5', # endo 54 | 'PTPRC', # immune 55 | 'CD2','CD3D','CD3E', # t cell 56 | 'CD1E','CD68','LYZ','CD14','FCGR3A', # myeloid 57 | 'MS4A2','KIT','CPA3', # mast 58 | 'CD19','CD79A','MS4A1', # B 59 | 'JCHAIN','IGHG2' # plasma 60 | ) 61 | 62 | epimarker = c('KRT15','MYC', # QP 63 | 'TOP2A','MKI67', # CY 64 | 'ANXA1','KRT13','KRT4','CSTA','S100A8','TRIM29', # MD 65 | 'SPRR3','SPRR2E','ECM1','EMP1', # TD 66 | 'HIF1A','SERPINE1','VEGFA', # HY 67 | 'KRT17', # RO 68 | 'SPP1','CES1', 'AKR1C3','AKR1C2','CBR1', # DO 69 | 'HLA-DRB1','HLA-DQB1'# AP 70 | ) 71 | 72 | myemarker = c('SFN','DCN','VWF','CD19','CD68','CD2', # pan marker 73 | 'GZMB','JCHAIN', # pDC 74 | 'CD1C','CD1E','FCER1A', # APC 75 | 'CCR7', # tDC 76 | 'FCN1','S100A9','S100A8','FCGR3A', # Mono 77 | 'IL1B', # Macro 78 | 'LYVE1','C1QC','C1QB','C1QA','SPP1','FN1' # Macro 79 | ) 80 | 81 | cd4marker = c('CD4','CD8A','CD8B', 82 | 'FOXP3','IL2RA','CTLA4', # reg 83 | 'TCF7','CCR7','SELL', # tn 84 | 'IL7R','CCL5', # mem 85 | 'CXCL13','CD200' # tfh 86 | ) 87 | 88 | cd8marker = c('CD4','CD8A','CD8B', 89 | 'TCF7','CCR7','SELL', # tn 90 | 'IL7R','CXCR4',#mem 91 | 'GNLY', 'NKG7', 'GZMB', 'GZMA', 'GZMH', 'GZMK', #Teff 92 | 'PDCD1','CXCL13','CTLA4','HAVCR2' # tex 93 | ) 94 | cytogene = c('GNLY', 'NKG7', 'GZMB', 'GZMA', 'GZMH', 'GZMK', 'KLRB1','TYROBP') 95 | exhgene = c('PDCD1','HAVCR2','CTLA4','TIGIT','LAG3','CXCL13') 96 | reggene = c('FOXP3','TIGIT','CD274','IL2RA') 97 | tmarker = c('CD4','CD8A','CD8B', 98 | 'GNLY', 'NKG7', 'GZMB', 'GZMA', 'GZMH', 'GZMK', 'KLRB1','TYROBP', 99 | 'PDCD1','HAVCR2','CTLA4','TIGIT','LAG3','CXCL13', 100 | 'FOXP3','CD274','IL2RA') 101 | 102 | fibmarker = c('PLA2G2A','GPX3','IGFBP6','PI16','ADH1B','TNXB','DCN','CXCL12', # NF 103 | 'IGF1','RGS5','HIF1A','CCND1','POSTN','STAT1','FAP','SERPINE1','DPT', # iCAF 104 | 'MMP11','MMP1','IL7R','STMN1','PDPN','RUNX1' # myCAF 105 | ) 106 | 107 | probelist = fread('data/235gene_label.txt',data.table = F) 108 | # probelist = fread('data/probe_list.txt',data.table = F) 109 | 110 | 111 | ##### color ##### 112 | primcol = brewer.pal(10, 'Paired') 113 | primcol[7] = '#999999' 114 | names(primcol) = c('B','Epi','Endo','Fib','Gland','Mye','Mus','Mast','Pla','T') 115 | primcol2 = c('#1f78b4','#ffc089','#B5EFB5','#793c1b', 116 | '#6a3d9a','#333333','#ffff33', 117 | '#df928e') 118 | names(primcol2) = c('B','Endo','Fib','Gland','Mye','Mus','T','Epi') 119 | 120 | 121 | redcol = c('#412626','#d62728','#fbb1b2') %>% colorRampPalette() 122 | yelcol = c('#77732d','#ede659','#faf9d5') %>% colorRampPalette() 123 | bluecol = c('#266065','#17becf','#7aecf8') %>% colorRampPalette() 124 | greencol = c('#202f20','#2ca02c','#b9fbb9') %>% colorRampPalette() 125 | orgcol = c('#7c4d24','#ff7f0e','#ffdcbd') %>% colorRampPalette() 126 | pinkcol = c('#a34085','#e377c2','#f29ed8') %>% colorRampPalette() 127 | purpcol = c('#7f52a9','#c5b0d5') %>% colorRampPalette() 128 | c('#b03533','#ff9896','#fed2d2') 129 | polychrome = c("#5A5156", 130 | "#E4E1E3", "#F6222E", "#FE00FA", "#16FF32", "#3283FE", 131 | "#FEAF16", "#B00068", "#1CFFCE", "#90AD1C", "#2ED9FF", 132 | "#DEA0FD", "#AA0DFE", "#F8A19F", "#325A9B", "#C4451C", 133 | "#1C8356", "#85660D", "#B10DA1", "#FBE426", "#1CBE4F", 134 | "#FA0087", "#FC1CBF", "#F7E1A0", "#C075A6", "#782AB6", 135 | "#AAF400", "#BDCDFF", "#822E1C", "#B5EFB5", "#7ED7D1", 136 | "#1C7F93", "#D85FF7", "#683B79", "#66B0FF", "#3B00FB") 137 | 138 | ctcol = c(bluecol(7), 139 | yelcol(3)[-1], 140 | redcol(9), 141 | pinkcol(3), 142 | purpcol(3),'#8a4071', 143 | orgcol(3)[-1], 144 | greencol(8),'#999999') 145 | names(ctcol) = c(paste0('NF-',1:3),'iCAF-1',paste0('myCAF-',1:3), 146 | 'Pla','B', 147 | paste0('Mac-',c('SPP1','IL1B','C1QC','LYVE1')),'cDC','pDC','Mo','Mo-Endo','Mast', 148 | paste0('CD4T-',c('n/m','fh','reg')), 149 | paste0('CD8T-',c('n/m','eff','ex')),'T-others', 150 | 'Endo','Gland', 151 | 'HY','DO-spp1','DO','AP','CY','QP','MD','TD', 152 | 'Mus') 153 | ctcol1 = c(bluecol(7), 154 | yelcol(3)[-3], 155 | redcol(9), 156 | pinkcol(3), 157 | purpcol(3),'#8a4071', 158 | orgcol(3)[-1], 159 | '#2ca02c','#999999') 160 | names(ctcol1) = c(paste0('myCAF-',3:1),'iCAF-1',paste0('NF-',3:1), 161 | 'Pla','B', 162 | paste0('Mac-',c('SPP1','IL1B','C1QC','LYVE1')),'cDC','pDC','Mo','Mo-Endo','Mast', 163 | paste0('CD4T-',c('n/m','fh','reg')), 164 | paste0('CD8T-',c('n/m','eff','ex')),'T-others', 165 | 'Endo','Gland', 166 | 'Epi', 167 | 'Mus') 168 | 169 | 170 | 171 | 172 | ##### sample-stage id ##### 173 | getSamplestage = function(sampleID){ 174 | sp1 = paste0('xe',sampleID,'_1') 175 | sp2 = paste0('xe',sampleID,'_2') 176 | sp3 = paste0('xe',sampleID,'_3') 177 | 178 | sp1 = get(sp1)@meta.data[,c('cellID','stage','primaryType')] %>% mutate(ID=paste0('T',sampleID,'S1')) 179 | sp2 = get(sp2)@meta.data[,c('cellID','stage','primaryType')] %>% mutate(ID=paste0('T',sampleID,'S2')) 180 | if(exists(sp3)){ 181 | sp3 = get(sp3)@meta.data[,c('cellID','stage','primaryType')] %>% mutate(ID=paste0('T',sampleID,'S3')) 182 | }else{sp3 = NULL} 183 | allmeta = Reduce(rbind, list(sp1,sp2,sp3)) 184 | allmeta$stage1 = allmeta$stage %>% str_split(fixed('-'),simplify = T) %>% .[,1] 185 | allmeta$ct3 = allmeta$primaryType %>% mapvalues(c('Mast','Pla'),c('Mye','B')) 186 | allmeta = allmeta %>% arrange(ID,stage) 187 | 188 | nmeta = allmeta %>% filter(stage1=='NOR') %>% select(ID,stage,stage1) %>% unique %>% mutate(num=row_number(.)) 189 | lmeta = allmeta %>% filter(stage1=='LGIN') %>% select(ID,stage,stage1) %>% unique %>% mutate(num=row_number(.)) 190 | hmeta = allmeta %>% filter(stage1=='HGIN') %>% select(ID,stage,stage1) %>% unique %>% mutate(num=row_number(.)) 191 | emeta = allmeta %>% filter(stage1=='ESCC') %>% select(ID,stage,stage1) %>% unique %>% mutate(num=row_number(.)) 192 | 193 | spstg = Reduce(rbind,list(nmeta,lmeta,hmeta,emeta)) 194 | spstg$sp_stg = paste0('T',sampleID,'-',spstg$stage1,'-',spstg$num) 195 | allmeta = spstg %>% dplyr::select(-num) %>% left_join(allmeta,.) 196 | rownames(allmeta) = allmeta$cellID 197 | return(allmeta[,c('cellID','stage1','sp_stg','ct3')]) 198 | } 199 | 200 | { 201 | spstg1 = getSamplestage(1) 202 | spstg2 = getSamplestage(2) 203 | spstg3 = getSamplestage(3) 204 | spstg4 = getSamplestage(4) 205 | spstg5 = getSamplestage(5) 206 | spstg6 = getSamplestage(6) 207 | 208 | xe1_1@meta.data = xe1_1@meta.data %>% dplyr::select(-contains(colnames(spstg1))) %>% 209 | mutate(cellID=rownames(.)) %>% left_join(spstg1) 210 | rownames(xe1_1@meta.data) = xe1_1$cellID 211 | xe1_2@meta.data = xe1_2@meta.data %>% dplyr::select(-contains(colnames(spstg1))) %>% 212 | mutate(cellID=rownames(.)) %>% left_join(spstg1) 213 | rownames(xe1_2@meta.data) = xe1_2$cellID 214 | xe1_3@meta.data = xe1_3@meta.data %>% dplyr::select(-contains(colnames(spstg1))) %>% 215 | mutate(cellID=rownames(.)) %>% left_join(spstg1) 216 | rownames(xe1_3@meta.data) = xe1_3$cellID 217 | 218 | xe2_1@meta.data = xe2_1@meta.data %>% dplyr::select(-contains(colnames(spstg2))) %>% 219 | mutate(cellID=rownames(.)) %>% left_join(spstg2) 220 | rownames(xe2_1@meta.data) = xe2_1$cellID 221 | xe2_2@meta.data = xe2_2@meta.data %>% dplyr::select(-contains(colnames(spstg2))) %>% 222 | mutate(cellID=rownames(.)) %>% left_join(spstg2) 223 | rownames(xe2_2@meta.data) = xe2_2$cellID 224 | xe2_3@meta.data = xe2_3@meta.data %>% dplyr::select(-contains(colnames(spstg2))) %>% 225 | mutate(cellID=rownames(.)) %>% left_join(spstg2) 226 | rownames(xe2_3@meta.data) = xe2_3$cellID 227 | 228 | xe3_1@meta.data = xe3_1@meta.data %>% dplyr::select(-contains(colnames(spstg3))) %>% 229 | mutate(cellID=rownames(.)) %>% left_join(spstg3) 230 | rownames(xe3_1@meta.data) = xe3_1$cellID 231 | xe3_2@meta.data = xe3_2@meta.data %>% dplyr::select(-contains(colnames(spstg3))) %>% 232 | mutate(cellID=rownames(.)) %>% left_join(spstg3) 233 | rownames(xe3_2@meta.data) = xe3_2$cellID 234 | 235 | xe4_1@meta.data = xe4_1@meta.data %>% dplyr::select(-contains(colnames(spstg4))) %>% 236 | mutate(cellID=rownames(.)) %>% left_join(spstg4) 237 | rownames(xe4_1@meta.data) = xe4_1$cellID 238 | xe4_2@meta.data = xe4_2@meta.data %>% dplyr::select(-contains(colnames(spstg4))) %>% 239 | mutate(cellID=rownames(.)) %>% left_join(spstg4) 240 | rownames(xe4_2@meta.data) = xe4_2$cellID 241 | 242 | xe5_1@meta.data = xe5_1@meta.data %>% dplyr::select(-contains(colnames(spstg5))) %>% 243 | mutate(cellID=rownames(.)) %>% left_join(spstg5) 244 | rownames(xe5_1@meta.data) = xe5_1$cellID 245 | xe5_2@meta.data = xe5_2@meta.data %>% dplyr::select(-contains(colnames(spstg5))) %>% 246 | mutate(cellID=rownames(.)) %>% left_join(spstg5) 247 | rownames(xe5_2@meta.data) = xe5_2$cellID 248 | xe5_3@meta.data = xe5_3@meta.data %>% dplyr::select(-contains(colnames(spstg5))) %>% 249 | mutate(cellID=rownames(.)) %>% left_join(spstg5) 250 | rownames(xe5_3@meta.data) = xe5_3$cellID 251 | xe5_2_split@meta.data = xe5_2_split@meta.data %>% dplyr::select(-contains(colnames(spstg5))) %>% 252 | mutate(cellID=rownames(.)) %>% left_join(spstg5) 253 | rownames(xe5_2_split@meta.data) = xe5_2_split$cellID 254 | xe5_3_split@meta.data = xe5_3_split@meta.data %>% dplyr::select(-contains(colnames(spstg5))) %>% 255 | mutate(cellID=rownames(.)) %>% left_join(spstg5) 256 | rownames(xe5_3_split@meta.data) = xe5_3_split$cellID 257 | 258 | xe6_1@meta.data = xe6_1@meta.data %>% dplyr::select(-contains(colnames(spstg6))) %>% 259 | mutate(cellID=rownames(.)) %>% left_join(spstg6) 260 | rownames(xe6_1@meta.data) = xe6_1$cellID 261 | xe6_2@meta.data = xe6_2@meta.data %>% dplyr::select(-contains(colnames(spstg6))) %>% 262 | mutate(cellID=rownames(.)) %>% left_join(spstg6) 263 | rownames(xe6_2@meta.data) = xe6_2$cellID 264 | xe6_3@meta.data = xe6_3@meta.data %>% dplyr::select(-contains(colnames(spstg6))) %>% 265 | mutate(cellID=rownames(.)) %>% left_join(spstg6) 266 | rownames(xe6_3@meta.data) = xe6_3$cellID 267 | 268 | saveRDS(xe1_1@meta.data, 'data/xenium_seurat_processed/xenium_t1_s1_meta.rds') 269 | saveRDS(xe1_2@meta.data, 'data/xenium_seurat_processed/xenium_t1_s2_meta.rds') 270 | saveRDS(xe1_3@meta.data, 'data/xenium_seurat_processed/xenium_t1_s3_meta.rds') 271 | saveRDS(xe2_1@meta.data, 'data/xenium_seurat_processed/xenium_t2_s1_meta.rds') 272 | saveRDS(xe2_2@meta.data, 'data/xenium_seurat_processed/xenium_t2_s2_meta.rds') 273 | saveRDS(xe2_3@meta.data, 'data/xenium_seurat_processed/xenium_t2_s3_meta.rds') 274 | saveRDS(xe3_1@meta.data, 'data/xenium_seurat_processed/xenium_t3_s1_meta.rds') 275 | saveRDS(xe3_2@meta.data, 'data/xenium_seurat_processed/xenium_t3_s2_meta.rds') 276 | saveRDS(xe4_1@meta.data, 'data/xenium_seurat_processed/xenium_t4_s1_meta.rds') 277 | saveRDS(xe4_2@meta.data, 'data/xenium_seurat_processed/xenium_t4_s2_meta.rds') 278 | saveRDS(xe5_1@meta.data, 'data/xenium_seurat_processed/xenium_t5_s1_meta.rds') 279 | saveRDS(xe5_2@meta.data, 'data/xenium_seurat_processed/xenium_t5_s2_meta.rds') 280 | saveRDS(xe5_3@meta.data, 'data/xenium_seurat_processed/xenium_t5_s3_meta.rds') 281 | saveRDS(xe5_2_split@meta.data, 'data/xenium_seurat_processed/xenium_t5_s2_split_meta.rds') 282 | saveRDS(xe5_3_split@meta.data, 'data/xenium_seurat_processed/xenium_t5_s3_split_meta.rds') 283 | saveRDS(xe6_1@meta.data, 'data/xenium_seurat_processed/xenium_t6_s1_meta.rds') 284 | saveRDS(xe6_2@meta.data, 'data/xenium_seurat_processed/xenium_t6_s2_meta.rds') 285 | saveRDS(xe6_3@meta.data, 'data/xenium_seurat_processed/xenium_t6_s3_meta.rds') 286 | } 287 | 288 | {## allmeta 289 | metalist = list(xe1_1@meta.data,xe1_2@meta.data,xe1_3@meta.data, 290 | xe2_1@meta.data,xe2_2@meta.data,xe2_3@meta.data, 291 | xe3_1@meta.data,xe3_2@meta.data, 292 | xe4_1@meta.data,xe4_2@meta.data, 293 | xe5_1@meta.data,xe5_2@meta.data,xe5_3@meta.data, 294 | xe6_1@meta.data,xe6_2@meta.data,xe6_3@meta.data) 295 | clm = lapply(metalist,colnames) %>% Reduce(intersect,.) 296 | allmeta = lapply(metalist,function(x){return(x[,clm])}) %>% Reduce(rbind,.) 297 | allmeta$stage1 = allmeta$stage1 %>% factor(levels = c('NOR','LGIN','HGIN','ESCC')) 298 | saveRDS(allmeta,'data/xenium_seurat_processed/allmeta.rds') 299 | 300 | ord = allmeta[,c('sp_stg','cellID','stage','stage1')] 301 | ord$sample = ord$cellID %>% str_split('_',simplify = T) %>% .[,1] 302 | ord = ord %>% select(-cellID) %>% unique() 303 | ord = ord %>% arrange(stage1,sample,stage) 304 | 305 | allmeta$sp_stg = allmeta$sp_stg %>% factor(levels = ord$sp_stg) 306 | allmeta$pid = allmeta$sp_stg %>% str_split(fixed('-'),simplify = T) %>% .[,1] 307 | allmeta$sid = allmeta$cellID %>% str_split('_',simplify = T) %>% .[,1] 308 | saveRDS(allmeta,'data/xenium_seurat_processed/allmeta.rds') 309 | } 310 | 311 | 312 | 313 | 314 | -------------------------------------------------------------------------------- /stp1_summary.R: -------------------------------------------------------------------------------- 1 | source('~/xenium/header.R') 2 | source('~/xenium/xenium_function.R') 3 | 4 | ##### input ##### 5 | job({ 6 | xe1_1 = read_rds('data/xenium_seurat_processed/xenium_t1_s1_seurat.rds') 7 | xe1_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s1_meta.rds') 8 | xe1_2 = read_rds('data/xenium_seurat_processed/xenium_t1_s2_seurat.rds') 9 | xe1_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s2_meta.rds') 10 | xe1_3 = read_rds('data/xenium_seurat_processed/xenium_t1_s3_seurat.rds') 11 | xe1_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s3_meta.rds') 12 | 13 | xe2_1 = read_rds('data/xenium_seurat_processed/xenium_t2_s1_seurat.rds') 14 | xe2_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s1_meta.rds') 15 | xe2_2 = read_rds('data/xenium_seurat_processed/xenium_t2_s2_seurat.rds') 16 | xe2_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s2_meta.rds') 17 | xe2_3 = read_rds('data/xenium_seurat_processed/xenium_t2_s3_seurat.rds') 18 | xe2_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s3_meta.rds') 19 | 20 | xe3_1 = read_rds('data/xenium_seurat_processed/xenium_t3_s1_seurat.rds') 21 | xe3_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t3_s1_meta.rds') 22 | xe3_2 = read_rds('data/xenium_seurat_processed/xenium_t3_s2_seurat.rds') 23 | xe3_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t3_s2_meta.rds') 24 | 25 | xe4_1 = read_rds('data/xenium_seurat_processed/xenium_t4_s1_seurat.rds') 26 | xe4_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t4_s1_meta.rds') 27 | xe4_2 = read_rds('data/xenium_seurat_processed/xenium_t4_s2_seurat.rds') 28 | xe4_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t4_s2_meta.rds') 29 | 30 | xe5_1 = read_rds('data/xenium_seurat_processed/xenium_t5_s1_seurat.rds') 31 | xe5_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s1_meta.rds') 32 | xe5_2 = read_rds('data/xenium_seurat_processed/xenium_t5_s2_seurat.rds') 33 | xe5_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s2_meta.rds') 34 | xe5_3 = read_rds('data/xenium_seurat_processed/xenium_t5_s3_seurat.rds') 35 | xe5_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s3_meta.rds') 36 | xe5_2_split = read_rds('data/xenium_seurat_processed/xenium_t5_s2_split_seurat.rds') 37 | xe5_2_split@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s2_split_meta.rds') 38 | xe5_3_split = read_rds('data/xenium_seurat_processed/xenium_t5_s3_split_seurat.rds') 39 | xe5_3_split@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s3_split_meta.rds') 40 | 41 | xe6_1 = read_rds('data/xenium_seurat_processed/xenium_t6_s1_seurat.rds') 42 | xe6_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s1_meta.rds') 43 | xe6_2 = read_rds('data/xenium_seurat_processed/xenium_t6_s2_seurat.rds') 44 | xe6_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s2_meta.rds') 45 | xe6_3 = read_rds('data/xenium_seurat_processed/xenium_t6_s3_seurat.rds') 46 | xe6_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s3_meta.rds') 47 | },import = 'auto') 48 | 49 | 50 | allmk = c('EPCAM','SFN','KRT4','KRT5', # epi 51 | 'FN1','DCN','COL1A1','COL1A2','COL3A1','COL6A1', # fibro 52 | 'PLN','MYH11', # muscle 53 | 'VWF','PECAM1','ENG','CDH5', # endo 54 | 'PTPRC', # immune 55 | 'CD2','CD3D','CD3E', # t cell 56 | 'CD1E','CD68','LYZ','CD14','FCGR3A', # myeloid 57 | 'MS4A2','KIT','CPA3', # mast 58 | 'CD19','CD79A','MS4A1', # B 59 | 'JCHAIN','IGHG2' # plasma 60 | ) 61 | allmk2 = c('SFN','KRT4','KRT5','EPCAM', # epi 62 | 'DCN','COL1A1','COL1A2','COL3A1', # fibro 63 | 'PTPRC', # immune 64 | 'CD2','CD3D','CD3E', # t cell 65 | 'CD19','CD79A','MS4A1', # B 66 | 'JCHAIN','IGHG2', # plasma 67 | 'VWF','PECAM1','ENG','CDH5', # endo 68 | 'CD68','LYZ','CD14','FCGR3A', # myeloid 69 | 'CPA3', # mast 70 | 'PLN','MYH11','ACTG2','CNN1','MYL9','TAGLN', # muscle 71 | 'AGR2','KRT23','WFDC2' # gland 72 | ) 73 | allmk3 = c('SFN','KRT4','KRT5','EPCAM', # epi 74 | 'VWF','PECAM1','ENG','CDH5', # endo 75 | 'COL1A1','COL1A2','COL3A1','PI16','FAP', # fibro 76 | 'PTPRC', # immune 77 | 'CD2','CD3D','CD3E','CD4','CD8A','CD8B','GZMH','PDCD1','HAVCR2','CD200','FOXP3', # t cell 78 | 'CD19','CD79A','MS4A1', # B 79 | 'JCHAIN','IGHG2', # plasma 80 | 'CD68','LYZ','CD14','FCGR3A', # myeloid 81 | 'CPA3', # mast 82 | 'AGR2','KRT23','WFDC2', # gland 83 | 'PLN','MYH11','ACTG2','CNN1','MYL9','TAGLN' # muscle 84 | ) 85 | 86 | epimarker = c('KRT15','MYC', # QP 87 | 'TOP2A','MKI67', # CY 88 | 'ANXA1','KRT13','KRT4','CSTA','S100A8','TRIM29', # MD 89 | 'SPRR3','SPRR2E','ECM1','EMP1', # TD 90 | 'HIF1A','SERPINE1','VEGFA', # HY 91 | 'KRT17', # RO 92 | 'SPP1','CES1', 'AKR1C3','AKR1C2','CBR1', # DO 93 | 'HLA-DRB1','HLA-DQB1'# AP 94 | ) 95 | 96 | myemarker = c('SFN','DCN','VWF','CD19','CD68','CD2', # pan marker 97 | 'GZMB','JCHAIN', # pDC 98 | 'CD1C','CD1E','FCER1A', # APC 99 | 'CCR7', # tDC 100 | 'FCN1','S100A9','S100A8','FCGR3A', # Mono 101 | 'IL1B', # Macro 102 | 'LYVE1','C1QC','C1QB','C1QA','SPP1','FN1' # Macro 103 | ) 104 | 105 | cd4marker = c('CD4','CD8A','CD8B', 106 | 'FOXP3','IL2RA','CTLA4', # reg 107 | 'TCF7','CCR7','SELL', # tn 108 | 'IL7R','CCL5', # mem 109 | 'CXCL13','CD200' # tfh 110 | ) 111 | 112 | cd8marker = c('CD4','CD8A','CD8B', 113 | 'TCF7','CCR7','SELL', # tn 114 | 'IL7R','CXCR4',#mem 115 | 'GNLY', 'NKG7', 'GZMB', 'GZMA', 'GZMH', 'GZMK', #Teff 116 | 'PDCD1','CXCL13','CTLA4','HAVCR2' # tex 117 | ) 118 | cytogene = c('GNLY', 'NKG7', 'GZMB', 'GZMA', 'GZMH', 'GZMK', 'KLRB1','TYROBP') 119 | exhgene = c('PDCD1','HAVCR2','CTLA4','TIGIT','LAG3','CXCL13') 120 | reggene = c('FOXP3','TIGIT','CD274','IL2RA') 121 | tmarker = c('CD4','CD8A','CD8B', 122 | 'GNLY', 'NKG7', 'GZMB', 'GZMA', 'GZMH', 'GZMK', 'KLRB1','TYROBP', 123 | 'PDCD1','HAVCR2','CTLA4','TIGIT','LAG3','CXCL13', 124 | 'FOXP3','CD274','IL2RA') 125 | 126 | fibmarker = c('PLA2G2A','GPX3','IGFBP6','PI16','ADH1B','TNXB','DCN','CXCL12', # NF 127 | 'IGF1','RGS5','HIF1A','CCND1','POSTN','STAT1','FAP','SERPINE1','DPT', # iCAF 128 | 'MMP11','MMP1','IL7R','STMN1','PDPN','RUNX1' # myCAF 129 | ) 130 | 131 | 132 | 133 | ##### colors ##### 134 | primcol = brewer.pal(10, 'Paired') 135 | primcol[7] = '#333333' 136 | names(primcol) = c('B','Epi','Endo','Fib','Gland','Mye','Mus','Mast','Pla','T') 137 | primcol1 = c('#1f78b4','#ffc089','#B5EFB5','#793c1b', 138 | '#6a3d9a','#333333','#ffff33', 139 | '#fbb4ae','#4a8cff','#ff8820','#a94e4e','#aa0000','#d7000588') 140 | names(primcol1) = c('B','Endo','Fib','Gland','Mye','Mus','T','NOR','LGIN','HGIN','ESCC') 141 | 142 | primcol2 = c('#1f78b4','#ffc089','#B5EFB5','#793c1b', 143 | '#6a3d9a','#333333','#ffff33', 144 | '#df928e') 145 | names(primcol2) = c('B','Endo','Fib','Gland','Mye','Mus','T','Epi') 146 | 147 | # primcol2 = c('#006837','#ffc089','white','#793c1b', 148 | # '#6a3d9a','#333333','#08519c', 149 | # '#df928e') 150 | # names(primcol2) = c('B','Endo','Fib','Gland','Mye','Mus','T','Epi') 151 | 152 | primcol3_ = c('#793c1b','#333333','#ffc089','#df928e', 153 | '#1f78b4','#6a3d9a','#df65b0','#1cbe4f','#e31a1c','#ffdcbd', 154 | '#ffffd4','#ffff68','#bbbb25','#ffe4cc','#ff9344','#a15008') 155 | names(primcol3_) = c('Gland','Mus','Endo','Epi', 156 | 'B','Mye','NF-PI16','NF','CAF','T-others', 157 | 'CD8T-n/m','CD8T-eff','CD8T-ex','CD4T-n/m','CD4T-fh','CD4T-reg') 158 | primcol3 = c('#793c1b','#333333','#ffc089','#df928e', 159 | '#1f78b4','#6a3d9a','#1cbe4f','#df65b0','#e31a1c','#ffdcbd', 160 | '#BDCDFF','#ffff68','#325A9B','#AAF400','#ff9344') 161 | names(primcol3) = c('Gland','Mus','Endo','Epi', 162 | 'B','Mye','NF','NF-PI16','CAF','T-others', 163 | 'T-naive','CD8T-eff','CD8T-ex','CD4T-fh','CD4T-reg') 164 | 165 | allcellcol = c('Basal'='#0339f8','Proliferation'='#ff9408','Differentiation'='#75bbfd', 166 | 'Terminal'='#4682B4','HT_Prolif'='#ff9408','HT_Diff/Term'='#4682B4', 167 | 'Invasive'='#de0c62','T cells'='#96f97b','B cells'='#028f1e', 168 | 'Myeloid cells'='#7e1e9c','Fibroblast'='#696969','CAF'='#FFFFFF', 169 | 'Endothelial cells'='#7b002c','Gland cells'='#c69f59','Myocytes'='#1b2431') 170 | 171 | 172 | 173 | redcol = c('#412626','#d62728','#fbb1b2') %>% colorRampPalette() 174 | yelcol = c('#77732d','#ede659','#faf9d5') %>% colorRampPalette() 175 | bluecol = c('#266065','#17becf','#7aecf8') %>% colorRampPalette() 176 | greencol = c('#202f20','#2ca02c','#b9fbb9') %>% colorRampPalette() 177 | orgcol = c('#7c4d24','#ff7f0e','#ffdcbd') %>% colorRampPalette() 178 | pinkcol = c('#a34085','#e377c2','#f29ed8') %>% colorRampPalette() 179 | purpcol = c('#7f52a9','#c5b0d5') %>% colorRampPalette() 180 | c('#b03533','#ff9896','#fed2d2') 181 | polychrome = c("#5A5156", 182 | "#E4E1E3", "#F6222E", "#FE00FA", "#16FF32", "#3283FE", 183 | "#FEAF16", "#B00068", "#1CFFCE", "#90AD1C", "#2ED9FF", 184 | "#DEA0FD", "#AA0DFE", "#F8A19F", "#325A9B", "#C4451C", 185 | "#1C8356", "#85660D", "#B10DA1", "#FBE426", "#1CBE4F", 186 | "#FA0087", "#FC1CBF", "#F7E1A0", "#C075A6", "#782AB6", 187 | "#AAF400", "#BDCDFF", "#822E1C", "#B5EFB5", "#7ED7D1", 188 | "#1C7F93", "#D85FF7", "#683B79", "#66B0FF", "#3B00FB") 189 | 190 | ctcol = c(bluecol(7), 191 | yelcol(3)[-1], 192 | redcol(9), 193 | pinkcol(3), 194 | purpcol(3),'#8a4071', 195 | orgcol(3)[-1], 196 | greencol(8),'#999999') 197 | names(ctcol) = c(paste0('NF-',1:3),'iCAF-1',paste0('myCAF-',1:3), 198 | 'Pla','B', 199 | paste0('Mac-',c('SPP1','IL1B','C1QC','LYVE1')),'cDC','pDC','Mo','Mo-Endo','Mast', 200 | paste0('CD4T-',c('n/m','fh','reg')), 201 | paste0('CD8T-',c('n/m','eff','ex')),'T-others', 202 | 'Endo','Gland', 203 | 'HY','DO-spp1','DO','AP','CY','QP','MD','TD', 204 | 'Mus') 205 | ctcol1 = c(bluecol(7), 206 | yelcol(3)[-3], 207 | redcol(9), 208 | pinkcol(3), 209 | purpcol(3),'#8a4071', 210 | orgcol(3)[-1], 211 | '#2ca02c','#999999') 212 | names(ctcol1) = c(paste0('myCAF-',3:1),'iCAF-1',paste0('NF-',3:1), 213 | 'Pla','B', 214 | paste0('Mac-',c('SPP1','IL1B','C1QC','LYVE1')),'cDC','pDC','Mo','Mo-Endo','Mast', 215 | paste0('CD4T-',c('n/m','fh','reg')), 216 | paste0('CD8T-',c('n/m','eff','ex')),'T-others', 217 | 'Endo','Gland', 218 | 'Epi', 219 | 'Mus') 220 | 221 | ## stage col 222 | colepi = c('#fdddda','#d2817d','#984646','#4c2323') %>% `names<-`(paste0('Epi-',c('NOR','LGIN','HGIN','ESCC'))) 223 | colfib = c('#e8fae8','#b5efb5','#7ea77e','#364736') %>% `names<-`(paste0('Fib-',c('NOR','LGIN','HGIN','ESCC'))) 224 | colendo = c('#ffecdb','#ffc089','#987352','#4c3929') %>% `names<-`(paste0('Endo-',c('NOR','LGIN','HGIN','ESCC'))) 225 | colmus = c('#d6d6d6','#adadad','#5b5b5b','#333333') %>% `names<-`(paste0('Mus-',c('NOR','LGIN','HGIN','ESCC'))) 226 | colgland = c('#e4d8d1','#ae8a76','#793c1b','#3c1e0d') %>% `names<-`(paste0('Gland-',c('NOR','LGIN','HGIN','ESCC'))) 227 | colt = c('#ffffd6','#ffff33','#b2b223','#32320a') %>% `names<-`(paste0('T-',c('NOR','LGIN','HGIN','ESCC'))) 228 | colb = c('#d2e3f0','#8fbbd9','#185f8f','#0f3c5a') %>% `names<-`(paste0('B-',c('NOR','LGIN','HGIN','ESCC'))) 229 | colmye = c('#e1d8ea','#9677b8','#54307b','#351e4d') %>% `names<-`(paste0('Mye-',c('NOR','LGIN','HGIN','ESCC'))) 230 | allcol = c(colepi,colfib,colendo,colmus,colgland,colt,colb,colmye) 231 | 232 | 233 | 234 | 235 | ##### spatial plot ##### 236 | {# balck bg 237 | objlist = setNames(list('xe1_1','xe1_2','xe1_3','xe2_1','xe2_2','xe2_3','xe3_1','xe3_2', 238 | 'xe4_1','xe4_2','xe5_1','xe5_2_split','xe5_3_split','xe6_1','xe6_2','xe6_3'), 239 | c('T1S1','T1S2','T1S3','T2S1','T2S2','T2S3','T3S1','T3S2', 240 | 'T4S1','T4S2','T5S1','T5S2','T5S3','T6S1','T6S2','T6S3')) 241 | lapply(names(objlist), function(x){ 242 | dt = get(objlist[[x]]) 243 | 244 | flip=F;reversex=F;reversey=F 245 | g1 = c('T1S1','T1S2','T1S3','T3S1','T3S2','T4S1') 246 | g2 = c('T2S1','T2S2','T2S3') 247 | g3 = c('T4S2') 248 | g4 = c('T5S1','T5S2','T5S3') 249 | g5 = c('T6S1') 250 | g6 = c('T6S2','T6S3') 251 | if(x%in%g1){flip=T;reversex=T} 252 | if(x%in%g2){flip=T} 253 | if(x%in%g3){flip=T;reversey=T} 254 | if(x%in%g5){reversey=T} 255 | if(x%in%g6){reversex=T} 256 | 257 | fname = paste0('stp1_summary/plot/',x,'_primarytype.png') 258 | p = xedimplot(dt, groupby = 'ct3',flip = flip,reversex = reversex,reversey = reversey,color = primcol2,bgcol = 'black',pt.size = 0.1)+NoLegend() 259 | ggsave(fname,p,width = 40, height = 15, limitsize = F) 260 | 261 | fname = paste0('stp1_summary/stage-prim_plot/',x,'_primarytype.png') 262 | dt$tmp = paste0(dt$ct3,'-',dt$stage1) 263 | p = xedimplot(dt, groupby = 'tmp',flip = flip,reversex = reversex,reversey = reversey,color = allcol,bgcol = 'black',pt.size = 0.1)+NoLegend() 264 | ggsave(fname,p,width = 40, height = 15, limitsize = F) 265 | }) 266 | } 267 | {# white bg 268 | objlist = setNames(list('xe1_1','xe1_2','xe1_3','xe2_1','xe2_2','xe2_3','xe3_1','xe3_2', 269 | 'xe4_1','xe4_2','xe5_1','xe5_2_split','xe5_3_split','xe6_1','xe6_2','xe6_3'), 270 | c('T1S1','T1S2','T1S3','T2S1','T2S2','T2S3','T3S1','T3S2', 271 | 'T4S1','T4S2','T5S1','T5S2','T5S3','T6S1','T6S2','T6S3')) 272 | lapply(names(objlist), function(x){ 273 | dt = get(objlist[[x]]) 274 | 275 | flip=F;reversex=F;reversey=F 276 | g1 = c('T1S1','T1S2','T1S3','T3S1','T3S2','T4S1') 277 | g2 = c('T2S1','T2S2','T2S3') 278 | g3 = c('T4S2') 279 | g4 = c('T5S1','T5S2','T5S3') 280 | g5 = c('T6S1') 281 | g6 = c('T6S2','T6S3') 282 | if(x%in%g1){flip=T;reversex=T} 283 | if(x%in%g2){flip=T} 284 | if(x%in%g3){flip=T;reversey=T} 285 | if(x%in%g5){reversey=T} 286 | if(x%in%g6){reversex=T} 287 | 288 | fname = paste0('stp1_summary/plot_whitebg/',x,'_primarytype.png') 289 | p = xedimplot(dt, groupby = 'ct3',flip = flip,reversex = reversex,reversey = reversey,color = primcol2,bgcol = 'white',pt.size = 0.1)+NoLegend() 290 | ggsave(fname,p,width = 30, height = 15, limitsize = F) 291 | }) 292 | } 293 | 294 | xedimplot(xe1_2, groupby = 'stage',reversex = T,flip = T,color = primcol1) 295 | xedimplot(xe2_1, groupby = 'stage',flip = T,color = primcol1) 296 | xedimplot(xe3_1, groupby = 'stage1',reversex = T,flip = T,color = primcol1) 297 | 298 | 299 | xe1_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s2_meta.rds') 300 | newmt = c('cellID','cellType_merge') 301 | newmt = rbind(srttc@meta.data[,newmt],srtfib@meta.data[,newmt]) 302 | newmt$cellType_merge = newmt$cellType_merge %>% 303 | mapvalues(c('CD8T-n/m','CD4T-n/m'),c('T-naive','T-naive')) 304 | xe1_2$cellType_merge = xe1_2$ct3 305 | xe1_2 = renewMeta(xe1_2, newmt) 306 | p = xedimplot(xe1_2,'cellType_merge',flip = T,reversex = T,color = primcol3,bgcol = 'black', 307 | ptlevels = rev(names(primcol3))) 308 | ggsave('xe12_grd.png',p,width = 30, height = 15, limitsize = F) 309 | p = xedimplot(xe1_2,'cellType_merge',flip = T,reversex = T,color = primcol3_,bgcol = 'black', 310 | ptlevels = rev(names(primcol3_))) 311 | ggsave('xe12_dis.png',p,width = 30, height = 15, limitsize = F) 312 | 313 | 314 | 315 | 316 | ##### spatial stage #### 317 | objlist = setNames(list('xe1_1','xe1_2','xe1_3','xe2_1','xe2_2','xe2_3','xe3_1','xe3_2', 318 | 'xe4_1','xe4_2','xe5_1','xe5_2_split','xe5_3_split','xe6_1','xe6_2','xe6_3'), 319 | c('T1S1','T1S2','T1S3','T2S1','T2S2','T2S3','T3S1','T3S2', 320 | 'T4S1','T4S2','T5S1','T5S2','T5S3','T6S1','T6S2','T6S3')) 321 | lapply(names(objlist), function(x){ 322 | dt = get(objlist[[x]]) 323 | 324 | flip=F;reversex=F;reversey=F 325 | g1 = c('T1S1','T1S2','T1S3','T3S1','T3S2','T4S1') 326 | g2 = c('T2S1','T2S2','T2S3') 327 | g3 = c('T4S2') 328 | g4 = c('T5S1','T5S2','T5S3') 329 | g5 = c('T6S1') 330 | g6 = c('T6S2','T6S3') 331 | if(x%in%g1){flip=T;reversex=T} 332 | if(x%in%g2){flip=T} 333 | if(x%in%g3){flip=T;reversey=T} 334 | if(x%in%g5){reversey=T} 335 | if(x%in%g6){reversex=T} 336 | 337 | fname = paste0('stp1_summary/stage-spatial_plot/',x,'.png') 338 | p = xedimplot(dt, groupby = 'stage1',flip = flip,reversex = reversex,reversey = reversey, 339 | color = col_stage,bgcol = 'black',pt.size = 0.1)+NoLegend() 340 | ggsave(fname,p,width = 20, height = 10, limitsize = F) 341 | }) 342 | 343 | 344 | 345 | 346 | ##### region of intrested ##### 347 | roi = data.frame(xmin=c(900,7500,15000,18500,19000,19800,21500), 348 | xmax=c(1900,8500,16000,19500,20000,20800,22500), 349 | ymin=c(3000,4000,3500,3300,3300,3300,3200), 350 | ymax=c(4000,500,4500,4300,4300,4300,4200)) 351 | rownames(roi) = c('T','HT','H','LH','L','NL','N') 352 | 353 | xedimplot(xenium1_2, 'primaryType', flip = T, reversex = T, color = primcol,pt.size=0.1)+NoLegend()+ 354 | geom_rect(aes(xmin=900,xmax=2000,ymin=3000,ymax=4000), fill=NA,color='yellow')+ 355 | geom_rect(aes(xmin=15000,xmax=16000,ymin=3800,ymax=4500), fill=NA,color='yellow')+ 356 | geom_rect(aes(xmin=19100,xmax=20000,ymin=3300,ymax=4200), fill=NA,color='yellow')+ 357 | geom_rect(aes(xmin=21000,xmax=22500,ymin=3000,ymax=4200), fill=NA,color='yellow') 358 | 359 | ggplot()+ 360 | geom_rect(aes(xmin=c(900,7000,15000,18500,19100,19500,21000), 361 | xmax=c(2000,9000,16000,19400,20000,21000,22500), 362 | ymin=c(3000,4000,3800,3300,3300,3300,3000), 363 | ymax=c(4000,5100,4500,4200,4200,4200,4200)), fill=NA,color='yellow') 364 | 365 | 366 | {## stage roi 367 | ## t1 368 | roi = data.frame(xmin=c(900,7500,15000,18500,19000,19800,21500), 369 | xmax=c(1900,8500,16000,19500,20000,20800,22500), 370 | ymin=c(3000,4000,3500,3300,3300,3300,3200), 371 | ymax=c(4000,500,4500,4300,4300,4300,4200)) 372 | 373 | p1 = xedimplot(xenium1_2, 'tmp', flip = T, reversex = T, color = ctcol1,pt.size=0.5,zoomin = roi[7,], 374 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 375 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 376 | NoLegend()+ggtitle(label = 'NOR Region') 377 | p2 = xedimplot(xenium1_2, 'tmp', flip = T, reversex = T, color = ctcol1,pt.size=0.5,zoomin = roi[5,], 378 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 379 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 380 | NoLegend()+ggtitle(label = 'LGIN Region') 381 | p3 = xedimplot(xenium1_2, 'tmp', flip = T, reversex = T, color = ctcol1,pt.size=0.5,zoomin = roi[3,], 382 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 383 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 384 | NoLegend()+ggtitle(label = 'HGIN Region') 385 | p4 = xedimplot(xenium1_2, 'tmp', flip = T, reversex = T, color = ctcol1,pt.size=0.5,zoomin = roi[1,], 386 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 387 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 388 | NoLegend()+ggtitle(label = 'ESCC Region') 389 | ggsave('stp1_summary/stage_plot/stage_NOR_1.png',p1,width = 10,height = 8) 390 | ggsave('stp1_summary/stage_plot/stage_LGIN_1.png',p2,width = 10,height = 8) 391 | ggsave('stp1_summary/stage_plot/stage_HGIN_1.png',p3,width = 10,height = 8) 392 | ggsave('stp1_summary/stage_plot/stage_ESCC_1.png',p4,width = 10,height = 8) 393 | 394 | ## t2 395 | roi = matrix(c(7500,8500,1200,2200, 396 | 14000,15000,2600,3600),ncol = 4,byrow = T) 397 | p1 = xedimplot(xenium2_1, 'tmp', flip = T, color = ctcol1,pt.size=0.5,zoomin = roi[1,], 398 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 399 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 400 | NoLegend()+ggtitle(label = 'NOR Region') 401 | p2 = xedimplot(xenium2_1, 'tmp', flip = T, color = ctcol1,pt.size=0.5,zoomin = roi[2,], 402 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 403 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 404 | NoLegend()+ggtitle(label = 'HGIN Region') 405 | ggsave('stp1_summary/stage_plot/stage_NOR_2.png',p1,width = 10,height = 8) 406 | ggsave('stp1_summary/stage_plot/stage_HGIN_2.png',p2,width = 10,height = 8) 407 | 408 | ## t3 409 | roi = matrix(c(16700,17700,2100,3100, 410 | 13400,14400,1700,2700, 411 | 4900,5900,3300,4300, 412 | 5900,6900,600,1600),ncol = 4,byrow = T) 413 | p1 = xedimplot(xenium3_1, 'tmp', flip = T,reversex = T, color = ctcol1,pt.size=0.5,zoomin = roi[1,], 414 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 415 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 416 | NoLegend()+ggtitle(label = 'NOR Region') 417 | p2 = xedimplot(xenium3_1, 'tmp', flip = T,reversex = T, color = ctcol1,pt.size=0.5,zoomin = roi[2,], 418 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 419 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 420 | NoLegend()+ggtitle(label = 'LGIN Region') 421 | p3 = xedimplot(xenium3_1, 'tmp', flip = T,reversex = T, color = ctcol1,pt.size=0.5,zoomin = roi[3,], 422 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 423 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 424 | NoLegend()+ggtitle(label = 'HGIN Region') 425 | p4 = xedimplot(xenium3_1, 'tmp', flip = T,reversex = T, color = ctcol1,pt.size=0.5,zoomin = roi[4,], 426 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 427 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 428 | NoLegend()+ggtitle(label = 'ESCC Region') 429 | ggsave('stp1_summary/stage_plot/stage_NOR_3.png',p1,width = 10,height = 8) 430 | ggsave('stp1_summary/stage_plot/stage_LGIN_3.png',p2,width = 10,height = 8) 431 | ggsave('stp1_summary/stage_plot/stage_HGIN_3.png',p3,width = 10,height = 8) 432 | ggsave('stp1_summary/stage_plot/stage_ESCC_3.png',p4,width = 10,height = 8) 433 | 434 | ## t4 435 | roi = matrix(c(13500,14500,3800,4800, 436 | 10800,11800,4000,5000, 437 | 6800,7800,0,1000),ncol = 4,byrow = T) 438 | p1 = xedimplot(xenium4_1, 'tmp', flip = T,reversex = T, color = ctcol1,pt.size=0.5,zoomin = roi[1,], 439 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 440 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 441 | NoLegend()+ggtitle(label = 'NOR Region') 442 | p2 = xedimplot(xenium4_1, 'tmp', flip = T,reversex = T, color = ctcol1,pt.size=0.5,zoomin = roi[2,], 443 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 444 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 445 | NoLegend()+ggtitle(label = 'HGIN Region') 446 | p3 = xedimplot(xenium4_1, 'tmp', flip = T,reversex = T, color = ctcol1,pt.size=0.5,zoomin = roi[3,], 447 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 448 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 449 | NoLegend()+ggtitle(label = 'ESCC Region') 450 | ggsave('stp1_summary/stage_plot/stage_NOR_4.png',p1,width = 10,height = 8) 451 | ggsave('stp1_summary/stage_plot/stage_HGIN_4.png',p2,width = 10,height = 8) 452 | ggsave('stp1_summary/stage_plot/stage_ESCC_4.png',p3,width = 10,height = 8) 453 | 454 | ## t5 455 | roi = matrix(c(1700,2700,1000,2000, 456 | 3500,4500,3200,4200),ncol = 4,byrow = T) 457 | p1 = xedimplot(xenium5_1, 'tmp', reversey = T, color = ctcol1,pt.size=0.5,zoomin = roi[1,], 458 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 459 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 460 | NoLegend()+ggtitle(label = 'LGIN Region') 461 | p2 = xedimplot(xenium5_1, 'tmp', reversey = F, color = ctcol1,pt.size=0.5,zoomin = roi[2,], 462 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 463 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 464 | NoLegend()+ggtitle(label = 'HGIN Region') 465 | ggsave('stp1_summary/stage_plot/stage_LGIN_5.png',p1,width = 10,height = 8) 466 | ggsave('stp1_summary/stage_plot/stage_HGIN_5.png',p2,width = 10,height = 8) 467 | 468 | ## t6 469 | roi = matrix(c(7500,8500,1700,2700, 470 | 2800,3800,1600,2600, 471 | 3800,4800,1400,2400),ncol = 4,byrow = T) 472 | p1 = xedimplot(xenium6_1, 'tmp', reversey = T, color = ctcol1,pt.size=0.5,zoomin = roi[1,], 473 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 474 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 475 | NoLegend()+ggtitle(label = 'NOR Region') 476 | p2 = xedimplot(xenium6_1, 'tmp', reversey = T, color = ctcol1,pt.size=0.5,zoomin = roi[2,], 477 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 478 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 479 | NoLegend()+ggtitle(label = 'HGIN Region') 480 | p3 = xedimplot(xenium6_1, 'tmp', reversey = T, color = ctcol1,pt.size=0.5,zoomin = roi[3,], 481 | cellborder = T,areaalpha = 0.3,linewidth = 0.2)+ 482 | theme(axis.text = element_blank(),axis.line = element_blank(),axis.ticks = element_blank())+xlab('')+ylab('')+ 483 | NoLegend()+ggtitle(label = 'ESCC Region') 484 | ggsave('stp1_summary/stage_plot/stage_NOR_6.png',p1,width = 10,height = 8) 485 | ggsave('stp1_summary/stage_plot/stage_HGIN_6.png',p2,width = 10,height = 8) 486 | ggsave('stp1_summary/stage_plot/stage_ESCC_6.png',p3,width = 10,height = 8) 487 | } 488 | 489 | 490 | 491 | 492 | ### zoomin epi 493 | xenium1_2$tmp = xenium1_2$cellType_merge 494 | xenium1_2$tmp[xenium1_2$primaryType!='Epi'] = xenium1_2$primaryType[xenium1_2$primaryType!='Epi'] %>% as.character() 495 | 496 | epicol = c(brewer.pal(7,'Set3'),brewer.pal(9,'Set1')) 497 | names(epicol) = c(unique(xenium1_2$tmp[xenium1_2$primaryType=='Epi']),unique(xenium1_2$tmp[xenium1_2$primaryType!='Epi'])) 498 | 499 | xenium1_2$tmp = xenium1_2$tmp %>% 500 | factor(levels = names(epicol)) 501 | 502 | 503 | 504 | xedimplot(xenium1_2, 'tmp', flip = T, reversex = T, color = epicol,pt.size=0.5,zoomin = roi[7,])+ggtitle(label = 'NOR Region') 505 | xedimplot(xenium1_2, 'tmp', flip = T, reversex = T, color = epicol,pt.size=0.5,zoomin = roi[6,])+ggtitle(label = 'NOR-LGIN') 506 | xedimplot(xenium1_2, 'tmp', flip = T, reversex = T, color = epicol,pt.size=0.5,zoomin = roi[5,])+ggtitle(label = 'LGIN Region') 507 | xedimplot(xenium1_2, 'tmp', flip = T, reversex = T, color = epicol,pt.size=0.5,zoomin = roi[3,])+ggtitle(label = 'HGIN Region') 508 | xedimplot(xenium1_2, 'tmp', flip = T, reversex = T, color = epicol,pt.size=0.5,zoomin = roi[1,])+ggtitle(label = 'ESCC Region') 509 | 510 | 511 | 512 | 513 | ##### umi violin ##### 514 | VlnPlot(srt, features = c('nCount_RNA','nFeature_RNA'),pt.size = 0, 515 | group.by = 'sp_stg',ncol = 1) 516 | 517 | 518 | 519 | 520 | ##### marker dotplot ##### 521 | DotPlot(srt, features = allmk2, group.by = 'ct3')+RotatedAxis() 522 | mydotp(srt,allmk2,'ct3',rev(brewer.pal(9,'RdBu')),lims = c(-2,2))+RotatedAxis() 523 | ggsave('stp1_summary/summary_plot/marker_dotp.pdf',width = 5,height = 8) 524 | 525 | srt$ct3 = srt$ct3 %>% factor(levels = c('Epi','Fib','T','B','Endo','Mye','Mus','Gland')) 526 | mydotp(srt,allmk2,'ct3',rev(brewer.pal(9,'RdBu')),lims = c(-2,2))+rotate_x_text() 527 | ggsave('stp1_summary/summary_plot/marker_dotp.pdf',width = 5,height = 8) 528 | 529 | srt$ct3 = srt$ct3 %>% factor(levels = rev(c('Epi','Fib','T','B','Endo','Mye','Mus','Gland'))) 530 | DotPlot(srt, features = allmk2, group.by = 'ct3',cols = 'RdBu')+rotate_x_text() 531 | ggsave('stp1_summary/summary_plot/marker_dotp_2.pdf',width = 10,height = 3.5) 532 | 533 | 534 | newmt = c('cellID','cellType_merge') 535 | newmt = rbind(srttc@meta.data[,newmt],srtfib@meta.data[,newmt]) 536 | newmt$cellType_merge = newmt$cellType_merge %>% 537 | mapvalues(c('CD8T-n/m','CD4T-n/m'),c('T-naive','T-naive')) 538 | 539 | srt$cellType_merge = srt$ct3 %>% as.character() 540 | srt@meta.data[newmt$cellID,'cellType_merge'] = newmt$cellType_merge 541 | srt$cellType_merge = srt$cellType_merge %>% 542 | factor(levels = c('Epi','Endo','NF','NF-PI16','CAF','T-others', 543 | 'T-naive','CD8T-eff','CD8T-ex','CD4T-fh','CD4T-reg', 544 | 'B','Mye','Gland','Mus')) 545 | srt$cellType_merge = srt$cellType_merge %>% 546 | mapvalues(c('Epi','Endo','B','Mye','Gland','Mus'), 547 | c('Epithelial cells','Endothelial cells ','B cells ','Myeloid cells','Gland cells','Myocytes')) 548 | p = mydotp(srt,allmk3,'cellType_merge',rev(brewer.pal(9,'RdBu')),lims = c(-2,2))+RotatedAxis() 549 | ggsave('stp1_summary/summary_plot/marker_dotp_subtype.pdf',width = 7,height = 14) 550 | 551 | 552 | 553 | 554 | 555 | 556 | -------------------------------------------------------------------------------- /stp2_epiArea.R: -------------------------------------------------------------------------------- 1 | source('~/xenium/header.R') 2 | source('~/xenium/xenium_function.R') 3 | 4 | 5 | 6 | ##### input ##### 7 | job({ 8 | xe1_1 = read_rds('data/xenium_seurat_processed/xenium_t1_s1_seurat.rds') 9 | xe1_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s1_meta.rds') 10 | xe1_2 = read_rds('data/xenium_seurat_processed/xenium_t1_s2_seurat.rds') 11 | xe1_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s2_meta.rds') 12 | xe1_3 = read_rds('data/xenium_seurat_processed/xenium_t1_s3_seurat.rds') 13 | xe1_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s3_meta.rds') 14 | 15 | xe2_1 = read_rds('data/xenium_seurat_processed/xenium_t2_s1_seurat.rds') 16 | xe2_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s1_meta.rds') 17 | xe2_2 = read_rds('data/xenium_seurat_processed/xenium_t2_s2_seurat.rds') 18 | xe2_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s2_meta.rds') 19 | xe2_3 = read_rds('data/xenium_seurat_processed/xenium_t2_s3_seurat.rds') 20 | xe2_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s3_meta.rds') 21 | 22 | xe3_1 = read_rds('data/xenium_seurat_processed/xenium_t3_s1_seurat.rds') 23 | xe3_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t3_s1_meta.rds') 24 | xe3_2 = read_rds('data/xenium_seurat_processed/xenium_t3_s2_seurat.rds') 25 | xe3_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t3_s2_meta.rds') 26 | 27 | xe4_1 = read_rds('data/xenium_seurat_processed/xenium_t4_s1_seurat.rds') 28 | xe4_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t4_s1_meta.rds') 29 | xe4_2 = read_rds('data/xenium_seurat_processed/xenium_t4_s2_seurat.rds') 30 | xe4_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t4_s2_meta.rds') 31 | 32 | xe5_1 = read_rds('data/xenium_seurat_processed/xenium_t5_s1_seurat.rds') 33 | xe5_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s1_meta.rds') 34 | xe5_2 = read_rds('data/xenium_seurat_processed/xenium_t5_s2_seurat.rds') 35 | xe5_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s2_meta.rds') 36 | xe5_3 = read_rds('data/xenium_seurat_processed/xenium_t5_s3_seurat.rds') 37 | xe5_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s3_meta.rds') 38 | xe5_2_split = read_rds('data/xenium_seurat_processed/xenium_t5_s2_split_seurat.rds') 39 | xe5_2_split@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s2_split_meta.rds') 40 | xe5_3_split = read_rds('data/xenium_seurat_processed/xenium_t5_s3_split_seurat.rds') 41 | xe5_3_split@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s3_split_meta.rds') 42 | 43 | xe6_1 = read_rds('data/xenium_seurat_processed/xenium_t6_s1_seurat.rds') 44 | xe6_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s1_meta.rds') 45 | xe6_2 = read_rds('data/xenium_seurat_processed/xenium_t6_s2_seurat.rds') 46 | xe6_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s2_meta.rds') 47 | xe6_3 = read_rds('data/xenium_seurat_processed/xenium_t6_s3_seurat.rds') 48 | xe6_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s3_meta.rds') 49 | },import = 'auto') 50 | 51 | srttc = read_rds('data/xenium_seurat_processed/ct_t_srt.rds') 52 | 53 | 54 | 55 | cd4marker = c('CD4','CD8A','CD8B', 56 | 'FOXP3','IL2RA','CTLA4', # reg 57 | 'TCF7','CCR7','SELL', # tn 58 | 'IL7R','CCL5', # mem 59 | 'CXCL13','CD200' # tfh 60 | ) 61 | 62 | cd8marker = c('CD4','CD8A','CD8B', 63 | 'TCF7','CCR7','SELL', # tn 64 | 'IL7R','CXCR4',#mem 65 | 'GNLY', 'NKG7', 'GZMB', 'GZMA', 'GZMH', 'GZMK', #Teff 66 | 'PDCD1','CXCL13','CTLA4','HAVCR2' # tex 67 | ) 68 | 69 | cytogene = c('GNLY', 'NKG7', 'GZMB', 'GZMA', 'GZMH', 'GZMK', 'KLRB1','TYROBP') 70 | exhgene = c('PDCD1','HAVCR2','CTLA4','TIGIT','LAG3','CXCL13') 71 | reggene = c('FOXP3','TIGIT','CD274','IL2RA') 72 | 73 | fibmarker = c('PLA2G2A','GPX3','IGFBP6','PI16','ADH1B','TNXB','DCN','CXCL12', # NF 74 | 'IGF1','RGS5','HIF1A','CCND1','POSTN','STAT1','FAP','SERPINE1','DPT', # iCAF 75 | 'MMP11','MMP1','IL7R','STMN1','PDPN','RUNX1' # myCAF 76 | ) 77 | 78 | 79 | 80 | primcol = brewer.pal(10, 'Paired') 81 | primcol[7] = '#999999' 82 | names(primcol) = c('B','Epi','Endo','Fib','Gland','Mye','Mus','Mast','Pla','T') 83 | 84 | primcol2 = c('#1f78b4','#ffc089','#B5EFB5','#793c1b', 85 | '#6a3d9a','#333333','#ffff33','#df928e') 86 | names(primcol2) = c('B','Endo','Fib','Gland','Mye','Mus','T','Epi') 87 | 88 | 89 | 90 | getp = function(srt, features, group.by){ 91 | p1 = (DimPlot(srt, group.by = group.by, label = T)+NoLegend())+DimPlot(srt, group.by = group.by, label = T) 92 | p2 = DotPlot(srt, features = features, group.by = group.by)+RotatedAxis() 93 | return(p1/p2) 94 | } 95 | 96 | srtpreproc = function(srt){ 97 | srt = SCTransform(srt) 98 | srt = RunPCA(srt) 99 | srt = RunUMAP(srt, dims = 1:10) 100 | srt = FindNeighbors(srt,dims = 1:10) 101 | srt = FindClusters(srt, resolution = seq(0.1,1,0.1)) 102 | return(srt) 103 | } 104 | 105 | 106 | 107 | ##### epi area ##### 108 | areap = function(coords,ap){ 109 | ap = ap[rownames(coords),] 110 | coords = cbind(coords,ap) 111 | 112 | coords %>% 113 | ggplot(aes(x,y))+ 114 | theme1+ 115 | geom_point(aes(color=type)) 116 | } 117 | 118 | {# t1s1 119 | tmp = subset(xe1_1, cells = xe1_1$cellID[xe1_1$ct3=='Epi']) 120 | crd = tmp@images$fov@boundaries$centroids@coords %>% data.frame 121 | rownames(crd) = colnames(tmp) 122 | colnames(crd) = c('y','x') 123 | 124 | crd_tmp = crd %>% filter(x<=3000) 125 | plotly::plot_ly(crd_tmp,x=~x,y=~y) 126 | cp = c('T1S1_clgbikjg-1','T1S1_mkmhefia-1','T1S1_kcaoffke-1','T1S1_mlocnknh-1') 127 | job({ap_tmp = areaPoint(dist(crd_tmp),cpoint = cp,r = 50)},import='auto') 128 | areap(crd_tmp,ap_tmp) 129 | id1 = ap_tmp$id[ap_tmp$type!='isolated'] 130 | crd_tmp = crd_tmp[id1,c('x','y')] 131 | as_tmp = alphahull::ashape(crd_tmp,alpha = 100) 132 | plot(as_tmp) 133 | poly1 = extractpoly(as_tmp) 134 | plotpoly(poly1) 135 | 136 | crd_tmp = crd %>% filter(x<=6000,x>2900) 137 | plotly::plot_ly(crd_tmp,x=~x,y=~y) 138 | cp = c('T1S1_jajnkhib-1','T1S1_ofjhckbd-1') 139 | job({ap_tmp = areaPoint(dist(crd_tmp),cpoint = cp,r = 30)},import='auto') 140 | areap(crd_tmp,ap_tmp) 141 | id1 = ap_tmp$id[ap_tmp$type!='isolated'] 142 | crd_tmp = crd_tmp[id1,c('x','y')] 143 | as_tmp = alphahull::ashape(crd_tmp,alpha = 100) 144 | plot(as_tmp) 145 | poly2 = extractpoly(as_tmp) 146 | plotpoly(poly2) 147 | 148 | crd_tmp = crd %>% filter(x<=9000,x>5900) 149 | plotly::plot_ly(crd_tmp,x=~x,y=~y) 150 | cp = c('T1S1_gaenjdkg-1','T1S1_ofjienlh-1') 151 | job({ap_tmp = areaPoint(dist(crd_tmp),cpoint = cp,r = 30)},import='auto') 152 | areap(crd_tmp,ap_tmp) 153 | id1 = ap_tmp$id[ap_tmp$type!='isolated'] 154 | crd_tmp = crd_tmp[id1,c('x','y')] 155 | as_tmp = alphahull::ashape(crd_tmp,alpha = 100) 156 | plot(as_tmp) 157 | poly3 = extractpoly(as_tmp) 158 | plotpoly(poly3) 159 | 160 | crd_tmp = crd %>% filter(x<=12000,x>8900) 161 | plotly::plot_ly(crd_tmp,x=~x,y=~y) 162 | cp = c('T1S1_jgnbndfl-1','T1S1_abgljkme-1') 163 | job({ap_tmp = areaPoint(dist(crd_tmp),cpoint = cp,r = 30)},import='auto') 164 | areap(crd_tmp,ap_tmp) 165 | id1 = ap_tmp$id[ap_tmp$type!='isolated'] 166 | crd_tmp = crd_tmp[id1,c('x','y')] 167 | as_tmp = alphahull::ashape(crd_tmp,alpha = 100) 168 | plot(as_tmp) 169 | poly4 = extractpoly(as_tmp) 170 | plotpoly(poly4) 171 | 172 | crd_tmp = crd %>% filter(x<=15000,x>11900) 173 | plotly::plot_ly(crd_tmp,x=~x,y=~y) 174 | cp = c('T1S1_lecadonb-1') 175 | job({ap_tmp = areaPoint(dist(crd_tmp),cpoint = cp,r = 30)},import='auto') 176 | areap(crd_tmp,ap_tmp) 177 | id1 = ap_tmp$id[ap_tmp$type!='isolated'] 178 | crd_tmp = crd_tmp[id1,c('x','y')] 179 | as_tmp = alphahull::ashape(crd_tmp,alpha = 100) 180 | plot(as_tmp) 181 | poly5 = extractpoly(as_tmp) 182 | plotpoly(poly5) 183 | 184 | crd_tmp = crd %>% filter(x<=18000,x>14900) 185 | plotly::plot_ly(crd_tmp,x=~x,y=~y) 186 | cp = c('T1S1_ofnedcgj-1','T1S1_oikjdmkd-1') 187 | job({ap_tmp = areaPoint(dist(crd_tmp),cpoint = cp,r = 30)},import='auto') 188 | areap(crd_tmp,ap_tmp) 189 | id1 = ap_tmp$id[ap_tmp$type!='isolated'] 190 | crd_tmp = crd_tmp[id1,c('x','y')] 191 | as_tmp = alphahull::ashape(crd_tmp,alpha = 100) 192 | plot(as_tmp) 193 | poly6 = extractpoly(as_tmp) 194 | plotpoly(poly6) 195 | 196 | crd_tmp = crd %>% filter(x>17900) 197 | plotly::plot_ly(crd_tmp,x=~x,y=~y) 198 | cp = c('T1S1_kgfolccj-1') 199 | job({ap_tmp = areaPoint(dist(crd_tmp),cpoint = cp,r = 30)},import='auto') 200 | areap(crd_tmp,ap_tmp) 201 | id1 = ap_tmp$id[ap_tmp$type!='isolated'] 202 | crd_tmp = crd_tmp[id1,c('x','y')] 203 | as_tmp = alphahull::ashape(crd_tmp,alpha = 100) 204 | plot(as_tmp) 205 | poly7 = extractpoly(as_tmp) 206 | plotpoly(poly7) 207 | 208 | poly_11 = Reduce(append,list(poly1,poly2,poly3,poly4,poly5,poly6,poly7)) 209 | poly_11 = poly_11[-c(2,4)] 210 | plotpoly(poly_11) 211 | saveRDS(poly_11,'stp3_tme/data/epiregion/T1S1_poly.rds') 212 | } 213 | {# t1s2 214 | tmp = subset(xe1_2, cells = xe1_2$cellID[xe1_2$ct3=='Epi']) 215 | xedimplot(tmp,groupby = 'ct3',flip = T,reversex = T) 216 | crd = tmp@images$fov@boundaries$centroids@coords %>% data.frame 217 | rownames(crd) = colnames(tmp) 218 | colnames(crd) = c('y','x') 219 | 220 | getcoordsrange(tmp,tmp$cellID[tmp$stage1=='ESCC'],flip = T) 221 | crd_escc = crd %>% filter(x<=7800) 222 | plotly::plot_ly(crd_escc,x=~x,y=~y) 223 | cp = c('T1S2_fiaehfid-1','T1S2_bndohmei-1') 224 | job({ap_escc = areaPoint(dist(crd_escc),cpoint = cp,r = 50)},import='auto') 225 | areap(crd_escc,ap_escc) 226 | id1 = ap_escc$id[ap_escc$type!='isolated'] 227 | crd_escc = crd_escc[id1,c('x','y')] 228 | as_escc = alphahull::ashape(crd_escc,alpha = 100) 229 | plot(as_escc) 230 | polyescc = extractpoly(as_escc) 231 | plotpoly(polyescc) 232 | 233 | getcoordsrange(tmp,tmp$cellID[tmp$stage1=='HGIN'],flip = T) 234 | crd_hgin = crd %>% filter(x<=19000,x>7700) 235 | plotly::plot_ly(crd_hgin,x=~x,y=~y) 236 | cp = c('T1S2_kpfhfmmm-1','T1S2_eknacomk-1','T1S2_hcbfnfgk-1') 237 | job({ap_hgin = areaPoint(dist(crd_hgin),cpoint = cp,r = 50)},import='auto') 238 | areap(crd_hgin,ap_hgin) 239 | id1 = ap_hgin$id[ap_hgin$type!='isolated'] 240 | crd_hgin = crd_hgin[id1,c('x','y')] 241 | as_hgin = alphahull::ashape(crd_hgin,alpha = 80) 242 | plot(as_hgin) 243 | polyhgin = extractpoly(as_hgin) 244 | plotpoly(polyhgin) 245 | 246 | getcoordsrange(tmp,tmp$cellID[tmp$stage1%in%c('LGIN','NOR')],flip = T) 247 | crd_ln = crd %>% filter(x>18900) 248 | plotly::plot_ly(crd_ln,x=~x,y=~y) 249 | cp = c('T1S2_lccialec-1') 250 | job({ap_ln = areaPoint(dist(crd_ln),cpoint = cp,r = 50)},import='auto') 251 | areap(crd_ln,ap_ln) 252 | id1 = ap_ln$id[ap_ln$type!='isolated'] 253 | crd_ln = crd_ln[id1,c('x','y')] 254 | as_ln = alphahull::ashape(crd_ln,alpha = 100) 255 | plot(as_ln) 256 | polyln = extractpoly(as_ln) 257 | plotpoly(polyln) 258 | 259 | poly_12 = Reduce(append,list(polyescc,polyhgin,polyln)) 260 | poly_12 = poly_12[-2] 261 | plotpoly(poly_12) 262 | saveRDS(poly_12,'stp3_tme/data/epiregion/T1S2_poly.rds') 263 | } 264 | {# t1s3 265 | tmp = subset(xe1_3, cells = xe1_3$cellID[xe1_3$ct3=='Epi']) 266 | xedimplot(tmp,groupby = 'stage',flip = T,reversex = T) 267 | crd = tmp@images$fov@boundaries$centroids@coords %>% data.frame 268 | rownames(crd) = colnames(tmp) 269 | colnames(crd) = c('y','x') 270 | 271 | getcoordsrange(tmp,tmp$cellID[tmp$stage1=='ESCC'],flip = T) 272 | crd_escc = crd %>% filter(x<=8050) 273 | plotly::plot_ly(crd_escc,x=~x,y=~y) 274 | cp = c('T1S3_mejdelan-1','T1S3_hjepljbp-1','T1S3_chmccjfm-1','T1S3_cgmfdafb-1') 275 | job({ap_escc = areaPoint(dist(crd_escc),cpoint = cp,r = 50)},import='auto') 276 | areap(crd_escc,ap_escc) 277 | id1 = ap_escc$id[ap_escc$type!='isolated'] 278 | crd_escc = crd_escc[id1,c('x','y')] 279 | as_escc = alphahull::ashape(crd_escc,alpha = 100) 280 | plot(as_escc) 281 | polyescc = extractpoly(as_escc) 282 | plotpoly(polyescc) 283 | 284 | getcoordsrange(tmp,tmp$cellID[tmp$stage1=='HGIN'],flip = T) 285 | crd_hgin = crd %>% filter(x<=19327,x>7950) 286 | plotly::plot_ly(crd_hgin,x=~x,y=~y) 287 | cp = c('T1S3_kgfeokol-1','T1S3_bielbglm-1') 288 | job({ap_hgin = areaPoint(dist(crd_hgin),cpoint = cp,r = 50)},import='auto') 289 | areap(crd_hgin,ap_hgin) 290 | id1 = ap_hgin$id[ap_hgin$type!='isolated'] 291 | crd_hgin = crd_hgin[id1,c('x','y')] 292 | as_hgin = alphahull::ashape(crd_hgin,alpha = 100) 293 | plot(as_hgin) 294 | polyhgin = extractpoly(as_hgin) 295 | plotpoly(polyhgin) 296 | 297 | getcoordsrange(tmp,tmp$cellID[tmp$stage1%in%c('LGIN','NOR')],flip = T) 298 | crd_ln = crd %>% filter(x>19227) 299 | plotly::plot_ly(crd_ln,x=~x,y=~y) 300 | cp = c('T1S3_gkjognbf-1','T1S3_iekjbggf-1') 301 | job({ap_ln = areaPoint(dist(crd_ln),cpoint = cp,r = 50)},import='auto') 302 | areap(crd_ln,ap_ln) 303 | id1 = ap_ln$id[ap_ln$type!='isolated'] 304 | crd_ln = crd_ln[id1,c('x','y')] 305 | as_ln = alphahull::ashape(crd_ln,alpha = 80) 306 | plot(as_ln) 307 | polyln = extractpoly(as_ln) 308 | plotpoly(polyln) 309 | 310 | poly_13 = Reduce(append,list(polyescc,polyhgin,polyln)) 311 | poly_13 = poly_13[-c(2:3)] 312 | plotpoly(poly_13) 313 | saveRDS(poly_13,'stp3_tme/data/epiregion/T1S3_poly.rds') 314 | } 315 | {# t2s1 316 | tmp = subset(xe2_1, cells = xe2_1$cellID[xe2_1$ct3=='Epi']) 317 | xedimplot(tmp,groupby = 'stage',flip = T) 318 | crd = tmp@images$fov@boundaries$centroids@coords %>% data.frame 319 | rownames(crd) = colnames(tmp) 320 | colnames(crd) = c('y','x') 321 | 322 | plotly::plot_ly(crd,x=~x,y=~y) 323 | cp = c('T2S1_jkfchill-1') 324 | job({ap = areaPoint(dist(crd),cpoint = cp,r = 50)},import='auto') 325 | areap(crd,ap) 326 | id1 = ap$id[ap$type!='isolated'] 327 | crd = crd[id1,c('x','y')] 328 | as = alphahull::ashape(crd,alpha = 100) 329 | plot(as) 330 | polytmp = extractpoly(as) 331 | plotpoly(polytmp) 332 | 333 | poly_21 = polytmp 334 | plotpoly(poly_21) 335 | saveRDS(poly_21,'stp3_tme/data/epiregion/T2S1_poly.rds') 336 | } 337 | {# t2s2 338 | tmp = subset(xe2_2, cells = xe2_2$cellID[xe2_2$ct3=='Epi']) 339 | xedimplot(tmp,groupby = 'stage',flip = T) 340 | crd = tmp@images$fov@boundaries$centroids@coords %>% data.frame 341 | rownames(crd) = colnames(tmp) 342 | colnames(crd) = c('y','x') 343 | 344 | plotly::plot_ly(crd,x=~x,y=~y) 345 | cp = c('T2S2_hcjginok-1') 346 | job({ap = areaPoint(dist(crd),cpoint = cp,r = 50)},import='auto') 347 | areap(crd,ap) 348 | id1 = ap$id[ap$type!='isolated'] 349 | crd = crd[id1,c('x','y')] 350 | as = alphahull::ashape(crd,alpha = 100) 351 | plot(as) 352 | polytmp = extractpoly(as) 353 | plotpoly(polytmp) 354 | 355 | poly_22 = polytmp 356 | plotpoly(poly_22) 357 | saveRDS(poly_22,'stp3_tme/data/epiregion/T2S2_poly.rds') 358 | } 359 | {# t2s3 360 | tmp = subset(xe2_3, cells = xe2_3$cellID[xe2_3$ct3=='Epi']) 361 | xedimplot(tmp,groupby = 'stage',flip = T) 362 | crd = tmp@images$fov@boundaries$centroids@coords %>% data.frame 363 | rownames(crd) = colnames(tmp) 364 | colnames(crd) = c('y','x') 365 | 366 | plotly::plot_ly(crd,x=~x,y=~y) 367 | cp = c('T2S3_ochlpbdm-1') 368 | job({ap = areaPoint(dist(crd),cpoint = cp,r = 50)},import='auto') 369 | areap(crd,ap) 370 | id1 = ap$id[ap$type!='isolated'] 371 | crd = crd[id1,c('x','y')] 372 | as = alphahull::ashape(crd,alpha = 100) 373 | plot(as) 374 | polytmp = extractpoly(as) 375 | plotpoly(polytmp) 376 | 377 | poly_23 = polytmp 378 | plotpoly(poly_23) 379 | saveRDS(poly_23,'stp3_tme/data/epiregion/T2S3_poly.rds') 380 | } 381 | {# t3s1 382 | tmp = subset(xe3_1, cells = xe3_1$cellID[xe3_1$ct3=='Epi']) 383 | xedimplot(tmp,groupby = 'stage',flip = T,reversex = T) 384 | crd = tmp@images$fov@boundaries$centroids@coords %>% data.frame 385 | rownames(crd) = colnames(tmp) 386 | colnames(crd) = c('y','x') 387 | 388 | crd_escc = crd[tmp$cellID[tmp$stage1=='ESCC'],] 389 | plotly::plot_ly(crd_escc,x=~x,y=~y) 390 | crd_escc = crd_escc[,c('x','y')] 391 | as_escc = alphahull::ashape(crd_escc,alpha = 200) 392 | plot(as_escc) 393 | polyescc = extractpoly(as_escc) 394 | plotpoly(polyescc) 395 | 396 | crd_hln = crd[tmp$cellID[tmp$stage1!='ESCC'],] 397 | plotly::plot_ly(crd_hln,x=~x,y=~y) 398 | cp = c('T3S1_clmiefdi-1') 399 | job({ap_hln = areaPoint(dist(crd_hln),cpoint = cp,r = 50)},import='auto') 400 | areap(crd_hln,ap_hln) 401 | id1 = ap_hln$id[ap_hln$type!='isolated'] 402 | crd_hln = crd_hln[id1,c('x','y')] 403 | as_hln = alphahull::ashape(crd_hln,alpha = 100) 404 | plot(as_hln) 405 | polyhln = extractpoly(as_hln) 406 | plotpoly(polyhln) 407 | polyhln = polyhln[-2] 408 | 409 | poly_31 = Reduce(append,list(polyescc,polyhln)) 410 | plotpoly(poly_31) 411 | saveRDS(poly_31,'stp3_tme/data/epiregion/T3S1_poly.rds') 412 | } 413 | {# t3s2 414 | tmp = subset(xe3_2, cells = xe3_2$cellID[xe3_2$ct3=='Epi']) 415 | xedimplot(tmp,groupby = 'stage',flip = T,reversex = T) 416 | crd = tmp@images$fov@boundaries$centroids@coords %>% data.frame 417 | rownames(crd) = colnames(tmp) 418 | colnames(crd) = c('y','x') 419 | 420 | crd_escc = crd[tmp$cellID[tmp$stage1=='ESCC'],] 421 | plotly::plot_ly(crd_escc,x=~x,y=~y) 422 | crd_escc = crd_escc[,c('x','y')] 423 | as_escc = alphahull::ashape(crd_escc,alpha = 200) 424 | plot(as_escc) 425 | polyescc = extractpoly(as_escc) 426 | plotpoly(polyescc) 427 | 428 | crd_hln = crd[tmp$cellID[tmp$stage1!='ESCC'],] 429 | plotly::plot_ly(crd_hln,x=~x,y=~y) 430 | cp = c('T3S2_dkbnfmna-1') 431 | job({ap_hln = areaPoint(dist(crd_hln),cpoint = cp,r = 50)},import='auto') 432 | areap(crd_hln,ap_hln) 433 | id1 = ap_hln$id[ap_hln$type!='isolated'] 434 | crd_hln = crd_hln[id1,c('x','y')] 435 | as_hln = alphahull::ashape(crd_hln,alpha = 150) 436 | plot(as_hln) 437 | polyhln = extractpoly(as_hln) 438 | plotpoly(polyhln) 439 | 440 | poly_32 = Reduce(append,list(polyescc,polyhln)) 441 | plotpoly(poly_32) 442 | saveRDS(poly_32,'stp3_tme/data/epiregion/T3S2_poly.rds') 443 | } 444 | {# t4s1 445 | tmp = subset(xe4_1, cells = xe4_1$cellID[xe4_1$ct3=='Epi']) 446 | xedimplot(tmp,groupby = 'stage',flip = T,reversex = T) 447 | crd = tmp@images$fov@boundaries$centroids@coords %>% data.frame 448 | rownames(crd) = colnames(tmp) 449 | colnames(crd) = c('y','x') 450 | 451 | crd_escc1 = crd[tmp$cellID[tmp$stage=='ESCC-1'],] 452 | plotly::plot_ly(crd_escc1,x=~x,y=~y) 453 | cp = c('T4S1_fkmgnkeb-1','T4S1_didhijpk-1','T4S1_nhfnhcfm-1') 454 | job({ap_escc1 = areaPoint(dist(crd_escc1),cpoint = cp,r = 50)},import='auto') 455 | areap(crd_escc1,ap_escc1) 456 | id1 = ap_escc1$id[ap_escc1$type!='isolated'] 457 | crd_escc1 = crd_escc1[id1,c('x','y')] 458 | as_escc1 = alphahull::ashape(crd_escc1,alpha = 100) 459 | plot(as_escc1) 460 | polyescc1 = extractpoly(as_escc1) 461 | plotpoly(polyescc1) 462 | polyescc1 = polyescc1[-2] 463 | 464 | crd_escc2 = crd[tmp$cellID[tmp$stage=='ESCC-2'],] 465 | plotly::plot_ly(crd_escc2,x=~x,y=~y) 466 | cp = c('T4S1_jajcljcm-1','T4S1_hbhjbjjk-1') 467 | job({ap_escc2 = areaPoint(dist(crd_escc2),cpoint = cp,r = 50)},import='auto') 468 | areap(crd_escc2,ap_escc2) 469 | id1 = ap_escc2$id[ap_escc2$type!='isolated'] 470 | crd_escc2 = crd_escc2[id1,c('x','y')] 471 | as_escc2 = alphahull::ashape(crd_escc2,alpha = 100) 472 | plot(as_escc2) 473 | polyescc2 = extractpoly(as_escc2) 474 | plotpoly(polyescc2) 475 | polyescc2 = polyescc2[-2] 476 | 477 | crd_escc3 = crd[tmp$cellID[tmp$stage=='ESCC-3'],] 478 | crd_escc3 = crd_escc3[,c('x','y')] 479 | as_escc3 = alphahull::ashape(crd_escc3,alpha = 300) 480 | plot(as_escc3) 481 | polyescc3 = extractpoly(as_escc3) 482 | plotpoly(polyescc3) 483 | 484 | crd_hln = crd[tmp$cellID[tmp$stage1!='ESCC'],] 485 | plotly::plot_ly(crd_hln,x=~x,y=~y) 486 | cp = c('T4S1_gpkmnace-1') 487 | job({ap_hln = areaPoint(dist(crd_hln),cpoint = cp,r = 50)},import='auto') 488 | areap(crd_hln,ap_hln) 489 | id1 = ap_hln$id[ap_hln$type!='isolated'] 490 | crd_hln = crd_hln[id1,c('x','y')] 491 | as_hln = alphahull::ashape(crd_hln,alpha = 100) 492 | plot(as_hln) 493 | polyhln = extractpoly(as_hln) 494 | plotpoly(polyhln) 495 | 496 | poly_41 = Reduce(append,list(polyescc1,polyescc2,polyescc3,polyhln)) 497 | plotpoly(poly_41) 498 | saveRDS(poly_41,'stp3_tme/data/epiregion/T4S1_poly.rds') 499 | } 500 | {# t4s2 501 | tmp = subset(xe4_2, cells = xe4_2$cellID[xe4_2$ct3=='Epi']) 502 | xedimplot(tmp,groupby = 'stage',flip = T,reversey = T) 503 | crd = tmp@images$fov@boundaries$centroids@coords %>% data.frame 504 | rownames(crd) = colnames(tmp) 505 | colnames(crd) = c('y','x') 506 | 507 | crd_escc1 = crd[tmp$cellID[tmp$stage=='ESCC-1'],] 508 | (crd_escc1 %>% mutate(grp=rownames(.)) %>% ggplot(aes(x,y,group=grp))+geom_point(size=0.5)) %>% plotly::ggplotly() 509 | cp = c('T4S2_iaoeikje-1','T4S2_lmamdlih-1','T4S2_cdoanplb-1','T4S2_hcoldbkn-1') 510 | job({ap_escc1 = areaPoint(dist(crd_escc1),cpoint = cp,r = 50)},import='auto') 511 | areap(crd_escc1,ap_escc1) 512 | id1 = ap_escc1$id[ap_escc1$type!='isolated'] 513 | crd_escc1 = crd_escc1[id1,c('x','y')] 514 | as_escc1 = alphahull::ashape(crd_escc1,alpha = 100) 515 | plot(as_escc1) 516 | polyescc1 = extractpoly(as_escc1) 517 | plotpoly(polyescc1) 518 | polyescc1 = polyescc1[-c(4,5,6)] 519 | 520 | crd_escc2 = crd[tmp$cellID[tmp$stage=='ESCC-2'],] 521 | crd_escc2 = crd_escc2[,c('x','y')] 522 | as_escc2 = alphahull::ashape(crd_escc2,alpha = 300) 523 | plot(as_escc2) 524 | polyescc2 = extractpoly(as_escc2) 525 | plotpoly(polyescc2) 526 | 527 | crd_escc3 = crd[tmp$cellID[tmp$stage=='ESCC-3'],] 528 | crd_escc3 = crd_escc3[,c('x','y')] 529 | as_escc3 = alphahull::ashape(crd_escc3,alpha = 300) 530 | plot(as_escc3) 531 | polyescc3 = extractpoly(as_escc3) 532 | plotpoly(polyescc3) 533 | 534 | crd_hln = crd[tmp$cellID[tmp$stage1!='ESCC'],] 535 | plotly::plot_ly(crd_hln,x=~x,y=~y) 536 | cp = c('T4S2_bcapfhbj-1') 537 | job({ap_hln = areaPoint(dist(crd_hln),cpoint = cp,r = 50)},import='auto') 538 | areap(crd_hln,ap_hln) 539 | id1 = ap_hln$id[ap_hln$type!='isolated'] 540 | crd_hln = crd_hln[id1,c('x','y')] 541 | as_hln = alphahull::ashape(crd_hln,alpha = 100) 542 | plot(as_hln) 543 | polyhln = extractpoly(as_hln) 544 | plotpoly(polyhln) 545 | 546 | poly_42 = Reduce(append,list(polyescc1,polyescc2,polyescc3,polyhln)) 547 | plotpoly(poly_42) 548 | saveRDS(poly_42,'stp3_tme/data/epiregion/T4S2_poly.rds') 549 | } 550 | {# t5s1 551 | tmp = subset(xe5_1, cells = xe5_1$cellID[xe5_1$ct3=='Epi']) 552 | xedimplot(tmp,groupby = 'stage') 553 | crd = tmp@images$fov@boundaries$centroids@coords %>% data.frame 554 | rownames(crd) = colnames(tmp) 555 | 556 | plotly::plot_ly(crd,x=~x,y=~y) 557 | cp = c('T5S1_aecemjfk-1') 558 | job({ap = areaPoint(dist(crd),cpoint = cp,r = 50)},import='auto') 559 | areap(crd,ap) 560 | id1 = ap$id[ap$type!='isolated'] 561 | crd = crd[id1,c('x','y')] 562 | as = alphahull::ashape(crd,alpha = 100) 563 | plot(as) 564 | polytmp = extractpoly(as) 565 | plotpoly(polytmp) 566 | 567 | poly_51 = polytmp 568 | plotpoly(poly_51) 569 | saveRDS(poly_51,'stp3_tme/data/epiregion/T5S1_poly.rds') 570 | } 571 | {# t5s2 572 | tmp = subset(xe5_2_split, cells = xe5_2_split$cellID[xe5_2_split$ct3=='Epi']) 573 | xedimplot(tmp,groupby = 'stage') 574 | crd = tmp@images$fov@boundaries$centroids@coords %>% data.frame 575 | rownames(crd) = colnames(tmp) 576 | 577 | plotly::plot_ly(crd,x=~x,y=~y) 578 | cp = c('T5S2_bgpiikon-1') 579 | job({ap = areaPoint(dist(crd),cpoint = cp,r = 50)},import='auto') 580 | areap(crd,ap) 581 | id1 = ap$id[ap$type!='isolated'] 582 | crd = crd[id1,c('x','y')] 583 | as = alphahull::ashape(crd,alpha = 100) 584 | plot(as) 585 | polytmp = extractpoly(as) 586 | plotpoly(polytmp) 587 | 588 | poly_52 = polytmp 589 | plotpoly(poly_52) 590 | saveRDS(poly_52,'stp3_tme/data/epiregion/T5S2_poly.rds') 591 | } 592 | {# t5s3 593 | tmp = subset(xe5_3_split, cells = xe5_3_split$cellID[xe5_3_split$ct3=='Epi']) 594 | xedimplot(tmp,groupby = 'stage') 595 | crd = tmp@images$fov@boundaries$centroids@coords %>% data.frame 596 | rownames(crd) = colnames(tmp) 597 | 598 | plotly::plot_ly(crd,x=~x,y=~y) 599 | cp = c('T5S3_belmiekd-1') 600 | job({ap = areaPoint(dist(crd),cpoint = cp,r = 50)},import='auto') 601 | areap(crd,ap) 602 | id1 = ap$id[ap$type!='isolated'] 603 | crd = crd[id1,c('x','y')] 604 | as = alphahull::ashape(crd,alpha = 100) 605 | plot(as) 606 | polytmp = extractpoly(as) 607 | plotpoly(polytmp) 608 | 609 | poly_53 = polytmp 610 | plotpoly(poly_53) 611 | saveRDS(poly_53,'stp3_tme/data/epiregion/T5S3_poly.rds') 612 | } 613 | {# t6s1 614 | tmp = subset(xe6_1, cells = xe6_1$cellID[xe6_1$ct3=='Epi']) 615 | xedimplot(tmp,groupby = 'stage',reversey = T) 616 | crd = tmp@images$fov@boundaries$centroids@coords %>% data.frame 617 | rownames(crd) = colnames(tmp) 618 | 619 | crd_escc = crd[tmp$cellID[tmp$stage1=='ESCC'],] 620 | plotly::plot_ly(crd_escc,x=~x,y=~y) 621 | cp = c('T6S1_hdolbmdo-1') 622 | job({ap_escc = areaPoint(dist(crd_escc),cpoint = cp,r = 50)},import='auto') 623 | areap(crd_escc,ap_escc) 624 | id1 = ap_escc$id[ap_escc$type!='isolated'] 625 | crd_escc = crd_escc[id1,c('x','y')] 626 | as_escc = alphahull::ashape(crd_escc,alpha = 100) 627 | plot(as_escc) 628 | polyescc = extractpoly(as_escc) 629 | plotpoly(polyescc) 630 | 631 | crd_hn = crd[tmp$cellID[tmp$stage1!='ESCC'],] 632 | plotly::plot_ly(crd_hn,x=~x,y=~y) 633 | cp = c('T6S1_npbjhlcc-1') 634 | job({ap_hn = areaPoint(dist(crd_hn),cpoint = cp,r = 50)},import='auto') 635 | areap(crd_hn,ap_hn) 636 | id1 = ap_hn$id[ap_hn$type!='isolated'] 637 | crd_hn = crd_hn[id1,c('x','y')] 638 | as_hn = alphahull::ashape(crd_hn,alpha = 100) 639 | plot(as_hn) 640 | polyhn = extractpoly(as_hn) 641 | plotpoly(polyhn) 642 | 643 | poly_61 = Reduce(append,list(polyescc,polyhn)) 644 | plotpoly(poly_61) 645 | saveRDS(poly_61,'stp3_tme/data/epiregion/T6S1_poly.rds') 646 | } 647 | {# t6s2 648 | tmp = subset(xe6_2, cells = xe6_2$cellID[xe6_2$ct3=='Epi']) 649 | xedimplot(tmp,groupby = 'stage') 650 | crd = tmp@images$fov@boundaries$centroids@coords %>% data.frame 651 | rownames(crd) = colnames(tmp) 652 | 653 | crd_escc = crd[tmp$cellID[tmp$stage%in%c('ESCC-1','ESCC-2')],] 654 | plotly::plot_ly(crd_escc,x=~x,y=~y) 655 | cp = c('T6S2_ckoclijc-1') 656 | job({ap_escc = areaPoint(dist(crd_escc),cpoint = cp,r = 50)},import='auto') 657 | areap(crd_escc,ap_escc) 658 | id1 = ap_escc$id[ap_escc$type!='isolated'] 659 | crd_escc = crd_escc[id1,c('x','y')] 660 | as_escc = alphahull::ashape(crd_escc,alpha = 100) 661 | plot(as_escc) 662 | polyescc = extractpoly(as_escc) 663 | plotpoly(polyescc) 664 | polyescc = polyescc[-2] 665 | 666 | crd_hn = crd[tmp$cellID[!tmp$stage%in%c('ESCC-1','ESCC-2')],] 667 | plotly::plot_ly(crd_hn,x=~x,y=~y) 668 | cp = c('T6S2_ednopeek-1') 669 | job({ap_hn = areaPoint(dist(crd_hn),cpoint = cp,r = 50)},import='auto') 670 | areap(crd_hn,ap_hn) 671 | id1 = ap_hn$id[ap_hn$type!='isolated'] 672 | crd_hn = crd_hn[id1,c('x','y')] 673 | as_hn = alphahull::ashape(crd_hn,alpha = 100) 674 | plot(as_hn) 675 | polyhn = extractpoly(as_hn) 676 | plotpoly(polyhn) 677 | 678 | poly_62 = Reduce(append,list(polyescc,polyhn)) 679 | plotpoly(poly_62) 680 | saveRDS(poly_62,'stp3_tme/data/epiregion/T6S2_poly.rds') 681 | } 682 | {# t6s3 683 | tmp = subset(xe6_3, cells = xe6_3$cellID[xe6_3$ct3=='Epi']) 684 | xedimplot(tmp,groupby = 'stage') 685 | crd = tmp@images$fov@boundaries$centroids@coords %>% data.frame 686 | rownames(crd) = colnames(tmp) 687 | 688 | crd_escc = crd[tmp$cellID[tmp$stage%in%c('ESCC-1','ESCC-2')],] 689 | plotly::plot_ly(crd_escc,x=~x,y=~y) 690 | cp = c('T6S3_klmannch-1','T6S3_ckgnepco-1') 691 | job({ap_escc = areaPoint(dist(crd_escc),cpoint = cp,r = 50)},import='auto') 692 | areap(crd_escc,ap_escc) 693 | id1 = ap_escc$id[ap_escc$type!='isolated'] 694 | crd_escc = crd_escc[id1,c('x','y')] 695 | as_escc = alphahull::ashape(crd_escc,alpha = 60) 696 | plot(as_escc) 697 | polyescc = extractpoly(as_escc) 698 | plotpoly(polyescc) 699 | polyescc = polyescc[-3] 700 | 701 | crd_hn = crd[tmp$cellID[!tmp$stage%in%c('ESCC-1','ESCC-2')],] 702 | plotly::plot_ly(crd_hn,x=~x,y=~y) 703 | cp = c('T6S3_licblgcj-1') 704 | job({ap_hn = areaPoint(dist(crd_hn),cpoint = cp,r = 50)},import='auto') 705 | areap(crd_hn,ap_hn) 706 | id1 = ap_hn$id[ap_hn$type!='isolated'] 707 | crd_hn = crd_hn[id1,c('x','y')] 708 | as_hn = alphahull::ashape(crd_hn,alpha = 100) 709 | plot(as_hn) 710 | polyhn = extractpoly(as_hn) 711 | plotpoly(polyhn) 712 | 713 | poly_63 = Reduce(append,list(polyescc,polyhn)) 714 | plotpoly(poly_63) 715 | saveRDS(poly_63,'stp3_tme/data/epiregion/T6S3_poly.rds') 716 | } 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | -------------------------------------------------------------------------------- /stp2_epiSpatial.R: -------------------------------------------------------------------------------- 1 | source('~/xenium/header.R') 2 | source('~/xenium/xenium_function.R') 3 | library(ggstar) 4 | 5 | ##### input ##### 6 | srtepi = read_rds('data/xenium_seurat_processed/ct_epi_srt_2.rds') 7 | 8 | # lamexpr = fread('stp2_epi/data/lamfish_epi12_jagnotchexpr.txt') 9 | lam = read_rds('stp2_epi/data/lam_epi12_seurat.rds') 10 | lam@meta.data = read_rds('stp2_epi/data/lam_epi12_meta.rds') 11 | 12 | epidt = read_rds('stp2_epi/data/epiid_correctstage.rds') 13 | 14 | epianno = fread('stp1_summary/data/xenium_epi.csv') 15 | 16 | epigeneannot = fread('stp3_tme/data/hm_gene_anno.txt') 17 | 18 | probelist = fread('data/probe_list.txt') 19 | 20 | gridid = read_rds('stp1_summary/data/allsample_gridid.rds') 21 | 22 | epiblacklist = fread('stp2_epi/data/gene_otherTissue.txt') 23 | epiblacklist = probelist[probelist$gene%in%epiblacklist$Gene_otherTissue,] 24 | 25 | 26 | allmk = c('EPCAM','SFN','KRT4','KRT5', # epi 27 | 'FN1','DCN','COL1A1','COL1A2','COL3A1','COL6A1', # fibro 28 | 'PLN','MYH11', # muscle 29 | 'VWF','PECAM1','ENG','CDH5', # endo 30 | 'PTPRC', # immune 31 | 'CD2','CD3D','CD3E', # t cell 32 | 'CD1E','CD68','LYZ','CD14','FCGR3A', # myeloid 33 | 'MS4A2','KIT','CPA3', # mast 34 | 'CD19','CD79A','MS4A1', # B 35 | 'JCHAIN','IGHG2' # plasma 36 | ) 37 | 38 | epimarker = c('KRT15','MYC', # QP 39 | 'TOP2A','MKI67', # CY 40 | 'ANXA1','KRT13','KRT4','CSTA','S100A8','TRIM29', # MD 41 | 'SPRR3','SPRR2E','ECM1','EMP1', # TD 42 | 'HIF1A','SERPINE1','VEGFA', # HY 43 | 'KRT17', # RO 44 | 'SPP1','CES1', 'AKR1C3','AKR1C2','CBR1', # DO 45 | 'HLA-DRB1','HLA-DQB1'# AP 46 | ) 47 | 48 | 49 | 50 | epicol = c('#0339f8','#ff9408','#75bbfd','#de0c62') 51 | names(epicol) = c('Basal','Proliferation','Differentiation','Invasive') 52 | 53 | 54 | 55 | 56 | ##### celltype spatial ##### 57 | {# balck bg 58 | objlist = setNames(list('xe1_1','xe1_2','xe1_3','xe2_1','xe2_2','xe2_3','xe3_1','xe3_2', 59 | 'xe4_1','xe4_2','xe5_1','xe5_2_split','xe5_3_split','xe6_1','xe6_2','xe6_3'), 60 | c('T1S1','T1S2','T1S3','T2S1','T2S2','T2S3','T3S1','T3S2', 61 | 'T4S1','T4S2','T5S1','T5S2','T5S3','T6S1','T6S2','T6S3')) 62 | lapply(names(objlist), function(x){ 63 | dt = get(objlist[[x]]) 64 | dt = subset(dt,cells=intersect(dt$cellID,epidt$cellID)) 65 | dt@meta.data = dt@meta.data %>% left_join(epidt[,c('cellID','celltype')]) %>% `rownames<-`(.$cellID) 66 | 67 | flip=F;reversex=F;reversey=F 68 | g1 = c('T1S1','T1S2','T1S3','T3S1','T3S2','T4S1') 69 | g2 = c('T2S1','T2S2','T2S3') 70 | g3 = c('T4S2') 71 | g4 = c('T5S1','T5S2','T5S3') 72 | g5 = c('T6S1') 73 | g6 = c('T6S2','T6S3') 74 | if(x%in%g1){flip=T;reversex=T} 75 | if(x%in%g2){flip=T} 76 | if(x%in%g3){flip=T;reversey=T} 77 | if(x%in%g5){reversey=T} 78 | if(x%in%g6){reversex=T} 79 | 80 | figsize = getcoordsrange(dt,dt$cellID,flip) 81 | wd = (figsize[2]-figsize[1])/500 82 | ht = (figsize[4]-figsize[3])/300 83 | fname = paste0('stp2_epi/plot_epitype/',x,'_celltype.png') 84 | p = xedimplot(dt, groupby = 'celltype',flip = flip,reversex = reversex,reversey = reversey,color = epicol,bgcol = 'black',pt.size = 0.1,gridline = T,gridcol = 'white')+NoLegend() 85 | ggsave(fname,p,width = 40, height = 15, limitsize = F) 86 | }) 87 | } 88 | 89 | 90 | 91 | 92 | 93 | 94 | ##### gene expr spatial ##### 95 | {# molecular index 96 | subsetmolecular = function(dt, genes, cells=NULL) 97 | { 98 | if(is.null(cells)){ 99 | cells=colnames(dt) 100 | } 101 | cells=intersect(colnames(dt),cells) 102 | 103 | cellpoly = dt@images$fov@boundaries$segmentation@polygons 104 | cellpoly = lapply(cellpoly[cells],function(x){ 105 | pl = x@Polygons[[1]]@coords %>% data.frame() 106 | dup = paste0(pl[,1],'-',pl[,2]) 107 | pl = pl[!duplicated(dup),] 108 | return(pl) 109 | }) 110 | cellpolyall = cellpoly %>% rbindlist() %>% data.frame() 111 | molrange = c(min(cellpolyall$x),max(cellpolyall$x),min(cellpolyall$y),max(cellpolyall$y)) 112 | 113 | mols = dt@images$fov@molecules$molecules[genes] 114 | mols = lapply(mols,function(x){ 115 | crd = x@coords %>% data.frame() 116 | crd = crd %>% filter(x>=molrange[1],x<=molrange[2],y>=molrange[3],y<=molrange[4]) 117 | inrange1 = lapply(cellpoly,function(y){ 118 | rst = isinner2(crd,y) 119 | }) %>% Reduce('|',.) 120 | rst = crd[inrange1,] 121 | }) 122 | 123 | return(mols) 124 | } 125 | 126 | dtlist = list(nl=list(dt=epnl,roi=roinl),lh=list(dt=eplh,roi=roilh),ht=list(dt=epht,roi=roiht)) 127 | job({ 128 | isinner2 129 | mols = lapply(dtlist,function(x){ 130 | rst = subsetmolecular(x$dt,cycgene,getcellinrange(x$dt,x$roi,T)) 131 | return(rst) 132 | }) 133 | saveRDS(mols,'stp2_epi/data/3roi_13genes_epimolecularcoords_v2.rds') 134 | },import='auto') 135 | 136 | mols = read_rds('stp2_epi/data/3roi_13genes_epimolecularcoords_v2.rds') 137 | epi_cycmol = lapply(cycgene,function(x){ 138 | dt = lapply(names(mols),function(y){ 139 | dt1 = mols[[y]][[x]] 140 | dt1$gene=x 141 | dt1$stage=y 142 | return(dt1) 143 | }) %>% Reduce(rbind,.) 144 | return(dt) 145 | }) %>% Reduce(rbind,.) 146 | epi_cycmol = epi_cycmol %>% dplyr::rename(c('x'='y','y'='x')) 147 | epi_cycmol$gene = epi_cycmol$gene %>% factor(levels = rev(c('JAG1','NOTCH1','EGFR','ERBB2','MDM2','MYC','STMN1','AURKA','CCND1','SPP1','VEGFA','SOX2','TP63'))) 148 | epi_cycmol$stage = epi_cycmol$stage %>% factor(levels = c('nl','lh','ht')) 149 | p=epi_cycmol %>% 150 | arrange(gene) %>% 151 | # filter(gene%in%c('JAG1','NOTCH1','EGFR','ERBB2','MDM2','MYC','STMN1','AURKA','CCND1')) %>% 152 | # filter(gene%in%c('SPP1','VEGFA','SOX2','TP63')) %>% 153 | filter(x>=11900) %>% 154 | ggplot(aes(x,y))+ 155 | geom_point(aes(color=gene),size=0.1,alpha=0.5)+ 156 | theme1+scale_x_reverse()+ 157 | # facet_wrap(~stage,scales = 'free')+ 158 | theme(panel.background = element_blank(),plot.background = element_blank())+ 159 | scale_color_manual(values = exprcol_gene)+guides(color=guide_legend(override.aes = list(size=3,alpha=1))) 160 | ggsave('stp2_epi/plot/epiexpr_spatial2/cycexpr_spatial_legend.pdf',p,width = 5,height = 5,limitsize = F) 161 | ggsave('stp2_epi/plot/tmp_cycscore/cycexpr.png',p,width = 40,height = 10,limitsize = F) 162 | 163 | for(stg in unique(epi_cycmol$stage)){ 164 | p=epi_cycmol %>% 165 | filter(stage==stg) %>% 166 | arrange(gene) %>% 167 | ggplot(aes(x,y))+ 168 | geom_point(aes(color=gene),size=0.1,alpha=1)+ 169 | theme1+scale_x_reverse()+coord_fixed()+ 170 | theme(panel.background = element_blank(),plot.background = element_blank())+ 171 | scale_color_manual(values = exprcol_gene)+guides(color=guide_legend(override.aes = list(size=3,alpha=1))) 172 | ggsave(paste0('stp2_epi/plot/epiexpr_spatial2/cycexpr_spatial_',stg,'.png'),p,width = 14,height = 10,limitsize = F) 173 | } 174 | 175 | {# pergene 176 | rg = list(c(10600,11100,2600,3000), 177 | c(10700,11300,2600,3000), 178 | c(10700,11300,2600,3000)) 179 | for(sp in 1:3) 180 | { 181 | dtrange = rg[[sp]] 182 | stg = unique(epi_cycmol$stage)[[sp]] 183 | dt = epi_cycmol %>% filter(stage==stg) 184 | # dt = dt %>% filter(x>=dtrange[1],x<=dtrange[2],y>=dtrange[3],y<=dtrange[4]) 185 | p = lapply(as.character(unique(dt$gene)),function(g){ 186 | dt1 = dt 187 | dt1$gene = dt1$gene %>% as.character() 188 | dt1$gene = ifelse(dt1$gene==g,g,'others') %>% factor(levels = c('others',g)) 189 | dt1 %>% 190 | arrange(gene) %>% 191 | ggplot(aes(x,y))+ 192 | geom_point(aes(color=gene),size=0.1,alpha=1)+ 193 | theme1+scale_x_reverse()+NoLegend()+ 194 | scale_color_manual(values = c(exprcol_gene,'others'='#cccccc'))+ 195 | guides(color=guide_legend(override.aes = list(size=3,alpha=1))) 196 | }) %>% cowplot::plot_grid(plotlist = .,nrow = 4,ncol = 4) 197 | 198 | ggsave(paste0('stp2_epi/plot/tmp_cycscore/',stg,'_cycexpr.png'),p,width = 50,height = 40,limitsize = F) 199 | } 200 | } 201 | } 202 | 203 | 204 | 205 | 206 | 207 | ##### gene expr, score box ##### 208 | epicol = c('#0339f8','#ff9408','#75bbfd','#de0c62') 209 | names(epicol) = c('Basal','Proliferation','Differentiation','Invasive') 210 | 211 | epidt = read_rds('stp2_epi/data/epiid_correctstage.rds') 212 | cycscore = getscore(srtepi,list(cycling=gns)) %>% filter(cellID%in%epidt$cellID) 213 | cycscore$celltype = cycscore$celltype %>% factor(c('Proliferation','Invasive','Basal','Differentiation')) 214 | 215 | cycscore1 = aggregate(list(cycling=cycscore$cycling),by=as.list(cycscore[,c('sp_stg','stage1')]),mean) 216 | cycscore1 %>% 217 | ggplot(aes(stage1, cycling))+ 218 | theme1+NoLegend()+ 219 | # theme(panel.border = element_rect(fill=NA,color='black',linewidth=0.5), 220 | # axis.line = element_blank())+ 221 | xlab('')+ylab('Proliferation score')+ 222 | scale_fill_manual(values = col_stage)+ 223 | stat_boxplot(geom = 'errorbar',size=0.5,width=0.5)+ 224 | geom_boxplot(aes(fill=stage1),outlier.shape = 21,outlier.size = 3,outlier.stroke = 0.1)+ 225 | stat_compare_means(comparisons = makecomparison(4)) 226 | ggsave('stp2_epi/plot/prolifeinv_score_stage_box.pdf',width = 4,height = 4) 227 | pv1 = wilcox.test(cycscore$cycling[cycscore$stage1=='NOR'],cycscore$cycling[cycscore$stage1=='LGIN'])$p.value 228 | pv2 = wilcox.test(cycscore$cycling[cycscore$stage1=='NOR'],cycscore$cycling[cycscore$stage1=='HGIN'])$p.value 229 | pv3 = wilcox.test(cycscore$cycling[cycscore$stage1=='NOR'],cycscore$cycling[cycscore$stage1=='ESCC'])$p.value 230 | 231 | aggregate(list(cycling=cycscore1$cycling),by=as.list(cycscore1[,c('stage1'),drop=F]),median) 232 | 233 | 234 | cycscore2 = aggregate(list(cycling=cycscore$cycling),by=as.list(cycscore[,c('sp_stg','celltype')]),mean) 235 | cycscore2$celltype = cycscore2$celltype %>% factor(c('Basal','Proliferation','Differentiation','Invasive')) 236 | cycscore %>% 237 | ggplot(aes(celltype, cycling))+ 238 | theme1+NoLegend()+ 239 | theme(panel.border = element_rect(fill=NA,color='black',size=0.5), 240 | axis.line = element_blank())+ 241 | scale_fill_manual(values = epicol)+ 242 | stat_compare_means(comparisons = makecomparison(4))+ 243 | stat_boxplot(geom = 'errorbar',size=0.5,width=0.5)+ 244 | geom_boxplot(aes(fill=celltype),outlier.shape = 21,outlier.size = 3,outlier.stroke = 0.1) 245 | ggsave('stp2_epi/plot/prolifeinv_score_celltype_box.pdf',width = 4,height = 4) 246 | pv1 = wilcox.test(cycscore$cycling[cycscore$celltype=='Proliferation'],cycscore$cycling[cycscore$celltype=='Invasive'])$p.value 247 | pv2 = wilcox.test(cycscore$cycling[cycscore$celltype=='Proliferation'],cycscore$cycling[cycscore$celltype=='Basal'])$p.value 248 | pv3 = wilcox.test(cycscore$cycling[cycscore$celltype=='Proliferation'],cycscore$cycling[cycscore$celltype=='Differentiation'])$p.value 249 | 250 | 251 | 252 | aggregate(cycscore1$cycling,by=as.list(cycscore1[,c('stage1'),drop=F]),median) 253 | 254 | 255 | # diffgene = c('ECM1','EMP1','SPRR2E','ANXA1','S100A8','CSTA') 256 | # difscore = getscore(srtepi,list(diff=diffgene)) %>% filter(cellID%in%epidt$cellID) 257 | # 258 | # difscore1 = aggregate(list(diff=difscore$diff),by=as.list(difscore[,c('sp_stg','celltype')]),mean) 259 | # difscore1$celltype = difscore1$celltype %>% factor(c('Differentiation','Basal','Proliferation','Invasive')) 260 | # difscore1 %>% 261 | # ggplot(aes(celltype, diff))+ 262 | # theme1+NoLegend()+ 263 | # theme(panel.border = element_rect(fill=NA,color='black',size=0.5), 264 | # axis.line = element_blank())+ 265 | # scale_fill_manual(values = epicol)+ 266 | # geom_boxplot(aes(fill=celltype))+ 267 | # stat_compare_means(comparisons = makecomparison(4)) 268 | # ggsave('stp2_epi/plot/diff_score_box.pdf',width = 3.5,height = 4) 269 | 270 | 271 | load('stp2_epi/data/predDiffRFxenium.Rd') 272 | predscore = data.frame(cellID=gsub('.','-',names(predDiffRFxenium.v),fixed = T),score=predDiffRFxenium.v) 273 | predscore$dediffscore = 1-(predscore$score-min(predscore$score))/(max(predscore$score)-min(predscore$score)) 274 | predscore1 = predscore %>% inner_join(srtepi@meta.data) 275 | predscore1$celltype = predscore1$celltype %>% factor(c('Differentiation','Proliferation','Basal','Invasive')) 276 | predscore2 = aggregate(list(score=predscore1$dediffscore),by=as.list(predscore1[,c('sp_stg','stage1')]),mean) 277 | 278 | predscore2 %>% 279 | ggplot(aes(stage1, score))+ 280 | theme1+NoLegend()+ 281 | # theme(panel.border = element_rect(fill=NA,color='black',linewidth=0.5), 282 | # axis.line = element_blank())+ 283 | xlab('')+ylab('Differentiation score')+ 284 | scale_fill_manual(values = col_stage)+ 285 | stat_boxplot(geom = 'errorbar',linewidth=0.5,width=0.5)+ 286 | geom_boxplot(aes(fill=stage1),outlier.shape = 21,outlier.size = 3,outlier.stroke = 0.1)+ 287 | stat_compare_means(comparisons = makecomparison(4)) 288 | ggsave('stp2_epi/plot/dediff_score_stage_box.pdf',width = 4,height = 4) 289 | pv1 = wilcox.test(predscore1$score[cycscore$stage1=='NOR'],cycscore$score[cycscore$celltype=='LGIN'])$p.value 290 | pv2 = wilcox.test(predscore1$score[cycscore$stage1=='NOR'],cycscore$score[cycscore$celltype=='HGIN'])$p.value 291 | pv3 = wilcox.test(predscore1$score[cycscore$stage1=='NOR'],cycscore$score[cycscore$celltype=='ESCC'])$p.value 292 | 293 | aggregate(list(score=predscore2$score),by=as.list(predscore2[,c('stage1'),drop=F]),median) 294 | 295 | 296 | predscore1 %>% 297 | ggplot(aes(celltype, score))+ 298 | theme1+NoLegend()+ 299 | theme(panel.border = element_rect(fill=NA,color='black',size=0.5), 300 | axis.line = element_blank())+ 301 | scale_fill_manual(values = epicol)+ 302 | stat_boxplot(geom = 'errorbar',size=0.5,width=0.5)+ 303 | geom_boxplot(aes(fill=celltype),outlier.shape = 21,outlier.size = 3,outlier.stroke = 0.1)+ 304 | stat_compare_means(comparisons = makecomparison(4)) 305 | ggsave('stp2_epi/plot/diff_score_celltype_box.pdf',width = 4,height = 4) 306 | 307 | pv1 = wilcox.test(predscore1$score[cycscore$celltype=='Differentiation'],cycscore$score[cycscore$celltype=='Proliferation'])$p.value 308 | pv2 = wilcox.test(predscore1$score[cycscore$celltype=='Differentiation'],cycscore$score[cycscore$celltype=='Basal'])$p.value 309 | pv3 = wilcox.test(predscore1$score[cycscore$celltype=='Differentiation'],cycscore$score[cycscore$celltype=='Invasive'])$p.value 310 | 311 | 312 | predscore3 = aggregate(list(score=predscore1$dediffscore),by=as.list(predscore1[,c('sp_stg','stage1','celltype','pid')]),mean) 313 | predscore3 %>% 314 | filter(celltype%in%c('Proliferation')) %>% 315 | ggplot(aes(stage1, score))+ 316 | theme1+NoLegend()+ 317 | # theme(panel.border = element_rect(fill=NA,color='black',size=0.5), 318 | # axis.line = element_blank())+ 319 | xlab('')+ylab('Stemness score')+ 320 | scale_fill_manual(values = col_stage)+ 321 | # scale_y_continuous(limits = c(0,1))+ 322 | facet_wrap(~celltype,scales = 'free')+ 323 | stat_boxplot(geom = 'errorbar',size=0.5,width=0.5)+ 324 | geom_boxplot(aes(fill=stage1),outlier.shape = 21,outlier.size = 3,outlier.stroke = 0.1)+ 325 | stat_compare_means(comparisons = makecomparison(4)) 326 | ggsave('stp2_epi/plot/dediff_score_stagecelltype_box.pdf',width = 4,height = 4) 327 | 328 | pv1 = wilcox.test(predscore1$score[cycscore$celltype=='Differentiation'],cycscore$score[cycscore$celltype=='Proliferation'])$p.value 329 | pv2 = wilcox.test(predscore1$score[cycscore$celltype=='Differentiation'],cycscore$score[cycscore$celltype=='Basal'])$p.value 330 | pv3 = wilcox.test(predscore1$score[cycscore$celltype=='Differentiation'],cycscore$score[cycscore$celltype=='Invasive'])$p.value 331 | 332 | aggregate(predscore3$score,by=as.list(predscore3[,c('stage1','celltype')]),median) 333 | 334 | 335 | 336 | {# cell level stage violin 337 | p = lapply(c('Basal','Proliferation','Invasive'),function(x){ 338 | dt = predscore1 %>% filter(celltype%in%x) 339 | compnum = unique(dt$stage1) %>% length 340 | 341 | dt %>% 342 | ggplot(aes(stage1, dediffscore))+ 343 | theme1+NoLegend()+ 344 | xlab('')+ylab('Stemness score')+ggtitle(x)+ 345 | stat_compare_means(comparisons = makecomparison(compnum))+ 346 | scale_fill_manual(values = col_stage)+ 347 | scale_y_continuous(limits = c(0,1.3),breaks = seq(0,1,0.5),labels = c('0','0.5','1.0'))+ 348 | geom_violin(aes(fill=stage1),color=NA)+ 349 | stat_summary(geom='errorbar',fun.min = median,fun.max = median) 350 | 351 | # lapply(unique(dt$stage1)[-1],function(y){ 352 | # dt1 = dt %>% filter(stage1==unique(dt$stage1)[1]) 353 | # dt2 = dt %>% filter(stage1==y) 354 | # pv=wilcox.test(dt1$dediffscore,dt2$dediffscore)$p.value 355 | # }) 356 | }) 357 | ggsave('stp2_epi/plot/dediff_score_stagecelltype_celllevel_violin.pdf',cowplot::plot_grid(plotlist = p,nrow = 1,rel_widths = c(2,3,2)),width = 8,height = 4) 358 | } 359 | 360 | {# cell level stage violin, patient t1 t3 361 | tmp = predscore1 %>% filter(celltype%in%c('Proliferation','Basal')) %>% filter(pid%in%c('T1','T3')) 362 | tmp$grp = tmp$stage1 %>% factor(levels = c('Basal','NOR','LGIN','HGIN','ESCC')) 363 | tmp$grp[tmp$celltype=='Basal'] = 'Basal' 364 | 365 | tmp %>% 366 | ggplot(aes(grp, dediffscore))+ 367 | theme1+NoLegend()+ 368 | xlab('')+ylab('Stemness score')+ 369 | facet_wrap(~pid)+ 370 | stat_compare_means(comparisons = makecomparison(4,startnum = 2))+ 371 | scale_fill_manual(values = c(epicol,col_stage))+ 372 | scale_y_continuous(limits = c(0,1.3),breaks = seq(0,1,0.5),labels = c('0','0.5','1.0'))+ 373 | geom_violin(aes(fill=grp),color=NA)+ 374 | stat_summary(geom='errorbar',fun.min = median,fun.max = median) 375 | ggsave('stp2_epi/plot/dediff_score_stageprolife_celllevel_violin.pdf',width = 6,height = 4) 376 | 377 | p = lapply(c('T2','T4','T5','T6'),function(x){ 378 | dt = predscore1 %>% filter(pid%in%x) %>% filter(celltype=='Proliferation') 379 | compnum = unique(dt$stage1) %>% length 380 | 381 | dt %>% 382 | ggplot(aes(stage1, dediffscore))+ 383 | theme1+NoLegend()+ 384 | xlab('')+ylab('Stemness score')+ 385 | facet_wrap(~pid)+ 386 | stat_compare_means(comparisons = makecomparison(compnum))+ 387 | scale_fill_manual(values = col_stage)+ 388 | scale_y_continuous(limits = c(0,1.3),breaks = seq(0,1,0.5),labels = c('0','0.5','1.0'))+ 389 | geom_violin(aes(fill=stage1),color=NA)+ 390 | stat_summary(geom='errorbar',fun.min = median,fun.max = median) 391 | 392 | # lapply(unique(dt$stage1)[-1],function(y){ 393 | # dt1 = dt %>% filter(stage1==unique(dt$stage1)[1]) 394 | # dt2 = dt %>% filter(stage1==y) 395 | # pv=wilcox.test(dt1$dediffscore,dt2$dediffscore)$p.value 396 | # }) 397 | }) 398 | ggsave('stp2_epi/plot/dediff_score_stageprolife_celllevel_violin_supp.pdf',cowplot::plot_grid(plotlist = p,nrow = 1,rel_widths = c(2,3,2.5,2.5)),width = 10,height = 4) 399 | } 400 | 401 | 402 | 403 | 404 | 405 | 406 | ##### prolife diff 2d plot ##### 407 | library(ggstar) 408 | cyc_diffscore = inner_join(cycscore,predscore) 409 | cyc_diffscore$celltype = cyc_diffscore$celltype %>% factor(c('Basal','Proliferation','Differentiation','Invasive')) 410 | cyc_diffscore = cyc_diffscore %>% dplyr::rename(c('prolifescore'='cycling','diffscore'='dediffscore')) 411 | cyc_diffscore1 = aggregate(list(prolifescore=cyc_diffscore$prolifescore,diffscore=cyc_diffscore$diffscore), 412 | by=as.list(cyc_diffscore[,c('celltype','sp_stg')]),mean) 413 | 414 | cyc_diff_median = aggregate(list(prolifescore=cyc_diffscore$prolifescore,diffscore=cyc_diffscore$diffscore), 415 | by=as.list(cyc_diffscore[,c('celltype'),drop=F]),mean) 416 | rownames(cyc_diff_median) = cyc_diff_median$celltype 417 | 418 | cyc_diffscore %>% 419 | ggplot(aes(prolifescore,diffscore))+ 420 | scale_color_manual(values = epicol)+scale_fill_manual(values = epicol)+ 421 | geom_point(size=0.1,aes(color=celltype))+ 422 | geom_star(data=cyc_diff_median,aes(fill=celltype),size=5)+ 423 | geom_hline(yintercept = median(cyc_diffscore$diffscore))+ 424 | geom_vline(xintercept = median(cyc_diffscore$prolifescore))+ 425 | theme1+ 426 | xlim(range(cyc_diffscore$prolifescore))+ylim(range(cyc_diffscore$diffscore)) 427 | ggsave('stp2_epi/plot/prolife_diff_scatter.png',width = 6,height=4) 428 | # ggsave('stp2_epi/plot/prolife_diff_scatter_foreg.pdf',width = 6,height=4) 429 | 430 | cyc_diffscore %>% 431 | ggplot(aes(prolifescore,diffscore))+ 432 | scale_color_manual(values = epicol)+scale_fill_manual(values = epicol)+ 433 | facet_wrap(~celltype)+ 434 | # geom_point(size=0.1,aes(color=celltype))+ 435 | # geom_density_2d_filled()+ 436 | geom_density_2d(binwidth=0.2,aes(color=celltype))+ 437 | geom_star(data=cyc_diff_median,aes(fill=celltype),size=5)+ 438 | geom_hline(yintercept = median(cyc_diffscore$diffscore))+ 439 | geom_vline(xintercept = median(cyc_diffscore$prolifescore))+ 440 | theme1+ 441 | xlim(range(cyc_diffscore$prolifescore))+ylim(range(cyc_diffscore$diffscore)) 442 | ggsave('stp2_epi/plot/prolife_diff_scatter_facet.png',width = 10,height=8) 443 | 444 | cyc_diffscore %>% 445 | ggplot(aes(prolifescore))+ 446 | theme1+NoLegend()+ 447 | xlim(c(0,1.5))+ 448 | scale_color_manual(values = epicol)+scale_fill_manual(values = epicol)+ 449 | # geom_point(size=0.1,aes(color=celltype))+ 450 | geom_density(aes(fill=celltype),alpha=0.5) 451 | ggsave('stp2_epi/plot/prolifeinv_score_celltype_density.pdf',width = 9,height = 3) 452 | cyc_diffscore %>% 453 | ggplot(aes(diffscore))+ 454 | theme1+NoLegend()+ 455 | # xlim(c(0,1.5))+ 456 | scale_color_manual(values = epicol)+scale_fill_manual(values = epicol)+ 457 | # geom_point(size=0.1,aes(color=celltype))+ 458 | geom_density(aes(fill=celltype),alpha=0.5) 459 | ggsave('stp2_epi/plot/dediff_score_celltype_density.pdf',width = 9,height = 3) 460 | 461 | 462 | 463 | 464 | 465 | heatdt = cyc_diff_median[,2:3] %>% t 466 | heatdt = heatdt[,c('Differentiation','Basal','Proliferation','Invasive')] 467 | 468 | col1 = c(brewer.pal(9,'Reds')) 469 | col2 = c(brewer.pal(9,'Blues')) 470 | 471 | # ComplexHeatmap::pheatmap(heatdt[c(1,1),],scale = 'row',color = col1,cluster_cols = F,cluster_rows = F) 472 | # ComplexHeatmap::pheatmap(heatdt[c(2,2),],scale = 'row',color = col2,cluster_cols = F,cluster_rows = F) 473 | 474 | heatdt1 = heatdt %>% t %>% scale 475 | heatdt1 = heatdt1 %>% data.frame %>% 476 | mutate(celltype=rownames(.)) %>% gather('prog','score',-celltype) 477 | heatdt1$celltype = heatdt1$celltype %>% factor(levels = c('Differentiation','Basal','Proliferation','Invasive')) 478 | heatdt1 %>% 479 | ggplot(aes(celltype,prog))+ 480 | theme1+RotatedAxis()+ 481 | scale_size_continuous(range = c(2,8))+ 482 | scale_fill_gradientn(colors = col2)+ 483 | geom_point(data=heatdt1[heatdt1$prog=='diffscore',],shape=21,aes(fill=score,size=score))+ 484 | new_scale('fill')+ 485 | scale_fill_gradientn(colors = col1)+ 486 | geom_point(data=heatdt1[heatdt1$prog=='prolifescore',],shape=21,aes(fill=score,size=score)) 487 | ggsave('stp2_epi/plot/prolife_diff_dotplot.pdf',width = 5,height = 5) 488 | 489 | 490 | heatdt2 = aggregate(list(prolifescore=cyc_diffscore$prolifescore,diffscore=cyc_diffscore$diffscore), 491 | by=as.list(cyc_diffscore[,c('celltype','stage1'),drop=F]),mean) 492 | heatdt2$celltype = heatdt2$celltype %>% factor(levels = c('Basal','Proliferation','Differentiation','Invasive')) 493 | heatdt2 = heatdt2 %>% gather('prog','score',prolifescore,diffscore) 494 | 495 | heatdt2 %>% 496 | ggplot(aes(celltype,prog))+ 497 | theme1+RotatedAxis()+ 498 | facet_wrap(~stage1,nrow=1,scales='free_x')+ 499 | scale_size_continuous(range = c(2,8))+ 500 | scale_fill_gradientn(colors = col2)+ 501 | geom_point(data=heatdt2[heatdt2$prog=='diffscore',],shape=21,aes(fill=score,size=score))+ 502 | new_scale('fill')+ 503 | scale_fill_gradientn(colors = col1)+ 504 | geom_point(data=heatdt2[heatdt2$prog=='prolifescore',],shape=21,aes(fill=score,size=score)) 505 | ggsave('stp2_epi/plot/prolife_diff_dotplot_stagect.pdf',width = 10,height = 5) 506 | 507 | -------------------------------------------------------------------------------- /stp2_epilinedist.R: -------------------------------------------------------------------------------- 1 | source('~/xenium/header.R') 2 | source('~/xenium/xenium_function.R') 3 | 4 | 5 | 6 | ##### input ##### 7 | job({ 8 | xe1_1 = read_rds('data/xenium_seurat_processed/xenium_t1_s1_seurat.rds') 9 | xe1_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s1_meta.rds') 10 | xe1_2 = read_rds('data/xenium_seurat_processed/xenium_t1_s2_seurat.rds') 11 | xe1_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s2_meta.rds') 12 | xe1_3 = read_rds('data/xenium_seurat_processed/xenium_t1_s3_seurat.rds') 13 | xe1_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s3_meta.rds') 14 | 15 | xe2_1 = read_rds('data/xenium_seurat_processed/xenium_t2_s1_seurat.rds') 16 | xe2_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s1_meta.rds') 17 | xe2_2 = read_rds('data/xenium_seurat_processed/xenium_t2_s2_seurat.rds') 18 | xe2_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s2_meta.rds') 19 | xe2_3 = read_rds('data/xenium_seurat_processed/xenium_t2_s3_seurat.rds') 20 | xe2_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s3_meta.rds') 21 | 22 | xe3_1 = read_rds('data/xenium_seurat_processed/xenium_t3_s1_seurat.rds') 23 | xe3_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t3_s1_meta.rds') 24 | xe3_2 = read_rds('data/xenium_seurat_processed/xenium_t3_s2_seurat.rds') 25 | xe3_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t3_s2_meta.rds') 26 | 27 | xe4_1 = read_rds('data/xenium_seurat_processed/xenium_t4_s1_seurat.rds') 28 | xe4_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t4_s1_meta.rds') 29 | xe4_2 = read_rds('data/xenium_seurat_processed/xenium_t4_s2_seurat.rds') 30 | xe4_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t4_s2_meta.rds') 31 | 32 | xe5_1 = read_rds('data/xenium_seurat_processed/xenium_t5_s1_seurat.rds') 33 | xe5_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s1_meta.rds') 34 | xe5_2 = read_rds('data/xenium_seurat_processed/xenium_t5_s2_seurat.rds') 35 | xe5_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s2_meta.rds') 36 | xe5_3 = read_rds('data/xenium_seurat_processed/xenium_t5_s3_seurat.rds') 37 | xe5_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s3_meta.rds') 38 | xe5_2_split = read_rds('data/xenium_seurat_processed/xenium_t5_s2_split_seurat.rds') 39 | xe5_2_split@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s2_split_meta.rds') 40 | xe5_3_split = read_rds('data/xenium_seurat_processed/xenium_t5_s3_split_seurat.rds') 41 | xe5_3_split@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s3_split_meta.rds') 42 | 43 | xe6_1 = read_rds('data/xenium_seurat_processed/xenium_t6_s1_seurat.rds') 44 | xe6_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s1_meta.rds') 45 | xe6_2 = read_rds('data/xenium_seurat_processed/xenium_t6_s2_seurat.rds') 46 | xe6_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s2_meta.rds') 47 | xe6_3 = read_rds('data/xenium_seurat_processed/xenium_t6_s3_seurat.rds') 48 | xe6_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s3_meta.rds') 49 | },import = 'auto') 50 | 51 | 52 | 53 | srtepi = read_rds('data/xenium_seurat_processed/ct_epi_srt.rds') 54 | srtepi$stage1 = srtepi$stage1 %>% factor(levels = c('NOR','LGIN','HGIN','ESCC')) 55 | 56 | epimeta = read_rds('data/xenium_seurat_processed/ct_epi_meta.rds') 57 | fibmeta = srtfib@meta.data 58 | crds = read_rds('stp1_summary/data/allsample_coords.rds') 59 | propriac = fibmeta$cellID[fibmeta$fibarea=='Propria'] 60 | 61 | epitype = fread('stp3_tme/data/all_epi_anno_meta_20240120.csv',data.table = F) 62 | epitype = epitype %>% dplyr::select('cellID','meta_anno') 63 | 64 | 65 | 66 | 67 | 68 | ##### color ##### 69 | primcol = brewer.pal(10, 'Paired') 70 | primcol[7] = '#999999' 71 | names(primcol) = c('B','Epi','Endo','Fib','Gland','Mye','Mus','Mast','Pla','T') 72 | 73 | primcol2 = c('#1f78b4','#ffc089','#B5EFB5','#793c1b', 74 | '#6a3d9a','#333333','#ffff33','#df928e') 75 | names(primcol2) = c('B','Endo','Fib','Gland','Mye','Mus','T','Epi') 76 | 77 | 78 | 79 | getp = function(srt, features, group.by){ 80 | p1 = (DimPlot(srt, group.by = group.by, label = T)+NoLegend())+DimPlot(srt, group.by = group.by, label = T) 81 | p2 = DotPlot(srt, features = features, group.by = group.by)+RotatedAxis() 82 | return(p1/p2) 83 | } 84 | 85 | srtpreproc = function(srt){ 86 | srt = SCTransform(srt) 87 | srt = RunPCA(srt) 88 | srt = RunUMAP(srt, dims = 1:10) 89 | srt = FindNeighbors(srt,dims = 1:10) 90 | srt = FindClusters(srt, resolution = seq(0.1,1,0.1)) 91 | return(srt) 92 | } 93 | 94 | 95 | 96 | 97 | ##### epi border ##### 98 | objlist = setNames(list('xe1_1','xe1_2','xe1_3','xe2_1','xe2_2','xe2_3','xe3_1','xe3_2', 99 | 'xe4_1','xe4_2','xe5_1','xe5_2_split','xe5_3_split','xe6_1','xe6_2','xe6_3'), 100 | c('T1S1','T1S2','T1S3','T2S1','T2S2','T2S3','T3S1','T3S2', 101 | 'T4S1','T4S2','T5S1','T5S2','T5S3','T6S1','T6S2','T6S3')) 102 | # bd = lapply(setNames(names(objlist),names(objlist)),function(x){ 103 | # nms = tolower(x) 104 | # bd = list(d=fread(paste0('stp3_tme/data/epiborder/',nms,'_D.csv'),data.table = F), u=fread(paste0('stp3_tme/data/epiborder/',nms,'_U.csv'),data.table = F)) 105 | # return(bd) 106 | # }) 107 | # saveRDS(bd,'stp3_tme/data/epiborder/allborder.rds') 108 | bd = read_rds('stp3_tme/data/epiborder/allborder.rds') 109 | 110 | 111 | 112 | { 113 | x = 'T6S2' 114 | nms = tolower(x) 115 | d=fread(paste0('stp3_tme/data/epiborder/',nms,'_D.csv'),data.table = F) 116 | u=fread(paste0('stp3_tme/data/epiborder/',nms,'_U.csv'),data.table = F) 117 | borderseg = list(d = d,u = u) 118 | 119 | tmp = crds[[x]] 120 | (tmp %>% 121 | filter(x<7000,x>6000,y<4000,y>3000,ct3=='Epi') %>% 122 | ggplot(aes(x,y))+ 123 | geom_point(size=0.5,color='lightblue')+coord_fixed()+ 124 | geom_path(data=borderseg[['d']])) %>% plotly::ggplotly() 125 | 126 | tmpdt = c(6390.497,3557.635,6556.084,3453.649,6661.388,3354.170) %>% matrix(byrow = T,ncol = 2) 127 | 128 | poly = read_rds(paste0('stp3_tme/data/epiregion/',x,'_poly.rds')) 129 | tmp1 = poly[2] 130 | plotpoly(tmp1) 131 | 132 | f1(tmp1) %>% plotly::ggplotly() 133 | tmp2 = tmp1[[1]][c(511:nrow(tmp1[[1]]),1:97),] %>% 134 | rbind(.,tmpdt) %>% rbind(tmp1[[1]][134:231,]) 135 | f2(tmp2) 136 | d = tmp2 %>% data.frame() %>% `colnames<-`(c('x','y')) 137 | fwrite(d,paste0('stp3_tme/data/epiborder/',nms,'_D.csv')) 138 | } 139 | 140 | 141 | 142 | 143 | ##### calculate epi distance ##### 144 | objlist = setNames(list('xe1_1','xe1_2','xe1_3','xe2_1','xe2_2','xe2_3','xe3_1','xe3_2', 145 | 'xe4_1','xe4_2','xe5_1','xe5_2_split','xe5_3_split','xe6_1','xe6_2','xe6_3'), 146 | c('T1S1','T1S2','T1S3','T2S1','T2S2','T2S3','T3S1','T3S2', 147 | 'T4S1','T4S2','T5S1','T5S2','T5S3','T6S1','T6S2','T6S3')) 148 | lapply(names(objlist),function(x){ 149 | message(x) 150 | 151 | crd = crds[[x]] 152 | epic = crd[rownames(crd)%in%srtepi$cellID,] 153 | epic = epic[,c('x','y')] 154 | 155 | {# label outlier 156 | poly = read_rds(paste0('stp3_tme/data/epiregion/',x,'_poly.rds')) 157 | 158 | polyall = lapply(1:length(poly), function(x){ 159 | dt = data.frame(poly[[x]]) 160 | dt$grp = as.character(x) 161 | return(dt) 162 | }) %>% Reduce(rbind,.) 163 | colnames(polyall) = c('x','y','grp') 164 | 165 | tmp = lapply(poly, function(x){ 166 | rst = isinner2(epic, x) 167 | }) %>% Reduce('|',.) 168 | 169 | dt = epic %>% mutate(grp=tmp) 170 | ggplot(polyall, aes(x,y))+ 171 | geom_point(data = dt,aes(color=grp),size=0.1)+ 172 | geom_polygon(aes(group=grp),alpha=0.25,color='black')+coord_fixed() 173 | ggsave(paste0('stp3_tme/plottmp/',x,'_outlier.png'),width = 20,height = 10) 174 | } 175 | 176 | 177 | borderseg = bd[[x]] 178 | segs = lapply(names(borderseg),function(x){ 179 | seg = borderseg[[x]][,c('x','y')] 180 | seg$grp=x 181 | return(seg) 182 | }) %>% Reduce(rbind,.) 183 | 184 | {# get invasive epi 185 | if(x%in%c("T5S1","T5S2","T5S3")){ 186 | dt$inv = FALSE 187 | dt %>% 188 | ggplot(aes(x,y))+ 189 | geom_point(aes(color=inv),size=0.1)+ 190 | geom_path(data=borderseg[['u']])+ 191 | geom_path(data=borderseg[['d']])+ 192 | coord_fixed() 193 | ggsave(paste0('stp3_tme/plottmp/',x,'_invasive.png'),width = 20,height = 10) 194 | }else{ 195 | segs %>% 196 | ggplot(aes(x,y))+ 197 | geom_point(data = dt,aes(color=grp),size=0.1)+ 198 | geom_path(aes(group=grp))+coord_fixed() 199 | 200 | segd = borderseg[['d']] 201 | pt1 = c(segd[1,'x'],0) 202 | pt2 = c(segd[nrow(segd),'x'],0) 203 | if(x%in%c('T4S2','T6S1')){ 204 | pt1 = c(segd[1,'x'],max(epic$y)) 205 | pt2 = c(segd[nrow(segd),'x'],max(epic$y)) 206 | } 207 | polybelow=rbind(pt1,segd) %>% rbind(pt2) 208 | 209 | system.time({ 210 | tmp1 = isinner2(epic, polybelow) 211 | }) 212 | 213 | dt$inv = tmp1 214 | dt %>% 215 | ggplot(aes(x,y))+ 216 | geom_point(aes(color=inv),size=0.1)+ 217 | geom_path(data=borderseg[['u']])+ 218 | geom_polygon(data=polybelow,alpha=0.3,color='black')+ 219 | coord_fixed() 220 | ggsave(paste0('stp3_tme/plottmp/',x,'_invasive.png'),width = 20,height = 10) 221 | } 222 | } 223 | 224 | {# calculate dist to border 225 | ddist = apply(epic,1,function(x){ 226 | return(disttoseg(x,borderseg[['d']])) 227 | }) 228 | udist = apply(epic,1,function(x){ 229 | return(disttoseg(x,borderseg[['u']])) 230 | }) 231 | dt$ddist = ddist 232 | dt$udist = udist 233 | dt %>% 234 | filter(grp==TRUE) %>% 235 | filter(inv==FALSE) %>% 236 | ggplot(aes(x,y))+ 237 | geom_point(aes(color=ddist),size=0.1)+ 238 | scale_color_gradientn(colors = brewer.pal(9,'Reds'))+ 239 | geom_path(data=segs,aes(group=grp))+coord_fixed() 240 | ggsave(paste0('stp3_tme/plottmp/',x,'_downDist.png'),width = 20,height = 10) 241 | dt %>% 242 | filter(grp==TRUE) %>% 243 | filter(inv==FALSE) %>% 244 | ggplot(aes(x,y))+ 245 | geom_point(aes(color=udist),size=0.1)+ 246 | scale_color_gradientn(colors = brewer.pal(9,'Reds'))+ 247 | geom_path(data=segs,aes(group=grp))+coord_fixed() 248 | ggsave(paste0('stp3_tme/plottmp/',x,'_upDist.png'),width = 20,height = 10) 249 | 250 | reldist = ifelse(dt$inv,ddist/(udist-ddist),ddist/(ddist+udist)) 251 | 252 | dt$reldist = reldist 253 | dt %>% 254 | filter(grp==TRUE) %>% 255 | ggplot(aes(x,y))+ 256 | geom_point(aes(color=reldist),size=0.1)+ 257 | scale_color_gradientn(colors = brewer.pal(9,'Reds'))+ 258 | geom_path(data=segs,aes(group=grp))+coord_fixed() 259 | ggsave(paste0('stp3_tme/plottmp/',x,'_relativeDist.png'),width = 20,height = 10) 260 | } 261 | saveRDS(dt,paste0('stp3_tme/data/epi_borderdist/',x,'.rds')) 262 | return(NULL) 263 | }) 264 | 265 | epidist = lapply(names(objlist),function(x){ 266 | dt = read_rds(paste0('stp3_tme/data/epi_borderdist/',x,'.rds')) 267 | return(dt) 268 | }) %>% Reduce(rbind,.) 269 | colnames(epidist) = c('x','y','inEpi','invasive','downDist','upDist','relativeDist') 270 | epidist$cellID = rownames(epidist) 271 | saveRDS(epidist,'stp3_tme/data/epi_borderdist/allepi_borderdist.rds') 272 | 273 | 274 | 275 | 276 | ##### analysis ##### 277 | epidist = read_rds('stp3_tme/data/epi_borderdist/allepi_borderdist.rds') 278 | epidist = epidist %>% left_join(srtepi@meta.data) %>% inner_join(epitype) 279 | epidist$epithick = epidist$upDist+epidist$downDist 280 | epidist = epidist[epidist$inEpi,] 281 | epidist = epidist[!epidist$invasive,] 282 | epidist = epidist %>% filter(stage1!='ESCC') 283 | epidist$sample1 = epidist$cellID %>% str_split('_',simplify= T) %>% .[,1] 284 | 285 | epidist_aggr = aggregate(epidist$epithick,by=as.list(epidist[,c('stage1','sp_stg')]),mean) 286 | # epidist_aggr = aggregate(epidist$epithick,by=as.list(epidist[,c('stage1','sample1')]),mean) 287 | epidist_aggr$stage1 = epidist_aggr$stage1 %>% factor(levels = c('NOR','LGIN','HGIN','ESCC')) 288 | epidist_aggr$reldist = epidist_aggr$x*0.2125 289 | epidist_aggr %>% 290 | .[grep('T3|T4',epidist_aggr$sp_stg,invert = T),] %>% 291 | # .[grep('T3',epidist_aggr$sample1,invert = T),] %>% 292 | ggplot(aes(stage1,reldist,color=stage1))+ 293 | theme1+ 294 | stat_compare_means(comparisons = makecomparison(3))+ 295 | scale_color_manual(values = col_stage)+ 296 | # scale_y_continuous(limits = c(45,220))+ 297 | scale_y_continuous(limits = c(50,220))+ 298 | NoLegend()+ 299 | geom_jitter(width = 0.3)+ 300 | geom_boxplot(fill=NA,outlier.color = NA) 301 | ggsave('stp3_tme/plottmp/epithick_sp.pdf',width = 3,height = 3) 302 | 303 | 304 | 305 | 306 | epidist_aggr1 = aggregate(as.list(epidist[,c('relativeDist','epithick')]),by=as.list(epidist[,c('stage1','sp_stg','meta_anno')]),mean) 307 | epidist_aggr1$meta_anno = epidist_aggr1$meta_anno %>% factor(levels = c('Basal','Proliferation','Differentiation','Terminal','Non-invasive','Invasive')) 308 | # epidist %>% 309 | epidist_aggr1 %>% 310 | filter(stage1!='HGIN',!meta_anno%in%c('Invasive','Non-invasive')) %>% 311 | ggplot(aes(stage1,relativeDist))+ 312 | # geom_jitter()+ 313 | geom_jitter(aes(fill=meta_anno),shape=21)+ 314 | geom_boxplot(aes(fill=meta_anno),outlier.color = NA,alpha=0.5)+ 315 | theme1+ 316 | RotatedAxis()+ 317 | NoLegend()+ 318 | scale_color_manual(values = brewer.pal(9,'Set3'))+ 319 | scale_fill_manual(values = brewer.pal(9,'Set3'))+ 320 | scale_y_continuous(limits = c(0,1.1),breaks = seq(0,1,0.25))+ 321 | stat_compare_means(comparisons = makecomparison(2))+ 322 | facet_wrap(~meta_anno,scales = 'free_x',nrow = 1) 323 | ggsave('stp3_tme/plottmp/reldist_program.pdf',width = 8,height = 4) 324 | 325 | 326 | 327 | tmp = aggregate(as.list(epidist[,c('relativeDist'),drop=F]),by=as.list(epidist[,c('stage1','sp_stg','meta_anno')]),mean) 328 | epidist_aggr2 = tmp %>% `colnames<-`(c('stage1','sp_stg','meta_anno','relativeDist-mean')) 329 | tmp = aggregate(as.list(epidist[,c('relativeDist'),drop=F]),by=as.list(epidist[,c('stage1','sp_stg','meta_anno')]),median) 330 | epidist_aggr2 = tmp %>% `colnames<-`(c('stage1','sp_stg','meta_anno','relativeDist-median')) %>% left_join(epidist_aggr2,.) 331 | tmp = aggregate(as.list(epidist[,c('relativeDist'),drop=F]),by=as.list(epidist[,c('stage1','sp_stg','meta_anno')]),min) 332 | epidist_aggr2 = tmp %>% `colnames<-`(c('stage1','sp_stg','meta_anno','relativeDist-min')) %>% left_join(epidist_aggr2,.) 333 | tmp = aggregate(as.list(epidist[,c('relativeDist'),drop=F]),by=as.list(epidist[,c('stage1','sp_stg','meta_anno')]),max) 334 | epidist_aggr2 = tmp %>% `colnames<-`(c('stage1','sp_stg','meta_anno','relativeDist-max')) %>% left_join(epidist_aggr2,.) 335 | tmp = aggregate(as.list(epidist[,c('relativeDist'),drop=F]),by=as.list(epidist[,c('stage1','sp_stg','meta_anno')]),function(x){return(quantile(x,0.25))}) 336 | epidist_aggr2 = tmp %>% `colnames<-`(c('stage1','sp_stg','meta_anno','relativeDist-q25')) %>% left_join(epidist_aggr2,.) 337 | tmp = aggregate(as.list(epidist[,c('relativeDist'),drop=F]),by=as.list(epidist[,c('stage1','sp_stg','meta_anno')]),function(x){return(quantile(x,0.75))}) 338 | epidist_aggr2 = tmp %>% `colnames<-`(c('stage1','sp_stg','meta_anno','relativeDist-q75')) %>% left_join(epidist_aggr2,.) 339 | epidist_aggr2$meta_anno = epidist_aggr2$meta_anno %>% factor(levels = c('Basal','Proliferation','Differentiation','Terminal','Non-invasive','Invasive')) 340 | fwrite(epidist_aggr2,'stp3_tme/allsample_layerPercent.csv',sep = ',') 341 | 342 | 343 | 344 | 345 | 346 | ##### t1s1 example ##### 347 | crds = read_rds('stp1_summary/data/allsample_coords.rds') 348 | 349 | {## t1s1 350 | poly11 = read_rds('stp3_tme/data/epiregion/T1S1_poly.rds') 351 | 352 | polyall = lapply(1:length(poly11), function(x){ 353 | dt = data.frame(poly11[[x]]) 354 | dt$grp = as.character(x) 355 | return(dt) 356 | }) %>% Reduce(rbind,.) 357 | colnames(polyall) = c('x','y','grp') 358 | 359 | c11 = crds[['T1S1']] 360 | epic11 = c11[rownames(c11)%in%srtepi$cellID,] 361 | epic11 = epic11[,c('x','y')] 362 | 363 | system.time({ 364 | tmp = lapply(poly11, function(x){ 365 | rst = isinner2(epic11, x) 366 | }) %>% Reduce('|',.) 367 | }) 368 | 369 | dt = epic11 %>% mutate(grp=tmp) 370 | ggplot(polyall, aes(x,y))+ 371 | geom_polygon(aes(group=grp),alpha=0.25)+ 372 | geom_point(data = dt,aes(color=grp),size=0.1)+coord_fixed() 373 | ggsave('stp3_tme/xe11_epi-outlier.png',width = 20,height = 10) 374 | 375 | 376 | 377 | borderseg = list(d=fread('stp3_tme/data/epiborder/t1s1_D.csv'),u=fread('stp3_tme/data/epiborder/t1s1_U.csv')) 378 | segs = lapply(names(borderseg),function(x){ 379 | seg = borderseg[[x]][,c('x','y')] 380 | seg$grp=x 381 | return(seg) 382 | }) %>% Reduce(rbind,.) 383 | 384 | ## get invasive epi 385 | segs %>% 386 | ggplot(aes(x,y))+ 387 | geom_point(data = dt,aes(color=grp),size=0.1)+ 388 | geom_path(aes(group=grp))+coord_fixed() 389 | ptbelow = c(max(epic11$x),0,0,0) %>% matrix(nrow = 2,byrow = T) %>% `colnames<-`(c('x','y')) 390 | ptup = c(max(epic11$x),max(epic11$y),0,max(epic11$y)) %>% matrix(nrow = 2,byrow = T) %>% `colnames<-`(c('x','y')) 391 | polybelow=rbind(borderseg[['d']][,c('x','y')],ptbelow) 392 | polybelow %>% 393 | ggplot(aes(x,y))+ 394 | geom_point(data = dt,aes(color=grp),size=0.1)+ 395 | geom_polygon(alpha=0.5)+coord_fixed() 396 | 397 | system.time({ 398 | tmp1 = isinner2(epic11, polybelow) 399 | }) 400 | 401 | dt$inv = tmp1 402 | polybelow %>% 403 | ggplot(aes(x,y))+ 404 | geom_point(data = dt,aes(color=inv),size=0.1)+ 405 | geom_polygon(alpha=0.5)+coord_fixed() 406 | 407 | ## calculate dist to border 408 | ddist = apply(epic11,1,function(x){ 409 | return(disttoseg(x,borderseg[['d']][,c('x','y')])) 410 | }) 411 | udist = apply(epic11,1,function(x){ 412 | return(disttoseg(x,borderseg[['u']][,c('x','y')])) 413 | }) 414 | dt %>% 415 | filter(grp==TRUE) %>% 416 | filter(inv==FALSE) %>% 417 | filter(x>8000) %>% 418 | ggplot(aes(x,y))+ 419 | geom_point(aes(color=ddist),size=0.1)+ 420 | scale_color_gradientn(colors = brewer.pal(9,'Reds'))+ 421 | geom_path(data=segs,aes(group=grp))+coord_fixed() 422 | ggsave('stp3_tme/xe11_epi-downDist.png',width = 40,height = 15) 423 | dt %>% 424 | filter(grp==TRUE) %>% 425 | filter(inv==FALSE) %>% 426 | filter(x>8000) %>% 427 | ggplot(aes(x,y))+ 428 | geom_point(aes(color=udist),size=0.1)+ 429 | scale_color_gradientn(colors = brewer.pal(9,'Reds'))+ 430 | geom_path(data=segs,aes(group=grp))+coord_fixed() 431 | ggsave('stp3_tme/xe11_epi-upDist.png',width = 40,height = 15) 432 | 433 | reldist = ifelse(dt$inv,ddist/(udist-ddist),ddist/(ddist+udist)) 434 | 435 | dt = dt %>% mutate(udist=udist,ddist=ddist,reldist=reldist) 436 | dt %>% 437 | filter(grp==TRUE) %>% 438 | ggplot(aes(x,y))+ 439 | geom_point(aes(color=reldist),size=0.1)+ 440 | scale_color_gradientn(colors = brewer.pal(9,'Reds'))+ 441 | geom_path(data=segs,aes(group=grp))+coord_fixed() 442 | ggsave('stp3_tme/xe11_epi-relativeDist.png',width = 40,height = 15) 443 | 444 | saveRDS(dt,'stp3_tme/xe11_epidist.rds') 445 | 446 | ### get quantile line 447 | tmp = dt %>% filter(grp==TRUE) %>% filter(inv==FALSE) 448 | tmp = tmp %>% filter(reldist<=0.25) 449 | as_tmp = alphahull::ashape(tmp[,c('x','y')],alpha = 150) 450 | poly25 = extractpoly(as_tmp) 451 | tmp = dt %>% filter(grp==TRUE) %>% filter(inv==FALSE) 452 | tmp = tmp %>% filter(reldist<=0.50) 453 | as_tmp = alphahull::ashape(tmp[,c('x','y')],alpha = 150) 454 | poly50 = extractpoly(as_tmp) 455 | tmp = dt %>% filter(grp==TRUE) %>% filter(inv==FALSE) 456 | tmp = tmp %>% filter(reldist<=0.75) 457 | as_tmp = alphahull::ashape(tmp[,c('x','y')],alpha = 150) 458 | poly75 = extractpoly(as_tmp) 459 | 460 | polytmp = append(poly25,poly50) %>% append(poly75) 461 | plotpoly(polytmp) 462 | polytmp = lapply(1:length(polytmp),function(x){ 463 | dt = polytmp[[x]] %>% data.frame 464 | colnames(dt) = c('x','y') 465 | dt$grp = x 466 | return(dt) 467 | }) %>% Reduce(rbind,.) 468 | 469 | dt %>% 470 | filter(grp==TRUE) %>% 471 | filter(inv==FALSE) %>% 472 | filter(x>8000) %>% 473 | ggplot(aes(x,y))+ 474 | geom_point(aes(color=reldist),size=0.1)+ 475 | scale_color_gradientn(colors = brewer.pal(9,'Reds'))+ 476 | coord_fixed()+ 477 | geom_polygon(data=polytmp,aes(group=grp),fill=NA,color='black')+ 478 | geom_path(data=borderseg[['u']]) 479 | ggsave('stp3_tme/xe11_epi-relativeDist_withquantline.png',width = 40,height = 15) 480 | 481 | 482 | ### calculate dist range 483 | epimeta = read.csv('stp3_tme/data/all_epi_anno_meta_20240120.csv') 484 | epimeta$stage1 = epimeta$stage1 %>% factor(levels = c('NOR','LGIN','HGIN','ESCC')) 485 | 486 | epimeta11 = dt %>% mutate(cellID=rownames(.)) %>% inner_join(epimeta) 487 | unique(epimeta11$meta_anno) 488 | epimeta11 %>% 489 | filter(stage1%in%c('NOR','LGIN')) %>% 490 | ggplot(aes(stage1,reldist))+ 491 | theme1+ 492 | RotatedAxis()+ 493 | scale_fill_manual(values = brewer.pal(9,'Set3'))+ 494 | stat_compare_means(comparisons = makecomparison(2))+ 495 | facet_wrap(~meta_anno)+ 496 | geom_boxplot(aes(fill=meta_anno)) 497 | } 498 | 499 | 500 | 501 | 502 | ##### get dist line ##### 503 | epidist = read_rds('stp3_tme/data/epi_borderdist/allepi_borderdist.rds') 504 | getDistline = function(dt, distances){ 505 | crd = dt[,c('x','y')] 506 | 507 | loopundone=T 508 | alp=10 509 | while(loopundone){ 510 | for(i in distances){ 511 | dt1 = dt %>% filter(downDist% filter(sp_stg%in%c('T1-NOR-2','T1-LGIN-2')) 527 | 528 | propplot(tmp$meta_anno,tmp$stage1) 529 | 530 | 531 | 532 | 533 | -------------------------------------------------------------------------------- /stp2_exprSpatial.R: -------------------------------------------------------------------------------- 1 | source('~/xenium/header.R') 2 | source('~/xenium/xenium_function.R') 3 | 4 | 5 | 6 | 7 | # input ##### 8 | xe1_1 = read_rds('data/xenium_seurat_processed/xenium_t1_s1_seurat.rds') 9 | xe1_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s1_meta.rds') 10 | xe1_2 = read_rds('data/xenium_seurat_processed/xenium_t1_s2_seurat.rds') 11 | xe1_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s2_meta.rds') 12 | xe1_3 = read_rds('data/xenium_seurat_processed/xenium_t1_s3_seurat.rds') 13 | xe1_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s3_meta.rds') 14 | 15 | xe2_1 = read_rds('data/xenium_seurat_processed/xenium_t2_s1_seurat.rds') 16 | xe2_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s1_meta.rds') 17 | xe2_2 = read_rds('data/xenium_seurat_processed/xenium_t2_s2_seurat.rds') 18 | xe2_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s2_meta.rds') 19 | xe2_3 = read_rds('data/xenium_seurat_processed/xenium_t2_s3_seurat.rds') 20 | xe2_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s3_meta.rds') 21 | 22 | xe3_1 = read_rds('data/xenium_seurat_processed/xenium_t3_s1_seurat.rds') 23 | xe3_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t3_s1_meta.rds') 24 | xe3_2 = read_rds('data/xenium_seurat_processed/xenium_t3_s2_seurat.rds') 25 | xe3_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t3_s2_meta.rds') 26 | 27 | xe4_1 = read_rds('data/xenium_seurat_processed/xenium_t4_s1_seurat.rds') 28 | xe4_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t4_s1_meta.rds') 29 | xe4_2 = read_rds('data/xenium_seurat_processed/xenium_t4_s2_seurat.rds') 30 | xe4_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t4_s2_meta.rds') 31 | 32 | xe5_1 = read_rds('data/xenium_seurat_processed/xenium_t5_s1_seurat.rds') 33 | xe5_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s1_meta.rds') 34 | xe5_2 = read_rds('data/xenium_seurat_processed/xenium_t5_s2_seurat.rds') 35 | xe5_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s2_meta.rds') 36 | xe5_3 = read_rds('data/xenium_seurat_processed/xenium_t5_s3_seurat.rds') 37 | xe5_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s3_meta.rds') 38 | xe5_2_split = read_rds('data/xenium_seurat_processed/xenium_t5_s2_split_seurat.rds') 39 | xe5_2_split@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s2_split_meta.rds') 40 | xe5_3_split = read_rds('data/xenium_seurat_processed/xenium_t5_s3_split_seurat.rds') 41 | xe5_3_split@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s3_split_meta.rds') 42 | 43 | xe6_1 = read_rds('data/xenium_seurat_processed/xenium_t6_s1_seurat.rds') 44 | xe6_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s1_meta.rds') 45 | xe6_2 = read_rds('data/xenium_seurat_processed/xenium_t6_s2_seurat.rds') 46 | xe6_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s2_meta.rds') 47 | xe6_3 = read_rds('data/xenium_seurat_processed/xenium_t6_s3_seurat.rds') 48 | xe6_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s3_meta.rds') 49 | 50 | allborder = read_rds('stp3_tme/data/epiborder/allborder.rds') 51 | 52 | 53 | primcol2 = c('#1f78b4','#ffc089','#B5EFB5','#793c1b', 54 | '#6a3d9a','#333333','#ffff33','#df928e') 55 | names(primcol2) = c('B','Endo','Fib','Gland','Mye','Mus','T','Epi') 56 | 57 | 58 | 59 | 60 | 61 | # gene expr spatial full ##### 62 | objlist = setNames(list('xe1_1','xe1_2','xe1_3','xe2_1','xe2_2','xe2_3','xe3_1','xe3_2', 63 | 'xe4_1','xe4_2','xe5_1','xe5_2_split','xe5_3_split','xe6_1','xe6_2','xe6_3'), 64 | c('T1S1','T1S2','T1S3','T2S1','T2S2','T2S3','T3S1','T3S2', 65 | 'T4S1','T4S2','T5S1','T5S2','T5S3','T6S1','T6S2','T6S3')) 66 | job({ 67 | lapply(names(objlist), function(x){ 68 | print(x) 69 | dt = get(objlist[[x]]) 70 | dt = subset(dt, cells = dt$cellID[dt$ct3!='Mus']) 71 | 72 | bd = allborder[[x]][['d']] 73 | 74 | flip=F;reversex=F;reversey=F 75 | g1 = c('T1S1','T1S2','T1S3','T3S1','T3S2','T4S1') 76 | g2 = c('T2S1','T2S2','T2S3') 77 | g3 = c('T4S2') 78 | g4 = c('T5S1','T5S2','T5S3') 79 | g5 = c('T6S1') 80 | g6 = c('T6S2','T6S3') 81 | if(x%in%g1){flip=T;reversex=T} 82 | if(x%in%g2){flip=T} 83 | if(x%in%g3){flip=T;reversey=T} 84 | if(x%in%g5){reversey=T} 85 | if(x%in%g6){reversex=T} 86 | 87 | exprcol = rev(brewer.pal(9,'RdBu')) 88 | exprcol = brewer.pal(9,'Reds') 89 | fname = paste0('stp3_tme/plottmp_expr/',x,'_expr.png') 90 | p1 = xefeatureplot(dt, feature = 'JAG1',flip = flip,reversex = reversex,reversey = reversey,color = exprcol,bgcol = 'white',pt.size = 0.1)+geom_path(data=bd,linetype='dashed') 91 | p2 = xefeatureplot(dt, feature = 'FAP',flip = flip,reversex = reversex,reversey = reversey,color = exprcol,bgcol = 'white',pt.size = 0.1)+geom_path(data=bd,linetype='dashed') 92 | p3 = xefeatureplot(dt, feature = 'ACTA2',flip = flip,reversex = reversex,reversey = reversey,color = exprcol,bgcol = 'white',pt.size = 0.1)+geom_path(data=bd,linetype='dashed') 93 | 94 | p = p1/p2/p3 95 | 96 | print('Saving') 97 | ggsave(fname,p,width = 30,height = 50,limitsize = F) 98 | }) 99 | },import = 'auto') 100 | 101 | 102 | 103 | 104 | 105 | # gene expr select ##### 106 | exprcol = brewer.pal(9,'Reds') 107 | themetrans=theme(panel.background=element_rect(fill='transparent'),plot.background=element_rect(fill='transparent',color = NA)) 108 | 109 | exprcol1 = c('white','white',brewer.pal(9,'Blues')) 110 | exprcol2 = c('white','white',brewer.pal(9,'Reds')) 111 | exprcol3 = c(rep('white',4),brewer.pal(9,'Greens')) 112 | 113 | 114 | roin = c(5500,7100,1500,2300) 115 | xedimplot(xe2_1,groupby = 'ct3',color = primcol2,flip = T,reversex = T,axis = T,gridline = T,bgcol = 'white',zoomin = roin,pt.size = 1) 116 | dtn = setdiff(getcellinrange(xe2_1,roin,flip = T),xe2_1$cellID[xe2_1$ct3=='Mus']) 117 | dtn = subset(xe2_1,cells = dtn) 118 | p1 = xedimplot(dtn,zoomin = roin,groupby = 'ct3',color = primcol2,flip = T,reversex = T,gridline = T,bgcol = 'white',pt.size = 1)+NoLegend() 119 | p2 = xefeatureplot(dtn,zoomin = roin, feature = 'JAG1',flip = T,reversex = T,gridline = T,color = exprcol,bgcol = 'white',pt.size = 1) 120 | p3 = xefeatureplot(dtn,zoomin = roin, feature = 'FAP',flip = T,reversex = T,gridline = T,color = exprcol,bgcol = 'white',pt.size = 1) 121 | p4 = xefeatureplot(dtn,zoomin = roin, feature = 'ACTA2',flip = T,reversex = T,gridline = T,color = exprcol,bgcol = 'white',pt.size = 1) 122 | (p1+p2)/(p3+p4) 123 | 124 | 125 | # roil = c(9000,10600,2700,3500) 126 | # xedimplot(xe3_1,groupby = 'ct3',color = primcol2,flip = T,reversex = T,axis = T,gridline = T,bgcol = 'white',zoomin = roil,pt.size = 1) 127 | # dtl = setdiff(getcellinrange(xe3_1,roil,flip = T),xe3_1$cellID[xe3_1$ct3=='Mus']) 128 | # dtl = subset(xe3_1,cells = dtl) 129 | roil = c(19000,20600,3200,4000) 130 | xedimplot(xe1_2,groupby = 'ct3',color = primcol2,flip = T,reversex = T,axis = T,gridline = T,bgcol = 'white',zoomin = roil,pt.size = 1) 131 | dtl = setdiff(getcellinrange(xe1_2,roil,flip = T),xe1_2$cellID[xe1_2$ct3=='Mus']) 132 | dtl = subset(xe1_2,cells = dtl) 133 | p1 = xedimplot(dtl,zoomin = roil,groupby = 'ct3',color = primcol2,flip = T,reversex = T,gridline = T,bgcol = 'white',pt.size = 1)+NoLegend() 134 | p2 = xefeatureplot(dtl,zoomin = roil, feature = 'JAG1',flip = T,reversex = T,gridline = T,color = exprcol,bgcol = 'white',pt.size = 1) 135 | p3 = xefeatureplot(dtl,zoomin = roil, feature = 'FAP',flip = T,reversex = T,gridline = T,color = exprcol,bgcol = 'white',pt.size = 1) 136 | p4 = xefeatureplot(dtl,zoomin = roil, feature = 'ACTA2',flip = T,reversex = T,gridline = T,color = exprcol,bgcol = 'white',pt.size = 1) 137 | (p1+p2)/(p3+p4) 138 | 139 | 140 | roih = c(8800,11800,2100,3600) 141 | xedimplot(xe1_3,groupby = 'ct3',color = primcol2,flip = T,reversex = T,axis = T,gridline = T,bgcol = 'white',zoomin = roih,pt.size = 1) 142 | dth = setdiff(getcellinrange(xe1_3,roih,flip = T),xe1_3$cellID[xe1_3$ct3=='Mus']) 143 | dth = subset(xe1_3,cells = dth) 144 | p1 = xedimplot(dth,zoomin = roih,groupby = 'ct3',color = primcol2,flip = T,reversex = T,gridline = T,bgcol = 'white',pt.size = 1)+NoLegend() 145 | p2 = xefeatureplot(dth,zoomin = roih, feature = 'JAG1',flip = T,reversex = T,gridline = T,color = exprcol,bgcol = 'white',pt.size = 1) 146 | p3 = xefeatureplot(dth,zoomin = roih, feature = 'FAP',flip = T,reversex = T,gridline = T,color = exprcol,bgcol = 'white',pt.size = 1) 147 | p4 = xefeatureplot(dth,zoomin = roih, feature = 'ACTA2',flip = T,reversex = T,gridline = T,color = exprcol,bgcol = 'white',pt.size = 1) 148 | (p1+p2)/(p3+p4) 149 | 150 | 151 | roit = c(8600,11600,700,2200) 152 | xedimplot(xe3_1,groupby = 'ct3',color = primcol2,flip = T,reversex = T,axis = T,gridline = T,bgcol = 'white',zoomin = roit,pt.size = 1) 153 | dtt = setdiff(getcellinrange(xe3_1,roit,flip = T),xe3_1$cellID[xe3_1$ct3=='Mus']) 154 | dtt = subset(xe3_1,cells = dtt) 155 | p1 = xedimplot(dtt,zoomin = roit,groupby = 'ct3',color = primcol2,flip = T,reversex = T,gridline = T,bgcol = 'white',pt.size = 1)+NoLegend() 156 | p2 = xefeatureplot(dtt,zoomin = roit, feature = 'JAG1',flip = T,reversex = T,gridline = T,color = exprcol,bgcol = 'white',pt.size = 1) 157 | p3 = xefeatureplot(dtt,zoomin = roit, feature = 'FAP',flip = T,reversex = T,gridline = T,color = exprcol,bgcol = 'white',pt.size = 1) 158 | p4 = xefeatureplot(dtt,zoomin = roit, feature = 'ACTA2',flip = T,reversex = T,gridline = T,color = exprcol,bgcol = 'white',pt.size = 1) 159 | (p1+p2)/(p3+p4) 160 | 161 | 162 | 163 | p1n = xedimplot(dtn,zoomin = roin,groupby = 'ct3',color = primcol2,flip = T,reversex = T,gridline = F,bgcol = 'transparent',pt.size = 1)+themetrans+NoLegend() 164 | p1l = xedimplot(dtl,zoomin = roil,groupby = 'ct3',color = primcol2,flip = T,reversex = T,gridline = F,bgcol = 'transparent',pt.size = 1)+themetrans+NoLegend() 165 | p1h = xedimplot(dth,zoomin = roih,groupby = 'ct3',color = primcol2,flip = T,reversex = T,gridline = F,bgcol = 'transparent',pt.size = 1)+themetrans+NoLegend() 166 | p1t = xedimplot(dtt,zoomin = roit,groupby = 'ct3',color = primcol2,flip = T,reversex = T,gridline = F,bgcol = 'transparent',pt.size = 1)+themetrans+NoLegend() 167 | p1 = p1n/p1l/p1h/p1t 168 | ggsave('stp3_tme/plot/expr_spatial/expr_fovct.png',p1,width = 15,height = 20,bg = 'transparent') 169 | ggsave('stp3_tme/plot/expr_spatial/expr_fovct_n.png',p1n,width = 15,height = 5,bg = 'transparent') 170 | ggsave('stp3_tme/plot/expr_spatial/expr_fovct_l.png',p1l,width = 15,height = 5,bg = 'transparent') 171 | ggsave('stp3_tme/plot/expr_spatial/expr_fovct_h.png',p1h,width = 15,height = 5,bg = 'transparent') 172 | ggsave('stp3_tme/plot/expr_spatial/expr_fovct_t.png',p1t,width = 15,height = 5,bg = 'transparent') 173 | 174 | p2n = xefeatureplot(dtn,zoomin = roin, feature = 'JAG1',flip = T,reversex = T,gridline = F,color = exprcol1,bgcol = 'white',pt.size = 1,collim = c(0,3))+themetrans 175 | p2l = xefeatureplot(dtl,zoomin = roil, feature = 'JAG1',flip = T,reversex = T,gridline = F,color = exprcol1,bgcol = 'white',pt.size = 1,collim = c(0,3))+themetrans 176 | p2h = xefeatureplot(dth,zoomin = roih, feature = 'JAG1',flip = T,reversex = T,gridline = F,color = exprcol1,bgcol = 'white',pt.size = 1,collim = c(0,3))+themetrans 177 | p2t = xefeatureplot(dtt,zoomin = roit, feature = 'JAG1',flip = T,reversex = T,gridline = F,color = exprcol1,bgcol = 'white',pt.size = 1,collim = c(0,3))+themetrans 178 | p2 = p2n/p2l/p2h/p2t 179 | # ggsave('stp3_tme/plot/expr_spatial/expr_jag1.pdf',p2,width = 15,height = 20) 180 | ggsave('stp3_tme/plot/expr_spatial/expr_jag1.pdf',p2n,width = 15,height = 5,bg = 'transparent') 181 | ggsave('stp3_tme/plot/expr_spatial/expr_jag1_n.png',p2n,width = 15,height = 5,bg = 'transparent') 182 | ggsave('stp3_tme/plot/expr_spatial/expr_jag1_l.png',p2l,width = 15,height = 5,bg = 'transparent') 183 | ggsave('stp3_tme/plot/expr_spatial/expr_jag1_h.png',p2h,width = 15,height = 5,bg = 'transparent') 184 | ggsave('stp3_tme/plot/expr_spatial/expr_jag1_t.png',p2t,width = 15,height = 5,bg = 'transparent') 185 | 186 | p3n = xefeatureplot(dtn,zoomin = roin, feature = 'FAP',flip = T,reversex = T,gridline = F,color = exprcol2,bgcol = 'white',pt.size = 1,collim = c(0,2.5))+themetrans 187 | p3l = xefeatureplot(dtl,zoomin = roil, feature = 'FAP',flip = T,reversex = T,gridline = F,color = exprcol2,bgcol = 'white',pt.size = 1,collim = c(0,2.5))+themetrans 188 | p3h = xefeatureplot(dth,zoomin = roih, feature = 'FAP',flip = T,reversex = T,gridline = F,color = exprcol2,bgcol = 'white',pt.size = 1,collim = c(0,2.5))+themetrans 189 | p3t = xefeatureplot(dtt,zoomin = roit, feature = 'FAP',flip = T,reversex = T,gridline = F,color = exprcol2,bgcol = 'white',pt.size = 1,collim = c(0,2.5))+themetrans 190 | p3 = p3n/p3l/p3h/p3t 191 | # ggsave('stp3_tme/plot/expr_spatial/expr_fap.pdf',p3,width = 15,height = 20) 192 | ggsave('stp3_tme/plot/expr_spatial/expr_fap.pdf',p3n,width = 15,height = 5,bg = 'transparent') 193 | ggsave('stp3_tme/plot/expr_spatial/expr_fap_n.png',p3n,width = 15,height = 5,bg = 'transparent') 194 | ggsave('stp3_tme/plot/expr_spatial/expr_fap_l.png',p3l,width = 15,height = 5,bg = 'transparent') 195 | ggsave('stp3_tme/plot/expr_spatial/expr_fap_h.png',p3h,width = 15,height = 5,bg = 'transparent') 196 | ggsave('stp3_tme/plot/expr_spatial/expr_fap_t.png',p3t,width = 15,height = 5,bg = 'transparent') 197 | 198 | p4n = xefeatureplot(dtn,zoomin = roin, feature = 'ACTA2',flip = T,reversex = T,gridline = F,color = exprcol3,bgcol = 'white',pt.size = 1,collim = c(0,3.5))+themetrans 199 | p4l = xefeatureplot(dtl,zoomin = roil, feature = 'ACTA2',flip = T,reversex = T,gridline = F,color = exprcol3,bgcol = 'white',pt.size = 1,collim = c(0,3.5))+themetrans 200 | p4h = xefeatureplot(dth,zoomin = roih, feature = 'ACTA2',flip = T,reversex = T,gridline = F,color = exprcol3,bgcol = 'white',pt.size = 1,collim = c(0,3.5))+themetrans 201 | p4t = xefeatureplot(dtt,zoomin = roit, feature = 'ACTA2',flip = T,reversex = T,gridline = F,color = exprcol3,bgcol = 'white',pt.size = 1,collim = c(0,3.5))+themetrans 202 | p4 = p4n/p4l/p4h/p4t 203 | # ggsave('stp3_tme/plot/expr_spatial/expr_acta2.pdf',p4,width = 15,height = 20) 204 | ggsave('stp3_tme/plot/expr_spatial/expr_acta2.pdf',p4n,width = 15,height = 5,bg = 'transparent') 205 | ggsave('stp3_tme/plot/expr_spatial/expr_acta2_n.png',p4n,width = 15,height = 5,bg = 'transparent') 206 | ggsave('stp3_tme/plot/expr_spatial/expr_acta2_l.png',p4l,width = 15,height = 5,bg = 'transparent') 207 | ggsave('stp3_tme/plot/expr_spatial/expr_acta2_h.png',p4h,width = 15,height = 5,bg = 'transparent') 208 | ggsave('stp3_tme/plot/expr_spatial/expr_acta2_t.png',p4t,width = 15,height = 5,bg = 'transparent') 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | -------------------------------------------------------------------------------- /stp2_fovplot.R: -------------------------------------------------------------------------------- 1 | source('~/xenium/header.R') 2 | source('~/xenium/xenium_function.R') 3 | library(ComplexHeatmap) 4 | library(trend) 5 | 6 | 7 | 8 | # input ##### 9 | allmeta_m = read_rds('stp5_batch2process/data/allcell/allmerge_allmeta.rds') 10 | epimt = read_rds('stp5_batch2process/data/epi_merge/epi_merge_meta.rds') 11 | crds = read_rds('stp1_summary/data/allsample_coords.rds') 12 | 13 | 14 | plotcluster = function(meta, groupby, 15 | fixed=T, color=NULL, pt.size=0.2,pt.shape=16,bgcol = 'white',ptlevels=NULL, 16 | reversex=F, reversey=F,flip=F, 17 | highlight = NULL,xintercept=NULL,yintercept=NULL, 18 | zoomin = NULL, cellborder = F, areaalpha = 0.5, cellgap = 0, cellradius = 0, linewidth = 0.5, celllinecol=NULL, 19 | gridline=F,gridcol='black',gridgap=100,gridlinewidth=0.03,axis=F,plotborder=T){ 20 | ### coords 21 | { 22 | coords = meta[,c('x','y',groupby)] 23 | rownames(coords) = rownames(meta) 24 | coords[,groupby] = coords[,groupby] %>% factor() 25 | 26 | if(flip){ 27 | coords = coords %>% dplyr::rename(c('x'='y','y'='x')) 28 | } 29 | 30 | xmin = min(coords$x) 31 | ymin = min(coords$y) 32 | xmax = max(coords$x) 33 | ymax = max(coords$y) 34 | 35 | if(length(zoomin)==4){ 36 | zoomin = zoomin %>% as.numeric() 37 | coords = coords %>% filter(x>=zoomin[1],x<=zoomin[2],y>=zoomin[3],y<=zoomin[4]) 38 | xmin = zoomin[1] 39 | xmax = zoomin[2] 40 | ymin = zoomin[3] 41 | ymax = zoomin[4] 42 | } 43 | 44 | if(!is.null(highlight)){ 45 | # coords[rownames(coords)%in%highlight,groupby] = 'group_1' 46 | levels(coords[,groupby]) = levels(coords[,groupby]) %>% append('Others') 47 | coords[!rownames(coords)%in%highlight,groupby] = 'Others' 48 | coords[,groupby] = coords[,groupby] %>% droplevels() 49 | 50 | getPalette = colorRampPalette(brewer.pal(8, "Set1")) 51 | 52 | if(is.null(color)){ 53 | cname = unique(coords[,groupby]) %>% setdiff('Others') 54 | num = cname %>% length() 55 | color = c(getPalette(num),'#d3d3d3') 56 | names(color) = c(cname,'Others') 57 | }else{ 58 | nms = c(names(color),'Others') 59 | color=c(color,'#d3d3d3') 60 | names(color) = nms 61 | } 62 | 63 | if(is.null(ptlevels)){ 64 | ptlevels = levels(coords[,groupby]) 65 | }else{ 66 | ptlevels = unique(c(ptlevels,'Others')) 67 | } 68 | } 69 | } 70 | 71 | 72 | ### make plot 73 | { 74 | p_bg = 75 | coords %>% 76 | arrange(groupby) %>% 77 | ggplot(aes(x,y))+ 78 | geom_rect(aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax), fill=NA,color=NA)+ 79 | theme1+ 80 | theme(panel.background = element_rect(fill=bgcol))+ 81 | labs(color=groupby) 82 | 83 | if(gridline){ 84 | glx = (xmax - xmin)%/%gridgap 85 | if(glx>0){ 86 | glx1 = c(0,glx+1)*gridgap+xmin 87 | glx = (0:(glx+1))*gridgap+xmin 88 | }else{ 89 | glx1 = c(0,glx+1)*gridgap+xmin 90 | glx=NULL 91 | } 92 | gly = (ymax - ymin)%/%gridgap 93 | if(gly>0){ 94 | gly1 = c(0,gly+1)*gridgap+ymin 95 | gly = (0:(gly+1))*gridgap+ymin 96 | }else{ 97 | gly1 = c(0,gly+1)*gridgap+ymin 98 | gly=NULL 99 | } 100 | 101 | p_bg = p_bg+ 102 | xlab('')+ylab('')+ 103 | scale_x_continuous(expand = c(0,0))+ 104 | scale_y_continuous(expand = c(0,0))+ 105 | geom_hline(yintercept = gly,linewidth=gridlinewidth,color=gridcol)+ 106 | geom_vline(xintercept = glx,linewidth=gridlinewidth,color=gridcol) 107 | } 108 | 109 | 110 | if(!is.null(ptlevels)){ 111 | p_layer = lapply(rev(ptlevels), function(x){ 112 | dt = coords[coords[,groupby]==x,] 113 | rst = geom_point(data=dt, aes(x,y,color=.data[[groupby]]),shape=pt.shape,size=pt.size) 114 | return(rst) 115 | }) 116 | p_layer = append(list(p_bg),p_layer) 117 | p = Reduce('+',p_layer) 118 | }else{p = p_bg+geom_point(aes(color=.data[[groupby]]),shape=pt.shape,size = pt.size)} 119 | 120 | 121 | if(cellborder){ 122 | bd = xenium@images$fov@boundaries$segmentation@polygons 123 | bd = bd[rownames(coords)] %>% 124 | lapply(function(x){ 125 | dt = x@Polygons[[1]]@coords %>% data.frame() 126 | dt$ID = x@ID 127 | return(dt) 128 | }) %>% rbindlist() %>% data.frame() 129 | 130 | celltype = data.frame(rownames(coords), coords[,3]) 131 | colnames(celltype) = c('ID',groupby) 132 | bd = bd %>% left_join(celltype) 133 | 134 | if(flip){ 135 | bd = bd %>% dplyr::rename(c('x'='y','y'='x')) 136 | } 137 | 138 | if(!is.null(ptlevels)){ 139 | if(is.null(celllinecol)){ 140 | p_layer1 = lapply(rev(ptlevels), function(x){ 141 | dt = bd[bd[,groupby]==x,] 142 | rst = ggforce::geom_shape(data=dt, 143 | aes(x,y,fill = .data[[groupby]],group=ID,color=.data[[groupby]]), 144 | linewidth = linewidth, 145 | alpha = areaalpha, 146 | expand = cellgap, 147 | radius = cellradius) 148 | return(rst) 149 | }) 150 | }else{ 151 | p_layer1 = lapply(rev(ptlevels), function(x){ 152 | dt = bd[bd[,groupby]==x,] 153 | rst = ggforce::geom_shape(data=dt, 154 | aes(x,y,fill = .data[[groupby]],group=ID), 155 | color=celllinecol, 156 | linewidth = linewidth, 157 | alpha = areaalpha, 158 | expand = cellgap, 159 | radius = cellradius) 160 | return(rst) 161 | }) 162 | } 163 | 164 | p_layer2 = list() 165 | p_layer2[1] = p_layer[1] 166 | p_layer2[1:length(ptlevels)*2] = p_layer[1:length(ptlevels)+1] 167 | p_layer2[1:length(ptlevels)*2+1] = p_layer1 168 | p = Reduce('+',p_layer2) 169 | }else{ 170 | if(is.null(celllinecol)){ 171 | p = p+ 172 | ggforce::geom_shape(data=bd, 173 | aes(x,y,fill = .data[[groupby]],group=ID,color=.data[[groupby]]), 174 | linewidth = linewidth, 175 | alpha = areaalpha, 176 | expand = cellgap, 177 | radius = cellradius) 178 | }else{ 179 | p = p+ 180 | ggforce::geom_shape(data=bd, 181 | aes(x,y,fill = .data[[groupby]],group=ID), 182 | color=celllinecol, 183 | linewidth = linewidth, 184 | alpha = areaalpha, 185 | expand = cellgap, 186 | radius = cellradius) 187 | } 188 | } 189 | } 190 | if(!is.null(color)){ 191 | p = p+scale_color_manual(values = color)+scale_fill_manual(values = color) 192 | } 193 | if(reversex){ 194 | p=p+scale_x_reverse() 195 | } 196 | if(reversey){ 197 | p=p+scale_y_reverse() 198 | } 199 | if(fixed){ 200 | p = p+coord_fixed() 201 | } 202 | if(!is.null(xintercept)){ 203 | p = p+geom_vline(xintercept = xintercept, color='black', linetype='dashed') 204 | } 205 | if(!is.null(yintercept)){ 206 | p = p+geom_hline(yintercept = yintercept, color='black', linetype='dashed') 207 | } 208 | if(!axis){ 209 | p = p+theme(axis.line = element_blank(),axis.ticks = element_blank(),axis.text = element_blank()) 210 | } 211 | if(plotborder){ 212 | p = p+theme(panel.border = element_rect(color='black',linewidth = 1,fill=NA))+xlab('')+ylab('') 213 | } 214 | } 215 | 216 | p = p+guides(color=guide_legend(override.aes = list(size=3))) 217 | return(p) 218 | } 219 | 220 | 221 | 222 | 223 | epicol = c('#0339f8','#ff9408','#75bbfd','#de0c62') 224 | names(epicol) = c('Basal','Proliferation','Differentiation','Invasive') 225 | epicol1 = epicol 226 | names(epicol1) = names(epicol1) %>% mapvalues(c('Proliferation','Differentiation'),c('Proliferative','Differentiated')) 227 | 228 | primcol2 = c('#1f78b4','#ffc089','#B5EFB5','#793c1b', 229 | '#6a3d9a','#333333','#ffff33','#df928e') 230 | names(primcol2) = c('B','Endo','Fib','Gland','Mye','Mus','T','Epi') 231 | 232 | progcol = c('Cell adhesion'="#0339f8", 233 | 'Cycling'="#ff9408", 234 | 'Mucosal defense'="#75bbfd", 235 | 'Inflammation'="#B3DE69", 236 | 'DNA repair'="#a6eee6", 237 | 'Oncogenic TFs'="#9467bd", 238 | 'Angiogenesis'="#41ab5d", 239 | 'EMT'="#ff0000") 240 | 241 | 242 | 243 | 244 | 245 | # preprocessing ##### 246 | borderdist = read_rds('stp3_tme/data/epi_borderdist/allepi_borderdist.rds') 247 | colnames(borderdist) = colnames(borderdist) %>% mapvalues('downDist','borderdist') 248 | 249 | 250 | 251 | 252 | 253 | # plot by fov #### 254 | ## t1 lgin fov ##### 255 | {# t1 lgin fov 256 | dir.create('stp2_epi/plot/epiProgram_spatial2/T1_L/',recursive = T) 257 | dt13 = allmeta_m %>% filter(sid=='T1S3') %>% filter(primaryType=='Epi') 258 | roi = c(19800,20800,2650,3250) 259 | plotcluster(dt13,'celltype',color = epicol1,xintercept = roi[1:2],yintercept = roi[3:4],zoomin = roi,axis = T,pt.size = 1) 260 | dt13_plot = dt13 %>% filter(x<=roi[2],x>=roi[1],y<=roi[4],y>=roi[3]) 261 | 262 | inepic = borderdist$cellID[borderdist$inEpi] 263 | dt13_plot = dt13_plot %>% filter(cellID%in%inepic) 264 | 265 | # generate contour line 266 | distdt = borderdist %>% filter(cellID%in%dt13_plot$cellID) 267 | distdt$dst = distdt$downDist 268 | 269 | crd = dt13_plot[,c('x','y')] 270 | bdlist = list() 271 | for(i in 1:10){ 272 | print(i) 273 | tmp = crd[distdt$cellID[distdt$relativeDist>0.1*(i-1)],] 274 | as = alphahull::ashape(tmp,alpha = 120) 275 | plot(as) 276 | print('wait') 277 | bdlist[[i]] = extractpoly(as)[[1]] %>% data.frame() 278 | colnames(bdlist[[i]]) = c('x','y') 279 | bdlist[[i]]$group = paste0('g',i) 280 | } 281 | bds = Reduce(rbind,bdlist) 282 | 283 | plotcluster(dt13_plot,groupby = 'celltype',color = epicol1, bgcol = 'white',pt.size = 0.8,gridline = T,gridcol = 'black',zoomin = roi)+ 284 | geom_path(data=bds,aes(group=group),color='black',linetype='dashed') 285 | ggsave('stp2_epi/plot/epiProgram_spatial2/T1_L/celltype_spatial.pdf',width = 6,height = 6) 286 | } 287 | 288 | 289 | 290 | 291 | ## t5 lgin fov ##### 292 | {# t5 lgin fov 293 | dir.create('stp2_epi/plot/epiProgram_spatial2/T5_L_v2/',recursive = T) 294 | dt52 = allmeta_m %>% filter(sid=='T5S2') %>% filter(primaryType=='Epi') 295 | roi = c(2500,3500,2930,3530) 296 | plotcluster(dt52,'celltype',color = epicol1,xintercept = roi[1:2],yintercept = roi[3:4],axis = T,zoomin = roi,pt.size = 1) %>% plotly::ggplotly() 297 | ancpt = c('T5S2_djllofgl-1') 298 | 299 | # rotate coordinates 300 | crd = dt52[,c('x','y')] 301 | rotdeg = -13 302 | rotrad = rotdeg/180*pi 303 | wt1 = sin(rotrad) 304 | wt2 = cos(rotrad) 305 | newcrd = crd 306 | newcrd$x = crd$x*wt2-crd$y*wt1 307 | newcrd$y = crd$x*wt1+crd$y*wt2 308 | 309 | dt52_new = dt52 310 | dt52_new[,c('x','y')] = newcrd 311 | roi = dt52_new[ancpt,c('x','y')] %>% unlist() 312 | roi = c(roi[1],roi[1]+1500,roi[2]-250,roi[2]+750) 313 | plotcluster(dt52_new,'celltype',color = epicol1,xintercept = roi[1:2],yintercept = roi[3:4],axis = T,zoomin = roi,pt.size = 1) 314 | dt52_plot = dt52_new %>% filter(x<=roi[2],x>=roi[1],y<=roi[4],y>=roi[3]) 315 | 316 | # generate contour line 317 | distdt = borderdist %>% filter(cellID%in%dt52_plot$cellID) 318 | distdt$dst = distdt$downDist 319 | 320 | crd = dt52_plot[,c('x','y')] 321 | bdlist = list() 322 | for(i in 1:10){ 323 | print(i) 324 | tmp = crd[distdt$cellID[distdt$relativeDist>0.1*(i-1)],] 325 | as = alphahull::ashape(tmp,alpha = 120) 326 | plot(as) 327 | print('wait') 328 | bdlist[[i]] = extractpoly(as)[[1]] %>% data.frame() 329 | colnames(bdlist[[i]]) = c('x','y') 330 | bdlist[[i]]$group = paste0('g',i) 331 | } 332 | bds = Reduce(rbind,bdlist) 333 | 334 | plotcluster(dt52_plot,groupby = 'celltype',color = epicol1, bgcol = 'white',pt.size = 0.8,gridline = T,gridcol = 'black',zoomin = roi)+ 335 | geom_path(data=bds,aes(group=group),color='black',linetype='dashed') 336 | ggsave('stp2_epi/plot/epiProgram_spatial2/T5_L_v2/celltype_spatial.pdf',width = 6,height = 6) 337 | } 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | -------------------------------------------------------------------------------- /stp2_programAnalysis.R: -------------------------------------------------------------------------------- 1 | source('~/xenium/header.R') 2 | source('~/xenium/xenium_function.R') 3 | library(ComplexHeatmap) 4 | 5 | ##### input ##### 6 | srtepi = read_rds('data/xenium_seurat_processed/ct_epi_SCT_2.rds') 7 | epimeta = read_rds('data/xenium_seurat_processed/ct_epi_meta_2.rds') 8 | epidt = read_rds('stp2_epi/data/epiid_correctstage.rds') 9 | 10 | # lamexpr = fread('stp2_epi/data/lamfish_epi12_jagnotchexpr.txt') 11 | lam = read_rds('stp2_epi/data/lam_epi12_seurat.rds') 12 | lam@meta.data = read_rds('stp2_epi/data/lam_epi12_meta.rds') 13 | 14 | epianno = fread('stp1_summary/data/xenium_epi.csv',data.table = F) 15 | 16 | epigeneannot = fread('stp3_tme/data/hm_gene_anno.txt',data.table = F) 17 | 18 | probelist = fread('data/probe_list.txt',data.table = F) 19 | 20 | gridid = read_rds('stp1_summary/data/allsample_gridid.rds') 21 | 22 | epiblacklist = fread('stp2_epi/data/gene_otherTissue.txt',data.table = F) 23 | epiblacklist = probelist[probelist$gene%in%epiblacklist$Gene_otherTissue,] 24 | 25 | 26 | allmk = c('EPCAM','SFN','KRT4','KRT5', # epi 27 | 'FN1','DCN','COL1A1','COL1A2','COL3A1','COL6A1', # fibro 28 | 'PLN','MYH11', # muscle 29 | 'VWF','PECAM1','ENG','CDH5', # endo 30 | 'PTPRC', # immune 31 | 'CD2','CD3D','CD3E', # t cell 32 | 'CD1E','CD68','LYZ','CD14','FCGR3A', # myeloid 33 | 'MS4A2','KIT','CPA3', # mast 34 | 'CD19','CD79A','MS4A1', # B 35 | 'JCHAIN','IGHG2' # plasma 36 | ) 37 | 38 | epimarker = c('KRT15','MYC', # QP 39 | 'TOP2A','MKI67', # CY 40 | 'ANXA1','KRT13','KRT4','CSTA','S100A8','TRIM29', # MD 41 | 'SPRR3','SPRR2E','ECM1','EMP1', # TD 42 | 'HIF1A','SERPINE1','VEGFA', # HY 43 | 'KRT17', # RO 44 | 'SPP1','CES1', 'AKR1C3','AKR1C2','CBR1', # DO 45 | 'HLA-DRB1','HLA-DQB1'# AP 46 | ) 47 | 48 | epicol = c('#0339f8','#ff9408','#75bbfd','#de0c62') 49 | names(epicol) = c('Basal','Proliferation','Differentiation','Invasive') 50 | epicol1 = epicol 51 | names(epicol1) = names(epicol1) %>% mapvalues(c('Proliferation','Differentiation'),c('Proliferative','Differentiated')) 52 | 53 | progcol = c('Cell adhesion'="#0339f8", 54 | 'Cycling'="#ff9408", 55 | 'Mucosal defense'="#75bbfd", 56 | 'Inflammation'="#B3DE69", 57 | 'DNA repair'="#a6eee6", 58 | 'Oncogenic TFs'="#9467bd", 59 | 'Angiogenesis'="#41ab5d", 60 | 'EMT'="#ff0000") 61 | 62 | getp = function(srt, features, group.by){ 63 | p1 = (DimPlot(srt, group.by = group.by, label = T)+NoLegend())+DimPlot(srt, group.by = group.by, label = T) 64 | p2 = DotPlot(srt, features = features, group.by = group.by)+RotatedAxis() 65 | return(p1/p2) 66 | } 67 | 68 | srtpreproc = function(srt){ 69 | srt = SCTransform(srt) 70 | srt = RunPCA(srt) 71 | srt = RunUMAP(srt, dims = 1:10) 72 | srt = FindNeighbors(srt,dims = 1:10) 73 | srt = FindClusters(srt, resolution = seq(0.1,1,0.1)) 74 | return(srt) 75 | } 76 | 77 | 78 | mp_malignant = fread('~/metaprogram_data/program_malignant.csv',data.table = F) 79 | mp_epi = fread('~/metaprogram_data/program_epi.csv',data.table = F) 80 | mpall = mp_epi %>% gather('prog','gene') %>% mutate(ct='epi') 81 | mpall = mp_malignant %>% gather('prog','gene') %>% mutate(ct='malignant') %>% rbind(mpall) 82 | 83 | supp2 = fread('data/supptable/supp2_0422_reshaped.txt',data.table = F) 84 | supp2_epig = supp2 %>% filter(inxe=='Yes') 85 | supp2_epig = supp2_epig[grep('^Marker|Other',supp2_epig$ann_new,invert = T),] 86 | 87 | prognew = read_rds('stp2_epi/data/epiProgram/epigene_annotation.rds') 88 | 89 | 90 | 91 | 92 | 93 | ##### progscore statplot v3 ##### 94 | prognew = read_rds('stp2_epi/data/epiProgram/epigene_annotation_v5.rds') 95 | # epiprogscore = getscore(srtepi,split(prognew$gene,prognew$annotation)) 96 | # saveRDS(epiprogscore,'stp2_epi/data/epiProgram/epiprogscore_v5.rds') 97 | epiprogscore = read_rds('stp2_epi/data/epiProgram/epiprogscore_v5.rds') 98 | progorder = c('Cell adhesion','Mucosal defense','Inflammation','Cycling','DNA repair','Angiogenesis','Oncogenic TFs','Metastasis') 99 | progorder = c('Cell adhesion','Mucosal defense','Inflammation','Cycling','DNA repair','Angiogenesis','Oncogenic TFs','EMT') 100 | 101 | epiprogscore$stage1 = epiprogscore$stage1 %>% factor(levels = c('NOR','LGIN','HGIN','ESCC')) 102 | epiprogscore_aggr = epiprogscore %>% filter(cellID%in%epidt$cellID) %>% select(all_of(unique(prognew$annotation)), stage1, sp_stg, celltype) %>% 103 | gather('prog','score',all_of(unique(prognew$annotation))) 104 | epiprogscore_aggr = aggregate(list(score=epiprogscore_aggr$score),by=as.list(epiprogscore_aggr[,c('stage1','sp_stg','prog')]), mean) 105 | epiprogscore_aggr$prog = epiprogscore_aggr$prog %>% factor(levels = progorder) 106 | 107 | # progcol = c("#8DD3C7","#41ab5d","#BEBADA","#FB8072","#80B1D3","#FDB462","#B3DE69","#FCCDE5") 108 | # names(progcol) = c('Cell adhesion','Cycling','Mucosal defense','DNA repair','Oncogenic TFs','Inflammation','Angiogenesis','Metastasis') 109 | 110 | epiprogscore_aggr %>% 111 | ggplot(aes(stage1,score))+ 112 | theme1+scale_color_manual(values = progcol)+scale_fill_manual(values = progcol)+ 113 | RotatedAxis()+xlab('')+ylab('')+NoLegend()+ 114 | facet_wrap(~prog,scales = 'free',nrow = 2)+ 115 | scale_y_continuous(expand = expansion(c(0.05,0.1)))+ 116 | stat_compare_means(comparisons = makecomparison(4))+ 117 | ggbeeswarm::geom_quasirandom(aes(color=prog),shape=16)+ 118 | geom_boxplot(aes(fill=prog),alpha=0.3,outliers = F) 119 | ggsave('stp2_epi/plot/epiProgram/progscore_stage_all.pdf',width = 10,height = 7) 120 | 121 | 122 | 123 | 124 | 125 | ##### progscore bubble ##### 126 | prognew = read_rds('stp2_epi/data/epiProgram/epigene_annotation_v5.rds') 127 | progorder = c('Cell adhesion','Mucosal defense','Inflammation','Cycling','DNA repair','Angiogenesis','Oncogenic TFs','Metastasis') 128 | progorder = c('Cell adhesion','Mucosal defense','Inflammation','Cycling','DNA repair','Angiogenesis','Oncogenic TFs','EMT') 129 | 130 | epiprogscore = read_rds('stp2_epi/data/epiProgram/epiprogscore_v5.rds') 131 | epiprogscore$stage1 = epiprogscore$stage1 %>% factor(levels = c('NOR','LGIN','HGIN','ESCC')) 132 | 133 | ## aggr score 134 | tmp = epiprogscore %>% filter(cellID%in%epidt$cellID) %>% select(all_of(unique(prognew$annotation)), stage1, sp_stg, celltype) %>% 135 | gather('prog','score',all_of(unique(prognew$annotation))) 136 | # tmp1 = aggregate(list(score=tmp$score),by=as.list(tmp[,c('stage1','celltype','prog')]), mean) 137 | tmp1 = aggregate(list(score=tmp$score),by=as.list(tmp[,c('celltype','prog')]), mean) 138 | 139 | ## calc dot size 140 | { 141 | # tmp2 = aggregate(list(thresh=tmp$score),by=as.list(tmp[,c('prog','stage1'),drop=F]),function(x) mean(x)+0.5*sd(x)) 142 | # tmp2 = left_join(tmp,tmp2) %>% mutate(highexpr=ifelse(score>thresh,1,0)) 143 | # tmp2 = aggregate(list(prop=tmp2$highexpr),by=as.list(tmp2[,c('stage1','celltype','prog')]),mean) 144 | 145 | tmp2 = aggregate(list(thresh=tmp$score),by=as.list(tmp[,c('prog'),drop=F]),function(x) mean(x)+0.5*sd(x)) 146 | tmp2 = left_join(tmp,tmp2) %>% mutate(highexpr=ifelse(score>thresh,1,0)) 147 | tmp2 = aggregate(list(prop=tmp2$highexpr),by=as.list(tmp2[,c('celltype','prog')]),mean) 148 | 149 | # tmp2 = getprop(epidt$celltype,epidt[,c('stage1'),drop=F]) %>% gather('celltype','prop',unique(epidt$celltype)) %>% `colnames<-`(c('stage1','celltype','prop')) 150 | } 151 | 152 | ## plot bubble 153 | { 154 | tmp1_scale = tmp1 %>% spread(prog,score) 155 | tmp1_scale[,unique(prognew$annotation)] = tmp1_scale[,unique(prognew$annotation)] %>% scale 156 | tmp1_scale = tmp1_scale %>% gather('prog','score_scale',unique(prognew$annotation)) 157 | tmp1_scale = tmp1_scale %>% left_join(tmp2) 158 | tmp1_scale$prog = tmp1_scale$prog %>% factor(levels = rev(progorder)) %>% 159 | mapvalues(c('Adhesion','Cell cycle','Hypoxia & Inflammation'),c('Cell adhesion','Cycling','Inflammation')) 160 | tmp1_scale$celltype = tmp1_scale$celltype %>% factor(levels = c('Basal','Proliferation','Differentiation','Invasive')) %>% 161 | mapvalues(c('Proliferation','Differentiation'),c('Proliferative','Differentiated')) 162 | 163 | 164 | bubcol = c(colorRampPalette(c('#440154','#482576'))(40),colorRampPalette(c('#482576','#21908c','#bbdf27'))(20),colorRampPalette(c('#bbdf27','#fde725'))(40)) 165 | bubcol = c(rep('#440154',20),colorRampPalette(c('#440154','#21908c','#fde725'))(60),rep('#fde725',20)) 166 | 167 | # tmp1_scale %>% 168 | # mutate(prop = ifelse(prop>0.5,0.5,prop)) %>% 169 | # ggplot(aes(celltype,prog))+ 170 | # theme1+RotatedAxis()+xlab('')+ylab('')+ 171 | # # theme(panel.spacing.x = unit(0.2,'cm'))+ 172 | # scale_fill_gradientn(name = 'Z-score',colors = bubcol)+ 173 | # scale_size_continuous(name = 'Proportion',range = c(1,8),labels = function(x){return(paste0(x*100,'%'))})+ 174 | # geom_point(shape=21,aes(fill=score_scale,size=prop))+ 175 | # facet_wrap(~stage1,scales = 'free_x',nrow = 1) 176 | # ggsave('stp2_epi/plot/epiProgram/progscore_bubble.pdf',width = 8,height = 5) 177 | # 178 | # tmp1_scale %>% 179 | # mutate(prop = ifelse(prop>0.5,0.5,prop)) %>% 180 | # ggplot(aes(stage1,prog))+ 181 | # theme1+RotatedAxis()+xlab('')+ylab('')+ 182 | # # theme(panel.spacing.x = unit(0.2,'cm'))+ 183 | # scale_fill_gradientn(name = 'Z-score',colors = bubcol)+ 184 | # scale_size_continuous(name = 'Proportion',range = c(1,8),labels = function(x){return(paste0(x*100,'%'))})+ 185 | # geom_point(shape=21,aes(fill=score_scale,size=prop))+ 186 | # facet_wrap(~celltype,scales = 'free_x',nrow = 1) 187 | # ggsave('stp2_epi/plot/epiProgram/progscore_bubble.pdf',width = 8,height = 5) 188 | 189 | tmp1_scale %>% 190 | mutate(prop = ifelse(prop>0.5,0.5,prop)) %>% 191 | ggplot(aes(celltype,prog))+ 192 | theme1+RotatedAxis()+xlab('')+ylab('')+ 193 | # theme(panel.spacing.x = unit(0.2,'cm'))+ 194 | scale_fill_gradientn(name = 'Z-score',colors = bubcol)+ 195 | scale_size_continuous(name = 'Proportion',range = c(1,8),labels = function(x){return(paste0(x*100,'%'))})+ 196 | geom_point(shape=21,aes(fill=score_scale,size=prop)) 197 | ggsave('stp2_epi/plot/epiProgram/progscore_bubble.pdf',width = 5,height = 5) 198 | } 199 | 200 | 201 | 202 | 203 | 204 | 205 | ##### gene heatmap new ##### 206 | prognew = read_rds('stp2_epi/data/epiProgram/epigene_annotation_v5.rds') 207 | epiprogscore = read_rds('stp2_epi/data/epiProgram/epiprogscore_v5.rds') 208 | rownames(epiprogscore) = epiprogscore$cellID 209 | 210 | progorder = unique(prognew$annotation) 211 | progorder = c('Mucosal defense','Cell death','Metastasis','Hypoxia','Inflammation','DNA damage repair', 212 | 'Angiogenesis','Transcription regulation','Cell cycle','Stemness') 213 | progorder = c('Basal','Cell cycle','Mucosal defense','P53 related','Angiogenesis & Metastasis','Hypoxia & Inflammation', 214 | 'Transcription regulation','Cell death') 215 | progorder = c('Adhesion','Cell cycle','Mucosal defense','P53 pathway','Oncogenic TFs','Hypoxia & Inflammation','Angiogenesis','Metastasis') 216 | progorder = c('Cell adhesion','Mucosal defense','Inflammation','Cycling','DNA repair','Angiogenesis','Oncogenic TFs','Metastasis') 217 | progorder = c('Cell adhesion','Mucosal defense','Inflammation','Cycling','DNA repair','Angiogenesis','Oncogenic TFs','EMT') 218 | progorder = c('Cell adhesion','Mucosal defense','Cycling','Inflammation','DNA repair','EMT','Oncogenic TFs','Angiogenesis') 219 | 220 | geneord2 = prognew %>% filter(annotation%in%progorder) 221 | geneord2$annotation = geneord2$annotation %>% factor(levels = progorder) 222 | 223 | 224 | {# cell order 225 | ordcell2 = function(cid,progname){ 226 | score1 = epiprogscore[cid,progname,drop=F] 227 | cellord = apply(score1,1,function(x) return(progname[which.max(x)])) 228 | cellord = data.frame(cellID=cid,prog=cellord,score=matrixStats::rowMaxs(as.matrix(score1))) 229 | 230 | cellord$prog = cellord$prog %>% factor(levels = progorder) 231 | cellord = cellord %>% arrange(prog,score) 232 | 233 | return(cellord) 234 | } 235 | 236 | cellsubset = lapply(levels(epidt$stage1),function(x){ 237 | dt1 = epidt[epidt$stage1==x,] 238 | dt1$celltype = dt1$celltype %>% factor %>% droplevels() 239 | cellid = lapply(levels(dt1$celltype),function(y){ 240 | cellid1 = dt1$cellID[dt1$celltype==y] 241 | return(sample(cellid1,1e4)) 242 | }) %>% unlist() 243 | return(cellid) 244 | }) %>% unlist 245 | 246 | metadt = srtepi@meta.data[cellsubset,] 247 | metadt$celltype = metadt$celltype %>% factor(levels = c('Basal','Proliferation','Invasive','Differentiation')) 248 | 249 | # sort by stage, celltype 250 | # cellord = lapply(levels(metadt$stage1),function(x){ 251 | # dt1 = metadt %>% filter(stage1==x) 252 | # dt1$celltype = dt1$celltype %>% droplevels() 253 | # cellord1 = lapply(levels(dt1$celltype),function(y){ 254 | # dt2 = dt1 %>% filter(celltype==y) 255 | # pname = switch(y, 256 | # 'Differentiation'='Mucosal defense', 257 | # 'Basal'='Adhesion', 258 | # 'Proliferation'='Cell cycle', 259 | # 'Invasive'=setdiff(progorder,c('Mucosal defense','Adhesion','Cell cycle'))) 260 | # cellord2 = ordcell2(dt2$cellID,pname) 261 | # 262 | # cellord2 = data.frame(cellID=dt2$cellID,prog=pname[[1]]) 263 | # return(cellord2) 264 | # }) %>% Reduce(rbind,.) 265 | # }) %>% Reduce(rbind,.) 266 | 267 | # sort by celltype, stage 268 | cellord = lapply(levels(metadt$celltype),function(x){ 269 | dt1 = metadt %>% filter(celltype==x) 270 | dt1$stage1 = dt1$stage1 %>% droplevels() 271 | pname = switch(x, 272 | 'Differentiation'='Mucosal defense', 273 | 'Basal'='Adhesion', 274 | 'Proliferation'='Cell cycle', 275 | 'Invasive'=setdiff(progorder,c('Mucosal defense','Adhesion','Cell cycle'))) 276 | cellord1 = lapply(levels(dt1$stage1),function(y){ 277 | dt2 = dt1 %>% filter(stage1==y) 278 | # cellord2 = ordcell2(dt2$cellID,pname) 279 | cellord2 = data.frame(cellID=dt2$cellID,prog=pname[[1]]) 280 | return(cellord2) 281 | }) %>% Reduce(rbind,.) 282 | }) %>% Reduce(rbind,.) 283 | 284 | metadt = metadt %>% inner_join(cellord) %>% `rownames<-`(.$cellID) %>% .[cellord$cellID,] 285 | exprdt = srtepi@assays$SCT@data[geneord2$gene,metadt$cellID] %>% as.matrix() 286 | } 287 | 288 | {# gene order 289 | exprscore = exprdt %>% t %>% scale %>% t 290 | exprscore[exprscore<0] = 0 291 | exprscore = rowSums(exprscore) %>% data.frame(gene=names(.),score=.) 292 | geneord3 = geneord2 %>% left_join(exprscore) 293 | geneord3 = geneord3 %>% arrange(annotation,desc(score)) 294 | saveRDS(geneord3,'stp2_epi/data/epiProgram/epigene_heatorder.rds') 295 | } 296 | 297 | {# plot 298 | exprdt = exprdt[geneord3$gene,metadt$cellID] 299 | 300 | ## plot together 301 | cellann = columnAnnotation(df=metadt[,c('stage1','celltype'),drop=F], 302 | col=list(stage1=col_stage,celltype=epicol)) 303 | geneann = rowAnnotation(df=geneord3[,c('annotation'),drop=F], 304 | col=list(annotation=progcol)) 305 | 306 | exprdt = exprdt %>% t %>% scale %>% t 307 | tmp = ComplexHeatmap::Heatmap(exprdt,cluster_rows = F,cluster_columns = F,show_column_names = F, 308 | # row_split = geneord3$annotation, 309 | # column_split = metadt$celltype, 310 | row_gap = unit(1,'mm'), 311 | column_gap = unit(1,'mm'), 312 | top_annotation = cellann, 313 | left_annotation = geneann, 314 | heatmap_legend_param = list(title='Z-score',direction='vertical',title_position='topleft',legend_height=unit(3,'cm')), 315 | col = circlize::colorRamp2(c(-1.5,0,1.5),c('purple','black','yellow'))) 316 | 317 | pdf('stp2_epi/plot/epiProgram/progheatmap/progene_heatmap_subset2_ctstage.pdf',width = 15,height = 20) 318 | ComplexHeatmap::draw(tmp,heatmap_legend_side='right') 319 | dev.off() 320 | } 321 | 322 | 323 | 324 | 325 | 326 | ##### gene heatmap order by program ##### 327 | prognew = read_rds('stp2_epi/data/epiProgram/epigene_annotation_v3.rds') 328 | epiprogscore = read_rds('stp2_epi/data/epiProgram/epiprogscore_v3.rds') 329 | rownames(epiprogscore) = epiprogscore$cellID 330 | 331 | progorder = unique(prognew$annotation) 332 | progorder = c('Mucosal defense','Cell death','Metastasis','Hypoxia','Inflammation','DNA damage repair', 333 | 'Angiogenesis','Transcription regulation','Cell cycle','Stemness') 334 | progorder = c('Basal','Cell cycle','Mucosal defense','P53 related','Angiogenesis & Metastasis','Hypoxia & Inflammation', 335 | 'Transcription regulation','Cell death') 336 | 337 | geneord2 = prognew %>% filter(annotation%in%progorder) 338 | geneord2$annotation = geneord2$annotation %>% factor(levels = progorder) 339 | 340 | 341 | {# cell order 342 | ordcell = function(cid){ 343 | cellord = lapply(cid,function(x){ 344 | dt1 = epiprogscore[x,progorder] %>% unlist() 345 | rst = data.frame(cellID = x, prog=progorder[which.max(dt1)],score=dt1[which.max(dt1)]) 346 | return(rst) 347 | }) %>% Reduce(rbind,.) 348 | 349 | cellord$prog = cellord$prog %>% factor(levels = rev(progorder)) 350 | cellord = cellord %>% arrange(prog,score) 351 | 352 | return(cellord) 353 | } 354 | ordcell2 = function(cid,progname){ 355 | score1 = epiprogscore[cid,progname,drop=F] 356 | cellord = apply(score1,1,function(x) return(progname[which.max(x)])) 357 | cellord = data.frame(cellID=cid,prog=cellord,score=matrixStats::rowMaxs(as.matrix(score1))) 358 | 359 | cellord$prog = cellord$prog %>% factor(levels = rev(progorder)) 360 | cellord = cellord %>% arrange(prog,score) 361 | 362 | return(cellord) 363 | } 364 | 365 | cellsubset = lapply(unique(epidt$stage1),function(x){ 366 | cellid = epidt$cellID[epidt$stage1==x] 367 | return(sample(cellid,1e5)) 368 | }) %>% unlist 369 | # cellsubset = epidt$cellID 370 | 371 | metadt = srtepi@meta.data[cellsubset,] 372 | metadt$celltype = metadt$celltype %>% factor(levels = c('Basal','Proliferation','Invasive','Differentiation')) 373 | 374 | # sort by stage 375 | cellord = lapply(levels(metadt$stage1),function(x){ 376 | dt1 = metadt %>% filter(stage1==x) 377 | 378 | cellord1 = ordcell(dt1$cellID) 379 | return(cellord1) 380 | }) %>% Reduce(rbind,.) 381 | 382 | # sort by stage, celltype 383 | cellord = lapply(levels(metadt$stage1),function(x){ 384 | dt1 = metadt %>% filter(stage1==x) 385 | dt1$celltype = dt1$celltype %>% droplevels() 386 | cellord1 = lapply(levels(dt1$celltype),function(y){ 387 | dt2 = dt1 %>% filter(celltype==y) 388 | pname = switch(y, 389 | 'Differentiation'='Mucosal defense', 390 | 'Basal'='Stemness', 391 | 'Proliferation'='Cell cycle', 392 | 'Invasive'=setdiff(progorder,c('Mucosal defense','Stemness','Cell cycle'))) 393 | cellord2 = ordcell2(dt2$cellID,pname) 394 | return(cellord2) 395 | }) %>% Reduce(rbind,.) 396 | }) %>% Reduce(rbind,.) 397 | 398 | # sort by celltype, stage 399 | cellord = lapply(levels(metadt$celltype),function(x){ 400 | dt1 = metadt %>% filter(celltype==x) 401 | dt1$stage1 = dt1$stage1 %>% droplevels() 402 | pname = switch(x, 403 | 'Differentiation'='Mucosal defense', 404 | 'Basal'='Stemness', 405 | 'Proliferation'='Cell cycle', 406 | 'Invasive'=setdiff(progorder,c('Mucosal defense','Stemness','Cell cycle'))) 407 | cellord1 = lapply(levels(dt1$stage1),function(y){ 408 | dt2 = dt1 %>% filter(stage1==y) 409 | cellord2 = ordcell2(dt2$cellID,pname) 410 | return(cellord2) 411 | }) %>% Reduce(rbind,.) 412 | }) %>% Reduce(rbind,.) 413 | 414 | metadt = metadt %>% inner_join(cellord) %>% `rownames<-`(.$cellID) %>% .[cellord$cellID,] 415 | exprdt = srtepi@assays$SCT@data[geneord2$gene,metadt$cellID] %>% as.matrix() 416 | 417 | 418 | ## plot together 419 | cellann = columnAnnotation(df=metadt[,c('prog','celltype','stage1'),drop=F], 420 | col=list(prog=setNames(brewer.pal(10,'Set3'),levels(geneord2$annotation)),celltype=epicol,stage1=col_stage)) 421 | # cellann = columnAnnotation(df=metadt[,c('stage1'),drop=F], 422 | # col=list(stage1=col_stage)) 423 | exprdt = exprdt %>% t %>% scale %>% t 424 | # exprdt = exprdt %>% apply(1,function(x){rescale(x,c(-2,2))}) %>% t 425 | tmp = ComplexHeatmap::Heatmap(exprdt,cluster_rows = F,cluster_columns = F,show_column_names = F, 426 | row_split = geneord2$annotation, 427 | column_split = metadt$celltype, 428 | row_gap = unit(2,'mm'), 429 | column_gap = unit(2,'mm'), 430 | top_annotation = cellann, 431 | heatmap_legend_param = list(title='Z-score',direction='vertical',title_position='topleft',legend_height=unit(3,'cm')), 432 | col = circlize::colorRamp2(c(-2,0,2),c('#377eb8','#f0f0f0','#e41a1c'))) 433 | 434 | pdf('stp2_epi/plot/epiProgram/progheatmap/progene_heatmap_subset_ctstage.pdf',width = 20,height = 20) 435 | ComplexHeatmap::draw(tmp,heatmap_legend_side='right') 436 | dev.off() 437 | 438 | 439 | ## plot per stage 440 | lapply(unique(metadt$stage1),function(x){ 441 | metadt = metadt %>% filter(stage1==x) 442 | exprdt = exprdt[,metadt$cellID] 443 | 444 | cellann = columnAnnotation(df=metadt[,c('prog','celltype','stage1'),drop=F], 445 | col=list(prog=setNames(brewer.pal(10,'Set3'),levels(geneord2$annotation)),celltype=epicol,stage1=col_stage)) 446 | exprdt = exprdt %>% t %>% scale %>% t 447 | tmp = ComplexHeatmap::Heatmap(exprdt,cluster_rows = F,cluster_columns = F,show_column_names = F, 448 | row_split = geneord2$annotation, 449 | column_split = metadt$stage1, 450 | top_annotation = cellann, 451 | heatmap_legend_param = list(title='Z-score',direction='vertical',title_position='topleft',legend_height=unit(3,'cm')), 452 | col = circlize::colorRamp2(c(-2,0,2),c('#377eb8','#f0f0f0','#e41a1c'))) 453 | 454 | fname = paste0('stp2_epi/plot/epiProgram/progene_heatmap_',x,'.pdf') 455 | pdf(fname,width = 20,height = 20) 456 | ComplexHeatmap::draw(tmp,heatmap_legend_side='right') 457 | dev.off() 458 | }) 459 | } 460 | 461 | 462 | 463 | 464 | -------------------------------------------------------------------------------- /stp3_CN_kmeans.R: -------------------------------------------------------------------------------- 1 | source('~/xenium/header.R') 2 | source('~/xenium/xenium_function.R') 3 | 4 | 5 | 6 | # input ##### 7 | km_test = read_rds('stp3_tme/data/Cellular_Neighborhood/cellfrac_50/kmeans_15.rds') 8 | 9 | allmeta_m = read_rds('stp5_batch2process/data/allcell/allmerge_allmeta.rds') 10 | 11 | epidt = read_rds('stp2_epi/data/epiid_correctstage.rds') 12 | crds = read_rds('stp1_summary/data/allsample_coords.rds') 13 | epipoly = read_rds('stp3_tme/data/epiregion/allsample_poly.rds') 14 | allborder = read_rds('stp3_tme/data/epiborder/allborder.rds') 15 | 16 | gridid = read_rds('stp1_summary/data/allsample_gridid.rds') 17 | 18 | allmk = c('EPCAM','SFN','KRT4','KRT5', # epi 19 | 'FN1','DCN','COL1A1','COL1A2','COL3A1','COL6A1', # fibro 20 | 'PLN','MYH11', # muscle 21 | 'VWF','PECAM1','ENG','CDH5', # endo 22 | 'PTPRC', # immune 23 | 'CD2','CD3D','CD3E', # t cell 24 | 'CD1E','CD68','LYZ','CD14','FCGR3A', # myeloid 25 | 'MS4A2','KIT','CPA3', # mast 26 | 'CD19','CD79A','MS4A1', # B 27 | 'JCHAIN','IGHG2' # plasma 28 | ) 29 | 30 | epimarker = c('KRT15','MYC', # QP 31 | 'TOP2A','MKI67', # CY 32 | 'ANXA1','KRT13','KRT4','CSTA','S100A8','TRIM29', # MD 33 | 'SPRR3','SPRR2E','ECM1','EMP1', # TD 34 | 'HIF1A','SERPINE1','VEGFA', # HY 35 | 'KRT17', # RO 36 | 'SPP1','CES1', 'AKR1C3','AKR1C2','CBR1', # DO 37 | 'HLA-DRB1','HLA-DQB1'# AP 38 | ) 39 | 40 | coltmp = c('#80b1d3','#8dd3c7','#ccebc5','#fccde5','red','#fdb462','#bc80bd','#b3de69','#ffffb3','#bebada','#d9d9d9') 41 | coltmp = c('#fdb462','#8dd3c7','#ccebc5','#fccde5','red','#80b1d3','#bc80bd','#ffff33','#bebada','#a6761d','#d9d9d9') 42 | coltmp = c('#ccebc5','#c6dbef','#238b45','#de0c62','#ff9408','#666666','#793c1b','#BC9DCC','#8dd3c7','#d9d9d9','#bbbbbb') 43 | names(coltmp) = c('Normal epi','Normal epi & Invasive epi','Normal epi & NF','Invasive epi','CAF-epi niche','CAF','TLS','NF','Endo & Fibro','Gland','Muscle') 44 | coltmp = coltmp[c('CAF','TLS','NF','Endo & Fibro','Gland','Muscle','Normal epi','Normal epi & Invasive epi','Normal epi & NF','Invasive epi','CAF-epi niche')] 45 | 46 | 47 | epicol = c('#0339f8','#ff9408','#75bbfd','#de0c62') 48 | names(epicol) = c('Basal','Proliferation','Differentiation','Invasive') 49 | 50 | progcol = c('Cell adhesion'="#0339f8", 51 | 'Cycling'="#ff9408", 52 | 'Mucosal defense'="#75bbfd", 53 | 'Inflammation'="#B3DE69", 54 | 'DNA repair'="#a6eee6", 55 | 'Oncogenic TFs'="#9467bd", 56 | 'Angiogenesis'="#41ab5d", 57 | 'Metastasis'="#ff0000") 58 | 59 | getp = function(srt, features, group.by){ 60 | p1 = (DimPlot(srt, group.by = group.by, label = T)+NoLegend())+DimPlot(srt, group.by = group.by, label = T) 61 | p2 = DotPlot(srt, features = features, group.by = group.by)+RotatedAxis() 62 | return(p1/p2) 63 | } 64 | 65 | srtpreproc = function(srt){ 66 | srt = SCTransform(srt) 67 | srt = RunPCA(srt) 68 | srt = RunUMAP(srt, dims = 1:10) 69 | srt = FindNeighbors(srt,dims = 1:10) 70 | srt = FindClusters(srt, resolution = seq(0.1,1,0.1)) 71 | return(srt) 72 | } 73 | 74 | 75 | allborder = read_rds('stp3_tme/data/epiborder/allborder.rds') 76 | t1dist = read_rds('stp3_tme/data/epi_borderdist/T1epi_borderdist_v2.rds') 77 | t1border = read_rds('stp3_tme/data/epiborder/T1border_v2.rds') 78 | t3dist = read_rds('stp3_tme/data/epi_borderdist/T3ESCC_borderdist_v2.rds') 79 | t3border = read_rds('stp3_tme/data/epiborder/T3ESCCborder_v2.rds') 80 | t4dist = read_rds('stp3_tme/data/epi_borderdist/T4partE_borderdist_v2.rds') 81 | t4dtlist = read_rds('stp3_tme/data/epiborder/T4partEborder_v2.rds') 82 | 83 | 84 | 85 | 86 | # func ##### 87 | plotcluster = function(meta, groupby, 88 | fixed=T, color=NULL, pt.size=0.2,pt.shape=16,bgcol = 'white',ptlevels=NULL, 89 | reversex=F, reversey=F,flip=F, 90 | highlight = NULL,xintercept=NULL,yintercept=NULL, 91 | zoomin = NULL, cellborder = F, areaalpha = 0.5, cellgap = 0, cellradius = 0, linewidth = 0.5, celllinecol=NULL, 92 | gridline=F,gridcol='black',gridgap=100,gridlinewidth=0.03,axis=F,plotborder=T){ 93 | ### coords 94 | { 95 | coords = meta[,c('x','y',groupby)] 96 | rownames(coords) = rownames(meta) 97 | coords[,groupby] = coords[,groupby] %>% factor() 98 | 99 | if(flip){ 100 | coords = coords %>% dplyr::rename(c('x'='y','y'='x')) 101 | } 102 | 103 | xmin = min(coords$x) 104 | ymin = min(coords$y) 105 | xmax = max(coords$x) 106 | ymax = max(coords$y) 107 | 108 | if(length(zoomin)==4){ 109 | zoomin = zoomin %>% as.numeric() 110 | coords = coords %>% filter(x>=zoomin[1],x<=zoomin[2],y>=zoomin[3],y<=zoomin[4]) 111 | xmin = zoomin[1] 112 | xmax = zoomin[2] 113 | ymin = zoomin[3] 114 | ymax = zoomin[4] 115 | } 116 | 117 | if(!is.null(highlight)){ 118 | # coords[rownames(coords)%in%highlight,groupby] = 'group_1' 119 | levels(coords[,groupby]) = levels(coords[,groupby]) %>% append('Others') 120 | coords[!rownames(coords)%in%highlight,groupby] = 'Others' 121 | coords[,groupby] = coords[,groupby] %>% droplevels() 122 | 123 | getPalette = colorRampPalette(brewer.pal(8, "Set1")) 124 | 125 | if(is.null(color)){ 126 | cname = unique(coords[,groupby]) %>% setdiff('Others') 127 | num = cname %>% length() 128 | color = c(getPalette(num),'#d3d3d3') 129 | names(color) = c(cname,'Others') 130 | }else{ 131 | nms = c(names(color),'Others') 132 | color=c(color,'#d3d3d3') 133 | names(color) = nms 134 | } 135 | 136 | if(is.null(ptlevels)){ 137 | ptlevels = levels(coords[,groupby]) 138 | }else{ 139 | ptlevels = unique(c(ptlevels,'Others')) 140 | } 141 | } 142 | } 143 | 144 | 145 | ### make plot 146 | { 147 | p_bg = 148 | coords %>% 149 | arrange(groupby) %>% 150 | ggplot(aes(x,y))+ 151 | geom_rect(aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax), fill=NA,color=NA)+ 152 | theme1+ 153 | theme(panel.background = element_rect(fill=bgcol))+ 154 | labs(color=groupby) 155 | 156 | if(gridline){ 157 | glx = (xmax - xmin)%/%gridgap 158 | if(glx>0){ 159 | glx1 = c(0,glx+1)*gridgap+xmin 160 | glx = (0:(glx+1))*gridgap+xmin 161 | }else{ 162 | glx1 = c(0,glx+1)*gridgap+xmin 163 | glx=NULL 164 | } 165 | gly = (ymax - ymin)%/%gridgap 166 | if(gly>0){ 167 | gly1 = c(0,gly+1)*gridgap+ymin 168 | gly = (0:(gly+1))*gridgap+ymin 169 | }else{ 170 | gly1 = c(0,gly+1)*gridgap+ymin 171 | gly=NULL 172 | } 173 | 174 | p_bg = p_bg+ 175 | xlab('')+ylab('')+ 176 | scale_x_continuous(expand = c(0,0))+ 177 | scale_y_continuous(expand = c(0,0))+ 178 | geom_hline(yintercept = gly,linewidth=gridlinewidth,color=gridcol)+ 179 | geom_vline(xintercept = glx,linewidth=gridlinewidth,color=gridcol) 180 | } 181 | 182 | 183 | if(!is.null(ptlevels)){ 184 | p_layer = lapply(rev(ptlevels), function(x){ 185 | dt = coords[coords[,groupby]==x,] 186 | rst = geom_point(data=dt, aes(x,y,color=.data[[groupby]]),shape=pt.shape,size=pt.size) 187 | return(rst) 188 | }) 189 | p_layer = append(list(p_bg),p_layer) 190 | p = Reduce('+',p_layer) 191 | }else{p = p_bg+geom_point(aes(color=.data[[groupby]]),shape=pt.shape,size = pt.size)} 192 | 193 | 194 | if(cellborder){ 195 | bd = xenium@images$fov@boundaries$segmentation@polygons 196 | bd = bd[rownames(coords)] %>% 197 | lapply(function(x){ 198 | dt = x@Polygons[[1]]@coords %>% data.frame() 199 | dt$ID = x@ID 200 | return(dt) 201 | }) %>% rbindlist() %>% data.frame() 202 | 203 | celltype = data.frame(rownames(coords), coords[,3]) 204 | colnames(celltype) = c('ID',groupby) 205 | bd = bd %>% left_join(celltype) 206 | 207 | if(flip){ 208 | bd = bd %>% dplyr::rename(c('x'='y','y'='x')) 209 | } 210 | 211 | if(!is.null(ptlevels)){ 212 | if(is.null(celllinecol)){ 213 | p_layer1 = lapply(rev(ptlevels), function(x){ 214 | dt = bd[bd[,groupby]==x,] 215 | rst = ggforce::geom_shape(data=dt, 216 | aes(x,y,fill = .data[[groupby]],group=ID,color=.data[[groupby]]), 217 | linewidth = linewidth, 218 | alpha = areaalpha, 219 | expand = cellgap, 220 | radius = cellradius) 221 | return(rst) 222 | }) 223 | }else{ 224 | p_layer1 = lapply(rev(ptlevels), function(x){ 225 | dt = bd[bd[,groupby]==x,] 226 | rst = ggforce::geom_shape(data=dt, 227 | aes(x,y,fill = .data[[groupby]],group=ID), 228 | color=celllinecol, 229 | linewidth = linewidth, 230 | alpha = areaalpha, 231 | expand = cellgap, 232 | radius = cellradius) 233 | return(rst) 234 | }) 235 | } 236 | 237 | p_layer2 = list() 238 | p_layer2[1] = p_layer[1] 239 | p_layer2[1:length(ptlevels)*2] = p_layer[1:length(ptlevels)+1] 240 | p_layer2[1:length(ptlevels)*2+1] = p_layer1 241 | p = Reduce('+',p_layer2) 242 | }else{ 243 | if(is.null(celllinecol)){ 244 | p = p+ 245 | ggforce::geom_shape(data=bd, 246 | aes(x,y,fill = .data[[groupby]],group=ID,color=.data[[groupby]]), 247 | linewidth = linewidth, 248 | alpha = areaalpha, 249 | expand = cellgap, 250 | radius = cellradius) 251 | }else{ 252 | p = p+ 253 | ggforce::geom_shape(data=bd, 254 | aes(x,y,fill = .data[[groupby]],group=ID), 255 | color=celllinecol, 256 | linewidth = linewidth, 257 | alpha = areaalpha, 258 | expand = cellgap, 259 | radius = cellradius) 260 | } 261 | } 262 | } 263 | if(!is.null(color)){ 264 | p = p+scale_color_manual(values = color)+scale_fill_manual(values = color) 265 | } 266 | if(reversex){ 267 | p=p+scale_x_reverse() 268 | } 269 | if(reversey){ 270 | p=p+scale_y_reverse() 271 | } 272 | if(fixed){ 273 | p = p+coord_fixed() 274 | } 275 | if(!is.null(xintercept)){ 276 | p = p+geom_vline(xintercept = xintercept, color='black', linetype='dashed') 277 | } 278 | if(!is.null(yintercept)){ 279 | p = p+geom_hline(yintercept = yintercept, color='black', linetype='dashed') 280 | } 281 | if(!axis){ 282 | p = p+theme(axis.line = element_blank(),axis.ticks = element_blank(),axis.text = element_blank()) 283 | } 284 | if(plotborder){ 285 | p = p+theme(panel.border = element_rect(color='black',linewidth = 1,fill=NA))+xlab('')+ylab('') 286 | } 287 | } 288 | 289 | p = p+guides(color=guide_legend(override.aes = list(size=3))) 290 | return(p) 291 | } 292 | 293 | plotkmeans = function(frac,meta, returnheat=T){ 294 | plotcol = c('#440154','#984ea3','#3288bd','#35b779','#fde725','red') 295 | ctlevel = c('Non-invasive','Invasive','CAF','T','B','Mye','NF','Endo','Gland','Mus') 296 | 297 | dt = frac %>% gather('ct','frac',-coreid) %>% left_join(meta) 298 | dt_aggr = aggregate(list(frac=dt$frac),by=as.list(dt[,c('ct','cluster')]),mean) 299 | 300 | if(returnheat){ 301 | # heat 302 | dt_aggr1 = dt_aggr %>% spread(ct,frac) 303 | rownames(dt_aggr1)=dt_aggr1$cluster %>% as.character() 304 | dt_aggr1=dt_aggr1[,c('cluster',ctlevel)] 305 | p = ComplexHeatmap::pheatmap(dt_aggr1[,-1],color=plotcol,cluster_cols = F,cluster_rows = F) 306 | }else{ 307 | dt_prop = aggregate(list(prop=(dt$frac>0.05)),by=as.list(dt[,c('ct','cluster')]),mean) 308 | 309 | dt_plot = left_join(dt_aggr,dt_prop) 310 | dt_plot$cluster = dt_plot$cluster %>% factor(levels = levels(meta$cluster)) 311 | dt_plot$ct = dt_plot$ct %>% factor(levels = ctlevel) 312 | p = dt_plot %>% 313 | ggplot(aes(ct,cluster))+ 314 | theme1+rotate_x_text()+ 315 | scale_fill_gradientn(colors = plotcol)+ 316 | geom_point(aes(fill=frac,size=prop),shape=21) 317 | } 318 | return(p) 319 | } 320 | 321 | cellfrac_all = lapply(grep('T',unique(allmeta_m$sid),value=T), function(slideid){ 322 | rst = read_rds(paste0('stp3_tme/data/Cellular_Neighborhood/cellfrac_50/',slideid,'_cellfrac.rds')) 323 | }) %>% Reduce(rbind,.) 324 | rownames(cellfrac_all) = cellfrac_all$coreid 325 | 326 | km_test = read_rds('stp3_tme/data/Cellular_Neighborhood/cellfrac_50/kmeans_15.rds') 327 | allmeta_m = read_rds('stp5_batch2process/data/allcell/allmerge_allmeta.rds') 328 | 329 | clevels = c('Normal epi','Normal epi & Invasive epi','Normal epi & NF','Invasive epi','CAF-epi niche','CAF','TLS','NF','Endo & Fibro','Gland','Muscle') 330 | clevels1 = c('n','n-inv','n-nf','inv','ce','caf','tls','nf','en-f','gld','mus') 331 | 332 | 333 | # summary ##### 334 | cnrst = read_rds('stp3_tme/data/Cellular_Neighborhood/cellfrac_50/clusterrst_all.rds') 335 | propplot2(cnrst,coltmp,'cluster')+scale_y_continuous(expand = c(0,0)) 336 | ggsave('stp3_tme/plot/CN_primtype/cluster_prop_bar.pdf',width = 6,height = 6) 337 | 338 | clusprop = getprop(cnrst$cluster,cnrst[,c('sampleID','stage')]) %>% gather('cluster','prop',all_of(names(coltmp))) 339 | colnames(clusprop) = c('sampleID','stage','cluster','prop') 340 | 341 | clusprop %>% 342 | ggplot(aes(stage,prop))+ 343 | theme1+NoLegend()+ 344 | xlab('')+ylab('Cluster proportion')+ 345 | scale_fill_manual(values = coltmp)+scale_color_manual(values = coltmp)+ 346 | scale_y_continuous(expand = expansion(c(0,0.1)))+ 347 | # stat_compare_means(comparisons = makecomparison(4))+ 348 | geom_pwc(method = 'dunn_test')+ 349 | ggbeeswarm::geom_quasirandom(aes(color=cluster))+ 350 | geom_boxplot(aes(fill=cluster),outlier.color = NA,alpha=0.5)+ 351 | facet_wrap(~cluster,scales = 'free') 352 | ggsave('stp3_tme/plot/CN_primtype/cluster_prop_box.pdf',width = 12,height = 11) 353 | 354 | {# test 355 | dunnrst = lapply(unique(clusprop$cluster),function(ct){ 356 | print(as.character(ct)) 357 | dt1= clusprop %>% filter(cluster==ct) 358 | dt1$stage = dt1$stage %>% droplevels() 359 | if(length(unique(dt1$stage))<3){ 360 | stg1 = unique(dt1$stage)[1] 361 | stg2 = unique(dt1$stage)[2] 362 | rst = wilcox.test(dt1$prop[dt1$stage==stg1],dt1$prop[dt1$stage==stg2]) 363 | rst = data.frame(Comparison=paste0(stg1,' - ',stg2),P.unadj=rst$p.value,celltype=ct) 364 | }else{ 365 | rst = FSA::dunnTest(prop~stage,data=dt1)$res 366 | rst$celltype=ct 367 | rst = rst[,c(1,3,5)] 368 | } 369 | return(rst) 370 | }) %>% Reduce(rbind,.) 371 | } 372 | 373 | 374 | 375 | 376 | 377 | # cluster heatmap ##### 378 | cellfrac_all = lapply(grep('T',unique(allmeta_m$sid),value=T), function(slideid){ 379 | rst = read_rds(paste0('stp3_tme/data/Cellular_Neighborhood/cellfrac_50/',slideid,'_cellfrac.rds')) 380 | }) %>% Reduce(rbind,.) 381 | rownames(cellfrac_all) = cellfrac_all$coreid 382 | cnrst = read_rds('stp3_tme/data/Cellular_Neighborhood/cellfrac_50/clusterrst_all.rds') 383 | 384 | pdf('stp3_tme/plot/CN_primtype/cluster_heat.pdf',width = 5,height = 7.5) 385 | plotkmeans(cellfrac_all,cnrst) 386 | dev.off() 387 | 388 | 389 | 390 | 391 | # spat plot ##### 392 | cnrst = read_rds('stp3_tme/data/Cellular_Neighborhood/cellfrac_50/clusterrst_all.rds') 393 | 394 | ## T1S2 395 | { 396 | dttmp = cnrst %>% filter(sid=='T1S2') 397 | plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,ptlevels = rev(names(coltmp))) 398 | ggsave('stp3_tme/plot/CN_primtype/T1S2_clustspat.png',width = 20,height = 10) 399 | plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,ptlevels = rev(names(coltmp)), 400 | highlight = dttmp$coreid[dttmp$cluster%in%c('CAF','CAF-epi niche','Invasive epi')]) 401 | ggsave('stp3_tme/plot/CN_primtype/T1S2_clustspat_highlight.png',width = 20,height = 10) 402 | 403 | p = plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,highlight = dttmp$coreid[dttmp$cluster%in%c('CAF','CAF-epi niche','Invasive epi')])/ 404 | plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,highlight = dttmp$coreid[dttmp$cluster=='Invasive epi'])/ 405 | plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,highlight = dttmp$coreid[dttmp$cluster=='CAF'])/ 406 | plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,highlight = dttmp$coreid[dttmp$cluster=='CAF-epi niche']) 407 | ggsave('stp3_tme/plot/CN_primtype/T1S2_clustspat_split.png',p,width = 20,height = 30,limitsize=F) 408 | } 409 | 410 | ## T1S3 411 | { 412 | dttmp = cnrst %>% filter(sid=='T1S3') 413 | plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,ptlevels = rev(names(coltmp))) 414 | ggsave('stp3_tme/plot/CN_primtype/T1S3_clustspat.png',width = 20,height = 10) 415 | plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,ptlevels = rev(names(coltmp)), 416 | highlight = dttmp$coreid[dttmp$cluster%in%c('CAF','CAF-epi niche','Invasive epi')]) 417 | ggsave('stp3_tme/plot/CN_primtype/T1S3_clustspat_highlight.png',width = 20,height = 10) 418 | 419 | p = plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,highlight = dttmp$coreid[dttmp$cluster%in%c('CAF','CAF-epi niche','Invasive epi')])/ 420 | plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,highlight = dttmp$coreid[dttmp$cluster=='Invasive epi'])/ 421 | plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,highlight = dttmp$coreid[dttmp$cluster=='CAF'])/ 422 | plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,highlight = dttmp$coreid[dttmp$cluster=='CAF-epi niche']) 423 | ggsave('stp3_tme/plot/CN_primtype/T1S3_clustspat_split.png',p,width = 20,height = 30,limitsize=F) 424 | } 425 | 426 | ## T3S1 427 | { 428 | dttmp = cnrst %>% filter(sid=='T3S1') 429 | plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,ptlevels = rev(names(coltmp))) 430 | ggsave('stp3_tme/plot/CN_primtype/T3S1_clustspat.png',width = 20,height = 10) 431 | plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,ptlevels = rev(names(coltmp)), 432 | highlight = dttmp$coreid[dttmp$cluster%in%c('CAF','CAF-epi niche','Invasive epi')]) 433 | ggsave('stp3_tme/plot/CN_primtype/T3S1_clustspat_highlight.png',width = 20,height = 10) 434 | 435 | p = plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,highlight = dttmp$coreid[dttmp$cluster%in%c('CAF','CAF-epi niche','Invasive epi')])/ 436 | plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,highlight = dttmp$coreid[dttmp$cluster=='Invasive epi'])/ 437 | plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,highlight = dttmp$coreid[dttmp$cluster=='CAF'])/ 438 | plotcluster(dttmp,'cluster',reversex = T,color = coltmp,bgcol = 'white',gridline = T,highlight = dttmp$coreid[dttmp$cluster=='CAF-epi niche']) 439 | ggsave('stp3_tme/plot/CN_primtype/T3S1_clustspat_split.png',p,width = 20,height = 30,limitsize=F) 440 | } 441 | 442 | 443 | 444 | 445 | -------------------------------------------------------------------------------- /stp3_fib_fibepidist.R: -------------------------------------------------------------------------------- 1 | source('~/xenium/header.R') 2 | source('~/xenium/xenium_function.R') 3 | library(scatterpie) 4 | 5 | 6 | 7 | 8 | # input ##### 9 | xe1_3 = read_rds('data/xenium_seurat_processed/xenium_t1_s3_seurat.rds') 10 | xe1_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s3_meta.rds') 11 | 12 | xe3_1 = read_rds('data/xenium_seurat_processed/xenium_t3_s1_seurat.rds') 13 | xe3_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t3_s1_meta.rds') 14 | 15 | xe4_2 = read_rds('data/xenium_seurat_processed/xenium_t4_s2_seurat.rds') 16 | xe4_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t4_s2_meta.rds') 17 | 18 | xe6_1 = read_rds('data/xenium_seurat_processed/xenium_t6_s1_seurat.rds') 19 | xe6_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s1_meta.rds') 20 | 21 | srtfib = read_rds('stp3_tme/data/fib/fibro_all_seurat.rds') 22 | srtfib@meta.data = read_rds('stp3_tme/data/fib/fibro_all_meta.rds') 23 | srtfib$stage1 = srtfib$stage1 %>% factor(levels = c('NOR','LGIN','HGIN','ESCC')) 24 | srtepi = read_rds('data/xenium_seurat_processed/ct_epi_srt_2.rds') 25 | fibtoepi = read_rds('stp3_tme/data/fib-epi_dist/fib-epi_dist.rds') 26 | 27 | epidt = read_rds('stp2_epi/data/epiid_correctstage.rds') 28 | crds = read_rds('stp1_summary/data/allsample_coords.rds') 29 | epipoly = read_rds('stp3_tme/data/epiregion/allsample_poly.rds') 30 | allborder = read_rds('stp3_tme/data/epiborder/allborder.rds') 31 | 32 | epianno = fread('stp1_summary/data/xenium_epi.csv') 33 | 34 | epigeneannot = fread('stp3_tme/data/hm_gene_anno.txt') 35 | 36 | probelist = fread('data/probe_list.txt') 37 | 38 | gridid = read_rds('stp1_summary/data/allsample_gridid.rds') 39 | 40 | epiblacklist = fread('stp2_epi/data/gene_otherTissue.txt') 41 | epiblacklist = probelist[probelist$gene%in%epiblacklist$Gene_otherTissue,] 42 | 43 | 44 | allmk = c('EPCAM','SFN','KRT4','KRT5', # epi 45 | 'FN1','DCN','COL1A1','COL1A2','COL3A1','COL6A1', # fibro 46 | 'PLN','MYH11', # muscle 47 | 'VWF','PECAM1','ENG','CDH5', # endo 48 | 'PTPRC', # immune 49 | 'CD2','CD3D','CD3E', # t cell 50 | 'CD1E','CD68','LYZ','CD14','FCGR3A', # myeloid 51 | 'MS4A2','KIT','CPA3', # mast 52 | 'CD19','CD79A','MS4A1', # B 53 | 'JCHAIN','IGHG2' # plasma 54 | ) 55 | 56 | epimarker = c('KRT15','MYC', # QP 57 | 'TOP2A','MKI67', # CY 58 | 'ANXA1','KRT13','KRT4','CSTA','S100A8','TRIM29', # MD 59 | 'SPRR3','SPRR2E','ECM1','EMP1', # TD 60 | 'HIF1A','SERPINE1','VEGFA', # HY 61 | 'KRT17', # RO 62 | 'SPP1','CES1', 'AKR1C3','AKR1C2','CBR1', # DO 63 | 'HLA-DRB1','HLA-DQB1'# AP 64 | ) 65 | 66 | epicol = c('#0339f8','#ff9408','#75bbfd','#de0c62') 67 | names(epicol) = c('Basal','Proliferation','Differentiation','Invasive') 68 | 69 | progcol = c('Cell adhesion'="#0339f8", 70 | 'Cycling'="#ff9408", 71 | 'Mucosal defense'="#75bbfd", 72 | 'Inflammation'="#B3DE69", 73 | 'DNA repair'="#a6eee6", 74 | 'Oncogenic TFs'="#9467bd", 75 | 'Angiogenesis'="#41ab5d", 76 | 'Metastasis'="#ff0000") 77 | 78 | getp = function(srt, features, group.by){ 79 | p1 = (DimPlot(srt, group.by = group.by, label = T)+NoLegend())+DimPlot(srt, group.by = group.by, label = T) 80 | p2 = DotPlot(srt, features = features, group.by = group.by)+RotatedAxis() 81 | return(p1/p2) 82 | } 83 | 84 | srtpreproc = function(srt){ 85 | srt = SCTransform(srt) 86 | srt = RunPCA(srt) 87 | srt = RunUMAP(srt, dims = 1:10) 88 | srt = FindNeighbors(srt,dims = 1:10) 89 | srt = FindClusters(srt, resolution = seq(0.1,1,0.1)) 90 | return(srt) 91 | } 92 | 93 | 94 | 95 | supp2 = fread('data/supptable/supp2_0422_reshaped.txt',data.table = F) 96 | supp2_epig = supp2 %>% filter(inxe=='Yes') 97 | supp2_epig = supp2_epig[grep('^Marker|Other',supp2_epig$ann_new,invert = T),] 98 | 99 | prognew = read_rds('stp2_epi/data/epiProgram/epigene_annotation_v4.rds') 100 | 101 | 102 | allborder = read_rds('stp3_tme/data/epiborder/allborder.rds') 103 | t1dist = read_rds('stp3_tme/data/epi_borderdist/T1epi_borderdist_v2.rds') 104 | t1border = read_rds('stp3_tme/data/epiborder/T1border_v2.rds') 105 | t3dist = read_rds('stp3_tme/data/epi_borderdist/T3ESCC_borderdist_v2.rds') 106 | t3border = read_rds('stp3_tme/data/epiborder/T3ESCCborder_v2.rds') 107 | t4dist = read_rds('stp3_tme/data/epi_borderdist/T4partE_borderdist_v2.rds') 108 | t4dtlist = read_rds('stp3_tme/data/epiborder/T4partEborder_v2.rds') 109 | 110 | 111 | 112 | 113 | # fib dist expr ##### 114 | fibtoinv = read_rds('stp3_tme/data/fib-epi_dist/fib-inv_dist.rds') 115 | grp = fibtoepi$fibdist1 116 | groupdist = 1 117 | fibtoepi$group1 = (grp%/%groupdist+1)*groupdist 118 | fibtoepi$group = paste0(fibtoepi$group1,'um') %>% 119 | factor(levels = paste0(sort(unique(fibtoepi$group1)),'um')) 120 | 121 | 122 | srtfib = read_rds('stp3_tme/data/fib/fibro_all_seurat.rds') 123 | srtfib@meta.data = read_rds('stp3_tme/data/fib/fibro_all_meta.rds') 124 | srtfib$stage1 = srtfib$stage1 %>% factor(levels = c('NOR','LGIN','HGIN','ESCC')) 125 | srtfib$celltype = srtfib$cellType_merge %>% mapvalues('NF-PI16','NF') 126 | 127 | cafgn = c('FAP','MMP1','MMP11','POSTN') 128 | expr = getexpr(srtfib,cafgn) %>% left_join(srtfib@meta.data) %>% 129 | filter(stage1%in%c('HGIN','ESCC')) %>% inner_join(fibtoinv) 130 | 131 | expr_aggr = aggregate(list(expression=expr$expression),by=as.list(expr[,c('group1','stage1','celltype','gene')]),mean) 132 | expr_aggr = expr_aggr %>% 133 | filter(group1<150) %>% 134 | # filter(gene=='POSTN') %>% 135 | filter(celltype=='NF') 136 | 137 | # trend test 138 | library(trend) 139 | 140 | trendtest = lapply(unique(expr_aggr$stage1),function(stg){ 141 | print(stg) 142 | dt1 = expr_aggr %>% filter(stage1==stg) 143 | rst1 = lapply(unique(dt1$gene),function(g){ 144 | print(g) 145 | dt2 = dt1 %>% filter(gene==g) 146 | dt2 = dt2 %>% arrange(group1) 147 | testrst = mk.test(dt2$expr) 148 | rst2 = data.frame(sval=testrst$estimates[1],zval=testrst$statistic,pval=testrst$p.value,gene=g,stage1=stg) 149 | }) %>% Reduce(rbind,.) 150 | }) %>% Reduce(rbind,.) 151 | trendtest$pval_adj = p.adjust(trendtest$pval,method = 'fdr') 152 | labdt = expr_aggr 153 | labdt = aggregate(list(yloc=labdt$expr),by=as.list(labdt[,c('stage1','gene')]),min) %>% left_join(trendtest) 154 | 155 | expr_aggr %>% 156 | # filter(group1>20) %>% 157 | ggplot(aes(group1,expression))+ 158 | theme1+xlab('Distance to Invasive cell')+ylab('Gene expression')+ 159 | scale_color_manual(values = c('POSTN'='#e4282a','FAP'='#ffdd2e','MMP11'='#f3a058','MMP1'='#206ed0'))+ 160 | geom_point(aes(group=gene,color=gene),shape=16)+ 161 | geom_smooth(method = 'gam')+ 162 | geom_text(data=labdt,x=5,aes(y=yloc,label=paste0('Trend test\np = ',signif(pval_adj,2),'\nz = ',signif(zval,2))),hjust=0,vjust=0)+ 163 | # stat_cor(method = 'spearman')+ 164 | facet_wrap(~stage1+gene,nrow = 2,scales = 'free') 165 | ggsave('stp3_tme/plot/fibepi_dist/Nf_cafexpr_epidist_trend.pdf',width = 12,height = 6) 166 | 167 | 168 | 169 | 170 | 171 | # progscore ##### 172 | {## cor with dist 173 | epicaf = read_rds('stp3_tme/data/fib-epi_dist/epi-CAF_dist.rds') 174 | 175 | prognew = read_rds('stp2_epi/data/epiProgram/epigene_annotation_v5.rds') 176 | gnlist = prognew 177 | gnlist = split(gnlist$gene,gnlist$annotation) 178 | 179 | exprdt = read_rds('stp2_epi/data/epiProgram/epiprogscore_v5.rds') 180 | rownames(exprdt) = exprdt$cellID 181 | exprdt = exprdt[,c('cellID','pid','stage1',names(gnlist)),drop=F] %>% inner_join(epicaf) 182 | exprdt = exprdt %>% gather('prog','score',all_of(names(gnlist))) 183 | 184 | grp = exprdt$epidist1 185 | groupdist = 1 186 | exprdt$group1 = (grp%/%groupdist+1)*groupdist 187 | exprdt$group1 = ifelse(exprdt$group1>150,min(exprdt$group1[exprdt$group1>150]),exprdt$group1) 188 | exprdt$group = paste0(exprdt$group1,'um') %>% 189 | factor(levels = paste0(sort(unique(exprdt$group1)),'um')) 190 | 191 | exprdt_aggr = aggregate(list(score=exprdt$score),by=as.list(exprdt[,c('stage1','prog','group1','group')]),mean) 192 | } 193 | 194 | 195 | ## plot ##### 196 | { 197 | exprdt_aggr %>% 198 | ggplot(aes(group1,score))+ 199 | theme1+ 200 | geom_point()+ 201 | facet_wrap(~stage1+prog,nrow = 4,scales = 'free') 202 | 203 | ## trend test, mann kendall 204 | library(trend) 205 | 206 | trendtest = lapply(unique(exprdt_aggr$stage1),function(stg){ 207 | print(stg) 208 | dt1 = exprdt_aggr %>% filter(stage1==stg) 209 | rst1 = lapply(unique(dt1$prog),function(prg){ 210 | print(prg) 211 | dt2 = dt1 %>% filter(prog==prg) 212 | testrst = mk.test(dt2$score) 213 | rst2 = data.frame(rval=testrst$estimates[3],zval=testrst$statistic,pval=testrst$p.value,prog=prg,stage1=stg) 214 | }) %>% Reduce(rbind,.) 215 | }) %>% Reduce(rbind,.) 216 | trendtest$pval_adj = p.adjust(trendtest$pval,method = 'fdr') 217 | 218 | labdt = exprdt_aggr 219 | labdt$group1 = labdt$group1 %>% as.character %>% as.numeric() 220 | labdt = aggregate(list(yloc=labdt$score),by=as.list(labdt[,c('prog'),drop=F]),max) %>% left_join(trendtest) 221 | 222 | exprdt_aggr %>% 223 | ggplot(aes(group1,score))+ 224 | theme1+theme(panel.spacing.y = unit(0.1,'cm'),panel.border = element_rect(fill=NA,color='black',linewidth = 0.5),strip.background.y = element_rect(fill='grey'))+ 225 | xlab('Distance')+ylab('Program score')+ 226 | scale_y_continuous(expand = expansion(c(0.05,0.1)))+ 227 | scale_color_manual(values = progcol)+scale_fill_manual(values = progcol)+ 228 | geom_point(aes(color=prog),shape=16,size=0.5)+ 229 | geom_smooth(linewidth=0.8)+ 230 | # stat_cor(method = 'kendal')+ 231 | geom_text(data=labdt,x=0.1,aes(y=yloc*1.1,label=paste0('r = ',signif(rval,2),'\nP = ',signif(pval_adj,2))),hjust=0,vjust=1)+ 232 | facet_grid(prog~stage1,scales = 'free') 233 | ggsave('stp3_tme/plot/fibepi_dist/epiprog_corwithcafdist.pdf',width = 10,height = 9) 234 | } 235 | 236 | 237 | 238 | 239 | # JAG1 NOTCH1 expr ##### 240 | {## cor with dist 241 | epicaf = read_rds('stp3_tme/data/fib-epi_dist/epi-CAF_dist.rds') 242 | 243 | tmp = srtepi 244 | tmp@meta.data = tmp@meta.data %>% left_join(epicaf) %>% `rownames<-`(.$cellID) 245 | 246 | grp = tmp$epidist1 247 | groupdist = 1 248 | tmp$group1 = (grp%/%groupdist+1)*groupdist 249 | tmp$group1 = ifelse(tmp$group1>150,min(tmp$group1[tmp$group1>150]),tmp$group1) 250 | tmp$group = paste0(tmp$group1,'um') %>% 251 | factor(levels = paste0(sort(unique(tmp$group1)),'um')) 252 | 253 | isout = function(x){ 254 | # return(x < quantile(x,0.25) - 1.5 * IQR(x) | x > quantile(x,0.75) + 1.5 * IQR(x)) 255 | return(x < mean(x)-1*sd(x) | x > mean(x)+1*sd(x)) 256 | } 257 | 258 | # gnexpr = aggrExpr(tmp,c('NOTCH1','JAG1'),aggrby = tmp@meta.data[,c('sp_stg','stage1','group1','group','pid')]) %>% gather('gene','expr',NOTCH1,JAG1) 259 | # gnexpr = aggrExpr(tmp,c('NOTCH1','JAG1'),aggrby = tmp@meta.data[,c('stage1','group1','group')]) %>% gather('gene','expr',NOTCH1,JAG1) 260 | gnexpr = getexpr(tmp,c('NOTCH1','JAG1')) %>% `colnames<-`(c('cellID','gene','expr')) %>% left_join(tmp@meta.data) 261 | gnexpr = gnexpr %>% filter(pid!='T2') 262 | gnexpr = aggregate(list(expr=gnexpr$expr),by=as.list(gnexpr[,c('stage1','group1','group','gene')]),mean) 263 | gnexpr = gnexpr %>% 264 | # group_by(interaction(gene,stage1,group)) %>% 265 | # mutate(outlier=ifelse(isout(expr),pid,NA)) %>% 266 | # filter(pid%in%c('T2')==F) %>% 267 | # filter(is.na(outlier)) %>% 268 | filter(stage1%in%c('HGIN','ESCC')) %>% 269 | filter(group1<=200) 270 | # gnexpr = getexpr(tmp,c('NOTCH1','JAG1')) %>% `colnames<-`(c('cellID','gene','expr')) %>% left_join(tmp@meta.data) 271 | 272 | # trend test 273 | library(trend) 274 | 275 | trendtest = lapply(unique(gnexpr$stage1),function(stg){ 276 | print(stg) 277 | dt1 = gnexpr %>% filter(stage1==stg) 278 | rst1 = lapply(unique(dt1$gene),function(g){ 279 | print(g) 280 | dt2 = dt1 %>% filter(gene==g) 281 | dt2 = dt2 %>% arrange(group1) 282 | testrst = mk.test(dt2$expr) 283 | rst2 = data.frame(sval=testrst$estimates[1],zval=testrst$statistic,pval=testrst$p.value,gene=g,stage1=stg) 284 | }) %>% Reduce(rbind,.) 285 | }) %>% Reduce(rbind,.) 286 | trendtest$pval_adj = p.adjust(trendtest$pval,method = 'fdr') 287 | labdt = gnexpr 288 | labdt = aggregate(list(yloc=labdt$expr),by=as.list(labdt[,c('stage1','gene')]),min) %>% left_join(trendtest) 289 | 290 | gnexpr %>% 291 | ggplot(aes(group1,expr))+ 292 | theme1+xlab('Distance to CAF')+ylab('Gene expression')+ 293 | geom_point(aes(color=gene))+ 294 | # scale_color_gradientn(colours = distcol,name='Distance to CAF')+ 295 | scale_color_manual(values = c('JAG1'='#7fbc41','NOTCH1'='#cab2d6'))+ 296 | geom_smooth(method='gam')+ 297 | geom_text(data=labdt,x=5,aes(y=yloc,label=paste0('Trend test\np = ',signif(pval_adj,2),'\nz = ',signif(zval,2))),hjust=0,vjust=0)+ 298 | # stat_cor(method='spearman')+ 299 | facet_wrap(~stage1+gene,scales = 'free') 300 | ggsave('stp3_tme/plot/fibepi_dist/jagnotch_corwithcafdist.pdf',width = 7,height = 6) 301 | 302 | # pval 303 | # lapply(c('JAG1','NOTCH1'),function(gn){ 304 | # dt1 = gnexpr %>% filter(gene==gn) 305 | # lapply(c('HGIN','ESCC'),function(stg){ 306 | # dt2 = dt1 %>% filter(stage1==stg) 307 | # dt2$group1 = dt2$group1 %>% as.character %>% as.numeric() 308 | # tst = cor.test(dt2$expr,dt2$group1,method='spearman') 309 | # return(data.frame(r=tst$estimate,pv=tst$p.value,stage=stg,gene=gn)) 310 | # }) %>% Reduce(rbind,.) 311 | # }) %>% Reduce(rbind,.) 312 | } 313 | 314 | 315 | 316 | -------------------------------------------------------------------------------- /stp3_mye_analysis.R: -------------------------------------------------------------------------------- 1 | source('~/xenium/header.R') 2 | source('~/xenium/xenium_function.R') 3 | 4 | 5 | 6 | ##### input ##### 7 | job({ 8 | xe1_1 = read_rds('data/xenium_seurat_processed/xenium_t1_s1_seurat.rds') 9 | xe1_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s1_meta.rds') 10 | xe1_2 = read_rds('data/xenium_seurat_processed/xenium_t1_s2_seurat.rds') 11 | xe1_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s2_meta.rds') 12 | xe1_3 = read_rds('data/xenium_seurat_processed/xenium_t1_s3_seurat.rds') 13 | xe1_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t1_s3_meta.rds') 14 | 15 | xe2_1 = read_rds('data/xenium_seurat_processed/xenium_t2_s1_seurat.rds') 16 | xe2_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s1_meta.rds') 17 | xe2_2 = read_rds('data/xenium_seurat_processed/xenium_t2_s2_seurat.rds') 18 | xe2_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s2_meta.rds') 19 | xe2_3 = read_rds('data/xenium_seurat_processed/xenium_t2_s3_seurat.rds') 20 | xe2_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t2_s3_meta.rds') 21 | 22 | xe3_1 = read_rds('data/xenium_seurat_processed/xenium_t3_s1_seurat.rds') 23 | xe3_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t3_s1_meta.rds') 24 | xe3_2 = read_rds('data/xenium_seurat_processed/xenium_t3_s2_seurat.rds') 25 | xe3_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t3_s2_meta.rds') 26 | 27 | xe4_1 = read_rds('data/xenium_seurat_processed/xenium_t4_s1_seurat.rds') 28 | xe4_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t4_s1_meta.rds') 29 | xe4_2 = read_rds('data/xenium_seurat_processed/xenium_t4_s2_seurat.rds') 30 | xe4_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t4_s2_meta.rds') 31 | 32 | xe5_1 = read_rds('data/xenium_seurat_processed/xenium_t5_s1_seurat.rds') 33 | xe5_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s1_meta.rds') 34 | xe5_2 = read_rds('data/xenium_seurat_processed/xenium_t5_s2_seurat.rds') 35 | xe5_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s2_meta.rds') 36 | xe5_3 = read_rds('data/xenium_seurat_processed/xenium_t5_s3_seurat.rds') 37 | xe5_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s3_meta.rds') 38 | xe5_2_split = read_rds('data/xenium_seurat_processed/xenium_t5_s2_split_seurat.rds') 39 | xe5_2_split@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s2_split_meta.rds') 40 | xe5_3_split = read_rds('data/xenium_seurat_processed/xenium_t5_s3_split_seurat.rds') 41 | xe5_3_split@meta.data = read_rds('data/xenium_seurat_processed/xenium_t5_s3_split_meta.rds') 42 | 43 | xe6_1 = read_rds('data/xenium_seurat_processed/xenium_t6_s1_seurat.rds') 44 | xe6_1@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s1_meta.rds') 45 | xe6_2 = read_rds('data/xenium_seurat_processed/xenium_t6_s2_seurat.rds') 46 | xe6_2@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s2_meta.rds') 47 | xe6_3 = read_rds('data/xenium_seurat_processed/xenium_t6_s3_seurat.rds') 48 | xe6_3@meta.data = read_rds('data/xenium_seurat_processed/xenium_t6_s3_meta.rds') 49 | },import = 'auto') 50 | 51 | allmeta = read_rds('data/xenium_seurat_processed/allcell_meta.rds') 52 | 53 | srtmye = read_rds('data/xenium_seurat_processed/ct_mye_srt.rds') 54 | srtmye@meta.data = read_rds('data/xenium_seurat_processed/ct_mye_meta.rds') 55 | 56 | 57 | myemarker = c('SFN','DCN','PLN','VWF','CD19','CD68','CD2', # pan marker 58 | 'MS4A2','KIT','CPA3', # mast 59 | 'GZMB','JCHAIN', # pDC 60 | 'CD1C','CD1E','FCER1A', # APC 61 | 'CCR7', # tDC 62 | 'FCN1','S100A9','S100A8','FCGR3A', # Mono 63 | 'IL1B', # Macro 64 | 'LYVE1','C1QC','C1QB','C1QA','SPP1','FN1' # Macro 65 | ) 66 | 67 | primcol2 = c('#1f78b4','#ffc089','#B5EFB5','#793c1b', 68 | '#6a3d9a','#333333','#ffff33','#df928e') 69 | names(primcol2) = c('B','Endo','Fib','Gland','Mye','Mus','T','Epi') 70 | 71 | primcol3 = c('#793c1b','#333333','#ffc089','#df928e', 72 | '#1f78b4','#6a3d9a','#df65b0','#1cbe4f','#e31a1c','#ffdcbd', 73 | '#BDCDFF','#ffff68','#325A9B','#AAF400','#ff9344') 74 | names(primcol3) = c('Gland','Mus','Endo','Epi', 75 | 'B','Mye','NF-PI16','NF','CAF','T-others', 76 | 'T-naive','CD8T-eff','CD8T-ex','CD4T-fh','CD4T-reg') 77 | areacol = c('#fccde5','#80b1d3') 78 | 79 | tmpcol = c('#df928e','#888888','#1cbe4f','#238b45','#B5EFB5', 80 | '#df65b0','#ef3b2c','#a50f15') 81 | names(tmpcol) = c('Epi','Others','CD8T-n/m','CD8T-eff','CD8T-ex', 82 | 'CD4T-fh','CD4T-reg','CD4T-n/m') 83 | 84 | allcellcol = c('Basal'='#0339f8','Proliferation'='#ff9408','Differentiation'='#75bbfd', 85 | 'Terminal'='#4682B4','HT_Prolif'='#ff9408','HT_Diff/Term'='#4682B4', 86 | 'Invasive'='#de0c62','T cells'='#96f97b','B cells'='#028f1e', 87 | 'Myeloid cells'='#7e1e9c','Fibroblast'='#696969','CAF'='#FFFFFF', 88 | 'Endothelial cells'='#7b002c','Gland cells'='#c69f59','Myocytes'='#1b2431') 89 | 90 | myecol = c(Mono='#7fc97f',Mac='#1f78b4','Mac-2'='#e31a1c','Mac-3'='#ffff33',DC='#beaed4',Mast='#b15928') 91 | 92 | 93 | getp = function(srt, features, group.by){ 94 | p1 = (DimPlot(srt, group.by = group.by, label = T)+NoLegend())+DimPlot(srt, group.by = group.by, label = T) 95 | p2 = DotPlot(srt, features = features, group.by = group.by)+RotatedAxis() 96 | return(p1/p2) 97 | } 98 | 99 | srtpreproc = function(srt){ 100 | srt = SCTransform(srt) 101 | srt = RunPCA(srt) 102 | srt = RunUMAP(srt, dims = 1:10) 103 | srt = FindNeighbors(srt,dims = 1:10) 104 | srt = FindClusters(srt, resolution = seq(0.1,1,0.1)) 105 | return(srt) 106 | } 107 | 108 | 109 | 110 | 111 | ##### umap ##### 112 | {## subset 113 | myesbst = sample(srtmye$cellID, dim(srtmye)[2]/6) 114 | myesbst = subset(srtmye, cells = myesbst) 115 | 116 | myesbst = SCTransform(myesbst, ncells=3000) 117 | myesbst = RunPCA(myesbst) 118 | myesbst = RunUMAP(myesbst, dims=1:10) 119 | 120 | mydimp(myesbst,groupby = 'cellType_merge',cols = brewer.pal(9,'Set1'))+NoLegend() 121 | } 122 | 123 | 124 | mydimp(srtmye,groupby = 'cellType_merge',cols = brewer.pal(9,'Set1'))+NoLegend() 125 | 126 | 127 | 128 | 129 | ##### dotp ##### 130 | myemarker2 = c('MS4A2','KIT','CPA3', # mast 131 | 'JCHAIN','CD1C','FCER1A','CCR7', 132 | 'FCN1','S100A9','S100A8','FCGR3A', # Mono 133 | 'LYVE1','C1QC','C1QB','C1QA','IL1B','SPP1') 134 | mydotp(srtmye,myemarker2,'cellType_merge',cols = rev(brewer.pal(9,'RdBu')))+coord_flip()+RotatedAxis() 135 | ggsave('stp3_tme/plot/mye_analysis/mye_marker_dot.pdf',width = 6,height = 4) 136 | DotPlot(srtmye,features = myemarker2,group.by = 'cellType_merge',cols = 'RdBu')+RotatedAxis()+xlab('')+ylab('') 137 | ggsave('stp3_tme/plot/mye_analysis/mye_marker_dot.pdf',width = 7,height = 3.2) 138 | 139 | 140 | 141 | ##### proportion ##### 142 | propplot2(srtmye@meta.data,myecol,groupby = 'stage1') 143 | 144 | 145 | propmye = getprop(srtmye$cellType_merge,srtmye@meta.data[,c('stage1','sp_stg')]) %>% 146 | gather('celltype','prop',all_of(unique(srtmye$cellType_merge))) 147 | 148 | propmye %>% 149 | ggplot(aes(group_1,prop))+ 150 | theme1+ 151 | theme(axis.ticks.x = element_blank())+rotate_x_text(angle = 30)+ 152 | NoLegend()+ 153 | ylab('Proportion')+xlab('')+ 154 | scale_y_continuous(labels = c('0','0.5','1.0'),breaks = seq(0,1,0.5),limits = c(0,1.1))+ 155 | scale_fill_manual(values = myecol)+scale_color_manual(values = myecol)+ 156 | stat_compare_means(comparisons = makecomparison(4))+ 157 | facet_wrap(~celltype)+ 158 | geom_boxplot(aes(fill=celltype),alpha=0.5,outlier.color = NA)+ 159 | geom_quasirandom(aes(color=celltype)) 160 | 161 | 162 | 163 | 164 | 165 | 166 | ##### proportion split by area ##### 167 | meta=srtmye@meta.data 168 | meta$cellType_merge = meta$cellType_merge %>% factor %>% droplevels() 169 | propnum = table(meta[,c('cellType_merge','stage1','inf','sp_stg')]) %>% data.frame() 170 | 171 | totalnum = aggregate(list(total=propnum$Freq),by=as.list(propnum[,c('stage1','inf','sp_stg')]),sum) %>% 172 | filter(total>0) 173 | propnum = propnum %>% inner_join(totalnum) 174 | propnum$prop = propnum$Freq/propnum$total 175 | propnum$prop[propnum$inf=='Out'] = -propnum$prop[propnum$inf=='Out'] 176 | 177 | propnum_mean = aggregate(list(meanvalue=propnum$prop),by=as.list(propnum[,c('stage1','inf','cellType_merge')]),median) 178 | 179 | propnum %>% 180 | ggplot(aes(stage1,prop))+ 181 | theme1+theme(axis.line.x = element_blank(),axis.ticks.x = element_blank())+ 182 | NoLegend()+ 183 | facet_wrap(~cellType_merge,nrow = 1)+ 184 | scale_fill_manual(values = myecol)+scale_color_manual(values = myecol)+ 185 | # scale_y_continuous(expand = c(0,0.3))+ 186 | # stat_mean(geom = 'bar',aes(fill=cellType_merge),alpha=0.5)+ 187 | xlab('')+ylab('Cell proportion')+ 188 | geom_bar(data=propnum_mean,aes(stage1,meanvalue,fill=cellType_merge),color='black',stat='identity',position = position_dodge(),alpha=0.5)+ 189 | geom_point(aes(color=cellType_merge),size=1.5,position=position_jitter())+ 190 | stat_summary(data=propnum[propnum$inf=='Infiltrated',],geom = 'errorbar', 191 | fun.max = function(x){quantile(x,0.75)}, 192 | fun.min = function(x){quantile(x,0.25)}, 193 | width=0.5,linewidth=0.6, 194 | aes(group=cellType_merge),position = position_dodge(width = 0.9))+ 195 | stat_compare_means(data=propnum[propnum$inf=='Infiltrated',], 196 | comparisons = makecomparison(4),tip.length = 0,label.y = c(1,1.2,1.4))+ 197 | stat_summary(data=propnum[propnum$inf=='Out',],geom = 'errorbar', 198 | fun.max = function(x){quantile(x,0.75)}, 199 | fun.min = function(x){quantile(x,0.25)}, 200 | width=0.5,linewidth=0.6, 201 | aes(group=cellType_merge),position = position_dodge(width = 0.9))+ 202 | stat_compare_means(data=propnum[propnum$inf=='Out',], 203 | comparisons = makecomparison(4),tip.length = 0,label.y = c(-1.2,-1.4,-1.6),vjust = 1.6)+ 204 | geom_hline(yintercept = 0) 205 | 206 | ggsave('stp3_tme/plot/mye_analysis/prop_mye_splitbyarea.pdf',width = 15,height = 6) 207 | 208 | 209 | 210 | 211 | 212 | ##### spatial ##### 213 | { 214 | objlist = setNames(list('xe1_1','xe1_2','xe1_3','xe2_1','xe2_2','xe2_3','xe3_1','xe3_2', 215 | 'xe4_1','xe4_2','xe5_1','xe5_2_split','xe5_3_split','xe6_1','xe6_2','xe6_3'), 216 | c('T1S1','T1S2','T1S3','T2S1','T2S2','T2S3','T3S1','T3S2', 217 | 'T4S1','T4S2','T5S1','T5S2','T5S3','T6S1','T6S2','T6S3')) 218 | lapply(names(objlist), function(x){ 219 | coltmp = c(myecol,'Epi'='#dddddd','Others'='#888888') 220 | dt = get(objlist[[x]]) 221 | 222 | dt$cellType_merge = 'Others' 223 | dt$cellType_merge[dt$ct3=='Epi'] = 'Epi' 224 | dt = renewMeta(dt, srtmye@meta.data[,c('cellID','cellType_merge')]) 225 | 226 | flip=F;reversex=F;reversey=F 227 | g1 = c('T1S1','T1S2','T1S3','T3S1','T3S2','T4S1') 228 | g2 = c('T2S1','T2S2','T2S3') 229 | g3 = c('T4S2') 230 | g4 = c('T5S1','T5S2','T5S3') 231 | g5 = c('T6S1') 232 | g6 = c('T6S2','T6S3') 233 | if(x%in%g1){flip=T;reversex=T} 234 | if(x%in%g2){flip=T} 235 | if(x%in%g3){flip=T;reversey=T} 236 | if(x%in%g5){reversey=T} 237 | if(x%in%g6){reversex=T} 238 | 239 | fname = paste0('stp3_tme/plot/mye_spatial/',x,'.png') 240 | p = xedimplot(dt, groupby = 'cellType_merge',flip = flip,ptlevels = c(names(myecol),'Epi','Others'), 241 | reversex = reversex,reversey = reversey,color = coltmp,bgcol = 'black') 242 | ggsave(fname,p,width=40,height=20) 243 | }) 244 | } 245 | 246 | 247 | 248 | 249 | 250 | ##### density by area ##### 251 | {## grid dens 252 | gridid = read_rds('stp1_summary/data/allsample_gridid.rds') 253 | myedt = inner_join(gridid[,c('cellID','gridid')],srtmye@meta.data) 254 | 255 | gridmyenum = table(myedt$inf,myedt$gridid,myedt$stage1,myedt$sp_stg, myedt$cellType_merge) %>% 256 | data.frame() %>% `colnames<-`(c('inf','gridid','stage','sp_stg','celltype','cellnum')) %>% 257 | filter(cellnum>0) 258 | 259 | meanmyenum = aggregate(list(meannum=gridmyenum$cellnum), 260 | by=as.list(gridmyenum[,c('inf','stage','sp_stg')]), 261 | mean) 262 | meanmyenum_ct = aggregate(list(meannum=gridmyenum$cellnum), 263 | by=as.list(gridmyenum[,c('inf','stage','sp_stg','celltype')]), 264 | mean) 265 | meanmyenum_ct %>% 266 | ggplot(aes(stage,meannum))+ 267 | theme1+theme(axis.ticks.x = element_blank())+ 268 | xlab('')+ylab('')+ 269 | NoLegend()+RotatedAxis()+ 270 | # scale_fill_manual(values = epicol)+scale_color_manual(values = epicol)+ 271 | scale_y_continuous(limits = function(x){ 272 | mindt = 0 273 | if(x[2]<=30){ 274 | maxdt = 30 275 | }else{ 276 | maxdt = 60 277 | } 278 | return(c(mindt,maxdt)) 279 | },breaks = function(x){ 280 | if(x[2]<=30){ 281 | dt = seq(0,30,10) 282 | }else{ 283 | dt = seq(0,60,20) 284 | } 285 | return(dt) 286 | },expand = c(0,0))+ 287 | # facet_wrap(~inf,scales = 'free_y',nrow = 1)+ 288 | facet_wrap(~inf+celltype,scales = 'free_y',nrow=2)+ 289 | ylim(c(0,NA))+stat_compare_means(comparisons = makecomparison(4))+ 290 | geom_jitter(size=2,aes(color=inf))+ 291 | geom_boxplot(aes(fill=inf),outlier.color = NA,alpha=0.5) 292 | } 293 | 294 | 295 | {## density per cell area 296 | dens_all = read_rds('stp1_summary/data/allsample_density_primtype.rds') 297 | dens_all = allmeta %>% left_join(dens_all[,c('cellID','dens')]) 298 | dens_mye = dens_all %>% filter(ct3=='Mye') %>% left_join(srtmye@meta.data[,c('cellID','inf')]) 299 | meandens_mye = aggregate(list(dens=dens_mye$dens),by=as.list(dens_mye[,c('sp_stg','stage1','inf')]),mean) 300 | 301 | meandens_mye %>% 302 | ggplot(aes(stage1,dens))+ 303 | theme1+theme(axis.ticks.x = element_blank())+ 304 | xlab('')+ylab('Cell density')+ 305 | NoLegend()+rotate_x_text(angle = 30)+ 306 | stat_compare_means(comparisons = makecomparison(4))+ 307 | scale_fill_manual(values = areacol)+scale_color_manual(values = areacol)+ 308 | scale_y_continuous(expand = c(0.1,0.3))+ 309 | facet_wrap(~inf,scales = 'free',nrow = 2)+ 310 | geom_jitter(size=2,aes(color=inf))+ 311 | geom_boxplot(aes(fill=inf),outlier.color = NA,alpha=0.5) 312 | ggsave('stp3_tme/plot/mye_analysis/mye_density_area.pdf',width = 3,height = 7) 313 | } 314 | 315 | 316 | {## density per cell subtype 317 | objlist = setNames(list('xe1_1','xe1_2','xe1_3','xe2_1','xe2_2','xe2_3','xe3_1','xe3_2', 318 | 'xe4_1','xe4_2','xe5_1','xe5_2_split','xe5_3_split','xe6_1','xe6_2','xe6_3'), 319 | c('T1S1','T1S2','T1S3','T2S1','T2S2','T2S3','T3S1','T3S2', 320 | 'T4S1','T4S2','T5S1','T5S2','T5S3','T6S1','T6S2','T6S3')) 321 | job({ 322 | dens_mye = lapply(names(objlist), function(x){ 323 | message(paste0('running sample ',x)) 324 | crd = crds[[x]] 325 | crd = crd[,c('x','y')] 326 | meta = srtmye@meta.data[srtmye$cellID%in%rownames(crd),] 327 | 328 | dens_ct = lapply(unique(meta$cellType_merge),function(y){ 329 | message(paste0('running ct ',y)) 330 | meta1 = meta[meta$cellType_merge==y,] 331 | crd1 = crd[rownames(crd)%in%meta1$cellID,] 332 | 333 | cdens = calcdens(crd1, 100) 334 | rst = data.frame(cellID=rownames(crd1),dens = cdens, cellType_merge = y) 335 | return(rst) 336 | }) %>% Reduce(rbind,.) 337 | 338 | return(dens_ct) 339 | }) %>% Reduce(rbind,.) 340 | saveRDS(dens_mye,'stp3_tme/data/mye/mye_subtypeDensity.rds') 341 | },import = 'auto') 342 | 343 | dens_mye = srtmye@meta.data %>% left_join(dens_mye[,c('cellID','dens')]) 344 | 345 | ## subtype 346 | meandens_mye_sub = aggregate(list(dens=dens_mye$dens),by=as.list(dens_mye[,c('sp_stg','stage1','cellType_merge')]),mean) 347 | meandens_mye_sub %>% 348 | ggplot(aes(stage1,dens))+ 349 | theme1+theme(axis.ticks.x = element_blank())+ 350 | xlab('')+ylab('Cell density')+ 351 | NoLegend()+rotate_x_text(angle = 30)+ 352 | stat_compare_means(comparisons = makecomparison(4))+ 353 | scale_fill_manual(values = myecol)+scale_color_manual(values = myecol)+ 354 | scale_y_continuous(expand = c(0.1,0.3))+ 355 | facet_wrap(~cellType_merge,scales = 'free',nrow = 1)+ 356 | geom_jitter(size=2,aes(color=cellType_merge))+ 357 | geom_boxplot(aes(fill=cellType_merge),outlier.color = NA,alpha=0.5) 358 | ggsave('stp3_tme/plot/mye_analysis/mye_density_subtype.pdf',width = 13,height = 3.5) 359 | 360 | ## subtype area 361 | meandens_mye_sub_area = aggregate(list(dens=dens_mye$dens),by=as.list(dens_mye[,c('sp_stg','stage1','cellType_merge','inf')]),mean) 362 | meandens_mye_sub_area %>% 363 | ggplot(aes(stage1,dens))+ 364 | theme1+theme(axis.ticks.x = element_blank())+ 365 | xlab('')+ylab('Cell density')+ 366 | NoLegend()+rotate_x_text(angle = 30)+ 367 | stat_compare_means(comparisons = makecomparison(4))+ 368 | scale_fill_manual(values = myecol)+scale_color_manual(values = myecol)+ 369 | scale_y_continuous(expand = c(0.1,0.3))+ 370 | facet_wrap(~inf+cellType_merge,scales = 'free',nrow = 2)+ 371 | geom_jitter(size=2,aes(color=cellType_merge))+ 372 | geom_boxplot(aes(fill=cellType_merge),outlier.color = NA,alpha=0.5) 373 | } 374 | 375 | 376 | 377 | 378 | ##### density compare with t cell ##### 379 | {## density per cell area 380 | dens_all = read_rds('stp1_summary/data/allsample_density_primtype.rds') 381 | dens_all = allmeta %>% left_join(dens_all[,c('cellID','dens')]) 382 | inftype = srtmye@meta.data[,c('cellID','inf')] %>% `colnames<-`(c('cellID','epiarea')) 383 | inftype = srttc@meta.data[,c('cellID','epiarea')] %>% rbind(inftype) 384 | dens_myet = dens_all %>% filter(ct3%in%c('Mye','T')) %>% left_join(inftype) 385 | meandens_myet = aggregate(list(dens=dens_myet$dens),by=as.list(dens_myet[,c('sp_stg','stage1','epiarea','ct3')]),mean) 386 | 387 | meandens_myet %>% 388 | ggplot(aes(ct3,dens))+ 389 | theme1+theme(axis.ticks.x = element_blank())+ 390 | xlab('')+ylab('Cell density')+ 391 | NoLegend()+ 392 | stat_compare_means(comparisons = makecomparison(2))+ 393 | scale_fill_manual(values = primcol2)+scale_color_manual(values = primcol2)+ 394 | scale_y_continuous(expand = c(0.1,0.3))+ 395 | facet_grid(epiarea~stage1,scales = 'free')+ 396 | geom_jitter(size=2,aes(color=ct3))+ 397 | geom_boxplot(aes(fill=ct3),outlier.color = NA,alpha=0.5) 398 | ggsave('stp3_tme/plot/mye_analysis/mye_density_compareWithT.pdf',width = 8,height = 5) 399 | } 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | --------------------------------------------------------------------------------