2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.rbvi.chimera;
23 import jalview.api.AlignmentViewPanel;
24 import jalview.api.FeatureRenderer;
25 import jalview.api.SequenceRenderer;
26 import jalview.api.SequenceStructureBinding;
27 import jalview.api.StructureSelectionManagerProvider;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.ColumnSelection;
30 import jalview.datamodel.PDBEntry;
31 import jalview.datamodel.SequenceI;
32 import jalview.io.AppletFormatAdapter;
33 import jalview.schemes.ColourSchemeI;
34 import jalview.schemes.ResidueProperties;
35 import jalview.structure.StructureListener;
36 import jalview.structure.StructureMapping;
37 import jalview.structure.StructureSelectionManager;
38 import jalview.structures.models.SequenceStructureBindingModel;
39 import jalview.util.MessageManager;
41 import java.awt.Color;
42 import java.awt.event.ComponentEvent;
44 import java.util.ArrayList;
45 import java.util.Enumeration;
46 import java.util.HashMap;
47 import java.util.LinkedHashMap;
48 import java.util.List;
51 import ext.edu.ucsf.rbvi.strucviz2.ChimeraManager;
52 import ext.edu.ucsf.rbvi.strucviz2.ChimeraModel;
53 import ext.edu.ucsf.rbvi.strucviz2.StructureManager;
54 import ext.edu.ucsf.rbvi.strucviz2.StructureManager.ModelType;
56 public abstract class JalviewChimeraBinding extends
57 SequenceStructureBindingModel implements StructureListener,
58 SequenceStructureBinding, StructureSelectionManagerProvider
61 private static final String PHOSPHORUS = "P";
63 private static final String ALPHACARBON = "CA";
65 private StructureManager csm;
67 private ChimeraManager viewer;
70 * set if chimera state is being restored from some source - instructs binding
71 * not to apply default display style when structure set is updated for first
74 private boolean loadingFromArchive = false;
77 * second flag to indicate if the jmol viewer should ignore sequence colouring
78 * events from the structure manager because the GUI is still setting up
80 private boolean loadingFinished = true;
83 * state flag used to check if the Jmol viewer's paint method can be called
85 private boolean finishedInit = false;
87 public boolean isFinishedInit()
92 public void setFinishedInit(boolean finishedInit)
94 this.finishedInit = finishedInit;
97 boolean allChainsSelected = false;
100 * when true, try to search the associated datamodel for sequences that are
101 * associated with any unknown structures in the Chimera view.
103 private boolean associateNewStructs = false;
105 List<String> atomsPicked = new ArrayList<String>();
107 public List<String> chainNames;
109 private Map<String, String> chainFile;
112 * array of target chains for sequences - tied to pdbentry and sequence[]
114 protected String[][] chains;
116 boolean colourBySequence = true;
118 StringBuffer eval = new StringBuffer();
120 public String fileLoadingError;
122 private Map<String, List<ChimeraModel>> chimmaps = new LinkedHashMap<String, List<ChimeraModel>>();
124 private List<String> mdlToFile = new ArrayList<String>();
127 * the default or current model displayed if the model cannot be identified
128 * from the selection message
136 boolean loadedInline;
138 public boolean openFile(PDBEntry pe)
140 String file = pe.getFile();
143 List<ChimeraModel> oldList = viewer.getModelList();
144 viewer.openModel(file, pe.getId(), ModelType.PDB_MODEL);
145 List<ChimeraModel> newList = viewer.getModelList();
146 if (oldList.size() < newList.size())
148 while (oldList.size() > 0)
153 chimmaps.put(file, newList);
154 for (ChimeraModel cm : newList)
156 while (mdlToFile.size() < 1 + cm.getModelNumber())
158 mdlToFile.add(new String(""));
160 mdlToFile.set(cm.getModelNumber(), file);
163 File fl = new File(file);
164 String protocol = AppletFormatAdapter.URL;
169 protocol = AppletFormatAdapter.FILE;
171 } catch (Exception e)
176 // Explicitly map to the filename used by Jmol ;
177 // pdbentry[pe].getFile(), protocol);
181 ssm.addStructureViewerListener(this);
182 // ssm.addSelectionListener(this);
183 FeatureRenderer fr = getFeatureRenderer(null);
192 } catch (Exception q)
194 log("Exception when trying to open model " + file + "\n"
202 * current set of model filenames loaded
204 String[] modelFileNames = null;
206 public PDBEntry[] pdbentry;
209 * datasource protocol for access to PDBEntrylatest
211 String protocol = null;
213 StringBuffer resetLastRes = new StringBuffer();
216 * sequences mapped to each pdbentry
218 public SequenceI[][] sequence;
220 public StructureSelectionManager ssm;
222 private List<String> lastReply;
224 public JalviewChimeraBinding(StructureSelectionManager ssm,
225 PDBEntry[] pdbentry, SequenceI[][] sequenceIs, String[][] chains,
229 this.sequence = sequenceIs;
230 this.chains = chains;
231 this.pdbentry = pdbentry;
232 this.protocol = protocol;
235 this.chains = new String[pdbentry.length][];
237 viewer = new ChimeraManager(
238 csm = new ext.edu.ucsf.rbvi.strucviz2.StructureManager(true));
240 * viewer = JmolViewer.allocateViewer(renderPanel, new SmarterJmolAdapter(),
241 * "jalviewJmol", ap.av.applet .getDocumentBase(),
242 * ap.av.applet.getCodeBase(), "", this);
244 * jmolpopup = JmolPopup.newJmolPopup(viewer, true, "Jmol", true);
248 public JalviewChimeraBinding(StructureSelectionManager ssm,
249 ChimeraManager viewer2)
253 csm = viewer.getStructureManager();
257 * Construct a title string for the viewer window based on the data Jalview
263 public String getViewerTitle(boolean verbose)
265 if (sequence == null || pdbentry == null || sequence.length < 1
266 || pdbentry.length < 1 || sequence[0].length < 1)
268 return ("Jalview Chimera Window");
270 // TODO: give a more informative title when multiple structures are
272 StringBuilder title = new StringBuilder(64);
273 title.append("Chimera view for " + sequence[0][0].getName() + ":"
274 + pdbentry[0].getId());
278 if (pdbentry[0].getProperty() != null)
280 if (pdbentry[0].getProperty().get("method") != null)
282 title.append(" Method: ");
283 title.append(pdbentry[0].getProperty().get("method"));
285 if (pdbentry[0].getProperty().get("chains") != null)
287 title.append(" Chain:");
288 title.append(pdbentry[0].getProperty().get("chains"));
292 return title.toString();
296 * prepare the view for a given set of models/chains. chainList contains
297 * strings of the form 'pdbfilename:Chaincode'
300 * list of chains to make visible
302 public void centerViewer(List<String> toshow)
304 StringBuilder cmd = new StringBuilder(64);
306 for (String lbl : toshow)
312 mlength = lbl.indexOf(":", p);
313 } while (p < mlength && mlength < (lbl.length() - 2));
314 // TODO: lookup each pdb id and recover proper model number for it.
315 cmd.append("#" + getModelNum(chainFile.get(lbl)) + "."
316 + lbl.substring(mlength + 1) + " or ");
318 if (cmd.length() > 0)
320 cmd.setLength(cmd.length() - 4);
322 String cmdstring = cmd.toString();
323 evalStateCommand("~display #*; ~ribbon #*; ribbon " + cmdstring
324 + ";focus " + cmdstring, false);
328 * Close down the Jalview viewer, and (optionally) the associate Chimera
331 public void closeViewer(boolean closeChimera)
333 ssm.removeStructureViewerListener(this, this.getPdbFile());
336 viewer.exitChimera();
338 // viewer.evalStringQuiet("zap");
339 // viewer.setJmolStatusListener(null);
342 releaseUIResources();
346 * called by JalviewJmolbinding after closeViewer is called - release any
347 * resources and references so they can be garbage collected.
349 protected abstract void releaseUIResources();
351 public void colourByChain()
353 colourBySequence = false;
354 // TODO: colour by chain should colour each chain distinctly across all
356 // TODO: http://issues.jalview.org/browse/JAL-628
357 evalStateCommand("select *;color chain",false);
360 public void colourByCharge()
362 colourBySequence = false;
363 evalStateCommand("colour *;color white;select ASP,GLU;color red;"
364 + "select LYS,ARG;color blue;select CYS;color yellow", false);
368 * superpose the structures associated with sequences in the alignment
369 * according to their corresponding positions.
371 public void superposeStructures(AlignmentI alignment)
373 superposeStructures(alignment, -1, null);
377 * superpose the structures associated with sequences in the alignment
378 * according to their corresponding positions. ded)
380 * @param refStructure
381 * - select which pdb file to use as reference (default is -1 - the
382 * first structure in the alignment)
384 public void superposeStructures(AlignmentI alignment, int refStructure)
386 superposeStructures(alignment, refStructure, null);
390 * superpose the structures associated with sequences in the alignment
391 * according to their corresponding positions. ded)
393 * @param refStructure
394 * - select which pdb file to use as reference (default is -1 - the
395 * first structure in the alignment)
399 public void superposeStructures(AlignmentI alignment, int refStructure,
400 ColumnSelection hiddenCols)
402 superposeStructures(new AlignmentI[]
403 { alignment }, new int[]
404 { refStructure }, new ColumnSelection[]
408 public void superposeStructures(AlignmentI[] _alignment,
409 int[] _refStructure, ColumnSelection[] _hiddenCols)
411 assert (_alignment.length == _refStructure.length && _alignment.length != _hiddenCols.length);
412 StringBuilder allComs = new StringBuilder(128); // Chimera superposition cmd
413 String[] files = getPdbFile();
414 // check to see if we are still waiting for Jmol files
415 long starttime = System.currentTimeMillis();
416 boolean waiting = true;
420 for (String file : files)
424 // HACK - in Jalview 2.8 this call may not be threadsafe so we catch
425 // every possible exception
426 StructureMapping[] sm = ssm.getMapping(file);
427 if (sm == null || sm.length == 0)
431 } catch (Exception x)
439 // we wait around for a reasonable time before we give up
441 && System.currentTimeMillis() < (10000 + 1000 * files.length + starttime));
445 .println("RUNTIME PROBLEM: Chimera seems to be taking a long time to process all the structures.");
449 StringBuffer selectioncom = new StringBuffer();
450 for (int a = 0; a < _alignment.length; a++)
452 int refStructure = _refStructure[a];
453 AlignmentI alignment = _alignment[a];
454 ColumnSelection hiddenCols = _hiddenCols[a];
456 && selectioncom.length() > 0
457 && !selectioncom.substring(selectioncom.length() - 1).equals(
460 selectioncom.append(" ");
462 // process this alignment
463 if (refStructure >= files.length)
465 System.err.println("Invalid reference structure value "
469 if (refStructure < -1)
474 boolean matched[] = new boolean[alignment.getWidth()];
475 for (int m = 0; m < matched.length; m++)
478 matched[m] = (hiddenCols != null) ? hiddenCols.isVisible(m) : true;
481 int commonrpositions[][] = new int[files.length][alignment.getWidth()];
482 String isel[] = new String[files.length];
483 String[] targetC = new String[files.length];
484 String[] chainNames = new String[files.length];
485 String[] atomSpec = new String[files.length];
486 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
488 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
489 // RACE CONDITION - getMapping only returns Jmol loaded filenames once
490 // Jmol callback has completed.
491 if (mapping == null || mapping.length < 1)
493 throw new Error(MessageManager.getString("error.implementation_error_chimera_getting_data"));
496 for (int s = 0; s < sequence[pdbfnum].length; s++)
498 for (int sp, m = 0; m < mapping.length; m++)
500 if (mapping[m].getSequence() == sequence[pdbfnum][s]
501 && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
503 if (refStructure == -1)
505 refStructure = pdbfnum;
507 SequenceI asp = alignment.getSequenceAt(sp);
508 for (int r = 0; r < matched.length; r++)
514 matched[r] = false; // assume this is not a good site
515 if (r >= asp.getLength())
520 if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
522 // no mapping to gaps in sequence
525 int t = asp.findPosition(r); // sequence position
526 int apos = mapping[m].getAtomNum(t);
527 int pos = mapping[m].getPDBResNum(t);
529 if (pos < 1 || pos == lastPos)
531 // can't align unmapped sequence
534 matched[r] = true; // this is a good ite
536 // just record this residue position
537 commonrpositions[pdbfnum][r] = pos;
539 // create model selection suffix
540 isel[pdbfnum] = "#" + pdbfnum;
541 if (mapping[m].getChain() == null
542 || mapping[m].getChain().trim().length() == 0)
544 targetC[pdbfnum] = "";
548 targetC[pdbfnum] = "." + mapping[m].getChain();
550 chainNames[pdbfnum] = mapping[m].getPdbId()
552 atomSpec[pdbfnum] = asp.getRNA() != null ? PHOSPHORUS : ALPHACARBON;
553 // move on to next pdb file
554 s = sequence[pdbfnum].length;
561 // TODO: consider bailing if nmatched less than 4 because superposition
564 // TODO: refactor superposable position search (above) from jmol selection
565 // construction (below)
567 String[] selcom = new String[files.length];
570 // generate select statements to select regions to superimpose structures
572 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
574 String chainCd = targetC[pdbfnum];
577 StringBuffer molsel = new StringBuffer();
578 for (int r = 0; r < matched.length; r++)
586 if (lpos != commonrpositions[pdbfnum][r] - 1)
591 molsel.append((run ? "" : ":") + lpos);
592 molsel.append(chainCd);
598 // continuous run - and lpos >-1
601 // at the beginning, so add dash
602 molsel.append(":" + lpos);
607 lpos = commonrpositions[pdbfnum][r];
608 // molsel.append(lpos);
611 // add final selection phrase
614 molsel.append((run ? "" : ":") + lpos);
615 molsel.append(chainCd);
616 // molsel.append("");
618 if (molsel.length() > 1)
620 selcom[pdbfnum] = molsel.toString();
621 selectioncom.append("#" + pdbfnum);
622 selectioncom.append(selcom[pdbfnum]);
623 selectioncom.append(" ");
624 if (pdbfnum < files.length - 1)
626 selectioncom.append("| ");
631 selcom[pdbfnum] = null;
635 StringBuilder command = new StringBuilder(256);
636 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
638 if (pdbfnum == refStructure || selcom[pdbfnum] == null
639 || selcom[refStructure] == null)
643 if (command.length() > 0)
649 * Form Chimera match command, from the 'new' structure to the
650 * 'reference' structure e.g. (residues 1-91, chain B/A, alphacarbons):
652 * match #1:1-91.B@CA #0:1-91.A@CA
655 * https://www.cgl.ucsf.edu/chimera/docs/UsersGuide/midas/match.html
657 command.append("match #" + pdbfnum /* +".1" */);
658 // TODO: handle sub-models
659 command.append(selcom[pdbfnum]);
660 command.append("@" + atomSpec[pdbfnum]);
661 command.append(" #" + refStructure /* +".1" */);
662 command.append(selcom[refStructure]);
663 command.append("@" + atomSpec[refStructure]);
665 if (selectioncom.length() > 0)
667 // TODO remove debug output
668 System.out.println("Select regions:\n" + selectioncom.toString());
670 .println("Superimpose command(s):\n" + command.toString());
671 allComs.append("~display all; chain @CA|P; ribbon "
672 + selectioncom.toString() + ";"+command.toString());
673 // selcom.append("; ribbons; ");
676 if (selectioncom.length() > 0)
677 {// finally, mark all regions that were superposed.
678 if (selectioncom.substring(selectioncom.length() - 1).equals("|"))
680 selectioncom.setLength(selectioncom.length() - 1);
682 System.out.println("Select regions:\n" + selectioncom.toString());
683 allComs.append("; ~display all; chain @CA|P; ribbon "
684 + selectioncom.toString() + "; focus");
685 // evalStateCommand("select *; backbone; select "+selcom.toString()+"; cartoons; center "+selcom.toString());
686 evalStateCommand(allComs.toString(), true /* false */);
691 private void checkLaunched()
693 if (!viewer.isChimeraLaunched())
695 viewer.launchChimera(csm.getChimeraPaths());
697 if (!viewer.isChimeraLaunched())
699 log("Failed to launch Chimera!");
704 * Answers true if the Chimera process is still running, false if ended or not
709 public boolean isChimeraRunning()
711 return viewer.isChimeraLaunched();
715 * Send a command to Chimera, and optionally log any responses.
720 public void evalStateCommand(final String command, boolean logResponse)
722 viewerCommandHistory(false);
724 if (lastCommand == null || !lastCommand.equals(command))
726 // Thread t = new Thread(new Runnable()
731 // trim command or it may never find a match in the replyLog!!
732 lastReply = viewer.sendChimeraCommand(command.trim(), logResponse);
733 if (debug && logResponse)
735 log("Response from command ('" + command + "') was:\n"
740 // TODO - use j7/8 thread management
744 // } catch (InterruptedException foo)
749 viewerCommandHistory(true);
750 lastCommand = command;
754 * colour any structures associated with sequences in the given alignment
755 * using the getFeatureRenderer() and getSequenceRenderer() renderers but only
756 * if colourBySequence is enabled.
758 public void colourBySequence(boolean showFeatures,
759 jalview.api.AlignmentViewPanel alignmentv)
761 if (!colourBySequence || !loadingFinished)
769 String[] files = getPdbFile();
771 SequenceRenderer sr = getSequenceRenderer(alignmentv);
773 FeatureRenderer fr = null;
776 fr = getFeatureRenderer(alignmentv);
778 AlignmentI alignment = alignmentv.getAlignment();
780 for (jalview.structure.StructureMappingcommandSet cpdbbyseq : ChimeraCommands
781 .getColourBySequenceCommand(ssm, files, sequence, sr, fr,
784 for (String cbyseq : cpdbbyseq.commands)
787 evalStateCommand(cbyseq, false);
793 private void waitForChimera()
795 while (viewer.isBusy())
799 } catch (InterruptedException q)
804 public boolean isColourBySequence()
806 return colourBySequence;
809 public void setColourBySequence(boolean colourBySequence)
811 this.colourBySequence = colourBySequence;
814 // End StructureListener
815 // //////////////////////////
817 public float[][] functionXY(String functionName, int x, int y)
822 public float[][][] functionXYZ(String functionName, int nx, int ny, int nz)
824 // TODO Auto-generated method stub
828 public Color getColour(int atomIndex, int pdbResNum, String chain,
831 if (getModelNum(pdbfile) < 0)
835 log("get model / residue colour attribute unimplemented");
840 * returns the current featureRenderer that should be used to colour the
847 public abstract FeatureRenderer getFeatureRenderer(
848 AlignmentViewPanel alignment);
851 * instruct the Jalview binding to update the pdbentries vector if necessary
852 * prior to matching the jmol view's contents to the list of structure files
853 * Jalview knows about.
855 public abstract void refreshPdbEntries();
857 private int getModelNum(String modelFileName)
859 String[] mfn = getPdbFile();
864 for (int i = 0; i < mfn.length; i++)
866 if (mfn[i].equalsIgnoreCase(modelFileName))
875 * map between index of model filename returned from getPdbFile and the first
876 * index of models from this file in the viewer. Note - this is not trimmed -
877 * use getPdbFile to get number of unique models.
879 private int _modelFileNameMap[];
881 // ////////////////////////////////
882 // /StructureListener
883 public synchronized String[] getPdbFile()
887 return new String[0];
889 // if (modelFileNames == null)
891 // Collection<ChimeraModel> chimodels = viewer.getChimeraModels();
892 // _modelFileNameMap = new int[chimodels.size()];
894 // for (ChimeraModel chimodel : chimodels)
896 // String mdlName = chimodel.getModelName();
898 // modelFileNames = new String[j];
899 // // System.arraycopy(mset, 0, modelFileNames, 0, j);
902 return chimmaps.keySet().toArray(
903 modelFileNames = new String[chimmaps.size()]);
907 * map from string to applet
909 public Map getRegistryInfo()
911 // TODO Auto-generated method stub
916 * returns the current sequenceRenderer that should be used to colour the
923 public abstract SequenceRenderer getSequenceRenderer(
924 AlignmentViewPanel alignment);
927 public void highlightAtom(int atomIndex, int pdbResNum, String chain,
930 List<ChimeraModel> cms = chimmaps.get(pdbfile);
933 int mdlNum = cms.get(0).getModelNumber();
935 viewerCommandHistory(false);
936 // viewer.stopListening();
937 if (resetLastRes.length() > 0)
940 eval.append(resetLastRes.toString() + ";");
943 eval.append("display "); // +modelNum
945 resetLastRes.setLength(0);
946 resetLastRes.append("~display ");
948 eval.append(" #" + (mdlNum));
949 resetLastRes.append(" #" + (mdlNum));
951 // complete select string
953 eval.append(":" + pdbResNum);
954 resetLastRes.append(":" + pdbResNum);
955 if (!chain.equals(" "))
957 eval.append("." + chain);
958 resetLastRes.append("." + chain);
961 viewer.sendChimeraCommand(eval.toString(), false);
962 viewerCommandHistory(true);
963 // viewer.startListening();
967 boolean debug = true;
969 private void log(String message)
971 System.err.println("## Chimera log: " + message);
974 private void viewerCommandHistory(boolean enable)
976 log("(Not yet implemented) History "
977 + ((debug || enable) ? "on" : "off"));
980 public void loadInline(String string)
984 // viewer.loadInline(strModel, isAppend);
986 // construct fake fullPathName and fileName so we can identify the file
988 // Then, construct pass a reader for the string to Jmol.
989 // ((org.jmol.Viewer.Viewer) viewer).loadModelFromFile(fullPathName,
990 // fileName, null, reader, false, null, null, 0);
991 // viewer.openStringInline(string);
992 log("cannot load inline in Chimera, yet");
995 public void mouseOverStructure(int atomIndex, String strInfo)
997 // function to parse a mouseOver event from Chimera
1000 int alocsep = strInfo.indexOf("^");
1001 int mdlSep = strInfo.indexOf("/");
1002 int chainSeparator = strInfo.indexOf(":"), chainSeparator1 = -1;
1004 if (chainSeparator == -1)
1006 chainSeparator = strInfo.indexOf(".");
1007 if (mdlSep > -1 && mdlSep < chainSeparator)
1009 chainSeparator1 = chainSeparator;
1010 chainSeparator = mdlSep;
1013 // handle insertion codes
1016 pdbResNum = Integer.parseInt(strInfo.substring(
1017 strInfo.indexOf("]") + 1, alocsep));
1022 pdbResNum = Integer.parseInt(strInfo.substring(
1023 strInfo.indexOf("]") + 1, chainSeparator));
1027 if (strInfo.indexOf(":") > -1)
1029 chainId = strInfo.substring(strInfo.indexOf(":") + 1,
1030 strInfo.indexOf("."));
1037 String pdbfilename = modelFileNames[frameNo]; // default is first or current
1041 if (chainSeparator1 == -1)
1043 chainSeparator1 = strInfo.indexOf(".", mdlSep);
1045 String mdlId = (chainSeparator1 > -1) ? strInfo.substring(mdlSep + 1,
1046 chainSeparator1) : strInfo.substring(mdlSep + 1);
1049 // recover PDB filename for the model hovered over.
1050 int _mp = _modelFileNameMap.length - 1, mnumber = new Integer(mdlId)
1052 while (mnumber < _modelFileNameMap[_mp])
1056 pdbfilename = modelFileNames[_mp];
1057 if (pdbfilename == null)
1059 // pdbfilename = new File(viewer.getModelFileName(mnumber))
1060 // .getAbsolutePath();
1063 } catch (Exception e)
1068 if (lastMessage == null || !lastMessage.equals(strInfo))
1070 ssm.mouseOverStructure(pdbResNum, chainId, pdbfilename);
1073 lastMessage = strInfo;
1076 public void notifyAtomPicked(int atomIndex, String strInfo, String strData)
1079 * this implements the toggle label behaviour copied from the original
1080 * structure viewer, MCView
1082 if (strData != null)
1084 System.err.println("Ignoring additional pick data string " + strData);
1086 // rewrite these selections for chimera (DNA, RNA and protein)
1087 int chainSeparator = strInfo.indexOf(":");
1089 if (chainSeparator == -1)
1091 chainSeparator = strInfo.indexOf(".");
1094 String picked = strInfo.substring(strInfo.indexOf("]") + 1,
1096 String mdlString = "";
1097 if ((p = strInfo.indexOf(":")) > -1)
1099 picked += strInfo.substring(p + 1, strInfo.indexOf("."));
1102 if ((p = strInfo.indexOf("/")) > -1)
1104 mdlString += strInfo.substring(p, strInfo.indexOf(" #"));
1106 picked = "((" + picked + ".CA" + mdlString + ")|(" + picked + ".P"
1108 viewerCommandHistory(false);
1110 if (!atomsPicked.contains(picked))
1112 viewer.select(picked);
1113 atomsPicked.add(picked);
1117 viewer.select("not " + picked);
1118 atomsPicked.remove(picked);
1120 viewerCommandHistory(true);
1121 // TODO: in application this happens
1123 // if (scriptWindow != null)
1125 // scriptWindow.sendConsoleMessage(strInfo);
1126 // scriptWindow.sendConsoleMessage("\n");
1131 // incremented every time a load notification is successfully handled -
1132 // lightweight mechanism for other threads to detect when they can start
1133 // referring to new structures.
1134 private long loadNotifiesHandled = 0;
1136 public long getLoadNotifiesHandled()
1138 return loadNotifiesHandled;
1141 public void notifyFileLoaded(String fullPathName, String fileName2,
1142 String modelName, String errorMsg, int modelParts)
1144 if (errorMsg != null)
1146 fileLoadingError = errorMsg;
1150 // TODO: deal sensibly with models loaded inLine:
1151 // modelName will be null, as will fullPathName.
1153 // the rest of this routine ignores the arguments, and simply interrogates
1154 // the Jmol view to find out what structures it contains, and adds them to
1155 // the structure selection manager.
1156 fileLoadingError = null;
1157 String[] oldmodels = modelFileNames;
1158 modelFileNames = null;
1159 chainNames = new ArrayList<String>();
1160 chainFile = new HashMap<String, String>();
1161 boolean notifyLoaded = false;
1162 String[] modelfilenames = getPdbFile();
1163 // first check if we've lost any structures
1164 if (oldmodels != null && oldmodels.length > 0)
1167 for (int i = 0; i < oldmodels.length; i++)
1169 for (int n = 0; n < modelfilenames.length; n++)
1171 if (modelfilenames[n] == oldmodels[i])
1173 oldmodels[i] = null;
1177 if (oldmodels[i] != null)
1184 String[] oldmfn = new String[oldm];
1186 for (int i = 0; i < oldmodels.length; i++)
1188 if (oldmodels[i] != null)
1190 oldmfn[oldm++] = oldmodels[i];
1193 // deregister the Jmol instance for these structures - we'll add
1194 // ourselves again at the end for the current structure set.
1195 ssm.removeStructureViewerListener(this, oldmfn);
1199 // register ourselves as a listener and notify the gui that it needs to
1201 ssm.addStructureViewerListener(this);
1205 FeatureRenderer fr = getFeatureRenderer(null);
1211 loadNotifiesHandled++;
1213 setLoadingFromArchive(false);
1216 public void setJalviewColourScheme(ColourSchemeI cs)
1218 colourBySequence = false;
1228 viewerCommandHistory(false);
1229 // TODO: Switch between nucleotide or aa selection expressions
1230 Enumeration en = ResidueProperties.aa3Hash.keys();
1231 StringBuffer command = new StringBuffer("select *;color white;");
1232 while (en.hasMoreElements())
1234 res = en.nextElement().toString();
1235 index = ((Integer) ResidueProperties.aa3Hash.get(res)).intValue();
1241 col = cs.findColour(ResidueProperties.aa[index].charAt(0));
1242 // TODO: need colour string function and res selection here
1243 command.append("select " + res + ";color[" + col.getRed() + ","
1244 + col.getGreen() + "," + col.getBlue() + "];");
1247 evalStateCommand(command.toString(),false);
1248 viewerCommandHistory(true);
1251 public void showHelp()
1254 showUrl("http://jmol.sourceforge.net/docs/JmolUserGuide/", "jmolHelp");
1258 * open the URL somehow
1262 public abstract void showUrl(String url, String target);
1265 * called when the binding thinks the UI needs to be refreshed after a Jmol
1266 * state change. this could be because structures were loaded, or because an
1267 * error has occured.
1269 public abstract void refreshGUI();
1271 public void componentResized(ComponentEvent e)
1276 public void componentMoved(ComponentEvent e)
1281 public void componentShown(ComponentEvent e)
1285 public void componentHidden(ComponentEvent e)
1289 public void setLoadingFromArchive(boolean loadingFromArchive)
1291 this.loadingFromArchive = loadingFromArchive;
1296 * @return true if Jmol is still restoring state or loading is still going on
1297 * (see setFinsihedLoadingFromArchive)
1299 public boolean isLoadingFromArchive()
1301 return loadingFromArchive && !loadingFinished;
1305 * modify flag which controls if sequence colouring events are honoured by the
1306 * binding. Should be true for normal operation
1308 * @param finishedLoading
1310 public void setFinishedLoadingFromArchive(boolean finishedLoading)
1312 loadingFinished = finishedLoading;
1315 public void setBackgroundColour(java.awt.Color col)
1317 viewerCommandHistory(false);
1318 // todo set background colour
1319 viewer.sendChimeraCommand(
1320 "background [" + col.getRed() + "," + col.getGreen() + ","
1321 + col.getBlue() + "];", false);
1322 viewerCommandHistory(true);
1326 * add structures and any known sequence associations
1328 * @returns the pdb entries added to the current set.
1330 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
1331 SequenceI[][] seq, String[][] chns)
1333 List<PDBEntry> v = new ArrayList<PDBEntry>();
1334 List<int[]> rtn = new ArrayList<int[]>();
1335 for (int i = 0; i < pdbentry.length; i++)
1339 for (int i = 0; i < pdbe.length; i++)
1341 int r = v.indexOf(pdbe[i]);
1342 if (r == -1 || r >= pdbentry.length)
1350 // just make sure the sequence/chain entries are all up to date
1351 addSequenceAndChain(r, seq[i], chns[i]);
1354 pdbe = v.toArray(new PDBEntry[v.size()]);
1358 // expand the tied sequence[] and string[] arrays
1359 SequenceI[][] sqs = new SequenceI[pdbentry.length][];
1360 String[][] sch = new String[pdbentry.length][];
1361 System.arraycopy(sequence, 0, sqs, 0, sequence.length);
1362 System.arraycopy(chains, 0, sch, 0, this.chains.length);
1365 pdbe = new PDBEntry[rtn.size()];
1366 for (int r = 0; r < pdbe.length; r++)
1368 int[] stri = (rtn.get(r));
1369 // record the pdb file as a new addition
1370 pdbe[r] = pdbentry[stri[0]];
1371 // and add the new sequence/chain entries
1372 addSequenceAndChain(stri[0], seq[stri[1]], chns[stri[1]]);
1383 * Adds sequences to the pe'th pdbentry's sequence set.
1388 public void addSequence(int pe, SequenceI[] seq)
1390 addSequenceAndChain(pe, seq, null);
1393 private void addSequenceAndChain(int pe, SequenceI[] seq, String[] tchain)
1395 if (pe < 0 || pe >= pdbentry.length)
1397 throw new Error(MessageManager.formatMessage(
1398 "error.implementation_error_no_pdbentry_from_index",
1400 { Integer.valueOf(pe).toString() }));
1402 final String nullChain = "TheNullChain";
1403 List<SequenceI> s = new ArrayList<SequenceI>();
1404 List<String> c = new ArrayList<String>();
1407 chains = new String[pdbentry.length][];
1409 if (sequence[pe] != null)
1411 for (int i = 0; i < sequence[pe].length; i++)
1413 s.add(sequence[pe][i]);
1414 if (chains[pe] != null)
1416 if (i < chains[pe].length)
1418 c.add(chains[pe][i]);
1427 if (tchain != null && tchain.length > 0)
1434 for (int i = 0; i < seq.length; i++)
1436 if (!s.contains(seq[i]))
1439 if (tchain != null && i < tchain.length)
1441 c.add(tchain[i] == null ? nullChain : tchain[i]);
1445 SequenceI[] tmp = s.toArray(new SequenceI[s.size()]);
1449 String[] tch = c.toArray(new String[c.size()]);
1450 for (int i = 0; i < tch.length; i++)
1452 if (tch[i] == nullChain)
1468 * @return text report of alignment between pdbfile and any associated
1469 * alignment sequences
1471 public String printMapping(String pdbfile)
1473 return ssm.printMapping(pdbfile);