Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / jalview / io / TCoffeeScoreFile.js
1 Clazz.declarePackage ("jalview.io");
2 Clazz.load (["jalview.io.AlignFile", "java.awt.Color", "java.util.LinkedHashMap", "java.util.regex.Pattern"], "jalview.io.TCoffeeScoreFile", ["jalview.analysis.SequenceIdMatcher", "jalview.datamodel.Annotation", "jalview.util.Comparison", "java.lang.StringBuilder", "java.util.ArrayList", "$.HashMap"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.header = null;
5 this.scores = null;
6 this.fWidth = null;
7 Clazz.instantialize (this, arguments);
8 }, jalview.io, "TCoffeeScoreFile", jalview.io.AlignFile);
9 Clazz.defineMethod (c$, "getHeight", 
10 function () {
11 return this.scores != null && this.scores.size () > 0 ? this.scores.size () - 1 : 0;
12 });
13 Clazz.defineMethod (c$, "getWidth", 
14 function () {
15 return this.fWidth != null ? this.fWidth : 0;
16 });
17 Clazz.defineMethod (c$, "getScoresFor", 
18 function (id) {
19 return this.scores != null && this.scores.containsKey (id) ? this.scores.get (id).toString () : "";
20 }, "~S");
21 Clazz.defineMethod (c$, "getScoresList", 
22 function () {
23 if (this.scores == null) {
24 return null;
25 }var result =  new java.util.ArrayList (this.scores.size ());
26 for (var it, $it = this.scores.entrySet ().iterator (); $it.hasNext () && ((it = $it.next ()) || true);) {
27 result.add (it.getValue ().toString ());
28 }
29 return result;
30 });
31 Clazz.defineMethod (c$, "getScoresArray", 
32 function () {
33 if (this.scores == null) {
34 return null;
35 }var result =  Clazz.newByteArray (this.scores.size (), 0);
36 var rowCount = 0;
37 for (var it, $it = this.scores.entrySet ().iterator (); $it.hasNext () && ((it = $it.next ()) || true);) {
38 var line = it.getValue ().toString ();
39 var seqValues =  Clazz.newByteArray (line.length, 0);
40 for (var j = 0, c = line.length; j < c; j++) {
41 var val = (line.charCodeAt (j) - 48);
42 seqValues[j] = (val >= 0 && val <= 9) ? val : -1;
43 }
44 result[rowCount++] = seqValues;
45 }
46 return result;
47 });
48 Clazz.overrideMethod (c$, "parse", 
49 function () {
50 this.header = jalview.io.TCoffeeScoreFile.readHeader (this);
51 if (this.header == null) {
52 this.error = true;
53 return;
54 }this.scores =  new java.util.LinkedHashMap ();
55 for (var entry, $entry = this.header.scores.entrySet ().iterator (); $entry.hasNext () && ((entry = $entry.next ()) || true);) {
56 this.scores.put (entry.getKey (),  new StringBuilder ());
57 }
58 var block;
59 while ((block = jalview.io.TCoffeeScoreFile.readBlock (this, this.header.scores.size ())) != null) {
60 for (var entry, $entry = block.items.entrySet ().iterator (); $entry.hasNext () && ((entry = $entry.next ()) || true);) {
61 var scoreStringBuilder = this.scores.get (entry.getKey ());
62 if (scoreStringBuilder == null) {
63 this.error = true;
64 this.errormessage = String.format ("Invalid T-Coffee score file: Sequence ID '%s' is not declared in header section", [entry.getKey ()]);
65 return;
66 }scoreStringBuilder.append (entry.getValue ());
67 }
68 }
69 for (var str, $str = this.scores.values ().iterator (); $str.hasNext () && ((str = $str.next ()) || true);) {
70 if (this.fWidth == null) {
71 this.fWidth = new Integer (str.length ());
72 } else if ((this.fWidth).intValue () !== str.length ()) {
73 this.error = true;
74 this.errormessage = "Invalid T-Coffee score file: All the score sequences must have the same length";
75 return;
76 }}
77 return;
78 });
79 c$.parseInt = Clazz.defineMethod (c$, "parseInt", 
80 function (str) {
81 try {
82 return Integer.parseInt (str);
83 } catch (e) {
84 if (Clazz.exceptionOf (e, NumberFormatException)) {
85 return 0;
86 } else {
87 throw e;
88 }
89 }
90 }, "~S");
91 c$.readHeader = Clazz.defineMethod (c$, "readHeader", 
92 function (reader) {
93 var result = null;
94 try {
95 result =  new jalview.io.TCoffeeScoreFile.Header ();
96 result.head = reader.nextLine ();
97 var line;
98 while ((line = reader.nextLine ()) != null) {
99 if (line.startsWith ("SCORE=")) {
100 result.score = jalview.io.TCoffeeScoreFile.parseInt (line.substring (6).trim ());
101 break;
102 }}
103 if ((line = reader.nextLine ()) == null || !"*".equals (line.trim ())) {
104 jalview.io.TCoffeeScoreFile.error (reader, "Invalid T-COFFEE score format (NO BAD/AVG/GOOD header)");
105 return null;
106 }if ((line = reader.nextLine ()) == null || !"BAD AVG GOOD".equals (line.trim ())) {
107 jalview.io.TCoffeeScoreFile.error (reader, "Invalid T-COFFEE score format (NO BAD/AVG/GOOD header)");
108 return null;
109 }if ((line = reader.nextLine ()) == null || !"*".equals (line.trim ())) {
110 jalview.io.TCoffeeScoreFile.error (reader, "Invalid T-COFFEE score format (NO BAD/AVG/GOOD header)");
111 return null;
112 }while ((line = reader.nextLine ()) != null) {
113 if ("".equals (line)) {
114 break;
115 }var p = line.indexOf (":");
116 if (p == -1) {
117 continue;
118 }var id = line.substring (0, p).trim ();
119 var val = jalview.io.TCoffeeScoreFile.parseInt (line.substring (p + 1).trim ());
120 if ("".equals (id)) {
121 continue;
122 }result.scores.put (id, new Integer (val));
123 }
124 if (result == null) {
125 jalview.io.TCoffeeScoreFile.error (reader, "T-COFFEE score file had no per-sequence scores");
126 }} catch (e) {
127 if (Clazz.exceptionOf (e, java.io.IOException)) {
128 jalview.io.TCoffeeScoreFile.error (reader, "Unexpected problem parsing T-Coffee score ascii file");
129 throw e;
130 } else {
131 throw e;
132 }
133 }
134 return result;
135 }, "jalview.io.FileParse");
136 c$.error = Clazz.defineMethod (c$, "error", 
137  function (reader, errm) {
138 reader.error = true;
139 if (reader.errormessage == null) {
140 reader.errormessage = errm;
141 } else {
142 reader.errormessage += "\n" + errm;
143 }}, "jalview.io.FileParse,~S");
144 c$.readBlock = Clazz.defineMethod (c$, "readBlock", 
145 function (reader, size) {
146 var result =  new jalview.io.TCoffeeScoreFile.Block (size);
147 var line;
148 while ((line = reader.nextLine ()) != null && "".equals (line.trim ())) {
149 }
150 if (line == null) {
151 return null;
152 }do {
153 if ("".equals (line.trim ())) {
154 break;
155 }var p = line.indexOf (" ");
156 if (p == -1) {
157 if (reader.warningMessage == null) {
158 reader.warningMessage = "";
159 }reader.warningMessage += "Possible parsing error - expected to find a space in line: '" + line + "'\n";
160 continue;
161 }var id = line.substring (0, p).trim ();
162 var val = line.substring (p + 1).trim ();
163 var m = jalview.io.TCoffeeScoreFile.SCORES_WITH_RESIDUE_NUMS.matcher (val);
164 if (m.matches ()) {
165 val = m.group (1);
166 }result.items.put (id, val);
167 } while ((line = reader.nextLine ()) != null);
168 return result;
169 }, "jalview.io.FileParse,~N");
170 Clazz.defineMethod (c$, "annotateAlignment", 
171 function (al, matchids) {
172 if (al.getHeight () != this.getHeight () || al.getWidth () != this.getWidth ()) {
173 var info = String.format ("align w: %s, h: %s; score: w: %s; h: %s ", [new Integer (al.getWidth ()), new Integer (al.getHeight ()), new Integer (this.getWidth ()), new Integer (this.getHeight ())]);
174 this.warningMessage = "Alignment shape does not match T-Coffee score file shape -- " + info;
175 return false;
176 }var added = false;
177 var i = 0;
178 var sidmatcher =  new jalview.analysis.SequenceIdMatcher (al.getSequencesArray ());
179 var scoreMatrix = this.getScoresArray ();
180 for (var id, $id = this.scores.entrySet ().iterator (); $id.hasNext () && ((id = $id.next ()) || true);) {
181 var srow = scoreMatrix[i];
182 var s;
183 if (matchids) {
184 s = sidmatcher.findIdMatch (id.getKey ());
185 } else {
186 s = al.getSequenceAt (i);
187 }i++;
188 if (s == null && i != this.scores.size () && !id.getKey ().equals ("cons")) {
189 System.err.println ("No " + (matchids ? "match " : " sequences left ") + " for TCoffee score set : " + id.getKey ());
190 continue;
191 }var jSize = al.getWidth () < srow.length ? al.getWidth () : srow.length;
192 var annotations =  new Array (al.getWidth ());
193 for (var j = 0; j < jSize; j++) {
194 var val = srow[j];
195 if (s != null && jalview.util.Comparison.isGap (s.getCharAt (j))) {
196 annotations[j] = null;
197 if (val > 0) {
198 System.err.println ("Warning: non-zero value for positional T-COFFEE score for gap at " + j + " in sequence " + s.getName ());
199 }} else {
200 annotations[j] =  new jalview.datamodel.Annotation (s == null ? "" + val : null, s == null ? "" + val : null, '\0', val * 1, val >= 0 && val < jalview.io.TCoffeeScoreFile.colors.length ? jalview.io.TCoffeeScoreFile.colors[val] : java.awt.Color.white);
201 }}
202 var aa = al.findOrCreateAnnotation ("TCoffeeScore", "TCoffeeScore", false, s, null);
203 if (s != null) {
204 aa.label = "T-COFFEE";
205 aa.description = "" + id.getKey ();
206 aa.annotations = annotations;
207 aa.visible = false;
208 aa.belowAlignment = false;
209 aa.setScore (this.header.getScoreFor (id.getKey ()));
210 aa.createSequenceMapping (s, s.getStart (), true);
211 s.addAlignmentAnnotation (aa);
212 aa.adjustForAlignment ();
213 } else {
214 aa.graph = 0;
215 aa.label = "T-COFFEE";
216 aa.description = "TCoffee column reliability score";
217 aa.annotations = annotations;
218 aa.belowAlignment = true;
219 aa.visible = true;
220 aa.setScore (this.header.getScoreAvg ());
221 }aa.showAllColLabels = true;
222 aa.validateRangeAndDisplay ();
223 added = true;
224 }
225 return added;
226 }, "jalview.datamodel.AlignmentI,~B");
227 Clazz.overrideMethod (c$, "print", 
228 function () {
229 return "Not valid.";
230 });
231 Clazz.pu$h(self.c$);
232 c$ = Clazz.decorateAsClass (function () {
233 this.head = null;
234 this.score = 0;
235 this.scores = null;
236 Clazz.instantialize (this, arguments);
237 }, jalview.io.TCoffeeScoreFile, "Header");
238 Clazz.prepareFields (c$, function () {
239 this.scores =  new java.util.LinkedHashMap ();
240 });
241 Clazz.defineMethod (c$, "getScoreAvg", 
242 function () {
243 return this.score;
244 });
245 Clazz.defineMethod (c$, "getScoreFor", 
246 function (a) {
247 return this.scores.containsKey (a) ? this.scores.get (a) : -1;
248 }, "~S");
249 c$ = Clazz.p0p ();
250 Clazz.pu$h(self.c$);
251 c$ = Clazz.decorateAsClass (function () {
252 this.size = 0;
253 this.items = null;
254 Clazz.instantialize (this, arguments);
255 }, jalview.io.TCoffeeScoreFile, "Block");
256 Clazz.makeConstructor (c$, 
257 function (a) {
258 this.size = a;
259 this.items =  new java.util.HashMap (a);
260 }, "~N");
261 Clazz.defineMethod (c$, "getScoresFor", 
262 function (a) {
263 return this.items.get (a);
264 }, "~S");
265 Clazz.defineMethod (c$, "getConsensus", 
266 function () {
267 return this.items.get ("cons");
268 });
269 c$ = Clazz.p0p ();
270 c$.SCORES_WITH_RESIDUE_NUMS = c$.prototype.SCORES_WITH_RESIDUE_NUMS = java.util.regex.Pattern.compile ("^\\d+\\s([^\\s]+)\\s+\\d+$");
271 c$.colors = c$.prototype.colors =  Clazz.newArray (-1, [ new java.awt.Color (102, 102, 255),  new java.awt.Color (0, 255, 0),  new java.awt.Color (102, 255, 0),  new java.awt.Color (204, 255, 0),  new java.awt.Color (255, 255, 0),  new java.awt.Color (255, 204, 0),  new java.awt.Color (255, 153, 0),  new java.awt.Color (255, 102, 0),  new java.awt.Color (255, 51, 0),  new java.awt.Color (255, 34, 0)]);
272 Clazz.defineStatics (c$,
273 "TCOFFEE_SCORE", "TCoffeeScore");
274 });