2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.ext.jmol;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Locale;
29 import java.util.Vector;
31 import org.jmol.api.JmolStatusListener;
32 import org.jmol.api.JmolViewer;
33 import org.jmol.c.CBK;
34 import org.jmol.c.STR;
35 import org.jmol.modelset.ModelSet;
36 import org.jmol.viewer.Viewer;
38 import com.stevesoft.pat.Regex;
40 import jalview.bin.Console;
41 import jalview.datamodel.Alignment;
42 import jalview.datamodel.AlignmentAnnotation;
43 import jalview.datamodel.Annotation;
44 import jalview.datamodel.PDBEntry;
45 import jalview.datamodel.SequenceI;
46 import jalview.datamodel.annotations.AlphaFoldAnnotationRowBuilder;
47 import jalview.datamodel.annotations.AnnotationRowBuilder;
48 import jalview.io.DataSourceType;
49 import jalview.io.FileParse;
50 import jalview.io.StructureFile;
51 import jalview.schemes.ResidueProperties;
52 import jalview.util.Format;
53 import jalview.util.MessageManager;
54 import jalview.ws.dbsources.EBIAlfaFold;
56 import mc_view.PDBChain;
57 import mc_view.Residue;
60 * Import and process files with Jmol for file like PDB, mmCIF
65 public class JmolParser extends StructureFile implements JmolStatusListener
69 private boolean alphaFoldModel;
71 public JmolParser(boolean immediate, Object inFile,
72 DataSourceType sourceType) throws IOException
74 // BH 2018 File or String for filename
75 super(immediate, inFile, sourceType);
78 public JmolParser(Object inFile, DataSourceType sourceType)
81 super(inFile, sourceType);
84 public JmolParser(FileParse fp) throws IOException
94 * Calls the Jmol library to parse the PDB/mmCIF file, and then inspects the
95 * resulting object model to generate Jalview-style sequences, with secondary
96 * structure annotation added where available (i.e. where it has been computed
97 * by Jmol using DSSP).
99 * @see jalview.io.AlignFile#parse()
102 public void parse() throws IOException
104 setChains(new Vector<PDBChain>());
105 Viewer jmolModel = getJmolData();
106 jmolModel.openReader(getDataName(), getDataName(), getReader());
107 waitForScript(jmolModel);
110 * Convert one or more Jmol Model objects to Jalview sequences
112 if (jmolModel.ms.mc > 0)
114 // ideally we do this
117 // setStructureFileType(jmolModel.evalString("show _fileType"));
118 // } catch (Exception q)
122 // instead, we distinguish .cif from non-.cif by filename
123 setStructureFileType(
124 getDataName().toLowerCase(Locale.ROOT).endsWith(".cif")
125 ? PDBEntry.Type.MMCIF.toString()
128 transformJmolModelToJalview(jmolModel.ms);
133 * create a headless jmol instance for dataprocessing
137 private Viewer getJmolData()
144 * params -o (output to sysout) -n (nodisplay) -x (exit when finished)
145 * see http://wiki.jmol.org/index.php/Jmol_Application
148 viewer = JalviewJmolBinding.getJmolData(this);
149 // ensure the 'new' (DSSP) not 'old' (Ramachandran) SS method is used
150 viewer.setBooleanProperty("defaultStructureDSSP", true);
151 } catch (ClassCastException x)
153 throw new Error(MessageManager.formatMessage(
154 "error.jmol_version_not_compatible_with_jalview_version",
156 { JmolViewer.getJmolVersion() }), x);
162 public static Regex getNewAlphafoldValidator()
164 Regex validator = new Regex("(AF-[A-Z]+[0-9]+[A-Z0-9]+-F1)");
165 validator.setIgnoreCase(true);
169 PDBEntry.Type jmolFiletype = null;
172 * resolve a jmol filetype string and update the jmolFiletype field
175 * @param jmolIdentifiedFileType
176 * @return true if filetype was identified as MMCIF, PDB
178 public boolean updateFileType(String jmolIdentifiedFileType)
180 if (jmolIdentifiedFileType == null
181 || jmolIdentifiedFileType.trim().equals(""))
185 if ("mmcif".equalsIgnoreCase(jmolIdentifiedFileType))
187 jmolFiletype = PDBEntry.Type.MMCIF;
190 if ("pdb".equalsIgnoreCase(jmolIdentifiedFileType))
192 jmolFiletype = PDBEntry.Type.PDB;
198 public void transformJmolModelToJalview(ModelSet ms) throws IOException
202 Regex alphaFold = getNewAlphafoldValidator();
204 List<SequenceI> rna = new ArrayList<SequenceI>();
205 List<SequenceI> prot = new ArrayList<SequenceI>();
207 String pdbId = (String) ms.getInfo(0, "title");
208 boolean isMMCIF = false;
209 String jmolFileType_String = (String) ms.getInfo(0, "fileType");
210 if (updateFileType(jmolFileType_String))
212 setStructureFileType(jmolFiletype.toString());
215 isMMCIF = PDBEntry.Type.MMCIF.equals(jmolFiletype);
219 setId(safeName(getDataName()));
220 setPDBIdAvailable(false);
225 setPDBIdAvailable(true);
226 alphaFoldModel = alphaFold.search(pdbId) && isMMCIF;
229 List<Atom> significantAtoms = convertSignificantAtoms(ms);
230 for (Atom tmpatom : significantAtoms)
232 if (tmpatom.resNumIns.trim().equals(lastID))
234 // phosphorylated protein - seen both CA and P..
237 tmpchain = findChain(tmpatom.chain);
238 if (tmpchain != null)
240 tmpchain.atoms.addElement(tmpatom);
244 AnnotationRowBuilder builder = null;
245 String tempFString = null;
246 if (isAlphafoldModel())
248 builder = new AlphaFoldAnnotationRowBuilder();
251 tmpchain = new PDBChain(getId(), tmpatom.chain, builder);
252 getChains().add(tmpchain);
253 tmpchain.atoms.addElement(tmpatom);
255 lastID = tmpatom.resNumIns.trim();
257 if (isParseImmediately())
259 // configure parsing settings from the static singleton
266 for (PDBChain chain : getChains())
268 SequenceI chainseq = postProcessChain(chain);
278 // look at local setting for adding secondary tructure
279 if (predictSecondaryStructure)
281 createAnnotation(chainseq, chain, ms.at);
284 if (isAlphafoldModel())
286 // TODO - work out how to handle different ways that pAE is provided
290 Console.info("retrieving pAE for " + pdbId);
291 Alignment al = new Alignment(prot.toArray(new SequenceI[0]));
292 EBIAlfaFold.retrieve_AlphaFold_pAE(pdbId, al, null);
293 if (al.getAlignmentAnnotation() != null)
295 for (AlignmentAnnotation alann : al.getAlignmentAnnotation())
297 annotations.add(alann);
301 } catch (Throwable t)
303 Console.error("Couldn't get the pAE for " + pdbId, t);
306 } catch (OutOfMemoryError er)
309 "OUT OF MEMORY LOADING TRANSFORMING JMOL MODEL TO JALVIEW MODEL");
310 throw new IOException(MessageManager
311 .getString("exception.outofmemory_loading_mmcif_file"));
315 private boolean isAlphafoldModel()
317 return alphaFoldModel;
320 private List<Atom> convertSignificantAtoms(ModelSet ms)
322 List<Atom> significantAtoms = new ArrayList<Atom>();
323 HashMap<String, org.jmol.modelset.Atom> chainTerMap = new HashMap<String, org.jmol.modelset.Atom>();
324 org.jmol.modelset.Atom prevAtom = null;
325 for (org.jmol.modelset.Atom atom : ms.at)
327 if (atom.getAtomName().equalsIgnoreCase("CA")
328 || atom.getAtomName().equalsIgnoreCase("P"))
330 if (!atomValidated(atom, prevAtom, chainTerMap))
334 Atom curAtom = new Atom(atom.x, atom.y, atom.z);
335 curAtom.atomIndex = atom.getIndex();
336 curAtom.chain = atom.getChainIDStr();
337 curAtom.insCode = atom.group.getInsertionCode() == '\000' ? ' '
338 : atom.group.getInsertionCode();
339 curAtom.name = atom.getAtomName();
340 curAtom.number = atom.getAtomNumber();
341 curAtom.resName = atom.getGroup3(true);
342 curAtom.resNumber = atom.getResno();
343 curAtom.occupancy = ms.occupancies != null
344 ? ms.occupancies[atom.getIndex()]
345 : Float.valueOf(atom.getOccupancy100());
346 String fmt = new Format("%4i").form(curAtom.resNumber);
347 curAtom.resNumIns = (fmt + curAtom.insCode);
348 curAtom.tfactor = atom.getBfactor100() / 100f;
350 // significantAtoms.add(curAtom);
351 // ignore atoms from subsequent models
352 if (!significantAtoms.contains(curAtom))
354 significantAtoms.add(curAtom);
359 return significantAtoms;
362 private boolean atomValidated(org.jmol.modelset.Atom curAtom,
363 org.jmol.modelset.Atom prevAtom,
364 HashMap<String, org.jmol.modelset.Atom> chainTerMap)
366 // System.out.println("Atom: " + curAtom.getAtomNumber()
367 // + " Last atom index " + curAtom.group.lastAtomIndex);
368 if (chainTerMap == null || prevAtom == null)
372 String curAtomChId = curAtom.getChainIDStr();
373 String prevAtomChId = prevAtom.getChainIDStr();
374 // new chain encoutered
375 if (!prevAtomChId.equals(curAtomChId))
377 // On chain switch add previous chain termination to xTerMap if not exists
378 if (!chainTerMap.containsKey(prevAtomChId))
380 chainTerMap.put(prevAtomChId, prevAtom);
382 // if current atom belongs to an already terminated chain and the resNum
383 // diff < 5 then mark as valid and update termination Atom
384 if (chainTerMap.containsKey(curAtomChId))
386 if (curAtom.getResno() < chainTerMap.get(curAtomChId).getResno())
390 if ((curAtom.getResno()
391 - chainTerMap.get(curAtomChId).getResno()) < 5)
393 chainTerMap.put(curAtomChId, curAtom);
399 // atom with previously terminated chain encountered
400 else if (chainTerMap.containsKey(curAtomChId))
402 if (curAtom.getResno() < chainTerMap.get(curAtomChId).getResno())
406 if ((curAtom.getResno()
407 - chainTerMap.get(curAtomChId).getResno()) < 5)
409 chainTerMap.put(curAtomChId, curAtom);
414 // HETATM with resNum jump > 2
415 return !(curAtom.isHetero()
416 && ((curAtom.getResno() - prevAtom.getResno()) > 2));
419 private void createAnnotation(SequenceI sequence, PDBChain chain,
420 org.jmol.modelset.Atom[] jmolAtoms)
422 char[] secstr = new char[sequence.getLength()];
423 char[] secstrcode = new char[sequence.getLength()];
425 // Ensure Residue size equals Seq size
426 if (chain.residues.size() != sequence.getLength())
431 for (Residue residue : chain.residues)
433 Atom repAtom = residue.getAtoms().get(0);
434 STR proteinStructureSubType = jmolAtoms[repAtom.atomIndex].group
435 .getProteinStructureSubType();
436 setSecondaryStructure(proteinStructureSubType, annotIndex, secstr,
440 addSecondaryStructureAnnotation(chain.pdbid, sequence, secstr,
441 secstrcode, chain.id, sequence.getStart());
445 * Helper method that adds an AlignmentAnnotation for secondary structure to
446 * the sequence, provided at least one secondary structure assignment has been
457 protected void addSecondaryStructureAnnotation(String modelTitle,
458 SequenceI sq, char[] secstr, char[] secstrcode, String chainId,
461 int length = sq.getLength();
462 boolean ssFound = false;
463 Annotation asecstr[] = new Annotation[length + firstResNum - 1];
464 for (int p = 0; p < length; p++)
466 if (secstr[p] >= 'A' && secstr[p] <= 'z')
470 asecstr[p] = new Annotation(null, null, secstrcode[p], Float.NaN);
472 } catch (Exception e)
474 // e.printStackTrace();
481 String mt = modelTitle == null ? getDataName() : modelTitle;
483 AlignmentAnnotation ann = new AlignmentAnnotation(
484 "Secondary Structure", "Secondary Structure for " + mt,
486 ann.belowAlignment = true;
488 ann.autoCalculated = false;
489 ann.setCalcId(getClass().getName());
490 ann.adjustForAlignment();
491 ann.validateRangeAndDisplay();
492 annotations.add(ann);
493 sq.addAlignmentAnnotation(ann);
497 private void waitForScript(Viewer jmd)
499 while (jmd.isScriptExecuting())
505 } catch (InterruptedException x)
512 * Convert Jmol's secondary structure code to Jalview's, and stored it in the
513 * secondary structure arrays at the given sequence position
515 * @param proteinStructureSubType
520 protected void setSecondaryStructure(STR proteinStructureSubType, int pos,
521 char[] secstr, char[] secstrcode)
523 switch (proteinStructureSubType)
542 switch (proteinStructureSubType)
548 secstrcode[pos] = 'H';
551 secstrcode[pos] = 'E';
559 * Convert any non-standard peptide codes to their standard code table
560 * equivalent. (Initial version only does Selenomethionine MSE->MET.)
562 * @param threeLetterCode
566 protected void replaceNonCanonicalResidue(String threeLetterCode,
569 String canonical = ResidueProperties
570 .getCanonicalAminoAcid(threeLetterCode);
571 if (canonical != null && !canonical.equalsIgnoreCase(threeLetterCode))
573 seq[pos] = ResidueProperties.getSingleCharacterCode(canonical);
578 * Not implemented - returns null
581 public String print(SequenceI[] seqs, boolean jvSuffix)
590 public void setCallbackFunction(String callbackType,
591 String callbackFunction)
596 public void notifyCallback(CBK cbType, Object[] data)
598 String strInfo = (data == null || data[1] == null ? null
599 : data[1].toString());
603 sendConsoleEcho(strInfo);
606 notifyScriptTermination((String) data[2],
607 ((Integer) data[3]).intValue());
610 String mystatus = (String) data[3];
611 if (mystatus.indexOf("Picked") >= 0
612 || mystatus.indexOf("Sequence") >= 0)
615 sendConsoleMessage(strInfo);
617 else if (mystatus.indexOf("Completed") >= 0)
619 sendConsoleEcho(strInfo.substring(strInfo.lastIndexOf(",") + 2,
620 strInfo.length() - 1));
624 sendConsoleMessage(data == null ? null : strInfo);
627 sendConsoleMessage(strInfo);
634 String lastConsoleEcho = "";
636 private void sendConsoleEcho(String string)
638 lastConsoleEcho += string;
639 lastConsoleEcho += "\n";
642 String lastConsoleMessage = "";
644 private void sendConsoleMessage(String string)
646 lastConsoleMessage += string;
647 lastConsoleMessage += "\n";
650 int lastScriptTermination = -1;
652 String lastScriptMessage = "";
654 private void notifyScriptTermination(String string, int intValue)
656 lastScriptMessage += string;
657 lastScriptMessage += "\n";
658 lastScriptTermination = intValue;
662 public boolean notifyEnabled(CBK callbackPick)
664 switch (callbackPick)
678 * Not implemented - returns null
681 public String eval(String strEval)
687 * Not implemented - returns null
690 public float[][] functionXY(String functionName, int x, int y)
696 * Not implemented - returns null
699 public float[][][] functionXYZ(String functionName, int nx, int ny,
706 * Not implemented - returns null
709 public String createImage(String fileName, String imageType,
710 Object text_or_bytes, int quality)
716 * Not implemented - returns null
719 public Map<String, Object> getRegistryInfo()
728 public void showUrl(String url)
733 * Not implemented - returns null
736 public int[] resizeInnerPanel(String data)
742 public Map<String, Object> getJSpecViewProperty(String arg0)
747 public boolean isPredictSecondaryStructure()
749 return predictSecondaryStructure;
752 public void setPredictSecondaryStructure(
753 boolean predictSecondaryStructure)
755 this.predictSecondaryStructure = predictSecondaryStructure;
758 public boolean isVisibleChainAnnotation()
760 return visibleChainAnnotation;
763 public void setVisibleChainAnnotation(boolean visibleChainAnnotation)
765 this.visibleChainAnnotation = visibleChainAnnotation;