changeset 32:4c3fcfd55d94 draft

Uploaded
author greg
date Mon, 26 Jun 2017 10:44:53 -0400
parents 812f09b96a62
children 5652711d5ae4
files ks_distribution.R
diffstat 1 files changed, 59 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- a/ks_distribution.R	Mon Jun 26 10:35:07 2017 -0400
+++ b/ks_distribution.R	Mon Jun 26 10:44:53 2017 -0400
@@ -15,6 +15,60 @@
 args <- parse_args(parser, positional_arguments=TRUE)
 opt <- args$options
 
+set_component_colors = function(colors, number_comp)
+{
+    # Handle colors for components.
+    if (is.na(colors))
+    {
+        # Randomly specify colors for components.
+        specified_colors <- c('red', 'yellow', 'green', 'black', 'blue', 'darkorange')
+    }
+    else
+    {
+        # Handle selected colors for components.
+        parser <- newJSONParser()
+        parser$addData(colors)
+        raw_colors <- parser$getObject()
+        specified_colors <- c()
+        for (raw_color in raw_colors)
+        {
+            specified_colors <- c(specified_colors, raw_color)
+        }
+        num_colors_specified = length(specified_colors)
+        if (num_colors_specified < number_comp)
+        {
+            for (i in num_colors_specified:number_comp)
+            {
+                if (!any(specified_colors=='red'))
+                {
+                    specified_colors <- c(specified_colors, 'red')
+                }
+                else if (!any(specified_colors=='yellow'))
+                {
+                    specified_colors <- c(specified_colors, 'yellow')
+                }
+                else if (!any(specified_colors=='green'))
+                {
+                    specified_colors <- c(specified_colors, 'green')
+                }
+                else if (!any(specified_colors=='black'))
+                {
+                    specified_colors <- c(specified_colors, 'black')
+                }
+                else if (!any(specified_colors=='blue'))
+                {
+                    specified_colors <- c(specified_colors, 'blue')
+                }
+                else
+                {
+                    specified_colors <- c(specified_colors, 'darkorange')
+                }
+            }
+        }
+    }
+    return specified_colors
+}
+
 get_pi_mu_var = function(components_data, number_comp)
 {
     # FixMe: enhance this to generically handle any integer value for number_comp.
@@ -58,7 +112,7 @@
     return(results)
 }
 
-plot_ks<-function(kaks_input, number_comp, colors, output, pi, mu, var)
+plot_ks<-function(kaks_input, number_comp, specified_colors, output, pi, mu, var)
 {
     # Start PDF device driver to save charts to output.
     pdf(file=output, bg="white")
@@ -83,54 +137,9 @@
     barplot(nc, space=0.25, offset=0, width=0.04, xlim=c(0, max_ks), ylim=c(0, ymax), col="lightpink1", border="lightpink3")
     # Add x-axis.
     axis(1)
-    if (length(colors) == 0))
-    {
-        color <- c('red', 'yellow', 'green', 'black', 'blue', 'darkorange')
-    }
-    else
-    {
-        # Handle specified colors for components.
-        cStr <- unlist(colors)
-        color <- c()
-        items <- strsplit(cStr, ",")
-        for (item in items) {
-            color <- c(color, item)
-        }
-        num_colors_specified = length(color)
-        if (num_colors_specified < number_comp)
-        {
-            for (i in num_colors_specified:number_comp)
-            {
-                if (!any(color=='red'))
-                {
-                    color <- c(color, 'red')
-                }
-                else if (!any(color=='yellow'))
-                {
-                    color <- c(color, 'yellow')
-                }
-                else if (!any(color=='green'))
-                {
-                    color <- c(color, 'green')
-                }
-                else if (!any(color=='black'))
-                {
-                    color <- c(color, 'black')
-                }
-                else if (!any(color=='blue'))
-                {
-                    color <- c(color, 'blue')
-                }
-                else
-                {
-                    color <- c(color, 'darkorange')
-                }
-            }
-        }
-    }
     for (i in 1:length(mu))
     {
-       lines(vx, g[,i] * h, lwd=2, col=color[i])
+       lines(vx, g[,i] * h, lwd=2, col=specified_colors[i])
     }
 }
 
@@ -153,29 +162,13 @@
     return(fx)
 }
 
-# Handle colors for components.
-if (is.na(opt$colors))
-{
-    # Randomly specify colors for components.
-    specified_colors <- c('red', 'yellow', 'green', 'black', 'blue', 'darkorange')
-}
-else
-{
-    # Handle selected colors for components.
-    parser <- newJSONParser()
-    parser$addData(opt$colors)
-    raw_colors <- parser$getObject()
-    specified_colors <- c()
-    for (raw_color in raw_colors)
-    {
-        specified_colors <- c(specified_colors, raw_color)
-    }
-}
-
 # Read in the components data.
 components_data <- read.delim(opt$components_input, header=TRUE)
 number_comp <- opt$number_comp
 
+# Set component colors.
+specified_colors = set_component_colors = function(opt$colors, number_comp)
+
 # Set pi, mu, var.
 items <- get_pi_mu_var(components_data, number_comp)
 if (number_comp == 1)