Skip to content

Commit

Permalink
sync records in sampleMetadata with those in df after possible removal
Browse files Browse the repository at this point in the history
  • Loading branch information
asizemore committed Sep 12, 2024
1 parent 905e3f1 commit bbeb36e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
4 changes: 3 additions & 1 deletion R/methods-CollectionWithMetadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ setGeneric("removeIncompleteRecords",
#' @aliases removeIncompleteRecords,CollectionWithMetadata-method
setMethod("removeIncompleteRecords", signature("CollectionWithMetadata"), function(object, colName = character(), verbose = c(TRUE, FALSE)) {
verbose <- veupathUtils::matchArg(verbose)

df <- getCollectionData(object, verbose = verbose)
sampleMetadata <- getSampleMetadata(object)
# df may have had rows removed due to getCollectionData behavior. Subset sampleMetadata to match
sampleMetadata <- sampleMetadata[sampleMetadata[[object@sampleMetadata@recordIdColumn]] %in% df[[object@recordIdColumn]], ]

# Remove Records with NA from data and metadata
if (any(is.na(sampleMetadata[[colName]]))) {
Expand All @@ -171,6 +172,7 @@ setMethod("removeIncompleteRecords", signature("CollectionWithMetadata"), functi
data = sampleMetadata,
recordIdColumn = object@sampleMetadata@recordIdColumn
)

validObject(object)
}

Expand Down
40 changes: 40 additions & 0 deletions tests/testthat/test-class-CollectionWithMetadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,44 @@ test_that("pruneFeatures works", {
testing <- pruneFeatures(testing, veupathUtils::predicateFactory('proportionNonZero', 0.5))
expect_equal(nrow(testing@data), 200)
expect_equal(ncol(testing@data), 3)
})

test_that("removeIncompleteRecords removes records from both metadata and data", {
nSamples <- 200
sampleMetadataDT <- data.table::data.table(
"entity.SampleID" = 1:nSamples,
"entity.contA" = rnorm(nSamples),
"entity.contB" = rnorm(nSamples),
"entity.contC" = rnorm(nSamples)
)
# set first 5 rows to NA
sampleMetadataDT[1:5, 2:ncol(sampleMetadataDT) ] <- NA

df <- data.table::data.table(
"entity.SampleID" = sampleMetadataDT$entity.SampleID,
"entity.cont1" = rnorm(nSamples),
"entity.cont2" = rnorm(nSamples),
"entity.cont3" = rnorm(nSamples)
)
# Set second 5 rows to NA
df[6:10, 2:ncol(df) ] <- NA

sampleMetadata <- SampleMetadata(
data = sampleMetadataDT,
recordIdColumn = 'entity.SampleID'
)

test_collection <- CollectionWithMetadata(
name = 'test',
data = df,
sampleMetadata = sampleMetadata,
recordIdColumn = 'entity.SampleID'
)

result <- removeIncompleteRecords(test_collection, 'entity.contA', verbose=F)
expect_equal(nrow(result@data), nSamples-10)
expect_equal(ncol(result@data), 4)
expect_equal(nrow(result@sampleMetadata@data), nSamples-10)
expect_equal(ncol(result@sampleMetadata@data), 4)

})

0 comments on commit bbeb36e

Please sign in to comment.