From a88cfb8b0d5ccac6e04925cbb614ae7baabab0aa Mon Sep 17 00:00:00 2001 From: eugenefischer <66030419+eugenefischer@users.noreply.github.com> Date: Wed, 28 Sep 2022 13:38:38 -0500 Subject: [PATCH] Add read counts for individual wells to graphml output --- src/main/java/GraphMLFileWriter.java | 56 ++++++++++++++++++---------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/src/main/java/GraphMLFileWriter.java b/src/main/java/GraphMLFileWriter.java index 8546061..89de8a8 100644 --- a/src/main/java/GraphMLFileWriter.java +++ b/src/main/java/GraphMLFileWriter.java @@ -5,7 +5,6 @@ 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; @@ -13,6 +12,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; public class GraphMLFileWriter { @@ -41,11 +41,11 @@ public class GraphMLFileWriter { } private Map createGraphAttributes(){ - Map ga = new HashMap<>(); + Map attributes = new HashMap<>(); //Sample plate filename - ga.put("sample plate filename", DefaultAttribute.createAttribute(data.getSourceFilename())); + attributes.put("sample plate filename", DefaultAttribute.createAttribute(data.getSourceFilename())); // Number of wells - ga.put("well count", DefaultAttribute.createAttribute(data.getNumWells().toString())); + attributes.put("well count", DefaultAttribute.createAttribute(data.getNumWells().toString())); //Well populations Integer[] wellPopulations = data.getWellPopulations(); StringBuilder populationsStringBuilder = new StringBuilder(); @@ -55,11 +55,36 @@ public class GraphMLFileWriter { populationsStringBuilder.append(wellPopulations[i].toString()); } String wellPopulationsString = populationsStringBuilder.toString(); - ga.put("well populations", DefaultAttribute.createAttribute(wellPopulationsString)); - ga.put("read depth", DefaultAttribute.createAttribute(data.getReadDepth().toString())); - ga.put("read error rate", DefaultAttribute.createAttribute(data.getReadErrorRate().toString())); - ga.put("error collision rate", DefaultAttribute.createAttribute(data.getErrorCollisionRate().toString())); - return ga; + attributes.put("well populations", DefaultAttribute.createAttribute(wellPopulationsString)); + attributes.put("read depth", DefaultAttribute.createAttribute(data.getReadDepth().toString())); + attributes.put("read error rate", DefaultAttribute.createAttribute(data.getReadErrorRate().toString())); + attributes.put("error collision rate", DefaultAttribute.createAttribute(data.getErrorCollisionRate().toString())); + return attributes; + } + + private Map createVertexAttributes(Vertex v){ + Map attributes = new HashMap<>(); + //sequence type + attributes.put("type", DefaultAttribute.createAttribute(v.getType().name())); + //sequence + attributes.put("sequence", DefaultAttribute.createAttribute(v.getSequence())); + //number of wells the sequence appears in + attributes.put("occupancy", DefaultAttribute.createAttribute(v.getOccupancy())); + //total number of times the sequence was read + attributes.put("total read count", DefaultAttribute.createAttribute(v.getReadCount())); + StringBuilder wellsAndReadCountsBuilder = new StringBuilder(); + Iterator> wellOccupancies = v.getWellOccupancies().entrySet().iterator(); + while (wellOccupancies.hasNext()) { + Map.Entry entry = wellOccupancies.next(); + wellsAndReadCountsBuilder.append(entry.getKey() + ":" + entry.getValue()); + if (wellOccupancies.hasNext()) { + wellsAndReadCountsBuilder.append(", "); + } + } + String wellsAndReadCounts = wellsAndReadCountsBuilder.toString(); + //the wells the sequence appears in and the read counts in those wells + attributes.put("wells:read counts", DefaultAttribute.createAttribute(wellsAndReadCounts)); + return attributes; } public void writeGraphToFile() { @@ -72,15 +97,7 @@ public class GraphMLFileWriter { //Set graph attributes exporter.setGraphAttributeProvider( () -> graphAttributes); //set type, sequence, and occupancy attributes for each vertex - //NEED TO ADD NEW FIELD FOR READ COUNT - exporter.setVertexAttributeProvider( v -> { - Map attributes = new HashMap<>(); - attributes.put("type", DefaultAttribute.createAttribute(v.getType().name())); - attributes.put("sequence", DefaultAttribute.createAttribute(v.getSequence())); - attributes.put("occupancy", DefaultAttribute.createAttribute(v.getOccupancy())); - attributes.put("read count", DefaultAttribute.createAttribute(v.getReadCount())); - return attributes; - }); + exporter.setVertexAttributeProvider(this::createVertexAttributes); //register the attributes for(String s : graphAttributes.keySet()) { exporter.registerAttribute(s, AttributeCategory.GRAPH, AttributeType.STRING); @@ -88,7 +105,8 @@ public class GraphMLFileWriter { exporter.registerAttribute("type", AttributeCategory.NODE, AttributeType.STRING); exporter.registerAttribute("sequence", AttributeCategory.NODE, AttributeType.STRING); exporter.registerAttribute("occupancy", AttributeCategory.NODE, AttributeType.STRING); - exporter.registerAttribute("read count", AttributeCategory.NODE, AttributeType.STRING); + exporter.registerAttribute("total read count", AttributeCategory.NODE, AttributeType.STRING); + exporter.registerAttribute("wells:read counts", AttributeCategory.NODE, AttributeType.STRING); //export the graph exporter.exportGraph(graph, writer); } catch(IOException ex){