updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / io / VamsasDatastore.java
index 3915446..a9735be 100755 (executable)
-/*\r
- * Jalview - A Sequence Alignment Editor and Viewer\r
- * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
- *\r
- * This program is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU General Public License\r
- * as published by the Free Software Foundation; either version 2\r
- * of the License, or (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program; if not, write to the Free Software\r
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
- */\r
-\r
-package jalview.io;\r
-\r
-import org.vamsas.client.Vobject;\r
-import org.vamsas.client.VorbaId;\r
-import org.vamsas.objects.core.*;\r
-import org.vamsas.objects.utils.DocumentStuff;\r
-import org.vamsas.test.simpleclient.ClientDoc;\r
-\r
-import jalview.bin.Cache;\r
-import jalview.datamodel.SequenceI;\r
-import jalview.gui.*;\r
-import java.io.*;\r
-import java.util.HashMap;\r
-import java.util.HashSet;\r
-import java.util.Hashtable;\r
-import java.util.IdentityHashMap;\r
-import java.util.jar.*;\r
-import org.exolab.castor.xml.*;\r
-import org.exolab.castor.mapping.Mapping;\r
-\r
-/*\r
-\r
- static {\r
- org.exolab.castor.util.LocalConfiguration.getInstance().getProperties().setProperty(\r
- "org.exolab.castor.serializer", "org.apache.xml.serialize.XMLSerilazizer");\r
- }\r
-\r
- */\r
-\r
-public class VamsasDatastore\r
-{\r
-       Entry provEntry=null;\r
-       AlignViewport av;\r
-       org.exolab.castor.types.Date date\r
-       = new org.exolab.castor.types.Date(new java.util.Date());\r
-       ClientDoc cdoc;\r
-       Hashtable vobj2jv;\r
-       IdentityHashMap jv2vobj;\r
-       public VamsasDatastore(ClientDoc cdoc, Hashtable vobj2jv, IdentityHashMap jv2vobj, Entry provEntry) {\r
-               \r
-               this.cdoc = cdoc;\r
-               this.vobj2jv = vobj2jv;\r
-               this.jv2vobj = jv2vobj;\r
-               this.provEntry = provEntry;\r
-       }\r
-       \r
-       /* public void storeJalview(String file, AlignFrame af)\r
-        {\r
-          try\r
-          {\r
-            // 1. Load the mapping information from the file\r
-            Mapping map = new Mapping(getClass().getClassLoader());\r
-            java.net.URL url = getClass().getResource("/jalview_mapping.xml");\r
-            map.loadMapping(url);\r
-\r
-            // 2. Unmarshal the data\r
-            // Unmarshaller unmar = new Unmarshaller();\r
-            //unmar.setIgnoreExtraElements(true);\r
-            //unmar.setMapping(map);\r
-            // uni = (UniprotFile) unmar.unmarshal(new FileReader(file));\r
-\r
-            // 3. marshal the data with the total price back and print the XML in the console\r
-              Marshaller marshaller = new Marshaller( new FileWriter(file) );\r
-\r
-             marshaller.setMapping(map);\r
-             marshaller.marshal(af);\r
-          }\r
-          catch (Exception e)\r
-          {\r
-            e.printStackTrace();\r
-          }\r
-        }\r
-\r
-\r
-        */\r
-       \r
-       protected Vobject getjv2vObj(Object jvobj) {\r
-               if (jv2vobj.containsKey(jvobj))\r
-                       return cdoc.getObject(((VorbaId)jv2vobj.get(jvobj)));\r
-               return null;\r
-       }\r
-       \r
-       protected Object getvObj2jv(org.vamsas.client.Vobject vobj) {\r
-               VorbaId id = vobj.getVorbaId();\r
-               if (id==null) {\r
-                       id = cdoc.registerObject(vobj);\r
-                       Cache.log.debug("Registering new object and returning null for getvObj2jv");\r
-                       return null;\r
-               }\r
-               if (vobj2jv.containsKey(vobj.getVorbaId()))\r
-                       return vobj2jv.get(id);\r
-               return null;\r
-       }\r
-       protected void bindjvvobj(Object jvobj, org.vamsas.client.Vobject vobj) {\r
-               VorbaId id = vobj.getVorbaId();\r
-               if (id==null) {\r
-                       id = cdoc.registerObject(vobj);\r
-               }\r
-               if (vobj2jv.containsKey(vobj.getVorbaId())||jv2vobj.containsKey(jvobj)) {\r
-                       Cache.log.error("Duplicate object binding!");\r
-               } else {\r
-                       vobj2jv.put(vobj.getVorbaId(),jvobj);// JBPNote - better implementing a hybrid invertible hash.\r
-                       jv2vobj.put(jvobj,vobj.getVorbaId());                           \r
-               }\r
-       }\r
-       \r
-       public void storeVAMSAS(AlignViewport av) \r
-       {\r
-               long time = System.currentTimeMillis();\r
-               \r
-               try\r
-               {\r
-                       \r
-                       jalview.datamodel.AlignmentI jal = av.getAlignment();\r
-                       \r
-                       \r
-                       ///////////////////////////////////////////\r
-                       // SAVE THE DATASET\r
-                       VAMSAS root = cdoc.getVamsasRoots()[0];\r
-                       if (jal.getDataset()==null) {\r
-                               Cache.log.warn("Creating new dataset for an alignment.");\r
-                               jal.setDataset(null); // \r
-                       }\r
-                       boolean nw=false;\r
-                       DataSet dataset = (DataSet) getjv2vObj(jal.getDataset());\r
-                       if (dataset==null) {\r
-                               dataset = new DataSet();\r
-                               root.addDataSet(dataset);\r
-                               bindjvvobj(jal.getDataset(),dataset);\r
-                               nw=true;\r
-                       } //else {\r
-                       // should really check root corresponds to dataset.\r
-                       // }\r
-                       // update dataset\r
-                       Sequence sequence;\r
-                       DbRef dbref;\r
-                       for (int i = 0; i < jal.getHeight(); i++)\r
-                       {\r
-                               SequenceI sq = jal.getDataset().getSequenceAt(i);\r
-                               sequence = (Sequence) getjv2vObj(sq);\r
-                               if (sequence==null) {\r
-                                       sequence=new Sequence();\r
-                                       bindjvvobj(sq, sequence);\r
-                                       sq.setVamsasId(sequence.getVorbaId().getId());\r
-                                       sequence.setSequence(sq.getSequence());\r
-                                       sequence.setName(jal.getDataset().getSequenceAt(i).getName());\r
-                                       sequence.setStart(jal.getDataset().getSequenceAt(i).getStart());\r
-                                       sequence.setEnd(jal.getDataset().getSequenceAt(i).getEnd());\r
-                                       dataset.addSequence(sequence);\r
-                               } else {\r
-                                       // verify principal attributes.\r
-                                       \r
-                               }\r
-                               \r
-                               if(jal.getDataset().getSequenceAt(i).getSequenceFeatures()!=null)\r
-                               {\r
-                                       int sfSize = jal.getDataset().getSequenceAt(i).getSequenceFeatures().length;\r
-                                       \r
-                                       for (int sf = 0; sf < sfSize; sf++)\r
-                                       {\r
-                                               jalview.datamodel.SequenceFeature feature =\r
-                                                       (jalview.datamodel.SequenceFeature)\r
-                                                       jal.getDataset().getSequenceAt(i).getSequenceFeatures()[sf];\r
-                                               \r
-                                               DataSetAnnotations dsa = (DataSetAnnotations) getjv2vObj(feature); \r
-                                               if (dsa==null) {\r
-                                                       dsa = new DataSetAnnotations();\r
-                                                       bindjvvobj(feature, dsa);\r
-                                                       dsa.setType(feature.getType());\r
-                                                       dsa.setBegin(feature.getBegin());\r
-                                                       dsa.setEnd(feature.getEnd());\r
-                                                       dsa.setDescription(feature.getDescription());\r
-                                                       dsa.setStatus(feature.getStatus());\r
-                                                       if (dsa.getProvenance()==null) {\r
-                                                               dsa.setProvenance(new Provenance());\r
-                                                       }\r
-                                                       \r
-                                                       dsa.getProvenance().addEntry(provEntry); // JBPNote - need to update jalview datamodel.\r
-                                                       dsa.setSeqRef(sequence);\r
-                                                       dataset.addDataSetAnnotations(dsa);\r
-                                               } else {\r
-                                                       // todo: verify\r
-                                               }\r
-                                       }\r
-                               }\r
-                               \r
-                               if(jal.getDataset().getSequenceAt(i).getDBRef()!=null)\r
-                               {\r
-                                       java.util.Vector entries = jal.getDataset().getSequenceAt(i).getDBRef();\r
-                                       jalview.datamodel.DBRefEntry dbentry;\r
-                                       for(int db=0; db<entries.size(); db++)\r
-                                       {\r
-                                               dbentry = (jalview.datamodel.DBRefEntry)entries.elementAt(db);\r
-                                               dbref = (DbRef) getjv2vObj(dbentry);\r
-                                               if (dbref==null) {\r
-                                                       dbref = new DbRef();\r
-                                                       bindjvvobj(dbentry, dbref);\r
-                                                       dbref.setAccessionId( dbentry.getAccessionId() );\r
-                                                       dbref.setSource( dbentry.getSource() );\r
-                                                       dbref.setVersion( dbentry.getVersion() );\r
-                                                       dbref.setId("db"+(time++));\r
-                                                       sequence.addDbRef(dbref);\r
-                                               } else {\r
-                                                       // todo: verify\r
-                                               }\r
-                                       }\r
-                                       \r
-                               }\r
-                               \r
-                               /* not needed any more.\r
-                               if(jal.getDataset().getSequenceAt(i).getVamsasId()==null)\r
-                                       sequence.setId("ds" + (time++));\r
-                               else\r
-                                       sequence.setId(jal.getDataset().getSequenceAt(i).getVamsasId());\r
-                                */\r
-                       }\r
-                       // dataset.setProvenance(getVamsasProvenance(jal.getDataset().getProvenance()));\r
-                       //////////////////////////////////////////////\r
-                       \r
-                       \r
-                       //////////////////////////////////////////////\r
-                       //Save the Alignments\r
-                       \r
-                       Alignment alignment = (Alignment) getjv2vObj(jal);\r
-                       if (alignment==null) {\r
-                               alignment=new Alignment();\r
-                               bindjvvobj(jal, alignment);\r
-                               if (alignment.getProvenance()==null)\r
-                                       alignment.setProvenance(new Provenance());\r
-                               alignment.getProvenance().addEntry(provEntry);\r
-                               dataset.addAlignment(alignment);\r
-                               alignment.setGapChar(av.getGapCharacter() + "");\r
-                               AlignmentSequence alseq = null;\r
-                               for (int i = 0; i < jal.getHeight(); i++)\r
-                               {\r
-                                       alseq = new AlignmentSequence();\r
-                                       alseq.setSequence(\r
-                                                       jal.getSequenceAt(i).getSequence()\r
-                                       );\r
-                                       alseq.setName(jal.getSequenceAt(i).getName());\r
-                                       alseq.setStart(jal.getSequenceAt(i).getStart());\r
-                                       alseq.setEnd(jal.getSequenceAt(i).getEnd());\r
-                                       // how is this going to work ? find alseq in DataSet by..\r
-                                       if (nw) {\r
-                                               // order is conserved because we just created a new dataset.\r
-                                               alseq.setRefid(dataset.getSequence(i));\r
-                                       }\r
-                                       alignment.addAlignmentSequence(alseq);\r
-                               }\r
-                       } else {\r
-                               // todo: verify and update mutable alignment props.\r
-                       }\r
-                       //////////////////////////////////////////////\r
-                       \r
-                       \r
-                       \r
-                       //////////////////////////////////////////////\r
-                       //SAVE ANNOTATIONS\r
-                       if (jal.getAlignmentAnnotation() != null)\r
-                       {\r
-                               jalview.datamodel.AlignmentAnnotation[] aa = jal.getAlignmentAnnotation();\r
-                               \r
-                               for (int i = 0; i < aa.length; i++)\r
-                               {\r
-                                       if (aa[i].label.equals("Quality") ||\r
-                                                       aa[i].label.equals("Conservation") ||\r
-                                                       aa[i].label.equals("Consensus"))\r
-                                       {\r
-                                               continue;\r
-                                       }\r
-                                       AlignmentAnnotations an = (AlignmentAnnotations) getjv2vObj(aa[i]);\r
-                                       if (an==null) {\r
-                                               an = new AlignmentAnnotations();\r
-                                               an.setDescription(aa[i].description);\r
-                                               // JBPNote - have to deal with the many lines for one graph thing\r
-                                               if (aa[i].graph>0)\r
-                                                       an.setGraph(true); //aa[i].graph);\r
-                                               // JBPNote - store graphGroup in the Jalview specific bits.\r
-                                               an.setLabel(aa[i].label);\r
-                                               an.setProvenance(dummyProvenance());\r
-                                               \r
-                                               AnnotationElement ae;\r
-                                               \r
-                                               for (int a = 0; a < aa[i].annotations.length; a++)\r
-                                               {\r
-                                                       if ((aa[i] == null) || (aa[i].annotations[a] == null))\r
-                                                       {\r
-                                                               continue;\r
-                                                       }\r
-                                                       \r
-                                                       ae = new AnnotationElement();\r
-                                                       ae.setDescription(aa[i].annotations[a].description);\r
-                                                       ae.setDisplayCharacter(aa[i].annotations[a].displayCharacter);\r
-                                                       ae.setValue(aa[i].annotations[a].value);\r
-                                                       ae.setPosition(a);\r
-                                                       ae.setSecondaryStructure(aa[i].annotations[a].secondaryStructure +\r
-                                                       "");\r
-                                                       an.addAnnotationElement(ae);\r
-                                               }\r
-                                               \r
-                                               alignment.addAlignmentAnnotations(an);\r
-                                       } else {\r
-                                               // verify annotation - update (perhaps)\r
-                                       }\r
-                               }\r
-                       }\r
-                       ///////////////////////////////////////////////////////\r
-                       \r
-                       ////////////////////////////////////////////////\r
-                       ///SAVE THE TREES\r
-                       ///////////////////////////////////\r
-                       // FIND ANY ASSOCIATED TREES\r
-                       if (Desktop.desktop != null)\r
-                       {\r
-                               javax.swing.JInternalFrame[] frames = Desktop.desktop.getAllFrames();\r
-                               \r
-                               for (int t = 0; t < frames.length; t++)\r
-                               {\r
-                                       if (frames[t] instanceof TreePanel)\r
-                                       {\r
-                                               TreePanel tp = (TreePanel) frames[t];\r
-                                               \r
-                                               if (tp.getAlignment() == jal)\r
-                                               {\r
-                                                       // JBPNote - can recover alignment and Vamsas Alignment for an associated tree using the getjv2vObj()\r
-                                                       \r
-                                                       Tree tree = (Tree) getjv2vObj(tp);\r
-                                                       if (tree==null) {\r
-                                                               tree = new Tree();\r
-                                                               bindjvvobj(tp,tree);\r
-                                                               tree.setTitle(tp.getTitle());\r
-                                                               Newick newick = new Newick();\r
-                                                               newick.setContent( tp.getTree().toString() );\r
-                                                               newick.setTitle( tp.getTitle() );\r
-                                                               tree.addNewick( newick );\r
-                                                               tree.setProvenance(dummyProvenance());\r
-                                                               alignment.addTree(tree);\r
-                                                       } else {\r
-                                                               // verify any changes.\r
-                                                       }\r
-                                               }\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
-               \r
-               catch (Exception ex)\r
-               {\r
-                       ex.printStackTrace();\r
-               }\r
-               \r
-       }\r
-       \r
-       \r
-       \r
-       public void updateJalview()\r
-       {\r
-               VAMSAS root = cdoc.getVamsasRoots()[0];\r
-               try\r
-               {\r
-                       boolean newds=false;\r
-                       for (int _ds=0,_nds=root.getDataSetCount(); _ds<_nds; _ds++) {\r
-                               /////////////////////////////////////\r
-                               /////LOAD DATASET\r
-                               DataSet dataset = vamsas.getDataSet(_ds);\r
-                               int i, iSize = dataset.getSequenceCount();\r
-                               jalview.datamodel.Alignment jdataset = (jalview.datamodel.Alignment) vobj2jv(dataset);\r
-                               if (jdataset!=null) {\r
-                                       // recover\r
-                               } else {\r
-                                       newds=true;\r
-                                       // construct new dataset\r
-                                       jalview.datamodel.Sequence [] seqs = new jalview.datamodel.Sequence[iSize];\r
-                                       DbRef [] dbref;\r
-                                       for (i = 0; i < iSize ; i++)\r
-                                       {\r
-                                               seqs[i] = new jalview.datamodel.Sequence(\r
-                                                               dataset.getSequence(i).getName(),\r
-                                                               dataset.getSequence(i).getSequence(),\r
-                                                               dataset.getSequence(i).getStart(),\r
-                                                               dataset.getSequence(i).getEnd()  );\r
-                                               bindjvvobj(seqs[i], dataset.getSequence(i));\r
-                                               seqs[i].setVamsasId(dataset.getSequence(i).getVorbaId().getId());\r
-                                               \r
-                                               if (dataset.getDataSetAnnotations() != null)\r
-                                               {\r
-                                                       for (int dsa = 0; dsa < dataset.getDataSetAnnotationsCount(); dsa++)\r
-                                                       {\r
-                                                               // could be optimised\r
-                                                               if (dataset.getDataSetAnnotations(dsa).getSeqRef() ==\r
-                                                                       dataset.getSequence(i))\r
-                                                               {\r
-                                                                       jalview.datamodel.SequenceFeature sf;\r
-                                                                       seqs[i].addSequenceFeature(sf=new jalview.datamodel.\r
-                                                                                       SequenceFeature(\r
-                                                                                                       dataset.getDataSetAnnotations(dsa).getType(),\r
-                                                                                                       dataset.getDataSetAnnotations(dsa).getDescription(),\r
-                                                                                                       dataset.getDataSetAnnotations(dsa).getStatus(),\r
-                                                                                                       dataset.getDataSetAnnotations(dsa).getBegin(),\r
-                                                                                                       dataset.getDataSetAnnotations(dsa).getEnd(),\r
-                                                                                       "vamsas"));\r
-                                                                       bindjvvobj(sf, dataset.getDataSetAnnotations(dsa));\r
-                                                               }\r
-                                                       }\r
-                                               }\r
-                                               dbref = dataset.getSequence(i).getDbRef();\r
-                                               \r
-                                               if(dbref.length>0)\r
-                                               {\r
-                                                       for(int db=0; db<dbref.length; db++)\r
-                                                       {\r
-                                                               jalview.datamodel.DBRefEntry dbr;\r
-                                                               seqs[i].addDBRef(dbr= new jalview.datamodel.DBRefEntry\r
-                                                                               (\r
-                                                                                               dbref[db].getSource().toString(),\r
-                                                                                               dbref[db].getVersion().toString(),\r
-                                                                                               dbref[db].getAccessionId().toString()));\r
-                                                               bindjvvobj(dbr, dvref[db]);\r
-                                                       }\r
-                                               }\r
-                                       }\r
-                                       jdataset = jalview.datamodel.Alignment(seqs); // complete construction\r
-                                       bindjvvobj(jdataset, dataset);\r
-                               }\r
-                               ////////////////////////////////////////////////\r
-                       }\r
-                       for (int _al=0,_nal=dataset.getAlignmentCount(); _al<_nal; _al++) {\r
-                               //////////////////////////////////////\r
-                               ////LOAD ALIGNMENT\r
-                               Alignment alignment = dataset.getAlignment(_al);\r
-                               iSize = alignment.getAlignmentSequenceCount();\r
-                               jalview.datamodel.Alignment jal = getvObj2jv(alignment); \r
-                               if (jal!=null) {\r
-                                       // merge/update\r
-                               } else {\r
-                                       seqs = new jalview.datamodel.Sequence[iSize];\r
-                                       String id;\r
-                                       int j, jSize = jdataset.getHeight();\r
-                                       for (i = 0; i < iSize; i++)\r
-                                       {\r
-                                               seqs[i] = new jalview.datamodel.Sequence(\r
-                                                               alignment.getAlignmentSequence(i).getName(),\r
-                                                               alignment.getAlignmentSequence(i).getSequence(),\r
-                                                               alignment.getAlignmentSequence(i).getStart(),\r
-                                                               alignment.getAlignmentSequence(i).getEnd());\r
-                                               bindjvvobj(seqs[i], alignment.getAlignmentSequence(i));\r
-                                               id = ( (Sequence) alignment.getAlignmentSequence(i).getRefid()).getVorbaId().getId();\r
-                                               \r
-                                               for(j=0; j<jSize; j++)\r
-                                               {\r
-                                                       if(jdataset.getSequenceAt(j).getVamsasId().equals(id))\r
-                                                       {\r
-                                                               seqs[i].setDatasetSequence( jdataset.getSequenceAt(j) );\r
-                                                               break;\r
-                                                       }\r
-                                               }\r
-                                       }\r
-                                       jalview.datamodel.Alignment jal = new jalview.datamodel.Alignment(seqs);\r
-                                       bindjvvobj(jal, alignment);\r
-                                       jal.setDataset(jdataset);\r
-                                       /*jal.getDataset().setProvenance(\r
-                       getJalviewProvenance(dataset.getProvenance()));\r
-                       jal.setProvenance(\r
-                       getJalviewProvenance(alignment.getProvenance()));\r
-                                        */\r
-                                       //////////////////////////////////////////////\r
-                                       \r
-                                       \r
-                                       ////////////////////////////////////////////\r
-                                       ////LOAD ANNOTATIONS\r
-                                       ////////////////////\r
-                                       if (alignment.getAlignmentAnnotationsCount()>0)\r
-                                       {\r
-                                               AlignmentAnnotations[] an = alignment.getAlignmentAnnotations();\r
-                                               \r
-                                               for (j = 0; j < an.length; j++)\r
-                                               {\r
-                                                       boolean topaliBreakpoint = false;\r
-                                                       \r
-                                                       AnnotationElement[] ae = an[j].getAnnotationElement();\r
-                                                       jalview.datamodel.Annotation[] anot = getvObj2jv(ae);\r
-                                                       if (anot!=null) {\r
-                                                               // update or stay the same.\r
-                                                       } else {\r
-                                                               // new one\r
-                                                               anot=new jalview.datamodel.Annotation[jal.\r
-                                                                 getWidth()];\r
-                                                               bindjvvobj(anot, ae);\r
-                                try{\r
-                                       for (int aa = 0; aa < ae.length; aa++)\r
-                                       {\r
-                                               String dc = ae[aa].getDisplayCharacter()==null ? "dc" : ae[aa].getDisplayCharacter();\r
-                                               String desc = ae[aa].getDescription()==null ? "desc" : ae[aa].getDescription();\r
-                                               String ss = ae[aa].getSecondaryStructure()==null ? "ss" : ae[aa].getSecondaryStructure();\r
-                                               float value = ae[aa].getValue();\r
-                                               if(an[j].getGraph())\r
-                                               {\r
-                                                       dc = value+"";\r
-                                                       desc = value + "";\r
-                                               }\r
-                                               anot[ae[aa].getPosition()-1] = new jalview.datamodel.\r
-                                               Annotation(dc,desc,ss.charAt(0),value);\r
-                                                                       \r
-                                               if(desc.equals("TOPALi Partition Breakpoint"))\r
-                                                                               topaliBreakpoint = true;\r
-                                                                       \r
-                                       }\r
-                                }catch(Exception ex)\r
-                                {\r
-                                       ex.printStackTrace();\r
-                                       System.out.println("problem parsing annotations\n"+ex);}\r
-                                }\r
-                                                       jalview.datamodel.AlignmentAnnotation jaa = null;\r
-                                                       if (an[j].getGraph())\r
-                                                       {\r
-                                                               jaa = new jalview.datamodel.AlignmentAnnotation(an[j].getLabel(),\r
-                                                                               an[j].getDescription(), anot, 0, 0, 1);\r
-                                                       }\r
-                                                       else\r
-                                                       {\r
-                                                               String label = an[j].getLabel();\r
-                                                               if(topaliBreakpoint)\r
-                                                                       label = "TOPALi Partition Breakpoint";\r
-                                                               jaa = new jalview.datamodel.AlignmentAnnotation(label,\r
-                                                                               an[j].getDescription(), anot);\r
-                                                       }\r
-                                                       \r
-                                                       jal.addAnnotation(jaa);\r
-                                               }\r
-                                       }\r
-                                       /////////////////////////////////\r
-                                       \r
-                                       AlignFrame alignFrame = new AlignFrame(jal);\r
-                                       jalview.gui.Desktop.addInternalFrame(alignFrame, "VAMSAS LOAD",\r
-                                                       AlignFrame.NEW_WINDOW_WIDTH,\r
-                                                       AlignFrame.NEW_WINDOW_HEIGHT);\r
-                                       \r
-                                       //LOAD TREES\r
-                                       ///////////////////////////////////////\r
-                                       if (alignment.getTreeCount() > 0)\r
-                                       {\r
-                                               for (int t = 0; t < alignment.getTreeCount(); t++)\r
-                                               {\r
-                                                       Tree tree = alignment.getTree(t);\r
-                                                       \r
-                                                       alignFrame.ShowNewickTree(\r
-                                                                       new jalview.io.NewickFile(tree.getNewick(0).getContent()),\r
-                                                                       tree.getNewick(0).getTitle(),\r
-                                                                       600, 500,\r
-                                                                       t * 20 + 50, t * 20 + 50);\r
-                                               }\r
-                                       }\r
-                                       \r
-                                       \r
-                                       in.close();\r
-                                       jin.close();\r
-                               }\r
-                               catch (Exception ex)\r
-                               {\r
-                                       ex.printStackTrace();\r
-                               }\r
-                               \r
-                               \r
-                       }\r
-                       \r
-                       \r
-                       \r
-                       Provenance getVamsasProvenance(jalview.datamodel.Provenance jprov)\r
-                       {\r
-                               jalview.datamodel.ProvenanceEntry [] entries = null;\r
-                               \r
-                               \r
-                               Provenance prov = new Provenance();\r
-                               org.exolab.castor.types.Date date = new org.exolab.castor.types.Date(\r
-                                               new java.util.Date());\r
-                               Entry provEntry;\r
-                               \r
-                               if(jprov!=null)\r
-                               {\r
-                                       entries = jprov.getEntries();\r
-                                       for (int i = 0; i < entries.length; i++)\r
-                                       {\r
-                                               provEntry = new Entry();\r
-                                               try\r
-                                               {\r
-                                                       date = new org.exolab.castor.types.Date(entries[i].getDate());\r
-                                               }\r
-                                               catch (Exception ex)\r
-                                               {\r
-                                                       ex.printStackTrace();\r
-                                                       \r
-                                                       date = new org.exolab.castor.types.Date(entries[i].getDate());\r
-                                               }\r
-                                               provEntry.setDate(date);\r
-                                               provEntry.setUser(entries[i].getUser());\r
-                                               provEntry.setAction(entries[i].getAction());\r
-                                               prov.addEntry(provEntry);\r
-                                       }\r
-                               }\r
-                               else\r
-                               {\r
-                                       provEntry = new Entry();\r
-                                       provEntry.setDate(date);\r
-                                       provEntry.setUser(System.getProperty("user.name"));\r
-                                       provEntry.setAction("Jalview");\r
-                                       prov.addEntry(provEntry);\r
-                               }\r
-                               \r
-                               return prov;\r
-                       }\r
-                       \r
-                       jalview.datamodel.Provenance getJalviewProvenance(Provenance prov)\r
-                       {\r
-                               \r
-                               jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();\r
-                               for (int i = 0; i < prov.getEntryCount(); i++)\r
-                               {\r
-                                       jprov.addEntry(\r
-                                                       prov.getEntry(i).getUser(),\r
-                                                       prov.getEntry(i).getAction(),\r
-                                                       prov.getEntry(i).getDate().toDate(),\r
-                                                       prov.getEntry(i).getId()\r
-                                       );\r
-                               }\r
-                               \r
-                               return jprov;\r
-                       }\r
-                       \r
-                       Provenance dummyProvenance()\r
-                       {\r
-                               Provenance prov = new Provenance();\r
-                               Entry entry = new Entry();\r
-                               entry.setAction("Jalview");\r
-                               entry.setDate(new org.exolab.castor.types.Date(new java.util.Date()));\r
-                               entry.setUser(System.getProperty("user.name"));\r
-                               prov.addEntry(entry);\r
-                               return prov;\r
-                       }\r
-                       \r
-               }\r
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer
+ * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ */
+
+package jalview.io;
+
+import org.vamsas.client.Vobject;
+import org.vamsas.client.VorbaId;
+import org.vamsas.objects.core.*;
+import org.vamsas.objects.utils.DocumentStuff;
+import org.vamsas.test.simpleclient.ClientDoc;
+
+import jalview.bin.Cache;
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.AlignmentView;
+import jalview.datamodel.DBRefEntry;
+import jalview.datamodel.SequenceFeature;
+import jalview.datamodel.SequenceI;
+import jalview.gui.*;
+
+import java.io.*;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.IdentityHashMap;
+import java.util.Vector;
+import java.util.jar.*;
+import org.exolab.castor.xml.*;
+import org.exolab.castor.mapping.Mapping;
+
+/*
+ * 
+ * static {
+ * org.exolab.castor.util.LocalConfiguration.getInstance().getProperties().setProperty(
+ * "org.exolab.castor.serializer", "org.apache.xml.serialize.XMLSerilazizer"); }
+ * 
+ */
+
+public class VamsasDatastore {
+  Entry provEntry = null;
+
+  // AlignViewport av;
+
+  org.exolab.castor.types.Date date = new org.exolab.castor.types.Date(
+      new java.util.Date());
+
+  ClientDoc cdoc;
+
+  Hashtable vobj2jv;
+
+  Hashtable root2jv;
+
+  IdentityHashMap jv2vobj;
+
+  IdentityHashMap jv2root;
+
+  public VamsasDatastore(ClientDoc cdoc, Hashtable vobj2jv,
+      IdentityHashMap jv2vobj, Entry provEntry) {
+    this(cdoc, vobj2jv, jv2vobj, provEntry, new Hashtable(),
+        new IdentityHashMap());
+  }
+
+  public VamsasDatastore(ClientDoc cdoc, Hashtable vobj2jv,
+      IdentityHashMap jv2vobj, Entry provEntry, Hashtable root2jv,
+      IdentityHashMap jv2root) {
+    this.cdoc = cdoc;
+    this.vobj2jv = vobj2jv;
+    this.jv2vobj = jv2vobj;
+    this.root2jv = root2jv;
+    this.jv2root = jv2root;
+    this.provEntry = provEntry;
+  }
+
+  /*
+   * public void storeJalview(String file, AlignFrame af) { try { // 1. Load the
+   * mapping information from the file Mapping map = new
+   * Mapping(getClass().getClassLoader()); java.net.URL url =
+   * getClass().getResource("/jalview_mapping.xml"); map.loadMapping(url); // 2.
+   * Unmarshal the data // Unmarshaller unmar = new Unmarshaller();
+   * //unmar.setIgnoreExtraElements(true); //unmar.setMapping(map); // uni =
+   * (UniprotFile) unmar.unmarshal(new FileReader(file)); // 3. marshal the data
+   * with the total price back and print the XML in the console Marshaller
+   * marshaller = new Marshaller( new FileWriter(file) );
+   * 
+   * marshaller.setMapping(map); marshaller.marshal(af); } catch (Exception e) {
+   * e.printStackTrace(); } }
+   * 
+   * 
+   */
+  /**
+   * @return the Vobject bound to Jalview datamodel object
+   */
+  protected Vobject getjv2vObj(Object jvobj) {
+    if (jv2vobj.containsKey(jvobj))
+      return cdoc.getObject(((VorbaId) jv2vobj.get(jvobj)));
+    return null;
+  }
+
+  /**
+   * 
+   * @param vobj
+   * @return Jalview datamodel object bound to the vamsas document object
+   */
+  protected Object getvObj2jv(org.vamsas.client.Vobject vobj) {
+    VorbaId id = vobj.getVorbaId();
+    if (id == null)
+    {
+      id = cdoc.registerObject(vobj);
+      Cache.log
+      .debug("Registering new object and returning null for getvObj2jv");
+      return null;
+    }
+    if (vobj2jv.containsKey(vobj.getVorbaId()))
+      return vobj2jv.get(id);
+    return null;
+  }
+
+  protected void bindjvvobj(Object jvobj, org.vamsas.client.Vobject vobj) {
+    VorbaId id = vobj.getVorbaId();
+    if (id == null)
+    {
+      id = cdoc.registerObject(vobj);
+    }
+    if (vobj2jv.containsKey(vobj.getVorbaId()) || jv2vobj.containsKey(jvobj))
+    {
+      Cache.log.error("Duplicate object binding!");
+    }
+    else
+    {
+      vobj2jv.put(vobj.getVorbaId(), jvobj);// JBPNote - better implementing a
+      // hybrid invertible hash.
+      jv2vobj.put(jvobj, vobj.getVorbaId());
+    }
+  }
+
+  /**
+   * put the alignment viewed by AlignViewport into cdoc.
+   * 
+   * @param av
+   */
+  public void storeVAMSAS(AlignViewport av) {
+    try
+    {
+      jalview.datamodel.AlignmentI jal = av.getAlignment();
+      boolean nw = false;
+      VAMSAS root = null; // will be resolved based on Dataset Parent.
+      // /////////////////////////////////////////
+      // SAVE THE DATASET
+      if (jal.getDataset() == null)
+      {
+        Cache.log.warn("Creating new dataset for an alignment.");
+        jal.setDataset(null);
+      }
+      DataSet dataset = (DataSet) getjv2vObj(jal.getDataset());
+      if (dataset == null)
+      {
+        root = cdoc.getVamsasRoots()[0]; // default vamsas root for modifying.
+        dataset = new DataSet();
+        root.addDataSet(dataset);
+        bindjvvobj(jal.getDataset(), dataset);
+        dataset.setProvenance(dummyProvenance());
+        dataset.getProvenance().addEntry(provEntry);
+        nw = true;
+      }
+      else
+      {
+        root = (VAMSAS) dataset.getV_parent();
+      }
+      // update dataset
+      Sequence sequence;
+      DbRef dbref;
+      // set new dataset and alignment sequences based on alignment Nucleotide
+      // flag.
+      // this *will* break when alignment contains both nucleotide and amino
+      // acid sequences.
+      String dict = jal.isNucleotide() ? org.vamsas.objects.utils.SymbolDictionary.STANDARD_NA
+          : org.vamsas.objects.utils.SymbolDictionary.STANDARD_AA;
+      for (int i = 0; i < jal.getHeight(); i++)
+      {
+        SequenceI sq = jal.getSequenceAt(i).getDatasetSequence(); // only insert
+        // referenced
+        // sequences
+        // to dataset.
+        sequence = (Sequence) getjv2vObj(sq);
+        if (sequence == null)
+        {
+          sequence = new Sequence();
+          bindjvvobj(sq, sequence);
+          sq.setVamsasId(sequence.getVorbaId().getId());
+          sequence.setSequence(sq.getSequence());
+          sequence.setDictionary(dict);
+          sequence.setName(jal.getDataset().getSequenceAt(i).getName());
+          sequence.setStart(jal.getDataset().getSequenceAt(i).getStart());
+          sequence.setEnd(jal.getDataset().getSequenceAt(i).getEnd());
+          dataset.addSequence(sequence);
+        }
+        else
+        {
+          // verify principal attributes. and update any new
+          // features/references.
+          System.out.println("update dataset sequence object.");
+        }
+        if (sq.getSequenceFeatures() != null)
+        {
+          int sfSize = sq.getSequenceFeatures().length;
+
+          for (int sf = 0; sf < sfSize; sf++)
+          {
+            jalview.datamodel.SequenceFeature feature = (jalview.datamodel.SequenceFeature) sq
+            .getSequenceFeatures()[sf];
+
+            DataSetAnnotations dsa = (DataSetAnnotations) getjv2vObj(feature);
+            if (dsa == null)
+            {
+              dsa = (DataSetAnnotations) getDSAnnotationFromJalview(
+                  new DataSetAnnotations(), feature);
+              if (dsa.getProvenance() == null)
+              {
+                dsa.setProvenance(new Provenance());
+              }
+              addProvenance(dsa.getProvenance(), "created"); // JBPNote - need
+              // to update
+              dsa.setSeqRef(sequence);
+              bindjvvobj(feature, dsa);
+              dataset.addDataSetAnnotations(dsa);
+            }
+            else
+            {
+              // todo: verify and update dataset annotations for sequence
+              System.out.println("update dataset sequence annotations.");
+            }
+          }
+        }
+
+        if (sq.getDBRef() != null)
+        {
+          DBRefEntry[] entries = sq.getDBRef();
+          jalview.datamodel.DBRefEntry dbentry;
+          for (int db = 0; db < entries.length; db++)
+          {
+            dbentry = entries[db];
+            dbref = (DbRef) getjv2vObj(dbentry);
+            if (dbref == null)
+            {
+              dbref = new DbRef();
+              bindjvvobj(dbentry, dbref);
+              dbref.setAccessionId(dbentry.getAccessionId());
+              dbref.setSource(dbentry.getSource());
+              dbref.setVersion(dbentry.getVersion());
+              /*
+               * TODO: Maps are not yet supported by Jalview. Map vMap = new
+               * Map(); vMap.set dbref.addMap(vMap);
+               */
+              sequence.addDbRef(dbref);
+            }
+            else
+            {
+              // TODO: verify and update dbrefs in vamsas document
+              // there will be trouble when a dataset sequence is modified to
+              // contain more residues than were originally referenced - we must
+              // then make a number of dataset sequence entries
+              System.out
+              .println("update dataset sequence database references.");
+            }
+          }
+
+        }
+      }
+      // dataset.setProvenance(getVamsasProvenance(jal.getDataset().getProvenance()));
+      // ////////////////////////////////////////////
+
+      // ////////////////////////////////////////////
+      // Save the Alignments
+
+      Alignment alignment = (Alignment) getjv2vObj(av); // this is so we can get the alignviewport back 
+      if (alignment == null)
+      {
+        alignment = new Alignment();
+        bindjvvobj(jal, alignment);
+        if (alignment.getProvenance() == null)
+          alignment.setProvenance(new Provenance());
+        addProvenance(alignment.getProvenance(), "added"); // TODO: insert some
+        // sensible source
+        // here
+        dataset.addAlignment(alignment);
+        alignment.setGapChar(String.valueOf(av.getGapCharacter()));
+        AlignmentSequence alseq = null;
+        for (int i = 0; i < jal.getHeight(); i++)
+        {
+          alseq = new AlignmentSequence();
+          // TODO: VAMSAS: translate lowercase symbols to annotation ?
+          alseq.setSequence(jal.getSequenceAt(i).getSequence());
+          alseq.setName(jal.getSequenceAt(i).getName());
+          alseq.setStart(jal.getSequenceAt(i).getStart());
+          alseq.setEnd(jal.getSequenceAt(i).getEnd());
+          alseq.setRefid(getjv2vObj(jal.getSequenceAt(i).getDatasetSequence()));
+          alignment.addAlignmentSequence(alseq);
+          bindjvvobj(jal.getSequenceAt(i), alseq);
+        }
+      }
+      else
+      {
+        // todo: verify and update mutable alignment props.
+        if (alignment.getModifiable())
+        {
+          System.out.println("update alignment in document.");
+        }
+        else
+        {
+          System.out
+          .println("update edited alignment to new alignment in document.");
+        }
+      }
+      // ////////////////////////////////////////////
+      // SAVE Alignment Sequence Features
+      for (int i = 0, iSize = alignment.getAlignmentSequenceCount(); i < iSize; i++)
+      {
+        AlignmentSequence valseq;
+        SequenceI alseq = (SequenceI) getvObj2jv(valseq = alignment
+            .getAlignmentSequence(i));
+        if (alseq != null && alseq.getSequenceFeatures() != null)
+        {
+          jalview.datamodel.SequenceFeature[] features = alseq
+          .getSequenceFeatures();
+          for (int f = 0; f < features.length; f++)
+          {
+            if (features[f] != null)
+            {
+              AlignmentSequenceAnnotation valseqf = (AlignmentSequenceAnnotation) getjv2vObj(features[i]);
+              if (valseqf == null)
+              {
+
+                valseqf = (AlignmentSequenceAnnotation) getDSAnnotationFromJalview(
+                    new AlignmentSequenceAnnotation(), features[i]);
+                if (valseqf.getProvenance() == null)
+                {
+                  valseqf.setProvenance(new Provenance());
+                }
+                addProvenance(valseqf.getProvenance(), "created"); // JBPNote -
+                // need to
+                // update
+                bindjvvobj(features[i], valseqf);
+                valseq.addAlignmentSequenceAnnotation(valseqf);
+              }
+            }
+
+          }
+        }
+      }
+
+      // ////////////////////////////////////////////
+      // SAVE ANNOTATIONS
+      if (jal.getAlignmentAnnotation() != null)
+      {
+        jalview.datamodel.AlignmentAnnotation[] aa = jal
+        .getAlignmentAnnotation();
+        java.util.HashMap AlSeqMaps = new HashMap(); // stores int maps from
+        // alignment columns to
+        // sequence positions.
+        for (int i = 0; i < aa.length; i++)
+        {
+          if (aa[i] == null || isJalviewOnly(aa[i]))
+          {
+            continue;
+          }
+          if (aa[i].sequenceRef != null)
+          {
+            org.vamsas.objects.core.AlignmentSequence alsref = (org.vamsas.objects.core.AlignmentSequence) getjv2vObj(aa[i].sequenceRef);
+            org.vamsas.objects.core.AlignmentSequenceAnnotation an = (org.vamsas.objects.core.AlignmentSequenceAnnotation) getjv2vObj(aa[i]);
+            int[] gapMap = null;
+            if (AlSeqMaps.containsKey(aa[i].sequenceRef))
+            {
+              gapMap = (int[]) AlSeqMaps.get(aa[i].sequenceRef);
+            }
+            else
+            {
+              gapMap = new int[aa[i].sequenceRef.getLength()];
+              // map from alignment position to sequence position.
+              int[] sgapMap = aa[i].sequenceRef.gapMap();
+              for (int a = 0; a < sgapMap.length; a++)
+                gapMap[sgapMap[a]] = a;
+            }
+            if (an == null)
+            {
+              an = new org.vamsas.objects.core.AlignmentSequenceAnnotation();
+              alsref.addAlignmentSequenceAnnotation(an);
+              // LATER: much of this is verbatim from the alignmentAnnotation
+              // method below. suggests refactoring to make rangeAnnotation the
+              // base class
+              an.setDescription(aa[i].description);
+              if (aa[i].graph > 0)
+                an.setGraph(true); // aa[i].graph);
+              an.setLabel(aa[i].label);
+              an.setProvenance(dummyProvenance()); // get provenance as user
+              // created, or jnet, or
+              // something else.
+              an.setGroup(Integer.toString(aa[i].graphGroup)); // // JBPNote -
+              // originally we
+              // were going to
+              // store
+              // graphGroup in
+              // the Jalview
+              // specific
+              // bits.
+              AnnotationElement ae;
+              for (int a = 0; a < aa[i].annotations.length; a++)
+              {
+                if (aa[i].annotations[a] == null)
+                {
+                  continue;
+                }
+
+                ae = new AnnotationElement();
+                ae.setDescription(aa[i].annotations[a].description);
+                ae.addGlyph(new Glyph());
+                ae.getGlyph(0)
+                .setContent(aa[i].annotations[a].displayCharacter); // assume
+                // jax-b
+                // takes
+                // care
+                // of
+                // utf8
+                // translation
+                ae.addValue(aa[i].annotations[a].value);
+                ae.setPosition(gapMap[a]); // position w.r.t. AlignmentSequence
+                // symbols
+                if (aa[i].annotations[a].secondaryStructure != ' ')
+                {
+                  // we only write an annotation where it really exists.
+                  Glyph ss = new Glyph();
+                  ss
+                  .setDict(org.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE);
+                  ss.setContent(String
+                      .valueOf(aa[i].annotations[a].secondaryStructure));
+                  ae.addGlyph(ss);
+                }
+                an.addAnnotationElement(ae);
+              }
+            }
+            else
+            {
+              // update reference sequence Annotation
+              if (an.getModifiable())
+              {
+                // verify existing alignment sequence annotation is up to date
+                System.out.println("update alignment sequence annotation.");
+              }
+              else
+              {
+                // verify existing alignment sequence annotation is up to date
+                System.out
+                .println("make new alignment sequence annotation if modification has happened.");
+              }
+            }
+          }
+          else
+          {
+            // add Alignment Annotation
+            org.vamsas.objects.core.AlignmentAnnotation an = (org.vamsas.objects.core.AlignmentAnnotation) getjv2vObj(aa[i]);
+            if (an == null)
+            {
+              an = new org.vamsas.objects.core.AlignmentAnnotation();
+              an.setDescription(aa[i].description);
+              alignment.addAlignmentAnnotation(an);
+              if (aa[i].graph > 0)
+                an.setGraph(true); // aa[i].graph);
+              an.setLabel(aa[i].label);
+              an.setProvenance(dummyProvenance());
+              an.setGroup(Integer.toString(aa[i].graphGroup)); // // JBPNote -
+              // originally we
+              // were going to
+              // store
+              // graphGroup in
+              // the Jalview
+              // specific
+              // bits.
+
+              AnnotationElement ae;
+
+              for (int a = 0; a < aa[i].annotations.length; a++)
+              {
+                if ((aa[i] == null) || (aa[i].annotations[a] == null))
+                {
+                  continue;
+                }
+
+                ae = new AnnotationElement();
+                ae.setDescription(aa[i].annotations[a].description);
+                ae.addGlyph(new Glyph());
+                ae.getGlyph(0)
+                .setContent(aa[i].annotations[a].displayCharacter); // assume
+                // jax-b
+                // takes
+                // care
+                // of
+                // utf8
+                // translation
+                ae.addValue(aa[i].annotations[a].value);
+                ae.setPosition(a);
+                if (aa[i].annotations[a].secondaryStructure != ' ')
+                {
+                  Glyph ss = new Glyph();
+                  ss
+                  .setDict(org.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE);
+                  ss.setContent(String
+                      .valueOf(aa[i].annotations[a].secondaryStructure));
+                  ae.addGlyph(ss);
+                }
+                an.addAnnotationElement(ae);
+              }
+              if (aa[i].editable) {
+                //an.addProperty(newProperty("jalview:editable", null, "true"));
+                an.setModifiable(true);
+              }
+              if (aa[i].graph!=jalview.datamodel.AlignmentAnnotation.NO_GRAPH) {
+                an.setGraph(true);
+                an.setGroup(Integer.toString(aa[i].graphGroup));
+                an.addProperty(newProperty("jalview:graphType",null,
+                    ((aa[i].graph==jalview.datamodel.AlignmentAnnotation.BAR_GRAPH) ? "BAR_GRAPH" : "LINE_GRAPH")));
+
+                /** and on and on.. 
+                 vProperty=new Property();
+                  vProperty.setName("jalview:graphThreshhold");
+                  vProperty.setContent(aa[i].threshold);
+                 */
+
+              }
+            }
+            else
+            {
+              if (an.getModifiable())
+              {
+                // verify annotation - update (perhaps)
+                Cache.log.info("update alignment sequence annotation. not yet implemented.");
+              }
+              else
+              {
+                // verify annotation - update (perhaps)
+                Cache.log.info("updated alignment sequence annotation added.");
+              }
+            }
+          }
+        }
+      }
+      // /////////////////////////////////////////////////////
+
+      // //////////////////////////////////////////////
+      // /SAVE THE TREES
+      // /////////////////////////////////
+      // FIND ANY ASSOCIATED TREES
+      if (Desktop.desktop != null)
+      {
+        javax.swing.JInternalFrame[] frames = Desktop.desktop.getAllFrames();
+
+        for (int t = 0; t < frames.length; t++)
+        {
+          if (frames[t] instanceof TreePanel)
+          {
+            TreePanel tp = (TreePanel) frames[t];
+
+            if (tp.getAlignment() == jal)
+            {
+              // LATER: can recover alignment and Vamsas Alignment for an
+              // associated tree using the getjv2vObj() so we could pass through
+              // the tree list just once rather than many times
+
+              Tree tree = (Tree) getjv2vObj(tp);
+              if (tree == null)
+              {
+                tree = new Tree();
+                bindjvvobj(tp, tree);
+                tree.setTitle(tp.getTitle());
+                Newick newick = new Newick();
+                // TODO: translate sequenceI to leaf mappings to vamsas
+                // references - see tree specification in schema.
+                newick.setContent(tp.getTree().toString());
+                newick.setTitle(tp.getTitle());
+                tree.addNewick(newick);
+                tree.setProvenance(makeTreeProvenance(jal, tp));
+                alignment.addTree(tree);
+              }
+              else
+              {
+                if (tree.getModifiable())
+                {
+                  // verify any changes.
+                  System.out.println("Update tree in document.");
+                }
+                else
+                {
+                  System.out
+                  .println("Add modified tree as new tree in document.");
+                }
+              }
+            }
+          }
+        }
+      }
+      // Store Jalview specific stuff in the Jalview appData
+      // not implemented in the SimpleDoc interface.
+    }
+
+    catch (Exception ex)
+    {
+      ex.printStackTrace();
+    }
+
+  }
+
+  private Property newProperty(String name, String type, String content) {
+    Property vProperty=new Property();
+    vProperty.setName(name);
+    if (type!=null)
+      vProperty.setType(type);
+    vProperty.setContent(content);
+    return vProperty;
+  }
+
+  /**
+   * correctly create a RangeAnnotation from a jalview sequence feature
+   * 
+   * @param dsa
+   *          (typically DataSetAnnotations or AlignmentSequenceAnnotation)
+   * @param feature
+   *          (the feature to be mapped from)
+   * @return
+   */
+  private RangeAnnotation getDSAnnotationFromJalview(RangeAnnotation dsa,
+      SequenceFeature feature) {
+    dsa.setType(feature.getType());
+    Seg vSeg = new Seg();
+    vSeg.setStart(feature.getBegin());
+    vSeg.setEnd(feature.getEnd());
+    dsa.addSeg(vSeg);
+    dsa.setDescription(feature.getDescription());
+    dsa.setStatus(feature.getStatus());
+    if (feature.links != null && feature.links.size() > 0)
+    {
+      for (int i = 0, iSize = feature.links.size(); i < iSize; i++)
+      {
+        String link = (String) feature.links.elementAt(i);
+        int sep = link.indexOf('|');
+        if (sep > -1)
+        {
+          Link vLink = new Link();
+          if (sep > 0)
+            vLink.setContent(link.substring(0, sep - 1));
+          else
+            vLink.setContent("");
+          vLink.setHref(link.substring(sep + 1)); // TODO: validate href.
+          dsa.addLink(vLink);
+        }
+      }
+    }
+    dsa.setGroup(feature.getFeatureGroup());
+    return dsa;
+  }
+
+  /**
+   * correctly creates provenance for trees calculated on an alignment by
+   * jalview.
+   * 
+   * @param jal
+   * @param tp
+   * @return
+   */
+  private Provenance makeTreeProvenance(AlignmentI jal, TreePanel tp) {
+    Input vInput = new Input();
+    // LATER: check to see if tree input data is contained in this alignment -
+    // or just correctly resolve the tree's seqData to the correct alignment in
+    // the document.
+    vInput.setObjRef(getjv2vObj(jal));
+    Provenance prov = dummyProvenance();
+    if (tp.getTree().hasOriginalSequenceData())
+    {
+      prov.getEntry(0).addInput(vInput);
+      int ranges[] = tp.getTree().seqData.getVisibleContigs();
+      for (int r = 0; r < ranges.length; r += 2)
+      {
+        Seg visSeg = new Seg();
+        visSeg.setStart(ranges[r]);
+        visSeg.setEnd(ranges[r + 1]);
+        visSeg.setInclusive(true);
+        vInput.addSeg(visSeg);
+      }
+    }
+    return null;
+  }
+
+  /**
+   * 
+   * @param tp
+   * @return Object[] { AlignmentView, AlignmentI - reference alignment for
+   *         input }
+   */
+  private Object[] recoverInputData(Provenance tp) {
+    for (int pe = 0; pe < tp.getEntryCount(); pe++)
+    {
+      if (tp.getEntry(pe).getInputCount() > 0)
+      {
+        if (tp.getEntry(pe).getInputCount() > 1)
+          Cache.log.warn("Ignoring additional input spec in provenance entry "
+              + tp.getEntry(pe).toString());
+        // LATER: deal sensibly with multiple inputs.
+        Input vInput = tp.getEntry(pe).getInput(0);
+        if (vInput.getObjRef() instanceof org.vamsas.objects.core.Alignment)
+        {
+          // recover an AlignmentView for the input data
+          AlignmentI jal = (AlignmentI) getvObj2jv((org.vamsas.client.Vobject) vInput
+              .getObjRef());
+          jalview.datamodel.CigarArray view = jal.getCompactAlignment();
+          int from = 0, to = jal.getWidth();
+          for (int r = 0, s = vInput.getSegCount(); r < s; r++)
+          {
+            Seg visSeg = vInput.getSeg(r);
+            int se[] = getSegRange(visSeg,true); // jalview doesn't do bidirection alignments yet.
+            if (to > se[1])
+              Cache.log.warn("Ignoring invalid segment in InputData spec.");
+            else
+            {
+              if (se[0] > from)
+              {
+                view.deleteRange(from, se[0] - 1);
+              }
+              from = se[1] + 1;
+            }
+          }
+          if (from < to)
+          {
+            view.deleteRange(from, to); // final deletion - TODO: check off by
+            // one for to
+          }
+          return new Object[] { new AlignmentView(view), jal };
+        }
+      }
+    }
+    Cache.log.debug("Returning null for input data recovery from provenance.");
+    return null;
+  }
+
+  /**
+   * get start<end range of segment, adjusting for inclusivity flag and
+   * polarity.
+   *  
+   * @param visSeg
+   * @param ensureDirection when true - always ensure start is less than end.
+   * @return int[] { start, end, direction} where direction==1 for range running from end to start.
+   */
+  private int[] getSegRange(Seg visSeg, boolean ensureDirection) {
+    boolean incl = visSeg.getInclusive();
+    // adjust for inclusive flag.
+    int pol = (visSeg.getStart() <= visSeg.getEnd()) ? 1 : -1; // polarity of
+    // region.
+    int start = visSeg.getStart() + (incl ? 0 : pol);
+    int end = visSeg.getEnd() + (incl ? -pol : 0);
+    if (ensureDirection && pol==-1)
+    {
+      // jalview doesn't deal with inverted ranges, yet.
+      int t = end;
+      end = start;
+      start = t;
+    }
+    return new int[] { start, end, pol<0 ? 1 : 0 };
+  }
+
+  /**
+   * 
+   * @param annotation
+   * @return true if annotation is not to be stored in document
+   */
+  private boolean isJalviewOnly(AlignmentAnnotation annotation) {
+    return annotation.label.equals("Quality")
+    || annotation.label.equals("Conservation")
+    || annotation.label.equals("Consensus");
+  }
+  /**
+   * This will return the first AlignFrame viewing AlignViewport av.
+   * It will break if there are more than one AlignFrames viewing a particular av.
+   * This also shouldn't be in the io package.
+   * @param av
+   * @return alignFrame for av
+   */
+  public AlignFrame getAlignFrameFor(AlignViewport av) {
+    if (Desktop.desktop != null)
+    {
+      javax.swing.JInternalFrame[] frames = Desktop.desktop.getAllFrames();
+
+      for (int t = 0; t < frames.length; t++)
+      {
+        if (frames[t] instanceof AlignFrame) {
+          if (((AlignFrame) frames[t]).getViewport()==av)
+          return (AlignFrame) frames[t];
+        }
+      }
+    }
+    return null;
+  }
+  public void updateToJalview() {
+    VAMSAS _roots[] = cdoc.getVamsasRoots();
+
+    for (int _root = 0; _root<_roots.length; _root++) {
+      VAMSAS root = _roots[_root];
+      boolean newds=false;
+      for (int _ds=0,_nds=root.getDataSetCount(); _ds<_nds; _ds++) {
+        // ///////////////////////////////////
+        // ///LOAD DATASET
+        DataSet dataset = root.getDataSet(_ds);
+        int i, iSize = dataset.getSequenceCount();
+        Vector dsseqs;
+        jalview.datamodel.Alignment jdataset = (jalview.datamodel.Alignment) getvObj2jv(dataset);
+        int jremain=0;
+        if (jdataset==null) {
+          Cache.log.debug("Initialising new jalview dataset fields");
+          newds=true;
+          dsseqs=new Vector();
+        } else {
+          Cache.log.debug("Update jalview dataset from vamsas.");
+          jremain=jdataset.getHeight();
+          dsseqs=jdataset.getSequences();
+        }
+
+        // TODO: test sequence merging - we preserve existing non vamsas
+        // sequences but add in any new vamsas ones, and don't yet update any
+        // sequence attributes
+        for (i = 0; i < iSize ; i++)
+        {
+          Sequence vdseq = dataset.getSequence(i);
+          jalview.datamodel.SequenceI dsseq = (SequenceI) getvObj2jv(vdseq);
+          if (dsseq!=null) {
+            if (!dsseq.getSequence().equals(vdseq.getSequence()))
+              throw new Error("Broken! - mismatch of dataset sequence and jalview internal dataset sequence.");
+            jremain--;
+          } else {
+            dsseq = new jalview.datamodel.Sequence(
+                dataset.getSequence(i).getName(),
+                dataset.getSequence(i).getSequence(),
+                dataset.getSequence(i).getStart(),
+                dataset.getSequence(i).getEnd()  );
+            bindjvvobj(dsseq, dataset.getSequence(i));
+            dsseq.setVamsasId(dataset.getSequence(i).getVorbaId().getId());
+            dsseqs.add(dsseq);
+          }
+          if (vdseq.getDbRefCount()>0) {
+            DbRef [] dbref = vdseq.getDbRef();
+            for(int db=0; db<dbref.length; db++)
+            {
+              jalview.datamodel.DBRefEntry dbr=(jalview.datamodel.DBRefEntry) getvObj2jv(dbref[db]);
+              if (dbr==null) {
+                // add new dbref
+                dsseq.addDBRef(dbr= new jalview.datamodel.DBRefEntry
+                    (
+                        dbref[db].getSource().toString(),
+                        dbref[db].getVersion().toString(),
+                        dbref[db].getAccessionId().toString()));
+                bindjvvobj(dbr, dbref[db]);
+              }
+            }
+          }
+        }
+
+        if (newds) {
+          SequenceI[] seqs = new SequenceI[dsseqs.size()];
+          for (i=0,iSize=dsseqs.size(); i<iSize; i++) {
+            seqs[i]=(SequenceI) dsseqs.elementAt(i);
+            dsseqs.setElementAt(null, i);
+          }
+          jdataset = new jalview.datamodel.Alignment(seqs);
+          Cache.log.debug("New vamsas dataset imported into jalview.");
+          bindjvvobj(jdataset, dataset);
+        }
+        // ////////
+        // add any new dataset sequence feature annotations
+        if (dataset.getDataSetAnnotations() != null) {
+          for (int dsa = 0; dsa < dataset.getDataSetAnnotationsCount(); dsa++) {
+            DataSetAnnotations dseta=dataset.getDataSetAnnotations(dsa);
+            SequenceI dsSeq=(SequenceI) getvObj2jv((Vobject) dseta.getSeqRef());
+            if (dsSeq==null) {
+              jalview.bin.Cache.log.warn("Couldn't resolve jalview sequenceI for dataset object reference "+((Vobject)dataset.getDataSetAnnotations(dsa).getSeqRef()).getVorbaId().getId());
+            } else {
+              if (dseta.getAnnotationElementCount()==0) {
+                jalview.datamodel.SequenceFeature sf=(jalview.datamodel.SequenceFeature) getvObj2jv(dseta);
+                if (sf==null) {
+                  dsSeq.addSequenceFeature(sf=getJalviewSeqFeature(dseta));
+                  bindjvvobj(sf, dseta);
+                }
+              } else {
+                // TODO: deal with alignmentAnnotation style annotation
+                // appearing on dataset sequences.
+                // JBPNote: we could just add them to all alignments but
+                // that may complicate cross references in the jalview
+                // datamodel
+                Cache.log.warn("Ignoring dataset annotation with annotationElements. Not yet supported in jalview.");
+              }
+            }
+          }
+        }
+
+        if (dataset.getAlignmentCount()>0) {
+          // LOAD ALIGNMENTS from DATASET 
+
+          for (int al=0,nal=dataset.getAlignmentCount(); al<nal; al++) {
+            org.vamsas.objects.core.Alignment alignment = dataset.getAlignment(al);
+            AlignViewport av = (AlignViewport) getvObj2jv(alignment);
+            jalview.datamodel.AlignmentI jal=null;
+            if (av!=null)
+              jal = av.getAlignment(); 
+            iSize = alignment.getAlignmentSequenceCount();
+            boolean newal=(jal==null) ? true : false;
+            Vector newasAnnots=new Vector();
+            char gapChar=' '; // default for new alignments read in from the document
+            if (jal!=null) {
+              dsseqs=jal.getSequences(); // for merge/update
+              gapChar=jal.getGapCharacter();
+            } else {
+              dsseqs=new Vector();
+            }
+            char valGapchar=alignment.getGapChar().charAt(0);
+            for (i = 0; i < iSize; i++)
+            {
+              AlignmentSequence valseq = alignment.getAlignmentSequence(i);
+              jalview.datamodel.SequenceI alseq = (SequenceI) getvObj2jv(valseq);
+              if (alseq!=null) {
+                //TODO: upperCase/LowerCase situation here ? do we allow it ?
+                //if (!alseq.getSequence().equals(valseq.getSequence())) {
+                // throw new Error("Broken! - mismatch of dataset sequence and jalview internal dataset sequence.");
+                if (Cache.log.isDebugEnabled())
+                  Cache.log.debug("Updating apparently edited sequence "+alseq.getName());
+                // this might go *horribly* wrong
+                alseq.setSequence(new String(valseq.getSequence()).replace(valGapchar, gapChar));
+                jremain--;
+              } else {
+                alseq = new jalview.datamodel.Sequence(
+                    valseq.getName(),
+                    valseq.getSequence().replace(valGapchar, gapChar),
+                    valseq.getStart(),
+                    valseq.getEnd()  );
+                alseq.setDatasetSequence((SequenceI)getvObj2jv((Vobject)valseq.getRefid())); // exceptions if AlignemntSequence reference isn't a simple SequenceI
+                bindjvvobj(alseq, valseq);
+                alseq.setVamsasId(valseq.getVorbaId().getId());
+                dsseqs.add(alseq);
+              }
+              if (valseq.getAlignmentSequenceAnnotationCount()>0) {
+                AlignmentSequenceAnnotation[] vasannot=valseq.getAlignmentSequenceAnnotation();
+                for (int a=0; a<vasannot.length; a++) {
+                  jalview.datamodel.AlignmentAnnotation asa = (jalview.datamodel.AlignmentAnnotation) getvObj2jv(vasannot[a]); // TODO: 1:many jalview alignment sequence annotations
+                  if (asa==null) {
+                    int se[] = getBounds(vasannot[a]);
+                    asa = getjAlignmentAnnotation(jal, vasannot[a]);
+                    asa.createSequenceMapping(alseq, se[0], false); // TODO: verify that positions in alseqAnnotation correspond to ungapped residue positions.
+                    bindjvvobj(asa, vasannot[a]);
+                    newasAnnots.add(asa);
+                  } else {
+                    // update existing annotation - can do this in place
+                    if (vasannot[a].getModifiable()) {
+                      Cache.log.info("UNIMPLEMENTED: not recovering user modifiable sequence alignment annotation");
+                      // TODO: should at least replace with new one - otherwise things will break
+                      // basically do this:
+                      // int se[] = getBounds(vasannot[a]);
+                      // asa.update(getjAlignmentAnnotation(jal, vasannot[a])); //  update from another annotation object in place.
+                      // asa.createSequenceMapping(alseq, se[0], false); 
+                      
+                    }
+                  }
+                }
+              }
+            }
+            if (jal==null) {
+              SequenceI[] seqs = new SequenceI[dsseqs.size()];
+              for (i=0,iSize=dsseqs.size(); i<iSize; i++) {
+                seqs[i]=(SequenceI) dsseqs.elementAt(i);
+                dsseqs.setElementAt(null, i);
+              }
+              jal = new jalview.datamodel.Alignment(seqs);
+              Cache.log.debug("New vamsas alignment imported into jalview "+alignment.getVorbaId().getId());
+              bindjvvobj(jal, alignment);
+              jal.setDataset(jdataset);
+            }
+            if (newasAnnots!=null && newasAnnots.size()>0) {
+              // Add the new sequence annotations in to the alignment.
+              for (int an=0,anSize=newasAnnots.size(); an<anSize; an++) {
+                jal.addAnnotation((AlignmentAnnotation) newasAnnots.elementAt(an));
+                // TODO: check if anything has to be done - like calling adjustForAlignment or something.
+                newasAnnots.setElementAt(null, an);
+              }
+              newasAnnots=null;
+            }
+            // //////////////////////////////////////////
+            // //LOAD ANNOTATIONS FOR THE ALIGNMENT
+            // ////////////////////////////////////
+            if (alignment.getAlignmentAnnotationCount()>0)
+            {
+              org.vamsas.objects.core.AlignmentAnnotation[] an = alignment.getAlignmentAnnotation();
+
+              for (int j = 0; j < an.length; j++)
+              {
+                jalview.datamodel.AlignmentAnnotation jan=(jalview.datamodel.AlignmentAnnotation) getvObj2jv(an[j]);
+                if (jan!=null) {
+                  // update or stay the same.
+                  // TODO: should at least replace with a new one - otherwise things will break
+                  // basically do this:
+                  // jan.update(getjAlignmentAnnotation(jal, an[a])); //  update from another annotation object in place.
+                  
+                  Cache.log.debug("update from vamsas alignment annotation to existing jalview alignment annotation.");
+                  if (an[i].getModifiable()) {
+                    // TODO: user defined annotation is totally mutable... - so load it up or throw away if locally edited.
+                    Cache.log.info("NOT IMPLEMENTED - Recovering user-modifiable annotation - yet...");
+                  }
+                  // TODO: compare annotation element rows
+                  // TODO: compare props.
+                } else {
+                  jan = getjAlignmentAnnotation(jal, an[j]);
+                  jal.addAnnotation(jan);
+                  bindjvvobj(jan, an[j]);
+                }
+              }
+            }
+            AlignFrame alignFrame;
+            if (av==null) {
+                // ///////////////////////////////
+              // construct alignment view
+              alignFrame = new AlignFrame(jal);
+              av=alignFrame.getViewport();
+              // TODO: automatically create meaningful title for a vamsas alignment using its provenance.
+              jalview.gui.Desktop.addInternalFrame(alignFrame, alignment.getProvenance().getEntry(alignment.getProvenance().getEntryCount()-1).toString(),
+                  AlignFrame.NEW_WINDOW_WIDTH,
+                  AlignFrame.NEW_WINDOW_HEIGHT);
+            } else {
+              // find the alignFrame for jal.
+              // TODO: fix this so we retrieve the alignFrame handing av *directly*
+              alignFrame=getAlignFrameFor(av);
+            }
+            // LOAD TREES
+            // /////////////////////////////////////
+            if (alignment.getTreeCount() > 0)
+            {
+              
+              for (int t = 0; t < alignment.getTreeCount(); t++)
+              {
+                Tree tree = alignment.getTree(t);
+                TreePanel tp=(TreePanel) getvObj2jv(tree);
+                if (tp!=null) {
+                  Cache.log.info("Update from vamsas document to alignment associated tree not implemented yet.");
+                } else {
+                  // make a new tree
+                  Object[] idata = this.recoverInputData(tree.getProvenance());
+                  try {
+                    
+                    tp = alignFrame.ShowNewickTree(
+                        new jalview.io.NewickFile(tree.getNewick(0).getContent()),
+                        tree.getNewick(0).getTitle(),
+                        600, 500,
+                        t * 20 + 50, t * 20 + 50);
+                    bindjvvobj(tp, tree);
+                    if (idata!=null) {
+                      // add it to tp.
+                      check jalview_2xml that it isn't an out of date version.
+                    }
+                        
+                  } catch (Exception e) {
+                    Cache.log.warn("Problems parsing treefile '"+tree.getNewick(0).getContent()+"'",e);
+                  }
+                }
+              }
+            }
+
+          }
+        }
+      }
+    }
+  }
+  // bitfields - should be a template in j1.5
+  private static int HASSECSTR=0;
+  private static int HASVALS=1;
+  private static int HASHPHOB=2;
+  private static int HASDC=3;
+  private static int HASDESCSTR=4;
+  private static int HASTWOSTATE=5; // not used yet.
+  /**
+   * parses the AnnotationElements - if they exist - into jalview.datamodel.Annotation[] rows
+   * Two annotation rows are made if there are distinct annotation for both at 'pos' and 'after pos' at any particular site.
+   * @param annotation
+   * @return { boolean[static int constants ], int[ae.length] - map to annotated object frame, jalview.datamodel.Annotation[], jalview.datamodel.Annotation[] (after)}
+   */
+  private Object[] parseRangeAnnotation(org.vamsas.objects.core.RangeAnnotation annotation) {
+    // set these attributes by looking in the annotation to decide what kind of alignment annotation rows will be made
+    // TODO: potentially we might make several annotation rows from one vamsas alignment annotation. the jv2Vobj binding mechanism
+    // may not quite cope with this (without binding an array of annotations to a vamsas alignment annotation)
+    // summary flags saying what we found over the set of annotation rows.
+    boolean[] AeContent = new boolean[] { false, false, false, false, false};
+    int[] rangeMap = getMapping(annotation);
+    jalview.datamodel.Annotation[][] anot=new jalview.datamodel.Annotation[][] { 
+        new jalview.datamodel.Annotation[rangeMap.length],
+        new jalview.datamodel.Annotation[rangeMap.length]
+    };
+    boolean mergeable=true; //false  if 'after positions cant be placed on same annotation row as positions. 
+
+    if (annotation.getAnnotationElementCount()>0) {
+      AnnotationElement ae[] = annotation.getAnnotationElement();
+      for (int aa = 0; aa < ae.length; aa++)
+      {
+        int pos = ae[aa].getPosition();
+        if (pos>=0 && pos<anot.length) {
+          int row=ae[aa].getAfter()?1:0;
+          if (anot[row][pos]!=null) {
+            // only time this should happen is if the After flag is set.
+            Cache.log.debug("Ignoring duplicate annotation site at "+pos);
+            continue;
+          }
+          if (anot[1-row][pos]!=null)
+            mergeable=false;
+          String desc = "";
+          if (ae[aa].getDescription()!=null) {
+            desc = ae[aa].getDescription();
+            if (desc.length()>0) {
+              // have imported valid description string
+              AeContent[HASDESCSTR]=true;
+            }
+          }
+          String dc = null;//ae[aa].getDisplayCharacter()==null ? "dc" : ae[aa].getDisplayCharacter();
+          String ss = null;//ae[aa].getSecondaryStructure()==null ? "ss" : ae[aa].getSecondaryStructure();
+          java.awt.Color colour = null;
+          if (ae[aa].getGlyphCount()>0) {
+            Glyph[] glyphs = ae[aa].getGlyph();
+            for (int g=0; g<glyphs.length; g++) {
+              if (glyphs[g].getDict()==org.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE) {
+                ss=glyphs[g].getContent();
+                AeContent[HASSECSTR]=true;
+              } else if (glyphs[g].getDict()==org.vamsas.objects.utils.GlyphDictionary.PROTEIN_HD_HYDRO) {
+                Cache.log.debug("ignoring hydrophobicity glyph marker.");
+                AeContent[HASHPHOB]=true;
+                char c=(dc=glyphs[g].getContent()).charAt(0);
+                // dc may get overwritten - but we still set the colour.
+                colour = new java.awt.Color(c=='+'?255:0,c=='.'?255:0,c=='-'?255:0);
+
+              } else if (glyphs[g].getDict()==org.vamsas.objects.utils.GlyphDictionary.DEFAULT) {
+                dc = glyphs[g].getContent();
+                AeContent[HASDC]=true;
+              } else {
+                Cache.log.debug("Ignoring unknown glyph type "+glyphs[g].getDict());
+              }
+            }
+          }
+          float val=0;
+          if (ae[aa].getValueCount()>0) {
+            AeContent[HASVALS]=true;
+            if (ae[aa].getValueCount()>1) {
+              Cache.log.warn("ignoring additional "+(ae[aa].getValueCount()-1)+"values in annotation element.");
+            }
+            val = ae[aa].getValue(0);
+          }
+          if (colour!=null) {
+            anot[row][pos]=new jalview.datamodel.Annotation(desc, (dc!=null) ? dc : "", (ss!=null)?ss.charAt(0):' ', val);
+          } else {
+            anot[row][pos]=new jalview.datamodel.Annotation(desc, (dc!=null) ? dc : "", (ss!=null)?ss.charAt(0):' ', val, colour);
+          }
+        } else {
+          Cache.log.warn("Ignoring out of bound annotation element "+aa+" in "+annotation.getVorbaId().getId());
+        }
+      }
+      // decide on how many annotation rows are needed.
+      if (mergeable) {
+        for (int i=0; i<anot[0].length;i++) {
+          if (anot[1][i]!=null) {
+            anot[0][i] = anot[1][i];
+            anot[0][i].description = anot[0][i].description+" (after)";
+            AeContent[HASDESCSTR]=true; // we have valid description string data
+            anot[1][i] = null;
+          }
+        }
+        anot[1] = null;
+      } else {
+        for (int i=0; i<anot[0].length;i++) {
+          anot[1][i].description = anot[1][i].description+" (after)";
+        }
+      }
+      return new Object[] { AeContent, rangeMap, anot[0], anot[1] };
+    } else {
+      // no annotations to parse.
+    }
+    return null;
+  }
+  /**
+   * @param jal the jalview alignment to which the annotation will be attached (ideally - freshly updated from corresponding vamsas alignment)
+   * @param annotation
+   * @return unbound jalview alignment annotation object.
+   */
+  private jalview.datamodel.AlignmentAnnotation getjAlignmentAnnotation(jalview.datamodel.AlignmentI jal, org.vamsas.objects.core.RangeAnnotation annotation) {
+    jalview.datamodel.AlignmentAnnotation jan =null;
+    if (annotation==null)
+      return null;
+    /*int se[] = getBounds(annotation);
+    if (se==null)
+      se=new int[] {0,jal.getWidth()-1};
+     */
+    Object[] parsedRangeAnnotation = parseRangeAnnotation(annotation);
+
+    if (parsedRangeAnnotation!=null) {
+      if (parsedRangeAnnotation[3]!=null) {
+        Cache.log.warn("Ignoring 'After' annotation row in "+annotation.getVorbaId());
+      }
+      jalview.datamodel.Annotation[] arow = (jalview.datamodel.Annotation[]) parsedRangeAnnotation[2];
+      boolean[] has=(boolean[])parsedRangeAnnotation[0];
+      // VAMSAS: getGraph is only on derived annotation for alignments - in this way its 'odd' - there is already an existing TODO about removing this flag as being redundant
+      if ((annotation.getClass().equals(org.vamsas.objects.core.AlignmentAnnotation.class) && ((org.vamsas.objects.core.AlignmentAnnotation)annotation).getGraph())
+          || (annotation.getClass().equals(AlignmentSequenceAnnotation.class) && ((org.vamsas.objects.core.AlignmentAnnotation)annotation).getGraph())) {
+        // make bounds and automatic description strings for jalview user's benefit (these shouldn't be written back to vamsas document)
+        boolean first=true;
+        float min=0,max=1;
+
+        for (int i=0;i<arow.length; i++) {
+          if (arow[i]!=null) {
+            // check range - shouldn't we have a min and max property in the annotation object ?
+            if (first) { min=max=arow[i].value; first=false;}
+            else { if (arow[i].value<min) { min=arow[i].value; }
+            else if (arow[i].value>max) { max=arow[i].value; }
+            }
+            // make tooltip and display char value
+            if (!has[HASDESCSTR]) arow[i].description = arow[i].value + "";
+            if (!has[HASDC]) arow[i].displayCharacter=arow[i].value+"";
+          }
+        }
+        int type=jalview.datamodel.AlignmentAnnotation.LINE_GRAPH;
+        if (has[HASHPHOB]) {
+          type = jalview.datamodel.AlignmentAnnotation.BAR_GRAPH;
+        }
+        jan = new jalview.datamodel.AlignmentAnnotation(annotation.getLabel(), annotation.getDescription(), arow, min, max, type);
+      } else {
+        jan = new jalview.datamodel.AlignmentAnnotation(annotation.getLabel(), annotation.getDescription(), arow);
+      }
+      if (annotation.getLinkCount()>0) {
+        Cache.log.warn("Ignoring "+annotation.getLinkCount()+"links added to AlignmentAnnotation.");
+      }
+      if (annotation.getModifiable()) {
+        jan.editable=true;
+      }
+
+      if (annotation.getPropertyCount()>0) {
+        // look for special jalview properties
+        org.vamsas.objects.core.Property[] props=annotation.getProperty();
+        for (int p=0;p<props.length; p++) {
+          if (props[p].getName().equalsIgnoreCase("jalview:graphType")) {
+            try { 
+              // probably a jalview annotation graph so recover the visualization hints.
+              jan.graph = jalview.datamodel.AlignmentAnnotation.getGraphValueFromString(props[p].getContent());
+            } catch (Exception e) {
+              Cache.log.debug("Invalid graph type value in jalview:graphType property.");
+            }
+            try {
+              if (annotation.getGroup()!=null && annotation.getGroup().length()>0)
+                jan.graphGroup = Integer.parseInt(annotation.getGroup());
+            } catch (Exception e) {
+              Cache.log.info("UNIMPLEMENTED : Couldn't parse non-integer group value for setting graphGroup correctly.");
+            }
+          }
+        }
+      }
+
+      return jan;
+
+    } else {
+      Cache.log.debug("(Ignoring so ... not) Inserting empty annotation row for whole-alignment annotation.");
+    }
+
+    return null;
+  }
+
+  private SequenceFeature getJalviewSeqFeature(RangeAnnotation dseta) {
+    int[] se = getBounds(dseta);
+    SequenceFeature sf = new jalview.datamodel.SequenceFeature(dseta.getType(),
+        dseta.getDescription(), dseta.getStatus(), se[0], se[1], dseta
+        .getGroup());
+    if (dseta.getLinkCount() > 0)
+    {
+      Link[] links = dseta.getLink();
+      for (int i = 0; i < links.length; i++)
+      {
+        sf.addLink(links[i].getContent() + "|" + links[i].getHref());
+      }
+    }
+    return sf;
+  }
+
+  /**
+   * get real bounds of a RangeType's specification. start and end are an
+   * inclusive range within which all segments and positions lie.
+   * TODO: refactor to vamsas utils
+   * @param dseta
+   * @return int[] { start, end}
+   */
+  private int[] getBounds(RangeType dseta) {
+    if (dseta != null)
+    {
+      int[] se = null;
+      if (dseta.getSegCount()>0 && dseta.getPosCount()>0)
+        throw new Error("Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
+      if (dseta.getSegCount() > 0)
+      {
+        se = getSegRange(dseta.getSeg(0),true);
+        for (int s = 1, sSize = dseta.getSegCount(); s < sSize; s++)
+        {
+          int nse[] = getSegRange(dseta.getSeg(s), true);
+          if (se[0] > nse[0])
+            se[0] = nse[0];
+          if (se[1] < nse[1])
+            se[1] = nse[1];
+        }
+      }
+      if (dseta.getPosCount() > 0)
+      {
+        // could do a polarity for pos range too. and pass back indication of discontinuities.
+        int pos = dseta.getPos(0).getI();
+        se = new int[] { pos, pos };
+        for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
+        {
+          pos = dseta.getPos(p).getI();
+          if (se[0] > pos)
+            se[0] = pos;
+          if (se[1] < pos)
+            se[1] = pos;
+        }
+      }
+      return se;
+    }
+    return null;
+  }
+  /**
+   * map from a rangeType's internal frame to the referenced object's coordinate frame.
+   * @param dseta
+   * @return int [] { ref(pos)...} for all pos in rangeType's frame.
+   */
+  private int[] getMapping(RangeType dseta) {
+    Vector posList=new Vector();
+    if (dseta != null)
+    {
+      int[] se = null;
+      if (dseta.getSegCount()>0 && dseta.getPosCount()>0)
+        throw new Error("Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
+      if (dseta.getSegCount() > 0)
+      {
+        for (int s = 0, sSize = dseta.getSegCount(); s < sSize; s++)
+        {
+          se = getSegRange(dseta.getSeg(s), false);
+          for (int p=se[se[2]]; p!=se[1-se[2]]; p+=se[2]==0 ? 1 : -1 ) {
+            posList.add(Integer.valueOf(p));
+          }
+        }
+      } 
+      else if (dseta.getPosCount() > 0)
+      {
+        int pos = dseta.getPos(0).getI();
+
+        for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
+        {
+          pos = dseta.getPos(p).getI();
+          posList.add(Integer.valueOf(pos));
+        }
+      }
+    }
+    if (posList!=null && posList.size()>0) {
+      int[] range=new int[posList.size()];
+      for (int i=0; i<range.length; i++)
+        range[i] = ((Integer)posList.elementAt(i)).intValue();
+      posList.clear();
+      return range;
+    }
+    return null;
+  }
+/* not needed now. 
+ * Provenance getVamsasProvenance(jalview.datamodel.Provenance jprov) {
+    jalview.datamodel.ProvenanceEntry[] entries = null;
+    // TODO: fix App and Action here.
+    Provenance prov = new Provenance();
+    org.exolab.castor.types.Date date = new org.exolab.castor.types.Date(
+        new java.util.Date());
+    Entry provEntry;
+
+    if (jprov != null)
+    {
+      entries = jprov.getEntries();
+      for (int i = 0; i < entries.length; i++)
+      {
+        provEntry = new Entry();
+        try
+        {
+          date = new org.exolab.castor.types.Date(entries[i].getDate());
+        } catch (Exception ex)
+        {
+          ex.printStackTrace();
+
+          date = new org.exolab.castor.types.Date(entries[i].getDate());
+        }
+        provEntry.setDate(date);
+        provEntry.setUser(entries[i].getUser());
+        provEntry.setAction(entries[i].getAction());
+        prov.addEntry(provEntry);
+      }
+    }
+    else
+    {
+      provEntry = new Entry();
+      provEntry.setDate(date);
+      provEntry.setUser(System.getProperty("user.name")); // TODO: ext string
+      provEntry.setApp("JVAPP"); // TODO: ext string
+      provEntry.setAction(action);
+      prov.addEntry(provEntry);
+    }
+
+    return prov;
+  }
+  */
+  jalview.datamodel.Provenance getJalviewProvenance(Provenance prov) {
+    // TODO: fix App and Action entries and check use of provenance in jalview.
+    jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();
+    for (int i = 0; i < prov.getEntryCount(); i++)
+    {
+      jprov.addEntry(prov.getEntry(i).getUser(), prov.getEntry(i).getAction(),
+          prov.getEntry(i).getDate().toDate(), prov.getEntry(i).getId());
+    }
+
+    return jprov;
+  }
+
+  /**
+   * 
+   * @return default initial provenance list for a Jalview created vamsas
+   *         object.
+   */
+  Provenance dummyProvenance() {
+    return dummyProvenance(null);
+  }
+
+  Entry dummyPEntry(String action) {
+    Entry entry = new Entry();
+    entry.setApp("Jalview");
+    if (action != null)
+      entry.setAction(action);
+    else
+      entry.setAction("created.");
+    entry.setDate(new org.exolab.castor.types.Date(new java.util.Date()));
+    entry.setUser(System.getProperty("user.name"));
+    return entry;
+  }
+
+  Provenance dummyProvenance(String action) {
+    Provenance prov = new Provenance();
+    prov.addEntry(dummyPEntry(action));
+    return prov;
+  }
+
+  void addProvenance(Provenance p, String action) {
+    p.addEntry(dummyPEntry(action));
+  }
+
+}