JAL-4124 Dataset holds list of MatrixType instances, MapOnAMatrixType allows multiple...
[jalview.git] / src / jalview / project / Jalview2XML.java
index b9e1f46..f8be544 100644 (file)
@@ -91,12 +91,15 @@ import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
+import jalview.datamodel.ContactListI;
 import jalview.datamodel.ContactMatrix;
 import jalview.datamodel.ContactMatrixI;
 import jalview.datamodel.DBRefEntry;
+import jalview.datamodel.FloatContactMatrix;
 import jalview.datamodel.GeneLocus;
 import jalview.datamodel.GraphLine;
 import jalview.datamodel.GroupSet;
+import jalview.datamodel.GroupSetI;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.Point;
 import jalview.datamodel.RnaViewerModel;
@@ -199,6 +202,7 @@ import jalview.xml.binding.jalview.JalviewUserColours.Colour;
 import jalview.xml.binding.jalview.MapListType;
 import jalview.xml.binding.jalview.MapListType.MapListFrom;
 import jalview.xml.binding.jalview.MapListType.MapListTo;
+import jalview.xml.binding.jalview.MapOnAMatrixType;
 import jalview.xml.binding.jalview.Mapping;
 import jalview.xml.binding.jalview.MatrixType;
 import jalview.xml.binding.jalview.NoValueColour;
@@ -2432,39 +2436,47 @@ public class Jalview2XML
   {
     String cmId = contactMatrices.get(cm);
     MatrixType xmlmat=null;
-    if (cmId==null)
+    
+    // first create an xml ref for the matrix data, if none exist
+    if (cmId == null)
     {
-xmlmat = new MatrixType();
-    xmlmat.setType(cm.getType());
-    xmlmat.setRows(BigInteger.valueOf(cm.getWidth()));
-    xmlmat.setCols(BigInteger.valueOf(cm.getHeight()));
-    // consider using an opaque to/from -> allow instance to control
-    // its representation ?
-    xmlmat.setElements(ContactMatrix.contactToFloatString(cm));
-    if (cm.hasGroups())
-    {
-      for (BitSet gp : cm.getGroups())
+      xmlmat = new MatrixType();
+      xmlmat.setType(cm.getType());
+      xmlmat.setRows(BigInteger.valueOf(cm.getWidth()));
+      xmlmat.setCols(BigInteger.valueOf(cm.getHeight()));
+      // consider using an opaque to/from -> allow instance to control
+      // its representation ?
+      xmlmat.setElements(ContactMatrix.contactToFloatString(cm));
+      if (cm.hasGroups())
       {
-        xmlmat.getGroups().add(stringifyBitset(gp));
+        for (BitSet gp : cm.getGroups())
+        {
+          xmlmat.getGroups().add(stringifyBitset(gp));
+        }
       }
-    }
-    if (cm.hasTree())
-    {
-      // provenance object for tree ?
-      xmlmat.getNewick().add(cm.getNewick());
-      xmlmat.setTreeMethod(cm.getTreeMethod());
-    }
-    if (cm.hasCutHeight())
-    {
-      xmlmat.setCutHeight(cm.getCutHeight());
-    }
-      xmlmat.setId(makeHashCode(cm, cm.get));
+      if (cm.hasTree())
+      {
+        // provenance object for tree ?
+        xmlmat.getNewick().add(cm.getNewick());
+        xmlmat.setTreeMethod(cm.getTreeMethod());
+      }
+      if (cm.hasCutHeight())
+      {
+        xmlmat.setCutHeight(cm.getCutHeight());
+      }
+      xmlmat.setId(cmId = makeHashCode(cm, null));
+      contactMatrices.put(cm, cmId);
+      contactMatrixRefs.put(cmId, cm);
       root.getMatrices().add(xmlmat);
     }
-    else {
-      
-    }
-    // set/get properties
+
+    // now store mapping
+
+    MapOnAMatrixType xmlmatmapping = new MapOnAMatrixType();
+    xmlmatmapping.setMatrix(cmId);
+    
+    // Pretty much all matrices currently managed in this way are
+    // mappableContactMatrixI implementations - but check anyway
     if (cm instanceof MappableContactMatrixI)
     {
       jalview.util.MapList mlst = ((MappableContactMatrixI) cm)
@@ -2490,14 +2502,13 @@ xmlmat = new MatrixType();
           // mp.addMapListTo(mto);
           mp.getMapListTo().add(mto);
         }
-        mp.setMapFromUnit(
-                BigInteger.valueOf(mlst.getFromRatio()));
+        mp.setMapFromUnit(BigInteger.valueOf(mlst.getFromRatio()));
         mp.setMapToUnit(BigInteger.valueOf(mlst.getToRatio()));
-        xmlmat.setMapping(mp);
+        xmlmatmapping.setMapping(mp);
       }
     }
     // and add to model
