Mercurial > repos > greg > ks_distribution
comparison ks_distribution.R @ 51:1a3973eea61c draft
Uploaded
author | greg |
---|---|
date | Mon, 26 Jun 2017 14:33:04 -0400 |
parents | 0b3d12ecce28 |
children | 53bc81f3ebad |
comparison
equal
deleted
inserted
replaced
50:0b3d12ecce28 | 51:1a3973eea61c |
---|---|
17 set_component_colors = function(colors, number_comp) | 17 set_component_colors = function(colors, number_comp) |
18 { | 18 { |
19 # Handle colors for components. | 19 # Handle colors for components. |
20 if (is.na(colors) == 0) { | 20 if (is.na(colors) == 0) { |
21 # Randomly specify colors for components. | 21 # Randomly specify colors for components. |
22 specified_colors <- c('red', 'yellow', 'green', 'black', 'blue', 'darkorange') | 22 component_colors <- c('red', 'yellow', 'green', 'black', 'blue', 'darkorange') |
23 } else { | 23 } else { |
24 # Handle selected colors for components. | 24 # Handle selected colors for components. |
25 colors <- as.character(colors) | 25 colors <- as.character(colors) |
26 specified_colors <- strsplit(colors, ",") | 26 component_colors <- strsplit(colors, ",") |
27 num_colors_specified <- length(specified_colors) | 27 num_colors_specified <- length(component_colors) |
28 if (num_colors_specified < number_comp) { | 28 if (num_colors_specified < number_comp) { |
29 for (i in num_colors_specified:number_comp) { | 29 for (i in num_colors_specified:number_comp) { |
30 if (!('red' %in% names(specified_colors))) { | 30 if (!('red' %in% names(component_colors))) { |
31 specified_colors <- c(specified_colors, 'red') | 31 component_colors <- c(component_colors, 'red') |
32 } else if (!('yellow' %in% names(specified_colors))) { | 32 } else if (!('yellow' %in% names(component_colors))) { |
33 specified_colors <- c(specified_colors, 'yellow') | 33 component_colors <- c(component_colors, 'yellow') |
34 } else if (!('green' %in% names(specified_colors))) { | 34 } else if (!('green' %in% names(component_colors))) { |
35 specified_colors <- c(specified_colors, 'green') | 35 component_colors <- c(component_colors, 'green') |
36 } else if (!('black' %in% names(specified_colors))) { | 36 } else if (!('black' %in% names(component_colors))) { |
37 specified_colors <- c(specified_colors, 'black') | 37 component_colors <- c(component_colors, 'black') |
38 } else if (!('blue' %in% names(specified_colors))) { | 38 } else if (!('blue' %in% names(component_colors))) { |
39 specified_colors <- c(specified_colors, 'blue') | 39 component_colors <- c(component_colors, 'blue') |
40 } else { | 40 } else { |
41 specified_colors <- c(specified_colors, 'darkorange') | 41 component_colors <- c(component_colors, 'darkorange') |
42 } | 42 } |
43 } | 43 } |
44 } | 44 } |
45 } | 45 } |
46 return(specified_colors) | 46 return(component_colors) |
47 } | 47 } |
48 | 48 |
49 get_pi_mu_var = function(components_data, number_comp) | 49 get_pi_mu_var = function(components_data, number_comp) |
50 { | 50 { |
51 # FixMe: enhance this to generically handle any integer value for number_comp. | 51 # FixMe: enhance this to generically handle any integer value for number_comp. |
72 } else if (number_comp == 6) { | 72 } else if (number_comp == 6) { |
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]) | 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]) | 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]) | 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 } | 76 } |
77 results = c(pi, mu, var) | 77 results <- c(pi, mu, var) |
78 return(results) | 78 return(results) |
79 } | 79 } |
80 | 80 |
81 plot_ks<-function(kaks_input, number_comp, specified_colors, output, pi, mu, var) | 81 plot_ks<-function(kaks_input, number_comp, component_colors, output, pi, mu, var) |
82 { | 82 { |
83 # Start PDF device driver to save charts to output. | 83 # Start PDF device driver to save charts to output. |
84 pdf(file=output, bg="white") | 84 pdf(file=output, bg="white") |
85 kaks <- read.table(file=kaks_input, header=T) | 85 kaks <- read.table(file=kaks_input, header=T) |
86 max_ks <- max(kaks$Ks, na.rm=TRUE) | 86 max_ks <- max(kaks$Ks, na.rm=TRUE) |
102 ymax <- max(nc) | 102 ymax <- max(nc) |
103 barplot(nc, space=0.25, offset=0, width=0.04, xlim=c(0, max_ks), ylim=c(0, ymax), col="lightpink1", border="lightpink3") | 103 barplot(nc, space=0.25, offset=0, width=0.04, xlim=c(0, max_ks), ylim=c(0, ymax), col="lightpink1", border="lightpink3") |
104 # Add x-axis. | 104 # Add x-axis. |
105 axis(1) | 105 axis(1) |
106 for (i in 1:length(mu)) { | 106 for (i in 1:length(mu)) { |
107 lines(vx, g[,i] * h, lwd=2, col=specified_colors[i]) | 107 lines(vx, g[,i] * h, lwd=2, col=component_colors[i]) |
108 } | 108 } |
109 } | 109 } |
110 | 110 |
111 calculate_fitted_density <- function(pi, mu, var, max_ks) | 111 calculate_fitted_density <- function(pi, mu, var, max_ks) |
112 { | 112 { |
130 # Read in the components data. | 130 # Read in the components data. |
131 components_data <- read.delim(opt$components_input, header=TRUE) | 131 components_data <- read.delim(opt$components_input, header=TRUE) |
132 number_comp <- as.integer(opt$number_comp) | 132 number_comp <- as.integer(opt$number_comp) |
133 | 133 |
134 # Set component colors. | 134 # Set component colors. |
135 specified_colors <- set_component_colors(opt$colors, number_comp) | 135 component_colors <- set_component_colors(opt$colors, number_comp) |
136 | 136 |
137 # Set pi, mu, var. | 137 # Set pi, mu, var. |
138 items <- get_pi_mu_var(components_data, number_comp) | 138 items <- get_pi_mu_var(components_data, number_comp) |
139 if (number_comp == 1) { | 139 if (number_comp == 1) { |
140 pi <- items[1] | 140 pi <- items[1] |
161 mu <- items[7:12] | 161 mu <- items[7:12] |
162 var <- items[13:18] | 162 var <- items[13:18] |
163 } | 163 } |
164 | 164 |
165 # Plot the output. | 165 # Plot the output. |
166 plot_ks(opt$kaks_input, number_comp, specified_colors, opt$output, pi, mu, var) | 166 plot_ks(opt$kaks_input, number_comp, component_colors, opt$output, pi, mu, var) |