library(ggchord)

library(ggchord)
library(dplyr)
scale_seqs <- seq_data_example[c("accver", "length")]
scale_links <- ribbon_data_example
scale_ids <- scale_seqs$accver
scale_colours <- setNames(c("#E64B35", "#233E4A", "#269B82", "#D59B17"), scale_ids)
scale_opacity <- setNames(c(0.35, 0.55, 0.75, 0.95), scale_ids)
scale_lines <- setNames(c("solid", "dashed", "dotted", "dotdash"), scale_ids)
scale_base <- ggchord(scale_seqs, ribbon_data = scale_links) + geom_seq() + coord_chord()
scale_numeric <- scale_base + geom_link_ribbon(aes(ribbon_fill = pident))


p_comparescalegradient <- scale_numeric +
  scale_ribbon_fill_gradientn(colours = c("#303B71", "#269B82", "#ECEB31"),
    limits = c(0, 100), breaks = c(0, 50, 80, 90, 95, 100), name = "Identity (%)") +
  guides(ribbon_fill = guide_ggchord_colourbar())
view_ggchord(p_comparescalegradient, width = 9, height = 7, units = "in")


p_comparescalesteps <- scale_numeric +
  scale_ribbon_fill_stepsn(colours = c("#303B71", "#269B82", "#ECEB31"),
    breaks = c(0, 50, 80, 90, 95, 100), limits = c(0, 100),
    name = "Identity (%)", guide = ggplot2::guide_coloursteps(available_aes = "ribbon_fill",
      even.steps = FALSE, show.limits = TRUE, position = "left",
      direction = "vertical", theme = theme(legend.key.height = grid::unit(80, "mm"))))
view_ggchord(p_comparescalesteps, width = 9, height = 7, units = "in")


scale_identity_links <- scale_links |>
  mutate(display_colour = if_else(pident >= 90, "#269B82", "#303B71"))
p_scale_identity <- ggchord(scale_seqs, ribbon_data = scale_identity_links) +
  geom_seq() + geom_link_ribbon(aes(ribbon_fill = display_colour)) +
  scale_ribbon_fill_identity() + coord_chord()
invisible(ggplot2::ggplot_build(p_scale_identity))


p_scale_manual <- scale_base +
  geom_link_ribbon(aes(ribbon_fill = qaccver, ribbon_alpha = qaccver,
    ribbon_colour = qaccver, ribbon_linetype = qaccver)) +
  scale_ribbon_fill_manual(values = scale_colours) +
  scale_ribbon_alpha_manual(values = scale_opacity) +
  scale_ribbon_colour_manual(values = scale_colours) +
  scale_ribbon_linetype_manual(values = scale_lines) +
  scale_seq_colour_manual(values = scale_colours) +
  guides(ribbon_fill = guide_ggchord_legend(nrow = 1))
invisible(ggplot2::ggplot_build(p_scale_manual))


scale_points <- scale_links |>
  slice_head(n = 6) |>
  mutate(qpos = floor((qstart + qend) / 2), spos = floor((sstart + send) / 2))
p_scale_linkbase <- ggchord(scale_seqs) + geom_seq() + coord_chord()


p_scale_linkmanual <- p_scale_linkbase +
  geom_link_line(aes(link_colour = saccver, link_alpha = saccver,
    link_linewidth = saccver, link_linetype = saccver), data = scale_points) +
  scale_link_colour_manual(values = scale_colours) +
  scale_link_alpha_manual(values = scale_opacity) +
  scale_link_linewidth_manual(values = setNames(c(0.3, 0.5, 0.7, 0.9), scale_ids)) +
  scale_link_linetype_manual(values = scale_lines)
invisible(ggplot2::ggplot_build(p_scale_linkmanual))


p_comparescalelinkcontinuous <- p_scale_linkbase +
  geom_link_line(aes(link_colour = pident, link_alpha = pident,
    link_linewidth = pident), data = scale_points) +
  scale_link_colour_gradientn(
    colours = c("#303B71", "#ECEB31"), limits = c(0, 100),
    breaks = c(0, 50, 80, 90, 95, 100), name = "Identity (%)",
    guide = guide_ggchord_legend()
  ) +
  scale_link_alpha_continuous(
    range = c(0.3, 0.9), limits = c(0, 100),
    breaks = c(0, 50, 80, 90, 95, 100), name = "Identity (%)"
  ) +
  scale_link_linewidth_continuous(
    range = c(0.2, 1), limits = c(0, 100),
    breaks = c(0, 50, 80, 90, 95, 100), name = "Identity (%)"
  )
view_ggchord(p_comparescalelinkcontinuous, width = 9, height = 7, units = "in")


