-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.5.1_Cell_cycle_and_vector.Rmd
193 lines (156 loc) · 7.66 KB
/
1.5.1_Cell_cycle_and_vector.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
---
title: "1.5.1 Cell cycle scoring and vector calculation"
author: "Vincent Gardeux"
date: "2023/12/15"
output:
html_document:
df_print: paged
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir = "./")
getwd()
```
## Libraries & functions
First, I'm loading the required libraries & functions
```{r}
suppressPackageStartupMessages(library(Seurat)) # For handling 10x h5 file
suppressPackageStartupMessages(library(data.table)) # For fread/fwrite functions
suppressPackageStartupMessages(library(Rfast)) # For colnth function
suppressPackageStartupMessages(library(crayon)) # Just for bolding the console output :D
cat(bold("Seurat"), "version", as.character(packageVersion("Seurat")), "\n")
cat(bold("data.table"), "version", as.character(packageVersion("data.table")), "\n")
cat(bold("Rfast"), "version", as.character(packageVersion("Rfast")), "\n")
cell_cycle <- function(exp_seurat, exp_tf_cell, suffix_cellnames, data_cellcycle_genes, exp_out_seurat){
# Get combinations (for exp12-13)
exp_combination_list <- "metadata/C3H10_10X_Metadata_exp12-13_combination.txt"
meta_combinations <- fread(exp_combination_list, data.table = F)
meta_combinations <- meta_combinations[!duplicated(meta_combinations$combination),]
rownames(meta_combinations) <- meta_combinations$combination
# Read Seurat object of previous step
data.seurat <- readRDS(exp_seurat)
# Renaming cell adding the Exp (for later merging all exps)
data.seurat <- RenameCells(data.seurat, new.names = paste0(colnames(data.seurat), suffix_cellnames))
# Putting Vector as metadata
data.seurat$Vector_10X <- data.seurat@assays$RNA@counts["Vector",]
data.seurat <- data.seurat[rownames(data.seurat) != "Vector",]
# TF matrix
data.tf_read <- fread(exp_tf_cell, data.table = F)
rownames(data.tf_read) <- data.tf_read$TFName
colnames(data.tf_read) <- paste0(colnames(data.tf_read), suffix_cellnames)
data.tf_read <- data.tf_read[,colnames(data.seurat)]
# Creating the Vector by extracting the UMI values from the TF barcode matrix
data.seurat$Vector_UMI <- NA
for(i in 1:ncol(data.tf_read)){
data.seurat$Vector_UMI[i] <- data.tf_read[data.seurat$TF[i],i]
if(is.na(data.seurat$Vector_UMI[i])){
# Combination
tf1 <- meta_combinations[data.seurat$TF[i],"TF1"]
tf2 <- meta_combinations[data.seurat$TF[i],"TF2"]
data.seurat$Vector_UMI[i] <- data.tf_read[tf1,i] + data.tf_read[tf2,i]
}
}
data.seurat$Log_Vector_UMI <- log(1 + data.seurat$Vector_UMI)
# I don't filter the genes anymore
data.seurat_tmp <- data.seurat
message(nrow(data.seurat), " genes used for computation...")
# Normalization (required for CellCycleScoring)
data.seurat_tmp <- NormalizeData(data.seurat_tmp, verbose = F)
# Calculate Cell Cycle score
data.seurat_tmp <- CellCycleScoring(data.seurat_tmp, s.features = subset(data_cellcycle_genes, phase == "S")$geneID, g2m.features = subset(data_cellcycle_genes, phase == "G2/M")$geneID)
message("\nPhase: ")
print(table(data.seurat_tmp$Phase))
# Calculate Cell Cycle score (corrected)
# Change threshold for Phase, Seurat assigns Phase if the max score is bigger than 0 which seems a too low threshold
data.seurat_tmp$Phase_corrected <- ifelse(pmax(data.seurat_tmp$G2M.Score, data.seurat_tmp$S.Score) > 0.1, as.character(data.seurat_tmp$Phase), "G1")
message("\nPhase corrected: ")
print(table(data.seurat_tmp$Phase_corrected))
# Transferring annotation to original Seurat object with all genes
data.seurat$Phase_corrected <- data.seurat_tmp[,colnames(data.seurat)]$Phase_corrected
data.seurat$Phase <- data.seurat_tmp[,colnames(data.seurat)]$Phase
data.seurat$Vector_10X <- data.seurat_tmp[,colnames(data.seurat)]$Vector_10X
data.seurat$Vector_UMI <- data.seurat_tmp[,colnames(data.seurat)]$Vector_UMI
data.seurat$Log_Vector_UMI <- data.seurat_tmp[,colnames(data.seurat)]$Log_Vector_UMI
data.seurat$S.Score <- data.seurat_tmp[,colnames(data.seurat)]$S.Score
data.seurat$G2M.Score <- data.seurat_tmp[,colnames(data.seurat)]$G2M.Score
# Saving Seurat object
saveRDS(data.seurat, file = exp_out_seurat)
}
```
## Load cell cycle genes (from https://raw.githubusercontent.com/hbc/tinyatlas/master/cell_cycle/Mus_musculus.csv)
```{r}
cellcycle_genes <- fread("metadata/Mus_musculus.csv", sep=",", data.table = F, stringsAsFactors = F)
message(nrow(cellcycle_genes), " genes annotated as Cell Cycle genes, initially downloaded from https://raw.githubusercontent.com/hbc/tinyatlas/master/cell_cycle/Mus_musculus.csv")
```
### Exp 05
```{r}
cell_cycle(exp_seurat = "results/C3H10_10X_exp05_enriched.cellranger_kneepoint_outliers.rds",
exp_tf_cell = "results/C3H10_10X_exp05_enriched.umi_matrix.txt",
suffix_cellnames = "-1-5",
data_cellcycle_genes = cellcycle_genes,
exp_out_seurat = "results/C3H10_10X_exp05_enriched.cellranger_kneepoint_outliers_cellcycle.rds"
)
```
### Exp 06
```{r}
cell_cycle(exp_seurat = "results/C3H10_10X_exp06_enriched.cellranger_kneepoint_outliers.rds",
exp_tf_cell = "results/C3H10_10X_exp06_enriched.umi_matrix.txt",
suffix_cellnames = "-1-6",
data_cellcycle_genes = cellcycle_genes,
exp_out_seurat = "results/C3H10_10X_exp06_enriched.cellranger_kneepoint_outliers_cellcycle.rds"
)
```
### Exp 07
```{r}
cell_cycle(exp_seurat = "results/C3H10_10X_exp07_enriched.cellranger_kneepoint_outliers.rds",
exp_tf_cell = "results/C3H10_10X_exp07_enriched.umi_matrix.txt",
suffix_cellnames = "-1-7",
data_cellcycle_genes = cellcycle_genes,
exp_out_seurat = "results/C3H10_10X_exp07_enriched.cellranger_kneepoint_outliers_cellcycle.rds"
)
```
### Exp 08
```{r}
cell_cycle(exp_seurat = "results/C3H10_10X_exp08_enriched.cellranger_kneepoint_outliers.rds",
exp_tf_cell = "results/C3H10_10X_exp08_enriched.umi_matrix.txt",
suffix_cellnames = "-1-8",
data_cellcycle_genes = cellcycle_genes,
exp_out_seurat = "results/C3H10_10X_exp08_enriched.cellranger_kneepoint_outliers_cellcycle.rds"
)
```
### Exp 09
```{r}
cell_cycle(exp_seurat = "results/C3H10_10X_exp09_enriched.cellranger_kneepoint_outliers.rds",
exp_tf_cell = "results/C3H10_10X_exp09_enriched.umi_matrix.txt",
suffix_cellnames = "-1-9",
data_cellcycle_genes = cellcycle_genes,
exp_out_seurat = "results/C3H10_10X_exp09_enriched.cellranger_kneepoint_outliers_cellcycle.rds"
)
```
### Exp 10
```{r}
cell_cycle(exp_seurat = "results/C3H10_10X_exp10_enriched.cellranger_kneepoint_outliers.rds",
exp_tf_cell = "results/C3H10_10X_exp10_enriched.umi_matrix.txt",
suffix_cellnames = "-1-10",
data_cellcycle_genes = cellcycle_genes,
exp_out_seurat = "results/C3H10_10X_exp10_enriched.cellranger_kneepoint_outliers_cellcycle.rds"
)
```
### Exp 11
```{r}
cell_cycle(exp_seurat = "results/C3H10_10X_exp11_enriched.cellranger_kneepoint_outliers.rds",
exp_tf_cell = "results/C3H10_10X_exp11_enriched.umi_matrix.txt",
suffix_cellnames = "-1-11",
data_cellcycle_genes = cellcycle_genes,
exp_out_seurat = "results/C3H10_10X_exp11_enriched.cellranger_kneepoint_outliers_cellcycle.rds"
)
```
### Exp 12-13
```{r}
cell_cycle(exp_seurat = "results/C3H10_10X_exp12-13_enriched.cellranger_kneepoint_outliers.rds",
exp_tf_cell = "results/C3H10_10X_exp12-13_enriched.umi_matrix.txt",
suffix_cellnames = "-12-13",
data_cellcycle_genes = cellcycle_genes,
exp_out_seurat = "results/C3H10_10X_exp12-13_enriched.cellranger_kneepoint_outliers_cellcycle.rds"
)
```