JAL-968 incremental - PNG/EPS output has annotation overlapping alignment when annota...
[jalview.git] / src / jalview / io / VamsasAppDatastore.java
index 108bcb7..735f415 100644 (file)
@@ -1,18 +1,18 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
- * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
- * 
+ * 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 
+ * 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 
+ *
+ * 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;
@@ -20,11 +20,7 @@ package jalview.io;
 import jalview.bin.Cache;
 import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.AlignmentAnnotation;
-import jalview.datamodel.AlignmentI;
-import jalview.datamodel.AlignmentView;
-import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.GraphLine;
-import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
 import jalview.gui.AlignViewport;
@@ -34,14 +30,13 @@ 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.List;
 import java.util.Vector;
 import java.util.jar.JarInputStream;
 import java.util.jar.JarOutputStream;
@@ -51,14 +46,14 @@ import uk.ac.vamsas.objects.core.*;
 import uk.ac.vamsas.objects.utils.Properties;
 
 /*
- * 
+ *
  * static {
  * org.exolab.castor.util.LocalConfiguration.getInstance().getProperties().setProperty(
  * "org.exolab.castor.serializer", "org.apache.xml.serialize.XMLSerilazizer"); }
- * 
+ *
  */
 /*
- * TODO: check/verify consistency for vamsas sync with group associated alignment annotation  
+ * TODO: check/verify consistency for vamsas sync with group associated alignment annotation
  */
 public class VamsasAppDatastore
 {
@@ -183,7 +178,7 @@ public class VamsasAppDatastore
   }
 
   /**
-   * 
+   *
    * @param vobj
    * @return Jalview datamodel object bound to the vamsas document object
    */
@@ -250,7 +245,7 @@ public class VamsasAppDatastore
 
   /**
    * put the alignment viewed by AlignViewport into cdoc.
-   * 
+   *
    * @param av
    *          alignViewport to be stored
    * @param aFtitle
@@ -424,8 +419,8 @@ public class VamsasAppDatastore
         {
           boolean modified = false;
           // check existing sequences in local and in document.
-          Vector docseqs = new Vector(alignment
-                  .getAlignmentSequenceAsReference());
+          Vector docseqs = new Vector(
+                  alignment.getAlignmentSequenceAsReference());
           for (int i = 0; i < jal.getHeight(); i++)
           {
             modified |= syncToAlignmentSequence(jal.getSequenceAt(i),
@@ -491,14 +486,14 @@ public class VamsasAppDatastore
           /*
            * We do not put local Alignment Sequence Features into the vamsas
            * document yet.
-           * 
-           * 
+           *
+           *
            * 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) {
-           * 
+           *
            * valseqf = (AlignmentSequenceAnnotation) getDSAnnotationFromJalview(
            * new AlignmentSequenceAnnotation(), features[i]);
            * valseqf.setGraph(false);
@@ -546,12 +541,7 @@ public class VamsasAppDatastore
             else
             {
               // first find the alignment sequence to associate this with.
-              SequenceI jvalsq = null;
-              Enumeration jval = av.getAlignment().getSequences()
-                      .elements();
-              while (jval.hasMoreElements())
-              {
-                jvalsq = (SequenceI) jval.nextElement();
+              for (SequenceI jvalsq:av.getAlignment().getSequences()) {
                 // saveDatasetSequenceAnnotation(AlSeqMaps,(uk.ac.vamsas.objects.core.Sequence)
                 // sref, aa[i]);
                 if (jvalsq.getDatasetSequence() == aa[i].sequenceRef)
@@ -633,11 +623,9 @@ public class VamsasAppDatastore
                 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);
@@ -657,10 +645,8 @@ public class VamsasAppDatastore
                 an.setGroup(Integer.toString(aa[i].graphGroup));
                 if (aa[i].threshold != null && aa[i].threshold.displayed)
                 {
-                  an
-                          .addProperty(Properties.newProperty(THRESHOLD,
-                                  Properties.FLOATTYPE, ""
-                                          + aa[i].threshold.value));
+                  an.addProperty(Properties.newProperty(THRESHOLD,
+                          Properties.FLOATTYPE, "" + aa[i].threshold.value));
                   if (aa[i].threshold.label != null)
                   {
                     an.addProperty(Properties.newProperty(THRESHOLD
@@ -708,8 +694,8 @@ public class VamsasAppDatastore
           {
             TreePanel tp = (TreePanel) frames[t];
 
-            if (tp.getViewPort().getSequenceSetId().equals(
-                    av.getSequenceSetId()))
+            if (tp.getViewPort().getSequenceSetId()
+                    .equals(av.getSequenceSetId()))
             {
               DatastoreItem vtree = new jalview.io.vamsas.Tree(this, tp,
                       jal, alignment);
@@ -733,7 +719,7 @@ public class VamsasAppDatastore
    * 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.
@@ -755,7 +741,7 @@ public class VamsasAppDatastore
   /**
    * remove docseqs from the given alignment marking provenance appropriately
    * and removing any references to the sequences.
-   * 
+   *
    * @param alignment
    * @param docseqs
    */
