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