comparison soft-threshold.R @ 1:b434ba108e9b draft

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