-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulk_rnaseq_preprocessing.R
732 lines (581 loc) · 18.5 KB
/
bulk_rnaseq_preprocessing.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
# Title: Bulk RNA-seq pre-processing (Geo2RNAseq pipeline)
# Author: Albert García López
# Load libraries and import functions ##########################################
library("Geo2RNAseq")
library("ggplot2")
library("ggfortify")
library("data.table")
library("gtools")
library("Rsamtools")
library("tidyverse")
# Set WD
setwd("path_to_dir")
stopifnot(dir.exists(getwd()))
# To resume previous workspaces
dir() %>% stringr::str_subset(string = ., pattern = "RData")
# Global Settings ##############################################################
# Output directories locations
indexDir <- paste0(getwd(), "/index")
fastqDir <- paste0(getwd(), "/fastq")
qualDir <- paste0(getwd(), "/quality")
mapDir <- paste0(getwd(), "/mapping")
countDir <- paste0(getwd(), "/counting")
tabDir <- paste0(getwd(), "/result_tables")
plotDir <- paste0(getwd(), "/result_plots")
degDir <- paste0(getwd(), "/diff_exp_genes")
# Create output directories
output_directories <- c(
indexDir, fastqDir, qualDir, mapDir, countDir, tabDir, plotDir, degDir
)
for (i in output_directories){
if (dir.exists(i) == FALSE){
dir.create(i, recursive = TRUE)
} else {
message(paste0(i, " is already there!"))
}
}
# Sanity check
for (i in output_directories){
if (dir.exists(i) == TRUE){
message(paste0("Directory [", i, "] exists!"))
} else {
message("Something has gone wrong! Repeat previous step please!")
}
}
# Allocate CPU's cores usage
MAX_CPUS <- 20
# Force overwritting of files
# NOTE:
# If TRUE, existing files will be overwritten without questioning.
# If FALSE, most methods will skip step for existing output files.
# In these cases, the method will return 'call[x] = "NOT USED"'
FORCE_OVERWRITE <- TRUE
# Is it single or paired-end?
paired <- TRUE
# Files ########################################################################
# NOTE: TopHat2/HISAT2 are looking for the genome file in the index directory
# move it there or create a link if the genome is located in its own directory
genome <- "path_to_fasta_reference_file"
anno <- "path_to_gtf_reference_file"
# Sanity check
stopifnot(file.exists(genome) == TRUE)
stopifnot(file.exists(anno) == TRUE)
# Build index with make_HiSat2_index() or make_Tophat_index() ##################
# NOTE: add something like "index" after the index directory.
index <- make_HiSat2_index(
genomeFile = genome,
customOut = paste0(indexDir, "/index")
)
# Load Raw FASTQ files #########################################################
raw_fastq_files <- list.files(
path = "raw_data/",
full.names = TRUE,
pattern = "fq.gz",
recursive = TRUE
)
stopifnot(file.exists(raw_fastq_files) == TRUE) # QC
if (paired == TRUE){
message(paste0("We have ", length(raw_fastq_files)/2, " samples (PAIRED)"))
} else {
message(paste0("We have ", length(raw_fastq_files), " samples (SINGLE)"))
}
writeLines("Working with files:")
print(raw_fastq_files)
# Quality Control 1 (before Trimming) ##########################################
qualityRawDir <- file.path(qualDir, "raw")
if (length(dir(qualityRawDir)) > 0){
warning(
paste0("Directory \"", qualityRawDir, "\" not empty! Overwrite? ", FORCE_OVERWRITE),
immediate. = TRUE
)
}
if (FORCE_OVERWRITE || length(dir(qualityRawDir)) == 0){
writeLines("FastQC - raw ...")
fq_res <- run_FastQC(
raw_fastq_files,
outDir = qualityRawDir,
cpus = MAX_CPUS,
extend = TRUE
)
}
# Trimming #####################################################################
trimmedDir <- fastqDir
windowsizetrimming <- 15
qualcuttrimming <- 25
phred <- "-phred33"
leading <- 3
trailing <- 3
minlen <- 30
if (length(list.files(trimmedDir, pattern = "\\.trimo(\\.pe)*\\.fastq$")) > 0){
warning(
paste0("Directory \"", trimmedDir, "\" not empty! Overwrite? ", FORCE_OVERWRITE),
immediate. = TRUE
)
}
trimming_res <- run_Trimmomatic(
files = raw_fastq_files,
is.paired = paired,
outDir = trimmedDir,
cpus = MAX_CPUS,
windowsize = windowsizetrimming,
qualcut = qualcuttrimming,
phred = phred,
leading = leading,
trailing = trailing,
minlen = minlen
)
# Number of reads after trimming based on Trimmomatic output
number_raw <- trimming_res$input
number_trimmed <- trimming_res$surviving
if (paired) {
trimmed_fastq_files <- asPairVector(trimming_res$files)
} else {
trimmed_fastq_files <- trimming_res$files
}
# Sanity check (Can be ignored during experimental sessions)
stopifnot(length(trimmed_fastq_files) == length(raw_fastq_files))
names(number_raw) <- basename(raw_fastq_files)
names(number_trimmed) <- basename(trimmed_fastq_files)
fastq_files <- trimmed_fastq_files
# SortMeRNA (removes rRNA reads) ###############################################
filterrRNA <- TRUE
sortmeDir <- file.path(fastqDir, "sortmerna")
if (filterrRNA && length(dir(sortmeDir)) > 0){
warning(
paste0("Directory \"", sortmeDir, "\" not empty! Overwrite? ", FORCE_OVERWRITE),
immediate. = TRUE
)
}
if (filterrRNA) {
sortmerna_res <- run_SortMeRNA(
files = fastq_files,
outDir = fastqDir,
mode = "fast",
paired = paired,
cpus = MAX_CPUS
)
non_rrna_files <- sortmerna_res$files
number_nonrRNA <- sortmerna_res$nonrrna
}
fastq_files <- if (filterrRNA) non_rrna_files else trimmed_fastq_files
# Check whether sample/file order has changed:
stopifnot(order(trimmed_fastq_files) == order(non_rrna_files))
# Quality Control 2 (After Trimming) ###########################################
qualitySubDir <- if (filterrRNA) {
file.path(qualDir, "non_rrna")
} else {
file.path(qualDir, "trimmed")
}
if (length(dir(qualitySubDir)) > 0){
warning(
paste0("Directory \"" , qualitySubDir, "\" not empty! overwrite? ",
FORCE_OVERWRITE)
)
}
if (FORCE_OVERWRITE || length(dir(qualitySubDir)) == 0){
writeLines("FastQC - trimmed ...")
fq_res <- run_FastQC(
fastq_files,
outDir = qualitySubDir,
cpus = MAX_CPUS,
extend = FALSE
)
}
# Mapping ######################################################################
# NOTE: Be sure that "index_map" is associated with your "index" object. Not
# doing this step as it is mentioned will end up in a HISAT2 error.
index_map <- index
anno_map <- anno
mapper <- "hisat2"
bamDir <- file.path(mapDir, "bamfiles")
samDir <- file.path(mapDir, "samfiles")
convert_sam <- TRUE # Should SAM files be converted to BAM files if BAM files are not found?
if (mapper == "tophat2"){
mapping_res <- run_Tophat(
fastq_files,
index = index_map,
outDir = mapDir,
is.paired = paired,
anno = anno_map,
addArgs = NA, # "-g 2 --b2-very-sensitive --no-coverage-search",
cpus = MAX_CPUS,
worker = if (paired) 5 else 10,
use.existing = TRUE,
overwrite = TRUE
)
bam_files <- mapping_res$files
} else {
if (FORCE_OVERWRITE || length(dir(samDir)) == 0){
mapping_res <- run_Hisat2(
files = fastq_files,
index = index_map,
outDir = mapDir,
is.paired = paired,
addArgs = "",
splice = TRUE,
cpus = MAX_CPUS,
overwrite = TRUE
)
## NOTE: use that only if the run_Hisat2() parameter 'as.bam' is FALSE.
# bam_files <- sam_to_bam(mapping_res$mapFiles, sort=TRUE, bamDir = bamDir)
bam_files <- mapping_res$files
} else {
if (convert_sam) {
sam_files <- list.files(samDir, pattern = "\\.sam$", full.names = TRUE)
if (length(sam_files) != number_samples)
stop(
paste("Invalid number of SAM files. Expected", number_samples, "but got",
length(sam_files))
)
bam_files <- sam_to_bam(mapping_res$mapFiles, sort=TRUE, bamDir = bamDir)
} else
bam_files <- list.files(bamDir, pattern = "\\.bam$", full.names = TRUE)
if (length(bam_files) != number_samples)
warning(
paste(
"Found",
length(bam_files),
"BAM files, but expected",
number_samples,
". Maybe SAMtools was interrupted. Try to convert again? ",
convert_sam),
immediate. = TRUE)
}
}
# Order BAM files properly
bam_files_sorted <- unlist(
mclapply(bam_files, sortBAMs, overwrite = FORCE_OVERWRITE, mc.cores = MAX_CPUS)
)
# Counting #####################################################################
anno_count <- anno
if (length(dir(countDir)) > 0){
warning(
paste("Directory", countDir, "not empty! Overwrite?", FORCE_OVERWRITE),
immediate. = TRUE
)
}
if (FORCE_OVERWRITE || length(dir(countDir)) == 0){
res_counting <- run_featureCounts(
files = bam_files_sorted,
annotation = anno_count,
isGTF = TRUE,
IDtype = "gene_id",
featureType = "exon",
outDir = countDir,
isPairedEnd = paired,
allowMultiOverlap = FALSE,
cpus = MAX_CPUS
)
} else {
res_counting <- read.featureCounts.files(countDir)
}
# Save results
counts <- res_counting$counts
countfile <- res_counting$countFile
sumfile <- res_counting$sumFile
info <- res_counting$anno
lib_sizes <- res_counting$summary[,1]
gene_lengths <- info$Length
names(gene_lengths) <- info$GeneID
# Rename sample names for "lib_sizes" and "counts"
names(lib_sizes) <- str_replace_all(
string = names(lib_sizes),
pattern = "_\\d.*",
replacement = ""
)
colnames(counts) <- str_replace_all(
string = colnames(counts),
pattern = "_\\d.*",
replacement = ""
)
# Write counts to CSV file
write_count_table(
file = file.path(tabDir, "counts"),
counts = counts
)
# Write counts to XLS file
write_count_table(
file = file.path(tabDir, "counts"),
counts = counts,
as.xls = TRUE,
sheetNames = basename(getwd())
)
# Check for rRNA contamination #################################################
detect_high_coverage(counts, lib_sizes)
# SAMtools #####################################################################
flagstatDir <- file.path(mapDir, "flagstats")
flag_files <- make_flagstats(bam_files_sorted, flagstatDir, MAX_CPUS)
# MultiQC ######################################################################
multiqc_config_yaml <- "multiqc_config.yaml"
run_MultiQC(
tools = c(
"fastqc",
"trimmomatic",
"sortmerna",
"tophat",
"hisat2",
"samtools",
"featureCounts"
),
config = multiqc_config_yaml,
force = FORCE_OVERWRITE
)
# Mapping stats ################################################################
if (!exists("number_trimmed")) {
warning("Variable 'number_trimmed' undefined. Setting it to NA.")
number_trimmed <- NA
}
if (!exists("number_nonrRNA")) {
warning("Variable 'number_nonrRNA' undefined. Setting it to NA.")
number_nonrRNA <- NA
}
# NOTE: Precise mapping stats requires BAM sorting.
# If TRUE, Bioconductor functions are used to determine mapping stats.
precise <- TRUE
mapping_stats_df <- calc_mapping_stats(
bamFiles = bam_files_sorted,
fqFiles = raw_fastq_files,
anno = anno_map, # annotation used for mapping ~ can be different from counting!
numReads = number_raw, # number of raw reads - mandatory!
numTrimmed = number_trimmed, # number of remaining reads after trimming - or NA
numNonrRNA = number_nonrRNA, # number of remaining reads after SortMeRNA - or NA
libSizes = lib_sizes,
paired = paired,
precise = precise,
remove.na = FALSE,
cpus = MAX_CPUS,
featureType = "exon",
samples = NA
)
mapping_stats_df_corrected <- mapping_stats_df %>%
remove_rownames() %>%
mutate(ID = str_extract(string = fastq_files, pattern = "R\\d+")) %>%
select(ID, everything()) %>%
mutate(ID = make.unique(names = ID, sep = "_")) %>%
mutate(ID = str_replace_all(ID, "_1", "_2")) %>%
mutate(ID = str_replace_all(ID, "(R\\d+$)", "\\1_1")) %>%
arrange(ID)
# Save mapping stats
data.table::fwrite(
x = mapping_stats_df_corrected,
file = paste0(tabDir, "/mapping_stats_df.tsv"),
sep = "\t",
quote = FALSE
)
WriteXLS::WriteXLS(
x = mapping_stats_df_corrected,
ExcelFileName = paste0(tabDir, "/mapping_stats_df.xlsx"),
FreezeCol = 1
)
# Sanity check
if (paired) {
if (F %in% (asPaired(number_raw) >= lib_sizes))
stop("Error: number of reads mapping in exons should not exceed original number of reads!")
} else {
if (F %in% (number_raw >= lib_sizes))
stop("Error: number of reads mapping in exons should not exceed original number of reads!")
}
# Normalized count values ######################################################
rpkm <- get_rpkm(counts, gene_lengths, lib_sizes)
tpm <- get_tpm(counts, gene_lengths, lib_sizes)
mrn <- get_mrn(counts)
# Write values to CSV file
write_count_table(
file = file.path(tabDir, "rpkm"),
counts = rpkm
)
write_count_table(
file = file.path(tabDir, "tpm"),
counts = tpm
)
write_count_table(
file = file.path(tabDir, "mrn"),
counts = mrn
)
# Write to XLS file
write_count_table(
file = file.path(tabDir, "rpkm"),
counts = rpkm,
as.xls = TRUE,
sheetNames = basename(getwd())
)
write_count_table(
file = file.path(tabDir, "tpm"),
counts = tpm,
as.xls = TRUE,
sheetNames = basename(getwd())
)
write_count_table(
file = file.path(tabDir, "mrn"),
counts = mrn,
as.xls = TRUE,
sheetNames = basename(getwd())
)
# Clustering ###################################################################
# Load sample information and design matrix
sample_info <- readxl::read_excel(path = "path_to_metadata.xlsx")
design_matrix <- sample_info %>%
mutate(pos_vs_neg = case_when(
Sample %in% str_subset(Sample, "pos") ~ "treatment",
Sample %in% str_subset(Sample, "neg") ~ "control"
)) %>%
column_to_rownames("ID") %>%
select(-Sample) %>%
as.matrix()
conds <- conditions_from_design(design_matrix)
conds
# Hierarchical clustering heat map version
make_heat_clustering_plot(
file.path(plotDir, "heat_hierarchical_clustering"),
counts = counts[, conds != "none"],
conds = conds[conds != "none"],
overwrite = TRUE
)
# Hierarchical clustering
make_hclust_plot(
file.path(plotDir, "hierarchical_clustering"),
counts = counts[, conds != "none"],
conds = conds[conds != "none"],
overwrite = TRUE
)
# Pearson correlation ##########################################################
make_correlation_plots(
dat = mrn,
outDir = plotDir,
prefix = "corr_",
designMatrix = design_matrix,
overwrite = TRUE
)
# PCA plots ####################################################################
# NOTE: if designMatrix is supplied, 'conds' is ignored.
# In that case, use 'designMatrix = NA'
mrn_to_pca <- t(mrn)[, which(apply(t(mrn), 2, var) != 0)]
vst_to_pca <- DESeq2::varianceStabilizingTransformation(object = counts) %>% t()
pca_object <- prcomp(x = vst_to_pca, center = T)
pca_var <- round(x = summary(pca_object)$importance[2,] * 100, digits = 2)
pca_df <- pca_object$x %>%
as.data.frame() %>%
select(PC1, PC2) %>%
rownames_to_column("ID") %>%
left_join(x = ., y = sample_info, by = "ID") %>%
mutate(Group = case_when(
Sample %in% str_subset(string = Sample, pattern = "pos") ~ "Positive",
Sample %in% str_subset(string = Sample, pattern = "neg") ~ "Negative"
))
pca_plot <- ggplot(data = pca_df, mapping = aes(x = PC1, y = PC2, fill = Group)) +
geom_point(size = 8, shape = 21, color = "black") +
labs(
x = paste0("PC1 (", pca_var[1], "% variance explained)"),
y = paste0("PC2 (", pca_var[2], "% variance explained)"),
title = "Principal Component Analysis",
subtitle = paste0(
"(", pca_var[1] + pca_var[2], "% cumulative variance explained)"
)
) +
scale_fill_brewer(palette = "Set1") +
theme(
aspect.ratio = 1,
panel.background = element_blank(),
panel.border = element_rect(fill = NA, size = 1),
text = element_text(size = 15),
axis.title = element_text(face = "bold"),
axis.title.x = element_text(vjust = -1),
plot.title = element_text(face = "bold"),
legend.key = element_blank(),
legend.title = element_text(face = "bold", hjust = 0.5)
)
barplot_variance <- data.frame(Variance_Explained = pca_var) %>%
rownames_to_column("PC") %>%
ggplot(aes(x = PC, y = Variance_Explained)) +
geom_col(color = "black", fill = "dodgerblue") +
geom_label(
aes(label = Variance_Explained),
position = position_dodge(width = 0.9),
size = 8
) +
labs(x = "Principal Components", y = "Variance Explained") +
theme(
panel.background = element_blank(),
panel.grid = element_line(colour = "grey90"),
panel.border = element_rect(fill = NA, size = 1),
text = element_text(size = 15),
axis.title = element_text(face = "bold"),
axis.title.x = element_text(vjust = -1),
plot.title = element_text(face = "bold"),
legend.key = element_blank(),
legend.title = element_text(face = "bold", hjust = 0.5)
)
cairo_pdf(
filename = "file_name.pdf"),
width = 10,
height = 10,
onefile = TRUE,
family = "sans",
antialias = "subpixel"
)
pca_plot
barplot_variance
dev.off()
# NOTE: row names of design matrix has to be the same order as column names of
# counts dataset:
# colnames(counts) <- samples (only in case of having a SDRF file)
if (all(colnames(counts) != rownames(design_matrix))){
counts_ordered <- counts[, match(rownames(design_matrix), colnames(counts))]
} else {
counts_ordered <- counts
}
# Differential Expressed Genes #################################################
pvalcut <- 0.05
logfcCut <- 1
tools <- c("DESeq", "DESeq2", "edgeR", "limma")
deg_anno <- "" # Fill in if you need additional annotation in DEG tables (optional)
deg_res <- calculate_DEGs(
counts = counts_ordered,
geneLengths = gene_lengths,
libSizes = lib_sizes,
designMatrix = design_matrix,
pValCut = pvalcut,
logfcCut = logfcCut,
tools = tools,
outDir = degDir,
prefix = "",
anno = deg_anno,
stop.on.error = FALSE,
cpus = 1,
workers = 10
)
# Intersection of DEGs
deg_intersect <- make_deg_overview_plot(
outDir = degDir,
degs = deg_res$DEGs,
tools = tools
)
print(deg_intersect)
# Export resulting tables
data.table::fwrite(
x = as.data.frame(deg_intersect),
file = "deg_intersect.tsv",
quote = FALSE,
sep = "\t"
)
WriteXLS::WriteXLS(
x = as.data.frame(deg_intersect),
ExcelFileName = "deg_intersect.xlsx",
row.names = TRUE
)
# Union of DEGs
degs_union <- deg_res$DEGs$pos_vs_neg %>%
filter(DESeq == TRUE | DESeq2 == TRUE | Limma == TRUE | EdgeR == TRUE)
paste0("Union of DEGs (n): ", degs_union$id %>% length())
# Export resulting tables
data.table::fwrite(
x = degs_union,
file = "degs_union.tsv",
quote = FALSE,
sep = "\t"
)
WriteXLS::WriteXLS(
x = degs_union,
ExcelFileName = "degs_union.xlsx",
row.names = TRUE
)