## ----prepare, echo=FALSE, results="hide", warning=FALSE-----------------------
knitr::opts_chunk$set(
    warning = FALSE, message = FALSE,
    fig.width = 12, fig.height = 4,
    dpi = 72
)

## ----installation, eval=FALSE-------------------------------------------------
# if (!require("BiocManager", quietly = TRUE)) {
#     install.packages("BiocManager")
# }
# BiocManager::install("orthoRibbon")

## ----loadlibrary,warning = FALSE,message=FALSE--------------------------------
library(orthoRibbon)
library(geneClusterPattern)
library(GenomeInfoDb)

## ----getAbbreviations---------------------------------------------------------
common_name <- geneClusterPattern::guessSpecies(
  c('human', 'fruit fly'))
names(common_name) <- common_name # must assign the names.
common_name

## ----load_human_fly_data------------------------------------------------------
data("human_fly")
ls()
names(chrom_infos)
head(chrom_infos[[1]], n=2)
## the homologs_df must contain columns 'gene_id', and 'ortholog_group'.\
head(homologs_df, n=2)

## ----prepare_human_fly_data---------------------------------------------------
plotData <- buildPlotData(common_name, homologs_df,
                          chrom_infos = chrom_infos,
                          chromosome_order_method = 'max',
                          max_links=1000)
names(plotData)
# Here subset the plotData to reduce the vignette file size
# This is not necessary for real plot.
plotData <- subset(plotData,
                   subset=list('hsapiens'=c('1', '2', '3', 'X'),
                               'dmelanogaster'=c('2L', '2R', 'X')))

## ----calBezierCurve_human_fly_data--------------------------------------------
# by homolog chromosomes
bezier_mini <- buildBezierDF(plotData$homolog_df_list,
                             colname1='topMini_finalOffset',
                             colname2='bottomMini_finalOffset')

## ----plotData_human_fly_data--------------------------------------------------
orthoRibbonPlot(common_name, bezier_mini,
                plotData$chrom_bars_df,
                plotData$chrom_label_df)

## ----retrieveHomologs,eval=FALSE----------------------------------------------
# # prepare mart for BioMart
# marts <- geneClusterPattern::guessSpecies(
#   common_name, output='mart',
#   host='https://jun2026.archive.ensembl.org')
# 
# # prepare all the Ensembl ids
# ids <- orthoRibbon::getGeneIDs(common_name, marts)
# 
# # get chromosome info
# chrom_infos<- lapply(common_name, GenomeInfoDb::getChromInfoFromEnsembl)
# 
# # retrieve all the homologs
# homologs <- orthoRibbon::getHomologGRs(ids, common_name, marts)
# 
# # retrieve all the gene ranges
# genes_gr <- orthoRibbon::getGeneGRs(ids, marts, homologs)
# 
# # reformat the homologs to data.frame
# homologs_df <- getHomologIDs(homologs)

## ----loadEnsemblData----------------------------------------------------------
common_name <- c("hsapiens", "mmusculus", "drerio")
common_name <- setNames(common_name, common_name)
data("ensembl_hs_mm_dr")
names(chrom_infos)
head(chrom_infos[[1]], n=2)
head(homologs_df, n=2)
head(genes_gr, n=2)

## ----otherOrderMethods,eval=FALSE---------------------------------------------
# # Different methods optimize different criteria:
# # 1. max - minimizes the chromosome distance
# chr_order_max <- buildPlotData(common_name, homologs_df, genes_gr, chrom_infos,
#                           chromosome_order_method = "max")
# # 2. GW (Gruvaeus-Wainer) - minimizes gradient measure
# chr_order_gw <- buildPlotData(common_name, homologs_df, genes_gr, chrom_infos,
#                           chromosome_order_method = "GW")
# 
# # 3. OLO (Optimal Leaf Ordering) - optimal for dendrograms
# chr_order_olo <- buildPlotData(common_name, homologs_df, genes_gr, chrom_infos,
#                           chromosome_order_method = "OLO")
# 
# # 4. TSP (Traveling Salesman Problem) - minimizes path length
# chr_order_tsp <- buildPlotData(common_name, homologs_df, genes_gr, chrom_infos,
#                           chromosome_order_method = "TSP")
# 
# # 5. Spectral - uses eigenvectors
# chr_order_spec <- buildPlotData(common_name, homologs_df, genes_gr, chrom_infos,
#                           chromosome_order_method = "Spectral")
# 
# # 6. Spearman distance
# chr_order_dist <- buildPlotData(common_name, homologs_df, genes_gr, chrom_infos,
#                           chromosome_order_method = "spearman")
# 
# # 7. customized
# seq_names <- lapply(chrom_infos, function(.ele) sortSeqlevels(.ele$name))
# chr_order_manu <- buildPlotData(common_name, homologs_df, genes_gr, chrom_infos,
#                                 chr_orders=seq_names)

## ----otherOrderMethodsHidden, echo=FALSE, results='hide'----------------------
chr_order_max <- buildPlotData(common_name, homologs_df, genes_gr, chrom_infos,
                          chromosome_order_method = "max")

## ----calBezierCurve-----------------------------------------------------------
# chr_order_max as plotData from last step
# Where are the homologs of mouse X-linked genes located in the zebrafish and human genomes?
plotData <- subset(chr_order_max, subset=list('mmusculus'=c('X')))
# by coordinates
bezier_chr <- buildBezierDF(plotData$homolog_df_list,
                           colname1='topChr_finalOffset',
                           colname2='bottomChr_finalOffset')
# by the index
bezier_ix <- buildBezierDF(plotData$homolog_df_list,
                           colname1='topIx_finalOffset',
                           colname2='bottomIx_finalOffset')
# by homolog chromosome names
bezier_mini <- buildBezierDF(plotData$homolog_df_list,
                             colname1='topMini_finalOffset',
                             colname2='bottomMini_finalOffset')

## ----plotData-----------------------------------------------------------------
# by coordinates
orthoRibbonPlot(common_name, bezier_chr,
                plotData$chrom_bars_df,
                plotData$chrom_label_df)
# # by the index
# orthoRibbonPlot(common_name, bezier_ix,
#                 plotData$chrom_bars_df,
#                 plotData$chrom_label_df)
# by homologous chromosome names
# the gene is not ordered by coordinates but ordered by the homologous chromosome names
## And Here we will show the symbols for each homologous.
symbol_df_top <- plotData$symbol_df_top
symbol_df_top <- 
  symbol_df_top[sample(seq.int(nrow(symbol_df_top)), 50), ] # random select 50 symbols
symbol_df_top$x <- symbol_df_top$xMini
symbol_df_bottom <- plotData$symbol_df_bottom
symbol_df_bottom <- 
  symbol_df_bottom[sample(seq.int(nrow(symbol_df_bottom)), 50), ] # random select 50 symbols
symbol_df_bottom$x <- symbol_df_bottom$xMini
orthoRibbonPlot(common_name, bezier_mini,
                plotData$chrom_bars_df,
                plotData$chrom_label_df,
                show_symbol=TRUE,
                symbol_df_top=symbol_df_top,
                symbol_df_bottom=symbol_df_bottom,
                symbol_size=3,
                link_alpha=0.1) # add alpha value for the link

## ----sessioninfo, echo=FALSE--------------------------------------------------
sessionInfo()

