-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monocle_E175.qmd
160 lines (129 loc) · 4.07 KB
/
Monocle_E175.qmd
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
---
title: Clustering and differential expression analysis using Monocle
---
```{r}
library(monocle3)
library(rjson)
source("./utils.R")
```
Data loading and preparation
```{r}
numTopMarkers = 500
minClusterSize = 0
datasetName = "E175"
datasetPath= paste("./data/", datasetName, sep='')
outDir = paste(datasetPath, '/monocle/', sep='')
outDirDefault = paste(outDir, 'default/', sep='')
outDirCelltypist = paste(outDir, 'celltypist/', sep='')
if (!dir.exists(outDirDefault)) {
dir.create(outDirDefault, recursive = TRUE)
}
if (!dir.exists(outDirCelltypist)) {
dir.create(outDirCelltypist, recursive = TRUE)
}
numClusterThreshold = 1/10
numClusterCelltypist = fromJSON(file=paste(datasetPath, '/nclusters.json', sep=''))$nclusters
minNumClusterCelltypist = round(numClusterCelltypist - numClusterThreshold*numClusterCelltypist)
maxNumClusterCelltypist = round(numClusterCelltypist + numClusterThreshold*numClusterCelltypist)
```
```{r}
matrix <- read.csv(paste(datasetPath,'CorticalCells_GSM2861514_E175_cleaned.csv', sep='/'), header=TRUE, row.names=1)
cells <- read.csv(paste(datasetPath, 'barcodes.csv', sep='/'), header=FALSE, row.names=1)
genes <- read.csv(paste(datasetPath, 'genes.csv', sep='/'), header=FALSE, row.names=1)
matrix <- as.matrix(matrix)
cds<- new_cell_data_set(
matrix,
cell_metadata = cells,
gene_metadata = genes
)
```
Studying feature variance
```{r}
cds <- preprocess_cds(cds, num_dim = 10, method = "PCA") # normalization
plot_pc_variance_explained(cds)
```
Clustering with default parameters
```{r}
cds <- cluster_cells(cds, reduction_method = "PCA")
cat(paste("Number of clusters:", length(levels(clusters(cds, reduction_method = "PCA")))))
```
Save the default clustering results
```{r}
defaultLabelsDf = data.frame(clusters(cds, reduction_method = "PCA"))
defaultLabelsDf$cell <- rownames(defaultLabelsDf)
rownames(defaultLabelsDf) <- 1:nrow(defaultLabelsDf)
colnames(defaultLabelsDf)[1] <- "computed_id"
defaultLabelsDf$computed_id <- as.numeric(defaultLabelsDf$computed_id)
write_clustering(outDirDefault, defaultLabelsDf, "cell", "computed_id")
```
Differential expression on default clustering results
```{r}
defaultMarkers <- top_markers(
cds,
group_cells_by="cluster",
genes_to_test_per_group = 3000,
reduction_method='PCA',
cores=10
)
```
Save markers
```{r}
write_markers(outDirDefault, defaultMarkers, "gene_id", "cell_group", "marker_test_p_value", FALSE, numTopMarkers)
```
Clustering tuning resolution according to celltypist
```{r}
cat("Clusters to find:")
numClusterCelltypist
```
```{r}
cds <- preprocess_cds(cds, num_dim = 10, method = "PCA") # normalization
plot_pc_variance_explained(cds)
```
```{r}
maxResolution = 3
minResolution = 0
repeat{
resolution = (maxResolution + minResolution) / 2
cat(paste("Trying resolution ", resolution, '\n', sep=''))
cds <- cluster_cells(cds, reduction_method = "PCA", resolution=resolution)
numClusters = length(levels(clusters(cds, reduction_method = "PCA")))
cat(paste("Got ", numClusters, ' clusters', '\n', sep=''))
if (numClusters >= minNumClusterCelltypist & numClusters <= maxNumClusterCelltypist){
break
}
else if (numClusters < minNumClusterCelltypist){
# increase the resolution
minResolution = resolution
}
else{
# reduce the resolution
maxResolution = resolution
}
}
```
Save the tuned clustering results
```{r}
celltypistLabelsDf = data.frame(clusters(cds, reduction_method = "PCA"))
celltypistLabelsDf$cell <- rownames(celltypistLabelsDf)
rownames(celltypistLabelsDf) <- 1:nrow(celltypistLabelsDf)
colnames(celltypistLabelsDf)[1] <- "computed_id"
celltypistLabelsDf$computed_id <- as.numeric(celltypistLabelsDf$computed_id)
write_clustering(outDirCelltypist, celltypistLabelsDf, "cell", "computed_id")
```
Differential expression on tuned clustering results
```{r}
celltypistMarkers <- top_markers(
cds,
group_cells_by="cluster",
genes_to_test_per_group = 3000,
reduction_method='PCA',
cores=10
)
```
Save markers
```{r}
write_markers(outDirCelltypist, celltypistMarkers, "gene_id", "cell_group", "marker_test_p_value", FALSE, numTopMarkers)
```
```{r}
sessionInfo()
```