JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / site / j2s / jalview / analysis / Finder.js
1 Clazz.declarePackage ("jalview.analysis");\r
2 Clazz.load (null, "jalview.analysis.Finder", ["jalview.datamodel.SearchResults", "jalview.jsdev.RegExp", "jalview.util.Comparison", "java.lang.StringBuffer", "java.util.Vector"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.searchResults = null;\r
5 this.alignment = null;\r
6 this.selection = null;\r
7 this.idMatch = null;\r
8 this.caseSensitive = false;\r
9 this.includeDescription = false;\r
10 this.findAll = false;\r
11 this.regex = null;\r
12 this.seqIndex = 0;\r
13 this.resIndex = -1;\r
14 Clazz.instantialize (this, arguments);\r
15 }, jalview.analysis, "Finder");\r
16 Clazz.makeConstructor (c$, \r
17 function (alignment, selection) {\r
18 this.alignment = alignment;\r
19 this.selection = selection;\r
20 }, "jalview.datamodel.AlignmentI,jalview.datamodel.SequenceGroup");\r
21 Clazz.makeConstructor (c$, \r
22 function (alignment, selectionGroup, seqIndex, resIndex) {\r
23 this.construct (alignment, selectionGroup);\r
24 this.seqIndex = seqIndex;\r
25 this.resIndex = resIndex;\r
26 }, "jalview.datamodel.AlignmentI,jalview.datamodel.SequenceGroup,~N,~N");\r
27 Clazz.defineMethod (c$, "find", \r
28 function (searchString) {\r
29 var hasResults = false;\r
30 if (!this.caseSensitive) {\r
31 searchString = searchString.toUpperCase ();\r
32 }this.regex = jalview.jsdev.RegExp.newRegex ([searchString]);\r
33 this.regex.setIgnoreCase (!this.caseSensitive);\r
34 this.searchResults =  new jalview.datamodel.SearchResults ();\r
35 this.idMatch =  new java.util.Vector ();\r
36 var seq;\r
37 var item = null;\r
38 var found = false;\r
39 var end = this.alignment.getHeight ();\r
40 if (this.selection != null) {\r
41 if ((this.selection.getSize () < 1) || ((this.selection.getEndRes () - this.selection.getStartRes ()) < 2)) {\r
42 this.selection = null;\r
43 }}while (!found && (this.seqIndex < end)) {\r
44 seq = this.alignment.getSequenceAt (this.seqIndex);\r
45 if ((this.selection != null && this.selection.getSize () > 0) && !this.selection.getSequences (null).contains (seq)) {\r
46 this.seqIndex++;\r
47 this.resIndex = -1;\r
48 continue;\r
49 }if (this.resIndex < 0) {\r
50 this.resIndex = 0;\r
51 try {\r
52 var res = Integer.parseInt (searchString);\r
53 if (seq.getEnd () >= res) {\r
54 this.searchResults.addResult (seq, res, res);\r
55 hasResults = true;\r
56 if (!this.findAll) {\r
57 found = true;\r
58 break;\r
59 }}} catch (ex) {\r
60 if (Clazz.exceptionOf (ex, NumberFormatException)) {\r
61 } else {\r
62 throw ex;\r
63 }\r
64 }\r
65 if (this.regex.search (seq.getName ())) {\r
66 this.idMatch.addElement (seq);\r
67 hasResults = true;\r
68 if (!this.findAll) {\r
69 found = true;\r
70 break;\r
71 }}if (this.isIncludeDescription () && seq.getDescription () != null && this.regex.search (seq.getDescription ())) {\r
72 this.idMatch.addElement (seq);\r
73 hasResults = true;\r
74 if (!this.findAll) {\r
75 found = true;\r
76 break;\r
77 }}}item = seq.getSequenceAsString ();\r
78 if ((this.selection != null) && (this.selection.getEndRes () < this.alignment.getWidth () - 1)) {\r
79 item = item.substring (0, this.selection.getEndRes () + 1);\r
80 }var noGapsSB =  new StringBuffer ();\r
81 var insertCount = 0;\r
82 var spaces =  new java.util.Vector ();\r
83 for (var j = 0; j < item.length; j++) {\r
84 if (!jalview.util.Comparison.isGap (item.charAt (j))) {\r
85 noGapsSB.append (item.charAt (j));\r
86 spaces.addElement ( new Integer (insertCount));\r
87 } else {\r
88 insertCount++;\r
89 }}\r
90 var noGaps = noGapsSB.toString ();\r
91 for (var r = this.resIndex; r < noGaps.length; r++) {\r
92 if (this.regex.searchFrom (noGaps, r)) {\r
93 this.resIndex = this.regex.matchedFrom ();\r
94 if ((this.selection != null && this.selection.getSize () > 0) && ((this.resIndex + Integer.parseInt (spaces.elementAt (this.resIndex).toString ())) < this.selection.getStartRes ())) {\r
95 continue;\r
96 }var sres = seq.findPosition (this.resIndex + Integer.parseInt (spaces.elementAt (this.resIndex).toString ()));\r
97 var eres = seq.findPosition (this.regex.matchedTo () - 1 + Integer.parseInt (spaces.elementAt (this.regex.matchedTo () - 1).toString ()));\r
98 this.searchResults.addResult (seq, sres, eres);\r
99 hasResults = true;\r
100 if (!this.findAll) {\r
101 found = true;\r
102 this.resIndex++;\r
103 break;\r
104 }r = this.resIndex;\r
105 } else {\r
106 break;\r
107 }}\r
108 if (!found) {\r
109 this.seqIndex++;\r
110 this.resIndex = -1;\r
111 }}\r
112 return hasResults;\r
113 }, "~S");\r
114 Clazz.defineMethod (c$, "getAlignment", \r
115 function () {\r
116 return this.alignment;\r
117 });\r
118 Clazz.defineMethod (c$, "setAlignment", \r
119 function (alignment) {\r
120 this.alignment = alignment;\r
121 }, "jalview.datamodel.AlignmentI");\r
122 Clazz.defineMethod (c$, "isCaseSensitive", \r
123 function () {\r
124 return this.caseSensitive;\r
125 });\r
126 Clazz.defineMethod (c$, "setCaseSensitive", \r
127 function (caseSensitive) {\r
128 this.caseSensitive = caseSensitive;\r
129 }, "~B");\r
130 Clazz.defineMethod (c$, "isFindAll", \r
131 function () {\r
132 return this.findAll;\r
133 });\r
134 Clazz.defineMethod (c$, "setFindAll", \r
135 function (findAll) {\r
136 this.findAll = findAll;\r
137 }, "~B");\r
138 Clazz.defineMethod (c$, "getSelection", \r
139 function () {\r
140 return this.selection;\r
141 });\r
142 Clazz.defineMethod (c$, "setSelection", \r
143 function (selection) {\r
144 this.selection = selection;\r
145 }, "jalview.datamodel.SequenceGroup");\r
146 Clazz.defineMethod (c$, "getIdMatch", \r
147 function () {\r
148 return this.idMatch;\r
149 });\r
150 Clazz.defineMethod (c$, "getRegex", \r
151 function () {\r
152 return this.regex;\r
153 });\r
154 Clazz.defineMethod (c$, "getSearchResults", \r
155 function () {\r
156 return this.searchResults;\r
157 });\r
158 Clazz.defineMethod (c$, "getResIndex", \r
159 function () {\r
160 return this.resIndex;\r
161 });\r
162 Clazz.defineMethod (c$, "setResIndex", \r
163 function (resIndex) {\r
164 this.resIndex = resIndex;\r
165 }, "~N");\r
166 Clazz.defineMethod (c$, "getSeqIndex", \r
167 function () {\r
168 return this.seqIndex;\r
169 });\r
170 Clazz.defineMethod (c$, "setSeqIndex", \r
171 function (seqIndex) {\r
172 this.seqIndex = seqIndex;\r
173 }, "~N");\r
174 Clazz.defineMethod (c$, "isIncludeDescription", \r
175 function () {\r
176 return this.includeDescription;\r
177 });\r
178 Clazz.defineMethod (c$, "setIncludeDescription", \r
179 function (includeDescription) {\r
180 this.includeDescription = includeDescription;\r
181 }, "~B");\r
182 });\r