-
Notifications
You must be signed in to change notification settings - Fork 0
/
scRNA_integration.R
177 lines (132 loc) · 5.45 KB
/
scRNA_integration.R
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
library(Seurat)
#data: scRNA-seq data analysis to compare the skeletal muscle transcriptome of an individual pre and post exercise
pre.exercise <- Read10X("/Users/maitreepatel/Desktop/R/scRNA analysis/GSE214544_GSM6611297 (2)")
pre.seurat <- CreateSeuratObject(counts = pre.exercise,
project = "muscle_pre")
post.exercise <- Read10X("/Users/maitreepatel/Desktop/R/scRNA analysis/GSE214544_GSM6611298 (3)")
post.seurat <- CreateSeuratObject(counts = post.exercise,
project = "muscle_post")
#creating a list
muscle.list <- list(pre.seurat,
post.seurat)
#merging the data
merged.muscle <- merge(x = pre.seurat,
y = post.seurat,
add.cell.ids = c("pre", "post"))
#getting the metadata
library(stringr)
sample <- names([email protected])
head(sample)
sample.detect <- ifelse(str_detect(sample, "pre"),
"pre",
"post")
#addding a column to the metadata with the conditions
[email protected]$sample <- sample.detect
#manipulating identity class?
Idents(object = merged.muscle) <- "sample"
#normalizing dataset
#dividing the datasets as pre or post
muscle.list <- SplitObject(merged.muscle,
split.by = "sample")
#performing pre-processing on both the elements in list using a loop
for (i in 1:length(muscle.list)) {
muscle.list[[i]] <- NormalizeData(muscle.list[[i]],
verbose = FALSE)
muscle.list[[i]] <- subset(muscle.list[[i]],
downsample = 1000)
muscle.list[[i]] <- FindVariableFeatures(muscle.list[[i]],
selection.method = "vst",
nfeatures = 2000, verbose = FALSE)
}
#use lapply!!
#especially with more than two datasets
#selecting features that are variable across the datasets
features.var <- SelectIntegrationFeatures(object.list = muscle.list)
#running PCA on highly variable features
muscle.list <- lapply(X = muscle.list, FUN = function(x) {
x <- ScaleData(x,
features = features.var,
verbose = FALSE)
x <- RunPCA(x,
features = features.var,
verbose = FALSE)
})
#finding anchor genes and integrating data
anchors <- FindIntegrationAnchors(object.list = muscle.list)
merged.muscle <- IntegrateData(anchorset = anchors)
#data manipulation and cleaning
#standard workflow
merged.muscle <- ScaleData(merged.muscle,
verbose = FALSE)
merged.muscle <- FindVariableFeatures(merged.muscle,
selection.method = "vst",
nfeatures = 2000,
verbose = FALSE)
#dimensionality reduction
merged.muscle <- RunPCA(merged.muscle,
npcs = 30,
verbose = FALSE)
merged.muscle <- RunUMAP(merged.muscle,
reduction = "pca",
dims = 1:20)
#clustering
merged.muscle <- FindNeighbors(merged.muscle,
reduction = "pca",
dims = 1:20)
merged.muscle <- FindClusters(merged.muscle,
resolution = 0.5)
DimPlot(merged.muscle,
reduction = "umap")
Idents(object = merged.muscle) <- "sample"
DimPlot(merged.muscle,
reduction = "umap")
#finding markers
Idents(object = merged.muscle) <- "sample"
#finding differentially expressed genes
sammple.markers <- FindMarkers(merged.muscle,
ident.1 = "pre",
ident.2 = "post")
head(sammple.markers)
library(ComplexHeatmap)
heatmapdf <- sammple.markers[1:25,]
row_ha = rowAnnotation("pre" = anno_barplot(heatmapdf$pct.1),
"post"= anno_barplot(heatmapdf$pct.2),
width = unit(10, "cm"))
ht0 <- Heatmap(heatmapdf$avg_log2FC,
name = "Log2FC",
cluster_rows = TRUE,
row_labels = rownames(heatmapdf),
right_annotation = row_ha,
width = unit(1, "cm"))
ht0
#finding differentially expressed genes between cluster 0 and the rest
Idents(object = merged.muscle) <- "seurat_clusters"
cluster0.markers <- FindMarkers(merged.muscle,
ident.1 = "0",
ident.2 = NULL,
only.pos = TRUE)
#heatmap cluster 0 versus others
head(cluster0.markers)
heatmapdf <- cluster0.markers[1:25,]
row_ha = rowAnnotation("Cluster 0" = anno_barplot(heatmapdf$pct.1),
"Others"= anno_barplot(heatmapdf$pct.2),
width = unit(10, "cm"))
ht1 <- Heatmap(heatmapdf$avg_log2FC,
name = "Log2FC",
cluster_rows = TRUE,
row_labels = rownames(heatmapdf),
right_annotation = row_ha,
width = unit(1, "cm"))
ht1
DefaultAssay(merged.muscle) <- "RNA"
gc()
merged.muscle[['groups']] <- sample(x = c('pre', 'post'),
size = ncol(x = merged.muscle),
replace = TRUE)
merged.muscle$groups[1] <- "pre"
merged.muscle$groups[2] <- "post"
FindConservedMarkers(merged.muscle,
ident.1 = 0,
ident.2 = 1,
grouping.var = "groups")
str(merged.muscle)