Fix GraphML writer
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
|
import org.jgrapht.graph.DefaultWeightedEdge;
|
||||||
import org.jgrapht.graph.SimpleWeightedGraph;
|
import org.jgrapht.graph.SimpleWeightedGraph;
|
||||||
|
import org.jgrapht.nio.Attribute;
|
||||||
|
import org.jgrapht.nio.AttributeType;
|
||||||
|
import org.jgrapht.nio.DefaultAttribute;
|
||||||
import org.jgrapht.nio.dot.DOTExporter;
|
import org.jgrapht.nio.dot.DOTExporter;
|
||||||
import org.jgrapht.nio.graphml.GraphMLExporter;
|
import org.jgrapht.nio.graphml.GraphMLExporter;
|
||||||
|
|
||||||
@@ -7,25 +11,69 @@ import java.io.IOException;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class GraphMLFileWriter {
|
public class GraphMLFileWriter {
|
||||||
|
|
||||||
String filename;
|
String filename;
|
||||||
SimpleWeightedGraph graph;
|
GraphWithMapData data;
|
||||||
|
|
||||||
|
|
||||||
public GraphMLFileWriter(String filename, SimpleWeightedGraph graph) {
|
public GraphMLFileWriter(String filename, GraphWithMapData data) {
|
||||||
if(!filename.matches(".*\\.graphml")){
|
if(!filename.matches(".*\\.graphml")){
|
||||||
filename = filename + ".graphml";
|
filename = filename + ".graphml";
|
||||||
}
|
}
|
||||||
this.filename = filename;
|
this.filename = filename;
|
||||||
this.graph = graph;
|
this.data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// public void writeGraphToFile() {
|
||||||
|
// try(BufferedWriter writer = Files.newBufferedWriter(Path.of(filename), StandardOpenOption.CREATE_NEW);
|
||||||
|
// ){
|
||||||
|
// GraphMLExporter<SimpleWeightedGraph, BufferedWriter> exporter = new GraphMLExporter<>();
|
||||||
|
// exporter.exportGraph(graph, writer);
|
||||||
|
// } catch(IOException ex){
|
||||||
|
// System.out.println("Could not make new file named "+filename);
|
||||||
|
// System.err.println(ex);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
public void writeGraphToFile() {
|
public void writeGraphToFile() {
|
||||||
|
SimpleWeightedGraph graph = data.getGraph();
|
||||||
|
Map<Integer, Integer> vertexToAlphaMap = data.getPlateVtoAMap();
|
||||||
|
Map<Integer, Integer> vertexToBetaMap = data.getPlateVtoBMap();
|
||||||
|
Map<Integer, Integer> alphaOccs = data.getAlphaWellCounts();
|
||||||
|
Map<Integer, Integer> betaOccs = data.getBetaWellCounts();
|
||||||
try(BufferedWriter writer = Files.newBufferedWriter(Path.of(filename), StandardOpenOption.CREATE_NEW);
|
try(BufferedWriter writer = Files.newBufferedWriter(Path.of(filename), StandardOpenOption.CREATE_NEW);
|
||||||
){
|
){
|
||||||
GraphMLExporter<SimpleWeightedGraph, BufferedWriter> exporter = new GraphMLExporter<>();
|
//create exporter. Let the vertex labels be the unique ids for the vertices
|
||||||
|
GraphMLExporter<Integer, SimpleWeightedGraph<Vertex, DefaultWeightedEdge>> exporter = new GraphMLExporter<>(v -> v.toString());
|
||||||
|
//set to export weights
|
||||||
|
exporter.setExportEdgeWeights(true);
|
||||||
|
//set type, sequence, and occupancy attributes for each vertex
|
||||||
|
exporter.setVertexAttributeProvider( v -> {
|
||||||
|
Map<String, Attribute> attributes = new HashMap<>();
|
||||||
|
if(vertexToAlphaMap.containsKey(v)) {
|
||||||
|
attributes.put("type", DefaultAttribute.createAttribute("CDR3 Alpha"));
|
||||||
|
attributes.put("sequence", DefaultAttribute.createAttribute(vertexToAlphaMap.get(v)));
|
||||||
|
attributes.put("occupancy", DefaultAttribute.createAttribute(
|
||||||
|
alphaOccs.get(vertexToAlphaMap.get(v))));
|
||||||
|
}
|
||||||
|
else if(vertexToBetaMap.containsKey(v)) {
|
||||||
|
attributes.put("type", DefaultAttribute.createAttribute("CDR3 Beta"));
|
||||||
|
attributes.put("sequence", DefaultAttribute.createAttribute(vertexToBetaMap.get(v)));
|
||||||
|
attributes.put("occupancy", DefaultAttribute.createAttribute(
|
||||||
|
betaOccs.get(vertexToBetaMap.get(v))));
|
||||||
|
}
|
||||||
|
return attributes;
|
||||||
|
});
|
||||||
|
//register the attributes
|
||||||
|
exporter.registerAttribute("type", GraphMLExporter.AttributeCategory.NODE, AttributeType.STRING);
|
||||||
|
exporter.registerAttribute("sequence", GraphMLExporter.AttributeCategory.NODE, AttributeType.STRING);
|
||||||
|
exporter.registerAttribute("occupancy", GraphMLExporter.AttributeCategory.NODE, AttributeType.STRING);
|
||||||
|
//export the graph
|
||||||
exporter.exportGraph(graph, writer);
|
exporter.exportGraph(graph, writer);
|
||||||
} catch(IOException ex){
|
} catch(IOException ex){
|
||||||
System.out.println("Could not make new file named "+filename);
|
System.out.println("Could not make new file named "+filename);
|
||||||
@@ -33,3 +81,4 @@ public class GraphMLFileWriter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -315,7 +315,7 @@ public class InteractiveInterface {
|
|||||||
System.out.println("Serialized binary graph/data file written to: " + filename);
|
System.out.println("Serialized binary graph/data file written to: " + filename);
|
||||||
}
|
}
|
||||||
if(BiGpairSEQ.outputGraphML()) {
|
if(BiGpairSEQ.outputGraphML()) {
|
||||||
GraphMLFileWriter graphMLWriter = new GraphMLFileWriter(filename, data.getGraph());
|
GraphMLFileWriter graphMLWriter = new GraphMLFileWriter(filename, data);
|
||||||
graphMLWriter.writeGraphToFile();
|
graphMLWriter.writeGraphToFile();
|
||||||
System.out.println("GraphML file written to: " + filename);
|
System.out.println("GraphML file written to: " + filename);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user