*/
public class Jalview2XML
{
-
- Hashtable seqRefIds = null;
+ /**
+ * create/return unique hash string for sq
+ * @param sq
+ * @return new or existing unique string for sq
+ */
+ String seqHash(SequenceI sq)
+ {
+ if (seqsToIds==null)
+ {
+ initSeqRefs();
+ }
+ if (seqsToIds.containsKey(sq))
+ {
+ return (String) seqsToIds.get(sq);
+ } else {
+ // create sequential key
+ String key = "sq"+(seqsToIds.size()+1);
+ seqsToIds.put(sq, key);
+ return key;
+ }
+ }
+ void clearSeqRefs()
+ {
+ seqRefIds.clear();
+ seqsToIds.clear();
+ }
+ void initSeqRefs()
+ {
+ if (seqsToIds==null)
+ {
+ seqsToIds = new IdentityHashMap();
+ }
+ if (seqRefIds==null)
+ {
+ seqRefIds = new Hashtable();
+ }
+ }
+ java.util.IdentityHashMap seqsToIds = null; // SequenceI->key resolution
+ java.util.Hashtable seqRefIds = null; // key->SequenceI resolution
Vector frefedSequence = null;
boolean raiseGUI = true; // whether errors are raised in dialog boxes or not
//NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS
////////////////////////////////////////////////////
- PrintWriter out = new PrintWriter(new OutputStreamWriter(jout,
- "UTF-8"));
+ //NOTE ALSO new PrintWriter must be used for each new JarEntry
+ PrintWriter out = null;
Vector shortNames = new Vector();
{
AlignmentPanel apanel = (AlignmentPanel) af.alignPanels
.elementAt(ap);
+ String fileName = apSize == 1 ? shortName : ap + shortName;
+ if (!fileName.endsWith(".xml"))
+ {
+ fileName = fileName + ".xml";
+ }
- SaveState(apanel, apSize == 1 ? shortName : ap + shortName,
+ JarEntry entry = new JarEntry(fileName);
+ jout.putNextEntry(entry);
+ out = new PrintWriter(new OutputStreamWriter(jout, "UTF-8"));
+ SaveState(apanel, fileName,
jout, out);
}
}
}
-
- out.close();
+ try { out.flush(); } catch (Exception foo) {};
+ jout.closeEntry();
+ try { jout.flush(); } catch (Exception foo) {};
jout.close();
} catch (Exception ex)
{
int ap, apSize = af.alignPanels.size();
FileOutputStream fos = new FileOutputStream(jarFile);
JarOutputStream jout = new JarOutputStream(fos);
- PrintWriter out = new PrintWriter(new OutputStreamWriter(jout,
- "UTF-8"));
for (ap = 0; ap < apSize; ap++)
{
AlignmentPanel apanel = (AlignmentPanel) af.alignPanels
.elementAt(ap);
-
- SaveState(apanel, apSize == 1 ? fileName : fileName + ap, jout, out);
+ String jfileName = apSize == 1 ? fileName : fileName + ap;
+ if (!jfileName.endsWith(".xml"))
+ {
+ jfileName = jfileName + ".xml";
+ }
+ JarEntry entry = new JarEntry(jfileName);
+ jout.putNextEntry(entry);
+ PrintWriter out = new PrintWriter(new OutputStreamWriter(jout,
+ "UTF-8"));
+ SaveState(apanel, jfileName, jout, out);
+ try { out.flush(); } catch (Exception foo) {};
+ jout.closeEntry();
}
- out.close();
+ try { jout.flush(); } catch (Exception foo) {};
jout.close();
return true;
} catch (Exception ex)
public JalviewModel SaveState(AlignmentPanel ap, String fileName,
JarOutputStream jout, PrintWriter out)
{
- if (seqRefIds == null)
- {
- seqRefIds = new Hashtable();
- }
-
+ initSeqRefs();
+
Vector userColours = new Vector();
AlignViewport av = ap.av;
JSeq jseq;
//SAVE SEQUENCES
- int id = 0;
+ String id = "";
jalview.datamodel.SequenceI jds;
for (int i = 0; i < jal.getHeight(); i++)
{
jds = jal.getSequenceAt(i);
- id = jds.hashCode();
+ id = seqHash(jds);
- if (seqRefIds.get(id + "") != null)
+ if (seqRefIds.get(id) != null)
{
-
+ // This happens for two reasons: 1. multiple views are being serialised. 2. the hashCode has collided with another sequence's code. This DOES HAPPEN! (PF00072.15.stk does this)
+ // JBPNote: Uncomment to debug writing out of files that do not read back in due to ArrayOutOfBoundExceptions.
+ //System.err.println("vamsasSeq backref: "+id+"");
+ //System.err.println(jds.getName()+" "+jds.getStart()+"-"+jds.getEnd()+" "+jds.getSequenceAsString());
+ //System.err.println("Hashcode: "+seqHash(jds));
+ //SequenceI rsq = (SequenceI) seqRefIds.get(id + "");
+ //System.err.println(rsq.getName()+" "+rsq.getStart()+"-"+rsq.getEnd()+" "+rsq.getSequenceAsString());
+ //System.err.println("Hashcode: "+seqHash(rsq));
}
else
{
vamsasSeq = createVamsasSequence(id, jds);
vamsasSet.addSequence(vamsasSeq);
- seqRefIds.put(id + "", jal.getSequenceAt(i));
+ seqRefIds.put(id, jds);
}
jseq = new JSeq();
jseq.setEnd(jds.getEnd());
jseq.setColour(av.getSequenceColour(jds).getRGB());
- jseq.setId(id);
+ jseq.setId(id); // jseq id should be a string not a number
if (av.hasHiddenRows)
{
DataOutputStream dout = new DataOutputStream(jout);
dout.write(data, 0, data.length);
+ dout.flush();
jout.closeEntry();
}
} catch (Exception ex)
{
jalview.datamodel.Sequence seq = (jalview.datamodel.Sequence) sg
.getSequenceAt(s);
- groups[i].addSeq(seq.hashCode());
+ groups[i].addSeq(seqHash(seq));
}
}
if (out != null)
{
- //We may not want to right the object to disk,
+ //We may not want to write the object to disk,
//eg we can copy the alignViewport to a new view object
//using save and then load
try
{
- if (!fileName.endsWith(".xml"))
- {
- fileName = fileName + ".xml";
- }
-
- JarEntry entry = new JarEntry(fileName);
- jout.putNextEntry(entry);
-
- object.marshal(out);
+ org.exolab.castor.xml.Marshaller marshaller = new org.exolab.castor.xml.Marshaller(out);
+ marshaller.marshal(object);
+ out.flush();
} catch (Exception ex)
{
ex.printStackTrace();
return object;
}
- private Sequence createVamsasSequence(int id, SequenceI jds)
+ private Sequence createVamsasSequence(String id, SequenceI jds)
{
return createVamsasSequence(true, id, jds, null);
}
- private Sequence createVamsasSequence(boolean recurse, int id,
+ private Sequence createVamsasSequence(boolean recurse, String id,
SequenceI jds, SequenceI parentseq)
{
Sequence vamsasSeq = new Sequence();
- vamsasSeq.setId(id + "");
+ vamsasSeq.setId(id);
vamsasSeq.setName(jds.getName());
vamsasSeq.setSequence(jds.getSequenceAsString());
vamsasSeq.setDescription(jds.getDescription());
jalview.datamodel.DBRefEntry[] dbrefs = null;
if (jds.getDatasetSequence() != null)
{
- vamsasSeq.setDsseqid(jds.getDatasetSequence().hashCode() + "");
+ vamsasSeq.setDsseqid(seqHash(jds.getDatasetSequence()));
if (jds.getDatasetSequence().getDBRef() != null)
{
dbrefs = jds.getDatasetSequence().getDBRef();
}
else
{
- vamsasSeq.setDsseqid(id + ""); // so we can tell which sequences really are dataset sequences only
+ vamsasSeq.setDsseqid(id); // so we can tell which sequences really are dataset sequences only
dbrefs = jds.getDBRef();
}
if (dbrefs != null)
&& (parentseq != jmp.getTo() || parentseq
.getDatasetSequence() != jmp.getTo()))
{
- mpc.setSequence(createVamsasSequence(false, jmp.getTo()
- .hashCode(), jmp.getTo(), jds));
+ mpc.setSequence(createVamsasSequence(false, seqHash(jmp.getTo())
+ , jmp.getTo(), jds));
}
else
{
- long jmpid = 0;
+ String jmpid = "";
SequenceI ps = null;
if (parentseq != jmp.getTo()
&& parentseq.getDatasetSequence() != jmp.getTo())
{
// chaining dbref rather than a handshaking one
- jmpid = (ps = jmp.getTo()).hashCode();
+ jmpid = seqHash(ps = jmp.getTo());
}
else
{
- jmpid = (ps = parentseq).hashCode();
+ jmpid = seqHash(ps = parentseq);
}
- mpc.setDseqFor("" + jmpid);
+ mpc.setDseqFor(jmpid);
if (!seqRefIds.containsKey(mpc.getDseqFor()))
{
jalview.bin.Cache.log.debug("creatign new DseqFor ID");
{
out.println(data);
}
+ try { out.flush(); } catch (Exception foo) {};
out.close();
alreadyLoadedPDB.put(pdbId, outFile.getAbsolutePath());
boolean multipleView = false;
JSeq[] JSEQ = object.getJalviewModelSequence().getJSeq();
+ int vi=0; // counter in vamsasSeq array
for (int i = 0; i < JSEQ.length; i++)
{
String seqId = JSEQ[i].getId() + "";
}
else
{
- jseq = new jalview.datamodel.Sequence(vamsasSeq[i].getName(),
- vamsasSeq[i].getSequence());
- jseq.setDescription(vamsasSeq[i].getDescription());
+ jseq = new jalview.datamodel.Sequence(vamsasSeq[vi].getName(),
+ vamsasSeq[vi].getSequence());
+ jseq.setDescription(vamsasSeq[vi].getDescription());
jseq.setStart(JSEQ[i].getStart());
jseq.setEnd(JSEQ[i].getEnd());
jseq.setVamsasId(uniqueSetSuffix + seqId);
- seqRefIds.put(vamsasSeq[i].getId(), jseq);
+ seqRefIds.put(vamsasSeq[vi].getId()+"", jseq);
tmpseqs.add(jseq);
+ vi++;
}
if (JSEQ[i].getHidden())
.getSecondaryStructure() == null || ae[aa]
.getSecondaryStructure().length() == 0) ? ' ' : ae[aa]
.getSecondaryStructure().charAt(0), ae[aa].getValue()
-
+
);
-
+ // JBPNote: Consider verifying dataflow for IO of secondary structure annotation read from Stockholm files
+ // this was added to try to ensure that
+ //if (anot[ae[aa].getPosition()].secondaryStructure>' ')
+ //{
+ // anot[ae[aa].getPosition()].displayCharacter = "";
+ //}
anot[ae[aa].getPosition()].colour = new java.awt.Color(ae[aa]
.getColour());
}
if (sqid == null)
{
// make up a new dataset reference for this sequence
- sqid = "" + dsq.hashCode();
+ sqid = seqHash(dsq);
}
dsq.setVamsasId(uniqueSetSuffix + sqid);
seqRefIds.put(sqid, dsq);
djs = (jalview.datamodel.Sequence) seqRefIds.get(sqid);
} else {
System.err.println("Warning - making up dataset sequence id for DbRef sequence map reference");
- sqid = ""+ms.hashCode(); // make up a new hascode for undefined dataset sequence hash (unlikely to happen)
+ sqid = ((Object)ms).toString(); // make up a new hascode for undefined dataset sequence hash (unlikely to happen)
}
if (djs==null) {
if (!keepSeqRefs)
{
- seqRefIds.clear();
+ clearSeqRefs();
jm.getJalviewModelSequence().getViewport(0).setSequenceSetId(null);
}
else
return af.alignPanel;
}
+ /* (non-Javadoc)
+ * @see java.lang.Object#finalize()
+ */
+ protected void finalize() throws Throwable
+ {
+ // really make sure we have no buried refs left.
+ clearSeqRefs();
+ this.seqRefIds = null;
+ this.seqsToIds = null;
+ super.finalize();
+ }
+
}
* given is outside the bounds of the collection\r
*/\r
public void addSeq(\r
- final int vSeq)\r
+ final java.lang.String vSeq)\r
throws java.lang.IndexOutOfBoundsException {\r
- this._seqList.addElement(new java.lang.Integer(vSeq));\r
+ this._seqList.addElement(vSeq);\r
}\r
\r
/**\r
*/\r
public void addSeq(\r
final int index,\r
- final int vSeq)\r
+ final java.lang.String vSeq)\r
throws java.lang.IndexOutOfBoundsException {\r
- this._seqList.add(index, new java.lang.Integer(vSeq));\r
+ this._seqList.add(index, vSeq);\r
}\r
\r
/**\r
/**\r
* Method enumerateSeq.\r
* \r
- * @return an Enumeration over all int elements\r
+ * @return an Enumeration over all java.lang.String elements\r
*/\r
public java.util.Enumeration enumerateSeq(\r
) {\r
* @param index\r
* @throws java.lang.IndexOutOfBoundsException if the index\r
* given is outside the bounds of the collection\r
- * @return the value of the int at the given index\r
+ * @return the value of the java.lang.String at the given index\r
*/\r
- public int getSeq(\r
+ public java.lang.String getSeq(\r
final int index)\r
throws java.lang.IndexOutOfBoundsException {\r
// check bounds for index\r
throw new IndexOutOfBoundsException("getSeq: Index value '" + index + "' not in range [0.." + (this._seqList.size() - 1) + "]");\r
}\r
\r
- return ((java.lang.Integer) _seqList.get(index)).intValue();\r
+ return (java.lang.String) _seqList.get(index);\r
}\r
\r
/**\r
* Method getSeq.Returns the contents of the collection in an\r
- * Array. \r
+ * Array. <p>Note: Just in case the collection contents are\r
+ * changing in another thread, we pass a 0-length Array of the\r
+ * correct type into the API call. This way we <i>know</i>\r
+ * that the Array returned is of exactly the correct length.\r
* \r
* @return this collection as an Array\r
*/\r
- public int[] getSeq(\r
+ public java.lang.String[] getSeq(\r
) {\r
- int size = this._seqList.size();\r
- int[] array = new int[size];\r
- java.util.Iterator iter = _seqList.iterator();\r
- for (int index = 0; index < size; index++) {\r
- array[index] = ((java.lang.Integer) iter.next()).intValue();\r
- }\r
- return array;\r
+ java.lang.String[] array = new java.lang.String[0];\r
+ return (java.lang.String[]) this._seqList.toArray(array);\r
}\r
\r
/**\r
* @return true if the object was removed from the collection.\r
*/\r
public boolean removeSeq(\r
- final int vSeq) {\r
- boolean removed = _seqList.remove(new java.lang.Integer(vSeq));\r
+ final java.lang.String vSeq) {\r
+ boolean removed = _seqList.remove(vSeq);\r
return removed;\r
}\r
\r
* @param index\r
* @return the element removed from the collection\r
*/\r
- public int removeSeqAt(\r
+ public java.lang.String removeSeqAt(\r
final int index) {\r
java.lang.Object obj = this._seqList.remove(index);\r
- return ((java.lang.Integer) obj).intValue();\r
+ return (java.lang.String) obj;\r
}\r
\r
/**\r
*/\r
public void setSeq(\r
final int index,\r
- final int vSeq)\r
+ final java.lang.String vSeq)\r
throws java.lang.IndexOutOfBoundsException {\r
// check bounds for index\r
if (index < 0 || index >= this._seqList.size()) {\r
throw new IndexOutOfBoundsException("setSeq: Index value '" + index + "' not in range [0.." + (this._seqList.size() - 1) + "]");\r
}\r
\r
- this._seqList.set(index, new java.lang.Integer(vSeq));\r
+ this._seqList.set(index, vSeq);\r
}\r
\r
/**\r
* @param vSeqArray\r
*/\r
public void setSeq(\r
- final int[] vSeqArray) {\r
+ final java.lang.String[] vSeqArray) {\r
//-- copy array\r
_seqList.clear();\r
\r
for (int i = 0; i < vSeqArray.length; i++) {\r
- this._seqList.add(new java.lang.Integer(vSeqArray[i]));\r
+ this._seqList.add(vSeqArray[i]);\r
}\r
}\r
\r