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.ensureJmol();
59 private static final String UNKNOWN_VIEWER_TYPE = "Unknown structure viewer type ";
61 StructureSelectionManager ssm;
64 * decide if new structures are aligned to existing ones
66 private boolean superposeAdded = true;
68 public enum ViewerType
76 * @param structureSelectionManager
78 public StructureViewer(StructureSelectionManager structureSelectionManager)
80 ssm = structureSelectionManager;
84 * Factory to create a proxy for modifying existing structure viewer
87 public static StructureViewer reconfigure(
88 JalviewStructureDisplayI display)
90 StructureViewer sv = new StructureViewer(display.getBinding().getSsm());
96 public String toString()
100 return sview.toString();
104 public ViewerType getViewerType()
106 String viewType = Cache.getDefault(Preferences.STRUCTURE_DISPLAY,
107 ViewerType.JMOL.name());
108 return ViewerType.valueOf(viewType);
111 public void setViewerType(ViewerType type)
113 Cache.setProperty(Preferences.STRUCTURE_DISPLAY, type.name());
117 * View multiple PDB entries, each with associated sequences
124 public JalviewStructureDisplayI viewStructures(PDBEntry[] pdbs,
125 SequenceI[] seqs, AlignmentPanel ap)
127 JalviewStructureDisplayI viewer = onlyOnePdb(pdbs, seqs, ap);
131 * user added structure to an existing viewer - all done
136 ViewerType viewerType = getViewerType();
138 Map<PDBEntry, SequenceI[]> seqsForPdbs = getSequencesForPdbs(pdbs,
140 PDBEntry[] pdbsForFile = seqsForPdbs.keySet().toArray(
141 new PDBEntry[seqsForPdbs.size()]);
142 SequenceI[][] theSeqs = seqsForPdbs.values().toArray(
143 new SequenceI[seqsForPdbs.size()][]);
146 sview.setAlignAddedStructures(superposeAdded);
147 new Thread(new Runnable()
153 for (int pdbep = 0; pdbep < pdbsForFile.length; pdbep++)
155 PDBEntry pdb = pdbsForFile[pdbep];
156 if (!sview.addAlreadyLoadedFile(theSeqs[pdbep], null, ap,
159 sview.addToExistingViewer(pdb, theSeqs[pdbep], null, ap,
164 sview.updateTitleAndMenus();
170 if (viewerType.equals(ViewerType.JMOL))
172 sview = new AppJmol(ap, superposeAdded, pdbsForFile, theSeqs);
174 else if (viewerType.equals(ViewerType.CHIMERA))
176 sview = new ChimeraViewFrame(pdbsForFile, superposeAdded, theSeqs,
181 Cache.log.error(UNKNOWN_VIEWER_TYPE + getViewerType().toString());
187 * Converts the list of selected PDB entries (possibly including duplicates
188 * for multiple chains), and corresponding sequences, into a map of sequences
189 * for each distinct PDB file. Returns null if either argument is null, or
190 * their lengths do not match.
196 Map<PDBEntry, SequenceI[]> getSequencesForPdbs(PDBEntry[] pdbs,
199 if (pdbs == null || seqs == null || pdbs.length != seqs.length)
205 * we want only one 'representative' PDBEntry per distinct file name
206 * (there may be entries for distinct chains)
208 Map<String, PDBEntry> pdbsSeen = new HashMap<>();
211 * LinkedHashMap preserves order of PDB entries (significant if they
212 * will get superimposed to the first structure)
214 Map<PDBEntry, List<SequenceI>> pdbSeqs = new LinkedHashMap<>();
215 for (int i = 0; i < pdbs.length; i++)
217 PDBEntry pdb = pdbs[i];
218 SequenceI seq = seqs[i];
219 String pdbFile = pdb.getFile();
220 if (pdbFile == null || pdbFile.length() == 0)
222 pdbFile = pdb.getId();
224 if (!pdbsSeen.containsKey(pdbFile))
226 pdbsSeen.put(pdbFile, pdb);
227 pdbSeqs.put(pdb, new ArrayList<SequenceI>());
231 pdb = pdbsSeen.get(pdbFile);
233 List<SequenceI> seqsForPdb = pdbSeqs.get(pdb);
234 if (!seqsForPdb.contains(seq))
241 * convert to Map<PDBEntry, SequenceI[]>
243 Map<PDBEntry, SequenceI[]> result = new LinkedHashMap<>();
244 for (Entry<PDBEntry, List<SequenceI>> entry : pdbSeqs.entrySet())
246 List<SequenceI> theSeqs = entry.getValue();
247 result.put(entry.getKey(),
248 theSeqs.toArray(new SequenceI[theSeqs.size()]));
255 * A strictly temporary method pending JAL-1761 refactoring. Determines if all
256 * the passed PDB entries are the same (this is the case if selected sequences
257 * to view structure for are chains of the same structure). If so, calls the
258 * single-pdb version of viewStructures and returns the viewer, else returns
266 private JalviewStructureDisplayI onlyOnePdb(PDBEntry[] pdbs,
267 SequenceI[] seqsForPdbs, AlignmentPanel ap)
269 List<SequenceI> seqs = new ArrayList<>();
270 if (pdbs == null || pdbs.length == 0)
275 String firstFile = pdbs[0].getFile();
276 for (PDBEntry pdb : pdbs)
278 String pdbFile = pdb.getFile();
279 if (pdbFile == null || !pdbFile.equals(firstFile))
283 SequenceI pdbseq = seqsForPdbs[i++];
289 return viewStructures(pdbs[0], seqs.toArray(new SequenceI[seqs.size()]),
293 JalviewStructureDisplayI sview = null;
295 public JalviewStructureDisplayI viewStructures(PDBEntry pdb,
296 SequenceI[] seqsForPdb, AlignmentPanel ap)
300 sview.setAlignAddedStructures(superposeAdded);
301 String pdbId = pdb.getId();
302 if (!sview.addAlreadyLoadedFile(seqsForPdb, null, ap, pdbId))
304 sview.addToExistingViewer(pdb, seqsForPdb, null, ap, pdbId);
306 sview.updateTitleAndMenus();
310 ViewerType viewerType = getViewerType();
311 if (viewerType.equals(ViewerType.JMOL))
313 sview = new AppJmol(pdb, seqsForPdb, null, ap);
315 else if (viewerType.equals(ViewerType.CHIMERA))
317 sview = new ChimeraViewFrame(pdb, seqsForPdb, null, ap);
321 Cache.log.error(UNKNOWN_VIEWER_TYPE + getViewerType().toString());
327 * Create a new panel controlling a structure viewer.
340 public JalviewStructureDisplayI createView(ViewerType type, String[] pdbf,
341 String[] id, SequenceI[][] sq, AlignmentPanel alignPanel,
342 StructureViewerModel viewerData, String fileloc, Rectangle rect,
345 final boolean useinViewerSuperpos = viewerData.isAlignWithPanel();
346 final boolean usetoColourbyseq = viewerData.isColourWithAlignPanel();
347 final boolean viewerColouring = viewerData.isColourByViewer();
352 sview = new AppJmol(pdbf, id, sq, alignPanel, usetoColourbyseq,
353 useinViewerSuperpos, viewerColouring, fileloc, rect, vid);
357 "Unsupported structure viewer type " + type.toString());
360 Cache.log.error(UNKNOWN_VIEWER_TYPE + type.toString());
365 public boolean isBusy()
369 if (!sview.hasMapping())
380 * @return true if view is already showing PDBid
382 public boolean hasPdbId(String pDBid)
389 return sview.getBinding().hasPdbId(pDBid);
392 public boolean isVisible()
394 return sview != null && sview.isVisible();
397 public void setSuperpose(boolean alignAddedStructures)
399 superposeAdded = alignAddedStructures;
403 * Launch a minimal implementation of a StructureViewer.
410 public static StructureViewer launchStructureViewer(
411 AlignmentPanel alignPanel, PDBEntry pdb, SequenceI[] seqs)
413 return launchStructureViewer(alignPanel, new PDBEntry[] { pdb }, seqs,
418 * Launch a structure viewer with or without an open StructureChooser.
420 * Moved from StructureChooser to enable JalviewJS startup with structure
424 * @param pdbEntriesToView
431 protected static StructureViewer launchStructureViewer(
432 final AlignmentPanel ap,
433 final PDBEntry[] pdbEntriesToView, SequenceI[] sequences,
434 boolean superimpose, StructureViewer theViewer,
435 IProgressIndicator pb)
437 final StructureSelectionManager ssm = ap.getStructureSelectionManager();
438 long progressId = sequences.hashCode();
441 pb.setProgressBar(MessageManager
442 .getString("status.launching_3d_structure_viewer"), progressId);
444 if (theViewer == null)
446 theViewer = new StructureViewer(ssm);
448 theViewer.setSuperpose(superimpose);
452 pb.setProgressBar(null, progressId);
454 if (SiftsSettings.isMapWithSifts())
456 List<SequenceI> seqsWithoutSourceDBRef = new ArrayList<>();
458 // TODO: skip PDBEntry:Sequence pairs where PDBEntry doesn't look like a
459 // real PDB ID. For moment, we can also safely do this if there is already
460 // a known mapping between the PDBEntry and the sequence.
461 for (SequenceI seq : sequences)
463 PDBEntry pdbe = pdbEntriesToView[p++];
464 if (pdbe != null && pdbe.getFile() != null)
466 StructureMapping[] smm = ssm.getMapping(pdbe.getFile());
467 if (smm != null && smm.length > 0)
469 for (StructureMapping sm : smm)
471 if (sm.getSequence() == seq)
478 if (seq.getPrimaryDBRefs().isEmpty())
480 seqsWithoutSourceDBRef.add(seq);
484 if (!seqsWithoutSourceDBRef.isEmpty())
486 int y = seqsWithoutSourceDBRef.size();
489 pb.setProgressBar(MessageManager.formatMessage(
490 "status.fetching_dbrefs_for_sequences_without_valid_refs",
493 SequenceI[] seqWithoutSrcDBRef = seqsWithoutSourceDBRef
494 .toArray(new SequenceI[y]);
495 DBRefFetcher dbRefFetcher = new DBRefFetcher(seqWithoutSrcDBRef);
496 dbRefFetcher.fetchDBRefs(true);
500 pb.setProgressBar("Fetch complete.", progressId); // todo i18n
504 if (pdbEntriesToView.length > 1)
508 pb.setProgressBar(MessageManager.getString(
509 "status.fetching_3d_structures_for_selected_entries"),
512 theViewer.viewStructures(pdbEntriesToView, sequences, ap);
518 pb.setProgressBar(MessageManager.formatMessage(
519 "status.fetching_3d_structures_for",
520 pdbEntriesToView[0].getId()),progressId);
522 theViewer.viewStructures(pdbEntriesToView[0], sequences, ap);
526 pb.setProgressBar(null, progressId);
528 // remember the last viewer we used...
529 Desktop.getInstance().lastTargetedView = theViewer;