Initial Commit - works well, but too memory intensive

This commit is contained in:
2021-11-09 20:24:22 -06:00
commit 82d5500ae8
3 changed files with 339 additions and 0 deletions

102
src/main/java/Plate.java Normal file
View File

@@ -0,0 +1,102 @@
import java.sql.Array;
import java.util.*;
public class Plate {
private List<List<Integer[]>> wells;
private Random rand = new Random();
private int size;
private double error;
public Plate (int size, double error) {
this.size = size;
this.error = error;
wells = new ArrayList<>();
}
public void fillWells(List<Integer[]> cells, int[] concentrations, double stdDev) {
int numSections = concentrations.length;
int section = 0;
double m;
int n;
boolean drop;
while (section < numSections){
for (int i = 0; i < (size / numSections); i++) {
List<Integer[]> well = new ArrayList<>();
for (int j = 0; j < concentrations[section]; j++) {
do {
m = Math.abs(rand.nextGaussian()) * stdDev;
} while (m >= cells.size());
n = (int) Math.floor(m);
Integer[] cellToAdd = cells.get(n).clone();
drop = Math.abs(rand.nextDouble()) < error;
if (drop) {
if (rand.nextBoolean()) {
cellToAdd[0] = -1;
} else {
cellToAdd[1] = -1;
}
}
well.add(cellToAdd);
}
wells.add(well);
}
section++;
}
}
public Map<Integer, Integer> assayWellsAlpha() {
return this.assayWellsAlpha(0, size);
}
public Map<Integer, Integer> assayWellsAlpha(int n) {
return this.assayWellsAlpha(n, n+1);
}
public Map<Integer, Integer> assayWellsAlpha(int start, int end) {
Map<Integer, Integer> assay = new HashMap<>();
for(int i = start; i < end; i++){
countAlphas(assay, wells.get(i));
}
return assay;
}
//given a map, counts distinct alphas in a well
private void countAlphas(Map<Integer, Integer> wellMap, List<Integer[]> well){
for(Integer[] cell : well) {
if(cell[0] != -1){
//keys are alphas, value is how many of them have been assayed
wellMap.merge(cell[0], 1, (oldValue, newValue) -> oldValue + newValue);
}
}
}
//assays all wells
public Map<Integer, Integer> assayWellsBeta() {
return this.assayWellsBeta(0, size);
}
//assays a specific well
public Map<Integer, Integer> assayWellsBeta(int n) {
return this.assayWellsBeta(n, n+1);
}
//assays a range of wells
public Map<Integer, Integer> assayWellsBeta(int start, int end) {
Map<Integer, Integer> assay = new HashMap<>();
for(int i = start; i < end; i++){
countBetas(assay, wells.get(i));
}
return assay;
}
//given a map, counts distinct betas in a well
private void countBetas(Map<Integer, Integer> wellMap, List<Integer[]> well){
for(Integer[] cell : well) {
if(cell[1] != -1){
wellMap.merge(cell[1], 1, (oldValue, newValue) -> oldValue + newValue);
}
}
}
}