Jalview 2.8 Source Header
[jalview.git] / src / jalview / io / vamsas / Sequencemapping.java
index de4a01e..9ac0ae5 100644 (file)
@@ -1,20 +1,19 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
- * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
+ * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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 file is part of Jalview.
  * 
- * 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.
+ * 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 this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ * 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.vamsas;
 
@@ -23,6 +22,7 @@ import java.util.Vector;
 import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.Mapping;
 import jalview.datamodel.SequenceI;
+import jalview.gui.Desktop;
 import jalview.io.VamsasAppDatastore;
 import uk.ac.vamsas.client.Vobject;
 import uk.ac.vamsas.objects.core.AlignmentSequence;
@@ -34,26 +34,32 @@ import uk.ac.vamsas.objects.core.Sequence;
 import uk.ac.vamsas.objects.core.SequenceMapping;
 import uk.ac.vamsas.objects.core.SequenceType;
 
+/**
+ * binds a vamsas sequence mapping object from the vamsas document to a maplist
+ * object associated with a mapping in the Jalview model. We use the maplist
+ * object because these are referred to both in the Mapping object associated
+ * with a jalview.datamodel.DBRefEntry and in the array of
+ * jalview.datamodel.AlCodonFrame objects that Jalview uses to propagate
+ * sequence mapping position highlighting across the views.
+ * 
+ * @author JimP
+ * 
+ */
 public class Sequencemapping extends Rangetype
 {
   public Sequencemapping(VamsasAppDatastore datastore,
           SequenceMapping sequenceMapping)
   {
-    super(datastore);
-    Object mjvmapping = getvObj2jv(sequenceMapping);
-    if (mjvmapping == null)
-    {
-      add(sequenceMapping);
-    }
-    else
-    {
-      if (sequenceMapping.isUpdated())
-      {
-        update((jalview.util.MapList) mjvmapping, sequenceMapping);
-      }
-    }
+    super(datastore, sequenceMapping, jalview.util.MapList.class);
+    doJvUpdate();
   }
 
+  private SequenceType from;
+
+  private DataSet ds;
+
+  private Mapping mjvmapping;
+
   /**
    * create or update a vamsas sequence mapping corresponding to a jalview
    * Mapping between two dataset sequences
@@ -68,36 +74,64 @@ public class Sequencemapping extends Rangetype
           uk.ac.vamsas.objects.core.SequenceType from,
           uk.ac.vamsas.objects.core.DataSet ds)
   {
-    super(datastore);
-    SequenceMapping sequenceMapping = (SequenceMapping) getjv2vObj(mjvmapping);
+    super(datastore, mjvmapping.getMap(), SequenceMapping.class);
+    this.from = from;
+    this.ds = ds;
+    this.mjvmapping = mjvmapping;
+    validate();
+    doSync();
+  }
+
+  /**
+   * local check that extant mapping context is valid
+   */
+  public void validate()
+  {
+
+    SequenceMapping sequenceMapping = (SequenceMapping) vobj;
     if (sequenceMapping == null)
     {
-      add(mjvmapping, from, ds);
+      return;
     }
-    else
+    if (from != null && sequenceMapping.getLoc() != from)
     {
-      if (from != null && sequenceMapping.getLoc() != from)
-      {
-        jalview.bin.Cache.log.warn("Probable IMPLEMENTATION ERROR: " + from
-                + " doesn't match the local mapping sequence.");
-      }
-      if (ds != null && sequenceMapping.is__stored_in_document()
-              && sequenceMapping.getV_parent() != ds)
-      {
-        jalview.bin.Cache.log
-                .warn("Probable IMPLEMENTATION ERROR: "
-                        + ds
-                        + " doesn't match the parent of the bound sequence mapping object.");
-      }
-      if (sequenceMapping.isUpdated())
-      {
-        conflict(mjvmapping, sequenceMapping);
-      }
-      else
-      {
-        update(mjvmapping, sequenceMapping);
-      }
+      jalview.bin.Cache.log.warn("Probable IMPLEMENTATION ERROR: " + from
+              + " doesn't match the local mapping sequence.");
     }
+    if (ds != null && sequenceMapping.is__stored_in_document()
+            && sequenceMapping.getV_parent() != ds)
+    {
+      jalview.bin.Cache.log
+              .warn("Probable IMPLEMENTATION ERROR: "
+                      + ds
+                      + " doesn't match the parent of the bound sequence mapping object.");
+    }
+  }
+
+  public void addToDocument()
+  {
+    add(mjvmapping, from, ds);
+  }
+
+  public void addFromDocument()
+  {
+    add((SequenceMapping) vobj);
+  }
+
+  public void conflict()
+  {
+    conflict(mjvmapping, (SequenceMapping) vobj);
+
+  }
+
+  public void updateToDoc()
+  {
+    update(mjvmapping, (SequenceMapping) vobj);
+  }
+
+  public void updateFromDoc()
+  {
+    update((SequenceMapping) vobj, (jalview.datamodel.Mapping) jvobj);
   }
 
   private void conflict(Mapping mjvmapping, SequenceMapping sequenceMapping)
@@ -118,7 +152,7 @@ public class Sequencemapping extends Rangetype
     if (to == null)
     {
       jalview.bin.Cache.log
-              .warn("NONFATAL - do a second update: Ignoring Forward Reference to seuqence not yet bound to vamsas seuqence object");
+              .warn("FIXME NONFATAL - do a second update: Ignoring Forward Reference to seuqence not yet bound to vamsas seuqence object");
       return;
     }
     SequenceMapping sequenceMapping = new SequenceMapping();
@@ -186,19 +220,26 @@ public class Sequencemapping extends Rangetype
     // source
     // of
     // mapping
-    bindjvvobj(mjvmapping, sequenceMapping);
+    bindjvvobj(mjvmapping.getMap(), sequenceMapping);
 
     jalview.bin.Cache.log.debug("Successfully created mapping "
             + sequenceMapping.getVorbaId());
   }
 