-    an.getContactmatrix().add(xmlmat);    
+    an.getContactmatrix().add(xmlmatmapping);
   }
 
   private String stringifyBitset(BitSet gp)
@@ -3521,6 +3532,14 @@ xmlmat = new MatrixType();
     }
 
     // ////////////////////////////////
+    // LOAD MATRICES (IF ANY)
+    
+    if (vamsasSet.getMatrices()!=null && vamsasSet.getMatrices().size()>0)
+    {
+      importMatrixData(vamsasSet.getMatrices());
+    }
+    
+    // ////////////////////////////////
     // LOAD SEQUENCES
 
     List<SequenceI> hiddenSeqs = null;
@@ -4052,80 +4071,10 @@ xmlmat = new MatrixType();
           if (annotation.getContactmatrix() != null
                   && annotation.getContactmatrix().size() > 0)
           {
-            for (MatrixType xmlmat : annotation.getContactmatrix())
+            for (MapOnAMatrixType xmlmat : annotation.getContactmatrix())
             {
-              if (PAEContactMatrix.PAEMATRIX.equals(xmlmat.getType()))
-              {
-                if (!xmlmat.getRows().equals(xmlmat.getCols()))
-                {
-                  Console.error("Can't handle non square PAE Matrices");
-                }
-                else
-                {
-                  float[][] elements = ContactMatrix
-                          .fromFloatStringToContacts(xmlmat.getElements(),
-                                  xmlmat.getCols().intValue(),
-                                  xmlmat.getRows().intValue());
-                  jalview.util.MapList mapping = null;
-                  if (xmlmat.getMapping() != null)
-                  {
-                    MapListType m = xmlmat.getMapping();
-                    // Mapping m = dr.getMapping();
-                    int fr[] = new int[m.getMapListFrom().size() * 2];
-                    Iterator<MapListFrom> from = m.getMapListFrom()
-                            .iterator();// enumerateMapListFrom();
-                    for (int _i = 0; from.hasNext(); _i += 2)
-                    {
-                      MapListFrom mf = from.next();
-                      fr[_i] = mf.getStart();
-                      fr[_i + 1] = mf.getEnd();
-                    }
-                    int fto[] = new int[m.getMapListTo().size() * 2];
-                    Iterator<MapListTo> to = m.getMapListTo().iterator();// enumerateMapListTo();
-                    for (int _i = 0; to.hasNext(); _i += 2)
-                    {
-                      MapListTo mf = to.next();
-                      fto[_i] = mf.getStart();
-                      fto[_i + 1] = mf.getEnd();
-                    }
-
-                    mapping = new jalview.util.MapList(fr, fto,
-                            m.getMapFromUnit().intValue(),
-                            m.getMapToUnit().intValue());
-                  }
-                  List<BitSet> newgroups = new ArrayList<BitSet>();
-                  if (xmlmat.getGroups().size() > 0)
-                  {
-                    for (String sgroup : xmlmat.getGroups())
-                    {
-                      newgroups.add(deStringifyBitset(sgroup));
-                    }
-                  }
-                  String nwk = xmlmat.getNewick().size() > 0
-                          ? xmlmat.getNewick().get(0)
-                          : null;
-                  if (xmlmat.getNewick().size() > 1)
-                  {
-                    Console.log.info(
-                            "Ignoring additional clusterings for contact matrix");
-                  }
-                  String treeMethod = xmlmat.getTreeMethod();
-                  double thresh = xmlmat.getCutHeight() != null
-                          ? xmlmat.getCutHeight()
-                          : 0;
-                  GroupSet grpset = new GroupSet();
-                  grpset.restoreGroups(newgroups, treeMethod, nwk, thresh);
-                  PAEContactMatrix newpae = new PAEContactMatrix(
-                          jaa.sequenceRef, mapping, elements, grpset);
-                  jaa.sequenceRef.addContactListFor(jaa, newpae);
-                }
-              }
-              else
-              {
-                Console.error("Ignoring CONTACT_MAP annotation with type "
-                        + xmlmat.getType());
-              }
-            }
+              restoreMatrixFor(jaa.sequenceRef, jaa, xmlmat);
+            } 
           }
         }
 
