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