2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.api.structures.JalviewStructureDisplayI;
24 import jalview.bin.Cache;
25 import jalview.datamodel.PDBEntry;
26 import jalview.datamodel.SequenceI;
27 import jalview.datamodel.StructureViewerModel;
28 import jalview.structure.StructureMapping;
29 import jalview.structure.StructureSelectionManager;
30 import jalview.util.MessageManager;
31 import jalview.util.Platform;
32 import jalview.ws.DBRefFetcher;
33 import jalview.ws.sifts.SiftsSettings;
35 import java.awt.Rectangle;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.LinkedHashMap;
39 import java.util.List;
41 import java.util.Map.Entry;
44 * A proxy for handling structure viewers, that orchestrates adding selected
45 * structures, associated with sequences in Jalview, to an existing viewer, or
46 * opening a new one. Currently supports either Jmol or Chimera as the structure
51 public class StructureViewer
56 Platform.loadStaticResource("core/core_jvjmol.z.js",
57 "org.jmol.viewer.Viewer");
63 private static final String UNKNOWN_VIEWER_TYPE = "Unknown structure viewer type ";
65 StructureSelectionManager ssm;
68 * decide if new structures are aligned to existing ones
70 private boolean superposeAdded = true;
72 public enum ViewerType
80 * @param structureSelectionManager
82 public StructureViewer(StructureSelectionManager structureSelectionManager)
84 ssm = structureSelectionManager;
88 * Factory to create a proxy for modifying existing structure viewer
91 public static StructureViewer reconfigure(
92 JalviewStructureDisplayI display)
94 StructureViewer sv = new StructureViewer(display.getBinding().getSsm());
100 public String toString()
104 return sview.toString();
108 public ViewerType getViewerType()
110 String viewType = Cache.getDefault(Preferences.STRUCTURE_DISPLAY,
111 ViewerType.JMOL.name());
112 return ViewerType.valueOf(viewType);
115 public void setViewerType(ViewerType type)
117 Cache.setProperty(Preferences.STRUCTURE_DISPLAY, type.name());
121 * View multiple PDB entries, each with associated sequences
128 public JalviewStructureDisplayI viewStructures(PDBEntry[] pdbs,
129 SequenceI[] seqs, AlignmentPanel ap)
131 JalviewStructureDisplayI viewer = onlyOnePdb(pdbs, seqs, ap);
135 * user added structure to an existing viewer - all done
140 ViewerType viewerType = getViewerType();
142 Map<PDBEntry, SequenceI[]> seqsForPdbs = getSequencesForPdbs(pdbs,
144 PDBEntry[] pdbsForFile = seqsForPdbs.keySet().toArray(
145 new PDBEntry[seqsForPdbs.size()]);
146 SequenceI[][] theSeqs = seqsForPdbs.values().toArray(
147 new SequenceI[seqsForPdbs.size()][]);
150 sview.setAlignAddedStructures(superposeAdded);
151 new Thread(new Runnable()
157 for (int pdbep = 0; pdbep < pdbsForFile.length; pdbep++)
159 PDBEntry pdb = pdbsForFile[pdbep];
160 if (!sview.addAlreadyLoadedFile(theSeqs[pdbep], null, ap,
163 sview.addToExistingViewer(pdb, theSeqs[pdbep], null, ap,
168 sview.updateTitleAndMenus();
174 if (viewerType.equals(ViewerType.JMOL))
176 sview = new AppJmol(ap, superposeAdded, pdbsForFile, theSeqs);
178 else if (viewerType.equals(ViewerType.CHIMERA))
180 sview = new ChimeraViewFrame(pdbsForFile, superposeAdded, theSeqs,
185 Cache.log.error(UNKNOWN_VIEWER_TYPE + getViewerType().toString());
191 * Converts the list of selected PDB entries (possibly including duplicates
192 * for multiple chains), and corresponding sequences, into a map of sequences
193 * for each distinct PDB file. Returns null if either argument is null, or
194 * their lengths do not match.
200 Map<PDBEntry, SequenceI[]> getSequencesForPdbs(PDBEntry[] pdbs,
203 if (pdbs == null || seqs == null || pdbs.length != seqs.length)
209 * we want only one 'representative' PDBEntry per distinct file name
210 * (there may be entries for distinct chains)
212 Map<String, PDBEntry> pdbsSeen = new HashMap<>();
215 * LinkedHashMap preserves order of PDB entries (significant if they
216 * will get superimposed to the first structure)
218 Map<PDBEntry, List<SequenceI>> pdbSeqs = new LinkedHashMap<>();
219 for (int i = 0; i < pdbs.length; i++)
221 PDBEntry pdb = pdbs[i];
222 SequenceI seq = seqs[i];
223 String pdbFile = pdb.getFile();
224 if (pdbFile == null || pdbFile.length() == 0)
226 pdbFile = pdb.getId();
228 if (!pdbsSeen.containsKey(pdbFile))
230 pdbsSeen.put(pdbFile, pdb);
231 pdbSeqs.put(pdb, new ArrayList<SequenceI>());
235 pdb = pdbsSeen.get(pdbFile);
237 List<SequenceI> seqsForPdb = pdbSeqs.get(pdb);
238 if (!seqsForPdb.contains(seq))
245 * convert to Map<PDBEntry, SequenceI[]>
247 Map<PDBEntry, SequenceI[]> result = new LinkedHashMap<>();
248 for (Entry<PDBEntry, List<SequenceI>> entry : pdbSeqs.entrySet())
250 List<SequenceI> theSeqs = entry.getValue();
251 result.put(entry.getKey(),
252 theSeqs.toArray(new SequenceI[theSeqs.size()]));
259 * A strictly temporary method pending JAL-1761 refactoring. Determines if all
260 * the passed PDB entries are the same (this is the case if selected sequences
261 * to view structure for are chains of the same structure). If so, calls the
262 * single-pdb version of viewStructures and returns the viewer, else returns
270 private JalviewStructureDisplayI onlyOnePdb(PDBEntry[] pdbs,
271 SequenceI[] seqsForPdbs, AlignmentPanel ap)
273 List<SequenceI> seqs = new ArrayList<>();
274 if (pdbs == null || pdbs.length == 0)
279 String firstFile = pdbs[0].getFile();
280 for (PDBEntry pdb : pdbs)
282 String pdbFile = pdb.getFile();
283 if (pdbFile == null || !pdbFile.equals(firstFile))
287 SequenceI pdbseq = seqsForPdbs[i++];
293 return viewStructures(pdbs[0], seqs.toArray(new SequenceI[seqs.size()]),
297 JalviewStructureDisplayI sview = null;
299 public JalviewStructureDisplayI viewStructures(PDBEntry pdb,
300 SequenceI[] seqsForPdb, AlignmentPanel ap)
304 sview.setAlignAddedStructures(superposeAdded);
305 String pdbId = pdb.getId();
306 if (!sview.addAlreadyLoadedFile(seqsForPdb, null, ap, pdbId))
308 sview.addToExistingViewer(pdb, seqsForPdb, null, ap, pdbId);
310 sview.updateTitleAndMenus();
314 ViewerType viewerType = getViewerType();
315 if (viewerType.equals(ViewerType.JMOL))
317 sview = new AppJmol(pdb, seqsForPdb, null, ap);
319 else if (viewerType.equals(ViewerType.CHIMERA))
321 sview = new ChimeraViewFrame(pdb, seqsForPdb, null, ap);
325 Cache.log.error(UNKNOWN_VIEWER_TYPE + getViewerType().toString());
331 * Create a new panel controlling a structure viewer.
344 public JalviewStructureDisplayI createView(ViewerType type, String[] pdbf,
345 String[] id, SequenceI[][] sq, AlignmentPanel alignPanel,
346 StructureViewerModel viewerData, String fileloc, Rectangle rect,
349 final boolean useinViewerSuperpos = viewerData.isAlignWithPanel();
350 final boolean usetoColourbyseq = viewerData.isColourWithAlignPanel();
351 final boolean viewerColouring = viewerData.isColourByViewer();
356 sview = new AppJmol(pdbf, id, sq, alignPanel, usetoColourbyseq,
357 useinViewerSuperpos, viewerColouring, fileloc, rect, vid);
361 "Unsupported structure viewer type " + type.toString());
364 Cache.log.error(UNKNOWN_VIEWER_TYPE + type.toString());
369 public boolean isBusy()
373 if (!sview.hasMapping())
384 * @return true if view is already showing PDBid
386 public boolean hasPdbId(String pDBid)
393 return sview.getBinding().hasPdbId(pDBid);
396 public boolean isVisible()
398 return sview != null && sview.isVisible();
401 public void setSuperpose(boolean alignAddedStructures)
403 superposeAdded = alignAddedStructures;
407 * Launch a minimal implementation of a StructureViewer.
414 public static StructureViewer launchStructureViewer(
415 AlignmentPanel alignPanel, PDBEntry pdb, SequenceI[] seqs)
417 return launchStructureViewer(alignPanel, new PDBEntry[] { pdb }, seqs,
422 * Adds PDB structures to a new or existing structure viewer
425 * @param pdbEntriesToView
430 protected static StructureViewer launchStructureViewer(
431 final AlignmentPanel ap, final PDBEntry[] pdbEntriesToView,
432 SequenceI[] sequences, boolean superimpose,
433 StructureViewer theViewer, IProgressIndicator pb)
435 final StructureSelectionManager ssm = ap.getStructureSelectionManager();
436 if (theViewer == null)
437 theViewer = new StructureViewer(ssm);
438 long progressId = sequences.hashCode();
440 pb.setProgressBar(MessageManager.getString(
441 "status.launching_3d_structure_viewer"), progressId);
442 theViewer.setSuperpose(superimpose);
445 * remember user's choice of superimpose or not
447 Cache.setProperty(StructureChooser.AUTOSUPERIMPOSE,
448 Boolean.valueOf(superimpose).toString());
451 pb.setProgressBar(null, progressId);
452 if (SiftsSettings.isMapWithSifts())
454 List<SequenceI> seqsWithoutSourceDBRef = new ArrayList<>();
456 // TODO: skip PDBEntry:Sequence pairs where PDBEntry doesn't look like a
457 // real PDB ID. For moment, we can also safely do this if there is already
458 // a known mapping between the PDBEntry and the sequence.
459 for (SequenceI seq : sequences)
461 PDBEntry pdbe = pdbEntriesToView[p++];
462 if (pdbe != null && pdbe.getFile() != null)
464 StructureMapping[] smm = ssm.getMapping(pdbe.getFile());
465 if (smm != null && smm.length > 0)
467 for (StructureMapping sm : smm)
469 if (sm.getSequence() == seq)
476 if (seq.getPrimaryDBRefs().isEmpty())
478 seqsWithoutSourceDBRef.add(seq);
482 if (!seqsWithoutSourceDBRef.isEmpty())
484 int y = seqsWithoutSourceDBRef.size();
486 pb.setProgressBar(MessageManager.formatMessage(
487 "status.fetching_dbrefs_for_sequences_without_valid_refs",
489 SequenceI[] seqWithoutSrcDBRef = seqsWithoutSourceDBRef
490 .toArray(new SequenceI[y]);
491 DBRefFetcher dbRefFetcher = new DBRefFetcher(seqWithoutSrcDBRef);
492 dbRefFetcher.fetchDBRefs(true);
495 pb.setProgressBar("Fetch complete.", progressId); // todo i18n
498 if (pdbEntriesToView.length > 1)
501 pb.setProgressBar(MessageManager.getString(
502 "status.fetching_3d_structures_for_selected_entries"),
504 theViewer.viewStructures(pdbEntriesToView, sequences, ap);
509 pb.setProgressBar(MessageManager.formatMessage(
510 "status.fetching_3d_structures_for",
511 pdbEntriesToView[0].getId()), progressId);
512 theViewer.viewStructures(pdbEntriesToView[0], sequences, ap);
515 pb.setProgressBar(null, progressId);
516 // remember the last viewer we used...
517 StructureChooser.lastTargetedView = theViewer;