-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathEcoTyper_discovery_scRNA.R
312 lines (267 loc) · 14.3 KB
/
EcoTyper_discovery_scRNA.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
suppressPackageStartupMessages({
library(config)
library(argparse)
source("pipeline/lib/config.R")
source("pipeline/lib/misc.R")
source("pipeline/lib/multithreading.R")
})
parser <- ArgumentParser(add_help = F)
arguments = parser$add_argument_group('Arguments')
arguments$add_argument("-c", "--config", type = "character", metavar="<PATH>",
help="Path to the config files [required].")
arguments$add_argument("-h", "--help", action='store_true', help="Print help message.")
args <- parser$parse_args()
#print(args)
if(args$h || is.null(args$config))
{
parser$print_help()
quit()
}
config_file = abspath(args$config)
config <- config::get(file = config_file)
check_discovery_configuration_scRNA(config)
discovery = config$Input$"Discovery dataset name"
discovery_type = config$Input$"Expression type"
scale_column = config$Input$"Annotation file column to scale by"
additional_columns = config$Input$"Annotation file column(s) to plot"
final_output = config$"Output"$"Output folder"
n_threads = config$"Pipeline settings"$"Number of threads"
nmf_restarts = config$"Pipeline settings"$"Number of NMF restarts"
max_clusters = config$"Pipeline settings"$"Maximum number of states per cell type"
cohpenetic_cutoff = config$"Pipeline settings"$"Cophenetic coefficient cutoff"
skip_steps = config$"Pipeline settings"$"Pipeline steps to skip"
p_value_cutoff = config$"Pipeline settings"$"Jaccard matrix p-value cutoff"
min_states = config$"Pipeline settings"$"Minimum number of states in ecotypes"
suppressWarnings({
final_output = abspath(final_output)
})
#Starting EcoTyper
setwd("pipeline")
start = Sys.time()
if(config$"Pipeline settings"$"Filter genes" == "cell type specific")
{
fractions = "Cell_type_specific_genes"
}else{
if(config$"Pipeline settings"$"Filter genes" == "no filter")
{
fractions = "All_genes"
}else{
n_genes = as.integer(as.numeric(config$"Pipeline settings"$"Filter genes"))
if(is.na(n_genes))
{
stop(paste0("Invalid value provided in field 'Filter genes' of the configuration file."))
}
fractions = paste0("Top_", n_genes)
}
}
if(!1 %in% skip_steps & fractions != "All_genes")
{
cat("\nStep 1 (extract cell type specific genes)...\n")
annotation = read.delim(file.path(file.path("../datasets/discovery", discovery, "annotation.txt")))
cell_types = unlist(levels(as.factor(as.character(annotation$CellType))))
for(cell_type in cell_types)
{
print(cell_type)
PushToJobQueue(paste("Rscript state_discovery_scRNA_filter_genes.R", discovery, fractions, cell_type, scale_column))
}
RunJobQueue()
cat("Step 1 (extract cell type specific genes) finished successfully!\n")
}else{
cat("Skipping step 1 (extract cell type specific genes)...\n")
}
if(!2 %in% skip_steps)
{
cat("\nStep 2 (cell state discovery on correrlation matrices): Calculating correlation matrices...\n")
annotation = read.delim(file.path(file.path("../datasets/discovery", discovery, "annotation.txt")))
cell_types = unlist(levels(as.factor(as.character(annotation$CellType))))
for(cell_type in cell_types)
{
filter_genes = (fractions == "Cell_type_specific_genes") || grepl("Top_", fractions)
PushToJobQueue(paste("Rscript state_discovery_scRNA_distances.R", discovery, fractions, cell_type, filter_genes, scale_column))
}
RunJobQueue()
cat("Step 2 (cell state discovery on correrlation matrices): Running NMF (Warning: This step might take a long time!)...\n")
for(cell_type in cell_types)
{
if(!file.exists(file.path("../EcoTyper", discovery, fractions, "Cell_States", "discovery_cross_cor", cell_type, "expression_top_genes_scaled.txt")))
{
next
}
for(n_clusters in 2:max_clusters)
{
for(restart in 1:nmf_restarts)
{
if(!file.exists(file.path("../EcoTyper", discovery, fractions, "Cell_States", "discovery_cross_cor", cell_type, n_clusters, "restarts", restart, "estim.RData")))
{
PushToJobQueue(paste("Rscript state_discovery_NMF.R", "discovery_cross_cor", discovery, fractions, cell_type, n_clusters, restart))
}else{
cat(paste0("Warning: Skipping NMF on '", cell_type, "' (number of states = ", n_clusters, ", restart ", restart, "), as the output file '", file.path("../EcoTyper", discovery, fractions, "Cell_States", "discovery", cell_type, n_clusters, "restarts", restart, "estim.RData"), "' already exists!\n"))
}
}
}
}
RunJobQueue()
cat("Step 2 (cell state discovery on correrlation matrices): Aggregating NMF results...\n")
for(cell_type in cell_types)
{
if(!file.exists(file.path("../EcoTyper", discovery, fractions, "Cell_States", "discovery_cross_cor", cell_type, "expression_top_genes_scaled.txt")))
{
next
}
PushToJobQueue(paste("Rscript state_discovery_combine_NMF_restarts.R", "discovery_cross_cor", discovery, fractions, cell_type, max_clusters, nmf_restarts))
}
RunJobQueue()
cat("Step 2 (cell state discovery on correrlation matrices) finished successfully!\n")
}else{
cat("Skipping step 2 (cell state discovery on correrlation matrices)...\n")
}
if(!3 %in% skip_steps)
{
cat("\nStep 3 (choosing the number of cell states)...\n")
PushToJobQueue(paste("Rscript state_discovery_rank_selection.R", "discovery_cross_cor", discovery, fractions, max_clusters, cohpenetic_cutoff))
RunJobQueue()
cat("Step 3 (choosing the number of cell states) finished successfully!\n")
}else{
cat("Skipping step 3 (choosing the number of cell states)...\n")
}
if(!4 %in% skip_steps)
{
cat("\nStep 4 (extracting cell state information)...\n")
system(paste("cp -f ", paste0("'", config_file, "'"), file.path("../EcoTyper", discovery, "config_used.yml")))
key = read.delim(file.path("../EcoTyper", discovery, fractions, "Analysis", "rank_selection", "rank_data.txt"))
for(cell_type in key[,1])
{
cat(paste("Extracting cell states information for:", cell_type, "\n"))
n_clusters = key[key[,1] == cell_type, 2]
PushToJobQueue(paste("Rscript state_discovery_initial_plots_scRNA.R", "discovery_cross_cor", discovery, fractions, cell_type, n_clusters, "State", paste(additional_columns, collapse = " ")))
}
RunJobQueue()
cat("Step 4 (extracting cell state information) finished successfully!\n")
}else{
cat("\nSkipping step 4 (extracting cell state information)...\n")
}
if(!5 %in% skip_steps)
{
cat("\nStep 5 (cell state re-discovery in expression matrices)...\n")
key = read.delim(file.path("../EcoTyper", discovery, fractions, "Analysis", "rank_selection", "rank_data.txt"))
for(cell_type in key[,1])
{
cat(paste("Extracting marker genes for cell states defined in:", cell_type, "\n"))
n_clusters = key[key[,1] == cell_type, 2]
PushToJobQueue(paste("Rscript state_discovery_extract_features_scRNA.R", discovery, fractions, cell_type, n_clusters))
}
RunJobQueue()
cat("\nStep 5 (cell state re-discovery in expression matrices): Running NMF on expression matrix...\n")
key = read.delim(file.path("../EcoTyper", discovery, fractions, "Analysis", "rank_selection", "rank_data.txt"))
for(cell_type in key[,1])
{
if(!file.exists(file.path("../EcoTyper", discovery, fractions, "Cell_States", "discovery", cell_type, "expression_top_genes_scaled.txt")))
{
next
}
n_clusters = key[key[,1] == cell_type, 2]
for(restart in 1:nmf_restarts)
{
if(!file.exists(file.path("../EcoTyper", discovery, fractions, "Cell_States", "discovery", cell_type, n_clusters, "restarts", restart, "estim.RData")))
{
PushToJobQueue(paste("Rscript state_discovery_NMF.R", "discovery", discovery, fractions, cell_type, n_clusters, restart))
}else{
cat(paste0("Warning: Skipping NMF on '", cell_type, "' (number of states = ", n_clusters, ", restart ", restart, "), as the output file '", file.path("../EcoTyper", discovery, fractions, "Cell_States", "discovery", cell_type, n_clusters, "restarts", restart, "estim.RData"), "' already exists!\n"))
}
}
}
RunJobQueue()
cat("Step 5 (cell state re-discovery in expression matrices): Aggregating NMF results...\n")
for(cell_type in key[,1])
{
if(!file.exists(file.path("../EcoTyper", discovery, fractions, "Cell_States", "discovery", cell_type, "expression_top_genes_scaled.txt")))
{
next
}
PushToJobQueue(paste("Rscript state_discovery_combine_NMF_restarts.R", "discovery", discovery, fractions, cell_type, max_clusters, nmf_restarts, "FALSE"))
}
RunJobQueue()
cat("Step 5 (cell state re-discovery in expression matrices) finished successfully!\n")
}else{
cat("Skipping step 5 (cell state re-discovery in expression matrices)...\n")
}
if(!6 %in% skip_steps)
{
cat("\nStep 6 (extracting information for re-discovered cell states)...\n")
key = read.delim(file.path("../EcoTyper", discovery, fractions, "Analysis", "rank_selection", "rank_data.txt"))
for(cell_type in key[,1])
{
cat(paste("Extracting cell states information for:", cell_type, "\n"))
n_clusters = key[key[,1] == cell_type, 2]
PushToJobQueue(paste("Rscript state_discovery_initial_plots.R", "discovery", discovery, fractions, cell_type, n_clusters, "State", paste(additional_columns, collapse = " ")))
}
RunJobQueue()
cat("Step 6 (extracting information for re-discovered cell states) finished successfully!\n")
}else{
cat("\nSkipping step 6 (extracting information for re-discovered cell states)...\n")
}
if(!7 %in% skip_steps)
{
cat("\nStep 7 (cell state QC filter)...\n")
key = read.delim(file.path("../EcoTyper", discovery, fractions, "Analysis", "rank_selection", "rank_data.txt"))
for(cell_type in key[,1])
{
cat(paste("Filtering low-quality cell states for:", cell_type, "\n"))
n_clusters = key[key[,1] == cell_type, 2]
PushToJobQueue(paste("Rscript state_discovery_first_filter_scRNA.R", discovery, fractions, cell_type, n_clusters, "State", paste(additional_columns, collapse = " ")))
}
RunJobQueue()
cat("Step 7 (cell state QC filter) finished successfully!\n")
}else{
cat("\nSkipping step 7 (cell state QC filter)...\n")
}
if(!8 %in% skip_steps)
{
cat("\nStep 8 (ecotype discovery)...\n")
PushToJobQueue(paste("Rscript ecotypes_scRNA.R", discovery, fractions, p_value_cutoff, min_states))
RunJobQueue()
PushToJobQueue(paste("Rscript ecotypes_assign_samples_scRNA.R", discovery, fractions, "Ecotype",paste(additional_columns, collapse = " ")))
cat("Step 8 (ecotype discovery) finished successfully!\n")
RunJobQueue()
}else{
cat("Skipping step 8 (ecotype discovery)...\n")
}
cat("\nCopying EcoTyper results to the output folder!\n")
if(file.exists(final_output) && length(list.files(final_output)) > 0)
{
old_results_folder = paste0(final_output, format(Sys.time(), " %a %b %d %X %Y"))
dir.create(old_results_folder, recursive = T, showWarnings = F)
warning(paste0("The output folder contains files from a previous run. Moving those files to: '", old_results_folder, "'"))
system(paste0("mv -f ", paste0("'", final_output, "'"), "/* '", old_results_folder, "'"))
}
dir.create(final_output, recursive = T, showWarnings = F)
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Analysis", "rank_selection", "rank_data.txt"), paste0("'", final_output, "'")))
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Analysis", "rank_selection", "rank_plot.pdf"), paste0("'", final_output, "'")))
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Analysis", "rank_selection", "rank_plot.png"), paste0("'", final_output, "'")))
key = read.delim(file.path("../EcoTyper", discovery, fractions, "Analysis", "rank_selection", "rank_data.txt"))
for(cell_type in key[,1])
{
n_clusters = key[key[,1] == cell_type, 2]
ct_output = file.path(final_output, cell_type)
dir.create(ct_output, recursive = T, showWarnings = F)
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Cell_States", "discovery", cell_type, n_clusters, "gene_info.txt"), paste0("'", ct_output, "'")))
#system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Cell_States", "discovery", cell_type, n_clusters, "state_abundances.txt"), paste0("'", ct_output, "'")))
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Cell_States", "discovery", cell_type, n_clusters, "state_assignment.txt"), paste0("'", ct_output, "'")))
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Cell_States", "discovery", cell_type, n_clusters, "state_assignment_heatmap.pdf"), paste0("'", ct_output, "'")))
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Cell_States", "discovery", cell_type, n_clusters, "state_assignment_heatmap.png"), paste0("'", ct_output, "'")))
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Cell_States", "discovery", cell_type, n_clusters, "heatmap_data.txt"), paste0("'", ct_output, "'")))
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Cell_States", "discovery", cell_type, n_clusters, "heatmap_top_ann.txt"), paste0("'", ct_output, "'")))
}
ct_output = file.path(final_output, "Ecotypes")
dir.create(ct_output, recursive = T, showWarnings = F)
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Ecotypes", "discovery", "ecotypes.txt"), paste0("'", ct_output, "'")))
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Ecotypes", "discovery", "ecotype_assignment.txt"), paste0("'", ct_output, "'")))
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Ecotypes", "discovery", "ecotype_abundance.txt"), paste0("'", ct_output, "'")))
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Ecotypes", "discovery", "heatmap_assigned_samples_viridis.pdf"), paste0("'", ct_output, "'")))
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Ecotypes", "discovery", "heatmap_assigned_samples_viridis.png"), paste0("'", ct_output, "'")))
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Ecotypes", "discovery", "jaccard_matrix.pdf"), paste0("'", ct_output, "'")))
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Ecotypes", "discovery", "jaccard_matrix.png"), paste0("'", ct_output, "'")))
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Ecotypes", "discovery", "nclusters_jaccard.png"), paste0("'", ct_output, "'")))
system(paste("cp -f", file.path("../EcoTyper", discovery, fractions, "Ecotypes", "discovery", "nclusters_jaccard.pdf"), paste0("'", ct_output, "'")))
end = Sys.time()
cat(paste0("\nEcoTyper finished succesfully! Please find the results in: '", paste0("'", final_output, "'"), "'.\nRun time: ", format(end - start, digits = 1), "\n"))