Cache everything

This commit is contained in:
2022-02-23 10:30:42 -06:00
parent 74ffbfd8ac
commit decdb147a9
5 changed files with 96 additions and 29 deletions

View File

@@ -34,6 +34,11 @@ public class BiGpairSEQ {
BiGpairSEQ.cellSampleInMemory = cellSampleInMemory; BiGpairSEQ.cellSampleInMemory = cellSampleInMemory;
} }
public static void clearCellSampleInMemory() {
cellSampleInMemory = null;
System.gc();
}
public static String getCellFilename() { public static String getCellFilename() {
return cellFilename; return cellFilename;
} }
@@ -50,7 +55,7 @@ public class BiGpairSEQ {
BiGpairSEQ.plateInMemory = plateInMemory; BiGpairSEQ.plateInMemory = plateInMemory;
} }
public static void clearPlate() { public static void clearPlateInMemory() {
plateInMemory = null; plateInMemory = null;
System.gc(); System.gc();
} }

View File

@@ -13,6 +13,7 @@ public class CellFileReader {
private String filename; private String filename;
private List<Integer[]> distinctCells = new ArrayList<>(); private List<Integer[]> distinctCells = new ArrayList<>();
private Integer cdr1Freq;
public CellFileReader(String filename) { public CellFileReader(String filename) {
if(!filename.matches(".*\\.csv")){ if(!filename.matches(".*\\.csv")){
@@ -38,19 +39,37 @@ public class CellFileReader {
cell[3] = Integer.valueOf(record.get("Beta CDR1")); cell[3] = Integer.valueOf(record.get("Beta CDR1"));
distinctCells.add(cell); distinctCells.add(cell);
} }
} catch(IOException ex){ } catch(IOException ex){
System.out.println("cell file " + filename + " not found."); System.out.println("cell file " + filename + " not found.");
System.err.println(ex); System.err.println(ex);
} }
//get CDR1 frequency
ArrayList<Integer> 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;} public String getFilename() { return filename;}
public List<Integer[]> getCells(){ //Refactor everything that uses this to have access to a Cell Sample and get the cells there instead.
public List<Integer[]> getListOfDistinctCellsDEPRECATED(){
return distinctCells; return distinctCells;
} }
public Integer getCellCount() { public Integer getCellCountDEPRECATED() {
//Refactor everything that uses this to have access to a Cell Sample and get the count there instead.
return distinctCells.size(); return distinctCells.size();
} }
} }

View File

@@ -18,7 +18,7 @@ public class CellSample {
return cdr1Freq; return cdr1Freq;
} }
public Integer population(){ public Integer getCellCount(){
return cells.size(); return cells.size();
} }

View File

