library(ggchord)

library(ggchord)
library(dplyr)


library(dplyr)
fusion_hsp <- sequence_detail_example$paf
fusion_seqs <- bind_rows(
  fusion_hsp |> transmute(accver = qaccver, length = qlen),
  fusion_hsp |> transmute(accver = saccver, length = slen)
) |>
  distinct()
fusion_colours <- setNames(c("#0072B2", "#D55E00"), fusion_seqs$accver)
fusion_links <- fusion_hsp |>
  filter(length >= 900, pident >= 85)
fusion_events <- fusion_hsp |>
  transmute(accver = qaccver, pos = qstart)


fusion_center <- ggchord(fusion_seqs |> slice(1)) +
  geom_seq(aes(seq_colour = accver), show.legend = FALSE) +
  geom_track(
    data = fusion_events, geom = "bar", stat = "bin", binwidth = 2000,
    boundary = 1, closed = "left", bounds = "clip",
    track = position_track("hsp-start-count", width = unit(6, "mm"))
  ) +
  scale_track_value_continuous("hsp-start-count", name = NULL, breaks = c(0, 5)) +
  scale_seq_colour_manual(values = fusion_colours) +
  coord_circular()


fusion_linear <- ggchord(fusion_seqs, fusion_hsp) +
  geom_seq(show.legend = FALSE) +
  geom_link_ribbon(show.legend = FALSE) +
  geom_seq_label() +
  scale_seq_colour_manual(values = fusion_colours) +
  coord_collinear()
fusion_vertical <- fusion_linear +
  coord_collinear(seq_axis = "vertical")


p_fusionview <- ggchord_fusion(fusion_center, layout = "adaptive") +
  fusion_panel(fusion_vertical, id = "detail", side = "right") +
  fusion_ribbon(fusion_links, qpanel = "center", spanel = "detail", alpha = 0.28)


plasmid <- plasmid_example_pETDuet_1
annotations <- find_common_features(plasmid)
selected_region <- data.frame(accver = plasmid$accver, start = 101, end = 1900)
local_source <- focus_ggchord_data(
  plasmid,
  feature_data = annotations |> filter(start <= end),
  loci = selected_region
)
whole_plasmid <- ggchord(plasmid, feature_data = annotations) +
  geom_seq(show.legend = FALSE) +
  geom_feature(position = position_plasmid(), show.legend = FALSE) +
  geom_seq_center_label() +
  coord_circular(rotation = -30)
local_labels <- local_source$feature_data |>
  filter(type %in% c("CDS", "rep_origin"), anno != "ATG")
local_detail <- ggchord(
  local_source$seq_data, feature_data = local_source$feature_data
) +
  geom_seq(seq_orientation = -1, show.legend = FALSE) +
  geom_feature(show.legend = FALSE) +
  geom_feature_label(
    data = local_labels, label_orientation = "horizontal",
    label_overlap = "allow", colour = "#202020", size = 3
  ) +
  coord_collinear(seq_axis = "vertical") +
  scale_seq_position_continuous(coordinates = "source")
focused_view <- magnify_ggchord(whole_plasmid, selected_region, local_detail)
focused_view
focus_geometry <- export_ggchord_fusion(focused_view, width = 12, height = 8)
focus_geometry$focus_regions
focus_geometry$leaders


source_frame <- selected_region |>
  mutate(start = start - 20, end = end + 20)
framed_view <- magnify_ggchord(whole_plasmid, selected_region, local_detail,
  frame_region = source_frame, highlight_width = 1.5)
export_ggchord_fusion(framed_view, width = 12, height = 7)$focus_regions |>
  select(accver, start, end, frame_start, frame_end)


library(dplyr)
seqs <- seq_data_example |> select(accver, length)
features <- gene_data_example |> mutate(strand = source_strand, type = 'CDS')
hsp <- ribbon_data_example |> slice(1)
regions <- bind_rows(
  transmute(hsp, accver = qaccver, start = pmin(qstart, qend), end = pmax(qstart, qend)),
  transmute(hsp, accver = saccver, start = pmin(sstart, send), end = pmax(sstart, send))
)
local <- focus_ggchord_data(seqs, feature_data = features,
  ribbon_data = hsp, loci = regions)
