Add graph validation step
authorPaul Hoffman <phoffman@nygenome.org>
Mon, 26 Mar 2018 21:36:01 +0000 (17:36 -0400)
committerPaul Hoffman <phoffman@nygenome.org>
Mon, 26 Mar 2018 21:36:01 +0000 (17:36 -0400)
R/loom.R

index 9b6046e0ad5c7ee10df88dc8a57ca8f7b57d06c4..e15ac77311d198e90ee9b92a595b0c8925d9ee07 100644 (file)
--- a/R/loom.R
+++ b/R/loom.R
@@ -547,6 +547,21 @@ loom <- R6Class(
         stop("'MARGIN' must be 1 or 2")
       )
       group <- paste0(group, '_graphs')
+      # Validate graph
+      if (!name %in% list.groups(object = self[[group]], full.names = FALSE, recursive = FALSE)) {
+        stop(paste("Cannot find a graph named", name, "in", group))
+      }
+      graph.datasets <- list.datasets(object = self[[group]][[name]], full.names = FALSE)
+      if (length(x = graph.datasets) != 3) {
+        stop(paste0(
+          "Malformed graph: wrong number of components for graph (",
+          length(x = graph.datasets),
+          ")"
+        ))
+      }
+      if (!all(graph.datasets %in% c('a', 'b', 'w'))) {
+        stop("Malformed graph: unknown dataset names")
+      }
       graph <- sparseMatrix(
         i = self[[group]][[name]][['a']][] + 1,
         j = self[[group]][[name]][['b']][],