JAL-1807 test
[jalviewjs.git] / bin / jalview / io / JPredFile.js
1 Clazz.declarePackage ("jalview.io");
2 Clazz.load (["jalview.io.AlignFile"], "jalview.io.JPredFile", ["jalview.datamodel.Alignment", "$.Sequence", "jalview.io.JnetAnnotationMaker", "jalview.util.MessageManager", "java.io.IOException", "java.lang.Float", "$.StringBuffer", "java.util.Hashtable", "$.StringTokenizer", "$.Vector"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.ids = null;
5 this.conf = null;
6 this.Scores = null;
7 this.Symscores = null;
8 this.QuerySeqPosition = 0;
9 this.annotSeqs = null;
10 Clazz.instantialize (this, arguments);
11 }, jalview.io, "JPredFile", jalview.io.AlignFile);
12 Clazz.defineMethod (c$, "setQuerySeqPosition", 
13 function (QuerySeqPosition) {
14 this.QuerySeqPosition = QuerySeqPosition;
15 }, "~N");
16 Clazz.defineMethod (c$, "getQuerySeqPosition", 
17 function () {
18 return this.QuerySeqPosition;
19 });
20 Clazz.defineMethod (c$, "getScores", 
21 function () {
22 return this.Scores;
23 });
24 Clazz.defineMethod (c$, "getSymscores", 
25 function () {
26 return this.Symscores;
27 });
28 Clazz.defineMethod (c$, "initData", 
29 function () {
30 Clazz.superCall (this, jalview.io.JPredFile, "initData", []);
31 this.Scores =  new java.util.Hashtable ();
32 this.ids = null;
33 this.conf = null;
34 });
35 Clazz.overrideMethod (c$, "parse", 
36 function () {
37 var line;
38 this.QuerySeqPosition = -1;
39 this.noSeqs = 0;
40 var seq_entries =  new java.util.Vector ();
41 var ids =  new java.util.Vector ();
42 var Symscores =  new java.util.Hashtable ();
43 while ((line = this.nextLine ()) != null) {
44 var str =  new java.util.StringTokenizer (line, ":");
45 var id = "";
46 if (!str.hasMoreTokens ()) {
47 continue;
48 }id = str.nextToken ();
49 var seqsym = str.nextToken ();
50 var symbols =  new java.util.StringTokenizer (seqsym, ",");
51 var numSymbols = symbols.countTokens ();
52 if (numSymbols == 0) {
53 continue;
54 }if (seqsym.length != (2 * numSymbols)) {
55 if (this.Scores.containsKey (id)) {
56 var i = 1;
57 while (this.Scores.containsKey (id + "_" + i)) {
58 i++;
59 }
60 id = id + "_" + i;
61 }var scores =  new java.util.Vector ();
62 var i = 0;
63 var ascore = "dead";
64 try {
65 while (symbols.hasMoreTokens ()) {
66 ascore = symbols.nextToken ();
67 var score =  new Float (ascore);
68 scores.addElement (score);
69 }
70 this.Scores.put (id, scores);
71 } catch (e) {
72 if (Clazz.exceptionOf (e, Exception)) {
73 i = scores.size ();
74 for (var j = 0; j < i; j++) {
75 scores.setElementAt ((scores.elementAt (j)).toString (), j);
76 }
77 scores.addElement (ascore);
78 while (symbols.hasMoreTokens ()) {
79 ascore = symbols.nextToken ();
80 scores.addElement (ascore);
81 }
82 this.Scores.put (id, scores);
83 } else {
84 throw e;
85 }
86 }
87 } else if (id.equals ("jnetconf")) {
88 id = "Prediction Confidence";
89 this.conf =  new java.util.Vector (numSymbols);
90 for (var i = 0; i < numSymbols; i++) {
91 this.conf.setElementAt (symbols.nextToken (), i);
92 }
93 } else {
94 var newseq =  new StringBuffer ();
95 for (var i = 0; i < numSymbols; i++) {
96 newseq.append (symbols.nextToken ());
97 }
98 if (id.indexOf (";") > -1) {
99 seq_entries.addElement (newseq);
100 var i = 1;
101 var name = id.substring (id.indexOf (";") + 1);
102 while (ids.lastIndexOf (name) > -1) {
103 name = id.substring (id.indexOf (";") + 1) + "_" + ++i;
104 }
105 if (this.QuerySeqPosition == -1) this.QuerySeqPosition = ids.size ();
106 ids.addElement (name);
107 this.noSeqs++;
108 } else {
109 if (id.equals ("JNETPRED")) {
110 id = "Predicted Secondary Structure";
111 }seq_entries.addElement (newseq.toString ());
112 ids.addElement (id);
113 Symscores.put (id,  new Integer (ids.size () - 1));
114 }}}
115 this.maxLength = seq_entries.elementAt (0).toString ().length;
116 for (var i = 0; i < ids.size (); i++) {
117 var newSeq =  new jalview.datamodel.Sequence (ids.elementAt (i).toString (), seq_entries.elementAt (i).toString (), 1, seq_entries.elementAt (i).toString ().length);
118 if (this.maxLength != seq_entries.elementAt (i).toString ().length) {
119 throw  new java.io.IOException (jalview.util.MessageManager.formatMessage ("exception.jpredconcide_entry_has_unexpected_number_of_columns",  Clazz.newArray (-1, [ids.elementAt (i).toString ()])));
120 }if ((newSeq.getName ().startsWith ("QUERY") || newSeq.getName ().startsWith ("align;")) && (this.QuerySeqPosition == -1)) {
121 this.QuerySeqPosition = this.seqs.size ();
122 }this.seqs.addElement (newSeq);
123 }
124 if (this.seqs.size () > 0 && this.QuerySeqPosition > -1) {
125 var tal =  new jalview.datamodel.Alignment (this.getSeqsAsArray ());
126 try {
127 jalview.io.JnetAnnotationMaker.add_annotation (this, tal, this.QuerySeqPosition, true);
128 } catch (e) {
129 if (Clazz.exceptionOf (e, Exception)) {
130 tal = null;
131 var ex =  new java.io.IOException (jalview.util.MessageManager.formatMessage ("exception.couldnt_parse_concise_annotation_for_prediction",  Clazz.newArray (-1, [e.getMessage ()])));
132 e.printStackTrace ();
133 throw ex;
134 } else {
135 throw e;
136 }
137 }
138 this.annotations =  new java.util.Vector ();
139 var aan = tal.getAlignmentAnnotation ();
140 for (var aai = 0; aan != null && aai < aan.length; aai++) {
141 this.annotations.addElement (aan[aai]);
142 }
143 }});
144 Clazz.overrideMethod (c$, "print", 
145 function () {
146 return "Not Supported";
147 });
148 c$.main = Clazz.defineMethod (c$, "main", 
149 function (args) {
150 try {
151 var blc =  new jalview.io.JPredFile (args[0], "File");
152 for (var i = 0; i < blc.seqs.size (); i++) {
153 System.out.println ((blc.seqs.elementAt (i)).getName () + "\n" + (blc.seqs.elementAt (i)).getSequenceAsString () + "\n");
154 }
155 } catch (e) {
156 if (Clazz.exceptionOf (e, java.io.IOException)) {
157 System.err.println ("Exception " + e);
158 } else {
159 throw e;
160 }
161 }
162 }, "~A");
163 Clazz.defineMethod (c$, "removeNonSequences", 
164 function () {
165 if (this.annotSeqs != null) {
166 return;
167 }this.annotSeqs =  new java.util.Vector ();
168 var newseqs =  new java.util.Vector ();
169 var i = 0;
170 var j = this.seqs.size ();
171 for (; i < this.QuerySeqPosition; i++) {
172 this.annotSeqs.addElement (this.seqs.elementAt (i));
173 }
174 {
175 var sq = this.seqs.elementAt (j - 1);
176 if (sq.getName ().toUpperCase ().startsWith ("JPRED")) {
177 this.annotSeqs.addElement (sq);
178 this.seqs.removeElementAt (--j);
179 }}for (; i < j; i++) {
180 newseqs.addElement (this.seqs.elementAt (i));
181 }
182 this.seqs.removeAllElements ();
183 this.seqs = newseqs;
184 });
185 });