import org.jgrapht.graph.DefaultWeightedEdge; import org.jgrapht.graph.SimpleWeightedGraph; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public interface GraphModificationFunctions { //remove over- and under-weight edges, return removed edges static Map filterByOverlapThresholds(SimpleWeightedGraph graph, int low, int high, boolean saveEdges) { Map removedEdges = new HashMap<>(); for (DefaultWeightedEdge e : graph.edgeSet()) { if ((graph.getEdgeWeight(e) > high) || (graph.getEdgeWeight(e) < low)) { if(saveEdges) { Vertex source = graph.getEdgeSource(e); Vertex target = graph.getEdgeTarget(e); Integer weight = (int) graph.getEdgeWeight(e); Vertex[] edge = {source, target}; removedEdges.put(edge, weight); } else { graph.setEdgeWeight(e, 0.0); } } } if(saveEdges) { for (Vertex[] edge : removedEdges.keySet()) { graph.removeEdge(edge[0], edge[1]); } } return removedEdges; } //Remove edges for pairs with large occupancy discrepancy, return removed edges static Map filterByRelativeOccupancy(SimpleWeightedGraph graph, Integer maxOccupancyDifference, boolean saveEdges) { Map removedEdges = new HashMap<>(); for (DefaultWeightedEdge e : graph.edgeSet()) { Integer alphaOcc = graph.getEdgeSource(e).getOccupancy(); Integer betaOcc = graph.getEdgeTarget(e).getOccupancy(); if (Math.abs(alphaOcc - betaOcc) >= maxOccupancyDifference) { if (saveEdges) { Vertex source = graph.getEdgeSource(e); Vertex target = graph.getEdgeTarget(e); Integer weight = (int) graph.getEdgeWeight(e); Vertex[] edge = {source, target}; removedEdges.put(edge, weight); } else { graph.setEdgeWeight(e, 0.0); } } } if(saveEdges) { for (Vertex[] edge : removedEdges.keySet()) { graph.removeEdge(edge[0], edge[1]); } } return removedEdges; } //Remove edges for pairs where overlap size is significantly lower than the well occupancy, return removed edges static Map filterByOverlapPercent(SimpleWeightedGraph graph, Integer minOverlapPercent, boolean saveEdges) { Map removedEdges = new HashMap<>(); for (DefaultWeightedEdge e : graph.edgeSet()) { Integer alphaOcc = graph.getEdgeSource(e).getOccupancy(); Integer betaOcc = graph.getEdgeTarget(e).getOccupancy(); double weight = graph.getEdgeWeight(e); double min = minOverlapPercent / 100.0; if ((weight / alphaOcc < min) || (weight / betaOcc < min)) { if (saveEdges) { Vertex source = graph.getEdgeSource(e); Vertex target = graph.getEdgeTarget(e); Integer intWeight = (int) graph.getEdgeWeight(e); Vertex[] edge = {source, target}; removedEdges.put(edge, intWeight); } else { graph.setEdgeWeight(e, 0.0); } } } if(saveEdges) { for (Vertex[] edge : removedEdges.keySet()) { graph.removeEdge(edge[0], edge[1]); } } return removedEdges; } static Map filterByRelativeReadCount (SimpleWeightedGraph graph, Integer threshold, boolean saveEdges) { Map removedEdges = new HashMap<>(); Boolean passes; for (DefaultWeightedEdge e : graph.edgeSet()) { Integer alphaReadCount = graph.getEdgeSource(e).getReadCount(); Integer betaReadCount = graph.getEdgeTarget(e).getReadCount(); passes = RelativeReadCountFilterFunction(threshold, alphaReadCount, betaReadCount); if (!passes) { if (saveEdges) { Vertex source = graph.getEdgeSource(e); Vertex target = graph.getEdgeTarget(e); Integer intWeight = (int) graph.getEdgeWeight(e); Vertex[] edge = {source, target}; removedEdges.put(edge, intWeight); } else { graph.setEdgeWeight(e, 0.0); } } } if(saveEdges) { for (Vertex[] edge : removedEdges.keySet()) { graph.removeEdge(edge[0], edge[1]); } } return removedEdges; } static Boolean RelativeReadCountFilterFunction(Integer threshold, Integer alphaReadCount, Integer betaReadCount) { return Math.abs(alphaReadCount - betaReadCount) < threshold; } static void addRemovedEdges(SimpleWeightedGraph graph, Map removedEdges) { for (Vertex[] edge : removedEdges.keySet()) { DefaultWeightedEdge e = graph.addEdge(edge[0], edge[1]); graph.setEdgeWeight(e, removedEdges.get(edge)); } } }