import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; import java.io.BufferedReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; public class CellFileReader { private String filename; private List distinctCells = new ArrayList<>(); private Integer cdr1Freq; 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 BufferedReader reader = Files.newBufferedReader(Path.of(filename)); CSVParser parser = new CSVParser(reader, cellFileFormat); ){ for(CSVRecord record: parser.getRecords()) { Integer[] cell = new Integer[4]; 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){ System.out.println("cell file " + filename + " not found."); System.err.println(ex); } //get CDR1 frequency ArrayList cdr1Alphas = new ArrayList<>(); for (Integer[] cell : distinctCells) { cdr1Alphas.add(cell[3]); } double count = cdr1Alphas.stream().distinct().count(); count = Math.ceil(distinctCells.size() / count); cdr1Freq = (int) count; } public CellSample getCellSample() { return new CellSample(distinctCells, cdr1Freq); } public String getFilename() { return filename;} //Refactor everything that uses this to have access to a Cell Sample and get the cells there instead. public List getListOfDistinctCellsDEPRECATED(){ return distinctCells; } public Integer getCellCountDEPRECATED() { //Refactor everything that uses this to have access to a Cell Sample and get the count there instead. return distinctCells.size(); } }