| 1 |
## read-sim.R -- contaminated read simulation using real Illumina qualities |
| 2 |
|
| 3 |
NUCLEOTIDES <- c('A', 'T', 'C', 'G') |
| 4 |
#adapters <- list("solexa_adapter_1"="AGATCGGAAGAGCTCGTATGCCGTCTTCTGCTTG") |
| 5 |
adapter <- "AGATCGGAAGAGCTCGTATGCCGTCTTCTGCTTG" |
| 6 |
#adapter <- "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" |
| 7 |
|
| 8 |
set.seed(0) |
| 9 |
|
| 10 |
addErrors <- |
| 11 |
# given a sequence, add errors based on an Illumina-encoded quality line |
| 12 |
function(seq, quality) { |
| 13 |
# Note: I have checked this against |
| 14 |
# http://seqanswers.com/forums/showthread.php?t=1523 and it appears |
| 15 |
# correct - VB |
| 16 |
stopifnot(nchar(seq) == nchar(quality)) |
| 17 |
|
| 18 |
probs.wrong <- sapply(charToRaw(quality), function(x) 1/(10^((as.integer(x) - 64)/10))) |
| 19 |
paste(mapply(function(base, p.wrong) { |
| 20 |
if (rbinom(1, 1, p.wrong)) |
| 21 |
return(sample(setdiff(NUCLEOTIDES, base), 1)) |
| 22 |
else |
| 23 |
return(base) |
| 24 |
}, unlist(strsplit(seq, '')), probs.wrong), collapse="") |
| 25 |
} |
| 26 |
|
| 27 |
generateRandomSeq <- |
| 28 |
# generate a random sequence, possibly with contamination of an adapter, it's length uniform |
| 29 |
function(length, adapter=NULL, quality=NULL, is.contam=FALSE, min.contam=3) { |
| 30 |
if (!is.contam) |
| 31 |
paste(sample(NUCLEOTIDES, length, replace=TRUE), collapse="") |
| 32 |
else { |
| 33 |
ll <- sample(min.contam:nchar(adapter), 1) |
| 34 |
contam <- substr(adapter, 1, ll) |
| 35 |
pre.error.seq <- paste(paste(sample(NUCLEOTIDES, length-nchar(contam), replace=TRUE), collapse=""), contam, sep="") |
| 36 |
return(list(contam.n=ll, seq=addErrors(pre.error.seq, quality))) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
contaminateFASTQEntry <- function(con, outfile, rate, min.contam=3, verbose=FALSE) { |
| 41 |
blocks.processed <- 0 |
| 42 |
reads <- readLines(con) |
| 43 |
outlist <- vector('character', length(reads)) |
| 44 |
while (blocks.processed*4 < length(reads)) { |
| 45 |
quality <- reads[4*blocks.processed+4] |
| 46 |
seq <- reads[4*blocks.processed+2] |
| 47 |
header <- reads[4*blocks.processed+1] |
| 48 |
|
| 49 |
if (runif(1) <= rate) { |
| 50 |
# contaminate |
| 51 |
tmp <- generateRandomSeq(nchar(seq), adapter=adapter, quality=quality, is.contam=TRUE, min.contam=min.contam) |
| 52 |
seq <- tmp$seq |
| 53 |
ll <- tmp$contam.n |
| 54 |
header <- sprintf("%s-contaminated-%d", header, ll) |
| 55 |
} else { |
| 56 |
header <- sprintf("%s-uncontaminated", header) |
| 57 |
seq <- generateRandomSeq(nchar(seq), is.contam=FALSE, min.contam=min.contam) |
| 58 |
} |
| 59 |
|
| 60 |
# output results to vector |
| 61 |
outlist[4*blocks.processed+1] <- header |
| 62 |
outlist[4*blocks.processed+2] <- seq |
| 63 |
outlist[4*blocks.processed+3] <- sprintf("+%s", substr(header, 2, nchar(header))) |
| 64 |
outlist[4*blocks.processed+4] <- quality |
| 65 |
|
| 66 |
blocks.processed <- blocks.processed + 1 |
| 67 |
if (blocks.processed %% 100 == 0 && verbose) |
| 68 |
message(sprintf("%d blocks processed.", blocks.processed)) |
| 69 |
} |
| 70 |
of <- file(outfile) |
| 71 |
writeLines(outlist, con=of) |
| 72 |
close(of) |
| 73 |
} |
| 74 |
|