b58d3ddf64780b6f6406ed07e4d00fe864674ccf
[jalviewjs.git] / bin / MCview / PDBChain.js
1 Clazz.declarePackage ("MCview");\r
2 Clazz.load (["java.util.Vector"], "MCview.PDBChain", ["MCview.Bond", "$.Residue", "jalview.datamodel.AlignmentAnnotation", "$.Annotation", "$.Sequence", "$.SequenceFeature", "jalview.schemes.ResidueProperties", "java.awt.Color", "java.lang.StringBuilder"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.id = null;\r
5 this.bonds = null;\r
6 this.atoms = null;\r
7 this.residues = null;\r
8 this.offset = 0;\r
9 this.sequence = null;\r
10 this.shadow = null;\r
11 this.isNa = false;\r
12 this.isVisible = true;\r
13 this.pdbstart = 0;\r
14 this.pdbend = 0;\r
15 this.seqstart = 0;\r
16 this.seqend = 0;\r
17 this.pdbid = "";\r
18 this.newline = null;\r
19 this.shadowMap = null;\r
20 Clazz.instantialize (this, arguments);\r
21 }, MCview, "PDBChain");\r
22 Clazz.prepareFields (c$, function () {\r
23 this.bonds =  new java.util.Vector ();\r
24 this.atoms =  new java.util.Vector ();\r
25 this.residues =  new java.util.Vector ();\r
26 this.newline = System.getProperty ("line.separator");\r
27 });\r
28 Clazz.makeConstructor (c$, \r
29 function (pdbid, id) {\r
30 this.pdbid = pdbid.toLowerCase ();\r
31 this.id = id;\r
32 }, "~S,~S");\r
33 Clazz.defineMethod (c$, "setNewlineString", \r
34 function (nl) {\r
35 this.newline = nl;\r
36 }, "~S");\r
37 Clazz.defineMethod (c$, "getNewlineString", \r
38 function () {\r
39 return this.newline;\r
40 });\r
41 Clazz.defineMethod (c$, "print", \r
42 function () {\r
43 var tmp =  new StringBuilder (256);\r
44 for (var b, $b = this.bonds.iterator (); $b.hasNext () && ((b = $b.next ()) || true);) {\r
45 tmp.append (b.at1.resName).append (" ").append (b.at1.resNumber).append (" ").append (this.offset).append (this.newline);\r
46 }\r
47 return tmp.toString ();\r
48 });\r
49 Clazz.defineMethod (c$, "makeExactMapping", \r
50 function (as, s1) {\r
51 var pdbpos = as.getSeq2Start () - 2;\r
52 var alignpos = s1.getStart () + as.getSeq1Start () - 3;\r
53 for (var atom, $atom = this.atoms.iterator (); $atom.hasNext () && ((atom = $atom.next ()) || true);) {\r
54 atom.alignmentMapping = -1;\r
55 }\r
56 for (var i = 0; i < as.astr1.length; i++) {\r
57 if (as.astr1.charAt (i) != '-') {\r
58 alignpos++;\r
59 }if (as.astr2.charAt (i) != '-') {\r
60 pdbpos++;\r
61 }if (as.astr1.charAt (i) == as.astr2.charAt (i)) {\r
62 var res = this.residues.elementAt (pdbpos);\r
63 for (var atom, $atom = res.atoms.iterator (); $atom.hasNext () && ((atom = $atom.next ()) || true);) {\r
64 atom.alignmentMapping = alignpos;\r
65 }\r
66 }}\r
67 }, "jalview.analysis.AlignSeq,jalview.datamodel.SequenceI");\r
68 Clazz.defineMethod (c$, "transferRESNUMFeatures", \r
69 function (seq, status) {\r
70 var sq = seq;\r
71 while (sq != null && sq.getDatasetSequence () != null) {\r
72 sq = sq.getDatasetSequence ();\r
73 if (sq === this.sequence) {\r
74 return null;\r
75 }}\r
76 if (status == null) {\r
77 status = "IEA:jalview";\r
78 }var features = this.sequence.getSequenceFeatures ();\r
79 for (var i = 0; i < features.length; i++) {\r
80 if (features[i].getFeatureGroup ().equals (this.pdbid)) {\r
81 var tx =  new jalview.datamodel.SequenceFeature (features[i]);\r
82 tx.setBegin (1 + this.residues.elementAt (tx.getBegin () - this.offset).atoms.elementAt (0).alignmentMapping);\r
83 tx.setEnd (1 + this.residues.elementAt (tx.getEnd () - this.offset).atoms.elementAt (0).alignmentMapping);\r
84 tx.setStatus (status + ((tx.getStatus () == null || tx.getStatus ().length == 0) ? "" : ":" + tx.getStatus ()));\r
85 if (tx.begin != 0 && tx.end != 0) {\r
86 sq.addSequenceFeature (tx);\r
87 }}}\r
88 return features;\r
89 }, "jalview.datamodel.SequenceI,~S");\r
90 Clazz.defineMethod (c$, "makeCaBondList", \r
91 function () {\r
92 var na = false;\r
93 var numNa = 0;\r
94 for (var i = 0; i < (this.residues.size () - 1); i++) {\r
95 var tmpres = this.residues.elementAt (i);\r
96 var tmpres2 = this.residues.elementAt (i + 1);\r
97 var at1 = tmpres.findAtom ("CA");\r
98 var at2 = tmpres2.findAtom ("CA");\r
99 na = false;\r
100 if ((at1 == null) && (at2 == null)) {\r
101 na = true;\r
102 at1 = tmpres.findAtom ("P");\r
103 at2 = tmpres2.findAtom ("P");\r
104 }if ((at1 != null) && (at2 != null)) {\r
105 if (at1.chain.equals (at2.chain)) {\r
106 if (na) {\r
107 numNa++;\r
108 }this.makeBond (at1, at2);\r
109 }} else {\r
110 System.out.println ("not found " + i);\r
111 }}\r
112 if (this.residues.size () > 0 && (Clazz.doubleToInt (numNa / (this.residues.size () - 1)) > 0.99)) {\r
113 this.isNa = true;\r
114 }});\r
115 Clazz.defineMethod (c$, "makeBond", \r
116 function (at1, at2) {\r
117 this.bonds.addElement ( new MCview.Bond (at1, at2));\r
118 }, "MCview.Atom,MCview.Atom");\r
119 Clazz.defineMethod (c$, "makeResidueList", \r
120 function (visibleChainAnnotation) {\r
121 var count = 0;\r
122 var symbol;\r
123 var deoxyn = false;\r
124 var nucleotide = false;\r
125 var seq =  new StringBuilder (256);\r
126 var resFeatures =  new java.util.Vector ();\r
127 var resAnnotation =  new java.util.Vector ();\r
128 var i;\r
129 var iSize = this.atoms.size () - 1;\r
130 var resNumber = -1;\r
131 for (i = 0; i <= iSize; i++) {\r
132 var tmp = this.atoms.elementAt (i);\r
133 resNumber = tmp.resNumber;\r
134 var res = resNumber;\r
135 if (i == 0) {\r
136 this.offset = resNumber;\r
137 }var resAtoms =  new java.util.Vector ();\r
138 while ((resNumber == res) && (i < this.atoms.size ())) {\r
139 resAtoms.add (this.atoms.elementAt (i));\r
140 i++;\r
141 if (i < this.atoms.size ()) {\r
142 resNumber = this.atoms.elementAt (i).resNumber;\r
143 } else {\r
144 resNumber++;\r
145 }}\r
146 i--;\r
147 this.residues.addElement ( new MCview.Residue (resAtoms, resNumber - 1, count));\r
148 var tmpres = this.residues.lastElement ();\r
149 var tmpat = tmpres.atoms.get (0);\r
150 var sf =  new jalview.datamodel.SequenceFeature ("RESNUM", tmpat.resName + ":" + tmpat.resNumIns + " " + this.pdbid + this.id, "", this.offset + count, this.offset + count, this.pdbid);\r
151 resFeatures.addElement (sf);\r
152 resAnnotation.addElement ( new jalview.datamodel.Annotation (tmpat.tfactor));\r
153 if ((symbol = jalview.schemes.ResidueProperties.getAA3Hash ().get (tmpat.resName)) == null) {\r
154 var nucname = tmpat.resName.trim ();\r
155 deoxyn = nucname.length == 2 && jalview.schemes.ResidueProperties.aaIndex[nucname.charCodeAt (0)] == jalview.schemes.ResidueProperties.aaIndex['D'.charCodeAt (0)];\r
156 if (tmpat.name.equalsIgnoreCase ("CA") || jalview.schemes.ResidueProperties.nucleotideIndex[nucname.charCodeAt ((deoxyn ? 1 : 0))] == -1) {\r
157 seq.append ("X");\r
158 } else {\r
159 nucleotide = true;\r
160 seq.append (nucname.charAt ((deoxyn ? 1 : 0)));\r
161 }} else {\r
162 if (nucleotide) {\r
163 System.err.println ("Warning: mixed nucleotide and amino acid chain.. its gonna do bad things to you!");\r
164 }seq.append (jalview.schemes.ResidueProperties.aa[(symbol).intValue ()]);\r
165 }count++;\r
166 }\r
167 if (this.id.length < 1) {\r
168 this.id = " ";\r
169 }this.isNa = nucleotide;\r
170 this.sequence =  new jalview.datamodel.Sequence (this.id, seq.toString (), this.offset, resNumber - 1);\r
171 for (i = 0, iSize = resFeatures.size (); i < iSize; i++) {\r
172 this.sequence.addSequenceFeature (resFeatures.elementAt (i));\r
173 resFeatures.setElementAt (null, i);\r
174 }\r
175 if (visibleChainAnnotation) {\r
176 var annots =  new Array (resAnnotation.size ());\r
177 var max = 0;\r
178 for (i = 0, iSize = annots.length; i < iSize; i++) {\r
179 annots[i] = resAnnotation.elementAt (i);\r
180 if (annots[i].value > max) {\r
181 max = annots[i].value;\r
182 }resAnnotation.setElementAt (null, i);\r
183 }\r
184 var tfactorann =  new jalview.datamodel.AlignmentAnnotation ("Temperature Factor", "Temperature Factor for " + this.pdbid + this.id, annots, 0, max, 2);\r
185 tfactorann.setSequenceRef (this.sequence);\r
186 this.sequence.addAlignmentAnnotation (tfactorann);\r
187 }}, "~B");\r
188 Clazz.defineMethod (c$, "setChargeColours", \r
189 function () {\r
190 for (var b, $b = this.bonds.iterator (); $b.hasNext () && ((b = $b.next ()) || true);) {\r
191 if (b.at1 != null && b.at2 != null) {\r
192 b.startCol = MCview.PDBChain.getChargeColour (b.at1.resName);\r
193 b.endCol = MCview.PDBChain.getChargeColour (b.at2.resName);\r
194 } else {\r
195 b.startCol = java.awt.Color.gray;\r
196 b.endCol = java.awt.Color.gray;\r
197 }}\r
198 });\r
199 c$.getChargeColour = Clazz.defineMethod (c$, "getChargeColour", \r
200 function (resName) {\r
201 var result = java.awt.Color.lightGray;\r
202 if ("ASP".equals (resName) || "GLU".equals (resName)) {\r
203 result = java.awt.Color.red;\r
204 } else if ("LYS".equals (resName) || "ARG".equals (resName)) {\r
205 result = java.awt.Color.blue;\r
206 } else if ("CYS".equals (resName)) {\r
207 result = java.awt.Color.yellow;\r
208 }return result;\r
209 }, "~S");\r
210 Clazz.defineMethod (c$, "setChainColours", \r
211 function (cs) {\r
212 var index;\r
213 for (var b, $b = this.bonds.iterator (); $b.hasNext () && ((b = $b.next ()) || true);) {\r
214 try {\r
215 index = jalview.schemes.ResidueProperties.aa3Hash.get (b.at1.resName).intValue ();\r
216 b.startCol = cs.findColour (jalview.schemes.ResidueProperties.aa[index].charAt (0));\r
217 index = jalview.schemes.ResidueProperties.aa3Hash.get (b.at2.resName).intValue ();\r
218 b.endCol = cs.findColour (jalview.schemes.ResidueProperties.aa[index].charAt (0));\r
219 } catch (e) {\r
220 if (Clazz.exceptionOf (e, Exception)) {\r
221 b.startCol = java.awt.Color.gray;\r
222 b.endCol = java.awt.Color.gray;\r
223 } else {\r
224 throw e;\r
225 }\r
226 }\r
227 }\r
228 }, "jalview.schemes.ColourSchemeI");\r
229 Clazz.defineMethod (c$, "setChainColours", \r
230 function (col) {\r
231 for (var b, $b = this.bonds.iterator (); $b.hasNext () && ((b = $b.next ()) || true);) {\r
232 b.startCol = col;\r
233 b.endCol = col;\r
234 }\r
235 }, "java.awt.Color");\r
236 Clazz.defineMethod (c$, "transferResidueAnnotation", \r
237 function (mapping, sqmpping) {\r
238 var sq = mapping.getSequence ();\r
239 var dsq = sq;\r
240 if (sq != null) {\r
241 while (dsq.getDatasetSequence () != null) {\r
242 dsq = dsq.getDatasetSequence ();\r
243 }\r
244 if (this.shadow != null && this.shadow.getAnnotation () != null) {\r
245 for (var ana, $ana = 0, $$ana = this.shadow.getAnnotation (); $ana < $$ana.length && ((ana = $$ana[$ana]) || true); $ana++) {\r
246 var transfer = sq.getAlignmentAnnotations (ana.getCalcId (), ana.label);\r
247 if (transfer == null || transfer.size () == 0) {\r
248 ana =  new jalview.datamodel.AlignmentAnnotation (ana);\r
249 ana.liftOver (this.sequence, this.shadowMap);\r
250 ana.liftOver (dsq, sqmpping);\r
251 dsq.addAlignmentAnnotation (ana);\r
252 } else {\r
253 continue;\r
254 }}\r
255 } else {\r
256 if (this.sequence != null && this.sequence.getAnnotation () != null) {\r
257 for (var ana, $ana = 0, $$ana = this.sequence.getAnnotation (); $ana < $$ana.length && ((ana = $$ana[$ana]) || true); $ana++) {\r
258 var transfer = sq.getAlignmentAnnotations (ana.getCalcId (), ana.label);\r
259 if (transfer == null || transfer.size () == 0) {\r
260 ana =  new jalview.datamodel.AlignmentAnnotation (ana);\r
261 ana.liftOver (dsq, sqmpping);\r
262 } else {\r
263 continue;\r
264 }}\r
265 }}if (false) {\r
266 var min = -1;\r
267 var max = 0;\r
268 var an =  new Array (sq.getEnd () - sq.getStart () + 1);\r
269 for (var i = sq.getStart (), j = sq.getEnd (), k = 0; i <= j; i++, k++) {\r
270 var prn = mapping.getPDBResNum (k + 1);\r
271 an[k] =  new jalview.datamodel.Annotation (prn);\r
272 if (min == -1) {\r
273 min = k;\r
274 max = k;\r
275 } else {\r
276 if (min > k) {\r
277 min = k;\r
278 } else if (max < k) {\r
279 max = k;\r
280 }}}\r
281 sq.addAlignmentAnnotation ( new jalview.datamodel.AlignmentAnnotation ("PDB.RESNUM", "PDB Residue Numbering for " + this.pdbid + ":" + this.id, an, min, max, 2));\r
282 }}}, "jalview.structure.StructureMapping,jalview.datamodel.Mapping");\r
283 Clazz.defineStatics (c$,\r
284 "PDBFILEFEATURE", "PDBFile",\r
285 "IEASTATUS", "IEA:jalview");\r
286 });\r