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> wells; private double stdDev; private double lambda; private Double error; private String filename; private String sourceFileName; private String[] headers; private Integer[] concentrations; 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.concentrations = plate.getPopulations(); Arrays.sort(concentrations); } public void writePlateFile(){ Comparator> listLengthDescending = Comparator.comparingInt(List::size); wells.sort(listLengthDescending.reversed()); int maxLength = wells.get(0).size(); List> wellsAsStrings = new ArrayList<>(); for (List w: wells){ List tmp = new ArrayList<>(); for(Integer[] c: w) { tmp.add(Arrays.toString(c)); } wellsAsStrings.add(tmp); } for(List w: wellsAsStrings){ while(w.size() < maxLength){ w.add(""); } } //this took forever and I don't use it List> rows = new ArrayList<>(); List tmp = new ArrayList<>(); for(int i = 0; i < wellsAsStrings.size(); i++){//List w: wells){ tmp.add("well " + (i+1)); } rows.add(tmp); for(int row = 0; row < maxLength; row++){ tmp = new ArrayList<>(); for(List c: wellsAsStrings){ tmp.add(c.get(row)); } rows.add(tmp); } //get list of well populations List wellPopulations = Arrays.asList(concentrations); //make string out of populations list StringBuilder populationsStringBuilder = new StringBuilder(); populationsStringBuilder.append(wellPopulations.remove(0).toString()); for(Integer i: wellPopulations){ populationsStringBuilder.append(", "); populationsStringBuilder.append(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); } } }