From 3d9890e16a094a961b2b6cd68f085cd4c45f652e Mon Sep 17 00:00:00 2001 From: efischer Date: Thu, 24 Feb 2022 15:32:27 -0600 Subject: [PATCH] Change GraphModificationFunctions to only save edges if graph data is cached --- src/main/java/GraphModificationFunctions.java | 82 ++++++++++++------- src/main/java/Simulator.java | 17 ++-- 2 files changed, 61 insertions(+), 38 deletions(-) diff --git a/src/main/java/GraphModificationFunctions.java b/src/main/java/GraphModificationFunctions.java index 51513f8..a80e9e3 100644 --- a/src/main/java/GraphModificationFunctions.java +++ b/src/main/java/GraphModificationFunctions.java @@ -9,56 +9,71 @@ import java.util.Set; public interface GraphModificationFunctions { //remove over- and under-weight edges - public static List filterByOverlapThresholds(SimpleWeightedGraph graph, - int low, int high) { + static List filterByOverlapThresholds(SimpleWeightedGraph graph, + int low, int high, boolean saveEdges) { List removedEdges = new ArrayList<>(); - for(DefaultWeightedEdge e: graph.edgeSet()){ - if ((graph.getEdgeWeight(e) > high) || (graph.getEdgeWeight(e) < low)){ - Integer source = graph.getEdgeSource(e); - Integer target = graph.getEdgeTarget(e); - Integer weight = (int) graph.getEdgeWeight(e); - Integer[] edge = {source, target, weight}; - removedEdges.add(edge); + for (DefaultWeightedEdge e : graph.edgeSet()) { + if ((graph.getEdgeWeight(e) > high) || (graph.getEdgeWeight(e) < low)) { + if(saveEdges) { + Integer source = graph.getEdgeSource(e); + Integer target = graph.getEdgeTarget(e); + Integer weight = (int) graph.getEdgeWeight(e); + Integer[] edge = {source, target, weight}; + removedEdges.add(edge); + } + else { + graph.setEdgeWeight(e, 0.0); + } } } - for (Integer[] edge : removedEdges) { - graph.removeEdge(edge[0], edge[1]); + if(saveEdges) { + for (Integer[] edge : removedEdges) { + graph.removeEdge(edge[0], edge[1]); + } } return removedEdges; } //Remove edges for pairs with large occupancy discrepancy - public static List filterByRelativeOccupancy(SimpleWeightedGraph graph, + static List filterByRelativeOccupancy(SimpleWeightedGraph graph, Map alphaWellCounts, Map betaWellCounts, Map plateVtoAMap, Map plateVtoBMap, - Integer maxOccupancyDifference) { + Integer maxOccupancyDifference, boolean saveEdges) { List removedEdges = new ArrayList<>(); for (DefaultWeightedEdge e : graph.edgeSet()) { Integer alphaOcc = alphaWellCounts.get(plateVtoAMap.get(graph.getEdgeSource(e))); Integer betaOcc = betaWellCounts.get(plateVtoBMap.get(graph.getEdgeTarget(e))); if (Math.abs(alphaOcc - betaOcc) >= maxOccupancyDifference) { - Integer source = graph.getEdgeSource(e); - Integer target = graph.getEdgeTarget(e); - Integer weight = (int) graph.getEdgeWeight(e); - Integer[] edge = {source, target, weight}; - removedEdges.add(edge); + if (saveEdges) { + Integer source = graph.getEdgeSource(e); + Integer target = graph.getEdgeTarget(e); + Integer weight = (int) graph.getEdgeWeight(e); + Integer[] edge = {source, target, weight}; + removedEdges.add(edge); + } + else { + graph.setEdgeWeight(e, 0.0); + } } } - for (Integer[] edge : removedEdges) { - graph.removeEdge(edge[0], edge[1]); + if(saveEdges) { + for (Integer[] edge : removedEdges) { + graph.removeEdge(edge[0], edge[1]); + } } return removedEdges; } //Remove edges for pairs where overlap size is significantly lower than the well occupancy - public static List filterByOverlapPercent(SimpleWeightedGraph graph, + static List filterByOverlapPercent(SimpleWeightedGraph graph, Map alphaWellCounts, Map betaWellCounts, Map plateVtoAMap, Map plateVtoBMap, - Integer minOverlapPercent) { + Integer minOverlapPercent, + boolean saveEdges) { List removedEdges = new ArrayList<>(); for (DefaultWeightedEdge e : graph.edgeSet()) { Integer alphaOcc = alphaWellCounts.get(plateVtoAMap.get(graph.getEdgeSource(e))); @@ -66,20 +81,27 @@ public interface GraphModificationFunctions { double weight = graph.getEdgeWeight(e); double min = minOverlapPercent / 100.0; if ((weight / alphaOcc < min) || (weight / betaOcc < min)) { - Integer source = graph.getEdgeSource(e); - Integer target = graph.getEdgeTarget(e); - Integer intWeight = (int) graph.getEdgeWeight(e); - Integer[] edge = {source, target, intWeight}; - removedEdges.add(edge); + if(saveEdges) { + Integer source = graph.getEdgeSource(e); + Integer target = graph.getEdgeTarget(e); + Integer intWeight = (int) graph.getEdgeWeight(e); + Integer[] edge = {source, target, intWeight}; + removedEdges.add(edge); + } + else { + graph.setEdgeWeight(e, 0.0); + } } } - for (Integer[] edge : removedEdges) { - graph.removeEdge(edge[0], edge[1]); + if(saveEdges) { + for (Integer[] edge : removedEdges) { + graph.removeEdge(edge[0], edge[1]); + } } return removedEdges; } - public static void addRemovedEdges(SimpleWeightedGraph graph, + static void addRemovedEdges(SimpleWeightedGraph graph, List removedEdges) { for (Integer[] edge : removedEdges) { DefaultWeightedEdge e = graph.addEdge(edge[0], edge[1]); diff --git a/src/main/java/Simulator.java b/src/main/java/Simulator.java index 8ed175d..42379c9 100644 --- a/src/main/java/Simulator.java +++ b/src/main/java/Simulator.java @@ -147,8 +147,8 @@ public class Simulator implements GraphModificationFunctions { Integer highThreshold, Integer maxOccupancyDifference, Integer minOverlapPercent, boolean verbose) { Instant start = Instant.now(); - //Integer arrays will contain TO VERTEX, FROM VERTEX, and WEIGHT (which I'll need to cast to double) List removedEdges = new ArrayList<>(); + boolean saveEdges = BiGpairSEQ.cacheGraph(); int numWells = data.getNumWells(); Integer alphaCount = data.getAlphaCount(); Integer betaCount = data.getBetaCount(); @@ -161,21 +161,21 @@ public class Simulator implements GraphModificationFunctions { //remove edges with weights outside given overlap thresholds, add those to removed edge list if(verbose){System.out.println("Eliminating edges with weights outside overlap threshold values");} - removedEdges.addAll(GraphModificationFunctions.filterByOverlapThresholds(graph, lowThreshold, highThreshold)); + removedEdges.addAll(GraphModificationFunctions.filterByOverlapThresholds(graph, lowThreshold, highThreshold, saveEdges)); if(verbose){System.out.println("Over- and under-weight edges removed");} //remove edges between vertices with too small an overlap size, add those to removed edge list if(verbose){System.out.println("Eliminating edges with weights less than " + minOverlapPercent.toString() + " percent of vertex occupancy value.");} removedEdges.addAll(GraphModificationFunctions.filterByOverlapPercent(graph, alphaWellCounts, betaWellCounts, - plateVtoAMap, plateVtoBMap, minOverlapPercent)); + plateVtoAMap, plateVtoBMap, minOverlapPercent, saveEdges)); if(verbose){System.out.println("Edges with weights too far below a vertex occupancy value removed");} //Filter by relative occupancy if(verbose){System.out.println("Eliminating edges between vertices with occupancy difference > " + maxOccupancyDifference);} removedEdges.addAll(GraphModificationFunctions.filterByRelativeOccupancy(graph, alphaWellCounts, betaWellCounts, - plateVtoAMap, plateVtoBMap, maxOccupancyDifference)); + plateVtoAMap, plateVtoBMap, maxOccupancyDifference, saveEdges)); if(verbose){System.out.println("Edges between vertices of with excessively different occupancy values " + "removed");} @@ -310,10 +310,11 @@ public class Simulator implements GraphModificationFunctions { } } - //put the removed edges back on the graph - System.out.println("Restoring removed edges to graph."); - GraphModificationFunctions.addRemovedEdges(graph, removedEdges); - + if(saveEdges) { + //put the removed edges back on the graph + System.out.println("Restoring removed edges to graph."); + GraphModificationFunctions.addRemovedEdges(graph, removedEdges); + } //return MatchingResult object return output; }