This commit is contained in:
2021-11-16 09:29:07 -06:00
parent c28378632f
commit 26304e5dff
3 changed files with 98 additions and 17 deletions

View File

@@ -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<Integer[]> well = new ArrayList<>();

View File

@@ -231,7 +231,7 @@ public class Simulator {
return new MatchingResult(comments, header, allResults, matchMap);
}
public static MatchingResult matchCDR1s(List<Integer[]> distinctCells,
public static MatchingResult[] matchCDR1s(List<Integer[]> distinctCells,
Plate samplePlate, Integer lowThreshold,
Integer highThreshold, Map<Integer, Integer> previousMatches){
int numWells = samplePlate.getSize();
@@ -256,9 +256,9 @@ public class Simulator {
Map<Integer, Integer> allCDR3s = samplePlate.assayWellsCDR3();
Map<Integer, Integer> 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<Integer, Integer> CDR3WellCounts = new HashMap<>();
//count how many wells each beta appears in
//count how many wells each CDR1 appears in
Map<Integer, Integer> 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,11 +300,13 @@ public class Simulator {
CDR1WellCounts.merge(b, 1, (oldValue, newValue) -> oldValue + newValue);
}
for (Integer i : wellNCDR3s.keySet()) {
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;
}
}
}
}
SimpleWeightedBipartiteGraphMatrixGenerator graphGenerator = new SimpleWeightedBipartiteGraphMatrixGenerator();
List<Integer> CDR3Vertices = new ArrayList<>();
CDR3Vertices.addAll(plateVtoCDR3Map.keySet()); //This will work because LinkedHashMap preserves order of entry
@@ -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<Integer, Integer> 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<Integer, Integer> 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<Integer, Integer> dualMatchesMap = new LinkedHashMap<>();
for(Integer alphaCDR3: CDR3AtoBMap.keySet()) {
@@ -387,6 +411,7 @@ public class Simulator {
}
}
//results for dual map
List<List<String>> allResults = new ArrayList<>();
Integer trueCount = 0;
Iterator iter = dualMatchesMap.keySet().iterator();
@@ -415,6 +440,7 @@ public class Simulator {
}
List<String> 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<String> 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;
}

View File

@@ -255,12 +255,15 @@ public class UserInterface {
}
List<Integer[]> 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();
}
}