Hidden representatives moved from sequence to viewport
[jalview.git] / src / jalview / analysis / Finder.java
1 package jalview.analysis;
2 import java.util.Vector;
3
4 import jalview.datamodel.*;
5
6 public class Finder {
7     /**
8      * Implements the search algorithms for the Find dialog box.
9      */
10     SearchResults searchResults;
11     AlignmentI alignment;
12     jalview.datamodel.SequenceGroup selection=null;
13     Vector idMatch=null;
14     boolean caseSensitive=false;
15     boolean findAll=false;
16     com.stevesoft.pat.Regex regex=null;
17     /**
18      * hold's last-searched position between calles to find(false)
19      */
20     int seqIndex=0,resIndex=0;
21     public Finder(AlignmentI alignment, SequenceGroup selection) {
22         this.alignment=alignment;
23         this.selection = selection;
24     }
25
26     public Finder(AlignmentI alignment, SequenceGroup selectionGroup, int seqIndex, int resIndex) {
27         this(alignment, selectionGroup);
28         this.seqIndex=seqIndex;
29         this.resIndex=resIndex;
30     }
31
32     public boolean find(String searchString) {
33         boolean hasResults=false;
34         if(!caseSensitive)
35             searchString = searchString.toUpperCase();
36         regex = new com.stevesoft.pat.Regex(searchString);
37         searchResults = new SearchResults();
38         idMatch = new Vector();
39         Sequence seq;
40         String item = null;
41         boolean found = false;
42
43         ////// is the searchString a residue number?
44         try
45         {
46             int res = Integer.parseInt(searchString);
47             found = true;
48             if (selection == null || selection.getSize() < 1)
49             {
50               seq = (Sequence) alignment.getSequenceAt(0);
51             }
52             else
53             {
54               seq = (Sequence) (selection.getSequenceAt(0));
55             }
56
57             searchResults.addResult(seq, res, res);
58             hasResults=true;
59         }
60         catch (NumberFormatException ex)
61         {
62         }
63
64         ///////////////////////////////////////////////
65
66         int end = alignment.getHeight();
67
68
69         if (selection != null)
70         {
71             if ((selection.getSize() < 1) ||
72                     ((selection.getEndRes() - selection.getStartRes()) < 2))
73             {
74                 selection = null;
75             }
76         }
77
78         while (!found && (seqIndex < end))
79         {
80             seq = (Sequence) alignment.getSequenceAt(seqIndex);
81
82             if ((selection != null) && !selection.getSequences(null).contains(seq))
83             {
84                 seqIndex++;
85                 resIndex = 0;
86
87                 continue;
88             }
89
90             item = seq.getSequenceAsString();
91             // JBPNote - check if this toUpper which is present in the application implementation makes a difference
92             //if(!caseSensitive)
93             //  item = item.toUpperCase();
94
95             if ((selection != null) &&
96                     (selection.getEndRes() < alignment.getWidth()-1))
97             {
98                 item = item.substring(0, selection.getEndRes() + 1);
99             }
100
101             ///Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
102             StringBuffer noGapsSB = new StringBuffer();
103             int insertCount = 0;
104             Vector spaces = new Vector();
105
106             for (int j = 0; j < item.length(); j++)
107             {
108                 if (!jalview.util.Comparison.isGap(item.charAt(j)))
109                 {
110                     noGapsSB.append(item.charAt(j));
111                     spaces.add(new Integer(insertCount));
112                 }
113                 else
114                 {
115                     insertCount++;
116                 }
117             }
118
119             String noGaps = noGapsSB.toString();
120
121             for (int r = resIndex; r < noGaps.length(); r++)
122             {
123
124                 if (regex.searchFrom(noGaps, r))
125                 {
126                     resIndex = regex.matchedFrom();
127
128                     if ((selection != null) &&
129                             ((resIndex +
130                             Integer.parseInt(spaces.get(resIndex).toString())) < selection.getStartRes()))
131                     {
132                         continue;
133                     }
134
135
136                     int sres = seq.findPosition(resIndex +
137                             Integer.parseInt(spaces.elementAt(resIndex)
138                                                    .toString()));
139                     int eres = seq.findPosition(regex.matchedTo() - 1 +
140                             Integer.parseInt(spaces.elementAt(regex.matchedTo() -
141                                     1).toString()));
142
143                     searchResults.addResult(seq, sres, eres);
144                     hasResults=true;
145                     if (!findAll)
146                     {
147                         // thats enough, break and display the result
148                         found = true;
149                         resIndex++;
150
151                         break;
152                     }
153
154                     r = resIndex;
155                 }
156                 else
157                 {
158                   break;
159                 }
160             }
161
162             if (!found)
163             {
164                 seqIndex++;
165                 resIndex = 0;
166             }
167         }
168
169         for (int id = 0; id < alignment.getHeight(); id++)
170         {
171             if (regex.search(alignment.getSequenceAt(id).getName()))
172             {
173                 idMatch.add(alignment.getSequenceAt(id));
174                 hasResults=true;
175             }
176         }
177         return hasResults;
178     }
179     /**
180      * @return the alignment
181      */
182     public AlignmentI getAlignment() {
183         return alignment;
184     }
185     /**
186      * @param alignment the alignment to set
187      */
188     public void setAlignment(AlignmentI alignment) {
189         this.alignment = alignment;
190     }
191     /**
192      * @return the caseSensitive
193      */
194     public boolean isCaseSensitive() {
195         return caseSensitive;
196     }
197     /**
198      * @param caseSensitive the caseSensitive to set
199      */
200     public void setCaseSensitive(boolean caseSensitive) {
201         this.caseSensitive = caseSensitive;
202     }
203     /**
204      * @return the findAll
205      */
206     public boolean isFindAll() {
207         return findAll;
208     }
209     /**
210      * @param findAll the findAll to set
211      */
212     public void setFindAll(boolean findAll) {
213         this.findAll = findAll;
214     }
215     /**
216      * @return the selection
217      */
218     public jalview.datamodel.SequenceGroup getSelection() {
219         return selection;
220     }
221     /**
222      * @param selection the selection to set
223      */
224     public void setSelection(jalview.datamodel.SequenceGroup selection) {
225         this.selection = selection;
226     }
227     /**
228      * @return the idMatch
229      */
230     public Vector getIdMatch() {
231         return idMatch;
232     }
233     /**
234      * @return the regex
235      */
236     public com.stevesoft.pat.Regex getRegex() {
237         return regex;
238     }
239     /**
240      * @return the searchResults
241      */
242     public SearchResults getSearchResults() {
243         return searchResults;
244     }
245
246     /**
247      * @return the resIndex
248      */
249     public int getResIndex() {
250         return resIndex;
251     }
252
253     /**
254      * @param resIndex the resIndex to set
255      */
256     public void setResIndex(int resIndex) {
257         this.resIndex = resIndex;
258     }
259
260     /**
261      * @return the seqIndex
262      */
263     public int getSeqIndex() {
264         return seqIndex;
265     }
266
267     /**
268      * @param seqIndex the seqIndex to set
269      */
270     public void setSeqIndex(int seqIndex) {
271         this.seqIndex = seqIndex;
272     }
273 }