From 928a38b99180bc8a5d4709a2647b5b4010e45f9e Mon Sep 17 00:00:00 2001 From: Kevin Bui Date: Wed, 25 May 2016 16:37:21 -0400 Subject: [PATCH] Fixed graph comparison bugs. --- .../commons/graph/SimpleGraphComparison.java | 57 ++++++++++++------- .../ccd/commons/graph/SimpleGraphUtil.java | 40 ++++++------- .../graph/SimpleGraphComparisonTest.java | 4 +- 3 files changed, 59 insertions(+), 42 deletions(-) diff --git a/src/main/java/edu/pitt/dbmi/ccd/commons/graph/SimpleGraphComparison.java b/src/main/java/edu/pitt/dbmi/ccd/commons/graph/SimpleGraphComparison.java index 9b8fff5..816bbe4 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/commons/graph/SimpleGraphComparison.java +++ b/src/main/java/edu/pitt/dbmi/ccd/commons/graph/SimpleGraphComparison.java @@ -38,13 +38,13 @@ public class SimpleGraphComparison { private final Set edgesNotInAll; - private final Set sameEndPoints; + private final Set sameEdgeTypes; public SimpleGraphComparison() { this.distinctEdges = new HashSet<>(); this.edgesInAll = new HashSet<>(); this.edgesNotInAll = new HashSet<>(); - this.sameEndPoints = new HashSet<>(); + this.sameEdgeTypes = new HashSet<>(); } public void compare(List simpleGraphs) { @@ -53,6 +53,8 @@ public void compare(List simpleGraphs) { } Pattern delimiter = Pattern.compile(","); + + // gather all the edges in the graph together simpleGraphs.forEach(simpleGraph -> { List edges = simpleGraph.getEdges(); edges.forEach(edge -> { @@ -66,34 +68,47 @@ public void compare(List simpleGraphs) { }); }); + // determine if each distinct edge is in all graph distinctEdges.forEach(edge -> { - boolean inAll = true; - for (SimpleGraph simpleGraph : simpleGraphs) { - Map edgeMap = simpleGraph.getEdgeMap(); - inAll = inAll && edgeMap.containsKey(edge); - } + String[] endPoints = delimiter.split(edge); + if (endPoints.length == 2) { + String reverseEdge = endPoints[1] + "," + endPoints[0]; + + boolean inAll = false; + for (SimpleGraph simpleGraph : simpleGraphs) { + Map edgeMap = simpleGraph.getEdgeMap(); + inAll = edgeMap.containsKey(edge) || edgeMap.containsKey(reverseEdge); + if (!inAll) { + break; + } + } - if (inAll) { - edgesInAll.add(edge); - } else { - edgesNotInAll.add(edge); + if (inAll) { + edgesInAll.add(edge); + } else { + edgesNotInAll.add(edge); + } } }); + // determine if each of the edge that is in all of the graphs has the same edge type edgesInAll.forEach(edge -> { - boolean sameEndPoint = true; - String endpoint = null; + boolean sameEdgeType = true; + String edgeType = null; for (SimpleGraph simpleGraph : simpleGraphs) { Map edgeMap = simpleGraph.getEdgeMap(); - String edgeEndPoint = edgeMap.get(edge); - if (endpoint == null) { - endpoint = edgeEndPoint; + String nodeEdgeType = edgeMap.get(edge); + if (edgeType == null) { + edgeType = nodeEdgeType; } else { - sameEndPoint = sameEndPoint && endpoint.equals(edgeEndPoint); + sameEdgeType = edgeType.equals(nodeEdgeType); + if (!sameEdgeType) { + break; + } } } - if (sameEndPoint) { - sameEndPoints.add(edge); + if (sameEdgeType) { + sameEdgeTypes.add(edge); } }); } @@ -110,8 +125,8 @@ public Set getEdgesNotInAll() { return edgesNotInAll; } - public Set getSameEndPoints() { - return sameEndPoints; + public Set getSameEdgeTypes() { + return sameEdgeTypes; } } diff --git a/src/main/java/edu/pitt/dbmi/ccd/commons/graph/SimpleGraphUtil.java b/src/main/java/edu/pitt/dbmi/ccd/commons/graph/SimpleGraphUtil.java index c66ae4e..e4c23d2 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/commons/graph/SimpleGraphUtil.java +++ b/src/main/java/edu/pitt/dbmi/ccd/commons/graph/SimpleGraphUtil.java @@ -38,48 +38,50 @@ private SimpleGraphUtil() { public static SimpleGraph readInSimpleGraph(BufferedReader reader) throws IOException { SimpleGraph simpleGraph = new SimpleGraph(); - CharSequence[] edgeTypes = { + String[] edgeTypes = { "---", "-->", "<--", "<->", "o->", "<-o", "o-o" }; - CharSequence emptyString = ""; List edges = simpleGraph.getEdges(); Map edgeMap = simpleGraph.getEdgeMap(); - Pattern space = Pattern.compile("\\s+"); boolean isData = false; - CharSequence delimiter = ";"; + Pattern space = Pattern.compile("\\s+"); for (String line = reader.readLine(); line != null; line = reader.readLine()) { line = line.trim(); if (isData) { String[] data = space.split(line, 2); if (data.length == 2) { String value = data[1].trim(); - for (CharSequence edgeType : edgeTypes) { - value = value.replace(edgeType, delimiter); + + String edge = ""; + for (String edgeType : edgeTypes) { + if (value.contains(edgeType)) { + edge = edgeType; + break; + } } - String[] nodeNames = value.split(delimiter.toString()); - if (nodeNames.length == 2) { - CharSequence source = nodeNames[0].trim(); - CharSequence target = nodeNames[1].trim(); - String edgeType = value.replace(source, emptyString).replace(target, emptyString).trim(); + String[] values = value.split(edge); + if (values.length == 2) { + String source = values[0].trim(); + String target = values[1].trim(); String forwardEdge = source + "," + target; String reverseEdge = target + "," + source; - switch (edgeType) { + switch (edge) { case "---": - edgeMap.put(forwardEdge, edgeType); - edgeMap.put(reverseEdge, edgeType); + edgeMap.put(forwardEdge, edge); + edgeMap.put(reverseEdge, edge); break; case "<->": - edgeMap.put(forwardEdge, edgeType); - edgeMap.put(reverseEdge, edgeType); + edgeMap.put(forwardEdge, edge); + edgeMap.put(reverseEdge, edge); break; case "o-o": - edgeMap.put(forwardEdge, edgeType); - edgeMap.put(reverseEdge, edgeType); + edgeMap.put(forwardEdge, edge); + edgeMap.put(reverseEdge, edge); break; default: - edgeMap.put(forwardEdge, edgeType); + edgeMap.put(forwardEdge, edge); break; } edges.add(forwardEdge); diff --git a/src/test/java/edu/pitt/dbmi/ccd/commons/graph/SimpleGraphComparisonTest.java b/src/test/java/edu/pitt/dbmi/ccd/commons/graph/SimpleGraphComparisonTest.java index f19336d..55af7ca 100644 --- a/src/test/java/edu/pitt/dbmi/ccd/commons/graph/SimpleGraphComparisonTest.java +++ b/src/test/java/edu/pitt/dbmi/ccd/commons/graph/SimpleGraphComparisonTest.java @@ -73,7 +73,7 @@ public void testCompare() throws Exception { Set distinctEdges = simpleGraphComparison.getDistinctEdges(); Set edgesInAll = simpleGraphComparison.getEdgesInAll(); Set edgesNotInAll = simpleGraphComparison.getEdgesNotInAll(); - Set sameEndPoints = simpleGraphComparison.getSameEndPoints(); + Set sameEdgeTypes = simpleGraphComparison.getSameEdgeTypes(); List results = new LinkedList<>(); distinctEdges.forEach(edge -> { @@ -90,7 +90,7 @@ public void testCompare() throws Exception { sb.append(", "); } - if (sameEndPoints.contains(edge)) { + if (sameEdgeTypes.contains(edge)) { sb.append(",X"); } else { sb.append(", ");