diff --git a/NAMESPACE b/NAMESPACE index 170799c..2b1504b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -148,4 +148,5 @@ importFrom(stats,predict) importFrom(stats,sd) importFrom(utils,capture.output) importFrom(utils,combn) +importFrom(utils,getFromNamespace) importFrom(utils,read.csv) diff --git a/R/704-calcProtSeqSim.R b/R/704-calcProtSeqSim.R index bd54048..f3f32a3 100644 --- a/R/704-calcProtSeqSim.R +++ b/R/704-calcProtSeqSim.R @@ -10,11 +10,12 @@ } else { + pwa = resolve_pwa() s1 = try(Biostrings::AAString(protlist[[id1]]), silent = TRUE) s2 = try(Biostrings::AAString(protlist[[id2]]), silent = TRUE) - s12 = try(Biostrings::pairwiseAlignment(s1, s2, type = type, substitutionMatrix = submat, scoreOnly = TRUE), silent = TRUE) - s11 = try(Biostrings::pairwiseAlignment(s1, s1, type = type, substitutionMatrix = submat, scoreOnly = TRUE), silent = TRUE) - s22 = try(Biostrings::pairwiseAlignment(s2, s2, type = type, substitutionMatrix = submat, scoreOnly = TRUE), silent = TRUE) + s12 = try(pwa(s1, s2, type = type, substitutionMatrix = submat, scoreOnly = TRUE), silent = TRUE) + s11 = try(pwa(s1, s1, type = type, substitutionMatrix = submat, scoreOnly = TRUE), silent = TRUE) + s22 = try(pwa(s2, s2, type = type, substitutionMatrix = submat, scoreOnly = TRUE), silent = TRUE) if ( is.numeric(s12) == FALSE | is.numeric(s11) == FALSE | is.numeric(s22) == FALSE ) { sim = 0L @@ -90,6 +91,8 @@ calcParProtSeqSim = function( tmp <- .calcSeqPairSim(rev(idx[, i]), protlist = protlist, type = type, submat = submat) } + # convert any error objects into length-1 message + seqsimlist = as.list(unlist(seqsimlist)) # convert list to matrix seqsimmat = matrix(0, length(protlist), length(protlist)) for (i in 1:length(seqsimlist)) seqsimmat[idx[2, i], idx[1, i]] = seqsimlist[[i]] @@ -141,9 +144,8 @@ calcTwoProtSeqSim = function( # sequence alignment for two protein sequences s1 = try(Biostrings::AAString(seq1), silent = TRUE) s2 = try(Biostrings::AAString(seq2), silent = TRUE) - s12 = try(Biostrings::pairwiseAlignment( - s1, s2, type = type, substitutionMatrix = submat), - silent = TRUE) + pwa = resolve_pwa() + s12 = try(pwa(s1, s2, type = type, substitutionMatrix = submat), silent = TRUE) return(s12) diff --git a/R/pwalign.R b/R/pwalign.R new file mode 100644 index 0000000..710229d --- /dev/null +++ b/R/pwalign.R @@ -0,0 +1,24 @@ +is_pwalign_needed <- function() { + rlang::is_installed("Biostrings", version = "2.72.0") +} + +is_pwalign_installed <- function() { + rlang::is_installed("pwalign") +} + +#' @importFrom utils getFromNamespace +get_pwa <- function(ns) { + getFromNamespace("pairwiseAlignment", ns = ns) +} + +resolve_pwa <- function() { + if (!is_pwalign_needed()) { + return(get_pwa("Biostrings")) + } + + if (!is_pwalign_installed()) { + stop("The package \"pwalign\" is required. Please install it from Bioconductor.", call. = FALSE) + } + + get_pwa("pwalign") +}