@@ -823,7 +809,7 @@ public class VamsasAppDatastore
    * 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)
@@ -888,7 +874,7 @@ public class VamsasAppDatastore
    * creates/syncs the jvalsq from the alignment sequence
    */
   private boolean syncFromAlignmentSequence(AlignmentSequence valseq,
-          char valGapchar, char gapChar, Vector dsseqs)
+          char valGapchar, char gapChar, List<SequenceI> dsseqs)
 
   {
     boolean modal = false;
@@ -936,8 +922,8 @@ public class VamsasAppDatastore
     else
     {
       alseq = new jalview.datamodel.Sequence(valseq.getName(), valseq
-              .getSequence().replace(valGapchar, gapChar), (int) valseq
-              .getStart(), (int) valseq.getEnd());
+              .getSequence().replace(valGapchar, gapChar),
+              (int) valseq.getStart(), (int) valseq.getEnd());
 
       Vobject datsetseq = (Vobject) valseq.getRefid();
       if (datsetseq != null)
@@ -1037,8 +1023,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);
@@ -1173,7 +1158,7 @@ public class VamsasAppDatastore
 
   /**
    * set vamsas annotation object type from jalview annotation
-   * 
+   *
    * @param an
    * @param alan
    */
@@ -1221,7 +1206,7 @@ public class VamsasAppDatastore
   /**
    * get start<end range of segment, adjusting for inclusivity flag and
    * polarity.
-   * 
+   *
    * @param visSeg
    * @param ensureDirection
    *          when true - always ensure start is less than end.
@@ -1248,7 +1233,7 @@ public class VamsasAppDatastore
   }
 
   /**
-   * 
+   *
    * @param annotation
    * @return true if annotation is not to be stored in document
    */
@@ -1265,7 +1250,7 @@ public class VamsasAppDatastore
   /**
    * list of alignment views created when updating Jalview from document.
    */
