Skip to content

Commit

Permalink
Add set operation for MAFs
Browse files Browse the repository at this point in the history
  • Loading branch information
ShixiangWang committed Apr 19, 2020
1 parent c915558 commit a062180
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
114 changes: 114 additions & 0 deletions R/setMaf.R
Original file line number Diff line number Diff line change
@@ -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))
}

}
35 changes: 35 additions & 0 deletions man/setMaf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a062180

Please sign in to comment.