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