| 1 |
## analysis.R -- read in sequences to analyze trimming results |
| 2 |
## note: assumes POSIX directory delimiter ("/") |
| 3 |
|
| 4 |
library(Biostrings) |
| 5 |
library(ShortRead) |
| 6 |
library(grid) |
| 7 |
|
| 8 |
# note: we use ShortRead because Biostrings can't: |
| 9 |
# |
| 10 |
# (1) read variable length FASTQ |
| 11 |
# (2) drops quality info |
| 12 |
|
| 13 |
### Settings |
| 14 |
# Currently, we're using simulated reads with a contamination rate of |
| 15 |
# 40% |
| 16 |
sim.contam.rate <- c(0.4, 0.7) |
| 17 |
|
| 18 |
# Here are the trimmer we are testing; the names refer to the output |
| 19 |
# directories of these trimmers |
| 20 |
trimmers <- c("scythe", "btrim", "cutadapt") |
| 21 |
|
| 22 |
# Simulatd read directories |
| 23 |
sim.read.dir <- "simulated-reads" |
| 24 |
|
| 25 |
### FASTQ file reading |
| 26 |
# This is designed for handling many contamination rates, even though |
| 27 |
# we're currently using only one. |
| 28 |
sim.reads <- lapply(sim.contam.rate, function(contam) { |
| 29 |
# list replicate FASTQ files |
| 30 |
reps <- list.files(file.path(sim.read.dir, contam), full.names=TRUE) |
| 31 |
|
| 32 |
# extract rep name |
| 33 |
rep.names <- sapply(basename(reps), function(n) { |
| 34 |
gsub("(\\d+)\\.fastq", "\\1", n) |
| 35 |
}) |
| 36 |
|
| 37 |
# read in FASTQ files |
| 38 |
out <- lapply(reps, function(f) { |
| 39 |
readFastq(f) |
| 40 |
}) |
| 41 |
names(out) <- rep.names |
| 42 |
out |
| 43 |
}) |
| 44 |
names(sim.reads) <- sim.contam.rate |
| 45 |
|
| 46 |
### Read in trimmed FASTQ files |
| 47 |
trimmed.results.dir <- "results" |
| 48 |
trimmed.reads <- lapply(trimmers, function(trimmer) { |
| 49 |
|
| 50 |
# iterate over contam rates for each trimmer, gather files into list |
| 51 |
found.contam.rates <- list.files(file.path(trimmer, trimmed.results.dir), full.name=TRUE) |
| 52 |
|
| 53 |
out <- lapply(found.contam.rates, function(path) { |
| 54 |
ff <- list.files(path, pattern="trimmed-.*", full.names=TRUE) |
| 55 |
trimmed.md <- local({ |
| 56 |
tmp <- gsub("trimmed-(\\d+)-(0?\\.?\\d+)\\.fastq", "\\2;;;;\\1", |
| 57 |
basename(ff)) |
| 58 |
reads <- lapply(ff, readFastq) |
| 59 |
names(reads) <- tmp |
| 60 |
reads |
| 61 |
}) |
| 62 |
}) |
| 63 |
|
| 64 |
names(out) <- basename(found.contam.rates) |
| 65 |
out |
| 66 |
}) |
| 67 |
names(trimmed.reads) <- trimmers |
| 68 |
|
| 69 |
### Main Analysis |
| 70 |
|
| 71 |
extractContam <- |
| 72 |
# extract the contamination from the header. |
| 73 |
function(x) { |
| 74 |
if (regexpr("uncontaminated", x) != -1) |
| 75 |
return(0) |
| 76 |
return(as.numeric(gsub(".*-contaminated-([0-9]+)", "\\1", x))) |
| 77 |
} |
| 78 |
|
| 79 |
compareFastqTrim <- function(original, trimmed) { |
| 80 |
original.d <- data.frame(id=as.character(id(original)), read=as.character(sread(original)), stringsAsFactors=FALSE) |
| 81 |
trimmed.d <- data.frame(id=as.character(id(trimmed)), read=as.character(sread(trimmed)), stringsAsFactors=FALSE) |
| 82 |
|
| 83 |
# full outer join of reads |
| 84 |
x <- merge(original.d, trimmed.d, by='id', all=TRUE) |
| 85 |
colnames(x) <- c("id", "original.read", "trimmed.read") |
| 86 |
|
| 87 |
x[is.na(x)] <- "" |
| 88 |
# Now, get actual contamination rate per sequence |
| 89 |
x$n.contam <- sapply(x$id, extractContam) |
| 90 |
|
| 91 |
# get sequence length difference (due to trimming) |
| 92 |
x$n.trimmed <- with(x, nchar(original.read) - nchar(trimmed.read)) |
| 93 |
x[, -c(2, 3)] |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
calcStats <- function(n.trimmed, n.contam) { |
| 98 |
stopifnot(length(n.trimmed) == length(n.contam)) |
| 99 |
n <- length(n.trimmed) |
| 100 |
false.positives <- sum(n.trimmed > 0 & n.contam == 0) |
| 101 |
true.positives <- sum(n.trimmed == n.contam & n.contam > 0) |
| 102 |
true.negatives <- sum(n.trimmed == n.contam & n.contam == 0) |
| 103 |
false.negatives <- sum(n.trimmed == 0 & n.contam > 0) |
| 104 |
incorrectly.trimmed <- sum(n.trimmed > 0 & n.contam > 0 & n.contam != n.trimmed) |
| 105 |
data.frame(false.positives, true.positives, incorrectly.trimmed, |
| 106 |
false.negatives, true.negatives, total=n) |
| 107 |
} |
| 108 |
|
| 109 |
## Now compare trimmed reads with original. |
| 110 |
sim.read.files <- list.files(sim.read.dir, recursive=TRUE) |
| 111 |
trimmed.read.files <- local({ |
| 112 |
f <- list.files(trimmers, recursive=TRUE, full.names=TRUE, pattern="trimmed-.*") |
| 113 |
tmp <- gsub("([a-z]+)/results/([\\.\\d]+)/trimmed-(\\d+)-([\\.\\d]+)\\.fastq", "\\1;;;\\2;;;\\3;;;\\4", |
| 114 |
f, perl=TRUE) |
| 115 |
chunks <- strsplit(tmp, ";;;") |
| 116 |
x <- do.call(rbind, chunks) |
| 117 |
data.frame(file=f, trimmer=x[, 1], contam.rate=x[, 2], rep=x[, 3], parameter=x[, 4], stringsAsFactors=FALSE) |
| 118 |
}) |
| 119 |
|
| 120 |
|
| 121 |
results <- with(trimmed.read.files, |
| 122 |
mapply(function(f, trimmer, contam, rep, parameter) { |
| 123 |
## get corresponding simulated reads |
| 124 |
sr <- sim.reads[[contam]][[rep]] |
| 125 |
|
| 126 |
## get corresponding trimmed reads |
| 127 |
tr <- trimmed.reads[[trimmer]][[contam]][[paste(parameter, rep, sep=";;;;")]] |
| 128 |
|
| 129 |
tmp <- compareFastqTrim(sr, tr) |
| 130 |
out <- with(tmp, calcStats(n.trimmed, n.contam)) |
| 131 |
out$trimmer <- trimmer |
| 132 |
out$contam.rate <- contam |
| 133 |
out$rep <- rep |
| 134 |
out$parameter <- parameter |
| 135 |
out |
| 136 |
}, file, trimmer, contam.rate, rep, parameter, SIMPLIFY=FALSE)) |
| 137 |
|
| 138 |
|
| 139 |
### Analysis of results |
| 140 |
# Currently the data is at the read-level. We need to summarize the |
| 141 |
# results at the trimmer, contamination, and parameter level. |
| 142 |
d <- do.call(rbind, results) |
| 143 |
|
| 144 |
|
| 145 |
addRates <- |
| 146 |
# add FP and TP rates for generating ROC curves |
| 147 |
function(x) { |
| 148 |
x$tpr <- with(x, true.positives/(true.positives + false.negatives)) |
| 149 |
x$fpr <- with(x, false.positives/(false.positives + true.negatives)) |
| 150 |
x |
| 151 |
} |
| 152 |
|
| 153 |
d <- addRates(d) |
| 154 |
write.table(d, file="testing-results-table.txt", sep="\t", quote=FALSE, row.names=FALSE) |
| 155 |
|
| 156 |
##### Graphics |
| 157 |
|
| 158 |
## summarized d; take mean across replicates. |
| 159 |
remove.cols <- which(colnames(d) %in% c("trimmer", "parameter", "rep", "contam.rate", "total")) |
| 160 |
ds <- aggregate(d[, -remove.cols], list(trimmer=d$trimmer, parameter=d$parameter, contam.rate=d$contam.rate), mean) |
| 161 |
stopifnot(length(unique(d$total)) == 1) |
| 162 |
ds$total <- unique(d$total) |
| 163 |
|
| 164 |
bs <- 9 |
| 165 |
p <- ggplot(ds) + geom_text(aes(x=fpr, y=tpr, color=trimmer, label=parameter), size=3) |
| 166 |
p <- p + scale_y_continuous("true positive rate") |
| 167 |
p <- p + scale_x_continuous("false positive rate") |
| 168 |
p <- p + theme_bw(base_size=bs) + facet_wrap(~ contam.rate) |
| 169 |
p <- p + opts(title="(a) ROC Curve for Btrim, Cutadapt, and Scythe Across\n40% and 70% Read Contamination Rates") |
| 170 |
#ggsave(file="trimmer-roc-curve.png", plot=p, height=600, width=800) |
| 171 |
|
| 172 |
## look at incorrect trimmed |
| 173 |
ds$width <- ifelse(ds$trimmer == 'btrim', 0.8, 0.04) |
| 174 |
q <- ggplot(ds, aes(x=parameter, y=incorrectly.trimmed/total, width=width)) + geom_bar(stat="identity") |
| 175 |
q <- q + scale_y_continuous("proportion\nincorrectly\ntrimmed") |
| 176 |
q <- q + facet_wrap(~ trimmer, scales="free_x") + theme_bw(base_size=bs) |
| 177 |
q <- q + opts(title="(b) Incorrectly Trimmed Reads by Trimmer and Parameter") |
| 178 |
|
| 179 |
vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y) |
| 180 |
|
| 181 |
postscript(file="../paper/graphics/roc-and-incorrect-trimmed.eps", width=6.4, height=5) |
| 182 |
grid.newpage() |
| 183 |
pushViewport(viewport(layout = grid.layout(10, 10))) |
| 184 |
print(p, vp = vplayout(1:6, 1:10)) |
| 185 |
print(q, vp = vplayout(7:10, 1:9)) |
| 186 |
dev.off() |