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