Add read counts for individual wells to graphml output

This commit is contained in:
eugenefischer
2022-09-28 13:38:38 -05:00
parent deed98e79d
commit a88cfb8b0d

View File

@@ -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<String, Attribute> createGraphAttributes(){
Map<String, Attribute> ga = new HashMap<>();
Map<String, Attribute> 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<String, Attribute> createVertexAttributes(Vertex v){
Map<String, Attribute> 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<Map.Entry<Integer, Integer>> wellOccupancies = v.getWellOccupancies().entrySet().iterator();
while (wellOccupancies.hasNext()) {
Map.Entry<Integer, Integer> 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<String, Attribute> 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){