+ private void linkFeatures(SequenceFeature[] intervals)
+ {
+ if (intervals.length < 2)
+ {
+ return;
+ }
+ int maxEnd = intervals[0].end;
+ for (int i = 1, n = intervals.length; i < n; i++)
+ {
+ SequenceFeature ithis = intervals[i];
+ if (ithis.begin <= maxEnd)
+ {
+ ithis.containedBy = getContainedBy(intervals[i - 1], ithis);
+ }
+ if (ithis.end > maxEnd)
+ {
+ maxEnd = ithis.end;
+ }
+ }
+ }
+
+ private SequenceFeature getContainedBy(SequenceFeature sf,
+ SequenceFeature sf0)
+ {
+ int begin = sf0.begin;
+ while (sf != null)
+ {
+ if (begin <= sf.end)
+ {
+ System.out.println("\nFS found " + sf0.index + ":" + sf0
+ + "\nFS in " + sf.index + ":" + sf);
+ return sf;
+ }
+ sf = sf.containedBy;
+ }
+ return null;
+ }
+
+ private SequenceFeature findClosestFeature(SequenceFeature[] l, int pos)
+ {
+ int low = 0;
+ int high = l.length - 1;
+ while (low <= high)
+ {
+ int mid = (low + high) >>> 1;
+ SequenceFeature f = l[mid];
+ switch (Long.signum(f.begin - pos))
+ {
+ case -1:
+ low = mid + 1;
+ continue;
+ case 1:
+ high = mid - 1;
+ continue;
+ case 0:
+
+ while (++mid <= high && l[mid].begin == pos)
+ {
+ ;
+ }
+ mid--;
+ return l[mid];
+ }
+ }
+ // -1 here?
+ return (high < 0 || low >= l.length ? null : l[high]);
+ }
+
+ private void checkOne(SequenceFeature sf, int pos,
+ List<SequenceFeature> result)
+ {
+ if (sf.begin <= pos && sf.end >= pos)
+ {
+ result.add(sf);
+ }
+ return;
+ }
+
+ /*
+ * contact features ordered by first contact position
+ */
+ private SequenceFeature[] orderedFeatureStarts;
+
+ private void rebuildArrays(int n)
+ {
+ if (startComp == null)
+ {
+ startComp = new StartComparator();
+ }
+ orderedFeatureStarts = new SequenceFeature[n];
+
+ for (int i = n; --i >= 0;)
+ {
+ SequenceFeature sf = featuresList.get(i);
+ sf.index = i;
+ orderedFeatureStarts[i] = sf;
+ }
+ Arrays.sort(orderedFeatureStarts, startComp);
+ linkFeatures(orderedFeatureStarts);
+ }
+
+ class StartComparator implements Comparator<SequenceFeature>
+ {
+
+ int pos;
+
+ @Override
+ public int compare(SequenceFeature o1, SequenceFeature o2)
+ {
+ int p1 = o1.begin;
+ int p2 = o2.begin;
+ return (p1 < p2 ? -1 : p1 > p2 ? 1 : 0);
+ }
+
+ }
+
+ static StartComparator startComp;
+
+ // class EndComparator implements Comparator<SequenceFeature>
+ // {
+ //
+ // int pos;
+ //
+ // @Override
+ // public int compare(SequenceFeature o1, SequenceFeature o2)
+ // {
+ // int p1 = o1.end;
+ // int p2 = o2.end;
+ // int val = (p1 < p2 ? 1 : p1 > p2 ? -1 : 0);
+ // return val;
+ // }
+ //
+ // }
+ //
+ // static EndComparator endComp;
+