Skip to content

Commit

Permalink
teach diagonal to use the 'diagonal' attribute if a matrix has one
Browse files Browse the repository at this point in the history
* closes #14
* could help with #8
  • Loading branch information
jefferis committed Mar 25, 2014
1 parent 68496bb commit e35d171
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
15 changes: 10 additions & 5 deletions R/nblast.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,27 @@ fc_subscoremat<-function(query, target, scoremat=NULL, distance=FALSE,
}

# utility function to extract diagonal terms from matrices
# uses the 'diagonal' attribute when available
#' @importFrom bigmemory is.big.matrix
#' @importFrom ff is.ff arrayIndex2vectorIndex
diagonal<-function(x, indices=NULL){
if(is.character(indices)) indices=match(indices,rownames(x))
if(is.logical(indices)) indices=which(indices)
ndiags <- if(is.null(indices)){
nrow(x)
} else {
length(indices)
if(!is.null(attr(x,'diagonal'))){
return(diagonal[indices])
}

if(is.logical(indices)) indices=which(indices)

if(is.ff(x)){
# convert array indices to vector indices
vidxs=arrayIndex2vectorIndex(cbind(indices,indices),dim=dim(x))
x[vidxs]
} else if(is.big.matrix(x)) {
ndiags <- if(is.null(indices)){
nrow(x)
} else {
length(indices)
}
diags=rep(NA,ndiags)
for(i in seq_len(ndiags)){
idx=indices[i]
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-nblast.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,13 @@ test_that('fc_subscoremat and fc_nblast agree', {
fc_subscoremat(nn,nn[1],scoremat=scores,normalisation='mean',distance=FALSE))
})

test_that('we can use diagonal attribute on a score matrix',{
library(ff)
scoresff=as.ff(scores)
scoresffd=scoresff
attr(scoresffd,'diagonal')=diag(scores)
expect_equal(fc_nblast(nn[1], nn, scoremat = scoresff),
fc_nblast(nn[1], nn, scoremat = scoresffd))
})

options(op)

0 comments on commit e35d171

Please sign in to comment.