Class GraphSaveLoadUtils

java.lang.Object
edu.cmu.tetrad.graph.GraphSaveLoadUtils

public class GraphSaveLoadUtils extends Object
Methods to load or save graphs.
Version:
$Id: $Id
Author:
josephramsey
  • Method Details

    • loadGraph

      public static Graph loadGraph(File file)

      loadGraph.

      Parameters:
      file - a File object
      Returns:
      a Graph object
    • loadGraphTxt

      public static Graph loadGraphTxt(File file)

      loadGraphTxt.

      Parameters:
      file - a File object
      Returns:
      a Graph object
    • loadGraphRuben

      public static Graph loadGraphRuben(File file)

      loadGraphRuben.

      Parameters:
      file - a File object
      Returns:
      a Graph object
    • loadGraphJson

      public static Graph loadGraphJson(File file)

      loadGraphJson.

      Parameters:
      file - a File object
      Returns:
      a Graph object
    • loadGraphBNTPcMatrix

      public static Graph loadGraphBNTPcMatrix(List<Node> vars, DataSet dataSet)

      loadGraphBNTPcMatrix.

      Parameters:
      vars - a List object
      dataSet - a DataSet object
      Returns:
      a Graph object
    • graphRMatrixTxt

      public static String graphRMatrixTxt(Graph graph) throws IllegalArgumentException

      graphRMatrixTxt.

      Parameters:
      graph - a Graph object
      Returns:
      a String object
      Throws:
      IllegalArgumentException - if any.
    • loadRSpecial

      public static Graph loadRSpecial(File file)

      loadRSpecial.

      Parameters:
      file - a File object
      Returns:
      a Graph object
    • loadGraphAmatCpdag

      public static Graph loadGraphAmatCpdag(File file)
      Loads a CPDAG in the "amat.cpdag" format of PCALG. We will assume here that the graph in R has been saved to disk using the write.table(mat, path) method. For the amat.cpdag format, for a matrix m, there are two cases where edges occur in the graph: (1) m[i][j] = 0 and m[j][i] = 1, in which case an edge i->j exists; or, (2) m[i][j] = 1 and m[j][i] = 1, in which case an undirected edge i--j exists. In all other cases, there is no edge between i and j.
      Parameters:
      file - a file in the "amat.cpdag" format of PCALG.
      Returns:
      a graph.
    • loadGraphAmatPag

      public static Graph loadGraphAmatPag(File file)
      Loads a PAG in the "amat.pag" format of PCALG. We will assume here that the graph in R has been saved to disk using the write.table(mat, path) method. For the amat.pag format, for a matrix m, endpoints are explicitly represented, as follows. 1 is a circle endpoint, 2 is an arrow endpoint, 3 is a tail endpoint, and 0 is a null endpoint (i.e., no edge). For an edge i->j, m[i][j] = 2 and m[j][i] = 3.
      Parameters:
      file - a file in the "amat.cpdag" format of PCALG.
      Returns:
      a graph.
    • loadGraphRMatrix

      public static String loadGraphRMatrix(Graph graph) throws IllegalArgumentException

      loadGraphRMatrix.

      Parameters:
      graph - a Graph object
      Returns:
      a String object
      Throws:
      IllegalArgumentException - if any.
    • readerToGraphTxt

      public static Graph readerToGraphTxt(String graphString) throws IOException

      readerToGraphTxt.

      Parameters:
      graphString - a String object
      Returns:
      a Graph object
      Throws:
      IOException - if any.
    • readerToGraphTxt

      public static Graph readerToGraphTxt(Reader reader) throws IOException

      readerToGraphTxt.

      Parameters:
      reader - a Reader object
      Returns:
      a Graph object
      Throws:
      IOException - if any.
    • saveGraph

      public static void saveGraph(Graph graph, File file, boolean xml)

      saveGraph.

      Parameters:
      graph - The graph to be saved.
      file - The file to save it in.
      xml - True if to be saved in XML, false if in text.
    • readerToGraphRuben

      public static Graph readerToGraphRuben(Reader reader) throws IOException

      readerToGraphRuben.

      Parameters:
      reader - a Reader object
      Returns:
      a Graph object
      Throws:
      IOException - if any.
    • readerToGraphJson

      public static Graph readerToGraphJson(Reader reader) throws IOException

      readerToGraphJson.

      Parameters:
      reader - a Reader object
      Returns:
      a Graph object
      Throws:
      IOException - if any.
    • graphToDot

      public static String graphToDot(Graph graph)
      Converts a graph to a Graphviz .dot file
      Parameters:
      graph - a Graph object
      Returns:
      a String object
    • graphToDot

      public static void graphToDot(Graph graph, File file)

      graphToDot.

      Parameters:
      graph - a Graph object
      file - a File object
    • convertToXml

      public static nu.xom.Element convertToXml(Graph graph)

      convertToXml.

      Parameters:
      graph - a Graph object
      Returns:
      an XML element representing the given graph. (Well, only a basic graph for now...)
    • graphToXml

      public static String graphToXml(Graph graph)

      graphToXml.

      Parameters:
      graph - a Graph object
      Returns:
      a String object
    • graphToLavaan

      public static String graphToLavaan(Graph g)

      graphToLavaan.

      Parameters:
      g - a Graph object
      Returns:
      a String object
    • graphToPcalg

      public static String graphToPcalg(Graph g)

      graphToPcalg.

      Parameters:
      g - a Graph object
      Returns:
      a String object
    • graphToAmatCpag

      public static String graphToAmatCpag(Graph g)
      Converts a given graph into an adjacency matrix in CPAG format.
      Parameters:
      g - the input graph to be converted
      Returns:
      the adjacency matrix representation of the graph in CPAG format
      Throws:
      IllegalArgumentException - if the graph is not a MPDAG (including CPDAG or DAG)
    • graphToAmatPag

      public static String graphToAmatPag(Graph g)
      Saves a PAG in the "amat.pag" format of PCALG. We will save it in the form that R would print the matrix to file using write.matrix(mat, path). For the amat.pag format, for a matrix m, endpoints are explicitly represented, as follows. 1 is a circle endpoint, 2 is an arrow endpoint, 3 is a tail endpoint, and 0 is a null endpoint (i.e., no edge)
      Parameters:
      g - a Graph object
      Returns:
      a String object
    • parseGraphXml

      public static Graph parseGraphXml(nu.xom.Element graphElement, Map<String,Node> nodes) throws nu.xom.ParsingException

      parseGraphXml.

      Parameters:
      graphElement - a Element object
      nodes - a Map object
      Returns:
      a Graph object
      Throws:
      nu.xom.ParsingException - if any.
    • getRootElement

      public static nu.xom.Element getRootElement(File file) throws nu.xom.ParsingException, IOException

      getRootElement.

      Parameters:
      file - a File object
      Returns:
      a Element object
      Throws:
      nu.xom.ParsingException - if any.
      IOException - if any.
    • grabLayout

      public static HashMap<String,PointXy> grabLayout(List<Node> nodes)

      grabLayout.

      Parameters:
      nodes - a List object
      Returns:
      a HashMap object
    • getCollidersFromGraph

      public static List<Triple> getCollidersFromGraph(Node node, Graph graph)

      getCollidersFromGraph.

      Parameters:
      node - a Node object
      graph - a Graph object
      Returns:
      A list of triples of the form X*->Y<-*Z.