annotate ks_distribution.R @ 0:c5846258c458 draft

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