2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 import jalview.bin.Cache;
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.AlignmentView;
26 import jalview.datamodel.DBRefEntry;
27 import jalview.datamodel.GraphLine;
28 import jalview.datamodel.SequenceFeature;
29 import jalview.datamodel.SequenceI;
30 import jalview.gui.AlignFrame;
31 import jalview.gui.AlignViewport;
32 import jalview.gui.Desktop;
33 import jalview.gui.TreePanel;
34 import jalview.io.vamsas.DatastoreItem;
35 import jalview.io.vamsas.Rangetype;
37 import java.util.Enumeration;
38 import java.util.HashMap;
39 import java.util.Hashtable;
40 import java.util.IdentityHashMap;
41 import java.util.Vector;
43 import uk.ac.vamsas.client.*;
44 import uk.ac.vamsas.objects.core.*;
49 * org.exolab.castor.util.LocalConfiguration.getInstance().getProperties().setProperty(
50 * "org.exolab.castor.serializer", "org.apache.xml.serialize.XMLSerilazizer"); }
54 public class VamsasAppDatastore
57 * Type used for general jalview generated annotation added to vamsas document
59 public static final String JALVIEW_ANNOTATION_ROW = "JalviewAnnotation";
62 * AlignmentAnnotation property to indicate that values should not be
65 public static final String DISCRETE_ANNOTATION = "discrete";
68 * continuous property - optional to specify that annotation should be
69 * represented as a continous graph line
71 private static final String CONTINUOUS_ANNOTATION = "continuous";
73 private static final String THRESHOLD = "threshold";
75 Entry provEntry = null;
81 IdentityHashMap jv2vobj;
83 Hashtable alignRDHash;
85 public VamsasAppDatastore(IClientDocument cdoc, Hashtable vobj2jv,
86 IdentityHashMap jv2vobj, Entry provEntry, Hashtable alignRDHash)
89 this.vobj2jv = vobj2jv;
90 this.jv2vobj = jv2vobj;
91 this.provEntry = provEntry;
92 this.alignRDHash = alignRDHash;
96 * @return the Vobject bound to Jalview datamodel object
98 protected Vobject getjv2vObj(Object jvobj)
100 if (jv2vobj.containsKey(jvobj))
102 return cdoc.getObject((VorbaId) jv2vobj.get(jvobj));
104 if (Cache.log.isDebugEnabled())
106 Cache.log.debug("Returning null VorbaID binding for jalview object "
115 * @return Jalview datamodel object bound to the vamsas document object
117 protected Object getvObj2jv(uk.ac.vamsas.client.Vobject vobj)
119 VorbaId id = vobj.getVorbaId();
122 id = cdoc.registerObject(vobj);
124 .debug("Registering new object and returning null for getvObj2jv");
127 if (vobj2jv.containsKey(vobj.getVorbaId()))
129 return vobj2jv.get(vobj.getVorbaId());
134 protected void bindjvvobj(Object jvobj, uk.ac.vamsas.client.Vobject vobj)
136 VorbaId id = vobj.getVorbaId();
139 id = cdoc.registerObject(vobj);
140 if (id == null || vobj.getVorbaId() == null
141 || cdoc.getObject(id) != vobj)
143 Cache.log.error("Failed to get id for "
144 + (vobj.isRegisterable() ? "registerable"
145 : "unregisterable") + " object " + vobj);
149 if (vobj2jv.containsKey(vobj.getVorbaId())
150 && !((VorbaId) vobj2jv.get(vobj.getVorbaId())).equals(jvobj))
153 "Warning? Overwriting existing vamsas id binding for "
154 + vobj.getVorbaId(), new Exception(
155 "Overwriting vamsas id binding."));
157 else if (jv2vobj.containsKey(jvobj)
158 && !((VorbaId) jv2vobj.get(jvobj)).equals(vobj.getVorbaId()))
161 "Warning? Overwriting existing jalview object binding for "
162 + jvobj, new Exception(
163 "Overwriting jalview object binding."));
166 * Cache.log.error("Attempt to make conflicting object binding! "+vobj+" id "
167 * +vobj.getVorbaId()+" already bound to "+getvObj2jv(vobj)+" and "+jvobj+"
168 * already bound to "+getjv2vObj(jvobj),new Exception("Excessive call to
171 // we just update the hash's regardless!
172 Cache.log.debug("Binding " + vobj.getVorbaId() + " to " + jvobj);
173 vobj2jv.put(vobj.getVorbaId(), jvobj);
174 // JBPNote - better implementing a hybrid invertible hash.
175 jv2vobj.put(jvobj, vobj.getVorbaId());
179 * put the alignment viewed by AlignViewport into cdoc.
182 * alignViewport to be stored
184 * title for alignment
186 public void storeVAMSAS(AlignViewport av, String aFtitle)
190 jalview.datamodel.AlignmentI jal = av.getAlignment();
192 VAMSAS root = null; // will be resolved based on Dataset Parent.
193 // /////////////////////////////////////////
195 DataSet dataset = null;
196 if (jal.getDataset() == null)
198 Cache.log.warn("Creating new dataset for an alignment.");
199 jal.setDataset(null);
201 dataset = (DataSet) getjv2vObj(jal.getDataset());
204 // it might be that one of the dataset sequences does actually have a
205 // binding, so search for it indirectly.
206 jalview.datamodel.SequenceI[] jdatset = jal.getDataset()
207 .getSequencesArray();
208 for (int i = 0; i < jdatset.length; i++)
210 Vobject vbound = getjv2vObj(jdatset[i]);
213 if (vbound instanceof uk.ac.vamsas.objects.core.Sequence)
217 dataset = (DataSet) vbound.getV_parent();
221 if (dataset != vbound.getV_parent())
224 "IMPLEMENTATION ERROR: Cannot map an alignment of sequences from datasets into the vamsas document.");
225 // This occurs because the dataset for the alignment we are
236 // we create a new dataset on the default vamsas root.
237 root = cdoc.getVamsasRoots()[0]; // default vamsas root for modifying.
238 dataset = new DataSet();
239 root.addDataSet(dataset);
240 bindjvvobj(jal.getDataset(), dataset);
241 dataset.setProvenance(dummyProvenance());
242 // dataset.getProvenance().addEntry(provEntry);
247 root = (VAMSAS) dataset.getV_parent();
252 // set new dataset and alignment sequences based on alignment Nucleotide
254 // this *will* break when alignment contains both nucleotide and amino
256 String dict = jal.isNucleotide() ? uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA
257 : uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_AA;
258 Vector dssmods = new Vector();
259 for (int i = 0; i < jal.getHeight(); i++)
261 SequenceI sq = jal.getSequenceAt(i).getDatasetSequence(); // only insert
265 sequence = (Sequence) getjv2vObj(sq);
266 if (sequence == null)
268 sequence = new Sequence();
269 bindjvvobj(sq, sequence);
270 sq.setVamsasId(sequence.getVorbaId().getId());
271 sequence.setSequence(sq.getSequenceAsString());
272 sequence.setDictionary(dict);
273 sequence.setName(sq.getName());
274 sequence.setStart(sq.getStart());
275 sequence.setEnd(sq.getEnd());
276 sequence.setDescription(sq.getDescription());
277 dataset.addSequence(sequence);
278 dssmods.addElement(dssmods);
282 boolean dsmod = false;
283 // verify and update principal attributes.
284 if (sq.getDescription() != null
285 && (sequence.getDescription() == null || !sequence
286 .getDescription().equals(sq.getDescription())))
288 sequence.setDescription(sq.getDescription());
291 if (sequence.getSequence() == null
292 || !sequence.getSequence().equals(
293 sq.getSequenceAsString()))
295 if (sequence.getStart() != sq.getStart()
296 || sequence.getEnd() != sq.getEnd())
298 // update modified sequence.
299 sequence.setSequence(sq.getSequenceAsString());
300 sequence.setStart(sq.getStart());
301 sequence.setEnd(sq.getEnd());
305 if (!dict.equals(sequence.getDictionary()))
307 sequence.setDictionary(dict);
310 if (!sequence.getName().equals(sq.getName()))
312 sequence.setName(sq.getName());
317 dssmods.addElement(sequence);
320 // add or update any new features/references on dataset sequence
321 if (sq.getSequenceFeatures() != null)
323 int sfSize = sq.getSequenceFeatures().length;
325 for (int sf = 0; sf < sfSize; sf++)
327 // TODO: update/modifiable synchronizer
328 jalview.datamodel.SequenceFeature feature = (jalview.datamodel.SequenceFeature) sq
329 .getSequenceFeatures()[sf];
331 DataSetAnnotations dsa = (DataSetAnnotations) getjv2vObj(feature);
334 dsa = (DataSetAnnotations) getDSAnnotationFromJalview(
335 new DataSetAnnotations(), feature);
336 if (dsa.getProvenance() == null)
338 dsa.setProvenance(new Provenance());
340 addProvenance(dsa.getProvenance(), "created"); // JBPNote - need
342 dsa.addSeqRef(sequence); // we have just created this annotation
343 // - so safe to use this
344 bindjvvobj(feature, dsa);
345 dataset.addDataSetAnnotations(dsa);
349 // todo: verify and update dataset annotations for sequence
350 System.out.println("update dataset sequence annotations.");
355 if (sq.getDBRef() != null)
357 DBRefEntry[] entries = sq.getDBRef();
358 jalview.datamodel.DBRefEntry dbentry;
359 for (int db = 0; db < entries.length; db++)
361 Rangetype dbr = new jalview.io.vamsas.Dbref(this,
362 dbentry = entries[db], sq, sequence);
367 if (dssmods.size() > 0)
371 Entry pentry = this.addProvenance(dataset.getProvenance(),
372 "updated sequences");
373 // pentry.addInput(vInput); could write in which sequences were
375 dssmods.removeAllElements();
378 // dataset.setProvenance(getVamsasProvenance(jal.getDataset().getProvenance()));
379 // ////////////////////////////////////////////
380 if (!av.getAlignment().isAligned())
381 return; // TODO: trees could be written - but for the moment we just
383 // ////////////////////////////////////////////
384 // Save the Alignments
386 Alignment alignment = (Alignment) getjv2vObj(av.getSequenceSetId()); // bind
399 if (alignment == null)
401 alignment = new Alignment();
402 bindjvvobj(av.getSequenceSetId(), alignment);
403 if (alignment.getProvenance() == null)
405 alignment.setProvenance(new Provenance());
407 addProvenance(alignment.getProvenance(), "added"); // TODO: insert some
410 dataset.addAlignment(alignment);
412 Property title = new Property();
413 title.setName("title");
414 title.setType("string");
415 title.setContent(aFtitle);
416 alignment.addProperty(title);
418 alignment.setGapChar(String.valueOf(av.getGapCharacter()));
419 for (int i = 0; i < jal.getHeight(); i++)
421 syncToAlignmentSequence(jal.getSequenceAt(i), alignment, null);
423 alignRDHash.put(av.getSequenceSetId(), av.getUndoRedoHash());
427 // always prepare to clone the alignment
428 boolean alismod = av.isUndoRedoHashModified((long[]) alignRDHash
429 .get(av.getSequenceSetId()));
430 // todo: verify and update mutable alignment props.
431 // TODO: Use isLocked methods
432 if (alignment.getModifiable() == null
433 || alignment.getModifiable().length() == 0)
434 // && !alignment.isDependedOn())
436 boolean modified = false;
437 // check existing sequences in local and in document.
438 Vector docseqs = new Vector(alignment
439 .getAlignmentSequenceAsReference());
440 for (int i = 0; i < jal.getHeight(); i++)
442 modified |= syncToAlignmentSequence(jal.getSequenceAt(i),
445 if (docseqs.size() > 0)
447 // removeValignmentSequences(alignment, docseqs);
448 docseqs.removeAllElements();
450 .println("Sequence deletion from alignment is not implemented.");
458 addProvenance(alignment.getProvenance(), "Edited"); // TODO:
467 addProvenance(alignment.getProvenance(), "Attributes Edited"); // TODO:
477 System.out.println("update alignment in document.");
481 System.out.println("alignment in document left unchanged.");
486 // unbind alignment from view.
487 // create new binding and new alignment.
488 // mark trail on new alignment as being derived from old ?
490 .println("update edited alignment to new alignment in document.");
493 // ////////////////////////////////////////////
494 // SAVE Alignment Sequence Features
495 for (int i = 0, iSize = alignment.getAlignmentSequenceCount(); i < iSize; i++)
497 AlignmentSequence valseq;
498 SequenceI alseq = (SequenceI) getvObj2jv(valseq = alignment
499 .getAlignmentSequence(i));
500 if (alseq != null && alseq.getSequenceFeatures() != null)
503 * We do not put local Alignment Sequence Features into the vamsas
507 * jalview.datamodel.SequenceFeature[] features = alseq
508 * .getSequenceFeatures(); for (int f = 0; f < features.length; f++) {
509 * if (features[f] != null) { AlignmentSequenceAnnotation valseqf = (
510 * AlignmentSequenceAnnotation) getjv2vObj(features[i]); if (valseqf ==
513 * valseqf = (AlignmentSequenceAnnotation) getDSAnnotationFromJalview(
514 * new AlignmentSequenceAnnotation(), features[i]);
515 * valseqf.setGraph(false);
516 * valseqf.addProperty(newProperty("jalview:feature","boolean","true"));
517 * if (valseqf.getProvenance() == null) { valseqf.setProvenance(new
518 * Provenance()); } addProvenance(valseqf.getProvenance(), "created"); //
519 * JBPNote - // need to // update bindjvvobj(features[i], valseqf);
520 * valseq.addAlignmentSequenceAnnotation(valseqf); } } }
525 // ////////////////////////////////////////////
527 if (jal.getAlignmentAnnotation() != null)
529 jalview.datamodel.AlignmentAnnotation[] aa = jal
530 .getAlignmentAnnotation();
531 java.util.HashMap AlSeqMaps = new HashMap(); // stores int maps from
532 // alignment columns to
533 // sequence positions.
534 for (int i = 0; i < aa.length; i++)
536 if (aa[i] == null || isJalviewOnly(aa[i]))
540 if (aa[i].sequenceRef != null)
542 // Deal with sequence associated annotation
543 Vobject sref = getjv2vObj(aa[i].sequenceRef);
544 if (sref instanceof uk.ac.vamsas.objects.core.AlignmentSequence)
546 saveAlignmentSequenceAnnotation(AlSeqMaps,
547 (AlignmentSequence) sref, aa[i]);
551 // first find the alignment sequence to associate this with.
552 SequenceI jvalsq = null;
553 Enumeration jval = av.getAlignment().getSequences()
555 while (jval.hasMoreElements())
557 jvalsq = (SequenceI) jval.nextElement();
558 // saveDatasetSequenceAnnotation(AlSeqMaps,(uk.ac.vamsas.objects.core.Sequence)
560 if (jvalsq.getDatasetSequence() == aa[i].sequenceRef)
562 Vobject alsref = getjv2vObj(jvalsq);
563 saveAlignmentSequenceAnnotation(AlSeqMaps,
564 (AlignmentSequence) alsref, aa[i]);
573 // add Alignment Annotation
574 uk.ac.vamsas.objects.core.AlignmentAnnotation an = (uk.ac.vamsas.objects.core.AlignmentAnnotation) getjv2vObj(aa[i]);
577 an = new uk.ac.vamsas.objects.core.AlignmentAnnotation();
578 an.setType(JALVIEW_ANNOTATION_ROW);
579 an.setDescription(aa[i].description);
580 alignment.addAlignmentAnnotation(an);
581 Seg vSeg = new Seg(); // TODO: refactor to have a default
582 // rangeAnnotationType initer/updater that
583 // takes a set of int ranges.
585 vSeg.setInclusive(true);
586 vSeg.setEnd(jal.getWidth());
590 an.setGraph(true); // aa[i].graph);
592 an.setLabel(aa[i].label);
593 an.setProvenance(dummyProvenance());
594 if (aa[i].graph != AlignmentAnnotation.NO_GRAPH)
596 an.setGroup(Integer.toString(aa[i].graphGroup)); // // JBPNote
611 AnnotationElement ae;
613 for (int a = 0; a < aa[i].annotations.length; a++)
615 if ((aa[i] == null) || (aa[i].annotations[a] == null))
620 ae = new AnnotationElement();
621 ae.setDescription(aa[i].annotations[a].description);
622 ae.addGlyph(new Glyph());
623 ae.getGlyph(0).setContent(
624 aa[i].annotations[a].displayCharacter); // assume
631 ae.addValue(aa[i].annotations[a].value);
632 ae.setPosition(a + 1);
633 if (aa[i].annotations[a].secondaryStructure != ' ')
635 Glyph ss = new Glyph();
637 .setDict(uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE);
640 .valueOf(aa[i].annotations[a].secondaryStructure));
643 an.addAnnotationElement(ae);
647 // an.addProperty(newProperty("jalview:editable", null,
649 // an.setModifiable(""); // TODO: This is not the way the
650 // modifiable flag is supposed to be used.
652 setAnnotationType(an, aa[i]);
654 if (aa[i].graph != jalview.datamodel.AlignmentAnnotation.NO_GRAPH)
657 an.setGroup(Integer.toString(aa[i].graphGroup));
658 if (aa[i].threshold != null && aa[i].threshold.displayed)
660 an.addProperty(newProperty(THRESHOLD, "float", ""
661 + aa[i].threshold.value));
662 if (aa[i].threshold.label != null)
664 an.addProperty(newProperty(THRESHOLD + "Name",
665 "string", "" + aa[i].threshold.label));
674 if (an.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT
677 // verify annotation - update (perhaps)
679 .info("update alignment sequence annotation. not yet implemented.");
683 // verify annotation - update (perhaps)
685 .info("updated alignment sequence annotation added.");
691 // /////////////////////////////////////////////////////
693 // //////////////////////////////////////////////
695 // /////////////////////////////////
696 // FIND ANY ASSOCIATED TREES
697 if (Desktop.desktop != null)
699 javax.swing.JInternalFrame[] frames = Desktop.instance
702 for (int t = 0; t < frames.length; t++)
704 if (frames[t] instanceof TreePanel)
706 TreePanel tp = (TreePanel) frames[t];
708 if (tp.getAlignment() == jal)
710 DatastoreItem vtree = new jalview.io.vamsas.Tree(this, tp,
716 // Store Jalview specific stuff in the Jalview appData
717 // not implemented in the SimpleDoc interface.
722 ex.printStackTrace();
728 * remove docseqs from the given alignment marking provenance appropriately
729 * and removing any references to the sequences.
734 private void removeValignmentSequences(Alignment alignment, Vector docseqs)
736 // delete these from document. This really needs to be a generic document
737 // API function derived by CASTOR.
738 Enumeration en = docseqs.elements();
739 while (en.hasMoreElements())
741 alignment.removeAlignmentSequence((AlignmentSequence) en
744 Entry pe = addProvenance(alignment.getProvenance(), "Removed "
745 + docseqs.size() + " sequences");
746 en = alignment.enumerateAlignmentAnnotation();
747 Vector toremove = new Vector();
748 while (en.hasMoreElements())
750 uk.ac.vamsas.objects.core.AlignmentAnnotation alan = (uk.ac.vamsas.objects.core.AlignmentAnnotation) en
752 if (alan.getSeqrefsCount() > 0)
755 Vector storem = new Vector();
756 Enumeration sr = alan.enumerateSeqrefs();
757 while (sr.hasMoreElements())
759 Object alsr = sr.nextElement();
760 if (docseqs.contains(alsr))
762 storem.addElement(alsr);
765 // remove references to the deleted sequences
766 sr = storem.elements();
767 while (sr.hasMoreElements())
769 alan.removeSeqrefs(sr.nextElement());
772 if (alan.getSeqrefsCount() == 0)
774 // should then delete alan from dataset
775 toremove.addElement(alan);
779 // remove any annotation that used to be associated to a specific bunch of
781 en = toremove.elements();
782 while (en.hasMoreElements())
785 .removeAlignmentAnnotation((uk.ac.vamsas.objects.core.AlignmentAnnotation) en
788 // TODO: search through alignment annotations to remove any references to
789 // this alignment sequence
793 * sync a jalview alignment seuqence into a vamsas alignment assumes all lock
794 * transformation/bindings have been sorted out before hand. creates/syncs the
795 * vamsas alignment sequence for jvalsq and adds it to the alignment if
796 * necessary. unbounddocseq is a duplicate of the vamsas alignment sequences
797 * and these are removed after being processed w.r.t a bound jvalsq
800 private boolean syncToAlignmentSequence(SequenceI jvalsq,
801 Alignment alignment, Vector unbounddocseq)
803 boolean modal = false;
804 // todo: islocked method here
805 boolean up2doc = false;
806 AlignmentSequence alseq = (AlignmentSequence) getjv2vObj(jvalsq);
809 alseq = new AlignmentSequence();
814 if (unbounddocseq != null)
816 unbounddocseq.removeElement(alseq);
819 // boolean locked = (alignment.getModifiable()==null ||
820 // alignment.getModifiable().length()>0);
821 // TODO: VAMSAS: translate lowercase symbols to annotation ?
822 if (up2doc || !alseq.getSequence().equals(jvalsq.getSequenceAsString()))
824 alseq.setSequence(jvalsq.getSequenceAsString());
825 alseq.setStart(jvalsq.getStart());
826 alseq.setEnd(jvalsq.getEnd());
829 if (up2doc || !alseq.getName().equals(jvalsq.getName()))
832 alseq.setName(jvalsq.getName());
834 if (jvalsq.getDescription() != null
835 && (alseq.getDescription() == null || !jvalsq.getDescription()
836 .equals(alseq.getDescription())))
839 alseq.setDescription(jvalsq.getDescription());
841 if (getjv2vObj(jvalsq.getDatasetSequence()) == null)
844 .warn("Serious Implementation error - Unbound dataset sequence in alignment: "
845 + jvalsq.getDatasetSequence());
847 alseq.setRefid(getjv2vObj(jvalsq.getDatasetSequence()));
851 alignment.addAlignmentSequence(alseq);
852 bindjvvobj(jvalsq, alseq);
854 return up2doc || modal;
858 * locally sync a jalview alignment seuqence from a vamsas alignment assumes
859 * all lock transformation/bindings have been sorted out before hand.
860 * creates/syncs the jvalsq from the alignment sequence
862 private boolean syncFromAlignmentSequence(AlignmentSequence valseq,
863 char valGapchar, char gapChar, Vector dsseqs)
866 boolean modal = false;
867 // todo: islocked method here
868 boolean upFromdoc = false;
869 jalview.datamodel.SequenceI alseq = (SequenceI) getvObj2jv(valseq);
877 // boolean locked = (alignment.getModifiable()==null ||
878 // alignment.getModifiable().length()>0);
879 // TODO: VAMSAS: translate lowercase symbols to annotation ?
881 || !valseq.getSequence().equals(alseq.getSequenceAsString()))
883 // this might go *horribly* wrong
884 alseq.setSequence(new String(valseq.getSequence()).replace(
885 valGapchar, gapChar));
886 alseq.setStart((int) valseq.getStart());
887 alseq.setEnd((int) valseq.getEnd());
890 if (!valseq.getName().equals(alseq.getName()))
893 alseq.setName(valseq.getName());
895 if (alseq.getDescription() == null
896 || (valseq.getDescription() == null || alseq.getDescription()
897 .equals(valseq.getDescription())))
899 alseq.setDescription(valseq.getDescription());
902 if (modal && Cache.log.isDebugEnabled())
904 Cache.log.debug("Updating apparently edited sequence "
910 alseq = new jalview.datamodel.Sequence(valseq.getName(), valseq
911 .getSequence().replace(valGapchar, gapChar), (int) valseq
912 .getStart(), (int) valseq.getEnd());
914 Vobject datsetseq = (Vobject) valseq.getRefid();
915 if (datsetseq != null)
917 alseq.setDatasetSequence((SequenceI) getvObj2jv(datsetseq)); // exceptions
929 .error("Invalid dataset sequence id (null) for alignment sequence "
930 + valseq.getVorbaId());
932 bindjvvobj(alseq, valseq);
933 alseq.setVamsasId(valseq.getVorbaId().getId());
936 Vobject datsetseq = (Vobject) valseq.getRefid();
937 if (datsetseq != null)
939 if (datsetseq != alseq.getDatasetSequence())
943 alseq.setDatasetSequence((SequenceI) getvObj2jv(datsetseq)); // exceptions
945 return upFromdoc || modal;
948 private void initRangeAnnotationType(RangeAnnotation an,
949 AlignmentAnnotation alan, int[] gapMap)
951 Seg vSeg = new Seg();
953 vSeg.setInclusive(true);
954 vSeg.setEnd(gapMap.length);
957 // LATER: much of this is verbatim from the alignmentAnnotation
958 // method below. suggests refactoring to make rangeAnnotation the
960 an.setDescription(alan.description);
961 an.setLabel(alan.label);
962 an.setGroup(Integer.toString(alan.graphGroup));
971 AnnotationElement ae;
972 for (int a = 0; a < alan.annotations.length; a++)
974 if (alan.annotations[a] == null)
979 ae = new AnnotationElement();
980 ae.setDescription(alan.annotations[a].description);
981 ae.addGlyph(new Glyph());
982 ae.getGlyph(0).setContent(alan.annotations[a].displayCharacter); // assume
989 if (alan.graph != jalview.datamodel.AlignmentAnnotation.NO_GRAPH)
991 ae.addValue(alan.annotations[a].value);
993 ae.setPosition(gapMap[a] + 1); // position w.r.t. AlignmentSequence
995 if (alan.annotations[a].secondaryStructure != ' ')
997 // we only write an annotation where it really exists.
998 Glyph ss = new Glyph();
1000 .setDict(uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE);
1001 ss.setContent(String
1002 .valueOf(alan.annotations[a].secondaryStructure));
1005 an.addAnnotationElement(ae);
1010 private void saveDatasetSequenceAnnotation(HashMap AlSeqMaps,
1011 uk.ac.vamsas.objects.core.Sequence sref, AlignmentAnnotation alan)
1015 // objects.core.AlignmentSequence alsref = (uk.ac.vamsas.
1016 // objects.core.AlignmentSequence) sref;
1017 uk.ac.vamsas.objects.core.DataSetAnnotations an = (uk.ac.vamsas.objects.core.DataSetAnnotations) getjv2vObj(alan);
1018 int[] gapMap = getGapMap(AlSeqMaps, alan);
1021 an = new uk.ac.vamsas.objects.core.DataSetAnnotations();
1022 initRangeAnnotationType(an, alan, gapMap);
1024 an.setProvenance(dummyProvenance()); // get provenance as user
1025 // created, or jnet, or
1027 setAnnotationType(an, alan);
1028 an.setGroup(Integer.toString(alan.graphGroup)); // // JBPNote -
1036 if (alan.getThreshold() != null && alan.getThreshold().displayed)
1038 an.addProperty(newProperty(THRESHOLD, "float", ""
1039 + alan.getThreshold().value));
1040 if (alan.getThreshold().label != null)
1041 an.addProperty(newProperty(THRESHOLD + "Name", "string", ""
1042 + alan.getThreshold().label));
1044 ((DataSet) sref.getV_parent()).addDataSetAnnotations(an);
1045 bindjvvobj(alan, an);
1049 // update reference sequence Annotation
1050 if (an.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT LOCK
1053 // verify existing alignment sequence annotation is up to date
1054 System.out.println("update dataset sequence annotation.");
1058 // verify existing alignment sequence annotation is up to date
1060 .println("make new alignment dataset sequence annotation if modification has happened.");
1066 private int[] getGapMap(HashMap AlSeqMaps, AlignmentAnnotation alan)
1069 if (AlSeqMaps.containsKey(alan.sequenceRef))
1071 gapMap = (int[]) AlSeqMaps.get(alan.sequenceRef);
1075 gapMap = new int[alan.sequenceRef.getLength()];
1076 // map from alignment position to sequence position.
1077 int[] sgapMap = alan.sequenceRef.gapMap();
1078 for (int a = 0; a < sgapMap.length; a++)
1080 gapMap[sgapMap[a]] = a;
1086 private void saveAlignmentSequenceAnnotation(HashMap AlSeqMaps,
1087 AlignmentSequence alsref, AlignmentAnnotation alan)
1091 // objects.core.AlignmentSequence alsref = (uk.ac.vamsas.
1092 // objects.core.AlignmentSequence) sref;
1093 uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation an = (uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation) getjv2vObj(alan);
1094 int[] gapMap = getGapMap(AlSeqMaps, alan);
1097 an = new uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation();
1098 initRangeAnnotationType(an, alan, gapMap);
1100 * I mean here that we don't actually have a semantic 'type' for the
1101 * annotation (this might be - score, intrinsic property, measurement,
1102 * something extracted from another program, etc)
1104 an.setType(JALVIEW_ANNOTATION_ROW); // TODO: better fix
1105 // this rough guess ;)
1106 alsref.addAlignmentSequenceAnnotation(an);
1107 bindjvvobj(alan, an);
1108 // These properties are directly supported by the
1109 // AlignmentSequenceAnnotation type.
1110 setAnnotationType(an, alan);
1111 an.setProvenance(dummyProvenance()); // get provenance as user
1112 // created, or jnet, or
1117 // update reference sequence Annotation
1118 if (an.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT LOCK
1121 // verify existing alignment sequence annotation is up to date
1122 System.out.println("update alignment sequence annotation.");
1126 // verify existing alignment sequence annotation is up to date
1128 .println("make new alignment sequence annotation if modification has happened.");
1134 * set vamsas annotation object type from jalview annotation
1139 private void setAnnotationType(RangeAnnotation an,
1140 AlignmentAnnotation alan)
1142 if (an instanceof AlignmentSequenceAnnotation)
1144 if (alan.graph != AlignmentAnnotation.NO_GRAPH)
1146 ((AlignmentSequenceAnnotation) an).setGraph(true);
1150 ((AlignmentSequenceAnnotation) an).setGraph(false);
1153 if (an instanceof uk.ac.vamsas.objects.core.AlignmentAnnotation)
1155 if (alan.graph != AlignmentAnnotation.NO_GRAPH)
1157 ((uk.ac.vamsas.objects.core.AlignmentAnnotation) an).setGraph(true);
1161 ((uk.ac.vamsas.objects.core.AlignmentAnnotation) an)
1167 case AlignmentAnnotation.BAR_GRAPH:
1168 an.addProperty(newProperty(DISCRETE_ANNOTATION, "boolean", "true"));
1170 case AlignmentAnnotation.LINE_GRAPH:
1171 an.addProperty(newProperty(CONTINUOUS_ANNOTATION, "boolean", "true"));
1174 // don't add any kind of discrete or continous property info.
1178 private Property newProperty(String name, String type, String content)
1180 Property vProperty = new Property();
1181 vProperty.setName(name);
1184 vProperty.setType(type);
1188 vProperty.setType("String");
1190 vProperty.setContent(content);
1195 * correctly create a RangeAnnotation from a jalview sequence feature
1198 * (typically DataSetAnnotations or
1199 * AlignmentSequenceAnnotation)
1201 * (the feature to be mapped from)
1204 private RangeAnnotation getDSAnnotationFromJalview(RangeAnnotation dsa,
1205 SequenceFeature feature)
1207 dsa.setType(feature.getType());
1208 Seg vSeg = new Seg();
1209 vSeg.setStart(feature.getBegin());
1210 vSeg.setEnd(feature.getEnd());
1211 vSeg.setInclusive(true);
1213 dsa.setDescription(feature.getDescription());
1214 dsa.setStatus(feature.getStatus());
1215 if (feature.links != null && feature.links.size() > 0)
1217 for (int i = 0, iSize = feature.links.size(); i < iSize; i++)
1219 String link = (String) feature.links.elementAt(i);
1220 int sep = link.indexOf('|');
1223 Link vLink = new Link();
1226 vLink.setContent(link.substring(0, sep - 1));
1230 vLink.setContent("");
1232 vLink.setHref(link.substring(sep + 1)); // TODO: validate href.
1237 dsa.setGroup(feature.getFeatureGroup());
1242 * get start<end range of segment, adjusting for inclusivity flag and
1246 * @param ensureDirection
1247 * when true - always ensure start is less than end.
1248 * @return int[] { start, end, direction} where direction==1 for range running
1249 * from end to start.
1251 private int[] getSegRange(Seg visSeg, boolean ensureDirection)
1253 boolean incl = visSeg.getInclusive();
1254 // adjust for inclusive flag.
1255 int pol = (visSeg.getStart() <= visSeg.getEnd()) ? 1 : -1; // polarity of
1257 int start = visSeg.getStart() + (incl ? 0 : pol);
1258 int end = visSeg.getEnd() + (incl ? 0 : -pol);
1259 if (ensureDirection && pol == -1)
1261 // jalview doesn't deal with inverted ranges, yet.
1267 { start, end, pol < 0 ? 1 : 0 };
1273 * @return true if annotation is not to be stored in document
1275 private boolean isJalviewOnly(AlignmentAnnotation annotation)
1277 return annotation.label.equals("Quality")
1278 || annotation.label.equals("Conservation")
1279 || annotation.label.equals("Consensus");
1283 * This will return the first AlignFrame viewing AlignViewport av. It will
1284 * break if there are more than one AlignFrames viewing a particular av. This
1285 * also shouldn't be in the io package.
1288 * @return alignFrame for av
1290 public AlignFrame getAlignFrameFor(AlignViewport av)
1292 if (Desktop.desktop != null)
1294 javax.swing.JInternalFrame[] frames = Desktop.instance.getAllFrames();
1296 for (int t = 0; t < frames.length; t++)
1298 if (frames[t] instanceof AlignFrame)
1300 if (((AlignFrame) frames[t]).getViewport() == av)
1302 return (AlignFrame) frames[t];
1310 public void updateToJalview()
1312 VAMSAS _roots[] = cdoc.getVamsasRoots();
1314 for (int _root = 0; _root < _roots.length; _root++)
1316 VAMSAS root = _roots[_root];
1317 boolean newds = false;
1318 for (int _ds = 0, _nds = root.getDataSetCount(); _ds < _nds; _ds++)
1320 // ///////////////////////////////////
1322 DataSet dataset = root.getDataSet(_ds);
1323 int i, iSize = dataset.getSequenceCount();
1325 jalview.datamodel.Alignment jdataset = (jalview.datamodel.Alignment) getvObj2jv(dataset);
1327 if (jdataset == null)
1329 Cache.log.debug("Initialising new jalview dataset fields");
1331 dsseqs = new Vector();
1335 Cache.log.debug("Update jalview dataset from vamsas.");
1336 jremain = jdataset.getHeight();
1337 dsseqs = jdataset.getSequences();
1340 // TODO: test sequence merging - we preserve existing non vamsas
1341 // sequences but add in any new vamsas ones, and don't yet update any
1342 // sequence attributes
1343 for (i = 0; i < iSize; i++)
1345 Sequence vdseq = dataset.getSequence(i);
1346 jalview.datamodel.SequenceI dsseq = (SequenceI) getvObj2jv(vdseq);
1349 if (!dsseq.getSequenceAsString().equals(vdseq.getSequence()))
1352 "Broken! - mismatch of dataset sequence: and jalview internal dataset sequence.");
1358 dsseq = new jalview.datamodel.Sequence(dataset.getSequence(i)
1359 .getName(), dataset.getSequence(i).getSequence(),
1360 (int) dataset.getSequence(i).getStart(), (int) dataset
1361 .getSequence(i).getEnd());
1362 dsseq.setDescription(dataset.getSequence(i).getDescription());
1363 bindjvvobj(dsseq, dataset.getSequence(i));
1364 dsseq.setVamsasId(dataset.getSequence(i).getVorbaId().getId());
1367 if (vdseq.getDbRefCount() > 0)
1369 DbRef[] dbref = vdseq.getDbRef();
1370 for (int db = 0; db < dbref.length; db++)
1372 new jalview.io.vamsas.Dbref(this, dbref[db], vdseq, dsseq);
1375 dsseq.updatePDBIds();
1381 SequenceI[] seqs = new SequenceI[dsseqs.size()];
1382 for (i = 0, iSize = dsseqs.size(); i < iSize; i++)
1384 seqs[i] = (SequenceI) dsseqs.elementAt(i);
1385 dsseqs.setElementAt(null, i);
1387 jdataset = new jalview.datamodel.Alignment(seqs);
1388 Cache.log.debug("New vamsas dataset imported into jalview.");
1389 bindjvvobj(jdataset, dataset);
1392 // add any new dataset sequence feature annotations
1393 if (dataset.getDataSetAnnotations() != null)
1395 for (int dsa = 0; dsa < dataset.getDataSetAnnotationsCount(); dsa++)
1397 DataSetAnnotations dseta = dataset.getDataSetAnnotations(dsa);
1398 // TODO: deal with group annotation on datset sequences.
1399 if (dseta.getSeqRefCount() == 1)
1401 SequenceI dsSeq = (SequenceI) getvObj2jv((Vobject) dseta
1402 .getSeqRef(0)); // TODO: deal with group dataset
1406 jalview.bin.Cache.log
1407 .warn("Couldn't resolve jalview sequenceI for dataset object reference "
1408 + ((Vobject) dataset.getDataSetAnnotations(
1409 dsa).getSeqRef(0)).getVorbaId()
1414 if (dseta.getAnnotationElementCount() == 0)
1416 jalview.datamodel.SequenceFeature sf = (jalview.datamodel.SequenceFeature) getvObj2jv(dseta);
1420 .addSequenceFeature(sf = getJalviewSeqFeature(dseta));
1421 bindjvvobj(sf, dseta);
1426 // TODO: deal with alignmentAnnotation style annotation
1427 // appearing on dataset sequences.
1428 // JBPNote: we could just add them to all alignments but
1429 // that may complicate cross references in the jalview
1432 .warn("Ignoring dataset annotation with annotationElements. Not yet supported in jalview.");
1438 if (dataset.getAlignmentCount() > 0)
1440 // LOAD ALIGNMENTS from DATASET
1442 for (int al = 0, nal = dataset.getAlignmentCount(); al < nal; al++)
1444 uk.ac.vamsas.objects.core.Alignment alignment = dataset
1446 AlignViewport av = findViewport(alignment);
1448 jalview.datamodel.AlignmentI jal = null;
1451 jal = av.getAlignment();
1453 iSize = alignment.getAlignmentSequenceCount();
1454 boolean newal = (jal == null) ? true : false;
1455 boolean refreshal = false;
1456 Vector newasAnnots = new Vector();
1457 char gapChar = ' '; // default for new alignments read in from the
1461 dsseqs = jal.getSequences(); // for merge/update
1462 gapChar = jal.getGapCharacter();
1466 dsseqs = new Vector();
1468 char valGapchar = alignment.getGapChar().charAt(0);
1469 for (i = 0; i < iSize; i++)
1471 AlignmentSequence valseq = alignment.getAlignmentSequence(i);
1472 jalview.datamodel.Sequence alseq = (jalview.datamodel.Sequence) getvObj2jv(valseq);
1473 if (syncFromAlignmentSequence(valseq, valGapchar, gapChar,
1478 // updated to sequence from the document
1482 if (valseq.getAlignmentSequenceAnnotationCount() > 0)
1484 AlignmentSequenceAnnotation[] vasannot = valseq
1485 .getAlignmentSequenceAnnotation();
1486 for (int a = 0; a < vasannot.length; a++)
1488 jalview.datamodel.AlignmentAnnotation asa = (jalview.datamodel.AlignmentAnnotation) getvObj2jv(vasannot[a]); // TODO:
1496 int se[] = getBounds(vasannot[a]);
1497 asa = getjAlignmentAnnotation(jal, vasannot[a]);
1498 asa.setSequenceRef(alseq);
1499 asa.createSequenceMapping(alseq, se[0], false); // TODO:
1510 alseq.addAlignmentAnnotation(asa);
1511 bindjvvobj(asa, vasannot[a]);
1512 newasAnnots.add(asa);
1516 // update existing annotation - can do this in place
1517 if (vasannot[a].getModifiable() == null) // TODO: USE
1523 .info("UNIMPLEMENTED: not recovering user modifiable sequence alignment annotation");
1524 // TODO: should at least replace with new one - otherwise
1525 // things will break
1526 // basically do this:
1527 // int se[] = getBounds(vasannot[a]);
1528 // asa.update(getjAlignmentAnnotation(jal, vasannot[a]));
1529 // // update from another annotation object in place.
1530 // asa.createSequenceMapping(alseq, se[0], false);
1539 SequenceI[] seqs = new SequenceI[dsseqs.size()];
1540 for (i = 0, iSize = dsseqs.size(); i < iSize; i++)
1542 seqs[i] = (SequenceI) dsseqs.elementAt(i);
1543 dsseqs.setElementAt(null, i);
1545 jal = new jalview.datamodel.Alignment(seqs);
1546 Cache.log.debug("New vamsas alignment imported into jalview "
1547 + alignment.getVorbaId().getId());
1548 jal.setDataset(jdataset);
1550 if (newasAnnots != null && newasAnnots.size() > 0)
1552 // Add the new sequence annotations in to the alignment.
1553 for (int an = 0, anSize = newasAnnots.size(); an < anSize; an++)
1555 jal.addAnnotation((AlignmentAnnotation) newasAnnots
1557 // TODO: check if anything has to be done - like calling
1558 // adjustForAlignment or something.
1559 newasAnnots.setElementAt(null, an);
1563 // //////////////////////////////////////////
1564 // //LOAD ANNOTATIONS FOR THE ALIGNMENT
1565 // ////////////////////////////////////
1566 if (alignment.getAlignmentAnnotationCount() > 0)
1568 uk.ac.vamsas.objects.core.AlignmentAnnotation[] an = alignment
1569 .getAlignmentAnnotation();
1571 for (int j = 0; j < an.length; j++)
1573 jalview.datamodel.AlignmentAnnotation jan = (jalview.datamodel.AlignmentAnnotation) getvObj2jv(an[j]);
1576 // update or stay the same.
1577 // TODO: should at least replace with a new one - otherwise
1578 // things will break
1579 // basically do this:
1580 // jan.update(getjAlignmentAnnotation(jal, an[a])); // update
1581 // from another annotation object in place.
1584 .debug("update from vamsas alignment annotation to existing jalview alignment annotation.");
1585 if (an[j].getModifiable() == null) // TODO: USE VAMSAS
1586 // LIBRARY OBJECT LOCK
1589 // TODO: user defined annotation is totally mutable... - so
1590 // load it up or throw away if locally edited.
1592 .info("NOT IMPLEMENTED - Recovering user-modifiable annotation - yet...");
1594 // TODO: compare annotation element rows
1595 // TODO: compare props.
1599 jan = getjAlignmentAnnotation(jal, an[j]);
1600 jal.addAnnotation(jan);
1601 bindjvvobj(jan, an[j]);
1605 AlignFrame alignFrame;
1608 Cache.log.debug("New alignframe for alignment "
1609 + alignment.getVorbaId());
1610 // ///////////////////////////////
1611 // construct alignment view
1612 alignFrame = new AlignFrame(jal, AlignFrame.DEFAULT_WIDTH,
1613 AlignFrame.DEFAULT_HEIGHT);
1614 av = alignFrame.getViewport();
1615 String title = alignment.getProvenance().getEntry(
1616 alignment.getProvenance().getEntryCount() - 1)
1618 if (alignment.getPropertyCount() > 0)
1620 for (int p = 0, pe = alignment.getPropertyCount(); p < pe; p++)
1622 if (alignment.getProperty(p).getName().equals("title"))
1624 title = alignment.getProperty(p).getContent();
1628 // TODO: automatically create meaningful title for a vamsas
1629 // alignment using its provenance.
1630 if (Cache.log.isDebugEnabled())
1632 title = title + "(" + alignment.getVorbaId() + ")";
1635 jalview.gui.Desktop.addInternalFrame(alignFrame, title,
1636 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
1637 bindjvvobj(av.getSequenceSetId(), alignment);
1641 // find the alignFrame for jal.
1642 // TODO: fix this so we retrieve the alignFrame handing av
1644 alignFrame = getAlignFrameFor(av);
1647 av.alignmentChanged(alignFrame.alignPanel);
1651 // /////////////////////////////////////
1652 if (alignment.getTreeCount() > 0)
1655 for (int t = 0; t < alignment.getTreeCount(); t++)
1657 jalview.io.vamsas.Tree vstree = new jalview.io.vamsas.Tree(
1658 this, alignFrame, alignment.getTree(t));
1659 TreePanel tp = null;
1660 if (vstree.isValidTree())
1662 tp = alignFrame.ShowNewickTree(vstree.getNewickTree(),
1663 vstree.getTitle(), vstree.getInputData(), 600,
1664 500, t * 20 + 50, t * 20 + 50);
1669 bindjvvobj(tp, alignment.getTree(t));
1672 vstree.UpdateSequenceTreeMap(tp);
1673 } catch (RuntimeException e)
1675 Cache.log.warn("update of labels failed.", e);
1680 Cache.log.warn("Cannot create tree for tree " + t
1682 + alignment.getTree(t).getVorbaId());
1690 // we do sequenceMappings last because they span all datasets in a vamsas
1692 for (int _ds = 0, _nds = root.getDataSetCount(); _ds < _nds; _ds++)
1694 DataSet dataset = root.getDataSet(_ds);
1695 if (dataset.getSequenceMappingCount() > 0)
1697 for (int sm = 0, smCount = dataset.getSequenceMappingCount(); sm < smCount; sm++)
1699 Rangetype seqmap = new jalview.io.vamsas.Sequencemapping(this,
1700 dataset.getSequenceMapping(sm));
1707 public AlignViewport findViewport(Alignment alignment)
1709 AlignViewport av = null;
1710 AlignViewport[] avs = findViewportForSequenceSetId((String) getvObj2jv(alignment));
1718 private AlignViewport[] findViewportForSequenceSetId(String sequenceSetId)
1720 Vector viewp = new Vector();
1721 if (Desktop.desktop != null)
1723 javax.swing.JInternalFrame[] frames = Desktop.instance.getAllFrames();
1725 for (int t = 0; t < frames.length; t++)
1727 if (frames[t] instanceof AlignFrame)
1729 if (((AlignFrame) frames[t]).getViewport().getSequenceSetId()
1730 .equals(sequenceSetId))
1732 viewp.addElement(((AlignFrame) frames[t]).getViewport());
1736 if (viewp.size() > 0)
1738 AlignViewport[] vp = new AlignViewport[viewp.size()];
1746 // bitfields - should be a template in j1.5
1747 private static int HASSECSTR = 0;
1749 private static int HASVALS = 1;
1751 private static int HASHPHOB = 2;
1753 private static int HASDC = 3;
1755 private static int HASDESCSTR = 4;
1757 private static int HASTWOSTATE = 5; // not used yet.
1760 * parses the AnnotationElements - if they exist - into
1761 * jalview.datamodel.Annotation[] rows Two annotation rows are made if there
1762 * are distinct annotation for both at 'pos' and 'after pos' at any particular
1766 * @return { boolean[static int constants ], int[ae.length] - map to annotated
1767 * object frame, jalview.datamodel.Annotation[],
1768 * jalview.datamodel.Annotation[] (after)}
1770 private Object[] parseRangeAnnotation(
1771 uk.ac.vamsas.objects.core.RangeAnnotation annotation)
1773 // set these attributes by looking in the annotation to decide what kind of
1774 // alignment annotation rows will be made
1775 // TODO: potentially we might make several annotation rows from one vamsas
1776 // alignment annotation. the jv2Vobj binding mechanism
1777 // may not quite cope with this (without binding an array of annotations to
1778 // a vamsas alignment annotation)
1779 // summary flags saying what we found over the set of annotation rows.
1780 boolean[] AeContent = new boolean[]
1781 { false, false, false, false, false };
1782 int[] rangeMap = getMapping(annotation);
1783 jalview.datamodel.Annotation[][] anot = new jalview.datamodel.Annotation[][]
1784 { new jalview.datamodel.Annotation[rangeMap.length],
1785 new jalview.datamodel.Annotation[rangeMap.length] };
1786 boolean mergeable = true; // false if 'after positions cant be placed on
1787 // same annotation row as positions.
1789 if (annotation.getAnnotationElementCount() > 0)
1791 AnnotationElement ae[] = annotation.getAnnotationElement();
1792 for (int aa = 0; aa < ae.length; aa++)
1794 int pos = (int) ae[aa].getPosition() - 1; // pos counts from 1 to
1795 // (|seg.start-seg.end|+1)
1796 if (pos >= 0 && pos < rangeMap.length)
1798 int row = ae[aa].getAfter() ? 1 : 0;
1799 if (anot[row][pos] != null)
1801 // only time this should happen is if the After flag is set.
1802 Cache.log.debug("Ignoring duplicate annotation site at " + pos);
1805 if (anot[1 - row][pos] != null)
1810 if (ae[aa].getDescription() != null)
1812 desc = ae[aa].getDescription();
1813 if (desc.length() > 0)
1815 // have imported valid description string
1816 AeContent[HASDESCSTR] = true;
1819 String dc = null; // ae[aa].getDisplayCharacter()==null ? "dc" :
1820 // ae[aa].getDisplayCharacter();
1821 String ss = null; // ae[aa].getSecondaryStructure()==null ? "ss" :
1822 // ae[aa].getSecondaryStructure();
1823 java.awt.Color colour = null;
1824 if (ae[aa].getGlyphCount() > 0)
1826 Glyph[] glyphs = ae[aa].getGlyph();
1827 for (int g = 0; g < glyphs.length; g++)
1832 uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE))
1834 ss = glyphs[g].getContent();
1835 AeContent[HASSECSTR] = true;
1840 uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_HD_HYDRO))
1842 Cache.log.debug("ignoring hydrophobicity glyph marker.");
1843 AeContent[HASHPHOB] = true;
1844 char c = (dc = glyphs[g].getContent()).charAt(0);
1845 // dc may get overwritten - but we still set the colour.
1846 colour = new java.awt.Color(c == '+' ? 255 : 0,
1847 c == '.' ? 255 : 0, c == '-' ? 255 : 0);
1850 else if (glyphs[g].getDict().equals(
1851 uk.ac.vamsas.objects.utils.GlyphDictionary.DEFAULT))
1853 dc = glyphs[g].getContent();
1854 AeContent[HASDC] = true;
1859 .debug("IMPLEMENTATION TODO: Ignoring unknown glyph type "
1860 + glyphs[g].getDict());
1865 if (ae[aa].getValueCount() > 0)
1867 AeContent[HASVALS] = true;
1868 if (ae[aa].getValueCount() > 1)
1870 Cache.log.warn("ignoring additional "
1871 + (ae[aa].getValueCount() - 1)
1872 + "values in annotation element.");
1874 val = ae[aa].getValue(0);
1878 anot[row][pos] = new jalview.datamodel.Annotation(
1879 (dc != null) ? dc : "", desc, (ss != null) ? ss
1880 .charAt(0) : ' ', val);
1884 anot[row][pos] = new jalview.datamodel.Annotation(
1885 (dc != null) ? dc : "", desc, (ss != null) ? ss
1886 .charAt(0) : ' ', val, colour);
1891 Cache.log.warn("Ignoring out of bound annotation element " + aa
1892 + " in " + annotation.getVorbaId().getId());
1895 // decide on how many annotation rows are needed.
1898 for (int i = 0; i < anot[0].length; i++)
1900 if (anot[1][i] != null)
1902 anot[0][i] = anot[1][i];
1903 anot[0][i].description = anot[0][i].description + " (after)";
1904 AeContent[HASDESCSTR] = true; // we have valid description string
1913 for (int i = 0; i < anot[0].length; i++)
1915 anot[1][i].description = anot[1][i].description + " (after)";
1919 { AeContent, rangeMap, anot[0], anot[1] };
1923 // no annotations to parse. Just return an empty annotationElement[]
1926 { AeContent, rangeMap, anot[0], anot[1] };
1933 * the jalview alignment to which the annotation will be
1934 * attached (ideally - freshly updated from corresponding
1937 * @return unbound jalview alignment annotation object.
1939 private jalview.datamodel.AlignmentAnnotation getjAlignmentAnnotation(
1940 jalview.datamodel.AlignmentI jal,
1941 uk.ac.vamsas.objects.core.RangeAnnotation annotation)
1943 if (annotation == null)
1948 // hasSequenceRef=annotation.getClass().equals(uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation.class);
1949 // boolean hasProvenance=hasSequenceRef ||
1950 // (annotation.getClass().equals(uk.ac.vamsas.objects.core.AlignmentAnnotation.class));
1952 * int se[] = getBounds(annotation); if (se==null) se=new int[]
1953 * {0,jal.getWidth()-1};
1955 Object[] parsedRangeAnnotation = parseRangeAnnotation(annotation);
1956 String a_label = annotation.getLabel();
1957 String a_descr = annotation.getDescription();
1958 GraphLine gl = null;
1960 boolean interp = true; // cleared if annotation is DISCRETE
1961 // set type and other attributes from properties
1962 if (annotation.getPropertyCount() > 0)
1964 // look for special jalview properties
1965 uk.ac.vamsas.objects.core.Property[] props = annotation.getProperty();
1966 for (int p = 0; p < props.length; p++)
1968 if (props[p].getName().equalsIgnoreCase(DISCRETE_ANNOTATION))
1970 type = AlignmentAnnotation.BAR_GRAPH;
1973 else if (props[p].getName().equalsIgnoreCase(CONTINUOUS_ANNOTATION))
1975 type = AlignmentAnnotation.LINE_GRAPH;
1977 else if (props[p].getName().equalsIgnoreCase(THRESHOLD))
1982 val = new Float(props[p].getContent());
1983 } catch (Exception e)
1985 Cache.log.warn("Failed to parse threshold property");
1990 gl = new GraphLine(val.floatValue(), "", java.awt.Color.black);
1994 gl.value = val.floatValue();
1997 else if (props[p].getName().equalsIgnoreCase(THRESHOLD + "Name"))
2000 gl = new GraphLine(0, "", java.awt.Color.black);
2001 gl.label = props[p].getContent();
2005 jalview.datamodel.AlignmentAnnotation jan = null;
2006 if (a_label == null || a_label.length() == 0)
2008 a_label = annotation.getType();
2009 if (a_label.length() == 0)
2011 a_label = "Unamed annotation";
2014 if (a_descr == null || a_descr.length() == 0)
2016 a_descr = "Annotation of type '" + annotation.getType() + "'";
2018 if (parsedRangeAnnotation == null)
2021 .debug("Inserting empty annotation row elements for a whole-alignment annotation.");
2025 if (parsedRangeAnnotation[3] != null)
2027 Cache.log.warn("Ignoring 'After' annotation row in "
2028 + annotation.getVorbaId());
2030 jalview.datamodel.Annotation[] arow = (jalview.datamodel.Annotation[]) parsedRangeAnnotation[2];
2031 boolean[] has = (boolean[]) parsedRangeAnnotation[0];
2032 // VAMSAS: getGraph is only on derived annotation for alignments - in this
2033 // way its 'odd' - there is already an existing TODO about removing this
2034 // flag as being redundant
2037 * ((annotation.getClass().equals(uk.ac.vamsas.objects.core.AlignmentAnnotation.class) &&
2038 * ((uk.ac.vamsas.objects.core.AlignmentAnnotation)annotation).getGraph()) ||
2039 * (hasSequenceRef=true &&
2040 * ((uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation)annotation).getGraph())) {
2046 type = jalview.datamodel.AlignmentAnnotation.BAR_GRAPH; // default
2052 // no hints - so we ensure HPHOB display is like this.
2053 type = jalview.datamodel.AlignmentAnnotation.BAR_GRAPH;
2056 // make bounds and automatic description strings for jalview user's
2057 // benefit (these shouldn't be written back to vamsas document)
2058 boolean first = true;
2059 float min = 0, max = 1;
2061 for (int i = 0; i < arow.length; i++)
2063 if (arow[i] != null)
2065 if (i - lastval > 1 && interp)
2067 // do some interpolation *between* points
2068 if (arow[lastval] != null)
2070 float interval = arow[i].value - arow[lastval].value;
2071 interval /= i - lastval;
2072 float base = arow[lastval].value;
2073 for (int ip = lastval + 1, np = 0; ip < i; np++, ip++)
2075 arow[ip] = new jalview.datamodel.Annotation("", "", ' ',
2076 interval * np + base);
2077 // NB - Interpolated points don't get a tooltip and
2083 // check range - shouldn't we have a min and max property in the
2084 // annotation object ?
2087 min = max = arow[i].value;
2092 if (arow[i].value < min)
2094 min = arow[i].value;
2096 else if (arow[i].value > max)
2098 max = arow[i].value;
2101 // make tooltip and display char value
2102 if (!has[HASDESCSTR])
2104 arow[i].description = arow[i].value + "";
2110 if (arow[i].description != null
2111 && arow[i].description.length() < 3)
2113 // copy over the description as the display char.
2114 arow[i].displayCharacter = new String(arow[i].description);
2119 // mark the position as a point used for the interpolation.
2120 arow[i].displayCharacter = arow[i].value + "";
2125 jan = new jalview.datamodel.AlignmentAnnotation(a_label, a_descr,
2126 arow, min, max, type);
2130 if (annotation.getAnnotationElementCount() == 0)
2132 // empty annotation array
2133 // TODO: alignment 'features' compare rangeType spec to alignment
2134 // width - if it is not complete, then mark regions on the annotation
2137 jan = new jalview.datamodel.AlignmentAnnotation(a_label, a_descr,
2139 jan.setThreshold(null);
2141 if (annotation.getLinkCount() > 0)
2143 Cache.log.warn("Ignoring " + annotation.getLinkCount()
2144 + "links added to AlignmentAnnotation.");
2146 if (annotation.getModifiable() == null
2147 || annotation.getModifiable().length() == 0) // TODO: USE VAMSAS
2151 jan.editable = true;
2155 if (annotation.getGroup() != null
2156 && annotation.getGroup().length() > 0)
2158 jan.graphGroup = Integer.parseInt(annotation.getGroup()); // TODO:
2166 } catch (Exception e)
2169 .info("UNIMPLEMENTED : Couldn't parse non-integer group value for setting graphGroup correctly.");
2178 private SequenceFeature getJalviewSeqFeature(RangeAnnotation dseta)
2180 int[] se = getBounds(dseta);
2181 SequenceFeature sf = new jalview.datamodel.SequenceFeature(dseta
2182 .getType(), dseta.getDescription(), dseta.getStatus(), se[0],
2183 se[1], dseta.getGroup());
2184 if (dseta.getLinkCount() > 0)
2186 Link[] links = dseta.getLink();
2187 for (int i = 0; i < links.length; i++)
2189 sf.addLink(links[i].getContent() + "|" + links[i].getHref());
2196 * get real bounds of a RangeType's specification. start and end are an
2197 * inclusive range within which all segments and positions lie. TODO: refactor
2201 * @return int[] { start, end}
2203 private int[] getBounds(RangeType dseta)
2208 if (dseta.getSegCount() > 0 && dseta.getPosCount() > 0)
2211 "Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
2213 if (dseta.getSegCount() > 0)
2215 se = getSegRange(dseta.getSeg(0), true);
2216 for (int s = 1, sSize = dseta.getSegCount(); s < sSize; s++)
2218 int nse[] = getSegRange(dseta.getSeg(s), true);
2229 if (dseta.getPosCount() > 0)
2231 // could do a polarity for pos range too. and pass back indication of
2233 int pos = dseta.getPos(0).getI();
2236 for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
2238 pos = dseta.getPos(p).getI();
2255 * map from a rangeType's internal frame to the referenced object's coordinate
2259 * @return int [] { ref(pos)...} for all pos in rangeType's frame.
2261 private int[] getMapping(RangeType dseta)
2263 Vector posList = new Vector();
2267 if (dseta.getSegCount() > 0 && dseta.getPosCount() > 0)
2270 "Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
2272 if (dseta.getSegCount() > 0)
2274 for (int s = 0, sSize = dseta.getSegCount(); s < sSize; s++)
2276 se = getSegRange(dseta.getSeg(s), false);
2277 int se_end = se[1 - se[2]] + (se[2] == 0 ? 1 : -1);
2278 for (int p = se[se[2]]; p != se_end; p += se[2] == 0 ? 1 : -1)
2280 posList.add(new Integer(p));
2284 else if (dseta.getPosCount() > 0)
2286 int pos = dseta.getPos(0).getI();
2288 for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
2290 pos = dseta.getPos(p).getI();
2291 posList.add(new Integer(pos));
2295 if (posList != null && posList.size() > 0)
2297 int[] range = new int[posList.size()];
2298 for (int i = 0; i < range.length; i++)
2300 range[i] = ((Integer) posList.elementAt(i)).intValue();
2311 * where the from range is the local mapped range, and the to
2312 * range is the 'mapped' range in the MapRangeType
2319 private jalview.util.MapList parsemapType(MapType maprange, int localu,
2322 jalview.util.MapList ml = null;
2323 int[] localRange = getMapping(maprange.getLocal());
2324 int[] mappedRange = getMapping(maprange.getMapped());
2325 long lu = maprange.getLocal().hasUnit() ? maprange.getLocal().getUnit()
2327 long mu = maprange.getMapped().hasUnit() ? maprange.getMapped()
2328 .getUnit() : mappedu;
2329 ml = new jalview.util.MapList(localRange, mappedRange, (int) lu,
2335 * initialise a range type object from a set of start/end inclusive intervals
2340 private void initRangeType(RangeType mrt, int[] range)
2342 for (int i = 0; i < range.length; i += 2)
2344 Seg vSeg = new Seg();
2345 vSeg.setStart(range[i]);
2346 vSeg.setEnd(range[i + 1]);
2352 * initialise a MapType object from a MapList object.
2358 private void initMapType(MapType maprange, jalview.util.MapList ml,
2361 maprange.setLocal(new Local());
2362 maprange.setMapped(new Mapped());
2363 initRangeType(maprange.getLocal(), ml.getFromRanges());
2364 initRangeType(maprange.getMapped(), ml.getToRanges());
2367 maprange.getLocal().setUnit(ml.getFromRatio());
2368 maprange.getLocal().setUnit(ml.getToRatio());
2373 * not needed now. Provenance getVamsasProvenance(jalview.datamodel.Provenance
2374 * jprov) { jalview.datamodel.ProvenanceEntry[] entries = null; // TODO: fix
2375 * App and Action here. Provenance prov = new Provenance();
2376 * org.exolab.castor.types.Date date = new org.exolab.castor.types.Date( new
2377 * java.util.Date()); Entry provEntry;
2379 * if (jprov != null) { entries = jprov.getEntries(); for (int i = 0; i <
2380 * entries.length; i++) { provEntry = new Entry(); try { date = new
2381 * org.exolab.castor.types.Date(entries[i].getDate()); } catch (Exception ex) {
2382 * ex.printStackTrace();
2384 * date = new org.exolab.castor.types.Date(entries[i].getDate()); }
2385 * provEntry.setDate(date); provEntry.setUser(entries[i].getUser());
2386 * provEntry.setAction(entries[i].getAction()); prov.addEntry(provEntry); } }
2387 * else { provEntry = new Entry(); provEntry.setDate(date);
2388 * provEntry.setUser(System.getProperty("user.name")); // TODO: ext string
2389 * provEntry.setApp("JVAPP"); // TODO: ext string provEntry.setAction(action);
2390 * prov.addEntry(provEntry); }
2394 jalview.datamodel.Provenance getJalviewProvenance(Provenance prov)
2396 // TODO: fix App and Action entries and check use of provenance in jalview.
2397 jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();
2398 for (int i = 0; i < prov.getEntryCount(); i++)
2400 jprov.addEntry(prov.getEntry(i).getUser(), prov.getEntry(i)
2401 .getAction(), prov.getEntry(i).getDate(), prov.getEntry(i)
2410 * @return default initial provenance list for a Jalview created vamsas
2413 Provenance dummyProvenance()
2415 return dummyProvenance(null);
2418 Entry dummyPEntry(String action)
2420 Entry entry = new Entry();
2421 entry.setApp(this.provEntry.getApp());
2424 entry.setAction(action);
2428 entry.setAction("created.");
2430 entry.setDate(new java.util.Date());
2431 entry.setUser(this.provEntry.getUser());
2435 Provenance dummyProvenance(String action)
2437 Provenance prov = new Provenance();
2438 prov.addEntry(dummyPEntry(action));
2442 Entry addProvenance(Provenance p, String action)
2444 Entry dentry = dummyPEntry(action);
2449 public Entry getProvEntry()
2454 public IClientDocument getClientDocument()
2459 public IdentityHashMap getJvObjectBinding()
2464 public Hashtable getVamsasObjectBinding()
2469 public void storeSequenceMappings(AlignViewport viewport, String title)
2472 AlignViewport av = viewport;
2475 jalview.datamodel.AlignmentI jal = av.getAlignment();
2476 // /////////////////////////////////////////
2478 DataSet dataset = null;
2479 if (jal.getDataset() == null)
2481 Cache.log.warn("Creating new dataset for an alignment.");
2482 jal.setDataset(null);
2484 dataset = (DataSet) getjv2vObj(jal.getDataset());
2485 // Store any sequence mappings.
2486 if (av.getAlignment().getCodonFrames() != null
2487 && av.getAlignment().getCodonFrames().length > 0)
2489 jalview.datamodel.AlignedCodonFrame[] cframes = av.getAlignment()
2491 for (int cf = 0; cf < cframes.length; cf++)
2493 if (cframes[cf].getdnaSeqs().length > 0)
2495 jalview.datamodel.SequenceI[] dmps = cframes[cf].getdnaSeqs();
2496 jalview.datamodel.Mapping[] mps = cframes[cf].getProtMappings();
2497 for (int smp = 0; smp < mps.length; smp++)
2499 uk.ac.vamsas.objects.core.SequenceType mfrom = (SequenceType) getjv2vObj(dmps[smp]);
2502 new jalview.io.vamsas.Sequencemapping(this, mps[smp],
2508 .warn("NO Vamsas Binding for local sequence! NOT CREATING MAPPING FOR "
2509 + dmps[smp].getDisplayId(true)
2511 + mps[smp].getTo().getName());
2517 } catch (Exception e)
2519 throw new Exception("Couldn't store sequence mappings for " + title,