+ private void storeMatrixFor(SequenceSet root, Annotation an,
+ AlignmentAnnotation annotation, ContactMatrixI cm)
+ {
+ String cmId = contactMatrices.get(cm);
+ MatrixType xmlmat = 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.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(cmId = "m" + contactMatrices.size()
+ + System.currentTimeMillis());
+ Console.trace("Matrix data stored :" + cmId);
+ contactMatrices.put(cm, cmId);
+ contactMatrixRefs.put(cmId, cm);
+ xmlMatrices.add(xmlmat);
+ }
+ else
+ {
+ Console.trace("Existing Matrix stored :" + cmId);
+ }
+
+ // 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)
+ .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()));
+ xmlmatmapping.setMapping(mp);
+ }
+ }
+ // and add to model
+ an.getContactmatrix().add(xmlmatmapping);
+ }
+
+ private String stringifyBitset(BitSet gp)
+ {
+ StringBuilder sb = new StringBuilder();
+ for (long val : gp.toLongArray())
+ {
+ if (sb.length() > 0)
+ {
+ sb.append(",");
+ }
+ sb.append(val);
+ }
+ return sb.toString();
+ }
+
+ private BitSet deStringifyBitset(String stringified)
+ {
+ if ("".equals(stringified) || stringified == null)
+ {
+ return new BitSet();
+ }
+ String[] longvals = stringified.split(",");
+ long[] newlongvals = new long[longvals.length];
+ for (int lv = 0; lv < longvals.length; lv++)
+ {
+ try
+ {
+ newlongvals[lv] = Long.valueOf(longvals[lv]);
+ } catch (Exception x)
+ {
+ errorMessage += "Couldn't destringify bitset from: '" + stringified
+ + "'";
+ newlongvals[lv] = 0;
+ }
+ }
+ return BitSet.valueOf(newlongvals);
+
+ }
+