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.AtomSpec;
36 import jalview.structure.StructureListener;
37 import jalview.structure.StructureMapping;
38 import jalview.structure.StructureSelectionManager;
39 import jalview.structures.models.SequenceStructureBindingModel;
40 import jalview.util.MessageManager;
42 import java.awt.Color;
43 import java.awt.event.ComponentEvent;
45 import java.util.ArrayList;
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 Chimera viewer should ignore sequence
78 * colouring events from the structure manager because the GUI is still
81 private boolean loadingFinished = true;
84 * state flag used to check if the Chimera viewer's paint method can be called
86 private boolean finishedInit = false;
88 public boolean isFinishedInit()
93 public void setFinishedInit(boolean finishedInit)
95 this.finishedInit = finishedInit;
98 boolean allChainsSelected = false;
101 * when true, try to search the associated datamodel for sequences that are
102 * associated with any unknown structures in the Chimera view.
104 private boolean associateNewStructs = false;
106 List<String> atomsPicked = new ArrayList<String>();
108 public List<String> chainNames;
110 private Map<String, String> chainFile;
113 * array of target chains for sequences - tied to pdbentry and sequence[]
115 protected String[][] chains;
117 boolean colourBySequence = true;
119 StringBuffer eval = new StringBuffer();
121 public String fileLoadingError;
123 private Map<String, List<ChimeraModel>> chimmaps = new LinkedHashMap<String, List<ChimeraModel>>();
125 private List<String> mdlToFile = new ArrayList<String>();
128 * the default or current model displayed if the model cannot be identified
129 * from the selection message
137 boolean loadedInline;
139 public boolean openFile(PDBEntry pe)
141 String file = pe.getFile();
144 List<ChimeraModel> oldList = viewer.getModelList();
145 viewer.openModel(file, pe.getId(), ModelType.PDB_MODEL);
146 List<ChimeraModel> newList = viewer.getModelList();
147 if (oldList.size() < newList.size())
149 while (oldList.size() > 0)
154 chimmaps.put(file, newList);
155 for (ChimeraModel cm : newList)
157 while (mdlToFile.size() < 1 + cm.getModelNumber())
159 mdlToFile.add(new String(""));
161 mdlToFile.set(cm.getModelNumber(), file);
164 File fl = new File(file);
165 String protocol = AppletFormatAdapter.URL;
170 protocol = AppletFormatAdapter.FILE;
172 } catch (Exception e)
177 // Explicitly map to the filename used by Chimera ;
178 // pdbentry[pe].getFile(), protocol);
182 ssm.addStructureViewerListener(this);
183 // ssm.addSelectionListener(this);
184 FeatureRenderer fr = getFeatureRenderer(null);
193 } catch (Exception q)
195 log("Exception when trying to open model " + file + "\n"
203 * current set of model filenames loaded
205 String[] modelFileNames = null;
207 public PDBEntry[] pdbentry;
210 * datasource protocol for access to PDBEntrylatest
212 String protocol = null;
214 StringBuffer resetLastRes = new StringBuffer();
217 * sequences mapped to each pdbentry
219 public SequenceI[][] sequence;
221 public StructureSelectionManager ssm;
223 private List<String> lastReply;
225 public JalviewChimeraBinding(StructureSelectionManager ssm,
226 PDBEntry[] pdbentry, SequenceI[][] sequenceIs, String[][] chains,
230 this.sequence = sequenceIs;
231 this.chains = chains;
232 this.pdbentry = pdbentry;
233 this.protocol = protocol;
236 this.chains = new String[pdbentry.length][];
238 viewer = new ChimeraManager(
239 csm = new ext.edu.ucsf.rbvi.strucviz2.StructureManager(true));
242 public JalviewChimeraBinding(StructureSelectionManager ssm,
243 ChimeraManager viewer2)
247 csm = viewer.getStructureManager();
251 * Construct a title string for the viewer window based on the data Jalview
257 public String getViewerTitle(boolean verbose)
259 if (sequence == null || pdbentry == null || sequence.length < 1
260 || pdbentry.length < 1 || sequence[0].length < 1)
262 return ("Jalview Chimera Window");
264 // TODO: give a more informative title when multiple structures are
266 StringBuilder title = new StringBuilder(64);
267 title.append("Chimera view for " + sequence[0][0].getName() + ":"
268 + pdbentry[0].getId());
272 if (pdbentry[0].getProperty() != null)
274 if (pdbentry[0].getProperty().get("method") != null)
276 title.append(" Method: ");
277 title.append(pdbentry[0].getProperty().get("method"));
279 if (pdbentry[0].getProperty().get("chains") != null)
281 title.append(" Chain:");
282 title.append(pdbentry[0].getProperty().get("chains"));
286 return title.toString();
290 * prepare the view for a given set of models/chains. chainList contains
291 * strings of the form 'pdbfilename:Chaincode'
294 * list of chains to make visible
296 public void centerViewer(List<String> toshow)
298 StringBuilder cmd = new StringBuilder(64);
300 for (String lbl : toshow)
306 mlength = lbl.indexOf(":", p);
307 } while (p < mlength && mlength < (lbl.length() - 2));
308 // TODO: lookup each pdb id and recover proper model number for it.
309 cmd.append("#" + getModelNum(chainFile.get(lbl)) + "."
310 + lbl.substring(mlength + 1) + " or ");
312 if (cmd.length() > 0)
314 cmd.setLength(cmd.length() - 4);
316 String cmdstring = cmd.toString();
317 evalStateCommand("~display #*; ~ribbon #*; ribbon " + cmdstring
318 + ";focus " + cmdstring, false);
322 * Close down the Jalview viewer, and (optionally) the associate Chimera
325 public void closeViewer(boolean closeChimera)
327 ssm.removeStructureViewerListener(this, this.getPdbFile());
330 viewer.exitChimera();
334 releaseUIResources();
338 * called by JalviewChimerabinding after closeViewer is called - release any
339 * resources and references so they can be garbage collected.
341 protected abstract void releaseUIResources();
343 public void colourByChain()
345 colourBySequence = false;
346 evalStateCommand("rainbow chain", false);
349 public void colourByCharge()
351 colourBySequence = false;
353 "color white;color red ::ASP;color red ::GLU;color blue ::LYS;color blue ::ARG;color yellow ::CYS",
358 * superpose the structures associated with sequences in the alignment
359 * according to their corresponding positions.
361 public void superposeStructures(AlignmentI alignment)
363 superposeStructures(alignment, -1, null);
367 * superpose the structures associated with sequences in the alignment
368 * according to their corresponding positions. ded)
370 * @param refStructure
371 * - select which pdb file to use as reference (default is -1 - the
372 * first structure in the alignment)
374 public void superposeStructures(AlignmentI alignment, int refStructure)
376 superposeStructures(alignment, refStructure, null);
380 * superpose the structures associated with sequences in the alignment
381 * according to their corresponding positions. ded)
383 * @param refStructure
384 * - select which pdb file to use as reference (default is -1 - the
385 * first structure in the alignment)
389 public void superposeStructures(AlignmentI alignment, int refStructure,
390 ColumnSelection hiddenCols)
392 superposeStructures(new AlignmentI[]
393 { alignment }, new int[]
394 { refStructure }, new ColumnSelection[]
398 public void superposeStructures(AlignmentI[] _alignment,
399 int[] _refStructure, ColumnSelection[] _hiddenCols)
401 assert (_alignment.length == _refStructure.length && _alignment.length != _hiddenCols.length);
402 StringBuilder allComs = new StringBuilder(128); // Chimera superposition cmd
403 String[] files = getPdbFile();
404 // check to see if we are still waiting for Chimera files
405 long starttime = System.currentTimeMillis();
406 boolean waiting = true;
410 for (String file : files)
414 // HACK - in Jalview 2.8 this call may not be threadsafe so we catch
415 // every possible exception
416 StructureMapping[] sm = ssm.getMapping(file);
417 if (sm == null || sm.length == 0)
421 } catch (Exception x)
429 // we wait around for a reasonable time before we give up
431 && System.currentTimeMillis() < (10000 + 1000 * files.length + starttime));
435 .println("RUNTIME PROBLEM: Chimera seems to be taking a long time to process all the structures.");
439 StringBuffer selectioncom = new StringBuffer();
440 for (int a = 0; a < _alignment.length; a++)
442 int refStructure = _refStructure[a];
443 AlignmentI alignment = _alignment[a];
444 ColumnSelection hiddenCols = _hiddenCols[a];
446 && selectioncom.length() > 0
447 && !selectioncom.substring(selectioncom.length() - 1).equals(
450 selectioncom.append(" ");
452 // process this alignment
453 if (refStructure >= files.length)
455 System.err.println("Invalid reference structure value "
459 if (refStructure < -1)
464 boolean matched[] = new boolean[alignment.getWidth()];
465 for (int m = 0; m < matched.length; m++)
468 matched[m] = (hiddenCols != null) ? hiddenCols.isVisible(m) : true;
471 int commonrpositions[][] = new int[files.length][alignment.getWidth()];
472 String isel[] = new String[files.length];
473 String[] targetC = new String[files.length];
474 String[] chainNames = new String[files.length];
475 String[] atomSpec = new String[files.length];
476 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
478 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
479 // RACE CONDITION - getMapping only returns Jmol loaded filenames once
480 // Jmol callback has completed.
481 if (mapping == null || mapping.length < 1)
483 throw new Error(MessageManager.getString("error.implementation_error_chimera_getting_data"));
486 for (int s = 0; s < sequence[pdbfnum].length; s++)
488 for (int sp, m = 0; m < mapping.length; m++)
490 if (mapping[m].getSequence() == sequence[pdbfnum][s]
491 && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
493 if (refStructure == -1)
495 refStructure = pdbfnum;
497 SequenceI asp = alignment.getSequenceAt(sp);
498 for (int r = 0; r < matched.length; r++)
504 matched[r] = false; // assume this is not a good site
505 if (r >= asp.getLength())
510 if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
512 // no mapping to gaps in sequence
515 int t = asp.findPosition(r); // sequence position
516 int apos = mapping[m].getAtomNum(t);
517 int pos = mapping[m].getPDBResNum(t);
519 if (pos < 1 || pos == lastPos)
521 // can't align unmapped sequence
524 matched[r] = true; // this is a good ite
526 // just record this residue position
527 commonrpositions[pdbfnum][r] = pos;
529 // create model selection suffix
530 isel[pdbfnum] = "#" + pdbfnum;
531 if (mapping[m].getChain() == null
532 || mapping[m].getChain().trim().length() == 0)
534 targetC[pdbfnum] = "";
538 targetC[pdbfnum] = "." + mapping[m].getChain();
540 chainNames[pdbfnum] = mapping[m].getPdbId()
542 atomSpec[pdbfnum] = asp.getRNA() != null ? PHOSPHORUS : ALPHACARBON;
543 // move on to next pdb file
544 s = sequence[pdbfnum].length;
551 // TODO: consider bailing if nmatched less than 4 because superposition
554 // TODO: refactor superposable position search (above) from jmol selection
555 // construction (below)
557 String[] selcom = new String[files.length];
560 // generate select statements to select regions to superimpose structures
562 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
564 String chainCd = targetC[pdbfnum];
567 StringBuffer molsel = new StringBuffer();
568 for (int r = 0; r < matched.length; r++)
576 if (lpos != commonrpositions[pdbfnum][r] - 1)
581 molsel.append((run ? "" : ":") + lpos);
582 molsel.append(chainCd);
588 // continuous run - and lpos >-1
591 // at the beginning, so add dash
592 molsel.append(":" + lpos);
597 lpos = commonrpositions[pdbfnum][r];
598 // molsel.append(lpos);
601 // add final selection phrase
604 molsel.append((run ? "" : ":") + lpos);
605 molsel.append(chainCd);
606 // molsel.append("");
608 if (molsel.length() > 1)
610 selcom[pdbfnum] = molsel.toString();
611 selectioncom.append("#" + pdbfnum);
612 selectioncom.append(selcom[pdbfnum]);
613 selectioncom.append(" ");
614 if (pdbfnum < files.length - 1)
616 selectioncom.append("| ");
621 selcom[pdbfnum] = null;
625 StringBuilder command = new StringBuilder(256);
626 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
628 if (pdbfnum == refStructure || selcom[pdbfnum] == null
629 || selcom[refStructure] == null)
633 if (command.length() > 0)
639 * Form Chimera match command, from the 'new' structure to the
640 * 'reference' structure e.g. (residues 1-91, chain B/A, alphacarbons):
642 * match #1:1-91.B@CA #0:1-91.A@CA
645 * https://www.cgl.ucsf.edu/chimera/docs/UsersGuide/midas/match.html
647 command.append("match #" + pdbfnum /* +".1" */);
648 // TODO: handle sub-models
649 command.append(selcom[pdbfnum]);
650 command.append("@" + atomSpec[pdbfnum]);
651 command.append(" #" + refStructure /* +".1" */);
652 command.append(selcom[refStructure]);
653 command.append("@" + atomSpec[refStructure]);
655 if (selectioncom.length() > 0)
657 // TODO remove debug output
658 System.out.println("Select regions:\n" + selectioncom.toString());
660 .println("Superimpose command(s):\n" + command.toString());
661 allComs.append("~display all; chain @CA|P; ribbon "
662 + selectioncom.toString() + ";"+command.toString());
663 // selcom.append("; ribbons; ");
666 if (selectioncom.length() > 0)
667 {// finally, mark all regions that were superposed.
668 if (selectioncom.substring(selectioncom.length() - 1).equals("|"))
670 selectioncom.setLength(selectioncom.length() - 1);
672 System.out.println("Select regions:\n" + selectioncom.toString());
673 allComs.append("; ~display all; chain @CA|P; ribbon "
674 + selectioncom.toString() + "; focus");
675 // evalStateCommand("select *; backbone; select "+selcom.toString()+"; cartoons; center "+selcom.toString());
676 evalStateCommand(allComs.toString(), true /* false */);
681 private void checkLaunched()
683 if (!viewer.isChimeraLaunched())
685 viewer.launchChimera(csm.getChimeraPaths());
687 if (!viewer.isChimeraLaunched())
689 log("Failed to launch Chimera!");
694 * Answers true if the Chimera process is still running, false if ended or not
699 public boolean isChimeraRunning()
701 return viewer.isChimeraLaunched();
705 * Send a command to Chimera, and optionally log any responses.
710 public void evalStateCommand(final String command, boolean logResponse)
712 viewerCommandHistory(false);
714 if (lastCommand == null || !lastCommand.equals(command))
716 // Thread t = new Thread(new Runnable()
721 // trim command or it may never find a match in the replyLog!!
722 lastReply = viewer.sendChimeraCommand(command.trim(), logResponse);
723 if (debug && logResponse)
725 log("Response from command ('" + command + "') was:\n"
730 // TODO - use j7/8 thread management
734 // } catch (InterruptedException foo)
739 viewerCommandHistory(true);
740 lastCommand = command;
744 * colour any structures associated with sequences in the given alignment
745 * using the getFeatureRenderer() and getSequenceRenderer() renderers but only
746 * if colourBySequence is enabled.
748 public void colourBySequence(boolean showFeatures,
749 jalview.api.AlignmentViewPanel alignmentv)
751 if (!colourBySequence || !loadingFinished)
759 String[] files = getPdbFile();
761 SequenceRenderer sr = getSequenceRenderer(alignmentv);
763 FeatureRenderer fr = null;
766 fr = getFeatureRenderer(alignmentv);
768 AlignmentI alignment = alignmentv.getAlignment();
770 for (jalview.structure.StructureMappingcommandSet cpdbbyseq : ChimeraCommands
771 .getColourBySequenceCommand(ssm, files, sequence, sr, fr,
774 for (String cbyseq : cpdbbyseq.commands)
777 evalStateCommand(cbyseq, false);
783 private void waitForChimera()
785 while (viewer != null && viewer.isBusy())
789 } catch (InterruptedException q)
794 public boolean isColourBySequence()
796 return colourBySequence;
799 public void setColourBySequence(boolean colourBySequence)
801 this.colourBySequence = colourBySequence;
804 // End StructureListener
805 // //////////////////////////
807 public float[][] functionXY(String functionName, int x, int y)
812 public float[][][] functionXYZ(String functionName, int nx, int ny, int nz)
814 // TODO Auto-generated method stub
818 public Color getColour(int atomIndex, int pdbResNum, String chain,
821 if (getModelNum(pdbfile) < 0)
825 log("get model / residue colour attribute unimplemented");
830 * returns the current featureRenderer that should be used to colour the
837 public abstract FeatureRenderer getFeatureRenderer(
838 AlignmentViewPanel alignment);
841 * instruct the Jalview binding to update the pdbentries vector if necessary
842 * prior to matching the jmol view's contents to the list of structure files
843 * Jalview knows about.
845 public abstract void refreshPdbEntries();
847 private int getModelNum(String modelFileName)
849 String[] mfn = getPdbFile();
854 for (int i = 0; i < mfn.length; i++)
856 if (mfn[i].equalsIgnoreCase(modelFileName))
865 * map between index of model filename returned from getPdbFile and the first
866 * index of models from this file in the viewer. Note - this is not trimmed -
867 * use getPdbFile to get number of unique models.
869 private int _modelFileNameMap[];
871 // ////////////////////////////////
872 // /StructureListener
873 public synchronized String[] getPdbFile()
877 return new String[0];
879 // if (modelFileNames == null)
881 // Collection<ChimeraModel> chimodels = viewer.getChimeraModels();
882 // _modelFileNameMap = new int[chimodels.size()];
884 // for (ChimeraModel chimodel : chimodels)
886 // String mdlName = chimodel.getModelName();
888 // modelFileNames = new String[j];
889 // // System.arraycopy(mset, 0, modelFileNames, 0, j);
892 return chimmaps.keySet().toArray(
893 modelFileNames = new String[chimmaps.size()]);
897 * map from string to applet
899 public Map getRegistryInfo()
901 // TODO Auto-generated method stub
906 * returns the current sequenceRenderer that should be used to colour the
913 public abstract SequenceRenderer getSequenceRenderer(
914 AlignmentViewPanel alignment);
917 * Highlight the specified atom positions in the structure.
925 public void highlightAtoms(List<AtomSpec> atoms)
927 for (AtomSpec atom : atoms)
929 int pdbResNum = atom.getPdbResNum();
930 String chain = atom.getChain();
931 List<ChimeraModel> cms = chimmaps.get(atom.getPdbId());
934 int mdlNum = cms.get(0).getModelNumber();
936 viewerCommandHistory(false);
937 // viewer.stopListening();
938 if (resetLastRes.length() > 0)
941 eval.append(resetLastRes.toString() + ";");
944 eval.append("display "); // +modelNum
946 resetLastRes.setLength(0);
947 resetLastRes.append("~display ");
949 eval.append(" #" + (mdlNum));
950 resetLastRes.append(" #" + (mdlNum));
952 // complete select string
954 eval.append(":" + pdbResNum);
955 resetLastRes.append(":" + pdbResNum);
956 if (!chain.equals(" "))
958 eval.append("." + chain);
959 resetLastRes.append("." + chain);
962 viewer.sendChimeraCommand(eval.toString(), false);
963 viewerCommandHistory(true);
964 // viewer.startListening();
969 boolean debug = false;
971 private void log(String message)
973 System.err.println("## Chimera log: " + message);
976 private void viewerCommandHistory(boolean enable)
978 // log("(Not yet implemented) History "
979 // + ((debug || enable) ? "on" : "off"));
982 public void loadInline(String string)
986 // viewer.loadInline(strModel, isAppend);
988 // construct fake fullPathName and fileName so we can identify the file
990 // Then, construct pass a reader for the string to Jmol.
991 // ((org.jmol.Viewer.Viewer) viewer).loadModelFromFile(fullPathName,
992 // fileName, null, reader, false, null, null, 0);
993 // viewer.openStringInline(string);
994 log("cannot load inline in Chimera, yet");
997 public void mouseOverStructure(int atomIndex, String strInfo)
999 // function to parse a mouseOver event from Chimera
1002 int alocsep = strInfo.indexOf("^");
1003 int mdlSep = strInfo.indexOf("/");
1004 int chainSeparator = strInfo.indexOf(":"), chainSeparator1 = -1;
1006 if (chainSeparator == -1)
1008 chainSeparator = strInfo.indexOf(".");
1009 if (mdlSep > -1 && mdlSep < chainSeparator)
1011 chainSeparator1 = chainSeparator;
1012 chainSeparator = mdlSep;
1015 // handle insertion codes
1018 pdbResNum = Integer.parseInt(strInfo.substring(
1019 strInfo.indexOf("]") + 1, alocsep));
1024 pdbResNum = Integer.parseInt(strInfo.substring(
1025 strInfo.indexOf("]") + 1, chainSeparator));
1029 if (strInfo.indexOf(":") > -1)
1031 chainId = strInfo.substring(strInfo.indexOf(":") + 1,
1032 strInfo.indexOf("."));
1039 String pdbfilename = modelFileNames[frameNo]; // default is first or current
1043 if (chainSeparator1 == -1)
1045 chainSeparator1 = strInfo.indexOf(".", mdlSep);
1047 String mdlId = (chainSeparator1 > -1) ? strInfo.substring(mdlSep + 1,
1048 chainSeparator1) : strInfo.substring(mdlSep + 1);
1051 // recover PDB filename for the model hovered over.
1052 int _mp = _modelFileNameMap.length - 1, mnumber = new Integer(mdlId)
1054 while (mnumber < _modelFileNameMap[_mp])
1058 pdbfilename = modelFileNames[_mp];
1059 if (pdbfilename == null)
1061 // pdbfilename = new File(viewer.getModelFileName(mnumber))
1062 // .getAbsolutePath();
1065 } catch (Exception e)
1070 if (lastMessage == null || !lastMessage.equals(strInfo))
1072 ssm.mouseOverStructure(pdbResNum, chainId, pdbfilename);
1075 lastMessage = strInfo;
1078 public void notifyAtomPicked(int atomIndex, String strInfo, String strData)
1081 * this implements the toggle label behaviour copied from the original
1082 * structure viewer, MCView
1084 if (strData != null)
1086 System.err.println("Ignoring additional pick data string " + strData);
1088 // rewrite these selections for chimera (DNA, RNA and protein)
1089 int chainSeparator = strInfo.indexOf(":");
1091 if (chainSeparator == -1)
1093 chainSeparator = strInfo.indexOf(".");
1096 String picked = strInfo.substring(strInfo.indexOf("]") + 1,
1098 String mdlString = "";
1099 if ((p = strInfo.indexOf(":")) > -1)
1101 picked += strInfo.substring(p + 1, strInfo.indexOf("."));
1104 if ((p = strInfo.indexOf("/")) > -1)
1106 mdlString += strInfo.substring(p, strInfo.indexOf(" #"));
1108 picked = "((" + picked + ".CA" + mdlString + ")|(" + picked + ".P"
1110 viewerCommandHistory(false);
1112 if (!atomsPicked.contains(picked))
1114 viewer.select(picked);
1115 atomsPicked.add(picked);
1119 viewer.select("not " + picked);
1120 atomsPicked.remove(picked);
1122 viewerCommandHistory(true);
1123 // TODO: in application this happens
1125 // if (scriptWindow != null)
1127 // scriptWindow.sendConsoleMessage(strInfo);
1128 // scriptWindow.sendConsoleMessage("\n");
1133 // incremented every time a load notification is successfully handled -
1134 // lightweight mechanism for other threads to detect when they can start
1135 // referring to new structures.
1136 private long loadNotifiesHandled = 0;
1138 public long getLoadNotifiesHandled()
1140 return loadNotifiesHandled;
1143 public void notifyFileLoaded(String fullPathName, String fileName2,
1144 String modelName, String errorMsg, int modelParts)
1146 if (errorMsg != null)
1148 fileLoadingError = errorMsg;
1152 // TODO: deal sensibly with models loaded inLine:
1153 // modelName will be null, as will fullPathName.
1155 // the rest of this routine ignores the arguments, and simply interrogates
1156 // the Jmol view to find out what structures it contains, and adds them to
1157 // the structure selection manager.
1158 fileLoadingError = null;
1159 String[] oldmodels = modelFileNames;
1160 modelFileNames = null;
1161 chainNames = new ArrayList<String>();
1162 chainFile = new HashMap<String, String>();
1163 boolean notifyLoaded = false;
1164 String[] modelfilenames = getPdbFile();
1165 // first check if we've lost any structures
1166 if (oldmodels != null && oldmodels.length > 0)
1169 for (int i = 0; i < oldmodels.length; i++)
1171 for (int n = 0; n < modelfilenames.length; n++)
1173 if (modelfilenames[n] == oldmodels[i])
1175 oldmodels[i] = null;
1179 if (oldmodels[i] != null)
1186 String[] oldmfn = new String[oldm];
1188 for (int i = 0; i < oldmodels.length; i++)
1190 if (oldmodels[i] != null)
1192 oldmfn[oldm++] = oldmodels[i];
1195 // deregister the Jmol instance for these structures - we'll add
1196 // ourselves again at the end for the current structure set.
1197 ssm.removeStructureViewerListener(this, oldmfn);
1201 // register ourselves as a listener and notify the gui that it needs to
1203 ssm.addStructureViewerListener(this);
1207 FeatureRenderer fr = getFeatureRenderer(null);
1213 loadNotifiesHandled++;
1215 setLoadingFromArchive(false);
1218 public void setJalviewColourScheme(ColourSchemeI cs)
1220 colourBySequence = false;
1229 // Chimera expects RBG values in the range 0-1
1230 final double normalise = 255D;
1231 viewerCommandHistory(false);
1232 // TODO: Switch between nucleotide or aa selection expressions
1233 StringBuilder command = new StringBuilder(128);
1234 command.append("color white;");
1235 for (String res : ResidueProperties.aa3Hash.keySet())
1237 index = ResidueProperties.aa3Hash.get(res).intValue();
1243 col = cs.findColour(ResidueProperties.aa[index].charAt(0));
1244 command.append("color " + col.getRed() / normalise + ","
1245 + col.getGreen() / normalise + "," + col.getBlue()
1246 / normalise + " ::" + res + ";");
1249 evalStateCommand(command.toString(),false);
1250 viewerCommandHistory(true);
1254 * called when the binding thinks the UI needs to be refreshed after a Chimera
1255 * state change. this could be because structures were loaded, or because an
1256 * error has occurred.
1258 public abstract void refreshGUI();
1260 public void componentResized(ComponentEvent e)
1265 public void componentMoved(ComponentEvent e)
1270 public void componentShown(ComponentEvent e)
1274 public void componentHidden(ComponentEvent e)
1278 public void setLoadingFromArchive(boolean loadingFromArchive)
1280 this.loadingFromArchive = loadingFromArchive;
1285 * @return true if Chimeral is still restoring state or loading is still going
1286 * on (see setFinsihedLoadingFromArchive)
1288 public boolean isLoadingFromArchive()
1290 return loadingFromArchive && !loadingFinished;
1294 * modify flag which controls if sequence colouring events are honoured by the
1295 * binding. Should be true for normal operation
1297 * @param finishedLoading
1299 public void setFinishedLoadingFromArchive(boolean finishedLoading)
1301 loadingFinished = finishedLoading;
1305 * Send the Chimera 'background solid <color>" command.
1308 * ://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/background
1312 public void setBackgroundColour(Color col)
1314 viewerCommandHistory(false);
1315 double normalise = 255D;
1316 final String command = "background solid " + col.getRed() / normalise + ","
1317 + col.getGreen() / normalise + "," + col.getBlue()
1319 viewer.sendChimeraCommand(command, false);
1320 viewerCommandHistory(true);
1324 * add structures and any known sequence associations
1326 * @returns the pdb entries added to the current set.
1328 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
1329 SequenceI[][] seq, String[][] chns)
1331 List<PDBEntry> v = new ArrayList<PDBEntry>();
1332 List<int[]> rtn = new ArrayList<int[]>();
1333 for (int i = 0; i < pdbentry.length; i++)
1337 for (int i = 0; i < pdbe.length; i++)
1339 int r = v.indexOf(pdbe[i]);
1340 if (r == -1 || r >= pdbentry.length)
1348 // just make sure the sequence/chain entries are all up to date
1349 addSequenceAndChain(r, seq[i], chns[i]);
1352 pdbe = v.toArray(new PDBEntry[v.size()]);
1356 // expand the tied sequence[] and string[] arrays
1357 SequenceI[][] sqs = new SequenceI[pdbentry.length][];
1358 String[][] sch = new String[pdbentry.length][];
1359 System.arraycopy(sequence, 0, sqs, 0, sequence.length);
1360 System.arraycopy(chains, 0, sch, 0, this.chains.length);
1363 pdbe = new PDBEntry[rtn.size()];
1364 for (int r = 0; r < pdbe.length; r++)
1366 int[] stri = (rtn.get(r));
1367 // record the pdb file as a new addition
1368 pdbe[r] = pdbentry[stri[0]];
1369 // and add the new sequence/chain entries
1370 addSequenceAndChain(stri[0], seq[stri[1]], chns[stri[1]]);
1381 * Adds sequences to the pe'th pdbentry's sequence set.
1386 public void addSequence(int pe, SequenceI[] seq)
1388 addSequenceAndChain(pe, seq, null);
1391 private void addSequenceAndChain(int pe, SequenceI[] seq, String[] tchain)
1393 if (pe < 0 || pe >= pdbentry.length)
1395 throw new Error(MessageManager.formatMessage(
1396 "error.implementation_error_no_pdbentry_from_index",
1398 { Integer.valueOf(pe).toString() }));
1400 final String nullChain = "TheNullChain";
1401 List<SequenceI> s = new ArrayList<SequenceI>();
1402 List<String> c = new ArrayList<String>();
1405 chains = new String[pdbentry.length][];
1407 if (sequence[pe] != null)
1409 for (int i = 0; i < sequence[pe].length; i++)
1411 s.add(sequence[pe][i]);
1412 if (chains[pe] != null)
1414 if (i < chains[pe].length)
1416 c.add(chains[pe][i]);
1425 if (tchain != null && tchain.length > 0)
1432 for (int i = 0; i < seq.length; i++)
1434 if (!s.contains(seq[i]))
1437 if (tchain != null && i < tchain.length)
1439 c.add(tchain[i] == null ? nullChain : tchain[i]);
1443 SequenceI[] tmp = s.toArray(new SequenceI[s.size()]);
1447 String[] tch = c.toArray(new String[c.size()]);
1448 for (int i = 0; i < tch.length; i++)
1450 if (tch[i] == nullChain)
1466 * @return text report of alignment between pdbfile and any associated
1467 * alignment sequences
1469 public String printMapping(String pdbfile)
1471 return ssm.printMapping(pdbfile);