From dede433392af34347ff4322bdb47263934273eb1 Mon Sep 17 00:00:00 2001 From: Won June Tai Date: Thu, 30 Mar 2017 19:01:29 -0400 Subject: [PATCH] Ready for review --- R/importFilterHypermutators.R | 18 ++++++++------ man/importFilterHypermutators.Rd | 15 +++++++---- {inst/extdata => tests/testthat}/devCNA.rds | Bin {inst/extdata => tests/testthat}/devSNV.rds | Bin tests/testthat/testFilter.R | 26 ++++++++++++++++---- 5 files changed, 41 insertions(+), 18 deletions(-) rename {inst/extdata => tests/testthat}/devCNA.rds (100%) rename {inst/extdata => tests/testthat}/devSNV.rds (100%) diff --git a/R/importFilterHypermutators.R b/R/importFilterHypermutators.R index e961e66..30af486 100644 --- a/R/importFilterHypermutators.R +++ b/R/importFilterHypermutators.R @@ -66,10 +66,11 @@ importFilterHypermutators <- function(rSNVFileIn = c(), for (file in rCNAFileIn) { # readRDS, count gene CNAs and add to hash for sample @ CNA and nMUT rCNA <- readRDS(file) - for (sample in colnames(rCNA)) { + for (sample in colnames(rCNA)[4:length(colnames(rCNA))]) { totalSamples <- totalSamples + 1 # if key is in hash, increment CNA and nMUT for every gene if there is an abberation for (copyNumberValue in rCNA[[sample]]) { + copyNumberValue <- as.double(copyNumberValue) if (copyNumberValue != 0) { if (!is.null(hashTable[[sample]])) { prevCNACount <- hashTable[[sample]]$CNA @@ -102,7 +103,7 @@ importFilterHypermutators <- function(rSNVFileIn = c(), prevCNACount <- hashTable[[sample]]$CNA prevSNVCount <- hashTable[[sample]]$SNV prevTotalCount <- hashTable[[sample]]$total - assign(sample, list(CNA = prevCNACount, SNV = prevSNVCount + 1, nMUT = prevTotalCount + 1), envir = hashTable) + assign(sample, list(CNA = prevCNACount, SNV = prevSNVCount + 1, total = prevTotalCount + 1), envir = hashTable) } else { assign(sample, list(CNA = 0, SNV = 1, total = 1), envir = hashTable) } @@ -122,24 +123,24 @@ importFilterHypermutators <- function(rSNVFileIn = c(), for (sample in ls(hashTable)) { # num samples with only CNA - if (hashTable[[sample]]$SNV == 0) { + if (hashTable[[sample]]$SNV == 0 && hashTable[[sample]]$CNA > 0) { numSamplesOnlyCNA <- numSamplesOnlyCNA + 1 } # num samples with only SNV - else if (hashTable[[sample]]$CNA == 0) { + if (hashTable[[sample]]$CNA == 0 && hashTable[[sample]]$SNV > 0) { numSamplesOnlySNV <- numSamplesOnlySNV + 1 } # num samples with both SNV and CNA - else if (hashTable[[sample]]$CNA > 0 && hashTable[[sample]]$SNV > 0) { + if (hashTable[[sample]]$CNA > 0 && hashTable[[sample]]$SNV > 0) { numSamplesBothSNVAndCNA <- numSamplesBothSNVAndCNA + 1 } - # num samples that exceeded threshold and need removal - else if (hashTable[[sample]]$total > xS) { + # num samples that exceeded threshold and need to be removed + if (hashTable[[sample]]$total > xS) { numRemovedSamples <- numRemovedSamples + 1 removedSamples <- c(removedSamples, sample) } # num samples with no change - else { + if (hashTable[[sample]]$total <= xS) { numSamplesNoChange <- numSamplesNoChange + 1 } } @@ -149,6 +150,7 @@ importFilterHypermutators <- function(rSNVFileIn = c(), # for each rCNAFileIn for (file in rCNAFileIn) { rCNA <- readRDS(file) + # remove sample if sample in `removedSamples` newRCNA <- rCNA[, !(names(rCNA) %in% removedSamples)] diff --git a/man/importFilterHypermutators.Rd b/man/importFilterHypermutators.Rd index 87e7771..29e3a05 100644 --- a/man/importFilterHypermutators.Rd +++ b/man/importFilterHypermutators.Rd @@ -4,11 +4,14 @@ \alias{importFilterHypermutators} \title{Filters out hypermutators} \usage{ -importFilterHypermutators(fNames = c(), dOut = getwd(), xS = 400, - silent = FALSE, writeLog = TRUE) +importFilterHypermutators(rSNVFileIn = c(), rCNAFileIn = c(), + dOut = getwd(), xS = 400, silent = FALSE, writeLog = TRUE, + writeDetailedLog = TRUE) } \arguments{ -\item{fNames}{A vector of local file names of rMUT and/or rCNA files} +\item{rSNVFileIn}{A vector of local file names of rSNV} + +\item{rCNAFileIn}{A vector of local file names of rCNA} \item{dOut}{Directory to store output, defaults to getwd().} @@ -19,15 +22,17 @@ by default.} \item{writeLog}{Controls whether writing the result to the global logfile is enabled. TRUE by default.} + +\item{writeDetailedLog}{Flag for extra details about log. TRUE by default.} } \description{ \code{importFilterHypermutators} identifies the number of SNV and CNA -mutations for each sample and removes the samples from rMUT and rCNA +mutations for each sample and removes the samples from rSNV and rCNA datasets. Default threshold for removal is 400 mutations per sample. } \examples{ \dontrun{ - importFilterHypermutators(fNames, dOut, xS) + importFilterHypermutators(rSNVFileIn, rCNAFileIn, dOut, xS) } } diff --git a/inst/extdata/devCNA.rds b/tests/testthat/devCNA.rds similarity index 100% rename from inst/extdata/devCNA.rds rename to tests/testthat/devCNA.rds diff --git a/inst/extdata/devSNV.rds b/tests/testthat/devSNV.rds similarity index 100% rename from inst/extdata/devSNV.rds rename to tests/testthat/devSNV.rds diff --git a/tests/testthat/testFilter.R b/tests/testthat/testFilter.R index 70c6829..da625dc 100644 --- a/tests/testthat/testFilter.R +++ b/tests/testthat/testFilter.R @@ -9,8 +9,8 @@ logFileName(fPath = tempdir(), setOption = TRUE) # make tempdir() the log dir logName <- unlist(getOption("rete.logfile")) if (file.exists(logName)) { file.remove(logName)} -SNVfileName <- '../../inst/extdata/devSNV.rds' -CNAfileName <- '../../inst/extdata/devCNA.rds' +SNVfileName <- 'devSNV.rds' +CNAfileName <- 'devCNA.rds' filteredSNVfileName <- paste(getwd(), "/filteredHypermutators_", basename(SNVfileName), sep = "") filteredCNAfileName <- paste(getwd(), "/filteredHypermutators_", basename(CNAfileName), sep = "") @@ -67,12 +67,28 @@ test_that("importFilterHypermutators rejects invalid xS arguments", { test_that("importFilterHypermutators correctly removes hypermutators", { # run importFilterHypermutators with a single hypermutator - testCNA <- readRDS('../../inst/extdata/devCNA.rds') - testSNV <- readRDS('../../inst/extdata/devSNV.rds') + testCNA <- readRDS(CNAfileName) + testSNV <- readRDS(SNVfileName) + # start off with a known number of samples in each CNA and SNV file + expect_equal(length(colnames(testCNA)[4:length(colnames(testCNA))]), 579) + expect_equal(length(testSNV$Tumor_Sample_Barcode), 15) + + expect_error(importFilterHypermutators(rSNVFileIn = c(SNVfileName), + rCNAFileIn = c(CNAfileName), + xS = 15, + silent = FALSE, + writeLog = TRUE), NA) # check output file - readRDS('') + processedCNA <- readRDS(filteredCNAfileName) + processedSNV <- readRDS(filteredSNVfileName) + + # expect that 458 samples are removed from rCNA (579 - 458 = 121) + expect_equal(length(colnames(processedCNA)[4:length(colnames(processedCNA))]), 121) + + # expect that no samples are removed from rSNV + expect_equal(length(processedSNV$Tumor_Sample_Barcode), 15) # test cleanup if (file.exists(filteredSNVfileName)) { file.remove(filteredSNVfileName)}