-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathview.R
249 lines (185 loc) · 9.4 KB
/
pathview.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
source("http://bioconductor.org/biocLite.R")
options(bitmapType='cairo')
options(error=traceback) # causes a traceback to appear if there is an error, and the traceback has the line number, prefixed by #
## options(echo=TRUE)
sessionInfo() # Keep track of versions used, especially packages
####################################################################################
# Evaluate command line arguments ##################################################
####################################################################################
# dir = "~/Documents/UMB/Riley_Lab/HiSeq_data/jose/"
## args = commandArgs(trailingOnly = T)
args = commandArgs(TRUE)
for (i in 1:length(args)) {
eval(parse(text=args[[i]]))
}
####################################################################################
# Functions ########################################################################
####################################################################################
head2 = function(object)
{
if (is.null(object)) {
return("NULL")
}
return(head(object))
}
####################################################################################
####################################################################################
####################################################################################
alphaDown = as.numeric(Sys.getenv("PATHVIEW_ALPHA_DOWN"))
alphaUp = as.numeric(Sys.getenv("PATHVIEW_ALPHA_UP"))
species = Sys.getenv("PATHVIEW_SPECIES")
numberPathwaysDown = as.numeric(Sys.getenv("PATHVIEW_NUMBER_PATHWAYS_DOWN"))
numberPathwaysUp = as.numeric(Sys.getenv("PATHVIEW_NUMBER_PATHWAYS_UP"))
pathwaysList = Sys.getenv("PATHVIEW_PATHWAYS_LIST")
## test = Sys.getenv("PATH")
## print(paste("test = ", test, sep=""))
## test = Sys.getenv("QUEUE")
## print(paste("test = ", test, sep=""))
print(paste("alphaDown = ", alphaDown, sep=""))
print(paste("alphaUp = ", alphaUp, sep=""))
print(paste("species = ", species, sep=""))
print(paste("numberPathwaysDown = ", numberPathwaysDown, sep=""))
print(paste("numberPathwaysUp = ", numberPathwaysUp, sep=""))
print(paste("pathwaysList = ", pathwaysList, sep=""))
## cuffdiff.table = read.xlsx2(paste(inDir, inFile, sep = ""), startRow = 2)
tablePathName = paste(inDir, "/", inFile, sep = "")
writeLines(c("\ntablePathName:", tablePathName))
cuffdiff.table = read.table(tablePathName, header=TRUE)
## writeLines("\ncuffdiff.table:", head2(cuffdiff.table))
writeLines("\ncuffdiff.table:")
cuffdiff.table[0:10, ]
## biocLite("pathview")
## biocLite("gage")
outDir
setwd(outDir)
getwd()
require(pathview)
require(gage)
simpleCap <- function(x) {
x <- tolower(x)
s <- strsplit(x, " ")[[1]]
paste(toupper(substring(s, 1,1)), substring(s, 2),
sep="", collapse=" ")
}
## cuffdiff.table = read.delim(file = "gene_exp.diff", sep = "\t")
## Cufflinks is a very popular RNA-Seq analysis tool. It is developed by the same group as TopHat. It is
## implemented independent of Bioconductor. However, we can read its differential expression analysis
## results into R easily. The result file is named *_exp_diff
## Notice that the gene symbols need to be converted to Entrez Gene IDs, which are used in
## KEGG pathways (for many research species) and GO gene sets.
# notice the column name special character changes. The column used to be
# cuffdiff.table$log2.fold_change. for older versions of Cufflinks.
# cuff.fc = cuffdiff.table$log2.FPKMy.FPKMx
cuff.fc = cuffdiff.table$log2.fold_change.
writeLines(c("\ncuff.fc: ", head2(cuff.fc)))
gnames = cuffdiff.table$gene
sel = gnames != "-"
head(sel)
## writeLines("sel:")
## sel[0:10]
gnames = as.character(gnames[sel])
head(gnames)
## writeLines("gnames:")
## gnames[0:10]
## head(gnames)
cuff.fc = cuff.fc[sel]
head(cuff.fc)
## cuff.fc[0:10]
names(cuff.fc) = gnames
## gnames[0:10]
# Notice that the gene sybmols need to be converted to Entrez Gene IDs, which are used in
# KEGG pathways (many research species) and GO gene sets.
data(korg)
## labels = colnames(korg)
## labels[3] = "commonName"
## labels[1] = kegg.code
## colnames(korg) = labels
head(korg)
dim(korg)
colnames(korg)
kegg.code = korg[which(korg[,"scientific.name"] == species),][["kegg.code"]]
kegg.code
# ADDED LINES
# The following line may serve as a substitute for species not in the conversational lexicon and lacking common names (i.e. bacteria)
# kegg.code = korg[which(korg[,"kegg.code"] == species),][[2]]; kegg.code
# kegg.code = korg[which(korg[,"scientific.name"] == species),][[1]]; kegg.code
# kegg.code = korg[which(korg[,"common.name"] == species),][[1]]
org.code = substr(simpleCap(kegg.code),1, 2)
org.code
gnames.entrez = pathview::id2eg(gnames, category = "symbol", org = org.code)
head(gnames.entrez)
#Modified Line
#gnames.entrez = pathview::id2eg(gnames, category = "symbol", org = kegg.code)
sel2 = gnames.entrez[,2]>""
head(sel2)
cuff.fc = cuff.fc[sel2]
names(cuff.fc) = gnames.entrez[sel2,2]
writeLines(c("\nrange(cuff.fc): ", range(cuff.fc)))
# remove the -Inf and Inf values, which block the downstream analysis
cuff.fc[cuff.fc > 10] = 10
cuff.fc[cuff.fc < -10] = -10
exp.fc = cuff.fc[ !is.na(cuff.fc) ]
myPathviewIds = unlist(strsplit(pathwaysList, " "))
if (length(myPathviewIds) > 0) {
pv.out.list <- sapply(myPathviewIds,
function(pid) pathview(gene.data = exp.fc, pathway.id = pid, species = kegg.code, out.suffix = "cuffdiff", kegg.dir = ".", kegg.native=TRUE))
pv.out.list
}
else {
## Next, we use GAGE for pathway analysis, and Pathview for visualization. Notice that
## this step (the same code) is identical for DESeq, edgeR, Limma and Cufflinks workflows.
# In pathview function, please be conscious of the argument gene.idtype. Default is gene.idtype="entrez",i.e. Entrez
# Gene, which are the primary KEGG gene ID for many common model organisms. For other species, gene.idtype
# should be set to "KEGG" since KEGG uses other types of gene IDs.
## data(kegg.gs)
kg.species<- kegg.gsets(species)
kegg.gs<- kg.species$kg.sets[kg.species$sigmet.idx]
#let's get the annotation files for mouse and convert the gene set to gene symbol format
writeLines(c("\nlapply(kegg.gs[1:3], head):", lapply(kegg.gs[1:3], head)))
writeLines(c("\nlength(kegg.gs): ", length(kegg.gs)))
writeLines(c("\nexp.fc:", head2(exp.fc)))
writeLines(c("\nstr(exp.fc):", str(exp.fc)))
fc.kegg.p <- gage(exp.fc, gsets = kegg.gs, ref = NULL, samp = NULL)
writeLines(c("\nfc.kegg.p$greater:", head2(fc.kegg.p$greater)))
writeLines(c("\nfc.kegg.p$greater[, \"p.val\"]:", (fc.kegg.p$greater[, "p.val"])))
upExpressed <- fc.kegg.p$greater[, "p.val"] < alphaUp & !is.na(fc.kegg.p$greater[, "p.val"])
downExpressed <- fc.kegg.p$less[, "p.val"] < alphaDown & !is.na(fc.kegg.p$less[,"p.val"])
print(paste("max(upRegulated p.val) = ", max(fc.kegg.p$greater[, "p.val"]), sep=""))
print(paste("min(upRegulated p.val) = ", min(fc.kegg.p$greater[, "p.val"]), sep=""))
print(paste("max(downRegulated p.val) = ", max(fc.kegg.p$less[, "p.val"]), sep=""))
print(paste("min(downRegulated p.val) = ", min(fc.kegg.p$less[, "p.val"]), sep=""))
path.ids.upExpr <- rownames(fc.kegg.p$greater)[upExpressed]
path.ids2.upExpr <- substr(path.ids.upExpr, 1, 8)
writeLines(c("\npath.ids.upExpr:", head2(path.ids.upExpr)))
writeLines(c("\nlength(path.ids.upExpr):", length(path.ids.upExpr)))
path.ids.downExpr <- rownames(fc.kegg.p$less)[downExpressed]
path.ids2.downExpr <- substr(path.ids.downExpr, 1, 8)
writeLines(c("\npath.ids.downExpr:", head2(path.ids.downExpr)))
writeLines(c("\nlength(path.ids.downExpr):", length(path.ids.downExpr)))
## path.ids.upOrDownExpr <- substr(c(path.ids, path.ids.l), 1, 8)
## writeLines(c("\npath.ids.upOrDownExpr:", head2(path.ids.upOrDownExpr)))
## writeLines(c("\nlength(path.ids.upOrDownExpr):", length(path.ids.upOrDownExpr)))
# view first N pathways as demo
# species = "hsa" is for human
topN = min(numberPathwaysDown, length(path.ids.upExpr))
topN
if (topN > 0) {
pv.out.list <- sapply(path.ids2.upExpr[1:topN],
function(pid) pathview(gene.data = exp.fc, pathway.id = pid, species = kegg.code, out.suffix = "cuffdiff", kegg.dir = ".", kegg.native=TRUE))
pv.out.list
}
topN = min(numberPathwaysUp, length(path.ids.downExpr))
topN
if (topN > 0) {
pv.out.list <- sapply(path.ids2.downExpr[1:topN],
function(pid) pathview(gene.data = exp.fc, pathway.id = pid, species = kegg.code, out.suffix = "cuffdiff", kegg.dir = ".", kegg.native=TRUE))
pv.out.list
}
## topN = min(numberPathways, length(path.ids.upOrDownExpr))
## pv.out.list <- sapply(path.ids.upOrDownExpr[1:topN],
## function(pid) pathview(gene.data = exp.fc, pathway.id = pid, species = kegg.code, out.suffix = "cuffdiff", kegg.dir = outDir, kegg.native=TRUE))
## pv.out.list
}
## Here we used GAGE to infer the significant pathways. But we are not limited to these pathways. We can
## use Pathview to visualize RNA-Seq data (exp.fc here) on all interesting pathways directly. We may also do GO
## and other types of gene set analysis as described in native workflow above.