111 lines
4.0 KiB
Java
111 lines
4.0 KiB
Java
import org.apache.commons.csv.CSVFormat;
|
|
import org.apache.commons.csv.CSVPrinter;
|
|
|
|
import java.io.BufferedWriter;
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.StandardOpenOption;
|
|
import java.util.*;
|
|
|
|
public class PlateFileWriter {
|
|
private int size;
|
|
private List<List<String[]>> wells;
|
|
private double stdDev;
|
|
private double lambda;
|
|
private Double error;
|
|
private String filename;
|
|
private String sourceFileName;
|
|
private Integer[] populations;
|
|
private boolean isExponential = false;
|
|
|
|
public PlateFileWriter(String filename, Plate plate) {
|
|
if(!filename.matches(".*\\.csv")){
|
|
filename = filename + ".csv";
|
|
}
|
|
this.filename = filename;
|
|
this.sourceFileName = plate.getSourceFileName();
|
|
this.size = plate.getSize();
|
|
this.isExponential = plate.isExponential();
|
|
if(isExponential) {
|
|
this.lambda = plate.getLambda();
|
|
}
|
|
else{
|
|
this.stdDev = plate.getStdDev();
|
|
}
|
|
this.error = plate.getError();
|
|
this.wells = plate.getWells();
|
|
this.populations = plate.getPopulations();
|
|
Arrays.sort(populations);
|
|
}
|
|
|
|
public void writePlateFile(){
|
|
Comparator<List<String[]>> listLengthDescending = Comparator.comparingInt(List::size);
|
|
wells.sort(listLengthDescending.reversed());
|
|
int maxLength = wells.get(0).size();
|
|
List<List<String>> wellsAsStrings = new ArrayList<>();
|
|
for (List<String[]> w: wells){
|
|
List<String> tmp = new ArrayList<>();
|
|
for(String[] c: w) {
|
|
tmp.add(Arrays.toString(c));
|
|
}
|
|
wellsAsStrings.add(tmp);
|
|
}
|
|
for(List<String> w: wellsAsStrings){
|
|
while(w.size() < maxLength){
|
|
w.add("");
|
|
}
|
|
}
|
|
|
|
// //this took forever and I don't use it
|
|
// //if I wanted to use it, I'd replace printer.printRecords(wellsAsStrings) with printer.printRecords(rows)
|
|
// List<List<String>> rows = new ArrayList<>();
|
|
// List<String> tmp = new ArrayList<>();
|
|
// for(int i = 0; i < wellsAsStrings.size(); i++){//List<Integer[]> w: wells){
|
|
// tmp.add("well " + (i+1));
|
|
// }
|
|
// rows.add(tmp);
|
|
// for(int row = 0; row < maxLength; row++){
|
|
// tmp = new ArrayList<>();
|
|
// for(List<String> c: wellsAsStrings){
|
|
// tmp.add(c.get(row));
|
|
// }
|
|
// rows.add(tmp);
|
|
// }
|
|
|
|
//make string out of populations array
|
|
StringBuilder populationsStringBuilder = new StringBuilder();
|
|
populationsStringBuilder.append(populations[0].toString());
|
|
for(int i = 1; i < populations.length; i++){
|
|
populationsStringBuilder.append(", ");
|
|
populationsStringBuilder.append(populations[i].toString());
|
|
}
|
|
String wellPopulationsString = populationsStringBuilder.toString();
|
|
|
|
//set CSV format
|
|
CSVFormat plateFileFormat = CSVFormat.Builder.create()
|
|
.setCommentMarker('#')
|
|
.build();
|
|
|
|
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);
|
|
printer.printComment("Well populations: " + wellPopulationsString);
|
|
if(isExponential){
|
|
printer.printComment("Lambda: " + lambda);
|
|
}
|
|
else {
|
|
printer.printComment("Std. dev.: " + stdDev);
|
|
}
|
|
printer.printRecords(wellsAsStrings);
|
|
} catch(IOException ex){
|
|
System.out.println("Could not make new file named "+filename);
|
|
System.err.println(ex);
|
|
}
|
|
}
|
|
}
|