/*
* Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
* Copyright (C) $$Year-Rel$$ The Jalview Authors
*
* This file is part of Jalview.
*
* Jalview is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Jalview is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jalview. If not, see .
* The Jalview Authors are detailed in the 'AUTHORS' file.
*/
package jalview.gui;
import jalview.api.structures.JalviewStructureDisplayI;
import jalview.bin.Cache;
import jalview.datamodel.PDBEntry;
import jalview.datamodel.SequenceI;
import jalview.datamodel.StructureViewerModel;
import jalview.structure.StructureSelectionManager;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* A proxy for handling structure viewers, that orchestrates adding selected
* structures, associated with sequences in Jalview, to an existing viewer, or
* opening a new one. Currently supports either Jmol or Chimera as the structure
* viewer.
*
* @author jprocter
*/
public class StructureViewer
{
private static final String UNKNOWN_VIEWER_TYPE = "Unknown structure viewer type ";
StructureSelectionManager ssm;
public enum ViewerType
{
JMOL, CHIMERA
};
/**
* Constructor
*
* @param structureSelectionManager
*/
public StructureViewer(StructureSelectionManager structureSelectionManager)
{
ssm = structureSelectionManager;
}
public ViewerType getViewerType()
{
String viewType = Cache.getDefault(Preferences.STRUCTURE_DISPLAY,
ViewerType.JMOL.name());
return ViewerType.valueOf(viewType);
}
public void setViewerType(ViewerType type)
{
Cache.setProperty(Preferences.STRUCTURE_DISPLAY, type.name());
}
/**
* View multiple PDB entries, each with associated sequences
*
* @param pdbs
* @param seqs
* @param ap
* @return
*/
public JalviewStructureDisplayI viewStructures(PDBEntry[] pdbs,
SequenceI[] seqs, AlignmentPanel ap)
{
JalviewStructureDisplayI viewer = onlyOnePdb(pdbs, seqs, ap);
if (viewer != null)
{
/*
* user added structure to an existing viewer - all done
*/
return viewer;
}
ViewerType viewerType = getViewerType();
Map seqsForPdbs = getSequencesForPdbs(pdbs,
seqs);
PDBEntry[] pdbsForFile = seqsForPdbs.keySet().toArray(
new PDBEntry[seqsForPdbs.size()]);
SequenceI[][] theSeqs = seqsForPdbs.values().toArray(
new SequenceI[seqsForPdbs.size()][]);
JalviewStructureDisplayI sview = null;
if (viewerType.equals(ViewerType.JMOL))
{
sview = new AppJmol(ap, pdbsForFile, theSeqs);
}
else if (viewerType.equals(ViewerType.CHIMERA))
{
sview = new ChimeraViewFrame(pdbsForFile, theSeqs, ap);
}
else
{
Cache.log.error(UNKNOWN_VIEWER_TYPE + getViewerType().toString());
}
return sview;
}
/**
* Converts the list of selected PDB entries (possibly including duplicates
* for multiple chains), and corresponding sequences, into a map of sequences
* for each distinct PDB file. Returns null if either argument is null, or
* their lengths do not match.
*
* @param pdbs
* @param seqs
* @return
*/
Map getSequencesForPdbs(PDBEntry[] pdbs,
SequenceI[] seqs)
{
if (pdbs == null || seqs == null || pdbs.length != seqs.length)
{
return null;
}
/*
* we want only one 'representative' PDBEntry per distinct file name
* (there may be entries for distinct chains)
*/
Map pdbsSeen = new HashMap<>();
/*
* LinkedHashMap preserves order of PDB entries (significant if they
* will get superimposed to the first structure)
*/
Map> pdbSeqs = new LinkedHashMap<>();
for (int i = 0; i < pdbs.length; i++)
{
PDBEntry pdb = pdbs[i];
SequenceI seq = seqs[i];
String pdbFile = pdb.getFile();
if (pdbFile == null || pdbFile.length() == 0)
{
pdbFile = pdb.getId();
}
if (!pdbsSeen.containsKey(pdbFile))
{
pdbsSeen.put(pdbFile, pdb);
pdbSeqs.put(pdb, new ArrayList());
}
else
{
pdb = pdbsSeen.get(pdbFile);
}
List seqsForPdb = pdbSeqs.get(pdb);
if (!seqsForPdb.contains(seq))
{
seqsForPdb.add(seq);
}
}
/*
* convert to Map
*/
Map result = new LinkedHashMap<>();
for (Entry> entry : pdbSeqs.entrySet())
{
List theSeqs = entry.getValue();
result.put(entry.getKey(),
theSeqs.toArray(new SequenceI[theSeqs.size()]));
}
return result;
}
/**
* A strictly temporary method pending JAL-1761 refactoring. Determines if all
* the passed PDB entries are the same (this is the case if selected sequences
* to view structure for are chains of the same structure). If so, calls the
* single-pdb version of viewStructures and returns the viewer, else returns
* null.
*
* @param pdbs
* @param seqsForPdbs
* @param ap
* @return
*/
private JalviewStructureDisplayI onlyOnePdb(PDBEntry[] pdbs,
SequenceI[] seqsForPdbs, AlignmentPanel ap)
{
List seqs = new ArrayList();
if (pdbs == null || pdbs.length == 0)
{
return null;
}
int i = 0;
String firstFile = pdbs[0].getFile();
for (PDBEntry pdb : pdbs)
{
String pdbFile = pdb.getFile();
if (pdbFile == null || !pdbFile.equals(firstFile))
{
return null;
}
SequenceI pdbseq = seqsForPdbs[i++];
if (pdbseq != null)
{
seqs.add(pdbseq);
}
}
return viewStructures(pdbs[0], seqs.toArray(new SequenceI[seqs.size()]),
ap);
}
public JalviewStructureDisplayI viewStructures(PDBEntry pdb,
SequenceI[] seqsForPdb, AlignmentPanel ap)
{
ViewerType viewerType = getViewerType();
JalviewStructureDisplayI sview = null;
if (viewerType.equals(ViewerType.JMOL))
{
sview = new AppJmol(pdb, seqsForPdb, null, ap);
}
else if (viewerType.equals(ViewerType.CHIMERA))
{
sview = new ChimeraViewFrame(pdb, seqsForPdb, null, ap);
}
else
{
Cache.log.error(UNKNOWN_VIEWER_TYPE + getViewerType().toString());
}
return sview;
}
/**
* Create a new panel controlling a structure viewer.
*
* @param type
* @param pdbf
* @param id
* @param sq
* @param alignPanel
* @param viewerData
* @param fileloc
* @param rect
* @param vid
* @return
*/
public JalviewStructureDisplayI createView(ViewerType type, String[] pdbf,
String[] id, SequenceI[][] sq, AlignmentPanel alignPanel,
StructureViewerModel viewerData, String fileloc, Rectangle rect,
String vid)
{
final boolean useinViewerSuperpos = viewerData.isAlignWithPanel();
final boolean usetoColourbyseq = viewerData.isColourWithAlignPanel();
final boolean viewerColouring = viewerData.isColourByViewer();
JalviewStructureDisplayI sview = null;
switch (type)
{
case JMOL:
sview = new AppJmol(pdbf, id, sq, alignPanel, usetoColourbyseq,
useinViewerSuperpos, viewerColouring, fileloc, rect, vid);
break;
case CHIMERA:
Cache.log.error(
"Unsupported structure viewer type " + type.toString());
break;
default:
Cache.log.error(UNKNOWN_VIEWER_TYPE + type.toString());
}
return sview;
}
}