-  private Vector newAlignmentViews = new Vector();
+  private final Vector newAlignmentViews = new Vector();
 
   /**
    * update local jalview view settings from the stored appdata (if any)
@@ -1297,6 +1282,7 @@ public class VamsasAppDatastore
             jalview.util.jarInputStreamProvider jprovider = new jalview.util.jarInputStreamProvider()
             {
 
+              @Override
               public String getFilename()
               {
 
@@ -1304,6 +1290,7 @@ public class VamsasAppDatastore
                 return "Jalview Vamsas Document Client Data";
               }
 
+              @Override
               public JarInputStream getJarInputStream() throws IOException
               {
                 jalview.bin.Cache.log
@@ -1343,6 +1330,7 @@ public class VamsasAppDatastore
           jalview.util.jarInputStreamProvider jarstream = new jalview.util.jarInputStreamProvider()
           {
 
+            @Override
             public String getFilename()
             {
 
@@ -1350,6 +1338,7 @@ public class VamsasAppDatastore
               return "Jalview Vamsas Document User Data";
             }
 
+            @Override
             public JarInputStream getJarInputStream() throws IOException
             {
               jalview.bin.Cache.log
@@ -1448,7 +1437,7 @@ public class VamsasAppDatastore
           if (mappings != null && mappings.length > 0)
           {
             jalview.structure.StructureSelectionManager
-                    .getStructureSelectionManager().addMappings(mappings);
+                    .getStructureSelectionManager(Desktop.instance).addMappings(mappings);
           }
         }
       }
@@ -1467,7 +1456,7 @@ public class VamsasAppDatastore
   /**
    * replaces oldjvobject with newjvobject in the Jalview Object <> VorbaID
    * binding tables
-   * 
+   *
    * @param oldjvobject
    * @param newjvobject
    *          (may be null)
@@ -1477,6 +1466,7 @@ public class VamsasAppDatastore
     Object vobject = jv2vobj.remove(oldjvobject);
     if (vobject == null)
     {
+      // NOTE: this happens if user deletes object in one session then updates from another client
       throw new Error(
               "IMPLEMENTATION ERROR: old jalview object is not bound ! ("
                       + oldjvobject + ")");
@@ -1512,8 +1502,7 @@ public class VamsasAppDatastore
       {
         // 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.",
+                .error("Couldn't update jalview client application data. Giving up - local settings probably lost.",
                         e);
       }
     }
@@ -1526,7 +1515,7 @@ public class VamsasAppDatastore
 
   /**
    * translate the Vobject keys to strings for use in Jalview2XML
-   * 
+   *
    * @param jv2vobj2
    * @return
    */
@@ -1544,7 +1533,7 @@ public class VamsasAppDatastore
 
   /**
    * translate the Vobject values to strings for use in Jalview2XML
-   * 
+   *
    * @param vobj2jv2
    * @return hashtable with string values
    */
@@ -1562,7 +1551,7 @@ public class VamsasAppDatastore
 
   /**
    * synchronize Jalview from the vamsas document
-   * 
+   *
    * @return number of new views from document
    */
   public int updateToJalview()
@@ -1579,7 +1568,7 @@ public class VamsasAppDatastore
         // ///LOAD DATASET
         DataSet dataset = root.getDataSet(_ds);
         int i, iSize = dataset.getSequenceCount();
-        Vector dsseqs;
+        List<SequenceI> dsseqs;
         jalview.datamodel.Alignment jdataset = (jalview.datamodel.Alignment) getvObj2jv(dataset);
         int jremain = 0;
         if (jdataset == null)
@@ -1626,8 +1615,8 @@ public class VamsasAppDatastore
           SequenceI[] seqs = new SequenceI[dsseqs.size()];
           for (i = 0, iSize = dsseqs.size(); i < iSize; i++)
           {
-            seqs[i] = (SequenceI) dsseqs.elementAt(i);
-            dsseqs.setElementAt(null, i);
+            seqs[i] = dsseqs.get(i);
+            dsseqs.set(i, null);
           }
           jdataset = new jalview.datamodel.Alignment(seqs);
           Cache.log.debug("New vamsas dataset imported into jalview.");
