1 package jalview.ext.rbvi.chimera;
3 import jalview.api.AlignmentViewPanel;
4 import jalview.api.FeatureRenderer;
5 import jalview.api.SequenceRenderer;
6 import jalview.bin.Cache;
7 import jalview.datamodel.AlignmentI;
8 import jalview.datamodel.ColumnSelection;
9 import jalview.datamodel.PDBEntry;
10 import jalview.datamodel.SequenceI;
11 import jalview.schemes.ColourSchemeI;
12 import jalview.schemes.ResidueProperties;
13 import jalview.structure.StructureMapping;
14 import jalview.structure.StructureMappingcommandSet;
15 import jalview.structure.StructureSelectionManager;
16 import jalview.structures.models.AAStructureBindingModel;
17 import jalview.util.Comparison;
18 import jalview.util.MessageManager;
20 import java.awt.Color;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.LinkedHashMap;
24 import java.util.List;
27 import ext.edu.ucsf.rbvi.strucviz2.ChimeraManager;
28 import ext.edu.ucsf.rbvi.strucviz2.ChimeraModel;
29 import ext.edu.ucsf.rbvi.strucviz2.StructureManager;
30 import ext.edu.ucsf.rbvi.strucviz2.StructureManager.ModelType;
32 public abstract class JalviewChimeraBinding extends AAStructureBindingModel
35 private static final boolean debug = false;
37 private static final String PHOSPHORUS = "P";
39 private static final String ALPHACARBON = "CA";
41 private StructureManager csm;
43 private ChimeraManager viewer;
46 * set if chimera state is being restored from some source - instructs binding
47 * not to apply default display style when structure set is updated for first
50 private boolean loadingFromArchive = false;
53 * flag to indicate if the Chimera viewer should ignore sequence colouring
54 * events from the structure manager because the GUI is still setting up
56 private boolean loadingFinished = true;
59 * state flag used to check if the Chimera viewer's paint method can be called
61 private boolean finishedInit = false;
63 private List<String> atomsPicked = new ArrayList<String>();
65 private List<String> chainNames;
67 private Map<String, String> chainFile;
69 public String fileLoadingError;
72 * Map of ChimeraModel objects keyed by PDB full local file name
74 private Map<String, List<ChimeraModel>> chimeraMaps = new LinkedHashMap<String, List<ChimeraModel>>();
77 * the default or current model displayed if the model cannot be identified
78 * from the selection message
80 private int frameNo = 0;
82 private String lastCommand;
84 private String lastMessage;
86 private boolean loadedInline;
89 * current set of model filenames loaded
91 String[] modelFileNames = null;
93 String lastMousedOverAtomSpec;
95 private List<String> lastReply;
98 * Open a PDB structure file in Chimera and set up mappings from Jalview.
100 * We check if the PDB model id is already loaded in Chimera, if so don't
101 * reopen it. This is the case if Chimera has opened a saved session file.
106 public boolean openFile(PDBEntry pe)
108 String file = pe.getFile();
111 List<ChimeraModel> modelsToMap = new ArrayList<ChimeraModel>();
112 List<ChimeraModel> oldList = viewer.getModelList();
113 boolean alreadyOpen = false;
116 * If Chimera already has this model, don't reopen it, but do remap it.
118 for (ChimeraModel open : oldList)
120 if (open.getModelName().equals(pe.getId()))
123 modelsToMap.add(open);
128 * If Chimera doesn't yet have this model, ask it to open it, and retrieve
129 * the model names added by Chimera.
133 viewer.openModel(file, pe.getId(), ModelType.PDB_MODEL);
134 modelsToMap = viewer.getModelList();
135 modelsToMap.removeAll(oldList);
138 chimeraMaps.put(file, modelsToMap);
140 if (getSsm() != null)
142 getSsm().addStructureViewerListener(this);
143 // ssm.addSelectionListener(this);
144 FeatureRenderer fr = getFeatureRenderer(null);
152 } catch (Exception q)
154 log("Exception when trying to open model " + file + "\n"
170 public JalviewChimeraBinding(StructureSelectionManager ssm,
171 PDBEntry[] pdbentry, SequenceI[][] sequenceIs, String[][] chains,
174 super(ssm, pdbentry, sequenceIs, chains, protocol);
175 viewer = new ChimeraManager(
176 csm = new ext.edu.ucsf.rbvi.strucviz2.StructureManager(true));
185 public JalviewChimeraBinding(StructureSelectionManager ssm,
186 ChimeraManager theViewer)
190 csm = viewer.getStructureManager();
194 * Construct a title string for the viewer window based on the data Jalview
200 public String getViewerTitle(boolean verbose)
202 return getViewerTitle("Chimera", verbose);
206 * prepare the view for a given set of models/chains. chainList contains
207 * strings of the form 'pdbfilename:Chaincode'
210 * list of chains to make visible
212 public void centerViewer(List<String> toshow)
214 StringBuilder cmd = new StringBuilder(64);
216 for (String lbl : toshow)
222 mlength = lbl.indexOf(":", p);
223 } while (p < mlength && mlength < (lbl.length() - 2));
224 // TODO: lookup each pdb id and recover proper model number for it.
225 cmd.append("#" + getModelNum(chainFile.get(lbl)) + "."
226 + lbl.substring(mlength + 1) + " or ");
228 if (cmd.length() > 0)
230 cmd.setLength(cmd.length() - 4);
232 String cmdstring = cmd.toString();
233 evalStateCommand("~display #*; ~ribbon #*; ribbon " + cmdstring
234 + ";focus " + cmdstring, false);
238 * Close down the Jalview viewer, and (optionally) the associated Chimera
241 public void closeViewer(boolean closeChimera)
243 getSsm().removeStructureViewerListener(this, this.getPdbFile());
246 viewer.exitChimera();
250 releaseUIResources();
253 public void colourByChain()
255 colourBySequence = false;
256 evalStateCommand("rainbow chain", false);
259 public void colourByCharge()
261 colourBySequence = false;
263 "color white;color red ::ASP;color red ::GLU;color blue ::LYS;color blue ::ARG;color yellow ::CYS",
268 * superpose the structures associated with sequences in the alignment
269 * according to their corresponding positions.
271 public void superposeStructures(AlignmentI alignment)
273 superposeStructures(alignment, -1, null);
277 * superpose the structures associated with sequences in the alignment
278 * according to their corresponding positions. ded)
280 * @param refStructure
281 * - select which pdb file to use as reference (default is -1 - the
282 * first structure in the alignment)
284 public void superposeStructures(AlignmentI alignment, int refStructure)
286 superposeStructures(alignment, refStructure, null);
290 * superpose the structures associated with sequences in the alignment
291 * according to their corresponding positions. ded)
293 * @param refStructure
294 * - select which pdb file to use as reference (default is -1 - the
295 * first structure in the alignment)
299 public void superposeStructures(AlignmentI alignment, int refStructure,
300 ColumnSelection hiddenCols)
302 superposeStructures(new AlignmentI[]
303 { alignment }, new int[]
304 { refStructure }, new ColumnSelection[]
308 public void superposeStructures(AlignmentI[] _alignment,
309 int[] _refStructure, ColumnSelection[] _hiddenCols)
311 assert (_alignment.length == _refStructure.length && _alignment.length != _hiddenCols.length);
312 StringBuilder allComs = new StringBuilder(128); // Chimera superposition cmd
313 String[] files = getPdbFile();
314 // check to see if we are still waiting for Chimera files
315 long starttime = System.currentTimeMillis();
316 boolean waiting = true;
320 for (String file : files)
324 // HACK - in Jalview 2.8 this call may not be threadsafe so we catch
325 // every possible exception
326 StructureMapping[] sm = getSsm().getMapping(file);
327 if (sm == null || sm.length == 0)
331 } catch (Exception x)
339 // we wait around for a reasonable time before we give up
341 && System.currentTimeMillis() < (10000 + 1000 * files.length + starttime));
345 .println("RUNTIME PROBLEM: Chimera seems to be taking a long time to process all the structures.");
349 StringBuffer selectioncom = new StringBuffer();
350 for (int a = 0; a < _alignment.length; a++)
352 int refStructure = _refStructure[a];
353 AlignmentI alignment = _alignment[a];
354 ColumnSelection hiddenCols = _hiddenCols[a];
356 && selectioncom.length() > 0
357 && !selectioncom.substring(selectioncom.length() - 1).equals(
360 selectioncom.append(" ");
362 // process this alignment
363 if (refStructure >= files.length)
365 System.err.println("Invalid reference structure value "
369 if (refStructure < -1)
374 boolean matched[] = new boolean[alignment.getWidth()];
375 for (int m = 0; m < matched.length; m++)
378 matched[m] = (hiddenCols != null) ? hiddenCols.isVisible(m) : true;
381 int commonrpositions[][] = new int[files.length][alignment.getWidth()];
382 String isel[] = new String[files.length];
383 String[] targetC = new String[files.length];
384 String[] chainNames = new String[files.length];
385 String[] atomSpec = new String[files.length];
386 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
388 StructureMapping[] mapping = getSsm().getMapping(files[pdbfnum]);
389 // RACE CONDITION - getMapping only returns Jmol loaded filenames once
390 // Jmol callback has completed.
391 if (mapping == null || mapping.length < 1)
393 throw new Error(MessageManager.getString("error.implementation_error_chimera_getting_data"));
396 final int seqCountForPdbFile = getSequence()[pdbfnum].length;
397 for (int s = 0; s < seqCountForPdbFile; s++)
399 for (int sp, m = 0; m < mapping.length; m++)
401 final SequenceI theSequence = getSequence()[pdbfnum][s];
402 if (mapping[m].getSequence() == theSequence
403 && (sp = alignment.findIndex(theSequence)) > -1)
405 if (refStructure == -1)
407 refStructure = pdbfnum;
409 SequenceI asp = alignment.getSequenceAt(sp);
410 for (int r = 0; r < matched.length; r++)
416 matched[r] = false; // assume this is not a good site
417 if (r >= asp.getLength())
422 if (Comparison.isGap(asp.getCharAt(r)))
424 // no mapping to gaps in sequence
427 int t = asp.findPosition(r); // sequence position
428 int apos = mapping[m].getAtomNum(t);
429 int pos = mapping[m].getPDBResNum(t);
431 if (pos < 1 || pos == lastPos)
433 // can't align unmapped sequence
436 matched[r] = true; // this is a good ite
438 // just record this residue position
439 commonrpositions[pdbfnum][r] = pos;
441 // create model selection suffix
442 isel[pdbfnum] = "#" + pdbfnum;
443 if (mapping[m].getChain() == null
444 || mapping[m].getChain().trim().length() == 0)
446 targetC[pdbfnum] = "";
450 targetC[pdbfnum] = "." + mapping[m].getChain();
452 chainNames[pdbfnum] = mapping[m].getPdbId()
454 atomSpec[pdbfnum] = asp.getRNA() != null ? PHOSPHORUS : ALPHACARBON;
455 // move on to next pdb file
456 s = seqCountForPdbFile;
463 // TODO: consider bailing if nmatched less than 4 because superposition
466 // TODO: refactor superposable position search (above) from jmol selection
467 // construction (below)
469 String[] selcom = new String[files.length];
472 // generate select statements to select regions to superimpose structures
474 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
476 String chainCd = targetC[pdbfnum];
479 StringBuffer molsel = new StringBuffer();
480 for (int r = 0; r < matched.length; r++)
488 if (lpos != commonrpositions[pdbfnum][r] - 1)
493 molsel.append((run ? "" : ":") + lpos);
494 molsel.append(chainCd);
500 // continuous run - and lpos >-1
503 // at the beginning, so add dash
504 molsel.append(":" + lpos);
509 lpos = commonrpositions[pdbfnum][r];
510 // molsel.append(lpos);
513 // add final selection phrase
516 molsel.append((run ? "" : ":") + lpos);
517 molsel.append(chainCd);
518 // molsel.append("");
520 if (molsel.length() > 1)
522 selcom[pdbfnum] = molsel.toString();
523 selectioncom.append("#" + pdbfnum);
524 selectioncom.append(selcom[pdbfnum]);
525 selectioncom.append(" ");
526 if (pdbfnum < files.length - 1)
528 selectioncom.append("| ");
533 selcom[pdbfnum] = null;
537 StringBuilder command = new StringBuilder(256);
538 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
540 if (pdbfnum == refStructure || selcom[pdbfnum] == null
541 || selcom[refStructure] == null)
545 if (command.length() > 0)
551 * Form Chimera match command, from the 'new' structure to the
552 * 'reference' structure e.g. (residues 1-91, chain B/A, alphacarbons):
554 * match #1:1-91.B@CA #0:1-91.A@CA
557 * https://www.cgl.ucsf.edu/chimera/docs/UsersGuide/midas/match.html
559 command.append("match #" + pdbfnum /* +".1" */);
560 // TODO: handle sub-models
561 command.append(selcom[pdbfnum]);
562 command.append("@" + atomSpec[pdbfnum]);
563 command.append(" #" + refStructure /* +".1" */);
564 command.append(selcom[refStructure]);
565 command.append("@" + atomSpec[refStructure]);
567 if (selectioncom.length() > 0)
571 System.out.println("Select regions:\n" + selectioncom.toString());
572 System.out.println("Superimpose command(s):\n"
573 + command.toString());
575 allComs.append("~display all; chain @CA|P; ribbon "
576 + selectioncom.toString() + ";"+command.toString());
577 // selcom.append("; ribbons; ");
580 if (selectioncom.length() > 0)
581 {// finally, mark all regions that were superposed.
582 if (selectioncom.substring(selectioncom.length() - 1).equals("|"))
584 selectioncom.setLength(selectioncom.length() - 1);
588 System.out.println("Select regions:\n" + selectioncom.toString());
590 allComs.append("; ~display all; chain @CA|P; ribbon "
591 + selectioncom.toString() + "; focus");
592 // evalStateCommand("select *; backbone; select "+selcom.toString()+"; cartoons; center "+selcom.toString());
593 evalStateCommand(allComs.toString(), true /* false */);
598 private void checkLaunched()
600 if (!viewer.isChimeraLaunched())
602 viewer.launchChimera(StructureManager.getChimeraPaths());
604 if (!viewer.isChimeraLaunched())
606 log("Failed to launch Chimera!");
611 * Answers true if the Chimera process is still running, false if ended or not
616 public boolean isChimeraRunning()
618 return viewer.isChimeraLaunched();
622 * Send a command to Chimera, launching it first if necessary, and optionally
628 public void evalStateCommand(final String command, boolean logResponse)
630 viewerCommandHistory(false);
632 if (lastCommand == null || !lastCommand.equals(command))
634 // trim command or it may never find a match in the replyLog!!
635 lastReply = viewer.sendChimeraCommand(command.trim(), logResponse);
636 if (debug && logResponse)
638 log("Response from command ('" + command + "') was:\n" + lastReply);
641 viewerCommandHistory(true);
642 lastCommand = command;
646 * colour any structures associated with sequences in the given alignment
647 * using the getFeatureRenderer() and getSequenceRenderer() renderers but only
648 * if colourBySequence is enabled.
650 public void colourBySequence(boolean showFeatures,
651 jalview.api.AlignmentViewPanel alignmentv)
653 if (!colourBySequence || !loadingFinished)
657 if (getSsm() == null)
661 String[] files = getPdbFile();
663 SequenceRenderer sr = getSequenceRenderer(alignmentv);
665 FeatureRenderer fr = null;
668 fr = getFeatureRenderer(alignmentv);
670 AlignmentI alignment = alignmentv.getAlignment();
672 for (jalview.structure.StructureMappingcommandSet cpdbbyseq : getColourBySequenceCommands(
673 files, sr, fr, alignment))
675 for (String command : cpdbbyseq.commands)
677 executeWhenReady(command);
689 protected StructureMappingcommandSet[] getColourBySequenceCommands(
690 String[] files, SequenceRenderer sr, FeatureRenderer fr,
691 AlignmentI alignment)
693 return ChimeraCommands.getColourBySequenceCommand(getSsm(), files,
694 getSequence(), sr, fr, alignment);
700 protected void executeWhenReady(String command)
703 evalStateCommand(command, false);
707 private void waitForChimera()
709 while (viewer != null && viewer.isBusy())
713 } catch (InterruptedException q)
720 // End StructureListener
721 // //////////////////////////
723 public Color getColour(int atomIndex, int pdbResNum, String chain,
726 if (getModelNum(pdbfile) < 0)
730 log("get model / residue colour attribute unimplemented");
735 * returns the current featureRenderer that should be used to colour the
742 public abstract FeatureRenderer getFeatureRenderer(
743 AlignmentViewPanel alignment);
746 * instruct the Jalview binding to update the pdbentries vector if necessary
747 * prior to matching the jmol view's contents to the list of structure files
748 * Jalview knows about.
750 public abstract void refreshPdbEntries();
752 private int getModelNum(String modelFileName)
754 String[] mfn = getPdbFile();
759 for (int i = 0; i < mfn.length; i++)
761 if (mfn[i].equalsIgnoreCase(modelFileName))
770 * map between index of model filename returned from getPdbFile and the first
771 * index of models from this file in the viewer. Note - this is not trimmed -
772 * use getPdbFile to get number of unique models.
774 private int _modelFileNameMap[];
776 // ////////////////////////////////
777 // /StructureListener
778 public synchronized String[] getPdbFile()
782 return new String[0];
784 // if (modelFileNames == null)
786 // Collection<ChimeraModel> chimodels = viewer.getChimeraModels();
787 // _modelFileNameMap = new int[chimodels.size()];
789 // for (ChimeraModel chimodel : chimodels)
791 // String mdlName = chimodel.getModelName();
793 // modelFileNames = new String[j];
794 // // System.arraycopy(mset, 0, modelFileNames, 0, j);
797 return chimeraMaps.keySet().toArray(
798 modelFileNames = new String[chimeraMaps.size()]);
802 * map from string to applet
804 public Map getRegistryInfo()
806 // TODO Auto-generated method stub
811 * returns the current sequenceRenderer that should be used to colour the
818 public abstract SequenceRenderer getSequenceRenderer(
819 AlignmentViewPanel alignment);
822 * Construct and send a command to highlight an atom.
825 * Done by generating a command like (to 'highlight' position 44)
826 * ~select #0:43.C;select #0:44.C
827 * Note this removes the selection from the previous position.
830 public void highlightAtom(int atomIndex, int pdbResNum, String chain,
833 List<ChimeraModel> cms = chimeraMaps.get(pdbfile);
836 StringBuilder sb = new StringBuilder();
837 sb.append(" #" + cms.get(0).getModelNumber());
838 sb.append(":" + pdbResNum);
839 if (!chain.equals(" "))
841 sb.append("." + chain);
843 String atomSpec = sb.toString();
845 StringBuilder command = new StringBuilder(32);
846 if (lastMousedOverAtomSpec != null)
848 command.append("~select " + lastMousedOverAtomSpec + ";");
850 viewerCommandHistory(false);
851 String cmd = command.toString();
852 cmd = "select " + atomSpec;
853 if (cmd.length() > 0)
855 viewer.sendChimeraCommand(cmd, false);
857 viewerCommandHistory(true);
858 this.lastMousedOverAtomSpec = atomSpec;
862 private void log(String message)
864 System.err.println("## Chimera log: " + message);
867 private void viewerCommandHistory(boolean enable)
869 // log("(Not yet implemented) History "
870 // + ((debug || enable) ? "on" : "off"));
873 public void loadInline(String string)
877 // viewer.loadInline(strModel, isAppend);
879 // construct fake fullPathName and fileName so we can identify the file
881 // Then, construct pass a reader for the string to Jmol.
882 // ((org.jmol.Viewer.Viewer) viewer).loadModelFromFile(fullPathName,
883 // fileName, null, reader, false, null, null, 0);
884 // viewer.openStringInline(string);
885 log("cannot load inline in Chimera, yet");
888 public void mouseOverStructure(int atomIndex, String strInfo)
890 // function to parse a mouseOver event from Chimera
893 int alocsep = strInfo.indexOf("^");
894 int mdlSep = strInfo.indexOf("/");
895 int chainSeparator = strInfo.indexOf(":"), chainSeparator1 = -1;
897 if (chainSeparator == -1)
899 chainSeparator = strInfo.indexOf(".");
900 if (mdlSep > -1 && mdlSep < chainSeparator)
902 chainSeparator1 = chainSeparator;
903 chainSeparator = mdlSep;
906 // handle insertion codes
909 pdbResNum = Integer.parseInt(strInfo.substring(
910 strInfo.indexOf("]") + 1, alocsep));
915 pdbResNum = Integer.parseInt(strInfo.substring(
916 strInfo.indexOf("]") + 1, chainSeparator));
920 if (strInfo.indexOf(":") > -1)
922 chainId = strInfo.substring(strInfo.indexOf(":") + 1,
923 strInfo.indexOf("."));
930 String pdbfilename = modelFileNames[frameNo]; // default is first or current
934 if (chainSeparator1 == -1)
936 chainSeparator1 = strInfo.indexOf(".", mdlSep);
938 String mdlId = (chainSeparator1 > -1) ? strInfo.substring(mdlSep + 1,
939 chainSeparator1) : strInfo.substring(mdlSep + 1);
942 // recover PDB filename for the model hovered over.
943 int _mp = _modelFileNameMap.length - 1, mnumber = new Integer(mdlId)
945 while (mnumber < _modelFileNameMap[_mp])
949 pdbfilename = modelFileNames[_mp];
950 if (pdbfilename == null)
952 // pdbfilename = new File(viewer.getModelFileName(mnumber))
953 // .getAbsolutePath();
956 } catch (Exception e)
961 if (lastMessage == null || !lastMessage.equals(strInfo))
963 getSsm().mouseOverStructure(pdbResNum, chainId, pdbfilename);
966 lastMessage = strInfo;
969 public void notifyAtomPicked(int atomIndex, String strInfo, String strData)
972 * this implements the toggle label behaviour copied from the original
973 * structure viewer, MCView
977 System.err.println("Ignoring additional pick data string " + strData);
979 // rewrite these selections for chimera (DNA, RNA and protein)
980 int chainSeparator = strInfo.indexOf(":");
982 if (chainSeparator == -1)
984 chainSeparator = strInfo.indexOf(".");
987 String picked = strInfo.substring(strInfo.indexOf("]") + 1,
989 String mdlString = "";
990 if ((p = strInfo.indexOf(":")) > -1)
992 picked += strInfo.substring(p + 1, strInfo.indexOf("."));
995 if ((p = strInfo.indexOf("/")) > -1)
997 mdlString += strInfo.substring(p, strInfo.indexOf(" #"));
999 picked = "((" + picked + ".CA" + mdlString + ")|(" + picked + ".P"
1001 viewerCommandHistory(false);
1003 if (!atomsPicked.contains(picked))
1005 viewer.select(picked);
1006 atomsPicked.add(picked);
1010 viewer.select("not " + picked);
1011 atomsPicked.remove(picked);
1013 viewerCommandHistory(true);
1014 // TODO: in application this happens
1016 // if (scriptWindow != null)
1018 // scriptWindow.sendConsoleMessage(strInfo);
1019 // scriptWindow.sendConsoleMessage("\n");
1024 // incremented every time a load notification is successfully handled -
1025 // lightweight mechanism for other threads to detect when they can start
1026 // referring to new structures.
1027 private long loadNotifiesHandled = 0;
1029 public long getLoadNotifiesHandled()
1031 return loadNotifiesHandled;
1034 public void notifyFileLoaded(String fullPathName, String fileName2,
1035 String modelName, String errorMsg, int modelParts)
1037 if (errorMsg != null)
1039 fileLoadingError = errorMsg;
1043 // TODO: deal sensibly with models loaded inLine:
1044 // modelName will be null, as will fullPathName.
1046 // the rest of this routine ignores the arguments, and simply interrogates
1047 // the Jmol view to find out what structures it contains, and adds them to
1048 // the structure selection manager.
1049 fileLoadingError = null;
1050 String[] oldmodels = modelFileNames;
1051 modelFileNames = null;
1052 chainNames = new ArrayList<String>();
1053 chainFile = new HashMap<String, String>();
1054 boolean notifyLoaded = false;
1055 String[] modelfilenames = getPdbFile();
1056 // first check if we've lost any structures
1057 if (oldmodels != null && oldmodels.length > 0)
1060 for (int i = 0; i < oldmodels.length; i++)
1062 for (int n = 0; n < modelfilenames.length; n++)
1064 if (modelfilenames[n] == oldmodels[i])
1066 oldmodels[i] = null;
1070 if (oldmodels[i] != null)
1077 String[] oldmfn = new String[oldm];
1079 for (int i = 0; i < oldmodels.length; i++)
1081 if (oldmodels[i] != null)
1083 oldmfn[oldm++] = oldmodels[i];
1086 // deregister the Jmol instance for these structures - we'll add
1087 // ourselves again at the end for the current structure set.
1088 getSsm().removeStructureViewerListener(this, oldmfn);
1092 // register ourselves as a listener and notify the gui that it needs to
1094 getSsm().addStructureViewerListener(this);
1098 FeatureRenderer fr = getFeatureRenderer(null);
1104 loadNotifiesHandled++;
1106 setLoadingFromArchive(false);
1109 public void setJalviewColourScheme(ColourSchemeI cs)
1111 colourBySequence = false;
1120 // Chimera expects RBG values in the range 0-1
1121 final double normalise = 255D;
1122 viewerCommandHistory(false);
1123 // TODO: Switch between nucleotide or aa selection expressions
1124 StringBuilder command = new StringBuilder(128);
1125 command.append("color white;");
1126 for (String res : ResidueProperties.aa3Hash.keySet())
1128 index = ResidueProperties.aa3Hash.get(res).intValue();
1134 col = cs.findColour(ResidueProperties.aa[index].charAt(0));
1135 command.append("color " + col.getRed() / normalise + ","
1136 + col.getGreen() / normalise + "," + col.getBlue()
1137 / normalise + " ::" + res + ";");
1140 evalStateCommand(command.toString(),false);
1141 viewerCommandHistory(true);
1145 * called when the binding thinks the UI needs to be refreshed after a Chimera
1146 * state change. this could be because structures were loaded, or because an
1147 * error has occurred.
1149 public abstract void refreshGUI();
1151 public void setLoadingFromArchive(boolean loadingFromArchive)
1153 this.loadingFromArchive = loadingFromArchive;
1158 * @return true if Chimeral is still restoring state or loading is still going
1159 * on (see setFinsihedLoadingFromArchive)
1161 public boolean isLoadingFromArchive()
1163 return loadingFromArchive && !loadingFinished;
1167 * modify flag which controls if sequence colouring events are honoured by the
1168 * binding. Should be true for normal operation
1170 * @param finishedLoading
1172 public void setFinishedLoadingFromArchive(boolean finishedLoading)
1174 loadingFinished = finishedLoading;
1178 * Send the Chimera 'background solid <color>" command.
1181 * ://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/background
1185 public void setBackgroundColour(Color col)
1187 viewerCommandHistory(false);
1188 double normalise = 255D;
1189 final String command = "background solid " + col.getRed() / normalise + ","
1190 + col.getGreen() / normalise + "," + col.getBlue()
1192 viewer.sendChimeraCommand(command, false);
1193 viewerCommandHistory(true);
1199 * @return text report of alignment between pdbfile and any associated
1200 * alignment sequences
1202 public String printMapping(String pdbfile)
1204 return getSsm().printMapping(pdbfile);
1208 * Ask Chimera to save its session to the given file. Returns true if
1209 * successful, else false.
1214 public boolean saveSession(String filepath)
1216 if (isChimeraRunning())
1218 List<String> reply = viewer.sendChimeraCommand("save " + filepath,
1220 if (reply.contains("Session written"))
1227 .error("Error saving Chimera session: " + reply.toString());
1234 * Ask Chimera to open a session file. Returns true if successful, else false.
1235 * The filename must have a .py extension for this command to work.
1240 public boolean openSession(String filepath)
1242 evalStateCommand("open " + filepath, true);
1243 // todo: test for failure - how?
1247 public boolean isFinishedInit()
1249 return finishedInit;
1252 public void setFinishedInit(boolean finishedInit)
1254 this.finishedInit = finishedInit;
1257 public List<String> getChainNames()