Debian Codegraph @

paper/containers/data/simulated/adjust_error_profiles.R

raw
1 # This script adjusts an ART error profile to increase the cumulative error
2 # rate to a given value.
3
4 ## Optimization of multiplier of counts vector to generate
5 ## a distribution with (approximately) a given error rate
6 q2p <- function(q) 10^(-q/10)
7 cum_err <- function(p, counts) {
8 s <- sum(counts)
9 sum(p * counts) / s
10 }
11 eq <- function(a, b, tol) {
12 return (abs(a-b) <= tol)
13 }
14 new_err <- function(d, p, counts, target.err) {
15 total <- sum(counts)
16 new.counts <- counts + (total * d)
17 abs(cum_err(p, (new.counts / sum(new.counts)) * total) - target.err)
18 }
19 find_new_counts <- function(p, counts, target.err, rescale=TRUE, d0=target.err*0.1) {
20 res <- optim(par=d0, fn=new_err, p=p, counts=counts, target.err=target.err, method = "BFGS")
21 new.counts <- counts + (sum(counts) * res$par)
22 if (rescale) {
23 m <- sum(counts) / sum(new.counts)
24 new.counts <- new.counts * m
25 }
26 list(counts=new.counts, res=res)
27 }
28
29 profile.error <- function(path) {
30 if (endsWith(path, ".gz")) {
31 lines <- readLines(gzfile(path))
32 }
33 else {
34 lines <- readLines(path)
35 }
36 comments <- grep("^#", lines, perl=T)
37 if (length(comments) > 0) {
38 lines <- lines[-comments]
39 }
40 num <- NULL
41 denom <- NULL
42 for (i in seq(1, length(lines), 2)) {
43 q <- unlist(strsplit(lines[i], "\t"))
44 if (length(q) < 3) {
45 next
46 }
47 h <- q[1:2]
48 q <- as.integer(q[3:length(q)])
49 p <- sapply(q, q2p)
50 counts <- unlist(strsplit(lines[i+1], "\t"))
51 stopifnot(counts[1] == h[1] && counts[2] == h[2])
52 counts <- as.numeric(counts[3:length(counts)])
53 stopifnot(length(counts) == length(p))
54 num <- c(num, p*counts)
55 denom <- c(denom, counts)
56 }
57 as.numeric(sum(num)) / as.numeric(sum(denom))
58 }
59
60 ## Generate a new error profile
61 adjust.profile <- function(orig.path, new.path, target.err, rescale=TRUE) {
62 if (endsWith(orig.path, ".gz")) {
63 lines <- readLines(gzfile(orig.path))
64 }
65 else {
66 lines <- readLines(orig.path)
67 }
68 comments <- grep("^#", lines, perl=T)
69 if (length(comments) > 0) {
70 lines <- lines[-comments]
71 }
72 n.lines <- length(lines)
73 read.length <- n.lines / 12
74 new.lines <- lines
75 process.lines <- function(i) {
76 q <- unlist(strsplit(lines[i], "\t"))
77 h <- q[1:2]
78 q <- as.integer(q[3:length(q)])
79 p <- sapply(q, q2p)
80 counts <- unlist(strsplit(lines[i+1], "\t"))
81 stopifnot(counts[1] == h[1] && counts[2] == h[2])
82 counts <- as.numeric(counts[3:length(counts)])
83 stopifnot(length(counts) == length(p))
84 new.counts <- find_new_counts(p, counts, target.err, rescale)
85 list(h=h, q=q, p=p, counts=new.counts$counts, line=paste0(c(h, round(new.counts$counts)), collapse="\t"))
86 }
87 new.N <- rep(NA, read.length)
88 for (ii in 1:read.length) {
89 i <- (ii*2)-1
90 result <- process.lines(i)
91 new.lines[i+1] <- result$line
92 w <- which(result$q == 2)
93 if (length(w) > 0) {
94 new.N[ii] <- result$counts[w[1]]
95 }
96 }
97 end <- read.length * 2 * 5
98 for (i in seq((read.length * 2) + 1, end, 2)) {
99 result <- process.lines(i)
100 new.lines[i+1] <- result$line
101 }
102 for (i in 1:read.length) {
103 if (!is.na(new.N[i])) {
104 new.lines[end+(i*2)] <- paste0(c('N', i-1, new.N[i]), collapse="\t")
105 }
106 }
107 if (endsWith(new.path, ".gz")) {
108 out <- gzfile(new.path, "w")
109 }
110 else {
111 out <- file(new.path, "w")
112 }
113 writeLines(new.lines, out)
114 close(out)
115 }
116
117 parser <- ArgumentParser()
118 parser$add_argument(
119 "-g", "--generate", action="store_true", default=FALSE,
120 help="Generate new profile.")
121 parser$add_argument(
122 "-c", "--calculate", action="store_true", default=FALSE,
123 help="Calculate error rate on existing profile.")
124 parser$add_argument(
125 "-p", "--profile", help="The source ART error profile.")
126 parser$add_argument(
127 "-e", "--error-rate", type="double", action="store_true", default=FALSE,
128 help="Calculate error rate of existing profile.")
129 parser$add_argument(
130 "-o", "--output", default=NULL,
131 help="New profile file.")
132 parser$add_argument(
133 "-t", "--table", default=NULL,
134 help="File where data will be written in tab-delimited format.")
135 args <- parser$parse_args()
136
137 if (args$generate) {
138 # for some reason, the acutal error rate in simulated reads is about twice
139 # what is expected based on the empirical distribution of quality scores
140 error.rate <- as.numeric(args$error_rate) / 2
141 adjust.profile(args$profile, args$output, error.rate)
142 actual.error <- profile.error(args$output)
143 print(paste("The acutal error rate of the new profile is", actual.error))
144 } else if (args$error) {
145 print(paste("The acutal error rate of the profile is", round(profile.error(args$profile), 4)))
146 }
147
148 if (args$table) {
149 if (!file.exists(args$table)) {
150 write(
151 paste("Profile", "RequestedErrorRate", "ActualErrorRate", sep="\t"),
152 file=args$table)
153 }
154 write(
155 paste(basename(args$profile), error.rate, actual.error, sep="\t"),
156 file=args$table, append=TRUE)
157 }