forked from joseah/regutools
/
get_gene_regulators.R
194 lines (180 loc) · 6.49 KB
/
get_gene_regulators.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
#' @title Get TFs or genes that regulate the genes of interest
#' @description Given a list of genes (name, bnumber or GI),
#' get all transcription factors or genes that regulate them.
#' The effect of regulators over the gene of interest can be positive (+),
#' negative (-) or dual (+/-)
#' @param regulondb A regulondb class.
#' @param genes Vector of genes (name, bnumber or GI).
#' @param format Output format: multirow, onerow, table
#' @param output.type How regulators will be represented: "TF"/"GENE"
#' @keywords regulation retrieval, TFs, networks,
#' @author Carmina Barberena Jonas, Jesús Emiliano Sotelo Fonseca,
#' José Alquicira Hernández, Joselyn Chávez
#' @return A [regulondb_result][regutools::regulondb_result-class] object.
#' @examples
#' ## Connect to the RegulonDB database if necessary
#' if (!exists("regulondb_conn")) regulondb_conn <- connect_database()
#'
#' ## Build the regulon db object
#' e_coli_regulondb <-
#' regulondb(
#' database_conn = regulondb_conn,
#' organism = "E.coli",
#' database_version = "1",
#' genome_version = "1"
#' )
#'
#' ## Get Transcription factors that regulate araC in one row
#' get_gene_regulators(
#' e_coli_regulondb,
#' genes = c("araC"),
#' output.type = "TF",
#' format = "onerow"
#' )
#'
#' ## Get genes that regulate araC in table format
#' get_gene_regulators(
#' e_coli_regulondb,
#' genes = c("araC"),
#' output.type = "GENE",
#' format = "table"
#' )
#' @export
get_gene_regulators <-
function(regulondb,
genes,
format = "multirow",
output.type = "TF") {
stopifnot(validObject(regulondb))
# Check genes parameter class
## if (!class(genes) %in% c("vector", "list", "character")) {
if (!(is(genes, "vector") | is(genes, "list") | is(genes, "character")
)) {
stop("Parameter 'genes' must be a character vector or list.",
call. = FALSE
)
}
# Check format parameter
if (!format %in% c("multirow", "onerow", "table")) {
stop("Parameter 'format' must be multirow, onerow, or table.",
call. = FALSE
)
}
# Check output.type
if (!output.type %in% c("TF", "GENE")) {
stop("Parameter 'output.type' must be either 'TF' or 'GENE'",
call. = FALSE
)
}
# Convert GIs to gene names
# Assign id per gene
gene_guesses <- vapply(genes, guess_id,
regulondb = regulondb,
FUN.VALUE = character(1)
)
# Check that guesses are names
if (!all(gene_guesses == "name")) {
split_ids <- split(x = genes, f = gene_guesses)
# Get synonyms for each group
names_list <- lapply(names(split_ids), function(id) {
get_gene_synonyms(regulondb,
genes = split_ids[[id]],
from = id,
to = "name"
)[["name"]]
})
# Cat id list
genes <- unlist(names_list)
names(genes) <- NULL
}
# Checks for type of network
if (output.type == "TF") {
network.type <- "TF-GENE"
} else if (output.type == "GENE") {
network.type <- "GENE-GENE"
}
# Retrieve data from NETWORK table
regulation <-
as.data.frame(get_dataset(
regulondb,
attributes = c("regulated_name", "regulator_name", "effect"),
filters = list(
"regulated_name" = genes,
"network_type" = network.type
),
dataset = "NETWORK"
))
colnames(regulation) <- c("genes", "regulators", "effect")
# Change effect to +, - and +/-
regulation$effect <-
sub(
pattern = "activator",
replacement = "+",
x = regulation$effect
)
regulation$effect <-
sub(
pattern = "repressor",
replacement = "-",
x = regulation$effect
)
regulation$effect <-
sub(
pattern = "dual",
replacement = "+/-",
x = regulation$effect
)
# Format output
# Multirow
if (format == "multirow") {
# Add internal attribute "format" to use in GetSummary function.
regulation <-
dataframe_to_dbresult(regulation, regulondb, "NETWORK")
metadata(regulation)$format <- format
return(regulation)
# Onerow
} else if (format == "onerow") {
regulation <- lapply(as.list(genes), function(x) {
genereg <- regulation[regulation[, "genes"] == x, ]
genereg <-
paste(
paste(
genereg$regulators,
genereg$effect,
sep = "(",
collapse = "), "
),
")",
sep = ""
)
})
regulation <- unlist(regulation)
genes <- unlist(genes)
regulation <- data.frame(genes, regulation)
colnames(regulation) <- c("genes", "regulators")
# Add internal attribute "format" to use in GetSummary function.
regulation <-
dataframe_to_dbresult(regulation, regulondb, "NETWORK")
metadata(regulation)$format <- format
return(regulation)
# Table
} else if (format == "table") {
# Empty dataframe
rtable <-
data.frame(matrix(nrow = length(genes), ncol = length(c(
unique(regulation$regulators)
))))
colnames(rtable) <- unique(regulation$regulators)
rownames(rtable) <- genes
# Fill dataframe with regulation
for (i in seq_len(dim(regulation)[1])) {
rtable[regulation[i, 1], regulation[i, 2]] <- regulation[i, 3]
}
regulation <- rtable
# Add internal attribute "format" to use in GetSummary function.
regulation <-
dataframe_to_dbresult(regulation, regulondb, "NETWORK")
metadata(regulation)$format <- format
return(regulation)
}
}