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