0
|
1 #!/usr/bin/env Rscript
|
|
2
|
|
3 suppressPackageStartupMessages(library("optparse"))
|
|
4
|
|
5 option_list <- list(
|
|
6 make_option(c("-c", "--components_input"), action="store", dest="components_input", help="Ks significant components input dataset"),
|
|
7 make_option(c("-k", "--kaks_input"), action="store", dest="kaks_input", help="KaKs analysis input dataset"),
|
9
|
8 make_option(c("-n", "--num_comp"), action="store", dest="num_comp", type="integer", help="Number of significant components in the Ks distribution"),
|
|
9 make_option(c("-o", "--output"), action="store", dest="output", help="Output dataset"),
|
21
|
10 make_option(c("-r", "--colors"), action="store", default=[], help="List of component colors")
|
0
|
11 )
|
|
12
|
|
13 parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
|
|
14 args <- parse_args(parser, positional_arguments=TRUE)
|
|
15 opt <- args$options
|
|
16
|
|
17 get_pi_mu_var = function(components_data, num_components)
|
|
18 {
|
|
19 # FixMe: enhance this to generically handle any integer value for num_components.
|
|
20 if (num_components == 1)
|
|
21 {
|
4
|
22 pi <- c(components_data[1, 9])
|
|
23 mu <- c(components_data[1, 7])
|
|
24 var <- c(components_data[1, 8])
|
0
|
25 }
|
|
26 else if (num_components == 2)
|
|
27 {
|
4
|
28 pi <- c(components_data[2, 9], components_data[3, 9])
|
|
29 mu <- c(components_data[2, 7], components_data[3, 7])
|
|
30 var <- c(components_data[2, 8], components_data[3, 8])
|
0
|
31 }
|
|
32 else if (num_components == 3)
|
|
33 {
|
4
|
34 pi <- c(components_data[4, 9], components_data[5, 9], components_data[6, 9])
|
|
35 mu <- c(components_data[4, 7], components_data[5, 7], components_data[6, 7])
|
|
36 var <- c(components_data[4, 8], components_data[5, 8], components_data[6, 8])
|
0
|
37 }
|
|
38 else if (num_components == 4)
|
|
39 {
|
4
|
40 pi <- c(components_data[7, 9], components_data[8, 9], components_data[9, 9], components_data[10, 9])
|
|
41 mu <- c(components_data[7, 7], components_data[8, 7], components_data[9, 7], components_data[10, 7])
|
|
42 var <- c(components_data[7, 8], components_data[8, 8], components_data[9, 8], components_data[10, 8])
|
0
|
43 }
|
4
|
44 else if (num_components == 5)
|
|
45 {
|
|
46 pi <- c(components_data[11, 9], components_data[12, 9], components_data[13, 9], components_data[14, 9], components_data[15, 9])
|
|
47 mu <- c(components_data[11, 7], components_data[12, 7], components_data[13, 7], components_data[14, 7], components_data[15, 7])
|
|
48 var <- c(components_data[11, 8], components_data[12, 8], components_data[13, 8], components_data[14, 8], components_data[15, 8])
|
|
49 }
|
|
50 else if (num_components == 6)
|
|
51 {
|
|
52 pi <- c(components_data[16, 9], components_data[17, 9], components_data[18, 9], components_data[19, 9], components_data[20, 9], components_data[21, 9])
|
|
53 mu <- c(components_data[16, 7], components_data[17, 7], components_data[18, 7], components_data[19, 7], components_data[20, 7], components_data[21, 7])
|
|
54 var <- c(components_data[16, 8], components_data[17, 8], components_data[18, 8], components_data[19, 8], components_data[20, 8], components_data[21, 8])
|
|
55 }
|
|
56 results = c(pi, mu, var)
|
|
57 return(results)
|
0
|
58 }
|
|
59
|
9
|
60 plot_ks<-function(kaks_input, num_components, colors, output, pi, mu, var)
|
0
|
61 {
|
|
62 # Start PDF device driver to save charts to output.
|
|
63 pdf(file=output, bg="white")
|
4
|
64 kaks <- read.table(file=kaks_input, header=T)
|
|
65 max_ks <- max(kaks$Ks, na.rm=TRUE)
|
0
|
66 # Change bin width
|
4
|
67 max_bin_range <- as.integer(max_ks / 0.05)
|
7
|
68 bin <- 0.05 * seq(0, (max_bin_range + 1 ))
|
|
69 kaks <- kaks[kaks$Ks<max_ks,]
|
4
|
70 h.kst <- hist(kaks$Ks, breaks=bin, plot=F)
|
|
71 nc <- h.kst$counts
|
|
72 vx <- h.kst$mids
|
|
73 ntot <- sum(nc)
|
0
|
74 # Set margin for plot bottom, left top, right.
|
4
|
75 par(mai=c(0.5, 0.5, 0, 0))
|
0
|
76 # Plot dimension in inches.
|
7
|
77 par(pin=c(3.0, 3.0))
|
|
78 g <- calculate_fitted_density(pi, mu, var, max_ks)
|
|
79 h <- ntot * 1.5 / sum(g)
|
|
80 vx <- seq(1, 100) * (max_ks / 100)
|
|
81 ymax <- max(nc)
|
4
|
82 barplot(nc, space=0.25, offset=0, width=0.04, xlim=c(0, max_ks), ylim=c(0, ymax), col="lightpink1", border="lightpink3")
|
0
|
83 # Add x-axis.
|
4
|
84 axis(1)
|
21
|
85 if (length(colors) == 0)
|
9
|
86 {
|
|
87 color <- c('red', 'yellow', 'green', 'black', 'blue', 'darkorange')
|
|
88 }
|
|
89 else
|
|
90 {
|
|
91 # Handle specified colors for components.
|
|
92 cStr <- unlist(colors)
|
|
93 color <- c()
|
|
94 items <- strsplit(cStr, ",")
|
|
95 for (item in items) {
|
|
96 color <- c(color, item)
|
|
97 }
|
|
98 num_colors_specified = length(color)
|
21
|
99 if (num_colors_specified < num_components):
|
9
|
100 {
|
|
101 for (i in num_colors_specified:num_components)
|
|
102 {
|
21
|
103 if !(any(color=='red'))
|
9
|
104 {
|
|
105 color <- c(color, 'red')
|
|
106 }
|
21
|
107 else if !(any(color=='yellow'))
|
9
|
108 {
|
|
109 color <- c(color, 'yellow')
|
|
110 }
|
21
|
111 else if !(any(color=='green'))
|
9
|
112 {
|
|
113 color <- c(color, 'green')
|
|
114 }
|
21
|
115 else if !(any(color=='black'))
|
9
|
116 {
|
|
117 color <- c(color, 'black')
|
|
118 }
|
21
|
119 else if !(any(color=='blue'))
|
9
|
120 {
|
|
121 color <- c(color, 'blue')
|
|
122 }
|
|
123 else
|
|
124 {
|
|
125 color <- c(color, 'darkorange')
|
|
126 }
|
|
127 }
|
|
128 }
|
|
129 }
|
0
|
130 for (i in 1:length(mu))
|
|
131 {
|
4
|
132 lines(vx, g[,i] * h, lwd=2, col=color[i])
|
0
|
133 }
|
4
|
134 }
|
0
|
135
|
7
|
136 calculate_fitted_density <- function(pi, mu, var, max_ks)
|
0
|
137 {
|
4
|
138 comp <- length(pi)
|
|
139 var <- var/mu^2
|
|
140 mu <- log(mu)
|
|
141 # Calculate lognormal density.
|
7
|
142 vx <- seq(1, 100) * (max_ks / 100)
|
4
|
143 fx <- matrix(0, 100, comp)
|
0
|
144 for (i in 1:100)
|
|
145 {
|
|
146 for (j in 1:comp)
|
|
147 {
|
4
|
148 fx[i, j] <- pi[j] * dlnorm(vx[i], meanlog=mu[j], sdlog=(sqrt(var[j])))
|
|
149 if (is.nan(fx[i,j])) fx[i,j]<-0
|
|
150 }
|
|
151 }
|
|
152 return(fx)
|
0
|
153 }
|
|
154
|
|
155 # Read in the components data.
|
4
|
156 components_data <- read.delim(opt$components_input, header=TRUE)
|
9
|
157 num_components <- opt$num_comp
|
0
|
158
|
|
159 # Set pi, mu, var.
|
4
|
160 items <- get_pi_mu_var(components_data, num_components)
|
7
|
161 if (num_components == 1)
|
|
162 {
|
|
163 pi <- items[1]
|
|
164 mu <- items[2]
|
|
165 var <- items[3]
|
|
166 }
|
|
167 if (num_components == 2)
|
|
168 {
|
|
169 pi <- items[1:2]
|
|
170 mu <- items[3:4]
|
|
171 var <- items[5:6]
|
|
172 }
|
|
173 if (num_components == 3)
|
|
174 {
|
|
175 pi <- items[1:3]
|
|
176 mu <- items[4:6]
|
|
177 var <- items[7:9]
|
|
178 }
|
|
179 if (num_components == 4)
|
|
180 {
|
|
181 pi <- items[1:4]
|
|
182 mu <- items[5:8]
|
|
183 var <- items[9:12]
|
|
184 }
|
|
185 if (num_components == 5)
|
|
186 {
|
|
187 pi <- items[1:5]
|
|
188 mu <- items[6:10]
|
|
189 var <- items[11:15]
|
|
190 }
|
|
191 if (num_components == 6)
|
|
192 {
|
|
193 pi <- items[1:6]
|
|
194 mu <- items[7:12]
|
|
195 var <- items[13:18]
|
|
196 }
|
0
|
197
|
|
198 # Plot the output.
|
9
|
199 plot_ks(opt$kaks_input, num_components, opt$colors, opt$output, pi, mu, var)
|