Skip to content

Commit d44349a

Browse files
committed
filterings: output full information (fixes #3); merge the functionalities of filterOutIn() and filterProtein() into filterOutIn(), and remove filterProtein().
1 parent 3d692c1 commit d44349a

2 files changed

Lines changed: 55 additions & 124 deletions

File tree

R/filterings.R

Lines changed: 52 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#'
4242
#' @details
4343
#' All forms of filtering are recommended for most use cases.
44-
#'
44+
#'
4545
#' @return
4646
#' A filtered 2d dataframe.
4747
#'
@@ -65,7 +65,7 @@ preProcessFiltering <- function(dataSet,
6565
removedData <- filteredData %>% filter(is.nan(PG.Quantity))
6666

6767
## save removed data to current working directory
68-
write.csv(removedData, file = "preprocess_Filtered_Out_NaN.csv", row.names = FALSE)
68+
write.csv(removedData, file = "preprocess_filterNaN.csv", row.names = FALSE)
6969
}
7070

7171
## filter out the proteins that have no recorded value
@@ -82,7 +82,7 @@ preProcessFiltering <- function(dataSet,
8282
filter(PG.NrOfStrippedSequencesIdentified < filterUnique)
8383

8484
## save removed data to current working directory
85-
write.csv(removedData, file = "preprocess_Filtered_Out_Unique.csv", row.names = FALSE)
85+
write.csv(removedData, file = "preprocess_filterUnique.csv", row.names = FALSE)
8686
}
8787

8888
## filter out proteins that have only 1 unique peptide
@@ -95,9 +95,9 @@ preProcessFiltering <- function(dataSet,
9595

9696
## replace blank protein name entries with their accession numbers
9797
filteredData <- filteredData %>%
98-
mutate(PG.ProteinNames =
99-
replace(PG.ProteinNames, PG.ProteinNames == "",
100-
PG.ProteinAccessions[PG.ProteinNames == ""]))
98+
mutate(PG.ProteinName =
99+
replace(PG.ProteinName, PG.ProteinName == "",
100+
PG.ProteinAccession[PG.ProteinName == ""]))
101101
}
102102

103103
## return the filtered data
@@ -114,11 +114,21 @@ preProcessFiltering <- function(dataSet,
114114
#'
115115
#' @param dataSet The 2d data set of experimental values.
116116
#'
117-
#' @param listName A character vector of proteins to select or remove.
117+
#' @param listName A character vector of text strings used as keys for selecting or
118+
#' removing.
118119
#'
119120
#' @param regexName A character vector specifying proteins for regular expression pattern
120121
#' matching to select or remove.
121122
#'
123+
#' @param by A character string (default = "PG.ProteinName" for Spectronaut, default =
124+
#' "AccessionNumber" for Scaffold) specifying the information to which \code{listName}
125+
#' and/or \code{regexName} filter is applied. Allowable options include:
126+
#' \itemize{
127+
#' \item For Spectronaut: "PG.Genes", "PG.ProteinAccession", "PG.ProteinDescriptions", and
128+
#' "PG.ProteinName".
129+
#' \item For Scaffold: "ProteinDescriptions", "AccessionNumber", and "AlternateID".
130+
#' }
131+
#'
122132
#' @param removeList A boolean (default = TRUE) specifying whether the list of proteins
123133
#' should be removed or selected.
124134
#' \itemize{
@@ -130,8 +140,8 @@ preProcessFiltering <- function(dataSet,
130140
#' current working directory. This option only works when \code{removeList = TRUE}.
131141
#'
132142
#' @details
133-
#' If both \code{listName} and \code{regexName} are provided, the protein names selected
134-
#' or removed will be the union of those specified in \code{listName} and those matching
143+
#' If both \code{listName} and \code{regexName} are provided, the proteins selected or
144+
#' removed will be the union of those specified in \code{listName} and those matching
135145
#' the regex pattern in \code{regexName}.
136146
#'
137147
#' @return
@@ -142,29 +152,39 @@ preProcessFiltering <- function(dataSet,
142152
filterOutIn <- function(dataSet,
143153
listName = c(),
144154
regexName = c(),
155+
by = NULL,
145156
removeList = TRUE,
146157
saveRm = TRUE) {
147158

148159
## relabel the data frame
149160
filteredData <- dataSet %>%
150161
select(-c(R.Condition, R.Replicate))
151162

163+
information <- read.csv("preprocess_protein_information.csv", check.names = FALSE)
164+
scaffoldCheck <- any(colnames(information) == "Visible?")
165+
IDcol <- ifelse(scaffoldCheck, "AccessionNumber", "PG.ProteinName")
166+
167+
if (is.null(by)) {
168+
by <- IDcol
169+
}
170+
152171
## only list filter if listName is present
153172
if (length(listName) != 0) {
154-
listIndex <- which(colnames(filteredData) %in% listName)
173+
listIndex <- which(information[[by]] %in% listName)
155174
} else {
156175
listIndex <- NULL
157176
}
158177

159178
## only regex filter if regexName is present
160179
if (length(regexName) != 0) {
161-
regexIndex <- grep(paste(regexName, collapse = "|"), colnames(filteredData))
180+
regexIndex <- grep(paste(regexName, collapse = "|"), information[[by]])
162181
} else {
163182
regexIndex <- NULL
164183
}
165184

166185
## combine protein names from list and regex filters
167-
unionName <- colnames(filteredData)[sort(union(listIndex, regexIndex))]
186+
unionIndex <- sort(union(listIndex, regexIndex))
187+
unionName <- information[unionIndex, IDcol]
168188

169189
## create a dataframe of the data of proteins
170190
unionData <- dataSet %>%
@@ -173,10 +193,13 @@ filterOutIn <- function(dataSet,
173193
## if contaminants are being removed
174194
if (removeList == TRUE) {
175195

196+
## save removed data to current working directory
176197
if (saveRm) {
177-
178-
## save removed data to current working directory
179-
write.csv(unionData, file = "filtered_out_data.csv", row.names = FALSE)
198+
unionData_long <- unionData %>%
199+
pivot_longer(-c("R.Condition", "R.Replicate"), names_to = IDcol, values_to = "PG.Quantity") %>%
200+
left_join(information, by = IDcol) %>%
201+
arrange(match(.data[[IDcol]], unionName), R.Condition)
202+
write.xlsx(list(unionData, unionData_long), file = "filterOutIn.xlsx")
180203
}
181204

182205
## remove all of the contaminants if they are present
@@ -189,109 +212,7 @@ filterOutIn <- function(dataSet,
189212
filteredData <- unionData
190213
}
191214

192-
## return the filtered data
193-
return(filteredData)
194-
}
195-
196-
197-
##----------------------------------------------------------------------------------------
198-
#'
199-
#' Filter proteins by gene, accession or description
200-
#'
201-
#' @description
202-
#' Filter the preprocessed dataset by gene, accession, or description.
203-
#'
204-
#' @param dataSet The 2d data set of experimental values.
205-
#'
206-
#' @param proteinInformation The name of the .csv file containing protein information data
207-
#' (including the path to the file, if needed). The file should include the following
208-
#' columns:
209-
#' \itemize{
210-
#' \item For Spectronaut: "PG.Genes", "PG.ProteinAccessions", "PG.ProteinDescriptions",
211-
#' and "PG.ProteinNames".
212-
#' \item For Scaffold: "ProteinDescriptions", "AccessionNumber", and "AlternateID".
213-
#' }
214-
#' This file is automatically generated by the function
215-
#' \code{\link[msDiaLogue]{preprocessing}} or
216-
#' \code{\link[msDiaLogue]{preprocessing_scaffold}}.
217-
#'
218-
#' @param text A character vector of text used as the key for selecting or removing.
219-
#'
220-
#' @param by A character string specifying the information to which the \code{text} filter
221-
#' is applied, with allowable options:
222-
#' \itemize{
223-
#' \item For Spectronaut: "PG.Genes", "PG.ProteinAccessions", "PG.ProteinDescriptions",
224-
#' and "PG.ProteinNames".
225-
#' \item For Scaffold: "ProteinDescriptions", "AccessionNumber", and "AlternateID".
226-
#' }
227-
#'
228-
#' @param removeList A boolean (default = TRUE) specifying whether the list of proteins
229-
#' should be removed or selected.
230-
#' \itemize{
231-
#' \item TRUE: Remove the list of proteins from the data set.
232-
#' \item FALSE: Remove all proteins not in the list from the data set.
233-
#' }
234-
#'
235-
#' @param saveRm A boolean (default = TRUE) specifying whether to save removed data to
236-
#' current working directory. This option only works when \code{removeList = TRUE}.
237-
#'
238-
#' @details
239-
#' The function is an extension of the function \code{\link[msDiaLogue]{preprocessing}} or
240-
#' \code{\link[msDiaLogue]{preprocessing_scaffold}} that allows for filtering proteins
241-
#' based on additional information.
242-
#'
243-
#' @return
244-
#' A filtered 2d dataframe.
245-
#'
246-
#' @export
247-
248-
filterProtein <- function(dataSet,
249-
proteinInformation = "preprocess_protein_information.csv",
250-
text = c(),
251-
by = c("PG.Genes", "PG.ProteinAccessions",
252-
"PG.ProteinDescriptions", "PG.ProteinNames",
253-
"ProteinDescriptions", "AccessionNumber", "AlternateID"),
254-
removeList = TRUE,
255-
saveRm = TRUE) {
256-
257-
proteinInformation <- read.csv(proteinInformation)
258-
259-
## relabel the data frame
260-
filteredData <- dataSet %>%
261-
select(-c(R.Condition, R.Replicate))
262-
263-
index <- grep(paste(text, collapse = "|"), proteinInformation[[by]])
264-
265-
if (by %in% c("PG.Genes", "PG.ProteinAccessions",
266-
"PG.ProteinDescriptions", "PG.ProteinNames")) {
267-
proteinName <- proteinInformation[index,]$PG.ProteinNames
268-
} else {
269-
proteinName <- proteinInformation[index,]$AccessionNumber
270-
}
271-
272-
result <- dataSet %>%
273-
select(any_of(c("R.Condition", "R.Replicate", proteinName)))
274-
275-
## if contaminants are being removed
276-
if (removeList == TRUE) {
277-
278-
if (saveRm) {
279-
280-
## save removed data to current working directory
281-
write.csv(result, file = "filtered_protein_data.csv", row.names = FALSE)
282-
}
283-
284-
## remove all of the contaminants if they are present
285-
filteredData <- dataSet %>% select(-any_of(proteinName))
286-
287-
## if certain proteins are being selected
288-
} else if (removeList == FALSE) {
289-
290-
## select only proteins of interest
291-
filteredData <- result
292-
}
293-
294-
## return the filtered data
215+
## return the filtered data
295216
return(filteredData)
296217
}
297218

@@ -300,7 +221,7 @@ filterProtein <- function(dataSet,
300221
#'
301222
#' Filtering NA's post-imputation
302223
#'
303-
#' @description
224+
#' @description
304225
#' Remove proteins with NA values.
305226
#'
306227
#' @param dataSet The 2d data set of experimental values.
@@ -319,20 +240,30 @@ filterProtein <- function(dataSet,
319240

320241
filterNA <- function(dataSet, saveRm = TRUE) {
321242

243+
information <- read.csv("preprocess_protein_information.csv", check.names = FALSE)
244+
322245
if (saveRm) {
323246

324247
## create a dataframe of the removed data
325248
removedData <- bind_cols(select(dataSet, c(R.Condition, R.Replicate)),
326249
select_if(dataSet, ~any(is.na(.))))
327250

251+
scaffoldCheck <- any(colnames(information) == "Visible?")
252+
IDcol <- ifelse(scaffoldCheck, "AccessionNumber", "PG.ProteinName")
253+
328254
## save removed data to current working directory
329-
write.csv(removedData, file = "filtered_NA_data.csv", row.names = FALSE)
255+
removedData_long <- removedData %>%
256+
pivot_longer(-c("R.Condition", "R.Replicate"), names_to = IDcol, values_to = "PG.Quantity") %>%
257+
left_join(information, by = IDcol)
258+
259+
write.xlsx(list(removedData, removedData_long), file = "filterNA.xlsx")
260+
330261
}
331262

332263
## remove all of the contaminants if they are present
333264
filteredData <- dataSet %>% select_if(~!any(is.na(.)))
334265

335-
## return the filtered data
266+
## return the filtered data
336267
return(filteredData)
337268
}
338269

R/summarize.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,17 @@ summarize <- function(dataSet, saveSumm = TRUE) {
7474

7575
## combine summaries into a single data frame
7676
proteinSummary <- do.call(rbind, lapply(conditionsList, function(k) {
77-
data.frame(t(proteinSummary[[k]])) %>%
77+
data.frame(t(proteinSummary[[k]]), check.names = FALSE) %>%
7878
select(-"R.Condition") %>%
7979
rownames_to_column("Stat") %>%
8080
filter(Stat != "vars") %>%
81-
mutate(Condition = k, .before = "Stat")
81+
mutate(R.Condition = k, .before = "Stat")
8282
}))
8383

8484
if (saveSumm) {
8585

8686
## save file to current working directory
87-
write.csv(proteinSummary, file = "summarize_data.csv", row.names = FALSE)
87+
write.csv(proteinSummary, file = "summarize.csv", row.names = FALSE)
8888
}
8989

9090
## return protein data summary

0 commit comments

Comments
 (0)