Added method using matrix builder
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import org.jgrapht.Graph;
|
||||
import org.jgrapht.alg.interfaces.MatchingAlgorithm;
|
||||
import org.jgrapht.alg.matching.MaximumWeightBipartiteMatching;
|
||||
import org.jgrapht.generate.SimpleWeightedBipartiteGraphMatrixGenerator;
|
||||
import org.jgrapht.graph.DefaultWeightedEdge;
|
||||
import org.jgrapht.graph.SimpleWeightedGraph;
|
||||
|
||||
@@ -74,28 +75,36 @@ public class Simulation {
|
||||
int alphaCount = allAlphas.size();
|
||||
int betaCount = allBetas.size();
|
||||
|
||||
//Using Integers instead of Strings to label vertices so I can do clever stuff with indices if I need to
|
||||
// when I refactor to use a 2d array to make the graph
|
||||
Integer vertexStartValue = 0;
|
||||
//keys are sequential integer vertices, values are alphas
|
||||
Map<String, Integer> plateVtoAMap = getVertexToPeptideMap(allAlphas, "A");
|
||||
Map<Integer, Integer> plateVtoAMap = getVertexToPeptideMap(allAlphas, vertexStartValue);
|
||||
//New start value for vertex to beta map should be one more than final vertex value in alpha map
|
||||
vertexStartValue += plateVtoAMap.size();
|
||||
//keys are sequential integers vertices, values are betas
|
||||
Map<String, Integer> plateVtoBMap = getVertexToPeptideMap(allBetas, "B");
|
||||
Map<Integer, Integer> plateVtoBMap = getVertexToPeptideMap(allBetas, vertexStartValue);
|
||||
//keys are alphas, values are sequential integer vertices from previous map
|
||||
Map<Integer, String> plateAtoVMap = invertVertexMap(plateVtoAMap);
|
||||
Map<Integer, Integer> plateAtoVMap = invertVertexMap(plateVtoAMap);
|
||||
//keys are betas, values are sequential integer vertices from previous map
|
||||
Map<Integer, String> plateBtoVMap = invertVertexMap(plateVtoBMap);
|
||||
Map<Integer, Integer> plateBtoVMap = invertVertexMap(plateVtoBMap);
|
||||
|
||||
//OUTPUT
|
||||
System.out.println("Maps made");
|
||||
System.out.println("Creating Graph");
|
||||
|
||||
|
||||
//construct graph
|
||||
SimpleWeightedGraph<String, DefaultWeightedEdge> graph = new SimpleWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
|
||||
SimpleWeightedGraph<Integer, DefaultWeightedEdge> graph = new SimpleWeightedGraph<Integer, DefaultWeightedEdge>(DefaultWeightedEdge.class);
|
||||
//add vertices
|
||||
for(String v: plateVtoAMap.keySet()) {
|
||||
for(Integer v: plateVtoAMap.keySet()) {
|
||||
graph.addVertex(v);
|
||||
}
|
||||
for(String v: plateVtoBMap.keySet()) {
|
||||
for(Integer v: plateVtoBMap.keySet()) {
|
||||
graph.addVertex(v);
|
||||
}
|
||||
|
||||
|
||||
//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
|
||||
Map<Integer, Integer> wellNAlphas = null;
|
||||
@@ -113,6 +122,7 @@ public class Simulation {
|
||||
for(Integer b: wellNBetas.keySet()){
|
||||
betaWellCounts.merge(b, 1, (oldValue, newValue) -> oldValue + newValue);
|
||||
}
|
||||
|
||||
for(Integer i: wellNAlphas.keySet()) {
|
||||
for(Integer j: wellNBetas.keySet()){
|
||||
addEdgeOrWeight(graph, plateAtoVMap.get(i), plateBtoVMap.get(j));
|
||||
@@ -120,6 +130,46 @@ public class Simulation {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
//The above code works, but is too memory intensive. Going to try with a 2D array of doubles.
|
||||
double[][] weights = new double[plateVtoAMap.size()][plateVtoBMap.size()];
|
||||
Map<Integer, Integer> wellNAlphas = null;
|
||||
Map<Integer, Integer> wellNBetas = null;
|
||||
//Count how many wells each alpha appears in
|
||||
Map<Integer, Integer> alphaWellCounts = new HashMap<>();
|
||||
//count how many wells each beta appears in
|
||||
Map<Integer, Integer> betaWellCounts = new HashMap<>();
|
||||
for (int n = 0; n < numWells; n++) {
|
||||
wellNAlphas = samplePlate.assayWellsAlpha(n);
|
||||
for(Integer a: wellNAlphas.keySet()){
|
||||
alphaWellCounts.merge(a, 1, (oldValue, newValue) -> oldValue + newValue);
|
||||
}
|
||||
wellNBetas = samplePlate.assayWellsBeta(n);
|
||||
for(Integer b: wellNBetas.keySet()){
|
||||
betaWellCounts.merge(b, 1, (oldValue, newValue) -> oldValue + newValue);
|
||||
}
|
||||
|
||||
for(Integer i: wellNAlphas.keySet()) {
|
||||
for(Integer j: wellNBetas.keySet()){
|
||||
weights[plateAtoVMap.get(i)][plateBtoVMap.get(j) - vertexStartValue] += 1.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SimpleWeightedBipartiteGraphMatrixGenerator graphGenerator = new SimpleWeightedBipartiteGraphMatrixGenerator();
|
||||
SimpleWeightedGraph<Integer, DefaultWeightedEdge> graph = new SimpleWeightedGraph<Integer, DefaultWeightedEdge>(DefaultWeightedEdge.class);
|
||||
List<Integer> alphaVertices = new ArrayList<>();
|
||||
alphaVertices.addAll(plateVtoAMap.keySet()); //This will work because LinkedHashMap preserves order of entry
|
||||
graphGenerator.first(alphaVertices);
|
||||
List<Integer> betaVertices = new ArrayList<>();
|
||||
betaVertices.addAll(plateVtoBMap.keySet());
|
||||
graphGenerator.second(betaVertices); //This will work because LinkedHashMap preserves order of entry
|
||||
graphGenerator.weights(weights);
|
||||
graphGenerator.generateGraph(graph);
|
||||
|
||||
*/
|
||||
|
||||
|
||||
//OUTPUT
|
||||
System.out.println("Graph created");
|
||||
System.out.println("Finding maximum weighted matching");
|
||||
@@ -143,8 +193,8 @@ public class Simulation {
|
||||
if(graph.getEdgeWeight(e) <= 2.0) {
|
||||
continue;
|
||||
}
|
||||
String source = graph.getEdgeSource(e);
|
||||
String target = graph.getEdgeTarget(e);
|
||||
Integer source = graph.getEdgeSource(e);
|
||||
Integer target = graph.getEdgeTarget(e);
|
||||
//Need map of each peptide and number of wells it appears in.
|
||||
check =printData(graph.getEdgeWeight(e), source, alphaWellCounts.get(plateVtoAMap.get(source)), target, betaWellCounts.get(plateVtoBMap.get(target)), distCellsMapAlphaKey, plateVtoAMap, plateVtoBMap);
|
||||
if(check) {
|
||||
@@ -183,25 +233,25 @@ public class Simulation {
|
||||
|
||||
}
|
||||
|
||||
private static Map<String, Integer> getVertexToPeptideMap(Map<Integer, Integer> peptides, String prefix) {
|
||||
Map<String, Integer> map = new LinkedHashMap<>();
|
||||
Integer index = 0;
|
||||
private static Map<Integer, Integer> getVertexToPeptideMap(Map<Integer, Integer> peptides, Integer startValue) {
|
||||
Map<Integer, Integer> map = new LinkedHashMap<>(); //LinkedHashMap to preserve order of entry
|
||||
Integer index = startValue;
|
||||
for (Integer k: peptides.keySet()) {
|
||||
map.put(prefix + index, k);
|
||||
map.put(index, k);
|
||||
index++;
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private static Map<Integer, String> invertVertexMap(Map<String, Integer> map) {
|
||||
Map<Integer, String> inverse = new HashMap<>();
|
||||
for(String k: map.keySet()) {
|
||||
private static Map<Integer, Integer> invertVertexMap(Map<Integer, Integer> map) {
|
||||
Map<Integer, Integer> inverse = new HashMap<>();
|
||||
for(Integer k: map.keySet()) {
|
||||
inverse.put(map.get(k), k);
|
||||
}
|
||||
return inverse;
|
||||
}
|
||||
|
||||
private static void addEdgeOrWeight(Graph g, String v1, String v2) {
|
||||
private static void addEdgeOrWeight(Graph g, Integer v1, Integer v2) {
|
||||
if (g.containsEdge(v1, v2)) {
|
||||
g.setEdgeWeight(v1, v2, g.getEdgeWeight(g.getEdge(v1, v2)) + 1.0);
|
||||
}
|
||||
@@ -211,7 +261,7 @@ public class Simulation {
|
||||
//System.out.println("added!");
|
||||
}
|
||||
|
||||
private static boolean printData (Double edgeWeight, String source, Integer sourceCount, String target, Integer targetCount, Map<Integer,Integer> AtoBMap, Map<String,Integer> VtoAMap, Map<String, Integer> VtoBMap) {
|
||||
private static boolean printData (Double edgeWeight, Integer source, Integer sourceCount, Integer target, Integer targetCount, Map<Integer,Integer> AtoBMap, Map<Integer,Integer> VtoAMap, Map<Integer, Integer> VtoBMap) {
|
||||
//placeholder for real calculations
|
||||
int trueCount = 0;
|
||||
int falseCount = 0;
|
||||
|
||||
Reference in New Issue
Block a user