@@ -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 )) {
0 commit comments