]> git.donarmstrong.com Git - rsem.git/blob - rsem-plot-model
v1.1.5.b
[rsem.git] / rsem-plot-model
1 #!/usr/bin/env Rscript
2
3 argv <- commandArgs(TRUE)
4 if (length(argv) != 2) {
5   cat("Usage: rsem-plot-model modelF outF\n")
6   q(status = 1)
7 }
8
9 con <- file(argv[1], open = "r")        
10 pdf(argv[2])
11
12 # model type and forward probability
13 model_type <- as.numeric(readLines(con, n = 4)[1])  
14
15 # fragment length distribution
16 strvec <- readLines(con, n = 3)
17 vec <- as.numeric(strsplit(strvec[1], split = " ")[[1]])
18 maxL <- vec[2] # maxL used for Profile
19 x <- (vec[1] + 1) : vec[2]
20 y <- as.numeric(strsplit(strvec[2], split = " ")[[1]])
21 mean <- weighted.mean(x, y)
22 std <- sqrt(weighted.mean((x - mean)^2, y))
23 plot(x, y, type = "h", main = "Fragment Length Distribution", sub = paste("Mean = ", mean, ", Std = ", std), xlab = "Fragment Length", ylab = "Probability")  
24
25 # mate length distribution
26 bval <- as.numeric(readLines(con, n = 1)[1])
27 if (bval == 1) {
28   list <- strsplit(readLines(con, n = 2), split = " ")
29   vec <- as.numeric(list[[1]])
30   maxL <- vec[2]
31   x <- (vec[1] + 1) : vec[2]
32   y <- as.numeric(list[[2]])
33   mean <- weighted.mean(x, y)
34   std <- sqrt(weighted.mean((x - mean)^2, y))
35   plot(x, y, type = "h", main = "Mate Length Distribution", sub = paste("Mean = ", mean, ", Std = ", std), xlab = "Mate Length", ylab = "Probability")  
36 }
37 strvec <- readLines(con, n = 1)
38
39 # RSPD
40 bval <- as.numeric(readLines(con, n = 1)[1])
41 if (bval == 1) {
42   bin_size <- as.numeric(readLines(con, n = 1)[1])
43   y <- as.numeric(strsplit(readLines(con, n = 1), split = " ")[[1]])
44   par(cex.axis = 0.7)
45   barplot(y, space = 0, names.arg = 1:bin_size, main = "Read Start Position Distribution", xlab = "Bin #", ylab = "Probability")
46   par(cex.axis = 1.0)
47 }
48 strvec <- readLines(con, n = 1)
49
50 # plot sequencing errors
51 if (model_type == 1 || model_type == 3) {
52   # skip QD
53   N <- as.numeric(readLines(con, n = 1)[1])
54   readLines(con, n = N + 1)
55   readLines(con, n = 1) # for the blank line
56   
57   # QProfile
58   readLines(con, n = 1)
59
60   peA <- c() # probability of sequencing error given reference base is A
61   peC <- c()
62   peG <- c()
63   peT <- c()
64
65   for (i in 1 : N) {
66     strvec <- readLines(con, n = 6)
67     list <- strsplit(strvec[1:4], split = " ")
68     vecA <- as.numeric(list[[1]])
69     vecC <- as.numeric(list[[2]])
70     vecG <- as.numeric(list[[3]])
71     vecT <- as.numeric(list[[4]])
72     if (sum(c(vecA, vecC, vecG, vecT)) < 1e-8) break
73     peA <- c(peA, ifelse(sum(vec) < 1e-8, NA, -10 * log(1.0 - vecA[1])))
74     peC <- c(peC, ifelse(sum(vec) < 1e-8, NA, -10 * log(1.0 - vecC[2])))
75     peG <- c(peG, ifelse(sum(vec) < 1e-8, NA, -10 * log(1.0 - vecG[3])))
76     peT <- c(peT, ifelse(sum(vec) < 1e-8, NA, -10 * log(1.0 - vecT[4])))
77   }
78
79   x <- 0 : (length(peA) - 1)
80   matplot(x, cbind(peA, peC, peG, peT), type = "b", lty = 1:4, pch = 0:3, col = 1:4, main = "Quality Score vs. Observed Quality", xlab = "Quality Score", ylab = "Observed Quality")
81   legend("topleft", c("A", "C", "G", "T"), lty = 1:4, pch = 0:3, col = 1:4)
82 } else {
83   # Profile
84   readLines(con, n = 1)
85   
86   peA <- c() # probability of sequencing error given reference base is A
87   peC <- c()
88   peG <- c()
89   peT <- c()
90
91   for (i in 1: maxL) {
92     strvec <- readLines(con, n = 6)
93     list <- strsplit(strvec[1:4], split = " ")
94     vecA <- as.numeric(list[[1]])
95     vecC <- as.numeric(list[[2]])
96     vecG <- as.numeric(list[[3]])
97     vecT <- as.numeric(list[[4]])
98     if (sum(c(vecA, vecC, vecG, vecT)) < 1e-8) break
99     peA <- c(peA, ifelse(sum(vec) < 1e-8, NA, (1.0 - vecA[1]) * 100))
100     peC <- c(peC, ifelse(sum(vec) < 1e-8, NA, (1.0 - vecC[2]) * 100))
101     peG <- c(peG, ifelse(sum(vec) < 1e-8, NA, (1.0 - vecG[3]) * 100))
102     peT <- c(peT, ifelse(sum(vec) < 1e-8, NA, (1.0 - vecT[4]) * 100))
103   }
104
105   x <- 1 : length(peA)
106   matplot(x, cbind(peA, peC, peG, peT), type = "b", lty = 1:4, pch = 0:3, col = 1:4, main = "Position vs. Percentage Sequence Error", xlab = "Position", ylab = "Percentage of Sequencing Error")
107   legend("topleft", c("A", "C", "G", "T"), lty = 1:4, pch = 0:3, col = 1:4)       
108 }
109
110 dev.off()
111 close(con)