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 java.util.Iterator;
24 import java.util.NoSuchElementException;
25 import java.util.Vector;
27 import jalview.util.Comparison;
28 import jalview.util.MapList;
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 * width of mapped unit in associated sequence
439 public int getWidth()
443 return map.getFromRatio();
449 * width of unit in mapped reference frame
453 public int getMappedWidth()
457 return map.getToRatio();
463 * get mapped position in the associated reference frame for position pos in
464 * the associated sequence.
469 public int getMappedPosition(int pos)
473 int[] mp = map.shiftFrom(pos);
482 public int[] getMappedWord(int pos)
486 int[] mp = map.shiftFrom(pos);
489 return new int[] { mp[0], mp[0] + mp[2] * (map.getToRatio() - 1) };
496 * locates the region of feature f in the associated sequence's reference
500 * @return one or more features corresponding to f
502 public SequenceFeature[] locateFeature(SequenceFeature f)
505 { // f.getBegin()!=f.getEnd()) {
508 int[] frange = map.locateInFrom(f.getBegin(), f.getEnd());
511 // JBPNote - this isprobably not the right thing to doJBPHack
514 SequenceFeature[] vf = new SequenceFeature[frange.length / 2];
515 for (int i = 0, v = 0; i < frange.length; i += 2, v++)
517 vf[v] = new SequenceFeature(f, frange[i], frange[i + 1],
518 f.getFeatureGroup(), f.getScore());
519 if (frange.length > 2)
521 vf[v].setDescription(f.getDescription() + "\nPart " + (v + 1));
528 // give up and just return the feature.
529 return new SequenceFeature[] { f };
533 * return a series of contigs on the associated sequence corresponding to the
534 * from,to interval on the mapped reference frame
538 * @return int[] { from_i, to_i for i=1 to n contiguous regions in the
539 * associated sequence}
541 public int[] locateRange(int from, int to)
547 from = (map.getToLowest() < from) ? from : map.getToLowest();
548 to = (map.getToHighest() > to) ? to : map.getToHighest();
556 from = (map.getToHighest() > from) ? from : map.getToHighest();
557 to = (map.getToLowest() < to) ? to : map.getToLowest();
563 return map.locateInFrom(from, to);
565 return new int[] { from, to };
569 * return a series of mapped contigs mapped from a range on the associated
576 public int[] locateMappedRange(int from, int to)
583 from = (map.getFromLowest() < from) ? from : map.getFromLowest();
584 to = (map.getFromHighest() > to) ? to : map.getFromHighest();
592 from = (map.getFromHighest() > from) ? from : map.getFromHighest();
593 to = (map.getFromLowest() < to) ? to : map.getFromLowest();
599 return map.locateInTo(from, to);
601 return new int[] { from, to };
605 * return a new mapping object with a maplist modifed to only map the visible
606 * regions defined by viscontigs.
611 public Mapping intersectVisContigs(int[] viscontigs)
613 Mapping copy = new Mapping(this);
618 Vector toRange = new Vector();
619 Vector fromRange = new Vector();
620 for (int vc = 0; vc < viscontigs.length; vc += 2)
622 // find a mapped range in this visible region
623 int[] mpr = locateMappedRange(1 + viscontigs[vc],
624 viscontigs[vc + 1] - 1);
627 for (int m = 0; m < mpr.length; m += 2)
629 toRange.addElement(new int[] { mpr[m], mpr[m + 1] });
630 int[] xpos = locateRange(mpr[m], mpr[m + 1]);
631 for (int x = 0; x < xpos.length; x += 2)
633 fromRange.addElement(new int[] { xpos[x], xpos[x + 1] });
638 int[] from = new int[fromRange.size() * 2];
639 int[] to = new int[toRange.size() * 2];
641 for (int f = 0, fSize = fromRange.size(); f < fSize; f++)
643 r = (int[]) fromRange.elementAt(f);
645 from[f * 2 + 1] = r[1];
647 for (int f = 0, fSize = toRange.size(); f < fSize; f++)
649 r = (int[]) toRange.elementAt(f);
651 to[f * 2 + 1] = r[1];
654 new MapList(from, to, map.getFromRatio(), map.getToRatio()));
660 * get the sequence being mapped to - if any
662 * @return null or a dataset sequence
664 public SequenceI getTo()
670 * set the dataset sequence being mapped to if any
674 public void setTo(SequenceI tto)
680 * Returns an iterator which can serve up the aligned codon column positions
681 * and their corresponding peptide products
684 * an aligned (i.e. possibly gapped) sequence
688 public Iterator<AlignedCodon> getCodonIterator(SequenceI seq,
691 return new AlignedCodonIterator(seq, gapChar);
695 * Readable representation for debugging only, not guaranteed not to change
698 public String toString()
700 return String.format("%s %s", this.map.toString(),
701 this.to == null ? "" : this.to.getName());
705 * Returns the identifier for the 'from' range sequence, or null if not set
709 public String getMappedFromId()
715 * Sets the identifier for the 'from' range sequence
717 public void setMappedFromId(String mappedFromId)
719 this.mappedFromId = mappedFromId;