library(ggchord)

library(ggchord)
library(dplyr)


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)


p_tracks <- ggchord(track_seq) +
  geom_seq() +
  geom_track_col(aes(track_value = gc), data = bins,
                 position = position_track("gc", width = grid::unit(10, "mm"))) +
  scale_track_value_continuous("gc", limits = c(0, 1), unit = "GC fraction") +
  coord_circular()


library(dplyr)
foundation_source <- ribbon_data_example |>
  mutate(source_hsp_id = paste0("bundled:", row_number()))
foundation_pair <- foundation_source |>
  slice_head(n = 1) |>
  select(qaccver, saccver)
foundation_ids <- c(foundation_pair$qaccver, foundation_pair$saccver)
foundation_seqs <- seq_data_example |>
  filter(accver %in% foundation_ids) |>
  select(accver, length)
foundation_hsp <- foundation_source |>
  filter(qaccver %in% foundation_ids, saccver %in% foundation_ids,
    qaccver != saccver)
foundation_features <- gene_data_example |>
  filter(accver %in% foundation_ids) |>
  transmute(accver, start, end, strand = source_strand, anno)


foundation_events <- foundation_hsp |>
  transmute(accver = qaccver, pos = qstart)
p_foundationcount <- ggchord(foundation_seqs) +
  geom_seq() +
  geom_track(
    aes(x = pos), data = foundation_events,
    geom = "bar", track = position_track("query_starts")
  ) +
  coord_collinear()
view_ggchord(p_foundationcount, width = 9, height = 5, units = "in")


p_count_shortcut <- ggchord(foundation_seqs) +
  geom_seq() +
  geom_track_bar(
    aes(x = pos), data = foundation_events,
    track = position_track("query_starts")
  ) +
  coord_collinear()


make_gc_tracks <- function(colour_order) {
  ggchord(track_seq) + geom_seq() +
    geom_track_col(aes(track_value = gc), data = bins,
      position = position_track("gc_height", order = 1, width = grid::unit(8, "mm"))) +
    geom_track_tile(aes(track_fill = gc), data = bins,
      position = position_track("gc_colour", order = colour_order, width = grid::unit(4, "mm"))) +
    scale_track_value_continuous("gc_height", limits = c(0, 1), unit = "GC fraction") +
    scale_track_fill_gradientn("gc_colour", colours = c("#F1E51D", "#31849B"), limits = c(0, 1)) +
    coord_circular()
}


p_comparetracktwo <- make_gc_tracks(colour_order = 2)
view_ggchord(p_comparetracktwo, width = 9, height = 8, units = "in")


p_comparetrackorder <- make_gc_tracks(colour_order = 0)
view_ggchord(p_comparetrackorder, width = 9, height = 8, units = "in")


estimate_bins <- bins |> dplyr::mutate(pos = (start + end) / 2)
estimate_band <- position_track("gc_fit", width = unit(12, "mm"))
p_estimate <- ggchord(track_seq) +
  geom_seq() +
  geom_track(
    aes(x = pos, y = gc), data = estimate_bins, geom = "ribbon", stat = "smooth",
    method = "lm", formula = y ~ x, se = TRUE, position = "identity",
    fill = "#387D88", alpha = 0.2, track = estimate_band
  ) +
  geom_track(
    aes(x = pos, y = gc), data = estimate_bins, geom = "line", stat = "smooth",
    method = "lm", formula = y ~ x, se = FALSE, colour = "#285B65", track = estimate_band
  ) +
  scale_track_value_continuous("gc_fit", name = "GC fit", unit = "fraction") +
  coord_collinear()
view_ggchord(p_estimate)


track_loci <- data.frame(accver = track_seq$accver, start = 500, end = 1500)
focused_bins <- focus_ggchord_track(bins, track_seq, track_loci, type = "interval")
head(focused_bins)

