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