Skip to content

Commit 2e8206f

Browse files
committed
improved eature_merge and added test #40
1 parent 49064be commit 2e8206f

2 files changed

Lines changed: 45 additions & 26 deletions

File tree

R/omics-class.R

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -546,42 +546,41 @@ omics <- R6::R6Class(
546546

547547
## MAIN
548548
#--------------------------------------------------------------------#
549-
# creates a subset of unique feature rank, hashes combined for each unique rank
550-
counts <- data.table::data.table()
551-
counts[, (private$.feature_id) := rownames(private$.countData)]
552-
553-
# Supports multiple features
554-
features <- data.table::copy(private$.featureData[private$.featureData[[ feature_rank[1] ]] != "", ])
549+
# Create temporary copies
550+
counts <- private$.countData
551+
features <- data.table::copy(private$.featureData[!is.na(private$.featureData[[ feature_rank[1] ]])])
555552

556553
# set keys
557-
data.table::setkey(counts, FEATURE_ID)
558554
data.table::setkey(features, FEATURE_ID)
559555

560556
# Create groups by ID
561557
grouped_ids <- features[, .(IDs = list(FEATURE_ID)), by = feature_rank]
562-
counts_glom <- Matrix::Matrix(0,
563-
nrow = nrow(grouped_ids),
564-
ncol = ncol(private$.countData),
565-
dimnames = list(NULL, colnames(private$.countData)),
566-
sparse = TRUE)
567-
568-
# Populate sparse matrix by colsums of identical taxa
569-
for (i in 1:nrow(grouped_ids)) {
570-
ids <- grouped_ids$IDs[[i]]
571-
if (length(ids) == 1) {
572-
counts_glom[i, ] <- private$.countData[ids, ]
573-
} else {
574-
counts_glom[i, ] <- Matrix::colSums(private$.countData[ids, ])
575-
}
558+
559+
## Skip over singletons
560+
groups <- base::which(base::lengths(grouped_ids$IDs) > 1)
561+
grouped_ids$ID_first <- sapply(grouped_ids$IDs, `[[`, 1)
562+
563+
## Flatten sparseMatrix to only specific feature ids
564+
counts <- counts[grouped_ids$ID_first, ]
565+
566+
## Sum over multiples
567+
for (i in groups) {
568+
i_id <- grouped_ids$ID_first[i]
569+
i_group <- grouped_ids$IDs[[i]]
570+
counts[i_id, ] <- Matrix::colSums(private$.countData[i_group, ])
576571
}
577572

578573
# Prepare final self-components
579574
private$.featureData <- base::unique(features, by = feature_rank)
580-
# Fetch first ID from each list
581-
grouped_ids$ID_first <- sapply(grouped_ids$IDs, `[[`, 1)
582-
# Reorder by matching IDs
583-
private$.featureData <- private$.featureData[ base::order(base::match(private$.featureData[[ private$.feature_id ]], grouped_ids$ID_first)) ]
584-
private$.countData <- counts_glom
575+
576+
# Add new agglomerated files
577+
private$.featureData <- private$.featureData[
578+
base::order(base::match(
579+
x = private$.featureData[[ private$.feature_id ]],
580+
table = grouped_ids$ID_first
581+
))
582+
]
583+
private$.countData <- counts
585584

586585
# Replaces strings matching feature_filter with NAs
587586
if (!is.null(feature_filter)) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
test_that("Testing `feature_merge`", {
2+
taxa <- metagenomics$new(
3+
biomData = "input/metagenomics/biom_with_taxonomy_hdf5.biom",
4+
metaData = "input/metagenomics/metadata.tsv",
5+
treeData = "input/metagenomics/rooted_tree.newick"
6+
)
7+
features_original <- data.table::copy(taxa$featureData)
8+
counts_original <- taxa$countData
9+
10+
## Feature merge
11+
taxa$feature_merge(
12+
feature_rank = "Genus",
13+
feature_filter = c("uncultured")
14+
)
15+
16+
expect_equal(
17+
Matrix::colSums(counts_original[features_original[Genus == "Woesearchaeales"]$FEATURE_ID, ]),
18+
taxa$countData[taxa$featureData[Genus == "Woesearchaeales"]$FEATURE_ID, ]
19+
)
20+
})

0 commit comments

Comments
 (0)