At the chunk to "### Filter the data to samples with at least r params$min_read_count"
filter_low_count_samples <- function(df) {
# calc read count sums for sample columns
sample_sums <- df %>%
select(starts_with("sample.")) %>% ###MT here: my sample names begin with "sample_marker.", need to substitute this for "sample."
summarise(across(everything(), \(x) sum(x, na.rm = TRUE)))
# get names of columns to keep (those with sums >= `min_read_count`)
columns_to_keep <- sample_sums %>%
pivot_longer(everything(), names_to = "column", values_to = "sum") %>%
filter(sum >= params$min_read_count) %>%
pull(column)
# get all non-sample columns to keep
non_sample_cols <- names(df)[!grepl("^sample\\.", names(df))]. ###MT here: my sample names being with "sample_marker", need to substitute this for "sample"
# return the filtered df with only samples passing `min_read_count`
df %>% select(all_of(non_sample_cols), any_of(columns_to_keep))
}
# apply the filtering function
global_keep <- filter_low_count_samples(global_keep)
global_keep$scientific_name <- make.unique(as.character(global_keep$scientific_name))
keep_rows <- nrow(global_keep)
print(paste0("Number of rows kept: ", keep_rows))
We should add a sentence above the chunk or within the lines of code to make this more obvious to new users. Something like:
"# May return an error message if sample names aren't correct. Check global_keep for sample names. Ex. if samples named sample_marker.XYZ001, enter "sample_marker." where code needs sample column header names.
Note that this applies again in the chunk: "## Make a table to represent the counts of each OTU in each sample":
OTU_table <- global_keep[,grep("sample_marker\\.", colnames(global_keep))] #MT here edit "sample\\." to "sample_marker\\."
names(OTU_table) <- gsub(pattern = "sample_marker.", replacement = "", x = names(OTU_table)) #MT here edit "sample." to "sample_marker."
rownames(OTU_table) <- sub("^", "P", rownames(OTU_table))
And again in "## Make a taxonomy table"
tax_table <- global_keep[,-grep("sample_marker\\.", colnames(global_keep))] #MT changed "sample\\." to "sample_marker\\."
tax_table <- relocate(tax_table, id, definition, {{ best_id_col }}, {{ best_match_col }}, count, .after = last_col())
tax_table2 <- as.matrix(tax_table)
rownames(tax_table2) <- sub("^", "P", rownames(tax_table2))
At the chunk to "### Filter the data to samples with at least
r params$min_read_count"We should add a sentence above the chunk or within the lines of code to make this more obvious to new users. Something like:
"# May return an error message if sample names aren't correct. Check global_keep for sample names. Ex. if samples named sample_marker.XYZ001, enter "sample_marker." where code needs sample column header names.
Note that this applies again in the chunk: "## Make a table to represent the counts of each OTU in each sample":
And again in "## Make a taxonomy table"