updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / datamodel / SearchResults.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.datamodel;
20
21 public class SearchResults
22 {
23
24   Match [] matches;
25
26     /**
27      * This method replaces the old search results which merely
28      * held an alignment index of search matches. This broke
29      * when sequences were moved around the alignment
30      * @param seq Sequence
31      * @param start int
32      * @param end int
33      */
34   public void addResult(SequenceI seq, int start, int end)
35   {
36     if(matches == null)
37     {
38       matches = new Match[]{new Match(seq, start, end)};
39       return;
40     }
41
42     int mSize = matches.length;
43
44     Match [] tmp = new Match[mSize+1];
45     int m;
46     for(m=0; m<mSize; m++)
47     {
48       tmp[m] = matches[m];
49     }
50
51     tmp[m] = new Match(seq, start, end);
52
53     matches = tmp;
54   }
55
56   /**
57   * This Method returns the search matches which lie between the
58   * start and end points of the sequence in question. It is
59   * optimised for returning objects for drawing on SequenceCanvas
60   */
61   public int [] getResults(SequenceI sequence, int start, int end)
62   {
63     if(matches==null)
64       return null;
65
66     int [] result = null;
67     int [] tmp = null;
68     int resultLength;
69
70     for(int m=0; m<matches.length; m++)
71     {
72       if( matches[m].sequence == sequence )
73       {
74         int matchStart = matches[m].sequence.findIndex( matches[m].start ) - 1;
75         int matchEnd = matches[m].sequence.findIndex( matches[m].end ) - 1;
76
77         if(matchStart<=end && matchEnd>=start)
78         {
79           if(matchStart<start)
80             matchStart = start;
81
82           if(matchEnd>end)
83             matchEnd = end;
84
85
86           if(result==null)
87             result = new int[]{matchStart, matchEnd};
88           else
89           {
90             resultLength = result.length;
91             tmp = new int[resultLength+2];
92             System.arraycopy(result,0,tmp,0,resultLength);
93             result = tmp;
94             result[resultLength] = matchStart;
95             result[resultLength+1] = matchEnd;
96           }
97         }
98       }
99     }
100     return result;
101   }
102
103   public int getSize()
104   {
105     return matches==null ? 0 : matches.length;
106   }
107
108   public SequenceI getResultSequence(int index)
109   {    return matches[index].sequence;  }
110
111   public int getResultStart(int index)
112   {    return matches[index].start;  }
113
114   public int getResultEnd(int index)
115   {    return matches[index].end;  }
116
117   class Match
118   {
119     SequenceI sequence;
120     int start;
121     int end;
122
123     public Match(SequenceI seq, int start, int end)
124     {
125      sequence = seq;
126      this.start = start;
127      this.end = end;
128     }
129   }
130 }