/
clusterGeneSim.R
executable file
·174 lines (158 loc) · 5.88 KB
/
clusterGeneSim.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
# mclusterGeneSim ####
#' Similarity score between clusters of genes based on genes similarity
#'
#' Looks for the similarity between genes of a group and then between each
#' group's genes.
#'
#' Differs with clusterSim that first each combination between genes is
#' calculated, and with this values then the comparison between the two
#' clusters is done. Thus applying combineScores twice, one at gene level and
#' another one at cluster level.
#' @inheritParams clusterSim
#' @inheritParams geneSim
#' @inheritParams pathSim
#' @param method A vector with two or one argument to be passed to
#' combineScores the first one is used to summarize the similarities of genes,
#' the second one for clusters.
#' @export
#' @author Lluís Revilla
#' @seealso [mclusterGeneSim()], [combineScores()] and
#' [clusterSim()]
#' @return Returns a similarity score between the genes of the two clusters.
#' @examples
#' if (require("org.Hs.eg.db")) {
#' # Extract the paths of all genes of org.Hs.eg.db from KEGG (last update in
#' # data of June 31st 2011)
#' genes.kegg <- as.list(org.Hs.egPATH)
#' clusterGeneSim(c("18", "81", "10"), c("100", "10", "1"), genes.kegg)
#' clusterGeneSim(
#' c("18", "81", "10"), c("100", "10", "1"), genes.kegg,
#' c("avg", "avg")
#' )
#' clusterGeneSim(
#' c("18", "81", "10"), c("100", "10", "1"), genes.kegg,
#' c("avg", "rcmax.avg")
#' )
#' (clus <- clusterGeneSim(
#' c("18", "81", "10"), c("100", "10", "1"),
#' genes.kegg, "avg"
#' ))
#' combineScores(clus, "rcmax.avg")
#' } else {
#' warning("You need org.Hs.eg.db package for this example")
#' }
clusterGeneSim <- function(cluster1, cluster2, info,
method = c("max", "rcmax.avg"), ...) {
if (length(unique(cluster1)) == 1L && length(unique(cluster2)) == 1L) {
stop(
"Introduce several genes in each cluster!\n",
"If you want to calculate similarities ",
"between two genes use geneSim"
)
}
if (!all(is.character(cluster1)) || !all(is.character(cluster2))) {
stop("The input genes should be characters")
}
cluster1 <- unique(cluster1)
cluster2 <- unique(cluster2)
if (!is.list(info)) {
stop("info should be a list. See documentation.")
}
if (any(!cluster1 %in% names(info)) || any(!cluster2 %in% names(info))) {
warning("Some genes are not in the list provided.")
}
if (length(method) > 2L || is.null(method)) {
stop(
"Please provide two or one methods to combine scores.",
"See Details"
)
}
# Extract all pathways for each gene
pathways1.a <- lapply(cluster1, getElement, object = info)
names(pathways1.a) <- cluster1
pathways2.a <- lapply(cluster2, getElement, object = info)
names(pathways2.a) <- cluster2
# Remove duplicated and NA
pathways1 <- unique(unlist(pathways1.a, use.names = FALSE))
pathways2 <- unique(unlist(pathways2.a, use.names = FALSE))
pathways1 <- pathways1[!is.na(pathways1)]
pathways2 <- pathways2[!is.na(pathways2)]
pathways <- unique(c(pathways1, pathways2))
if (is.null(pathways1) || is.null(pathways2)) {
return(NA)
}
simPaths <- mpathSim(pathways, info, method = NULL, ...)
genes <- combineScoresPar(simPaths, method[[1L]],
c(pathways1.a, pathways2.a),
... = ...
)
genes <- genes[names(pathways1.a), names(pathways2.a), drop = FALSE]
if (length(method) == 2L) {
combineScoresPar(as.matrix(genes), method = method[[2L]], ... = ...)
} else {
as.matrix(genes)
}
}
#' @describeIn clusterGeneSim Calculates the gene similarities in a
#' GeneSetCollection and combine them using [combineScoresPar()]
#' @export
setMethod(
"clusterGeneSim",
c(
info = "GeneSetCollection", cluster1 = "character",
cluster2 = "character"
),
function(cluster1, cluster2, info, method, ...) {
if (length(unique(cluster1)) == 1L & length(unique(cluster2)) == 1L) {
stop(
"Introduce several genes in each cluster!\n",
"If you want to calculate similarities ",
"between two genes use geneSim"
)
}
if (!all(is.character(cluster1)) | !all(is.character(cluster2))) {
stop("The input genes should be characters")
}
cluster1 <- unique(cluster1)
cluster2 <- unique(cluster2)
# Revert back to list
list_info <- inverseList(GSEABase::geneIds(info))
if (any(!cluster1 %in% names(list_info)) | any(!cluster2 %in% names(list_info))) {
cluster1 <- cluster1[cluster1 %in% names(list_info)]
cluster2 <- cluster2[cluster2 %in% names(list_info)]
warning("Some genes are not in the GeneSetCollection provided.")
}
if (length(method) > 2L | is.null(method)) {
stop(
"Please provide two or one method to combine scores.",
"See Details"
)
}
# FIXME: Should take advantage of GSC object (if any)
# Extract all pathways for each gene
pathways1.a <- lapply(cluster1, getElement, object = list_info)
names(pathways1.a) <- cluster1
pathways2.a <- lapply(cluster2, getElement, object = list_info)
names(pathways2.a) <- cluster2
# Remove duplicated and NA
pathways1 <- unique(unlist(pathways1.a, use.names = FALSE))
pathways2 <- unique(unlist(pathways2.a, use.names = FALSE))
pathways1 <- pathways1[!is.na(pathways1)]
pathways2 <- pathways2[!is.na(pathways2)]
if (is.null(pathways1) || is.null(pathways2)) {
return(NA)
}
pathways <- unique(c(pathways1, pathways2))
simPaths <- mpathSim(pathways, list_info, method = NULL, ...)
genes <- combineScoresPar(simPaths, method[[1L]],
c(pathways1.a, pathways2.a),
... = ...
)
genes <- genes[names(pathways1.a), names(pathways2.a), drop = FALSE]
if (length(method) == 2L) {
combineScoresPar(as.matrix(genes), method = method[[2L]], ... = ...)
} else {
as.matrix(genes)
}
}
)