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.StructureSelectionManager;
30 import java.awt.Rectangle;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.LinkedHashMap;
34 import java.util.List;
36 import java.util.Map.Entry;
39 * proxy for handling structure viewers.
41 * this allows new views to be created with the currently configured viewer, the
42 * preferred viewer to be set/read and existing views created previously with a
43 * particular viewer to be recovered
47 public class StructureViewer
49 StructureSelectionManager ssm;
51 public enum ViewerType
56 public ViewerType getViewerType()
58 String viewType = Cache.getDefault(Preferences.STRUCTURE_DISPLAY,
59 ViewerType.JMOL.name());
60 return ViewerType.valueOf(viewType);
63 public void setViewerType(ViewerType type)
65 Cache.setProperty(Preferences.STRUCTURE_DISPLAY, type.name());
68 public StructureViewer(
69 StructureSelectionManager structureSelectionManager)
71 ssm = structureSelectionManager;
75 * View multiple PDB entries, each with associated sequences
82 public JalviewStructureDisplayI viewStructures(PDBEntry[] pdbs,
83 SequenceI[] seqsForPdbs, AlignmentPanel ap)
85 JalviewStructureDisplayI viewer = onlyOnePdb(pdbs, seqsForPdbs, ap);
89 * user added structure to an existing viewer - all done
94 ViewerType viewerType = getViewerType();
97 // PDBEntry[] pdbsForFile = getUniquePdbFiles(pdbs);
100 Map<PDBEntry, SequenceI[]> seqsForPdb = getSequencesForPdbs(pdbs,
102 PDBEntry[] pdbsForFile = seqsForPdb.keySet().toArray(
103 new PDBEntry[seqsForPdb.size()]);
104 SequenceI[][] theSeqs = seqsForPdb.values().toArray(
105 new SequenceI[seqsForPdb.size()][]);
106 JalviewStructureDisplayI sview = null;
107 if (viewerType.equals(ViewerType.JMOL))
109 sview = new AppJmol(ap, pdbsForFile, theSeqs);
110 // ap.av.collateForPDB(pdbsForFile));
112 else if (viewerType.equals(ViewerType.CHIMERA))
114 sview = new ChimeraViewFrame(pdbsForFile, theSeqs, ap);
115 // ap.av.collateForPDB(pdbsForFile), ap);
119 Cache.log.error("Unknown structure viewer type "
120 + getViewerType().toString());
126 * Converts the list of selected PDB entries (possibly including duplicates
127 * for multiple chains), and corresponding sequences, into a map of sequences
128 * for each distinct PDB file. Returns null if either argument is null, or
129 * their lengths do not match.
135 static Map<PDBEntry, SequenceI[]> getSequencesForPdbs(PDBEntry[] pdbs,
138 if (pdbs == null || seqs == null || pdbs.length != seqs.length)
144 * we want only one 'representative' PDBEntry per distinct file name
145 * (there may be entries for distinct chains)
147 Map<String, PDBEntry> pdbsSeen = new HashMap<>();
150 * LinkedHashMap preserves order of PDB entries (significant if they
151 * will get superimposed to the first structure)
153 Map<PDBEntry, List<SequenceI>> pdbSeqs = new LinkedHashMap<>();
154 for (int i = 0; i < pdbs.length; i++)
156 PDBEntry pdb = pdbs[i];
157 SequenceI seq = seqs[i];
158 String pdbFile = pdb.getFile();
159 if (!pdbsSeen.containsKey(pdbFile))
161 pdbsSeen.put(pdbFile, pdb);
162 pdbSeqs.put(pdb, new ArrayList<SequenceI>());
166 pdb = pdbsSeen.get(pdbFile);
168 List<SequenceI> seqsForPdb = pdbSeqs.get(pdb);
169 if (!seqsForPdb.contains(seq))
176 * convert to Map<PDBEntry, SequenceI[]>
178 Map<PDBEntry, SequenceI[]> result = new LinkedHashMap<>();
179 for (Entry<PDBEntry, List<SequenceI>> entry : pdbSeqs.entrySet())
181 List<SequenceI> theSeqs = entry.getValue();
182 result.put(entry.getKey(),
183 theSeqs.toArray(new SequenceI[theSeqs.size()]));
190 * A strictly temporary method pending JAL-1761 refactoring. Determines if all
191 * the passed PDB entries are the same (this is the case if selected sequences
192 * to view structure for are chains of the same structure). If so, calls the
193 * single-pdb version of viewStructures and returns the viewer, else returns
201 private JalviewStructureDisplayI onlyOnePdb(PDBEntry[] pdbs,
202 SequenceI[] seqsForPdbs, AlignmentPanel ap)
204 List<SequenceI> seqs = new ArrayList<SequenceI>();
205 if (pdbs == null || pdbs.length == 0)
210 String firstFile = pdbs[0].getFile();
211 for (PDBEntry pdb : pdbs)
213 String pdbFile = pdb.getFile();
214 if (pdbFile == null || !pdbFile.equals(firstFile))
218 SequenceI pdbseq = seqsForPdbs[i++];
224 return viewStructures(pdbs[0], seqs.toArray(new SequenceI[seqs.size()]),
228 public JalviewStructureDisplayI viewStructures(PDBEntry pdb,
229 SequenceI[] seqsForPdb, AlignmentPanel ap)
231 ViewerType viewerType = getViewerType();
232 JalviewStructureDisplayI sview = null;
233 if (viewerType.equals(ViewerType.JMOL))
235 sview = new AppJmol(pdb, seqsForPdb, null, ap);
237 else if (viewerType.equals(ViewerType.CHIMERA))
239 sview = new ChimeraViewFrame(pdb, seqsForPdb, null, ap);
243 Cache.log.error("Unknown structure viewer type "
244 + getViewerType().toString());
250 * Create a new panel controlling a structure viewer.
263 public JalviewStructureDisplayI createView(ViewerType type, String[] pdbf,
264 String[] id, SequenceI[][] sq, AlignmentPanel alignPanel,
265 StructureViewerModel viewerData, String fileloc, Rectangle rect,
268 final boolean useinViewerSuperpos = viewerData.isAlignWithPanel();
269 final boolean usetoColourbyseq = viewerData.isColourWithAlignPanel();
270 final boolean viewerColouring = viewerData.isColourByViewer();
272 JalviewStructureDisplayI sview = null;
276 sview = new AppJmol(pdbf, id, sq, alignPanel, usetoColourbyseq,
277 useinViewerSuperpos, viewerColouring, fileloc, rect, vid);
281 "Unsupported structure viewer type " + type.toString());
284 Cache.log.error("Unknown structure viewer type " + type.toString());