annotate soft-threshold.R @ 1:b434ba108e9b draft

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