--- /dev/null
+/*\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.objects.core.*;\r
+\r
+import jalview.gui.*;\r
+import java.io.*;\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
+ AlignViewport av;\r
+ org.exolab.castor.types.Date date\r
+ = new org.exolab.castor.types.Date(new java.util.Date());\r
+\r
+ public VamsasDatastore(AlignViewport av)\r
+ {\r
+ this.av = av;\r
+ }\r
+\r
+ public void store(String file)\r
+ {\r
+ storeVAMSAS(file);\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
+ public VAMSAS storeVAMSAS(String file)\r
+ {\r
+ long time = System.currentTimeMillis();\r
+ VAMSAS vamsas = new VAMSAS();\r
+\r
+ try\r
+ {\r
+ FileOutputStream fos = new FileOutputStream(file);\r
+ JarOutputStream jout = new JarOutputStream(fos);\r
+\r
+\r
+ ////////////////////////////////////////////////////\r
+ //NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS\r
+ PrintWriter out = new PrintWriter(new OutputStreamWriter(jout,\r
+ "UTF-8"));\r
+\r
+ jalview.datamodel.AlignmentI jal = av.getAlignment();\r
+\r
+\r
+ ///////////////////////////////////////////\r
+ // SAVE THE DATASET\r
+ DataSet dataset = new DataSet();\r
+ vamsas.addDataSet(dataset);\r
+\r
+ Sequence sequence;\r
+ DbRef dbref;\r
+ for (int i = 0; i < jal.getHeight(); i++)\r
+ {\r
+ sequence = new Sequence();\r
+ sequence.setSequence(jal.getDataset().getSequenceAt(i).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
+\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 = new DataSetAnnotations();\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
+ dsa.addProvenance(dummyProvenance());\r
+ dsa.setSeqRef(sequence);\r
+ dsa.setId("sf"+(time++));\r
+ dataset.addDataSetAnnotations(dsa);\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 = new 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
+ }\r
+\r
+ }\r
+\r
+\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
+ dataset.addSequence(sequence);\r
+ }\r
+ // dataset.setProvenance(getVamsasProvenance(jal.getDataset().getProvenance()));\r
+ //////////////////////////////////////////////\r
+\r
+\r
+ //////////////////////////////////////////////\r
+ //Save the Alignments\r
+ Alignment alignment = new 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
+ alseq.setRefid(dataset.getSequence(i));\r
+ alseq.setId("as" + (time++));\r
+ alignment.addAlignmentSequence(alseq);\r
+ }\r
+ // alignment.setProvenance(getVamsasProvenance(jal.getProvenance()));\r
+ dataset.addAlignment(alignment);\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
+\r
+ AlignmentAnnotations an = new AlignmentAnnotations();\r
+\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
+ }\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
+ Tree tree = new Tree();\r
+ tree.setTitle(tp.getTitle());\r
+ Newick newick = new Newick();\r
+ newick.setContent( tp.getTree().toString() );\r
+ newick.setTitle( tp.getTitle() );\r
+ newick.setId("t"+(time++));\r
+ tree.addNewick( newick );\r
+ tree.setProvenance(dummyProvenance());\r
+ alignment.addTree(tree);\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+\r
+ JarEntry entry = new JarEntry("vamsas.xml");\r
+ jout.putNextEntry(entry);\r
+\r
+ vamsas.marshal(out);\r
+\r
+ out.close();\r
+ jout.close();\r
+\r
+\r
+ }\r
+ catch (Exception ex)\r
+ {\r
+ ex.printStackTrace();\r
+ }\r
+\r
+ return vamsas;\r
+ }\r
+\r
+ Tree jalview2VamsasTree()\r
+ {\r
+ Tree tree = new Tree();\r
+\r
+ return tree;\r
+ }\r
+\r
+\r
+ public void load(String file)\r
+ {\r
+ VAMSAS vamsas = new VAMSAS() ;\r
+\r
+ try\r
+ {\r
+ FileInputStream fis = new FileInputStream(file);\r
+ JarInputStream jin = new JarInputStream(fis);\r
+\r
+\r
+ ////////////////////////////////////////////////////\r
+ //NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS\r
+ InputStreamReader in = new InputStreamReader(jin, "UTF-8");\r
+ JarEntry jarentry = jin.getNextJarEntry();\r
+ System.out.println("reading: "+jarentry.getName());\r
+ vamsas = (VAMSAS) vamsas.unmarshal(in);\r
+\r
+\r
+ /////////////////////////////////////\r
+ /////LOAD DATASET\r
+ DataSet dataset = vamsas.getDataSet(0);\r
+\r
+ int i, iSize = dataset.getSequenceCount();\r
+ jalview.datamodel.Sequence [] seqs = new jalview.datamodel.Sequence[iSize];\r
+ DbRef [] dbref;\r
+\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
+ seqs[i].setVamsasId(dataset.getSequence(i).getId());\r
+\r
+ if (dataset.getDataSetAnnotations() != null)\r
+ {\r
+ for (int dsa = 0; dsa < dataset.getDataSetAnnotationsCount(); dsa++)\r
+ {\r
+ if (dataset.getDataSetAnnotations(dsa).getSeqRef() ==\r
+ dataset.getSequence(i))\r
+ {\r
+ seqs[i].addSequenceFeature(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
+ }\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
+ seqs[i].addDBRef( new jalview.datamodel.DBRefEntry\r
+ (\r
+ dbref[db].getSource().toString(),\r
+ dbref[db].getVersion().toString(),\r
+ dbref[db].getAccessionId().toString()));\r
+ }\r
+ }\r
+ }\r
+ jalview.datamodel.Alignment jdataset = new jalview.datamodel.Alignment(seqs);\r
+ ////////////////////////////////////////////////\r
+\r
+\r
+ //////////////////////////////////////\r
+ ////LOAD ALIGNMENT\r
+ Alignment alignment = dataset.getAlignment(0);\r
+ iSize = alignment.getAlignmentSequenceCount();\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
+\r
+ id = ( (Sequence) alignment.getAlignmentSequence(i).getRefid()).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
+ 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 = new jalview.datamodel.Annotation[jal.\r
+ getWidth()];\r
+\r
+ try{\r
+\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
+\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
+\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