From 75b2aa955301a585c52609ada8d907de0cfbc874 Mon Sep 17 00:00:00 2001 From: efischer Date: Sat, 26 Feb 2022 08:58:52 -0600 Subject: [PATCH] testing graph attributes --- src/main/java/BiGpairSEQ.java | 4 ++-- src/main/java/GraphMLFileReader.java | 14 ++++++++++++-- src/main/java/GraphMLFileWriter.java | 18 ++++++++++++------ src/main/java/InteractiveInterface.java | 2 +- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/main/java/BiGpairSEQ.java b/src/main/java/BiGpairSEQ.java index 5a894ed..23798b7 100644 --- a/src/main/java/BiGpairSEQ.java +++ b/src/main/java/BiGpairSEQ.java @@ -14,8 +14,8 @@ public class BiGpairSEQ { private static boolean cachePlate = false; private static boolean cacheGraph = false; private static HeapType priorityQueueHeapType = HeapType.FIBONACCI; - private static boolean outputBinary = false; - private static boolean outputGraphML = true; + private static boolean outputBinary = true; + private static boolean outputGraphML = false; public static void main(String[] args) { if (args.length == 0) { diff --git a/src/main/java/GraphMLFileReader.java b/src/main/java/GraphMLFileReader.java index fc75f3b..9b9d550 100644 --- a/src/main/java/GraphMLFileReader.java +++ b/src/main/java/GraphMLFileReader.java @@ -6,11 +6,14 @@ import java.io.BufferedReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; public class GraphMLFileReader { - private String filename; - private SimpleWeightedGraph graph; + private final String filename; + private final SimpleWeightedGraph graph; + private final Map graphAttributes = new HashMap<>(); public GraphMLFileReader(String filename, SimpleWeightedGraph graph) { if(!filename.matches(".*\\.graphml")){ @@ -23,6 +26,9 @@ public class GraphMLFileReader { BufferedReader reader = Files.newBufferedReader(Path.of(filename)); ){ GraphMLImporter importer = new GraphMLImporter<>(); + importer.addGraphAttributeConsumer((str, attribute) -> { + graphAttributes.put(str, attribute.getValue()); + }); importer.addVertexWithAttributesConsumer((vertex, attributes) -> { vertex.setType(attributes.get("type").getValue()); vertex.setSequence(attributes.get("sequence").getValue()); @@ -38,4 +44,8 @@ public class GraphMLFileReader { public SimpleWeightedGraph getGraph() { return graph; } + public Map getGraphAttributes() { return graphAttributes; } + + public String getFilename() {return filename;} + } diff --git a/src/main/java/GraphMLFileWriter.java b/src/main/java/GraphMLFileWriter.java index f357828..3d6c8fb 100644 --- a/src/main/java/GraphMLFileWriter.java +++ b/src/main/java/GraphMLFileWriter.java @@ -5,6 +5,7 @@ import org.jgrapht.nio.AttributeType; import org.jgrapht.nio.DefaultAttribute; import org.jgrapht.nio.graphml.GraphMLExporter; import org.jgrapht.nio.graphml.GraphMLExporter.AttributeCategory; +import org.w3c.dom.Attr; import java.io.BufferedWriter; import java.io.IOException; @@ -19,7 +20,7 @@ public class GraphMLFileWriter { String filename; SimpleWeightedGraph graph; GraphWithMapData data; - Map graphAttributes = new HashMap<>(); + Map graphAttributes; public GraphMLFileWriter(String filename, GraphWithMapData data) { if(!filename.matches(".*\\.graphml")){ @@ -28,7 +29,7 @@ public class GraphMLFileWriter { this.filename = filename; this.data = data; this.graph = data.getGraph(); - createGraphAttributes(); + graphAttributes = createGraphAttributes(); } public GraphMLFileWriter(String filename, SimpleWeightedGraph graph) { @@ -39,11 +40,12 @@ public class GraphMLFileWriter { this.graph = graph; } - private void createGraphAttributes(){ + private Map createGraphAttributes(){ + Map ga = new HashMap<>(); //Sample plate filename - graphAttributes.put("sample plate filename", DefaultAttribute.createAttribute(data.getSourceFilename())); + ga.put("sample plate filename", DefaultAttribute.createAttribute(data.getSourceFilename())); // Number of wells - graphAttributes.put("well count", DefaultAttribute.createAttribute(data.getNumWells().toString())); + ga.put("well count", DefaultAttribute.createAttribute(data.getNumWells().toString())); //Well populations Integer[] wellPopulations = data.getWellPopulations(); StringBuilder populationsStringBuilder = new StringBuilder(); @@ -53,7 +55,8 @@ public class GraphMLFileWriter { populationsStringBuilder.append(wellPopulations[i].toString()); } String wellPopulationsString = populationsStringBuilder.toString(); - graphAttributes.put("well populations", DefaultAttribute.createAttribute(wellPopulationsString)); + ga.put("well populations", DefaultAttribute.createAttribute(wellPopulationsString)); + return ga; } public void writeGraphToFile() { @@ -74,6 +77,9 @@ public class GraphMLFileWriter { return attributes; }); //register the attributes + for(String s : graphAttributes.keySet()) { + exporter.registerAttribute(s, AttributeCategory.GRAPH, AttributeType.STRING); + } exporter.registerAttribute("type", AttributeCategory.NODE, AttributeType.STRING); exporter.registerAttribute("sequence", AttributeCategory.NODE, AttributeType.STRING); exporter.registerAttribute("occupancy", AttributeCategory.NODE, AttributeType.STRING); diff --git a/src/main/java/InteractiveInterface.java b/src/main/java/InteractiveInterface.java index 0ab30b2..776f433 100644 --- a/src/main/java/InteractiveInterface.java +++ b/src/main/java/InteractiveInterface.java @@ -315,7 +315,7 @@ public class InteractiveInterface { System.out.println("Serialized binary graph/data file written to: " + filename); } if(BiGpairSEQ.outputGraphML()) { - GraphMLFileWriter graphMLWriter = new GraphMLFileWriter(filename, data.getGraph()); + GraphMLFileWriter graphMLWriter = new GraphMLFileWriter(filename, data); graphMLWriter.writeGraphToFile(); System.out.println("GraphML file written to: " + filename); }