@@ -4355,6 +4304,103 @@ xmlmat = new MatrixType();
     return af;
   }
 
+  private void importMatrixData(List<MatrixType> xmlmatrices)
+  {
+    for (MatrixType xmlmat:xmlmatrices)
+    {
+      if (!PAEContactMatrix.PAEMATRIX.equals(xmlmat.getType()))
+      {
+        Console.error("Ignoring matrix '"+xmlmat.getId()+"' of type '"+xmlmat.getType());
+        continue;
+      }
+
+      if (!xmlmat.getRows().equals(xmlmat.getCols()))
+      {
+        Console.error("Can't handle non square matrices");
+        continue;
+      }
+
+      float[][] elements = ContactMatrix
+              .fromFloatStringToContacts(xmlmat.getElements(),
+                      xmlmat.getCols().intValue(),
+                      xmlmat.getRows().intValue());
+      
+      List<BitSet> newgroups = new ArrayList<BitSet>();
+      if (xmlmat.getGroups().size() > 0)
+      {
+        for (String sgroup : xmlmat.getGroups())
+        {
+          newgroups.add(deStringifyBitset(sgroup));
+        }
+      }
+      String nwk = xmlmat.getNewick().size() > 0
+              ? xmlmat.getNewick().get(0)
+              : null;
+      if (xmlmat.getNewick().size() > 1)
+      {
+        Console.log.info(
+                "Ignoring additional clusterings for contact matrix");
+      }
+      String treeMethod = xmlmat.getTreeMethod();
+      double thresh = xmlmat.getCutHeight() != null
+              ? xmlmat.getCutHeight()
+              : 0;
+      GroupSet grpset = new GroupSet();
+      grpset.restoreGroups(newgroups, treeMethod, nwk, thresh);
+      
+      FloatContactMatrix newcm = new FloatContactMatrix(elements, grpset);
+      contactMatrixRefs.put(xmlmat.getId(), newcm);
+      Console.trace("Restored base contact matrix "+xmlmat.getId());
+    }
+  }
+
+  private void restoreMatrixFor(SequenceI sequenceRef,
+          AlignmentAnnotation jaa, MapOnAMatrixType xmlmatmapping)
+  {
+    MatrixType xmlmat;
+
+    // locate matrix data in project XML and import
+    ContactMatrixI cm = contactMatrixRefs.get(xmlmatmapping.getMatrix());
+    if (cm == null)
+    {
+      Console.error("Cannot restore mapping to matrix "
+              + xmlmatmapping.getMatrix() + " - not found in project.");
+    }
+
+    // restore mapping data to matrix data
+    jalview.util.MapList mapping = null;
+    if (xmlmatmapping.getMapping() != null)
+    {
+      MapListType m = xmlmatmapping.getMapping();
+      // Mapping m = dr.getMapping();
+      int fr[] = new int[m.getMapListFrom().size() * 2];
+      Iterator<MapListFrom> from = m.getMapListFrom().iterator();// enumerateMapListFrom();
+      for (int _i = 0; from.hasNext(); _i += 2)
+      {
+        MapListFrom mf = from.next();
+        fr[_i] = mf.getStart();
+        fr[_i + 1] = mf.getEnd();
+      }
+      int fto[] = new int[m.getMapListTo().size() * 2];
+      Iterator<MapListTo> to = m.getMapListTo().iterator();// enumerateMapListTo();
+      for (int _i = 0; to.hasNext(); _i += 2)
+      {
+        MapListTo mf = to.next();
+        fto[_i] = mf.getStart();
+        fto[_i + 1] = mf.getEnd();
+      }
+
+      mapping = new jalview.util.MapList(fr, fto,
+              m.getMapFromUnit().intValue(), m.getMapToUnit().intValue());
+    }
+    PAEContactMatrix newpae = new PAEContactMatrix(jaa.sequenceRef, mapping,
+            cm);
+
+    jaa.sequenceRef.addContactListFor(jaa, newpae);
+
+    return;
+  }
+
   /**
    * Load Overview window, restoring colours, 'show hidden regions' flag, title
    * and geometry as saved