diff --git a/DESCRIPTION b/DESCRIPTION index f545c09..50d00a5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,7 +22,7 @@ Imports: wordcloud, grDevices, survival -RoxygenNote: 7.0.2 +RoxygenNote: 7.1.0 Encoding: UTF-8 Suggests: knitr, rmarkdown, BSgenome, Biostrings, NMF, mclust, berryFunctions VignetteBuilder: knitr diff --git a/R/setMaf.R b/R/setMaf.R new file mode 100644 index 0000000..64c6b2d --- /dev/null +++ b/R/setMaf.R @@ -0,0 +1,114 @@ +#' Set Operations for MAF objects +#' @param x the first `MAF` object. +#' @param y the second `MAF` object. +#' @param ... other parameters passing to `subsetMaf` for subsetting operations. +#' @name setMaf +#' @examples +#' laml.maf <- system.file("extdata", "tcga_laml.maf.gz", package = "maftools") +#' laml <- read.maf(maf = laml.maf) +#' x <- subsetMaf(maf = laml, tsb = c('TCGA-AB-3009')) +#' y <- subsetMaf(maf = laml, tsb = c('TCGA-AB-2933')) +#' setdiffMAF(x, y) +#' intersectMAF(x, y) +#' # This is similar to merge_mafs() +#' unionMAF(x, y) +NULL + + +#' @rdname setMaf +setdiffMAF <- function(x, y, mafObj = TRUE, ...) { + stopifnot(inherits(x, "MAF"), inherits(y, "MAF")) + + args = list(...) + if (length(args) == 0) { + maf_x = rbind(x@data, x@maf.silent) + maf_y = rbind(y@data, y@maf.silent) + } else { + maf_x = subsetMaf(x, mafObj = FALSE, ...) + maf_y = subsetMaf(y, mafObj = FALSE, ...) + } + + maf_x[, Label := paste(Chromosome, Start_Position, End_Position, Reference_Allele, Tumor_Seq_Allele2, Variant_Type)] + maf_y[, Label := paste(Chromosome, Start_Position, End_Position, Reference_Allele, Tumor_Seq_Allele2, Variant_Type)] + + set = setdiff(maf_x$Label, maf_y$Label) + + if (length(set) == 0) { + message("No variants left.") + return(NULL) + } + + maf_x = maf_x[Label %in% set] + maf_x$Label = NULL + + if (!mafObj) { + maf_x + } else { + return(read.maf(maf_x, clinicalData = x@clinical.data, verbose = FALSE)) + } +} + +#' @rdname setMaf +unionMAF <- function(x, y, mafObj = TRUE, ...) { + stopifnot(inherits(x, "MAF"), inherits(y, "MAF")) + + args = list(...) + + if (length(args) == 0) { + maf_x = rbind(x@data, x@maf.silent) + maf_y = rbind(y@data, y@maf.silent) + } else { + maf_x = subsetMaf(x, mafObj = FALSE, ...) + maf_y = subsetMaf(y, mafObj = FALSE, ...) + } + + maf = unique(rbind(maf_x, maf_y, fill = TRUE)) + + if (!mafObj) { + return(maf) + } else { + cli_merged = rbind(x@clinical.data, y@clinical.data) + return(read.maf(maf, clinicalData = cli_merged, verbose = FALSE)) + } + +} + + +#' @rdname setMaf +#' +intersectMAF <- function(x, y, mafObj = TRUE, ...) { + stopifnot(inherits(x, "MAF"), inherits(y, "MAF")) + + args = list(...) + if (length(args) == 0) { + maf_x = rbind(x@data, x@maf.silent) + maf_y = rbind(y@data, y@maf.silent) + } else { + maf_x = subsetMaf(x, mafObj = FALSE, ...) + maf_y = subsetMaf(y, mafObj = FALSE, ...) + } + + maf_x[, Label := paste(Chromosome, Start_Position, End_Position, Reference_Allele, Tumor_Seq_Allele2, Variant_Type)] + maf_y[, Label := paste(Chromosome, Start_Position, End_Position, Reference_Allele, Tumor_Seq_Allele2, Variant_Type)] + + set = intersect(maf_x$Label, maf_y$Label) + if (length(set) == 0) { + message("No common variants found.") + return(NULL) + } + + maf_x = maf_x[Label %in% set] + maf_x$Label = NULL + maf_y = maf_y[Label %in% set] + maf_y$Label = NULL + + + maf = rbind(maf_x, maf_y, fill = TRUE) + if (!mafObj) { + return(maf) + } else { + cli_merged = rbind(x@clinical.data, y@clinical.data) + return(read.maf(maf, clinicalData = cli_merged, verbose = FALSE)) + } + +} diff --git a/man/setMaf.Rd b/man/setMaf.Rd new file mode 100644 index 0000000..071581e --- /dev/null +++ b/man/setMaf.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/setMaf.R +\name{setMaf} +\alias{setMaf} +\alias{setdiffMAF} +\alias{unionMAF} +\alias{intersectMAF} +\title{Set Operations for MAF objects} +\usage{ +setdiffMAF(x, y, mafObj = TRUE, ...) + +unionMAF(x, y, mafObj = TRUE, ...) + +intersectMAF(x, y, mafObj = TRUE, ...) +} +\arguments{ +\item{x}{the first `MAF` object.} + +\item{y}{the second `MAF` object.} + +\item{...}{other parameters passing to `subsetMaf` for subsetting operations.} +} +\description{ +Set Operations for MAF objects +} +\examples{ +laml.maf <- system.file("extdata", "tcga_laml.maf.gz", package = "maftools") +laml <- read.maf(maf = laml.maf) +x <- subsetMaf(maf = laml, tsb = c('TCGA-AB-3009')) +y <- subsetMaf(maf = laml, tsb = c('TCGA-AB-2933')) +setdiffMAF(x, y) +intersectMAF(x, y) +# This is similar to merge_mafs() +unionMAF(x, y) +}