JAL-1836 temporary workaround for negative resNumIns in PDB file
[jalview.git] / src / jalview / ext / jmol / PDBFileWithJmol.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ext.jmol;
22
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.Annotation;
25 import jalview.datamodel.PDBEntry;
26 import jalview.datamodel.Sequence;
27 import jalview.datamodel.SequenceI;
28 import jalview.io.AlignFile;
29 import jalview.io.FileParse;
30 import jalview.schemes.ResidueProperties;
31 import jalview.util.MessageManager;
32
33 import java.io.IOException;
34 import java.util.Hashtable;
35 import java.util.Map;
36
37 import javajs.awt.Dimension;
38
39 import org.jmol.api.JmolStatusListener;
40 import org.jmol.api.JmolViewer;
41 import org.jmol.c.CBK;
42 import org.jmol.modelset.Group;
43 import org.jmol.modelset.Model;
44 import org.jmol.modelset.ModelSet;
45 import org.jmol.modelsetbio.BioModel;
46 import org.jmol.modelsetbio.BioPolymer;
47 import org.jmol.viewer.Viewer;
48 import org.openscience.jmol.app.JmolApp;
49
50 /**
51  * Import and process PDB files with Jmol
52  * 
53  * @author jprocter
54  * 
55  */
56 public class PDBFileWithJmol extends AlignFile implements
57         JmolStatusListener
58 {
59
60   JmolApp jmolApp = null;
61
62   Viewer viewer = null;
63
64   public PDBFileWithJmol(String inFile, String type) throws IOException
65   {
66     super(inFile, type);
67   }
68
69   public PDBFileWithJmol(FileParse fp) throws IOException
70   {
71     super(fp);
72   }
73
74   public PDBFileWithJmol()
75   {
76     // TODO Auto-generated constructor stub
77   }
78
79   /**
80    * create a headless jmol instance for dataprocessing
81    * 
82    * @return
83    */
84   private Viewer getJmolData()
85   {
86     if (viewer == null)
87     { // note that -o -n -x are all implied // TODO check for Jmol 14.2
88       jmolApp = new JmolApp();
89       jmolApp.isDataOnly = true;
90       jmolApp.haveConsole = false;
91       jmolApp.haveDisplay = false;
92       try
93       {
94         viewer = (Viewer) JmolViewer.allocateViewer(null, null, null, null,
95                 null, "-x -o -n", this);
96         viewer.setScreenDimension(jmolApp.startupWidth,
97                 jmolApp.startupHeight);
98         jmolApp.startViewer(viewer, null, false);
99       } catch (ClassCastException x)
100       {
101         throw new Error(MessageManager.formatMessage("error.jmol_version_not_compatible_with_jalview_version", new String[]{JmolViewer.getJmolVersion()}),
102                 x);
103       }
104     }
105     return viewer;
106   }
107
108   private void waitForScript(Viewer jmd)
109   {
110     while (jmd.isScriptExecuting())
111     {
112       try
113       {
114         Thread.sleep(50);
115
116       } catch (InterruptedException x)
117       {
118       }
119     }
120   }
121
122   /*
123    * (non-Javadoc)
124    * 
125    * @see jalview.io.AlignFile#parse()
126    */
127   @Override
128   public void parse() throws IOException
129   {
130     Viewer jmd = getJmolData();
131     jmd.openReader(getDataName(), getDataName(), getReader());
132     waitForScript(jmd);
133
134     if (jmd.ms.mc > 0)
135     {
136       ModelSet ms = jmd.ms;
137       // Jmol 14.2 added third argument doReport = false
138       ms.calculateStructures(null, true, false, false, true);
139       // System.out.println("Structs\n"+structs);
140       Group group = null;
141       int modelIndex = -1;
142       for (Model model : ms.am)
143       {
144         modelIndex++;
145         for (BioPolymer bp : ((BioModel) model).bioPolymers)
146         {
147           int lastChainId = 0; // int value of character e.g. 65 for A
148           String lastChainIdAlpha = "";
149
150           int[] groups = bp.getLeadAtomIndices();
151           char seq[] = new char[groups.length], secstr[] = new char[groups.length], secstrcode[] = new char[groups.length];
152           int groupc = 0, len = 0, firstrnum = 1, lastrnum = 0;
153
154           do
155           {
156             if (groupc >= groups.length
157                     || ms.at[groups[groupc]].group.chain.chainID != lastChainId)
158             {
159               /*
160                * on change of chain (or at end), construct the sequence and
161                * secondary structure annotation for the last chain
162                */
163               if (len > 0)
164               {
165                 boolean isNa = bp.isNucleic();
166                 // normalise sequence from Jmol to jalview
167                 int[] cinds = isNa ? ResidueProperties.nucleotideIndex
168                         : ResidueProperties.aaIndex;
169                 int nonGap = isNa ? ResidueProperties.maxNucleotideIndex
170                         : ResidueProperties.maxProteinIndex;
171                 char ngc = 'X';
172                 char newseq[] = new char[len];
173                 Annotation asecstr[] = new Annotation[len + firstrnum - 1];
174                 for (int p = 0; p < len; p++)
175                 {
176                   newseq[p] = cinds[seq[p]] == nonGap ? ngc : seq[p];
177                   if (secstr[p] >= 'A' && secstr[p] <= 'z')
178                   {
179                     try
180                     {
181                       asecstr[p] = new Annotation("" + secstr[p], null,
182                               secstrcode[p], Float.NaN);
183                     } catch (ArrayIndexOutOfBoundsException e)
184                     {
185                       // skip - patch for JAL-1836
186                     }
187                   }
188                 }
189                 String modelTitle = (String) ms
190                         .getInfo(modelIndex, "title");
191                 SequenceI sq = new Sequence("" + getDataName() + "|"
192                         + modelTitle + "|" + lastChainIdAlpha, newseq,
193                         firstrnum, lastrnum);
194                 PDBEntry pdbe = new PDBEntry();
195                 pdbe.setFile(getDataName());
196                 pdbe.setId(getDataName());
197                 pdbe.setProperty(new Hashtable());
198                 // pdbe.getProperty().put("CHAIN", "" + _lastChainId);
199                 pdbe.setChainCode(lastChainIdAlpha);
200                 sq.addPDBId(pdbe);
201                 // JAL-1533
202                 // Need to put the number of models for this polymer somewhere
203                 // for Chimera/others to grab
204                 // pdbe.getProperty().put("PDBMODELS", biopoly.)
205                 seqs.add(sq);
206                 if (!isNa)
207                 {
208                   String mt = modelTitle == null ? getDataName()
209                           : modelTitle;
210                   if (lastChainId >= ' ')
211                   {
212                     mt += lastChainIdAlpha;
213                   }
214                   AlignmentAnnotation ann = new AlignmentAnnotation(
215                           "Secondary Structure", "Secondary Structure for "
216                                   + mt, asecstr);
217                   ann.belowAlignment = true;
218                   ann.visible = true;
219                   ann.autoCalculated = false;
220                   ann.setCalcId(getClass().getName());
221                   sq.addAlignmentAnnotation(ann);
222                   ann.adjustForAlignment();
223                   ann.validateRangeAndDisplay();
224                   annotations.add(ann);
225                 }
226               }
227               len = 0;
228               firstrnum = 1;
229               lastrnum = 0;
230             }
231             if (groupc < groups.length)
232             {
233               group = ms.at[groups[groupc]].group;
234               if (len == 0)
235               {
236                 firstrnum = group.getResno();
237                 lastChainId = group.chain.chainID;
238                 lastChainIdAlpha = group.chain.getIDStr();
239               }
240               else
241               {
242                 lastrnum = group.getResno();
243               }
244               seq[len] = group.getGroup1();
245               switch (group.getProteinStructureSubType())
246               {
247               case HELIX310:
248                 if (secstr[len] == 0)
249                 {
250                   secstr[len] = '3';
251                 }
252               case HELIXALPHA:
253                 if (secstr[len] == 0)
254                 {
255                   secstr[len] = 'H';
256                 }
257               case HELIXPI:
258                 if (secstr[len] == 0)
259                 {
260                   secstr[len] = 'P';
261                 }
262               case HELIX:
263                 if (secstr[len] == 0)
264                 {
265                   secstr[len] = 'H';
266                 }
267                 secstrcode[len] = 'H';
268                 break;
269               case SHEET:
270                 secstr[len] = 'E';
271                 secstrcode[len] = 'E';
272                 break;
273               default:
274                 secstr[len] = 0;
275                 secstrcode[len] = 0;
276               }
277               len++;
278             }
279           } while (groupc++ < groups.length);
280         }
281       }
282
283       /*
284        * lastScriptTermination = -9465; String dsspOut =
285        * jmd.evalString("calculate STRUCTURE"); if (dsspOut.equals("pending")) {
286        * while (lastScriptTermination == -9465) { try { Thread.sleep(50); }
287        * catch (Exception x) { } ; } } System.out.println(lastConsoleEcho);
288        */
289     }
290   }
291
292   /*
293    * (non-Javadoc)
294    * 
295    * @see jalview.io.AlignFile#print()
296    */
297   @Override
298   public String print()
299   {
300     // TODO Auto-generated method stub
301     return null;
302   }
303
304   @Override
305   public void setCallbackFunction(String callbackType,
306           String callbackFunction)
307   {
308     // TODO Auto-generated method stub
309
310   }
311
312   /*
313    * @Override public void notifyCallback(EnumCallback type, Object[] data) {
314    * try { switch (type) { case ERROR: case SCRIPT:
315    * notifyScriptTermination((String) data[2], ((Integer) data[3]).intValue());
316    * break; case MESSAGE: sendConsoleMessage((data == null) ? ((String) null) :
317    * (String) data[1]); break; case LOADSTRUCT: notifyFileLoaded((String)
318    * data[1], (String) data[2], (String) data[3], (String) data[4], ((Integer)
319    * data[5]).intValue());
320    * 
321    * break; default: // System.err.println("Unhandled callback " + type + " " //
322    * + data[1].toString()); break; } } catch (Exception e) {
323    * System.err.println("Squashed Jmol callback handler error:");
324    * e.printStackTrace(); } }
325    */
326   public void notifyCallback(CBK type, Object[] data)
327   {
328     String strInfo = (data == null || data[1] == null ? null : data[1]
329             .toString());
330     switch (type)
331     {
332     case ECHO:
333       sendConsoleEcho(strInfo);
334       break;
335     case SCRIPT:
336       notifyScriptTermination((String) data[2],
337               ((Integer) data[3]).intValue());
338       break;
339     case MEASURE:
340       String mystatus = (String) data[3];
341       if (mystatus.indexOf("Picked") >= 0
342               || mystatus.indexOf("Sequence") >= 0)
343       {
344         // Picking mode
345         sendConsoleMessage(strInfo);
346       }
347       else if (mystatus.indexOf("Completed") >= 0)
348       {
349         sendConsoleEcho(strInfo.substring(strInfo.lastIndexOf(",") + 2,
350                 strInfo.length() - 1));
351       }
352       break;
353     case MESSAGE:
354       sendConsoleMessage(data == null ? null : strInfo);
355       break;
356     case PICK:
357       sendConsoleMessage(strInfo);
358       break;
359     default:
360       break;
361     }
362   }
363
364   String lastConsoleEcho = "";
365
366   private void sendConsoleEcho(String string)
367   {
368     lastConsoleEcho += string;
369     lastConsoleEcho += "\n";
370   }
371
372   String lastConsoleMessage = "";
373
374   private void sendConsoleMessage(String string)
375   {
376     lastConsoleMessage += string;
377     lastConsoleMessage += "\n";
378   }
379
380   int lastScriptTermination = -1;
381
382   String lastScriptMessage = "";
383
384   private void notifyScriptTermination(String string, int intValue)
385   {
386     lastScriptMessage += string;
387     lastScriptMessage += "\n";
388     lastScriptTermination = intValue;
389   }
390
391   @Override
392   public boolean notifyEnabled(CBK callbackPick)
393   {
394     switch (callbackPick)
395     {
396     case MESSAGE:
397     case SCRIPT:
398     case ECHO:
399     case LOADSTRUCT:
400     case ERROR:
401       return true;
402     default:
403       return false;
404     }
405   }
406
407   @Override
408   public String eval(String strEval)
409   {
410     // TODO Auto-generated method stub
411     return null;
412   }
413
414   @Override
415   public float[][] functionXY(String functionName, int x, int y)
416   {
417     // TODO Auto-generated method stub
418     return null;
419   }
420
421   @Override
422   public float[][][] functionXYZ(String functionName, int nx, int ny, int nz)
423   {
424     // TODO Auto-generated method stub
425     return null;
426   }
427
428   @Override
429   public String createImage(String fileName, String type,
430           Object text_or_bytes, int quality)
431   {
432     // TODO Auto-generated method stub
433     return null;
434   }
435
436   @Override
437   public Map<String, Object> getRegistryInfo()
438   {
439     // TODO Auto-generated method stub
440     return null;
441   }
442
443   @Override
444   public void showUrl(String url)
445   {
446     // TODO Auto-generated method stub
447
448   }
449
450   @Override
451   public Dimension resizeInnerPanel(String data)
452   {
453     return null;
454   }
455
456   @Override
457   public Map<String, Object> getJSpecViewProperty(String arg0)
458   {
459     return null;
460   }
461
462 }