From: gmungoc Date: Wed, 10 Jun 2015 14:23:33 +0000 (+0100) Subject: Merge remote-tracking branch X-Git-Tag: Release_2_10_0~612 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=7a3ba197b00d4016556f3cdd635919fe6817867b;hp=cb7c4a276c52b0bb3e448e6761886fecc0e9d05c;p=jalview.git Merge remote-tracking branch 'origin/schema/JAL-1764_structureViewAttribute' into develop JAL-1764 save structure viewer type in project file --- diff --git a/src/jalview/datamodel/StructureViewerModel.java b/src/jalview/datamodel/StructureViewerModel.java index 098372b..8bc745a 100644 --- a/src/jalview/datamodel/StructureViewerModel.java +++ b/src/jalview/datamodel/StructureViewerModel.java @@ -27,6 +27,11 @@ public class StructureViewerModel private String stateData = ""; + private String viewId; + + // CHIMERA or JMOL (for projects from Jalview 2.9 on) + private String type; + private Map fileData = new HashMap(); public class StructureData @@ -36,7 +41,6 @@ public class StructureViewerModel private String pdbId; private List seqList; - // TODO and possibly a list of chains? /** @@ -85,7 +89,7 @@ public class StructureViewerModel public StructureViewerModel(int x, int y, int width, int height, boolean alignWithPanel, boolean colourWithAlignPanel, - boolean colourByViewer) + boolean colourByViewer, String viewId, String type) { this.x = x; this.y = y; @@ -94,6 +98,8 @@ public class StructureViewerModel this.alignWithPanel = alignWithPanel; this.colourWithAlignPanel = colourWithAlignPanel; this.colourByViewer = colourByViewer; + this.viewId = viewId; + this.type = type; } public int getX() @@ -186,4 +192,13 @@ public class StructureViewerModel this.fileData = fileData; } + public String getViewId() + { + return this.viewId; + } + + public String getType() + { + return this.type; + } } diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index ad2bbc3..1970388 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -1098,11 +1098,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, .lastIndexOf(java.io.File.separatorChar) + 1); } - /* - * First save any linked Chimera session. - */ - Desktop.instance.saveChimeraSessions(file); - success = new Jalview2XML().saveAlignment(this, file, shortName); statusBar.setText(MessageManager.formatMessage( @@ -1126,7 +1121,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, String output = f.formatSequences(format, exportData.getAlignment(), // class cast exceptions will // occur in the distant future - exportData.getOmitHidden(), f.getCacheSuffixDefault(format), + exportData.getOmitHidden(), exportData.getStartEndPostions(), + f.getCacheSuffixDefault(format), viewport.getColumnSelection()); if (output == null) @@ -1202,7 +1198,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, cap.setText(new FormatAdapter(viewport).formatSequences( e.getActionCommand(), exportData.getAlignment(), - exportData.getOmitHidden(), + exportData.getOmitHidden(), exportData.getStartEndPostions(), viewport.getColumnSelection())); Desktop.addInternalFrame(cap, MessageManager.formatMessage( "label.alignment_output_command", new Object[] @@ -1219,10 +1215,17 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, { AlignmentI alignmentToExport = null; String[] omitHidden = null; + int[] alignmentStartEnd = new int[2]; FeatureRenderer fr = new FeatureRenderer(this.alignPanel); viewport.setFeatureRenderer(fr); HiddenSequences hiddenSeqs = viewport.getAlignment() .getHiddenSequences(); + + + alignmentToExport = viewport.getAlignment(); + alignmentStartEnd = new int[] + { 0, alignmentToExport.getWidth() - 1 }; + if (viewport.hasHiddenColumns() || hiddenSeqs.getSize() > 0) { int reply = JOptionPane @@ -1237,19 +1240,81 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, if (reply == JOptionPane.YES_OPTION) { + // export only visible region omitHidden = viewport.getViewAsString(false); + alignmentToExport = viewport.getAlignment(); + alignmentStartEnd = getStartEnd(alignmentStartEnd, viewport + .getColumnSelection().getHiddenColumns()); } else { - alignmentToExport = viewport.getAlignment().getHiddenSequences() - .getFullAlignment(); + // export all region including visible + alignmentToExport = hiddenSeqs.getFullAlignment(); } } - if (alignmentToExport == null) + + return new ExportData(alignmentToExport, omitHidden, alignmentStartEnd); + } + + private static int[] getStartEnd(int[] aligmentStartEnd, + List hiddenCols) + { + int startPos = aligmentStartEnd[0]; + int endPos = aligmentStartEnd[1]; + + int[] lowestRange = new int[2]; + int[] higestRange = new int[2]; + + for (int[] hiddenCol : hiddenCols) + { + // System.out.println("comparing : " + hiddenCol[0] + "-" + hiddenCol[1]); + + lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange; + higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange; + } + // System.out.println("min : " + lowestRange[0] + "-" + lowestRange[1]); + // System.out.println("max : " + higestRange[0] + "-" + higestRange[1]); + + if (lowestRange[0] == 0 && lowestRange[1] == 0) + { + startPos = aligmentStartEnd[0]; + } + else { - alignmentToExport = viewport.getAlignment(); + startPos = lowestRange[1] + 1; } - return new ExportData(alignmentToExport, omitHidden); + + if (higestRange[0] == 0 && higestRange[1] == 0) + { + endPos = aligmentStartEnd[1]; + } + else + { + endPos = higestRange[0]; + } + + // System.out.println("Export range : " + minPos + " - " + maxPos); + return new int[] + { startPos, endPos }; + } + + public static void main(String[] args) + { + ArrayList hiddenCols = new ArrayList(); + hiddenCols.add(new int[] + { 0, 4 }); + hiddenCols.add(new int[] + { 6, 9 }); + hiddenCols.add(new int[] + { 11, 12 }); + hiddenCols.add(new int[] + { 33, 33 }); + hiddenCols.add(new int[] + { 45, 50 }); + + int[] x = getStartEnd(new int[] + { 0, 50 }, hiddenCols); + // System.out.println("Export range : " + x[0] + " - " + x[1]); } /** @@ -1776,7 +1841,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, } String output = new FormatAdapter().formatSequences("Fasta", seqs, - omitHidden); + omitHidden, null); StringSelection ss = new StringSelection(output); @@ -6001,11 +6066,13 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, private String[] omitHidden; - public ExportData(AlignmentI align, String[] ommit) + private int[] startEnd; + + public ExportData(AlignmentI align, String[] ommit, int[] startEnd) { this.alignment = align; this.omitHidden = ommit; - System.out.println(); + this.startEnd = startEnd; } public AlignmentI getAlignment() @@ -6027,6 +6094,16 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, { this.omitHidden = omitHidden; } + + public int[] getStartEndPostions() + { + return startEnd; + } + + public void setStartEndPostions(int[] startEnd) + { + this.startEnd = startEnd; + } } @Override diff --git a/src/jalview/gui/AnnotationLabels.java b/src/jalview/gui/AnnotationLabels.java index a544813..fad9dcd 100755 --- a/src/jalview/gui/AnnotationLabels.java +++ b/src/jalview/gui/AnnotationLabels.java @@ -875,7 +875,7 @@ public class AnnotationLabels extends JPanel implements MouseListener, } String output = new FormatAdapter().formatSequences("Fasta", seqs, - omitHidden); + omitHidden, null); Toolkit.getDefaultToolkit().getSystemClipboard() .setContents(new StringSelection(output), Desktop.instance); diff --git a/src/jalview/gui/AppJmol.java b/src/jalview/gui/AppJmol.java index a3f6778..6139669 100644 --- a/src/jalview/gui/AppJmol.java +++ b/src/jalview/gui/AppJmol.java @@ -57,6 +57,7 @@ import jalview.datamodel.AlignmentI; import jalview.datamodel.ColumnSelection; import jalview.datamodel.PDBEntry; import jalview.datamodel.SequenceI; +import jalview.gui.StructureViewer.ViewerType; import jalview.io.AppletFormatAdapter; import jalview.io.JalviewFileChooser; import jalview.io.JalviewFileView; @@ -1234,4 +1235,10 @@ public class AppJmol extends StructureViewerBase return jmb == null ? null : jmb.viewer.getStateInfo(); } + @Override + public ViewerType getViewerType() + { + return ViewerType.JMOL; + } + } diff --git a/src/jalview/gui/ChimeraViewFrame.java b/src/jalview/gui/ChimeraViewFrame.java index b933345..1e6da78 100644 --- a/src/jalview/gui/ChimeraViewFrame.java +++ b/src/jalview/gui/ChimeraViewFrame.java @@ -26,9 +26,11 @@ import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; +import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; @@ -55,6 +57,7 @@ import jalview.datamodel.ColumnSelection; import jalview.datamodel.PDBEntry; import jalview.datamodel.SequenceI; import jalview.ext.rbvi.chimera.JalviewChimeraBinding; +import jalview.gui.StructureViewer.ViewerType; import jalview.io.AppletFormatAdapter; import jalview.io.JalviewFileChooser; import jalview.io.JalviewFileView; @@ -445,23 +448,25 @@ public class ChimeraViewFrame extends StructureViewerBase /** * Create a new viewer from saved session state data including Chimera session - * file. - * - * @param chimeraSession + * file * + * @param chimeraSessionFile * @param alignPanel * @param pdbArray * @param seqsArray * @param colourByChimera * @param colourBySequence + * @param newViewId */ - public ChimeraViewFrame(String chimeraSession, AlignmentPanel alignPanel, + public ChimeraViewFrame(String chimeraSessionFile, + AlignmentPanel alignPanel, PDBEntry[] pdbArray, SequenceI[][] seqsArray, boolean colourByChimera, - boolean colourBySequence) + boolean colourBySequence, String newViewId) { super(); - this.chimeraSessionFile = chimeraSession; + setViewId(newViewId); + this.chimeraSessionFile = chimeraSessionFile; openNewChimera(alignPanel, pdbArray, seqsArray); if (colourByChimera) { @@ -1310,31 +1315,74 @@ public class ChimeraViewFrame extends StructureViewerBase } /** - * Ask Chimera to save its session to the designated file path. Returns true - * if successful, else false. + * Ask Chimera to save its session to the designated file path, or to a + * temporary file if the path is null. Returns the file path if successful, + * else null. * * @param filepath * @see getStateInfo */ - public boolean saveSession(String filepath) + protected String saveSession(String filepath) { - boolean result = jmb.saveSession(filepath); - if (result) + String pathUsed = filepath; + try + { + if (pathUsed == null) + { + File tempFile = File.createTempFile("chimera", ".py"); + tempFile.deleteOnExit(); + pathUsed = tempFile.getPath(); + } + boolean result = jmb.saveSession(pathUsed); + if (result) + { + this.chimeraSessionFile = pathUsed; + return pathUsed; + } + } catch (IOException e) { - this.chimeraSessionFile = filepath; } - return result; + return null; } /** - * Returns the file path of the Chimera session file the last time it was - * saved. If it was never saved, returns an empty string. There is no - * guarantee that the Chimera session has not changed since it was saved. + * Returns a string representing the state of the Chimera session. This is + * done by requesting Chimera to save its session to a temporary file, then + * reading the file contents. Returns an empty string on any error. */ @Override public String getStateInfo() { - return this.chimeraSessionFile == null ? "" : chimeraSessionFile; + String sessionFile = saveSession(null); + if (sessionFile == null) + { + return ""; + } + InputStream is = null; + try + { + File f = new File(sessionFile); + byte[] bytes = new byte[(int) f.length()]; + is = new FileInputStream(sessionFile); + is.read(bytes); + return new String(bytes); + } catch (IOException e) + { + return ""; + } finally + { + if (is != null) + { + try + { + is.close(); + } catch (IOException e) + { + // ignoreß + } + } + } + // return this.chimeraSessionFile == null ? "" : chimeraSessionFile; } @Override @@ -1342,4 +1390,10 @@ public class ChimeraViewFrame extends StructureViewerBase { jmb.focusView(); } + + @Override + public ViewerType getViewerType() + { + return ViewerType.CHIMERA; + } } diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index e8d7ffd..9e84407 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -1485,9 +1485,6 @@ public class Desktop extends jalview.jbgui.GDesktop implements final java.io.File choice = chooser.getSelectedFile(); setProjectFile(choice); - // TODO or move inside the new Thread? - saveChimeraSessions(choice.getAbsolutePath()); - new Thread(new Runnable() { public void run() @@ -1525,32 +1522,6 @@ public class Desktop extends jalview.jbgui.GDesktop implements } } - /** - * Request any open, linked Chimera sessions to save their state. - * - * @param jalviewProjectFilename - * the filename of the Jalview project; Chimera session files should - * be given distinct, but obviously related, names. - */ - public void saveChimeraSessions(String jalviewProjectFilename) - { - int i = 0; - for (JInternalFrame frame : getAllFrames()) - { - if (frame instanceof ChimeraViewFrame) - { - /* - * Construct a filename for the Chimera session by append _chimera.py - * to the Jalview project file name. - */ - String chimeraPath = jalviewProjectFilename + "_chimera_" + i - + ".py"; - ((ChimeraViewFrame) frame).saveSession(chimeraPath); - i++; - } - } - } - private void setProjectFile(File choice) { this.projectFile = choice; diff --git a/src/jalview/gui/Jalview2XML.java b/src/jalview/gui/Jalview2XML.java index f56e531..80cff07 100644 --- a/src/jalview/gui/Jalview2XML.java +++ b/src/jalview/gui/Jalview2XML.java @@ -20,6 +20,45 @@ */ package jalview.gui; +import java.awt.Rectangle; +import java.io.BufferedReader; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.lang.reflect.InvocationTargetException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Hashtable; +import java.util.IdentityHashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.StringTokenizer; +import java.util.Vector; +import java.util.jar.JarEntry; +import java.util.jar.JarInputStream; +import java.util.jar.JarOutputStream; + +import javax.swing.JInternalFrame; +import javax.swing.JOptionPane; +import javax.swing.SwingUtilities; + +import org.exolab.castor.xml.Marshaller; +import org.exolab.castor.xml.Unmarshaller; + import jalview.api.structures.JalviewStructureDisplayI; import jalview.bin.Cache; import jalview.datamodel.AlignedCodonFrame; @@ -30,6 +69,7 @@ import jalview.datamodel.PDBEntry; import jalview.datamodel.SequenceI; import jalview.datamodel.StructureViewerModel; import jalview.datamodel.StructureViewerModel.StructureData; +import jalview.gui.StructureViewer.ViewerType; import jalview.schemabinding.version2.AlcodMap; import jalview.schemabinding.version2.AlcodonFrame; import jalview.schemabinding.version2.Annotation; @@ -83,44 +123,6 @@ import jalview.ws.params.ArgumentI; import jalview.ws.params.AutoCalcSetting; import jalview.ws.params.WsParamSetI; -import java.awt.Rectangle; -import java.io.BufferedReader; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.lang.reflect.InvocationTargetException; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Hashtable; -import java.util.IdentityHashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.StringTokenizer; -import java.util.Vector; -import java.util.jar.JarEntry; -import java.util.jar.JarInputStream; -import java.util.jar.JarOutputStream; - -import javax.swing.JInternalFrame; -import javax.swing.JOptionPane; -import javax.swing.SwingUtilities; - -import org.exolab.castor.xml.Unmarshaller; - /** * Write out the current jalview desktop state as a Jalview XML stream. * @@ -133,6 +135,8 @@ import org.exolab.castor.xml.Unmarshaller; */ public class Jalview2XML { + private static final String UTF_8 = "UTF-8"; + /* * SequenceI reference -> XML ID string in jalview XML. Populated as XML reps * of sequence objects are created. @@ -380,7 +384,7 @@ public class Jalview2XML // NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS // ////////////////////////////////////////////////// - Vector shortNames = new Vector(); + List shortNames = new ArrayList(); // REVERSE ORDER for (int i = frames.length - 1; i > -1; i--) @@ -394,33 +398,7 @@ public class Jalview2XML continue; } - String shortName = af.getTitle(); - - if (shortName.indexOf(File.separatorChar) > -1) - { - shortName = shortName.substring(shortName - .lastIndexOf(File.separatorChar) + 1); - } - - int count = 1; - - while (shortNames.contains(shortName)) - { - if (shortName.endsWith("_" + (count - 1))) - { - shortName = shortName.substring(0, shortName.lastIndexOf("_")); - } - - shortName = shortName.concat("_" + count); - count++; - } - - shortNames.addElement(shortName); - - if (!shortName.endsWith(".xml")) - { - shortName = shortName + ".xml"; - } + String shortName = makeFilename(af, shortNames); int ap, apSize = af.alignPanels.size(); @@ -467,6 +445,47 @@ public class Jalview2XML } } + /** + * Generates a distinct file name, based on the title of the AlignFrame, by + * appending _n for increasing n until an unused name is generated. The new + * name (without its extension) is added to the list. + * + * @param af + * @param namesUsed + * @return the generated name, with .xml extension + */ + protected String makeFilename(AlignFrame af, List namesUsed) + { + String shortName = af.getTitle(); + + if (shortName.indexOf(File.separatorChar) > -1) + { + shortName = shortName.substring(shortName + .lastIndexOf(File.separatorChar) + 1); + } + + int count = 1; + + while (namesUsed.contains(shortName)) + { + if (shortName.endsWith("_" + (count - 1))) + { + shortName = shortName.substring(0, shortName.lastIndexOf("_")); + } + + shortName = shortName.concat("_" + count); + count++; + } + + namesUsed.add(shortName); + + if (!shortName.endsWith(".xml")) + { + shortName = shortName + ".xml"; + } + return shortName; + } + // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW public boolean saveAlignment(AlignFrame af, String jarFile, String fileName) @@ -735,7 +754,8 @@ public class Jalview2XML jalview.datamodel.PDBEntry entry = (jalview.datamodel.PDBEntry) en .nextElement(); - pdb.setId(entry.getId()); + String pdbId = entry.getId(); + pdb.setId(pdbId); pdb.setType(entry.getType()); /* @@ -753,6 +773,24 @@ public class Jalview2XML StructureViewerBase viewFrame = (StructureViewerBase) frames[f]; matchedFile = saveStructureState(ap, jds, pdb, entry, viewIds, matchedFile, viewFrame); + /* + * Only store each structure viewer's state once in each XML + * document. First time through only (storeDS==false) + */ + String viewId = viewFrame.getViewId(); + if (!storeDS && !viewIds.contains(viewId)) + { + viewIds.add(viewId); + try + { + writeJarEntry(jout, getViewerJarEntryName(viewId), + viewFrame.getStateInfo().getBytes()); + } catch (IOException e) + { + System.err.println("Error saving viewer state: " + + e.getMessage()); + } + } } } @@ -769,46 +807,14 @@ public class Jalview2XML pdbfiles = new ArrayList(); } - if (!pdbfiles.contains(entry.getId())) + if (!pdbfiles.contains(pdbId)) { - pdbfiles.add(entry.getId()); - DataInputStream dis = null; - try - { - File file = new File(matchedFile); - if (file.exists() && jout != null) - { - byte[] data = new byte[(int) file.length()]; - jout.putNextEntry(new JarEntry(entry.getId())); - dis = new DataInputStream(new FileInputStream(file)); - dis.readFully(data); - - DataOutputStream dout = new DataOutputStream(jout); - dout.write(data, 0, data.length); - dout.flush(); - jout.closeEntry(); - } - } catch (Exception ex) - { - ex.printStackTrace(); - } finally - { - if (dis != null) - { - try - { - dis.close(); - } catch (IOException e) - { - // ignore - } - } - } - + pdbfiles.add(pdbId); + copyFileToJar(jout, matchedFile, pdbId); } } - if (entry.getProperty() != null) + if (entry.getProperty() != null && !entry.getProperty().isEmpty()) { PdbentryItem item = new PdbentryItem(); Hashtable properties = entry.getProperty(); @@ -935,6 +941,25 @@ public class Jalview2XML } } } + + /* + * Save associated Varna panels + */ + if (Desktop.desktop != null) + { + for (JInternalFrame frame : Desktop.desktop.getAllFrames()) + { + if (frame instanceof AppVarna) + { + AppVarna vp = (AppVarna) frame; + if (vp.ap == ap) + { + // save Varna state + } + } + } + } + // SAVE ANNOTATIONS /** * store forward refs from an annotationRow to any groups @@ -1288,9 +1313,8 @@ public class Jalview2XML JarEntry entry = new JarEntry(fileName); jout.putNextEntry(entry); PrintWriter pout = new PrintWriter(new OutputStreamWriter(jout, - "UTF-8")); - org.exolab.castor.xml.Marshaller marshaller = new org.exolab.castor.xml.Marshaller( - pout); + UTF_8)); + Marshaller marshaller = new Marshaller(pout); marshaller.marshal(object); pout.flush(); jout.closeEntry(); @@ -1304,6 +1328,66 @@ public class Jalview2XML } /** + * Copy the contents of a file to a new file added to the output jar + * + * @param jout + * @param infilePath + * @param jarfileName + */ + protected void copyFileToJar(JarOutputStream jout, String infilePath, + String jarfileName) + { + DataInputStream dis = null; + try + { + File file = new File(infilePath); + if (file.exists() && jout != null) + { + dis = new DataInputStream(new FileInputStream(file)); + byte[] data = new byte[(int) file.length()]; + dis.readFully(data); + writeJarEntry(jout, jarfileName, data); + } + } catch (Exception ex) + { + ex.printStackTrace(); + } finally + { + if (dis != null) + { + try + { + dis.close(); + } catch (IOException e) + { + // ignore + } + } + } + } + + /** + * Write the data to a new entry of given name in the output jar file + * + * @param jout + * @param jarfileName + * @param data + * @throws IOException + */ + protected void writeJarEntry(JarOutputStream jout, String jarfileName, + byte[] data) throws IOException + { + if (jout != null) + { + jout.putNextEntry(new JarEntry(jarfileName)); + DataOutputStream dout = new DataOutputStream(jout); + dout.write(data, 0, data.length); + dout.flush(); + jout.closeEntry(); + } + } + + /** * Save the state of a structure viewer * * @param ap @@ -1321,6 +1405,11 @@ public class Jalview2XML String matchedFile, StructureViewerBase viewFrame) { final AAStructureBindingModel bindingModel = viewFrame.getBinding(); + + /* + * Look for any bindings for this viewer to the PDB file of interest + * (including part matches excluding chain id) + */ for (int peid = 0; peid < bindingModel.getPdbCount(); peid++) { final PDBEntry pdbentry = bindingModel.getPdbEntry(peid); @@ -1329,6 +1418,9 @@ public class Jalview2XML && !(entry.getId().length() > 4 && entry.getId() .toLowerCase().startsWith(pdbId.toLowerCase()))) { + /* + * not interested in a binding to a different PDB entry here + */ continue; } if (matchedFile == null) @@ -1346,7 +1438,6 @@ public class Jalview2XML // can get at it if the ID // match is ambiguous (e.g. // 1QIP==1qipA) - String statestring = viewFrame.getStateInfo(); for (int smap = 0; smap < viewFrame.getBinding().getSequence()[peid].length; smap++) { @@ -1364,18 +1455,7 @@ public class Jalview2XML state.setAlignwithAlignPanel(viewFrame.isUsedforaligment(ap)); state.setColourwithAlignPanel(viewFrame.isUsedforcolourby(ap)); state.setColourByJmol(viewFrame.isColouredByViewer()); - /* - * Only store each structure viewer's state once in each XML document. - */ - if (!viewIds.contains(viewId)) - { - viewIds.add(viewId); - state.setContent(statestring.replaceAll("\n", "")); - } - else - { - state.setContent("# duplicate state"); - } + state.setType(viewFrame.getViewerType().toString()); pdb.addStructureState(state); } } @@ -2067,7 +2147,7 @@ public class Jalview2XML if (jarentry != null && jarentry.getName().endsWith(".xml")) { - InputStreamReader in = new InputStreamReader(jin, "UTF-8"); + InputStreamReader in = new InputStreamReader(jin, UTF_8); JalviewModel object = new JalviewModel(); Unmarshaller unmar = new Unmarshaller(object); @@ -2319,6 +2399,16 @@ public class Jalview2XML */ private final boolean updateLocalViews = false; + /** + * Returns the path to a temporary file holding the PDB file for the given PDB + * id. The first time of asking, searches for a file of that name in the + * Jalview project jar, and copies it to a new temporary file. Any repeat + * requests just return the path to the file previously created. + * + * @param jprovider + * @param pdbId + * @return + */ String loadPDBFile(jarInputStreamProvider jprovider, String pdbId) { if (alreadyLoadedPDB.containsKey(pdbId)) @@ -2326,6 +2416,31 @@ public class Jalview2XML return alreadyLoadedPDB.get(pdbId).toString(); } + String tempFile = copyJarEntry(jprovider, pdbId, "jalview_pdb"); + if (tempFile != null) + { + alreadyLoadedPDB.put(pdbId, tempFile); + } + return tempFile; + } + + /** + * Copies the jar entry of given name to a new temporary file and returns the + * path to the file, or null if the entry is not found. + * + * @param jprovider + * @param jarEntryName + * @param prefix + * a prefix for the temporary file name, must be at least three + * characters long + * @return + */ + protected String copyJarEntry(jarInputStreamProvider jprovider, + String jarEntryName, String prefix) + { + BufferedReader in = null; + PrintWriter out = null; + try { JarInputStream jin = jprovider.getJarInputStream(); @@ -2339,38 +2454,46 @@ public class Jalview2XML do { entry = jin.getNextJarEntry(); - } while (entry != null && !entry.getName().equals(pdbId)); + } while (entry != null && !entry.getName().equals(jarEntryName)); if (entry != null) { - BufferedReader in = new BufferedReader(new InputStreamReader(jin)); - File outFile = File.createTempFile("jalview_pdb", ".txt"); + in = new BufferedReader(new InputStreamReader(jin, UTF_8)); + File outFile = File.createTempFile(prefix, ".tmp"); outFile.deleteOnExit(); - PrintWriter out = new PrintWriter(new FileOutputStream(outFile)); + out = new PrintWriter(new FileOutputStream(outFile)); String data; while ((data = in.readLine()) != null) { out.println(data); } - try - { - out.flush(); - } catch (Exception foo) - { - } - ; - out.close(); + out.flush(); String t = outFile.getAbsolutePath(); - alreadyLoadedPDB.put(pdbId, t); return t; } else { - warn("Couldn't find PDB file entry in Jalview Jar for " + pdbId); + warn("Couldn't find entry in Jalview Jar for " + jarEntryName); } } catch (Exception ex) { ex.printStackTrace(); + } finally + { + if (in != null) + { + try + { + in.close(); + } catch (IOException e) + { + // ignore + } + } + if (out != null) + { + out.close(); + } } return null; @@ -3184,8 +3307,10 @@ public class Jalview2XML } if (!structureViewers.containsKey(sviewid)) { - structureViewers.put(sviewid, new StructureViewerModel(x, y, - width, height, false, false, true)); + structureViewers.put(sviewid, + new StructureViewerModel(x, y, width, height, false, + false, true, structureState.getViewId(), + structureState.getType())); // Legacy pre-2.7 conversion JAL-823 : // do not assume any view has to be linked for colour by // sequence @@ -3255,8 +3380,16 @@ public class Jalview2XML // Instantiate the associated structure views for (Entry entry : structureViewers .entrySet()) + { + try { - createOrLinkStructureViewer(entry, af, ap); + createOrLinkStructureViewer(entry, af, ap, jprovider); + } catch (Exception e) + { + System.err.println("Error loading structure viewer: " + + e.getMessage()); + // failed - try the next one + } } } @@ -3265,12 +3398,13 @@ public class Jalview2XML * @param viewerData * @param af * @param ap + * @param jprovider */ protected void createOrLinkStructureViewer( Entry viewerData, AlignFrame af, - AlignmentPanel ap) + AlignmentPanel ap, jarInputStreamProvider jprovider) { - final StructureViewerModel svattrib = viewerData.getValue(); + final StructureViewerModel stateData = viewerData.getValue(); /* * Search for any viewer windows already open from other alignment views @@ -3280,68 +3414,75 @@ public class Jalview2XML if (comp != null) { - linkStructureViewer(ap, comp, svattrib); + linkStructureViewer(ap, comp, stateData); return; } /* - * Pending an XML element for ViewerType, just check if stateData contains - * "chimera" (part of the chimera session filename). + * From 2.9: stateData.type contains JMOL or CHIMERA, data is in jar entry + * "viewer_"+stateData.viewId */ - if (svattrib.getStateData().indexOf("chimera") > -1) + if (ViewerType.CHIMERA.toString().equals(stateData.getType())) { - createChimeraViewer(viewerData, af); + createChimeraViewer(viewerData, af, jprovider); } else { - createJmolViewer(viewerData, af); + /* + * else Jmol (if pre-2.9, stateData contains JMOL state string) + */ + createJmolViewer(viewerData, af, jprovider); } } /** * Create a new Chimera viewer. * - * @param viewerData + * @param data * @param af + * @param jprovider */ - protected void createChimeraViewer( - Entry viewerData, AlignFrame af) + protected void createChimeraViewer(Entry viewerData, + AlignFrame af, + jarInputStreamProvider jprovider) { - final StructureViewerModel data = viewerData.getValue(); - String chimeraSession = data.getStateData(); - - if (new File(chimeraSession).exists()) - { - Set> fileData = data.getFileData() - .entrySet(); - List pdbs = new ArrayList(); - List allseqs = new ArrayList(); - for (Entry 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 seqList = pdb.getValue().getSeqList(); - SequenceI[] seqs = seqList.toArray(new SequenceI[seqList.size()]); - allseqs.add(seqs); - } + StructureViewerModel data = viewerData.getValue(); + String chimeraSessionFile = data.getStateData(); - boolean colourByChimera = data.isColourByViewer(); - boolean colourBySequence = data.isColourWithAlignPanel(); - - // TODO can/should this be done via StructureViewer (like Jmol)? - final PDBEntry[] pdbArray = pdbs.toArray(new PDBEntry[pdbs.size()]); - final SequenceI[][] seqsArray = allseqs.toArray(new SequenceI[allseqs - .size()][]); - new ChimeraViewFrame(chimeraSession, af.alignPanel, pdbArray, - seqsArray, colourByChimera, colourBySequence); - } - else - { - Cache.log.error("Chimera session file " + chimeraSession - + " not found"); - } + /* + * Copy Chimera session from jar entry "viewer_"+viewId to a temporary file + * + * Note this is the 'saved' viewId as in the project file XML, _not_ the + * 'uniquified' sviewid used to reconstruct the viewer here + */ + chimeraSessionFile = copyJarEntry(jprovider, + getViewerJarEntryName(data.getViewId()), "chimera"); + + Set> fileData = data.getFileData() + .entrySet(); + List pdbs = new ArrayList(); + List allseqs = new ArrayList(); + for (Entry 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 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(); + new ChimeraViewFrame(chimeraSessionFile, af.alignPanel, pdbArray, + seqsArray, colourByChimera, colourBySequence, newViewId); } /** @@ -3351,13 +3492,27 @@ public class Jalview2XML * * @param viewerData * @param af + * @param jprovider */ protected void createJmolViewer( final Entry viewerData, - AlignFrame af) + 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 pdbfilenames = new ArrayList(); List seqmaps = new ArrayList(); List pdbids = new ArrayList(); @@ -3457,8 +3612,6 @@ public class Jalview2XML JalviewStructureDisplayI sview = null; try { - // JAL-1333 note - we probably can't migrate Jmol views to UCSF - // Chimera! sview = new StructureViewer(alf.alignPanel .getStructureSelectionManager()).createView( StructureViewer.ViewerType.JMOL, pdbf, id, sq, @@ -3489,6 +3642,18 @@ public class Jalview2XML } /** + * Generates a name for the entry in the project jar file to hold state + * information for a structure viewer + * + * @param viewId + * @return + */ + protected String getViewerJarEntryName(String viewId) + { + return "viewer_" + viewId; + } + + /** * Returns any open frame that matches given structure viewer data. The match * is based on the unique viewId, or (for older project versions) the frame's * geometry. @@ -3514,8 +3679,8 @@ public class Jalview2XML && ((StructureViewerBase) frame).getViewId() .equals(sviewid)) { - comp = (AppJmol) frame; - // todo: break? + comp = (StructureViewerBase) frame; + break; // break added in 2.9 } /* * Otherwise test for matching position and size of viewer frame @@ -3525,8 +3690,8 @@ public class Jalview2XML && frame.getHeight() == svattrib.getHeight() && frame.getWidth() == svattrib.getWidth()) { - comp = (AppJmol) frame; - // todo: break? + comp = (StructureViewerBase) frame; + // no break in faint hope of an exact match on viewId } } } @@ -3544,15 +3709,15 @@ public class Jalview2XML * @param viewerColouring */ protected void linkStructureViewer(AlignmentPanel ap, - StructureViewerBase viewer, StructureViewerModel svattrib) + StructureViewerBase viewer, StructureViewerModel stateData) { // NOTE: if the jalview project is part of a shared session then // view synchronization should/could be done here. - final boolean useinViewerSuperpos = svattrib.isAlignWithPanel(); - final boolean usetoColourbyseq = svattrib.isColourWithAlignPanel(); - final boolean viewerColouring = svattrib.isColourByViewer(); - Map oldFiles = svattrib.getFileData(); + final boolean useinViewerSuperpos = stateData.isAlignWithPanel(); + final boolean usetoColourbyseq = stateData.isColourWithAlignPanel(); + final boolean viewerColouring = stateData.isColourByViewer(); + Map oldFiles = stateData.getFileData(); /* * Add mapping for sequences in this view to an already open viewer @@ -4851,4 +5016,67 @@ public class Jalview2XML { skipList = skipList2; } + + /** + * Reads the jar entry of given name and returns its contents, or null if the + * entry is not found. + * + * @param jprovider + * @param jarEntryName + * @return + */ + protected String readJarEntry(jarInputStreamProvider jprovider, + String jarEntryName) + { + String result = null; + BufferedReader in = null; + + try + { + /* + * Reopen the jar input stream and traverse its entries to find a matching + * name + */ + JarInputStream jin = jprovider.getJarInputStream(); + JarEntry entry = null; + do + { + entry = jin.getNextJarEntry(); + } while (entry != null && !entry.getName().equals(jarEntryName)); + + if (entry != null) + { + StringBuilder out = new StringBuilder(256); + in = new BufferedReader(new InputStreamReader(jin, UTF_8)); + String data; + + while ((data = in.readLine()) != null) + { + out.append(data); + } + result = out.toString(); + } + else + { + warn("Couldn't find entry in Jalview Jar for " + jarEntryName); + } + } catch (Exception ex) + { + ex.printStackTrace(); + } finally + { + if (in != null) + { + try + { + in.close(); + } catch (IOException e) + { + // ignore + } + } + } + + return result; + } } diff --git a/src/jalview/gui/StructureViewerBase.java b/src/jalview/gui/StructureViewerBase.java index 13af0e8..1feaa7c 100644 --- a/src/jalview/gui/StructureViewerBase.java +++ b/src/jalview/gui/StructureViewerBase.java @@ -1,8 +1,5 @@ package jalview.gui; -import jalview.gui.ViewSelectionMenu.ViewSetProvider; -import jalview.jbgui.GStructureViewer; - import java.awt.Component; import java.util.ArrayList; import java.util.List; @@ -10,6 +7,10 @@ import java.util.Vector; import javax.swing.JMenuItem; +import jalview.gui.StructureViewer.ViewerType; +import jalview.gui.ViewSelectionMenu.ViewSetProvider; +import jalview.jbgui.GStructureViewer; + /** * Base class with common functionality for JMol, Chimera or other structure * viewers. @@ -222,4 +223,6 @@ public abstract class StructureViewerBase extends GStructureViewer _colourwith.remove(nap); } } + + public abstract ViewerType getViewerType(); } diff --git a/src/jalview/io/FormatAdapter.java b/src/jalview/io/FormatAdapter.java index 7c117b9..d241308 100755 --- a/src/jalview/io/FormatAdapter.java +++ b/src/jalview/io/FormatAdapter.java @@ -70,10 +70,11 @@ public class FormatAdapter extends AppletFormatAdapter } public String formatSequences(String format, SequenceI[] seqs, - String[] omitHiddenColumns) + String[] omitHiddenColumns, int[] exportRange) { - return formatSequences(format, replaceStrings(seqs, omitHiddenColumns)); + return formatSequences(format, + replaceStrings(seqs, omitHiddenColumns, exportRange)); } /** @@ -85,15 +86,48 @@ public class FormatAdapter extends AppletFormatAdapter * @return new sequences */ public SequenceI[] replaceStrings(SequenceI[] seqs, - String[] omitHiddenColumns) + String[] omitHiddenColumns, int[] startEnd) { if (omitHiddenColumns != null) { SequenceI[] tmp = new SequenceI[seqs.length]; + + int startRes; + int endRes; + int startIndex; + int endIndex; for (int i = 0; i < seqs.length; i++) { + startRes = seqs[i].getStart(); + endRes = seqs[i].getEnd(); + + startIndex = startEnd[0]; + endIndex = startEnd[1]; + + if (startEnd != null) + { + // get first non-gaped residue start position + while (jalview.util.Comparison.isGap(seqs[i] + .getCharAt(startIndex)) && startIndex < endIndex) + { + startIndex++; + } + + // get last non-gaped residue end position + while (jalview.util.Comparison.isGap(seqs[i].getCharAt(endIndex)) + && endIndex > startIndex) + { + endIndex--; + } + + startRes = seqs[i].findPosition(startIndex); + startRes = seqs[i].getStart() > 1 ? startRes - seqs[i].getStart() + : startRes; + endRes = seqs[i].findPosition(endIndex) - seqs[i].getStart(); + } + tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i], - seqs[i].getStart(), seqs[i].getEnd()); + startRes, endRes); tmp[i].setDescription(seqs[i].getDescription()); } seqs = tmp; @@ -199,16 +233,17 @@ public class FormatAdapter extends AppletFormatAdapter } public String formatSequences(String format, AlignmentI alignment, - String[] omitHidden, ColumnSelection colSel) + String[] omitHidden, int[] exportRange, ColumnSelection colSel) { - return formatSequences(format, alignment, omitHidden, + return formatSequences(format, alignment, omitHidden, exportRange, getCacheSuffixDefault(format), colSel, null); } public String formatSequences(String format, AlignmentI alignment, - String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp) + String[] omitHidden, int[] exportRange, ColumnSelection colSel, + SequenceGroup sgp) { - return formatSequences(format, alignment, omitHidden, + return formatSequences(format, alignment, omitHidden, exportRange, getCacheSuffixDefault(format), colSel, sgp); } @@ -225,14 +260,17 @@ public class FormatAdapter extends AppletFormatAdapter * @return string representation of the alignment formatted as format */ public String formatSequences(String format, AlignmentI alignment, - String[] omitHidden, boolean suffix, ColumnSelection colSel) + String[] omitHidden, int[] exportRange, boolean suffix, + ColumnSelection colSel) { - return formatSequences(format, alignment, omitHidden, suffix, colSel, + return formatSequences(format, alignment, omitHidden, exportRange, + suffix, colSel, null); } public String formatSequences(String format, AlignmentI alignment, - String[] omitHidden, boolean suffix, ColumnSelection colSel, + String[] omitHidden, int[] exportRange, boolean suffix, + ColumnSelection colSel, jalview.datamodel.SequenceGroup selgp) { if (omitHidden != null) @@ -242,7 +280,7 @@ public class FormatAdapter extends AppletFormatAdapter // TODO: JAL-1486 - set start and end for output correctly. basically, // AlignmentView.getVisibleContigs does this. Alignment alv = new Alignment(replaceStrings( - alignment.getSequencesArray(), omitHidden)); + alignment.getSequencesArray(), omitHidden, exportRange)); AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation(); if (ala != null) { diff --git a/src/jalview/jbgui/GStructureViewer.java b/src/jalview/jbgui/GStructureViewer.java index c76fb23..7885f74 100644 --- a/src/jalview/jbgui/GStructureViewer.java +++ b/src/jalview/jbgui/GStructureViewer.java @@ -348,6 +348,7 @@ public abstract class GStructureViewer extends JInternalFrame implements colourButtons.add(strandColour); colourButtons.add(turnColour); colourButtons.add(buriedColour); + colourButtons.add(purinePyrimidineColour); colourButtons.add(userColour); colourButtons.add(viewerColour);