Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
mmRmeta/R/subset.metadata.R
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
30 lines (28 sloc)
1.64 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Filter Metadata for Primary Cancer Site (Organ) | |
#' @description This function subsets the metadata for an organ. It works for meta data directly downloaded from TCGA or already filtered meta data. The subset is done by matching the "key" with the column names (patient ID or case_id) of the expression matrix. | |
#' @param meta_data Meta data provided by TCGA. | |
#' @param cancer_data A large list created prior by multimodalR for a cancer site. | |
#' @param key character - Unique identifier, default = "case_id" | |
#' @param additional_key needs to be added | |
#' @param additional_value needs to be added | |
#' @importFrom magrittr %>% | |
#' @examples | |
#' lungMetaData <- subset.metadata(metadata, lungXY) | |
#' @return Returns a data table of the metadata for a primary cancer site (organ). | |
subset.metadata <- function(meta_data, cancer_data, key = "case_id", additional_key = NULL, additional_value = NULL) { | |
if(!is.character(meta_data[[key]])) { | |
meta_data[[key]] <- as.character(meta_data[[key]]) #transform factor to character | |
} | |
correctRows <- numeric() #empty vector to be filled later | |
caseNames <- names(cancer_data$Expressionmatrix) #vector of case_id from expressionmatrix | |
for (cases in caseNames) { #match case_id of the vector with case_id of metadata | |
matchedRow <- grep(cases, meta_data[[key]]) | |
correctRows <- c(correctRows, matchedRow) | |
} | |
meta_data <- meta_data[c(correctRows),] #subset by machted case_id | |
if(!is.null(additional_key)) { | |
additional_key <- rlang::sym(additional_key) | |
meta_data <- meta_data %>% filter_(., !!(additional_key) == additional_value) | |
} | |
return(meta_data) | |
} |