Expanding simulation to attempt CDR3/CDR1 matching - doubles size of cells
This commit is contained in:
@@ -30,8 +30,10 @@ public class CellFileReader {
|
||||
){
|
||||
for(CSVRecord record: parser.getRecords()) {
|
||||
Integer[] cell = new Integer[2];
|
||||
cell[0] = Integer.valueOf(record.get("Alpha"));
|
||||
cell[1] = Integer.valueOf(record.get("Beta"));
|
||||
cell[0] = Integer.valueOf(record.get("Alpha CDR3"));
|
||||
cell[1] = Integer.valueOf(record.get("Beta CDR3"));
|
||||
cell[2] = Integer.valueOf(record.get("Alpha CDR1"));
|
||||
cell[3] = Integer.valueOf(record.get("Beta CDR1"));
|
||||
distinctCells.add(cell);
|
||||
}
|
||||
} catch(IOException ex){
|
||||
|
||||
32
src/main/java/MatchingResult.java
Normal file
32
src/main/java/MatchingResult.java
Normal file
@@ -0,0 +1,32 @@
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class MatchingResult {
|
||||
private List<String> comments;
|
||||
private List<String> headers;
|
||||
private List<List<String>> allResults;
|
||||
private Map<Integer, Integer> matchMap;
|
||||
|
||||
public MatchingResult(List<String> comments, List<String> headers, List<List<String>> allResults, Map<Integer, Integer>matchMap){
|
||||
this.comments = comments;
|
||||
this.headers = headers;
|
||||
this.allResults = allResults;
|
||||
this.matchMap = matchMap;
|
||||
}
|
||||
|
||||
public List<String> getComments() {
|
||||
return comments;
|
||||
}
|
||||
|
||||
public List<List<String>> getAllResults() {
|
||||
return allResults;
|
||||
}
|
||||
|
||||
public List<String> getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
public Map<Integer, Integer> getMatchMap() {
|
||||
return matchMap;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
import java.sql.Array;
|
||||
import java.util.*;
|
||||
|
||||
//Need to write function to output plate to a file that I can read in.
|
||||
@@ -29,7 +28,6 @@ public class Plate {
|
||||
int section = 0;
|
||||
double m;
|
||||
int n;
|
||||
boolean drop;
|
||||
while (section < numSections){
|
||||
for (int i = 0; i < (size / numSections); i++) {
|
||||
List<Integer[]> well = new ArrayList<>();
|
||||
@@ -39,12 +37,9 @@ public class Plate {
|
||||
} while (m >= cells.size() || m < 0);
|
||||
n = (int) Math.floor(m);
|
||||
Integer[] cellToAdd = cells.get(n).clone();
|
||||
drop = Math.abs(rand.nextDouble()) < error;
|
||||
if (drop) {
|
||||
if (rand.nextBoolean()) {
|
||||
cellToAdd[0] = -1;
|
||||
} else {
|
||||
cellToAdd[1] = -1;
|
||||
for(int k = 0; k < cellToAdd.length; k++){
|
||||
if(Math.abs(rand.nextDouble()) < error){//error applied to each peptide
|
||||
cellToAdd[k] = -1;
|
||||
}
|
||||
}
|
||||
well.add(cellToAdd);
|
||||
@@ -80,24 +75,112 @@ public class Plate {
|
||||
return wells;
|
||||
}
|
||||
|
||||
public Map<Integer, Integer> assayWellsAlpha() {
|
||||
return this.assayWellsAlpha(0, size);
|
||||
|
||||
//returns a map of counts of all the CDR3s (alphas and betas) in all wells
|
||||
public Map<Integer, Integer>assayWellsCDR3(){
|
||||
return this.assayWellsCDR3(0, size);
|
||||
}
|
||||
//returns a map of counts of all the CDR3 alphas in all wells
|
||||
public Map<Integer, Integer> assayWellsCDR3Alpha() {
|
||||
return this.assayWellsCDR3Alpha(0, size);
|
||||
}
|
||||
//returns a map of counts of all the CDR3 betas in all wells
|
||||
public Map<Integer, Integer> assayWellsCDR3Beta() {
|
||||
return this.assayWellsCDR3Beta(0, size);
|
||||
}
|
||||
//returns a map of counts of all CDR1s (alphas and betas) in all wells
|
||||
public Map<Integer, Integer> assayWellsCDR1(){
|
||||
return this.assayWellsCDR1(0, size);
|
||||
}
|
||||
//returns a map of counts of all the CDR1 alphas in all wells
|
||||
public Map<Integer, Integer> assayWellsCDR1Alpha() {
|
||||
return this.assayWellsCDR1Alpha(0, size);
|
||||
}
|
||||
//returns a map of counts of all the CDR1 betas in all wells
|
||||
public Map<Integer, Integer> assayWellsCDR1Beta() {
|
||||
return this.assayWellsCDR1Beta(0, size);
|
||||
}
|
||||
|
||||
public Map<Integer, Integer> assayWellsAlpha(int n) {
|
||||
return this.assayWellsAlpha(n, n+1);
|
||||
//returns a map of counts of the CDR3s (alphas and betas) in a specific well
|
||||
public Map<Integer, Integer>assayWellsCDR3(int n){
|
||||
return this.assayWellsCDR3(n, n+1);
|
||||
}
|
||||
//returns a map of counts of the CDR1s (alphas and betas) in a specific well
|
||||
public Map<Integer, Integer> assayWellsCDR1(int n){
|
||||
return this.assayWellsCDR1(n, n+1);
|
||||
}
|
||||
//returns a map of counts of the CDR3 alphas in a specific well
|
||||
public Map<Integer, Integer> assayWellsCDR3Alpha(int n) {
|
||||
return this.assayWellsCDR3Alpha(n, n+1);
|
||||
}
|
||||
//returns a map of counts of the CDR3 betas in a specific well
|
||||
public Map<Integer, Integer> assayWellsCDR3Beta(int n) {
|
||||
return this.assayWellsCDR3Beta(n, n+1);
|
||||
}
|
||||
//returns a map of counts of the CDR1 alphas in a specific well
|
||||
public Map<Integer, Integer> assayWellsCDR1Alpha(int n) {
|
||||
return this.assayWellsCDR1Alpha(n, n+1);
|
||||
}
|
||||
//returns a map of counts of the CDR1 betas in a specific well
|
||||
public Map<Integer, Integer> assayWellsCDR1Beta(int n) {
|
||||
return this.assayWellsCDR1Beta(n, n+1);
|
||||
}
|
||||
|
||||
public Map<Integer, Integer> assayWellsAlpha(int start, int end) {
|
||||
|
||||
//returns a map of the counts of the CDR3s (alphas and betas) in a range of wells
|
||||
public Map<Integer, Integer>assayWellsCDR3(int start, int end){
|
||||
Map<Integer,Integer> assay = new HashMap<>();
|
||||
for(int i = start; i < end; i++){
|
||||
countCDR3Alphas(assay, wells.get(i));
|
||||
countCDR3Betas(assay,wells.get(i));
|
||||
}
|
||||
return assay;
|
||||
}
|
||||
//returns a map of the counts of the CDR1s (alphas and betas) in a range of wells
|
||||
public Map<Integer, Integer>assayWellsCDR1(int start, int end){
|
||||
Map<Integer,Integer> assay = new HashMap<>();
|
||||
for(int i = start; i < end; i++){
|
||||
countCDR1Alphas(assay, wells.get(i));
|
||||
countCDR1Betas(assay,wells.get(i));
|
||||
}
|
||||
return assay;
|
||||
}
|
||||
//returns a map of the counts of the CDR3 alphas in a range of wells
|
||||
public Map<Integer, Integer> assayWellsCDR3Alpha(int start, int end) {
|
||||
Map<Integer, Integer> assay = new HashMap<>();
|
||||
for(int i = start; i < end; i++){
|
||||
countAlphas(assay, wells.get(i));
|
||||
countCDR3Alphas(assay, wells.get(i));
|
||||
}
|
||||
return assay;
|
||||
}
|
||||
//returns a map of the counts of the CDR3 betas in a range of wells
|
||||
public Map<Integer, Integer> assayWellsCDR3Beta(int start, int end) {
|
||||
Map<Integer, Integer> assay = new HashMap<>();
|
||||
for(int i = start; i < end; i++){
|
||||
countCDR3Betas(assay, wells.get(i));
|
||||
}
|
||||
return assay;
|
||||
}
|
||||
//returns a map of the counts of the CDR1 alphas in a range of wells
|
||||
public Map<Integer, Integer> assayWellsCDR1Alpha(int start, int end) {
|
||||
Map<Integer, Integer> assay = new HashMap<>();
|
||||
for(int i = start; i < end; i++){
|
||||
countCDR1Alphas(assay, wells.get(i));
|
||||
}
|
||||
return assay;
|
||||
}
|
||||
//returns a map of the counts of the CDR1 betas in a range of wells
|
||||
public Map<Integer, Integer> assayWellsCDR1Beta(int start, int end) {
|
||||
Map<Integer, Integer> assay = new HashMap<>();
|
||||
for(int i = start; i < end; i++){
|
||||
countCDR1Betas(assay, wells.get(i));
|
||||
}
|
||||
return assay;
|
||||
}
|
||||
|
||||
//given a map, counts distinct alphas in a well
|
||||
private void countAlphas(Map<Integer, Integer> wellMap, List<Integer[]> well){
|
||||
|
||||
//given a map, counts distinct CDR3 alphas in a well
|
||||
private void countCDR3Alphas(Map<Integer, Integer> wellMap, List<Integer[]> well){
|
||||
for(Integer[] cell : well) {
|
||||
if(cell[0] != -1){
|
||||
//keys are alphas, value is how many of them have been assayed
|
||||
@@ -105,34 +188,29 @@ public class Plate {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//assays all wells
|
||||
public Map<Integer, Integer> assayWellsBeta() {
|
||||
return this.assayWellsBeta(0, size);
|
||||
}
|
||||
|
||||
//assays a specific well
|
||||
public Map<Integer, Integer> assayWellsBeta(int n) {
|
||||
return this.assayWellsBeta(n, n+1);
|
||||
}
|
||||
|
||||
//assays a range of wells
|
||||
public Map<Integer, Integer> assayWellsBeta(int start, int end) {
|
||||
Map<Integer, Integer> assay = new HashMap<>();
|
||||
for(int i = start; i < end; i++){
|
||||
countBetas(assay, wells.get(i));
|
||||
}
|
||||
return assay;
|
||||
}
|
||||
|
||||
//given a map, counts distinct betas in a well
|
||||
private void countBetas(Map<Integer, Integer> wellMap, List<Integer[]> well){
|
||||
//given a map, counts distinct CDR3 betas in a well
|
||||
private void countCDR3Betas(Map<Integer, Integer> wellMap, List<Integer[]> well){
|
||||
for(Integer[] cell : well) {
|
||||
if(cell[1] != -1){
|
||||
wellMap.merge(cell[1], 1, (oldValue, newValue) -> oldValue + newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//given a map, counts distinct CDR1 alphas in a well
|
||||
private void countCDR1Alphas(Map<Integer, Integer> wellMap, List<Integer[]> well){
|
||||
for(Integer[] cell: well){
|
||||
if(cell[2] != -1){
|
||||
wellMap.merge(cell[2], 1, (oldValue, newValue) -> oldValue + newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
//given a map, counts distinct CDR1 betas in a well
|
||||
private void countCDR1Betas(Map<Integer, Integer> wellMap, List<Integer[]> well){
|
||||
for(Integer[] cell: well){
|
||||
if(cell[3] != -1){
|
||||
wellMap.merge(cell[3], 1, (oldValue, newValue) -> oldValue + newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -71,7 +71,8 @@ public class Simulator {
|
||||
return new CellSample(distinctCells);
|
||||
}
|
||||
|
||||
public static void matchCDR3s(String filename, List<Integer[]> distinctCells, Plate samplePlate, Integer lowThreshold, Integer highThreshold){
|
||||
public static MatchingResult matchCDR3s(List<Integer[]> distinctCells,
|
||||
Plate samplePlate, Integer lowThreshold, Integer highThreshold){
|
||||
System.out.println("Cells: " + distinctCells.size());
|
||||
int numWells = samplePlate.getSize();
|
||||
System.out.println("Making cell maps");
|
||||
@@ -88,8 +89,8 @@ public class Simulator {
|
||||
System.out.println("Cell maps made");
|
||||
|
||||
System.out.println("Making well maps");
|
||||
Map<Integer, Integer> allAlphas = samplePlate.assayWellsAlpha();
|
||||
Map<Integer, Integer> allBetas = samplePlate.assayWellsBeta();
|
||||
Map<Integer, Integer> allAlphas = samplePlate.assayWellsCDR3Alpha();
|
||||
Map<Integer, Integer> allBetas = samplePlate.assayWellsCDR3Beta();
|
||||
int alphaCount = allAlphas.size();
|
||||
System.out.println("all alphas count: " + alphaCount);
|
||||
int betaCount = allBetas.size();
|
||||
@@ -127,11 +128,11 @@ public class Simulator {
|
||||
new SimpleWeightedGraph<>(DefaultWeightedEdge.class);
|
||||
double[][] weights = new double[plateVtoAMap.size()][plateVtoBMap.size()];
|
||||
for (int n = 0; n < numWells; n++) {
|
||||
wellNAlphas = samplePlate.assayWellsAlpha(n);
|
||||
wellNAlphas = samplePlate.assayWellsCDR3Alpha(n);
|
||||
for (Integer a : wellNAlphas.keySet()) {
|
||||
alphaWellCounts.merge(a, 1, (oldValue, newValue) -> oldValue + newValue);
|
||||
}
|
||||
wellNBetas = samplePlate.assayWellsBeta(n);
|
||||
wellNBetas = samplePlate.assayWellsCDR3Beta(n);
|
||||
for (Integer b : wellNBetas.keySet()) {
|
||||
betaWellCounts.merge(b, 1, (oldValue, newValue) -> oldValue + newValue);
|
||||
}
|
||||
@@ -178,6 +179,7 @@ public class Simulator {
|
||||
int trueCount = 0;
|
||||
int falseCount = 0;
|
||||
boolean check = false;
|
||||
Map<Integer, Integer> matchMap = new HashMap<>();
|
||||
while(weightIter.hasNext()) {
|
||||
e = weightIter.next();
|
||||
if(graph.getEdgeWeight(e) < lowThreshold || graph.getEdgeWeight(e) > highThreshold) {
|
||||
@@ -185,6 +187,8 @@ public class Simulator {
|
||||
}
|
||||
Integer source = graph.getEdgeSource(e);
|
||||
Integer target = graph.getEdgeTarget(e);
|
||||
//The match map is all matches found, not just true matches!
|
||||
matchMap.put(plateVtoAMap.get(source), plateVtoBMap.get(target));
|
||||
check = plateVtoBMap.get(target).equals(distCellsMapAlphaKey.get(plateVtoAMap.get(source)));
|
||||
if(check) {
|
||||
trueCount++;
|
||||
@@ -224,11 +228,210 @@ public class Simulator {
|
||||
comments.add("Incorrect pairings: " + falseCount);
|
||||
comments.add("Pairing error rate: " + pairingErrorRateTrunc);
|
||||
|
||||
//result writer
|
||||
MatchingFileWriter writer = new MatchingFileWriter(filename, comments, header, allResults);
|
||||
writer.writeResultsToFile();
|
||||
return new MatchingResult(comments, header, allResults, matchMap);
|
||||
}
|
||||
|
||||
public static MatchingResult matchCDR1s(List<Integer[]> distinctCells,
|
||||
Plate samplePlate, Integer lowThreshold,
|
||||
Integer highThreshold, Map<Integer, Integer> previousMatches){
|
||||
int numWells = samplePlate.getSize();
|
||||
System.out.println("Making previous match maps");
|
||||
Map<Integer, Integer> CDR3AtoBMap = previousMatches;
|
||||
Map<Integer, Integer> CDR3BtoAMap = invertVertexMap(CDR3AtoBMap);
|
||||
System.out.println("Previous match maps made");
|
||||
|
||||
System.out.println("Making cell maps");
|
||||
Map<Integer, Integer> alphaCDR3toCDR1Map = new HashMap<>();
|
||||
for (Integer[] cell : distinctCells) {
|
||||
alphaCDR3toCDR1Map.put(cell[0], cell[2]);
|
||||
}
|
||||
//HashMap keyed to Betas, values Alphas
|
||||
Map<Integer, Integer> betaCDR3toCDR1Map = new HashMap<>();
|
||||
for (Integer[] cell : distinctCells) {
|
||||
betaCDR3toCDR1Map.put(cell[1], cell[3]);
|
||||
}
|
||||
System.out.println("Cell maps made");
|
||||
|
||||
System.out.println("Making well maps");
|
||||
Map<Integer, Integer> allCDR3s = samplePlate.assayWellsCDR3();
|
||||
Map<Integer, Integer> allCDR1s = samplePlate.assayWellsCDR1();
|
||||
int CDR3Count = allCDR3s.size();
|
||||
System.out.println("all alphas count: " + CDR3Count);
|
||||
int CDR1Count = allCDR1s.size();
|
||||
System.out.println("all betas count: " + CDR1Count);
|
||||
System.out.println("Well maps made");
|
||||
|
||||
System.out.println("Making vertex maps");
|
||||
//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
|
||||
//For the autogenerator, all vertices must have distinct numbers associated with them
|
||||
Integer vertexStartValue = 0;
|
||||
//keys are sequential integer vertices, values are CDR3s
|
||||
Map<Integer, Integer> plateVtoCDR3Map = getVertexToPeptideMap(allCDR3s, vertexStartValue);
|
||||
//New start value for vertex to beta map should be one more than final vertex value in alpha map
|
||||
vertexStartValue += plateVtoCDR3Map.size();
|
||||
//keys are sequential integers vertices, values are CDR1s
|
||||
Map<Integer, Integer> plateVtoCDR1Map = getVertexToPeptideMap(allCDR1s, vertexStartValue);
|
||||
//keys are CDR3s, values are sequential integer vertices from previous map
|
||||
Map<Integer, Integer> plateCDR3toVMap = invertVertexMap(plateVtoCDR3Map);
|
||||
//keys are CDR1s, values are sequential integer vertices from previous map
|
||||
Map<Integer, Integer> plateCDR1toVMap = invertVertexMap(plateVtoCDR1Map);
|
||||
System.out.println("Vertex maps made");
|
||||
|
||||
System.out.println("Creating Graph");
|
||||
//Count how many wells each alpha appears in
|
||||
Map<Integer, Integer> CDR3WellCounts = new HashMap<>();
|
||||
//count how many wells each beta 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
|
||||
Map<Integer, Integer> wellNCDR3s = null;
|
||||
Map<Integer, Integer> wellNCDR1s = null;
|
||||
SimpleWeightedGraph<Integer, DefaultWeightedEdge> graph =
|
||||
new SimpleWeightedGraph<>(DefaultWeightedEdge.class);
|
||||
double[][] weights = new double[plateVtoCDR3Map.size()][plateVtoCDR1Map.size()];
|
||||
for (int n = 0; n < numWells; n++) {
|
||||
wellNCDR3s = samplePlate.assayWellsCDR3(n);
|
||||
for (Integer a : wellNCDR3s.keySet()) {
|
||||
CDR3WellCounts.merge(a, 1, (oldValue, newValue) -> oldValue + newValue);
|
||||
}
|
||||
wellNCDR1s = samplePlate.assayWellsCDR1(n);
|
||||
for (Integer b : wellNCDR1s.keySet()) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
SimpleWeightedBipartiteGraphMatrixGenerator graphGenerator = new SimpleWeightedBipartiteGraphMatrixGenerator();
|
||||
List<Integer> CDR3Vertices = new ArrayList<>();
|
||||
CDR3Vertices.addAll(plateVtoCDR3Map.keySet()); //This will work because LinkedHashMap preserves order of entry
|
||||
graphGenerator.first(CDR3Vertices);
|
||||
List<Integer> CDR1Vertices = new ArrayList<>();
|
||||
CDR1Vertices.addAll(plateVtoCDR1Map.keySet());
|
||||
graphGenerator.second(CDR1Vertices); //This will work because LinkedHashMap preserves order of entry
|
||||
graphGenerator.weights(weights);
|
||||
graphGenerator.generateGraph(graph);
|
||||
System.out.println("Graph created");
|
||||
|
||||
System.out.println("Finding first maximum weighted matching");
|
||||
MaximumWeightBipartiteMatching firstMaxWeightMatching =
|
||||
new MaximumWeightBipartiteMatching(graph, plateVtoCDR3Map.keySet(), plateVtoCDR1Map.keySet());
|
||||
MatchingAlgorithm.Matching<String, DefaultWeightedEdge> graphMatching = firstMaxWeightMatching.getMatching();
|
||||
System.out.println("First maximum weighted matching found");
|
||||
|
||||
|
||||
//first processing run
|
||||
Map<Integer, Integer> firstMatchCDR3toCDR1Map = new HashMap<>();
|
||||
Iterator<DefaultWeightedEdge> weightIter = graphMatching.iterator();
|
||||
DefaultWeightedEdge e = null;
|
||||
while(weightIter.hasNext()){
|
||||
e = weightIter.next();
|
||||
if(graph.getEdgeWeight(e) < lowThreshold || graph.getEdgeWeight(e) > highThreshold) {
|
||||
continue;
|
||||
}
|
||||
Integer source = graph.getEdgeSource(e);
|
||||
if(!(CDR3AtoBMap.containsKey(source) || CDR3BtoAMap.containsKey(source))){
|
||||
continue;
|
||||
}
|
||||
Integer target = graph.getEdgeTarget(e);
|
||||
firstMatchCDR3toCDR1Map.put(plateVtoCDR3Map.get(source), plateVtoCDR1Map.get(target));
|
||||
}
|
||||
|
||||
//zero out the edge weights in the matching
|
||||
weightIter = graphMatching.iterator();
|
||||
while(weightIter.hasNext()){
|
||||
graph.removeEdge(weightIter.next());
|
||||
}
|
||||
|
||||
//Generate a new matching
|
||||
System.out.println("Finding second maximum weighted matching");
|
||||
MaximumWeightBipartiteMatching secondMaxWeightMatching =
|
||||
new MaximumWeightBipartiteMatching(graph, plateVtoCDR3Map.keySet(), plateVtoCDR1Map.keySet());
|
||||
graphMatching = secondMaxWeightMatching.getMatching();
|
||||
System.out.println("Second maximum weighted matching found");
|
||||
|
||||
//second processing run
|
||||
Map<Integer, Integer> secondMatchCDR3toCDR1Map = new HashMap<>();
|
||||
weightIter = graphMatching.iterator();
|
||||
while(weightIter.hasNext()){
|
||||
e = weightIter.next();
|
||||
if(graph.getEdgeWeight(e) < lowThreshold || graph.getEdgeWeight(e) > highThreshold) {
|
||||
continue;
|
||||
}
|
||||
Integer source = graph.getEdgeSource(e);
|
||||
if(!(CDR3AtoBMap.containsKey(source) || CDR3BtoAMap.containsKey(source))){
|
||||
continue;
|
||||
}
|
||||
Integer target = graph.getEdgeTarget(e);
|
||||
secondMatchCDR3toCDR1Map.put(plateVtoCDR3Map.get(source), plateVtoCDR1Map.get(target));
|
||||
}
|
||||
|
||||
//Look for matches that simply swapped already-matched alpha and beta CDR3s
|
||||
Map<Integer, Integer> dualMatchesMap = new LinkedHashMap<>();
|
||||
for(Integer alphaCDR3: CDR3AtoBMap.keySet()) {
|
||||
if (!(firstMatchCDR3toCDR1Map.containsKey(alphaCDR3) && secondMatchCDR3toCDR1Map.containsKey(alphaCDR3))) {
|
||||
continue;
|
||||
}
|
||||
Integer betaCDR3 = CDR3AtoBMap.get(alphaCDR3);
|
||||
if (!(firstMatchCDR3toCDR1Map.containsKey(betaCDR3) && secondMatchCDR3toCDR1Map.containsKey(betaCDR3))) {
|
||||
continue;
|
||||
}
|
||||
if(firstMatchCDR3toCDR1Map.get(alphaCDR3).equals(secondMatchCDR3toCDR1Map.get(betaCDR3))){
|
||||
if(firstMatchCDR3toCDR1Map.get(betaCDR3).equals(secondMatchCDR3toCDR1Map.get(alphaCDR3))){
|
||||
dualMatchesMap.put(alphaCDR3, firstMatchCDR3toCDR1Map.get(alphaCDR3));
|
||||
dualMatchesMap.put(betaCDR3, firstMatchCDR3toCDR1Map.get(betaCDR3));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<List<String>> allResults = new ArrayList<>();
|
||||
Integer trueCount = 0;
|
||||
Iterator iter = dualMatchesMap.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(dualMatchesMap.get(Integer.valueOf(tmp.get(0))).toString());
|
||||
tmp.add(dualMatchesMap.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++;
|
||||
}
|
||||
}
|
||||
|
||||
List<String> comments = new ArrayList<>();
|
||||
comments.add("Previous pairs found: " + previousMatches.size());
|
||||
comments.add("CDR1 matches attempted: " + allResults.size());
|
||||
double attemptRate = (double) allResults.size() / previousMatches.size();
|
||||
comments.add("Matching attempt rate: " + attemptRate);
|
||||
comments.add("Number of correct matches: " + trueCount);
|
||||
double correctRate = (double) trueCount / allResults.size();
|
||||
comments.add("Correct matching rate: " + correctRate);
|
||||
|
||||
List<String> headers = new ArrayList<>();
|
||||
headers.add("CDR3 alpha");
|
||||
headers.add("CDR3 beta");
|
||||
headers.add("first matched CDR1");
|
||||
headers.add("second matched CDR1");
|
||||
headers.add("Correct match?");
|
||||
|
||||
return new MatchingResult(comments, headers, allResults, dualMatchesMap);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
@@ -25,7 +25,7 @@ public class UserInterface {
|
||||
case 1 -> makeCells();
|
||||
case 2 -> makePlate();
|
||||
case 3 -> matchCells();
|
||||
//case 4 -> //method call here
|
||||
case 4 -> matchCellsExpanded();
|
||||
case 5 -> acknowledge();
|
||||
case 0 -> quit = true;
|
||||
default -> throw new InputMismatchException("Invalid input.");
|
||||
@@ -42,10 +42,9 @@ public class UserInterface {
|
||||
String filename = null;
|
||||
Integer numCells = 0;
|
||||
try {
|
||||
System.out.println("\nSimulated T-Cells consist of matched pairs of alpha and beta peptides, " +
|
||||
"represented by unique integer values.");
|
||||
System.out.println("(Note: peptide values are unique within a simulated population, " +
|
||||
"but repeated between simulated populations.)");
|
||||
System.out.println("\nSimulated T-Cells consist of integer values representing:\n" +
|
||||
"* a pair of alpha and beta CDR3 peptides (unique within simulated population)\n" +
|
||||
"* a pair of alpha and beta CDR1 peptides (not necessarily unique).");
|
||||
System.out.println("\nThe cells will be written to a file.");
|
||||
System.out.print("Please enter a file name: ");
|
||||
filename = sc.next();
|
||||
@@ -58,7 +57,7 @@ public class UserInterface {
|
||||
System.out.println(ex);
|
||||
sc.next();
|
||||
}
|
||||
CellSample sample = Simulator.generateCellSample(numCells);
|
||||
CellSample sample = Simulator.generateExpandedCellSample(numCells);
|
||||
CellFileWriter writer = new CellFileWriter(filename, sample);
|
||||
writer.writeCellsToFile();
|
||||
}
|
||||
@@ -187,7 +186,82 @@ public class UserInterface {
|
||||
highThreshold = plate.getSize() - 1;
|
||||
}
|
||||
List<Integer[]> cells = cellReader.getCells();
|
||||
Simulator.matchCDR3s(filename, cells, plate, lowThreshold, highThreshold);
|
||||
MatchingResult results = Simulator.matchCDR3s(cells, plate, lowThreshold, highThreshold);
|
||||
//result writer
|
||||
MatchingFileWriter writer = new MatchingFileWriter(filename, results.getComments(),
|
||||
results.getHeaders(), results.getAllResults());
|
||||
writer.writeResultsToFile();
|
||||
}
|
||||
}
|
||||
|
||||
public static void matchCellsExpanded(){
|
||||
/*
|
||||
The idea here is that we'll get the CDR3 alpha/beta matches first. Then we'll try to match CDR3s to CDR1s by
|
||||
looking at the top two matches for each CDR3. If CDR3s in the same cell simply swap CDR1s, we assume a correct
|
||||
match
|
||||
*/
|
||||
String filename = null;
|
||||
String cellFile = null;
|
||||
String plateFile = null;
|
||||
Integer lowThresholdCDR3 = 0;
|
||||
Integer highThresholdCDR3 = Integer.MAX_VALUE;
|
||||
Integer lowThresholdCDR1 = 0;
|
||||
Integer highThresholdCDR1 = Integer.MAX_VALUE;
|
||||
try {
|
||||
System.out.println("\nSimulated experiment requires a cell sample file and a sample plate file.");
|
||||
System.out.print("Please enter name of an existing cell sample file: ");
|
||||
cellFile = sc.next();
|
||||
System.out.print("Please enter name of an existing sample plate file: ");
|
||||
plateFile = sc.next();
|
||||
System.out.println("The matching results will be written to a file.");
|
||||
System.out.print("Please enter a name for the output file: ");
|
||||
filename = sc.next();
|
||||
System.out.println("What is the minimum number of CDR3 alpha/beta overlap wells to attempt matching?");
|
||||
lowThresholdCDR3 = sc.nextInt();
|
||||
if(lowThresholdCDR3 < 1){
|
||||
throw new InputMismatchException("Minimum value for low threshold is 1");
|
||||
}
|
||||
System.out.println("What is the maximum number of CDR3 alpha/beta overlap wells to attempt matching?");
|
||||
highThresholdCDR3 = sc.nextInt();
|
||||
System.out.println("What is the minimum number of CDR3/CDR1 overlap wells to attempt matching?");
|
||||
lowThresholdCDR1 = sc.nextInt();
|
||||
if(lowThresholdCDR1 < 1){
|
||||
throw new InputMismatchException("Minimum value for low threshold is 1");
|
||||
}
|
||||
System.out.println("What is the maximum number of CDR3/CDR1 overlap wells to attempt matching?");
|
||||
highThresholdCDR1 = sc.nextInt();
|
||||
} catch (InputMismatchException ex) {
|
||||
System.out.println(ex);
|
||||
sc.next();
|
||||
}
|
||||
CellFileReader cellReader = new CellFileReader(cellFile);
|
||||
PlateFileReader plateReader = new PlateFileReader(plateFile);
|
||||
Plate plate = new Plate(plateReader.getWells());
|
||||
if (cellReader.getCells().size() == 0){
|
||||
System.out.println("No cell sample found.");
|
||||
System.out.println("Returning to main menu.");
|
||||
}
|
||||
else if(plate.getWells().size() == 0){
|
||||
System.out.println("No sample plate found.");
|
||||
System.out.println("Returning to main menu.");
|
||||
|
||||
}
|
||||
else{
|
||||
if(highThresholdCDR3 >= plate.getSize()){
|
||||
highThresholdCDR3 = plate.getSize() - 1;
|
||||
}
|
||||
if(highThresholdCDR1 >= plate.getSize()){
|
||||
highThresholdCDR1 = plate.getSize() - 1;
|
||||
}
|
||||
List<Integer[]> cells = cellReader.getCells();
|
||||
MatchingResult preliminaryResults = Simulator.matchCDR3s(cells, plate, lowThresholdCDR3, highThresholdCDR3);
|
||||
MatchingResult results = Simulator.matchCDR1s(cells, plate, lowThresholdCDR1,
|
||||
highThresholdCDR1, preliminaryResults.getMatchMap());
|
||||
|
||||
//result writer
|
||||
MatchingFileWriter writer = new MatchingFileWriter(filename, results.getComments(),
|
||||
results.getHeaders(), results.getAllResults());
|
||||
writer.writeResultsToFile();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user