aboutsummaryrefslogtreecommitdiff
path: root/scripts/wgcna_analysis.R
blob: 16a44fd9c062dc715dcdcd1fff717f22d259a8d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# initial workspace setup

library(WGCNA);
library(stringi);

options(stringsAsFactors = FALSE);

# load expression data **assumes csv format row(traits)(columns info+samples)

wgcnaRawData <- read.csv(file = "wgcna_data.csv")

# transform expressionData

dataExpr <- as.data.frame(t(wgcnaRawData));

# data cleaning

# adopted from docs
gsg = goodSamplesGenes(dataExpr, verbose = 3);



if (!gsg$allOK)
{
# Optionally, print the gene and sample names that were removed:
if (sum(!gsg$goodGenes)>0)
printFlush(paste("Removing genes:", paste(names(datExpr0)[!gsg$goodGenes], collapse = ", ")));
if (sum(!gsg$goodSamples)>0)
printFlush(paste("Removing samples:", paste(rownames(datExpr0)[!gsg$goodSamples], collapse = ", ")));
# Remove the offending genes and samples from the data:
dataExpr <- dataExpr[gsg$goodSamples, gsg$goodGenes]
}

# network constructions and modules

# choose softthreshhold (Calculate soft threshold if the user specified the)

powers <- c(c(1:10), seq(from = 12, to=20, by=2)) 
sft <- pickSoftThreshold(dataExpr, powerVector = powers, verbose = 5)

# pass user options
network <- blockwiseModules(dataExpr,
                  #similarity  matrix options
                  corType = "pearson",
                  #adjacency  matrix options

                  power = sft$powerEstimate,
                  networkType = "unsigned",
                  #TOM options
                  TOMtype =  "unsigned",

                  #module indentification

                  minmodulesSize = 30,
                  deepSplit = 5,
                  PamRespectsDendro = FALSE
                )



genImageRandStr <- function(prefix){

	randStr <- paste(prefix,stri_rand_strings(1, 9, pattern = "[A-Za-z0-9]"),sep="_")

	return(paste(randStr,".png",sep=""))
}


mergedColors = labels2colors(net$colors)

png(genImageRandStr,width=1000,height=600,type='cairo-png')



plotDendroAndColors(network$dendrograms[[1]],mergedColors[net$blockGenes[[1]]],
"Module colors",
dendroLabels = FALSE, hang = 0.03,
addGuide = TRUE, guideHang = 0.05)