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.structure.StructureImportSettings;
53 import jalview.util.Format;
54 import jalview.util.MessageManager;
55 import jalview.ws.dbsources.EBIAlfaFold;
57 import mc_view.PDBChain;
58 import mc_view.Residue;
61 * Import and process files with Jmol for file like PDB, mmCIF
66 public class JmolParser extends StructureFile implements JmolStatusListener
70 private boolean alphaFoldModel;
72 public JmolParser(boolean immediate, Object inFile,
73 DataSourceType sourceType) throws IOException
75 // BH 2018 File or String for filename
76 super(immediate, inFile, sourceType);
80 public JmolParser(Object inFile, DataSourceType sourceType)
83 this(inFile, sourceType, null);
86 public JmolParser(Object inFile, DataSourceType sourceType,
87 StructureImportSettings.TFType tempfacType) throws IOException
89 super(inFile, sourceType, tempfacType);
92 public JmolParser(FileParse fp) throws IOException
102 * Calls the Jmol library to parse the PDB/mmCIF file, and then inspects the
103 * resulting object model to generate Jalview-style sequences, with secondary
104 * structure annotation added where available (i.e. where it has been computed
105 * by Jmol using DSSP).
107 * @see jalview.io.AlignFile#parse()
110 public void parse() throws IOException
112 setChains(new Vector<PDBChain>());
113 Viewer jmolModel = getJmolData();
114 jmolModel.openReader(getDataName(), getDataName(), getReader());
115 waitForScript(jmolModel);
118 * Convert one or more Jmol Model objects to Jalview sequences
120 if (jmolModel.ms.mc > 0)
122 // ideally we do this
125 // setStructureFileType(jmolModel.evalString("show _fileType"));
126 // } catch (Exception q)
130 // instead, we distinguish .cif from non-.cif by filename
131 setStructureFileType(
132 getDataName().toLowerCase(Locale.ROOT).endsWith(".cif")
133 ? PDBEntry.Type.MMCIF.toString()
136 transformJmolModelToJalview(jmolModel.ms);
141 * create a headless jmol instance for dataprocessing
145 private Viewer getJmolData()
152 * params -o (output to sysout) -n (nodisplay) -x (exit when finished)
153 * see http://wiki.jmol.org/index.php/Jmol_Application
156 viewer = JalviewJmolBinding.getJmolData(this);
157 // ensure the 'new' (DSSP) not 'old' (Ramachandran) SS method is used
158 viewer.setBooleanProperty("defaultStructureDSSP", true);
159 } catch (ClassCastException x)
161 throw new Error(MessageManager.formatMessage(
162 "error.jmol_version_not_compatible_with_jalview_version",
164 { JmolViewer.getJmolVersion() }), x);
170 public static Regex getNewAlphafoldValidator()
172 Regex validator = new Regex("(AF-[A-Z]+[0-9]+[A-Z0-9]+-F1)");
173 validator.setIgnoreCase(true);
177 PDBEntry.Type jmolFiletype = null;
180 * resolve a jmol filetype string and update the jmolFiletype field
183 * @param jmolIdentifiedFileType
184 * @return true if filetype was identified as MMCIF, PDB
186 public boolean updateFileType(String jmolIdentifiedFileType)
188 if (jmolIdentifiedFileType == null
189 || jmolIdentifiedFileType.trim().equals(""))
193 if ("mmcif".equalsIgnoreCase(jmolIdentifiedFileType))
195 jmolFiletype = PDBEntry.Type.MMCIF;
198 if ("pdb".equalsIgnoreCase(jmolIdentifiedFileType))
200 jmolFiletype = PDBEntry.Type.PDB;
206 public void transformJmolModelToJalview(ModelSet ms) throws IOException
210 Regex alphaFold = getNewAlphafoldValidator();
212 List<SequenceI> rna = new ArrayList<SequenceI>();
213 List<SequenceI> prot = new ArrayList<SequenceI>();
215 String pdbId = (String) ms.getInfo(0, "title");
216 boolean isMMCIF = false;
217 String jmolFileType_String = (String) ms.getInfo(0, "fileType");
218 if (updateFileType(jmolFileType_String))
220 setStructureFileType(jmolFiletype.toString());
223 isMMCIF = PDBEntry.Type.MMCIF.equals(jmolFiletype);
227 setId(safeName(getDataName()));
228 setPDBIdAvailable(false);
233 setPDBIdAvailable(true);
234 alphaFoldModel = alphaFold.search(pdbId) && isMMCIF;
237 List<Atom> significantAtoms = convertSignificantAtoms(ms);
238 for (Atom tmpatom : significantAtoms)
240 if (tmpatom.resNumIns.trim().equals(lastID))
242 // phosphorylated protein - seen both CA and P..
245 tmpchain = findChain(tmpatom.chain);
246 if (tmpchain != null)
248 tmpchain.atoms.addElement(tmpatom);
252 AnnotationRowBuilder builder = null;
253 String tempFString = null;
254 if (isAlphafoldModel() || StructureImportSettings.TFType.PLDDT
255 .equals(getTemperatureFactorType()))
257 builder = new AlphaFoldAnnotationRowBuilder();
260 tmpchain = new PDBChain(getId(), tmpatom.chain, builder);
261 getChains().add(tmpchain);
262 tmpchain.atoms.addElement(tmpatom);
264 lastID = tmpatom.resNumIns.trim();
266 if (isParseImmediately())
268 // configure parsing settings from the static singleton
275 for (PDBChain chain : getChains())
277 SequenceI chainseq = postProcessChain(chain);
287 // look at local setting for adding secondary tructure
288 if (predictSecondaryStructure)
290 createAnnotation(chainseq, chain, ms.at);
293 if (isAlphafoldModel())
295 // TODO - work out how to handle different ways that pAE is provided
299 Console.info("retrieving pAE for " + pdbId);
300 Alignment al = new Alignment(prot.toArray(new SequenceI[0]));
301 EBIAlfaFold.retrieve_AlphaFold_pAE(pdbId, al, null);
302 if (al.getAlignmentAnnotation() != null)
304 for (AlignmentAnnotation alann : al.getAlignmentAnnotation())
306 annotations.add(alann);
310 } catch (Throwable t)
312 Console.error("Couldn't get the pAE for " + pdbId, t);
315 } catch (OutOfMemoryError er)
318 "OUT OF MEMORY LOADING TRANSFORMING JMOL MODEL TO JALVIEW MODEL");
319 throw new IOException(MessageManager
320 .getString("exception.outofmemory_loading_mmcif_file"));
324 public void setAlphafoldModel(boolean afm)
326 alphaFoldModel = afm;
329 private boolean isAlphafoldModel()
331 return alphaFoldModel;
334 private List<Atom> convertSignificantAtoms(ModelSet ms)
336 List<Atom> significantAtoms = new ArrayList<Atom>();
337 HashMap<String, org.jmol.modelset.Atom> chainTerMap = new HashMap<String, org.jmol.modelset.Atom>();
338 org.jmol.modelset.Atom prevAtom = null;
339 for (org.jmol.modelset.Atom atom : ms.at)
341 if (atom.getAtomName().equalsIgnoreCase("CA")
342 || atom.getAtomName().equalsIgnoreCase("P"))
344 if (!atomValidated(atom, prevAtom, chainTerMap))
348 Atom curAtom = new Atom(atom.x, atom.y, atom.z);
349 curAtom.atomIndex = atom.getIndex();
350 curAtom.chain = atom.getChainIDStr();
351 curAtom.insCode = atom.group.getInsertionCode() == '\000' ? ' '
352 : atom.group.getInsertionCode();
353 curAtom.name = atom.getAtomName();
354 curAtom.number = atom.getAtomNumber();
355 curAtom.resName = atom.getGroup3(true);
356 curAtom.resNumber = atom.getResno();
357 curAtom.occupancy = ms.occupancies != null
358 ? ms.occupancies[atom.getIndex()]
359 : Float.valueOf(atom.getOccupancy100());
360 String fmt = new Format("%4i").form(curAtom.resNumber);
361 curAtom.resNumIns = (fmt + curAtom.insCode);
362 curAtom.tfactor = atom.getBfactor100() / 100f;
364 // significantAtoms.add(curAtom);
365 // ignore atoms from subsequent models
366 if (!significantAtoms.contains(curAtom))
368 significantAtoms.add(curAtom);
373 return significantAtoms;
376 private boolean atomValidated(org.jmol.modelset.Atom curAtom,
377 org.jmol.modelset.Atom prevAtom,
378 HashMap<String, org.jmol.modelset.Atom> chainTerMap)
380 // System.out.println("Atom: " + curAtom.getAtomNumber()
381 // + " Last atom index " + curAtom.group.lastAtomIndex);
382 if (chainTerMap == null || prevAtom == null)
386 String curAtomChId = curAtom.getChainIDStr();
387 String prevAtomChId = prevAtom.getChainIDStr();
388 // new chain encoutered
389 if (!prevAtomChId.equals(curAtomChId))
391 // On chain switch add previous chain termination to xTerMap if not exists
392 if (!chainTerMap.containsKey(prevAtomChId))
394 chainTerMap.put(prevAtomChId, prevAtom);
396 // if current atom belongs to an already terminated chain and the resNum
397 // diff < 5 then mark as valid and update termination Atom
398 if (chainTerMap.containsKey(curAtomChId))
400 if (curAtom.getResno() < chainTerMap.get(curAtomChId).getResno())
404 if ((curAtom.getResno()
405 - chainTerMap.get(curAtomChId).getResno()) < 5)
407 chainTerMap.put(curAtomChId, curAtom);
413 // atom with previously terminated chain encountered
414 else if (chainTerMap.containsKey(curAtomChId))
416 if (curAtom.getResno() < chainTerMap.get(curAtomChId).getResno())
420 if ((curAtom.getResno()
421 - chainTerMap.get(curAtomChId).getResno()) < 5)
423 chainTerMap.put(curAtomChId, curAtom);
428 // HETATM with resNum jump > 2
429 return !(curAtom.isHetero()
430 && ((curAtom.getResno() - prevAtom.getResno()) > 2));
433 private void createAnnotation(SequenceI sequence, PDBChain chain,
434 org.jmol.modelset.Atom[] jmolAtoms)
436 char[] secstr = new char[sequence.getLength()];
437 char[] secstrcode = new char[sequence.getLength()];
439 // Ensure Residue size equals Seq size
440 if (chain.residues.size() != sequence.getLength())
445 for (Residue residue : chain.residues)
447 Atom repAtom = residue.getAtoms().get(0);
448 STR proteinStructureSubType = jmolAtoms[repAtom.atomIndex].group
449 .getProteinStructureSubType();
450 setSecondaryStructure(proteinStructureSubType, annotIndex, secstr,
454 addSecondaryStructureAnnotation(chain.pdbid, sequence, secstr,
455 secstrcode, chain.id, sequence.getStart());
459 * Helper method that adds an AlignmentAnnotation for secondary structure to
460 * the sequence, provided at least one secondary structure assignment has been
471 protected void addSecondaryStructureAnnotation(String modelTitle,
472 SequenceI sq, char[] secstr, char[] secstrcode, String chainId,
475 int length = sq.getLength();
476 boolean ssFound = false;
477 Annotation asecstr[] = new Annotation[length + firstResNum - 1];
478 for (int p = 0; p < length; p++)
480 if (secstr[p] >= 'A' && secstr[p] <= 'z')
484 asecstr[p] = new Annotation(null, null, secstrcode[p], Float.NaN);
486 } catch (Exception e)
488 // e.printStackTrace();
495 String mt = modelTitle == null ? getDataName() : modelTitle;
497 AlignmentAnnotation ann = new AlignmentAnnotation(
498 "Secondary Structure", "Secondary Structure for " + mt,
500 ann.belowAlignment = true;
502 ann.autoCalculated = false;
503 ann.setCalcId(getClass().getName());
504 ann.adjustForAlignment();
505 ann.validateRangeAndDisplay();
506 annotations.add(ann);
507 sq.addAlignmentAnnotation(ann);
511 private void waitForScript(Viewer jmd)
513 while (jmd.isScriptExecuting())
519 } catch (InterruptedException x)
526 * Convert Jmol's secondary structure code to Jalview's, and stored it in the
527 * secondary structure arrays at the given sequence position
529 * @param proteinStructureSubType
534 protected void setSecondaryStructure(STR proteinStructureSubType, int pos,
535 char[] secstr, char[] secstrcode)
537 switch (proteinStructureSubType)
556 switch (proteinStructureSubType)
562 secstrcode[pos] = 'H';
565 secstrcode[pos] = 'E';
573 * Convert any non-standard peptide codes to their standard code table
574 * equivalent. (Initial version only does Selenomethionine MSE->MET.)
576 * @param threeLetterCode
580 protected void replaceNonCanonicalResidue(String threeLetterCode,
583 String canonical = ResidueProperties
584 .getCanonicalAminoAcid(threeLetterCode);
585 if (canonical != null && !canonical.equalsIgnoreCase(threeLetterCode))
587 seq[pos] = ResidueProperties.getSingleCharacterCode(canonical);
592 * Not implemented - returns null
595 public String print(SequenceI[] seqs, boolean jvSuffix)
604 public void setCallbackFunction(String callbackType,
605 String callbackFunction)
610 public void notifyCallback(CBK cbType, Object[] data)
612 String strInfo = (data == null || data[1] == null ? null
613 : data[1].toString());
617 sendConsoleEcho(strInfo);
620 notifyScriptTermination((String) data[2],
621 ((Integer) data[3]).intValue());
624 String mystatus = (String) data[3];
625 if (mystatus.indexOf("Picked") >= 0
626 || mystatus.indexOf("Sequence") >= 0)
629 sendConsoleMessage(strInfo);
631 else if (mystatus.indexOf("Completed") >= 0)
633 sendConsoleEcho(strInfo.substring(strInfo.lastIndexOf(",") + 2,
634 strInfo.length() - 1));
638 sendConsoleMessage(data == null ? null : strInfo);
641 sendConsoleMessage(strInfo);
648 String lastConsoleEcho = "";
650 private void sendConsoleEcho(String string)
652 lastConsoleEcho += string;
653 lastConsoleEcho += "\n";
656 String lastConsoleMessage = "";
658 private void sendConsoleMessage(String string)
660 lastConsoleMessage += string;
661 lastConsoleMessage += "\n";
664 int lastScriptTermination = -1;
666 String lastScriptMessage = "";
668 private void notifyScriptTermination(String string, int intValue)
670 lastScriptMessage += string;
671 lastScriptMessage += "\n";
672 lastScriptTermination = intValue;
676 public boolean notifyEnabled(CBK callbackPick)
678 switch (callbackPick)
692 * Not implemented - returns null
695 public String eval(String strEval)
701 * Not implemented - returns null
704 public float[][] functionXY(String functionName, int x, int y)
710 * Not implemented - returns null
713 public float[][][] functionXYZ(String functionName, int nx, int ny,
720 * Not implemented - returns null
723 public String createImage(String fileName, String imageType,
724 Object text_or_bytes, int quality)
730 * Not implemented - returns null
733 public Map<String, Object> getRegistryInfo()
742 public void showUrl(String url)
747 * Not implemented - returns null
750 public int[] resizeInnerPanel(String data)
756 public Map<String, Object> getJSpecViewProperty(String arg0)
761 public boolean isPredictSecondaryStructure()
763 return predictSecondaryStructure;
766 public void setPredictSecondaryStructure(
767 boolean predictSecondaryStructure)
769 this.predictSecondaryStructure = predictSecondaryStructure;
772 public boolean isVisibleChainAnnotation()
774 return visibleChainAnnotation;
777 public void setVisibleChainAnnotation(boolean visibleChainAnnotation)
779 this.visibleChainAnnotation = visibleChainAnnotation;