Recording source file names in output files, allowing output of intermediate results

This commit is contained in:
2021-11-18 15:38:29 -06:00
parent 09aa5961f3
commit 2ab93dd4b7
8 changed files with 73 additions and 29 deletions

View File

@@ -11,17 +11,19 @@ import java.util.List;
public class CellFileReader { public class CellFileReader {
private String filename;
private List<Integer[]> distinctCells = new ArrayList<>(); private List<Integer[]> distinctCells = new ArrayList<>();
public CellFileReader(String filename) { public CellFileReader(String filename) {
if(!filename.matches(".*\\.csv")){ if(!filename.matches(".*\\.csv")){
filename = filename + ".csv"; filename = filename + ".csv";
} }
this.filename = filename;
CSVFormat cellFileFormat = CSVFormat.Builder.create() CSVFormat cellFileFormat = CSVFormat.Builder.create()
.setHeader("Alpha CDR3", "Beta CDR3", "Alpha CDR1", "Beta CDR1") .setHeader("Alpha CDR3", "Beta CDR3", "Alpha CDR1", "Beta CDR1")
.setSkipHeaderRecord(true) .setSkipHeaderRecord(true)
.setCommentMarker('#')
.build(); .build();
try(//don't need to close reader bc of try-with-resources auto-closing try(//don't need to close reader bc of try-with-resources auto-closing
@@ -42,6 +44,8 @@ public class CellFileReader {
} }
} }
public String getFilename() { return filename;}
public List<Integer[]> getCells(){ public List<Integer[]> getCells(){
return distinctCells; return distinctCells;
} }

View File

@@ -12,18 +12,20 @@ import java.util.List;
public class MatchingFileWriter { public class MatchingFileWriter {
private String filename; private String filename;
private String sourceFileName;
private List<String> comments; private List<String> comments;
private List<String> headers; private List<String> headers;
private List<List<String>> results; private List<List<String>> allResults;
public MatchingFileWriter(String filename, List<String> comments, List<String> headers, List<List<String>> results){ public MatchingFileWriter(String filename, MatchingResult result){
if(!filename.matches(".*\\.csv")){ if(!filename.matches(".*\\.csv")){
filename = filename + ".csv"; filename = filename + ".csv";
} }
this.filename = filename; this.filename = filename;
this.comments = comments; this.sourceFileName = result.getSourceFileName();
this.headers = headers; this.comments = result.getComments();
this.results = results; this.headers = result.getHeaders();
this.allResults = result.getAllResults();
} }
public void writeResultsToFile(){ public void writeResultsToFile(){
@@ -41,8 +43,8 @@ public class MatchingFileWriter {
for(String comment: comments){ for(String comment: comments){
printer.printComment(comment); printer.printComment(comment);
} }
results.add(0, headers); allResults.add(0, headers);
printer.printRecords(results); printer.printRecords(allResults);
} catch(IOException ex){ } catch(IOException ex){
System.out.println("Could not make new file named "+filename); System.out.println("Could not make new file named "+filename);

View File

@@ -3,13 +3,15 @@ import java.util.List;
import java.util.Map; import java.util.Map;
public class MatchingResult { public class MatchingResult {
private String sourceFile;
private List<String> comments; private List<String> comments;
private List<String> headers; private List<String> headers;
private List<List<String>> allResults; private List<List<String>> allResults;
private Map<Integer, Integer> matchMap; private Map<Integer, Integer> matchMap;
private Duration time; private Duration time;
public MatchingResult(List<String> comments, List<String> headers, List<List<String>> allResults, Map<Integer, Integer>matchMap, Duration time){ public MatchingResult(String sourceFileName, List<String> comments, List<String> headers, List<List<String>> allResults, Map<Integer, Integer>matchMap, Duration time){
this.sourceFile = sourceFileName;
this.comments = comments; this.comments = comments;
this.headers = headers; this.headers = headers;
this.allResults = allResults; this.allResults = allResults;
@@ -37,4 +39,8 @@ public class MatchingResult {
public Duration getTime() { public Duration getTime() {
return time; return time;
} }
public String getSourceFileName() {
return sourceFile;
}
} }

View File

@@ -5,6 +5,7 @@ TODO: Implement discrete frequency distributions using Vose's Alias Method
*/ */
public class Plate { public class Plate {
private String sourceFile;
private List<List<Integer[]>> wells; private List<List<Integer[]>> wells;
private Random rand = new Random(); private Random rand = new Random();
private int size; private int size;
@@ -12,6 +13,7 @@ public class Plate {
private Integer[] concentrations; private Integer[] concentrations;
private double stdDev; private double stdDev;
public Plate (int size, double error, Integer[] concentrations, double stdDev) { public Plate (int size, double error, Integer[] concentrations, double stdDev) {
this.size = size; this.size = size;
this.error = error; this.error = error;
@@ -20,12 +22,14 @@ public class Plate {
wells = new ArrayList<>(); wells = new ArrayList<>();
} }
public Plate(List<List<Integer[]>> wells){ public Plate(String sourceFileName, List<List<Integer[]>> wells){
this.sourceFile = sourceFileName;
this.wells = wells; this.wells = wells;
this.size = wells.size(); this.size = wells.size();
} }
public void fillWells(List<Integer[]> cells) { public void fillWells(String sourceFileName, List<Integer[]> cells) {
sourceFile = sourceFileName;
int numSections = concentrations.length; int numSections = concentrations.length;
int section = 0; int section = 0;
double m; double m;
@@ -100,4 +104,8 @@ public class Plate {
} }
} }
} }
public String getSourceFileName() {
return sourceFile;
}
} }

View File

@@ -14,12 +14,14 @@ import java.util.regex.Pattern;
public class PlateFileReader { public class PlateFileReader {
private List<List<Integer[]>> wells = new ArrayList<>(); private List<List<Integer[]>> wells = new ArrayList<>();
private String filename;
public PlateFileReader(String filename){ public PlateFileReader(String filename){
if(!filename.matches(".*\\.csv")){ if(!filename.matches(".*\\.csv")){
filename = filename + ".csv"; filename = filename + ".csv";
} }
this.filename = filename;
CSVFormat plateFileFormat = CSVFormat.Builder.create() CSVFormat plateFileFormat = CSVFormat.Builder.create()
.setCommentMarker('#') .setCommentMarker('#')
@@ -58,4 +60,7 @@ public class PlateFileReader {
return wells; return wells;
} }
public String getFilename() {
return filename;
}
} }

View File

@@ -15,6 +15,7 @@ public class PlateFileWriter {
private double stdDev; private double stdDev;
private Double error; private Double error;
private String filename; private String filename;
private String sourceFileName;
private String[] headers; private String[] headers;
private List<Integer> concentrations; private List<Integer> concentrations;
@@ -23,6 +24,7 @@ public class PlateFileWriter {
filename = filename + ".csv"; filename = filename + ".csv";
} }
this.filename = filename; this.filename = filename;
this.sourceFileName = plate.getSourceFileName();
this.size = plate.getSize(); this.size = plate.getSize();
this.stdDev = plate.getStdDev(); this.stdDev = plate.getStdDev();
this.error = plate.getError(); this.error = plate.getError();
@@ -32,10 +34,6 @@ public class PlateFileWriter {
} }
public void writePlateFile(){ public void writePlateFile(){
//works as is, but too many columns in csv, need to make them all rows.
//will now redo it so that every column is a well, with well names as headers
//need to give plate error, sample pop size, stdDev, num sections, concentration per section as comments
Comparator<List<Integer[]>> listLengthDescending = Comparator.comparingInt(List::size); Comparator<List<Integer[]>> listLengthDescending = Comparator.comparingInt(List::size);
wells.sort(listLengthDescending.reversed()); wells.sort(listLengthDescending.reversed());
int maxLength = wells.get(0).size(); int maxLength = wells.get(0).size();
@@ -79,6 +77,7 @@ public class PlateFileWriter {
try(BufferedWriter writer = Files.newBufferedWriter(Path.of(filename), StandardOpenOption.CREATE_NEW); try(BufferedWriter writer = Files.newBufferedWriter(Path.of(filename), StandardOpenOption.CREATE_NEW);
CSVPrinter printer = new CSVPrinter(writer, plateFileFormat); CSVPrinter printer = new CSVPrinter(writer, plateFileFormat);
){ ){
printer.printComment("Cell source file name: " + sourceFileName);
printer.printComment("Each row represents one well on the plate."); printer.printComment("Each row represents one well on the plate.");
printer.printComment("Plate size: " + size); printer.printComment("Plate size: " + size);
printer.printComment("Error rate: " + error); printer.printComment("Error rate: " + error);

View File

@@ -213,7 +213,7 @@ public class Simulator {
System.out.println(s); System.out.println(s);
} }
return new MatchingResult(comments, header, allResults, matchMap, time); return new MatchingResult(samplePlate.getSourceFileName(), comments, header, allResults, matchMap, time);
} }
//Simulated matching of CDR1s to CDR3s. Requires MatchingResult from prior run of matchCDR3s. //Simulated matching of CDR1s to CDR3s. Requires MatchingResult from prior run of matchCDR3s.
@@ -466,7 +466,8 @@ public class Simulator {
headers.add("second matched CDR1"); headers.add("second matched CDR1");
headers.add("Correct match?"); headers.add("Correct match?");
MatchingResult firstTest = new MatchingResult(comments, headers, allResults, dualMatchesMap, time); MatchingResult firstTest = new MatchingResult(samplePlate.getSourceFileName(),
comments, headers, allResults, dualMatchesMap, time);
//results for dual map //results for dual map
System.out.println("RESULTS FOR SECOND PASS MATCHING"); System.out.println("RESULTS FOR SECOND PASS MATCHING");
@@ -515,7 +516,8 @@ public class Simulator {
} }
System.out.println("Simulation time: " + nf.format(time.toSeconds()) + " seconds"); System.out.println("Simulation time: " + nf.format(time.toSeconds()) + " seconds");
MatchingResult dualTest = new MatchingResult(comments, headers, allResults, dualMatchesMap, time); MatchingResult dualTest = new MatchingResult(samplePlate.getSourceFileName(), comments, headers,
allResults, dualMatchesMap, time);
MatchingResult[] output = {firstTest, dualTest}; MatchingResult[] output = {firstTest, dualTest};
return output; return output;
} }

View File

@@ -1,6 +1,8 @@
import java.util.List; import java.util.List;
import java.util.Scanner; import java.util.Scanner;
import java.util.InputMismatchException; import java.util.InputMismatchException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// //
public class UserInterface { public class UserInterface {
@@ -142,7 +144,7 @@ public class UserInterface {
stdDev = Math.sqrt(cellReader.getCellCount()); //gaussian with square root of elements approximates poisson stdDev = Math.sqrt(cellReader.getCellCount()); //gaussian with square root of elements approximates poisson
} }
Plate samplePlate = new Plate(numWells, dropOutRate, concentrations, stdDev); Plate samplePlate = new Plate(numWells, dropOutRate, concentrations, stdDev);
samplePlate.fillWells(cellReader.getCells()); samplePlate.fillWells(cellReader.getFilename(), cellReader.getCells());
PlateFileWriter writer = new PlateFileWriter(filename, samplePlate); PlateFileWriter writer = new PlateFileWriter(filename, samplePlate);
writer.writePlateFile(); writer.writePlateFile();
} }
@@ -175,7 +177,7 @@ public class UserInterface {
} }
CellFileReader cellReader = new CellFileReader(cellFile); CellFileReader cellReader = new CellFileReader(cellFile);
PlateFileReader plateReader = new PlateFileReader(plateFile); PlateFileReader plateReader = new PlateFileReader(plateFile);
Plate plate = new Plate(plateReader.getWells()); Plate plate = new Plate(plateReader.getFilename(), plateReader.getWells());
if (cellReader.getCells().size() == 0){ if (cellReader.getCells().size() == 0){
System.out.println("No cell sample found."); System.out.println("No cell sample found.");
System.out.println("Returning to main menu."); System.out.println("Returning to main menu.");
@@ -192,8 +194,7 @@ public class UserInterface {
List<Integer[]> cells = cellReader.getCells(); List<Integer[]> cells = cellReader.getCells();
MatchingResult results = Simulator.matchCDR3s(cells, plate, lowThreshold, highThreshold); MatchingResult results = Simulator.matchCDR3s(cells, plate, lowThreshold, highThreshold);
//result writer //result writer
MatchingFileWriter writer = new MatchingFileWriter(filename, results.getComments(), MatchingFileWriter writer = new MatchingFileWriter(filename, results);
results.getHeaders(), results.getAllResults());
writer.writeResultsToFile(); writer.writeResultsToFile();
} }
} }
@@ -205,12 +206,14 @@ public class UserInterface {
match match
*/ */
String filename = null; String filename = null;
String preliminaryResultsFilename = null;
String cellFile = null; String cellFile = null;
String plateFile = null; String plateFile = null;
Integer lowThresholdCDR3 = 0; Integer lowThresholdCDR3 = 0;
Integer highThresholdCDR3 = Integer.MAX_VALUE; Integer highThresholdCDR3 = Integer.MAX_VALUE;
Integer lowThresholdCDR1 = 0; Integer lowThresholdCDR1 = 0;
Integer highThresholdCDR1 = Integer.MAX_VALUE; Integer highThresholdCDR1 = Integer.MAX_VALUE;
boolean outputCDR3Matches = false;
try { try {
System.out.println("\nSimulated experiment requires a cell sample file and a sample plate file."); 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: "); System.out.print("Please enter name of an existing cell sample file: ");
@@ -234,13 +237,28 @@ public class UserInterface {
} }
System.out.println("What is the maximum number of CDR3/CDR1 overlap wells to attempt matching?"); System.out.println("What is the maximum number of CDR3/CDR1 overlap wells to attempt matching?");
highThresholdCDR1 = sc.nextInt(); highThresholdCDR1 = sc.nextInt();
System.out.println("Matching CDR3s to CDR1s requires first matching CDR3 alpha/betas.");
System.out.println("Output a file for CDR3 alpha/beta match results as well?");
System.out.print("Please enter y/n: ");
String ans = sc.next();
Pattern pattern = Pattern.compile("(?:yes|y)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(ans);
if(matcher.matches()){
outputCDR3Matches = true;
System.out.println("Please enter filename for CDR3 alpha/beta match results");
preliminaryResultsFilename = sc.next();
System.out.println("CDR3 alpha/beta matches will be output to file");
}
else{
System.out.println("CDR3 alpha/beta matches will not be output to file");
}
} catch (InputMismatchException ex) { } catch (InputMismatchException ex) {
System.out.println(ex); System.out.println(ex);
sc.next(); sc.next();
} }
CellFileReader cellReader = new CellFileReader(cellFile); CellFileReader cellReader = new CellFileReader(cellFile);
PlateFileReader plateReader = new PlateFileReader(plateFile); PlateFileReader plateReader = new PlateFileReader(plateFile);
Plate plate = new Plate(plateReader.getWells()); Plate plate = new Plate(plateReader.getFilename(), plateReader.getWells());
if (cellReader.getCells().size() == 0){ if (cellReader.getCells().size() == 0){
System.out.println("No cell sample found."); System.out.println("No cell sample found.");
System.out.println("Returning to main menu."); System.out.println("Returning to main menu.");
@@ -261,14 +279,14 @@ public class UserInterface {
MatchingResult preliminaryResults = Simulator.matchCDR3s(cells, plate, lowThresholdCDR3, highThresholdCDR3); MatchingResult preliminaryResults = Simulator.matchCDR3s(cells, plate, lowThresholdCDR3, highThresholdCDR3);
MatchingResult[] results = Simulator.matchCDR1s(cells, plate, lowThresholdCDR1, MatchingResult[] results = Simulator.matchCDR1s(cells, plate, lowThresholdCDR1,
highThresholdCDR1, preliminaryResults); highThresholdCDR1, preliminaryResults);
MatchingFileWriter writer = new MatchingFileWriter(filename + "_FirstPass", results[0]);
//result writer
MatchingFileWriter writer = new MatchingFileWriter(filename + "First", results[0].getComments(),
results[0].getHeaders(), results[0].getAllResults());
writer.writeResultsToFile(); writer.writeResultsToFile();
writer = new MatchingFileWriter(filename + "Dual", results[1].getComments(), writer = new MatchingFileWriter(filename + "_SecondPass", results[1]);
results[1].getHeaders(), results[1].getAllResults());
writer.writeResultsToFile(); writer.writeResultsToFile();
if(outputCDR3Matches){
writer = new MatchingFileWriter(preliminaryResultsFilename, preliminaryResults);
writer.writeResultsToFile();
}
} }
} }