cayek / TESS3

[DEPRECATED]
GNU General Public License v2.0
8 stars 3 forks source link

as.qmatrix error #5

Open thesnakeguy opened 2 years ago

thesnakeguy commented 2 years ago

Hi,

Currently following example code (https://rdrr.io/github/cayek/TESS3_encho_sen/man/as.qmatrix.html) is throwing an error:

library(tess3r)
## an example with 3 individuals and 2 clusters
test <- matrix(c(0.4,0.6,0.3,0.7, 0.2, 0.8), byrow = TRUE, nrow = 3)
Qmatrix <- as.qmatrix(test)
barplot(Qmatrix, space = 0, xlab = "individuals",
        ylab = "Ancestry proportions", main = "Ancestry matrix")

the error:

Error in if (class(matrix) != c("matrix")) Q <- as.matrix(matrix) : 
  the condition has length > 1

This is because

class(test)
[1] "matrix" "array"

Adapting the function to

as.qmatrix <- function(Q){
  if (class(Q)[2] != "matrix") Q <- as.matrix(Q)
  if (min(Q) < 0) stop("Q contains negative elements.")
  sumofq <- apply(Q, MARGIN = 1, sum)
  sumofq <- round(sum(sumofq))
  if (sumofq != nrow(Q)) stop("Input matrix is not an ancestry matrix: The sum of ancestry coefficients is not equal to one")
  class(Q) = "tess3Q"
  return(Q)
}

solves it. Might be related to my R version: R version 4.2.1 (2022-06-23 ucrt)

Just thought I let you know.

Cheers