JAL-892 JAL-1779 save/restore Varna viewer to/from project
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 18 Jun 2015 16:13:10 +0000 (17:13 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 18 Jun 2015 16:13:10 +0000 (17:13 +0100)
src/jalview/datamodel/RnaViewerModel.java [new file with mode: 0644]
src/jalview/ext/varna/RnaModel.java [new file with mode: 0644]
src/jalview/gui/AppVarna.java
src/jalview/gui/AppVarnaBinding.java
src/jalview/gui/Jalview2XML.java
src/jalview/gui/PopupMenu.java

diff --git a/src/jalview/datamodel/RnaViewerModel.java b/src/jalview/datamodel/RnaViewerModel.java
new file mode 100644 (file)
index 0000000..33783ec
--- /dev/null
@@ -0,0 +1,46 @@
+package jalview.datamodel;
+
+
+/**
+ * A data bean class to hold properties of an RNA viewer
+ */
+public class RnaViewerModel
+{
+  public final String viewId;
+
+  public final String title;
+
+  public final int x;
+
+  public final int y;
+
+  public final int width;
+
+  public final int height;
+
+  public final int dividerLocation;
+
+  /**
+   * Constructor
+   * 
+   * @param viewId
+   * @param title
+   * @param xpos
+   * @param ypos
+   * @param width
+   * @param height
+   * @param dividerLocation
+   */
+  public RnaViewerModel(String viewId, String title, int xpos, int ypos,
+          int width,
+ int height, int dividerLocation)
+  {
+    this.viewId = viewId;
+    this.title = title;
+    this.x = xpos;
+    this.y = ypos;
+    this.width = width;
+    this.height = height;
+    this.dividerLocation = dividerLocation;
+  }
+}
diff --git a/src/jalview/ext/varna/RnaModel.java b/src/jalview/ext/varna/RnaModel.java
new file mode 100644 (file)
index 0000000..44c6cf6
--- /dev/null
@@ -0,0 +1,37 @@
+package jalview.ext.varna;
+
+import fr.orsay.lri.varna.models.rna.RNA;
+
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.SequenceI;
+
+/**
+ * Data bean wrapping the data items that define one RNA view
+ */
+public class RnaModel
+{
+  public final String title;
+
+  public final AlignmentAnnotation ann;
+
+  public final SequenceI seq;
+
+  public final boolean gapped;
+
+  public final RNA rna;
+
+  // path to a file holding VARNA session state XML
+  public final String varnaSession;
+
+  public RnaModel(String t, AlignmentAnnotation aa, SequenceI s, RNA r,
+          boolean g,
+          String sessionFile)
+  {
+    title = t;
+    ann = aa;
+    seq = s;
+    gapped = g;
+    rna = r;
+    varnaSession = sessionFile;
+  }
+}
\ No newline at end of file
index 5c3785a..9316b05 100644 (file)
@@ -22,8 +22,10 @@ package jalview.gui;
 
 import java.awt.BorderLayout;
 import java.awt.Color;
-import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Hashtable;
+import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -35,102 +37,207 @@ import javax.swing.event.InternalFrameEvent;
 
 import fr.orsay.lri.varna.VARNAPanel;
 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
+import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
-import fr.orsay.lri.varna.interfaces.InterfaceVARNAListener;
 import fr.orsay.lri.varna.interfaces.InterfaceVARNASelectionListener;
 import fr.orsay.lri.varna.models.BaseList;
-import fr.orsay.lri.varna.models.VARNAConfig;
+import fr.orsay.lri.varna.models.FullBackup;
 import fr.orsay.lri.varna.models.annotations.HighlightRegionAnnotation;
 import fr.orsay.lri.varna.models.rna.ModeleBase;
 import fr.orsay.lri.varna.models.rna.RNA;
 
-import jalview.bin.Cache;
+import jalview.analysis.AlignSeq;
+import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.RnaViewerModel;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
+import jalview.ext.varna.RnaModel;
 import jalview.structure.SecondaryStructureListener;
 import jalview.structure.SelectionListener;
 import jalview.structure.SelectionSource;
 import jalview.structure.StructureSelectionManager;
 import jalview.structure.VamsasSource;
+import jalview.util.Comparison;
 import jalview.util.MessageManager;
 import jalview.util.ShiftList;
 
-public class AppVarna extends JInternalFrame implements
-        InterfaceVARNAListener, SelectionListener,
-        SecondaryStructureListener// implements
-                                  // Runnable,SequenceStructureBinding,
-                                  // ViewSetProvider
-        , InterfaceVARNASelectionListener, VamsasSource
-
+public class AppVarna extends JInternalFrame implements SelectionListener,
+        SecondaryStructureListener, InterfaceVARNASelectionListener,
+        VamsasSource
 {
-  AppVarnaBinding vab;
+  private static final Pattern PAIRS_PATTERN = Pattern
+          .compile("[^([{<>}])]");
+
+  private AppVarnaBinding vab;
 
-  VARNAPanel varnaPanel;
+  private AlignmentPanel ap;
 
-  public String name;
+  private String viewId;
 
-  public StructureSelectionManager ssm;
+  private StructureSelectionManager ssm;
 
   /*
-   * public AppVarna(){ vab = new AppVarnaBinding(); initVarna(); }
+   * Lookup for sequence and annotation mapped to each RNA in the viewer. Using
+   * a linked hashmap means that order is preserved when saved to the project.
    */
+  private Map<RNA, RnaModel> models = new LinkedHashMap<RNA, RnaModel>();
+
+  private Map<RNA, ShiftList> offsets = new Hashtable<RNA, ShiftList>();
+
+  private Map<RNA, ShiftList> offsetsInv = new Hashtable<RNA, ShiftList>();
+
+  private JSplitPane split;
 
-  AlignmentPanel ap;
+  private VarnaHighlighter mouseOverHighlighter = new VarnaHighlighter();
 
-  public AppVarna(String sname, SequenceI seq, String strucseq,
-          String struc, String name, AlignmentPanel ap)
+  private VarnaHighlighter selectionHighlighter = new VarnaHighlighter();
+
+  private class VarnaHighlighter
   {
+    private HighlightRegionAnnotation _lastHighlight;
 
-    // System.out.println("1:"+sname);
-    // System.out.println("2:"+seq);
-    // System.out.println("3:"+strucseq);
-    // System.out.println("4:"+struc);
-    // System.out.println("5:"+name);
-    // System.out.println("6:"+ap);
-    this.ap = ap;
-    ArrayList<RNA> rnaList = new ArrayList<RNA>();
-    RNA rna1 = new RNA(name);
-    try
+    private RNA _lastRNAhighlighted = null;
+
+    public VarnaHighlighter()
     {
 
-      rna1.setRNA(strucseq, replaceOddGaps(struc));
-      // System.out.println("The sequence is :"+rna1.getSeq());
-      // System.out.println("The sequence is:"+struc);
-      // System.out.println("The sequence is:"+replaceOddGaps(struc).toString());
-    } catch (ExceptionUnmatchedClosingParentheses e2)
+    }
+
+    /**
+     * Constructor when restoring from Varna session, including any highlight
+     * state
+     * 
+     * @param rna
+     */
+    public VarnaHighlighter(RNA rna)
     {
-      e2.printStackTrace();
-    } catch (ExceptionFileFormatOrSyntax e3)
+      // TODO nice try but doesn't work; do we need a highlighter per model?
+      _lastRNAhighlighted = rna;
+      List<HighlightRegionAnnotation> highlights = rna.getHighlightRegion();
+      if (highlights != null && !highlights.isEmpty())
+      {
+        _lastHighlight = highlights.get(0);
+      }
+    }
+
+    public void highlightRegion(RNA rna, int start, int end)
     {
-      e3.printStackTrace();
+      clearLastSelection();
+      HighlightRegionAnnotation highlight = new HighlightRegionAnnotation(
+              rna.getBasesBetween(start, end));
+      rna.addHighlightRegion(highlight);
+      _lastHighlight = highlight;
+      _lastRNAhighlighted = rna;
     }
-    RNA trim = trimRNA(rna1, "trimmed " + sname);
-    rnaList.add(trim);
-    rnaList.add(rna1);
 
-    rnas.put(seq, rna1);
-    rnas.put(seq, trim);
-    rna1.setName(sname + " (with gaps)");
+    public HighlightRegionAnnotation getLastHighlight()
+    {
+      return _lastHighlight;
+    }
 
+    /**
+     * Clears all structure selection and refreshes the display
+     */
+    public void clearSelection()
     {
-      seqs.put(trim, seq);
-      seqs.put(rna1, seq);
+      if (_lastRNAhighlighted != null)
+      {
+        _lastRNAhighlighted.getHighlightRegion().clear();
+        vab.updateSelectedRNA(_lastRNAhighlighted);
+        _lastRNAhighlighted = null;
+        _lastHighlight = null;
+      }
+    }
 
-      /**
-       * if (false || seq.getStart()!=1) { for (RNA rshift:rnaList) { ShiftList
-       * shift=offsets.get(rshift); if (shift==null) { offsets.put(rshift,
-       * shift=new ShiftList());} shift.addShift(1, seq.getStart()-1);
-       * offsetsInv.put(rshift, shift.getInverse()); } }
-       **/
+    /**
+     * Clear the last structure selection
+     */
+    public void clearLastSelection()
+    {
+      if (_lastRNAhighlighted != null)
+      {
+        _lastRNAhighlighted.removeHighlightRegion(_lastHighlight);
+        _lastRNAhighlighted = null;
+        _lastHighlight = null;
+      }
     }
-    vab = new AppVarnaBinding(rnaList);
-    // vab = new AppVarnaBinding(seq,struc);
-    this.name = sname + " trimmed to " + name;
+  }
+
+  /**
+   * Constructor
+   * 
+   * @param sname
+   *          a descriptive name
+   * @param seq
+   *          the RNA sequence
+   * @param aa
+   *          the annotation with the secondary structure string
+   * @param ap
+   *          the AlignmentPanel creating this object
+   */
+  public AppVarna(String sname, SequenceI seq, AlignmentAnnotation aa,
+          AlignmentPanel ap)
+  {
+    this(ap);
+
+    String name = sname + " trimmed to " + seq.getName();
+    String fullName = MessageManager.formatMessage("label.varna_params",
+            new String[]
+            { name });
+    setTitle(fullName);
+
+    /*
+     * if (!aa.isValidStruc()) { throw new
+     * IllegalArgumentException("Invalid RNA structure annotation"); } final
+     * String struc = aa.getRNAStruc();
+     * 
+     * String strucseq = seq.getSequenceAsString();
+     * 
+     * String gappedTitle = sname + " (with gaps)"; String rnaTitle =
+     * gappedTitle; RNA gapped = new RNA(rnaTitle); try {
+     * gapped.setRNA(strucseq, replaceOddGaps(struc)); } catch
+     * (ExceptionUnmatchedClosingParentheses e2) { e2.printStackTrace(); } catch
+     * (ExceptionFileFormatOrSyntax e3) { e3.printStackTrace(); }
+     * models.put(gapped, new RnaModel(rnaTitle, aa, seq, gapped, true, null));
+     * 
+     * String trimmedTitle = "trimmed " + sname; rnaTitle = trimmedTitle; RNA
+     * trimmed = trimRNA(gapped, rnaTitle); models.put(trimmed, new
+     * RnaModel(rnaTitle, aa, seq, trimmed, false, null));
+     */
+    // vab = new AppVarnaBinding(Arrays.asList(new RNA[]
+    // { trimmed, gapped }));
+    // vab = new AppVarnaBinding();
+    // // String seqName = seq.getName();
+    // // String name = sname + " trimmed to " + seqName;
+    // initVarna(name);
+
+    String gappedTitle = sname + " (with gaps)";
+    RnaModel gappedModel = new RnaModel(gappedTitle, aa, seq, null, true,
+            null);
+    addModel(gappedModel, gappedTitle);
+
+    String trimmedTitle = "trimmed " + sname;
+    RnaModel trimmedModel = new RnaModel(trimmedTitle, aa, seq, null,
+            false, null);
+    addModel(trimmedModel, trimmedTitle);
+    vab.setSelectedIndex(0);
+  }
+
+  /**
+   * Constructor that links the viewer to a parent panel (but has no structures
+   * yet - use addModel to add them)
+   * 
+   * @param ap
+   */
+  protected AppVarna(AlignmentPanel ap)
+  {
+    this.ap = ap;
+    this.viewId = System.currentTimeMillis() + "." + this.hashCode();
+    vab = new AppVarnaBinding();
     initVarna();
 
-    ssm = ap.getStructureSelectionManager();
-    // System.out.println(ssm.toString());
+    this.ssm = ap.getStructureSelectionManager();
     ssm.addStructureViewerListener(this);
     ssm.addSelectionListener(this);
     addInternalFrameListener(new InternalFrameAdapter()
@@ -141,40 +248,59 @@ public class AppVarna extends JInternalFrame implements
         close();
       }
     });
+  }
 
+  /**
+   * Constructor given viewer data read from a saved project file
+   * 
+   * @param model
+   * @param ap
+   *          the (or a) parent alignment panel
+   */
+  public AppVarna(RnaViewerModel model, AlignmentPanel ap)
+  {
+    this(ap);
+    setTitle(model.title);
+    this.viewId = model.viewId;
+    setBounds(model.x, model.y, model.width, model.height);
+    this.split.setDividerLocation(model.dividerLocation);
   }
 
+  /**
+   * Constructs a split pane with an empty selection list and display panel, and
+   * adds it to the desktop
+   */
   public void initVarna()
   {
-
-    // vab.setFinishedInit(false);
-    varnaPanel = vab.get_varnaPanel();
+    VARNAPanel varnaPanel = vab.get_varnaPanel();
     setBackground(Color.white);
-    JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
+    split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
             vab.getListPanel(), varnaPanel);
     getContentPane().setLayout(new BorderLayout());
     getContentPane().add(split, BorderLayout.CENTER);
-    // getContentPane().add(vab.getTools(), BorderLayout.NORTH);
-    varnaPanel.addVARNAListener(this);
+
     varnaPanel.addSelectionListener(this);
-    jalview.gui.Desktop.addInternalFrame(this,
-            MessageManager.formatMessage("label.varna_params", new String[]
-            { name }), getBounds().width, getBounds().height);
+    jalview.gui.Desktop.addInternalFrame(this, "", getBounds().width,
+            getBounds().height);
     this.pack();
     showPanel(true);
-
   }
 
   public String replaceOddGaps(String oldStr)
   {
-    String patternStr = "[^([{<>}])]";
-    String replacementStr = ".";
-    Pattern pattern = Pattern.compile(patternStr);
-    Matcher matcher = pattern.matcher(oldStr);
-    String newStr = matcher.replaceAll(replacementStr);
+    Matcher matcher = PAIRS_PATTERN.matcher(oldStr);
+    String newStr = matcher.replaceAll(".");
     return newStr;
   }
 
