library(ggchord)

library(ggchord)
library(dplyr)


library(dplyr)
seqs <- seq_data_example |>
  select(accver, length)
links <- ribbon_data_example |>
  select(qaccver, saccver, qstart, qend, sstart, send)
# 从原始注释字段恢复真实链。
genes <- gene_data_example |>
  transmute(accver, start, end, strand = source_strand)


bed_path <- tempfile(fileext = ".bed")
writeLines("pUC19\t866\t1455", bed_path)
bed_features <- read_bed(bed_path)
unlink(bed_path)
bed_features


paf_source <- read_paf("examples/format-inputs/emales-pair.paf")
paf_links <- filter(paf_source, !is.na(mapq), mapq >= 30)
paf_seqs <- bind_rows(
  transmute(paf_source, accver = qaccver, length = qlen),
  transmute(paf_source, accver = saccver, length = slen)
) |> distinct()
head(select(paf_links, qaccver, qstart, qend, saccver, sstart, send), 3)


p_paf <- ggchord(paf_seqs) +
  geom_seq() +
  geom_seq_label() +
  geom_link_ribbon(data = paf_links) +
  coord_collinear()
view_ggchord(p_paf, width = 10, height = 5, units = "in")


gr <- GenomicRanges::GRanges("a", IRanges::IRanges(10, 30), strand = "+", seqlengths = c(a = 100))
as_ggchord_feature(gr)


query <- GenomicRanges::GRanges("a", IRanges::IRanges(10, 30), seqlengths = c(a = 100))
subject <- GenomicRanges::GRanges("b", IRanges::IRanges(40, 60), seqlengths = c(b = 120))
pair <- InteractionSet::GInteractions(query, subject)
as_ggchord_link(pair)


data(ribbon_data_example)
paired_rows <- ribbon_data_example |>
  transmute(
    chrom1 = qaccver, start1 = pmin(qstart, qend) - 1, end1 = pmax(qstart, qend),
    chrom2 = saccver, start2 = pmin(sstart, send) - 1, end2 = pmax(sstart, send)
  )
paired_file <- tempfile(fileext = ".bedpe")
write.table(paired_rows, paired_file,
  sep = "\t", quote = FALSE, row.names = FALSE, col.names = FALSE
)
paired_links <- read_bedpe(paired_file)
unlink(paired_file)
paired_links |>
  select(qaccver, saccver, qstart, qend, sstart, send, q_mapped, s_mapped)

