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)
# Recover biological strands from the source annotation field.
genes <- gene_data_example |>
  transmute(accver, start, end, strand = source_strand)


if (requireNamespace("VariantAnnotation", quietly = TRUE)) {
  variant_file <- system.file("extdata", "chr22.vcf.gz", package = "VariantAnnotation")
  imported_variants <- read_vcf(variant_file, source_file = TRUE)
  imported_variants$features$variant |>
    select(accver, start, end, feature_id, REF, ALT) |>
    head()
}

if (requireNamespace("StructuralVariantAnnotation", quietly = TRUE)) {
  structural_file <- system.file("extdata", "gridss.vcf", package = "StructuralVariantAnnotation")
  structural <- read_vcf(structural_file, structural = TRUE)
  structural$links |>
    select(qaccver, saccver, qstart, qend, sstart, send,
      q_breakend_orientation, s_breakend_orientation)
  structural$report$records[[1]]$warnings
}


alignment_python <- Sys.getenv("ALIGNMENT_PYTHON", "python3")
alignment_available <- nzchar(Sys.which(alignment_python)) &&
  identical(suppressWarnings(system2(
    alignment_python,
    c("-c", shQuote("from Bio import Align; assert hasattr(Align, 'parse')")),
    stdout = FALSE, stderr = FALSE
  )), 0L)
if (alignment_available) {
  maf <- read_maf(
    "examples/genome-alignments/ucsc_mm9_chr10.maf",
    alphabet = "DNA", python = alignment_python, source_file = TRUE
  )
  local_maf <- maf$alignment |>
    filter(.source_input == 1, source_block == 1, between(alignment_column, 61, 100))
  ggplot2::ggplot(local_maf, aes(alignment_column, accver, label = symbol)) +
    geom_residue(overflow = "tile") +
    ggplot2::labs(x = "MAF alignment column", y = NULL) +
    ggplot2::theme_minimal()

  chains <- read_chain(
    "examples/genome-alignments/dna_rna.chain", python = alignment_python
  )
  observed_chain <- chains$ribbon_data |>
    filter(source_block == 1)
  observed_chain |>
    select(qaccver, saccver, qstart, qend, sstart, send)
}


delta_decoder <- Sys.getenv("DELTA_DECODER", "show-coords")
if (nzchar(Sys.which(delta_decoder))) {
  delta <- read_delta(
    "examples/genome-alignments/promer.delta",
    executable = delta_decoder, source_file = TRUE
  )
  observed_intervals <- delta$ribbon_data |>
    filter(qaccver == "3210101")
  displayed_ids <- unique(c(observed_intervals$qaccver, observed_intervals$saccver))
  displayed_sources <- delta$seq_data |>
    filter(accver %in% displayed_ids)

  ggchord(displayed_sources) +
    geom_seq(show.legend = FALSE) +
    geom_seq_label() +
    geom_link_ribbon(data = observed_intervals) +
    coord_collinear() +
    labs(
      title = "Existing translated alignment intervals",
      subtitle = "Drosophila source coordinates in bp; native identity in AA"
    )
}


if (nzchar(Sys.which(delta_decoder))) {
  plant_delta <- read_delta(
    "examples/genome-alignments/arabidopsis-first-pair.delta",
    executable = delta_decoder, source_file = TRUE
  )
  plant_delta$ribbon_data |>
    select(qaccver, qstart, qend, saccver, sstart, send, pident) |>
    head()
}


signature_file <- system.file("extdata", "P00517-signatures.tsv", package = "ggchord")
signatures <- read_signature_matches(signature_file, source_file = TRUE)
signatures$features$signature |>
  select(accver, start, end, signature_id, analysis, source_score)

if (requireNamespace("jsonlite", quietly = TRUE)) {
  cached_file <- system.file("extdata", "P51587-pfam.json", package = "ggchord")
  cached <- read_signature_matches(cached_file, format = "entry_json")
  ggchord(cached$seq_data) +
    geom_seq(show.legend = FALSE) +
    geom_seq_label() +
    geom_feature(
      aes(feature_fill = signature_id), data = cached$features$signature,
      shape = "block", position = position_feature_stack()
    ) +
    coord_collinear()
}


pair_file <- system.file("extdata", "plant-feature-pairs.homology", package = "ggchord")
group_file <- system.file("extdata", "plant-feature-groups.txt", package = "ggchord")
position_file <- system.file("extdata", "plant-pair-features.gff", package = "ggchord")

# This four-column tool table is not GFF3. Explicitly declare source anchors.
anchors <- read.table(
  position_file, header = FALSE, na.strings = character(),
  col.names = c("accver", "feature_id", "start", "end"),
  colClasses = c("character", "character", "numeric", "numeric")
)
relations <- read_feature_relations(
  pair_file, feature_data = anchors, source_file = TRUE
)
relations$ribbon_data |>
  select(qfeature_id, sfeature_id, qaccver, qstart, qend, source_score)

groups <- read_feature_relations(group_file, format = "named_groups")
groups$membership |>
  select(group_id, feature_id, .source_group, .source_member)

