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)


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)
view_ggchord(p_fusionview, width = 11, height = 8, units = "in")


p_fusionfour <- ggchord_fusion(fusion_center, layout = "adaptive") +
  fusion_panel(fusion_vertical, id = "east", side = "right") +
  fusion_panel(fusion_vertical, id = "west", side = "left") +
  fusion_panel(fusion_linear, id = "north", side = "top") +
  fusion_panel(fusion_linear, id = "south", side = "bottom") +
  fusion_ribbon(fusion_hsp |> slice(3), qpanel = "center", spanel = "north", fill = "#0072B2") +
  fusion_ribbon(fusion_hsp |> slice(2), qpanel = "center", spanel = "west", fill = "#0072B2") +
  fusion_ribbon(fusion_hsp |> slice(9), qpanel = "center", spanel = "south", fill = "#0072B2") +
  fusion_ribbon(fusion_hsp |> slice(1), qpanel = "center", spanel = "east", fill = "#0072B2", alpha = 0.16)
view_ggchord(p_fusionfour, width = 14, height = 11, units = "in")


fusion_layout <- export_ggchord_fusion(p_fusionview, width = 11, height = 8)
fusion_layout$connections |>
  select(layer_id, source_row, qpanel, q_instance, spanel, s_instance, retained)
fusion_layout$fronts |>
  select(panel_id, accver, position_boundary, x, y) |>
  slice_head(n = 4)


ggsave("fusion.png", p_fusionview, width = 11, height = 8, units = "in", dpi = 300)
view_ggchord(p_fusionview, width = 11, height = 8, viewer = "none")


# 可选依赖为 dplyr；这里使用真实随包 HSP 记录。
library(ggchord)
library(dplyr)
first_hit <- ribbon_data_example |> slice_head(n = 1)
channel_hits <- ribbon_data_example |>
  filter(qaccver == first_hit$qaccver, saccver == first_hit$saccver) |>
  mutate(route_group = 'explicit-display-channel')
# 共用通道描述绘制方式，不合并生物学证据。
panel <- ggchord(seq_data_example) +
  geom_seq(show.legend = FALSE) +
  geom_seq_label(size = 3, seq_label_rotation = 90) +
  coord_collinear(seq_axis = 'vertical') +
  theme(ggchord.axis = element_blank())
composition <- ggchord_fusion(panel, center_box = c(.02, .28, .12, .88)) +
  fusion_panel(panel, 'target', box = c(.72, .98, .12, .88)) +
  fusion_panel(panel, 'obstacle', box = c(.4, .6, .22, .78))
routed <- composition +
  fusion_ribbon(channel_hits, qpanel = 'center', spanel = 'target',
    route = 'bundle', route_width = .8, fill = '#548CA0', alpha = .3)
view_ggchord(routed, width = 12, height = 8, units = "in")

