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 () {
7 Clazz.instantialize (this, arguments);
8 }, jalview.io, "TCoffeeScoreFile", jalview.io.AlignFile);
9 Clazz.defineMethod (c$, "getHeight",
11 return this.scores != null && this.scores.size () > 0 ? this.scores.size () - 1 : 0;
13 Clazz.defineMethod (c$, "getWidth",
15 return this.fWidth != null ? this.fWidth : 0;
17 Clazz.defineMethod (c$, "getScoresFor",
19 return this.scores != null && this.scores.containsKey (id) ? this.scores.get (id).toString () : "";
21 Clazz.defineMethod (c$, "getScoresList",
23 if (this.scores == 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 ());
31 Clazz.defineMethod (c$, "getScoresArray",
33 if (this.scores == null) {
35 }var result = Clazz.newByteArray (this.scores.size (), 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;
44 result[rowCount++] = seqValues;
48 Clazz.overrideMethod (c$, "parse",
50 this.header = jalview.io.TCoffeeScoreFile.readHeader (this);
51 if (this.header == null) {
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 ());
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) {
64 this.errormessage = String.format ("Invalid T-Coffee score file: Sequence ID '%s' is not declared in header section", [entry.getKey ()]);
66 }scoreStringBuilder.append (entry.getValue ());
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 ()) {
74 this.errormessage = "Invalid T-Coffee score file: All the score sequences must have the same length";
79 c$.parseInt = Clazz.defineMethod (c$, "parseInt",
82 return Integer.parseInt (str);
84 if (Clazz.exceptionOf (e, NumberFormatException)) {
91 c$.readHeader = Clazz.defineMethod (c$, "readHeader",
95 result = new jalview.io.TCoffeeScoreFile.Header ();
96 result.head = reader.nextLine ();
98 while ((line = reader.nextLine ()) != null) {
99 if (line.startsWith ("SCORE=")) {
100 result.score = jalview.io.TCoffeeScoreFile.parseInt (line.substring (6).trim ());
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)");
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)");
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)");
112 }while ((line = reader.nextLine ()) != null) {
113 if ("".equals (line)) {
115 }var p = line.indexOf (":");
118 }var id = line.substring (0, p).trim ();
119 var val = jalview.io.TCoffeeScoreFile.parseInt (line.substring (p + 1).trim ());
120 if ("".equals (id)) {
122 }result.scores.put (id, new Integer (val));
124 if (result == null) {
125 jalview.io.TCoffeeScoreFile.error (reader, "T-COFFEE score file had no per-sequence scores");
127 if (Clazz.exceptionOf (e, java.io.IOException)) {
128 jalview.io.TCoffeeScoreFile.error (reader, "Unexpected problem parsing T-Coffee score ascii file");
135 }, "jalview.io.FileParse");
136 c$.error = Clazz.defineMethod (c$, "error",
137 function (reader, errm) {
139 if (reader.errormessage == null) {
140 reader.errormessage = errm;
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);
148 while ((line = reader.nextLine ()) != null && "".equals (line.trim ())) {
153 if ("".equals (line.trim ())) {
155 }var p = line.indexOf (" ");
157 if (reader.warningMessage == null) {
158 reader.warningMessage = "";
159 }reader.warningMessage += "Possible parsing error - expected to find a space in line: '" + line + "'\n";
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);
166 }result.items.put (id, val);
167 } while ((line = reader.nextLine ()) != null);
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;
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];
184 s = sidmatcher.findIdMatch (id.getKey ());
186 s = al.getSequenceAt (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 ());
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++) {
195 if (s != null && jalview.util.Comparison.isGap (s.getCharAt (j))) {
196 annotations[j] = null;
198 System.err.println ("Warning: non-zero value for positional T-COFFEE score for gap at " + j + " in sequence " + s.getName ());
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);
202 var aa = al.findOrCreateAnnotation ("TCoffeeScore", "TCoffeeScore", false, s, null);
204 aa.label = "T-COFFEE";
205 aa.description = "" + id.getKey ();
206 aa.annotations = annotations;
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 ();
215 aa.label = "T-COFFEE";
216 aa.description = "TCoffee column reliability score";
217 aa.annotations = annotations;
218 aa.belowAlignment = true;
220 aa.setScore (this.header.getScoreAvg ());
221 }aa.showAllColLabels = true;
222 aa.validateRangeAndDisplay ();
226 }, "jalview.datamodel.AlignmentI,~B");
227 Clazz.overrideMethod (c$, "print",
232 c$ = Clazz.decorateAsClass (function () {
236 Clazz.instantialize (this, arguments);
237 }, jalview.io.TCoffeeScoreFile, "Header");
238 Clazz.prepareFields (c$, function () {
239 this.scores = new java.util.LinkedHashMap ();
241 Clazz.defineMethod (c$, "getScoreAvg",
245 Clazz.defineMethod (c$, "getScoreFor",
247 return this.scores.containsKey (a) ? this.scores.get (a) : -1;
251 c$ = Clazz.decorateAsClass (function () {
254 Clazz.instantialize (this, arguments);
255 }, jalview.io.TCoffeeScoreFile, "Block");
256 Clazz.makeConstructor (c$,
259 this.items = new java.util.HashMap (a);
261 Clazz.defineMethod (c$, "getScoresFor",
263 return this.items.get (a);
265 Clazz.defineMethod (c$, "getConsensus",
267 return this.items.get ("cons");
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");