2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3 * Copyright (C) 2015 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 * Next position (base 0) in the aligned sequence
53 private int alignedColumn = 0;
56 * Count of bases up to and including alignedColumn position
58 private int alignedBases = 0;
61 * [start, end] from ranges (base 1)
63 private Iterator<int[]> fromRanges;
66 * [start, end] to ranges (base 1)
68 private Iterator<int[]> toRanges;
71 * The current [start, end] (base 1) from range
73 private int[] currentFromRange = null;
76 * The current [start, end] (base 1) to range
78 private int[] currentToRange = null;
81 * The next 'from' position (base 1) to process
83 private int fromPosition = 0;
86 * The next 'to' position (base 1) to process
88 private int toPosition = 0;
94 * the aligned sequence characters
97 public AlignedCodonIterator(char[] cs, char gapChar)
101 fromRanges = map.getFromRanges().iterator();
102 toRanges = map.getToRanges().iterator();
103 if (fromRanges.hasNext())
105 currentFromRange = fromRanges.next();
106 fromPosition = currentFromRange[0];
108 if (toRanges.hasNext())
110 currentToRange = toRanges.next();
111 toPosition = currentToRange[0];
116 * Returns true unless we have already traversed the whole mapping.
119 public boolean hasNext()
121 if (fromRanges.hasNext())
125 if (currentFromRange == null || fromPosition >= currentFromRange[1])
133 * Returns the next codon's aligned positions, and translated value.
135 * @throws NoSuchElementException
136 * if hasNext() would have returned false
137 * @throws IncompleteCodonException
138 * if not enough mapped bases are left to make up a codon
141 public AlignedCodon next() throws IncompleteCodonException
145 throw new NoSuchElementException();
148 int[] codon = getNextCodon();
149 int[] alignedCodon = getAlignedCodon(codon);
151 String peptide = getPeptide();
152 return new AlignedCodon(alignedCodon[0], alignedCodon[1],
153 alignedCodon[2], peptide);
157 * Retrieve the translation as the 'mapped to' position in the mapped to
162 private String getPeptide()
164 // TODO should ideally handle toRatio other than 1 as well...
165 // i.e. code like getNextCodon()
166 if (toPosition <= currentToRange[1])
168 char pep = Mapping.this.to.getSequence()[toPosition - 1];
170 return String.valueOf(pep);
172 if (!toRanges.hasNext())
174 throw new NoSuchElementException("Ran out of peptide at position "
177 currentToRange = toRanges.next();
178 toPosition = currentToRange[0];
183 * Get the (base 1) dataset positions for the next codon in the mapping.
185 * @throws IncompleteCodonException
186 * if less than 3 remaining bases are mapped
188 private int[] getNextCodon()
190 int[] codon = new int[3];
193 while (codonbase < 3)
195 if (fromPosition <= currentFromRange[1])
198 * Add next position from the current start-end range
200 codon[codonbase++] = fromPosition++;
205 * Move to the next range - if there is one
207 if (!fromRanges.hasNext())
209 throw new IncompleteCodonException();
211 currentFromRange = fromRanges.next();
212 fromPosition = currentFromRange[0];
219 * Get the aligned column positions (base 0) for the given sequence
220 * positions (base 1), by counting ungapped characters in the aligned
226 private int[] getAlignedCodon(int[] codon)
228 int[] aligned = new int[codon.length];
229 for (int i = 0; i < codon.length; i++)
231 aligned[i] = getAlignedColumn(codon[i]);
237 * Get the aligned column position (base 0) for the given sequence position
243 private int getAlignedColumn(int sequencePos)
245 while (alignedBases < sequencePos
246 && alignedColumn < alignedSeq.length)
248 if (alignedSeq[alignedColumn++] != gap)
253 return alignedColumn - 1;
265 * Contains the start-end pairs mapping from the associated sequence to the
266 * sequence in the database coordinate system. It also takes care of step
267 * difference between coordinate systems.
272 * The sequence that map maps the associated sequence to (if any).
276 public Mapping(MapList map)
282 public Mapping(SequenceI to, MapList map)
289 * create a new mapping from
292 * the sequence being mapped
294 * int[] {start,end,start,end} series on associated sequence
296 * int[] {start,end,...} ranges on the reference frame being mapped
299 * step size on associated sequence
301 * step size on mapped frame
303 public Mapping(SequenceI to, int[] exon, int[] is, int i, int j)
305 this(to, new MapList(exon, is, i, j));
309 * create a duplicate (and independent) mapping object with the same reference
310 * to any SequenceI being mapped to.
314 public Mapping(Mapping map2)
316 if (map2 != this && map2 != null)
318 if (map2.map != null)
320 map = new MapList(map2.map);
329 public MapList getMap()
338 public void setMap(MapList map)
344 * Equals that compares both the to references and MapList mappings.
350 public boolean equals(Object o)
352 // TODO should override Object.hashCode() to ensure that equal objects have
354 if (o == null || !(o instanceof Mapping))
358 Mapping other = (Mapping) o;
367 if ((map != null && other.map == null)
368 || (map == null && other.map != null))
372 if ((map == null && other.map == null) || map.equals(other.map))
380 * get the 'initial' position in the associated sequence for a position in the
381 * mapped reference frame
386 public int getPosition(int mpos)
390 int[] mp = map.shiftTo(mpos);
400 * gets boundary in direction of mapping
403 * in mapped reference frame
404 * @return int{start, end} positions in associated sequence (in direction of
407 public int[] getWord(int mpos)
411 return map.getToWord(mpos);
417 * width of mapped unit in associated sequence
420 public int getWidth()
424 return map.getFromRatio();
430 * width of unit in mapped reference frame
434 public int getMappedWidth()
438 return map.getToRatio();
444 * get mapped position in the associated reference frame for position pos in
445 * the associated sequence.
450 public int getMappedPosition(int pos)
454 int[] mp = map.shiftFrom(pos);
463 public int[] getMappedWord(int pos)
467 int[] mp = map.shiftFrom(pos);
470 return new int[] { mp[0], mp[0] + mp[2] * (map.getToRatio() - 1) };
477 * locates the region of feature f in the associated sequence's reference
481 * @return one or more features corresponding to f
483 public SequenceFeature[] locateFeature(SequenceFeature f)
486 { // f.getBegin()!=f.getEnd()) {
489 int[] frange = map.locateInFrom(f.getBegin(), f.getEnd());
492 // JBPNote - this isprobably not the right thing to doJBPHack
495 SequenceFeature[] vf = new SequenceFeature[frange.length / 2];
496 for (int i = 0, v = 0; i < frange.length; i += 2, v++)
498 vf[v] = new SequenceFeature(f);
499 vf[v].setBegin(frange[i]);
500 vf[v].setEnd(frange[i + 1]);
501 if (frange.length > 2)
503 vf[v].setDescription(f.getDescription() + "\nPart " + (v + 1));
511 int[] word = getWord(f.getBegin());
512 if (word[0] < word[1])
520 word = getWord(f.getEnd());
521 if (word[0] > word[1])
530 // give up and just return the feature.
531 return new SequenceFeature[] { f };
535 * return a series of contigs on the associated sequence corresponding to the
536 * from,to interval on the mapped reference frame
540 * @return int[] { from_i, to_i for i=1 to n contiguous regions in the
541 * associated sequence}
543 public int[] locateRange(int from, int to)
549 from = (map.getToLowest() < from) ? from : map.getToLowest();
550 to = (map.getToHighest() > to) ? to : map.getToHighest();
558 from = (map.getToHighest() > from) ? from : map.getToHighest();
559 to = (map.getToLowest() < to) ? to : map.getToLowest();
565 return map.locateInFrom(from, to);
567 return new int[] { from, to };
571 * return a series of mapped contigs mapped from a range on the associated
578 public int[] locateMappedRange(int from, int to)
585 from = (map.getFromLowest() < from) ? from : map.getFromLowest();
586 to = (map.getFromHighest() > to) ? to : map.getFromHighest();
594 from = (map.getFromHighest() > from) ? from : map.getFromHighest();
595 to = (map.getFromLowest() < to) ? to : map.getFromLowest();
601 return map.locateInTo(from, to);
603 return new int[] { from, to };
607 * return a new mapping object with a maplist modifed to only map the visible
608 * regions defined by viscontigs.
613 public Mapping intersectVisContigs(int[] viscontigs)
615 Mapping copy = new Mapping(this);
620 Vector toRange = new Vector();
621 Vector fromRange = new Vector();
622 for (int vc = 0; vc < viscontigs.length; vc += 2)
624 // find a mapped range in this visible region
625 int[] mpr = locateMappedRange(1 + viscontigs[vc],
626 viscontigs[vc + 1] - 1);
629 for (int m = 0; m < mpr.length; m += 2)
631 toRange.addElement(new int[] { mpr[m], mpr[m + 1] });
632 int[] xpos = locateRange(mpr[m], mpr[m + 1]);
633 for (int x = 0; x < xpos.length; x += 2)
635 fromRange.addElement(new int[] { xpos[x], xpos[x + 1] });
640 int[] from = new int[fromRange.size() * 2];
641 int[] to = new int[toRange.size() * 2];
643 for (int f = 0, fSize = fromRange.size(); f < fSize; f++)
645 r = (int[]) fromRange.elementAt(f);
647 from[f * 2 + 1] = r[1];
649 for (int f = 0, fSize = toRange.size(); f < fSize; f++)
651 r = (int[]) toRange.elementAt(f);
653 to[f * 2 + 1] = r[1];
655 copy.setMap(new MapList(from, to, map.getFromRatio(), map
662 * get the sequence being mapped to - if any
664 * @return null or a dataset sequence
666 public SequenceI getTo()
672 * set the dataset sequence being mapped to if any
676 public void setTo(SequenceI tto)
684 * @see java.lang.Object#finalize()
686 protected void finalize() throws Throwable
693 public Iterator<AlignedCodon> getCodonIterator(SequenceI seq, char gapChar)
695 return new AlignedCodonIterator(seq.getSequence(), gapChar);