-  private void update(jalview.util.MapList mjvmapping,
-          SequenceMapping sequenceMapping)
+  // private void update(jalview.util.MapList mjvmapping,
+  // SequenceMapping sequenceMapping)
   {
     jalview.bin.Cache.log
             .error("Not implemented: Jalview Update Alcodon Mapping:TODO!");
   }
 
+  private void update(SequenceMapping sequenceMapping,
+          jalview.datamodel.Mapping mjvmapping)
+  {
+    jalview.bin.Cache.log
+            .error("Not implemented: Update DBRef Mapping from Jalview");
+  }
+
   private void update(jalview.datamodel.Mapping mjvmapping,
           SequenceMapping sequenceMapping)
   {
@@ -207,8 +248,9 @@ public class Sequencemapping extends Rangetype
   }
 
   /**
-   * limitations: Currently, jalview only deals with mappings between dataset
-   * sequences, and even then, only between those that map from DNA to Protein.
+   * bind a SequenceMapping to a live AlCodonFrame element limitations:
+   * Currently, jalview only deals with mappings between dataset sequences, and
+   * even then, only between those that map from DNA to Protein.
    * 
    * @param sequenceMapping
    */
@@ -300,22 +342,30 @@ public class Sequencemapping extends Rangetype
     // create and add the new mapping to (each) dataset's codonFrame
 
     jalview.util.MapList mapping = null;
-    if (!sense)
+    if (dnaToProt)
     {
-      mapping = this.parsemapType(sequenceMapping, 1, 3); // invert sense
-      mapping = new jalview.util.MapList(mapping.getToRanges(), mapping
-              .getFromRanges(), mapping.getToRatio(), mapping
-              .getFromRatio());
-      afc.addMap(to, from, mapping);
+      if (!sense)
+      {
+        mapping = this.parsemapType(sequenceMapping, 1, 3); // invert sense
+        mapping = new jalview.util.MapList(mapping.getToRanges(),
+                mapping.getFromRanges(), mapping.getToRatio(),
+                mapping.getFromRatio());
+        afc.addMap(to, from, mapping);
+      }
+      else
+      {
+        mapping = this.parsemapType(sequenceMapping, 3, 1); // correct sense
+        afc.addMap(from, to, mapping);
+      }
     }
     else
     {
-      mapping = this.parsemapType(sequenceMapping, 3, 1); // correct sense
+      mapping = this.parsemapType(sequenceMapping, 1, 1); // correct sense
       afc.addMap(from, to, mapping);
     }
     bindjvvobj(mapping, sequenceMapping);
     jalview.structure.StructureSelectionManager
-            .getStructureSelectionManager().addMappings(
+            .getStructureSelectionManager(Desktop.instance).addMappings(
                     new AlignedCodonFrame[]
                     { afc });
     // Try to link up any conjugate database references in the two sequences
@@ -329,12 +379,11 @@ public class Sequencemapping extends Rangetype
    * associated with conjugate DBRefEntry under given mapping
    * 
    * @param from
-   *                sequence corresponding to from reference for sequence
-   *                mapping
+   *          sequence corresponding to from reference for sequence mapping
    * @param to
-   *                sequence correspondeing to to reference for sequence mapping
+   *          sequence correspondeing to to reference for sequence mapping
    * @param smap
-   *                maplist parsed in same sense as from and to
+   *          maplist parsed in same sense as from and to
    */
   private void matchConjugateDBRefs(SequenceI from, SequenceI to,
           jalview.util.MapList smap)