1
|
1 #!/usr/binenv Rscript
|
|
2
|
4
|
3 ## Create the personal library if it doesn't exist. Ignore a warning if the directory already exists.
|
|
4 dir.create(Sys.getenv("R_LIBS_USER"), showWarnings = FALSE, recursive = TRUE)
|
|
5 ## Install one package.
|
|
6 install.packages("getopt", Sys.getenv("R_LIBS_USER"), repos = "http://cran.case.edu", dependencies = TRUE )
|
|
7 install.packages("ggplot2", Sys.getenv("R_LIBS_USER"), repos = "http://cran.case.edu" , dependencies = TRUE)
|
|
8 install.packages("ggdendro", Sys.getenv("R_LIBS_USER"), repos = "http://cran.case.edu" , dependencies = TRUE)
|
|
9 install.packages("class", Sys.getenv("R_LIBS_USER"), repos = "http://cran.case.edu" , dependencies = TRUE)
|
|
10 install.packages("cluster", Sys.getenv("R_LIBS_USER"), repos = "http://cran.case.edu" , dependencies = TRUE)
|
|
11 install.packages("impute", Sys.getenv("R_LIBS_USER"), repos = "http://cran.case.edu" , dependencies = TRUE)
|
|
12 install.packages("Hmisc", Sys.getenv("R_LIBS_USER"), repos = "http://cran.case.edu" , dependencies = TRUE)
|
|
13 install.packages("WGCNA", Sys.getenv("R_LIBS_USER"), repos = "http://cran.case.edu" , dependencies = TRUE)
|
|
14
|
|
15
|
1
|
16 # A command-line interface to WGCNA for use with galaxy
|
|
17
|
|
18
|
|
19 # setup R error handline to go to stderr
|
|
20 options(show.error.messages=FALSE,
|
|
21 error=function(){
|
|
22 cat(geterrmessage(), file=stderr())
|
|
23 quit("no", 1, F)
|
|
24 })
|
|
25
|
|
26 # we need that to not crash galaxy with an UTF8 error on German LC settings.
|
|
27 loc = Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
|
|
28
|
|
29 # suppress warning
|
|
30 options(warn = -1)
|
|
31
|
|
32 suppressPackageStartupMessages({
|
|
33 library(getopt)
|
|
34 library(tools)
|
|
35 })
|
|
36
|
|
37 options(stringsAsFactors=FALSE, useFancyQuotes=FALSE)
|
|
38 args = commandArgs(trailingOnly=TRUE)
|
|
39
|
|
40 # column 1: the long flag name
|
|
41 # column 2: the short flag alias. A SINGLE character string
|
|
42 # column 3: argument mask
|
|
43 # 0: no argument
|
|
44 # 1: argument required
|
|
45 # 2: argument is optional
|
|
46 # column 4: date type to which the flag's argument shall be cast.
|
|
47 # possible values: logical, integer, double, complex, character.
|
|
48 spec_list=list()
|
|
49 spec_list$help = c('help', 'h', '0', 'logical')
|
|
50 spec_list$threads = c('threads', '-t', '2', 'integer')
|
|
51 spec_list$expressionData = c('expressionData', 'f', '1', 'character')
|
|
52 spec_list$betaMaximum = c('betaMaximum', 'b', 1, 'integer')
|
|
53 spec_list$rPowerTableOutput = c('rPowerTableOutput', 'r', 1, 'character')
|
|
54 spec_list$scaleFreeFitPlot = c('scaleFreeFitPlot', 'p', 1, 'character')
|
|
55 spec = t(as.data.frame(spec_list))
|
|
56
|
|
57 opt = getopt(spec)
|
|
58 # arguments are accessed by long flag name (the first column in the spec matrix)
|
|
59 # NOT by element name in the spec_list
|
|
60 # example: opt$help, opt$expression_file
|
|
61
|
|
62 suppressPackageStartupMessages({
|
|
63 library(MASS)
|
|
64 library(ggplot2)
|
|
65 library(ggdendro)
|
|
66 library(class)
|
|
67 library(cluster)
|
|
68 library(impute)
|
|
69 library(Hmisc)
|
|
70 library(WGCNA)
|
|
71 })
|
|
72
|
|
73 # allow multi-threads for WGCNA
|
|
74 if(!is.null(opt$threads)){
|
|
75 allowWGCNAThreads(nThreads = opt$threads)
|
|
76 }
|
|
77 # disableWGCNAThreads()
|
|
78
|
|
79 # read expression data
|
|
80 # column names are genes, rows are samples
|
|
81 expressionData = read.csv(opt$expressionData, header = TRUE, row.names = 1)
|
|
82
|
|
83 cat("Calculate R power table\n")
|
|
84 powers = seq(1, opt$betaMaximum)
|
|
85 RpowerTable = pickSoftThreshold(expressionData, powerVector = powers)[[2]]
|
|
86 cat("write R power table into file\n")
|
|
87 write.csv(RpowerTable, file=opt$rPowerTableOutput)
|
|
88
|
|
89
|
|
90 pdf(file = opt$scaleFreeFitPlot)
|
|
91
|
|
92 Rpower = RpowerTable[,1]
|
|
93 # plot scale free fit R^2 versus different soft threshold beta
|
|
94 signedRSq = -sign(RpowerTable[, 3]) * RpowerTable[, 2]
|
|
95 Rpower_df = data.frame(Rpower, signedRSq)
|
|
96
|
|
97 p = ggplot(aes(x = Rpower, y = signedRSq), data = Rpower_df)
|
|
98 p + geom_label(label = Rpower, color = "red") +
|
|
99 xlab("R power") +
|
|
100 ylab(expression("Scale Free Topology Model Fit, Signed "~R^2)) +
|
|
101 geom_hline(yintercept = 0.95, color = "blue")
|
|
102
|
|
103 # mean connectivity versus different soft threshold beta
|
|
104 meanConn = RpowerTable[,5]
|
|
105 meanConn_df = data.frame(Rpower, meanConn)
|
|
106 p = ggplot(aes(x = Rpower, y = meanConn), data = meanConn_df)
|
|
107 p + geom_label(label = Rpower, color = "red") +
|
|
108 xlab("R power") +
|
|
109 ylab("Mean connectivity")
|
|
110
|
4
|
111 dev.off()
|