bact_filt_out = function(genus){
bact <- top_hits[grep(genus, top_hits$Genus),]
bact = bact[which(bact$p_adjust < 0.05),]
bact = bact[order(bact$p_adjust),]
bact_to_pull = unique(bact$Contig_name)
asy = SummarizedExperiment::assay(se_q)
asy = as.matrix(asy[unname(sapply(bact_to_pull, function(x) which(x == rownames(asy)))),])
bact_long <- as.data.frame(asy) %>%
tibble::rownames_to_column("Contig_name") %>%
pivot_longer(-Contig_name, names_to = "run_accession", values_to = "ANI") %>%
left_join(meta %>% select(run_accession, subject, days), by = "run_accession") %>%
mutate(
Species = top_hits$Species[match(Contig_name, top_hits$Contig_name)],
Genus = top_hits$Genus[match(Contig_name, top_hits$Contig_name)]
)
bact_filtered <- bact_long %>%
group_by(subject, Contig_name) %>%
# Keep only those with at least one non-zero ANI for that subject
filter(any(ANI != 0)) %>%
ungroup() %>%
group_by(Contig_name, subject) %>%
mutate(
ANI_day0 = ANI[days == 0], # baseline for that contig+subject
ANI_relative = ANI - ANI_day0 # drop or gain relative to day0
) %>%
ungroup() %>%
filter(abs(ANI_relative)< 10 & abs(ANI_relative) > 1)
return(bact_filtered)
}
bact_filtered = bact_filt_out("Bacteroides")
contigs_subset <-names(sort(table(bact_filtered$Contig_name), decreasing = T))[c(9, 11, 26)]
ct_to_check = contigs_subset
bact_filtered = bact_filt_out("Alistipes")
contigs_subset <-names(sort(table(bact_filtered$Contig_name), decreasing = T))[c(3)]
ct_to_check = c(ct_to_check, contigs_subset)
bact_filtered = bact_filt_out("Veillonella")
contigs_subset <-names(sort(table(bact_filtered$Contig_name), decreasing = T))[c(8,2)]
ct_to_check = c(ct_to_check, contigs_subset)
bact_filtered = bact_filt_out("Prevotella")
contigs_subset <-names(sort(table(bact_filtered$Contig_name), decreasing = T))[c(2)]
ct_to_check = c(ct_to_check, contigs_subset)
bact <- top_hits
bact = bact[which(bact$p_adjust < 0.05),]
bact = bact[order(bact$p_adjust),]
bact_to_pull = unique(bact$Contig_name)
asy = SummarizedExperiment::assay(se_q)
asy = as.matrix(asy[unname(sapply(bact_to_pull, function(x) which(x == rownames(asy)))),])
bact_long <- as.data.frame(asy) %>%
tibble::rownames_to_column("Contig_name") %>%
pivot_longer(-Contig_name, names_to = "run_accession", values_to = "ANI") %>%
left_join(meta %>% select(run_accession, subject, days), by = "run_accession") %>%
mutate(
Species = top_hits$Species[match(Contig_name, top_hits$Contig_name)],
Genus = top_hits$Genus[match(Contig_name, top_hits$Contig_name)]
)
bact_filtered <- bact_long %>%
group_by(subject, Contig_name) %>%
# Keep only those with at least one non-zero ANI for that subject
filter(any(ANI != 0)) %>%
ungroup() %>%
group_by(Contig_name, subject) %>%
mutate(
ANI_day0 = ANI[days == 0], # baseline for that contig+subject
ANI_relative = ANI - ANI_day0 # drop or gain relative to day0
) %>%
ungroup() %>%
filter(abs(ANI_relative)< 10 & abs(ANI_relative) > 1)
ct_to_check_ = ct_to_check[c(1, 5, 4)]
# All hits with very small beta p-values indicate strain replacement, but we also track strain disappearances. Let's try to keep strains that persist
df_plot <- bact_long %>%
filter(Contig_name %in% ct_to_check_, ANI > 0)
df_plot$Contig_name = factor(df_plot$Contig_name, levels = ct_to_check_ )
for(i in 1:nrow(df_plot)){
df_plot$Species[i] = paste(df_plot$Species[i], str_extract(df_plot$Contig_name[i], "^[^ ]+"))
}
df_plot$Species = factor(df_plot$Species, levels = unique(df_plot$Species) )
ggplot(df_plot, aes(x = factor(days), y = ANI, group = subject, color = subject)) +
geom_point(size = 3, alpha = 0.8) + # points per subject/day
geom_line(size = 1) + # lines connecting days per subject
scale_color_manual(values = c(
"#D55E00", # reddish-orange
"#0072B2", # deep blue
"#009E73", # teal/green
"#CC79A7", # magenta
"#F0E442", # yellow
"#E69F00", # orange
"#56B4E9", # sky blue
"#999999", # gray
"#A6761D", # brown
"#66CC99", # mint
"#CC6666", # muted red
"#6699CC" # muted blue
)) +
facet_wrap(~Species, scales = "free_x") +
labs(x = "Day", y = "ANI", color = "Subject") +
theme(axis.text.x = element_text(hjust = 0.5)) +
theme_clean(base_size = 14)