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