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 {
private String filename;
private List<Integer[]> distinctCells = new ArrayList<>();
public CellFileReader(String filename) {
if(!filename.matches(".*\\.csv")){
filename = filename + ".csv";
}
this.filename = filename;
CSVFormat cellFileFormat = CSVFormat.Builder.create()
.setHeader("Alpha CDR3", "Beta CDR3", "Alpha CDR1", "Beta CDR1")
.setSkipHeaderRecord(true)
.setCommentMarker('#')
.build();
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(){
return distinctCells;
}

View File

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

View File

@@ -3,13 +3,15 @@ import java.util.List;
import java.util.Map;
public class MatchingResult {
private String sourceFile;
private List<String> comments;
private List<String> headers;
private List<List<String>> allResults;
private Map<Integer, Integer> matchMap;
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.headers = headers;
this.allResults = allResults;
@@ -37,4 +39,8 @@ public class MatchingResult {
public Duration getTime() {
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 {
private String sourceFile;
private List<List<Integer[]>> wells;
private Random rand = new Random();
private int size;
@@ -12,6 +13,7 @@ public class Plate {
private Integer[] concentrations;
private double stdDev;
public Plate (int size, double error, Integer[] concentrations, double stdDev) {
this.size = size;
this.error = error;
@@ -20,12 +22,14 @@ public class Plate {
wells = new ArrayList<>();
}
public Plate(List<List<Integer[]>> wells){
public Plate(String sourceFileName, List<List<Integer[]>> wells){
this.sourceFile = sourceFileName;
this.wells = wells;
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 section = 0;
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 {
private List<List<Integer[]>> wells = new ArrayList<>();
private String filename;
public PlateFileReader(String filename){
if(!filename.matches(".*\\.csv")){
filename = filename + ".csv";
}
this.filename = filename;
CSVFormat plateFileFormat = CSVFormat.Builder.create()
.setCommentMarker('#')
@@ -58,4 +60,7 @@ public class PlateFileReader {
return wells;
}
public String getFilename() {
return filename;
}
}

View File

@@ -15,6 +15,7 @@ public class PlateFileWriter {
private double stdDev;
private Double error;
private String filename;
private String sourceFileName;
private String[] headers;
private List<Integer> concentrations;
@@ -23,6 +24,7 @@ public class PlateFileWriter {
filename = filename + ".csv";
}
this.filename = filename;
this.sourceFileName = plate.getSourceFileName();
this.size = plate.getSize();
this.stdDev = plate.getStdDev();
this.error = plate.getError();
@@ -32,10 +34,6 @@ public class PlateFileWriter {
}
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);
wells.sort(listLengthDescending.reversed());
int maxLength = wells.get(0).size();
@@ -79,6 +77,7 @@ public class PlateFileWriter {
try(BufferedWriter writer = Files.newBufferedWriter(Path.of(filename), StandardOpenOption.CREATE_NEW);
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("Plate size: " + size);
printer.printComment("Error rate: " + error);

View File

@@ -213,7 +213,7 @@ public class Simulator {
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.
@@ -466,7 +466,8 @@ public class Simulator {
headers.add("second matched CDR1");
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
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");
MatchingResult dualTest = new MatchingResult(comments, headers, allResults, dualMatchesMap, time);
MatchingResult dualTest = new MatchingResult(samplePlate.getSourceFileName(), comments, headers,
allResults, dualMatchesMap, time);
MatchingResult[] output = {firstTest, dualTest};
return output;
}

View File

@@ -1,6 +1,8 @@
import java.util.List;
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//
public class UserInterface {
@@ -142,7 +144,7 @@ public class UserInterface {
stdDev = Math.sqrt(cellReader.getCellCount()); //gaussian with square root of elements approximates poisson
}
Plate samplePlate = new Plate(numWells, dropOutRate, concentrations, stdDev);
samplePlate.fillWells(cellReader.getCells());
samplePlate.fillWells(cellReader.getFilename(), cellReader.getCells());
PlateFileWriter writer = new PlateFileWriter(filename, samplePlate);
writer.writePlateFile();
}
@@ -175,7 +177,7 @@ public class UserInterface {
}
CellFileReader cellReader = new CellFileReader(cellFile);
PlateFileReader plateReader = new PlateFileReader(plateFile);
Plate plate = new Plate(plateReader.getWells());
Plate plate = new Plate(plateReader.getFilename(), plateReader.getWells());
if (cellReader.getCells().size() == 0){
System.out.println("No cell sample found.");
System.out.println("Returning to main menu.");
@@ -192,8 +194,7 @@ public class UserInterface {
List<Integer[]> cells = cellReader.getCells();
MatchingResult results = Simulator.matchCDR3s(cells, plate, lowThreshold, highThreshold);
//result writer
MatchingFileWriter writer = new MatchingFileWriter(filename, results.getComments(),
results.getHeaders(), results.getAllResults());
MatchingFileWriter writer = new MatchingFileWriter(filename, results);
writer.writeResultsToFile();
}
}
@@ -205,12 +206,14 @@ public class UserInterface {
match
*/
String filename = null;
String preliminaryResultsFilename = null;
String cellFile = null;
String plateFile = null;
Integer lowThresholdCDR3 = 0;
Integer highThresholdCDR3 = Integer.MAX_VALUE;
Integer lowThresholdCDR1 = 0;
Integer highThresholdCDR1 = Integer.MAX_VALUE;
boolean outputCDR3Matches = false;
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: ");
@@ -234,13 +237,28 @@ public class UserInterface {
}
System.out.println("What is the maximum number of CDR3/CDR1 overlap wells to attempt matching?");
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) {
System.out.println(ex);
sc.next();
}
CellFileReader cellReader = new CellFileReader(cellFile);
PlateFileReader plateReader = new PlateFileReader(plateFile);
Plate plate = new Plate(plateReader.getWells());
Plate plate = new Plate(plateReader.getFilename(), plateReader.getWells());
if (cellReader.getCells().size() == 0){
System.out.println("No cell sample found.");
System.out.println("Returning to main menu.");
@@ -261,14 +279,14 @@ public class UserInterface {
MatchingResult preliminaryResults = Simulator.matchCDR3s(cells, plate, lowThresholdCDR3, highThresholdCDR3);
MatchingResult[] results = Simulator.matchCDR1s(cells, plate, lowThresholdCDR1,
highThresholdCDR1, preliminaryResults);
//result writer
MatchingFileWriter writer = new MatchingFileWriter(filename + "First", results[0].getComments(),
results[0].getHeaders(), results[0].getAllResults());
MatchingFileWriter writer = new MatchingFileWriter(filename + "_FirstPass", results[0]);
writer.writeResultsToFile();
writer = new MatchingFileWriter(filename + "Dual", results[1].getComments(),
results[1].getHeaders(), results[1].getAllResults());
writer = new MatchingFileWriter(filename + "_SecondPass", results[1]);
writer.writeResultsToFile();
if(outputCDR3Matches){
writer = new MatchingFileWriter(preliminaryResultsFilename, preliminaryResults);
writer.writeResultsToFile();
}
}
}