#!/usr/bin/env Rscript
# title:
# description: generate module colors
# authors: Tyler W Bradshaw
## ---- INPUT
# input data in root/data
root = "~/projects/Uezu2016"
part_file = "cpm_partition"
# not clustered == "gray"
NC_color = "#BEBEBE"
## ---- FUNCTIONS
str_to_vec <- function(response) {
# parse the python dictionary returned as a string from
# system(random_color.py)
vec <- gsub("'","",gsub("\\[|\\]","",
trimws(unlist(strsplit(response,",")))))
return(vec)
}
## ---- Prepare the workspace
# Global Imports
suppressPackageStartupMessages({
library(dplyr)
library(data.table)
})
# Local Imports
devtools::load_all(root, quiet=TRUE)
# input multi-resolution partition
myfile <- file.path(root, "rdata", paste0(part_file,".csv"))
stopifnot(file.exists(myfile))
# load and coerce to list of vectors (partitions)
part_df <- fread(myfile,drop=1)
part_list <- unlist(apply(part_df,1,list), recursive=FALSE)
## we really only need to generate colors for the partition with max clusters
k = sapply(part_list,function(x) length(unique(x)))
r = which(k==max(k))
partition = part_list[[r]]
## ---- Generate Module colors
prots <- names(partition)
modules <- split(prots, partition)
names(modules) <- paste0("M",names(modules))
n_colors <- length(modules)
message("generating: ", n_colors, " random colors")
# Path to python script which is a simple script that uses the python
# port of randomcolors to generate random colors.
script <- file.path(root,"Py","random_color.py")
stopifnot(file.exists(script))
# FIXME: how can we conveniently package this python code for R?
# Generate n random colors
cmd <- paste(script,"--count", n_colors, "--luminosity", "bright")
response <- system(cmd, intern = TRUE)
# NOTE: to install randomcolor, use conda:
# conda install -c conda-forge randomcolor
# Parse the response
colors <- toupper(str_to_vec(response))
# Module color assignments
# Initialize a vector for the module colors
module_colors <- setNames(rep(NA, length(modules)), nm=names(modules))
# Insure that M0 is gray
module_colors["M0"] <- NC_color
# The reamining colors are random
idx <- is.na(module_colors)
module_colors[idx] <- sample(colors,sum(idx))
## ---- work
module_colors
prots = names(partition)
modules = split(prots,partition)
names(modules) <- paste0("M", names(modules))
as.list(module_colors[names(modules)])
prot_colors = unlist(lapply(names(modules), function(x) setNames(rep(module_colors[[x]],length(modules[[x]])), nm=modules[[x]])))
part0 = part_list[[5]]
table(part0)
df = data.table(resolution=seq(1,100),k=k)
library(ggplot2)
plot <- ggplot(df)
plot <- plot + geom_line(aes(x=resolution,y=k))
plot <- plot + theme(panel.background=element_blank())
plot <- plot + theme(axis.line.x=element_line())
plot <- plot + theme(axis.line.y=element_line())
ggsave("cpm.pdf",plot)
## ---- Save the data
# Save updated module colors
namen <- paste0(gsub("partition","colors",part_file),".rda")
myfile <- file.path(root,"data", namen)
save(module_colors,file=myfile,version=2)
message("saved: ", myfile)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.