update author list in license for (JAL-826)
[jalview.git] / src / jalview / io / VamsasAppDatastore.java
index 379982a..fb6465c 100644 (file)
@@ -1,25 +1,24 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer
- * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
+ * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ * 
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
  */
-
 package jalview.io;
 
 import jalview.bin.Cache;
+import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AlignmentView;
@@ -31,17 +30,25 @@ import jalview.gui.AlignFrame;
 import jalview.gui.AlignViewport;
 import jalview.gui.Desktop;
 import jalview.gui.TreePanel;
+import jalview.io.vamsas.Datasetsequence;
 import jalview.io.vamsas.DatastoreItem;
+import jalview.io.vamsas.DatastoreRegistry;
 import jalview.io.vamsas.Rangetype;
+import jalview.util.UrlLink;
 
+import java.io.IOException;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.IdentityHashMap;
+import java.util.Iterator;
 import java.util.Vector;
+import java.util.jar.JarInputStream;
+import java.util.jar.JarOutputStream;
 
 import uk.ac.vamsas.client.*;
 import uk.ac.vamsas.objects.core.*;
+import uk.ac.vamsas.objects.utils.Properties;
 
 /*
  * 
@@ -50,7 +57,9 @@ import uk.ac.vamsas.objects.core.*;
  * "org.exolab.castor.serializer", "org.apache.xml.serialize.XMLSerilazizer"); }
  * 
  */
-
+/*
+ * TODO: check/verify consistency for vamsas sync with group associated alignment annotation  
+ */
 public class VamsasAppDatastore
 {
   /**
@@ -59,33 +68,70 @@ public class VamsasAppDatastore
   public static final String JALVIEW_ANNOTATION_ROW = "JalviewAnnotation";
 
   /**
-   * AlignmentAnnotation property to indicate that values should not be interpolated
+   * AlignmentAnnotation property to indicate that values should not be
+   * interpolated
    */
   public static final String DISCRETE_ANNOTATION = "discrete";
+
   /**
-   * continuous property - optional to specify that annotation should be represented
-   * as a continous graph line
+   * continuous property - optional to specify that annotation should be
+   * represented as a continous graph line
    */
   private static final String CONTINUOUS_ANNOTATION = "continuous";
 
-  private static final String THRESHOLD = "threshold"; 
-  
+  private static final String THRESHOLD = "threshold";
 
+  /**
+   * template for provenance entries written to vamsas session document
+   */
   Entry provEntry = null;
 
+  /**
+   * Instance of the session document being synchronized with
+   */
   IClientDocument cdoc;
 
+  /**
+   * map Vorba (vamsas object xml ref) IDs to live jalview object references
+   */
   Hashtable vobj2jv;
 
+  /**
+   * map live jalview object references to Vorba IDs
+   */
   IdentityHashMap jv2vobj;
 
+  /**
+   * map jalview sequence set ID (which is vorba ID for alignment) to last
+   * recorded hash value for the alignment viewport (the undo/redo hash value)
+   */
+  Hashtable alignRDHash;
+
   public VamsasAppDatastore(IClientDocument cdoc, Hashtable vobj2jv,
-          IdentityHashMap jv2vobj, Entry provEntry)
+          IdentityHashMap jv2vobj, Entry provEntry, Hashtable alignRDHash)
   {
     this.cdoc = cdoc;
     this.vobj2jv = vobj2jv;
     this.jv2vobj = jv2vobj;
     this.provEntry = provEntry;
+    this.alignRDHash = alignRDHash;
+    buildSkipList();
+  }
+
+  /**
+   * the skipList used to skip over views from Jalview Appdata's that we've
+   * already syncrhonized
+   */
+  Hashtable skipList;
+
+  private void buildSkipList()
+  {
+    skipList = new Hashtable();
+    AlignFrame[] al = Desktop.getAlignframes();
+    for (int f = 0; al != null && f < al.length; f++)
+    {
+      skipList.put(al[f].getViewport().getSequenceSetId(), al[f]);
+    }
   }
 
   /**
@@ -97,6 +143,37 @@ public class VamsasAppDatastore
     {
       return cdoc.getObject((VorbaId) jv2vobj.get(jvobj));
     }
+    // check if we're working with a string - then workaround
+    // the use of IdentityHashTable because different strings
+    // have different object IDs.
+    if (jvobj instanceof String)
+    {
+      Object seqsetidobj = null;
+      seqsetidobj = getVamsasObjectBinding().get(jvobj);
+      if (seqsetidobj != null)
+      {
+        if (seqsetidobj instanceof String)
+        {
+          // what is expected. object returned by av.getSequenceSetId() -
+          // reverse lookup to get the 'registered' instance of this string
+          Vobject obj = getjv2vObj(seqsetidobj);
+          if (obj != null && !(obj instanceof Alignment))
+          {
+            Cache.log
+                    .warn("IMPLEMENTATION ERROR?: Unexpected mapping for unmapped jalview string object content:"
+                            + seqsetidobj + " to object " + obj);
+          }
+          return obj;
+        }
+        else
+        {
+          Cache.log.warn("Unexpected mapping for Jalview String Object ID "
+                  + seqsetidobj + " to another jalview dataset object "
+                  + seqsetidobj);
+        }
+      }
+    }
+
     if (Cache.log.isDebugEnabled())
     {
       Cache.log.debug("Returning null VorbaID binding for jalview object "
@@ -178,29 +255,47 @@ public class VamsasAppDatastore
    *          alignViewport to be stored
    * @param aFtitle
    *          title for alignment
+   * @return true if alignment associated with viewport was stored/synchronized
+   *         to document
    */
