Skip to content

Commit

Permalink
Addresses comments
Browse files Browse the repository at this point in the history
  • Loading branch information
elijah-tai committed Mar 28, 2017
1 parent efc477b commit e2de46e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 30 deletions.
63 changes: 43 additions & 20 deletions R/importFilterHypermutators.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,44 @@ importFilterHypermutators <- function(rSNVFileIn = c(),
rCNA <- readRDS(file)
for (sample in colnames(rCNA)) {
totalSamples <- totalSamples + 1
# if key is in hash, increment CNA and nMUT
if (!is.null(hashTable$sample)) {
prevCNACount <- hashTable$sample$CNA
prevTotalCount <- hashTable$sample$total
assign(sample, list(CNA = prevCNACount + 1, SNV = 0, total = prevTotalCount + 1), envir = hashTable)
# if key is in hash, increment CNA and nMUT for every gene if there is an abberation
for (copyNumberValue in rCNA[[sample]]) {
if (copyNumberValue != 0) {
if (!is.null(hashTable[[sample]])) {
prevCNACount <- hashTable[[sample]]$CNA
prevTotalCount <- hashTable[[sample]]$total
assign(sample, list(CNA = prevCNACount + 1, SNV = 0, total = prevTotalCount + 1), envir = hashTable)
} else {
assign(sample, list(CNA = 1, SNV = 0, total = 1), envir = hashTable)
}
}
}
# question - does each gene-CNA count as one mutation? I'm guessing yes?
assign(sample, list(CNA = 1, SNV = 0, total = 1), envir = hashTable)
}
}

# for each file in rSNVFileIn
for (file in rSNVFileIn) {
# readRDS, increment counter for SNV and nMUT
rSNV <- readRDS(file)
for (sample in rSNV$Tumor_Sample_Barcode) {
for (i in 1:length(rSNV$Tumor_Sample_Barcode)) {
# need to substitute dashes with period for consistency in files
sample <- gsub("-", ".", sample)
sample <- gsub("-", ".", rSNV$Tumor_Sample_Barcode[i])
totalSamples <- totalSamples + 1

# Present a progress bar.
if (!silent) {
.pBar(i, length(rSNV$Tumor_Sample_Barcode))
}

# if key is in hash, increment SNV and nMUT
if (!is.null(hashTable$sample)) {
prevCNACount <- hashTable$sample$CNA
prevSNVCount <- hashTable$sample$SNV
prevTotalCount <- hashTable$sample$total
if (!is.null(hashTable[[sample]])) {
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)
} else {
assign(sample, list(CNA = 0, SNV = 1, total = 1), envir = hashTable)
}
assign(sample, list(CNA = 0, SNV = 1, total = 1), envir = hashTable)
}
}

Expand Down Expand Up @@ -137,17 +148,29 @@ importFilterHypermutators <- function(rSNVFileIn = c(),

# for each rCNAFileIn
for (file in rCNAFileIn) {
# for each sample, if hashTable$sample$total > xS, remove it
# update metadata (getUUID(object))
# save new CNAFile
}
rCNA <- readRDS(file)
# remove sample if sample in `removedSamples`
newRCNA <- rCNA[, !(names(rCNA) %in% removedSamples)]

# update metadata - having issues, getting `invalid first argument`
# getUUID(newRCNA)

# save new CNAFile with "filtered" prepended to filename
saveRDS(newRCNA, file = paste(dOut, "/filteredHypermutators_", basename(file), sep = ""))
}

# for each rSNVFileIn
for (file in rSNVFileIn) {
# for each sample (need to gsub("\.", "-", sample)), if hashTable$sample$total > xS, remove it
# update metadata (getUUID(object))
rSNV <- readRDS(file)

# for each sample (need to gsub("-", ".", sample)), if sample in `removedSamples` remove it
newRSNV <- rSNV[, !(gsub("-", ".", rSNV$Tumor_Sample_Barcode) %in% removedSamples)]

# update metadata - having issues, getting `invalid first argument`
# getUUID(newRSNV)

# save new SNVFile
saveRDS(newRSNV, file = paste(dOut, "/filteredHypermutators_", basename(file), sep = ""))
}

## LOGGING ###############
Expand Down
41 changes: 31 additions & 10 deletions tests/testthat/testFilter.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ OLOG <- as.character(getOption("rete.logfile")) # save original logfile name
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'

filteredSNVfileName <- paste(getwd(), "/filteredHypermutators_", basename(SNVfileName), sep = "")
filteredCNAfileName <- paste(getwd(), "/filteredHypermutators_", basename(CNAfileName), sep = "")
# ==== END SETUP AND PREPARE ===================================================


Expand All @@ -16,25 +22,28 @@ if (file.exists(logName)) { file.remove(logName)}
# set up a tempdir and tempfiles for outputs of importFilterHypermutators
test_that("importFilterHypermutators works correctly on valid input", {
# run importFilterHypermutators on valid input
expect_error(importFilterHypermutators(rSNVFileIn = c('../../inst/extdata/devSNV.rds'),
rCNAFileIn = c('../../inst/extdata/devCNA.rds'),
expect_error(importFilterHypermutators(rSNVFileIn = c(SNVfileName),
rCNAFileIn = c(CNAfileName),
dOut = getwd(),
xS = 400,
silent = FALSE,
writeLog = TRUE), NA)

if (file.exists(filteredSNVfileName)) { file.remove(filteredSNVfileName)}
if (file.exists(filteredCNAfileName)) { file.remove(filteredCNAfileName)}
})

test_that("importFilterHypermutators rejects invalid dOut arguments", {
# check for NULL object
expect_error(importFilterHypermutators(rSNVFileIn = c('../../inst/extdata/devSNV.rds'),
rCNAFileIn = c('../../inst/extdata/devCNA.rds'),
expect_error(importFilterHypermutators(rSNVFileIn = c(SNVfileName),
rCNAFileIn = c(CNAfileName),
dOut = NULL,
xS = 400,
silent = FALSE,
writeLog = TRUE))
# check for paths that don't exist
expect_error(importFilterHypermutators(rSNVFileIn = c('../../inst/extdata/devSNV.rds'),
rCNAFileIn = c('../../inst/extdata/devCNA.rds'),
expect_error(importFilterHypermutators(rSNVFileIn = c(SNVfileName),
rCNAFileIn = c(CNAfileName),
dOut = 'no/such/path',
xS = 400,
silent = FALSE,
Expand All @@ -43,29 +52,41 @@ test_that("importFilterHypermutators rejects invalid dOut arguments", {

test_that("importFilterHypermutators rejects invalid xS arguments", {
# check for including NULL objects
expect_error(importFilterHypermutators(rSNVFileIn = c('../../inst/extdata/devSNV.rds'),
rCNAFileIn = c('../../inst/extdata/devCNA.rds'),
expect_error(importFilterHypermutators(rSNVFileIn = c(SNVfileName),
rCNAFileIn = c(CNAfileName),
xS = NULL,
silent = FALSE,
writeLog = TRUE))
# check character
expect_error(importFilterHypermutators(rSNVFileIn = c('../../inst/extdata/devSNV.rds'),
rCNAFileIn = c('../../inst/extdata/devCNA.rds'),
expect_error(importFilterHypermutators(rSNVFileIn = c(SNVfileName),
rCNAFileIn = c(CNAfileName),
xS = "400",
silent = FALSE,
writeLog = TRUE))
})

test_that("importFilterHypermutators correctly removes hypermutators", {
# run importFilterHypermutators with a single hypermutator
testCNA <- readRDS('../../inst/extdata/devCNA.rds')
testSNV <- readRDS('../../inst/extdata/devSNV.rds')


# check output file
readRDS('')

# test cleanup
if (file.exists(filteredSNVfileName)) { file.remove(filteredSNVfileName)}
if (file.exists(filteredCNAfileName)) { file.remove(filteredCNAfileName)}
})

# ==== BEGIN TEARDOWN AND RESTORE ==============================================
logName <- unlist(getOption("rete.logfile"))
if (file.exists(logName)) { file.remove(logName)}
options("rete.logfile" = OLOG)

# another check for function artifacts
if (file.exists(filteredSNVfileName)) { file.remove(filteredSNVfileName)}
if (file.exists(filteredCNAfileName)) { file.remove(filteredCNAfileName)}
# ==== END TEARDOWN AND RESTORE ===============================================

# [END]

0 comments on commit e2de46e

Please sign in to comment.