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.util.Locale;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.Annotation;
27 import jalview.datamodel.PDBEntry;
28 import jalview.datamodel.SequenceI;
29 import jalview.io.DataSourceType;
30 import jalview.io.FileParse;
31 import jalview.io.StructureFile;
32 import jalview.schemes.ResidueProperties;
33 import jalview.util.Format;
34 import jalview.util.MessageManager;
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.List;
41 import java.util.Vector;
43 import org.jmol.api.JmolStatusListener;
44 import org.jmol.api.JmolViewer;
45 import org.jmol.c.CBK;
46 import org.jmol.c.STR;
47 import org.jmol.modelset.ModelSet;
48 import org.jmol.viewer.Viewer;
50 import com.stevesoft.pat.Regex;
53 import mc_view.PDBChain;
54 import mc_view.Residue;
57 * Import and process files with Jmol for file like PDB, mmCIF
62 public class JmolParser extends StructureFile implements JmolStatusListener
66 private boolean alphaFoldModel;
68 public JmolParser(boolean immediate, Object inFile,
69 DataSourceType sourceType) throws IOException
71 // BH 2018 File or String for filename
72 super(immediate, inFile, sourceType);
75 public JmolParser(Object inFile, DataSourceType sourceType)
78 super(inFile, sourceType);
81 public JmolParser(FileParse fp) throws IOException
91 * Calls the Jmol library to parse the PDB/mmCIF file, and then inspects the
92 * resulting object model to generate Jalview-style sequences, with secondary
93 * structure annotation added where available (i.e. where it has been computed
94 * by Jmol using DSSP).
96 * @see jalview.io.AlignFile#parse()
99 public void parse() throws IOException
101 setChains(new Vector<PDBChain>());
102 Viewer jmolModel = getJmolData();
103 jmolModel.openReader(getDataName(), getDataName(), getReader());
104 waitForScript(jmolModel);
107 * Convert one or more Jmol Model objects to Jalview sequences
109 if (jmolModel.ms.mc > 0)
111 // ideally we do this
114 // setStructureFileType(jmolModel.evalString("show _fileType"));
115 // } catch (Exception q)
119 // instead, we distinguish .cif from non-.cif by filename
120 setStructureFileType(
121 getDataName().toLowerCase(Locale.ROOT).endsWith(".cif")
122 ? PDBEntry.Type.MMCIF.toString()
125 transformJmolModelToJalview(jmolModel.ms);
130 * create a headless jmol instance for dataprocessing
134 private Viewer getJmolData()
141 * params -o (output to sysout) -n (nodisplay) -x (exit when finished)
142 * see http://wiki.jmol.org/index.php/Jmol_Application
145 viewer = JalviewJmolBinding.getJmolData(this);
146 // ensure the 'new' (DSSP) not 'old' (Ramachandran) SS method is used
147 viewer.setBooleanProperty("defaultStructureDSSP", true);
148 } catch (ClassCastException x)
150 throw new Error(MessageManager.formatMessage(
151 "error.jmol_version_not_compatible_with_jalview_version",
153 { JmolViewer.getJmolVersion() }), x);
159 public static Regex getNewAlphafoldValidator()
161 Regex validator = new Regex("(AF-[A-Z]+[0-9]+[A-Z0-9]+-F1)");
162 validator.setIgnoreCase(true);
166 PDBEntry.Type jmolFiletype = null;
169 * resolve a jmol filetype string and update the jmolFiletype field
172 * @param jmolIdentifiedFileType
173 * @return true if filetype was identified as MMCIF, PDB
175 public boolean updateFileType(String jmolIdentifiedFileType)
177 if (jmolIdentifiedFileType == null
178 || jmolIdentifiedFileType.trim().equals(""))
182 if ("mmcif".equalsIgnoreCase(jmolIdentifiedFileType))
184 jmolFiletype = PDBEntry.Type.MMCIF;
187 if ("pdb".equalsIgnoreCase(jmolIdentifiedFileType))
189 jmolFiletype = PDBEntry.Type.PDB;
195 public void transformJmolModelToJalview(ModelSet ms) throws IOException
199 Regex alphaFold = getNewAlphafoldValidator();
201 List<SequenceI> rna = new ArrayList<SequenceI>();
202 List<SequenceI> prot = new ArrayList<SequenceI>();
204 String pdbId = (String) ms.getInfo(0, "title");
205 boolean isMMCIF = false;
206 String jmolFileType_String = (String) ms.getInfo(0, "fileType");
207 if (updateFileType(jmolFileType_String))
209 setStructureFileType(jmolFiletype.toString());
212 isMMCIF = PDBEntry.Type.MMCIF.equals(jmolFiletype);
216 setId(safeName(getDataName()));
217 setPDBIdAvailable(false);
222 setPDBIdAvailable(true);
223 alphaFoldModel = alphaFold.search(pdbId) && isMMCIF;
226 List<Atom> significantAtoms = convertSignificantAtoms(ms);
227 for (Atom tmpatom : significantAtoms)
229 if (tmpatom.resNumIns.trim().equals(lastID))
231 // phosphorylated protein - seen both CA and P..
234 tmpchain = findChain(tmpatom.chain);
235 if (tmpchain != null)
237 tmpchain.atoms.addElement(tmpatom);
241 String tempFString = null;
242 if (isAlphafoldModel())
244 tempFString = "Alphafold Reliability";
247 tmpchain = new PDBChain(getId(), tmpatom.chain, tempFString);
248 getChains().add(tmpchain);
249 tmpchain.atoms.addElement(tmpatom);
251 lastID = tmpatom.resNumIns.trim();
253 if (isParseImmediately())
255 // configure parsing settings from the static singleton
262 for (PDBChain chain : getChains())
264 SequenceI chainseq = postProcessChain(chain);
274 // look at local setting for adding secondary tructure
275 if (predictSecondaryStructure)
277 createAnnotation(chainseq, chain, ms.at);
280 } catch (OutOfMemoryError er)
283 "OUT OF MEMORY LOADING TRANSFORMING JMOL MODEL TO JALVIEW MODEL");
284 throw new IOException(MessageManager
285 .getString("exception.outofmemory_loading_mmcif_file"));
289 private boolean isAlphafoldModel()
291 return alphaFoldModel;
294 private List<Atom> convertSignificantAtoms(ModelSet ms)
296 List<Atom> significantAtoms = new ArrayList<Atom>();
297 HashMap<String, org.jmol.modelset.Atom> chainTerMap = new HashMap<String, org.jmol.modelset.Atom>();
298 org.jmol.modelset.Atom prevAtom = null;
299 for (org.jmol.modelset.Atom atom : ms.at)
301 if (atom.getAtomName().equalsIgnoreCase("CA")
302 || atom.getAtomName().equalsIgnoreCase("P"))
304 if (!atomValidated(atom, prevAtom, chainTerMap))
308 Atom curAtom = new Atom(atom.x, atom.y, atom.z);
309 curAtom.atomIndex = atom.getIndex();
310 curAtom.chain = atom.getChainIDStr();
311 curAtom.insCode = atom.group.getInsertionCode() == '\000' ? ' '
312 : atom.group.getInsertionCode();
313 curAtom.name = atom.getAtomName();
314 curAtom.number = atom.getAtomNumber();
315 curAtom.resName = atom.getGroup3(true);
316 curAtom.resNumber = atom.getResno();
317 curAtom.occupancy = ms.occupancies != null
318 ? ms.occupancies[atom.getIndex()]
319 : Float.valueOf(atom.getOccupancy100());
320 String fmt = new Format("%4i").form(curAtom.resNumber);
321 curAtom.resNumIns = (fmt + curAtom.insCode);
322 curAtom.tfactor = atom.getBfactor100() / 100f;
324 // significantAtoms.add(curAtom);
325 // ignore atoms from subsequent models
326 if (!significantAtoms.contains(curAtom))
328 significantAtoms.add(curAtom);
333 return significantAtoms;
336 private boolean atomValidated(org.jmol.modelset.Atom curAtom,
337 org.jmol.modelset.Atom prevAtom,
338 HashMap<String, org.jmol.modelset.Atom> chainTerMap)
340 // System.out.println("Atom: " + curAtom.getAtomNumber()
341 // + " Last atom index " + curAtom.group.lastAtomIndex);
342 if (chainTerMap == null || prevAtom == null)
346 String curAtomChId = curAtom.getChainIDStr();
347 String prevAtomChId = prevAtom.getChainIDStr();
348 // new chain encoutered
349 if (!prevAtomChId.equals(curAtomChId))
351 // On chain switch add previous chain termination to xTerMap if not exists
352 if (!chainTerMap.containsKey(prevAtomChId))
354 chainTerMap.put(prevAtomChId, prevAtom);
356 // if current atom belongs to an already terminated chain and the resNum
357 // diff < 5 then mark as valid and update termination Atom
358 if (chainTerMap.containsKey(curAtomChId))
360 if (curAtom.getResno() < chainTerMap.get(curAtomChId).getResno())
364 if ((curAtom.getResno()
365 - chainTerMap.get(curAtomChId).getResno()) < 5)
367 chainTerMap.put(curAtomChId, curAtom);
373 // atom with previously terminated chain encountered
374 else if (chainTerMap.containsKey(curAtomChId))
376 if (curAtom.getResno() < chainTerMap.get(curAtomChId).getResno())
380 if ((curAtom.getResno()
381 - chainTerMap.get(curAtomChId).getResno()) < 5)
383 chainTerMap.put(curAtomChId, curAtom);
388 // HETATM with resNum jump > 2
389 return !(curAtom.isHetero()
390 && ((curAtom.getResno() - prevAtom.getResno()) > 2));
393 private void createAnnotation(SequenceI sequence, PDBChain chain,
394 org.jmol.modelset.Atom[] jmolAtoms)
396 char[] secstr = new char[sequence.getLength()];
397 char[] secstrcode = new char[sequence.getLength()];
399 // Ensure Residue size equals Seq size
400 if (chain.residues.size() != sequence.getLength())
405 for (Residue residue : chain.residues)
407 Atom repAtom = residue.getAtoms().get(0);
408 STR proteinStructureSubType = jmolAtoms[repAtom.atomIndex].group
409 .getProteinStructureSubType();
410 setSecondaryStructure(proteinStructureSubType, annotIndex, secstr,
414 addSecondaryStructureAnnotation(chain.pdbid, sequence, secstr,
415 secstrcode, chain.id, sequence.getStart());
419 * Helper method that adds an AlignmentAnnotation for secondary structure to
420 * the sequence, provided at least one secondary structure assignment has been
431 protected void addSecondaryStructureAnnotation(String modelTitle,
432 SequenceI sq, char[] secstr, char[] secstrcode, String chainId,
435 int length = sq.getLength();
436 boolean ssFound = false;
437 Annotation asecstr[] = new Annotation[length + firstResNum - 1];
438 for (int p = 0; p < length; p++)
440 if (secstr[p] >= 'A' && secstr[p] <= 'z')
444 asecstr[p] = new Annotation(String.valueOf(secstr[p]), null,
445 secstrcode[p], Float.NaN);
447 } catch (Exception e)
449 // e.printStackTrace();
456 String mt = modelTitle == null ? getDataName() : modelTitle;
458 AlignmentAnnotation ann = new AlignmentAnnotation(
459 "Secondary Structure", "Secondary Structure for " + mt,
461 ann.belowAlignment = true;
463 ann.autoCalculated = false;
464 ann.setCalcId(getClass().getName());
465 ann.adjustForAlignment();
466 ann.validateRangeAndDisplay();
467 annotations.add(ann);
468 sq.addAlignmentAnnotation(ann);
472 private void waitForScript(Viewer jmd)
474 while (jmd.isScriptExecuting())
480 } catch (InterruptedException x)
487 * Convert Jmol's secondary structure code to Jalview's, and stored it in the
488 * secondary structure arrays at the given sequence position
490 * @param proteinStructureSubType
495 protected void setSecondaryStructure(STR proteinStructureSubType, int pos,
496 char[] secstr, char[] secstrcode)
498 switch (proteinStructureSubType)
517 switch (proteinStructureSubType)
523 secstrcode[pos] = 'H';
526 secstrcode[pos] = 'E';
534 * Convert any non-standard peptide codes to their standard code table
535 * equivalent. (Initial version only does Selenomethionine MSE->MET.)
537 * @param threeLetterCode
541 protected void replaceNonCanonicalResidue(String threeLetterCode,
544 String canonical = ResidueProperties
545 .getCanonicalAminoAcid(threeLetterCode);
546 if (canonical != null && !canonical.equalsIgnoreCase(threeLetterCode))
548 seq[pos] = ResidueProperties.getSingleCharacterCode(canonical);
553 * Not implemented - returns null
556 public String print(SequenceI[] seqs, boolean jvSuffix)
565 public void setCallbackFunction(String callbackType,
566 String callbackFunction)
571 public void notifyCallback(CBK cbType, Object[] data)
573 String strInfo = (data == null || data[1] == null ? null
574 : data[1].toString());
578 sendConsoleEcho(strInfo);
581 notifyScriptTermination((String) data[2],
582 ((Integer) data[3]).intValue());
585 String mystatus = (String) data[3];
586 if (mystatus.indexOf("Picked") >= 0
587 || mystatus.indexOf("Sequence") >= 0)
590 sendConsoleMessage(strInfo);
592 else if (mystatus.indexOf("Completed") >= 0)
594 sendConsoleEcho(strInfo.substring(strInfo.lastIndexOf(",") + 2,
595 strInfo.length() - 1));
599 sendConsoleMessage(data == null ? null : strInfo);
602 sendConsoleMessage(strInfo);
609 String lastConsoleEcho = "";
611 private void sendConsoleEcho(String string)
613 lastConsoleEcho += string;
614 lastConsoleEcho += "\n";
617 String lastConsoleMessage = "";
619 private void sendConsoleMessage(String string)
621 lastConsoleMessage += string;
622 lastConsoleMessage += "\n";
625 int lastScriptTermination = -1;
627 String lastScriptMessage = "";
629 private void notifyScriptTermination(String string, int intValue)
631 lastScriptMessage += string;
632 lastScriptMessage += "\n";
633 lastScriptTermination = intValue;
637 public boolean notifyEnabled(CBK callbackPick)
639 switch (callbackPick)
653 * Not implemented - returns null
656 public String eval(String strEval)
662 * Not implemented - returns null
665 public float[][] functionXY(String functionName, int x, int y)
671 * Not implemented - returns null
674 public float[][][] functionXYZ(String functionName, int nx, int ny,
681 * Not implemented - returns null
684 public String createImage(String fileName, String imageType,
685 Object text_or_bytes, int quality)
691 * Not implemented - returns null
694 public Map<String, Object> getRegistryInfo()
703 public void showUrl(String url)
708 * Not implemented - returns null
711 public int[] resizeInnerPanel(String data)
717 public Map<String, Object> getJSpecViewProperty(String arg0)
722 public boolean isPredictSecondaryStructure()
724 return predictSecondaryStructure;
727 public void setPredictSecondaryStructure(
728 boolean predictSecondaryStructure)
730 this.predictSecondaryStructure = predictSecondaryStructure;
733 public boolean isVisibleChainAnnotation()
735 return visibleChainAnnotation;
738 public void setVisibleChainAnnotation(boolean visibleChainAnnotation)
740 this.visibleChainAnnotation = visibleChainAnnotation;