Bugfix, can now do plates with <96 wells

This commit is contained in:
2021-11-12 11:16:21 -06:00
parent e15cbc6672
commit 92add6786a
2 changed files with 34 additions and 17 deletions

View File

@@ -1,4 +1,4 @@
import org.jgrapht.Graph; //import org.jgrapht.Graph;
import org.jgrapht.alg.interfaces.MatchingAlgorithm; import org.jgrapht.alg.interfaces.MatchingAlgorithm;
import org.jgrapht.alg.matching.MaximumWeightBipartiteMatching; import org.jgrapht.alg.matching.MaximumWeightBipartiteMatching;
import org.jgrapht.generate.SimpleWeightedBipartiteGraphMatrixGenerator; import org.jgrapht.generate.SimpleWeightedBipartiteGraphMatrixGenerator;
@@ -8,15 +8,18 @@ import org.jgrapht.graph.SimpleWeightedGraph;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.MathContext; import java.math.MathContext;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.time.Duration; //import java.time.Duration;
import java.time.Instant; //import java.time.Instant;
import java.util.*; import java.util.*;
import java.util.stream.IntStream; import java.util.stream.IntStream;
public class Simulator { public class Simulator {
/*
* Old instance fields from before object-oriented refactor
*
private static Integer numDistinctCells = 2_000_000; private static Integer numDistinctCells = 2_000_000;
private static double stdDeviation = 200; //square root of numDistCells would approximate poisson dist private static double stdDeviation = 200; //square root of numDistCells would approximate poisson dist
private static int numWells = 96; private static int numWells;
private static int numConcentrations = 1; private static int numConcentrations = 1;
private static double errorRate = 0.1; private static double errorRate = 0.1;
private static Integer[] concentrations = {500}; private static Integer[] concentrations = {500};
@@ -24,7 +27,7 @@ public class Simulator {
private static int highThreshold = numWells - 3; //max number of shared wells to attempt pairing private static int highThreshold = numWells - 3; //max number of shared wells to attempt pairing
private static boolean use2DArrayForGraph = true; //Doing this is much faster for larger graphs private static boolean use2DArrayForGraph = true; //Doing this is much faster for larger graphs
private static boolean useJGraphTGraphMatrixGenerator = true; //fastest option private static boolean useJGraphTGraphMatrixGenerator = true; //fastest option
*/
public static CellSample generateCellSample(Integer numDistinctCells) { public static CellSample generateCellSample(Integer numDistinctCells) {
List<Integer> numbers = new ArrayList<>(); List<Integer> numbers = new ArrayList<>();
@@ -45,7 +48,7 @@ public class Simulator {
public static void matchCells(String filename, List<Integer[]> distinctCells, Plate samplePlate, Integer lowThreshold, Integer highThreshold){ public static void matchCells(String filename, List<Integer[]> distinctCells, Plate samplePlate, Integer lowThreshold, Integer highThreshold){
System.out.println("Cells: " + distinctCells.size()); System.out.println("Cells: " + distinctCells.size());
int numWells = samplePlate.getSize();
System.out.println("Making cell maps"); System.out.println("Making cell maps");
//HashMap keyed to Alphas, values Betas //HashMap keyed to Alphas, values Betas
Map<Integer, Integer> distCellsMapAlphaKey = new HashMap<>(); Map<Integer, Integer> distCellsMapAlphaKey = new HashMap<>();
@@ -82,10 +85,8 @@ public class Simulator {
Map<Integer, Integer> plateVtoBMap = getVertexToPeptideMap(allBetas, vertexStartValue); Map<Integer, Integer> plateVtoBMap = getVertexToPeptideMap(allBetas, vertexStartValue);
//keys are alphas, values are sequential integer vertices from previous map //keys are alphas, values are sequential integer vertices from previous map
Map<Integer, Integer> plateAtoVMap = invertVertexMap(plateVtoAMap); Map<Integer, Integer> plateAtoVMap = invertVertexMap(plateVtoAMap);
System.out.println(plateAtoVMap.size());
//keys are betas, values are sequential integer vertices from previous map //keys are betas, values are sequential integer vertices from previous map
Map<Integer, Integer> plateBtoVMap = invertVertexMap(plateVtoBMap); Map<Integer, Integer> plateBtoVMap = invertVertexMap(plateVtoBMap);
System.out.println(plateAtoVMap.size());
System.out.println("Vertex maps made"); System.out.println("Vertex maps made");
System.out.println("Creating Graph"); System.out.println("Creating Graph");
@@ -145,7 +146,8 @@ public class Simulator {
//Results for csv file //Results for csv file
List<List<String>> allResults = new ArrayList<>(); List<List<String>> allResults = new ArrayList<>();
int size = samplePlate.getSize(); NumberFormat nf = NumberFormat.getInstance(Locale.US);
MathContext mc = new MathContext(3);
Iterator<DefaultWeightedEdge> weightIter = graphMatching.iterator(); Iterator<DefaultWeightedEdge> weightIter = graphMatching.iterator();
DefaultWeightedEdge e = null; DefaultWeightedEdge e = null;
int trueCount = 0; int trueCount = 0;
@@ -175,23 +177,27 @@ public class Simulator {
//overlap count //overlap count
result.add(Double.toString(graph.getEdgeWeight(e))); result.add(Double.toString(graph.getEdgeWeight(e)));
result.add(Boolean.toString(check)); result.add(Boolean.toString(check));
result.add(Double.toString(Equations.pValue(size, alphaWellCounts.get(plateVtoAMap.get(source)), double pValue = Equations.pValue(numWells, alphaWellCounts.get(plateVtoAMap.get(source)),
betaWellCounts.get(plateVtoBMap.get(target)), graph.getEdgeWeight(e)))); betaWellCounts.get(plateVtoBMap.get(target)), graph.getEdgeWeight(e));
BigDecimal pValueTrunc = new BigDecimal(pValue, mc);
result.add(pValueTrunc.toString());
allResults.add(result); allResults.add(result);
} }
//Metadate comments for CSV file //Metadate comments for CSV file
int min = alphaCount > betaCount ? betaCount : alphaCount; int min = alphaCount > betaCount ? betaCount : alphaCount;
double attemptRate = (double) (trueCount + falseCount) / min; double attemptRate = (double) (trueCount + falseCount) / min;
BigDecimal attemptRateTrunc = new BigDecimal(attemptRate, mc);
double pairingErrorRate = (double) falseCount / (trueCount + falseCount); double pairingErrorRate = (double) falseCount / (trueCount + falseCount);
BigDecimal pairingErrorRateTrunc = new BigDecimal(pairingErrorRate, mc);
List<String> comments = new ArrayList<>(); List<String> comments = new ArrayList<>();
comments.add("Total alphas found: " + alphaCount); comments.add("Total alphas found: " + alphaCount);
comments.add("Total betas found: " + betaCount); comments.add("Total betas found: " + betaCount);
comments.add("Pairing attempt rate: " + attemptRate); comments.add("Pairing attempt rate: " + attemptRateTrunc);
comments.add("Correct pairings: " + trueCount); comments.add("Correct pairings: " + trueCount);
comments.add("Incorrect pairings: " + falseCount); comments.add("Incorrect pairings: " + falseCount);
comments.add("Pairing error rate: " + pairingErrorRate); comments.add("Pairing error rate: " + pairingErrorRateTrunc);
//result writer //result writer
MatchingFileWriter writer = new MatchingFileWriter(filename, comments, header, allResults); MatchingFileWriter writer = new MatchingFileWriter(filename, comments, header, allResults);
@@ -200,7 +206,9 @@ public class Simulator {
/*
** Old main method before object-oriented refactor
*
public static void Simulate() { public static void Simulate() {
Instant start = Instant.now(); Instant start = Instant.now();
//Four things to try to improve this //Four things to try to improve this
@@ -440,6 +448,7 @@ public class Simulator {
} }
*/
private static Map<Integer, Integer> getVertexToPeptideMap(Map<Integer, Integer> peptides, Integer startValue) { private static Map<Integer, Integer> getVertexToPeptideMap(Map<Integer, Integer> peptides, Integer startValue) {
Map<Integer, Integer> map = new LinkedHashMap<>(); //LinkedHashMap to preserve order of entry Map<Integer, Integer> map = new LinkedHashMap<>(); //LinkedHashMap to preserve order of entry
@@ -459,6 +468,9 @@ public class Simulator {
return inverse; return inverse;
} }
/*
* Old methods for graph generation algorithms less efficient than built in matrix generator
*
private static void addEdgeWithWeight(Graph g, Integer v1, Integer v2, double weight) { private static void addEdgeWithWeight(Graph g, Integer v1, Integer v2, double weight) {
g.addEdge(v1, v2); g.addEdge(v1, v2);
g.setEdgeWeight(v1, v2, weight); g.setEdgeWeight(v1, v2, weight);
@@ -472,7 +484,12 @@ public class Simulator {
g.addEdge(v1, v2); g.addEdge(v1, v2);
} }
} }
*/
/*
* Old output method before object-oriented refactor
*
private static boolean printData (Double edgeWeight, Integer source, Integer sourceCount, Integer target, private static boolean printData (Double edgeWeight, Integer source, Integer sourceCount, Integer target,
Integer targetCount, Map<Integer,Integer> AtoBMap, Map<Integer,Integer> VtoAMap, Integer targetCount, Map<Integer,Integer> AtoBMap, Map<Integer,Integer> VtoAMap,
Map<Integer, Integer> VtoBMap) { Map<Integer, Integer> VtoBMap) {
@@ -490,5 +507,5 @@ public class Simulator {
System.out.println("The real pair is " + source + " and " + AtoBMap.get(VtoAMap.get(source))); System.out.println("The real pair is " + source + " and " + AtoBMap.get(VtoAMap.get(source)));
return false; return false;
} }
} }*/
} }

View File

@@ -182,8 +182,8 @@ public class UserInterface {
} }
else{ else{
if(highThreshold > plate.getSize()){ if(highThreshold >= plate.getSize()){
highThreshold = plate.getSize(); highThreshold = plate.getSize() - 1;
} }
List<Integer[]> cells = cellReader.getCells(); List<Integer[]> cells = cellReader.getCells();
Simulator.matchCells(filename, cells, plate, lowThreshold, highThreshold); Simulator.matchCells(filename, cells, plate, lowThreshold, highThreshold);