@@ -297,7 +297,7 @@ public class CommandLineInterface {
Integer numWells, Integer[] concentrations, Double dropOutRate){ Integer numWells, Integer[] concentrations, Double dropOutRate){
CellFileReader cellReader = new CellFileReader(cellFile); CellFileReader cellReader = new CellFileReader(cellFile);
Plate samplePlate = new Plate(numWells, dropOutRate, concentrations); Plate samplePlate = new Plate(numWells, dropOutRate, concentrations);
samplePlate.fillWellsExponential(cellReader.getFilename(), cellReader.getCells(), lambda); samplePlate.fillWellsExponential(cellReader.getFilename(), cellReader.getListOfDistinctCellsDEPRECATED(), lambda);
PlateFileWriter writer = new PlateFileWriter(filename, samplePlate); PlateFileWriter writer = new PlateFileWriter(filename, samplePlate);
writer.writePlateFile(); writer.writePlateFile();
} }
@@ -305,9 +305,9 @@ public class CommandLineInterface {
private static void makePlatePoisson(String cellFile, String filename, Integer numWells, private static void makePlatePoisson(String cellFile, String filename, Integer numWells,
Integer[] concentrations, Double dropOutRate){ Integer[] concentrations, Double dropOutRate){
CellFileReader cellReader = new CellFileReader(cellFile); CellFileReader cellReader = new CellFileReader(cellFile);
Double stdDev = Math.sqrt(cellReader.getCellCount()); Double stdDev = Math.sqrt(cellReader.getCellCountDEPRECATED());
Plate samplePlate = new Plate(numWells, dropOutRate, concentrations); Plate samplePlate = new Plate(numWells, dropOutRate, concentrations);
samplePlate.fillWells(cellReader.getFilename(), cellReader.getCells(), stdDev); samplePlate.fillWells(cellReader.getFilename(), cellReader.getListOfDistinctCellsDEPRECATED(), stdDev);
PlateFileWriter writer = new PlateFileWriter(filename, samplePlate); PlateFileWriter writer = new PlateFileWriter(filename, samplePlate);
writer.writePlateFile(); writer.writePlateFile();
} }
@@ -316,7 +316,7 @@ public class CommandLineInterface {
Integer numWells, Integer[] concentrations, Double dropOutRate){ Integer numWells, Integer[] concentrations, Double dropOutRate){
CellFileReader cellReader = new CellFileReader(cellFile); CellFileReader cellReader = new CellFileReader(cellFile);
Plate samplePlate = new Plate(numWells, dropOutRate, concentrations); Plate samplePlate = new Plate(numWells, dropOutRate, concentrations);
samplePlate.fillWells(cellReader.getFilename(), cellReader.getCells(), stdDev); samplePlate.fillWells(cellReader.getFilename(), cellReader.getListOfDistinctCellsDEPRECATED(), stdDev);
PlateFileWriter writer = new PlateFileWriter(filename, samplePlate); PlateFileWriter writer = new PlateFileWriter(filename, samplePlate);
writer.writePlateFile(); writer.writePlateFile();
} }

View File

