-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeal_GO.R
141 lines (123 loc) · 4.69 KB
/
deal_GO.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
# get_gene_mat <- function(net_disease, GO_human) {
# net_genes <- union(net_disease[, 1], net_disease[, 2])
# aa <- which(GO_human[, 1] %in% net_genes)
# GO_file <- GO_human[aa, ]
# genes <- unique(GO_file[, 1])
# genes_mat <- matrix("gaga", length(genes), 3)
# genes_mat[, 1] <- genes
# rownames(genes_mat) <- genes_mat[, 1]
# for (i in seq_len(dim(GO_file)[1])) {
# genes_mat[GO_file[i, 1], 2] <- paste(genes_mat[GO_file[i, 1], 2],
# GO_file[i, 2], sep = ",")
# }
# for (i in seq_len(dim(genes_mat)[1])) {
# genes_mat[i, 2] <- sub("gaga,", "", genes_mat[i, 2])
# genes_mat[i, 3]<- length(unique(unlist(strsplit(genes_mat[i, 2],","))))
# }
# return(genes_mat)
# }
#' Get a one-to-many matrix of gene and GO term
#'
#' @param net_disease a disease related network, matrix
#' @return a matrix
#' @importFrom AnnotationDbi select
#' @importFrom org.Hs.eg.db org.Hs.eg.db
#' @export
#'
#' @examples
#' get_gene_mat(net_disease)
get_gene_mat <- function(net_disease) {
net_genes <- union(net_disease[, 1], net_disease[, 2])
GO_file <- select(org.Hs.eg.db, keys=net_genes, columns="GO", keytype="SYMBOL")
genes <- unique(GO_file[, "SYMBOL"])
genes_mat <- matrix("gaga", length(genes), 3)
genes_mat[, 1] <- genes
rownames(genes_mat) <- genes_mat[, 1]
for (i in seq_len(dim(GO_file)[1])) {
if(!is.na(GO_file[i, "GO"]))
genes_mat[GO_file[i, "SYMBOL"], 2] <- paste(genes_mat[GO_file[i, "SYMBOL"], 2],
GO_file[i, "GO"], sep = ",")
}
for (i in seq_len(dim(genes_mat)[1])) {
genes_mat[i, 2] <- sub("gaga,", "", genes_mat[i, 2])
genes_mat[i, 3]<- length(unique(unlist(strsplit(genes_mat[i, 2],","))))
}
genes_mat <- genes_mat[which(genes_mat[,2] != "gaga"),]
return(genes_mat)
}
# get_term_mat <- function(net_disease, GO_human) {
# net_genes <- union(net_disease[, 1], net_disease[, 2])
# aa <- which(GO_human[, 1] %in% net_genes)
# GO_file <- GO_human[aa, ]
# go_terms <- unique(GO_file[, 2])
# terms_mat <- matrix("gaga", length(go_terms), 3)
# terms_mat[, 1] <- go_terms
# rownames(terms_mat) <- terms_mat[, 1]
# for (i in seq_len(dim(GO_file)[1])) {
# terms_mat[GO_file[i, 2], 2] <- paste(terms_mat[GO_file[i, 2], 2],
# GO_file[i, 1], sep = ",")
# }
# for (i in seq_len(dim(terms_mat)[1])) {
# terms_mat[i, 2] <- sub("gaga,", "", terms_mat[i, 2])
# terms_mat[i, 3] <-length(unique(unlist(strsplit(terms_mat[i, 2],","))))
# }
# return(terms_mat)
# }
#' Get a one-to-many matrix of GO term and gene
#'
#' @param net_disease a disease related network, matrix
#' @return a matrix
#' @importFrom AnnotationDbi select
#' @importFrom org.Hs.eg.db org.Hs.eg.db
#' @export
#'
#' @examples
#' get_term_mat(net_disease)
get_term_mat <- function(net_disease) {
net_genes <- union(net_disease[, 1], net_disease[, 2])
GO_file <- select(org.Hs.eg.db, keys=net_genes, columns="GO", keytype="SYMBOL")
go_terms <- unique(GO_file[, "GO"])
terms_mat <- matrix("gaga", length(go_terms), 3)
terms_mat[, 1] <- go_terms
rownames(terms_mat) <- terms_mat[, 1]
for (i in seq_len(dim(GO_file)[1])) {
if(!is.na(GO_file[i, "GO"]))
terms_mat[GO_file[i, "GO"], 2] <- paste(terms_mat[GO_file[i, "GO"], 2],
GO_file[i, "SYMBOL"], sep = ",")
}
for (i in seq_len(dim(terms_mat)[1])) {
terms_mat[i, 2] <- sub("gaga,", "", terms_mat[i, 2])
terms_mat[i, 3] <-length(unique(unlist(strsplit(terms_mat[i, 2],","))))
}
terms_mat <- terms_mat[which(terms_mat[,2] != "gaga"),]
return(terms_mat)
}
#' Title Get the GO terms for each pair of nodes in the network
#'
#' @param genes_mat a one-to-many matrix of GO term and gene
#' @param net_disease a disease related network, matrix
#'
#' @return a matrix
#' @export
#'
#' @examples
#' get_net_disease_term(genes_mat,net_disease)
get_net_disease_term <- function(genes_mat, net_disease) {
genes <- genes_mat[, 1]
aa1 <- which(net_disease[, 1] %in% genes)
aa2 <- which(net_disease[, 2] %in% genes)
aa <- intersect(aa1, aa2)
net_disease_small <- net_disease[aa, ]
inter_term <- rep(0, dim(net_disease_small)[1])
inter_term_num <- rep(0, dim(net_disease_small)[1])
for (i in seq_len(length(inter_term))) {
aa1 <- unlist(strsplit(genes_mat[net_disease_small[i, 1], 2], ","))
aa2 <- unlist(strsplit(genes_mat[net_disease_small[i, 2], 2], ","))
aa <- intersect(aa1, aa2)
inter_term[i] <- paste(aa, collapse = ",")
inter_term_num[i] <- length(aa)
}
net_disease_term <- cbind(net_disease_small,
inter_term, inter_term_num)
return(net_disease_term)
}