@@ -93,44 +93,34 @@ mStat_normalize_data <-
9393 # Different normalization methods are applied based on the selected method
9494 # Each method addresses different aspects of microbiome data variability
9595
96- if (method == " Rarefy-TSS" ) {
97- # Rarefaction followed by Total Sum Scaling
98- # This method first standardizes sampling depth, then converts to relative abundance
96+ if (method %in% c(" Rarefy-TSS" , " Rarefy" )) {
97+ # Rarefaction-based normalization methods
98+ # - Rarefy: Standardizes sampling depth across all samples
99+ # - Rarefy-TSS: Rarefaction followed by Total Sum Scaling (converts to relative abundance)
100+
101+ # Validate and determine rarefaction depth
102+ min_depth <- min(colSums(otu_tab ))
103+
99104 if (is.null(depth )) {
100- depth <- min(colSums( otu_tab ))
101- } else if (depth > min(colSums( otu_tab )) ) {
105+ depth <- min_depth
106+ } else if (depth > min_depth ) {
102107 stop(" Depth is greater than the smallest total count across samples." )
103108 }
104- rarefy_depth <- depth
105109
106110 # Check if data is already in relative abundance format
107- if (all(round(colSums(otu_tab ),5 ) == 1 )){
111+ if (all(round(colSums(otu_tab ), 5 ) == 1 )) {
108112 rarefied_otu_tab <- as.matrix(otu_tab )
109113 } else {
110114 # Perform rarefaction using vegan package
111- rarefied_otu_tab <- t(vegan :: rrarefy(t(otu_tab ), sample = rarefy_depth ))
112- }
113- # Convert to relative abundance
114- rarefied_otu_tab <- rarefied_otu_tab / rarefy_depth
115- scale_factor <- rarefy_depth
116- } else if (method == " Rarefy" ) {
117- # Rarefaction only
118- # This method standardizes sampling depth across all samples
119- if (is.null(depth )) {
120- depth <- min(colSums(otu_tab ))
121- } else if (depth > min(colSums(otu_tab ))) {
122- stop(" Depth is greater than the smallest total count across samples." )
115+ rarefied_otu_tab <- t(vegan :: rrarefy(t(otu_tab ), sample = depth ))
123116 }
124- rarefy_depth <- depth
125-
126- # Check if data is already in relative abundance format
127- if (all(round(colSums(otu_tab ),5 ) == 1 )){
128- rarefied_otu_tab <- as.matrix(otu_tab )
129- } else {
130- # Perform rarefaction using vegan package
131- rarefied_otu_tab <- t(vegan :: rrarefy(t(otu_tab ), sample = rarefy_depth ))
117+
118+ # Apply TSS normalization (convert to relative abundance) for Rarefy-TSS only
119+ if (method == " Rarefy-TSS" ) {
120+ rarefied_otu_tab <- rarefied_otu_tab / depth
132121 }
133- scale_factor <- rarefy_depth
122+
123+ scale_factor <- depth
134124 } else if (method == " TSS" ) {
135125 # Total Sum Scaling
136126 # This method converts counts to relative abundances
0 commit comments