-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotateProteinInfoFromIndra.R
More file actions
212 lines (202 loc) · 8.11 KB
/
Copy pathannotateProteinInfoFromIndra.R
File metadata and controls
212 lines (202 loc) · 8.11 KB
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
#' Annotate Protein Information from Indra
#'
#' This function annotates a data frame with protein information from Indra.
#'
#' @param df output of \code{\link[MSstats]{groupComparison}} function's
#' comparisonResult table, which contains a list of proteins and their
#' corresponding p-values, logFCs, along with additional HGNC ID and HGNC
#' name columns
#' @param proteinIdType A character string specifying the type of protein ID.
#' It can be either "Uniprot", "Uniprot_Mnemonic", or "Hgnc_Name".
#' @return A data frame with the following columns:
#' \describe{
#' \item{Protein}{Character. The original protein identifier.}
#' \item{UniprotID}{Character. The Uniprot ID of the protein.}
#' \item{HgncID}{Character. The HGNC ID of the protein.}
#' \item{HgncName}{Character. The HGNC name of the protein.}
#' \item{IsTranscriptionFactor}{Logical. Indicates if the protein is a transcription factor.}
#' \item{IsKinase}{Logical. Indicates if the protein is a kinase.}
#' \item{IsPhosphatase}{Logical. Indicates if the protein is a phosphatase.}
#' }
#' @examples
#' df <- data.frame(Protein = c("CLH1_HUMAN"))
#' annotated_df <- annotateProteinInfoFromIndra(df, "Uniprot_Mnemonic")
#' head(annotated_df)
#' @export
annotateProteinInfoFromIndra <- function(df, proteinIdType) {
.validateAnnotateProteinInfoFromIndraInput(df)
df <- .populateUniprotIdsInDataFrame(df, proteinIdType)
df <- .populateHgncIdsInDataFrame(df, proteinIdType)
df <- .populateHgncNamesInDataFrame(df)
df <- .populateTranscriptionFactorInfoInDataFrame(df)
df <- .populateKinaseInfoInDataFrame(df)
df <- .populatePhophataseInfoInDataFrame(df)
return(df)
}
#' Validate Annotate Protein Info Input
#'
#' This function validates the input data frame for the annotateProteinInfoFromIndra function.
#'
#' @param df A data frame containing protein information.
#' @return None. Throws an error if validation fails.
.validateAnnotateProteinInfoFromIndraInput <- function(df) {
if (!"Protein" %in% colnames(df)) {
stop("Input dataframe must contain 'Protein' column.")
}
}
#' Populate Uniprot IDs in Data Frame
#'
#' This function populates the Uniprot IDs in the data frame based on the protein ID type.
#'
#' @param df A data frame containing protein information.
#' @param proteinIdType A character string specifying the type of protein ID.
#' It can be either "Uniprot" or "Uniprot_Mnemonic".
#' @return A data frame with populated Uniprot IDs.
.populateUniprotIdsInDataFrame <- function(df, proteinIdType) {
if ("GlobalProtein" %in% colnames(df)) {
protein_ids <- unique(as.character(df$GlobalProtein))
} else {
df$Protein <- as.character(df$Protein)
df$GlobalProtein <- ifelse(grepl("_[A-Z][0-9]", df$Protein),
gsub("_[A-Z][0-9].*", "", df$Protein, perl = TRUE),
df$Protein
)
protein_ids <- unique(df$GlobalProtein)
}
df$UniprotId <- NA
if (proteinIdType == "Uniprot") {
df$UniprotId <- as.character(df$GlobalProtein)
}
if (proteinIdType == "Uniprot_Mnemonic") {
mnemonicProteins <- protein_ids
if (length(mnemonicProteins) > 0) {
uniprotMapping <- .callGetUniprotIdsFromUniprotMnemonicIdsApi(as.list(mnemonicProteins))
for (mnemonicId in names(uniprotMapping)) {
if (!is.null(uniprotMapping[[mnemonicId]])) {
df$UniprotId[df$GlobalProtein == mnemonicId] <- uniprotMapping[[mnemonicId]]
}
}
}
}
if (proteinIdType == "Hgnc_Name") {
df$UniprotId <- NA
}
return(df)
}
#' Populate HGNC IDs in Data Frame
#'
#' This function populates the HGNC IDs in the data frame based on the Uniprot IDs.
#'
#' @param df A data frame containing protein information.
#' @param proteinIdType A character string specifying the type of protein ID.
#' It can be either "Uniprot", "Uniprot_Mnemonic", or "Hgnc_Name".
#' @return A data frame with populated HGNC IDs.
.populateHgncIdsInDataFrame <- function(df, proteinIdType) {
df$HgncId <- NA
if (proteinIdType == "Uniprot" || proteinIdType == "Uniprot_Mnemonic") {
validMask <- !is.na(df$UniprotId)
validUniprots <- unique(df$UniprotId[validMask])
if (length(validUniprots) > 0) {
hgncMapping <- .callGetHgncIdsFromUniprotIdsApi(as.list(validUniprots))
for (uniprotId in names(hgncMapping)) {
if (!is.null(hgncMapping[[uniprotId]])) {
df$HgncId[df$UniprotId == uniprotId] <- hgncMapping[[uniprotId]]
}
}
}
} else {
hgncNames <- unique(df$GlobalProtein)
if (length(hgncNames) > 0) {
hgncMapping <- .callGetHgncIdsFromGildaApi(as.list(hgncNames))
for (hgncName in names(hgncMapping)) {
if (!is.null(hgncMapping[[hgncName]])) {
df$HgncId[df$GlobalProtein == hgncName] <- hgncMapping[[hgncName]]
}
}
}
}
return(df)
}
#' Populate HGNC Names in Data Frame
#'
#' This function populates the HGNC names in the data frame based on the HGNC IDs.
#'
#' @param df A data frame containing protein information.
#' @return A data frame with populated HGNC names.
.populateHgncNamesInDataFrame <- function(df) {
df$HgncName <- NA
validHgncMask <- !is.na(df$HgncId)
validHgncs <- unique(df$HgncId[validHgncMask])
if (length(validHgncs) > 0) {
nameMapping <- .callGetHgncNamesFromHgncIdsApi(as.list(validHgncs))
for (hgncId in names(nameMapping)) {
if (!is.null(nameMapping[[hgncId]])) {
df$HgncName[df$HgncId == hgncId] <- nameMapping[[hgncId]]
}
}
}
return(df)
}
#' Populate Transcription Factor Info in Data Frame
#'
#' This function populates the transcription factor information in the data frame based on the HGNC names.
#'
#' @param df A data frame containing protein information.
#' @return A data frame with populated transcription factor information.
.populateTranscriptionFactorInfoInDataFrame <- function(df) {
df$IsTranscriptionFactor <- NA
validNameMask <- !is.na(df$HgncName)
validNames <- unique(df$HgncName[validNameMask])
if (length(validNames) > 0) {
validNamesList <- as.list(validNames)
charMapping <- .callIsTranscriptionFactorApi(validNamesList)
for (hgncName in names(charMapping)) {
if (!is.null(charMapping[[hgncName]])) {
df$IsTranscriptionFactor[df$HgncName == hgncName] <- charMapping[[hgncName]]
}
}
}
return(df)
}
#' Populate Kinase Info in Data Frame
#'
#' This function populates the kinase information in the data frame based on the HGNC names.
#'
#' @param df A data frame containing protein information.
#' @return A data frame with populated kinase information.
.populateKinaseInfoInDataFrame <- function(df) {
df$IsKinase <- NA
validNameMask <- !is.na(df$HgncName)
validNames <- unique(df$HgncName[validNameMask])
if (length(validNames) > 0) {
validNamesList <- as.list(validNames)
charMapping <- .callIsKinaseApi(validNamesList)
for (hgncName in names(charMapping)) {
if (!is.null(charMapping[[hgncName]])) {
df$IsKinase[df$HgncName == hgncName] <- charMapping[[hgncName]]
}
}
}
return(df)
}
#' Populate Phosphatase Info in Data Frame
#'
#' This function populates the phosphatase information in the data frame based on the HGNC names.
#'
#' @param df A data frame containing protein information.
#' @return A data frame with populated phosphatase information.
.populatePhophataseInfoInDataFrame <- function(df) {
df$IsPhosphatase <- NA
validNameMask <- !is.na(df$HgncName)
validNames <- unique(df$HgncName[validNameMask])
if (length(validNames) > 0) {
validNamesList <- as.list(validNames)
charMapping <- .callIsPhosphataseApi(validNamesList)
for (hgncName in names(charMapping)) {
if (!is.null(charMapping[[hgncName]])) {
df$IsPhosphatase[df$HgncName == hgncName] <- charMapping[[hgncName]]
}
}
}
return(df)
}