4c873062d13a0d46b1a8cbf996bafa9edd80c4f7
[jalview.git] / src / intervalstore / impl / NCListBuilder.java
1 /*
2 BSD 3-Clause License
3
4 Copyright (c) 2018, Mungo Carstairs
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice, this
11   list of conditions and the following disclaimer.
12
13 * Redistributions in binary form must reproduce the above copyright notice,
14   this list of conditions and the following disclaimer in the documentation
15   and/or other materials provided with the distribution.
16
17 * Neither the name of the copyright holder nor the names of its
18   contributors may be used to endorse or promote products derived from
19   this software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 package intervalstore.impl;
33
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.Comparator;
37 import java.util.List;
38
39 import intervalstore.api.IntervalI;
40 import intervalstore.impl.Range;
41
42 /**
43  * A comparator that orders ranges by either start position ascending. If
44  * position matches, ordering is by length descending. This provides the
45  * canonical ordering of intervals into subranges in order to build a nested
46  * containment list.
47  * 
48  * @author gmcarstairs
49  */
50 public class NCListBuilder<T extends IntervalI>
51 {
52   class NCListComparator<V extends IntervalI> implements Comparator<V>
53   {
54     /**
55      * Compares two intervals in a way that will sort a list by start position
56      * ascending, then by length descending. Answers
57      * <ul>
58      * <li>a negative value if o1.begin < o2.begin</li>
59      * <li>else a positive value if o1.begin > o2.begin</li>
60      * <li>else a negative value if o1.end > o2.end</li>
61      * <li>else a positive value of o1.end < o2.end</li>
62      * <li>else zero</li
63      * </ul>
64      */
65     @Override
66     public int compare(V o1, V o2)
67     {
68       int order = Integer.compare(o1.getBegin(), o2.getBegin());
69       if (order == 0)
70       {
71         /*
72          * if tied on start position, longer length sorts to left
73          * i.e. the negation of normal ordering by length
74          */
75         order = Integer.compare(o2.getEnd(), o1.getEnd());
76       }
77       return order;
78     }
79   }
80
81   private Comparator<T> comparator = new NCListComparator<>();
82   
83   /**
84    * Default constructor
85    */
86   public NCListBuilder()
87   {
88   }
89
90   /**
91    * Answers a comparator which sorts items of type T by start position
92    * ascending, and within that by end position (length) descending)
93    * 
94    * @return
95    */
96   Comparator<T> getComparator()
97   {
98     return comparator;
99   }
100
101   /**
102    * Sorts and traverses the ranges to identify sublists, whose start intervals
103    * are overlapping or disjoint but not mutually contained. Answers a list of
104    * start-end indices of the sorted list of ranges.
105    * 
106    * @param ranges
107    * @return
108    */
109   List<IntervalI> partitionNestedSublists(List<T> ranges)
110   {
111     List<IntervalI> sublists = new ArrayList<>();
112   
113     /*
114      * sort by start ascending, length descending, so that
115      * contained intervals follow their containing interval
116      */
117     Collections.sort(ranges, comparator);
118   
119     int listStartIndex = 0;
120   
121     IntervalI lastParent = ranges.get(0);
122     boolean first = true;
123   
124     for (int i = 0; i < ranges.size(); i++)
125     {
126       IntervalI nextInterval = ranges.get(i);
127       if (!first && !lastParent.properlyContainsInterval(nextInterval))
128       {
129         /*
130          * this interval is not properly contained in the parent; 
131          * close off the last sublist
132          */
133         sublists.add(new Range(listStartIndex, i - 1));
134         listStartIndex = i;
135         lastParent = nextInterval;
136       }
137       first = false;
138     }
139   
140     sublists.add(new Range(listStartIndex, ranges.size() - 1));
141     return sublists;
142   }
143 }