diff --git a/src/main/java/Plate.java b/src/main/java/Plate.java index 3ba7a1b..b81a140 100644 --- a/src/main/java/Plate.java +++ b/src/main/java/Plate.java @@ -162,15 +162,18 @@ public class Plate { public Map countSequences(Integer readDepth, Double readErrorRate, Double errorCollisionRate, Double realSequenceCollisionRate, int... sIndices) { SequenceType[] sequenceTypes = EnumSet.allOf(SequenceType.class).toArray(new SequenceType[0]); - //Map of all sequences read. Keys are sequences, values are ways sequence has been misread. + //Map of all real sequences read. Keys are sequences, values are ways sequence has been misread. Map> sequencesAndMisreads = new HashMap<>(); + //Map of all sequences read. Keys are sequences, values are associated SequenceRecords Map sequenceMap = new LinkedHashMap<>(); for (int well = 0; well < size; well++) { - for (String[] cell : wells.get(well)) { - for (int sIndex : sIndices) { + for (String[] cell: wells.get(well)) { + for (int sIndex: sIndices) { + //the sequence being read String currentSequence = cell[sIndex]; //skip dropout sequences, which have value -1 if (!"-1".equals(currentSequence)) { + //keep rereading the sequence until the read depth is reached for (int j = 0; j < readDepth; j++) { //The sequence is misread if (rand.nextDouble() < readErrorRate) { @@ -180,7 +183,7 @@ public class Plate { } //The specific misread hasn't happened before if (rand.nextDouble() >= errorCollisionRate || sequencesAndMisreads.get(currentSequence).size() == 0) { - //The misread doesn't collide with a real sequence already on the plate + //The misread doesn't collide with a real sequence already on the plate and some sequences have already been read if(rand.nextDouble() >= realSequenceCollisionRate || !sequenceMap.isEmpty()){ StringBuilder spurious = new StringBuilder(currentSequence); for (int k = 0; k <= sequencesAndMisreads.get(currentSequence).size(); k++) { @@ -202,9 +205,11 @@ public class Plate { wrongSequence = sequencesAndMisreads.keySet().toArray(new String[0])[index]; //make sure it's not accidentally the *right* sequence //Also that it's not a wrong sequence already in the misread list - } while(cell[sIndex].equals(wrongSequence) || sequencesAndMisreads.get(currentSequence).contains(wrongSequence)); + } while(currentSequence.equals(wrongSequence) || sequencesAndMisreads.get(currentSequence).contains(wrongSequence)); + //update the SequenceRecord for wrongSequence sequenceMap.get(wrongSequence).addRead(well); - + //add wrongSequence to the misreads for currentSequence + sequencesAndMisreads.get(currentSequence).add(wrongSequence); } } }