import jalview.datamodel.features.FeatureMatcherI;
import jalview.datamodel.features.FeatureMatcherSet;
import jalview.datamodel.features.FeatureMatcherSetI;
-import jalview.ext.rbvi.chimera.JalviewChimeraBinding;
import jalview.ext.varna.RnaModel;
import jalview.gui.AlignFrame;
import jalview.gui.AlignViewport;
import jalview.gui.AlignmentPanel;
import jalview.gui.AppVarna;
-import jalview.gui.ChimeraViewFrame;
-import jalview.gui.ChimeraXViewFrame;
import jalview.gui.Desktop;
-import jalview.gui.JalviewChimeraXBindingModel;
import jalview.gui.JvOptionPane;
import jalview.gui.OOMWarning;
import jalview.gui.PCAPanel;
import jalview.gui.PaintRefresher;
-import jalview.gui.PymolViewer;
import jalview.gui.SplitFrame;
import jalview.gui.StructureViewer;
import jalview.gui.StructureViewer.ViewerType;
}
if (!structureViewers.containsKey(sviewid))
{
+ String viewerType = structureState.getType();
+ if (viewerType == null) // pre Jalview 2.9
+ {
+ viewerType = ViewerType.JMOL.toString();
+ }
structureViewers.put(sviewid,
new StructureViewerModel(x, y, width, height, false,
false, true, structureState.getViewId(),
- structureState.getType()));
+ viewerType));
// Legacy pre-2.7 conversion JAL-823 :
// do not assume any view has to be linked for colour by
// sequence
return;
}
- /*
- * From 2.9: stateData.type contains JMOL or CHIMERA, data is in jar entry
- * "viewer_"+stateData.viewId
- */
String type = stateData.getType();
- if (type == null)
- {
- type = ViewerType.JMOL.toString();
- }
try
{
ViewerType viewerType = ViewerType.valueOf(type);
- switch (viewerType)
- {
- case CHIMERA:
- createChimeraViewer(viewerData, af, jprovider, false);
- break;
- case CHIMERAX:
- createChimeraViewer(viewerData, af, jprovider, true);
- break;
- case PYMOL:
- createPymolViewer(viewerData, af, jprovider);
- break;
- case JMOL:
- createJmolViewer(viewerData, af, jprovider);
- }
+ createStructureViewer(viewerType, viewerData, af, jprovider);
} catch (IllegalArgumentException | NullPointerException e)
{
+ // TODO JAL-3619 show error dialog / offer an alternative viewer
Cache.log.error(
"Invalid structure viewer type: " + type);
}
}
/**
- * Create a new Chimera or ChimeraX viewer
- *
- * @param data
- * @param af
- * @param jprovider
- * @param isChimeraX
- */
- protected void createChimeraViewer(
- Entry<String, StructureViewerModel> viewerData, AlignFrame af,
- jarInputStreamProvider jprovider, boolean isChimeraX)
- {
- StructureViewerModel data = viewerData.getValue();
- String chimeraSessionFile = data.getStateData();
-
- /*
- * Copy Chimera session from jar entry "viewer_"+viewId to a temporary file
- *
- * NB this is the 'saved' viewId as in the project file XML, _not_ the
- * 'uniquified' sviewid used to reconstruct the viewer here
- */
- String viewerJarEntryName = getViewerJarEntryName(data.getViewId());
- String extension = isChimeraX
- ? JalviewChimeraXBindingModel.CHIMERAX_SESSION_EXTENSION
- : JalviewChimeraBinding.CHIMERA_SESSION_EXTENSION;
- chimeraSessionFile = copyJarEntry(jprovider, viewerJarEntryName,
- "chimera", extension);
-
- Set<Entry<File, StructureData>> fileData = data.getFileData()
- .entrySet();
- List<PDBEntry> pdbs = new ArrayList<>();
- List<SequenceI[]> allseqs = new ArrayList<>();
- for (Entry<File, StructureData> pdb : fileData)
- {
- String filePath = pdb.getValue().getFilePath();
- String pdbId = pdb.getValue().getPdbId();
- // pdbs.add(new PDBEntry(filePath, pdbId));
- pdbs.add(new PDBEntry(pdbId, null, PDBEntry.Type.PDB, filePath));
- final List<SequenceI> seqList = pdb.getValue().getSeqList();
- SequenceI[] seqs = seqList.toArray(new SequenceI[seqList.size()]);
- allseqs.add(seqs);
- }
-
- boolean colourByChimera = data.isColourByViewer();
- boolean colourBySequence = data.isColourWithAlignPanel();
-
- // TODO use StructureViewer as a factory here, see JAL-1761
- final PDBEntry[] pdbArray = pdbs.toArray(new PDBEntry[pdbs.size()]);
- final SequenceI[][] seqsArray = allseqs
- .toArray(new SequenceI[allseqs.size()][]);
- String newViewId = viewerData.getKey();
-
- ChimeraViewFrame cvf = isChimeraX
- ? new ChimeraXViewFrame(chimeraSessionFile, af.alignPanel,
- pdbArray, seqsArray, colourByChimera, colourBySequence,
- newViewId)
- : new ChimeraViewFrame(chimeraSessionFile, af.alignPanel,
- pdbArray, seqsArray, colourByChimera, colourBySequence,
- newViewId);
- cvf.setSize(data.getWidth(), data.getHeight());
- cvf.setLocation(data.getX(), data.getY());
- }
-
- /**
- * Create a new Jmol window. First parse the Jmol state to translate filenames
- * loaded into the view, and record the order in which files are shown in the
- * Jmol view, so we can add the sequence mappings in same order.
- *
- * @param viewerData
- * @param af
- * @param jprovider
- */
- protected void createJmolViewer(
- final Entry<String, StructureViewerModel> viewerData,
- AlignFrame af, jarInputStreamProvider jprovider)
- {
- final StructureViewerModel svattrib = viewerData.getValue();
- String state = svattrib.getStateData();
-
- /*
- * Pre-2.9: state element value is the Jmol state string
- *
- * 2.9+: @type is "JMOL", state data is in a Jar file member named "viewer_"
- * + viewId
- */
- if (ViewerType.JMOL.toString().equals(svattrib.getType()))
- {
- state = readJarEntry(jprovider,
- getViewerJarEntryName(svattrib.getViewId()));
- }
-
- List<String> pdbfilenames = new ArrayList<>();
- List<SequenceI[]> seqmaps = new ArrayList<>();
- List<String> pdbids = new ArrayList<>();
- StringBuilder newFileLoc = new StringBuilder(64);
- int cp = 0, ncp, ecp;
- Map<File, StructureData> oldFiles = svattrib.getFileData();
- while ((ncp = state.indexOf("load ", cp)) > -1)
- {
- do
- {
- // look for next filename in load statement
- newFileLoc.append(state.substring(cp,
- ncp = (state.indexOf("\"", ncp + 1) + 1)));
- String oldfilenam = state.substring(ncp,
- ecp = state.indexOf("\"", ncp));
- // recover the new mapping data for this old filename
- // have to normalize filename - since Jmol and jalview do
- // filename
- // translation differently.
- StructureData filedat = oldFiles.get(new File(oldfilenam));
- if (filedat == null)
- {
- String reformatedOldFilename = oldfilenam.replaceAll("/", "\\\\");
- filedat = oldFiles.get(new File(reformatedOldFilename));
- }
- newFileLoc.append(Platform.escapeBackslashes(filedat.getFilePath()));
- pdbfilenames.add(filedat.getFilePath());
- pdbids.add(filedat.getPdbId());
- seqmaps.add(filedat.getSeqList().toArray(new SequenceI[0]));
- newFileLoc.append("\"");
- cp = ecp + 1; // advance beyond last \" and set cursor so we can
- // look for next file statement.
- } while ((ncp = state.indexOf("/*file*/", cp)) > -1);
- }
- if (cp > 0)
- {
- // just append rest of state
- newFileLoc.append(state.substring(cp));
- }
- else
- {
- System.err.print("Ignoring incomplete Jmol state for PDB ids: ");
- newFileLoc = new StringBuilder(state);
- newFileLoc.append("; load append ");
- for (File id : oldFiles.keySet())
- {
- // add this and any other pdb files that should be present in
- // the viewer
- StructureData filedat = oldFiles.get(id);
- newFileLoc.append(filedat.getFilePath());
- pdbfilenames.add(filedat.getFilePath());
- pdbids.add(filedat.getPdbId());
- seqmaps.add(filedat.getSeqList().toArray(new SequenceI[0]));
- newFileLoc.append(" \"");
- newFileLoc.append(filedat.getFilePath());
- newFileLoc.append("\"");
-
- }
- newFileLoc.append(";");
- }
-
- if (newFileLoc.length() == 0)
- {
- return;
- }
- int histbug = newFileLoc.indexOf("history = ");
- if (histbug > -1)
- {
- /*
- * change "history = [true|false];" to "history = [1|0];"
- */
- histbug += 10;
- int diff = histbug == -1 ? -1 : newFileLoc.indexOf(";", histbug);
- String val = (diff == -1) ? null
- : newFileLoc.substring(histbug, diff);
- if (val != null && val.length() >= 4)
- {
- if (val.contains("e")) // eh? what can it be?
- {
- if (val.trim().equals("true"))
- {
- val = "1";
- }
- else
- {
- val = "0";
- }
- newFileLoc.replace(histbug, diff, val);
- }
- }
- }
-
- final String[] pdbf = pdbfilenames
- .toArray(new String[pdbfilenames.size()]);
- final String[] id = pdbids.toArray(new String[pdbids.size()]);
- final SequenceI[][] sq = seqmaps
- .toArray(new SequenceI[seqmaps.size()][]);
- final String fileloc = newFileLoc.toString();
- final String sviewid = viewerData.getKey();
- final AlignFrame alf = af;
- final Rectangle rect = new Rectangle(svattrib.getX(), svattrib.getY(),
- svattrib.getWidth(), svattrib.getHeight());
- try
- {
- javax.swing.SwingUtilities.invokeAndWait(new Runnable()
- {
- @Override
- public void run()
- {
- JalviewStructureDisplayI sview = null;
- try
- {
- sview = new StructureViewer(
- alf.alignPanel.getStructureSelectionManager())
- .createView(StructureViewer.ViewerType.JMOL,
- pdbf, id, sq, alf.alignPanel, svattrib,
- fileloc, rect, sviewid);
- addNewStructureViewer(sview);
- } catch (OutOfMemoryError ex)
- {
- new OOMWarning("restoring structure view for PDB id " + id,
- (OutOfMemoryError) ex.getCause());
- if (sview != null && sview.isVisible())
- {
- sview.closeViewer(false);
- sview.setVisible(false);
- sview.dispose();
- }
- }
- }
- });
- } catch (InvocationTargetException ex)
- {
- warn("Unexpected error when opening Jmol view.", ex);
-
- } catch (InterruptedException e)
- {
- // e.printStackTrace();
- }
-
- }
-
- /**
* Generates a name for the entry in the project jar file to hold state
* information for a structure viewer
*
addDatasetRef(vamsasSet.getDatasetId(), ds);
}
}
- Vector dseqs = null;
+ Vector<SequenceI> dseqs = null;
if (!ignoreUnrefed)
{
// recovering an alignment View
// try even harder to restore dataset
AlignmentI xtantDS = checkIfHasDataset(vamsasSet.getSequence());
// create a list of new dataset sequences
- dseqs = new Vector();
+ dseqs = new Vector<>();
}
for (int i = 0, iSize = vamsasSet.getSequence().size(); i < iSize; i++)
{
* vamsasSeq array ordering, to preserve ordering of dataset
*/
private void ensureJalviewDatasetSequence(Sequence vamsasSeq,
- AlignmentI ds, Vector dseqs, boolean ignoreUnrefed, int vseqpos)
+ AlignmentI ds, Vector<SequenceI> dseqs, boolean ignoreUnrefed,
+ int vseqpos)
{
// JBP TODO: Check this is called for AlCodonFrames to support recovery of
// xRef Codon Maps
}
/**
- * Create a new PyMol viewer
+ * Creates a new structure viewer window
*
- * @param data
+ * @param viewerType
+ * @param viewerData
* @param af
* @param jprovider
*/
- protected void createPymolViewer(
- Entry<String, StructureViewerModel> viewerData, AlignFrame af,
+ protected void createStructureViewer(
+ ViewerType viewerType, final Entry<String, StructureViewerModel> viewerData,
+ AlignFrame af, jarInputStreamProvider jprovider)
+ {
+ final StructureViewerModel viewerModel = viewerData.getValue();
+ String sessionFilePath = null;
+
+ if (viewerType == ViewerType.JMOL)
+ {
+ sessionFilePath = rewriteJmolSession(viewerModel, jprovider);
+ }
+ else
+ {
+ String viewerJarEntryName = getViewerJarEntryName(
+ viewerModel.getViewId());
+ sessionFilePath = copyJarEntry(jprovider,
+ viewerJarEntryName,
+ "viewerSession", ".tmp");
+ }
+ final String sessionPath = sessionFilePath;
+ final String sviewid = viewerData.getKey();
+ try
+ {
+ SwingUtilities.invokeAndWait(new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ JalviewStructureDisplayI sview = null;
+ try
+ {
+ sview = StructureViewer.createView(viewerType, af.alignPanel,
+ viewerModel, sessionPath, sviewid);
+ addNewStructureViewer(sview);
+ } catch (OutOfMemoryError ex)
+ {
+ new OOMWarning("Restoring structure view for "
+ + viewerType,
+ (OutOfMemoryError) ex.getCause());
+ if (sview != null && sview.isVisible())
+ {
+ sview.closeViewer(false);
+ sview.setVisible(false);
+ sview.dispose();
+ }
+ }
+ }
+ });
+ } catch (InvocationTargetException | InterruptedException ex)
+ {
+ warn("Unexpected error when opening " + viewerType
+ + " structure viewer", ex);
+ }
+ }
+
+ /**
+ * Rewrites a Jmol session script, saves it to a temporary file, and returns
+ * the path of the file. "load file" commands are rewritten to change the
+ * original PDB file names to those created as the Jalview project is loaded.
+ *
+ * @param svattrib
+ * @param jprovider
+ * @return
+ */
+ private String rewriteJmolSession(StructureViewerModel svattrib,
jarInputStreamProvider jprovider)
{
- StructureViewerModel data = viewerData.getValue();
- String pymolSessionFile = data.getStateData();
-
- /*
- * Copy PyMol session from jar entry "viewer_"+viewId to a temporary file
- *
- * NB this is the 'saved' viewId as in the project file XML, _not_ the
- * 'uniquified' sviewid used to reconstruct the viewer here
- */
- String viewerJarEntryName = getViewerJarEntryName(data.getViewId());
- pymolSessionFile = copyJarEntry(jprovider, viewerJarEntryName,
- "pymol", ".pse");
-
- Set<Entry<File, StructureData>> fileData = data.getFileData()
- .entrySet();
- List<PDBEntry> pdbs = new ArrayList<>();
- List<SequenceI[]> allseqs = new ArrayList<>();
- for (Entry<File, StructureData> pdb : fileData)
- {
- String filePath = pdb.getValue().getFilePath();
- String pdbId = pdb.getValue().getPdbId();
- // pdbs.add(new PDBEntry(filePath, pdbId));
- pdbs.add(new PDBEntry(pdbId, null, PDBEntry.Type.PDB, filePath));
- final List<SequenceI> seqList = pdb.getValue().getSeqList();
- SequenceI[] seqs = seqList.toArray(new SequenceI[seqList.size()]);
- allseqs.add(seqs);
+ String state = svattrib.getStateData(); // Jalview < 2.9
+ if (state == null || state.isEmpty()) // Jalview >= 2.9
+ {
+ state = readJarEntry(jprovider,
+ getViewerJarEntryName(svattrib.getViewId()));
}
-
- boolean colourByPymol = data.isColourByViewer();
- boolean colourBySequence = data.isColourWithAlignPanel();
-
- // TODO use StructureViewer as a factory here, see JAL-1761
- final PDBEntry[] pdbArray = pdbs.toArray(new PDBEntry[pdbs.size()]);
- final SequenceI[][] seqsArray = allseqs
- .toArray(new SequenceI[allseqs.size()][]);
- String newViewId = viewerData.getKey();
-
- PymolViewer pv = new PymolViewer(pymolSessionFile,
- af.alignPanel, pdbArray, seqsArray, colourByPymol,
- colourBySequence, newViewId);
- pv.setSize(data.getWidth(), data.getHeight());
- pv.setLocation(data.getX(), data.getY());
+ // TODO or simpler? for each key in oldFiles,
+ // replace key.getPath() in state with oldFiles.get(key).getFilePath()
+ // (allowing for different path escapings)
+ StringBuilder rewritten = new StringBuilder(state.length());
+ int cp = 0, ncp, ecp;
+ Map<File, StructureData> oldFiles = svattrib.getFileData();
+ while ((ncp = state.indexOf("load ", cp)) > -1)
+ {
+ do
+ {
+ // look for next filename in load statement
+ rewritten.append(state.substring(cp,
+ ncp = (state.indexOf("\"", ncp + 1) + 1)));
+ String oldfilenam = state.substring(ncp,
+ ecp = state.indexOf("\"", ncp));
+ // recover the new mapping data for this old filename
+ // have to normalize filename - since Jmol and jalview do
+ // filename translation differently.
+ StructureData filedat = oldFiles.get(new File(oldfilenam));
+ if (filedat == null)
+ {
+ String reformatedOldFilename = oldfilenam.replaceAll("/", "\\\\");
+ filedat = oldFiles.get(new File(reformatedOldFilename));
+ }
+ rewritten
+ .append(Platform.escapeBackslashes(filedat.getFilePath()));
+ rewritten.append("\"");
+ cp = ecp + 1; // advance beyond last \" and set cursor so we can
+ // look for next file statement.
+ } while ((ncp = state.indexOf("/*file*/", cp)) > -1);
+ }
+ if (cp > 0)
+ {
+ // just append rest of state
+ rewritten.append(state.substring(cp));
+ }
+ else
+ {
+ System.err.print("Ignoring incomplete Jmol state for PDB ids: ");
+ rewritten = new StringBuilder(state);
+ rewritten.append("; load append ");
+ for (File id : oldFiles.keySet())
+ {
+ // add pdb files that should be present in the viewer
+ StructureData filedat = oldFiles.get(id);
+ rewritten.append(filedat.getFilePath()).append(" \"")
+ .append(filedat.getFilePath()).append("\"");
+ }
+ rewritten.append(";");
+ }
+
+ if (rewritten.length() == 0)
+ {
+ return null;
+ }
+ final String history = "history = ";
+ int historyIndex = rewritten.indexOf(history);
+ if (historyIndex > -1)
+ {
+ /*
+ * change "history = [true|false];" to "history = [1|0];"
+ */
+ historyIndex += history.length();
+ String val = rewritten.substring(historyIndex, historyIndex + 5);
+ if (val.startsWith("true"))
+ {
+ rewritten.replace(historyIndex, historyIndex + 4, "1");
+ }
+ else if (val.startsWith("false"))
+ {
+ rewritten.replace(historyIndex, historyIndex + 5, "0");
+ }
+ }
+
+ try
+ {
+ File tmp = File.createTempFile("viewerSession", ".tmp");
+ try (OutputStream os = new FileOutputStream(tmp))
+ {
+ InputStream is = new ByteArrayInputStream(
+ rewritten.toString().getBytes());
+ copyAll(is, os);
+ return tmp.getAbsolutePath();
+ }
+ } catch (IOException e)
+ {
+ Cache.log.error("Error restoring Jmol session: " + e.toString());
+ }
+ return null;
}
/**