X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FSearchResults.java;h=9d357c8e2f3c2a5a09125858d3910ee89d9198ec;hb=HEAD;hp=7c3bba70cfb05baea401cc0fb178ea188b908f2d;hpb=6b13a606027284408286d1cd2833470d1f9b9bfe;p=jalview.git diff --git a/src/jalview/datamodel/SearchResults.java b/src/jalview/datamodel/SearchResults.java index 7c3bba7..9d357c8 100755 --- a/src/jalview/datamodel/SearchResults.java +++ b/src/jalview/datamodel/SearchResults.java @@ -35,13 +35,14 @@ public class SearchResults implements SearchResultsI { private int count; - private List matches = new ArrayList<>(); + private ArrayList matches = new ArrayList<>(); /** * One match consists of a sequence reference, start and end positions. * Discontiguous ranges in a sequence require two or more Match objects. */ - public class Match implements SearchResultMatchI + public class Match + implements SearchResultMatchI, Comparable { final SequenceI sequence; @@ -160,6 +161,42 @@ public class SearchResults implements SearchResultsI { return (sequence == seq && start <= from && end >= to); } + + @Override + public boolean adjacent(SequenceI seq, int from, int to) + { + return (sequence == seq && ((start <= from && end >= to) + || (from <= (end + 1) && to >= (end + 1)) + || (from <= (start - 1) && to >= (start - 1)))); + } + + @Override + public int compareTo(SearchResultMatchI o) + { + if (start < o.getStart()) + { + return -1; + } + if (start > o.getStart()) + { + return +1; + } + if (end < o.getEnd()) + { + return -1; + } + if (end > o.getEnd()) + { + return +1; + } + if (sequence != o.getSequence()) + { + int hashc = sequence.hashCode(), oseq = o.getSequence().hashCode(); + return (hashc < oseq) ? -1 : 1; + } + return 0; + } + } @Override @@ -193,6 +230,58 @@ public class SearchResults implements SearchResultsI } @Override + public boolean appendResult(SequenceI sequence, int start, int end) + { + + Match m = new Match(sequence, start, end); + + boolean appending = false; + + // we dynamically maintain an interval to add as we test each range in the + // list + + int cstart = start, cend = end; + List toRemove = new ArrayList<>(); + for (SearchResultMatchI thatm : matches) + { + if (thatm.getSequence() == sequence) + { + if (thatm.contains(sequence, cstart, cend)) + { + // found a match containing the current range. nothing else to do + // except report if we operated on the list + return appending; + } + if (thatm.adjacent(sequence, cstart, cend)) + { + // update the match to add with the adjacent start/end + start = Math.min(m.start, thatm.getStart()); + end = Math.max(m.end, thatm.getEnd()); + // and check if we keep or remove the old one + if (thatm.getStart() != start || thatm.getEnd() != end) + { + toRemove.add(thatm); + count--; + cstart = start; + cend = end; + appending = true; + } + else + { + return false; + } + } + } + } + matches.removeAll(toRemove); + { + matches.add(new Match(sequence, cstart, cend)); + count++; + } + return appending; + } + + @Override public boolean involvesSequence(SequenceI sequence) { final int start = sequence.getStart(); @@ -234,8 +323,8 @@ public class SearchResults implements SearchResultsI { mfound = true; matchStart = sequence.findIndex(m.start) - 1; - matchEnd = m.start == m.end ? matchStart : sequence - .findIndex(m.end) - 1; + matchEnd = m.start == m.end ? matchStart + : sequence.findIndex(m.end) - 1; } if (mfound) @@ -269,7 +358,8 @@ public class SearchResults implements SearchResultsI else { // debug - // System.err.println("Outwith bounds!" + matchStart+">"+end +" or " + // jalview.bin.Console.errPrintln("Outwith bounds!" + + // matchStart+">"+end +" or " // + matchEnd+"<"+start); } } @@ -349,8 +439,10 @@ public class SearchResults implements SearchResultsI } /** - * Two SearchResults are considered equal if they contain the same matches in - * the same order. + * Two SearchResults are considered equal if they contain the same matches + * (Sequence, start position, end position) in the same order + * + * @see Match#equals(Object) */ @Override public boolean equals(Object obj) @@ -368,4 +460,28 @@ public class SearchResults implements SearchResultsI { matches.addAll(toAdd.getResults()); } + + @Override + public List getMatchingSubSequences() + { + List seqs = new ArrayList<>(); + + /* + * assemble dataset sequences, and template new sequence features, + * for the amend features dialog + */ + for (SearchResultMatchI match : matches) + { + SequenceI seq = match.getSequence(); + while (seq.getDatasetSequence() != null) + { + seq = seq.getDatasetSequence(); + } + // getSubSequence is index-base0, findIndex returns index-base1 + seqs.add(seq.getSubSequence(seq.findIndex(match.getStart()) - 1, + seq.findIndex(match.getEnd()))); + } + return seqs; + } + }