678593d20065cda77c3bf46a41d1ca626da48c98
[jalview.git] / src2 / 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
41 /**
42  * A comparator that orders ranges by either start position ascending. If
43  * position matches, ordering is by length descending. This provides the
44  * canonical ordering of intervals into subranges in order to build a nested
45  * containment list.
46  * 
47  * @author gmcarstairs
48  */
49 public class NCListBuilder<T extends IntervalI>
50 {
51   /**
52    * Compares two intervals in a way that will sort a list by start position
53    * ascending, then by length descending
54    */
55   class NCListComparator<V extends IntervalI> implements Comparator<V>
56   {
57     @Override
58     public int compare(V o1, V o2)
59     {
60       int order = Integer.compare(o1.getBegin(), o2.getBegin());
61       if (order == 0)
62       {
63         order = Integer.compare(o2.getEnd(), o1.getEnd());
64       }
65       return order;
66     }
67   }
68   
69   /**
70    * Default constructor
71    */
72   public NCListBuilder()
73   {
74   }
75
76   /**
77    * Sorts and traverses the ranges to identify sublists, whose start intervals
78    * are overlapping or disjoint but not mutually contained. Answers a list of
79    * start-end indices of the sorted list of ranges.
80    * 
81    * @param ranges
82    * @return
83    */
84   List<IntervalI> partitionNestedSublists(List<T> ranges)
85   {
86     List<IntervalI> sublists = new ArrayList<>();
87   
88     /*
89      * sort by start ascending, length descending, so that
90      * contained intervals follow their containing interval
91      */
92     Collections.sort(ranges, IntervalI.COMPARE_BEGIN_ASC_END_DESC);
93   
94     int listStartIndex = 0;
95   
96     IntervalI lastParent = ranges.get(0);
97     boolean first = true;
98   
99     for (int i = 0; i < ranges.size(); i++)
100     {
101       IntervalI nextInterval = ranges.get(i);
102       if (!first && !lastParent.properlyContainsInterval(nextInterval))
103       {
104         /*
105          * this interval is not properly contained in the parent; 
106          * close off the last sublist
107          */
108         sublists.add(new Range(listStartIndex, i - 1));
109         listStartIndex = i;
110         lastParent = nextInterval;
111       }
112       first = false;
113     }
114   
115     sublists.add(new Range(listStartIndex, ranges.size() - 1));
116     return sublists;
117   }
118 }