+ /**
+ * make a new dataset ID for this jalview dataset alignment
+ *
+ * @param dataset
+ * @return
+ */
+ private String getDatasetIdRef(jalview.datamodel.Alignment dataset)
+ {
+ if (dataset.getDataset() != null)
+ {
+ warn("Serious issue! Dataset Object passed to getDatasetIdRef is not a Jalview DATASET alignment...");
+ }
+ String datasetId = makeHashCode(dataset, null);
+ if (datasetId == null)
+ {
+ // make a new datasetId and record it
+ if (dataset2Ids == null)
+ {
+ dataset2Ids = new IdentityHashMap();
+ }
+ else
+ {
+ datasetId = (String) dataset2Ids.get(dataset);
+ }
+ if (datasetId == null)
+ {
+ datasetId = "ds" + dataset2Ids.size() + 1;
+ dataset2Ids.put(dataset, datasetId);
+ }
+ }
+ return datasetId;
+ }
+
+ private void addDBRefs(SequenceI datasetSequence, Sequence sequence)
+ {
+ for (int d = 0; d < sequence.getDBRefCount(); d++)
+ {
+ DBRef dr = sequence.getDBRef(d);
+ jalview.datamodel.DBRefEntry entry = new jalview.datamodel.DBRefEntry(
+ sequence.getDBRef(d).getSource(), sequence.getDBRef(d)
+ .getVersion(), sequence.getDBRef(d).getAccessionId());
+ if (dr.getMapping() != null)
+ {
+ entry.setMap(addMapping(dr.getMapping()));
+ }
+ datasetSequence.addDBRef(entry);
+ }
+ }
+
+ private jalview.datamodel.Mapping addMapping(Mapping m)
+ {
+ SequenceI dsto = null;
+ // Mapping m = dr.getMapping();
+ int fr[] = new int[m.getMapListFromCount() * 2];
+ Enumeration f = m.enumerateMapListFrom();
+ for (int _i = 0; f.hasMoreElements(); _i += 2)
+ {
+ MapListFrom mf = (MapListFrom) f.nextElement();
+ fr[_i] = mf.getStart();
+ fr[_i + 1] = mf.getEnd();
+ }
+ int fto[] = new int[m.getMapListToCount() * 2];
+ f = m.enumerateMapListTo();
+ for (int _i = 0; f.hasMoreElements(); _i += 2)
+ {
+ MapListTo mf = (MapListTo) f.nextElement();
+ fto[_i] = mf.getStart();
+ fto[_i + 1] = mf.getEnd();
+ }
+ jalview.datamodel.Mapping jmap = new jalview.datamodel.Mapping(dsto,
+ fr, fto, (int) m.getMapFromUnit(), (int) m.getMapToUnit());
+ if (m.getMappingChoice() != null)
+ {
+ MappingChoice mc = m.getMappingChoice();
+ if (mc.getDseqFor() != null)
+ {
+ String dsfor = "" + mc.getDseqFor();
+ if (seqRefIds.containsKey(dsfor))
+ {
+ /**
+ * recover from hash
+ */
+ jmap.setTo((SequenceI) seqRefIds.get(dsfor));
+ }
+ else
+ {
+ frefedSequence.add(new Object[]
+ { dsfor, jmap });
+ }
+ }
+ else
+ {
+ /**
+ * local sequence definition
+ */
+ Sequence ms = mc.getSequence();
+ jalview.datamodel.Sequence djs = null;
+ String sqid = ms.getDsseqid();
+ if (sqid != null && sqid.length() > 0)
+ {
+ /*
+ * recover dataset sequence
+ */
+ djs = (jalview.datamodel.Sequence) seqRefIds.get(sqid);
+ }
+ else
+ {
+ System.err
+ .println("Warning - making up dataset sequence id for DbRef sequence map reference");
+ sqid = ((Object) ms).toString(); // make up a new hascode for
+ // undefined dataset sequence hash
+ // (unlikely to happen)
+ }
+
+ if (djs == null)
+ {
+ /**
+ * make a new dataset sequence and add it to refIds hash
+ */
+ djs = new jalview.datamodel.Sequence(ms.getName(), ms
+ .getSequence());
+ djs.setStart(jmap.getMap().getToLowest());
+ djs.setEnd(jmap.getMap().getToHighest());
+ djs.setVamsasId(uniqueSetSuffix + sqid);
+ jmap.setTo(djs);
+ seqRefIds.put(sqid, djs);
+
+ }
+ jalview.bin.Cache.log.debug("about to recurse on addDBRefs.");
+ addDBRefs(djs, ms);
+
+ }
+ }
+ return (jmap);
+
+ }
+
+ public jalview.gui.AlignmentPanel copyAlignPanel(AlignmentPanel ap,
+ boolean keepSeqRefs)
+ {
+ initSeqRefs();
+ jalview.schemabinding.version2.JalviewModel jm = SaveState(ap, null,
+ null);
+
+ if (!keepSeqRefs)
+ {
+ clearSeqRefs();
+ jm.getJalviewModelSequence().getViewport(0).setSequenceSetId(null);
+ }
+ else
+ {
+ uniqueSetSuffix = "";
+ jm.getJalviewModelSequence().getViewport(0).setId(null); // we don't
+ // overwrite the
+ // view we just
+ // copied
+ }
+ if (this.frefedSequence == null)
+ {
+ frefedSequence = new Vector();
+ }
+
+ viewportsAdded = new Hashtable();
+
+ AlignFrame af = LoadFromObject(jm, null, false, null);
+ af.alignPanels.clear();
+ af.closeMenuItem_actionPerformed(true);
+
+ /*
+ * if(ap.av.alignment.getAlignmentAnnotation()!=null) { for(int i=0;
+ * i<ap.av.alignment.getAlignmentAnnotation().length; i++) {
+ * if(!ap.av.alignment.getAlignmentAnnotation()[i].autoCalculated) {
+ * af.alignPanel.av.alignment.getAlignmentAnnotation()[i] =
+ * ap.av.alignment.getAlignmentAnnotation()[i]; } } }
+ */
+
+ return af.alignPanel;
+ }
+
+ /**
+ * flag indicating if hashtables should be cleared on finalization TODO this
+ * flag may not be necessary
+ */
+ private boolean _cleartables = true;
+
+ private Hashtable jvids2vobj;
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#finalize()
+ */
+ protected void finalize() throws Throwable
+ {
+ // really make sure we have no buried refs left.
+ if (_cleartables)
+ {
+ clearSeqRefs();
+ }
+ this.seqRefIds = null;
+ this.seqsToIds = null;
+ super.finalize();
+ }
+
+ private void warn(String msg)
+ {
+ warn(msg, null);
+ }
+
+ private void warn(String msg, Exception e)
+ {
+ if (Cache.log != null)
+ {
+ if (e != null)
+ {
+ Cache.log.warn(msg, e);
+ }
+ else
+ {
+ Cache.log.warn(msg);
+ }
+ }
+ else
+ {
+ System.err.println("Warning: " + msg);
+ if (e != null)
+ {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private void debug(String string)
+ {
+ debug(string, null);
+ }
+
+ private void debug(String msg, Exception e)
+ {
+ if (Cache.log != null)
+ {
+ if (e != null)
+ {
+ Cache.log.debug(msg, e);
+ }
+ else
+ {
+ Cache.log.debug(msg);
+ }
+ }
+ else
+ {
+ System.err.println("Warning: " + msg);
+ if (e != null)
+ {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /**
+ * set the object to ID mapping tables used to write/recover objects and XML
+ * ID strings for the jalview project. If external tables are provided then
+ * finalize and clearSeqRefs will not clear the tables when the Jalview2XML
+ * object goes out of scope. - also populates the datasetIds hashtable with
+ * alignment objects containing dataset sequences
+ *
+ * @param vobj2jv
+ * Map from ID strings to jalview datamodel
+ * @param jv2vobj
+ * Map from jalview datamodel to ID strings
+ *
+ *
+ */
+ public void setObjectMappingTables(Hashtable vobj2jv,
+ IdentityHashMap jv2vobj)
+ {
+ this.jv2vobj = jv2vobj;
+ this.vobj2jv = vobj2jv;
+ Iterator ds = jv2vobj.keySet().iterator();
+ String id;
+ while (ds.hasNext())
+ {
+ Object jvobj = ds.next();
+ id = jv2vobj.get(jvobj).toString();
+ if (jvobj instanceof jalview.datamodel.Alignment)
+ {
+ if (((jalview.datamodel.Alignment) jvobj).getDataset() == null)
+ {
+ addDatasetRef(id, (jalview.datamodel.Alignment) jvobj);
+ }
+ }
+ else if (jvobj instanceof jalview.datamodel.Sequence)
+ {
+ // register sequence object so the XML parser can recover it.
+ if (seqRefIds == null)
+ {
+ seqRefIds = new Hashtable();
+ }
+ if (seqsToIds == null)
+ {
+ seqsToIds = new IdentityHashMap();
+ }
+ seqRefIds.put(jv2vobj.get(jvobj).toString(), jvobj);
+ seqsToIds.put(jvobj, id);
+ }
+ else if (jvobj instanceof jalview.datamodel.AlignmentAnnotation)
+ {
+ if (annotationIds == null)
+ {
+ annotationIds = new Hashtable();
+ }
+ String anid;
+ annotationIds.put(anid = jv2vobj.get(jvobj).toString(), jvobj);
+ jalview.datamodel.AlignmentAnnotation jvann = (jalview.datamodel.AlignmentAnnotation) jvobj;
+ if (jvann.annotationId == null)
+ {
+ jvann.annotationId = anid;
+ }
+ if (!jvann.annotationId.equals(anid))
+ {
+ // TODO verify that this is the correct behaviour
+ this.warn("Overriding Annotation ID for " + anid
+ + " from different id : " + jvann.annotationId);
+ jvann.annotationId = anid;
+ }
+ }
+ else if (jvobj instanceof String)
+ {
+ if (jvids2vobj == null)
+ {
+ jvids2vobj = new Hashtable();
+ jvids2vobj.put(jvobj, jv2vobj.get(jvobj).toString());
+ }
+ }
+ else
+ Cache.log.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
+ }
+ }
+
+ /**
+ * set the uniqueSetSuffix used to prefix/suffix object IDs for jalview
+ * objects created from the project archive. If string is null (default for
+ * construction) then suffix will be set automatically.
+ *
+ * @param string
+ */
+ public void setUniqueSetSuffix(String string)
+ {
+ uniqueSetSuffix = string;
+
+ }
+
+ /**
+ * uses skipList2 as the skipList for skipping views on sequence sets
+ * associated with keys in the skipList
+ *
+ * @param skipList2
+ */
+ public void setSkipList(Hashtable skipList2)
+ {
+ skipList = skipList2;
+ }
+
+}