-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViral_infection_pseudotime_macrophages_notebook.Rmd
200 lines (168 loc) · 8.56 KB
/
Viral_infection_pseudotime_macrophages_notebook.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
---
title: "SARS_CoV-2 viral infection pseudotime analysis for human lung mascrophages"
output:
html_document: default
html_notebook: default
---
This code reproduces our infection pseudotime analysis detailed in our manuscript, "Activated interstitial macrophages are a predominant target of viral takeover and focus of inflammation in COVID-19 initiation in human lung" (https://www.biorxiv.org/content/10.1101/2022.05.10.491266v2.full).
```{r Install relevant packages}
if (!requireNamespace("devtools", quietly = TRUE))
install.packages("devtools")
# Specify packages and their versions
packages_versions <- list(
Seurat = "4.0.2",
here = "1.0.1",
sctransform = "0.3.2",
tidyverse = "1.3.1", # This will install the tidyverse package set, which includes ggplot2, dplyr, tibble, and more.
ggsci = "2.9",
gplots = "3.1.1",
heatmap3 = "1.1.9",
PseudotimeDE = "0.9.0",
SingleCellExperiment = "1.12.0",
tibble = "3.1.2",
scales = "1.1.1",
irlba = "2.3.3"
)
# Install packages with specified versions
for (pkg in names(packages_versions)) {
version <- packages_versions[[pkg]]
message(sprintf("Installing %s version %s", pkg, version))
devtools::install_version(pkg, version = version)
}
```
```{r setup, include=FALSE}
library(Seurat)
library(Matrix)
library(here)
library(sctransform)
library(loomR)
library(tidyverse)
library(SeuratDisk)
library(plyr)
library(dplyr)
library(cowplot)
library(ggsci)
library(gplots)
library(heatmap3)
source(here::here("boilerplate_3.0.R"))
sessionInfo()
```
```{r}
suppressPackageStartupMessages(library(PseudotimeDE))
suppressPackageStartupMessages(library(SingleCellExperiment))
suppressPackageStartupMessages(library(tibble))
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(scales))
suppressPackageStartupMessages(library(irlba))
```
```{r Functions}
seurat_process <- function(seurat_object){
# Standard normalization
seurat_object <- NormalizeData(seurat_object, normalization.method = "LogNormalize", scale.factor = 10000)
seurat_object <- FindVariableFeatures(seurat_object, selection.method = "vst", nfeatures = 3000)
all.genes <- rownames(seurat_object)
seurat_object <- ScaleData(seurat_object, features = all.genes)
seurat_object <- SCTransform(seurat_object, verbose = FALSE)
seurat_object <- RunPCA(seurat_object, verbose = FALSE)
return (seurat_object)
}
```
# 1. Setup seurat object and initialize
### Load seurat object of infected macrophages; seurat normalization, and PCA
```{r}
tiss.mac.1_infected_sub <- readRDS(here::here("data", "tiss.mac.1_infected.rds"))
tiss.mac.1_infected_sub <- seurat_process(tiss.mac.1_infected_sub)
```
### Visuzlie top principal components
```{r fig.height=10, fig.width=10}
ElbowPlot(tiss.mac.1_infected_sub)
DimHeatmap(tiss.mac.1_infected_sub, dims = 1:9, cells = 500, balanced = TRUE)
DimHeatmap(tiss.mac.1_infected_sub, dims = 10:18, cells = 500, balanced = TRUE)
DimHeatmap(tiss.mac.1_infected_sub, dims = 19:27, cells = 500, balanced = TRUE)
DimPlot(tiss.mac.1_infected_sub, label = TRUE, dims = c(1,2), reduction = "pca") + NoLegend()
```
### Visualize cell clusters by UMAP
```{r, fig.height=5, fig.width=6}
DimPlot(tiss.mac.1_infected_sub, label = TRUE, pt.size = 1.2) + NoLegend()
```
### Visualize metadata
```{r fig.height=6, fig.width=8}
UMAPPlot(tiss.mac.1_infected_sub, group.by = "tissue", label = TRUE, pt.size = 1) + ggtitle("Tissue")
UMAPPlot(tiss.mac.1_infected_sub, group.by = "cultured", label = TRUE, pt.size = 1) + ggtitle("Cultured")
UMAPPlot(tiss.mac.1_infected_sub, group.by = "condition", label = TRUE, pt.size = 1) + ggtitle("Culture Time")
UMAPPlot(tiss.mac.1_infected_sub, group.by = "infected", label = TRUE, pt.size = 1) + ggtitle("Infected")
UMAPPlot(tiss.mac.1_infected_sub, group.by = "free_annotation_MP", label = TRUE, pt.size = 1) + ggtitle("Free Annotation")
FeaturePlot(tiss.mac.1_infected_sub, features = "pct_mt_counts", pt.size = 1, cols = my.cols) + ggtitle("pct_mt_counts")
```
### Plot of viral infection pseudotime
```{r, fig.height=6, fig.width=8}
vpt_mac_mat <- tiss.mac.1_infected_sub@meta.data
ggplot(vpt_mac_mat, aes(x=log10_viral_total_UMIs, y=viral_UMIs_to_total_UMIs, color = free_annotation_MP)) +
geom_point(size = 1) +
geom_smooth(method = "loess", aes(colour = free_annotation_MP)) +
theme_classic() + ylim(0,0.6) + scale_colour_manual(values = c("#0072B5FF", "#BC3C29FF"))
```
# 2. Compute viral infection pseudotime from PCA
### Visualize principal components
```{r}
tiss.mac.1_infected_sub_PCA <- tiss.mac.1_infected_sub@reductions$pca
tiss.mac.1_infected_sub_PCA_feature_loadings <- tiss.mac.1_infected_sub_PCA@feature.loadings
tiss.mac.1_infected_sub_PCA_cell_embeddings <- tiss.mac.1_infected_sub_PCA@cell.embeddings
```
### Calculate PC scores
```{r, fig.height=5, fig.width=6}
tiss.mac.1_infected_sub$PC2_3_score <- tiss.mac.1_infected_sub_PCA_cell_embeddings[,3] + tiss.mac.1_infected_sub_PCA_cell_embeddings[,2] + tiss.mac.1_infected_sub_PCA_cell_embeddings[,4]
tiss.mac.1_infected_sub$PC2_3_score_norm = normalize(tiss.mac.1_infected_sub$PC2_3_score)
FeaturePlot(tiss.mac.1_infected_sub, features = c("PC2_3_score"), pt.size = 0.7, cols = my.cols) + ggtitle("")
tiss.mac.1_infected_sub$PC1_3score <- tiss.mac.1_infected_sub_PCA_cell_embeddings[,1] - tiss.mac.1_infected_sub_PCA_cell_embeddings[,3]
tiss.mac.1_infected_sub$PC1score <- tiss.mac.1_infected_sub_PCA_cell_embeddings[,1] - tiss.mac.1_infected_sub_PCA_cell_embeddings[,3] -tiss.mac.1_infected_sub_PCA_cell_embeddings[,4]
FeaturePlot(tiss.mac.1_infected_sub, features = c("PC1score"), pt.size = 0.7, cols = my.cols)
tiss.mac.1_infected_sub$PC1_3_score_norm = normalize(tiss.mac.1_infected_sub$PC1_3score)
FeaturePlot(tiss.mac.1_infected_sub, features = c("PC1_3_score_norm"), pt.size = 0.7, cols = my.cols)
```
### Visualize cell annotations, viral RNA, and PC scores
```{r, fig.height=4.5, fig.width=5}
DimPlot(tiss.mac.1_infected_sub, group.by = c("free_annotation_MP"), pt.size = 1.5, cols = rev(pal_nejm()(2)), ) + NoLegend() + ggtitle("Free Annotation") + theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()) + NoLegend()
FeaturePlot(tiss.mac.1_infected_sub, features = c("log10_viral_total_UMIs"), pt.size = 1.5, cols = my.cols) + NoLegend() + ggtitle("log10_viral_UMIs") + theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()) + NoLegend()
FeaturePlot(tiss.mac.1_infected_sub, features = c("PC2_3_score"), pt.size = 1.75, cols = my.cols) + NoLegend() + ggtitle("PC2_3_score") + theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()) + NoLegend()
FeaturePlot(tiss.mac.1_infected_sub, features = c("PC1score"), pt.size = 1.05, cols = my.cols) + NoLegend() + ggtitle("PC1score") + theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()) + NoLegend()
```
### Get PC Loadings
```{r}
# IM
PC2_3_loadings <- tiss.mac.1_infected_sub_PCA_feature_loadings[,2] + tiss.mac.1_infected_sub_PCA_feature_loadings[,3] + tiss.mac.1_infected_sub_PCA_feature_loadings[,4]
PC2_3_loadings <- as.data.frame(PC2_3_loadings)
PC2_3_loadings_genes <- rownames(PC2_3_loadings)
# AM
PC1_3_loadings <- tiss.mac.1_infected_sub_PCA_feature_loadings[,1] - tiss.mac.1_infected_sub_PCA_feature_loadings[,3]
PC1_3_loadings <- as.data.frame(PC1_3_loadings)
PC1_3_loadings_genes <- rownames(PC1_3_loadings)
```
# 3. Perform pseudotime differential gene expression analysis
```{r}
ori_pseudotime_tbl <- tibble(cell = colnames(tiss.mac.1_infected_sub), pseudotime = tiss.mac.1_infected_sub$PC2_3_score_norm)
mac.sce <- as.SingleCellExperiment(tiss.mac.1_infected_sub)
res_fix_PC2_3 <- PseudotimeDE::runPseudotimeDE(gene.vec = PC2_3_loadings_genes[1:250],
ori.tbl = ori_pseudotime_tbl,
sub.tbl = NULL, # Set as NULL to only get fix.pv
mat = mac.sce,
model = "nb")
ori_pseudotime_AM_tbl <- tibble(cell = colnames(tiss.mac.1_infected_sub), pseudotime = tiss.mac.1_infected_sub$PC1_3_score_norm)
res_fix_PC1_3 <- PseudotimeDE::runPseudotimeDE(gene.vec = PC1_3_loadings_genes[1:250],
ori.tbl = ori_pseudotime_AM_tbl,
sub.tbl = NULL, # Set as NULL to only get fix.pv
mat = mac.sce,
model = "nb")
```