exploring making PAE storage in projects more efficient
[jalview.git] / src / jalview / project / Jalview2XML.java
index 6f26036..b9e1f46 100644 (file)
@@ -283,6 +283,12 @@ public class Jalview2XML
    * entry names
    */
   private Map<RnaModel, String> rnaSessions = new HashMap<>();
+  
+  /**
+   * map from contact matrices to their XML ids
+   */
+  private Map<ContactMatrixI,String> contactMatrices = new HashMap<>();
+  private Map<String, ContactMatrixI> contactMatrixRefs = new HashMap<>();
 
   /**
    * A helper method for safely using the value of an optional attribute that
@@ -2315,64 +2321,7 @@ public class Jalview2XML
                     .getContactMatrixFor(annotation);
             if (cm != null)
             {
-              MatrixType 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.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());
-              }
-              // set/get properties
-              if (cm instanceof MappableContactMatrixI)
-              {
-                jalview.util.MapList mlst = ((MappableContactMatrixI) cm)
-                        .getMapFor(annotation.sequenceRef);
-                if (mlst != null)
-                {
-                  MapListType mp = new MapListType();
-                  List<int[]> r = mlst.getFromRanges();
-                  for (int[] range : r)
-                  {
-                    MapListFrom mfrom = new MapListFrom();
-                    mfrom.setStart(range[0]);
-                    mfrom.setEnd(range[1]);
-                    // mp.addMapListFrom(mfrom);
-                    mp.getMapListFrom().add(mfrom);
-                  }
-                  r = mlst.getToRanges();
-                  for (int[] range : r)
-                  {
-                    MapListTo mto = new MapListTo();
-                    mto.setStart(range[0]);
-                    mto.setEnd(range[1]);
-                    // mp.addMapListTo(mto);
-                    mp.getMapListTo().add(mto);
-                  }
-                  mp.setMapFromUnit(
-                          BigInteger.valueOf(mlst.getFromRatio()));
-                  mp.setMapToUnit(BigInteger.valueOf(mlst.getToRatio()));
-                  xmlmat.setMapping(mp);
-                }
-              }
-              // and add to model
-              an.getContactmatrix().add(xmlmat);
+              storeMatrixFor(vamsasSet, an,annotation, cm);
             }
           }
         }
@@ -2479,6 +2428,78 @@ public class Jalview2XML
 
   }
 
+  private void storeMatrixFor(SequenceSet root, Annotation an, AlignmentAnnotation annotation, ContactMatrixI cm)
+  {
+    String cmId = contactMatrices.get(cm);
+    MatrixType xmlmat=null;
+    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.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));
+      root.getMatrices().add(xmlmat);
+    }
+    else {
+      
+    }
+    // set/get properties
+    if (cm instanceof MappableContactMatrixI)
+    {
+      jalview.util.MapList mlst = ((MappableContactMatrixI) cm)
+              .getMapFor(annotation.sequenceRef);
+      if (mlst != null)
+      {
+        MapListType mp = new MapListType();
+        List<int[]> r = mlst.getFromRanges();
+        for (int[] range : r)
+        {
+          MapListFrom mfrom = new MapListFrom();
+          mfrom.setStart(range[0]);
+          mfrom.setEnd(range[1]);
+          // mp.addMapListFrom(mfrom);
+          mp.getMapListFrom().add(mfrom);
+        }
+        r = mlst.getToRanges();
+        for (int[] range : r)
+        {
+          MapListTo mto = new MapListTo();
+          mto.setStart(range[0]);
+          mto.setEnd(range[1]);
+          // mp.addMapListTo(mto);
+          mp.getMapListTo().add(mto);
+        }
+        mp.setMapFromUnit(
+                BigInteger.valueOf(mlst.getFromRatio()));
+        mp.setMapToUnit(BigInteger.valueOf(mlst.getToRatio()));
+        xmlmat.setMapping(mp);
+      }
+    }
+    // and add to model
+    an.getContactmatrix().add(xmlmat);    
+  }
+
   private String stringifyBitset(BitSet gp)
   {
     StringBuilder sb = new StringBuilder();