Refactor to reduce code repetition

This commit is contained in:
2021-11-18 14:11:04 -06:00
parent 2064d7e9fc
commit 34e96d3b3d
3 changed files with 179 additions and 590 deletions

View File

@@ -1,6 +1,8 @@
import java.util.*;
//Need to write function to output plate to a file that I can read in.
/*
TODO: Implement discrete frequency distributions using Vose's Alias Method
*/
public class Plate {
private List<List<Integer[]>> wells;
@@ -28,8 +30,6 @@ public class Plate {
int section = 0;
double m;
int n;
//testing
//System.out.println("Cell size: " + cells.get(0).length);
while (section < numSections){
for (int i = 0; i < (size / numSections); i++) {
List<Integer[]> well = new ArrayList<>();
@@ -52,11 +52,6 @@ public class Plate {
}
}
public void writePlateToFile(String filename) {
}
public Integer[] getConcentrations(){
return concentrations;
}
@@ -77,142 +72,32 @@ public class Plate {
return wells;
}
//returns a map of counts of all the CDR3s (alphas and betas) in all wells
public Map<Integer, Integer>assayWellsCDR3(){
return this.assayWellsCDR3(0, size);
}
//returns a map of counts of all the CDR3 alphas in all wells
public Map<Integer, Integer> assayWellsCDR3Alpha() {
return this.assayWellsCDR3Alpha(0, size);
}
//returns a map of counts of all the CDR3 betas in all wells
public Map<Integer, Integer> assayWellsCDR3Beta() {
return this.assayWellsCDR3Beta(0, size);
}
//returns a map of counts of all CDR1s (alphas and betas) in all wells
public Map<Integer, Integer> assayWellsCDR1(){
return this.assayWellsCDR1(0, size);
}
//returns a map of counts of all the CDR1 alphas in all wells
public Map<Integer, Integer> assayWellsCDR1Alpha() {
return this.assayWellsCDR1Alpha(0, size);
}
//returns a map of counts of all the CDR1 betas in all wells
public Map<Integer, Integer> assayWellsCDR1Beta() {
return this.assayWellsCDR1Beta(0, size);
//returns a map of the counts of the peptide at cell index pIndex, in all wells
public Map<Integer, Integer> assayWellsPeptideP(int... pIndices){
return this.assayWellsPeptideP(0, size, pIndices);
}
//returns a map of counts of the CDR3s (alphas and betas) in a specific well
public Map<Integer, Integer>assayWellsCDR3(int n){
return this.assayWellsCDR3(n, n+1);
}
//returns a map of counts of the CDR1s (alphas and betas) in a specific well
public Map<Integer, Integer> assayWellsCDR1(int n){
return this.assayWellsCDR1(n, n+1);
}
//returns a map of counts of the CDR3 alphas in a specific well
public Map<Integer, Integer> assayWellsCDR3Alpha(int n) {
return this.assayWellsCDR3Alpha(n, n+1);
}
//returns a map of counts of the CDR3 betas in a specific well
public Map<Integer, Integer> assayWellsCDR3Beta(int n) {
return this.assayWellsCDR3Beta(n, n+1);
}
//returns a map of counts of the CDR1 alphas in a specific well
public Map<Integer, Integer> assayWellsCDR1Alpha(int n) {
return this.assayWellsCDR1Alpha(n, n+1);
}
//returns a map of counts of the CDR1 betas in a specific well
public Map<Integer, Integer> assayWellsCDR1Beta(int n) {
return this.assayWellsCDR1Beta(n, n+1);
}
//returns a map of the counts of the peptide at cell index pIndex, in a specific well
public Map<Integer, Integer> assayWellsPeptideP(int n, int... pIndices) { return this.assayWellsPeptideP(n, n+1, pIndices);}
//returns a map of the counts of the CDR3s (alphas and betas) in a range of wells
public Map<Integer, Integer>assayWellsCDR3(int start, int end){
//returns a map of the counts of the peptide at cell index pIndex, in a range of wells
public Map<Integer, Integer> assayWellsPeptideP(int start, int end, int... pIndices) {
Map<Integer,Integer> assay = new HashMap<>();
for(int i = start; i < end; i++){
countCDR3Alphas(assay, wells.get(i));
countCDR3Betas(assay,wells.get(i));
for(int pIndex: pIndices){
for(int i = start; i < end; i++){
countPeptides(assay, wells.get(i), pIndex);
}
}
return assay;
}
//returns a map of the counts of the CDR1s (alphas and betas) in a range of wells
public Map<Integer, Integer>assayWellsCDR1(int start, int end){
Map<Integer,Integer> assay = new HashMap<>();
for(int i = start; i < end; i++){
countCDR1Alphas(assay, wells.get(i));
countCDR1Betas(assay,wells.get(i));
}
return assay;
}
//returns a map of the counts of the CDR3 alphas in a range of wells
public Map<Integer, Integer> assayWellsCDR3Alpha(int start, int end) {
Map<Integer, Integer> assay = new HashMap<>();
for(int i = start; i < end; i++){
countCDR3Alphas(assay, wells.get(i));
}
return assay;
}
//returns a map of the counts of the CDR3 betas in a range of wells
public Map<Integer, Integer> assayWellsCDR3Beta(int start, int end) {
Map<Integer, Integer> assay = new HashMap<>();
for(int i = start; i < end; i++){
countCDR3Betas(assay, wells.get(i));
}
return assay;
}
//returns a map of the counts of the CDR1 alphas in a range of wells
public Map<Integer, Integer> assayWellsCDR1Alpha(int start, int end) {
Map<Integer, Integer> assay = new HashMap<>();
for(int i = start; i < end; i++){
countCDR1Alphas(assay, wells.get(i));
}
return assay;
}
//returns a map of the counts of the CDR1 betas in a range of wells
public Map<Integer, Integer> assayWellsCDR1Beta(int start, int end) {
Map<Integer, Integer> assay = new HashMap<>();
for(int i = start; i < end; i++){
countCDR1Betas(assay, wells.get(i));
}
return assay;
}
//given a map, counts distinct CDR3 alphas in a well
private void countCDR3Alphas(Map<Integer, Integer> wellMap, List<Integer[]> well){
//For the peptides at cell indices pIndices, counts number of unique peptides in the given well into the given map
private void countPeptides(Map<Integer, Integer> wellMap, List<Integer[]> well, int... pIndices) {
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);
for(int pIndex: pIndices){
if(cell[pIndex] != -1){
wellMap.merge(cell[pIndex], 1, (oldValue, newValue) -> oldValue + newValue);
}
}
}
}
//given a map, counts distinct CDR3 betas in a well
private void countCDR3Betas(Map<Integer, Integer> wellMap, List<Integer[]> well){
for(Integer[] cell : well) {
if(cell[1] != -1){
wellMap.merge(cell[1], 1, (oldValue, newValue) -> oldValue + newValue);
}
}
}
//given a map, counts distinct CDR1 alphas in a well
private void countCDR1Alphas(Map<Integer, Integer> wellMap, List<Integer[]> well){
for(Integer[] cell: well){
if(cell[2] != -1){
wellMap.merge(cell[2], 1, (oldValue, newValue) -> oldValue + newValue);
}
}
}
//given a map, counts distinct CDR1 betas in a well
private void countCDR1Betas(Map<Integer, Integer> wellMap, List<Integer[]> well){
for(Integer[] cell: well){
if(cell[3] != -1){
wellMap.merge(cell[3], 1, (oldValue, newValue) -> oldValue + newValue);
}
}
}
}