Skip to content

Commit

Permalink
draft constructors for Collections and MbioDataset
Browse files Browse the repository at this point in the history
  • Loading branch information
d-callan committed Feb 12, 2024
1 parent 0ca97cf commit 858855c
Showing 1 changed file with 42 additions and 15 deletions.
57 changes: 42 additions & 15 deletions R/constructors-MbioDataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,43 @@ setMethod("Collections", signature("character"), function(collections) {
}
})

sampleMetadataBuilder <- function(dataSource) {
dt <- getDataFromSource(dataSource)
dataColNames <- names(dt)
recordIdColumn <- findRecordIdColumn(dataColNames)
findAncestorIdColumns <- findAncestorIdColumns(dataColNames)

sampleMetadata <- new("SampleMetadata",
data=dt,
recordIdColumn = recordIdColumn,
findAncestorIdColumns = findAncestorIdColumns
)

return(sampleMetadata)
}

# TODO turn this into a proper S4 method of SampleMetadata and put it in a different package?
# this will not work for metadata across different branches of the tree. IDK if we have that case in mbio?
mergeSampleMetadata <- function(x, y) {
uniqueAncestorIdColumns <- unique(x@findAncestorIdColumns, y@findAncestorIdColumns)
recordIdColumn <- ifelse(x@recordIdColumn %in% uniqueAncestorIdColumns, y@recordIdColumn, x@recordIdColumn)
data <- merge(x@data, y@data, by = uniqueAncestorIdColumns, all = TRUE)

sampleMetadata <- new("SampleMetadata",
data = data,
recordIdColumn = recordIdColumn,
findAncestorIdColumns = uniqueAncestorIdColumns
)

return(sampleMetadata)
}

sampleMetadataFromDataSources <- function(dataSources) {
sampleMetataList <- lapply(dataSources, sampleMetadataBuilder)
sampleMetadata <- purrr::reduce(sampleMetataList, mergeSampleMetadata)

return(sampleMetadata)
}

#' Create a Microbiome Dataset
#'
Expand All @@ -205,7 +242,7 @@ setMethod("MbioDataset", signature("missing", "missing"), function(collections,

#' @export
setMethod("MbioDataset", signature("Collections", "data.frame"), function(collections, metadata) {
new("MbioDataset", collections = collections, metadata = SampleMetadata(metadata))
new("MbioDataset", collections = collections, metadata = sampleMetadataBuilder(metadata))
})

#' @export
Expand All @@ -215,40 +252,30 @@ setMethod("MbioDataset", signature("Collections", "missing"), function(collectio

#' @export
setMethod("MbioDataset", signature("Collection", "data.frame"), function(collections, metadata) {
new("MbioDataset", collections = Collections(collections), metadata = SampleMetadata(metadata))
})

#' @export
setMethod("MbioDataset", signature("Collection", "missing"), function(collections, metadata) {
new("MbioDataset", collections = Collections(collections))
new("MbioDataset", collections = Collections(collections), metadata = sampleMetadataBuilder(metadata))
})

#' @export
setMethod("MbioDataset", signature("list", "data.frame"), function(collections, metadata) {
# TODO make Collections from a list of files containing data
new("MbioDataset", collections = Collections(collections), metadata = SampleMetadata(metadata))
new("MbioDataset", collections = Collections(collections), metadata = sampleMetadataBuilder(metadata))
})

#' @export
setMethod("MbioDataset", signature("list", "missing"), function(collections, metadata) {
# TODO make Collections from a list of files containing data
new("MbioDataset", collections = Collections(collections))
})

#' @export
setMethod("MbioDataset", signature("list", "list"), function(collections, metadata) {
# TODO make Collections and SampleMetadata from a list of files containing data
new("MbioDataset", collections = Collections(collections), metadata = SampleMetadata(metadata))
new("MbioDataset", collections = Collections(collections), metadata = sampleMetadataFromDataSources(metadata))
})

#' @export
setMethod("MbioDataset", signature("data.frame", "missing"), function(collections, metadata) {
# TODO parse data frame into Collections
new("MbioDataset", collections = Collections(collections))
})

#' @export
setMethod("MbioDataset", signature("data.frame", "data.frame"), function(collections, metadata) {
# TODO parse data frame into Collections
new("MbioDataset", collections = Collections(collections), metadata = SampleMetadata(metadata))
new("MbioDataset", collections = Collections(collections), metadata = sampleMetadataBuilder(metadata))
})

0 comments on commit 858855c

Please sign in to comment.