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