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
62 private static final boolean debug = false;
64 private static final String PHOSPHORUS = "P";
66 private static final String ALPHACARBON = "CA";
68 private StructureManager csm;
70 private ChimeraManager viewer;
73 * set if chimera state is being restored from some source - instructs binding
74 * not to apply default display style when structure set is updated for first
77 private boolean loadingFromArchive = false;
80 * second flag to indicate if the Chimera viewer should ignore sequence
81 * colouring events from the structure manager because the GUI is still
84 private boolean loadingFinished = true;
87 * state flag used to check if the Chimera viewer's paint method can be called
89 private boolean finishedInit = false;
91 public boolean isFinishedInit()
96 public void setFinishedInit(boolean finishedInit)
98 this.finishedInit = finishedInit;
101 boolean allChainsSelected = false;
104 * when true, try to search the associated datamodel for sequences that are
105 * associated with any unknown structures in the Chimera view.
107 private boolean associateNewStructs = false;
109 List<String> atomsPicked = new ArrayList<String>();
111 public List<String> chainNames;
113 private Map<String, String> chainFile;
116 * array of target chains for sequences - tied to pdbentry and sequence[]
118 protected String[][] chains;
120 boolean colourBySequence = true;
122 StringBuffer eval = new StringBuffer();
124 public String fileLoadingError;
126 private Map<String, List<ChimeraModel>> chimmaps = new LinkedHashMap<String, List<ChimeraModel>>();
128 private List<String> mdlToFile = new ArrayList<String>();
131 * the default or current model displayed if the model cannot be identified
132 * from the selection message
140 boolean loadedInline;
142 public boolean openFile(PDBEntry pe)
144 String file = pe.getFile();
147 List<ChimeraModel> oldList = viewer.getModelList();
148 viewer.openModel(file, pe.getId(), ModelType.PDB_MODEL);
149 List<ChimeraModel> newList = viewer.getModelList();
150 if (oldList.size() < newList.size())
152 while (oldList.size() > 0)
157 chimmaps.put(file, newList);
158 for (ChimeraModel cm : newList)
160 while (mdlToFile.size() < 1 + cm.getModelNumber())
162 mdlToFile.add(new String(""));
164 mdlToFile.set(cm.getModelNumber(), file);
167 File fl = new File(file);
168 String protocol = AppletFormatAdapter.URL;
173 protocol = AppletFormatAdapter.FILE;
175 } catch (Exception e)
180 // Explicitly map to the filename used by Chimera ;
181 // pdbentry[pe].getFile(), protocol);
185 ssm.addStructureViewerListener(this);
186 // ssm.addSelectionListener(this);
187 FeatureRenderer fr = getFeatureRenderer(null);
196 } catch (Exception q)
198 log("Exception when trying to open model " + file + "\n"
206 * current set of model filenames loaded
208 String[] modelFileNames = null;
210 public PDBEntry[] pdbentry;
213 * datasource protocol for access to PDBEntrylatest
215 String protocol = null;
217 StringBuffer resetLastRes = new StringBuffer();
220 * sequences mapped to each pdbentry
222 public SequenceI[][] sequence;
224 public StructureSelectionManager ssm;
226 private List<String> lastReply;
228 public JalviewChimeraBinding(StructureSelectionManager ssm,
229 PDBEntry[] pdbentry, SequenceI[][] sequenceIs, String[][] chains,
233 this.sequence = sequenceIs;
234 this.chains = chains;
235 this.pdbentry = pdbentry;
236 this.protocol = protocol;
239 this.chains = new String[pdbentry.length][];
241 viewer = new ChimeraManager(
242 csm = new ext.edu.ucsf.rbvi.strucviz2.StructureManager(true));
245 public JalviewChimeraBinding(StructureSelectionManager ssm,
246 ChimeraManager viewer2)
250 csm = viewer.getStructureManager();
254 * Construct a title string for the viewer window based on the data Jalview
260 public String getViewerTitle(boolean verbose)
262 if (sequence == null || pdbentry == null || sequence.length < 1
263 || pdbentry.length < 1 || sequence[0].length < 1)
265 return ("Jalview Chimera Window");
267 // TODO: give a more informative title when multiple structures are
269 StringBuilder title = new StringBuilder(64);
270 title.append("Chimera view for " + sequence[0][0].getName() + ":"
271 + pdbentry[0].getId());
275 if (pdbentry[0].getProperty() != null)
277 if (pdbentry[0].getProperty().get("method") != null)
279 title.append(" Method: ");
280 title.append(pdbentry[0].getProperty().get("method"));
282 if (pdbentry[0].getProperty().get("chains") != null)
284 title.append(" Chain:");
285 title.append(pdbentry[0].getProperty().get("chains"));
289 return title.toString();
293 * prepare the view for a given set of models/chains. chainList contains
294 * strings of the form 'pdbfilename:Chaincode'
297 * list of chains to make visible
299 public void centerViewer(List<String> toshow)
301 StringBuilder cmd = new StringBuilder(64);
303 for (String lbl : toshow)
309 mlength = lbl.indexOf(":", p);
310 } while (p < mlength && mlength < (lbl.length() - 2));
311 // TODO: lookup each pdb id and recover proper model number for it.
312 cmd.append("#" + getModelNum(chainFile.get(lbl)) + "."
313 + lbl.substring(mlength + 1) + " or ");
315 if (cmd.length() > 0)
317 cmd.setLength(cmd.length() - 4);
319 String cmdstring = cmd.toString();
320 evalStateCommand("~display #*; ~ribbon #*; ribbon " + cmdstring
321 + ";focus " + cmdstring, false);
325 * Close down the Jalview viewer, and (optionally) the associate Chimera
328 public void closeViewer(boolean closeChimera)
330 ssm.removeStructureViewerListener(this, this.getPdbFile());
333 viewer.exitChimera();
337 releaseUIResources();
341 * called by JalviewChimerabinding after closeViewer is called - release any
342 * resources and references so they can be garbage collected.
344 protected abstract void releaseUIResources();
346 public void colourByChain()
348 colourBySequence = false;
349 evalStateCommand("rainbow chain", false);
352 public void colourByCharge()
354 colourBySequence = false;
356 "color white;color red ::ASP;color red ::GLU;color blue ::LYS;color blue ::ARG;color yellow ::CYS",
361 * superpose the structures associated with sequences in the alignment
362 * according to their corresponding positions.
364 public void superposeStructures(AlignmentI alignment)
366 superposeStructures(alignment, -1, null);
370 * superpose the structures associated with sequences in the alignment
371 * according to their corresponding positions. ded)
373 * @param refStructure
374 * - select which pdb file to use as reference (default is -1 - the
375 * first structure in the alignment)
377 public void superposeStructures(AlignmentI alignment, int refStructure)
379 superposeStructures(alignment, refStructure, null);
383 * superpose the structures associated with sequences in the alignment
384 * according to their corresponding positions. ded)
386 * @param refStructure
387 * - select which pdb file to use as reference (default is -1 - the
388 * first structure in the alignment)
392 public void superposeStructures(AlignmentI alignment, int refStructure,
393 ColumnSelection hiddenCols)
395 superposeStructures(new AlignmentI[]
396 { alignment }, new int[]
397 { refStructure }, new ColumnSelection[]
401 public void superposeStructures(AlignmentI[] _alignment,
402 int[] _refStructure, ColumnSelection[] _hiddenCols)
404 assert (_alignment.length == _refStructure.length && _alignment.length != _hiddenCols.length);
405 StringBuilder allComs = new StringBuilder(128); // Chimera superposition cmd
406 String[] files = getPdbFile();
407 // check to see if we are still waiting for Chimera files
408 long starttime = System.currentTimeMillis();
409 boolean waiting = true;
413 for (String file : files)
417 // HACK - in Jalview 2.8 this call may not be threadsafe so we catch
418 // every possible exception
419 StructureMapping[] sm = ssm.getMapping(file);
420 if (sm == null || sm.length == 0)
424 } catch (Exception x)
432 // we wait around for a reasonable time before we give up
434 && System.currentTimeMillis() < (10000 + 1000 * files.length + starttime));
438 .println("RUNTIME PROBLEM: Chimera seems to be taking a long time to process all the structures.");
442 StringBuffer selectioncom = new StringBuffer();
443 for (int a = 0; a < _alignment.length; a++)
445 int refStructure = _refStructure[a];
446 AlignmentI alignment = _alignment[a];
447 ColumnSelection hiddenCols = _hiddenCols[a];
449 && selectioncom.length() > 0
450 && !selectioncom.substring(selectioncom.length() - 1).equals(
453 selectioncom.append(" ");
455 // process this alignment
456 if (refStructure >= files.length)
458 System.err.println("Invalid reference structure value "
462 if (refStructure < -1)
467 boolean matched[] = new boolean[alignment.getWidth()];
468 for (int m = 0; m < matched.length; m++)
471 matched[m] = (hiddenCols != null) ? hiddenCols.isVisible(m) : true;
474 int commonrpositions[][] = new int[files.length][alignment.getWidth()];
475 String isel[] = new String[files.length];
476 String[] targetC = new String[files.length];
477 String[] chainNames = new String[files.length];
478 String[] atomSpec = new String[files.length];
479 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
481 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
482 // RACE CONDITION - getMapping only returns Jmol loaded filenames once
483 // Jmol callback has completed.
484 if (mapping == null || mapping.length < 1)
486 throw new Error(MessageManager.getString("error.implementation_error_chimera_getting_data"));
489 for (int s = 0; s < sequence[pdbfnum].length; s++)
491 for (int sp, m = 0; m < mapping.length; m++)
493 if (mapping[m].getSequence() == sequence[pdbfnum][s]
494 && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
496 if (refStructure == -1)
498 refStructure = pdbfnum;
500 SequenceI asp = alignment.getSequenceAt(sp);
501 for (int r = 0; r < matched.length; r++)
507 matched[r] = false; // assume this is not a good site
508 if (r >= asp.getLength())
513 if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
515 // no mapping to gaps in sequence
518 int t = asp.findPosition(r); // sequence position
519 int apos = mapping[m].getAtomNum(t);
520 int pos = mapping[m].getPDBResNum(t);
522 if (pos < 1 || pos == lastPos)
524 // can't align unmapped sequence
527 matched[r] = true; // this is a good ite
529 // just record this residue position
530 commonrpositions[pdbfnum][r] = pos;
532 // create model selection suffix
533 isel[pdbfnum] = "#" + pdbfnum;
534 if (mapping[m].getChain() == null
535 || mapping[m].getChain().trim().length() == 0)
537 targetC[pdbfnum] = "";
541 targetC[pdbfnum] = "." + mapping[m].getChain();
543 chainNames[pdbfnum] = mapping[m].getPdbId()
545 atomSpec[pdbfnum] = asp.getRNA() != null ? PHOSPHORUS : ALPHACARBON;
546 // move on to next pdb file
547 s = sequence[pdbfnum].length;
554 // TODO: consider bailing if nmatched less than 4 because superposition
557 // TODO: refactor superposable position search (above) from jmol selection
558 // construction (below)
560 String[] selcom = new String[files.length];
563 // generate select statements to select regions to superimpose structures
565 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
567 String chainCd = targetC[pdbfnum];
570 StringBuffer molsel = new StringBuffer();
571 for (int r = 0; r < matched.length; r++)
579 if (lpos != commonrpositions[pdbfnum][r] - 1)
584 molsel.append((run ? "" : ":") + lpos);
585 molsel.append(chainCd);
591 // continuous run - and lpos >-1
594 // at the beginning, so add dash
595 molsel.append(":" + lpos);
600 lpos = commonrpositions[pdbfnum][r];
601 // molsel.append(lpos);
604 // add final selection phrase
607 molsel.append((run ? "" : ":") + lpos);
608 molsel.append(chainCd);
609 // molsel.append("");
611 if (molsel.length() > 1)
613 selcom[pdbfnum] = molsel.toString();
614 selectioncom.append("#" + pdbfnum);
615 selectioncom.append(selcom[pdbfnum]);
616 selectioncom.append(" ");
617 if (pdbfnum < files.length - 1)
619 selectioncom.append("| ");
624 selcom[pdbfnum] = null;
628 StringBuilder command = new StringBuilder(256);
629 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
631 if (pdbfnum == refStructure || selcom[pdbfnum] == null
632 || selcom[refStructure] == null)
636 if (command.length() > 0)
642 * Form Chimera match command, from the 'new' structure to the
643 * 'reference' structure e.g. (residues 1-91, chain B/A, alphacarbons):
645 * match #1:1-91.B@CA #0:1-91.A@CA
648 * https://www.cgl.ucsf.edu/chimera/docs/UsersGuide/midas/match.html
650 command.append("match #" + pdbfnum /* +".1" */);
651 // TODO: handle sub-models
652 command.append(selcom[pdbfnum]);
653 command.append("@" + atomSpec[pdbfnum]);
654 command.append(" #" + refStructure /* +".1" */);
655 command.append(selcom[refStructure]);
656 command.append("@" + atomSpec[refStructure]);
658 if (selectioncom.length() > 0)
662 System.out.println("Select regions:\n" + selectioncom.toString());
663 System.out.println("Superimpose command(s):\n"
664 + command.toString());
666 allComs.append("~display all; chain @CA|P; ribbon "
667 + selectioncom.toString() + ";"+command.toString());
668 // selcom.append("; ribbons; ");
671 if (selectioncom.length() > 0)
672 {// finally, mark all regions that were superposed.
673 if (selectioncom.substring(selectioncom.length() - 1).equals("|"))
675 selectioncom.setLength(selectioncom.length() - 1);
679 System.out.println("Select regions:\n" + selectioncom.toString());
681 allComs.append("; ~display all; chain @CA|P; ribbon "
682 + selectioncom.toString() + "; focus");
683 // evalStateCommand("select *; backbone; select "+selcom.toString()+"; cartoons; center "+selcom.toString());
684 evalStateCommand(allComs.toString(), true /* false */);
689 private void checkLaunched()
691 if (!viewer.isChimeraLaunched())
693 viewer.launchChimera(csm.getChimeraPaths());
695 if (!viewer.isChimeraLaunched())
697 log("Failed to launch Chimera!");
702 * Answers true if the Chimera process is still running, false if ended or not
707 public boolean isChimeraRunning()
709 return viewer.isChimeraLaunched();
713 * Send a command to Chimera, and optionally log any responses.
718 public void evalStateCommand(final String command, boolean logResponse)
720 viewerCommandHistory(false);
722 if (lastCommand == null || !lastCommand.equals(command))
724 // Thread t = new Thread(new Runnable()
729 // trim command or it may never find a match in the replyLog!!
730 lastReply = viewer.sendChimeraCommand(command.trim(), logResponse);
731 if (debug && logResponse)
733 log("Response from command ('" + command + "') was:\n"
738 // TODO - use j7/8 thread management
742 // } catch (InterruptedException foo)
747 viewerCommandHistory(true);
748 lastCommand = command;
752 * colour any structures associated with sequences in the given alignment
753 * using the getFeatureRenderer() and getSequenceRenderer() renderers but only
754 * if colourBySequence is enabled.
756 public void colourBySequence(boolean showFeatures,
757 jalview.api.AlignmentViewPanel alignmentv)
759 if (!colourBySequence || !loadingFinished)
767 String[] files = getPdbFile();
769 SequenceRenderer sr = getSequenceRenderer(alignmentv);
771 FeatureRenderer fr = null;
774 fr = getFeatureRenderer(alignmentv);
776 AlignmentI alignment = alignmentv.getAlignment();
778 for (jalview.structure.StructureMappingcommandSet cpdbbyseq : ChimeraCommands
779 .getColourBySequenceCommand(ssm, files, sequence, sr, fr,
782 for (String cbyseq : cpdbbyseq.commands)
785 evalStateCommand(cbyseq, false);
791 private void waitForChimera()
793 while (viewer != null && viewer.isBusy())
797 } catch (InterruptedException q)
802 public boolean isColourBySequence()
804 return colourBySequence;
807 public void setColourBySequence(boolean colourBySequence)
809 this.colourBySequence = colourBySequence;
812 // End StructureListener
813 // //////////////////////////
815 public float[][] functionXY(String functionName, int x, int y)
820 public float[][][] functionXYZ(String functionName, int nx, int ny, int nz)
822 // TODO Auto-generated method stub
826 public Color getColour(int atomIndex, int pdbResNum, String chain,
829 if (getModelNum(pdbfile) < 0)
833 log("get model / residue colour attribute unimplemented");
838 * returns the current featureRenderer that should be used to colour the
845 public abstract FeatureRenderer getFeatureRenderer(
846 AlignmentViewPanel alignment);
849 * instruct the Jalview binding to update the pdbentries vector if necessary
850 * prior to matching the jmol view's contents to the list of structure files
851 * Jalview knows about.
853 public abstract void refreshPdbEntries();
855 private int getModelNum(String modelFileName)
857 String[] mfn = getPdbFile();
862 for (int i = 0; i < mfn.length; i++)
864 if (mfn[i].equalsIgnoreCase(modelFileName))
873 * map between index of model filename returned from getPdbFile and the first
874 * index of models from this file in the viewer. Note - this is not trimmed -
875 * use getPdbFile to get number of unique models.
877 private int _modelFileNameMap[];
879 // ////////////////////////////////
880 // /StructureListener
881 public synchronized String[] getPdbFile()
885 return new String[0];
887 // if (modelFileNames == null)
889 // Collection<ChimeraModel> chimodels = viewer.getChimeraModels();
890 // _modelFileNameMap = new int[chimodels.size()];
892 // for (ChimeraModel chimodel : chimodels)
894 // String mdlName = chimodel.getModelName();
896 // modelFileNames = new String[j];
897 // // System.arraycopy(mset, 0, modelFileNames, 0, j);
900 return chimmaps.keySet().toArray(
901 modelFileNames = new String[chimmaps.size()]);
905 * map from string to applet
907 public Map getRegistryInfo()
909 // TODO Auto-generated method stub
914 * returns the current sequenceRenderer that should be used to colour the
921 public abstract SequenceRenderer getSequenceRenderer(
922 AlignmentViewPanel alignment);
925 public void highlightAtom(int atomIndex, int pdbResNum, String chain,
928 List<ChimeraModel> cms = chimmaps.get(pdbfile);
931 int mdlNum = cms.get(0).getModelNumber();
933 viewerCommandHistory(false);
934 // viewer.stopListening();
935 if (resetLastRes.length() > 0)
938 eval.append(resetLastRes.toString() + ";");
941 eval.append("display "); // +modelNum
943 resetLastRes.setLength(0);
944 resetLastRes.append("~display ");
946 eval.append(" #" + (mdlNum));
947 resetLastRes.append(" #" + (mdlNum));
949 // complete select string
951 eval.append(":" + pdbResNum);
952 resetLastRes.append(":" + pdbResNum);
953 if (!chain.equals(" "))
955 eval.append("." + chain);
956 resetLastRes.append("." + chain);
959 viewer.sendChimeraCommand(eval.toString(), false);
960 viewerCommandHistory(true);
961 // viewer.startListening();
965 private void log(String message)
967 System.err.println("## Chimera log: " + message);
970 private void viewerCommandHistory(boolean enable)
972 // log("(Not yet implemented) History "
973 // + ((debug || enable) ? "on" : "off"));
976 public void loadInline(String string)
980 // viewer.loadInline(strModel, isAppend);
982 // construct fake fullPathName and fileName so we can identify the file
984 // Then, construct pass a reader for the string to Jmol.
985 // ((org.jmol.Viewer.Viewer) viewer).loadModelFromFile(fullPathName,
986 // fileName, null, reader, false, null, null, 0);
987 // viewer.openStringInline(string);
988 log("cannot load inline in Chimera, yet");
991 public void mouseOverStructure(int atomIndex, String strInfo)
993 // function to parse a mouseOver event from Chimera
996 int alocsep = strInfo.indexOf("^");
997 int mdlSep = strInfo.indexOf("/");
998 int chainSeparator = strInfo.indexOf(":"), chainSeparator1 = -1;
1000 if (chainSeparator == -1)
1002 chainSeparator = strInfo.indexOf(".");
1003 if (mdlSep > -1 && mdlSep < chainSeparator)
1005 chainSeparator1 = chainSeparator;
1006 chainSeparator = mdlSep;
1009 // handle insertion codes
1012 pdbResNum = Integer.parseInt(strInfo.substring(
1013 strInfo.indexOf("]") + 1, alocsep));
1018 pdbResNum = Integer.parseInt(strInfo.substring(
1019 strInfo.indexOf("]") + 1, chainSeparator));
1023 if (strInfo.indexOf(":") > -1)
1025 chainId = strInfo.substring(strInfo.indexOf(":") + 1,
1026 strInfo.indexOf("."));
1033 String pdbfilename = modelFileNames[frameNo]; // default is first or current
1037 if (chainSeparator1 == -1)
1039 chainSeparator1 = strInfo.indexOf(".", mdlSep);
1041 String mdlId = (chainSeparator1 > -1) ? strInfo.substring(mdlSep + 1,
1042 chainSeparator1) : strInfo.substring(mdlSep + 1);
1045 // recover PDB filename for the model hovered over.
1046 int _mp = _modelFileNameMap.length - 1, mnumber = new Integer(mdlId)
1048 while (mnumber < _modelFileNameMap[_mp])
1052 pdbfilename = modelFileNames[_mp];
1053 if (pdbfilename == null)
1055 // pdbfilename = new File(viewer.getModelFileName(mnumber))
1056 // .getAbsolutePath();
1059 } catch (Exception e)
1064 if (lastMessage == null || !lastMessage.equals(strInfo))
1066 ssm.mouseOverStructure(pdbResNum, chainId, pdbfilename);
1069 lastMessage = strInfo;
1072 public void notifyAtomPicked(int atomIndex, String strInfo, String strData)
1075 * this implements the toggle label behaviour copied from the original
1076 * structure viewer, MCView
1078 if (strData != null)
1080 System.err.println("Ignoring additional pick data string " + strData);
1082 // rewrite these selections for chimera (DNA, RNA and protein)
1083 int chainSeparator = strInfo.indexOf(":");
1085 if (chainSeparator == -1)
1087 chainSeparator = strInfo.indexOf(".");
1090 String picked = strInfo.substring(strInfo.indexOf("]") + 1,
1092 String mdlString = "";
1093 if ((p = strInfo.indexOf(":")) > -1)
1095 picked += strInfo.substring(p + 1, strInfo.indexOf("."));
1098 if ((p = strInfo.indexOf("/")) > -1)
1100 mdlString += strInfo.substring(p, strInfo.indexOf(" #"));
1102 picked = "((" + picked + ".CA" + mdlString + ")|(" + picked + ".P"
1104 viewerCommandHistory(false);
1106 if (!atomsPicked.contains(picked))
1108 viewer.select(picked);
1109 atomsPicked.add(picked);
1113 viewer.select("not " + picked);
1114 atomsPicked.remove(picked);
1116 viewerCommandHistory(true);
1117 // TODO: in application this happens
1119 // if (scriptWindow != null)
1121 // scriptWindow.sendConsoleMessage(strInfo);
1122 // scriptWindow.sendConsoleMessage("\n");
1127 // incremented every time a load notification is successfully handled -
1128 // lightweight mechanism for other threads to detect when they can start
1129 // referring to new structures.
1130 private long loadNotifiesHandled = 0;
1132 public long getLoadNotifiesHandled()
1134 return loadNotifiesHandled;
1137 public void notifyFileLoaded(String fullPathName, String fileName2,
1138 String modelName, String errorMsg, int modelParts)
1140 if (errorMsg != null)
1142 fileLoadingError = errorMsg;
1146 // TODO: deal sensibly with models loaded inLine:
1147 // modelName will be null, as will fullPathName.
1149 // the rest of this routine ignores the arguments, and simply interrogates
1150 // the Jmol view to find out what structures it contains, and adds them to
1151 // the structure selection manager.
1152 fileLoadingError = null;
1153 String[] oldmodels = modelFileNames;
1154 modelFileNames = null;
1155 chainNames = new ArrayList<String>();
1156 chainFile = new HashMap<String, String>();
1157 boolean notifyLoaded = false;
1158 String[] modelfilenames = getPdbFile();
1159 // first check if we've lost any structures
1160 if (oldmodels != null && oldmodels.length > 0)
1163 for (int i = 0; i < oldmodels.length; i++)
1165 for (int n = 0; n < modelfilenames.length; n++)
1167 if (modelfilenames[n] == oldmodels[i])
1169 oldmodels[i] = null;
1173 if (oldmodels[i] != null)
1180 String[] oldmfn = new String[oldm];
1182 for (int i = 0; i < oldmodels.length; i++)
1184 if (oldmodels[i] != null)
1186 oldmfn[oldm++] = oldmodels[i];
1189 // deregister the Jmol instance for these structures - we'll add
1190 // ourselves again at the end for the current structure set.
1191 ssm.removeStructureViewerListener(this, oldmfn);
1195 // register ourselves as a listener and notify the gui that it needs to
1197 ssm.addStructureViewerListener(this);
1201 FeatureRenderer fr = getFeatureRenderer(null);
1207 loadNotifiesHandled++;
1209 setLoadingFromArchive(false);
1212 public void setJalviewColourScheme(ColourSchemeI cs)
1214 colourBySequence = false;
1224 // Chimera expects RBG values in the range 0-1
1225 final double normalise = 255D;
1226 viewerCommandHistory(false);
1227 // TODO: Switch between nucleotide or aa selection expressions
1228 Enumeration en = ResidueProperties.aa3Hash.keys();
1229 StringBuilder command = new StringBuilder(128);
1230 command.append("color white;");
1231 while (en.hasMoreElements())
1233 res = en.nextElement().toString();
1234 index = ((Integer) ResidueProperties.aa3Hash.get(res)).intValue();
1240 col = cs.findColour(ResidueProperties.aa[index].charAt(0));
1241 command.append("color " + col.getRed() / normalise + ","
1242 + col.getGreen() / normalise + "," + col.getBlue()
1243 / normalise + " ::" + res + ";");
1246 evalStateCommand(command.toString(),false);
1247 viewerCommandHistory(true);
1251 * called when the binding thinks the UI needs to be refreshed after a Chimera
1252 * state change. this could be because structures were loaded, or because an
1253 * error has occurred.
1255 public abstract void refreshGUI();
1257 public void componentResized(ComponentEvent e)
1262 public void componentMoved(ComponentEvent e)
1267 public void componentShown(ComponentEvent e)
1271 public void componentHidden(ComponentEvent e)
1275 public void setLoadingFromArchive(boolean loadingFromArchive)
1277 this.loadingFromArchive = loadingFromArchive;
1282 * @return true if Chimeral is still restoring state or loading is still going
1283 * on (see setFinsihedLoadingFromArchive)
1285 public boolean isLoadingFromArchive()
1287 return loadingFromArchive && !loadingFinished;
1291 * modify flag which controls if sequence colouring events are honoured by the
1292 * binding. Should be true for normal operation
1294 * @param finishedLoading
1296 public void setFinishedLoadingFromArchive(boolean finishedLoading)
1298 loadingFinished = finishedLoading;
1302 * Send the Chimera 'background solid <color>" command.
1305 * ://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/background
1309 public void setBackgroundColour(Color col)
1311 viewerCommandHistory(false);
1312 double normalise = 255D;
1313 final String command = "background solid " + col.getRed() / normalise + ","
1314 + col.getGreen() / normalise + "," + col.getBlue()
1316 viewer.sendChimeraCommand(command, false);
1317 viewerCommandHistory(true);
1321 * add structures and any known sequence associations
1323 * @returns the pdb entries added to the current set.
1325 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
1326 SequenceI[][] seq, String[][] chns)
1328 List<PDBEntry> v = new ArrayList<PDBEntry>();
1329 List<int[]> rtn = new ArrayList<int[]>();
1330 for (int i = 0; i < pdbentry.length; i++)
1334 for (int i = 0; i < pdbe.length; i++)
1336 int r = v.indexOf(pdbe[i]);
1337 if (r == -1 || r >= pdbentry.length)
1345 // just make sure the sequence/chain entries are all up to date
1346 addSequenceAndChain(r, seq[i], chns[i]);
1349 pdbe = v.toArray(new PDBEntry[v.size()]);
1353 // expand the tied sequence[] and string[] arrays
1354 SequenceI[][] sqs = new SequenceI[pdbentry.length][];
1355 String[][] sch = new String[pdbentry.length][];
1356 System.arraycopy(sequence, 0, sqs, 0, sequence.length);
1357 System.arraycopy(chains, 0, sch, 0, this.chains.length);
1360 pdbe = new PDBEntry[rtn.size()];
1361 for (int r = 0; r < pdbe.length; r++)
1363 int[] stri = (rtn.get(r));
1364 // record the pdb file as a new addition
1365 pdbe[r] = pdbentry[stri[0]];
1366 // and add the new sequence/chain entries
1367 addSequenceAndChain(stri[0], seq[stri[1]], chns[stri[1]]);
1378 * Adds sequences to the pe'th pdbentry's sequence set.
1383 public void addSequence(int pe, SequenceI[] seq)
1385 addSequenceAndChain(pe, seq, null);
1388 private void addSequenceAndChain(int pe, SequenceI[] seq, String[] tchain)
1390 if (pe < 0 || pe >= pdbentry.length)
1392 throw new Error(MessageManager.formatMessage(
1393 "error.implementation_error_no_pdbentry_from_index",
1395 { Integer.valueOf(pe).toString() }));
1397 final String nullChain = "TheNullChain";
1398 List<SequenceI> s = new ArrayList<SequenceI>();
1399 List<String> c = new ArrayList<String>();
1402 chains = new String[pdbentry.length][];
1404 if (sequence[pe] != null)
1406 for (int i = 0; i < sequence[pe].length; i++)
1408 s.add(sequence[pe][i]);
1409 if (chains[pe] != null)
1411 if (i < chains[pe].length)
1413 c.add(chains[pe][i]);
1422 if (tchain != null && tchain.length > 0)
1429 for (int i = 0; i < seq.length; i++)
1431 if (!s.contains(seq[i]))
1434 if (tchain != null && i < tchain.length)
1436 c.add(tchain[i] == null ? nullChain : tchain[i]);
1440 SequenceI[] tmp = s.toArray(new SequenceI[s.size()]);
1444 String[] tch = c.toArray(new String[c.size()]);
1445 for (int i = 0; i < tch.length; i++)
1447 if (tch[i] == nullChain)
1463 * @return text report of alignment between pdbfile and any associated
1464 * alignment sequences
1466 public String printMapping(String pdbfile)
1468 return ssm.printMapping(pdbfile);