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