-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKEGGLoader.R
272 lines (233 loc) · 8.07 KB
/
KEGGLoader.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
source("initLibrary.R")
source("utilityFunction.R")
getPathwayGenes <- function(hsa_code){
#get list of genes by its symbol for a specified pathway
data <- getURL(paste("http://togows.dbcls.jp/entry/pathway/",hsa_code,"/genes.json",sep=""))
data <- t(fromJSON(data))
entrezID <- row.names(data)
gene.db[gene.db$EntrezGene.ID%in%entrezID,]
}
loadKGML <- function(hsa_code){
#download KGML file from the server
message("Get KGML file from the server")
dataXML <- xmlParse(paste("http://rest.kegg.jp/get/",hsa_code,"/kgml",sep=""))
dataXML <- xmlToList(dataXML)
dataXML
}
KGMLParser <- function(KGML){
#parse KGML to get the data, only select entry and relationship type from the KGML
#there are several tag in KGML data format. The network information is stored in entry and relationship tag.
#entry defined vertices and relationship defined edges
#entry in original KEGG network doesn't represent a single gene. It represents a group or a fimily of gene
#each entry is given an entry ID and this entry ID is used in relationship data to connect 2 entries in network
message("get the nodes from the pathway")
entry <- list()
idxEntry<-1
relation <- list()
idxRelation<-1
for(i in 1:length(KGML)){
if(names(KGML[i])=="entry"){
entry[[idxEntry]] <- KGML[[i]]
idxEntry <- idxEntry+1
}else if(names(KGML[i])=="relation"){
relation[[idxRelation]] <- KGML[[i]]
idxRelation <- idxRelation+1
}
}
entry <- lapply(entry,function(x)x[".attrs"])
entry <- lapply(entry,function(x) as.data.frame(t(x[[1]]),stringsAsFactors=FALSE))
entry <- lapply(entry,function(x){
if(ncol(x)==4){
x
}
})
entry <- do.call(rbind,entry)
entry <- entry[entry$type=="gene",]
list("entry"=entry[entry$type=="gene",],"relationship"=relationshipDataFormatter(relation))
}
relationshipDataFormatter <- function(relation){
#format the relationship data extracted from KGML as a table
#data extracted from KGML is not formatted. It is just parsed string by R function
#this function reformat it to table format
message("formatting relationship data")
relationship <- c()
for(xx in 1:length(relation)){
innerList <- relation[[xx]]
#innerList consist of subtype and .attrs
c <-c()
attr <- NULL
for(j in 1:length(innerList)){
if(names(innerList[j])=="subtype"){
c<-rbind(c,innerList[[j]])
}else{
attr <- as.data.frame(t(innerList[[j]]))
}
}
relationship <- rbind(relationship,merge(c,attr))
}
relationship
}
createRelationshipTable <- function(entry,relationship){
#create gene-gene relationnship
#it extracts the gene information from entry table
#use relationship data based on its entry ID to make gene-gene relationship
relationshipTable <- data.frame()
for (idx in 1:nrow(relationship)){
row <- relationship[idx,]
relType <- as.character(levels(relationship$name)[relationship[idx,"name"]])
en1 <- as.numeric(levels(relationship$entry1)[relationship[idx,"entry1"]])
en2 <- as.numeric(levels(relationship$entry2)[relationship[idx,"entry2"]])
entry1 <- entry[entry$id==en1,"name"]
entry2 <- entry[entry$id==en2,"name"]
if ((length(entry1) == 0) && (typeof(entry1) == "character")){
next
}
if ((length(entry2) == 0) && (typeof(entry2) == "character")){
next
}
entry1genes <- strsplit(entry1, " ")[[1]]
entry2genes <- strsplit(entry2, " ")[[1]]
for (gene1 in entry1genes){
gene1 <- substring(gene1,5)
for (gene2 in entry2genes){
gene2 <- substring(gene2,5)
newRow <- data.frame("relType"=relType,"entry1"=en1,"entry2"=en2,stringsAsFactors = FALSE)
newRow$gene1 <- gene1
newRow$gene2 <- gene2
relationshipTable <- rbind(relationshipTable,newRow)
}
}
}
relationshipTable
}
createAdjacencyMatrix <- function(relationshipTable,distinct=TRUE){
#change the format of relationship table into adjacency matrix
#because there is a possibilities for a gene has 2 entry ID, the entry ID is attached to gene name to identify it
#it happens if in one KGML, there are several separated network, for example : alternative pathway
#the matrix result is : column == target gene, row == TF gene
if(distinct){
element <- unique(c(paste(relationshipTable$gene1,relationshipTable$entry1,sep="."),paste(relationshipTable$gene2,relationshipTable$entry2,sep=".")))
}else{
element <- unique(c(relationshipTable$gene1,relationshipTable$gene2))
}
adjMat <- matrix(0, nrow = length(element), ncol = length(element))
colnames(adjMat) <- element
rownames(adjMat) <- element
for (i in 1:nrow(relationshipTable)){
r <- relationshipTable[i,]
rel <- getKEGGRelationCode(r$relType)
if(distinct){
adjMat[paste(r$gene1,r$entry1,sep="."),paste(r$gene2,r$entry2,sep=".")] <- rel
}else{
adjMat[r$gene1,r$gene2] <- rel
}
}
adjMat
}
tableToAdjmatrix <- function(table){
element <- union(table$Target,table$TF)
adjMat <- matrix(0, nrow = length(element), ncol = length(element))
colnames(adjMat) <- element
rownames(adjMat) <- element
for (i in 1:nrow(table)){
adjMat[table[i,"TF"],table[i,"Target"]] <- table[i,"relType"]
}
adjMat
}
setGeneSymbolAdjMatrix <- function(list){
for (i in 1:length(list)){
for(x in 1:(length(colnames(list[[i]])))){
colnames(list[[i]])[x] <- convertGeneID(unlist(strsplit(colnames(list[[i]])[x],"[.]"))[1],"entrez","symbol")
}
for(x in 1:(length(rownames(list[[i]])))){
rownames(list[[i]])[x] <- convertGeneID(unlist(strsplit(rownames(list[[i]])[x],"[.]"))[1],"entrez","symbol")
}
}
return(list)
}
setAdjMatrixColRowNames <- function(list){
for (i in 1:length(list)){
for(x in 1:(length(colnames(list[[i]])))){
colnames(list[[i]])[x] <- unlist(strsplit(colnames(list[[i]])[x],"[.]"))[1]
}
for(x in 1:(length(rownames(list[[i]])))){
rownames(list[[i]])[x] <- unlist(strsplit(colnames(list[[i]])[x],"[.]"))[1]
}
}
return(list)
}
separateAdjMatrix <- function(matrix){
#for adj matrix which contains more than one pathway,
#separate it so that the adj matrix consist only 1 pathway
#output is a list of matrix
dfs <- function(gene,matrix){
evaluated <<- c(evaluated,gene)
TFs <- getTF(gene,matrix)
if(!is.null(TFs)){
for(tf in TFs){
if(nrow(tesDFS[tesDFS$TF==tf & tesDFS$Target==gene,])==0){
tesDFS <<- rbind(tesDFS,data.frame("TF"=tf,"Target"=gene,"relType"=matrix[tf,gene],stringsAsFactors = FALSE))
}
}
for(tf in TFs){
if(!tf%in%evaluated){
dfs(tf,matrix)
}
}
}
}
resList <- list()
for(gene in getEndNode(matrix)){
tesDFS <- data.frame()
evaluated <- c()
dfs(gene,matrix)
resList[[gene]]<-tesDFS
}
resListBak <- resList
mergedRes <- list()
cur <- 1
while(length(resListBak)>0){
mergedRes[[cur]] <- resListBak[[1]]
resListBak <- resListBak[-1]
if(length(resListBak)==0){
break
}
merged <- c()
for(i in 1:length(resListBak)){
temp <- union(mergedRes[[cur]],resListBak[[i]])
if(nrow(temp)<nrow(mergedRes[[cur]])+nrow(resListBak[[i]])){
mergedRes[[cur]] <- temp
merged <- c(merged,i)
}
}
if(!is.null(merged)){
resListBak <- resListBak[-c(merged)]
}
cur <- cur+1
}
for(i in 1:length(mergedRes)){
mergedRes[[i]]<-tableToAdjmatrix(mergedRes[[i]])
}
return(setAdjMatrixColRowNames(mergedRes))
}
getPathwayRelationshipTable<-function(hsa_code){
#get KEGG pathway network in the form of relationship table
KGML <- loadKGML(hsa_code)
parsedKGML <- KGMLParser(KGML)
entry <- parsedKGML[["entry"]]
relationship <- parsedKGML[["relationship"]]
relationshipTable <- createRelationshipTable(entry,relationship)
}
getEndNode <- function(matrix){
#get the end node of the network represents in the adj matrix
output <- c()
for (g in colnames(matrix)){
if (is.null(getTarget(g,matrix))){
output <- c(output,g)
}
}
output
}
getTFType <- function(tf,target,matrix){
matrix[tf,target]
}