Add informative error when matrix size is diff
Add informative error when matrix size is diff

When the number of species in the Host or Parasite phylogeny is
different to the number of species on the interaction matrix it used to
spit the “subscript out of bounds” error. Now it tells the actual
problem

efcaguab/paco
#' Prepare the datapoll
#' Simple wrapper to make sure that the matrices are sorted accordingly
#' @param H Host distance matrix 
#' @param P Parasite distance matrix 
#' @param HP Host-parasite association matrix, hosts in rows
#' @return A list with objects H, P, HP
#' @export
#' @examples 
#' data(gopherlice)
#' library(ape)
#' gdist <- cophenetic(gophertree)
#' ldist <- cophenetic(licetree)
#' D <- prepare_paco_data(gdist, ldist, gl_links)
prepare_paco_data <- function(H, P, HP)
{
   if(NROW(H) != NCOL(H))
      stop("H should be a square matrix")
   if(NROW(P) != NCOL(P))
      stop("P should be a square matrix")
   if(NROW(H) != NROW(HP)){
      warning("The HP matrix should have hosts in rows. It has been translated.")
      HP <- t(HP)
   }
   if (!(NROW (H) %in% dim(HP)))
     stop ("The number of species in H and HP don't match")
   if (!(NROW (P) %in% dim(HP)))
     stop ("The number of species in P and HP don't match")
   H <- H[rownames(HP),rownames(HP)]
   P <- P[colnames(HP),colnames(HP)]
   HP[HP>0] <- 1
   return(list(H=H, P=P, HP=HP))
}

#' Prepare the datapoll
#' Simple wrapper to make sure that the matrices are sorted accordingly
#' @param H Host distance matrix 
#' @param P Parasite distance matrix 
#' @param HP Host-parasite association matrix, hosts in rows
#' @return A list with objects H, P, HP
#' @export
#' @examples 
#' data(gopherlice)
#' library(ape)
#' gdist <- cophenetic(gophertree)
#' ldist <- cophenetic(licetree)
#' D <- prepare_paco_data(gdist, ldist, gl_links)
prepare_paco_data <- function(H, P, HP)
{
   if(NROW(H) != NCOL(H))
      stop("H should be a square matrix")
   if(NROW(P) != NCOL(P))
      stop("P should be a square matrix")
   if(NROW(H) != NROW(HP)){
      warning("The HP matrix should have hosts in rows. It has been translated.")
      HP <- t(HP)
   }
   H <- H[rownames(HP),rownames(HP)]
   P <- P[colnames(HP),colnames(HP)]
   HP[HP>0] <- 1
   return(list(H=H, P=P, HP=HP))
}