-  public void storeVAMSAS(AlignViewport av, String aFtitle)
+  public boolean storeVAMSAS(AlignViewport av, String aFtitle)
   {
     try
     {
       jalview.datamodel.AlignmentI jal = av.getAlignment();
+      jalview.datamodel.AlignmentI jds = jal.getDataset();
       boolean nw = false;
       VAMSAS root = null; // will be resolved based on Dataset Parent.
       // /////////////////////////////////////////
       // SAVE THE DATASET
       DataSet dataset = null;
-      if (jal.getDataset() == null)
+      if (jds == null)
       {
         Cache.log.warn("Creating new dataset for an alignment.");
         jal.setDataset(null);
+        jds = jal.getDataset();
       }
-      dataset = (DataSet) getjv2vObj(jal.getDataset());
+
+      // try and get alignment and association for sequence set id
+
+      Alignment alignment = (Alignment) getjv2vObj(av.getSequenceSetId());
+      if (alignment != null)
+      {
+        dataset = (DataSet) alignment.getV_parent();
+      }
+      else
+      {
+        // is the dataset already registered
+        dataset = (DataSet) getjv2vObj(jds);
+      }
+
       if (dataset == null)
       {
         // it might be that one of the dataset sequences does actually have a
-        // binding, so search for it indirectly.
-        jalview.datamodel.SequenceI[] jdatset = jal.getDataset()
-                .getSequencesArray();
+        // binding, so search for it indirectly. If it does, then the local
+        // jalview dataset
+        // must be merged with the existing vamsas dataset.
+        jalview.datamodel.SequenceI[] jdatset = jds.getSequencesArray();
         for (int i = 0; i < jdatset.length; i++)
         {
           Vobject vbound = getjv2vObj(jdatset[i]);
@@ -214,10 +309,11 @@ public class VamsasAppDatastore
               }
               else
               {
-                if (dataset != vbound.getV_parent())
+                if (vbound.getV_parent() != null
+                        && dataset != vbound.getV_parent())
                 {
                   throw new Error(
-                          "IMPLEMENTATION ERROR: Cannot map an alignment of sequences from datasets into the vamsas document.");
+                          "IMPLEMENTATION ERROR: Cannot map an alignment of sequences from different datasets into a single alignment in the vamsas document.");
                   // This occurs because the dataset for the alignment we are
                   // trying to
                 }
@@ -229,13 +325,15 @@ public class VamsasAppDatastore
 
       if (dataset == null)
       {
+        Cache.log.warn("Creating new vamsas dataset for alignment view "
+                + av.getSequenceSetId());
         // we create a new dataset on the default vamsas root.
         root = cdoc.getVamsasRoots()[0]; // default vamsas root for modifying.
         dataset = new DataSet();
         root.addDataSet(dataset);
-        bindjvvobj(jal.getDataset(), dataset);
+        bindjvvobj(jds, dataset);
         dataset.setProvenance(dummyProvenance());
-        dataset.getProvenance().addEntry(provEntry);
+        // dataset.getProvenance().addEntry(provEntry);
         nw = true;
       }
       else
@@ -244,100 +342,53 @@ public class VamsasAppDatastore
       }
       // update dataset
       Sequence sequence;
-      DbRef dbref;
       // set new dataset and alignment sequences based on alignment Nucleotide
       // flag.
       // this *will* break when alignment contains both nucleotide and amino
       // acid sequences.
       String dict = jal.isNucleotide() ? uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA
               : uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_AA;
+      Vector dssmods = new Vector();
       for (int i = 0; i < jal.getHeight(); i++)
       {
         SequenceI sq = jal.getSequenceAt(i).getDatasetSequence(); // only insert
         // referenced
         // sequences
         // to dataset.
-        sequence = (Sequence) getjv2vObj(sq);
-        if (sequence == null)
-        {
-          sequence = new Sequence();
-          bindjvvobj(sq, sequence);
-          sq.setVamsasId(sequence.getVorbaId().getId());
-          sequence.setSequence(sq.getSequenceAsString());
-          sequence.setDictionary(dict);
-          sequence.setName(jal.getDataset().getSequenceAt(i).getName());
-          sequence.setStart(jal.getDataset().getSequenceAt(i).getStart());
-          sequence.setEnd(jal.getDataset().getSequenceAt(i).getEnd());
-          sequence.setDescription(jal.getDataset().getSequenceAt(i)
-                  .getDescription());
-          dataset.addSequence(sequence);
-        }
-        else
+        Datasetsequence dssync = new jalview.io.vamsas.Datasetsequence(
+                this, sq, dict, dataset);
+        sequence = (Sequence) dssync.getVobj();
+        if (dssync.getModified())
         {
-          // verify principal attributes. and update any new
-          // features/references.
-          System.out.println("update dataset sequence object.");
+          dssmods.addElement(sequence);
         }
-        if (sq.getSequenceFeatures() != null)
-        {
-          int sfSize = sq.getSequenceFeatures().length;
-
-          for (int sf = 0; sf < sfSize; sf++)
-          {
-            jalview.datamodel.SequenceFeature feature = (jalview.datamodel.SequenceFeature) sq
-                    .getSequenceFeatures()[sf];
-
-            DataSetAnnotations dsa = (DataSetAnnotations) getjv2vObj(feature);
-            if (dsa == null)
-            {
-              dsa = (DataSetAnnotations) getDSAnnotationFromJalview(
-                      new DataSetAnnotations(), feature);
-              if (dsa.getProvenance() == null)
-              {
-                dsa.setProvenance(new Provenance());
-              }
-              addProvenance(dsa.getProvenance(), "created"); // JBPNote - need
-              // to update
-              dsa.addSeqRef(sequence); // we have just created this annotation
-                                        // - so safe to use this
-              bindjvvobj(feature, dsa);
-              dataset.addDataSetAnnotations(dsa);
-            }
-            else
-            {
-              // todo: verify and update dataset annotations for sequence
-              System.out.println("update dataset sequence annotations.");
-            }
-          }
-        }
-
-        if (sq.getDBRef() != null)
+        ;
+      }
+      if (dssmods.size() > 0)
+      {
+        if (!nw)
         {
-          DBRefEntry[] entries = sq.getDBRef();
-          jalview.datamodel.DBRefEntry dbentry;
-          for (int db = 0; db < entries.length; db++)
-          {
-            Rangetype dbr = new jalview.io.vamsas.Dbref(this,
-                    dbentry = entries[db], sq, sequence);
-          }
-
+          Entry pentry = this.addProvenance(dataset.getProvenance(),
+                  "updated sequences");
+          // pentry.addInput(vInput); could write in which sequences were
+          // modified.
+          dssmods.removeAllElements();
         }
       }
       // dataset.setProvenance(getVamsasProvenance(jal.getDataset().getProvenance()));
       // ////////////////////////////////////////////
-      if (!av.getAlignment().isAligned())
-        return; // TODO: trees could be written - but for the moment we just skip
-      
-      // ////////////////////////////////////////////
-      // Save the Alignments
+      if (alignmentWillBeSkipped(av))
+      {
+        // TODO: trees could be written - but for the moment we just
+        addToSkipList(av);
+        // add to the JalviewXML skipList and ..
+        return false;
+      }
 
-      Alignment alignment = (Alignment) getjv2vObj(av); // this is so we can get
-                                                        // the alignviewport
-                                                        // back
       if (alignment == null)
       {
         alignment = new Alignment();
-        bindjvvobj(av, alignment);
+        bindjvvobj(av.getSequenceSetId(), alignment);
         if (alignment.getProvenance() == null)
         {
           alignment.setProvenance(new Provenance());
@@ -354,37 +405,76 @@ public class VamsasAppDatastore
           alignment.addProperty(title);
         }
         alignment.setGapChar(String.valueOf(av.getGapCharacter()));
-        AlignmentSequence alseq = null;
         for (int i = 0; i < jal.getHeight(); i++)
         {
-          alseq = new AlignmentSequence();
-          // TODO: VAMSAS: translate lowercase symbols to annotation ?
-          alseq.setSequence(jal.getSequenceAt(i).getSequenceAsString());
-          alseq.setName(jal.getSequenceAt(i).getName());
-          alseq.setStart(jal.getSequenceAt(i).getStart());
-          alseq.setEnd(jal.getSequenceAt(i).getEnd());
-          if (getjv2vObj(jal.getSequenceAt(i).getDatasetSequence()) == null)
-          {
-            Cache.log
-                    .warn("Serious. Unbound dataset sequence in alignment: "
-                            + jal.getSequenceAt(i).getDatasetSequence());
-          }
-          alseq.setRefid(getjv2vObj(jal.getSequenceAt(i)
-                  .getDatasetSequence()));
-          alignment.addAlignmentSequence(alseq);
-          bindjvvobj(jal.getSequenceAt(i), alseq);
+          syncToAlignmentSequence(jal.getSequenceAt(i), alignment, null);
         }
+        alignRDHash.put(av.getSequenceSetId(), av.getUndoRedoHash());
       }
       else
       {
+        // always prepare to clone the alignment
+        boolean alismod = av.isUndoRedoHashModified((long[]) alignRDHash
+                .get(av.getSequenceSetId()));
         // todo: verify and update mutable alignment props.
-        if (alignment.getModifiable() == null) // TODO: USE VAMSAS LIBRARY
-                                                // OBJECT LOCK METHODS
+        // TODO: Use isLocked methods
+        if (alignment.getModifiable() == null
+                || alignment.getModifiable().length() == 0)
+        // && !alignment.isDependedOn())
         {
-          System.out.println("update alignment in document.");
+          boolean modified = false;
+          // check existing sequences in local and in document.
+          Vector docseqs = new Vector(
+                  alignment.getAlignmentSequenceAsReference());
+          for (int i = 0; i < jal.getHeight(); i++)
+          {
+            modified |= syncToAlignmentSequence(jal.getSequenceAt(i),
+                    alignment, docseqs);
+          }
+          if (docseqs.size() > 0)
+          {
+            // removeValignmentSequences(alignment, docseqs);
+            docseqs.removeAllElements();
+            System.out
+                    .println("Sequence deletion from alignment is not implemented.");
+
+          }
+          if (modified)
+          {
+            if (alismod)
+            {
+              // info in the undo
+              addProvenance(alignment.getProvenance(), "Edited"); // TODO:
+              // insert
+              // something
+              // sensible
+              // here again
+            }
+            else
+            {
+              // info in the undo
+              addProvenance(alignment.getProvenance(), "Attributes Edited"); // TODO:
+              // insert
+              // something
+              // sensible
+              // here
+              // again
+            }
+          }
+          if (alismod)
+          {
+            System.out.println("update alignment in document.");
+          }
+          else
+          {
+            System.out.println("alignment in document left unchanged.");
+          }
         }
         else
         {
+          // unbind alignment from view.
+          // create new binding and new alignment.
+          // mark trail on new alignment as being derived from old ?
           System.out
                   .println("update edited alignment to new alignment in document.");
         }
@@ -406,18 +496,18 @@ public class VamsasAppDatastore
            * jalview.datamodel.SequenceFeature[] features = alseq
            * .getSequenceFeatures(); for (int f = 0; f < features.length; f++) {
            * if (features[f] != null) { AlignmentSequenceAnnotation valseqf = (
-           * AlignmentSequenceAnnotation) getjv2vObj(features[i]); if (valseqf ==
-           * null) {
+           * AlignmentSequenceAnnotation) getjv2vObj(features[i]); if (valseqf
+           * == null) {
            * 
            * valseqf = (AlignmentSequenceAnnotation) getDSAnnotationFromJalview(
            * new AlignmentSequenceAnnotation(), features[i]);
            * valseqf.setGraph(false);
-           * valseqf.addProperty(newProperty("jalview:feature","boolean","true"));
-           * if (valseqf.getProvenance() == null) { valseqf.setProvenance(new
-           * Provenance()); } addProvenance(valseqf.getProvenance(), "created"); //
-           * JBPNote - // need to // update bindjvvobj(features[i], valseqf);
-           * valseq.addAlignmentSequenceAnnotation(valseqf); } }
-           *  }
+           * valseqf.addProperty(newProperty("jalview:feature"
+           * ,"boolean","true")); if (valseqf.getProvenance() == null) {
+           * valseqf.setProvenance(new Provenance()); }
+           * addProvenance(valseqf.getProvenance(), "created"); // JBPNote - //
+           * need to // update bindjvvobj(features[i], valseqf);
+           * valseq.addAlignmentSequenceAnnotation(valseqf); } } }
            */
         }
       }
@@ -437,6 +527,13 @@ public class VamsasAppDatastore
           {
             continue;
           }
