## ----echo=FALSE, results="hide", warning=FALSE--------------------------------
suppressPackageStartupMessages({
    library(org.Dr.eg.db)
    library(geneClusterPattern)
    library(GenomeInfoDb)
    fish <- readRDS(system.file('extdata', 'fish.rds',
                            package = 'geneClusterPattern'))
    homologs <- readRDS(system.file('extdata', 'homologs.rds',
                                package = 'geneClusterPattern'))
})
knitr::opts_chunk$set(warning=FALSE, message=FALSE)

## ----installation, eval=FALSE-------------------------------------------------
# if (!require("BiocManager", quietly = TRUE)) {
#     install.packages("BiocManager")
# }
# BiocManager::install('geneClusterPattern')
# # or install from github
# BiocManager::install('jianhong/geneClusterPattern')

## ----quickstart---------------------------------------------------------------
## load library
library(geneClusterPattern)
library(org.Dr.eg.db)
library(GenomeInfoDb)
## prepare all the ensembl ids
ids <- as.list(org.Dr.egENSEMBL)
ensembl_gene_ids <- sort(unique(unlist(ids)))

## ----guessmart, eval=FALSE----------------------------------------------------
# ## extract gene information via biomaRt
# fish_mart <- guessSpecies('zebrafish', output='mart', version=112)
# fish <- grangesFromEnsemblIDs(mart = fish_mart,
#                               ensembl_gene_ids = ensembl_gene_ids)
# ## if failed, try load presaved data
# # fish <- readRDS(system.file('extdata', 'fish.rds',
# #                             package = 'geneClusterPattern'))

## ----loadSpecies--------------------------------------------------------------
## subset the ensembl_gene_ids to save time
ensembl_gene_ids <- names(fish[seqnames(fish)=='24'])
## keep the standard sequence only
fish <- pruningSequences(fish)
## define the species scientific name
species <- guessSpecies(c('human', 'house mouse', 'Japanese medaka', 'turquoise killifish', 'gaculeatus'), version=112) # three-spined stickleback
species

## ----retrieveDataFromEnsembl, eval=FALSE--------------------------------------
# ## retrieve homologs from Ensembl
# homologs <- getHomologGeneList(species, fish_mart, ensembl_gene_ids)
# homologs <- pruningSequences(homologs)
# ## if failed, try to load pre-saved data
# # homologs <- readRDS(system.file('extdata', 'homologs.rds',
# #                                 package = 'geneClusterPattern'))

## ----getGeneCluster-----------------------------------------------------------
## get gene cluster for target gene
queryGene <- 'pcolce2b'
## define the gene cluster
nearest10neighbors <- getGeneCluster(fish, queryGene, homologs, k=10)

## prepare the gene annotations for all homologs for plot
genesList <- c(drerio=fish, homologs)[
  c("hsapiens", "mmusculus", "drerio", "olatipes", "nfurzeri", "gaculeatus")]

## plot the cluster
pgp <- plotGeneClusterPatterns(genesList, nearest10neighbors)

## ----orthofinder--------------------------------------------------------------
## import the ortholog groups
path <- system.file('extdata/orthofinder/Danio_rerio.GRCz11.pep.all.tsv.gz',
                    package='geneClusterPattern')
orthologs <- orthologPairsFromOrthoFinder(path)
## annotations
annoGR_list <- readRDS(
    system.file('extdata/orthofinder/grange.obj.rds',
                 package='geneClusterPattern'))
## reformat to meet the requirement of the package
homologs <- getHomologListForOrthoFinder(orthologs, annoGR_list[-1])
## set reference species
queryGR <- annoGR_list[["Danio rerio"]]
## define the gene cluster
nearest10neighbors <- getGeneCluster(queryGR, queryGene, homologs, k=10)
pgp <- plotGeneClusterPatterns(c('zebrafish'=queryGR, homologs),
                               nearest10neighbors)

## ----sessionInfo, results='asis'----------------------------------------------
sessionInfo()