+  /**
+   * Constructs a new RNA model from the given one, without gaps. Also
+   * calculates and saves a 'shift list'
+   * 
+   * @param rna
+   * @param name
+   * @return
+   */
   public RNA trimRNA(RNA rna, String name)
   {
     ShiftList offset = new ShiftList();
@@ -191,19 +317,23 @@ public class AppVarna extends JInternalFrame implements
       e3.printStackTrace();
     }
 
-    StringBuffer seq = new StringBuffer(rnaTrim.getSeq());
-    StringBuffer struc = new StringBuffer(rnaTrim.getStructDBN());
-    int ofstart = -1, sleng = rnaTrim.getSeq().length();
+    String seq = rnaTrim.getSeq();
+    StringBuilder struc = new StringBuilder(256);
+    struc.append(rnaTrim.getStructDBN());
+    int ofstart = -1;
+    int sleng = seq.length();
+
     for (int i = 0; i < sleng; i++)
     {
-      // TODO: Jalview utility for gap detection java.utils.isGap()
-      // TODO: Switch to jalview rna datamodel
-      if (jalview.util.Comparison.isGap(seq.charAt(i)))
+      if (Comparison.isGap(seq.charAt(i)))
       {
         if (ofstart == -1)
         {
           ofstart = i;
         }
+        /*
+         * mark base or base pair in the structure with *
+         */
         if (!rnaTrim.findPair(i).isEmpty())
         {
           int m = rnaTrim.findPair(i).get(1);
@@ -232,35 +362,37 @@ public class AppVarna extends JInternalFrame implements
       offset.addShift(offset.shift(ofstart), ofstart - sleng);
       ofstart = -1;
     }
-    String newSeq = rnaTrim.getSeq().replace("-", "");
-    rnaTrim.getSeq().replace(".", "");
+
+    /*
+     * remove the marked gaps from the structure
+     */
     String newStruc = struc.toString().replace("*", "");
 
+    /*
+     * remove gaps from the sequence
+     */
+    String newSeq = AlignSeq.extractGaps(Comparison.GapChars, seq);
+
     try
     {
       rnaTrim.setRNA(newSeq, newStruc);
       registerOffset(rnaTrim, offset);
     } catch (ExceptionUnmatchedClosingParentheses e)
     {
-      // TODO Auto-generated catch block
       e.printStackTrace();
     } catch (ExceptionFileFormatOrSyntax e)
     {
-      // TODO Auto-generated catch block
       e.printStackTrace();
     }
     return rnaTrim;
   }
 
-  // needs to be many-many
-  Map<RNA, SequenceI> seqs = new Hashtable<RNA, SequenceI>();
-
-  Map<SequenceI, RNA> rnas = new Hashtable<SequenceI, RNA>();
-
-  Map<RNA, ShiftList> offsets = new Hashtable<RNA, ShiftList>();
-
-  Map<RNA, ShiftList> offsetsInv = new Hashtable<RNA, ShiftList>();
-
+  /**
+   * Save the sequence to structure mapping, and also its inverse.
+   * 
+   * @param rnaTrim
+   * @param offset
+   */
   private void registerOffset(RNA rnaTrim, ShiftList offset)
   {
     offsets.put(rnaTrim, offset);
@@ -272,83 +404,6 @@ public class AppVarna extends JInternalFrame implements
     this.setVisible(show);
   }
 
-  private boolean _started = false;
-
-  public void run()
-  {
-    _started = true;
-
-    try
-    {
-      initVarna();
-    } catch (OutOfMemoryError oomerror)
-    {
-      new OOMWarning("When trying to open the Varna viewer!", oomerror);
-    } catch (Exception ex)
-    {
-      Cache.log.error("Couldn't open Varna viewer!", ex);
-    }
-  }
-
-  @Override
-  public void onUINewStructure(VARNAConfig v, RNA r)
-  {
-
-  }
-
-  @Override
-  public void onWarningEmitted(String s)
-  {
-    // TODO Auto-generated method stub
-
-  }
-
-  private class VarnaHighlighter
-  {
-    private HighlightRegionAnnotation _lastHighlight;
-
-    private RNA _lastRNAhighlighted = null;
-
-    public void highlightRegion(RNA rna, int start, int end)
-    {
-      clearSelection(null);
-      HighlightRegionAnnotation highlight = new HighlightRegionAnnotation(
-              rna.getBasesBetween(start, end));
-      rna.addHighlightRegion(highlight);
-      _lastHighlight = highlight;
-      _lastRNAhighlighted = rna;
-
-    }
-
-    public HighlightRegionAnnotation getLastHighlight()
-    {
-      return _lastHighlight;
-    }
-
-    public RNA getLastRNA()
-    {
-      return _lastRNAhighlighted;
-    }
-
-    public void clearSelection(AppVarnaBinding vab)
-    {
-      if (_lastRNAhighlighted != null)
-      {
-        _lastRNAhighlighted.removeHighlightRegion(_lastHighlight);
-        if (vab != null)
-        {
-          vab.updateSelectedRNA(_lastRNAhighlighted);
-        }
-        _lastRNAhighlighted = null;
-        _lastHighlight = null;
-
-      }
-    }
-  }
-
-  VarnaHighlighter mouseOverHighlighter = new VarnaHighlighter(),
-          selectionHighlighter = new VarnaHighlighter();
-
   /**
    * If a mouseOver event from the AlignmentPanel is noticed the currently
    * selected RNA in the VARNA window is highlighted at the specific position.
@@ -359,7 +414,11 @@ public class AppVarna extends JInternalFrame implements
   public void mouseOverSequence(SequenceI sequence, int index)
   {
     RNA rna = vab.getSelectedRNA();
-    if (seqs.get(rna) == sequence)
+    if (rna == null)
+    {
+      return;
+    }
+    if (models.get(rna).seq == sequence)
     {
       ShiftList shift = offsets.get(rna);
       if (shift != null)
@@ -374,13 +433,6 @@ public class AppVarna extends JInternalFrame implements
   }
 
   @Override
-  public void onStructureRedrawn()
-  {
-    // TODO Auto-generated method stub
-
-  }
-
-  @Override
   public void selection(SequenceGroup seqsel, ColumnSelection colsel,
           SelectionSource source)
   {
@@ -390,10 +442,14 @@ public class AppVarna extends JInternalFrame implements
       // TODO - reuse many-one panel-view system in jmol viewer
       return;
     }
+    RNA rna = vab.getSelectedRNA();
+    if (rna == null)
+    {
+      return;
+    }
     if (seqsel != null && seqsel.getSize() > 0)
     {
       int start = seqsel.getStartRes(), end = seqsel.getEndRes();
-      RNA rna = vab.getSelectedRNA();
       ShiftList shift = offsets.get(rna);
       if (shift != null)
       {
@@ -408,27 +464,30 @@ public class AppVarna extends JInternalFrame implements
     }
     else
     {
-      selectionHighlighter.clearSelection(vab);
+      selectionHighlighter.clearSelection();
     }
   }
 
+  /**
+   * Respond to a change of the base hovered over in the Varna viewer
+   */
   @Override
-  public void onHoverChanged(ModeleBase arg0, ModeleBase arg1)
+  public void onHoverChanged(ModeleBase previousBase, ModeleBase newBase)
   {
     RNA rna = vab.getSelectedRNA();
     ShiftList shift = offsetsInv.get(rna);
-    SequenceI seq = seqs.get(rna);
-    if (arg1 != null && seq != null)
+    SequenceI seq = models.get(rna).seq;
+    if (newBase != null && seq != null)
     {
       if (shift != null)
       {
-        int i = shift.shift(arg1.getIndex());
+        int i = shift.shift(newBase.getIndex());
         // System.err.println("shifted "+(arg1.getIndex())+" to "+i);
         ssm.mouseOverVamsasSequence(seq, i, this);
       }
       else
       {
-        ssm.mouseOverVamsasSequence(seq, arg1.getIndex(), this);
+        ssm.mouseOverVamsasSequence(seq, newBase.getIndex(), this);
       }
     }
   }
@@ -441,18 +500,56 @@ public class AppVarna extends JInternalFrame implements
 
   }
 
-  @Override
-  public void onTranslationChanged()
+  /**
+   * Returns the path to a temporary file containing a representation of the
+   * state of one Varna display
+   * 
+   * @param rna
+   * 
+   * @return
+   */
+  public String getStateInfo(RNA rna)
+  {
+    return vab.getStateInfo(rna);
+  }
+
+  public AlignmentPanel getAlignmentPanel()
   {
-    // TODO Auto-generated method stub
+    return ap;
+  }
 
+  public String getViewId()
+  {
+    return viewId;
   }
 
-  @Override
-  public void onZoomLevelChanged()
+  /**
+   * Returns true if any of the viewer's models (not necessarily the one
+   * currently displayed) is for the given sequence
+   * 
+   * @param seq
+   * @return
+   */
+  public boolean isListeningFor(SequenceI seq)
   {
-    // TODO Auto-generated method stub
+    for (RnaModel model : models.values())
+    {
+      if (model.seq == seq)
+      {
+        return true;
+      }
+    }
+    return false;
+  }
 
+  /**
+   * Returns a value representing the horizontal split divider location
+   * 
+   * @return
+   */
+  public int getDividerLocation()
+  {
+    return split == null ? 0 : split.getDividerLocation();
   }
 
   /**
@@ -461,7 +558,7 @@ public class AppVarna extends JInternalFrame implements
   protected void close()
   {
     /*
-     * Deregister as a listener, to free references to this object
+     * Deregister as a listener, to release references to this object
      */
     if (ssm != null)
     {
@@ -470,4 +567,158 @@ public class AppVarna extends JInternalFrame implements
     }
   }
 
+  /**
+   * Returns the secondary structure annotation that this viewer displays for
+   * the given sequence
+   * 
+   * @return
+   */
+  public AlignmentAnnotation getAnnotation(SequenceI seq)
+  {
+    for (RnaModel model : models.values())
+    {
+      if (model.seq == seq)
+      {
+        return model.ann;
+      }
+    }
+    return null;
+  }
+
+  public int getSelectedIndex()
+  {
+    return this.vab.getSelectedIndex();
+  }
+
+  /**
+   * Returns the set of models shown by the viewer
+   * 
+   * @return
+   */
+  public Collection<RnaModel> getModels()
+  {
+    return models.values();
+  }
+
+  /**
+   * Add a model (e.g. loaded from project file)
+   * 
+   * @param rna
+   * @param modelName
+   */
+  public RNA addModel(RnaModel model, String modelName)
+  {
+    if (!model.ann.isValidStruc())
+    {
+      throw new IllegalArgumentException("Invalid RNA structure annotation");
+    }
+
+    /*
+     * loaded from project file with Varna session data
+     */
+    if (model.varnaSession != null)
+    {
+      try
+      {
+        FullBackup fromSession = vab.vp.loadSession(model.varnaSession);
+        vab.addStructure(fromSession.rna, fromSession.config);
+        RNA rna = fromSession.rna;
+        // copy the model, but now including the RNA object
+        RnaModel newModel = new RnaModel(model.title, model.ann, model.seq,
+                rna, model.gapped, model.varnaSession);
+        if (!model.gapped)
+        {
+          registerOffset(rna, buildOffset(model.seq));
+        }
+        // TODO and add mapping (offsets)
+        models.put(rna, newModel);
+        // capture rna selection state when saved
+        selectionHighlighter = new VarnaHighlighter(rna);
+        return fromSession.rna;
+      } catch (ExceptionLoadingFailed e)
+      {
+        e.printStackTrace();
+        return null;
+      }
+    }
+
+    /*
+     * opened on request in Jalview session
+     */
+    RNA rna = new RNA(modelName);
+    String struc = model.ann.getRNAStruc();
+    struc = replaceOddGaps(struc);
+
+    String strucseq = model.seq.getSequenceAsString();
+    try
+    {
+      rna.setRNA(strucseq, struc);
+    } catch (ExceptionUnmatchedClosingParentheses e2)
+    {
+      e2.printStackTrace();
+    } catch (ExceptionFileFormatOrSyntax e3)
+    {
+      e3.printStackTrace();
+    }
+
+    if (!model.gapped)
+    {
+      rna = trimRNA(rna, modelName);
+    }
+    models.put(rna, new RnaModel(modelName, model.ann, model.seq, rna,
+            model.gapped, null));
+    vab.addStructure(rna);
+    return rna;
+  }
+
+  /**
+   * Constructs a shift list that describes the gaps in the sequence
+   * 
+   * @param seq
+   * @return
+   */
+  protected ShiftList buildOffset(SequenceI seq)
+  {
+    // TODO refactor to avoid duplication with trimRNA()
+    ShiftList offset = new ShiftList();
+    int ofstart = -1;
+    int sleng = seq.getLength();
+    char[] seqChars = seq.getSequence();
+
+    for (int i = 0; i < sleng; i++)
+    {
+      if (Comparison.isGap(seqChars[i]))
+      {
+        if (ofstart == -1)
+        {
+          ofstart = i;
+        }
+      }
+      else
+      {
+        if (ofstart > -1)
+        {
+          offset.addShift(offset.shift(ofstart), ofstart - i);
+          ofstart = -1;
+        }
+      }
+    }
+    // final gap
+    if (ofstart > -1)
+    {
+      offset.addShift(offset.shift(ofstart), ofstart - sleng);
+      ofstart = -1;
+    }
+    return offset;
+  }
+
+  /**
+   * Set the selected index in the model selection list
+   * 
+   * @param selectedRna
+   */
+  public void setSelectedIndex(int selectedRna)
+  {
+    vab.setSelectedIndex(selectedRna);
+  }
 }
index 38385f0..3ea8e4d 100644 (file)
@@ -25,33 +25,27 @@ import java.awt.Color;
 import java.awt.Component;
 import java.awt.Dimension;
 import java.awt.Font;
-import java.awt.GridLayout;
 import java.awt.datatransfer.DataFlavor;
 import java.awt.datatransfer.Transferable;
 import java.awt.dnd.DnDConstants;
 import java.awt.dnd.DropTarget;
-import java.awt.dnd.DropTargetDragEvent;
+import java.awt.dnd.DropTargetAdapter;
 import java.awt.dnd.DropTargetDropEvent;
-import java.awt.dnd.DropTargetEvent;
-import java.awt.dnd.DropTargetListener;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
 import java.awt.event.ComponentEvent;
+import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
-import java.awt.event.MouseListener;
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
 import javax.swing.DefaultListModel;
 import javax.swing.DefaultListSelectionModel;
-import javax.swing.JButton;
 import javax.swing.JLabel;
 import javax.swing.JList;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
-import javax.swing.JTextField;
 import javax.swing.ListModel;
 import javax.swing.ListSelectionModel;
 import javax.swing.event.ListSelectionEvent;
@@ -59,70 +53,44 @@ import javax.swing.event.ListSelectionListener;
 
 import fr.orsay.lri.varna.VARNAPanel;
 import fr.orsay.lri.varna.components.ReorderableJList;
-import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
 import fr.orsay.lri.varna.exceptions.ExceptionNonEqualLength;
-import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
-import fr.orsay.lri.varna.interfaces.InterfaceVARNAListener;
 import fr.orsay.lri.varna.models.FullBackup;
 import fr.orsay.lri.varna.models.VARNAConfig;
