3 argv <- commandArgs(TRUE)
4 if (length(argv) != 2) {
5 cat("Usage: rsem-plot-model sample_name output_plot_file\n")
9 strvec <- strsplit(argv[1], split = "/")[[1]]
10 token <- strvec[length(strvec)]
12 stat.dir <- paste(argv[1], ".stat", sep = "")
13 if (!file.exists(stat.dir)) {
14 cat("Error: directory does not exist: ", stat.dir, "\n", sep = "")
15 cat(strwrap("This version of rsem-plot-model only works with the output of RSEM versions >= 1.1.8"), sep="\n")
18 modelF <- paste(stat.dir, "/", token, ".model", sep = "")
19 cntF <- paste(stat.dir, "/", token, ".cnt", sep = "")
23 con <- file(modelF, open = "r")
25 # model type and forward probability
26 model_type <- as.numeric(readLines(con, n = 4)[1])
28 # fragment length distribution
29 strvec <- readLines(con, n = 3)
30 vec <- as.numeric(strsplit(strvec[1], split = " ")[[1]])
31 maxL <- vec[2] # maxL used for Profile
32 x <- (vec[1] + 1) : vec[2]
33 y <- as.numeric(strsplit(strvec[2], split = " ")[[1]])
34 mean <- weighted.mean(x, y)
35 std <- sqrt(weighted.mean((x - mean)^2, y))
36 plot(x, y, type = "h", main = "Fragment Length Distribution", sub = paste("Mean = ", mean, ", Std = ", std), xlab = "Fragment Length", ylab = "Probability")
38 # mate length distribution
39 if (model_type == 0 || model_type == 1) bval <- as.numeric(readLines(con, n = 1)[1]) else bval <- 1
42 list <- strsplit(readLines(con, n = 2), split = " ")
43 vec <- as.numeric(list[[1]])
45 x <- (vec[1] + 1) : vec[2]
46 y <- as.numeric(list[[2]])
47 mean <- weighted.mean(x, y)
48 std <- sqrt(weighted.mean((x - mean)^2, y))
49 plot(x, y, type = "h", main = "Mate Length Distribution", sub = paste("Mean = ", mean, ", Std = ", std), xlab = "Mate Length", ylab = "Probability")
51 strvec <- readLines(con, n = 1)
54 bval <- as.numeric(readLines(con, n = 1)[1])
56 bin_size <- as.numeric(readLines(con, n = 1)[1])
57 y <- as.numeric(strsplit(readLines(con, n = 1), split = " ")[[1]])
59 barplot(y, space = 0, names.arg = 1:bin_size, main = "Read Start Position Distribution", xlab = "Bin #", ylab = "Probability")
62 strvec <- readLines(con, n = 1)
64 # plot sequencing errors
65 if (model_type == 1 || model_type == 3) {
67 N <- as.numeric(readLines(con, n = 1)[1])
68 readLines(con, n = N + 1)
69 readLines(con, n = 1) # for the blank line
75 peA <- c() # probability of sequencing error given reference base is A
81 strvec <- readLines(con, n = 6)
82 list <- strsplit(strvec[1:4], split = " ")
84 vecA <- as.numeric(list[[1]])
85 vecC <- as.numeric(list[[2]])
86 vecG <- as.numeric(list[[3]])
87 vecT <- as.numeric(list[[4]])
89 if (sum(c(vecA, vecC, vecG, vecT)) < 1e-8) next
91 peA <- c(peA, ifelse(sum(vecA) < 1e-8, NA, -10 * log10(1.0 - vecA[1])))
92 peC <- c(peC, ifelse(sum(vecC) < 1e-8, NA, -10 * log10(1.0 - vecC[2])))
93 peG <- c(peG, ifelse(sum(vecG) < 1e-8, NA, -10 * log10(1.0 - vecG[3])))
94 peT <- c(peT, ifelse(sum(vecT) < 1e-8, NA, -10 * log10(1.0 - vecT[4])))
97 matplot(x, cbind(peA, peC, peG, peT), type = "b", lty = 1:4, pch = 0:3, col = 1:4, main = "Phred Quality Score vs. Observed Quality", xlab = "Quality Score", ylab = "Observed Quality")
98 legend("topleft", c("A", "C", "G", "T"), lty = 1:4, pch = 0:3, col = 1:4)
101 readLines(con, n = 1)
104 peA <- c() # probability of sequencing error given reference base is A
110 strvec <- readLines(con, n = 6)
111 list <- strsplit(strvec[1:4], split = " ")
113 vecA <- as.numeric(list[[1]])
114 vecC <- as.numeric(list[[2]])
115 vecG <- as.numeric(list[[3]])
116 vecT <- as.numeric(list[[4]])
118 if (sum(c(vecA, vecC, vecG, vecT)) < 1e-8) next
120 peA <- c(peA, ifelse(sum(vecA) < 1e-8, NA, (1.0 - vecA[1]) * 100))
121 peC <- c(peC, ifelse(sum(vecC) < 1e-8, NA, (1.0 - vecC[2]) * 100))
122 peG <- c(peG, ifelse(sum(vecG) < 1e-8, NA, (1.0 - vecG[3]) * 100))
123 peT <- c(peT, ifelse(sum(vecT) < 1e-8, NA, (1.0 - vecT[4]) * 100))
126 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")
127 legend("topleft", c("A", "C", "G", "T"), lty = 1:4, pch = 0:3, col = 1:4)
132 pair <- read.table(file = cntF, skip = 3, sep = "\t")
133 barplot(pair[,2], names.arg = pair[,1], xlab = "Number of Alignments", ylab = "Number of Reads", main = "Histogram of Reads with Different Number of Alignments")