From 26304e5dffa90a9fe2dd6e7e16dac8d921cfe472 Mon Sep 17 00:00:00 2001 From: efischer Date: Tue, 16 Nov 2021 09:29:07 -0600 Subject: [PATCH] bugfix --- src/main/java/Plate.java | 2 + src/main/java/Simulator.java | 104 ++++++++++++++++++++++++++----- src/main/java/UserInterface.java | 9 ++- 3 files changed, 98 insertions(+), 17 deletions(-) diff --git a/src/main/java/Plate.java b/src/main/java/Plate.java index 9bc447b..a33c769 100644 --- a/src/main/java/Plate.java +++ b/src/main/java/Plate.java @@ -28,6 +28,8 @@ public class Plate { int section = 0; double m; int n; + //testing + //System.out.println("Cell size: " + cells.get(0).length); while (section < numSections){ for (int i = 0; i < (size / numSections); i++) { List well = new ArrayList<>(); diff --git a/src/main/java/Simulator.java b/src/main/java/Simulator.java index 48815fe..ac54a77 100644 --- a/src/main/java/Simulator.java +++ b/src/main/java/Simulator.java @@ -231,7 +231,7 @@ public class Simulator { return new MatchingResult(comments, header, allResults, matchMap); } - public static MatchingResult matchCDR1s(List distinctCells, + public static MatchingResult[] matchCDR1s(List distinctCells, Plate samplePlate, Integer lowThreshold, Integer highThreshold, Map previousMatches){ int numWells = samplePlate.getSize(); @@ -256,9 +256,9 @@ public class Simulator { Map allCDR3s = samplePlate.assayWellsCDR3(); Map allCDR1s = samplePlate.assayWellsCDR1(); int CDR3Count = allCDR3s.size(); - System.out.println("all alphas count: " + CDR3Count); + System.out.println("all CDR3s count: " + CDR3Count); int CDR1Count = allCDR1s.size(); - System.out.println("all betas count: " + CDR1Count); + System.out.println("all CDR1s count: " + CDR1Count); System.out.println("Well maps made"); System.out.println("Making vertex maps"); @@ -279,9 +279,9 @@ public class Simulator { System.out.println("Vertex maps made"); System.out.println("Creating Graph"); - //Count how many wells each alpha appears in + //Count how many wells each CDR3 appears in Map CDR3WellCounts = new HashMap<>(); - //count how many wells each beta appears in + //count how many wells each CDR1 appears in Map CDR1WellCounts = new HashMap<>(); //add edges, where weights are number of wells the peptides share in common. //If this is too slow, can make a 2d array and use the SimpleWeightedGraphMatrixGenerator class @@ -300,8 +300,10 @@ public class Simulator { CDR1WellCounts.merge(b, 1, (oldValue, newValue) -> oldValue + newValue); } for (Integer i : wellNCDR3s.keySet()) { - for (Integer j : wellNCDR1s.keySet()) { - weights[plateCDR3toVMap.get(i)][plateCDR1toVMap.get(j) - vertexStartValue] += 1.0; + if(CDR3AtoBMap.containsKey(i)||CDR3BtoAMap.containsKey(i)){//only consider ones we have matches for + for (Integer j : wellNCDR1s.keySet()) { + weights[plateCDR3toVMap.get(i)][plateCDR1toVMap.get(j) - vertexStartValue] += 1.0; + } } } } @@ -315,6 +317,7 @@ public class Simulator { graphGenerator.weights(weights); graphGenerator.generateGraph(graph); System.out.println("Graph created"); + System.out.println("Number of edges: " + graph.edgeSet().size()); System.out.println("Finding first maximum weighted matching"); MaximumWeightBipartiteMatching firstMaxWeightMatching = @@ -333,13 +336,15 @@ public class Simulator { continue; } Integer source = graph.getEdgeSource(e); - if(!(CDR3AtoBMap.containsKey(source) || CDR3BtoAMap.containsKey(source))){ - continue; - } +// if(!(CDR3AtoBMap.containsKey(source) || CDR3BtoAMap.containsKey(source))){ +// continue; +// } Integer target = graph.getEdgeTarget(e); firstMatchCDR3toCDR1Map.put(plateVtoCDR3Map.get(source), plateVtoCDR1Map.get(target)); } + System.out.println("First pass matches: " + firstMatchCDR3toCDR1Map.size()); + //zero out the edge weights in the matching weightIter = graphMatching.iterator(); while(weightIter.hasNext()){ @@ -353,6 +358,7 @@ public class Simulator { graphMatching = secondMaxWeightMatching.getMatching(); System.out.println("Second maximum weighted matching found"); + //second processing run Map secondMatchCDR3toCDR1Map = new HashMap<>(); weightIter = graphMatching.iterator(); @@ -362,13 +368,31 @@ public class Simulator { continue; } Integer source = graph.getEdgeSource(e); - if(!(CDR3AtoBMap.containsKey(source) || CDR3BtoAMap.containsKey(source))){ - continue; - } +// if(!(CDR3AtoBMap.containsKey(source) || CDR3BtoAMap.containsKey(source))){ +// continue; +// } Integer target = graph.getEdgeTarget(e); secondMatchCDR3toCDR1Map.put(plateVtoCDR3Map.get(source), plateVtoCDR1Map.get(target)); } + System.out.println("Second pass matches: " + secondMatchCDR3toCDR1Map.size()); + + + //get linked map for first matching attempt + Map firstMatchesMap = new LinkedHashMap<>(); + for(Integer alphaCDR3: CDR3AtoBMap.keySet()) { + if (!(firstMatchCDR3toCDR1Map.containsKey(alphaCDR3))) { + continue; + } + Integer betaCDR3 = CDR3AtoBMap.get(alphaCDR3); + if (!(firstMatchCDR3toCDR1Map.containsKey(betaCDR3))) { + continue; + } + firstMatchesMap.put(alphaCDR3, firstMatchCDR3toCDR1Map.get(alphaCDR3)); + firstMatchesMap.put(betaCDR3, firstMatchCDR3toCDR1Map.get(betaCDR3)); + } + + //Look for matches that simply swapped already-matched alpha and beta CDR3s Map dualMatchesMap = new LinkedHashMap<>(); for(Integer alphaCDR3: CDR3AtoBMap.keySet()) { @@ -387,6 +411,7 @@ public class Simulator { } } + //results for dual map List> allResults = new ArrayList<>(); Integer trueCount = 0; Iterator iter = dualMatchesMap.keySet().iterator(); @@ -415,6 +440,7 @@ public class Simulator { } List comments = new ArrayList<>(); + comments.add("Plate size: " + samplePlate.getSize() + " wells"); comments.add("Previous pairs found: " + previousMatches.size()); comments.add("CDR1 matches attempted: " + allResults.size()); double attemptRate = (double) allResults.size() / previousMatches.size(); @@ -430,7 +456,57 @@ public class Simulator { headers.add("second matched CDR1"); headers.add("Correct match?"); - return new MatchingResult(comments, headers, allResults, dualMatchesMap); + for(String s: comments){ + System.out.println(s); + } + + MatchingResult dualTest = new MatchingResult(comments, headers, allResults, dualMatchesMap); + + + //results for first map + allResults = new ArrayList<>(); + trueCount = 0; + iter = firstMatchesMap.keySet().iterator(); + while(iter.hasNext()){ + Boolean proven = false; + List tmp = new ArrayList<>(); + tmp.add(iter.next().toString()); + tmp.add(iter.next().toString()); + tmp.add(firstMatchesMap.get(Integer.valueOf(tmp.get(0))).toString()); + tmp.add(firstMatchesMap.get(Integer.valueOf(tmp.get(1))).toString()); + if(alphaCDR3toCDR1Map.get(Integer.valueOf(tmp.get(0))).equals(Integer.valueOf(tmp.get(2)))){ + if(betaCDR3toCDR1Map.get(Integer.valueOf(tmp.get(1))).equals(Integer.valueOf(tmp.get(3)))){ + proven = true; + } + } + else if(alphaCDR3toCDR1Map.get(Integer.valueOf(tmp.get(0))).equals(Integer.valueOf(tmp.get(3)))){ + if(betaCDR3toCDR1Map.get(Integer.valueOf(tmp.get(1))).equals(Integer.valueOf(tmp.get(2)))){ + proven = true; + } + } + tmp.add(proven.toString()); + allResults.add(tmp); + if(proven){ + trueCount++; + } + } + + comments = new ArrayList<>(); + comments.add("Plate size: " + samplePlate.getSize() + " wells"); + comments.add("Previous pairs found: " + previousMatches.size()); + comments.add("CDR1 matches attempted: " + allResults.size()); + attemptRate = (double) allResults.size() / previousMatches.size(); + comments.add("Matching attempt rate: " + attemptRate); + comments.add("Number of correct matches: " + trueCount); + correctRate = (double) trueCount / allResults.size(); + comments.add("Correct matching rate: " + correctRate); + + for(String s: comments){ + System.out.println(s); + } + MatchingResult firstTest = new MatchingResult(comments, headers, allResults, dualMatchesMap); + MatchingResult[] output = {firstTest, dualTest}; + return output; } diff --git a/src/main/java/UserInterface.java b/src/main/java/UserInterface.java index 6f21711..a875dbb 100644 --- a/src/main/java/UserInterface.java +++ b/src/main/java/UserInterface.java @@ -255,12 +255,15 @@ public class UserInterface { } List cells = cellReader.getCells(); MatchingResult preliminaryResults = Simulator.matchCDR3s(cells, plate, lowThresholdCDR3, highThresholdCDR3); - MatchingResult results = Simulator.matchCDR1s(cells, plate, lowThresholdCDR1, + MatchingResult[] results = Simulator.matchCDR1s(cells, plate, lowThresholdCDR1, highThresholdCDR1, preliminaryResults.getMatchMap()); //result writer - MatchingFileWriter writer = new MatchingFileWriter(filename, results.getComments(), - results.getHeaders(), results.getAllResults()); + MatchingFileWriter writer = new MatchingFileWriter(filename + "First", results[0].getComments(), + results[0].getHeaders(), results[0].getAllResults()); + writer.writeResultsToFile(); + writer = new MatchingFileWriter(filename + "Dual", results[1].getComments(), + results[1].getHeaders(), results[1].getAllResults()); writer.writeResultsToFile(); } }