scale_feature_data <- head(find_common_features(plasmid_example_pUC19), 6)
feature_types <- unique(scale_feature_data$type)
feature_colours <- setNames(rep(c("#993366", "#31849B", "#D59B17"),
  length.out = length(feature_types)), feature_types)
p_scale_feature <- ggchord(plasmid_example_pUC19) + geom_seq() +
  geom_feature(aes(feature_fill = type, feature_shape = type), data = scale_feature_data) +
  scale_feature_fill_manual(values = feature_colours) +
  scale_feature_shape_manual(values = setNames(rep("arrow", length(feature_types)), feature_types)) +
  coord_circular()
invisible(ggplot2::ggplot_build(p_scale_feature))


p_scale_preset <- ggchord(plasmid_example_pUC19) + geom_seq() +
  geom_feature(aes(feature_fill = type, feature_shape = type), data = scale_feature_data) +
  scale_feature_fill_plasmid() + scale_feature_shape_plasmid() + coord_circular()
invisible(ggplot2::ggplot_build(p_scale_preset))


scale_gene_data <- gene_data_example |>
  transmute(accver, start, end, strand = source_strand)
p_scale_gene <- ggchord(scale_seqs, gene_data = scale_gene_data) + geom_seq() +
  geom_gene() + scale_gene_fill_manual(values = c("+" = "#CE1536", "-" = "#346B9D")) + coord_chord()
p_scale_ring <- ggchord(scale_seqs) + geom_seq(aes(seq_ring = accver)) +
  scale_seq_ring_manual(values = setNames(c(0.8, 1, 1.2, 1.4), scale_ids)) + coord_chord()
invisible(ggplot2::ggplot_build(p_scale_gene))
invisible(ggplot2::ggplot_build(p_scale_ring))


scale_region_data <- scale_links |>
  slice_head(n = 3) |>
  transmute(accver = qaccver, start = pmin(qstart, qend), end = pmax(qstart, qend))
p_scale_region <- ggchord(scale_seqs) + geom_seq() +
  geom_seq_region(aes(region_fill = accver), data = scale_region_data) +
  scale_region_fill_manual(values = scale_colours) + coord_chord()
invisible(ggplot2::ggplot_build(p_scale_region))


plasmid <- plasmid_example_pUC19
track_seq <- plasmid[c("accver", "length")]
starts <- seq.int(1, plasmid$length, by = 100)
bins <- data.frame(accver = plasmid$accver, start = starts,
                   end = pmin(starts + 99, plasmid$length))
bins$gc <- vapply(seq_len(nrow(bins)), function(i) {
  bases <- strsplit(substr(plasmid$sequence, bins$start[i], bins$end[i]), "")[[1]]
  mean(bases %in% c("G", "C"))
}, numeric(1))
head(bins)


scale_track_bins <- bins |>
  mutate(gc_class = if_else(gc >= 0.5, "GC >= 0.5", "GC < 0.5"))
p_scale_trackfill <- ggchord(track_seq) + geom_seq() +
  geom_track_tile(aes(track_fill = gc_class), data = scale_track_bins,
    position = position_track("classes")) +
  scale_track_fill_manual("classes", values = c("GC >= 0.5" = "#31849B", "GC < 0.5" = "#D59B17")) +
  coord_circular()
p_scale_trackcolour <- ggchord(track_seq) + geom_seq() +
  geom_track_tile(aes(track_colour = gc_class), data = scale_track_bins,
    position = position_track("classes"), fill = NA) +
  scale_track_colour_manual("classes", values = c("GC >= 0.5" = "#31849B", "GC < 0.5" = "#D59B17")) +
  coord_circular()
invisible(ggplot2::ggplot_build(p_scale_trackfill))
invisible(ggplot2::ggplot_build(p_scale_trackcolour))


p_scale_trackgradient <- ggchord(track_seq) + geom_seq() +
  geom_track_tile(aes(track_fill = gc, track_colour = gc), data = bins,
    position = position_track("gc")) +
  scale_track_fill_gradientn("gc", colours = c("#D59B17", "#31849B")) +
  scale_track_colour_gradientn("gc", colours = c("#D59B17", "#31849B")) +
  coord_circular()
invisible(ggplot2::ggplot_build(p_scale_trackgradient))


theme_examples <- list(
  default = theme_ggchord(), minimal = theme_ggchord_minimal(),
  publication = theme_ggchord_publication(), dark = theme_ggchord_dark()
)
for (selected_theme in theme_examples) {
  invisible(ggplot2::ggplot_build(scale_base + selected_theme))
}
p_comparethememinimal <- scale_base + theme_ggchord_minimal()
p_comparethemedark <- scale_base + theme_ggchord_dark()
p_theme_plasmid <- ggchord(plasmid_example_pUC19) + geom_seq() +
  coord_circular() + theme_ggchord_plasmid()
invisible(ggplot2::ggplot_build(p_theme_plasmid))

