-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharrayLimmaRoast.R
158 lines (113 loc) · 4.77 KB
/
arrayLimmaRoast.R
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
library(limma)
library(GSEABase)
library(lumi)
cl <- commandArgs(trailingOnly=TRUE)
eset_file <- cl[1]
experimentfile <- cl[2]
contrastsfile <- cl[3]
annotationfile <- cl[4]
entrezgenefield <- cl[5]
idfield <- cl[6]
nrot <- as.numeric(cl[7])
threads <- as.numeric(cl[8])
msigdbdir <- cl[9]
outfile <- cl[10]
batchvars <- cl[11]
experiment <- read.delim(experimentfile, sep='\t', stringsAsFactors=F)
allcontrasts <- read.delim(contrastsfile, sep='\t', stringsAsFactors=F)
batchvars <- unlist(strsplit(batchvars, ','))
batchvars <- batchvars[batchvars %in% colnames(experiment)]
eset <- readRDS(eset_file)
annotation <- read.csv(annotationfile, row.names=NULL, stringsAsFactors = FALSE)
annotation[[entrezgenefield]] <- as.character(annotation[[entrezgenefield]])
# Get the gene sets
msigdb_files <- list.files(msigdbdir, full.names=TRUE, pattern='.gmt')
gene_sets <- lapply(msigdb_files, getGmt)
names(gene_sets) <- sub('.entrez.gmt', '', basename(msigdb_files))
# Remove anything with a tiny gene set
gene_sets <- lapply(gene_sets, function(pgss){
pgss[ unlist(lapply(pgss, function(x) length(geneIds(x)))) >= 5 ]
})
# Convert to probe IDs
print("Converting gene sets to probes")
gene_sets <- lapply(gene_sets, function(gene_set_collection) {
# gene_set_collection doesn't behave exactly like a list (it's a GSEABase object), so we have to make sure the result
# gets named properly
gsc <- lapply(gene_set_collection, function(gene_set) {
set_gene_ids <- GSEABase::geneIds(gene_set)
gs <- annotation[[idfield]][annotation[[entrezgenefield]] %in% set_gene_ids]
gs[!is.na(gs)]
})
names(gsc) <- names(gene_set_collection)
gsc
})
print("Done gene set conversion")
# This one isn't very useful
gene_sets <- gene_sets[names(gene_sets) != "c2.all.v5.0"]
roastres <- lapply(unique(allcontrasts$variable), function(contrast_variable){
print(contrast_variable)
# Exclude samples with blank values for this variable
subexperiment <- experiment[! is.na(experiment[[contrast_variable]]),, drop = FALSE]
subexperiment[[contrast_variable]] <- factor(subexperiment[[contrast_variable]])
subeset <- eset[,rownames(subexperiment), drop = FALSE]
contrasts <- allcontrasts[allcontrasts$variable == contrast_variable,]
contrast_names_for_output <- apply(contrasts, 1, function(contrast) paste(contrast[1], paste(contrast[-1], collapse='-'), sep=':'))
if (length(batchvars) > 0){
contmodel <- paste('~0', contrast_variable, paste(batchvars, collapse = '+'), sep='+')
for (bv in batchvars){
subexperiment[[bv]] <- factor(subexperiment[[bv]])
}
}else{
print("No batch variable provided")
contmodel <- paste('~0', contrast_variable, sep='+')
}
print(paste('Model:', contmodel))
design <- model.matrix( as.formula(contmodel), data=subexperiment)
colnames(design) <- sub(contrast_variable, paste0(contrast_variable, '.'), colnames(design))
fit <- lmFit(subeset, design)
# Contrasts bit
contrast_names <- paste(paste(contrast_variable, make.names(contrasts$group1), sep="."), paste(contrast_variable, make.names(contrasts$group2), sep="."), sep="-")[which(contrasts$variable == contrast_variable)] # for limma et al
contrast_names <- gsub(".X", ".", contrast_names)
contrast.matrix <- makeContrasts(contrasts=contrast_names, levels=design)
mroast <- lapply(1:nrow(contrasts), function(n){
print(contrast_names[n])
contrast <- as.character(contrasts[n,])
gsnames <- names(gene_sets)
names(gsnames) <- gsnames
#clust <- makeCluster(getOption("cl.cores", threads))
#clusterExport(clust, c('gsnames', 'contrast.matrix', 'gene_sets', 'subeset', 'design', 'nrot', 'contrast_names_for_output', 'n'), envir=environment())
#clusterEvalQ(clust, {library(limma)})
#clusterEvalQ(clust, {library(lumi)})
#gsres <- parLapply(clust, gsnames, function(pgss){
gsres <- lapply(gsnames, function(pgss){
print(paste0('...', pgss))
# Reverse the sense of the contrast so it makes sense
mrcont <- contrast.matrix[,n]
mrcont[contrast.matrix[,n] == -1] <- 1
mrcont[contrast.matrix[,n] == 1] <- -1
res <- mroast(
y=subeset,
index=ids2indices(gene_sets[[pgss]], rownames(subeset)),
design=design,
contrast=mrcont,
nrot=nrot)
data.frame(
cbind(
contrast_names_for_output[n],
gene_set_type=pgss,
gene_set=rownames(res),
res,
row.names = NULL
),
row.names=NULL
)
})
#stopCluster(clust)
do.call(rbind, gsres)
})
do.call(rbind, mroast)
})
final <- do.call(rbind, roastres)
colnames(final)[1] <- 'contrast'
colnames(final) <- gsub('\\.', '_', colnames(final))
write.csv(final, file=outfile, row.names=F)