JAL-1807 includes ?j2sdebug flag and DebugJS._(msg)
[jalviewjs.git] / bin / MCview / PDBfile.js
1 Clazz.declarePackage ("MCview");
2 Clazz.load (["jalview.io.AlignFile"], "MCview.PDBfile", ["MCview.Atom", "$.PDBChain", "jalview.analysis.AlignSeq", "jalview.datamodel.Alignment", "$.AlignmentI", "$.PDBEntry", "jalview.io.FileParse", "jalview.util.MessageManager", "java.awt.Color", "java.io.IOException", "java.util.ArrayList", "$.Hashtable", "$.Vector"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.chains = null;
5 this.id = null;
6 this.visibleChainAnnotation = false;
7 this.$predictSecondaryStructure = true;
8 this.externalSecondaryStructure = false;
9 Clazz.instantialize (this, arguments);
10 }, MCview, "PDBfile", jalview.io.AlignFile);
11 Clazz.makeConstructor (c$, 
12 function (addAlignmentAnnotations, predictSecondaryStructure, externalSecStr) {
13 Clazz.superConstructor (this, MCview.PDBfile);
14 this.visibleChainAnnotation = addAlignmentAnnotations;
15 this.$predictSecondaryStructure = predictSecondaryStructure;
16 this.externalSecondaryStructure = externalSecStr;
17 }, "~B,~B,~B");
18 Clazz.makeConstructor (c$, 
19 function (addAlignmentAnnotations, predictSecondaryStructure, externalSecStr, file, protocol) {
20 Clazz.superConstructor (this, MCview.PDBfile, [false, file, protocol]);
21 this.visibleChainAnnotation = addAlignmentAnnotations;
22 this.$predictSecondaryStructure = predictSecondaryStructure;
23 this.externalSecondaryStructure = externalSecStr;
24 this.doParse ();
25 }, "~B,~B,~B,~S,~S");
26 Clazz.makeConstructor (c$, 
27 function (addAlignmentAnnotations, predictSecondaryStructure, externalSecStr, source) {
28 Clazz.superConstructor (this, MCview.PDBfile, [false, source]);
29 this.visibleChainAnnotation = addAlignmentAnnotations;
30 this.$predictSecondaryStructure = predictSecondaryStructure;
31 this.externalSecondaryStructure = externalSecStr;
32 this.doParse ();
33 }, "~B,~B,~B,jalview.io.FileParse");
34 Clazz.overrideMethod (c$, "print", 
35 function () {
36 return null;
37 });
38 Clazz.overrideMethod (c$, "parse", 
39 function () {
40 this.id = this.safeName (this.getDataName ());
41 this.chains =  new java.util.Vector ();
42 var rna =  new java.util.ArrayList ();
43 var prot =  new java.util.ArrayList ();
44 var tmpchain;
45 var line = null;
46 var modelFlag = false;
47 var terFlag = false;
48 var lastID = "";
49 var indexx = 0;
50 var atomnam = null;
51 try {
52 while ((line = this.nextLine ()) != null) {
53 if (line.indexOf ("HEADER") == 0) {
54 if (line.length > 62) {
55 var tid;
56 if (line.length > 67) {
57 tid = line.substring (62, 67).trim ();
58 } else {
59 tid = line.substring (62).trim ();
60 }if (tid.length > 0) {
61 this.id = tid;
62 }continue;
63 }}if (line.indexOf ("SEQRES") == 0) {
64 }if (line.indexOf ("MODEL") == 0) {
65 modelFlag = true;
66 }if (line.indexOf ("TER") == 0) {
67 terFlag = true;
68 }if (modelFlag && line.indexOf ("ENDMDL") == 0) {
69 break;
70 }if (line.indexOf ("ATOM") == 0 || (line.indexOf ("HETATM") == 0 && !terFlag)) {
71 terFlag = false;
72 atomnam = line.substring (12, 15).trim ();
73 if (!atomnam.equals ("CA") && !atomnam.equals ("P")) {
74 continue;
75 }var tmpatom =  new MCview.Atom (line);
76 tmpchain = this.findChain (tmpatom.chain);
77 if (tmpchain != null) {
78 if (tmpatom.resNumIns.trim ().equals (lastID)) {
79 continue;
80 }tmpchain.atoms.addElement (tmpatom);
81 } else {
82 tmpchain =  new MCview.PDBChain (this.id, tmpatom.chain);
83 this.chains.addElement (tmpchain);
84 tmpchain.atoms.addElement (tmpatom);
85 }lastID = tmpatom.resNumIns.trim ();
86 }this.index++;
87 }
88 this.makeResidueList ();
89 this.makeCaBondList ();
90 if (this.id == null) {
91 this.id = this.inFile.getName ();
92 }for (var chain, $chain = this.chains.iterator (); $chain.hasNext () && ((chain = $chain.next ()) || true);) {
93 var chainseq = this.postProcessChain (chain);
94 if (MCview.PDBfile.isRNA (chainseq)) {
95 rna.add (chainseq);
96 } else {
97 prot.add (chainseq);
98 }}
99 if (this.$predictSecondaryStructure) {
100 this.predictSecondaryStructure (rna, prot);
101 }} catch (e$$) {
102 if (Clazz.exceptionOf (e$$, OutOfMemoryError)) {
103 var er = e$$;
104 {
105 System.out.println ("OUT OF MEMORY LOADING PDB FILE");
106 throw  new java.io.IOException (jalview.util.MessageManager.getString ("exception.outofmemory_loading_pdb_file"));
107 }
108 } else if (Clazz.exceptionOf (e$$, NumberFormatException)) {
109 var ex = e$$;
110 {
111 if (line != null) {
112 System.err.println ("Couldn't read number from line:");
113 System.err.println (line);
114 }}
115 } else {
116 throw e$$;
117 }
118 }
119 this.markCalcIds ();
120 });
121 Clazz.defineMethod (c$, "predictSecondaryStructure", 
122 function (rnaSequences, proteinSequences) {
123 if (this.externalSecondaryStructure && rnaSequences.size () > 0) {
124 try {
125 this.processPdbFileWithAnnotate3d (rnaSequences);
126 } catch (x) {
127 if (Clazz.exceptionOf (x, Exception)) {
128 System.err.println ("Exceptions when dealing with RNA in pdb file");
129 x.printStackTrace ();
130 } else {
131 throw x;
132 }
133 }
134 }if (proteinSequences.size () > 0) {
135 try {
136 this.processPdbFileWithJmol (proteinSequences);
137 } catch (x) {
138 if (Clazz.exceptionOf (x, Exception)) {
139 System.err.println ("Exceptions from Jmol when processing data in pdb file");
140 x.printStackTrace ();
141 } else {
142 throw x;
143 }
144 }
145 }}, "java.util.List,java.util.List");
146 Clazz.defineMethod (c$, "postProcessChain", 
147 function (chain) {
148 var dataset = chain.sequence;
149 dataset.setName (this.id + "|" + dataset.getName ());
150 var entry =  new jalview.datamodel.PDBEntry ();
151 entry.setId (this.id);
152 entry.setType (jalview.datamodel.PDBEntry.Type.PDB);
153 entry.setProperty ( new java.util.Hashtable ());
154 if (chain.id != null) {
155 entry.setChainCode (String.valueOf (chain.id));
156 }if (this.inFile != null) {
157 entry.setFile (this.inFile.getAbsolutePath ());
158 } else {
159 entry.setFile (this.getDataName ());
160 }dataset.addPDBId (entry);
161 var chainseq = dataset.deriveSequence ();
162 this.seqs.addElement (chainseq);
163 var chainannot = chainseq.getAnnotation ();
164 if (chainannot != null && this.visibleChainAnnotation) {
165 for (var ai = 0; ai < chainannot.length; ai++) {
166 chainannot[ai].visible = this.visibleChainAnnotation;
167 this.annotations.addElement (chainannot[ai]);
168 }
169 }return chainseq;
170 }, "MCview.PDBChain");
171 c$.isCalcIdHandled = Clazz.defineMethod (c$, "isCalcIdHandled", 
172 function (calcId) {
173 return calcId != null && (MCview.PDBfile.CALC_ID_PREFIX.equals (calcId));
174 }, "~S");
175 c$.isCalcIdForFile = Clazz.defineMethod (c$, "isCalcIdForFile", 
176 function (alan, pdbFile) {
177 return alan.getCalcId () != null && MCview.PDBfile.CALC_ID_PREFIX.equals (alan.getCalcId ()) && pdbFile.equals (alan.getProperty ("PDBID"));
178 }, "jalview.datamodel.AlignmentAnnotation,~S");
179 c$.relocateCalcId = Clazz.defineMethod (c$, "relocateCalcId", 
180 function (calcId, alreadyLoadedPDB) {
181 var s = MCview.PDBfile.CALC_ID_PREFIX.length;
182 var end = calcId.indexOf (MCview.PDBfile.CALC_ID_PREFIX, s);
183 var between = calcId.substring (s, end - 1);
184 return MCview.PDBfile.CALC_ID_PREFIX + alreadyLoadedPDB.get (between) + ":" + calcId.substring (end);
185 }, "~S,java.util.Hashtable");
186 Clazz.defineMethod (c$, "markCalcIds", 
187 ($fz = function () {
188 for (var sq, $sq = this.seqs.iterator (); $sq.hasNext () && ((sq = $sq.next ()) || true);) {
189 if (sq.getAnnotation () != null) {
190 for (var aa, $aa = 0, $$aa = sq.getAnnotation (); $aa < $$aa.length && ((aa = $$aa[$aa]) || true); $aa++) {
191 var oldId = aa.getCalcId ();
192 if (oldId == null) {
193 oldId = "";
194 }aa.setCalcId (MCview.PDBfile.CALC_ID_PREFIX);
195 aa.setProperty ("PDBID", this.id);
196 aa.setProperty ("oldCalcId", oldId);
197 }
198 }}
199 }, $fz.isPrivate = true, $fz));
200 Clazz.defineMethod (c$, "processPdbFileWithJmol", 
201 ($fz = function (prot) {
202 try {
203 var cl = Class.forName ("jalview.ext.jmol.PDBFileWithJmol");
204 if (cl != null) {
205 var constructor = cl.getConstructor ( Clazz.newArray (-1, [jalview.io.FileParse]));
206 var args =  Clazz.newArray (-1, [ new jalview.io.FileParse (this.getDataName (), this.type)]);
207 var jmf = constructor.newInstance (args);
208 var al =  new jalview.datamodel.Alignment (cl.getMethod ("getSeqsAsArray",  Clazz.newArray (-1, [])).invoke (jmf, []));
209 cl.getMethod ("addAnnotations",  Clazz.newArray (-1, [jalview.datamodel.AlignmentI])).invoke (jmf, [al]);
210 for (var sq, $sq = al.getSequences ().iterator (); $sq.hasNext () && ((sq = $sq.next ()) || true);) {
211 if (sq.getDatasetSequence () != null) {
212 sq.getDatasetSequence ().getPDBId ().clear ();
213 } else {
214 sq.getPDBId ().clear ();
215 }}
216 this.replaceAndUpdateChains (prot, al, "pep", false);
217 }} catch (q) {
218 if (Clazz.exceptionOf (q, ClassNotFoundException)) {
219 } else {
220 throw q;
221 }
222 }
223 }, $fz.isPrivate = true, $fz), "java.util.List");
224 Clazz.defineMethod (c$, "replaceAndUpdateChains", 
225 ($fz = function (prot, al, pep, b) {
226 var replaced = jalview.analysis.AlignSeq.replaceMatchingSeqsWith (this.seqs, this.annotations, prot, al, pep, false);
227 for (var ch, $ch = this.chains.iterator (); $ch.hasNext () && ((ch = $ch.next ()) || true);) {
228 var p = 0;
229 for (var sq, $sq = replaced.get (0).iterator (); $sq.hasNext () && ((sq = $sq.next ()) || true);) {
230 p++;
231 if (sq === ch.sequence || sq.getDatasetSequence () === ch.sequence) {
232 p = -p;
233 break;
234 }}
235 if (p < 0) {
236 p = -p - 1;
237 ch.shadow = replaced.get (1).get (p);
238 ch.shadowMap = (replaced.get (2).get (p)).getMappingFromS1 (false);
239 }}
240 }, $fz.isPrivate = true, $fz), "java.util.List,jalview.datamodel.AlignmentI,~S,~B");
241 Clazz.defineMethod (c$, "processPdbFileWithAnnotate3d", 
242 ($fz = function (rna) {
243 try {
244 var cl = Class.forName ("jalview.ws.jws1.Annotate3D");
245 if (cl != null) {
246 var annotate3d = cl.getConstructor ( Clazz.newArray (-1, [])).newInstance ( Clazz.newArray (-1, []));
247 var al = (cl.getMethod ("getRNAMLFor",  Clazz.newArray (-1, [jalview.io.FileParse])).invoke (annotate3d,  Clazz.newArray (-1, [ new jalview.io.FileParse (this.getDataName (), this.type)])));
248 for (var sq, $sq = al.getSequences ().iterator (); $sq.hasNext () && ((sq = $sq.next ()) || true);) {
249 if (sq.getDatasetSequence () != null) {
250 if (sq.getDatasetSequence ().getPDBId () != null) {
251 sq.getDatasetSequence ().getPDBId ().clear ();
252 }} else {
253 if (sq.getPDBId () != null) {
254 sq.getPDBId ().clear ();
255 }}}
256 this.replaceAndUpdateChains (rna, al, "dna", false);
257 }} catch (x) {
258 if (Clazz.exceptionOf (x, ClassNotFoundException)) {
259 } else {
260 throw x;
261 }
262 }
263 ;}, $fz.isPrivate = true, $fz), "java.util.List");
264 Clazz.defineMethod (c$, "safeName", 
265 ($fz = function (dataName) {
266 var p = 0;
267 while ((p = dataName.indexOf ("/")) > -1 && p < dataName.length) {
268 dataName = dataName.substring (p + 1);
269 }
270 return dataName;
271 }, $fz.isPrivate = true, $fz), "~S");
272 Clazz.defineMethod (c$, "makeResidueList", 
273 function () {
274 for (var i = 0; i < this.chains.size (); i++) {
275 this.chains.elementAt (i).makeResidueList (this.visibleChainAnnotation);
276 }
277 });
278 Clazz.defineMethod (c$, "makeCaBondList", 
279 function () {
280 for (var i = 0; i < this.chains.size (); i++) {
281 this.chains.elementAt (i).makeCaBondList ();
282 }
283 });
284 Clazz.defineMethod (c$, "findChain", 
285 function (id) {
286 for (var i = 0; i < this.chains.size (); i++) {
287 if (this.chains.elementAt (i).id.equals (id)) {
288 return this.chains.elementAt (i);
289 }}
290 return null;
291 }, "~S");
292 Clazz.defineMethod (c$, "setChargeColours", 
293 function () {
294 for (var i = 0; i < this.chains.size (); i++) {
295 this.chains.elementAt (i).setChargeColours ();
296 }
297 });
298 Clazz.defineMethod (c$, "setColours", 
299 function (cs) {
300 for (var i = 0; i < this.chains.size (); i++) {
301 this.chains.elementAt (i).setChainColours (cs);
302 }
303 }, "jalview.schemes.ColourSchemeI");
304 Clazz.defineMethod (c$, "setChainColours", 
305 function () {
306 for (var i = 0; i < this.chains.size (); i++) {
307 this.chains.elementAt (i).setChainColours (java.awt.Color.getHSBColor (1.0 / i, .4, 1.0));
308 }
309 });
310 c$.isRNA = Clazz.defineMethod (c$, "isRNA", 
311 function (seq) {
312 for (var c, $c = 0, $$c = seq.getSequence (); $c < $$c.length && ((c = $$c[$c]) || true); $c++) {
313 if ((c != 'A') && (c != 'C') && (c != 'G') && (c != 'U')) {
314 return false;
315 }}
316 return true;
317 }, "jalview.datamodel.SequenceI");
318 Clazz.defineStatics (c$,
319 "CALC_ID_PREFIX", "JalviewPDB");
320 });