@@ -74,8 +74,15 @@ public class InteractiveInterface {
} }
CellSample sample = Simulator.generateCellSample(numCells, cdr1Freq); CellSample sample = Simulator.generateCellSample(numCells, cdr1Freq);
assert filename != null; assert filename != null;
System.out.println("Writing cells to file");
CellFileWriter writer = new CellFileWriter(filename, sample); CellFileWriter writer = new CellFileWriter(filename, sample);
writer.writeCellsToFile(); writer.writeCellsToFile();
System.out.println("Cell sample written to: " + filename);
if(BiGpairSEQ.getCellSampleInMemory() != null) {
BiGpairSEQ.clearCellSampleInMemory();
}
BiGpairSEQ.setCellSampleInMemory(sample);
BiGpairSEQ.setCellFilename(filename);
} }
//Output a CSV of sample plate //Output a CSV of sample plate
@@ -203,27 +210,40 @@ public class InteractiveInterface {
System.out.println(ex); System.out.println(ex);
sc.next(); sc.next();
} }
System.out.println("Reading Cell Sample file: " + cellFile);
assert cellFile != null; assert cellFile != null;
CellFileReader cellReader = new CellFileReader(cellFile); CellSample cells;
if (cellFile.equals(BiGpairSEQ.getCellFilename())){
cells = BiGpairSEQ.getCellSampleInMemory();
}
else {
System.out.println("Reading Cell Sample file: " + cellFile);
CellFileReader cellReader = new CellFileReader(cellFile);
cells = cellReader.getCellSample();
BiGpairSEQ.clearCellSampleInMemory();
BiGpairSEQ.setCellSampleInMemory(cells);
BiGpairSEQ.setCellFilename(cellFile);
}
assert filename != null;
Plate samplePlate;
PlateFileWriter writer;
if(exponential){ if(exponential){
Plate samplePlate = new Plate(numWells, dropOutRate, populations); samplePlate = new Plate(numWells, dropOutRate, populations);
samplePlate.fillWellsExponential(cellReader.getFilename(), cellReader.getCells(), lambda); samplePlate.fillWellsExponential(cellFile, cells.getCells(), lambda);
PlateFileWriter writer = new PlateFileWriter(filename, samplePlate); writer = new PlateFileWriter(filename, samplePlate);
writer.writePlateFile();
} }
else { else {
if (poisson) { if (poisson) {
stdDev = Math.sqrt(cellReader.getCellCount()); //gaussian with square root of elements approximates poisson stdDev = Math.sqrt(cells.getCellCount()); //gaussian with square root of elements approximates poisson
} }
Plate samplePlate = new Plate(numWells, dropOutRate, populations); samplePlate = new Plate(numWells, dropOutRate, populations);
samplePlate.fillWells(cellReader.getFilename(), cellReader.getCells(), stdDev); samplePlate.fillWells(cellFile, cells.getCells(), stdDev);
assert filename != null; writer = new PlateFileWriter(filename, samplePlate);
PlateFileWriter writer = new PlateFileWriter(filename, samplePlate);
System.out.println("Writing Sample Plate to file");
writer.writePlateFile();
System.out.println("Sample Plate written to file: " + filename);
} }
System.out.println("Writing Sample Plate to file");
writer.writePlateFile();
System.out.println("Sample Plate written to file: " + filename);
BiGpairSEQ.setPlateInMemory(samplePlate);
BiGpairSEQ.setPlateFilename(filename);
} }
//Output serialized binary of GraphAndMapData object //Output serialized binary of GraphAndMapData object
@@ -247,14 +267,37 @@ public class InteractiveInterface {
System.out.println(ex); System.out.println(ex);
sc.next(); sc.next();
} }
System.out.println("Reading Cell Sample file: " + cellFile);
assert cellFile != null; assert cellFile != null;
CellFileReader cellReader = new CellFileReader(cellFile); CellSample cellSample;
System.out.println("Reading Sample Plate file: " + plateFile); //check if cells are already in memory
if(cellFile.equals(BiGpairSEQ.getCellFilename())) {
cellSample = BiGpairSEQ.getCellSampleInMemory();
}
else {
BiGpairSEQ.clearCellSampleInMemory();
System.out.println("Reading Cell Sample file: " + cellFile);
CellFileReader cellReader = new CellFileReader(cellFile);
cellSample = cellReader.getCellSample();
BiGpairSEQ.setCellSampleInMemory(cellSample);
BiGpairSEQ.setCellFilename(cellFile);
}
assert plateFile != null; assert plateFile != null;
PlateFileReader plateReader = new PlateFileReader(plateFile); Plate plate;
Plate plate = new Plate(plateReader.getFilename(), plateReader.getWells()); //check if plate is already in memory
if (cellReader.getCells().size() == 0){ if(plateFile.equals(BiGpairSEQ.getPlateFilename())){
plate = BiGpairSEQ.getPlateInMemory();
}
else {
BiGpairSEQ.clearPlateInMemory();
System.out.println("Reading Sample Plate file: " + plateFile);
PlateFileReader plateReader = new PlateFileReader(plateFile);
plate = new Plate(plateReader.getFilename(), plateReader.getWells());
BiGpairSEQ.setPlateInMemory(plate);
BiGpairSEQ.setPlateFilename(plateFile);
}
if (cellSample.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.");
} }
@@ -263,7 +306,7 @@ public class InteractiveInterface {
System.out.println("Returning to main menu."); System.out.println("Returning to main menu.");
} }
else{ else{
List<Integer[]> cells = cellReader.getCells(); List<Integer[]> cells = cellSample.getCells();
GraphWithMapData data = Simulator.makeGraph(cells, plate, true); GraphWithMapData data = Simulator.makeGraph(cells, plate, true);
assert filename != null; assert filename != null;
GraphDataObjectWriter dataWriter = new GraphDataObjectWriter(filename, data); GraphDataObjectWriter dataWriter = new GraphDataObjectWriter(filename, data);