Clover icon

Coverage Report

  1. Project Clover database Fri Nov 1 2024 11:46:37 GMT
  2. Package jalview.datamodel

File Mapping.java

 

Coverage histogram

../../img/srcFileCovDistChart8.png
20% of files have more coverage

Code metrics

116
172
31
2
722
412
99
0.58
5.55
15.5
3.19

Classes

Class Line # Actions
Mapping 30 118 75
0.731601773.2%
Mapping.AlignedCodonIterator 39 54 24
0.9204545692%
 

Contributing tests

This file is covered by 162 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.datamodel;
22   
23    import java.util.Iterator;
24    import java.util.NoSuchElementException;
25    import java.util.Vector;
26   
27    import jalview.util.Comparison;
28    import jalview.util.MapList;
29   
 
30    public class Mapping
31    {
32    /**
33    * An iterator that serves the aligned codon positions (with their protein
34    * products).
35    *
36    * @author gmcarstairs
37    *
38    */
 
39    public class AlignedCodonIterator implements Iterator<AlignedCodon>
40    {
41    /*
42    * The gap character used in the aligned sequence
43    */
44    private final char gap;
45   
46    /*
47    * The characters of the aligned sequence e.g. "-cGT-ACgTG-"
48    */
49    private final SequenceI alignedSeq;
50   
51    /*
52    * the sequence start residue
53    */
54    private int start;
55   
56    /*
57    * Next position (base 0) in the aligned sequence
58    */
59    private int alignedColumn = 0;
60   
61    /*
62    * Count of bases up to and including alignedColumn position
63    */
64    private int alignedBases = 0;
65   
66    /*
67    * [start, end] from ranges (base 1)
68    */
69    private Iterator<int[]> fromRanges;
70   
71    /*
72    * [start, end] to ranges (base 1)
73    */
74    private Iterator<int[]> toRanges;
75   
76    /*
77    * The current [start, end] (base 1) from range
78    */
79    private int[] currentFromRange = null;
80   
81    /*
82    * The current [start, end] (base 1) to range
83    */
84    private int[] currentToRange = null;
85   
86    /*
87    * The next 'from' position (base 1) to process
88    */
89    private int fromPosition = 0;
90   
91    /*
92    * The next 'to' position (base 1) to process
93    */
94    private int toPosition = 0;
95   
96    /**
97    * Constructor
98    *
99    * @param seq
100    * the aligned sequence
101    * @param gapChar
102    */
 
103  37 toggle public AlignedCodonIterator(SequenceI seq, char gapChar)
104    {
105  37 this.alignedSeq = seq;
106  37 this.start = seq.getStart();
107  37 this.gap = gapChar;
108  37 fromRanges = map.getFromRanges().iterator();
109  37 toRanges = map.getToRanges().iterator();
110  37 if (fromRanges.hasNext())
111    {
112  37 currentFromRange = fromRanges.next();
113  37 fromPosition = currentFromRange[0];
114    }
115  37 if (toRanges.hasNext())
116    {
117  37 currentToRange = toRanges.next();
118  37 toPosition = currentToRange[0];
119    }
120    }
121   
122    /**
123    * Returns true unless we have already traversed the whole mapping.
124    */
 
125  21419 toggle @Override
126    public boolean hasNext()
127    {
128  21419 if (fromRanges.hasNext())
129    {
130  13 return true;
131    }
132  21406 if (currentFromRange == null || fromPosition >= currentFromRange[1])
133    {
134  36 return false;
135    }
136  21370 return true;
137    }
138   
139    /**
140    * Returns the next codon's aligned positions, and translated value.
141    *
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
146    */
 
147  10699 toggle @Override
148    public AlignedCodon next() throws IncompleteCodonException
149    {
150  10699 if (!hasNext())
151    {
152  0 throw new NoSuchElementException();
153    }
154   
155  10699 int[] codon = getNextCodon();
156  10698 int[] alignedCodon = getAlignedCodon(codon);
157   
158  10698 String peptide = getPeptide();
159  10698 int peptideCol = toPosition - 1 - Mapping.this.to.getStart();
160  10698 return new AlignedCodon(alignedCodon[0], alignedCodon[1],
161    alignedCodon[2], peptide, peptideCol);
162    }
163   
164    /**
165    * Retrieve the translation as the 'mapped to' position in the mapped to
166    * sequence.
167    *
168    * @return
169    * @throws NoSuchElementException
170    * if the 'toRange' is exhausted (nothing to map to)
171    */
 
172  10699 toggle private String getPeptide()
173    {
174    // TODO should ideally handle toRatio other than 1 as well...
175    // i.e. code like getNextCodon()
176  10699 if (toPosition <= currentToRange[1])
177    {
178  10698 SequenceI seq = Mapping.this.to;
179  10698 char pep = seq.getCharAt(toPosition - seq.getStart());
180  10698 toPosition++;
181  10698 return String.valueOf(pep);
182    }
183  1 if (!toRanges.hasNext())
184    {
185  0 throw new NoSuchElementException(
186    "Ran out of peptide at position " + toPosition);
187    }
188  1 currentToRange = toRanges.next();
189  1 toPosition = currentToRange[0];
190  1 return getPeptide();
191    }
192   
193    /**
194    * Get the (base 1) dataset positions for the next codon in the mapping.
195    *
196    * @throws IncompleteCodonException
197    * if less than 3 remaining bases are mapped
198    */
 
199  10699 toggle private int[] getNextCodon()
200    {
201  10699 int[] codon = new int[3];
202  10699 int codonbase = 0;
203   
204  42811 while (codonbase < 3)
205    {
206  32113 if (fromPosition <= currentFromRange[1])
207    {
208    /*
209    * Add next position from the current start-end range
210    */
211  32096 codon[codonbase++] = fromPosition++;
212    }
213    else
214    {
215    /*
216    * Move to the next range - if there is one
217    */
218  17 if (!fromRanges.hasNext())
219    {
220  1 throw new IncompleteCodonException();
221    }
222  16 currentFromRange = fromRanges.next();
223  16 fromPosition = currentFromRange[0];
224    }
225    }
226  10698 return codon;
227    }
228   
229    /**
230    * Get the aligned column positions (base 0) for the given sequence
231    * positions (base 1), by counting ungapped characters in the aligned
232    * sequence.
233    *
234    * @param codon
235    * @return
236    */
 
237  10698 toggle private int[] getAlignedCodon(int[] codon)
238    {
239  10698 int[] aligned = new int[codon.length];
240  42792 for (int i = 0; i < codon.length; i++)
241    {
242  32094 aligned[i] = getAlignedColumn(codon[i]);
243    }
244  10698 return aligned;
245    }
246   
247    /**
248    * Get the aligned column position (base 0) for the given sequence position
249    * (base 1).
250    *
251    * @param sequencePos
252    * @return
253    */
 
254  32094 toggle private int getAlignedColumn(int sequencePos)
255    {
256    /*
257    * allow for offset e.g. treat pos 8 as 2 if sequence starts at 7
258    */
259  32094 int truePos = sequencePos - (start - 1);
260  32094 int length = alignedSeq.getLength();
261  75377 while (alignedBases < truePos && alignedColumn < length)
262    {
263  43283 char c = alignedSeq.getCharAt(alignedColumn++);
264  43283 if (c != gap && !Comparison.isGap(c))
265    {
266  32122 alignedBases++;
267    }
268    }
269  32094 return alignedColumn - 1;
270    }
271   
 
272  0 toggle @Override
273    public void remove()
274    {
275    // ignore
276    }
277   
278    }
279   
280    /*
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.
284    */
285    MapList map = null;
286   
287    /*
288    * The sequence that map maps the associated sequence to (if any).
289    */
290    SequenceI to = null;
291   
292    /*
293    * optional sequence id for the 'from' ranges
294    */
295    private String mappedFromId;
296   
 
297  945 toggle public Mapping(MapList map)
298    {
299  945 super();
300  945 this.map = map;
301    }
302   
 
303  664 toggle public Mapping(SequenceI to, MapList map)
304    {
305  664 this(map);
306  664 this.to = to;
307    }
308   
309    /**
310    * create a new mapping from
311    *
312    * @param to
313    * the sequence being mapped
314    * @param exon
315    * int[] {start,end,start,end} series on associated sequence
316    * @param is
317    * int[] {start,end,...} ranges on the reference frame being mapped
318    * to
319    * @param i
320    * step size on associated sequence
321    * @param j
322    * step size on mapped frame
323    */
 
324  65 toggle public Mapping(SequenceI to, int[] exon, int[] is, int i, int j)
325    {
326  65 this(to, new MapList(exon, is, i, j));
327    }
328   
329    /**
330    * create a duplicate (and independent) mapping object with the same reference
331    * to any SequenceI being mapped to.
332    *
333    * @param map2
334    */
 
335  8 toggle public Mapping(Mapping map2)
336    {
337  8 if (map2 != this && map2 != null)
338    {
339  8 if (map2.map != null)
340    {
341  8 map = new MapList(map2.map);
342    }
343  8 to = map2.to;
344  8 mappedFromId = map2.mappedFromId;
345    }
346    }
347   
348    /**
349    * @return the map
350    */
 
351  22848 toggle public MapList getMap()
352    {
353  22848 return map;
354    }
355   
356    /**
357    * @param map
358    * the map to set
359    */
 
360  2 toggle public void setMap(MapList map)
361    {
362  2 this.map = map;
363    }
364   
365    /**
366    * Equals that compares both the to references and MapList mappings.
367    *
368    * @param o
369    * @return
370    * @see MapList#equals
371    */
 
372  43 toggle @Override
373    public boolean equals(Object o)
374    {
375  43 if (o == null || !(o instanceof Mapping))
376    {
377  0 return false;
378    }
379  43 Mapping other = (Mapping) o;
380  43 if (other == this)
381    {
382  6 return true;
383    }
384  37 if (other.to != to)
385    {
386  25 return false;
387    }
388  12 if ((map != null && other.map == null)
389    || (map == null && other.map != null))
390    {
391  0 return false;
392    }
393  12 if ((map == null && other.map == null) || map.equals(other.map))
394    {
395  4 return true;
396    }
397  8 return false;
398    }
399   
400    /**
401    * Returns a hashCode made from the sequence and maplist
402    */
 
403  2 toggle @Override
404    public int hashCode()
405    {
406  2 int hashCode = (this.to == null ? 1 : this.to.hashCode());
407  2 if (this.map != null)
408    {
409  2 hashCode = hashCode * 31 + this.map.hashCode();
410    }
411   
412  2 return hashCode;
413    }
414   
415    /**
416    * get the 'initial' position in the associated sequence for a position in the
417    * mapped reference frame
418    *
419    * @param mpos
420    * @return
421    */
 
422  16335 toggle public int getPosition(int mpos)
423    {
424  16335 if (map != null)
425    {
426  16335 int[] mp = map.shiftTo(mpos);
427  16335 if (mp != null)
428    {
429  16320 return mp[0];
430    }
431    }
432  15 return mpos;
433    }
434   
435    /**
436    * width of mapped unit in associated sequence
437    *
438    */
 
439  223 toggle public int getWidth()
440    {
441  223 if (map != null)
442    {
443  223 return map.getFromRatio();
444    }
445  0 return 1;
446    }
447   
448    /**
449    * width of unit in mapped reference frame
450    *
451    * @return
452    */
 
453  223 toggle public int getMappedWidth()
454    {
455  223 if (map != null)
456    {
457  223 return map.getToRatio();
458    }
459  0 return 1;
460    }
461   
462    /**
463    * get mapped position in the associated reference frame for position pos in
464    * the associated sequence.
465    *
466    * @param pos
467    * @return
468    */
 
469  9 toggle public int getMappedPosition(int pos)
470    {
471  9 if (map != null)
472    {
473  9 int[] mp = map.shiftFrom(pos);
474  9 if (mp != null)
475    {
476  4 return mp[0];
477    }
478    }
479  5 return pos;
480    }
481   
 
482  0 toggle public int[] getMappedWord(int pos)
483    {
484  0 if (map != null)
485    {
486  0 int[] mp = map.shiftFrom(pos);
487  0 if (mp != null)
488    {
489  0 return new int[] { mp[0], mp[0] + mp[2] * (map.getToRatio() - 1) };
490    }
491    }
492  0 return null;
493    }
494   
495    /**
496    * locates the region of feature f in the associated sequence's reference
497    * frame
498    *
499    * @param f
500    * @return one or more features corresponding to f
501    */
 
502  3352 toggle public SequenceFeature[] locateFeature(SequenceFeature f)
503    {
504  3352 if (true)
505    { // f.getBegin()!=f.getEnd()) {
506  3352 if (map != null)
507    {
508  3352 int[] frange = map.locateInFrom(f.getBegin(), f.getEnd());
509  3352 if (frange == null)
510    {
511    // JBPNote - this isprobably not the right thing to doJBPHack
512  0 return null;
513    }
514  3352 SequenceFeature[] vf = new SequenceFeature[frange.length / 2];
515  6704 for (int i = 0, v = 0; i < frange.length; i += 2, v++)
516    {
517  3352 vf[v] = new SequenceFeature(f, frange[i], frange[i + 1],
518    f.getFeatureGroup(), f.getScore());
519  3352 if (frange.length > 2)
520    {
521  0 vf[v].setDescription(f.getDescription() + "\nPart " + (v + 1));
522    }
523    }
524  3352 return vf;
525    }
526    }
527   
528    // give up and just return the feature.
529  0 return new SequenceFeature[] { f };
530    }
531   
532    /**
533    * return a series of contigs on the associated sequence corresponding to the
534    * from,to interval on the mapped reference frame
535    *
536    * @param from
537    * @param to
538    * @return int[] { from_i, to_i for i=1 to n contiguous regions in the
539    * associated sequence}
540    */
 
541  3 toggle public int[] locateRange(int from, int to)
542    {
543  3 if (map != null)
544    {
545  3 if (from <= to)
546    {
547  3 from = (map.getToLowest() < from) ? from : map.getToLowest();
548  3 to = (map.getToHighest() > to) ? to : map.getToHighest();
549  3 if (from > to)
550    {
551  0 return null;
552    }
553    }
554    else
555    {
556  0 from = (map.getToHighest() > from) ? from : map.getToHighest();
557  0 to = (map.getToLowest() < to) ? to : map.getToLowest();
558  0 if (from < to)
559    {
560  0 return null;
561    }
562    }
563  3 return map.locateInFrom(from, to);
564    }
565  0 return new int[] { from, to };
566    }
567   
568    /**
569    * return a series of mapped contigs mapped from a range on the associated
570    * sequence
571    *
572    * @param from
573    * @param to
574    * @return
575    */
 
576  34 toggle public int[] locateMappedRange(int from, int to)
577    {
578  34 if (map != null)
579    {
580   
581  34 if (from <= to)
582    {
583  34 from = (map.getFromLowest() < from) ? from : map.getFromLowest();
584  34 to = (map.getFromHighest() > to) ? to : map.getFromHighest();
585  34 if (from > to)
586    {
587  0 return null;
588    }
589    }
590    else
591    {
592  0 from = (map.getFromHighest() > from) ? from : map.getFromHighest();
593  0 to = (map.getFromLowest() < to) ? to : map.getFromLowest();
594  0 if (from < to)
595    {
596  0 return null;
597    }
598    }
599  34 return map.locateInTo(from, to);
600    }
601  0 return new int[] { from, to };
602    }
603   
604    /**
605    * return a new mapping object with a maplist modifed to only map the visible
606    * regions defined by viscontigs.
607    *
608    * @param viscontigs
609    * @return
610    */
 
611  2 toggle public Mapping intersectVisContigs(int[] viscontigs)
612    {
613  2 Mapping copy = new Mapping(this);
614  2 if (map != null)
615    {
616  2 int vpos = 0;
617  2 int apos = 0;
618  2 Vector toRange = new Vector();
619  2 Vector fromRange = new Vector();
620  5 for (int vc = 0; vc < viscontigs.length; vc += 2)
621    {
622    // find a mapped range in this visible region
623  3 int[] mpr = locateMappedRange(1 + viscontigs[vc],
624    viscontigs[vc + 1] - 1);
625  3 if (mpr != null)
626    {
627  6 for (int m = 0; m < mpr.length; m += 2)
628    {
629  3 toRange.addElement(new int[] { mpr[m], mpr[m + 1] });
630  3 int[] xpos = locateRange(mpr[m], mpr[m + 1]);
631  9 for (int x = 0; x < xpos.length; x += 2)
632    {
633  6 fromRange.addElement(new int[] { xpos[x], xpos[x + 1] });
634    }
635    }
636    }
637    }
638  2 int[] from = new int[fromRange.size() * 2];
639  2 int[] to = new int[toRange.size() * 2];
640  2 int[] r;
641  8 for (int f = 0, fSize = fromRange.size(); f < fSize; f++)
642    {
643  6 r = (int[]) fromRange.elementAt(f);
644  6 from[f * 2] = r[0];
645  6 from[f * 2 + 1] = r[1];
646    }
647  5 for (int f = 0, fSize = toRange.size(); f < fSize; f++)
648    {
649  3 r = (int[]) toRange.elementAt(f);
650  3 to[f * 2] = r[0];
651  3 to[f * 2 + 1] = r[1];
652    }
653  2 copy.setMap(
654    new MapList(from, to, map.getFromRatio(), map.getToRatio()));
655    }
656  2 return copy;
657    }
658   
659    /**
660    * get the sequence being mapped to - if any
661    *
662    * @return null or a dataset sequence
663    */
 
664  1175 toggle public SequenceI getTo()
665    {
666  1175 return to;
667    }
668   
669    /**
670    * set the dataset sequence being mapped to if any
671    *
672    * @param tto
673    */
 
674  256 toggle public void setTo(SequenceI tto)
675    {
676  256 to = tto;
677    }
678   
679    /**
680    * Returns an iterator which can serve up the aligned codon column positions
681    * and their corresponding peptide products
682    *
683    * @param seq
684    * an aligned (i.e. possibly gapped) sequence
685    * @param gapChar
686    * @return
687    */
 
688  37 toggle public Iterator<AlignedCodon> getCodonIterator(SequenceI seq,
689    char gapChar)
690    {
691  37 return new AlignedCodonIterator(seq, gapChar);
692    }
693   
694    /**
695    * Readable representation for debugging only, not guaranteed not to change
696    */
 
697  2 toggle @Override
698    public String toString()
699    {
700  2 return String.format("%s %s", this.map.toString(),
701  2 this.to == null ? "" : this.to.getName());
702    }
703   
704    /**
705    * Returns the identifier for the 'from' range sequence, or null if not set
706    *
707    * @return
708    */
 
709  34 toggle public String getMappedFromId()
710    {
711  34 return mappedFromId;
712    }
713   
714    /**
715    * Sets the identifier for the 'from' range sequence
716    */
 
717  450 toggle public void setMappedFromId(String mappedFromId)
718    {
719  450 this.mappedFromId = mappedFromId;
720    }
721   
722    }