generalised feature attribute parsing slightly (no embl specs to work from yet)
[jalview.git] / src / jalview / datamodel / SearchResults.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2007 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[]\r
39           {\r
40           new Match(seq, start, end)};\r
41       return;\r
42     }\r
43 \r
44     int mSize = matches.length;\r
45 \r
46     Match[] tmp = new Match[mSize + 1];\r
47     int m;\r
48     for (m = 0; m < mSize; m++)\r
49     {\r
50       tmp[m] = matches[m];\r
51     }\r
52 \r
53     tmp[m] = new Match(seq, start, end);\r
54 \r
55     matches = tmp;\r
56   }\r
57 \r
58   /**\r
59    * This Method returns the search matches which lie between the\r
60    * start and end points of the sequence in question. It is\r
61    * optimised for returning objects for drawing on SequenceCanvas\r
62    */\r
63   public int[] getResults(SequenceI sequence, int start, int end)\r
64   {\r
65     if (matches == null)\r
66     {\r
67       return null;\r
68     }\r
69 \r
70     int[] result = null;\r
71     int[] tmp = null;\r
72     int resultLength;\r
73 \r
74     for (int m = 0; m < matches.length; m++)\r
75     {\r
76       if (matches[m].sequence == sequence)\r
77       {\r
78         int matchStart = matches[m].sequence.findIndex(matches[m].start) - 1;\r
79         int matchEnd = matches[m].sequence.findIndex(matches[m].end) - 1;\r
80 \r
81         if (matchStart <= end && matchEnd >= start)\r
82         {\r
83           if (matchStart < start)\r
84           {\r
85             matchStart = start;\r
86           }\r
87 \r
88           if (matchEnd > end)\r
89           {\r
90             matchEnd = end;\r
91           }\r
92 \r
93           if (result == null)\r
94           {\r
95             result = new int[]\r
96                 {\r
97                 matchStart, matchEnd};\r
98           }\r
99           else\r
100           {\r
101             resultLength = result.length;\r
102             tmp = new int[resultLength + 2];\r
103             System.arraycopy(result, 0, tmp, 0, resultLength);\r
104             result = tmp;\r
105             result[resultLength] = matchStart;\r
106             result[resultLength + 1] = matchEnd;\r
107           }\r
108         }\r
109       }\r
110     }\r
111     return result;\r
112   }\r
113 \r
114   public int getSize()\r
115   {\r
116     return matches == null ? 0 : matches.length;\r
117   }\r
118 \r
119   public SequenceI getResultSequence(int index)\r
120   {\r
121     return matches[index].sequence;\r
122   }\r
123 \r
124   public int getResultStart(int index)\r
125   {\r
126     return matches[index].start;\r
127   }\r
128 \r
129   public int getResultEnd(int index)\r
130   {\r
131     return matches[index].end;\r
132   }\r
133 \r
134   class Match\r
135   {\r
136     SequenceI sequence;\r
137     int start;\r
138     int end;\r
139 \r
140     public Match(SequenceI seq, int start, int end)\r
141     {\r
142       sequence = seq;\r
143       this.start = start;\r
144       this.end = end;\r
145     }\r
146   }\r
147 }\r