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.AlignmentAnnotation.TFType;
44 import jalview.datamodel.Annotation;
45 import jalview.datamodel.PDBEntry;
46 import jalview.datamodel.SequenceI;
47 import jalview.datamodel.annotations.AlphaFoldAnnotationRowBuilder;
48 import jalview.datamodel.annotations.AnnotationRowBuilder;
49 import jalview.io.DataSourceType;
50 import jalview.io.FileParse;
51 import jalview.io.StructureFile;
52 import jalview.schemes.ResidueProperties;
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 AlignmentAnnotation.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()
255 || this.getTemperatureFactorType() == AlignmentAnnotation.TFType.PLDDT)
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 private boolean isAlphafoldModel()
326 return alphaFoldModel;
329 private List<Atom> convertSignificantAtoms(ModelSet ms)
331 List<Atom> significantAtoms = new ArrayList<Atom>();
332 HashMap<String, org.jmol.modelset.Atom> chainTerMap = new HashMap<String, org.jmol.modelset.Atom>();
333 org.jmol.modelset.Atom prevAtom = null;
334 for (org.jmol.modelset.Atom atom : ms.at)
336 if (atom.getAtomName().equalsIgnoreCase("CA")
337 || atom.getAtomName().equalsIgnoreCase("P"))
339 if (!atomValidated(atom, prevAtom, chainTerMap))
343 Atom curAtom = new Atom(atom.x, atom.y, atom.z);
344 curAtom.atomIndex = atom.getIndex();
345 curAtom.chain = atom.getChainIDStr();
346 curAtom.insCode = atom.group.getInsertionCode() == '\000' ? ' '
347 : atom.group.getInsertionCode();
348 curAtom.name = atom.getAtomName();
349 curAtom.number = atom.getAtomNumber();
350 curAtom.resName = atom.getGroup3(true);
351 curAtom.resNumber = atom.getResno();
352 curAtom.occupancy = ms.occupancies != null
353 ? ms.occupancies[atom.getIndex()]
354 : Float.valueOf(atom.getOccupancy100());
355 String fmt = new Format("%4i").form(curAtom.resNumber);
356 curAtom.resNumIns = (fmt + curAtom.insCode);
357 curAtom.tfactor = atom.getBfactor100() / 100f;
359 // significantAtoms.add(curAtom);
360 // ignore atoms from subsequent models
361 if (!significantAtoms.contains(curAtom))
363 significantAtoms.add(curAtom);
368 return significantAtoms;
371 private boolean atomValidated(org.jmol.modelset.Atom curAtom,
372 org.jmol.modelset.Atom prevAtom,
373 HashMap<String, org.jmol.modelset.Atom> chainTerMap)
375 // System.out.println("Atom: " + curAtom.getAtomNumber()
376 // + " Last atom index " + curAtom.group.lastAtomIndex);
377 if (chainTerMap == null || prevAtom == null)
381 String curAtomChId = curAtom.getChainIDStr();
382 String prevAtomChId = prevAtom.getChainIDStr();
383 // new chain encoutered
384 if (!prevAtomChId.equals(curAtomChId))
386 // On chain switch add previous chain termination to xTerMap if not exists
387 if (!chainTerMap.containsKey(prevAtomChId))
389 chainTerMap.put(prevAtomChId, prevAtom);
391 // if current atom belongs to an already terminated chain and the resNum
392 // diff < 5 then mark as valid and update termination Atom
393 if (chainTerMap.containsKey(curAtomChId))
395 if (curAtom.getResno() < chainTerMap.get(curAtomChId).getResno())
399 if ((curAtom.getResno()
400 - chainTerMap.get(curAtomChId).getResno()) < 5)
402 chainTerMap.put(curAtomChId, curAtom);
408 // atom with previously terminated chain encountered
409 else if (chainTerMap.containsKey(curAtomChId))
411 if (curAtom.getResno() < chainTerMap.get(curAtomChId).getResno())
415 if ((curAtom.getResno()
416 - chainTerMap.get(curAtomChId).getResno()) < 5)
418 chainTerMap.put(curAtomChId, curAtom);
423 // HETATM with resNum jump > 2
424 return !(curAtom.isHetero()
425 && ((curAtom.getResno() - prevAtom.getResno()) > 2));
428 private void createAnnotation(SequenceI sequence, PDBChain chain,
429 org.jmol.modelset.Atom[] jmolAtoms)
431 char[] secstr = new char[sequence.getLength()];
432 char[] secstrcode = new char[sequence.getLength()];
434 // Ensure Residue size equals Seq size
435 if (chain.residues.size() != sequence.getLength())
440 for (Residue residue : chain.residues)
442 Atom repAtom = residue.getAtoms().get(0);
443 STR proteinStructureSubType = jmolAtoms[repAtom.atomIndex].group
444 .getProteinStructureSubType();
445 setSecondaryStructure(proteinStructureSubType, annotIndex, secstr,
449 addSecondaryStructureAnnotation(chain.pdbid, sequence, secstr,
450 secstrcode, chain.id, sequence.getStart());
454 * Helper method that adds an AlignmentAnnotation for secondary structure to
455 * the sequence, provided at least one secondary structure assignment has been
466 protected void addSecondaryStructureAnnotation(String modelTitle,
467 SequenceI sq, char[] secstr, char[] secstrcode, String chainId,
470 int length = sq.getLength();
471 boolean ssFound = false;
472 Annotation asecstr[] = new Annotation[length + firstResNum - 1];
473 for (int p = 0; p < length; p++)
475 if (secstr[p] >= 'A' && secstr[p] <= 'z')
479 asecstr[p] = new Annotation(null, null, secstrcode[p], Float.NaN);
481 } catch (Exception e)
483 // e.printStackTrace();
490 String mt = modelTitle == null ? getDataName() : modelTitle;
492 AlignmentAnnotation ann = new AlignmentAnnotation(
493 "Secondary Structure", "Secondary Structure for " + mt,
495 ann.belowAlignment = true;
497 ann.autoCalculated = false;
498 ann.setCalcId(getClass().getName());
499 ann.adjustForAlignment();
500 ann.validateRangeAndDisplay();
501 annotations.add(ann);
502 sq.addAlignmentAnnotation(ann);
506 private void waitForScript(Viewer jmd)
508 while (jmd.isScriptExecuting())
514 } catch (InterruptedException x)
521 * Convert Jmol's secondary structure code to Jalview's, and stored it in the
522 * secondary structure arrays at the given sequence position
524 * @param proteinStructureSubType
529 protected void setSecondaryStructure(STR proteinStructureSubType, int pos,
530 char[] secstr, char[] secstrcode)
532 switch (proteinStructureSubType)
551 switch (proteinStructureSubType)
557 secstrcode[pos] = 'H';
560 secstrcode[pos] = 'E';
568 * Convert any non-standard peptide codes to their standard code table
569 * equivalent. (Initial version only does Selenomethionine MSE->MET.)
571 * @param threeLetterCode
575 protected void replaceNonCanonicalResidue(String threeLetterCode,
578 String canonical = ResidueProperties
579 .getCanonicalAminoAcid(threeLetterCode);
580 if (canonical != null && !canonical.equalsIgnoreCase(threeLetterCode))
582 seq[pos] = ResidueProperties.getSingleCharacterCode(canonical);
587 * Not implemented - returns null
590 public String print(SequenceI[] seqs, boolean jvSuffix)
599 public void setCallbackFunction(String callbackType,
600 String callbackFunction)
605 public void notifyCallback(CBK cbType, Object[] data)
607 String strInfo = (data == null || data[1] == null ? null
608 : data[1].toString());
612 sendConsoleEcho(strInfo);
615 notifyScriptTermination((String) data[2],
616 ((Integer) data[3]).intValue());
619 String mystatus = (String) data[3];
620 if (mystatus.indexOf("Picked") >= 0
621 || mystatus.indexOf("Sequence") >= 0)
624 sendConsoleMessage(strInfo);
626 else if (mystatus.indexOf("Completed") >= 0)
628 sendConsoleEcho(strInfo.substring(strInfo.lastIndexOf(",") + 2,
629 strInfo.length() - 1));
633 sendConsoleMessage(data == null ? null : strInfo);
636 sendConsoleMessage(strInfo);
643 String lastConsoleEcho = "";
645 private void sendConsoleEcho(String string)
647 lastConsoleEcho += string;
648 lastConsoleEcho += "\n";
651 String lastConsoleMessage = "";
653 private void sendConsoleMessage(String string)
655 lastConsoleMessage += string;
656 lastConsoleMessage += "\n";
659 int lastScriptTermination = -1;
661 String lastScriptMessage = "";
663 private void notifyScriptTermination(String string, int intValue)
665 lastScriptMessage += string;
666 lastScriptMessage += "\n";
667 lastScriptTermination = intValue;
671 public boolean notifyEnabled(CBK callbackPick)
673 switch (callbackPick)
687 * Not implemented - returns null
690 public String eval(String strEval)
696 * Not implemented - returns null
699 public float[][] functionXY(String functionName, int x, int y)
705 * Not implemented - returns null
708 public float[][][] functionXYZ(String functionName, int nx, int ny,
715 * Not implemented - returns null
718 public String createImage(String fileName, String imageType,
719 Object text_or_bytes, int quality)
725 * Not implemented - returns null
728 public Map<String, Object> getRegistryInfo()
737 public void showUrl(String url)
742 * Not implemented - returns null
745 public int[] resizeInnerPanel(String data)
751 public Map<String, Object> getJSpecViewProperty(String arg0)
756 public boolean isPredictSecondaryStructure()
758 return predictSecondaryStructure;
761 public void setPredictSecondaryStructure(
762 boolean predictSecondaryStructure)
764 this.predictSecondaryStructure = predictSecondaryStructure;
767 public boolean isVisibleChainAnnotation()
769 return visibleChainAnnotation;
772 public void setVisibleChainAnnotation(boolean visibleChainAnnotation)
774 this.visibleChainAnnotation = visibleChainAnnotation;