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