Skip to content

Commit

Permalink
Merge pull request #23 from nanxstats/pwalign
Browse files Browse the repository at this point in the history
Use pwalign for pairwise sequence alignment if Biostrings >= 2.72.0
  • Loading branch information
nanxstats authored Aug 30, 2024
2 parents 6d16a7f + 6dc3e20 commit bb82d29
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,5 @@ importFrom(stats,predict)
importFrom(stats,sd)
importFrom(utils,capture.output)
importFrom(utils,combn)
importFrom(utils,getFromNamespace)
importFrom(utils,read.csv)
14 changes: 8 additions & 6 deletions R/704-calcProtSeqSim.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]]
Expand Down Expand Up @@ -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)

Expand Down
24 changes: 24 additions & 0 deletions R/pwalign.R
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit bb82d29

Please sign in to comment.