Skip to content

Commit

Permalink
Fixed graph comparison bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
kvb2univpitt committed May 25, 2016
1 parent 2f73ecf commit 928a38b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public class SimpleGraphComparison {

private final Set<String> edgesNotInAll;

private final Set<String> sameEndPoints;
private final Set<String> 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<SimpleGraph> simpleGraphs) {
Expand All @@ -53,6 +53,8 @@ public void compare(List<SimpleGraph> simpleGraphs) {
}

Pattern delimiter = Pattern.compile(",");

// gather all the edges in the graph together
simpleGraphs.forEach(simpleGraph -> {
List<String> edges = simpleGraph.getEdges();
edges.forEach(edge -> {
Expand All @@ -66,34 +68,47 @@ public void compare(List<SimpleGraph> simpleGraphs) {
});
});

// determine if each distinct edge is in all graph
distinctEdges.forEach(edge -> {
boolean inAll = true;
for (SimpleGraph simpleGraph : simpleGraphs) {
Map<String, String> 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<String, String> 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<String, String> 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);
}
});
}
Expand All @@ -110,8 +125,8 @@ public Set<String> getEdgesNotInAll() {
return edgesNotInAll;
}

public Set<String> getSameEndPoints() {
return sameEndPoints;
public Set<String> getSameEdgeTypes() {
return sameEdgeTypes;
}

}
40 changes: 21 additions & 19 deletions src/main/java/edu/pitt/dbmi/ccd/commons/graph/SimpleGraphUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> edges = simpleGraph.getEdges();
Map<String, String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void testCompare() throws Exception {
Set<String> distinctEdges = simpleGraphComparison.getDistinctEdges();
Set<String> edgesInAll = simpleGraphComparison.getEdgesInAll();
Set<String> edgesNotInAll = simpleGraphComparison.getEdgesNotInAll();
Set<String> sameEndPoints = simpleGraphComparison.getSameEndPoints();
Set<String> sameEdgeTypes = simpleGraphComparison.getSameEdgeTypes();

List<String> results = new LinkedList<>();
distinctEdges.forEach(edge -> {
Expand All @@ -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(", ");
Expand Down

0 comments on commit 928a38b

Please sign in to comment.