@@ -1720,8 +1709,7 @@ public class VamsasAppDatastore
               AlignmentSequence valseq = alignment.getAlignmentSequence(i);
               jalview.datamodel.Sequence alseq = (jalview.datamodel.Sequence) getvObj2jv(valseq);
               if (syncFromAlignmentSequence(valseq, valGapchar, gapChar,
-                      dsseqs)
-                      && alseq != null)
+                      dsseqs) && alseq != null)
               {
 
                 // updated to sequence from the document
@@ -1789,8 +1777,8 @@ public class VamsasAppDatastore
               SequenceI[] seqs = new SequenceI[dsseqs.size()];
               for (i = 0, iSize = dsseqs.size(); i < iSize; i++)
               {
-                seqs[i] = (SequenceI) dsseqs.elementAt(i);
-                dsseqs.setElementAt(null, i);
+                seqs[i] = dsseqs.get(i);
+                dsseqs.set(i,null);
               }
               jal = new jalview.datamodel.Alignment(seqs);
               Cache.log.debug("New vamsas alignment imported into jalview "
@@ -1867,8 +1855,10 @@ public class VamsasAppDatastore
                               .toString());
               av = alignFrame.getViewport();
               newAlignmentViews.addElement(av);
-              String title = alignment.getProvenance().getEntry(
-                      alignment.getProvenance().getEntryCount() - 1)
+              String title = alignment
+                      .getProvenance()
+                      .getEntry(
+                              alignment.getProvenance().getEntryCount() - 1)
                       .getAction();
               if (alignment.getPropertyCount() > 0)
               {
@@ -1992,7 +1982,7 @@ public class VamsasAppDatastore
    * jalview.datamodel.Annotation[] rows Two annotation rows are made if there
    * are distinct annotation for both at 'pos' and 'after pos' at any particular
    * site.
-   * 
+   *
    * @param annotation
    * @return { boolean[static int constants ], int[ae.length] - map to annotated
    *         object frame, jalview.datamodel.Annotation[],
@@ -2059,16 +2049,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;
@@ -2107,14 +2095,14 @@ public class VamsasAppDatastore
           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
@@ -2412,7 +2400,7 @@ public class VamsasAppDatastore
    * get real bounds of a RangeType's specification. start and end are an
    * inclusive range within which all segments and positions lie. TODO: refactor
    * to vamsas utils
-   * 
+   *
    * @param dseta
    * @return int[] { start, end}
    */
@@ -2470,7 +2458,7 @@ public class VamsasAppDatastore
   /**
    * map from a rangeType's internal frame to the referenced object's coordinate
    * frame.
-   * 
+   *
    * @param dseta
    * @return int [] { ref(pos)...} for all pos in rangeType's frame.
    */
@@ -2522,7 +2510,7 @@ 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
@@ -2547,7 +2535,7 @@ public class VamsasAppDatastore
 
   /**
    * initialise a range type object from a set of start/end inclusive intervals
-   * 
+   *
    * @param mrt
    * @param range
    */
@@ -2564,7 +2552,7 @@ public class VamsasAppDatastore
 
   /**
    * initialise a MapType object from a MapList object.
-   * 
+   *
    * @param maprange
    * @param ml
    * @param setUnits
@@ -2589,12 +2577,12 @@ public class VamsasAppDatastore
    * App and Action here. Provenance prov = new Provenance();
    * org.exolab.castor.types.Date date = new org.exolab.castor.types.Date( new
    * java.util.Date()); Entry provEntry;
-   * 
+   *
    * 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();
-   * 
+   *
    * date = new org.exolab.castor.types.Date(entries[i].getDate()); }
    * provEntry.setDate(date); provEntry.setUser(entries[i].getUser());
    * provEntry.setAction(entries[i].getAction()); prov.addEntry(provEntry); } }
@@ -2602,7 +2590,7 @@ public class VamsasAppDatastore
    * provEntry.setUser(System.getProperty("user.name")); // TODO: ext string
    * provEntry.setApp("JVAPP"); // TODO: ext string provEntry.setAction(action);
    * prov.addEntry(provEntry); }
-   * 
+   *
    * return prov; }
    */
   jalview.datamodel.Provenance getJalviewProvenance(Provenance prov)
@@ -2620,7 +2608,7 @@ public class VamsasAppDatastore
   }
 
   /**
-   * 
+   *
    * @return default initial provenance list for a Jalview created vamsas
    *         object.
    */