2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import jalview.util.Comparison;
24 import jalview.util.MapList;
26 import java.util.Iterator;
27 import java.util.NoSuchElementException;
28 import java.util.Vector;
33 * An iterator that serves the aligned codon positions (with their protein
39 public class AlignedCodonIterator implements Iterator<AlignedCodon>
42 * The gap character used in the aligned sequence
44 private final char gap;
47 * The characters of the aligned sequence e.g. "-cGT-ACgTG-"
49 private final SequenceI alignedSeq;
52 * the sequence start residue
57 * Next position (base 0) in the aligned sequence
59 private int alignedColumn = 0;
62 * Count of bases up to and including alignedColumn position
64 private int alignedBases = 0;
67 * [start, end] from ranges (base 1)
69 private Iterator<int[]> fromRanges;
72 * [start, end] to ranges (base 1)
74 private Iterator<int[]> toRanges;
77 * The current [start, end] (base 1) from range
79 private int[] currentFromRange = null;
82 * The current [start, end] (base 1) to range
84 private int[] currentToRange = null;
87 * The next 'from' position (base 1) to process
89 private int fromPosition = 0;
92 * The next 'to' position (base 1) to process
94 private int toPosition = 0;
100 * the aligned sequence
103 public AlignedCodonIterator(SequenceI seq, char gapChar)
105 this.alignedSeq = seq;
106 this.start = seq.getStart();
108 fromRanges = map.getFromRanges().iterator();
109 toRanges = map.getToRanges().iterator();
110 if (fromRanges.hasNext())
112 currentFromRange = fromRanges.next();
113 fromPosition = currentFromRange[0];
115 if (toRanges.hasNext())
117 currentToRange = toRanges.next();
118 toPosition = currentToRange[0];
123 * Returns true unless we have already traversed the whole mapping.
126 public boolean hasNext()
128 if (fromRanges.hasNext())
132 if (currentFromRange == null || fromPosition >= currentFromRange[1])
140 * Returns the next codon's aligned positions, and translated value.
142 * @throws NoSuchElementException
143 * if hasNext() would have returned false
144 * @throws IncompleteCodonException
145 * if not enough mapped bases are left to make up a codon
148 public AlignedCodon next() throws IncompleteCodonException
152 throw new NoSuchElementException();
155 int[] codon = getNextCodon();
156 int[] alignedCodon = getAlignedCodon(codon);
158 String peptide = getPeptide();
159 int peptideCol = toPosition - 1 - Mapping.this.to.getStart();
160 return new AlignedCodon(alignedCodon[0], alignedCodon[1],
161 alignedCodon[2], peptide, peptideCol);
165 * Retrieve the translation as the 'mapped to' position in the mapped to
169 * @throws NoSuchElementException
170 * if the 'toRange' is exhausted (nothing to map to)
172 private String getPeptide()
174 // TODO should ideally handle toRatio other than 1 as well...
175 // i.e. code like getNextCodon()
176 if (toPosition <= currentToRange[1])
178 SequenceI seq = Mapping.this.to;
179 char pep = seq.getCharAt(toPosition - seq.getStart());
181 return String.valueOf(pep);
183 if (!toRanges.hasNext())
185 throw new NoSuchElementException(
186 "Ran out of peptide at position " + toPosition);
188 currentToRange = toRanges.next();
189 toPosition = currentToRange[0];
194 * Get the (base 1) dataset positions for the next codon in the mapping.
196 * @throws IncompleteCodonException
197 * if less than 3 remaining bases are mapped
199 private int[] getNextCodon()
201 int[] codon = new int[3];
204 while (codonbase < 3)
206 if (fromPosition <= currentFromRange[1])
209 * Add next position from the current start-end range
211 codon[codonbase++] = fromPosition++;
216 * Move to the next range - if there is one
218 if (!fromRanges.hasNext())
220 throw new IncompleteCodonException();
222 currentFromRange = fromRanges.next();
223 fromPosition = currentFromRange[0];
230 * Get the aligned column positions (base 0) for the given sequence
231 * positions (base 1), by counting ungapped characters in the aligned
237 private int[] getAlignedCodon(int[] codon)
239 int[] aligned = new int[codon.length];
240 for (int i = 0; i < codon.length; i++)
242 aligned[i] = getAlignedColumn(codon[i]);
248 * Get the aligned column position (base 0) for the given sequence position
254 private int getAlignedColumn(int sequencePos)
257 * allow for offset e.g. treat pos 8 as 2 if sequence starts at 7
259 int truePos = sequencePos - (start - 1);
260 int length = alignedSeq.getLength();
261 while (alignedBases < truePos && alignedColumn < length)
263 char c = alignedSeq.getCharAt(alignedColumn++);
264 if (c != gap && !Comparison.isGap(c))
269 return alignedColumn - 1;
281 * Contains the start-end pairs mapping from the associated sequence to the
282 * sequence in the database coordinate system. It also takes care of step
283 * difference between coordinate systems.
288 * The sequence that map maps the associated sequence to (if any).
293 * optional sequence id for the 'from' ranges
295 private String mappedFromId;
297 public Mapping(MapList map)
303 public Mapping(SequenceI to, MapList map)
310 * create a new mapping from
313 * the sequence being mapped
315 * int[] {start,end,start,end} series on associated sequence
317 * int[] {start,end,...} ranges on the reference frame being mapped
320 * step size on associated sequence
322 * step size on mapped frame
324 public Mapping(SequenceI to, int[] exon, int[] is, int i, int j)
326 this(to, new MapList(exon, is, i, j));
330 * create a duplicate (and independent) mapping object with the same reference
331 * to any SequenceI being mapped to.
335 public Mapping(Mapping map2)
337 if (map2 != this && map2 != null)
339 if (map2.map != null)
341 map = new MapList(map2.map);
344 mappedFromId = map2.mappedFromId;
351 public MapList getMap()
360 public void setMap(MapList map)
366 * Equals that compares both the to references and MapList mappings.
370 * @see MapList#equals
373 public boolean equals(Object o)
375 if (o == null || !(o instanceof Mapping))
379 Mapping other = (Mapping) o;
388 if ((map != null && other.map == null)
389 || (map == null && other.map != null))
393 if ((map == null && other.map == null) || map.equals(other.map))
401 * Returns a hashCode made from the sequence and maplist
404 public int hashCode()
406 int hashCode = (this.to == null ? 1 : this.to.hashCode());
407 if (this.map != null)
409 hashCode = hashCode * 31 + this.map.hashCode();
416 * get the 'initial' position in the associated sequence for a position in the
417 * mapped reference frame
422 public int getPosition(int mpos)
426 int[] mp = map.shiftTo(mpos);
436 * gets boundary in direction of mapping
439 * in mapped reference frame
440 * @return int{start, end} positions in associated sequence (in direction of
443 public int[] getWord(int mpos)
447 return map.getToWord(mpos);
453 * width of mapped unit in associated sequence
456 public int getWidth()
460 return map.getFromRatio();
466 * width of unit in mapped reference frame
470 public int getMappedWidth()
474 return map.getToRatio();
480 * get mapped position in the associated reference frame for position pos in
481 * the associated sequence.
486 public int getMappedPosition(int pos)
490 int[] mp = map.shiftFrom(pos);
499 public int[] getMappedWord(int pos)
503 int[] mp = map.shiftFrom(pos);
506 return new int[] { mp[0], mp[0] + mp[2] * (map.getToRatio() - 1) };
513 * locates the region of feature f in the associated sequence's reference
517 * @return one or more features corresponding to f
519 public SequenceFeature[] locateFeature(SequenceFeature f)
522 { // f.getBegin()!=f.getEnd()) {
525 int[] frange = map.locateInFrom(f.getBegin(), f.getEnd());
528 // JBPNote - this isprobably not the right thing to doJBPHack
531 SequenceFeature[] vf = new SequenceFeature[frange.length / 2];
532 for (int i = 0, v = 0; i < frange.length; i += 2, v++)
534 vf[v] = new SequenceFeature(f, frange[i], frange[i + 1],
535 f.getFeatureGroup(), f.getScore());
536 if (frange.length > 2)
538 vf[v].setDescription(f.getDescription() + "\nPart " + (v + 1));
545 // give up and just return the feature.
546 return new SequenceFeature[] { f };
550 * return a series of contigs on the associated sequence corresponding to the
551 * from,to interval on the mapped reference frame
555 * @return int[] { from_i, to_i for i=1 to n contiguous regions in the
556 * associated sequence}
558 public int[] locateRange(int from, int to)
564 from = (map.getToLowest() < from) ? from : map.getToLowest();
565 to = (map.getToHighest() > to) ? to : map.getToHighest();
573 from = (map.getToHighest() > from) ? from : map.getToHighest();
574 to = (map.getToLowest() < to) ? to : map.getToLowest();
580 return map.locateInFrom(from, to);
582 return new int[] { from, to };
586 * return a series of mapped contigs mapped from a range on the associated
593 public int[] locateMappedRange(int from, int to)
600 from = (map.getFromLowest() < from) ? from : map.getFromLowest();
601 to = (map.getFromHighest() > to) ? to : map.getFromHighest();
609 from = (map.getFromHighest() > from) ? from : map.getFromHighest();
610 to = (map.getFromLowest() < to) ? to : map.getFromLowest();
616 return map.locateInTo(from, to);
618 return new int[] { from, to };
622 * return a new mapping object with a maplist modifed to only map the visible
623 * regions defined by viscontigs.
628 public Mapping intersectVisContigs(int[] viscontigs)
630 Mapping copy = new Mapping(this);
635 Vector toRange = new Vector();
636 Vector fromRange = new Vector();
637 for (int vc = 0; vc < viscontigs.length; vc += 2)
639 // find a mapped range in this visible region
640 int[] mpr = locateMappedRange(1 + viscontigs[vc],
641 viscontigs[vc + 1] - 1);
644 for (int m = 0; m < mpr.length; m += 2)
646 toRange.addElement(new int[] { mpr[m], mpr[m + 1] });
647 int[] xpos = locateRange(mpr[m], mpr[m + 1]);
648 for (int x = 0; x < xpos.length; x += 2)
650 fromRange.addElement(new int[] { xpos[x], xpos[x + 1] });
655 int[] from = new int[fromRange.size() * 2];
656 int[] to = new int[toRange.size() * 2];
658 for (int f = 0, fSize = fromRange.size(); f < fSize; f++)
660 r = (int[]) fromRange.elementAt(f);
662 from[f * 2 + 1] = r[1];
664 for (int f = 0, fSize = toRange.size(); f < fSize; f++)
666 r = (int[]) toRange.elementAt(f);
668 to[f * 2 + 1] = r[1];
671 new MapList(from, to, map.getFromRatio(), map.getToRatio()));
677 * get the sequence being mapped to - if any
679 * @return null or a dataset sequence
681 public SequenceI getTo()
687 * set the dataset sequence being mapped to if any
691 public void setTo(SequenceI tto)
697 * Returns an iterator which can serve up the aligned codon column positions
698 * and their corresponding peptide products
701 * an aligned (i.e. possibly gapped) sequence
705 public Iterator<AlignedCodon> getCodonIterator(SequenceI seq,
708 return new AlignedCodonIterator(seq, gapChar);
712 * Readable representation for debugging only, not guaranteed not to change
715 public String toString()
717 return String.format("%s %s", this.map.toString(),
718 this.to == null ? "" : this.to.getName());
722 * Returns the identifier for the 'from' range sequence, or null if not set
726 public String getMappedFromId()
732 * Sets the identifier for the 'from' range sequence
734 public void setMappedFromId(String mappedFromId)
736 this.mappedFromId = mappedFromId;