0f3fc2e4cfea5d718281c4988fee2c9871253f43
[jalview.git] / src / jalview / util / MapList.java
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.util;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.BitSet;
26 import java.util.List;
27
28 import jalview.bin.Cache;
29
30 /**
31  * A simple way of bijectively mapping a non-contiguous linear range to another
32  * non-contiguous linear range.
33  * 
34  * Use at your own risk!
35  * 
36  * TODO: test/ensure that sense of from and to ratio start position is conserved
37  * (codon start position recovery)
38  */
39 public class MapList
40 {
41
42   /*
43    * Subregions (base 1) described as { [start1, end1], [start2, end2], ...}
44    */
45   private List<int[]> fromShifts;
46
47   /*
48    * Same format as fromShifts, for the 'mapped to' sequence
49    */
50   private List<int[]> toShifts;
51
52   /*
53    * number of steps in fromShifts to one toRatio unit
54    */
55   private int fromRatio;
56
57   /*
58    * number of steps in toShifts to one fromRatio
59    */
60   private int toRatio;
61
62   /*
63    * lowest and highest value in the from Map
64    */
65   private int fromLowest;
66
67   private int fromHighest;
68
69   /*
70    * lowest and highest value in the to Map
71    */
72   private int toLowest;
73
74   private int toHighest;
75
76   /**
77    * Constructor
78    */
79   public MapList()
80   {
81     fromShifts = new ArrayList<>();
82     toShifts = new ArrayList<>();
83   }
84
85   /**
86    * Two MapList objects are equal if they are the same object, or they both
87    * have populated shift ranges and all values are the same.
88    */
89   @Override
90   public boolean equals(Object o)
91   {
92     if (o == null || !(o instanceof MapList))
93     {
94       return false;
95     }
96
97     MapList obj = (MapList) o;
98     if (obj == this)
99     {
100       return true;
101     }
102     if (obj.fromRatio != fromRatio || obj.toRatio != toRatio
103             || obj.fromShifts == null || obj.toShifts == null)
104     {
105       return false;
106     }
107     return Arrays.deepEquals(fromShifts.toArray(), obj.fromShifts.toArray())
108             && Arrays.deepEquals(toShifts.toArray(),
109                     obj.toShifts.toArray());
110   }
111
112   /**
113    * Returns a hashcode made from the fromRatio, toRatio, and from/to ranges
114    */
115   @Override
116   public int hashCode()
117   {
118     int hashCode = 31 * fromRatio;
119     hashCode = 31 * hashCode + toRatio;
120     for (int[] shift : fromShifts)
121     {
122       hashCode = 31 * hashCode + shift[0];
123       hashCode = 31 * hashCode + shift[1];
124     }
125     for (int[] shift : toShifts)
126     {
127       hashCode = 31 * hashCode + shift[0];
128       hashCode = 31 * hashCode + shift[1];
129     }
130
131     return hashCode;
132   }
133
134   /**
135    * Returns the 'from' ranges as {[start1, end1], [start2, end2], ...}
136    * 
137    * @return
138    */
139   public List<int[]> getFromRanges()
140   {
141     return fromShifts;
142   }
143
144   /**
145    * Returns the 'to' ranges as {[start1, end1], [start2, end2], ...}
146    * 
147    * @return
148    */
149   public List<int[]> getToRanges()
150   {
151     return toShifts;
152   }
153
154   /**
155    * Flattens a list of [start, end] into a single [start1, end1, start2,
156    * end2,...] array.
157    * 
158    * @param shifts
159    * @return
160    */
161   protected static int[] getRanges(List<int[]> shifts)
162   {
163     int[] rnges = new int[2 * shifts.size()];
164     int i = 0;
165     for (int[] r : shifts)
166     {
167       rnges[i++] = r[0];
168       rnges[i++] = r[1];
169     }
170     return rnges;
171   }
172
173   /**
174    * 
175    * @return length of mapped phrase in from
176    */
177   public int getFromRatio()
178   {
179     return fromRatio;
180   }
181
182   /**
183    * 
184    * @return length of mapped phrase in to
185    */
186   public int getToRatio()
187   {
188     return toRatio;
189   }
190
191   public int getFromLowest()
192   {
193     return fromLowest;
194   }
195
196   public int getFromHighest()
197   {
198     return fromHighest;
199   }
200
201   public int getToLowest()
202   {
203     return toLowest;
204   }
205
206   public int getToHighest()
207   {
208     return toHighest;
209   }
210
211   /**
212    * Constructor given from and to ranges as [start1, end1, start2, end2,...].
213    * There is no validation check that the ranges do not overlap each other.
214    * 
215    * @param from
216    *          contiguous regions as [start1, end1, start2, end2, ...]
217    * @param to
218    *          same format as 'from'
219    * @param fromRatio
220    *          phrase length in 'from' (e.g. 3 for dna)
221    * @param toRatio
222    *          phrase length in 'to' (e.g. 1 for protein)
223    */
224   public MapList(int from[], int to[], int fromRatio, int toRatio)
225   {
226     this();
227     this.fromRatio = fromRatio;
228     this.toRatio = toRatio;
229     fromLowest = Integer.MAX_VALUE;
230     fromHighest = Integer.MIN_VALUE;
231
232     for (int i = 0; i < from.length; i += 2)
233     {
234       /*
235        * note lowest and highest values - bearing in mind the
236        * direction may be reversed
237        */
238       fromLowest = Math.min(fromLowest, Math.min(from[i], from[i + 1]));
239       fromHighest = Math.max(fromHighest, Math.max(from[i], from[i + 1]));
240       fromShifts.add(new int[] { from[i], from[i + 1] });
241     }
242
243     toLowest = Integer.MAX_VALUE;
244     toHighest = Integer.MIN_VALUE;
245     for (int i = 0; i < to.length; i += 2)
246     {
247       toLowest = Math.min(toLowest, Math.min(to[i], to[i + 1]));
248       toHighest = Math.max(toHighest, Math.max(to[i], to[i + 1]));
249       toShifts.add(new int[] { to[i], to[i + 1] });
250     }
251   }
252
253   /**
254    * Copy constructor. Creates an identical mapping.
255    * 
256    * @param map
257    */
258   public MapList(MapList map)
259   {
260     this();
261     // TODO not used - remove?
262     this.fromLowest = map.fromLowest;
263     this.fromHighest = map.fromHighest;
264     this.toLowest = map.toLowest;
265     this.toHighest = map.toHighest;
266
267     this.fromRatio = map.fromRatio;
268     this.toRatio = map.toRatio;
269     if (map.fromShifts != null)
270     {
271       for (int[] r : map.fromShifts)
272       {
273         fromShifts.add(new int[] { r[0], r[1] });
274       }
275     }
276     if (map.toShifts != null)
277     {
278       for (int[] r : map.toShifts)
279       {
280         toShifts.add(new int[] { r[0], r[1] });
281       }
282     }
283   }
284
285   /**
286    * Constructor given ranges as lists of [start, end] positions. There is no
287    * validation check that the ranges do not overlap each other.
288    * 
289    * @param fromRange
290    * @param toRange
291    * @param fromRatio
292    * @param toRatio
293    */
294   public MapList(List<int[]> fromRange, List<int[]> toRange, int fromRatio,
295           int toRatio)
296   {
297     this();
298     fromRange = coalesceRanges(fromRange);
299     toRange = coalesceRanges(toRange);
300     this.fromShifts = fromRange;
301     this.toShifts = toRange;
302     this.fromRatio = fromRatio;
303     this.toRatio = toRatio;
304
305     fromLowest = Integer.MAX_VALUE;
306     fromHighest = Integer.MIN_VALUE;
307     for (int[] range : fromRange)
308     {
309       if (range.length != 2)
310       {
311         // throw new IllegalArgumentException(range);
312         Cache.error("Invalid format for fromRange "
313                 + Arrays.toString(range) + " may cause errors");
314       }
315       fromLowest = Math.min(fromLowest, Math.min(range[0], range[1]));
316       fromHighest = Math.max(fromHighest, Math.max(range[0], range[1]));
317     }
318
319     toLowest = Integer.MAX_VALUE;
320     toHighest = Integer.MIN_VALUE;
321     for (int[] range : toRange)
322     {
323       if (range.length != 2)
324       {
325         // throw new IllegalArgumentException(range);
326         Cache.error("Invalid format for toRange "
327                 + Arrays.toString(range) + " may cause errors");
328       }
329       toLowest = Math.min(toLowest, Math.min(range[0], range[1]));
330       toHighest = Math.max(toHighest, Math.max(range[0], range[1]));
331     }
332   }
333
334   /**
335    * Consolidates a list of ranges so that any contiguous ranges are merged.
336    * This assumes the ranges are already in start order (does not sort them).
337    * <p>
338    * The main use case for this method is when mapping cDNA sequence to its
339    * protein product, based on CDS feature ranges which derive from spliced
340    * exons, but are contiguous on the cDNA sequence. For example
341    * 
342    * <pre>
343    *   CDS 1-20  // from exon1
344    *   CDS 21-35 // from exon2
345    *   CDS 36-71 // from exon3
346    * 'coalesce' to range 1-71
347    * </pre>
348    * 
349    * @param ranges
350    * @return the same list (if unchanged), else a new merged list, leaving the
351    *         input list unchanged
352    */
353   public static List<int[]> coalesceRanges(final List<int[]> ranges)
354   {
355     if (ranges == null || ranges.size() < 2)
356     {
357       return ranges;
358     }
359
360     boolean changed = false;
361     List<int[]> merged = new ArrayList<>();
362     int[] lastRange = ranges.get(0);
363     int lastDirection = lastRange[1] >= lastRange[0] ? 1 : -1;
364     lastRange = new int[] { lastRange[0], lastRange[1] };
365     merged.add(lastRange);
366     boolean first = true;
367
368     for (final int[] range : ranges)
369     {
370       if (first)
371       {
372         first = false;
373         continue;
374       }
375
376       int direction = range[1] >= range[0] ? 1 : -1;
377
378       /*
379        * if next range is in the same direction as last and contiguous,
380        * just update the end position of the last range
381        */
382       boolean sameDirection = range[1] == range[0]
383               || direction == lastDirection;
384       boolean extending = range[0] == lastRange[1] + lastDirection;
385       if (sameDirection && extending)
386       {
387         lastRange[1] = range[1];
388         changed = true;
389       }
390       else
391       {
392         lastRange = new int[] { range[0], range[1] };
393         merged.add(lastRange);
394         // careful: merging [5, 5] after [7, 6] should keep negative direction
395         lastDirection = (range[1] == range[0]) ? lastDirection : direction;
396       }
397     }
398
399     return changed ? merged : ranges;
400   }
401
402   /**
403    * get all mapped positions from 'from' to 'to'
404    * 
405    * @return int[][] { int[] { fromStart, fromFinish, toStart, toFinish }, int
406    *         [fromFinish-fromStart+2] { toStart..toFinish mappings}}
407    */
408   protected int[][] makeFromMap()
409   {
410     // TODO only used for test - remove??
411     return posMap(fromShifts, fromRatio, toShifts, toRatio);
412   }
413
414   /**
415    * get all mapped positions from 'to' to 'from'
416    * 
417    * @return int[to position]=position mapped in from
418    */
419   protected int[][] makeToMap()
420   {
421     // TODO only used for test - remove??
422     return posMap(toShifts, toRatio, fromShifts, fromRatio);
423   }
424
425   /**
426    * construct an int map for intervals in intVals
427    * 
428    * @param shiftTo
429    * @return int[] { from, to pos in range }, int[range.to-range.from+1]
430    *         returning mapped position
431    */
432   private int[][] posMap(List<int[]> shiftTo, int sourceRatio,
433           List<int[]> shiftFrom, int targetRatio)
434   {
435     // TODO only used for test - remove??
436     int iv = 0, ivSize = shiftTo.size();
437     if (iv >= ivSize)
438     {
439       return null;
440     }
441     int[] intv = shiftTo.get(iv++);
442     int from = intv[0], to = intv[1];
443     if (from > to)
444     {
445       from = intv[1];
446       to = intv[0];
447     }
448     while (iv < ivSize)
449     {
450       intv = shiftTo.get(iv++);
451       if (intv[0] < from)
452       {
453         from = intv[0];
454       }
455       if (intv[1] < from)
456       {
457         from = intv[1];
458       }
459       if (intv[0] > to)
460       {
461         to = intv[0];
462       }
463       if (intv[1] > to)
464       {
465         to = intv[1];
466       }
467     }
468     int tF = 0, tT = 0;
469     int mp[][] = new int[to - from + 2][];
470     for (int i = 0; i < mp.length; i++)
471     {
472       int[] m = shift(i + from, shiftTo, sourceRatio, shiftFrom,
473               targetRatio);
474       if (m != null)
475       {
476         if (i == 0)
477         {
478           tF = tT = m[0];
479         }
480         else
481         {
482           if (m[0] < tF)
483           {
484             tF = m[0];
485           }
486           if (m[0] > tT)
487           {
488             tT = m[0];
489           }
490         }
491       }
492       mp[i] = m;
493     }
494     int[][] map = new int[][] { new int[] { from, to, tF, tT },
495         new int[to - from + 2] };
496
497     map[0][2] = tF;
498     map[0][3] = tT;
499
500     for (int i = 0; i < mp.length; i++)
501     {
502       if (mp[i] != null)
503       {
504         map[1][i] = mp[i][0] - tF;
505       }
506       else
507       {
508         map[1][i] = -1; // indicates an out of range mapping
509       }
510     }
511     return map;
512   }
513
514   /**
515    * addShift
516    * 
517    * @param pos
518    *          start position for shift (in original reference frame)
519    * @param shift
520    *          length of shift
521    * 
522    *          public void addShift(int pos, int shift) { int sidx = 0; int[]
523    *          rshift=null; while (sidx<shifts.size() && (rshift=(int[])
524    *          shifts.elementAt(sidx))[0]<pos) sidx++; if (sidx==shifts.size())
525    *          shifts.insertElementAt(new int[] { pos, shift}, sidx); else
526    *          rshift[1]+=shift; }
527    */
528
529   /**
530    * shift from pos to To(pos)
531    * 
532    * @param pos
533    *          int
534    * @return int shifted position in To, frameshift in From, direction of mapped
535    *         symbol in To
536    */
537   public int[] shiftFrom(int pos)
538   {
539     return shift(pos, fromShifts, fromRatio, toShifts, toRatio);
540   }
541
542   /**
543    * inverse of shiftFrom - maps pos in To to a position in From
544    * 
545    * @param pos
546    *          (in To)
547    * @return shifted position in From, frameshift in To, direction of mapped
548    *         symbol in From
549    */
550   public int[] shiftTo(int pos)
551   {
552     return shift(pos, toShifts, toRatio, fromShifts, fromRatio);
553   }
554
555   /**
556    * 
557    * @param shiftTo
558    * @param fromRatio
559    * @param shiftFrom
560    * @param toRatio
561    * @return
562    */
563   protected static int[] shift(int pos, List<int[]> shiftTo, int fromRatio,
564           List<int[]> shiftFrom, int toRatio)
565   {
566     // TODO: javadoc; tests
567     int[] fromCount = countPositions(shiftTo, pos);
568     if (fromCount == null)
569     {
570       return null;
571     }
572     int fromRemainder = (fromCount[0] - 1) % fromRatio;
573     int toCount = 1 + (((fromCount[0] - 1) / fromRatio) * toRatio);
574     int[] toPos = traverseToPosition(shiftFrom, toCount);
575     if (toPos == null)
576     {
577       return null;
578     }
579     return new int[] { toPos[0], fromRemainder, toPos[1] };
580   }
581
582   /**
583    * Counts how many positions pos is along the series of intervals. Returns an
584    * array of two values:
585    * <ul>
586    * <li>the number of positions traversed (inclusive) to reach {@code pos}</li>
587    * <li>+1 if the last interval traversed is forward, -1 if in a negative
588    * direction</li>
589    * </ul>
590    * Returns null if {@code pos} does not lie in any of the given intervals.
591    * 
592    * @param intervals
593    *          a list of start-end intervals
594    * @param pos
595    *          a position that may lie in one (or more) of the intervals
596    * @return
597    */
598   protected static int[] countPositions(List<int[]> intervals, int pos)
599   {
600     int count = 0;
601     int iv = 0;
602     int ivSize = intervals.size();
603
604     while (iv < ivSize)
605     {
606       int[] intv = intervals.get(iv++);
607       if (intv[0] <= intv[1])
608       {
609         /*
610          * forwards interval
611          */
612         if (pos >= intv[0] && pos <= intv[1])
613         {
614           return new int[] { count + pos - intv[0] + 1, +1 };
615         }
616         else
617         {
618           count += intv[1] - intv[0] + 1;
619         }
620       }
621       else
622       {
623         /*
624          * reverse interval
625          */
626         if (pos >= intv[1] && pos <= intv[0])
627         {
628           return new int[] { count + intv[0] - pos + 1, -1 };
629         }
630         else
631         {
632           count += intv[0] - intv[1] + 1;
633         }
634       }
635     }
636     return null;
637   }
638
639   /**
640    * Reads through the given intervals until {@code count} positions have been
641    * traversed, and returns an array consisting of two values:
642    * <ul>
643    * <li>the value at the {@code count'th} position</li>
644    * <li>+1 if the last interval read is forwards, -1 if reverse direction</li>
645    * </ul>
646    * Returns null if the ranges include less than {@code count} positions, or if
647    * {@code count < 1}.
648    * 
649    * @param intervals
650    *          a list of [start, end] ranges
651    * @param count
652    *          the number of positions to traverse
653    * @return
654    */
655   protected static int[] traverseToPosition(List<int[]> intervals,
656           final int count)
657   {
658     int traversed = 0;
659     int ivSize = intervals.size();
660     int iv = 0;
661
662     if (count < 1)
663     {
664       return null;
665     }
666
667     while (iv < ivSize)
668     {
669       int[] intv = intervals.get(iv++);
670       int diff = intv[1] - intv[0];
671       if (diff >= 0)
672       {
673         if (count <= traversed + 1 + diff)
674         {
675           return new int[] { intv[0] + (count - traversed - 1), +1 };
676         }
677         else
678         {
679           traversed += 1 + diff;
680         }
681       }
682       else
683       {
684         if (count <= traversed + 1 - diff)
685         {
686           return new int[] { intv[0] - (count - traversed - 1), -1 };
687         }
688         else
689         {
690           traversed += 1 - diff;
691         }
692       }
693     }
694     return null;
695   }
696
697   /**
698    * like shift - except returns the intervals in the given vector of shifts
699    * which were spanned in traversing fromStart to fromEnd
700    * 
701    * @param shiftFrom
702    * @param fromStart
703    * @param fromEnd
704    * @param fromRatio2
705    * @return series of from,to intervals from from first position of starting
706    *         region to final position of ending region inclusive
707    */
708   protected static int[] getIntervals(List<int[]> shiftFrom,
709           int[] fromStart, int[] fromEnd, int fromRatio2)
710   {
711     if (fromStart == null || fromEnd == null)
712     {
713       return null;
714     }
715     int startpos, endpos;
716     startpos = fromStart[0]; // first position in fromStart
717     endpos = fromEnd[0]; // last position in fromEnd
718     int endindx = (fromRatio2 - 1); // additional positions to get to last
719     // position from endpos
720     int intv = 0, intvSize = shiftFrom.size();
721     int iv[], i = 0, fs = -1, fe_s = -1, fe = -1; // containing intervals
722     // search intervals to locate ones containing startpos and count endindx
723     // positions on from endpos
724     while (intv < intvSize && (fs == -1 || fe == -1))
725     {
726       iv = shiftFrom.get(intv++);
727       if (fe_s > -1)
728       {
729         endpos = iv[0]; // start counting from beginning of interval
730         endindx--; // inclusive of endpos
731       }
732       if (iv[0] <= iv[1])
733       {
734         if (fs == -1 && startpos >= iv[0] && startpos <= iv[1])
735         {
736           fs = i;
737         }
738         if (endpos >= iv[0] && endpos <= iv[1])
739         {
740           if (fe_s == -1)
741           {
742             fe_s = i;
743           }
744           if (fe_s != -1)
745           {
746             if (endpos + endindx <= iv[1])
747             {
748               fe = i;
749               endpos = endpos + endindx; // end of end token is within this
750               // interval
751             }
752             else
753             {
754               endindx -= iv[1] - endpos; // skip all this interval too
755             }
756           }
757         }
758       }
759       else
760       {
761         if (fs == -1 && startpos <= iv[0] && startpos >= iv[1])
762         {
763           fs = i;
764         }
765         if (endpos <= iv[0] && endpos >= iv[1])
766         {
767           if (fe_s == -1)
768           {
769             fe_s = i;
770           }
771           if (fe_s != -1)
772           {
773             if (endpos - endindx >= iv[1])
774             {
775               fe = i;
776               endpos = endpos - endindx; // end of end token is within this
777               // interval
778             }
779             else
780             {
781               endindx -= endpos - iv[1]; // skip all this interval too
782             }
783           }
784         }
785       }
786       i++;
787     }
788     if (fs == fe && fe == -1)
789     {
790       return null;
791     }
792     List<int[]> ranges = new ArrayList<>();
793     if (fs <= fe)
794     {
795       intv = fs;
796       i = fs;
797       // truncate initial interval
798       iv = shiftFrom.get(intv++);
799       iv = new int[] { iv[0], iv[1] };// clone
800       if (i == fs)
801       {
802         iv[0] = startpos;
803       }
804       while (i != fe)
805       {
806         ranges.add(iv); // add initial range
807         iv = shiftFrom.get(intv++); // get next interval
808         iv = new int[] { iv[0], iv[1] };// clone
809         i++;
810       }
811       if (i == fe)
812       {
813         iv[1] = endpos;
814       }
815       ranges.add(iv); // add only - or final range
816     }
817     else
818     {
819       // walk from end of interval.
820       i = shiftFrom.size() - 1;
821       while (i > fs)
822       {
823         i--;
824       }
825       iv = shiftFrom.get(i);
826       iv = new int[] { iv[1], iv[0] };// reverse and clone
827       // truncate initial interval
828       if (i == fs)
829       {
830         iv[0] = startpos;
831       }
832       while (--i != fe)
833       { // fix apparent logic bug when fe==-1
834         ranges.add(iv); // add (truncated) reversed interval
835         iv = shiftFrom.get(i);
836         iv = new int[] { iv[1], iv[0] }; // reverse and clone
837       }
838       if (i == fe)
839       {
840         // interval is already reversed
841         iv[1] = endpos;
842       }
843       ranges.add(iv); // add only - or final range
844     }
845     // create array of start end intervals.
846     int[] range = null;
847     if (ranges != null && ranges.size() > 0)
848     {
849       range = new int[ranges.size() * 2];
850       intv = 0;
851       intvSize = ranges.size();
852       i = 0;
853       while (intv < intvSize)
854       {
855         iv = ranges.get(intv);
856         range[i++] = iv[0];
857         range[i++] = iv[1];
858         ranges.set(intv++, null); // remove
859       }
860     }
861     return range;
862   }
863
864   /**
865    * get the 'initial' position of mpos in To
866    * 
867    * @param mpos
868    *          position in from
869    * @return position of first word in to reference frame
870    */
871   public int getToPosition(int mpos)
872   {
873     int[] mp = shiftTo(mpos);
874     if (mp != null)
875     {
876       return mp[0];
877     }
878     return mpos;
879   }
880
881   /**
882    * 
883    * @return a MapList whose From range is this maplist's To Range, and vice
884    *         versa
885    */
886   public MapList getInverse()
887   {
888     return new MapList(getToRanges(), getFromRanges(), getToRatio(),
889             getFromRatio());
890   }
891
892   /**
893    * String representation - for debugging, not guaranteed not to change
894    */
895   @Override
896   public String toString()
897   {
898     StringBuilder sb = new StringBuilder(64);
899     sb.append("[");
900     for (int[] shift : fromShifts)
901     {
902       sb.append(" ").append(Arrays.toString(shift));
903     }
904     sb.append(" ] ");
905     sb.append(fromRatio).append(":").append(toRatio);
906     sb.append(" to [");
907     for (int[] shift : toShifts)
908     {
909       sb.append(" ").append(Arrays.toString(shift));
910     }
911     sb.append(" ]");
912     return sb.toString();
913   }
914
915   /**
916    * Extend this map list by adding the given map's ranges. There is no
917    * validation check that the ranges do not overlap existing ranges (or each
918    * other), but contiguous ranges are merged.
919    * 
920    * @param map
921    */
922   public void addMapList(MapList map)
923   {
924     if (this.equals(map))
925     {
926       return;
927     }
928     this.fromLowest = Math.min(fromLowest, map.fromLowest);
929     this.toLowest = Math.min(toLowest, map.toLowest);
930     this.fromHighest = Math.max(fromHighest, map.fromHighest);
931     this.toHighest = Math.max(toHighest, map.toHighest);
932
933     for (int[] range : map.getFromRanges())
934     {
935       addRange(range, fromShifts);
936     }
937     for (int[] range : map.getToRanges())
938     {
939       addRange(range, toShifts);
940     }
941   }
942
943   /**
944    * Adds the given range to a list of ranges. If the new range just extends
945    * existing ranges, the current endpoint is updated instead.
946    * 
947    * @param range
948    * @param addTo
949    */
950   static void addRange(int[] range, List<int[]> addTo)
951   {
952     /*
953      * list is empty - add to it!
954      */
955     if (addTo.size() == 0)
956     {
957       addTo.add(range);
958       return;
959     }
960
961     int[] last = addTo.get(addTo.size() - 1);
962     boolean lastForward = last[1] >= last[0];
963     boolean newForward = range[1] >= range[0];
964
965     /*
966      * contiguous range in the same direction - just update endpoint
967      */
968     if (lastForward == newForward && last[1] == range[0])
969     {
970       last[1] = range[1];
971       return;
972     }
973
974     /*
975      * next range starts at +1 in forward sense - update endpoint
976      */
977     if (lastForward && newForward && range[0] == last[1] + 1)
978     {
979       last[1] = range[1];
980       return;
981     }
982
983     /*
984      * next range starts at -1 in reverse sense - update endpoint
985      */
986     if (!lastForward && !newForward && range[0] == last[1] - 1)
987     {
988       last[1] = range[1];
989       return;
990     }
991
992     /*
993      * just add the new range
994      */
995     addTo.add(range);
996   }
997
998   /**
999    * Returns true if mapping is from forward strand, false if from reverse
1000    * strand. Result is just based on the first 'from' range that is not a single
1001    * position. Default is true unless proven to be false. Behaviour is not well
1002    * defined if the mapping has a mixture of forward and reverse ranges.
1003    * 
1004    * @return
1005    */
1006   public boolean isFromForwardStrand()
1007   {
1008     return isForwardStrand(getFromRanges());
1009   }
1010
1011   /**
1012    * Returns true if mapping is to forward strand, false if to reverse strand.
1013    * Result is just based on the first 'to' range that is not a single position.
1014    * Default is true unless proven to be false. Behaviour is not well defined if
1015    * the mapping has a mixture of forward and reverse ranges.
1016    * 
1017    * @return
1018    */
1019   public boolean isToForwardStrand()
1020   {
1021     return isForwardStrand(getToRanges());
1022   }
1023
1024   /**
1025    * A helper method that returns true unless at least one range has start >
1026    * end. Behaviour is undefined for a mixture of forward and reverse ranges.
1027    * 
1028    * @param ranges
1029    * @return
1030    */
1031   private boolean isForwardStrand(List<int[]> ranges)
1032   {
1033     boolean forwardStrand = true;
1034     for (int[] range : ranges)
1035     {
1036       if (range[1] > range[0])
1037       {
1038         break; // forward strand confirmed
1039       }
1040       else if (range[1] < range[0])
1041       {
1042         forwardStrand = false;
1043         break; // reverse strand confirmed
1044       }
1045     }
1046     return forwardStrand;
1047   }
1048
1049   /**
1050    * 
1051    * @return true if from, or to is a three to 1 mapping
1052    */
1053   public boolean isTripletMap()
1054   {
1055     return (toRatio == 3 && fromRatio == 1)
1056             || (fromRatio == 3 && toRatio == 1);
1057   }
1058
1059   /**
1060    * Returns a map which is the composite of this one and the input map. That
1061    * is, the output map has the fromRanges of this map, and its toRanges are the
1062    * toRanges of this map as transformed by the input map.
1063    * <p>
1064    * Returns null if the mappings cannot be traversed (not all toRanges of this
1065    * map correspond to fromRanges of the input), or if this.toRatio does not
1066    * match map.fromRatio.
1067    * 
1068    * <pre>
1069    * Example 1:
1070    *    this:   from [1-100] to [501-600]
1071    *    input:  from [10-40] to [60-90]
1072    *    output: from [10-40] to [560-590]
1073    * Example 2 ('reverse strand exons'):
1074    *    this:   from [1-100] to [2000-1951], [1000-951] // transcript to loci
1075    *    input:  from [1-50]  to [41-90] // CDS to transcript
1076    *    output: from [10-40] to [1960-1951], [1000-971] // CDS to gene loci
1077    * </pre>
1078    * 
1079    * @param map
1080    * @return
1081    */
1082   public MapList traverse(MapList map)
1083   {
1084     if (map == null)
1085     {
1086       return null;
1087     }
1088
1089     /*
1090      * compound the ratios by this rule:
1091      * A:B with M:N gives A*M:B*N
1092      * reduced by greatest common divisor
1093      * so 1:3 with 3:3 is 3:9 or 1:3
1094      * 1:3 with 3:1 is 3:3 or 1:1
1095      * 1:3 with 1:3 is 1:9
1096      * 2:5 with 3:7 is 6:35
1097      */
1098     int outFromRatio = getFromRatio() * map.getFromRatio();
1099     int outToRatio = getToRatio() * map.getToRatio();
1100     int gcd = MathUtils.gcd(outFromRatio, outToRatio);
1101     outFromRatio /= gcd;
1102     outToRatio /= gcd;
1103
1104     List<int[]> toRanges = new ArrayList<>();
1105     for (int[] range : getToRanges())
1106     {
1107       int fromLength = Math.abs(range[1] - range[0]) + 1;
1108       int[] transferred = map.locateInTo(range[0], range[1]);
1109       if (transferred == null || transferred.length % 2 != 0)
1110       {
1111         return null;
1112       }
1113
1114       /*
1115        *  convert [start1, end1, start2, end2, ...] 
1116        *  to [[start1, end1], [start2, end2], ...]
1117        */
1118       int toLength = 0;
1119       for (int i = 0; i < transferred.length;)
1120       {
1121         toRanges.add(new int[] { transferred[i], transferred[i + 1] });
1122         toLength += Math.abs(transferred[i + 1] - transferred[i]) + 1;
1123         i += 2;
1124       }
1125
1126       /*
1127        * check we mapped the full range - if not, abort
1128        */
1129       if (fromLength * map.getToRatio() != toLength * map.getFromRatio())
1130       {
1131         return null;
1132       }
1133     }
1134
1135     return new MapList(getFromRanges(), toRanges, outFromRatio, outToRatio);
1136   }
1137
1138   /**
1139    * Answers true if the mapping is from one contiguous range to another, else
1140    * false
1141    * 
1142    * @return
1143    */
1144   public boolean isContiguous()
1145   {
1146     return fromShifts.size() == 1 && toShifts.size() == 1;
1147   }
1148
1149   /**
1150    * <<<<<<< HEAD Returns the [start1, end1, start2, end2, ...] positions in the
1151    * 'from' range that map to positions between {@code start} and {@code end} in
1152    * the 'to' range. Note that for a reverse strand mapping this will return
1153    * ranges with end < start. Returns null if no mapped positions are found in
1154    * start-end.
1155    * 
1156    * @param start
1157    * @param end
1158    * @return
1159    */
1160   public int[] locateInFrom(int start, int end)
1161   {
1162     return mapPositions(start, end, toShifts, fromShifts, toRatio,
1163             fromRatio);
1164   }
1165
1166   /**
1167    * Returns the [start1, end1, start2, end2, ...] positions in the 'to' range
1168    * that map to positions between {@code start} and {@code end} in the 'from'
1169    * range. Note that for a reverse strand mapping this will return ranges with
1170    * end < start. Returns null if no mapped positions are found in start-end.
1171    * 
1172    * @param start
1173    * @param end
1174    * @return
1175    */
1176   public int[] locateInTo(int start, int end)
1177   {
1178     return mapPositions(start, end, fromShifts, toShifts, fromRatio,
1179             toRatio);
1180   }
1181
1182   /**
1183    * Helper method that returns the [start1, end1, start2, end2, ...] positions
1184    * in {@code targetRange} that map to positions between {@code start} and
1185    * {@code end} in {@code sourceRange}. Note that for a reverse strand mapping
1186    * this will return ranges with end < start. Returns null if no mapped
1187    * positions are found in start-end.
1188    * 
1189    * @param start
1190    * @param end
1191    * @param sourceRange
1192    * @param targetRange
1193    * @param sourceWordLength
1194    * @param targetWordLength
1195    * @return
1196    */
1197   final static int[] mapPositions(int start, int end,
1198           List<int[]> sourceRange, List<int[]> targetRange,
1199           int sourceWordLength, int targetWordLength)
1200   {
1201     if (end < start)
1202     {
1203       int tmp = end;
1204       end = start;
1205       start = tmp;
1206     }
1207
1208     /*
1209      * traverse sourceRange and mark offsets in targetRange 
1210      * of any positions that lie in [start, end]
1211      */
1212     BitSet offsets = getMappedOffsetsForPositions(start, end, sourceRange,
1213             sourceWordLength, targetWordLength);
1214
1215     /*
1216      * traverse targetRange and collect positions at the marked offsets
1217      */
1218     List<int[]> mapped = getPositionsForOffsets(targetRange, offsets);
1219
1220     // TODO: or just return the List and adjust calling code to match
1221     return mapped.isEmpty() ? null : MappingUtils.rangeListToArray(mapped);
1222   }
1223
1224   /**
1225    * Scans the list of {@code ranges} for any values (positions) that lie
1226    * between start and end (inclusive), and records the <em>offsets</em> from
1227    * the start of the list as a BitSet. The offset positions are converted to
1228    * corresponding words in blocks of {@code wordLength2}.
1229    * 
1230    * <pre>
1231    * For example:
1232    * 1:1 (e.g. gene to CDS):
1233    * ranges { [10-20], [31-40] }, wordLengthFrom = wordLength 2 = 1
1234    *   for start = 1, end = 9, returns a BitSet with no bits set
1235    *   for start = 1, end = 11, returns a BitSet with bits 0-1 set
1236    *   for start = 15, end = 35, returns a BitSet with bits 5-15 set
1237    * 1:3 (peptide to codon):
1238    * ranges { [1-200] }, wordLengthFrom = 1, wordLength 2 = 3
1239    *   for start = 9, end = 9, returns a BitSet with bits 24-26 set
1240    * 3:1 (codon to peptide):
1241    * ranges { [101-150], [171-180] }, wordLengthFrom = 3, wordLength 2 = 1
1242    *   for start = 101, end = 102 (partial first codon), returns a BitSet with bit 0 set
1243    *   for start = 150, end = 171 (partial 17th codon), returns a BitSet with bit 16 set
1244    * 3:1 (circular DNA to peptide):
1245    * ranges { [101-150], [21-30] }, wordLengthFrom = 3, wordLength 2 = 1
1246    *   for start = 24, end = 40 (spans codons 18-20), returns a BitSet with bits 17-19 set
1247    * </pre>
1248    * 
1249    * @param start
1250    * @param end
1251    * @param sourceRange
1252    * @param sourceWordLength
1253    * @param targetWordLength
1254    * @return
1255    */
1256   protected final static BitSet getMappedOffsetsForPositions(int start,
1257           int end, List<int[]> sourceRange, int sourceWordLength,
1258           int targetWordLength)
1259   {
1260     BitSet overlaps = new BitSet();
1261     int offset = 0;
1262     final int s1 = sourceRange.size();
1263     for (int i = 0; i < s1; i++)
1264     {
1265       int[] range = sourceRange.get(i);
1266       final int offset1 = offset;
1267       int overlapStartOffset = -1;
1268       int overlapEndOffset = -1;
1269
1270       if (range[1] >= range[0])
1271       {
1272         /*
1273          * forward direction range
1274          */
1275         if (start <= range[1] && end >= range[0])
1276         {
1277           /*
1278            * overlap
1279            */
1280           int overlapStart = Math.max(start, range[0]);
1281           overlapStartOffset = offset1 + overlapStart - range[0];
1282           int overlapEnd = Math.min(end, range[1]);
1283           overlapEndOffset = offset1 + overlapEnd - range[0];
1284         }
1285       }
1286       else
1287       {
1288         /*
1289          * reverse direction range
1290          */
1291         if (start <= range[0] && end >= range[1])
1292         {
1293           /*
1294            * overlap
1295            */
1296           int overlapStart = Math.max(start, range[1]);
1297           int overlapEnd = Math.min(end, range[0]);
1298           overlapStartOffset = offset1 + range[0] - overlapEnd;
1299           overlapEndOffset = offset1 + range[0] - overlapStart;
1300         }
1301       }
1302
1303       if (overlapStartOffset > -1)
1304       {
1305         /*
1306          * found an overlap
1307          */
1308         if (sourceWordLength != targetWordLength)
1309         {
1310           /*
1311            * convert any overlap found to whole words in the target range
1312            * (e.g. treat any partial codon overlap as if the whole codon)
1313            */
1314           overlapStartOffset -= overlapStartOffset % sourceWordLength;
1315           overlapStartOffset = overlapStartOffset / sourceWordLength
1316                   * targetWordLength;
1317
1318           /*
1319            * similar calculation for range end, adding 
1320            * (wordLength2 - 1) for end of mapped word
1321            */
1322           overlapEndOffset -= overlapEndOffset % sourceWordLength;
1323           overlapEndOffset = overlapEndOffset / sourceWordLength
1324                   * targetWordLength;
1325           overlapEndOffset += targetWordLength - 1;
1326         }
1327         overlaps.set(overlapStartOffset, overlapEndOffset + 1);
1328       }
1329       offset += 1 + Math.abs(range[1] - range[0]);
1330     }
1331     return overlaps;
1332   }
1333
1334   /**
1335    * Returns a (possibly empty) list of the [start-end] values (positions) at
1336    * offsets in the {@code targetRange} list that are marked by 'on' bits in the
1337    * {@code offsets} bitset.
1338    * 
1339    * @param targetRange
1340    * @param offsets
1341    * @return
1342    */
1343   protected final static List<int[]> getPositionsForOffsets(
1344           List<int[]> targetRange, BitSet offsets)
1345   {
1346     List<int[]> mapped = new ArrayList<>();
1347     if (offsets.isEmpty())
1348     {
1349       return mapped;
1350     }
1351
1352     /*
1353      * count of positions preceding ranges[i]
1354      */
1355     int traversed = 0;
1356
1357     /*
1358      * for each [from-to] range in ranges:
1359      * - find subranges (if any) at marked offsets
1360      * - add the start-end values at the marked positions
1361      */
1362     final int toAdd = offsets.cardinality();
1363     int added = 0;
1364     final int s2 = targetRange.size();
1365     for (int i = 0; added < toAdd && i < s2; i++)
1366     {
1367       int[] range = targetRange.get(i);
1368       added += addOffsetPositions(mapped, traversed, range, offsets);
1369       traversed += Math.abs(range[1] - range[0]) + 1;
1370     }
1371     return mapped;
1372   }
1373
1374   /**
1375    * Helper method that adds any start-end subranges of {@code range} that are
1376    * at offsets in {@code range} marked by set bits in overlaps.
1377    * {@code mapOffset} is added to {@code range} offset positions. Returns the
1378    * count of positions added.
1379    * 
1380    * @param mapped
1381    * @param mapOffset
1382    * @param range
1383    * @param overlaps
1384    * @return
1385    */
1386   final static int addOffsetPositions(List<int[]> mapped,
1387           final int mapOffset, final int[] range, final BitSet overlaps)
1388   {
1389     final int rangeLength = 1 + Math.abs(range[1] - range[0]);
1390     final int step = range[1] < range[0] ? -1 : 1;
1391     int offsetStart = 0; // offset into range
1392     int added = 0;
1393
1394     while (offsetStart < rangeLength)
1395     {
1396       /*
1397        * find the start of the next marked overlap offset;
1398        * if there is none, or it is beyond range, then finished
1399        */
1400       int overlapStart = overlaps.nextSetBit(mapOffset + offsetStart);
1401       if (overlapStart == -1 || overlapStart - mapOffset >= rangeLength)
1402       {
1403         /*
1404          * no more overlaps, or no more within range[]
1405          */
1406         return added;
1407       }
1408       overlapStart -= mapOffset;
1409
1410       /*
1411        * end of the overlap range is just before the next clear bit;
1412        * restrict it to end of range if necessary;
1413        * note we may add a reverse strand range here (end < start)
1414        */
1415       int overlapEnd = overlaps.nextClearBit(mapOffset + overlapStart + 1);
1416       overlapEnd = (overlapEnd == -1) ? rangeLength - 1
1417               : Math.min(rangeLength - 1, overlapEnd - mapOffset - 1);
1418       int startPosition = range[0] + step * overlapStart;
1419       int endPosition = range[0] + step * overlapEnd;
1420       mapped.add(new int[] { startPosition, endPosition });
1421       offsetStart = overlapEnd + 1;
1422       added += Math.abs(endPosition - startPosition) + 1;
1423     }
1424
1425     return added;
1426   }
1427
1428   /*
1429    * Returns the [start, end...] positions in the range mapped from, that are
1430    * mapped to by part or all of the given begin-end of the range mapped to.
1431    * Returns null if begin-end does not overlap any position mapped to.
1432    * 
1433    * @param begin
1434    * @param end
1435    * @return
1436    */
1437   public int[] getOverlapsInFrom(final int begin, final int end)
1438   {
1439     int[] overlaps = MappingUtils.findOverlap(toShifts, begin, end);
1440
1441     return overlaps == null ? null : locateInFrom(overlaps[0], overlaps[1]);
1442   }
1443
1444   /**
1445    * Returns the [start, end...] positions in the range mapped to, that are
1446    * mapped to by part or all of the given begin-end of the range mapped from.
1447    * Returns null if begin-end does not overlap any position mapped from.
1448    * 
1449    * @param begin
1450    * @param end
1451    * @return
1452    */
1453   public int[] getOverlapsInTo(final int begin, final int end)
1454   {
1455     int[] overlaps = MappingUtils.findOverlap(fromShifts, begin, end);
1456
1457     return overlaps == null ? null : locateInTo(overlaps[0], overlaps[1]);
1458   }
1459 }