/* * 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.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.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; IdentityHashMap jv2vobj; public VamsasDatastore(ClientDoc cdoc, Hashtable vobj2jv, IdentityHashMap jv2vobj, Entry provEntry) { this.cdoc = cdoc; this.vobj2jv = vobj2jv; this.jv2vobj = jv2vobj; 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(); } } */ protected Vobject getjv2vObj(Object jvobj) { if (jv2vobj.containsKey(jvobj)) return cdoc.getObject(((VorbaId)jv2vobj.get(jvobj))); return null; } 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()); } } public void storeVAMSAS(AlignViewport av) { long time = System.currentTimeMillis(); try { jalview.datamodel.AlignmentI jal = av.getAlignment(); /////////////////////////////////////////// // SAVE THE DATASET VAMSAS root = cdoc.getVamsasRoots()[0]; if (jal.getDataset()==null) { Cache.log.warn("Creating new dataset for an alignment."); jal.setDataset(null); // } boolean nw=false; DataSet dataset = (DataSet) getjv2vObj(jal.getDataset()); if (dataset==null) { dataset = new DataSet(); root.addDataSet(dataset); bindjvvobj(jal.getDataset(),dataset); nw=true; } //else { // should really check root corresponds to dataset. // } // update dataset Sequence sequence; DbRef dbref; for (int i = 0; i < jal.getHeight(); i++) { SequenceI sq = jal.getDataset().getSequenceAt(i); sequence = (Sequence) getjv2vObj(sq); if (sequence==null) { sequence=new Sequence(); bindjvvobj(sq, sequence); sq.setVamsasId(sequence.getVorbaId().getId()); sequence.setSequence(sq.getSequence()); 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. } if(jal.getDataset().getSequenceAt(i).getSequenceFeatures()!=null) { int sfSize = jal.getDataset().getSequenceAt(i).getSequenceFeatures().length; for (int sf = 0; sf < sfSize; sf++) { jalview.datamodel.SequenceFeature feature = (jalview.datamodel.SequenceFeature) jal.getDataset().getSequenceAt(i).getSequenceFeatures()[sf]; DataSetAnnotations dsa = (DataSetAnnotations) getjv2vObj(feature); if (dsa==null) { dsa = new DataSetAnnotations(); bindjvvobj(feature, dsa); dsa.setType(feature.getType()); dsa.setBegin(feature.getBegin()); dsa.setEnd(feature.getEnd()); dsa.setDescription(feature.getDescription()); dsa.setStatus(feature.getStatus()); if (dsa.getProvenance()==null) { dsa.setProvenance(new Provenance()); } dsa.getProvenance().addEntry(provEntry); // JBPNote - need to update jalview datamodel. dsa.setSeqRef(sequence); dataset.addDataSetAnnotations(dsa); } else { // todo: verify } } } if(jal.getDataset().getSequenceAt(i).getDBRef()!=null) { java.util.Vector entries = jal.getDataset().getSequenceAt(i).getDBRef(); jalview.datamodel.DBRefEntry dbentry; for(int db=0; db0) an.setGraph(true); //aa[i].graph); // JBPNote - store graphGroup in the Jalview specific bits. an.setLabel(aa[i].label); an.setProvenance(dummyProvenance()); 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.setDisplayCharacter(aa[i].annotations[a].displayCharacter); ae.setValue(aa[i].annotations[a].value); ae.setPosition(a); ae.setSecondaryStructure(aa[i].annotations[a].secondaryStructure + ""); an.addAnnotationElement(ae); } alignment.addAlignmentAnnotations(an); } else { // verify annotation - update (perhaps) } } } /////////////////////////////////////////////////////// //////////////////////////////////////////////// ///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) { // JBPNote - can recover alignment and Vamsas Alignment for an associated tree using the getjv2vObj() Tree tree = (Tree) getjv2vObj(tp); if (tree==null) { tree = new Tree(); bindjvvobj(tp,tree); tree.setTitle(tp.getTitle()); Newick newick = new Newick(); newick.setContent( tp.getTree().toString() ); newick.setTitle( tp.getTitle() ); tree.addNewick( newick ); tree.setProvenance(dummyProvenance()); alignment.addTree(tree); } else { // verify any changes. } } } } } } catch (Exception ex) { ex.printStackTrace(); } } public void updateJalview() { VAMSAS root = cdoc.getVamsasRoots()[0]; try { boolean newds=false; for (int _ds=0,_nds=root.getDataSetCount(); _ds<_nds; _ds++) { ///////////////////////////////////// /////LOAD DATASET DataSet dataset = vamsas.getDataSet(_ds); int i, iSize = dataset.getSequenceCount(); jalview.datamodel.Alignment jdataset = (jalview.datamodel.Alignment) vobj2jv(dataset); if (jdataset!=null) { // recover } else { newds=true; // construct new dataset jalview.datamodel.Sequence [] seqs = new jalview.datamodel.Sequence[iSize]; DbRef [] dbref; for (i = 0; i < iSize ; i++) { seqs[i] = new jalview.datamodel.Sequence( dataset.getSequence(i).getName(), dataset.getSequence(i).getSequence(), dataset.getSequence(i).getStart(), dataset.getSequence(i).getEnd() ); bindjvvobj(seqs[i], dataset.getSequence(i)); seqs[i].setVamsasId(dataset.getSequence(i).getVorbaId().getId()); if (dataset.getDataSetAnnotations() != null) { for (int dsa = 0; dsa < dataset.getDataSetAnnotationsCount(); dsa++) { // could be optimised if (dataset.getDataSetAnnotations(dsa).getSeqRef() == dataset.getSequence(i)) { jalview.datamodel.SequenceFeature sf; seqs[i].addSequenceFeature(sf=new jalview.datamodel. SequenceFeature( dataset.getDataSetAnnotations(dsa).getType(), dataset.getDataSetAnnotations(dsa).getDescription(), dataset.getDataSetAnnotations(dsa).getStatus(), dataset.getDataSetAnnotations(dsa).getBegin(), dataset.getDataSetAnnotations(dsa).getEnd(), "vamsas")); bindjvvobj(sf, dataset.getDataSetAnnotations(dsa)); } } } dbref = dataset.getSequence(i).getDbRef(); if(dbref.length>0) { for(int db=0; db0) { AlignmentAnnotations[] an = alignment.getAlignmentAnnotations(); for (j = 0; j < an.length; j++) { boolean topaliBreakpoint = false; AnnotationElement[] ae = an[j].getAnnotationElement(); jalview.datamodel.Annotation[] anot = getvObj2jv(ae); if (anot!=null) { // update or stay the same. } else { // new one anot=new jalview.datamodel.Annotation[jal. getWidth()]; bindjvvobj(anot, ae); try{ for (int aa = 0; aa < ae.length; aa++) { String dc = ae[aa].getDisplayCharacter()==null ? "dc" : ae[aa].getDisplayCharacter(); String desc = ae[aa].getDescription()==null ? "desc" : ae[aa].getDescription(); String ss = ae[aa].getSecondaryStructure()==null ? "ss" : ae[aa].getSecondaryStructure(); float value = ae[aa].getValue(); if(an[j].getGraph()) { dc = value+""; desc = value + ""; } anot[ae[aa].getPosition()-1] = new jalview.datamodel. Annotation(dc,desc,ss.charAt(0),value); if(desc.equals("TOPALi Partition Breakpoint")) topaliBreakpoint = true; } }catch(Exception ex) { ex.printStackTrace(); System.out.println("problem parsing annotations\n"+ex);} } jalview.datamodel.AlignmentAnnotation jaa = null; if (an[j].getGraph()) { jaa = new jalview.datamodel.AlignmentAnnotation(an[j].getLabel(), an[j].getDescription(), anot, 0, 0, 1); } else { String label = an[j].getLabel(); if(topaliBreakpoint) label = "TOPALi Partition Breakpoint"; jaa = new jalview.datamodel.AlignmentAnnotation(label, an[j].getDescription(), anot); } jal.addAnnotation(jaa); } } ///////////////////////////////// AlignFrame alignFrame = new AlignFrame(jal); jalview.gui.Desktop.addInternalFrame(alignFrame, "VAMSAS LOAD", AlignFrame.NEW_WINDOW_WIDTH, AlignFrame.NEW_WINDOW_HEIGHT); //LOAD TREES /////////////////////////////////////// if (alignment.getTreeCount() > 0) { for (int t = 0; t < alignment.getTreeCount(); t++) { Tree tree = alignment.getTree(t); alignFrame.ShowNewickTree( new jalview.io.NewickFile(tree.getNewick(0).getContent()), tree.getNewick(0).getTitle(), 600, 500, t * 20 + 50, t * 20 + 50); } } in.close(); jin.close(); } catch (Exception ex) { ex.printStackTrace(); } } Provenance getVamsasProvenance(jalview.datamodel.Provenance jprov) { jalview.datamodel.ProvenanceEntry [] entries = null; 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")); provEntry.setAction("Jalview"); prov.addEntry(provEntry); } return prov; } jalview.datamodel.Provenance getJalviewProvenance(Provenance prov) { 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; } Provenance dummyProvenance() { Provenance prov = new Provenance(); Entry entry = new Entry(); entry.setAction("Jalview"); entry.setDate(new org.exolab.castor.types.Date(new java.util.Date())); entry.setUser(System.getProperty("user.name")); prov.addEntry(entry); return prov; } }