source <- ggchord(seqs, feature_data = features, ribbon_data = hsp) +
  geom_seq(show.legend = FALSE) + geom_gene(show.legend = FALSE) +
  geom_link_ribbon(show.legend = FALSE)
detail <- ggchord(local$seq_data, feature_data = local$feature_data,
  ribbon_data = local$ribbon_data) + geom_seq(show.legend = FALSE) +
  geom_gene(show.legend = FALSE) + geom_link_ribbon(show.legend = FALSE) + coord_collinear()
paired_detail <- magnify_ggchord(
  source + coord_collinear(), regions, detail, side = "bottom"
)
paired_detail
export_ggchord_fusion(paired_detail, width = 12, height = 8)$focus_regions


library(ggchord)
library(dplyr)
seqs <- seq_data_example |> select(accver, length)
hsp <- ribbon_data_example |> slice(1)
regions <- bind_rows(
  transmute(hsp, accver = qaccver, start = pmin(qstart, qend),
    end = pmax(qstart, qend), detail_panel = "query"),
  transmute(hsp, accver = saccver, start = pmin(sstart, send),
    end = pmax(sstart, send), detail_panel = "subject")
)
make_detail <- function(region) {
  local <- focus_ggchord_data(seqs, loci = region)
  ggchord(local$seq_data) +
    geom_seq(show.legend = FALSE) +
    coord_collinear(seq_axis = "vertical")
}
source <- ggchord(seqs, hsp) +
  geom_seq(show.legend = FALSE) +
  geom_link_ribbon(show.legend = FALSE)
details <- list(
  query = make_detail(regions[1, ]),
  subject = make_detail(regions[2, ])
)
multiple <- magnify_ggchord(
  source, regions, details,
  side = c(query = "left", subject = "right")
)
export_ggchord_fusion(multiple, width = 12, height = 8)$focus_regions


library(dplyr)
cells <- sequence_detail_example$dna_alignment
rows <- unique(cells$accver)[c(1, 3)]
window <- cells |>
  filter(accver %in% rows, between(alignment_column, 321, 350))
sequences <- cells |>
  filter(accver %in% rows, !is_gap) |>
  group_by(accver) |>
  summarise(length = max(source_position), .groups = "drop")
regions <- window |>
  filter(!is_gap) |>
  group_by(source_accver) |>
  summarise(start = min(source_position), end = max(source_position), .groups = "drop") |>
  rename(accver = source_accver)
letters <- ggplot2::ggplot(window, aes(alignment_column, accver, label = symbol)) +
  geom_residue(overflow = "error", show.legend = FALSE) +
  ggplot2::scale_y_discrete(limits = rev(rows),
    labels = setNames(c("AF191661.1", "AF191659.1"), rows),
    expand = expansion(add = 0.6)) +
  ggplot2::scale_x_continuous(breaks = seq(321, 350, 5), expand = expansion(add = 0.6)) +
  labs(title = "Aligned nucleotide detail", x = "Alignment column", y = NULL) +
  ggplot2::theme_minimal(base_size = 12) +
  theme(panel.grid = element_blank(), plot.title = element_text(face = "bold"),
    axis.text = element_text(colour = "#25363B"))
letters <- register_ggchord_alignment(letters, window)
macro <- ggchord(sequences) +
  geom_seq(aes(seq_colour = accver), show.legend = FALSE) +
  geom_seq_label(labels = setNames(c("AF191661.1", "AF191659.1"), rows), size = 3.5) +
  coord_collinear() +
  labs(title = "Sequence context") +
  theme_ggchord_publication()
character_detail <- magnify_ggchord(
  macro, regions, letters,
  center_box = c(.01, .28, .20, .85), detail_box = c(.33, .99, .15, .85),
  connector = "envelope", colour = "#725094", highlight_width = 3
)
export_ggchord_fusion(character_detail, width = 16, height = 5)$focus_regions
# ggsave("character-detail.png", character_detail, width = 16, height = 5)

