annotate soft-threshold.R @ 7:e614e0988eda draft default tip

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