e62c1507f9e4fdb1af855acb701ab6e28bc22ab5
[jalview.git] / src / jalview / datamodel / SearchResults.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 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 held an alignment
28    * index of search matches. This broke when sequences were moved around the
29    * alignment
30    * 
31    * @param seq
32    *                Sequence
33    * @param start
34    *                int
35    * @param end
36    *                int
37    */
38   public void addResult(SequenceI seq, int start, int end)
39   {
40     if (matches == null)
41     {
42       matches = new Match[]
43       { new Match(seq, start, end) };
44       return;
45     }
46
47     int mSize = matches.length;
48
49     Match[] tmp = new Match[mSize + 1];
50     int m;
51     for (m = 0; m < mSize; m++)
52     {
53       tmp[m] = matches[m];
54     }
55
56     tmp[m] = new Match(seq, start, end);
57
58     matches = tmp;
59   }
60
61   /**
62    * This Method returns the search matches which lie between the start and end
63    * points of the sequence in question. It is optimised for returning objects
64    * for drawing on SequenceCanvas
65    */
66   public int[] getResults(SequenceI sequence, int start, int end)
67   {
68     if (matches == null)
69     {
70       return null;
71     }
72
73     int[] result = null;
74     int[] tmp = null;
75     int resultLength, matchStart = 0, matchEnd = 0;
76     boolean mfound;
77     for (int m = 0; m < matches.length; m++)
78     {
79       mfound = false;
80       if (matches[m].sequence == sequence)
81       {
82         mfound = true;
83         // locate aligned position
84         matchStart = sequence.findIndex(matches[m].start) - 1;
85         matchEnd = sequence.findIndex(matches[m].end) - 1;
86       }
87       else if (matches[m].sequence == sequence.getDatasetSequence())
88       {
89         mfound = true;
90         // locate region in local context
91         matchStart = sequence.findIndex(matches[m].start) - 1;
92         matchEnd = sequence.findIndex(matches[m].end) - 1;
93       }
94       if (mfound)
95       {
96         if (matchStart <= end && matchEnd >= start)
97         {
98           if (matchStart < start)
99           {
100             matchStart = start;
101           }
102
103           if (matchEnd > end)
104           {
105             matchEnd = end;
106           }
107
108           if (result == null)
109           {
110             result = new int[]
111             { matchStart, matchEnd };
112           }
113           else
114           {
115             resultLength = result.length;
116             tmp = new int[resultLength + 2];
117             System.arraycopy(result, 0, tmp, 0, resultLength);
118             result = tmp;
119             result[resultLength] = matchStart;
120             result[resultLength + 1] = matchEnd;
121           }
122         }
123       }
124     }
125     return result;
126   }
127
128   public int getSize()
129   {
130     return matches == null ? 0 : matches.length;
131   }
132
133   public SequenceI getResultSequence(int index)
134   {
135     return matches[index].sequence;
136   }
137
138   public int getResultStart(int index)
139   {
140     return matches[index].start;
141   }
142
143   public int getResultEnd(int index)
144   {
145     return matches[index].end;
146   }
147
148   class Match
149   {
150     SequenceI sequence;
151
152     int start;
153
154     int end;
155
156     public Match(SequenceI seq, int start, int end)
157     {
158       sequence = seq;
159       this.start = start;
160       this.end = end;
161     }
162   }
163 }