+          if (aa[i].groupRef != null)
+          {
+            // TODO: store any group associated annotation references
+            Cache.log
+                    .warn("Group associated sequence annotation is not stored in VAMSAS document.");
+            continue;
+          }
           if (aa[i].sequenceRef != null)
           {
             // Deal with sequence associated annotation
@@ -479,8 +576,8 @@ public class VamsasAppDatastore
               an.setDescription(aa[i].description);
               alignment.addAlignmentAnnotation(an);
               Seg vSeg = new Seg(); // TODO: refactor to have a default
-                                    // rangeAnnotationType initer/updater that
-                                    // takes a set of int ranges.
+              // rangeAnnotationType initer/updater that
+              // takes a set of int ranges.
               vSeg.setStart(1);
               vSeg.setInclusive(true);
               vSeg.setEnd(jal.getWidth());
@@ -494,7 +591,7 @@ public class VamsasAppDatastore
               if (aa[i].graph != AlignmentAnnotation.NO_GRAPH)
               {
                 an.setGroup(Integer.toString(aa[i].graphGroup)); // // JBPNote
-                                                                  // -
+                // -
                 // originally we
                 // were going to
                 // store
@@ -528,16 +625,17 @@ public class VamsasAppDatastore
                 // of
                 // utf8
                 // translation
-                ae.addValue(aa[i].annotations[a].value);
+                if (an.isGraph())
+                {
+                  ae.addValue(aa[i].annotations[a].value);
+                }
                 ae.setPosition(a + 1);
                 if (aa[i].annotations[a].secondaryStructure != ' ')
                 {
                   Glyph ss = new Glyph();
-                  ss
-                          .setDict(uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE);
-                  ss
-                          .setContent(String
-                                  .valueOf(aa[i].annotations[a].secondaryStructure));
+                  ss.setDict(uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE);
+                  ss.setContent(String
+                          .valueOf(aa[i].annotations[a].secondaryStructure));
                   ae.addGlyph(ss);
                 }
                 an.addAnnotationElement(ae);
@@ -550,23 +648,30 @@ public class VamsasAppDatastore
                 // modifiable flag is supposed to be used.
               }
               setAnnotationType(an, aa[i]);
-              
+
               if (aa[i].graph != jalview.datamodel.AlignmentAnnotation.NO_GRAPH)
               {
                 an.setGraph(true);
                 an.setGroup(Integer.toString(aa[i].graphGroup));
-                if (aa[i].threshold!=null && aa[i].threshold.displayed)
-                  an.addProperty(newProperty(THRESHOLD, "float", ""+aa[i].threshold.value));
-                  if (aa[i].threshold.label!=null)
-                    an.addProperty(newProperty(THRESHOLD+"Name", "string", ""+aa[i].threshold.label));
+                if (aa[i].threshold != null && aa[i].threshold.displayed)
+                {
+                  an.addProperty(Properties.newProperty(THRESHOLD,
+                          Properties.FLOATTYPE, "" + aa[i].threshold.value));
+                  if (aa[i].threshold.label != null)
+                  {
+                    an.addProperty(Properties.newProperty(THRESHOLD
+                            + "Name", Properties.STRINGTYPE, ""
+                            + aa[i].threshold.label));
+                  }
                 }
-              
               }
-            
+
+            }
+
             else
             {
               if (an.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT
-                                              // LOCK METHODS)
+              // LOCK METHODS)
               {
                 // verify annotation - update (perhaps)
                 Cache.log
@@ -599,7 +704,8 @@ public class VamsasAppDatastore
           {
             TreePanel tp = (TreePanel) frames[t];
 
-            if (tp.getAlignment() == jal)
+            if (tp.getViewPort().getSequenceSetId()
+                    .equals(av.getSequenceSetId()))
             {
               DatastoreItem vtree = new jalview.io.vamsas.Tree(this, tp,
                       jal, alignment);
@@ -614,8 +720,266 @@ public class VamsasAppDatastore
     catch (Exception ex)
     {
       ex.printStackTrace();
+      return false;
+    }
+    return true;
+  }
+
+  /**
+   * very quick test to see if the viewport would be stored in the vamsas
+   * document. Reasons for not storing include the unaligned flag being false
+   * (for all sequences, including the hidden ones!)
+   * 
+   * @param av
+   * @return true if alignment associated with this view will be stored in
+   *         document.
+   */
+  public boolean alignmentWillBeSkipped(AlignViewport av)
+  {
+    return (!av.getAlignment().isAligned());
+  }
+
+  private void addToSkipList(AlignViewport av)
+  {
+    if (skipList == null)
+    {
+      skipList = new Hashtable();
+    }
+    skipList.put(av.getSequenceSetId(), av);
+  }
+
+  /**
+   * remove docseqs from the given alignment marking provenance appropriately
+   * and removing any references to the sequences.
+   * 
+   * @param alignment
+   * @param docseqs
+   */
+  private void removeValignmentSequences(Alignment alignment, Vector docseqs)
+  {
+    // delete these from document. This really needs to be a generic document
+    // API function derived by CASTOR.
+    Enumeration en = docseqs.elements();
+    while (en.hasMoreElements())
+    {
+      alignment.removeAlignmentSequence((AlignmentSequence) en
+              .nextElement());
+    }
+    Entry pe = addProvenance(alignment.getProvenance(), "Removed "
+            + docseqs.size() + " sequences");
+    en = alignment.enumerateAlignmentAnnotation();
+    Vector toremove = new Vector();
+    while (en.hasMoreElements())
+    {
+      uk.ac.vamsas.objects.core.AlignmentAnnotation alan = (uk.ac.vamsas.objects.core.AlignmentAnnotation) en
+              .nextElement();
+      if (alan.getSeqrefsCount() > 0)
+      {
+        int p = 0;
+        Vector storem = new Vector();
+        Enumeration sr = alan.enumerateSeqrefs();
+        while (sr.hasMoreElements())
+        {
+          Object alsr = sr.nextElement();
+          if (docseqs.contains(alsr))
+          {
+            storem.addElement(alsr);
+          }
+        }
+        // remove references to the deleted sequences
+        sr = storem.elements();
+        while (sr.hasMoreElements())
+        {
+          alan.removeSeqrefs(sr.nextElement());
+        }
+
+        if (alan.getSeqrefsCount() == 0)
+        {
+          // should then delete alan from dataset
+          toremove.addElement(alan);
+        }
+      }
+    }
+    // remove any annotation that used to be associated to a specific bunch of
+    // sequences
+    en = toremove.elements();
+    while (en.hasMoreElements())
+    {
+      alignment
+              .removeAlignmentAnnotation((uk.ac.vamsas.objects.core.AlignmentAnnotation) en
+                      .nextElement());
+    }
+    // TODO: search through alignment annotations to remove any references to
+    // this alignment sequence
+  }
+
+  /**
+   * sync a jalview alignment seuqence into a vamsas alignment assumes all lock
+   * transformation/bindings have been sorted out before hand. creates/syncs the
+   * vamsas alignment sequence for jvalsq and adds it to the alignment if
+   * necessary. unbounddocseq is a duplicate of the vamsas alignment sequences
+   * and these are removed after being processed w.r.t a bound jvalsq
+   * 
+   */
+  private boolean syncToAlignmentSequence(SequenceI jvalsq,
+          Alignment alignment, Vector unbounddocseq)
+  {
+    boolean modal = false;
+    // todo: islocked method here
+    boolean up2doc = false;
+    AlignmentSequence alseq = (AlignmentSequence) getjv2vObj(jvalsq);
+    if (alseq == null)
+    {
+      alseq = new AlignmentSequence();
+      up2doc = true;
     }
+    else
+    {
+      if (unbounddocseq != null)
+      {
+        unbounddocseq.removeElement(alseq);
+      }
+    }
+    // boolean locked = (alignment.getModifiable()==null ||
+    // alignment.getModifiable().length()>0);
+    // TODO: VAMSAS: translate lowercase symbols to annotation ?
+    if (up2doc || !alseq.getSequence().equals(jvalsq.getSequenceAsString()))
+    {
+      alseq.setSequence(jvalsq.getSequenceAsString());
+      alseq.setStart(jvalsq.getStart());
+      alseq.setEnd(jvalsq.getEnd());
+      modal = true;
+    }
+    if (up2doc || !alseq.getName().equals(jvalsq.getName()))
+    {
+      modal = true;
+      alseq.setName(jvalsq.getName());
+    }
+    if (jvalsq.getDescription() != null
+            && (alseq.getDescription() == null || !jvalsq.getDescription()
+                    .equals(alseq.getDescription())))
+    {
+      modal = true;
+      alseq.setDescription(jvalsq.getDescription());
+    }
+    if (getjv2vObj(jvalsq.getDatasetSequence()) == null)
+    {
+      Cache.log
+              .warn("Serious Implementation error - Unbound dataset sequence in alignment: "
+                      + jvalsq.getDatasetSequence());
+    }
+    alseq.setRefid(getjv2vObj(jvalsq.getDatasetSequence()));
+    if (up2doc)
+    {
 
+      alignment.addAlignmentSequence(alseq);
+      bindjvvobj(jvalsq, alseq);
+    }
+    return up2doc || modal;
+  }
+
+  /**
+   * locally sync a jalview alignment seuqence from a vamsas alignment assumes
+   * all lock transformation/bindings have been sorted out before hand.
+   * creates/syncs the jvalsq from the alignment sequence
+   */
+  private boolean syncFromAlignmentSequence(AlignmentSequence valseq,
+          char valGapchar, char gapChar, Vector dsseqs)
+
+  {
+    boolean modal = false;
+    // todo: islocked method here
+    boolean upFromdoc = false;
+    jalview.datamodel.SequenceI alseq = (SequenceI) getvObj2jv(valseq);
+    if (alseq == null)
+    {
+      upFromdoc = true;
+    }
+    if (alseq != null)
+    {
+
+      // boolean locked = (alignment.getModifiable()==null ||
+      // alignment.getModifiable().length()>0);
+      // TODO: VAMSAS: translate lowercase symbols to annotation ?
+      if (upFromdoc
+              || !valseq.getSequence().equals(alseq.getSequenceAsString()))
+      {
+        // this might go *horribly* wrong
+        alseq.setSequence(new String(valseq.getSequence()).replace(
+                valGapchar, gapChar));
+        alseq.setStart((int) valseq.getStart());
+        alseq.setEnd((int) valseq.getEnd());
+        modal = true;
+      }
+      if (!valseq.getName().equals(alseq.getName()))
+      {
+        modal = true;
+        alseq.setName(valseq.getName());
+      }
+      if (alseq.getDescription() == null
+              || (valseq.getDescription() != null && !alseq
+                      .getDescription().equals(valseq.getDescription())))
+      {
+        alseq.setDescription(valseq.getDescription());
+        modal = true;
+      }
+      if (modal && Cache.log.isDebugEnabled())
+      {
+        Cache.log.debug("Updating apparently edited sequence "
+                + alseq.getName());
+      }
+    }
+    else
+    {
+      alseq = new jalview.datamodel.Sequence(valseq.getName(), valseq
+              .getSequence().replace(valGapchar, gapChar),
+              (int) valseq.getStart(), (int) valseq.getEnd());
+
+      Vobject datsetseq = (Vobject) valseq.getRefid();
+      if (datsetseq != null)
+      {
+        alseq.setDatasetSequence((SequenceI) getvObj2jv(datsetseq)); // exceptions
+        if (valseq.getDescription() != null)
+        {
+          alseq.setDescription(valseq.getDescription());
+        }
+        else
+        {
+          // inherit description line from dataset.
+          if (alseq.getDatasetSequence().getDescription() != null)
+          {
+            alseq.setDescription(alseq.getDatasetSequence()
+                    .getDescription());
+          }
+        }
+        // if
+        // AlignemntSequence
+        // reference
+        // isn't
+        // a
+        // simple
+        // SequenceI
+      }
+      else
+      {
+        Cache.log
+                .error("Invalid dataset sequence id (null) for alignment sequence "
+                        + valseq.getVorbaId());
+      }
+      bindjvvobj(alseq, valseq);
+      alseq.setVamsasId(valseq.getVorbaId().getId());
+      dsseqs.add(alseq);
+    }
+    Vobject datsetseq = (Vobject) valseq.getRefid();
+    if (datsetseq != null)
+    {
+      if (datsetseq != alseq.getDatasetSequence())
+      {
+        modal = true;
+      }
+      alseq.setDatasetSequence((SequenceI) getvObj2jv(datsetseq)); // exceptions
+    }
+    return upFromdoc || modal;
   }
 
   private void initRangeAnnotationType(RangeAnnotation an,
@@ -669,8 +1033,7 @@ public class VamsasAppDatastore
       {
         // we only write an annotation where it really exists.
         Glyph ss = new Glyph();
-        ss
-                .setDict(uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE);
+        ss.setDict(uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE);
         ss.setContent(String
                 .valueOf(alan.annotations[a].secondaryStructure));
         ae.addGlyph(ss);
@@ -706,11 +1069,13 @@ public class VamsasAppDatastore
       // the Jalview
       // specific
       // bits.
-      if (alan.getThreshold()!=null && alan.getThreshold().displayed)
+      if (alan.getThreshold() != null && alan.getThreshold().displayed)
       {
-        an.addProperty(newProperty(THRESHOLD, "float", ""+alan.getThreshold().value));
-        if (alan.getThreshold().label!=null)
-          an.addProperty(newProperty(THRESHOLD+"Name", "string", ""+alan.getThreshold().label));
+        an.addProperty(Properties.newProperty(THRESHOLD,
+                Properties.FLOATTYPE, "" + alan.getThreshold().value));
+        if (alan.getThreshold().label != null)
+          an.addProperty(Properties.newProperty(THRESHOLD + "Name",
+                  Properties.STRINGTYPE, "" + alan.getThreshold().label));
       }
       ((DataSet) sref.getV_parent()).addDataSetAnnotations(an);
       bindjvvobj(alan, an);
@@ -719,7 +1084,7 @@ public class VamsasAppDatastore
     {
       // update reference sequence Annotation
       if (an.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT LOCK
-                                      // METHODS)
+      // METHODS)
       {
         // verify existing alignment sequence annotation is up to date
         System.out.println("update dataset sequence annotation.");
@@ -773,7 +1138,7 @@ public class VamsasAppDatastore
        * something extracted from another program, etc)
        */
       an.setType(JALVIEW_ANNOTATION_ROW); // TODO: better fix
-                                                          // this rough guess ;)
+      // this rough guess ;)
       alsref.addAlignmentSequenceAnnotation(an);
       bindjvvobj(alan, an);
       // These properties are directly supported by the
@@ -787,7 +1152,7 @@ public class VamsasAppDatastore
     {
       // update reference sequence Annotation
       if (an.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT LOCK
-                                      // METHODS)
+      // METHODS)
       {
         // verify existing alignment sequence annotation is up to date
         System.out.println("update alignment sequence annotation.");
@@ -803,104 +1168,49 @@ public class VamsasAppDatastore
 
   /**
    * set vamsas annotation object type from jalview annotation
+   * 
    * @param an
    * @param alan
    */
-  private void setAnnotationType(RangeAnnotation an, AlignmentAnnotation alan)
+  private void setAnnotationType(RangeAnnotation an,
+          AlignmentAnnotation alan)
   {
     if (an instanceof AlignmentSequenceAnnotation)
     {
       if (alan.graph != AlignmentAnnotation.NO_GRAPH)
       {
-        ((AlignmentSequenceAnnotation)an).setGraph(true);
-      } else
+        ((AlignmentSequenceAnnotation) an).setGraph(true);
+      }
+      else
       {
-      ((AlignmentSequenceAnnotation)an).setGraph(false);
+        ((AlignmentSequenceAnnotation) an).setGraph(false);
       }
     }
     if (an instanceof uk.ac.vamsas.objects.core.AlignmentAnnotation)
     {
       if (alan.graph != AlignmentAnnotation.NO_GRAPH)
       {
-        ((uk.ac.vamsas.objects.core.AlignmentAnnotation)an).setGraph(true);
-      } else
+        ((uk.ac.vamsas.objects.core.AlignmentAnnotation) an).setGraph(true);
+      }
+      else
       {
-      ((uk.ac.vamsas.objects.core.AlignmentAnnotation)an).setGraph(false);
+        ((uk.ac.vamsas.objects.core.AlignmentAnnotation) an)
+                .setGraph(false);
       }
     }
     switch (alan.graph)
     {
-      case AlignmentAnnotation.BAR_GRAPH:
-        an.addProperty(newProperty(DISCRETE_ANNOTATION, "boolean","true"));
-        break;
-      case AlignmentAnnotation.LINE_GRAPH:
-        an.addProperty(newProperty(CONTINUOUS_ANNOTATION, "boolean","true"));
-        break;
-        default:
-          // don't add any kind of discrete or continous property info.
-    }
-  }
-
-  private Property newProperty(String name, String type, String content)
-  {
-    Property vProperty = new Property();
-    vProperty.setName(name);
-    if (type != null)
-    {
-      vProperty.setType(type);
-    }
-    else
-    {
-      vProperty.setType("String");
+    case AlignmentAnnotation.BAR_GRAPH:
+      an.addProperty(Properties.newProperty(DISCRETE_ANNOTATION,
+              Properties.BOOLEANTYPE, "true"));
+      break;
+    case AlignmentAnnotation.LINE_GRAPH:
+      an.addProperty(Properties.newProperty(CONTINUOUS_ANNOTATION,
+              Properties.BOOLEANTYPE, "true"));
+      break;
+    default:
+      // don't add any kind of discrete or continous property info.
     }
-    vProperty.setContent(content);
-    return vProperty;
-  }
-
-  /**
-   * correctly create a RangeAnnotation from a jalview sequence feature
-   * 
-   * @param dsa
-   *          (typically DataSetAnnotations or AlignmentSequenceAnnotation)
-   * @param feature
-   *          (the feature to be mapped from)
-   * @return
-   */
-  private RangeAnnotation getDSAnnotationFromJalview(RangeAnnotation dsa,
-          SequenceFeature feature)
-  {
-    dsa.setType(feature.getType());
-    Seg vSeg = new Seg();
-    vSeg.setStart(feature.getBegin());
-    vSeg.setEnd(feature.getEnd());
-    vSeg.setInclusive(true);
-    dsa.addSeg(vSeg);
-    dsa.setDescription(feature.getDescription());
-    dsa.setStatus(feature.getStatus());
-    if (feature.links != null && feature.links.size() > 0)
-    {
-      for (int i = 0, iSize = feature.links.size(); i < iSize; i++)
-      {
-        String link = (String) feature.links.elementAt(i);
-        int sep = link.indexOf('|');
-        if (sep > -1)
-        {
-          Link vLink = new Link();
-          if (sep > 0)
-          {
-            vLink.setContent(link.substring(0, sep - 1));
-          }
-          else
-          {
-            vLink.setContent("");
-          }
-          vLink.setHref(link.substring(sep + 1)); // TODO: validate href.
-          dsa.addLink(vLink);
-        }
-      }
-    }
-    dsa.setGroup(feature.getFeatureGroup());
-    return dsa;
   }
 
   /**
@@ -939,40 +1249,317 @@ public class VamsasAppDatastore
    */
   private boolean isJalviewOnly(AlignmentAnnotation annotation)
   {
-    return annotation.label.equals("Quality")
+    return annotation.autoCalculated || annotation.label.equals("Quality")
             || annotation.label.equals("Conservation")
             || annotation.label.equals("Consensus");
   }
 
+  boolean dojvsync = true;
+
+  // boolean dojvsync = false; // disables Jalview AppData IO
   /**
-   * This will return the first AlignFrame viewing AlignViewport av. It will
-   * break if there are more than one AlignFrames viewing a particular av. This
-   * also shouldn't be in the io package.
-   * 
-   * @param av
-   * @return alignFrame for av
+   * list of alignment views created when updating Jalview from document.
    */
-  public AlignFrame getAlignFrameFor(AlignViewport av)
+  private Vector newAlignmentViews = new Vector();
+
+  /**
+   * update local jalview view settings from the stored appdata (if any)
+   */
+  public void updateJalviewFromAppdata()
   {
-    if (Desktop.desktop != null)
+    // recover any existing Jalview data from appdata
+    // TODO: recover any PDB files stored as attachments in the vamsas session
+    // and initialise the Jalview2XML.alreadyLoadedPDB hashtable with mappings
+    // to temp files.
     {
-      javax.swing.JInternalFrame[] frames = Desktop.instance.getAllFrames();
+      final IClientAppdata cappdata = cdoc.getClientAppdata();
+      if (cappdata != null)
+      {
+        if (cappdata.hasClientAppdata())
+        {
+          // TODO: how to check version of Jalview client app data and whether
+          // it has been modified
+          // client data is shared over all app clients
+          try
+          {
+            jalview.gui.Jalview2XML fromxml = new jalview.gui.Jalview2XML();
+            fromxml.attemptversion1parse = false;
+            fromxml.setUniqueSetSuffix("");
+            fromxml.setObjectMappingTables(vobj2jv, jv2vobj); // mapKeysToString
+            // and
+            // mapValuesToString
+            fromxml.setSkipList(skipList);
+            jalview.util.jarInputStreamProvider jprovider = new jalview.util.jarInputStreamProvider()
+            {
+
+              public String getFilename()
+              {
+
+                // TODO Get the vamsas session ID here
+                return "Jalview Vamsas Document Client Data";
+              }
+
+              public JarInputStream getJarInputStream() throws IOException
+              {
+                jalview.bin.Cache.log
+                        .debug("Returning client input stream for Jalview from Vamsas Document.");
+                return new JarInputStream(cappdata.getClientInputStream());
+              }
+            };
+            if (dojvsync)
+            {
+              fromxml.LoadJalviewAlign(jprovider);
+            }
+          } catch (Exception e)
+          {
+
+          } catch (OutOfMemoryError e)
+          {
 
-      for (int t = 0; t < frames.length; t++)
+          } catch (Error e)
+          {
+
+          }
+        }
+      }
+      if (cappdata.hasUserAppdata())
       {
-        if (frames[t] instanceof AlignFrame)
+        // TODO: how to check version of Jalview user app data and whether it
+        // has been modified
+        // user data overrides data shared over all app clients ?
+        try
         {
-          if (((AlignFrame) frames[t]).getViewport() == av)
+          jalview.gui.Jalview2XML fromxml = new jalview.gui.Jalview2XML();
+          fromxml.attemptversion1parse = false;
+          fromxml.setUniqueSetSuffix("");
+          fromxml.setSkipList(skipList);
+          fromxml.setObjectMappingTables(mapKeysToString(vobj2jv),
+                  mapValuesToString(jv2vobj));
+          jalview.util.jarInputStreamProvider jarstream = new jalview.util.jarInputStreamProvider()
           {
-            return (AlignFrame) frames[t];
+
+            public String getFilename()
+            {
+
+              // TODO Get the vamsas session ID here
+              return "Jalview Vamsas Document User Data";
+            }
+
+            public JarInputStream getJarInputStream() throws IOException
+            {
+              jalview.bin.Cache.log
+                      .debug("Returning user input stream for Jalview from Vamsas Document.");
+              return new JarInputStream(cappdata.getUserInputStream());
+            }
+          };
+          if (dojvsync)
+          {
+            fromxml.LoadJalviewAlign(jarstream);
           }
+        } catch (Exception e)
+        {
+
+        } catch (OutOfMemoryError e)
+        {
+
+        } catch (Error e)
+        {
+
         }
       }
+
     }
-    return null;
+    flushAlignViewports();
   }
 
-  public void updateToJalview()
+  /**
+   * remove any spurious views generated by document synchronization
+   */
+  private void flushAlignViewports()
+  {
+    // remove any additional viewports originally recovered from the vamsas
+    // document.
+    // search for all alignframes containing viewports generated from document
+    // sync,
+    // and if any contain more than one view, then remove the one generated by
+    // document update.
+    AlignViewport views[], av = null;
+    AlignFrame af = null;
+    Iterator newviews = newAlignmentViews.iterator();
+    while (newviews.hasNext())
+    {
+      av = (AlignViewport) newviews.next();
+      af = Desktop.getAlignFrameFor(av);
+      // TODO implement this : af.getNumberOfViews
+      String seqsetidobj = av.getSequenceSetId();
+      views = Desktop.getViewports(seqsetidobj);
+      Cache.log.debug("Found "
+              + (views == null ? " no " : "" + views.length)
+              + " views for '" + av.getSequenceSetId() + "'");
+      if (views.length > 1)
+      {
+        // we need to close the original document view.
+
+        // work out how to do this by seeing if the views are gathered.
+        // pretty clunky but the only way to do this without adding more flags
+        // to the align frames.
+        boolean gathered = false;
+        String newviewid = null;
+        AlignedCodonFrame[] mappings = av.getAlignment().getCodonFrames();
+        for (int i = 0; i < views.length; i++)
+        {
+          if (views[i] != av)
+          {
+            AlignFrame viewframe = Desktop.getAlignFrameFor(views[i]);
+            if (viewframe == af)
+            {
+              gathered = true;
+            }
+            newviewid = views[i].getSequenceSetId();
+          }
+          else
+          {
+            // lose the reference to the vamsas document created view
+            views[i] = null;
+          }
+        }
+        // close the view generated by the vamsas document synchronization
+        if (gathered)
+        {
+          af.closeView(av);
+        }
+        else
+        {
+          af.closeMenuItem_actionPerformed(false);
+        }
+        replaceJvObjMapping(seqsetidobj, newviewid);
+        seqsetidobj = newviewid;
+        // not sure if we need to do this:
+
+        if (false) // mappings != null)
+        {
+          // ensure sequence mappings from vamsas document view still
+          // active
+          if (mappings != null && mappings.length > 0)
+          {
+            jalview.structure.StructureSelectionManager
+                    .getStructureSelectionManager(Desktop.instance).addMappings(mappings);
+          }
+        }
+      }
+      // ensure vamsas object binds to the stored views retrieved from
+      // Jalview appdata
+      // jalview.structure.StructureSelectionManager
+      // .getStructureSelectionManager()
+      // .addStructureViewerListener(viewframe.alignPanel);
+
+    }
+
+    newviews = null;
+    newAlignmentViews.clear();
+  }
+
+  /**
+   * replaces oldjvobject with newjvobject in the Jalview Object <> VorbaID
+   * binding tables
+   * 
+   * @param oldjvobject
+   * @param newjvobject
+   *          (may be null)
+   */
+  private void replaceJvObjMapping(Object oldjvobject, Object newjvobject)
+  {
+    Object vobject = jv2vobj.remove(oldjvobject);
+    if (vobject == null)
+    {
+      throw new Error(
+              "IMPLEMENTATION ERROR: old jalview object is not bound ! ("
+                      + oldjvobject + ")");
+    }
+    if (newjvobject != null)
+    {
+      jv2vobj.put(newjvobject, vobject);
+      vobj2jv.put(vobject, newjvobject);
+    }
+  }
+
+  /**
+   * Update the jalview client and user appdata from the local jalview settings
+   */
+  public void updateJalviewClientAppdata()
+  {
+    final IClientAppdata cappdata = cdoc.getClientAppdata();
+    if (cappdata != null)
+    {
+      try
+      {
+        jalview.gui.Jalview2XML jxml = new jalview.gui.Jalview2XML();
+        jxml.setObjectMappingTables(mapKeysToString(vobj2jv),
+                mapValuesToString(jv2vobj));
+        jxml.setSkipList(skipList);
+        if (dojvsync)
+        {
+          jxml.SaveState(new JarOutputStream(cappdata
+                  .getClientOutputStream()));
+        }
+
+      } catch (Exception e)
+      {
+        // TODO raise GUI warning if user requests it.
+        jalview.bin.Cache.log
+                .error("Couldn't update jalview client application data. Giving up - local settings probably lost.",
+                        e);
+      }
+    }
+    else
+    {
+      jalview.bin.Cache.log
+              .error("Couldn't access client application data for vamsas session. This is probably a vamsas client bug.");
+    }
+  }
+
+  /**
+   * translate the Vobject keys to strings for use in Jalview2XML
+   * 
+   * @param jv2vobj2
+   * @return
+   */
+  private IdentityHashMap mapValuesToString(IdentityHashMap jv2vobj2)
+  {
+    IdentityHashMap mapped = new IdentityHashMap();
+    Iterator keys = jv2vobj2.keySet().iterator();
+    while (keys.hasNext())
+    {
+      Object key = keys.next();
+      mapped.put(key, jv2vobj2.get(key).toString());
+    }
+    return mapped;
+  }
+
+  /**
+   * translate the Vobject values to strings for use in Jalview2XML
+   * 
+   * @param vobj2jv2
+   * @return hashtable with string values
+   */
+  private Hashtable mapKeysToString(Hashtable vobj2jv2)
+  {
+    Hashtable mapped = new Hashtable();
+    Iterator keys = vobj2jv2.keySet().iterator();
+    while (keys.hasNext())
+    {
+      Object key = keys.next();
+      mapped.put(key.toString(), vobj2jv2.get(key));
+    }
+    return mapped;
+  }
+
+  /**
+   * synchronize Jalview from the vamsas document
+   * 
+   * @return number of new views from document
+   */
+  public int updateToJalview()
   {
     VAMSAS _roots[] = cdoc.getVamsasRoots();
 
@@ -1008,25 +1595,12 @@ public class VamsasAppDatastore
         for (i = 0; i < iSize; i++)
         {
           Sequence vdseq = dataset.getSequence(i);
-          jalview.datamodel.SequenceI dsseq = (SequenceI) getvObj2jv(vdseq);
-          if (dsseq != null)
-          {
-            if (!dsseq.getSequenceAsString().equals(vdseq.getSequence()))
-            {
-              throw new Error(
-                      "Broken! - mismatch of dataset sequence: and jalview internal dataset sequence.");
-            }
-            jremain--;
-          }
-          else
+          jalview.io.vamsas.Datasetsequence dssync = new Datasetsequence(
+                  this, vdseq);
+
+          jalview.datamodel.SequenceI dsseq = (SequenceI) dssync.getJvobj();
+          if (dssync.isAddfromdoc())
           {
-            dsseq = new jalview.datamodel.Sequence(dataset.getSequence(i)
-                    .getName(), dataset.getSequence(i).getSequence(),
-                    (int) dataset.getSequence(i).getStart(), (int) dataset
-                            .getSequence(i).getEnd());
-            dsseq.setDescription(dataset.getSequence(i).getDescription());
-            bindjvvobj(dsseq, dataset.getSequence(i));
-            dsseq.setVamsasId(dataset.getSequence(i).getVorbaId().getId());
             dsseqs.add(dsseq);
           }
           if (vdseq.getDbRefCount() > 0)
@@ -1037,6 +1611,7 @@ public class VamsasAppDatastore
               new jalview.io.vamsas.Dbref(this, dbref[db], vdseq, dsseq);
 
             }
+            dsseq.updatePDBIds();
           }
         }
 
@@ -1064,7 +1639,7 @@ public class VamsasAppDatastore
             {
               SequenceI dsSeq = (SequenceI) getvObj2jv((Vobject) dseta
                       .getSeqRef(0)); // TODO: deal with group dataset
-                                      // annotations
+              // annotations
               if (dsSeq == null)
               {
                 jalview.bin.Cache.log
@@ -1077,13 +1652,8 @@ public class VamsasAppDatastore
               {
                 if (dseta.getAnnotationElementCount() == 0)
                 {
-                  jalview.datamodel.SequenceFeature sf = (jalview.datamodel.SequenceFeature) getvObj2jv(dseta);
-                  if (sf == null)
-                  {
-                    dsSeq
-                            .addSequenceFeature(sf = getJalviewSeqFeature(dseta));
-                    bindjvvobj(sf, dseta);
-                  }
+                  new jalview.io.vamsas.Sequencefeature(this, dseta, dsSeq);
+
                 }
                 else
                 {
@@ -1097,6 +1667,11 @@ public class VamsasAppDatastore
                 }
               }
             }
+            else
+            {
+              Cache.log
+                      .warn("Ignoring multiply referenced dataset sequence annotation for binding to datsaet sequence features.");
+            }
           }
         }
         if (dataset.getAlignmentCount() > 0)
@@ -1107,17 +1682,23 @@ public class VamsasAppDatastore
           {
             uk.ac.vamsas.objects.core.Alignment alignment = dataset
                     .getAlignment(al);
-            AlignViewport av = (AlignViewport) getvObj2jv(alignment);
+            // TODO check this handles multiple views properly
+            AlignViewport av = findViewport(alignment);
+
             jalview.datamodel.AlignmentI jal = null;
             if (av != null)
             {
-              jal = av.getAlignment();
+              // TODO check that correct alignment object is retrieved when
+              // hidden seqs exist.
+              jal = (av.hasHiddenRows()) ? av.getAlignment()
+                      .getHiddenSequences().getFullAlignment() : av
+                      .getAlignment();
             }
             iSize = alignment.getAlignmentSequenceCount();
-            boolean newal = (jal == null) ? true : false;
+            boolean refreshal = false;
             Vector newasAnnots = new Vector();
             char gapChar = ' '; // default for new alignments read in from the
-                                // document
+            // document
             if (jal != null)
             {
               dsseqs = jal.getSequences(); // for merge/update
@@ -1131,51 +1712,14 @@ public class VamsasAppDatastore
             for (i = 0; i < iSize; i++)
             {
               AlignmentSequence valseq = alignment.getAlignmentSequence(i);
-              jalview.datamodel.SequenceI alseq = (SequenceI) getvObj2jv(valseq);
-              if (alseq != null)
-              {
-                // TODO: upperCase/LowerCase situation here ? do we allow it ?
-                // if (!alseq.getSequence().equals(valseq.getSequence())) {
-                // throw new Error("Broken! - mismatch of dataset sequence and
-                // jalview internal dataset sequence.");
-                if (Cache.log.isDebugEnabled())
-                {
-                  Cache.log.debug("Updating apparently edited sequence "
-                          + alseq.getName());
-                }
-                // this might go *horribly* wrong
-                alseq.setSequence(new String(valseq.getSequence()).replace(
-                        valGapchar, gapChar));
-                jremain--;
-              }
-              else
+              jalview.datamodel.Sequence alseq = (jalview.datamodel.Sequence) getvObj2jv(valseq);
+              if (syncFromAlignmentSequence(valseq, valGapchar, gapChar,
+                      dsseqs) && alseq != null)
               {
-                alseq = new jalview.datamodel.Sequence(valseq.getName(),
-                        valseq.getSequence().replace(valGapchar, gapChar),
-                        (int) valseq.getStart(), (int) valseq.getEnd());
 
-                Vobject datsetseq = (Vobject) valseq.getRefid();
-                if (datsetseq != null)
-                {
-                  alseq
-                          .setDatasetSequence((SequenceI) getvObj2jv(datsetseq)); // exceptions
-                                                                                  // if
-                                                                                  // AlignemntSequence
-                                                                                  // reference
-                                                                                  // isn't
-                                                                                  // a
-                                                                                  // simple
-                                                                                  // SequenceI
-                }
-                else
-                {
-                  Cache.log
-                          .error("Invalid dataset sequence id (null) for alignment sequence "
-                                  + valseq.getVorbaId());
-                }
-                bindjvvobj(alseq, valseq);
-                alseq.setVamsasId(valseq.getVorbaId().getId());
-                dsseqs.add(alseq);
+                // updated to sequence from the document
+                jremain--;
+                refreshal = true;
               }
               if (valseq.getAlignmentSequenceAnnotationCount() > 0)
               {
@@ -1184,38 +1728,39 @@ public class VamsasAppDatastore
                 for (int a = 0; a < vasannot.length; a++)
                 {
                   jalview.datamodel.AlignmentAnnotation asa = (jalview.datamodel.AlignmentAnnotation) getvObj2jv(vasannot[a]); // TODO:
-                                                                                                                                // 1:many
-                                                                                                                                // jalview
-                                                                                                                                // alignment
-                                                                                                                                // sequence
-                                                                                                                                // annotations
+                  // 1:many
+                  // jalview
+                  // alignment
+                  // sequence
+                  // annotations
                   if (asa == null)
                   {
                     int se[] = getBounds(vasannot[a]);
                     asa = getjAlignmentAnnotation(jal, vasannot[a]);
                     asa.setSequenceRef(alseq);
                     asa.createSequenceMapping(alseq, se[0], false); // TODO:
-                                                                    // verify
-                                                                    // that
-                                                                    // positions
-                                                                    // in
-                                                                    // alseqAnnotation
-                                                                    // correspond
-                                                                    // to
-                                                                    // ungapped
-                                                                    // residue
-                                                                    // positions.
+                    // verify
+                    // that
+                    // positions
+                    // in
+                    // alseqAnnotation
+                    // correspond
+                    // to
+                    // ungapped
+                    // residue
+                    // positions.
                     alseq.addAlignmentAnnotation(asa);
                     bindjvvobj(asa, vasannot[a]);
+                    refreshal = true;
                     newasAnnots.add(asa);
                   }
                   else
                   {
                     // update existing annotation - can do this in place
                     if (vasannot[a].getModifiable() == null) // TODO: USE
-                                                              // VAMSAS LIBRARY
-                                                              // OBJECT LOCK
-                                                              // METHODS)
+                    // VAMSAS LIBRARY
+                    // OBJECT LOCK
+                    // METHODS)
                     {
                       Cache.log
                               .info("UNIMPLEMENTED: not recovering user modifiable sequence alignment annotation");
@@ -1281,8 +1826,8 @@ public class VamsasAppDatastore
                   Cache.log
                           .debug("update from vamsas alignment annotation to existing jalview alignment annotation.");
                   if (an[j].getModifiable() == null) // TODO: USE VAMSAS
-                                                      // LIBRARY OBJECT LOCK
-                                                      // METHODS)
+                  // LIBRARY OBJECT LOCK
+                  // METHODS)
                   {
                     // TODO: user defined annotation is totally mutable... - so
                     // load it up or throw away if locally edited.
@@ -1295,8 +1840,11 @@ public class VamsasAppDatastore
                 else
                 {
                   jan = getjAlignmentAnnotation(jal, an[j]);
+                  // TODO: ensure we add the alignment annotation before the
+                  // automatic annotation rows
                   jal.addAnnotation(jan);
                   bindjvvobj(jan, an[j]);
+                  refreshal = true;
                 }
               }
             }
@@ -1308,17 +1856,20 @@ public class VamsasAppDatastore
               // ///////////////////////////////
               // construct alignment view
               alignFrame = new AlignFrame(jal, AlignFrame.DEFAULT_WIDTH,
-                      AlignFrame.DEFAULT_HEIGHT);
+                      AlignFrame.DEFAULT_HEIGHT, alignment.getVorbaId()
+                              .toString());
               av = alignFrame.getViewport();
-              String title = alignment.getProvenance().getEntry(
-                      alignment.getProvenance().getEntryCount() - 1)
+              newAlignmentViews.addElement(av);
+              String title = alignment
+                      .getProvenance()
+                      .getEntry(
+                              alignment.getProvenance().getEntryCount() - 1)
                       .getAction();
               if (alignment.getPropertyCount() > 0)
               {
                 for (int p = 0, pe = alignment.getPropertyCount(); p < pe; p++)
                 {
-                  if (alignment.getProperty(p).getName().equals(
-                          "title"))
+                  if (alignment.getProperty(p).getName().equals("title"))
                   {
                     title = alignment.getProperty(p).getContent();
                   }
@@ -1328,21 +1879,25 @@ public class VamsasAppDatastore
               // alignment using its provenance.
               if (Cache.log.isDebugEnabled())
               {
-                title = title + "("
-                 + alignment.getVorbaId() + ")";
-                
+                title = title + "(" + alignment.getVorbaId() + ")";
+
               }
-              jalview.gui.Desktop.addInternalFrame(alignFrame, title 
-                      ,
+              jalview.gui.Desktop.addInternalFrame(alignFrame, title,
                       AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
-              bindjvvobj(av, alignment);
+              bindjvvobj(av.getSequenceSetId(), alignment);
             }
             else
             {
               // find the alignFrame for jal.
               // TODO: fix this so we retrieve the alignFrame handing av
-              // *directly*
-              alignFrame = getAlignFrameFor(av);
+              // *directly* (JBPNote - don't understand this now)
+              // TODO: make sure all associated views are refreshed
+              alignFrame = Desktop.getAlignFrameFor(av);
+              if (refreshal)
+              {
+                av.alignmentChanged(alignFrame.alignPanel);
+                alignFrame.alignPanel.adjustAnnotationHeight();
+              }
             }
             // LOAD TREES
             // /////////////////////////////////////
@@ -1355,17 +1910,28 @@ public class VamsasAppDatastore
                         this, alignFrame, alignment.getTree(t));
                 TreePanel tp = null;
                 if (vstree.isValidTree())
-                { 
-                  tp = alignFrame
-                        .ShowNewickTree(vstree.getNewickTree(), vstree
-                                .getTitle(), vstree.getInputData(), 600,
-                                500, t * 20 + 50, t * 20 + 50);
+                {
+                  tp = alignFrame.ShowNewickTree(vstree.getNewickTree(),
+                          vstree.getTitle(), vstree.getInputData(), 600,
+                          500, t * 20 + 50, t * 20 + 50);
+
                 }
-                if (tp!=null)
+                if (tp != null)
+                {
                   bindjvvobj(tp, alignment.getTree(t));
+                  try
+                  {
+                    vstree.UpdateSequenceTreeMap(tp);
+                  } catch (RuntimeException e)
+                  {
+                    Cache.log.warn("update of labels failed.", e);
+                  }
+                }
                 else
                 {
-                  Cache.log.warn("Cannot create tree for tree "+t+" in document ("+alignment.getTree(t).getVorbaId());
+                  Cache.log.warn("Cannot create tree for tree " + t
+                          + " in document ("
+                          + alignment.getTree(t).getVorbaId());
                 }
 
               }
@@ -1388,6 +1954,19 @@ public class VamsasAppDatastore
         }
       }
     }
+    return newAlignmentViews.size();
+  }
+
+  public AlignViewport findViewport(Alignment alignment)
+  {
+    AlignViewport av = null;
+    AlignViewport[] avs = Desktop
+            .getViewports((String) getvObj2jv(alignment));
+    if (avs != null)
+    {
+      av = avs[0];
+    }
+    return av;
   }
 
   // bitfields - should be a template in j1.5
@@ -1431,7 +2010,7 @@ public class VamsasAppDatastore
     { new jalview.datamodel.Annotation[rangeMap.length],
         new jalview.datamodel.Annotation[rangeMap.length] };
     boolean mergeable = true; // false if 'after positions cant be placed on
-                              // same annotation row as positions.
+    // same annotation row as positions.
 
     if (annotation.getAnnotationElementCount() > 0)
     {
@@ -1439,7 +2018,7 @@ public class VamsasAppDatastore
       for (int aa = 0; aa < ae.length; aa++)
       {
         int pos = (int) ae[aa].getPosition() - 1; // pos counts from 1 to
-                                                  // (|seg.start-seg.end|+1)
+        // (|seg.start-seg.end|+1)
         if (pos >= 0 && pos < rangeMap.length)
         {
           int row = ae[aa].getAfter() ? 1 : 0;
@@ -1464,9 +2043,9 @@ public class VamsasAppDatastore
             }
           }
           String dc = null; // ae[aa].getDisplayCharacter()==null ? "dc" :
-                            // ae[aa].getDisplayCharacter();
+          // ae[aa].getDisplayCharacter();
           String ss = null; // ae[aa].getSecondaryStructure()==null ? "ss" :
-                            // ae[aa].getSecondaryStructure();
+          // ae[aa].getSecondaryStructure();
           java.awt.Color colour = null;
           if (ae[aa].getGlyphCount() > 0)
           {
@@ -1475,16 +2054,14 @@ public class VamsasAppDatastore
             {
               if (glyphs[g]
                       .getDict()
-                      .equals(
-                              uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE))
+                      .equals(uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE))
               {
                 ss = glyphs[g].getContent();
                 AeContent[HASSECSTR] = true;
               }
               else if (glyphs[g]
                       .getDict()
-                      .equals(
-                              uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_HD_HYDRO))
+                      .equals(uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_HD_HYDRO))
               {
                 Cache.log.debug("ignoring hydrophobicity glyph marker.");
                 AeContent[HASHPHOB] = true;
@@ -1502,8 +2079,9 @@ public class VamsasAppDatastore
               }
               else
               {
-                Cache.log.debug("IMPLEMENTATION TODO: Ignoring unknown glyph type "
-                        + glyphs[g].getDict());
+                Cache.log
+                        .debug("IMPLEMENTATION TODO: Ignoring unknown glyph type "
+                                + glyphs[g].getDict());
               }
             }
           }
@@ -1515,21 +2093,21 @@ public class VamsasAppDatastore
             {
               Cache.log.warn("ignoring additional "
                       + (ae[aa].getValueCount() - 1)
-                      + "values in annotation element.");
+                      + " values in annotation element.");
             }
             val = ae[aa].getValue(0);
           }
           if (colour == null)
           {
             anot[row][pos] = new jalview.datamodel.Annotation(
-                    (dc != null) ? dc : "", desc, (ss != null) ? ss
-                            .charAt(0) : ' ', val);
+                    (dc != null) ? dc : "", desc,
+                    (ss != null) ? ss.charAt(0) : ' ', val);
           }
           else
           {
             anot[row][pos] = new jalview.datamodel.Annotation(
-                    (dc != null) ? dc : "", desc, (ss != null) ? ss
-                            .charAt(0) : ' ', val, colour);
+                    (dc != null) ? dc : "", desc,
+                    (ss != null) ? ss.charAt(0) : ' ', val, colour);
           }
         }
         else
@@ -1548,7 +2126,7 @@ public class VamsasAppDatastore
             anot[0][i] = anot[1][i];
             anot[0][i].description = anot[0][i].description + " (after)";
             AeContent[HASDESCSTR] = true; // we have valid description string
-                                          // data
+            // data
             anot[1][i] = null;
           }
         }
@@ -1600,46 +2178,48 @@ public class VamsasAppDatastore
     Object[] parsedRangeAnnotation = parseRangeAnnotation(annotation);
     String a_label = annotation.getLabel();
     String a_descr = annotation.getDescription();
-    GraphLine gl=null;
-    int type=0;
-    boolean interp=true; // cleared if annotation is DISCRETE
+    GraphLine gl = null;
+    int type = 0;
+    boolean interp = true; // cleared if annotation is DISCRETE
     // set type and other attributes from properties
     if (annotation.getPropertyCount() > 0)
     {
       // look for special jalview properties
-      uk.ac.vamsas.objects.core.Property[] props = annotation
-              .getProperty();
+      uk.ac.vamsas.objects.core.Property[] props = annotation.getProperty();
       for (int p = 0; p < props.length; p++)
       {
         if (props[p].getName().equalsIgnoreCase(DISCRETE_ANNOTATION))
         {
           type = AlignmentAnnotation.BAR_GRAPH;
-          interp=false;
-        } else if
-         (props[p].getName().equalsIgnoreCase(CONTINUOUS_ANNOTATION)) {
+          interp = false;
+        }
+        else if (props[p].getName().equalsIgnoreCase(CONTINUOUS_ANNOTATION))
+        {
           type = AlignmentAnnotation.LINE_GRAPH;
-        } else if
-        (props[p].getName().equalsIgnoreCase(THRESHOLD))
+        }
+        else if (props[p].getName().equalsIgnoreCase(THRESHOLD))
         {
-          Float val=null;
-          try {
+          Float val = null;
+          try
+          {
             val = new Float(props[p].getContent());
-          }catch (Exception e)
+          } catch (Exception e)
           {
             Cache.log.warn("Failed to parse threshold property");
           }
-          if (val!=null)
-          if (gl==null)
-          {
-            gl = new GraphLine(val.floatValue(), "", java.awt.Color.black);
-          } else
-          {
-            gl.value = val.floatValue();
-          }
+          if (val != null)
+            if (gl == null)
+            {
+              gl = new GraphLine(val.floatValue(), "", java.awt.Color.black);
+            }
+            else
+            {
+              gl.value = val.floatValue();
+            }
         }
-        else if (props[p].getName().equalsIgnoreCase(THRESHOLD+"Name"))
+        else if (props[p].getName().equalsIgnoreCase(THRESHOLD + "Name"))
         {
-          if (gl==null)
+          if (gl == null)
             gl = new GraphLine(0, "", java.awt.Color.black);
           gl.label = props[p].getContent();
         }
@@ -1676,17 +2256,21 @@ public class VamsasAppDatastore
       // way its 'odd' - there is already an existing TODO about removing this
       // flag as being redundant
       /*
-       * if
-       * ((annotation.getClass().equals(uk.ac.vamsas.objects.core.AlignmentAnnotation.class) &&
-       * ((uk.ac.vamsas.objects.core.AlignmentAnnotation)annotation).getGraph()) ||
-       * (hasSequenceRef=true &&
-       * ((uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation)annotation).getGraph())) {
+       * if((annotation.getClass().equals(uk.ac.vamsas.objects.core.
+       * AlignmentAnnotation.class) &&
+       * ((uk.ac.vamsas.objects.core.AlignmentAnnotation)annotation).getGraph())
+       * || (hasSequenceRef=true &&
+       * ((uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation
+       * )annotation).getGraph())) {
        */
       if (has[HASVALS])
       {
-        if (type==0)
+        if (type == 0)
         {
-          type = jalview.datamodel.AlignmentAnnotation.BAR_GRAPH; // default type of value annotation
+          type = jalview.datamodel.AlignmentAnnotation.BAR_GRAPH; // default
+          // type of
+          // value
+          // annotation
           if (has[HASHPHOB])
           {
             // no hints - so we ensure HPHOB display is like this.
@@ -1746,17 +2330,19 @@ public class VamsasAppDatastore
             if (!has[HASDC])
             {
               if (!interp)
-                {
-                if (arow[i].description!=null && arow[i].description.length()<3)
+              {
+                if (arow[i].description != null
+                        && arow[i].description.length() < 3)
                 {
                   // copy over the description as the display char.
                   arow[i].displayCharacter = new String(arow[i].description);
                 }
-                } else
-                {
-                  // mark the position as a point used for the interpolation.
+              }
+              else
+              {
+                // mark the position as a point used for the interpolation.
                 arow[i].displayCharacter = arow[i].value + "";
-                }
+              }
             }
           }
         }
@@ -1765,22 +2351,28 @@ public class VamsasAppDatastore
       }
       else
       {
-        if (annotation.getAnnotationElementCount()==0)
+        if (annotation.getAnnotationElementCount() == 0)
         {
           // empty annotation array
-          // TODO: alignment 'features' compare rangeType spec to alignment width - if it is not complete, then mark regions on the annotation row.
+          // TODO: alignment 'features' compare rangeType spec to alignment
+          // width - if it is not complete, then mark regions on the annotation
+          // row.
         }
         jan = new jalview.datamodel.AlignmentAnnotation(a_label, a_descr,
                 arow);
         jan.setThreshold(null);
+        jan.annotationId = annotation.getVorbaId().toString(); // keep all the
+        // ids together.
       }
       if (annotation.getLinkCount() > 0)
       {
         Cache.log.warn("Ignoring " + annotation.getLinkCount()
                 + "links added to AlignmentAnnotation.");
       }
-      if (annotation.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT
-                                              // LOCK METHODS)
+      if (annotation.getModifiable() == null
+              || annotation.getModifiable().length() == 0) // TODO: USE VAMSAS
+      // LIBRARY OBJECT
+      // LOCK METHODS)
       {
         jan.editable = true;
       }
@@ -1789,7 +2381,13 @@ public class VamsasAppDatastore
         if (annotation.getGroup() != null
                 && annotation.getGroup().length() > 0)
         {
-          jan.graphGroup = Integer.parseInt(annotation.getGroup());
+          jan.graphGroup = Integer.parseInt(annotation.getGroup()); // TODO:
+          // group
+          // similarly
+          // named
+          // annotation
+          // together
+          // ?
         }
       } catch (Exception e)
       {
@@ -1803,23 +2401,6 @@ public class VamsasAppDatastore
     return null;
   }
 
-  private SequenceFeature getJalviewSeqFeature(RangeAnnotation dseta)
-  {
-    int[] se = getBounds(dseta);
-    SequenceFeature sf = new jalview.datamodel.SequenceFeature(dseta
-            .getType(), dseta.getDescription(), dseta.getStatus(), se[0],
-            se[1], dseta.getGroup());
-    if (dseta.getLinkCount() > 0)
-    {
-      Link[] links = dseta.getLink();
-      for (int i = 0; i < links.length; i++)
-      {
-        sf.addLink(links[i].getContent() + "|" + links[i].getHref());
-      }
-    }
-    return sf;
-  }
-
   /**
    * get real bounds of a RangeType's specification. start and end are an
    * inclusive range within which all segments and positions lie. TODO: refactor
@@ -1938,10 +2519,8 @@ public class VamsasAppDatastore
    * @param maprange
    *          where the from range is the local mapped range, and the to range
    *          is the 'mapped' range in the MapRangeType
-   * @param default
-   *          unit for local
-   * @param default
-   *          unit for mapped
+   * @param default unit for local
+   * @param default unit for mapped
    * @return MapList
    */
   private jalview.util.MapList parsemapType(MapType maprange, int localu,
@@ -2006,8 +2585,8 @@ public class VamsasAppDatastore
    * 
    * if (jprov != null) { entries = jprov.getEntries(); for (int i = 0; i <
    * entries.length; i++) { provEntry = new Entry(); try { date = new
-   * org.exolab.castor.types.Date(entries[i].getDate()); } catch (Exception ex) {
-   * ex.printStackTrace();
+   * org.exolab.castor.types.Date(entries[i].getDate()); } catch (Exception ex)
+   * { ex.printStackTrace();
    * 
    * date = new org.exolab.castor.types.Date(entries[i].getDate()); }
    * provEntry.setDate(date); provEntry.setUser(entries[i].getUser());
@@ -2067,9 +2646,11 @@ public class VamsasAppDatastore
     return prov;
   }
 
-  void addProvenance(Provenance p, String action)
+  Entry addProvenance(Provenance p, String action)
   {
-    p.addEntry(dummyPEntry(action));
+    Entry dentry = dummyPEntry(action);
+    p.addEntry(dentry);
+    return dentry;
   }
 
   public Entry getProvEntry()
@@ -2107,7 +2688,15 @@ public class VamsasAppDatastore
         Cache.log.warn("Creating new dataset for an alignment.");
         jal.setDataset(null);
       }
-      dataset = (DataSet) getjv2vObj(jal.getDataset());
+      dataset = (DataSet) ((Alignment) getjv2vObj(viewport
+              .getSequenceSetId())).getV_parent(); // jal.getDataset());
+      if (dataset == null)
+      {
+        dataset = (DataSet) getjv2vObj(jal.getDataset());
+        Cache.log
+                .error("Can't find the correct dataset for the alignment in this view. Creating new one.");
+
+      }
       // Store any sequence mappings.
       if (av.getAlignment().getCodonFrames() != null
               && av.getAlignment().getCodonFrames().length > 0)
@@ -2116,7 +2705,8 @@ public class VamsasAppDatastore
                 .getCodonFrames();
         for (int cf = 0; cf < cframes.length; cf++)
         {
-          if (cframes[cf].getdnaSeqs().length > 0)
+          if (cframes[cf].getdnaSeqs() != null
+                  && cframes[cf].getdnaSeqs().length > 0)
           {
             jalview.datamodel.SequenceI[] dmps = cframes[cf].getdnaSeqs();
             jalview.datamodel.Mapping[] mps = cframes[cf].getProtMappings();
@@ -2132,7 +2722,7 @@ public class VamsasAppDatastore
               {
                 Cache.log
                         .warn("NO Vamsas Binding for local sequence! NOT CREATING MAPPING FOR "
-                                + mfrom.getName()
+                                + dmps[smp].getDisplayId(true)
                                 + " to "
                                 + mps[smp].getTo().getName());
               }
@@ -2146,4 +2736,43 @@ public class VamsasAppDatastore
               e);
     }
   }
+
+  public void clearSkipList()
+  {
+    if (skipList != null)
+    {
+      skipList.clear();
+    }
+  }
+
+  /**
+   * @return the skipList
+   */
+  public Hashtable getSkipList()
+  {
+    return skipList;
+  }
+
+  /**
+   * @param skipList
+   *          the skipList to set
+   */
+  public void setSkipList(Hashtable skipList)
+  {
+    this.skipList = skipList;
+  }
+
+  /**
+   * registry for datastoreItems
+   */
+  DatastoreRegistry dsReg = new DatastoreRegistry();
+
+  public DatastoreRegistry getDatastoreRegisty()
+  {
+    if (dsReg == null)
+    {
+      dsReg = new DatastoreRegistry();
+    }
+    return dsReg;
+  }
 }