annotate ks_distribution.R @ 3:f5bdcd2729cc draft

Uploaded
author greg
date Thu, 24 Aug 2017 13:45:41 -0400
parents 56f42cc1dd58
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c5846258c458 Uploaded
greg
parents:
diff changeset
1 #!/usr/bin/env Rscript
c5846258c458 Uploaded
greg
parents:
diff changeset
2
c5846258c458 Uploaded
greg
parents:
diff changeset
3 suppressPackageStartupMessages(library("optparse"))
c5846258c458 Uploaded
greg
parents:
diff changeset
4
c5846258c458 Uploaded
greg
parents:
diff changeset
5 option_list <- list(
c5846258c458 Uploaded
greg
parents:
diff changeset
6 make_option(c("-c", "--components_input"), action="store", dest="components_input", help="Ks significant components input dataset"),
c5846258c458 Uploaded
greg
parents:
diff changeset
7 make_option(c("-k", "--kaks_input"), action="store", dest="kaks_input", help="KaKs analysis input dataset"),
1
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
8 make_option(c("-n", "--number_comp"), action="store", dest="number_comp", type="integer", help="Number of significant components in the Ks distribution"),
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
9 make_option(c("-o", "--output"), action="store", dest="output", help="Output dataset"),
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
10 make_option(c("-s", "--specified_colors"), action="store", dest="specified_colors", default=NULL, help="List of component colors")
0
c5846258c458 Uploaded
greg
parents:
diff changeset
11 )
c5846258c458 Uploaded
greg
parents:
diff changeset
12
c5846258c458 Uploaded
greg
parents:
diff changeset
13 parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
c5846258c458 Uploaded
greg
parents:
diff changeset
14 args <- parse_args(parser, positional_arguments=TRUE)
c5846258c458 Uploaded
greg
parents:
diff changeset
15 opt <- args$options
c5846258c458 Uploaded
greg
parents:
diff changeset
16
1
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
17 set_component_colors = function(colors, number_comp) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
18 # Handle colors for components.
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
19 if (is.null(colors)) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
20 # Randomly specify colors for components.
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
21 component_colors <- c("red", "yellow", "green", "black", "blue", "darkorange")
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
22 } else {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
23 # Handle selected colors for components.
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
24 component_colors <- c()
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
25 colors <- as.character(colors)
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
26 items <- strsplit(colors, ",")
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
27 for (item in items) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
28 component_colors <- c(component_colors, item)
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
29 }
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
30 num_colors_specified <- length(component_colors)
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
31 if (num_colors_specified < number_comp) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
32 # The number of selected colors is less than the number of
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
33 # components, so we'll add random colors that were not
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
34 # selected to the set of component colors until we have a
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
35 # color for each component.
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
36 loop_count <- number_comp - num_colors_specified
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
37 for (i in 1:loop_count) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
38 if (!(is.element("red", component_colors))) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
39 component_colors <- c(component_colors, "red")
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
40 } else if (!(is.element("yellow", component_colors))) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
41 component_colors <- c(component_colors, "yellow")
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
42 } else if (!(is.element("green", component_colors))) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
43 component_colors <- c(component_colors, "green")
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
44 } else if (!(is.element("black", component_colors))) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
45 component_colors <- c(component_colors, "black")
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
46 } else if (!(is.element("blue", component_colors))) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
47 component_colors <- c(component_colors, "blue")
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
48 } else if (!(is.element("darkorange", component_colors))) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
49 component_colors <- c(component_colors, "darkorange")
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
50 }
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
51 }
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
52 }
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
53 }
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
54 return(component_colors)
0
c5846258c458 Uploaded
greg
parents:
diff changeset
55 }
c5846258c458 Uploaded
greg
parents:
diff changeset
56
1
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
57 get_pi_mu_var = function(components_data, number_comp) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
58 if (number_comp == 1) {
0
c5846258c458 Uploaded
greg
parents:
diff changeset
59 pi <- c(components_data[1, 9])
c5846258c458 Uploaded
greg
parents:
diff changeset
60 mu <- c(components_data[1, 7])
c5846258c458 Uploaded
greg
parents:
diff changeset
61 var <- c(components_data[1, 8])
1
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
62 } else if (number_comp == 2) {
0
c5846258c458 Uploaded
greg
parents:
diff changeset
63 pi <- c(components_data[2, 9], components_data[3, 9])
c5846258c458 Uploaded
greg
parents:
diff changeset
64 mu <- c(components_data[2, 7], components_data[3, 7])
c5846258c458 Uploaded
greg
parents:
diff changeset
65 var <- c(components_data[2, 8], components_data[3, 8])
1
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
66 } else if (number_comp == 3) {
0
c5846258c458 Uploaded
greg
parents:
diff changeset
67 pi <- c(components_data[4, 9], components_data[5, 9], components_data[6, 9])
c5846258c458 Uploaded
greg
parents:
diff changeset
68 mu <- c(components_data[4, 7], components_data[5, 7], components_data[6, 7])
c5846258c458 Uploaded
greg
parents:
diff changeset
69 var <- c(components_data[4, 8], components_data[5, 8], components_data[6, 8])
1
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
70 } else if (number_comp == 4) {
0
c5846258c458 Uploaded
greg
parents:
diff changeset
71 pi <- c(components_data[7, 9], components_data[8, 9], components_data[9, 9], components_data[10, 9])
c5846258c458 Uploaded
greg
parents:
diff changeset
72 mu <- c(components_data[7, 7], components_data[8, 7], components_data[9, 7], components_data[10, 7])
c5846258c458 Uploaded
greg
parents:
diff changeset
73 var <- c(components_data[7, 8], components_data[8, 8], components_data[9, 8], components_data[10, 8])
1
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
74 } else if (number_comp == 5) {
0
c5846258c458 Uploaded
greg
parents:
diff changeset
75 pi <- c(components_data[11, 9], components_data[12, 9], components_data[13, 9], components_data[14, 9], components_data[15, 9])
c5846258c458 Uploaded
greg
parents:
diff changeset
76 mu <- c(components_data[11, 7], components_data[12, 7], components_data[13, 7], components_data[14, 7], components_data[15, 7])
c5846258c458 Uploaded
greg
parents:
diff changeset
77 var <- c(components_data[11, 8], components_data[12, 8], components_data[13, 8], components_data[14, 8], components_data[15, 8])
1
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
78 } else if (number_comp == 6) {
0
c5846258c458 Uploaded
greg
parents:
diff changeset
79 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])
c5846258c458 Uploaded
greg
parents:
diff changeset
80 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])
c5846258c458 Uploaded
greg
parents:
diff changeset
81 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])
c5846258c458 Uploaded
greg
parents:
diff changeset
82 }
1
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
83 results <- c(pi, mu, var)
0
c5846258c458 Uploaded
greg
parents:
diff changeset
84 return(results)
c5846258c458 Uploaded
greg
parents:
diff changeset
85 }
c5846258c458 Uploaded
greg
parents:
diff changeset
86
1
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
87 plot_ks<-function(kaks_input, component_colors, output, pi, mu, var) {
0
c5846258c458 Uploaded
greg
parents:
diff changeset
88 # Start PDF device driver to save charts to output.
c5846258c458 Uploaded
greg
parents:
diff changeset
89 pdf(file=output, bg="white")
c5846258c458 Uploaded
greg
parents:
diff changeset
90 kaks <- read.table(file=kaks_input, header=T)
c5846258c458 Uploaded
greg
parents:
diff changeset
91 max_ks <- max(kaks$Ks, na.rm=TRUE)
c5846258c458 Uploaded
greg
parents:
diff changeset
92 # Change bin width
c5846258c458 Uploaded
greg
parents:
diff changeset
93 max_bin_range <- as.integer(max_ks / 0.05)
c5846258c458 Uploaded
greg
parents:
diff changeset
94 bin <- 0.05 * seq(0, (max_bin_range + 1 ))
c5846258c458 Uploaded
greg
parents:
diff changeset
95 kaks <- kaks[kaks$Ks<max_ks,]
c5846258c458 Uploaded
greg
parents:
diff changeset
96 h.kst <- hist(kaks$Ks, breaks=bin, plot=F)
c5846258c458 Uploaded
greg
parents:
diff changeset
97 nc <- h.kst$counts
c5846258c458 Uploaded
greg
parents:
diff changeset
98 vx <- h.kst$mids
c5846258c458 Uploaded
greg
parents:
diff changeset
99 ntot <- sum(nc)
c5846258c458 Uploaded
greg
parents:
diff changeset
100 # Set margin for plot bottom, left top, right.
c5846258c458 Uploaded
greg
parents:
diff changeset
101 par(mai=c(0.5, 0.5, 0, 0))
c5846258c458 Uploaded
greg
parents:
diff changeset
102 # Plot dimension in inches.
c5846258c458 Uploaded
greg
parents:
diff changeset
103 par(pin=c(3.0, 3.0))
c5846258c458 Uploaded
greg
parents:
diff changeset
104 g <- calculate_fitted_density(pi, mu, var, max_ks)
c5846258c458 Uploaded
greg
parents:
diff changeset
105 h <- ntot * 1.5 / sum(g)
c5846258c458 Uploaded
greg
parents:
diff changeset
106 vx <- seq(1, 100) * (max_ks / 100)
c5846258c458 Uploaded
greg
parents:
diff changeset
107 ymax <- max(nc)
c5846258c458 Uploaded
greg
parents:
diff changeset
108 barplot(nc, space=0.25, offset=0, width=0.04, xlim=c(0, max_ks), ylim=c(0, ymax), col="lightpink1", border="lightpink3")
c5846258c458 Uploaded
greg
parents:
diff changeset
109 # Add x-axis.
c5846258c458 Uploaded
greg
parents:
diff changeset
110 axis(1)
1
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
111 for (i in 1:length(mu)) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
112 lines(vx, g[,i] * h, lwd=2, col=component_colors[i])
0
c5846258c458 Uploaded
greg
parents:
diff changeset
113 }
c5846258c458 Uploaded
greg
parents:
diff changeset
114 }
c5846258c458 Uploaded
greg
parents:
diff changeset
115
1
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
116 calculate_fitted_density <- function(pi, mu, var, max_ks) {
0
c5846258c458 Uploaded
greg
parents:
diff changeset
117 comp <- length(pi)
c5846258c458 Uploaded
greg
parents:
diff changeset
118 var <- var/mu^2
c5846258c458 Uploaded
greg
parents:
diff changeset
119 mu <- log(mu)
c5846258c458 Uploaded
greg
parents:
diff changeset
120 # Calculate lognormal density.
c5846258c458 Uploaded
greg
parents:
diff changeset
121 vx <- seq(1, 100) * (max_ks / 100)
c5846258c458 Uploaded
greg
parents:
diff changeset
122 fx <- matrix(0, 100, comp)
1
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
123 for (i in 1:100) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
124 for (j in 1:comp) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
125 fx[i, j] <- pi[j] * dlnorm(vx[i], meanlog=mu[j], sdlog=(sqrt(var[j])))
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
126 if (is.nan(fx[i,j])) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
127 fx[i,j]<-0
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
128 }
0
c5846258c458 Uploaded
greg
parents:
diff changeset
129 }
c5846258c458 Uploaded
greg
parents:
diff changeset
130 }
c5846258c458 Uploaded
greg
parents:
diff changeset
131 return(fx)
c5846258c458 Uploaded
greg
parents:
diff changeset
132 }
c5846258c458 Uploaded
greg
parents:
diff changeset
133
c5846258c458 Uploaded
greg
parents:
diff changeset
134 # Read in the components data.
c5846258c458 Uploaded
greg
parents:
diff changeset
135 components_data <- read.delim(opt$components_input, header=TRUE)
1
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
136 number_comp <- as.integer(opt$number_comp)
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
137 if (number_comp == 0) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
138 # Default to 1 component to allow functional testing.
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
139 number_comp <- 1
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
140 }
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
141
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
142 # Set component colors.
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
143 component_colors <- set_component_colors(opt$specified_colors, number_comp)
0
c5846258c458 Uploaded
greg
parents:
diff changeset
144
c5846258c458 Uploaded
greg
parents:
diff changeset
145 # Set pi, mu, var.
1
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
146 items <- get_pi_mu_var(components_data, number_comp)
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
147 if (number_comp == 1) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
148 pi <- items[1]
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
149 mu <- items[2]
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
150 var <- items[3]
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
151 } else if (number_comp == 2) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
152 pi <- items[1:2]
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
153 mu <- items[3:4]
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
154 var <- items[5:6]
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
155 } else if (number_comp == 3) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
156 pi <- items[1:3]
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
157 mu <- items[4:6]
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
158 var <- items[7:9]
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
159 } else if (number_comp == 4) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
160 pi <- items[1:4]
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
161 mu <- items[5:8]
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
162 var <- items[9:12]
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
163 } else if (number_comp == 5) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
164 pi <- items[1:5]
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
165 mu <- items[6:10]
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
166 var <- items[11:15]
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
167 } else if (number_comp == 6) {
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
168 pi <- items[1:6]
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
169 mu <- items[7:12]
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
170 var <- items[13:18]
0
c5846258c458 Uploaded
greg
parents:
diff changeset
171 }
c5846258c458 Uploaded
greg
parents:
diff changeset
172
c5846258c458 Uploaded
greg
parents:
diff changeset
173 # Plot the output.
1
56f42cc1dd58 Uploaded
greg
parents: 0
diff changeset
174 plot_ks(opt$kaks_input, component_colors, opt$output, pi, mu, var)