-import fr.orsay.lri.varna.models.rna.Mapping;
 import fr.orsay.lri.varna.models.rna.RNA;
 
 import jalview.datamodel.SequenceI;
+import jalview.ext.varna.JalviewVarnaBinding;
 import jalview.structure.AtomSpec;
 import jalview.util.MessageManager;
 
-public class AppVarnaBinding extends jalview.ext.varna.JalviewVarnaBinding
-        implements DropTargetListener, InterfaceVARNAListener,
-        MouseListener
+public class AppVarnaBinding extends JalviewVarnaBinding
 {
-
-  /**
-        * 
-        */
-  // private static final long serialVersionUID = -790155708306987257L;
-
-  private String DEFAULT_SEQUENCE = "CAGCACGACACUAGCAGUCAGUGUCAGACUGCAIACAGCACGACACUAGCAGUCAGUGUCAGACUGCAIACAGCACGACACUAGCAGUCAGUGUCAGACUGCAIA";
-
-  private String DEFAULT_STRUCTURE1 = "..(((((...(((((...(((((...(((((.....)))))...))))).....(((((...(((((.....)))))...))))).....)))))...)))))..";
-
-  private String DEFAULT_STRUCTURE2 = "..(((((...(((((...(((((........(((((...(((((.....)))))...)))))..................))))).....)))))...)))))..";
-
   public VARNAPanel vp;
 
-  protected JPanel _tools = new JPanel();
-
-  private JPanel _input = new JPanel();
-
-  private JPanel _seqPanel = new JPanel();
-
-  private JPanel _strPanel = new JPanel();
-
-  private JLabel _info = new JLabel();
-
-  private JTextField _str = new JTextField();
-
-  private JTextField _seq = new JTextField();
-
-  private JLabel _strLabel = new JLabel(
-          MessageManager.getString("label.str"));
-
-  private JLabel _seqLabel = new JLabel(
-          MessageManager.getString("label.seq"));
-
-  private JButton _createButton = new JButton(
-          MessageManager.getString("action.create"));
-
-  private JButton _updateButton = new JButton(
-          MessageManager.getString("action.update"));
-
-  private JButton _deleteButton = new JButton(
-          MessageManager.getString("action.delete"));
-
-  private JButton _duplicateButton = new JButton(
-          MessageManager.getString("action.snapshot"));
-
+  // remove unused (commented out) fields?
+  // protected JPanel _tools = new JPanel();
+  //
+  // private JPanel _input = new JPanel();
+  //
+  // private JPanel _strPanel = new JPanel();
+  //
+  // private JTextField _str = new JTextField();
+  //
+  // private JTextField _seq = new JTextField();
+  //
+  // private JLabel _strLabel = new JLabel(
+  // MessageManager.getString("label.str"));
+  //
+  // private JButton _updateButton = new JButton(
+  // MessageManager.getString("action.update"));
+  //
+  // private JButton _deleteButton = new JButton(
+  // MessageManager.getString("action.delete"));
+  //
+  // private JButton _duplicateButton = new JButton(
+  // MessageManager.getString("action.snapshot"));
+  //
   protected JPanel _listPanel = new JPanel();
 
   private ReorderableJList _sideList = null;
@@ -141,32 +109,23 @@ public class AppVarnaBinding extends jalview.ext.varna.JalviewVarnaBinding
 
   private BackupHolder _rnaList;
 
-  /*
-   * public AppVarnaBinding() { //super("VARNA in Jalview");
-   * //this.set_seq("ATGC"); //this.set_str(".()."); //RNAPanelDemoInit();
-   * 
-   * //initVarna("ATGCATGATATATATATAT","....((((...))))....");
-   * initVarna(this.DEFAULT_SEQUENCE,this.DEFAULT_STRUCTURE1); }
+  /**
+   * Constructor
    */
-
-  public AppVarnaBinding(String seq, String struc)
-  {
-    // super("VARNA in Jalview");
-    initVarna(seq, struc);
-
-  }
-
-  public AppVarnaBinding(ArrayList<RNA> rnaList)
+  public AppVarnaBinding()
   {
-
-    // super("VARNA in Jalview");
-    initVarnaEdit(rnaList);
+    init();
   }
 
-  private void initVarna(String seq, String str)
+  /**
+   * Constructs the VARNAPanel and an (empty) selection list of structures to
+   * show in it
+   */
+  private void init()
   {
+    DefaultListModel<FullBackup> dlm = new DefaultListModel<FullBackup>();
 
-    DefaultListModel dlm = new DefaultListModel();
+    int marginTools = 40;
 
     DefaultListSelectionModel m = new DefaultListSelectionModel();
     m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
@@ -174,124 +133,32 @@ public class AppVarnaBinding extends jalview.ext.varna.JalviewVarnaBinding
 
     _sideList = new ReorderableJList();
     _sideList.setModel(dlm);
-    _sideList.addMouseListener(this);
-    _sideList.setSelectionModel(m);
-    _sideList.setPreferredSize(new Dimension(100, 0));
-    _sideList.addListSelectionListener(new ListSelectionListener()
+    _sideList.addMouseListener(new MouseAdapter()
     {
-      public void valueChanged(ListSelectionEvent arg0)
-      {
-        if (!_sideList.isSelectionEmpty() && !arg0.getValueIsAdjusting())
-        {
-          FullBackup sel = (FullBackup) _sideList.getSelectedValue();
-          Mapping map = Mapping.DefaultOutermostMapping(vp.getRNA()
-                  .getSize(), sel.rna.getSize());
-          vp.showRNAInterpolated(sel.rna, sel.config, map);
-          _seq.setText(sel.rna.getSeq());
-          _str.setText(sel.rna.getStructDBN());
-        }
+      @Override
+      public void mouseClicked(MouseEvent e) {
+        AppVarnaBinding.this.mouseClicked(e);
       }
     });
-
-    _rnaList = new BackupHolder(dlm, _sideList);
-    RNA _RNA1 = new RNA("User defined 1");
-
-    try
-    {
-
-      vp = new VARNAPanel("0", ".");
-      _RNA1.setRNA(seq, str);
-      _RNA1.drawRNARadiate(vp.getConfig());
-    } catch (ExceptionNonEqualLength e)
-    {
-      vp.errorDialog(e);
-    } catch (ExceptionUnmatchedClosingParentheses e2)
-    {
-      e2.printStackTrace();
-    } catch (ExceptionFileFormatOrSyntax e3)
-    {
-      e3.printStackTrace();
-    }
-    vp.setPreferredSize(new Dimension(400, 400));
-    _rnaList.add(vp.getConfig().clone(), _RNA1, generateDefaultName(), true);
-
-    // TODO setBackground(_backgroundColor);
-    vp.setBackground(_backgroundColor);
-
-    // TODO getContentPane().setLayout(new BorderLayout());
-    // TODO getContentPane().add(vp, BorderLayout.CENTER);
-
-    // setVisible(true);
-    vp.addVARNAListener(this);
-  }
-
-  private void initVarnaEdit(ArrayList<RNA> rnaInList)
-  {
-
-    DefaultListModel dlm = new DefaultListModel();
-
-    int marginTools = 40;
-
-    DefaultListSelectionModel m = new DefaultListSelectionModel();
-    m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
-    m.setLeadAnchorNotificationEnabled(false);
-
-    _sideList = new ReorderableJList();
-    _sideList.setModel(dlm);
-    _sideList.addMouseListener(this);
     _sideList.setSelectionModel(m);
     _sideList.setPreferredSize(new Dimension(100, 0));
     _sideList.addListSelectionListener(new ListSelectionListener()
     {
-      public void valueChanged(ListSelectionEvent arg0)
+      public void valueChanged(ListSelectionEvent evt)
       {
-        // System.out.println(arg0);
-        if (!_sideList.isSelectionEmpty() && !arg0.getValueIsAdjusting())
-        {
-          FullBackup sel = (FullBackup) _sideList.getSelectedValue();
-          Mapping map = Mapping.DefaultOutermostMapping(vp.getRNA()
-                  .getSize(), sel.rna.getSize());
-          // vp.showRNAInterpolated(sel.rna, sel.config, map);
-          vp.showRNA(sel.rna, sel.config);
-          // _seq.setText(sel.rna.getSeq());
-          _str.setText(sel.rna.getStructDBN());
-        }
+        changeSelectedStructure_actionPerformed(evt);
       }
     });
     _rnaList = new BackupHolder(dlm, _sideList);
 
     try
     {
-
       vp = new VARNAPanel("0", ".");
-      for (int i = 0; i < rnaInList.size(); i++)
-      {
-        rnaInList.get(i).drawRNARadiate(vp.getConfig());
-
-      }
     } catch (ExceptionNonEqualLength e)
     {
       vp.errorDialog(e);
     }
     vp.setPreferredSize(new Dimension(400, 400));
-    for (int i = 0; i < rnaInList.size(); i++)
-    {
-      if (i < rnaInList.size() - 1)
-      {
-        _rnaList.add(vp.getConfig().clone(), rnaInList.get(i), rnaInList
-                .get(i).getName());
-      }
-      else
-      {
-        _rnaList.add(vp.getConfig().clone(), rnaInList.get(i), rnaInList
-                .get(i).getName(), true);
-      }
-    }
-
-    /*
-     * _rnaList.add(vp.getConfig().clone(),_RNA2,generateDefaultName());
-     * _rnaList.add(vp.getConfig().clone(),_RNA1,generateDefaultName(),true);
-     */
 
     JScrollPane listScroller = new JScrollPane(_sideList);
     listScroller.setPreferredSize(new Dimension(150, 0));
@@ -300,86 +167,61 @@ public class AppVarnaBinding extends jalview.ext.varna.JalviewVarnaBinding
 
     Font textFieldsFont = Font.decode("MonoSpaced-PLAIN-12");
 
-    // _seqLabel.setHorizontalTextPosition(JLabel.LEFT);
-    // _seqLabel.setPreferredSize(new Dimension(marginTools, 15));
-    _seq.setFont(textFieldsFont);
-    _seq.setText(rnaInList.get(0).getSeq());
-
-    _updateButton.addActionListener(new ActionListener()
-    {
-      public void actionPerformed(ActionEvent e)
-      {
-        FullBackup sel = (FullBackup) _sideList.getSelectedValue();
-        sel.rna.setSequence("A");
-      }
-    });
-
-    // _seqPanel.setLayout(new BorderLayout());
-    // _seqPanel.add(_seqLabel, BorderLayout.WEST);
-    // _seqPanel.add(_seq, BorderLayout.CENTER);
-
-    _strLabel.setPreferredSize(new Dimension(marginTools, 15));
-    _strLabel.setHorizontalTextPosition(JLabel.LEFT);
-    _str.setFont(textFieldsFont);
-    _strPanel.setLayout(new BorderLayout());
-    _strPanel.add(_strLabel, BorderLayout.WEST);
-    _strPanel.add(_str, BorderLayout.CENTER);
-
-    _input.setLayout(new GridLayout(1, 0));
-    // _input.add(_seqPanel);
-    _input.add(_strPanel);
-
-    JPanel goPanel = new JPanel();
-    goPanel.setLayout(new BorderLayout());
-
-    _tools.setLayout(new BorderLayout());
-    _tools.add(_input, BorderLayout.CENTER);
-    // _tools.add(_info, BorderLayout.SOUTH);
-    _tools.add(goPanel, BorderLayout.EAST);
-
-    /*
-     * _deleteButton.addActionListener(new ActionListener() { public void
-     * actionPerformed(ActionEvent e) { _rnaList.removeSelected(); } });
-     * _duplicateButton.addActionListener(new ActionListener() { public void
-     * actionPerformed(ActionEvent e) {
-     * _rnaList.add((VARNAConfig)vp.getConfig().
-     * clone(),vp.getRNA().clone(),vp.getRNA
-     * ().getName()+"-"+DateFormat.getTimeInstance(DateFormat.LONG).format(new
-     * Date()),true); }});
-     */
-    goPanel.add(_updateButton, BorderLayout.CENTER);
-
-    JPanel ops = new JPanel();
-    ops.setLayout(new GridLayout(1, 2));
-    ops.add(_deleteButton);
-    ops.add(_duplicateButton);
+    // MC commented out stuff not added to panel - remove?
+    // _seq.setFont(textFieldsFont);
+    // if (!rnaList.isEmpty())
+    // {
+    // _seq.setText(rnaList.get(0).getSeq());
+    // }
+
+    // _updateButton.addActionListener(new ActionListener()
+    // {
+    // public void actionPerformed(ActionEvent e)
+    // {
+    // FullBackup sel = (FullBackup) _sideList.getSelectedValue();
+    // sel.rna.setSequence("A");
+    // }
+    // });
+    //
+    // _strLabel.setPreferredSize(new Dimension(marginTools, 15));
+    // _strLabel.setHorizontalTextPosition(JLabel.LEFT);
+    // _str.setFont(textFieldsFont);
+    // _strPanel.setLayout(new BorderLayout());
+    // _strPanel.add(_strLabel, BorderLayout.WEST);
+    // _strPanel.add(_str, BorderLayout.CENTER);
+    //
+    // _input.setLayout(new GridLayout(1, 0));
+    // _input.add(_strPanel);
+    //
+    // JPanel goPanel = new JPanel();
+    // goPanel.setLayout(new BorderLayout());
+    //
+    // _tools.setLayout(new BorderLayout());
+    // _tools.add(_input, BorderLayout.CENTER);
+    // _tools.add(goPanel, BorderLayout.EAST);
+    //
+    // goPanel.add(_updateButton, BorderLayout.CENTER);
+    //
+    // JPanel ops = new JPanel();
+    // ops.setLayout(new GridLayout(1, 2));
+    // ops.add(_deleteButton);
+    // ops.add(_duplicateButton);
 
     JLabel j = new JLabel(
             MessageManager.getString("label.structures_manager"),
             JLabel.CENTER);
     _listPanel.setLayout(new BorderLayout());
 
-    // _listPanel.add(ops, BorderLayout.SOUTH);
     _listPanel.add(j, BorderLayout.NORTH);
     _listPanel.add(listScroller, BorderLayout.CENTER);
 
-    // JSplitPane split = new
-    // JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,_listPanel,vp);
-    /**
-     * TODO getContentPane().setLayout(new BorderLayout());
-     * getContentPane().add(split, BorderLayout.CENTER);
-     * getContentPane().add(_tools, BorderLayout.NORTH);
-     */
-
-    // TODO setVisible(true);
-    DropTarget dt = new DropTarget(vp, this);
-
-    vp.addVARNAListener(this);
-  }
-
-  public JPanel getTools()
-  {
-    return _tools;
+    new DropTarget(vp, new DropTargetAdapter() {
+      @Override
+      public void drop(DropTargetDropEvent dtde)
+      {
+        AppVarnaBinding.this.drop(dtde);
+      }
+    });
   }
 
   public JPanel getListPanel()
@@ -388,13 +230,19 @@ public class AppVarnaBinding extends jalview.ext.varna.JalviewVarnaBinding
   }
 
   /**
-   * TODO: Is it effective to transfer the whole RNA?
+   * Returns the currently selected RNA, or null if none selected
    * 
-   * @return Currently selected RNA
+   * @return
    */
   public RNA getSelectedRNA()
   {
-    return _rnaList.getElementAt(_sideList.getSelectedIndex()).rna;
+    int selectedIndex = _sideList.getSelectedIndex();
+    if (selectedIndex < 0)
+    {
+      return null;
+    }
+    FullBackup selected = _rnaList.getElementAt(selectedIndex);
+    return selected.rna;
   }
 
   /**
@@ -408,123 +256,11 @@ public class AppVarnaBinding extends jalview.ext.varna.JalviewVarnaBinding
     vp.showRNA(rnaEdit);
   }
 
-  /*
-   * private void RNAPanelDemoInit() { DefaultListModel dlm = new
-   * DefaultListModel();
-   * 
-   * 
-   * int marginTools = 40;
-   * 
-   * DefaultListSelectionModel m = new DefaultListSelectionModel();
-   * m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
-   * m.setLeadAnchorNotificationEnabled(false);
-   * 
-   * 
-   * _sideList = new ReorderableJList(); _sideList.setModel(dlm);
-   * _sideList.addMouseListener(this); _sideList.setSelectionModel(m);
-   * _sideList.setPreferredSize(new Dimension(100, 0));
-   * _sideList.addListSelectionListener( new ListSelectionListener(){ public
-   * void valueChanged(ListSelectionEvent arg0) { //System.out.println(arg0); if
-   * (!_sideList.isSelectionEmpty() && !arg0.getValueIsAdjusting()) { FullBackup
-   * sel = (FullBackup) _sideList.getSelectedValue(); Mapping map =
-   * Mapping.DefaultOutermostMapping(vp.getRNA().getSize(), sel.rna.getSize());
-   * vp.showRNAInterpolated(sel.rna,sel.config,map);
-   * _seq.setText(sel.rna.getSeq()); _str.setText(sel.rna.getStructDBN()); } }
-   * });
-   * 
-   * _rnaList = new BackupHolder(dlm,_sideList); RNA _RNA1 = new
-   * RNA("User defined 1"); RNA _RNA2 = new RNA("User defined 2"); try { vp =
-   * new VARNAPanel("0","."); _RNA1.setRNA(DEFAULT_SEQUENCE,
-   * DEFAULT_STRUCTURE1); _RNA1.drawRNARadiate(vp.getConfig());
-   * _RNA2.setRNA(DEFAULT_SEQUENCE, DEFAULT_STRUCTURE2);
-   * _RNA2.drawRNARadiate(vp.getConfig()); } catch (ExceptionNonEqualLength e) {
-   * vp.errorDialog(e); } catch (ExceptionUnmatchedClosingParentheses e2) {
-   * e2.printStackTrace(); } catch (ExceptionFileFormatOrSyntax e3) {
-   * e3.printStackTrace(); } vp.setPreferredSize(new Dimension(400, 400));
-   * _rnaList.add(vp.getConfig().clone(),_RNA2,generateDefaultName());
-   * _rnaList.add(vp.getConfig().clone(),_RNA1,generateDefaultName(),true);
-   * 
-   * JScrollPane listScroller = new JScrollPane(_sideList);
-   * listScroller.setPreferredSize(new Dimension(150, 0));
-   * 
-   * setBackground(_backgroundColor); vp.setBackground(_backgroundColor);
-   * 
-   * 
-   * Font textFieldsFont = Font.decode("MonoSpaced-PLAIN-12");
-   * 
-   * _seqLabel.setHorizontalTextPosition(JLabel.LEFT);
-   * _seqLabel.setPreferredSize(new Dimension(marginTools, 15));
-   * _seq.setFont(textFieldsFont); _seq.setText(DEFAULT_SEQUENCE);
-   * 
-   * _createButton.addActionListener(new ActionListener() { public void
-   * actionPerformed(ActionEvent e) { try { RNA nRNA = new
-   * RNA(generateDefaultName()); nRNA.setRNA(_seq.getText(), _str.getText());
-   * nRNA.drawRNARadiate(vp.getConfig()); _rnaList.add(new
-   * VARNAConfig(),nRNA,true); } catch (ExceptionUnmatchedClosingParentheses e1)
-   * { JOptionPane.showMessageDialog(vp, e1.getMessage(),"Error",
-   * JOptionPane.ERROR_MESSAGE); } catch (ExceptionFileFormatOrSyntax e1) {
-   * JOptionPane.showMessageDialog(vp, e1.getMessage(),"Error",
-   * JOptionPane.ERROR_MESSAGE); } } });
-   * 
-   * 
-   * _seqPanel.setLayout(new BorderLayout()); _seqPanel.add(_seqLabel,
-   * BorderLayout.WEST); _seqPanel.add(_seq, BorderLayout.CENTER);
-   * 
-   * _strLabel.setPreferredSize(new Dimension(marginTools, 15));
-   * _strLabel.setHorizontalTextPosition(JLabel.LEFT);
-   * _str.setFont(textFieldsFont); _strPanel.setLayout(new BorderLayout());
-   * _strPanel.add(_strLabel, BorderLayout.WEST); _strPanel.add(_str,
-   * BorderLayout.CENTER);
-   * 
-   * _input.setLayout(new GridLayout(2, 0)); _input.add(_seqPanel);
-   * _input.add(_strPanel);
-   * 
-   * JPanel goPanel = new JPanel(); goPanel.setLayout(new BorderLayout());
-   * 
-   * _tools.setLayout(new BorderLayout()); _tools.add(_input,
-   * BorderLayout.CENTER); _tools.add(_info, BorderLayout.SOUTH);
-   * _tools.add(goPanel, BorderLayout.EAST);
-   * 
-   * _deleteButton.addActionListener(new ActionListener() { public void
-   * actionPerformed(ActionEvent e) { _rnaList.removeSelected(); } });
-   * _duplicateButton.addActionListener(new ActionListener() { public void
-   * actionPerformed(ActionEvent e) {
-   * _rnaList.add((VARNAConfig)vp.getConfig().clone
-   * (),vp.getRNA().clone(),vp.getRNA
-   * ().getName()+"-"+DateFormat.getTimeInstance(DateFormat.LONG).format(new
-   * Date()),true); }});
-   * 
-   * JPanel ops = new JPanel(); ops.setLayout(new GridLayout(1,2));
-   * ops.add(_deleteButton); ops.add(_duplicateButton);
-   * 
-   * JLabel j = new JLabel("Structures Manager",JLabel.CENTER);
-   * _listPanel.setLayout(new BorderLayout());
-   * 
-   * _listPanel.add(ops,BorderLayout.SOUTH);
-   * _listPanel.add(j,BorderLayout.NORTH);
-   * _listPanel.add(listScroller,BorderLayout.CENTER);
-   * 
-   * goPanel.add(_createButton, BorderLayout.CENTER);
-   * 
-   * JSplitPane split = new
-   * JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,_listPanel,vp);
-   * getContentPane().setLayout(new BorderLayout()); getContentPane().add(split,
-   * BorderLayout.CENTER); getContentPane().add(_tools, BorderLayout.NORTH);
-   * 
-   * setVisible(true); DropTarget dt = new DropTarget(vp, this);
-   * 
-   * vp.addVARNAListener(this); }
-   */
   public static String generateDefaultName()
   {
     return "User file #" + _nextID++;
   }
 
-  public RNA getRNA()
-  {
-    return (RNA) _sideList.getSelectedValue();
-  }
-
   public String[][] getParameterInfo()
   {
     String[][] info =
@@ -537,12 +273,6 @@ public class AppVarnaBinding extends jalview.ext.varna.JalviewVarnaBinding
     return info;
   }
 
-  public void init()
-  {
-    vp.setBackground(_backgroundColor);
-    _error = true;
-  }
-
   @SuppressWarnings("unused")
   private Color getSafeColor(String col, Color def)
   {
@@ -573,60 +303,6 @@ public class AppVarnaBinding extends jalview.ext.varna.JalviewVarnaBinding
     vp = surface;
   }
 
-  public String get_seq()
-  {
-    return _seq.getText();
-  }
-
-  public void set_seq(String _seq)
-  {
-    this._seq.setText(_seq);
-  }
-
-  public String get_str()
-  {
-    return _str.getText();
-  }
-
-  public void set_str(String _str)
-  {
-    this._str.setText(_str);
-  }
-
-  public JLabel get_info()
-  {
-    return _info;
-  }
-
-  public void set_info(JLabel _info)
-  {
-    this._info = _info;
-  }
-
-  /*
-   * public static void main(String[] args) { AppVarnaBinding d = new
-   * AppVarnaBinding(); d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-   * d.pack(); d.setVisible(true); }
-   */
-
-  public void dragEnter(DropTargetDragEvent arg0)
-  {
-    // TODO Auto-generated method stub
-
-  }
-
-  public void dragExit(DropTargetEvent arg0)
-  {
-    // TODO Auto-generated method stub
-
-  }
-
-  public void dragOver(DropTargetDragEvent arg0)
-  {
-    // TODO Auto-generated method stub
-
-  }
-
   public void drop(DropTargetDropEvent dtde)
   {
     try
@@ -653,7 +329,7 @@ public class AppVarnaBinding extends jalview.ext.varna.JalviewVarnaBinding
                 if (c instanceof VARNAPanel)
                 {
                   String path = o.toString();
-                  VARNAPanel vp = (VARNAPanel) c;
+                  VARNAPanel varnaPanel = (VARNAPanel) c;
                   try
                   {
                     FullBackup bck = VARNAPanel.importSession(path);
@@ -665,7 +341,7 @@ public class AppVarnaBinding extends jalview.ext.varna.JalviewVarnaBinding
                             .loadSecStr(path);
                     for (RNA r : mdls)
                     {
-                      r.drawRNA(vp.getConfig());
+                      r.drawRNA(varnaPanel.getConfig());
                       String name = r.getName();
                       if (name.equals(""))
                       {
@@ -676,7 +352,7 @@ public class AppVarnaBinding extends jalview.ext.varna.JalviewVarnaBinding
                       {
                         name += " (Model " + mn++ + ")";
                       }
-                      _rnaList.add(vp.getConfig().clone(), r, name, true);
+                      _rnaList.add(varnaPanel.getConfig().clone(), r, name, true);
                     }
                   }
                 }
@@ -698,147 +374,67 @@ public class AppVarnaBinding extends jalview.ext.varna.JalviewVarnaBinding
 
   }
 
-  public void dropActionChanged(DropTargetDragEvent arg0)
-  {
-  }
-
   private class BackupHolder
   {
-    private DefaultListModel _rnaList;
+    private DefaultListModel<FullBackup> _rnalist;
 
-    private ArrayList<RNA> _rnas = new ArrayList<RNA>();
+    private List<RNA> _rnas = new ArrayList<RNA>();
 
     JList _l;
 
-    public BackupHolder(DefaultListModel rnaList, JList l)
+    public BackupHolder(DefaultListModel<FullBackup> rnaList, JList l)
     {
-      _rnaList = rnaList;
+      _rnalist = rnaList;
       _l = l;
     }
 
-    public void add(VARNAConfig c, RNA r)
-    {
-      add(c, r, r.getName(), false);
-    }
-
-    public void add(VARNAConfig c, RNA r, boolean select)
-    {
-      add(c, r, r.getName(), select);
-    }
-
     public void add(VARNAConfig c, RNA r, String name)
     {
       add(c, r, name, false);
     }
 
+    /**
+     * Adds an entry to the end of the selection list and (optionally) sets it
+     * as selected
+     * 
+     * @param c
+     * @param r
+     * @param name
+     * @param select
+     */
     public void add(VARNAConfig c, RNA r, String name, boolean select)
     {
       if (select)
       {
-        _l.removeSelectionInterval(0, _rnaList.size());
+        _l.removeSelectionInterval(0, _rnalist.size());
       }
       if (name.equals(""))
       {
         name = generateDefaultName();
       }
       FullBackup bck = new FullBackup(c, r, name);
-      _rnas.add(0, r);
-      _rnaList.add(0, bck);
+      _rnas.add(r);
+      _rnalist.addElement(bck);
       if (select)
       {
         _l.setSelectedIndex(0);
       }
     }
 
-    public void remove(int i)
-    {
-      _rnas.remove(i);
-      _rnaList.remove(i);
-
-    }
-
-    public DefaultListModel getModel()
-    {
-      return _rnaList;
-    }
-
-    public boolean contains(RNA r)
-    {
-      return _rnas.contains(r);
-    }
-
-    /*
-     * public int getSize() { return _rnaList.getSize(); }
-     */
     public FullBackup getElementAt(int i)
     {
-      return (FullBackup) _rnaList.getElementAt(i);
-    }
-
-    public void removeSelected()
-    {
-      int i = _l.getSelectedIndex();
-      if (i != -1)
-      {
-        if (_rnaList.getSize() == 1)
-        {
-          RNA r = new RNA();
-          try
-          {
-            r.setRNA(" ", ".");
-          } catch (ExceptionUnmatchedClosingParentheses e1)
-          {
-          } catch (ExceptionFileFormatOrSyntax e1)
-          {
-          }
-          vp.showRNA(r);
-          vp.repaint();
-        }
-        else
-        {
-          int newi = i + 1;
-          if (newi == _rnaList.getSize())
-          {
-            newi = _rnaList.getSize() - 2;
-          }
-          FullBackup bck = (FullBackup) _rnaList.getElementAt(newi);
-          _l.setSelectedValue(bck, true);
-        }
-        _rnaList.remove(i);
-      }
-
+      return _rnalist.getElementAt(i);
     }
   }
 
-  public void onLayoutChanged()
-  {
-    // TODO Auto-generated method stub
-
-  }
-
-  public void onUINewStructure(VARNAConfig v, RNA r)
-  {
-    // patch to fix infinite loop
-    // The problem is that onUINewStructure is called when user clicks
-    // check with Yann about whether Jalview should do anything with this event.
-    // e.g. if user has used VARNA's menu to import a structure .. Jalview may
-    // need to be told which structure is displayed.
-
-    // _rnaList.add(v, r, "", true);
-  }
-
-  public void onWarningEmitted(String s)
-  {
-  }
-
   public void mouseClicked(MouseEvent e)
   {
     if (e.getClickCount() == 2)
     {
       int index = _sideList.locationToIndex(e.getPoint());
-      ListModel dlm = _sideList.getModel();
-      FullBackup item = (FullBackup) dlm.getElementAt(index);
-      ;
+      ListModel<FullBackup> dlm = _sideList.getModel();
+      // FullBackup item = dlm.getElementAt(index);
+
       _sideList.ensureIndexIsVisible(index);
       /*
        * TODO Object newName = JOptionPane.showInputDialog( this,
@@ -850,22 +446,6 @@ public class AppVarnaBinding extends jalview.ext.varna.JalviewVarnaBinding
     }
   }
 
-  public void mouseEntered(MouseEvent arg0)
-  {
-  }
-
-  public void mouseExited(MouseEvent arg0)
-  {
-  }
-
-  public void mousePressed(MouseEvent arg0)
-  {
-  }
-
-  public void mouseReleased(MouseEvent arg0)
-  {
-  }
-
   @Override
   public String[] getPdbFile()
   {
@@ -903,28 +483,148 @@ public class AppVarnaBinding extends jalview.ext.varna.JalviewVarnaBinding
   }
 
   @Override
-  public void onStructureRedrawn()
+  public void highlightAtoms(List<AtomSpec> atoms)
   {
   }
 
   @Override
-  public void onZoomLevelChanged()
+  public boolean isListeningFor(SequenceI seq)
   {
+    return true;
   }
 
-  @Override
-  public void onTranslationChanged()
+  /**
+   * Returns the path to a temporary file containing a representation of the
+   * state of the Varna display, or null on any error
+   * 
+   * @param rna
+   * @param jds
+   * 
+   * @return
+   */
+  public String getStateInfo(RNA rna)
   {
+    if (vp == null)
+    {
+      return null;
+    }
+
+    /*
+     * we have to show the RNA we want to save in the viewer; get the currently
+     * displayed model first so we can restore it
+     */
+    FullBackup sel = (FullBackup) _sideList.getSelectedValue();
+
+    FullBackup model = null;
+    ListModel models = _sideList.getModel();
+    for (int i = 0; i < models.getSize(); i++)
+    {
+      model = (FullBackup) models.getElementAt(i);
+      if (model.rna == rna)
+      {
+        break;
+      }
+    }
+    if (model == null)
+    {
+      return null;
+    }
+
+    /*
+     * switch display if not already shown
+     */
+    if (sel.rna != rna)
+    {
+      vp.showRNA(model.rna, model.config);
+    }
+
+    try
+    {
+      File temp;
+      temp = File.createTempFile("varna", null);
+      temp.deleteOnExit();
+      String filePath = temp.getAbsolutePath();
+      vp.toXML(filePath);
+
+      /*
+       * restore the previous display if needed
+       */
+      if (sel.rna != rna)
+      {
+        vp.showRNA(sel.rna, sel.config);
+      }
+      
+      return filePath;
+    } catch (IOException e)
+    {
+      return null;
+    }
   }
 
-  @Override
-  public void highlightAtoms(List<AtomSpec> atoms)
+  public int getSelectedIndex()
   {
+    return _sideList.getSelectedIndex();
   }
 
-  @Override
-  public boolean isListeningFor(SequenceI seq)
+  /**
+   * Switch the Varna display to the structure selected in the left hand panel
+   * 
+   * @param evt
+   */
+  protected void changeSelectedStructure_actionPerformed(ListSelectionEvent evt)
   {
-    return true;
+    if (!evt.getValueIsAdjusting())
+    {
+      showSelectedStructure();
+    }
+  }
+
+  /**
+   * 
+   */
+  protected void showSelectedStructure()
+  {
+    FullBackup sel = (FullBackup) _sideList.getSelectedValue();
+    if (sel != null)
+    {
+      vp.showRNA(sel.rna, sel.config);
+    }
+  }
+
+  /**
+   * Set and display the selected item in the list of structures
+   * 
+   * @param selectedRna
+   */
+  public void setSelectedIndex(int selectedRna)
+  {
+    /*
+     * note this does nothing if, say, selecting item 3 when only 1 has been
+     * added on load
+     */
+    _sideList.setSelectedIndex(selectedRna);
+    // TODO ? need a worker thread to get this to happen properly
+    showSelectedStructure();
+  }
+
+  /**
+   * Add an RNA structure to the selection list
+   * 
+   * @param rna
+   */
+  public void addStructure(RNA rna)
+  {
+    VARNAConfig config = vp.getConfig().clone();
+    addStructure(rna, config);
+  }
+
+  /**
+   * @param rna
+   * @param config
+   */
+  protected void addStructure(RNA rna, VARNAConfig config)
+  {
+    rna.drawRNARadiate(config);
+    _rnaList.add(config, rna, rna.getName());
   }
 }
index da9c6a9..8dfe839 100644 (file)
@@ -66,9 +66,12 @@ import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.PDBEntry;
+import jalview.datamodel.RnaViewerModel;
+import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.datamodel.StructureViewerModel;
 import jalview.datamodel.StructureViewerModel.StructureData;
+import jalview.ext.varna.RnaModel;
 import jalview.gui.StructureViewer.ViewerType;
 import jalview.schemabinding.version2.AlcodMap;
 import jalview.schemabinding.version2.AlcodonFrame;
@@ -92,6 +95,8 @@ import jalview.schemabinding.version2.OtherData;
 import jalview.schemabinding.version2.PdbentryItem;
 import jalview.schemabinding.version2.Pdbids;
 import jalview.schemabinding.version2.Property;
+import jalview.schemabinding.version2.RnaViewer;
+import jalview.schemabinding.version2.SecondaryStructure;
 import jalview.schemabinding.version2.Sequence;
 import jalview.schemabinding.version2.SequenceSet;
 import jalview.schemabinding.version2.SequenceSetProperties;
@@ -135,8 +140,15 @@ import jalview.ws.params.WsParamSetI;
  */
 public class Jalview2XML
 {
+  private static final String VIEWER_PREFIX = "viewer_";
+
+  private static final String RNA_PREFIX = "rna_";
+
   private static final String UTF_8 = "UTF-8";
 
+  // use this with nextCounter() to make unique names for entities
+  private int counter = 0;
+
   /*
    * SequenceI reference -> XML ID string in jalview XML. Populated as XML reps
    * of sequence objects are created.
@@ -160,6 +172,12 @@ public class Jalview2XML
    */
   private Map<Viewport, AlignFrame> splitFrameCandidates = new HashMap<Viewport, AlignFrame>();
 
+  /*
+   * Map from displayed rna structure models to their saved session state jar
+   * entry names
+   */
+  private Map<RnaModel, String> rnaSessions = new HashMap<RnaModel, String>();
+
   /**
    * create/return unique hash string for sq
    * 
@@ -369,7 +387,7 @@ public class Jalview2XML
    */
   public void saveState(JarOutputStream jout)
   {
-    AlignFrame[] frames = Desktop.getAlignFrames(); // Desktop.desktop.getAllFrames();
+    AlignFrame[] frames = Desktop.getAlignFrames();
 
     if (frames == null)
     {
@@ -378,6 +396,13 @@ public class Jalview2XML
 
     Hashtable<String, AlignFrame> dsses = new Hashtable<String, AlignFrame>();
 
+    /*
+     * ensure cached data is clear before starting
+     */
+    // todo tidy up seqRefIds, seqsToIds initialisation / reset
+    rnaSessions.clear();
+    splitFrameCandidates.clear();
+
     try
     {
 
@@ -385,6 +410,7 @@ public class Jalview2XML
       // //////////////////////////////////////////////////
 
       List<String> shortNames = new ArrayList<String>();
+      List<String> viewIds = new ArrayList<String>();
 
       // REVERSE ORDER
       for (int i = frames.length - 1; i > -1; i--)
@@ -411,7 +437,7 @@ public class Jalview2XML
             fileName = fileName + ".xml";
           }
 
-          saveState(apanel, fileName, jout);
+          saveState(apanel, fileName, jout, viewIds);
 
           String dssid = getDatasetIdRef(af.getViewport().getAlignment()
                   .getDataset());
@@ -497,6 +523,8 @@ public class Jalview2XML
       FileOutputStream fos = new FileOutputStream(jarFile);
       JarOutputStream jout = new JarOutputStream(fos);
       Hashtable<String, AlignFrame> dsses = new Hashtable<String, AlignFrame>();
+      List<String> viewIds = new ArrayList<String>();
+
       for (AlignmentPanel apanel : af.alignPanels)
       {
         String jfileName = apSize == 1 ? fileName : fileName + ap;
@@ -505,7 +533,7 @@ public class Jalview2XML
         {
           jfileName = jfileName + ".xml";
         }
-        saveState(apanel, jfileName, jout);
+        saveState(apanel, jfileName, jout, viewIds);
         String dssid = getDatasetIdRef(af.getViewport().getAlignment()
                 .getDataset());
         if (!dsses.containsKey(dssid))
@@ -543,7 +571,7 @@ public class Jalview2XML
       {
         jfileName = jfileName + ".xml";
       }
-      saveState(_af.alignPanel, jfileName, true, jout);
+      saveState(_af.alignPanel, jfileName, true, jout, null);
     }
   }
 
@@ -557,13 +585,14 @@ public class Jalview2XML
    *          name of alignment panel written to output stream
    * @param jout
    *          jar output stream
+   * @param viewIds
    * @param out
    *          jar entry name
    */
   public JalviewModel saveState(AlignmentPanel ap, String fileName,
-          JarOutputStream jout)
+          JarOutputStream jout, List<String> viewIds)
   {
-    return saveState(ap, fileName, false, jout);
+    return saveState(ap, fileName, false, jout, viewIds);
   }
 
   /**
@@ -583,10 +612,15 @@ public class Jalview2XML
    *          jar entry name
    */
   public JalviewModel saveState(AlignmentPanel ap, String fileName,
-          boolean storeDS, JarOutputStream jout)
+          boolean storeDS, JarOutputStream jout, List<String> viewIds)
   {
+    if (viewIds == null)
+    {
+      viewIds = new ArrayList<String>();
+    }
+
     initSeqRefs();
-    List<String> viewIds = new ArrayList<String>();
+
     List<UserColourScheme> userColours = new ArrayList<UserColourScheme>();
 
     AlignViewport av = ap.av;
@@ -638,14 +672,13 @@ public class Jalview2XML
     Set<String> calcIdSet = new HashSet<String>();
 
     // SAVE SEQUENCES
-    String id = "";
-    jalview.datamodel.SequenceI jds, jdatasq;
     for (int i = 0; i < jal.getHeight(); i++)
     {
-      jds = jal.getSequenceAt(i);
-      jdatasq = jds.getDatasetSequence() == null ? jds : jds
+      final SequenceI jds = jal.getSequenceAt(i);
+      final SequenceI jdatasq = jds.getDatasetSequence() == null ? jds
+              : jds
               .getDatasetSequence();
-      id = seqHash(jds);
+      String id = seqHash(jds);
 
       if (seqRefIds.get(id) != null)
       {
@@ -774,8 +807,8 @@ public class Jalview2XML
               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)
+               * Only store each structure viewer's state once in the project
+               * jar. First time through only (storeDS==false)
                */
               String viewId = viewFrame.getViewId();
               if (!storeDS && !viewIds.contains(viewId))
@@ -783,8 +816,9 @@ public class Jalview2XML
                 viewIds.add(viewId);
                 try
                 {
+                  String viewerState = viewFrame.getStateInfo();
                   writeJarEntry(jout, getViewerJarEntryName(viewId),
-                          viewFrame.getStateInfo().getBytes());
+                          viewerState.getBytes());
                 } catch (IOException e)
                 {
                   System.err.println("Error saving viewer state: "
@@ -834,6 +868,8 @@ public class Jalview2XML
         }
       }
 
+      saveRnaViewers(jout, jseq, jds, viewIds, storeDS);
+
       jms.addJSeq(jseq);
     }
 
@@ -942,35 +978,17 @@ 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
      */
-    IdentityHashMap groupRefs = new IdentityHashMap();
+    IdentityHashMap<SequenceGroup, String> groupRefs = new IdentityHashMap<SequenceGroup, String>();
     if (storeDS)
     {
       for (SequenceI sq : jal.getSequences())
       {
         // Store annotation on dataset sequences only
-        jalview.datamodel.AlignmentAnnotation[] aa = sq.getAnnotation();
+        AlignmentAnnotation[] aa = sq.getAnnotation();
         if (aa != null && aa.length > 0)
         {
           storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
@@ -983,8 +1001,7 @@ public class Jalview2XML
       if (jal.getAlignmentAnnotation() != null)
       {
         // Store the annotation shown on the alignment.
-        jalview.datamodel.AlignmentAnnotation[] aa = jal
-                .getAlignmentAnnotation();
+        AlignmentAnnotation[] aa = jal.getAlignmentAnnotation();
         storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
                 vamsasSet);
       }
@@ -996,70 +1013,69 @@ public class Jalview2XML
       int i = -1;
       for (jalview.datamodel.SequenceGroup sg : jal.getGroups())
       {
-        groups[++i] = new JGroup();
+        JGroup jGroup = new JGroup();
+        groups[++i] = jGroup;
 
-        groups[i].setStart(sg.getStartRes());
-        groups[i].setEnd(sg.getEndRes());
-        groups[i].setName(sg.getName());
+        jGroup.setStart(sg.getStartRes());
+        jGroup.setEnd(sg.getEndRes());
+        jGroup.setName(sg.getName());
         if (groupRefs.containsKey(sg))
         {
-          // group has references so set it's ID field
-          groups[i].setId(groupRefs.get(sg).toString());
+          // group has references so set its ID field
+          jGroup.setId(groupRefs.get(sg));
         }
         if (sg.cs != null)
         {
           if (sg.cs.conservationApplied())
           {
-            groups[i].setConsThreshold(sg.cs.getConservationInc());
+            jGroup.setConsThreshold(sg.cs.getConservationInc());
 
             if (sg.cs instanceof jalview.schemes.UserColourScheme)
             {
-              groups[i].setColour(setUserColourScheme(sg.cs, userColours,
+              jGroup.setColour(setUserColourScheme(sg.cs, userColours,
                       jms));
             }
             else
             {
-              groups[i]
+              jGroup
                       .setColour(ColourSchemeProperty.getColourName(sg.cs));
             }
           }
           else if (sg.cs instanceof jalview.schemes.AnnotationColourGradient)
           {
-            groups[i].setColour("AnnotationColourGradient");
-            groups[i].setAnnotationColours(constructAnnotationColours(
+            jGroup.setColour("AnnotationColourGradient");
+            jGroup.setAnnotationColours(constructAnnotationColours(
                     (jalview.schemes.AnnotationColourGradient) sg.cs,
                     userColours, jms));
           }
           else if (sg.cs instanceof jalview.schemes.UserColourScheme)
           {
-            groups[i]
+            jGroup
                     .setColour(setUserColourScheme(sg.cs, userColours, jms));
           }
           else
           {
-            groups[i].setColour(ColourSchemeProperty.getColourName(sg.cs));
+            jGroup.setColour(ColourSchemeProperty.getColourName(sg.cs));
           }
 
-          groups[i].setPidThreshold(sg.cs.getThreshold());
+          jGroup.setPidThreshold(sg.cs.getThreshold());
         }
 
-        groups[i].setOutlineColour(sg.getOutlineColour().getRGB());
-        groups[i].setDisplayBoxes(sg.getDisplayBoxes());
-        groups[i].setDisplayText(sg.getDisplayText());
-        groups[i].setColourText(sg.getColourText());
-        groups[i].setTextCol1(sg.textColour.getRGB());
-        groups[i].setTextCol2(sg.textColour2.getRGB());
-        groups[i].setTextColThreshold(sg.thresholdTextColour);
-        groups[i].setShowUnconserved(sg.getShowNonconserved());
-        groups[i].setIgnoreGapsinConsensus(sg.getIgnoreGapsConsensus());
-        groups[i].setShowConsensusHistogram(sg.isShowConsensusHistogram());
-        groups[i].setShowSequenceLogo(sg.isShowSequenceLogo());
-        groups[i].setNormaliseSequenceLogo(sg.isNormaliseSequenceLogo());
-        for (int s = 0; s < sg.getSize(); s++)
-        {
-          jalview.datamodel.Sequence seq = (jalview.datamodel.Sequence) sg
-                  .getSequenceAt(s);
-          groups[i].addSeq(seqHash(seq));
+        jGroup.setOutlineColour(sg.getOutlineColour().getRGB());
+        jGroup.setDisplayBoxes(sg.getDisplayBoxes());
+        jGroup.setDisplayText(sg.getDisplayText());
+        jGroup.setColourText(sg.getColourText());
+        jGroup.setTextCol1(sg.textColour.getRGB());
+        jGroup.setTextCol2(sg.textColour2.getRGB());
+        jGroup.setTextColThreshold(sg.thresholdTextColour);
+        jGroup.setShowUnconserved(sg.getShowNonconserved());
+        jGroup.setIgnoreGapsinConsensus(sg.getIgnoreGapsConsensus());
+        jGroup.setShowConsensusHistogram(sg.isShowConsensusHistogram());
+        jGroup.setShowSequenceLogo(sg.isShowSequenceLogo());
+        jGroup.setNormaliseSequenceLogo(sg.isNormaliseSequenceLogo());
+        for (SequenceI seq : sg.getSequences())
+        {
+          jGroup.addSeq(seqHash(seq));
         }
       }
 
@@ -1310,6 +1326,7 @@ public class Jalview2XML
       // using save and then load
       try
       {
+        System.out.println("Writing jar entry " + fileName);
         JarEntry entry = new JarEntry(fileName);
         jout.putNextEntry(entry);
         PrintWriter pout = new PrintWriter(new OutputStreamWriter(jout,
@@ -1328,14 +1345,106 @@ public class Jalview2XML
   }
 
   /**
-   * Copy the contents of a file to a new file added to the output jar
+   * Save any Varna viewers linked to this sequence. Writes an rnaViewer element
+   * for each viewer, with
+   * <ul>
+   * <li>viewer geometry (position, size, split pane divider location)</li>
+   * <li>index of the selected structure in the viewer (currently shows gapped
+   * or ungapped)</li>
+   * <li>the id of the annotation holding RNA secondary structure</li>
+   * <li>(currently only one SS is shown per viewer, may be more in future)</li>
+   * </ul>
+   * Varna viewer state is also written out (in native Varna XML) to separate
+   * project jar entries. A separate entry is written for each RNA structure
+   * displayed, with the naming convention
+   * <ul>
+   * <li>rna_viewId_sequenceId_annotationId_[gapped|trimmed]</li>
+   * </ul>
+   * 
+   * @param jout
+   * @param jseq
+   * @param jds
+   * @param viewIds
+   * @param storeDataset
+   */
+  protected void saveRnaViewers(JarOutputStream jout, JSeq jseq,
+          final SequenceI jds, List<String> viewIds, boolean storeDataset)
+  {
+    JInternalFrame[] frames = Desktop.desktop.getAllFrames();
+    for (int f = frames.length - 1; f > -1; f--)
+    {
+      if (frames[f] instanceof AppVarna)
+      {
+        AppVarna varna = (AppVarna) frames[f];
+        if (varna.isListeningFor(jds))
+        {
+          /*
+           * link the sequence to every viewer that is showing it
+           */
+          String viewId = varna.getViewId();
+          RnaViewer rna = new RnaViewer();
+          rna.setViewId(viewId);
+          rna.setTitle(varna.getTitle());
+          rna.setXpos(varna.getX());
+          rna.setYpos(varna.getY());
+          rna.setWidth(varna.getWidth());
+          rna.setHeight(varna.getHeight());
+          rna.setDividerLocation(varna.getDividerLocation());
+          rna.setSelectedRna(varna.getSelectedIndex());
+          jseq.addRnaViewer(rna);
+
+          /*
+           * Store each Varna panel's state once in the project per sequence.
+           * First time through only (storeDataset==false)
+           */
+          // boolean storeSessions = false;
+          // String sequenceViewId = viewId + seqsToIds.get(jds);
+          // if (!storeDataset && !viewIds.contains(sequenceViewId))
+          // {
+          // viewIds.add(sequenceViewId);
+          // storeSessions = true;
+          // }
+          for (RnaModel model : varna.getModels())
+          {
+            if (model.seq == jds)
+            {
+              /*
+               * VARNA saves each view (sequence or alignment secondary
+               * structure, gapped or trimmed) as a separate XML file
+               */
+              String jarEntryName = rnaSessions.get(model);
+              if (jarEntryName == null)
+              {
+
+                String varnaStateFile = varna.getStateInfo(model.rna);
+                jarEntryName = RNA_PREFIX + viewId + "_"
+                      + nextCounter();
+                copyFileToJar(jout, varnaStateFile, jarEntryName);
+                rnaSessions.put(model, jarEntryName);
+              }
+              SecondaryStructure ss = new SecondaryStructure();
+              String annotationId = varna.getAnnotation(jds).annotationId;
+              ss.setAnnotationId(annotationId);
+              ss.setViewerState(jarEntryName);
+              ss.setGapped(model.gapped);
+              ss.setTitle(model.title);
+              rna.addSecondaryStructure(ss);
+            }
+          }
+        }
+      }
+    }
+  }
+
+  /**
+   * Copy the contents of a file to a new entry added to the output jar
    * 
    * @param jout
    * @param infilePath
-   * @param jarfileName
+   * @param jarEntryName
    */
   protected void copyFileToJar(JarOutputStream jout, String infilePath,
-          String jarfileName)
+          String jarEntryName)
   {
     DataInputStream dis = null;
     try
@@ -1346,7 +1455,7 @@ public class Jalview2XML
         dis = new DataInputStream(new FileInputStream(file));
         byte[] data = new byte[(int) file.length()];
         dis.readFully(data);
-        writeJarEntry(jout, jarfileName, data);
+        writeJarEntry(jout, jarEntryName, data);
       }
     } catch (Exception ex)
     {
@@ -1370,16 +1479,17 @@ public class Jalview2XML
    * Write the data to a new entry of given name in the output jar file
    * 
    * @param jout
-   * @param jarfileName
+   * @param jarEntryName
    * @param data
    * @throws IOException
    */
-  protected void writeJarEntry(JarOutputStream jout, String jarfileName,
+  protected void writeJarEntry(JarOutputStream jout, String jarEntryName,
           byte[] data) throws IOException
   {
     if (jout != null)
     {
-      jout.putNextEntry(new JarEntry(jarfileName));
+      System.out.println("Writing jar entry " + jarEntryName);
+      jout.putNextEntry(new JarEntry(jarEntryName));
       DataOutputStream dout = new DataOutputStream(jout);
       dout.write(data, 0, data.length);
       dout.flush();
@@ -1490,7 +1600,8 @@ public class Jalview2XML
   }
 
   private void storeAlignmentAnnotation(AlignmentAnnotation[] aa,
-          IdentityHashMap groupRefs, AlignmentViewport av,
+          IdentityHashMap<SequenceGroup, String> groupRefs,
+          AlignmentViewport av,
           Set<String> calcIdSet, boolean storeDS, SequenceSet vamsasSet)
   {
 
@@ -1498,53 +1609,54 @@ public class Jalview2XML
     {
       Annotation an = new Annotation();
 
-      if (aa[i].annotationId != null)
+      AlignmentAnnotation annotation = aa[i];
+      if (annotation.annotationId != null)
       {
-        annotationIds.put(aa[i].annotationId, aa[i]);
+        annotationIds.put(annotation.annotationId, annotation);
       }
 
-      an.setId(aa[i].annotationId);
+      an.setId(annotation.annotationId);
 
-      an.setVisible(aa[i].visible);
+      an.setVisible(annotation.visible);
 
-      an.setDescription(aa[i].description);
+      an.setDescription(annotation.description);
 
-      if (aa[i].sequenceRef != null)
+      if (annotation.sequenceRef != null)
       {
         // 2.9 JAL-1781 xref on sequence id rather than name
-        an.setSequenceRef(seqsToIds.get(aa[i].sequenceRef));
+        an.setSequenceRef(seqsToIds.get(annotation.sequenceRef));
       }
-      if (aa[i].groupRef != null)
+      if (annotation.groupRef != null)
       {
-        Object groupIdr = groupRefs.get(aa[i].groupRef);
+        String groupIdr = groupRefs.get(annotation.groupRef);
         if (groupIdr == null)
         {
           // make a locally unique String
-          groupRefs.put(aa[i].groupRef,
+          groupRefs.put(annotation.groupRef,
                   groupIdr = ("" + System.currentTimeMillis()
-                          + aa[i].groupRef.getName() + groupRefs.size()));
+                          + annotation.groupRef.getName() + groupRefs.size()));
         }
         an.setGroupRef(groupIdr.toString());
       }
 
       // store all visualization attributes for annotation
-      an.setGraphHeight(aa[i].graphHeight);
-      an.setCentreColLabels(aa[i].centreColLabels);
-      an.setScaleColLabels(aa[i].scaleColLabel);
-      an.setShowAllColLabels(aa[i].showAllColLabels);
-      an.setBelowAlignment(aa[i].belowAlignment);
+      an.setGraphHeight(annotation.graphHeight);
+      an.setCentreColLabels(annotation.centreColLabels);
+      an.setScaleColLabels(annotation.scaleColLabel);
+      an.setShowAllColLabels(annotation.showAllColLabels);
+      an.setBelowAlignment(annotation.belowAlignment);
 
-      if (aa[i].graph > 0)
+      if (annotation.graph > 0)
       {
         an.setGraph(true);
-        an.setGraphType(aa[i].graph);
-        an.setGraphGroup(aa[i].graphGroup);
-        if (aa[i].getThreshold() != null)
+        an.setGraphType(annotation.graph);
+        an.setGraphGroup(annotation.graphGroup);
+        if (annotation.getThreshold() != null)
         {
           ThresholdLine line = new ThresholdLine();
-          line.setLabel(aa[i].getThreshold().label);
-          line.setValue(aa[i].getThreshold().value);
-          line.setColour(aa[i].getThreshold().colour.getRGB());
+          line.setLabel(annotation.getThreshold().label);
+          line.setValue(annotation.getThreshold().value);
+          line.setColour(annotation.getThreshold().colour.getRGB());
           an.setThresholdLine(line);
         }
       }
@@ -1553,78 +1665,78 @@ public class Jalview2XML
         an.setGraph(false);
       }
 
-      an.setLabel(aa[i].label);
+      an.setLabel(annotation.label);
 
-      if (aa[i] == av.getAlignmentQualityAnnot()
-              || aa[i] == av.getAlignmentConservationAnnotation()
-              || aa[i] == av.getAlignmentConsensusAnnotation()
-              || aa[i].autoCalculated)
+      if (annotation == av.getAlignmentQualityAnnot()
+              || annotation == av.getAlignmentConservationAnnotation()
+              || annotation == av.getAlignmentConsensusAnnotation()
+              || annotation.autoCalculated)
       {
         // new way of indicating autocalculated annotation -
-        an.setAutoCalculated(aa[i].autoCalculated);
+        an.setAutoCalculated(annotation.autoCalculated);
       }
-      if (aa[i].hasScore())
+      if (annotation.hasScore())
       {
-        an.setScore(aa[i].getScore());
+        an.setScore(annotation.getScore());
       }
 
-      if (aa[i].getCalcId() != null)
+      if (annotation.getCalcId() != null)
       {
-        calcIdSet.add(aa[i].getCalcId());
-        an.setCalcId(aa[i].getCalcId());
+        calcIdSet.add(annotation.getCalcId());
+        an.setCalcId(annotation.getCalcId());
       }
-      if (aa[i].hasProperties())
+      if (annotation.hasProperties())
       {
-        for (String pr : aa[i].getProperties())
+        for (String pr : annotation.getProperties())
         {
           Property prop = new Property();
           prop.setName(pr);
-          prop.setValue(aa[i].getProperty(pr));
+          prop.setValue(annotation.getProperty(pr));
           an.addProperty(prop);
         }
       }
 
       AnnotationElement ae;
-      if (aa[i].annotations != null)
+      if (annotation.annotations != null)
       {
         an.setScoreOnly(false);
-        for (int a = 0; a < aa[i].annotations.length; a++)
+        for (int a = 0; a < annotation.annotations.length; a++)
         {
-          if ((aa[i] == null) || (aa[i].annotations[a] == null))
+          if ((annotation == null) || (annotation.annotations[a] == null))
           {
             continue;
           }
 
           ae = new AnnotationElement();
-          if (aa[i].annotations[a].description != null)
+          if (annotation.annotations[a].description != null)
           {
-            ae.setDescription(aa[i].annotations[a].description);
+            ae.setDescription(annotation.annotations[a].description);
           }
-          if (aa[i].annotations[a].displayCharacter != null)
+          if (annotation.annotations[a].displayCharacter != null)
           {
-            ae.setDisplayCharacter(aa[i].annotations[a].displayCharacter);
+            ae.setDisplayCharacter(annotation.annotations[a].displayCharacter);
           }
 
-          if (!Float.isNaN(aa[i].annotations[a].value))
+          if (!Float.isNaN(annotation.annotations[a].value))
           {
-            ae.setValue(aa[i].annotations[a].value);
+            ae.setValue(annotation.annotations[a].value);
           }
 
           ae.setPosition(a);
-          if (aa[i].annotations[a].secondaryStructure > ' ')
+          if (annotation.annotations[a].secondaryStructure > ' ')
           {
-            ae.setSecondaryStructure(aa[i].annotations[a].secondaryStructure
+            ae.setSecondaryStructure(annotation.annotations[a].secondaryStructure
                     + "");
           }
 
-          if (aa[i].annotations[a].colour != null
-                  && aa[i].annotations[a].colour != java.awt.Color.black)
+          if (annotation.annotations[a].colour != null
+                  && annotation.annotations[a].colour != java.awt.Color.black)
           {
-            ae.setColour(aa[i].annotations[a].colour.getRGB());
+            ae.setColour(annotation.annotations[a].colour.getRGB());
           }
 
           an.addAnnotationElement(ae);
-          if (aa[i].autoCalculated)
+          if (annotation.autoCalculated)
           {
             // only write one non-null entry into the annotation row -
             // sufficient to get the visualization attributes necessary to
@@ -1637,7 +1749,7 @@ public class Jalview2XML
       {
         an.setScoreOnly(true);
       }
-      if (!storeDS || (storeDS && !aa[i].autoCalculated))
+      if (!storeDS || (storeDS && !annotation.autoCalculated))
       {
         // skip autocalculated annotation - these are only provided for
         // alignments
@@ -2173,13 +2285,7 @@ public class Jalview2XML
         }
       } while (jarentry != null);
       resolveFrefedSequences();
-    } catch (java.io.FileNotFoundException ex)
-    {
-      ex.printStackTrace();
-      errorMessage = "Couldn't locate Jalview XML file : " + file;
-      System.err.println("Exception whilst loading jalview XML file : "
-              + ex + "\n");
-    } catch (java.net.UnknownHostException ex)
+    } catch (IOException ex)
     {
       ex.printStackTrace();
       errorMessage = "Couldn't locate Jalview XML file : " + file;
@@ -2739,10 +2845,11 @@ public class Jalview2XML
     // ////////////////////////////////
     // LOAD ANNOTATIONS
     List<JvAnnotRow> autoAlan = new ArrayList<JvAnnotRow>();
-    /**
+
+    /*
      * store any annotations which forward reference a group's ID
      */
-    Hashtable<String, ArrayList<jalview.datamodel.AlignmentAnnotation>> groupAnnotRefs = new Hashtable<String, ArrayList<jalview.datamodel.AlignmentAnnotation>>();
+    Map<String, List<AlignmentAnnotation>> groupAnnotRefs = new Hashtable<String, List<AlignmentAnnotation>>();
 
     if (vamsasSet.getAnnotationCount() > 0)
     {
@@ -2750,39 +2857,43 @@ public class Jalview2XML
 
       for (int i = 0; i < an.length; i++)
       {
+        Annotation annotation = an[i];
+
         /**
          * test if annotation is automatically calculated for this view only
          */
         boolean autoForView = false;
-        if (an[i].getLabel().equals("Quality")
-                || an[i].getLabel().equals("Conservation")
-                || an[i].getLabel().equals("Consensus"))
+        if (annotation.getLabel().equals("Quality")
+                || annotation.getLabel().equals("Conservation")
+                || annotation.getLabel().equals("Consensus"))
         {
           // Kludge for pre 2.5 projects which lacked the autocalculated flag
           autoForView = true;
-          if (!an[i].hasAutoCalculated())
+          if (!annotation.hasAutoCalculated())
           {
-            an[i].setAutoCalculated(true);
+            annotation.setAutoCalculated(true);
           }
         }
         if (autoForView
-                || (an[i].hasAutoCalculated() && an[i].isAutoCalculated()))
+                || (annotation.hasAutoCalculated() && annotation
+                        .isAutoCalculated()))
         {
           // remove ID - we don't recover annotation from other views for
           // view-specific annotation
-          an[i].setId(null);
+          annotation.setId(null);
         }
 
         // set visiblity for other annotation in this view
-        if (an[i].getId() != null
-                && annotationIds.containsKey(an[i].getId()))
+        String annotationId = annotation.getId();
+        if (annotationId != null
+                && annotationIds.containsKey(annotationId))
         {
-          AlignmentAnnotation jda = annotationIds.get(an[i].getId());
+          AlignmentAnnotation jda = annotationIds.get(annotationId);
           // in principle Visible should always be true for annotation displayed
           // in multiple views
-          if (an[i].hasVisible())
+          if (annotation.hasVisible())
           {
-            jda.visible = an[i].getVisible();
+            jda.visible = annotation.getVisible();
           }
 
           al.addAnnotation(jda);
@@ -2790,11 +2901,11 @@ public class Jalview2XML
           continue;
         }
         // Construct new annotation from model.
-        AnnotationElement[] ae = an[i].getAnnotationElement();
+        AnnotationElement[] ae = annotation.getAnnotationElement();
         jalview.datamodel.Annotation[] anot = null;
         java.awt.Color firstColour = null;
         int anpos;
-        if (!an[i].getScoreOnly())
+        if (!annotation.getScoreOnly())
         {
           anot = new jalview.datamodel.Annotation[al.getWidth()];
           for (int aa = 0; aa < ae.length && aa < anot.length; aa++)
@@ -2831,27 +2942,27 @@ public class Jalview2XML
         }
         jalview.datamodel.AlignmentAnnotation jaa = null;
 
-        if (an[i].getGraph())
+        if (annotation.getGraph())
         {
           float llim = 0, hlim = 0;
           // if (autoForView || an[i].isAutoCalculated()) {
           // hlim=11f;
           // }
-          jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
-                  an[i].getDescription(), anot, llim, hlim,
-                  an[i].getGraphType());
+          jaa = new jalview.datamodel.AlignmentAnnotation(
+                  annotation.getLabel(), annotation.getDescription(), anot,
+                  llim, hlim, annotation.getGraphType());
 
-          jaa.graphGroup = an[i].getGraphGroup();
+          jaa.graphGroup = annotation.getGraphGroup();
           jaa._linecolour = firstColour;
-          if (an[i].getThresholdLine() != null)
+          if (annotation.getThresholdLine() != null)
           {
-            jaa.setThreshold(new jalview.datamodel.GraphLine(an[i]
-                    .getThresholdLine().getValue(), an[i]
+            jaa.setThreshold(new jalview.datamodel.GraphLine(annotation
+                    .getThresholdLine().getValue(), annotation
                     .getThresholdLine().getLabel(), new java.awt.Color(
-                    an[i].getThresholdLine().getColour())));
+                    annotation.getThresholdLine().getColour())));
 
           }
-          if (autoForView || an[i].isAutoCalculated())
+          if (autoForView || annotation.isAutoCalculated())
           {
             // Hardwire the symbol display line to ensure that labels for
             // histograms are displayed
@@ -2890,7 +3001,7 @@ public class Jalview2XML
         // and make a note of any group association
         if (an[i].getGroupRef() != null && an[i].getGroupRef().length() > 0)
         {
-          ArrayList<jalview.datamodel.AlignmentAnnotation> aal = groupAnnotRefs
+          List<jalview.datamodel.AlignmentAnnotation> aal = groupAnnotRefs
                   .get(an[i].getGroupRef());
           if (aal == null)
           {
@@ -2964,37 +3075,37 @@ public class Jalview2XML
       boolean addAnnotSchemeGroup = false;
       for (int i = 0; i < groups.length; i++)
       {
+        JGroup jGroup = groups[i];
         ColourSchemeI cs = null;
-
-        if (groups[i].getColour() != null)
+        if (jGroup.getColour() != null)
         {
-          if (groups[i].getColour().startsWith("ucs"))
+          if (jGroup.getColour().startsWith("ucs"))
           {
-            cs = getUserColourScheme(jms, groups[i].getColour());
+            cs = getUserColourScheme(jms, jGroup.getColour());
           }
-          else if (groups[i].getColour().equals("AnnotationColourGradient")
-                  && groups[i].getAnnotationColours() != null)
+          else if (jGroup.getColour().equals("AnnotationColourGradient")
+                  && jGroup.getAnnotationColours() != null)
           {
             addAnnotSchemeGroup = true;
             cs = null;
           }
           else
           {
-            cs = ColourSchemeProperty.getColour(al, groups[i].getColour());
+            cs = ColourSchemeProperty.getColour(al, jGroup.getColour());
           }
 
           if (cs != null)
           {
-            cs.setThreshold(groups[i].getPidThreshold(), true);
+            cs.setThreshold(jGroup.getPidThreshold(), true);
           }
         }
 
-        Vector seqs = new Vector();
+        Vector<SequenceI> seqs = new Vector<SequenceI>();
 
-        for (int s = 0; s < groups[i].getSeqCount(); s++)
+        for (int s = 0; s < jGroup.getSeqCount(); s++)
         {
-          String seqId = groups[i].getSeq(s) + "";
-          jalview.datamodel.SequenceI ts = seqRefIds.get(seqId);
+          String seqId = jGroup.getSeq(s) + "";
+          SequenceI ts = seqRefIds.get(seqId);
 
           if (ts != null)
           {
@@ -3007,36 +3118,36 @@ public class Jalview2XML
           continue;
         }
 
-        jalview.datamodel.SequenceGroup sg = new jalview.datamodel.SequenceGroup(
-                seqs, groups[i].getName(), cs, groups[i].getDisplayBoxes(),
-                groups[i].getDisplayText(), groups[i].getColourText(),
-                groups[i].getStart(), groups[i].getEnd());
+        SequenceGroup sg = new SequenceGroup(seqs, jGroup.getName(), cs,
+                jGroup.getDisplayBoxes(), jGroup.getDisplayText(),
+                jGroup.getColourText(), jGroup.getStart(),
+                jGroup.getEnd());
 
-        sg.setOutlineColour(new java.awt.Color(groups[i].getOutlineColour()));
+        sg.setOutlineColour(new java.awt.Color(jGroup.getOutlineColour()));
 
-        sg.textColour = new java.awt.Color(groups[i].getTextCol1());
-        sg.textColour2 = new java.awt.Color(groups[i].getTextCol2());
-        sg.setShowNonconserved(groups[i].hasShowUnconserved() ? groups[i]
+        sg.textColour = new java.awt.Color(jGroup.getTextCol1());
+        sg.textColour2 = new java.awt.Color(jGroup.getTextCol2());
+        sg.setShowNonconserved(jGroup.hasShowUnconserved() ? jGroup
                 .isShowUnconserved() : false);
-        sg.thresholdTextColour = groups[i].getTextColThreshold();
-        if (groups[i].hasShowConsensusHistogram())
+        sg.thresholdTextColour = jGroup.getTextColThreshold();
+        if (jGroup.hasShowConsensusHistogram())
         {
-          sg.setShowConsensusHistogram(groups[i].isShowConsensusHistogram());
+          sg.setShowConsensusHistogram(jGroup.isShowConsensusHistogram());
         }
         ;
-        if (groups[i].hasShowSequenceLogo())
+        if (jGroup.hasShowSequenceLogo())
         {
-          sg.setshowSequenceLogo(groups[i].isShowSequenceLogo());
+          sg.setshowSequenceLogo(jGroup.isShowSequenceLogo());
         }
-        if (groups[i].hasNormaliseSequenceLogo())
+        if (jGroup.hasNormaliseSequenceLogo())
         {
-          sg.setNormaliseSequenceLogo(groups[i].isNormaliseSequenceLogo());
+          sg.setNormaliseSequenceLogo(jGroup.isNormaliseSequenceLogo());
         }
-        if (groups[i].hasIgnoreGapsinConsensus())
+        if (jGroup.hasIgnoreGapsinConsensus())
         {
-          sg.setIgnoreGapsConsensus(groups[i].getIgnoreGapsinConsensus());
+          sg.setIgnoreGapsConsensus(jGroup.getIgnoreGapsinConsensus());
         }
-        if (groups[i].getConsThreshold() != 0)
+        if (jGroup.getConsThreshold() != 0)
         {
           jalview.analysis.Conservation c = new jalview.analysis.Conservation(
                   "All", ResidueProperties.propHash, 3,
@@ -3046,14 +3157,14 @@ public class Jalview2XML
           sg.cs.setConservation(c);
         }
 
-        if (groups[i].getId() != null && groupAnnotRefs.size() > 0)
+        if (jGroup.getId() != null && groupAnnotRefs.size() > 0)
         {
           // re-instate unique group/annotation row reference
-          ArrayList<jalview.datamodel.AlignmentAnnotation> jaal = groupAnnotRefs
-                  .get(groups[i].getId());
+          List<AlignmentAnnotation> jaal = groupAnnotRefs
+                  .get(jGroup.getId());
           if (jaal != null)
           {
-            for (jalview.datamodel.AlignmentAnnotation jaa : jaal)
+            for (AlignmentAnnotation jaa : jaal)
             {
               jaa.groupRef = sg;
               if (jaa.autoCalculated)
@@ -3079,7 +3190,7 @@ public class Jalview2XML
         {
           // reconstruct the annotation colourscheme
           sg.cs = constructAnnotationColour(
-                  groups[i].getAnnotationColours(), null, al, jms, false);
+                  jGroup.getAnnotationColours(), null, al, jms, false);
         }
       }
     }
@@ -3170,90 +3281,203 @@ public class Jalview2XML
       av = af.viewport;
       ap = af.alignPanel;
     }
-    // LOAD TREES
-    // /////////////////////////////////////
-    if (loadTreesAndStructures && jms.getTreeCount() > 0)
+
+    /*
+     * Load any trees, PDB structures and viewers
+     * 
+     * Not done if flag is false (when this method is used for New View)
+     */
+    if (loadTreesAndStructures)
     {
-      try
+      loadTrees(jms, view, af, av, ap);
+      loadPDBStructures(jprovider, jseqs, af, ap);
+      loadRnaViewers(jprovider, jseqs, ap);
+    }
+    // and finally return.
+    return af;
+  }
+
+  /**
+   * Instantiate and link any saved RNA (Varna) viewers. The state of the Varna
+   * panel is restored from separate jar entries, two (gapped and trimmed) per
+   * sequence and secondary structure.
+   * 
+   * Currently each viewer shows just one sequence and structure (gapped and
+   * trimmed), however this method is designed to support multiple sequences or
+   * structures in viewers if wanted in future.
+   * 
+   * @param jprovider
+   * @param jseqs
+   * @param ap
+   */
+  private void loadRnaViewers(jarInputStreamProvider jprovider,
+          JSeq[] jseqs, AlignmentPanel ap)
+  {
+    /*
+     * scan the sequences for references to viewers; create each one the first
+     * time it is referenced, add Rna models to existing viewers
+     */
+    for (JSeq jseq : jseqs)
+    {
+      for (int i = 0; i < jseq.getRnaViewerCount(); i++)
       {
-        for (int t = 0; t < jms.getTreeCount(); t++)
+        RnaViewer viewer = jseq.getRnaViewer(i);
+        AppVarna appVarna = findOrCreateVarnaViewer(viewer, uniqueSetSuffix,
+                ap);
+
+        for (int j = 0; j < viewer.getSecondaryStructureCount(); j++)
         {
+          SecondaryStructure ss = viewer.getSecondaryStructure(j);
+          SequenceI seq = seqRefIds.get(jseq.getId());
+          AlignmentAnnotation ann = this.annotationIds.get(ss
+                  .getAnnotationId());
 
-          Tree tree = jms.getTree(t);
+          /*
+           * add the structure to the Varna display (with session state copied
+           * from the jar to a temporary file)
+           */
+          boolean gapped = ss.isGapped();
+          String rnaTitle = ss.getTitle();
+          String sessionState = ss.getViewerState();
+          String tempStateFile = copyJarEntry(jprovider, sessionState,
+                  "varna");
+          RnaModel rna = new RnaModel(rnaTitle, ann, seq, null, gapped,
+                  tempStateFile);
+          appVarna.addModel(rna, rnaTitle);
+        }
+        appVarna.setSelectedIndex(viewer.getSelectedRna());
+      }
+    }
+  }
 
-          TreePanel tp = (TreePanel) retrieveExistingObj(tree.getId());
-          if (tp == null)
-          {
-            tp = af.ShowNewickTree(
-                    new jalview.io.NewickFile(tree.getNewick()),
-                    tree.getTitle(), tree.getWidth(), tree.getHeight(),
-                    tree.getXpos(), tree.getYpos());
-            if (tree.getId() != null)
-            {
-              // perhaps bind the tree id to something ?
-            }
-          }
-          else
-          {
-            // update local tree attributes ?
-            // TODO: should check if tp has been manipulated by user - if so its
-            // settings shouldn't be modified
-            tp.setTitle(tree.getTitle());
-            tp.setBounds(new Rectangle(tree.getXpos(), tree.getYpos(), tree
-                    .getWidth(), tree.getHeight()));
-            tp.av = av; // af.viewport; // TODO: verify 'associate with all
-            // views'
-            // works still
-            tp.treeCanvas.av = av; // af.viewport;
-            tp.treeCanvas.ap = ap; // af.alignPanel;
+  /**
+   * Locate and return an already instantiated matching AppVarna, or create one
+   * if not found
+   * 
+   * @param viewer
+   * @param viewIdSuffix
+   * @param ap
+   * @return
+   */
+  protected AppVarna findOrCreateVarnaViewer(RnaViewer viewer,
+          String viewIdSuffix, AlignmentPanel ap)
+  {
+    /*
+     * on each load a suffix is appended to the saved viewId, to avoid conflicts
+     * if load is repeated
+     */
+    String postLoadId = viewer.getViewId() + viewIdSuffix;
+    for (JInternalFrame frame : getAllFrames())
+    {
+      if (frame instanceof AppVarna)
+      {
+        AppVarna varna = (AppVarna) frame;
+        if (postLoadId.equals(varna.getViewId()))
+        {
+          // this viewer is already instantiated
+          // could in future here add ap as another 'parent' of the
+          // AppVarna window; currently just 1-to-many
+          return varna;
+        }
+      }
+    }
 
-          }
-          if (tp == null)
-          {
-            warn("There was a problem recovering stored Newick tree: \n"
-                    + tree.getNewick());
-            continue;
-          }
+    /*
+     * viewer not found - make it
+     */
+    RnaViewerModel model = new RnaViewerModel(postLoadId,
+            viewer.getTitle(), viewer.getXpos(),
+            viewer.getYpos(), viewer.getWidth(), viewer.getHeight(),
+            viewer.getDividerLocation());
+    AppVarna varna = new AppVarna(model, ap);
 
-          tp.fitToWindow.setState(tree.getFitToWindow());
-          tp.fitToWindow_actionPerformed(null);
+    return varna;
+  }
 
-          if (tree.getFontName() != null)
-          {
-            tp.setTreeFont(new java.awt.Font(tree.getFontName(), tree
-                    .getFontStyle(), tree.getFontSize()));
-          }
-          else
+  /**
+   * Load any saved trees
+   * 
+   * @param jms
+   * @param view
+   * @param af
+   * @param av
+   * @param ap
+   */
+  protected void loadTrees(JalviewModelSequence jms, Viewport view,
+          AlignFrame af, AlignViewport av, AlignmentPanel ap)
+  {
+    // TODO result of automated refactoring - are all these parameters needed?
+    try
+    {
+      for (int t = 0; t < jms.getTreeCount(); t++)
+      {
+
+        Tree tree = jms.getTree(t);
+
+        TreePanel tp = (TreePanel) retrieveExistingObj(tree.getId());
+        if (tp == null)
+        {
+          tp = af.ShowNewickTree(
+                  new jalview.io.NewickFile(tree.getNewick()),
+                  tree.getTitle(), tree.getWidth(), tree.getHeight(),
+                  tree.getXpos(), tree.getYpos());
+          if (tree.getId() != null)
           {
-            tp.setTreeFont(new java.awt.Font(view.getFontName(), view
-                    .getFontStyle(), tree.getFontSize()));
+            // perhaps bind the tree id to something ?
           }
+        }
+        else
+        {
+          // update local tree attributes ?
+          // TODO: should check if tp has been manipulated by user - if so its
+          // settings shouldn't be modified
+          tp.setTitle(tree.getTitle());
+          tp.setBounds(new Rectangle(tree.getXpos(), tree.getYpos(), tree
+                  .getWidth(), tree.getHeight()));
+          tp.av = av; // af.viewport; // TODO: verify 'associate with all
+          // views'
+          // works still
+          tp.treeCanvas.av = av; // af.viewport;
+          tp.treeCanvas.ap = ap; // af.alignPanel;
 
-          tp.showPlaceholders(tree.getMarkUnlinked());
-          tp.showBootstrap(tree.getShowBootstrap());
-          tp.showDistances(tree.getShowDistances());
+        }
+        if (tp == null)
+        {
+          warn("There was a problem recovering stored Newick tree: \n"
+                  + tree.getNewick());
+          continue;
+        }
 
-          tp.treeCanvas.threshold = tree.getThreshold();
+        tp.fitToWindow.setState(tree.getFitToWindow());
+        tp.fitToWindow_actionPerformed(null);
 
-          if (tree.getCurrentTree())
-          {
-            af.viewport.setCurrentTree(tp.getTree());
-          }
+        if (tree.getFontName() != null)
+        {
+          tp.setTreeFont(new java.awt.Font(tree.getFontName(), tree
+                  .getFontStyle(), tree.getFontSize()));
+        }
+        else
+        {
+          tp.setTreeFont(new java.awt.Font(view.getFontName(), view
+                  .getFontStyle(), tree.getFontSize()));
         }
 
-      } catch (Exception ex)
-      {
-        ex.printStackTrace();
+        tp.showPlaceholders(tree.getMarkUnlinked());
+        tp.showBootstrap(tree.getShowBootstrap());
+        tp.showDistances(tree.getShowDistances());
+
+        tp.treeCanvas.threshold = tree.getThreshold();
+
+        if (tree.getCurrentTree())
+        {
+          af.viewport.setCurrentTree(tp.getTree());
+        }
       }
-    }
 
-    // //LOAD STRUCTURES
-    if (loadTreesAndStructures)
+    } catch (Exception ex)
     {
-      loadStructures(jprovider, jseqs, af, ap);
+      ex.printStackTrace();
     }
-    // and finally return.
-    return af;
   }
 
   /**
@@ -3264,7 +3488,7 @@ public class Jalview2XML
    * @param af
    * @param ap
    */
-  protected void loadStructures(jarInputStreamProvider jprovider,
+  protected void loadPDBStructures(jarInputStreamProvider jprovider,
           JSeq[] jseqs, AlignFrame af, AlignmentPanel ap)
   {
     /*
@@ -3458,11 +3682,12 @@ public class Jalview2XML
     /*
      * 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
+     * NB 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");
+    String viewerJarEntryName = getViewerJarEntryName(data.getViewId());
+    chimeraSessionFile = copyJarEntry(jprovider, viewerJarEntryName,
+            "chimera");
 
     Set<Entry<File, StructureData>> fileData = data.getFileData()
             .entrySet();
@@ -3487,6 +3712,7 @@ public class Jalview2XML
     final SequenceI[][] seqsArray = allseqs.toArray(new SequenceI[allseqs
             .size()][]);
     String newViewId = viewerData.getKey();
+
     ChimeraViewFrame cvf = new ChimeraViewFrame(chimeraSessionFile,
             af.alignPanel, pdbArray,
             seqsArray, colourByChimera, colourBySequence, newViewId);
@@ -3659,7 +3885,7 @@ public class Jalview2XML
    */
   protected String getViewerJarEntryName(String viewId)
   {
-    return "viewer_" + viewId;
+    return VIEWER_PREFIX + viewId;
   }
 
   /**
@@ -3800,7 +4026,8 @@ public class Jalview2XML
    * @return true if version is development/null or evaluates to the same or
    *         later X.Y.Z (where X,Y,Z are like [0-9]+b?[0-9]*)
    */
-  private boolean isVersionStringLaterThan(String supported, String version)
+  protected boolean isVersionStringLaterThan(String supported,
+          String version)
   {
     if (version == null || version.equalsIgnoreCase("DEVELOPMENT BUILD")
             || version.equalsIgnoreCase("Test")
@@ -4636,20 +4863,24 @@ public class Jalview2XML
     }
   }
 
-  java.util.Hashtable datasetIds = null;
+  /*
+   * TODO use AlignmentI here and in related methods - needs
+   * AlignmentI.getDataset() changed to return AlignmentI instead of Alignment
+   */
+  Hashtable<String, Alignment> datasetIds = null;
 
-  java.util.IdentityHashMap dataset2Ids = null;
+  IdentityHashMap<Alignment, String> dataset2Ids = null;
 
   private Alignment getDatasetFor(String datasetId)
   {
     if (datasetIds == null)
     {
-      datasetIds = new Hashtable();
+      datasetIds = new Hashtable<String, Alignment>();
       return null;
     }
     if (datasetIds.containsKey(datasetId))
     {
-      return (Alignment) datasetIds.get(datasetId);
+      return datasetIds.get(datasetId);
     }
     return null;
   }
@@ -4658,7 +4889,7 @@ public class Jalview2XML
   {
     if (datasetIds == null)
     {
-      datasetIds = new Hashtable();
+      datasetIds = new Hashtable<String, Alignment>();
     }
     datasetIds.put(datasetId, dataset);
   }
@@ -4669,7 +4900,7 @@ public class Jalview2XML
    * @param dataset
    * @return
    */
-  private String getDatasetIdRef(jalview.datamodel.Alignment dataset)
+  private String getDatasetIdRef(Alignment dataset)
   {
     if (dataset.getDataset() != null)
     {
@@ -4681,11 +4912,11 @@ public class Jalview2XML
       // make a new datasetId and record it
       if (dataset2Ids == null)
       {
-        dataset2Ids = new IdentityHashMap();
+        dataset2Ids = new IdentityHashMap<Alignment, String>();
       }
       else
       {
-        datasetId = (String) dataset2Ids.get(dataset);
+        datasetId = dataset2Ids.get(dataset);
       }
       if (datasetId == null)
       {
@@ -4804,8 +5035,7 @@ public class Jalview2XML
           boolean keepSeqRefs)
   {
     initSeqRefs();
-    jalview.schemabinding.version2.JalviewModel jm = saveState(ap, null,
-            null);
+    JalviewModel jm = saveState(ap, null, null, null);
 
     if (!keepSeqRefs)
     {
@@ -5088,4 +5318,14 @@ public class Jalview2XML
   
     return result;
   }
+
+  /**
+   * Returns an incrementing counter (0, 1, 2...)
+   * 
+   * @return
+   */
+  private synchronized int nextCounter()
+  {
+    return counter++;
+  }
 }
index 0d28a70..2ba4427 100644 (file)
  */
 package jalview.gui;
 
+import java.awt.Color;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Hashtable;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.Vector;
+
+import javax.swing.ButtonGroup;
+import javax.swing.JCheckBoxMenuItem;
+import javax.swing.JColorChooser;
+import javax.swing.JMenu;
+import javax.swing.JMenuItem;
+import javax.swing.JOptionPane;
+import javax.swing.JPopupMenu;
+import javax.swing.JRadioButtonMenuItem;
+
 import jalview.analysis.AAFrequency;
 import jalview.analysis.AlignmentAnnotationUtils;
 import jalview.analysis.AlignmentUtils;
@@ -59,27 +80,6 @@ import jalview.util.GroupUrlLink.UrlStringTooLongException;
 import jalview.util.MessageManager;
 import jalview.util.UrlLink;
 
-import java.awt.Color;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Hashtable;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.TreeMap;
-import java.util.Vector;
-
-import javax.swing.ButtonGroup;
-import javax.swing.JCheckBoxMenuItem;
-import javax.swing.JColorChooser;
-import javax.swing.JMenu;
-import javax.swing.JMenuItem;
-import javax.swing.JOptionPane;
-import javax.swing.JPopupMenu;
-import javax.swing.JRadioButtonMenuItem;
-
 /**
  * DOCUMENT ME!
  * 
@@ -322,49 +322,56 @@ public class PopupMenu extends JPopupMenu
         makeReferenceSeq.setText("Mark as representative");
       }
 
-      if (ap.av.getAlignment().isNucleotide() == false)
+      if (!ap.av.getAlignment().isNucleotide())
       {
         remove(rnaStructureMenu);
       }
-
-      if (ap.av.getAlignment().isNucleotide() == true)
+      else
       {
-        AlignmentAnnotation[] aa = ap.av.getAlignment()
+        /*
+         * add menu items to 2D-render any alignment or sequence secondary
+         * structure annotation
+         */
+        AlignmentAnnotation[] aas = ap.av.getAlignment()
                 .getAlignmentAnnotation();
-        for (int i = 0; aa != null && i < aa.length; i++)
+        if (aas != null)
         {
-          if (aa[i].isValidStruc() && aa[i].sequenceRef == null)
+          for (final AlignmentAnnotation aa : aas)
           {
-            final String rnastruc = aa[i].getRNAStruc();
-            final String structureLine = aa[i].label + " (alignment)";
-            menuItem = new JMenuItem();
-            menuItem.setText(MessageManager.formatMessage(
-                    "label.2d_rna_structure_line", new Object[]
-                    { structureLine }));
-            menuItem.addActionListener(new java.awt.event.ActionListener()
+            if (aa.isValidStruc() && aa.sequenceRef == null)
             {
-              @Override
-              public void actionPerformed(ActionEvent e)
+              /*
+               * valid alignment RNA secondary structure annotation
+               */
+              final String structureLine = aa.label + " (alignment)";
+              menuItem = new JMenuItem();
+              menuItem.setText(MessageManager.formatMessage(
+                      "label.2d_rna_structure_line", new Object[]
+                      { structureLine }));
+              menuItem.addActionListener(new java.awt.event.ActionListener()
               {
-                new AppVarna(structureLine, seq, seq.getSequenceAsString(),
-                        rnastruc, seq.getName(), ap);
-                System.out.println("end");
-              }
-            });
-            rnaStructureMenu.add(menuItem);
+                @Override
+                public void actionPerformed(ActionEvent e)
+                {
+                  new AppVarna(structureLine, seq, aa, ap);
+                }
+              });
+              rnaStructureMenu.add(menuItem);
+            }
           }
         }
 
 
         if (seq.getAnnotation() != null)
         {
-          AlignmentAnnotation seqAnno[] = seq.getAnnotation();
-          for (int i = 0; i < seqAnno.length; i++)
+          AlignmentAnnotation seqAnns[] = seq.getAnnotation();
+          for (final AlignmentAnnotation aa : seqAnns)
           {
-            if (seqAnno[i].isValidStruc())
+            if (aa.isValidStruc())
             {
-              final String rnastruc = seqAnno[i].getRNAStruc();
-
+              /*
+               * valid sequence RNA secondary structure annotation
+               */
               // TODO: make rnastrucF a bit more nice
               menuItem = new JMenuItem();
               menuItem.setText(MessageManager.formatMessage(
@@ -377,8 +384,7 @@ public class PopupMenu extends JPopupMenu
                 {
                   // TODO: VARNA does'nt print gaps in the sequence
 
-                  new AppVarna(seq.getName() + " structure", seq, seq
-                          .getSequenceAsString(), rnastruc, seq.getName(),
+                  new AppVarna(seq.getName() + " structure", seq, aa,
                           ap);
                 }
               });