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.MapList;
25 import java.util.Iterator;
26 import java.util.NoSuchElementException;
27 import java.util.Vector;
32 * An iterator that serves the aligned codon positions (with their protein
38 public class AlignedCodonIterator implements Iterator<AlignedCodon>
41 * The gap character used in the aligned sequence
43 private final char gap;
46 * The characters of the aligned sequence e.g. "-cGT-ACgTG-"
48 private final char[] alignedSeq;
51 * the sequence start residue
56 * Next position (base 0) in the aligned sequence
58 private int alignedColumn = 0;
61 * Count of bases up to and including alignedColumn position
63 private int alignedBases = 0;
66 * [start, end] from ranges (base 1)
68 private Iterator<int[]> fromRanges;
71 * [start, end] to ranges (base 1)
73 private Iterator<int[]> toRanges;
76 * The current [start, end] (base 1) from range
78 private int[] currentFromRange = null;
81 * The current [start, end] (base 1) to range
83 private int[] currentToRange = null;
86 * The next 'from' position (base 1) to process
88 private int fromPosition = 0;
91 * The next 'to' position (base 1) to process
93 private int toPosition = 0;
99 * the aligned sequence
102 public AlignedCodonIterator(SequenceI seq, char gapChar)
104 this.alignedSeq = seq.getSequence();
105 this.start = seq.getStart();
107 fromRanges = map.getFromRanges().iterator();
108 toRanges = map.getToRanges().iterator();
109 if (fromRanges.hasNext())
111 currentFromRange = fromRanges.next();
112 fromPosition = currentFromRange[0];
114 if (toRanges.hasNext())
116 currentToRange = toRanges.next();
117 toPosition = currentToRange[0];
122 * Returns true unless we have already traversed the whole mapping.
125 public boolean hasNext()
127 if (fromRanges.hasNext())
131 if (currentFromRange == null || fromPosition >= currentFromRange[1])
139 * Returns the next codon's aligned positions, and translated value.
141 * @throws NoSuchElementException
142 * if hasNext() would have returned false
143 * @throws IncompleteCodonException
144 * if not enough mapped bases are left to make up a codon
147 public AlignedCodon next() throws IncompleteCodonException
151 throw new NoSuchElementException();
154 int[] codon = getNextCodon();
155 int[] alignedCodon = getAlignedCodon(codon);
157 String peptide = getPeptide();
158 int peptideCol = toPosition - 1 - Mapping.this.to.getStart();
159 return new AlignedCodon(alignedCodon[0], alignedCodon[1],
160 alignedCodon[2], peptide, peptideCol);
164 * Retrieve the translation as the 'mapped to' position in the mapped to
168 * @throws NoSuchElementException
169 * if the 'toRange' is exhausted (nothing to map to)
171 private String getPeptide()
173 // TODO should ideally handle toRatio other than 1 as well...
174 // i.e. code like getNextCodon()
175 if (toPosition <= currentToRange[1])
177 SequenceI seq = Mapping.this.to;
178 char pep = seq.getSequence()[toPosition - seq.getStart()];
180 return String.valueOf(pep);
182 if (!toRanges.hasNext())
184 throw new NoSuchElementException("Ran out of peptide at position "
187 currentToRange = toRanges.next();
188 toPosition = currentToRange[0];
193 * Get the (base 1) dataset positions for the next codon in the mapping.
195 * @throws IncompleteCodonException
196 * if less than 3 remaining bases are mapped
198 private int[] getNextCodon()
200 int[] codon = new int[3];
203 while (codonbase < 3)
205 if (fromPosition <= currentFromRange[1])
208 * Add next position from the current start-end range
210 codon[codonbase++] = fromPosition++;
215 * Move to the next range - if there is one
217 if (!fromRanges.hasNext())
219 throw new IncompleteCodonException();
221 currentFromRange = fromRanges.next();
222 fromPosition = currentFromRange[0];
229 * Get the aligned column positions (base 0) for the given sequence
230 * positions (base 1), by counting ungapped characters in the aligned
236 private int[] getAlignedCodon(int[] codon)
238 int[] aligned = new int[codon.length];
239 for (int i = 0; i < codon.length; i++)
241 aligned[i] = getAlignedColumn(codon[i]);
247 * Get the aligned column position (base 0) for the given sequence position
253 private int getAlignedColumn(int sequencePos)
256 * allow for offset e.g. treat pos 8 as 2 if sequence starts at 7
258 int truePos = sequencePos - (start - 1);
259 while (alignedBases < truePos && alignedColumn < alignedSeq.length)
261 if (alignedSeq[alignedColumn++] != gap)
266 return alignedColumn - 1;
278 * Contains the start-end pairs mapping from the associated sequence to the
279 * sequence in the database coordinate system. It also takes care of step
280 * difference between coordinate systems.
285 * The sequence that map maps the associated sequence to (if any).
289 public Mapping(MapList map)
295 public Mapping(SequenceI to, MapList map)
302 * create a new mapping from
305 * the sequence being mapped
307 * int[] {start,end,start,end} series on associated sequence
309 * int[] {start,end,...} ranges on the reference frame being mapped
312 * step size on associated sequence
314 * step size on mapped frame
316 public Mapping(SequenceI to, int[] exon, int[] is, int i, int j)
318 this(to, new MapList(exon, is, i, j));
322 * create a duplicate (and independent) mapping object with the same reference
323 * to any SequenceI being mapped to.
327 public Mapping(Mapping map2)
329 if (map2 != this && map2 != null)
331 if (map2.map != null)
333 map = new MapList(map2.map);
342 public MapList getMap()
351 public void setMap(MapList map)
357 * Equals that compares both the to references and MapList mappings.
363 public boolean equals(Object o)
365 // TODO should override Object.hashCode() to ensure that equal objects have
367 if (o == null || !(o instanceof Mapping))
371 Mapping other = (Mapping) o;
380 if ((map != null && other.map == null)
381 || (map == null && other.map != null))
385 if ((map == null && other.map == null) || map.equals(other.map))
393 * get the 'initial' position in the associated sequence for a position in the
394 * mapped reference frame
399 public int getPosition(int mpos)
403 int[] mp = map.shiftTo(mpos);
413 * gets boundary in direction of mapping
416 * in mapped reference frame
417 * @return int{start, end} positions in associated sequence (in direction of
420 public int[] getWord(int mpos)
424 return map.getToWord(mpos);
430 * width of mapped unit in associated sequence
433 public int getWidth()
437 return map.getFromRatio();
443 * width of unit in mapped reference frame
447 public int getMappedWidth()
451 return map.getToRatio();
457 * get mapped position in the associated reference frame for position pos in
458 * the associated sequence.
463 public int getMappedPosition(int pos)
467 int[] mp = map.shiftFrom(pos);
476 public int[] getMappedWord(int pos)
480 int[] mp = map.shiftFrom(pos);
483 return new int[] { mp[0], mp[0] + mp[2] * (map.getToRatio() - 1) };
490 * locates the region of feature f in the associated sequence's reference
494 * @return one or more features corresponding to f
496 public SequenceFeature[] locateFeature(SequenceFeature f)
499 { // f.getBegin()!=f.getEnd()) {
502 int[] frange = map.locateInFrom(f.getBegin(), f.getEnd());
505 // JBPNote - this isprobably not the right thing to doJBPHack
508 SequenceFeature[] vf = new SequenceFeature[frange.length / 2];
509 for (int i = 0, v = 0; i < frange.length; i += 2, v++)
511 vf[v] = new SequenceFeature(f);
512 vf[v].setBegin(frange[i]);
513 vf[v].setEnd(frange[i + 1]);
514 if (frange.length > 2)
516 vf[v].setDescription(f.getDescription() + "\nPart " + (v + 1));
524 int[] word = getWord(f.getBegin());
525 if (word[0] < word[1])
533 word = getWord(f.getEnd());
534 if (word[0] > word[1])
543 // give up and just return the feature.
544 return new SequenceFeature[] { f };
548 * return a series of contigs on the associated sequence corresponding to the
549 * from,to interval on the mapped reference frame
553 * @return int[] { from_i, to_i for i=1 to n contiguous regions in the
554 * associated sequence}
556 public int[] locateRange(int from, int to)
562 from = (map.getToLowest() < from) ? from : map.getToLowest();
563 to = (map.getToHighest() > to) ? to : map.getToHighest();
571 from = (map.getToHighest() > from) ? from : map.getToHighest();
572 to = (map.getToLowest() < to) ? to : map.getToLowest();
578 return map.locateInFrom(from, to);
580 return new int[] { from, to };
584 * return a series of mapped contigs mapped from a range on the associated
591 public int[] locateMappedRange(int from, int to)
598 from = (map.getFromLowest() < from) ? from : map.getFromLowest();
599 to = (map.getFromHighest() > to) ? to : map.getFromHighest();
607 from = (map.getFromHighest() > from) ? from : map.getFromHighest();
608 to = (map.getFromLowest() < to) ? to : map.getFromLowest();
614 return map.locateInTo(from, to);
616 return new int[] { from, to };
620 * return a new mapping object with a maplist modifed to only map the visible
621 * regions defined by viscontigs.
626 public Mapping intersectVisContigs(int[] viscontigs)
628 Mapping copy = new Mapping(this);
633 Vector toRange = new Vector();
634 Vector fromRange = new Vector();
635 for (int vc = 0; vc < viscontigs.length; vc += 2)
637 // find a mapped range in this visible region
638 int[] mpr = locateMappedRange(1 + viscontigs[vc],
639 viscontigs[vc + 1] - 1);
642 for (int m = 0; m < mpr.length; m += 2)
644 toRange.addElement(new int[] { mpr[m], mpr[m + 1] });
645 int[] xpos = locateRange(mpr[m], mpr[m + 1]);
646 for (int x = 0; x < xpos.length; x += 2)
648 fromRange.addElement(new int[] { xpos[x], xpos[x + 1] });
653 int[] from = new int[fromRange.size() * 2];
654 int[] to = new int[toRange.size() * 2];
656 for (int f = 0, fSize = fromRange.size(); f < fSize; f++)
658 r = (int[]) fromRange.elementAt(f);
660 from[f * 2 + 1] = r[1];
662 for (int f = 0, fSize = toRange.size(); f < fSize; f++)
664 r = (int[]) toRange.elementAt(f);
666 to[f * 2 + 1] = r[1];
668 copy.setMap(new MapList(from, to, map.getFromRatio(), map
675 * get the sequence being mapped to - if any
677 * @return null or a dataset sequence
679 public SequenceI getTo()
685 * set the dataset sequence being mapped to if any
689 public void setTo(SequenceI tto)
697 * @see java.lang.Object#finalize()
700 protected void finalize() throws Throwable
708 * Returns an iterator which can serve up the aligned codon column positions
709 * and their corresponding peptide products
712 * an aligned (i.e. possibly gapped) sequence
716 public Iterator<AlignedCodon> getCodonIterator(SequenceI seq, char gapChar)
718 return new AlignedCodonIterator(seq, gapChar);
722 * Readable representation for debugging only, not guaranteed not to change
725 public String toString()
727 return String.format("%s %s", this.map.toString(), this.to == null ? ""
728 : this.to.getName());