Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ Imports:
GenomeInfoDb,
GenomicRanges,
stats,
S4Vectors
S4Vectors,
grDevices,
tools
Suggests:
biomaRt,
circlize,
Expand All @@ -49,8 +51,8 @@ Suggests:
ggraph,
ggrepel,
igraph,
grDevices,
matrixStats,
mockery,
openxlsx,
pheatmap,
readr,
Expand Down
10 changes: 10 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(print,circos_genome)
export(add_annotations)
export(addgenesPA)
export(detect_filter)
Expand All @@ -12,6 +13,7 @@ export(get_superterm)
export(getgenesPA)
export(heatmap_PA)
export(heatmap_path_PA)
export(is_circos_genome)
export(list_ensembl_species)
export(list_ensembl_versions)
export(list_gmts)
Expand All @@ -26,17 +28,25 @@ export(nice_UMAP)
export(nice_VSB)
export(nice_VSB_DEseq2)
export(nice_Volcano)
export(nice_circos)
export(nice_tSNE)
export(power_analysis)
export(resolve_genome)
export(save_results)
export(split_cases)
export(splot_PA)
export(tpm)
import(ggplot2)
importFrom(S4Vectors,"mcols<-")
importFrom(S4Vectors,mcols)
importFrom(grDevices,adjustcolor)
importFrom(grDevices,dev.off)
importFrom(grDevices,pdf)
importFrom(grid,gpar)
importFrom(magrittr,"%>%")
importFrom(patchwork,plot_layout)
importFrom(rlang,.data)
importFrom(stats,na.omit)
importFrom(stats,setNames)
importFrom(tools,file_ext)
importFrom(utils,modifyList)
85 changes: 56 additions & 29 deletions R/nice_GenomeTrack.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,12 @@ nice_GenomeTrack <- function(
call. = FALSE
)
}
if (!requireNamespace("biomaRt", quietly = TRUE)) {
needs_biomart <- is.null(annotations) || isTRUE(show_transcripts)

if (isTRUE(needs_biomart) && !requireNamespace("biomaRt", quietly = TRUE)) {
stop(
"Package \"biomaRt\" must be installed to use this function.",
"Package \"biomaRt\" must be installed when annotations are queried ",
"or when show_transcripts = TRUE.",
call. = FALSE
)
}
Expand Down Expand Up @@ -124,35 +127,61 @@ nice_GenomeTrack <- function(
stop("`region` start must be less than or equal to end.", call. = FALSE)
}

# ---- Resolve Ensembl host --------------------------------------------------
if (!exists(".resolve_ensembl_host",
mode = "function")) {
stop(
"Internal helper `.resolve_ensembl_host()` not found. ",
"Please update OmicsKit to include the Ensembl helpers from PR #44.",
call. = FALSE
# ---- Validate local annotations before any BioMart connection ---------------
required_annotation_cols <- c(
"geneID", "symbol", "chromosome", "gene_start", "gene_end"
)

if (!is.null(annotations)) {
annotations <- as.data.frame(annotations)

missing_annotation_cols <- setdiff(
required_annotation_cols,
names(annotations)
)

if (length(missing_annotation_cols) > 0) {
stop(
"`annotations` must include columns: ",
paste(required_annotation_cols, collapse = ", "),
call. = FALSE
)
}
}

host <- .resolve_ensembl_host(ensembl_version)
# ---- Resolve Ensembl host and connect to BioMart only if needed -------------
host <- NULL
mart <- NULL

# ---- Connect to BioMart ----------------------------------------------------
mart <- tryCatch(
biomaRt::useMart("ENSEMBL_MART_ENSEMBL", dataset = organism, host = host),
error = function(e) {
if (exists(".handle_biomart_connection_error", mode = "function")) {
.handle_biomart_connection_error(e, host, organism, ensembl_version)
}
stop(e$message, call. = FALSE)
if (isTRUE(needs_biomart)) {
if (!exists(".resolve_ensembl_host", mode = "function")) {
stop(
"Internal helper `.resolve_ensembl_host()` not found. ",
"Please update OmicsKit to include the Ensembl helpers.",
call. = FALSE
)
}
)

host <- .resolve_ensembl_host(ensembl_version)

mart <- tryCatch(
biomaRt::useMart("ENSEMBL_MART_ENSEMBL", dataset = organism, host = host),
error = function(e) {
if (exists(".handle_biomart_connection_error", mode = "function")) {
.handle_biomart_connection_error(e, host, organism, ensembl_version)
}
stop(e$message, call. = FALSE)
}
)
}

# ---- Basic metadata --------------------------------------------------------
chr_num <- sub("^chr", "", chr)
is_human <- grepl("^hsapiens", organism)

# Resolve the best symbol attribute for non-human species if available
symbol_info <- if (exists(".get_symbol_attribute", mode = "function")) {
symbol_info <- if (isTRUE(needs_biomart) &&
exists(".get_symbol_attribute", mode = "function")) {
.get_symbol_attribute(mart, organism)
} else {
list(attr = "external_gene_name", source = "External")
Expand Down Expand Up @@ -216,15 +245,6 @@ nice_GenomeTrack <- function(

# ---- Build annotation data -----------------------------------------------
if (!is.null(annotations)) {
required_cols <- c("geneID", "symbol", "chromosome", "gene_start", "gene_end")
if (!all(required_cols %in% names(annotations))) {
stop(
"`annotations` must include columns: ",
paste(required_cols, collapse = ", "),
call. = FALSE
)
}

# Use provided annotations directly
anno_data <- data.frame(
ensembl_gene_id = annotations$geneID,
Expand Down Expand Up @@ -427,6 +447,13 @@ nice_GenomeTrack <- function(

# ---- Optional transcript track --------------------------------------------
if (isTRUE(show_transcripts)) {
if (is.null(mart)) {
stop(
"`show_transcripts = TRUE` requires a BioMart connection.",
call. = FALSE
)
}

bm_transcripts <- biomaRt::getBM(
attributes = c(
"ensembl_gene_id", "ensembl_transcript_id",
Expand Down
Loading
Loading