+ * An iterator that serves the aligned codon positions (with their protein
+ * products).
+ *
+ * @author gmcarstairs
+ *
+ */
+ public class AlignedCodonIterator implements Iterator<AlignedCodon>
+ {
+ /*
+ * The gap character used in the aligned sequence
+ */
+ private final char gap;
+
+ /*
+ * The characters of the aligned sequence e.g. "-cGT-ACgTG-"
+ */
+ private final char[] alignedSeq;
+
+ /*
+ * the sequence start residue
+ */
+ private int start;
+
+ /*
+ * Next position (base 0) in the aligned sequence
+ */
+ private int alignedColumn = 0;
+
+ /*
+ * Count of bases up to and including alignedColumn position
+ */
+ private int alignedBases = 0;
+
+ /*
+ * [start, end] from ranges (base 1)
+ */
+ private Iterator<int[]> fromRanges;
+
+ /*
+ * [start, end] to ranges (base 1)
+ */
+ private Iterator<int[]> toRanges;
+
+ /*
+ * The current [start, end] (base 1) from range
+ */
+ private int[] currentFromRange = null;
+
+ /*
+ * The current [start, end] (base 1) to range
+ */
+ private int[] currentToRange = null;
+
+ /*
+ * The next 'from' position (base 1) to process
+ */
+ private int fromPosition = 0;
+
+ /*
+ * The next 'to' position (base 1) to process
+ */
+ private int toPosition = 0;
+
+ /**
+ * Constructor
+ *
+ * @param seq
+ * the aligned sequence
+ * @param gapChar
+ */
+ public AlignedCodonIterator(SequenceI seq, char gapChar)
+ {
+ this.alignedSeq = seq.getSequence();
+ this.start = seq.getStart();
+ this.gap = gapChar;
+ fromRanges = map.getFromRanges().iterator();
+ toRanges = map.getToRanges().iterator();
+ if (fromRanges.hasNext())
+ {
+ currentFromRange = fromRanges.next();
+ fromPosition = currentFromRange[0];
+ }
+ if (toRanges.hasNext())
+ {
+ currentToRange = toRanges.next();
+ toPosition = currentToRange[0];
+ }
+ }
+
+ /**
+ * Returns true unless we have already traversed the whole mapping.
+ */
+ @Override
+ public boolean hasNext()
+ {
+ if (fromRanges.hasNext())
+ {
+ return true;
+ }
+ if (currentFromRange == null || fromPosition >= currentFromRange[1])
+ {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Returns the next codon's aligned positions, and translated value.
+ *
+ * @throws NoSuchElementException
+ * if hasNext() would have returned false
+ * @throws IncompleteCodonException
+ * if not enough mapped bases are left to make up a codon
+ */
+ @Override
+ public AlignedCodon next() throws IncompleteCodonException
+ {
+ if (!hasNext())
+ {
+ throw new NoSuchElementException();
+ }
+
+ int[] codon = getNextCodon();
+ int[] alignedCodon = getAlignedCodon(codon);
+
+ String peptide = getPeptide();
+ int peptideCol = toPosition - 1 - Mapping.this.to.getStart();
+ return new AlignedCodon(alignedCodon[0], alignedCodon[1],
+ alignedCodon[2], peptide, peptideCol);
+ }
+
+ /**
+ * Retrieve the translation as the 'mapped to' position in the mapped to
+ * sequence.
+ *
+ * @return
+ * @throws NoSuchElementException
+ * if the 'toRange' is exhausted (nothing to map to)
+ */
+ private String getPeptide()
+ {
+ // TODO should ideally handle toRatio other than 1 as well...
+ // i.e. code like getNextCodon()
+ if (toPosition <= currentToRange[1])
+ {
+ SequenceI seq = Mapping.this.to;
+ char pep = seq.getSequence()[toPosition - seq.getStart()];
+ toPosition++;
+ return String.valueOf(pep);
+ }
+ if (!toRanges.hasNext())
+ {
+ throw new NoSuchElementException("Ran out of peptide at position "
+ + toPosition);
+ }
+ currentToRange = toRanges.next();
+ toPosition = currentToRange[0];
+ return getPeptide();
+ }
+
+ /**
+ * Get the (base 1) dataset positions for the next codon in the mapping.
+ *
+ * @throws IncompleteCodonException
+ * if less than 3 remaining bases are mapped
+ */
+ private int[] getNextCodon()
+ {
+ int[] codon = new int[3];
+ int codonbase = 0;
+
+ while (codonbase < 3)
+ {
+ if (fromPosition <= currentFromRange[1])
+ {
+ /*
+ * Add next position from the current start-end range
+ */
+ codon[codonbase++] = fromPosition++;
+ }
+ else
+ {
+ /*
+ * Move to the next range - if there is one
+ */
+ if (!fromRanges.hasNext())
+ {
+ throw new IncompleteCodonException();
+ }
+ currentFromRange = fromRanges.next();
+ fromPosition = currentFromRange[0];
+ }
+ }
+ return codon;
+ }
+
+ /**
+ * Get the aligned column positions (base 0) for the given sequence
+ * positions (base 1), by counting ungapped characters in the aligned
+ * sequence.
+ *
+ * @param codon
+ * @return
+ */
+ private int[] getAlignedCodon(int[] codon)
+ {
+ int[] aligned = new int[codon.length];
+ for (int i = 0; i < codon.length; i++)
+ {
+ aligned[i] = getAlignedColumn(codon[i]);
+ }
+ return aligned;
+ }
+
+ /**
+ * Get the aligned column position (base 0) for the given sequence position
+ * (base 1).
+ *
+ * @param sequencePos
+ * @return
+ */
+ private int getAlignedColumn(int sequencePos)
+ {
+ /*
+ * allow for offset e.g. treat pos 8 as 2 if sequence starts at 7
+ */
+ int truePos = sequencePos - (start - 1);
+ while (alignedBases < truePos && alignedColumn < alignedSeq.length)
+ {
+ char c = alignedSeq[alignedColumn++];
+ if (c != gap && !Comparison.isGap(c))
+ {
+ alignedBases++;
+ }
+ }
+ return alignedColumn - 1;
+ }
+
+ @Override
+ public void remove()
+ {
+ // ignore
+ }
+
+ }
+
+ /*