-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcount_reads_table.R
executable file
·73 lines (52 loc) · 2.34 KB
/
count_reads_table.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
### FUNCTION TO COUNT READS FROM FASTQS, BAMFILES AND FCOUNTS OUTPUTS
fqReads <- function(fqcDir, readsCol = "Reads", returnAll = F){
# Load packages
require(dplyr)
require(magrittr)
require(fastqcr)
fqc_info <- fastqcr::qc_aggregate(qc.dir = fqcDir, progressbar = F) %>% suppressMessages()
fqc_stat <- fastqcr::qc_stats(object = fqc_info)
fqc_num <- fqc_stat %>% dplyr::select(sample, tot.seq) %>% magrittr::set_colnames(c("Sample", readsCol))
fqc_num <- fqc_num %>% dplyr::mutate(Sample = Sample %>% gsub(".trim", "", .) %>% gsub(".filt", "", .))
if(returnAll){
output <- list("BasicStats" = fqc_stat, "NumReads" = fqc_num, "AllInfo" = fqc_info)
return(output)
}
return(fqc_num)
}
bamReads <- function(bamDir, bamPattern = "bam$", bamNames = NULL, readsCol = "align_reads"){
# Load packages
require(dplyr)
require(purrr)
require(tibble)
require(magrittr)
require(Rsamtools)
bam_paths <- list.files(path = bamDir, pattern = bamPattern, full.names = T, recursive = T)
if(is.null(bamNames)) { bamNames <- bam_paths %>% purrr::map(~basename(.x) %>% gsub(".bam", "", .)) }
bam_paths <- bam_paths %>% purrr::set_names(bamNames)
bam_stats <- bam_paths %>% purrr::map(~Rsamtools::idxstatsBam(.x)) %>%
purrr::map(~dplyr::select(.x, mapped) %>%
sum() %>%
tibble::tibble(bamreads = .) %>%
magrittr::set_colnames(readsCol)) %>%
purrr::imap(~dplyr::mutate(.x, Sample = .y)) %>%
dplyr::bind_rows()
return(bam_stats)
}
fcountsNum <- function(countsDir, countsPat = "counts.summary$", countsNames = NULL, countsCol = "fcounts"){
require(dplyr)
require(purrr)
require(stringr)
require(magrittr)
require(tibble)
fcounts_paths <- list.files(path = countsDir, pattern = countsPat, full.names = T, recursive = T)
if(is.null(countsNames)) { countsNames <- fcounts_paths %>% purrr::map(~basename(.x) %>% gsub("\\..*", "", .)) }
fcounts_paths <- fcounts_paths %>% purrr::set_names(countsNames)
fcounts_info <- purrr::map(fcounts_paths, read.delim, header = T) %>%
purrr::map(~dplyr::filter(.x, Status == "Assigned")) %>%
purrr::map(~magrittr::set_colnames(.x, c("Status", countsCol))) %>%
purrr::imap(~dplyr::mutate(.x, Sample = .y)) %>%
purrr::map(~dplyr::select(.x, Sample, countsCol)) %>%
dplyr::bind_rows()
return(fcounts_info)
}