Skip to content
Snippets Groups Projects

Draft: construct pangenome database from GFA

Open Workum, Dirk-Jan van requested to merge add_gfa_build_pangenome into add_gfa_export
2 files
+ 49
24
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -11,11 +11,12 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;
public class GfaReader {
private final Path gfaFile;
private final Map<Long, GraphNode> vertices = new HashMap<>();
private final Map<Long, GraphEdge> edges = new HashMap<>();
private final Map<EdgeKey, GraphEdge> edges = new HashMap<>();
private final List<GraphPath> paths = new ArrayList<>();
public GfaReader(Path gfaFile) throws IOException {
@@ -96,8 +97,7 @@ public class GfaReader {
final GraphNode source = this.vertices.get(sourceId);
final char sourceOrientation = steps[j - 1].charAt(steps[j - 1].length() - 1);
final char orientation = step.charAt(step.length() - 1);
final GraphEdge edgeId = findGraphEdge(source, sourceOrientation, vertex, orientation);
final GraphEdge pathGraphEdge = this.edges.get(edgeId.getId());
final GraphEdge pathGraphEdge = findGraphEdge(source, sourceOrientation, vertex, orientation);
pathGraphEdges.add(pathGraphEdge);
}
}
@@ -121,7 +121,8 @@ public class GfaReader {
final char targetOrientation = fields[4].charAt(0);
final String overlap = fields[5];
final GraphEdge edge = new GraphEdge(i, this.vertices.get(sourceId), sourceOrientation, this.vertices.get(targetId), targetOrientation, overlap);
this.edges.put(edge.getId(), edge);
EdgeKey key = new EdgeKey(sourceId, sourceOrientation, targetId, targetOrientation);
this.edges.put(key, edge);
i++;
}
}
@@ -141,7 +142,7 @@ public class GfaReader {
}
/**
* Find an edge in the graph.
* Find an edge in the graph using an EdgeKey
* @param source source vertex
* @param sourceOrientation orientation of the source vertex
* @param target target vertex
@@ -149,16 +150,43 @@ public class GfaReader {
* @return the edge
*/
public GraphEdge findGraphEdge(GraphNode source, char sourceOrientation, GraphNode target, char targetOrientation) {
for (GraphEdge edge : this.edges.values()) {
if (edge.getSource().equals(source) &&
edge.getSourceOrientation() == sourceOrientation &&
edge.getTarget().equals(target) &&
edge.getTargetOrientation() == targetOrientation) {
return edge;
}
EdgeKey key = new EdgeKey(source.getId(), sourceOrientation, target.getId(), targetOrientation);
GraphEdge edge = this.edges.get(key);
if (edge == null) {
Pantools.logger.error("GraphEdge {}{} -> {}{} does not exist.", source.getId(), sourceOrientation, target.getId(), targetOrientation);
throw new RuntimeException("GraphEdge " + source.getId() + sourceOrientation + " -> " + target.getId() + targetOrientation + " does not exist");
}
return edge;
}
private static class EdgeKey {
private final long sourceId;
private final char sourceOrientation;
private final long targetId;
private final char targetOrientation;
public EdgeKey(long sourceId, char sourceOrientation, long targetId, char targetOrientation) {
this.sourceId = sourceId;
this.sourceOrientation = sourceOrientation;
this.targetId = targetId;
this.targetOrientation = targetOrientation;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EdgeKey edgeKey = (EdgeKey) o;
return sourceId == edgeKey.sourceId &&
sourceOrientation == edgeKey.sourceOrientation &&
targetId == edgeKey.targetId &&
targetOrientation == edgeKey.targetOrientation;
}
@Override
public int hashCode() {
return Objects.hash(sourceId, sourceOrientation, targetId, targetOrientation);
}
Pantools.logger.error("GraphEdge {}{} -> {}{} does not exist.", source.getId(), sourceOrientation, target.getId(), targetOrientation);
throw new RuntimeException("GraphEdge " + source.getId() + sourceOrientation + " -> " + target.getId() + targetOrientation + " does not exist");
}
/**
@@ -184,14 +212,6 @@ public class GfaReader {
throw new RuntimeException("Invalid GFA header");
}
public Map<Long, GraphNode> getVertices() {
return this.vertices;
}
public Map<Long, GraphEdge> getGraphEdges() {
return this.edges;
}
public List<GraphPath> getGraphPaths() {
return this.paths;
}
@@ -205,6 +225,11 @@ public class GfaReader {
}
public Graph getGraph() {
return new Graph(this.vertices, this.edges, new HashSet<>(this.paths));
return new Graph(this.vertices,
this.edges.values()
.stream()
.collect(Collectors.toMap(GraphEdge::getId,
edge -> edge)),
new HashSet<>(this.paths));
}
}
Loading