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.");
354 if (sq.getDatasetSequence()==null && sq.getDBRef() != null)
356 // only sync database references for dataset sequences
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
633 ae.addValue(aa[i].annotations[a].value);
635 ae.setPosition(a + 1);
636 if (aa[i].annotations[a].secondaryStructure != ' ')
638 Glyph ss = new Glyph();
640 .setDict(uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE);
643 .valueOf(aa[i].annotations[a].secondaryStructure));
646 an.addAnnotationElement(ae);
650 // an.addProperty(newProperty("jalview:editable", null,
652 // an.setModifiable(""); // TODO: This is not the way the
653 // modifiable flag is supposed to be used.
655 setAnnotationType(an, aa[i]);
657 if (aa[i].graph != jalview.datamodel.AlignmentAnnotation.NO_GRAPH)
660 an.setGroup(Integer.toString(aa[i].graphGroup));
661 if (aa[i].threshold != null && aa[i].threshold.displayed)
663 an.addProperty(newProperty(THRESHOLD, "float", ""
664 + aa[i].threshold.value));
665 if (aa[i].threshold.label != null)
667 an.addProperty(newProperty(THRESHOLD + "Name",
668 "string", "" + aa[i].threshold.label));
677 if (an.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT
680 // verify annotation - update (perhaps)
682 .info("update alignment sequence annotation. not yet implemented.");
686 // verify annotation - update (perhaps)
688 .info("updated alignment sequence annotation added.");
694 // /////////////////////////////////////////////////////
696 // //////////////////////////////////////////////
698 // /////////////////////////////////
699 // FIND ANY ASSOCIATED TREES
700 if (Desktop.desktop != null)
702 javax.swing.JInternalFrame[] frames = Desktop.instance
705 for (int t = 0; t < frames.length; t++)
707 if (frames[t] instanceof TreePanel)
709 TreePanel tp = (TreePanel) frames[t];
711 if (tp.getAlignment() == jal)
713 DatastoreItem vtree = new jalview.io.vamsas.Tree(this, tp,
719 // Store Jalview specific stuff in the Jalview appData
720 // not implemented in the SimpleDoc interface.
725 ex.printStackTrace();
731 * remove docseqs from the given alignment marking provenance appropriately
732 * and removing any references to the sequences.
737 private void removeValignmentSequences(Alignment alignment, Vector docseqs)
739 // delete these from document. This really needs to be a generic document
740 // API function derived by CASTOR.
741 Enumeration en = docseqs.elements();
742 while (en.hasMoreElements())
744 alignment.removeAlignmentSequence((AlignmentSequence) en
747 Entry pe = addProvenance(alignment.getProvenance(), "Removed "
748 + docseqs.size() + " sequences");
749 en = alignment.enumerateAlignmentAnnotation();
750 Vector toremove = new Vector();
751 while (en.hasMoreElements())
753 uk.ac.vamsas.objects.core.AlignmentAnnotation alan = (uk.ac.vamsas.objects.core.AlignmentAnnotation) en
755 if (alan.getSeqrefsCount() > 0)
758 Vector storem = new Vector();
759 Enumeration sr = alan.enumerateSeqrefs();
760 while (sr.hasMoreElements())
762 Object alsr = sr.nextElement();
763 if (docseqs.contains(alsr))
765 storem.addElement(alsr);
768 // remove references to the deleted sequences
769 sr = storem.elements();
770 while (sr.hasMoreElements())
772 alan.removeSeqrefs(sr.nextElement());
775 if (alan.getSeqrefsCount() == 0)
777 // should then delete alan from dataset
778 toremove.addElement(alan);
782 // remove any annotation that used to be associated to a specific bunch of
784 en = toremove.elements();
785 while (en.hasMoreElements())
788 .removeAlignmentAnnotation((uk.ac.vamsas.objects.core.AlignmentAnnotation) en
791 // TODO: search through alignment annotations to remove any references to
792 // this alignment sequence
796 * sync a jalview alignment seuqence into a vamsas alignment assumes all lock
797 * transformation/bindings have been sorted out before hand. creates/syncs the
798 * vamsas alignment sequence for jvalsq and adds it to the alignment if
799 * necessary. unbounddocseq is a duplicate of the vamsas alignment sequences
800 * and these are removed after being processed w.r.t a bound jvalsq
803 private boolean syncToAlignmentSequence(SequenceI jvalsq,
804 Alignment alignment, Vector unbounddocseq)
806 boolean modal = false;
807 // todo: islocked method here
808 boolean up2doc = false;
809 AlignmentSequence alseq = (AlignmentSequence) getjv2vObj(jvalsq);
812 alseq = new AlignmentSequence();
817 if (unbounddocseq != null)
819 unbounddocseq.removeElement(alseq);
822 // boolean locked = (alignment.getModifiable()==null ||
823 // alignment.getModifiable().length()>0);
824 // TODO: VAMSAS: translate lowercase symbols to annotation ?
825 if (up2doc || !alseq.getSequence().equals(jvalsq.getSequenceAsString()))
827 alseq.setSequence(jvalsq.getSequenceAsString());
828 alseq.setStart(jvalsq.getStart());
829 alseq.setEnd(jvalsq.getEnd());
832 if (up2doc || !alseq.getName().equals(jvalsq.getName()))
835 alseq.setName(jvalsq.getName());
837 if (jvalsq.getDescription() != null
838 && (alseq.getDescription() == null || !jvalsq.getDescription()
839 .equals(alseq.getDescription())))
842 alseq.setDescription(jvalsq.getDescription());
844 if (getjv2vObj(jvalsq.getDatasetSequence()) == null)
847 .warn("Serious Implementation error - Unbound dataset sequence in alignment: "
848 + jvalsq.getDatasetSequence());
850 alseq.setRefid(getjv2vObj(jvalsq.getDatasetSequence()));
854 alignment.addAlignmentSequence(alseq);
855 bindjvvobj(jvalsq, alseq);
857 return up2doc || modal;
861 * locally sync a jalview alignment seuqence from a vamsas alignment assumes
862 * all lock transformation/bindings have been sorted out before hand.
863 * creates/syncs the jvalsq from the alignment sequence
865 private boolean syncFromAlignmentSequence(AlignmentSequence valseq,
866 char valGapchar, char gapChar, Vector dsseqs)
869 boolean modal = false;
870 // todo: islocked method here
871 boolean upFromdoc = false;
872 jalview.datamodel.SequenceI alseq = (SequenceI) getvObj2jv(valseq);
880 // boolean locked = (alignment.getModifiable()==null ||
881 // alignment.getModifiable().length()>0);
882 // TODO: VAMSAS: translate lowercase symbols to annotation ?
884 || !valseq.getSequence().equals(alseq.getSequenceAsString()))
886 // this might go *horribly* wrong
887 alseq.setSequence(new String(valseq.getSequence()).replace(
888 valGapchar, gapChar));
889 alseq.setStart((int) valseq.getStart());
890 alseq.setEnd((int) valseq.getEnd());
893 if (!valseq.getName().equals(alseq.getName()))
896 alseq.setName(valseq.getName());
898 if (alseq.getDescription() == null
899 || (valseq.getDescription() == null || alseq.getDescription()
900 .equals(valseq.getDescription())))
902 alseq.setDescription(valseq.getDescription());
905 if (modal && Cache.log.isDebugEnabled())
907 Cache.log.debug("Updating apparently edited sequence "
913 alseq = new jalview.datamodel.Sequence(valseq.getName(), valseq
914 .getSequence().replace(valGapchar, gapChar), (int) valseq
915 .getStart(), (int) valseq.getEnd());
917 Vobject datsetseq = (Vobject) valseq.getRefid();
918 if (datsetseq != null)
920 alseq.setDatasetSequence((SequenceI) getvObj2jv(datsetseq)); // exceptions
932 .error("Invalid dataset sequence id (null) for alignment sequence "
933 + valseq.getVorbaId());
935 bindjvvobj(alseq, valseq);
936 alseq.setVamsasId(valseq.getVorbaId().getId());
939 Vobject datsetseq = (Vobject) valseq.getRefid();
940 if (datsetseq != null)
942 if (datsetseq != alseq.getDatasetSequence())
946 alseq.setDatasetSequence((SequenceI) getvObj2jv(datsetseq)); // exceptions
948 return upFromdoc || modal;
951 private void initRangeAnnotationType(RangeAnnotation an,
952 AlignmentAnnotation alan, int[] gapMap)
954 Seg vSeg = new Seg();
956 vSeg.setInclusive(true);
957 vSeg.setEnd(gapMap.length);
960 // LATER: much of this is verbatim from the alignmentAnnotation
961 // method below. suggests refactoring to make rangeAnnotation the
963 an.setDescription(alan.description);
964 an.setLabel(alan.label);
965 an.setGroup(Integer.toString(alan.graphGroup));
974 AnnotationElement ae;
975 for (int a = 0; a < alan.annotations.length; a++)
977 if (alan.annotations[a] == null)
982 ae = new AnnotationElement();
983 ae.setDescription(alan.annotations[a].description);
984 ae.addGlyph(new Glyph());
985 ae.getGlyph(0).setContent(alan.annotations[a].displayCharacter); // assume
992 if (alan.graph != jalview.datamodel.AlignmentAnnotation.NO_GRAPH)
994 ae.addValue(alan.annotations[a].value);
996 ae.setPosition(gapMap[a] + 1); // position w.r.t. AlignmentSequence
998 if (alan.annotations[a].secondaryStructure != ' ')
1000 // we only write an annotation where it really exists.
1001 Glyph ss = new Glyph();
1003 .setDict(uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE);
1004 ss.setContent(String
1005 .valueOf(alan.annotations[a].secondaryStructure));
1008 an.addAnnotationElement(ae);
1013 private void saveDatasetSequenceAnnotation(HashMap AlSeqMaps,
1014 uk.ac.vamsas.objects.core.Sequence sref, AlignmentAnnotation alan)
1018 // objects.core.AlignmentSequence alsref = (uk.ac.vamsas.
1019 // objects.core.AlignmentSequence) sref;
1020 uk.ac.vamsas.objects.core.DataSetAnnotations an = (uk.ac.vamsas.objects.core.DataSetAnnotations) getjv2vObj(alan);
1021 int[] gapMap = getGapMap(AlSeqMaps, alan);
1024 an = new uk.ac.vamsas.objects.core.DataSetAnnotations();
1025 initRangeAnnotationType(an, alan, gapMap);
1027 an.setProvenance(dummyProvenance()); // get provenance as user
1028 // created, or jnet, or
1030 setAnnotationType(an, alan);
1031 an.setGroup(Integer.toString(alan.graphGroup)); // // JBPNote -
1039 if (alan.getThreshold() != null && alan.getThreshold().displayed)
1041 an.addProperty(newProperty(THRESHOLD, "float", ""
1042 + alan.getThreshold().value));
1043 if (alan.getThreshold().label != null)
1044 an.addProperty(newProperty(THRESHOLD + "Name", "string", ""
1045 + alan.getThreshold().label));
1047 ((DataSet) sref.getV_parent()).addDataSetAnnotations(an);
1048 bindjvvobj(alan, an);
1052 // update reference sequence Annotation
1053 if (an.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT LOCK
1056 // verify existing alignment sequence annotation is up to date
1057 System.out.println("update dataset sequence annotation.");
1061 // verify existing alignment sequence annotation is up to date
1063 .println("make new alignment dataset sequence annotation if modification has happened.");
1069 private int[] getGapMap(HashMap AlSeqMaps, AlignmentAnnotation alan)
1072 if (AlSeqMaps.containsKey(alan.sequenceRef))
1074 gapMap = (int[]) AlSeqMaps.get(alan.sequenceRef);
1078 gapMap = new int[alan.sequenceRef.getLength()];
1079 // map from alignment position to sequence position.
1080 int[] sgapMap = alan.sequenceRef.gapMap();
1081 for (int a = 0; a < sgapMap.length; a++)
1083 gapMap[sgapMap[a]] = a;
1089 private void saveAlignmentSequenceAnnotation(HashMap AlSeqMaps,
1090 AlignmentSequence alsref, AlignmentAnnotation alan)
1094 // objects.core.AlignmentSequence alsref = (uk.ac.vamsas.
1095 // objects.core.AlignmentSequence) sref;
1096 uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation an = (uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation) getjv2vObj(alan);
1097 int[] gapMap = getGapMap(AlSeqMaps, alan);
1100 an = new uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation();
1101 initRangeAnnotationType(an, alan, gapMap);
1103 * I mean here that we don't actually have a semantic 'type' for the
1104 * annotation (this might be - score, intrinsic property, measurement,
1105 * something extracted from another program, etc)
1107 an.setType(JALVIEW_ANNOTATION_ROW); // TODO: better fix
1108 // this rough guess ;)
1109 alsref.addAlignmentSequenceAnnotation(an);
1110 bindjvvobj(alan, an);
1111 // These properties are directly supported by the
1112 // AlignmentSequenceAnnotation type.
1113 setAnnotationType(an, alan);
1114 an.setProvenance(dummyProvenance()); // get provenance as user
1115 // created, or jnet, or
1120 // update reference sequence Annotation
1121 if (an.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT LOCK
1124 // verify existing alignment sequence annotation is up to date
1125 System.out.println("update alignment sequence annotation.");
1129 // verify existing alignment sequence annotation is up to date
1131 .println("make new alignment sequence annotation if modification has happened.");
1137 * set vamsas annotation object type from jalview annotation
1142 private void setAnnotationType(RangeAnnotation an,
1143 AlignmentAnnotation alan)
1145 if (an instanceof AlignmentSequenceAnnotation)
1147 if (alan.graph != AlignmentAnnotation.NO_GRAPH)
1149 ((AlignmentSequenceAnnotation) an).setGraph(true);
1153 ((AlignmentSequenceAnnotation) an).setGraph(false);
1156 if (an instanceof uk.ac.vamsas.objects.core.AlignmentAnnotation)
1158 if (alan.graph != AlignmentAnnotation.NO_GRAPH)
1160 ((uk.ac.vamsas.objects.core.AlignmentAnnotation) an).setGraph(true);
1164 ((uk.ac.vamsas.objects.core.AlignmentAnnotation) an)
1170 case AlignmentAnnotation.BAR_GRAPH:
1171 an.addProperty(newProperty(DISCRETE_ANNOTATION, "boolean", "true"));
1173 case AlignmentAnnotation.LINE_GRAPH:
1174 an.addProperty(newProperty(CONTINUOUS_ANNOTATION, "boolean", "true"));
1177 // don't add any kind of discrete or continous property info.
1181 private Property newProperty(String name, String type, String content)
1183 Property vProperty = new Property();
1184 vProperty.setName(name);
1187 vProperty.setType(type);
1191 vProperty.setType("String");
1193 vProperty.setContent(content);
1198 * correctly create a RangeAnnotation from a jalview sequence feature
1201 * (typically DataSetAnnotations or
1202 * AlignmentSequenceAnnotation)
1204 * (the feature to be mapped from)
1207 private RangeAnnotation getDSAnnotationFromJalview(RangeAnnotation dsa,
1208 SequenceFeature feature)
1210 dsa.setType(feature.getType());
1211 Seg vSeg = new Seg();
1212 vSeg.setStart(feature.getBegin());
1213 vSeg.setEnd(feature.getEnd());
1214 vSeg.setInclusive(true);
1216 dsa.setDescription(feature.getDescription());
1217 dsa.setStatus(feature.getStatus());
1218 if (feature.links != null && feature.links.size() > 0)
1220 for (int i = 0, iSize = feature.links.size(); i < iSize; i++)
1222 String link = (String) feature.links.elementAt(i);
1223 int sep = link.indexOf('|');
1226 Link vLink = new Link();
1229 vLink.setContent(link.substring(0, sep - 1));
1233 vLink.setContent("");
1235 vLink.setHref(link.substring(sep + 1)); // TODO: validate href.
1240 dsa.setGroup(feature.getFeatureGroup());
1245 * get start<end range of segment, adjusting for inclusivity flag and
1249 * @param ensureDirection
1250 * when true - always ensure start is less than end.
1251 * @return int[] { start, end, direction} where direction==1 for range running
1252 * from end to start.
1254 private int[] getSegRange(Seg visSeg, boolean ensureDirection)
1256 boolean incl = visSeg.getInclusive();
1257 // adjust for inclusive flag.
1258 int pol = (visSeg.getStart() <= visSeg.getEnd()) ? 1 : -1; // polarity of
1260 int start = visSeg.getStart() + (incl ? 0 : pol);
1261 int end = visSeg.getEnd() + (incl ? 0 : -pol);
1262 if (ensureDirection && pol == -1)
1264 // jalview doesn't deal with inverted ranges, yet.
1270 { start, end, pol < 0 ? 1 : 0 };
1276 * @return true if annotation is not to be stored in document
1278 private boolean isJalviewOnly(AlignmentAnnotation annotation)
1280 return annotation.label.equals("Quality")
1281 || annotation.label.equals("Conservation")
1282 || annotation.label.equals("Consensus");
1286 * This will return the first AlignFrame viewing AlignViewport av. It will
1287 * break if there are more than one AlignFrames viewing a particular av. This
1288 * also shouldn't be in the io package.
1291 * @return alignFrame for av
1293 public AlignFrame getAlignFrameFor(AlignViewport av)
1295 if (Desktop.desktop != null)
1297 javax.swing.JInternalFrame[] frames = Desktop.instance.getAllFrames();
1299 for (int t = 0; t < frames.length; t++)
1301 if (frames[t] instanceof AlignFrame)
1303 if (((AlignFrame) frames[t]).getViewport() == av)
1305 return (AlignFrame) frames[t];
1313 public void updateToJalview()
1315 VAMSAS _roots[] = cdoc.getVamsasRoots();
1317 for (int _root = 0; _root < _roots.length; _root++)
1319 VAMSAS root = _roots[_root];
1320 boolean newds = false;
1321 for (int _ds = 0, _nds = root.getDataSetCount(); _ds < _nds; _ds++)
1323 // ///////////////////////////////////
1325 DataSet dataset = root.getDataSet(_ds);
1326 int i, iSize = dataset.getSequenceCount();
1328 jalview.datamodel.Alignment jdataset = (jalview.datamodel.Alignment) getvObj2jv(dataset);
1330 if (jdataset == null)
1332 Cache.log.debug("Initialising new jalview dataset fields");
1334 dsseqs = new Vector();
1338 Cache.log.debug("Update jalview dataset from vamsas.");
1339 jremain = jdataset.getHeight();
1340 dsseqs = jdataset.getSequences();
1343 // TODO: test sequence merging - we preserve existing non vamsas
1344 // sequences but add in any new vamsas ones, and don't yet update any
1345 // sequence attributes
1346 for (i = 0; i < iSize; i++)
1348 Sequence vdseq = dataset.getSequence(i);
1349 jalview.datamodel.SequenceI dsseq = (SequenceI) getvObj2jv(vdseq);
1352 if (!dsseq.getSequenceAsString().equals(vdseq.getSequence()))
1355 "Broken! - mismatch of dataset sequence: and jalview internal dataset sequence.");
1361 dsseq = new jalview.datamodel.Sequence(dataset.getSequence(i)
1362 .getName(), dataset.getSequence(i).getSequence(),
1363 (int) dataset.getSequence(i).getStart(), (int) dataset
1364 .getSequence(i).getEnd());
1365 dsseq.setDescription(dataset.getSequence(i).getDescription());
1366 bindjvvobj(dsseq, dataset.getSequence(i));
1367 dsseq.setVamsasId(dataset.getSequence(i).getVorbaId().getId());
1370 if (vdseq.getDbRefCount() > 0)
1372 DbRef[] dbref = vdseq.getDbRef();
1373 for (int db = 0; db < dbref.length; db++)
1375 new jalview.io.vamsas.Dbref(this, dbref[db], vdseq, dsseq);
1378 dsseq.updatePDBIds();
1384 SequenceI[] seqs = new SequenceI[dsseqs.size()];
1385 for (i = 0, iSize = dsseqs.size(); i < iSize; i++)
1387 seqs[i] = (SequenceI) dsseqs.elementAt(i);
1388 dsseqs.setElementAt(null, i);
1390 jdataset = new jalview.datamodel.Alignment(seqs);
1391 Cache.log.debug("New vamsas dataset imported into jalview.");
1392 bindjvvobj(jdataset, dataset);
1395 // add any new dataset sequence feature annotations
1396 if (dataset.getDataSetAnnotations() != null)
1398 for (int dsa = 0; dsa < dataset.getDataSetAnnotationsCount(); dsa++)
1400 DataSetAnnotations dseta = dataset.getDataSetAnnotations(dsa);
1401 // TODO: deal with group annotation on datset sequences.
1402 if (dseta.getSeqRefCount() == 1)
1404 SequenceI dsSeq = (SequenceI) getvObj2jv((Vobject) dseta
1405 .getSeqRef(0)); // TODO: deal with group dataset
1409 jalview.bin.Cache.log
1410 .warn("Couldn't resolve jalview sequenceI for dataset object reference "
1411 + ((Vobject) dataset.getDataSetAnnotations(
1412 dsa).getSeqRef(0)).getVorbaId()
1417 if (dseta.getAnnotationElementCount() == 0)
1419 jalview.datamodel.SequenceFeature sf = (jalview.datamodel.SequenceFeature) getvObj2jv(dseta);
1423 .addSequenceFeature(sf = getJalviewSeqFeature(dseta));
1424 bindjvvobj(sf, dseta);
1429 // TODO: deal with alignmentAnnotation style annotation
1430 // appearing on dataset sequences.
1431 // JBPNote: we could just add them to all alignments but
1432 // that may complicate cross references in the jalview
1435 .warn("Ignoring dataset annotation with annotationElements. Not yet supported in jalview.");
1441 if (dataset.getAlignmentCount() > 0)
1443 // LOAD ALIGNMENTS from DATASET
1445 for (int al = 0, nal = dataset.getAlignmentCount(); al < nal; al++)
1447 uk.ac.vamsas.objects.core.Alignment alignment = dataset
1449 AlignViewport av = findViewport(alignment);
1451 jalview.datamodel.AlignmentI jal = null;
1454 jal = av.getAlignment();
1456 iSize = alignment.getAlignmentSequenceCount();
1457 boolean newal = (jal == null) ? true : false;
1458 boolean refreshal = false;
1459 Vector newasAnnots = new Vector();
1460 char gapChar = ' '; // default for new alignments read in from the
1464 dsseqs = jal.getSequences(); // for merge/update
1465 gapChar = jal.getGapCharacter();
1469 dsseqs = new Vector();
1471 char valGapchar = alignment.getGapChar().charAt(0);
1472 for (i = 0; i < iSize; i++)
1474 AlignmentSequence valseq = alignment.getAlignmentSequence(i);
1475 jalview.datamodel.Sequence alseq = (jalview.datamodel.Sequence) getvObj2jv(valseq);
1476 if (syncFromAlignmentSequence(valseq, valGapchar, gapChar,
1481 // updated to sequence from the document
1485 if (valseq.getAlignmentSequenceAnnotationCount() > 0)
1487 AlignmentSequenceAnnotation[] vasannot = valseq
1488 .getAlignmentSequenceAnnotation();
1489 for (int a = 0; a < vasannot.length; a++)
1491 jalview.datamodel.AlignmentAnnotation asa = (jalview.datamodel.AlignmentAnnotation) getvObj2jv(vasannot[a]); // TODO:
1499 int se[] = getBounds(vasannot[a]);
1500 asa = getjAlignmentAnnotation(jal, vasannot[a]);
1501 asa.setSequenceRef(alseq);
1502 asa.createSequenceMapping(alseq, se[0], false); // TODO:
1513 alseq.addAlignmentAnnotation(asa);
1514 bindjvvobj(asa, vasannot[a]);
1515 newasAnnots.add(asa);
1519 // update existing annotation - can do this in place
1520 if (vasannot[a].getModifiable() == null) // TODO: USE
1526 .info("UNIMPLEMENTED: not recovering user modifiable sequence alignment annotation");
1527 // TODO: should at least replace with new one - otherwise
1528 // things will break
1529 // basically do this:
1530 // int se[] = getBounds(vasannot[a]);
1531 // asa.update(getjAlignmentAnnotation(jal, vasannot[a]));
1532 // // update from another annotation object in place.
1533 // asa.createSequenceMapping(alseq, se[0], false);
1542 SequenceI[] seqs = new SequenceI[dsseqs.size()];
1543 for (i = 0, iSize = dsseqs.size(); i < iSize; i++)
1545 seqs[i] = (SequenceI) dsseqs.elementAt(i);
1546 dsseqs.setElementAt(null, i);
1548 jal = new jalview.datamodel.Alignment(seqs);
1549 Cache.log.debug("New vamsas alignment imported into jalview "
1550 + alignment.getVorbaId().getId());
1551 jal.setDataset(jdataset);
1553 if (newasAnnots != null && newasAnnots.size() > 0)
1555 // Add the new sequence annotations in to the alignment.
1556 for (int an = 0, anSize = newasAnnots.size(); an < anSize; an++)
1558 jal.addAnnotation((AlignmentAnnotation) newasAnnots
1560 // TODO: check if anything has to be done - like calling
1561 // adjustForAlignment or something.
1562 newasAnnots.setElementAt(null, an);
1566 // //////////////////////////////////////////
1567 // //LOAD ANNOTATIONS FOR THE ALIGNMENT
1568 // ////////////////////////////////////
1569 if (alignment.getAlignmentAnnotationCount() > 0)
1571 uk.ac.vamsas.objects.core.AlignmentAnnotation[] an = alignment
1572 .getAlignmentAnnotation();
1574 for (int j = 0; j < an.length; j++)
1576 jalview.datamodel.AlignmentAnnotation jan = (jalview.datamodel.AlignmentAnnotation) getvObj2jv(an[j]);
1579 // update or stay the same.
1580 // TODO: should at least replace with a new one - otherwise
1581 // things will break
1582 // basically do this:
1583 // jan.update(getjAlignmentAnnotation(jal, an[a])); // update
1584 // from another annotation object in place.
1587 .debug("update from vamsas alignment annotation to existing jalview alignment annotation.");
1588 if (an[j].getModifiable() == null) // TODO: USE VAMSAS
1589 // LIBRARY OBJECT LOCK
1592 // TODO: user defined annotation is totally mutable... - so
1593 // load it up or throw away if locally edited.
1595 .info("NOT IMPLEMENTED - Recovering user-modifiable annotation - yet...");
1597 // TODO: compare annotation element rows
1598 // TODO: compare props.
1602 jan = getjAlignmentAnnotation(jal, an[j]);
1603 jal.addAnnotation(jan);
1604 bindjvvobj(jan, an[j]);
1608 AlignFrame alignFrame;
1611 Cache.log.debug("New alignframe for alignment "
1612 + alignment.getVorbaId());
1613 // ///////////////////////////////
1614 // construct alignment view
1615 alignFrame = new AlignFrame(jal, AlignFrame.DEFAULT_WIDTH,
1616 AlignFrame.DEFAULT_HEIGHT);
1617 av = alignFrame.getViewport();
1618 String title = alignment.getProvenance().getEntry(
1619 alignment.getProvenance().getEntryCount() - 1)
1621 if (alignment.getPropertyCount() > 0)
1623 for (int p = 0, pe = alignment.getPropertyCount(); p < pe; p++)
1625 if (alignment.getProperty(p).getName().equals("title"))
1627 title = alignment.getProperty(p).getContent();
1631 // TODO: automatically create meaningful title for a vamsas
1632 // alignment using its provenance.
1633 if (Cache.log.isDebugEnabled())
1635 title = title + "(" + alignment.getVorbaId() + ")";
1638 jalview.gui.Desktop.addInternalFrame(alignFrame, title,
1639 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
1640 bindjvvobj(av.getSequenceSetId(), alignment);
1644 // find the alignFrame for jal.
1645 // TODO: fix this so we retrieve the alignFrame handing av
1647 alignFrame = getAlignFrameFor(av);
1650 av.alignmentChanged(alignFrame.alignPanel);
1654 // /////////////////////////////////////
1655 if (alignment.getTreeCount() > 0)
1658 for (int t = 0; t < alignment.getTreeCount(); t++)
1660 jalview.io.vamsas.Tree vstree = new jalview.io.vamsas.Tree(
1661 this, alignFrame, alignment.getTree(t));
1662 TreePanel tp = null;
1663 if (vstree.isValidTree())
1665 tp = alignFrame.ShowNewickTree(vstree.getNewickTree(),
1666 vstree.getTitle(), vstree.getInputData(), 600,
1667 500, t * 20 + 50, t * 20 + 50);
1672 bindjvvobj(tp, alignment.getTree(t));
1675 vstree.UpdateSequenceTreeMap(tp);
1676 } catch (RuntimeException e)
1678 Cache.log.warn("update of labels failed.", e);
1683 Cache.log.warn("Cannot create tree for tree " + t
1685 + alignment.getTree(t).getVorbaId());
1693 // we do sequenceMappings last because they span all datasets in a vamsas
1695 for (int _ds = 0, _nds = root.getDataSetCount(); _ds < _nds; _ds++)
1697 DataSet dataset = root.getDataSet(_ds);
1698 if (dataset.getSequenceMappingCount() > 0)
1700 for (int sm = 0, smCount = dataset.getSequenceMappingCount(); sm < smCount; sm++)
1702 Rangetype seqmap = new jalview.io.vamsas.Sequencemapping(this,
1703 dataset.getSequenceMapping(sm));
1710 public AlignViewport findViewport(Alignment alignment)
1712 AlignViewport av = null;
1713 AlignViewport[] avs = findViewportForSequenceSetId((String) getvObj2jv(alignment));
1721 private AlignViewport[] findViewportForSequenceSetId(String sequenceSetId)
1723 Vector viewp = new Vector();
1724 if (Desktop.desktop != null)
1726 javax.swing.JInternalFrame[] frames = Desktop.instance.getAllFrames();
1728 for (int t = 0; t < frames.length; t++)
1730 if (frames[t] instanceof AlignFrame)
1732 if (((AlignFrame) frames[t]).getViewport().getSequenceSetId()
1733 .equals(sequenceSetId))
1735 viewp.addElement(((AlignFrame) frames[t]).getViewport());
1739 if (viewp.size() > 0)
1741 AlignViewport[] vp = new AlignViewport[viewp.size()];
1749 // bitfields - should be a template in j1.5
1750 private static int HASSECSTR = 0;
1752 private static int HASVALS = 1;
1754 private static int HASHPHOB = 2;
1756 private static int HASDC = 3;
1758 private static int HASDESCSTR = 4;
1760 private static int HASTWOSTATE = 5; // not used yet.
1763 * parses the AnnotationElements - if they exist - into
1764 * jalview.datamodel.Annotation[] rows Two annotation rows are made if there
1765 * are distinct annotation for both at 'pos' and 'after pos' at any particular
1769 * @return { boolean[static int constants ], int[ae.length] - map to annotated
1770 * object frame, jalview.datamodel.Annotation[],
1771 * jalview.datamodel.Annotation[] (after)}
1773 private Object[] parseRangeAnnotation(
1774 uk.ac.vamsas.objects.core.RangeAnnotation annotation)
1776 // set these attributes by looking in the annotation to decide what kind of
1777 // alignment annotation rows will be made
1778 // TODO: potentially we might make several annotation rows from one vamsas
1779 // alignment annotation. the jv2Vobj binding mechanism
1780 // may not quite cope with this (without binding an array of annotations to
1781 // a vamsas alignment annotation)
1782 // summary flags saying what we found over the set of annotation rows.
1783 boolean[] AeContent = new boolean[]
1784 { false, false, false, false, false };
1785 int[] rangeMap = getMapping(annotation);
1786 jalview.datamodel.Annotation[][] anot = new jalview.datamodel.Annotation[][]
1787 { new jalview.datamodel.Annotation[rangeMap.length],
1788 new jalview.datamodel.Annotation[rangeMap.length] };
1789 boolean mergeable = true; // false if 'after positions cant be placed on
1790 // same annotation row as positions.
1792 if (annotation.getAnnotationElementCount() > 0)
1794 AnnotationElement ae[] = annotation.getAnnotationElement();
1795 for (int aa = 0; aa < ae.length; aa++)
1797 int pos = (int) ae[aa].getPosition() - 1; // pos counts from 1 to
1798 // (|seg.start-seg.end|+1)
1799 if (pos >= 0 && pos < rangeMap.length)
1801 int row = ae[aa].getAfter() ? 1 : 0;
1802 if (anot[row][pos] != null)
1804 // only time this should happen is if the After flag is set.
1805 Cache.log.debug("Ignoring duplicate annotation site at " + pos);
1808 if (anot[1 - row][pos] != null)
1813 if (ae[aa].getDescription() != null)
1815 desc = ae[aa].getDescription();
1816 if (desc.length() > 0)
1818 // have imported valid description string
1819 AeContent[HASDESCSTR] = true;
1822 String dc = null; // ae[aa].getDisplayCharacter()==null ? "dc" :
1823 // ae[aa].getDisplayCharacter();
1824 String ss = null; // ae[aa].getSecondaryStructure()==null ? "ss" :
1825 // ae[aa].getSecondaryStructure();
1826 java.awt.Color colour = null;
1827 if (ae[aa].getGlyphCount() > 0)
1829 Glyph[] glyphs = ae[aa].getGlyph();
1830 for (int g = 0; g < glyphs.length; g++)
1835 uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE))
1837 ss = glyphs[g].getContent();
1838 AeContent[HASSECSTR] = true;
1843 uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_HD_HYDRO))
1845 Cache.log.debug("ignoring hydrophobicity glyph marker.");
1846 AeContent[HASHPHOB] = true;
1847 char c = (dc = glyphs[g].getContent()).charAt(0);
1848 // dc may get overwritten - but we still set the colour.
1849 colour = new java.awt.Color(c == '+' ? 255 : 0,
1850 c == '.' ? 255 : 0, c == '-' ? 255 : 0);
1853 else if (glyphs[g].getDict().equals(
1854 uk.ac.vamsas.objects.utils.GlyphDictionary.DEFAULT))
1856 dc = glyphs[g].getContent();
1857 AeContent[HASDC] = true;
1862 .debug("IMPLEMENTATION TODO: Ignoring unknown glyph type "
1863 + glyphs[g].getDict());
1868 if (ae[aa].getValueCount() > 0)
1870 AeContent[HASVALS] = true;
1871 if (ae[aa].getValueCount() > 1)
1873 Cache.log.warn("ignoring additional "
1874 + (ae[aa].getValueCount() - 1)
1875 + "values in annotation element.");
1877 val = ae[aa].getValue(0);
1881 anot[row][pos] = new jalview.datamodel.Annotation(
1882 (dc != null) ? dc : "", desc, (ss != null) ? ss
1883 .charAt(0) : ' ', val);
1887 anot[row][pos] = new jalview.datamodel.Annotation(
1888 (dc != null) ? dc : "", desc, (ss != null) ? ss
1889 .charAt(0) : ' ', val, colour);
1894 Cache.log.warn("Ignoring out of bound annotation element " + aa
1895 + " in " + annotation.getVorbaId().getId());
1898 // decide on how many annotation rows are needed.
1901 for (int i = 0; i < anot[0].length; i++)
1903 if (anot[1][i] != null)
1905 anot[0][i] = anot[1][i];
1906 anot[0][i].description = anot[0][i].description + " (after)";
1907 AeContent[HASDESCSTR] = true; // we have valid description string
1916 for (int i = 0; i < anot[0].length; i++)
1918 anot[1][i].description = anot[1][i].description + " (after)";
1922 { AeContent, rangeMap, anot[0], anot[1] };
1926 // no annotations to parse. Just return an empty annotationElement[]
1929 { AeContent, rangeMap, anot[0], anot[1] };
1936 * the jalview alignment to which the annotation will be
1937 * attached (ideally - freshly updated from corresponding
1940 * @return unbound jalview alignment annotation object.
1942 private jalview.datamodel.AlignmentAnnotation getjAlignmentAnnotation(
1943 jalview.datamodel.AlignmentI jal,
1944 uk.ac.vamsas.objects.core.RangeAnnotation annotation)
1946 if (annotation == null)
1951 // hasSequenceRef=annotation.getClass().equals(uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation.class);
1952 // boolean hasProvenance=hasSequenceRef ||
1953 // (annotation.getClass().equals(uk.ac.vamsas.objects.core.AlignmentAnnotation.class));
1955 * int se[] = getBounds(annotation); if (se==null) se=new int[]
1956 * {0,jal.getWidth()-1};
1958 Object[] parsedRangeAnnotation = parseRangeAnnotation(annotation);
1959 String a_label = annotation.getLabel();
1960 String a_descr = annotation.getDescription();
1961 GraphLine gl = null;
1963 boolean interp = true; // cleared if annotation is DISCRETE
1964 // set type and other attributes from properties
1965 if (annotation.getPropertyCount() > 0)
1967 // look for special jalview properties
1968 uk.ac.vamsas.objects.core.Property[] props = annotation.getProperty();
1969 for (int p = 0; p < props.length; p++)
1971 if (props[p].getName().equalsIgnoreCase(DISCRETE_ANNOTATION))
1973 type = AlignmentAnnotation.BAR_GRAPH;
1976 else if (props[p].getName().equalsIgnoreCase(CONTINUOUS_ANNOTATION))
1978 type = AlignmentAnnotation.LINE_GRAPH;
1980 else if (props[p].getName().equalsIgnoreCase(THRESHOLD))
1985 val = new Float(props[p].getContent());
1986 } catch (Exception e)
1988 Cache.log.warn("Failed to parse threshold property");
1993 gl = new GraphLine(val.floatValue(), "", java.awt.Color.black);
1997 gl.value = val.floatValue();
2000 else if (props[p].getName().equalsIgnoreCase(THRESHOLD + "Name"))
2003 gl = new GraphLine(0, "", java.awt.Color.black);
2004 gl.label = props[p].getContent();
2008 jalview.datamodel.AlignmentAnnotation jan = null;
2009 if (a_label == null || a_label.length() == 0)
2011 a_label = annotation.getType();
2012 if (a_label.length() == 0)
2014 a_label = "Unamed annotation";
2017 if (a_descr == null || a_descr.length() == 0)
2019 a_descr = "Annotation of type '" + annotation.getType() + "'";
2021 if (parsedRangeAnnotation == null)
2024 .debug("Inserting empty annotation row elements for a whole-alignment annotation.");
2028 if (parsedRangeAnnotation[3] != null)
2030 Cache.log.warn("Ignoring 'After' annotation row in "
2031 + annotation.getVorbaId());
2033 jalview.datamodel.Annotation[] arow = (jalview.datamodel.Annotation[]) parsedRangeAnnotation[2];
2034 boolean[] has = (boolean[]) parsedRangeAnnotation[0];
2035 // VAMSAS: getGraph is only on derived annotation for alignments - in this
2036 // way its 'odd' - there is already an existing TODO about removing this
2037 // flag as being redundant
2040 * ((annotation.getClass().equals(uk.ac.vamsas.objects.core.AlignmentAnnotation.class) &&
2041 * ((uk.ac.vamsas.objects.core.AlignmentAnnotation)annotation).getGraph()) ||
2042 * (hasSequenceRef=true &&
2043 * ((uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation)annotation).getGraph())) {
2049 type = jalview.datamodel.AlignmentAnnotation.BAR_GRAPH; // default
2055 // no hints - so we ensure HPHOB display is like this.
2056 type = jalview.datamodel.AlignmentAnnotation.BAR_GRAPH;
2059 // make bounds and automatic description strings for jalview user's
2060 // benefit (these shouldn't be written back to vamsas document)
2061 boolean first = true;
2062 float min = 0, max = 1;
2064 for (int i = 0; i < arow.length; i++)
2066 if (arow[i] != null)
2068 if (i - lastval > 1 && interp)
2070 // do some interpolation *between* points
2071 if (arow[lastval] != null)
2073 float interval = arow[i].value - arow[lastval].value;
2074 interval /= i - lastval;
2075 float base = arow[lastval].value;
2076 for (int ip = lastval + 1, np = 0; ip < i; np++, ip++)
2078 arow[ip] = new jalview.datamodel.Annotation("", "", ' ',
2079 interval * np + base);
2080 // NB - Interpolated points don't get a tooltip and
2086 // check range - shouldn't we have a min and max property in the
2087 // annotation object ?
2090 min = max = arow[i].value;
2095 if (arow[i].value < min)
2097 min = arow[i].value;
2099 else if (arow[i].value > max)
2101 max = arow[i].value;
2104 // make tooltip and display char value
2105 if (!has[HASDESCSTR])
2107 arow[i].description = arow[i].value + "";
2113 if (arow[i].description != null
2114 && arow[i].description.length() < 3)
2116 // copy over the description as the display char.
2117 arow[i].displayCharacter = new String(arow[i].description);
2122 // mark the position as a point used for the interpolation.
2123 arow[i].displayCharacter = arow[i].value + "";
2128 jan = new jalview.datamodel.AlignmentAnnotation(a_label, a_descr,
2129 arow, min, max, type);
2133 if (annotation.getAnnotationElementCount() == 0)
2135 // empty annotation array
2136 // TODO: alignment 'features' compare rangeType spec to alignment
2137 // width - if it is not complete, then mark regions on the annotation
2140 jan = new jalview.datamodel.AlignmentAnnotation(a_label, a_descr,
2142 jan.setThreshold(null);
2144 if (annotation.getLinkCount() > 0)
2146 Cache.log.warn("Ignoring " + annotation.getLinkCount()
2147 + "links added to AlignmentAnnotation.");
2149 if (annotation.getModifiable() == null
2150 || annotation.getModifiable().length() == 0) // TODO: USE VAMSAS
2154 jan.editable = true;
2158 if (annotation.getGroup() != null
2159 && annotation.getGroup().length() > 0)
2161 jan.graphGroup = Integer.parseInt(annotation.getGroup()); // TODO:
2169 } catch (Exception e)
2172 .info("UNIMPLEMENTED : Couldn't parse non-integer group value for setting graphGroup correctly.");
2181 private SequenceFeature getJalviewSeqFeature(RangeAnnotation dseta)
2183 int[] se = getBounds(dseta);
2184 SequenceFeature sf = new jalview.datamodel.SequenceFeature(dseta
2185 .getType(), dseta.getDescription(), dseta.getStatus(), se[0],
2186 se[1], dseta.getGroup());
2187 if (dseta.getLinkCount() > 0)
2189 Link[] links = dseta.getLink();
2190 for (int i = 0; i < links.length; i++)
2192 sf.addLink(links[i].getContent() + "|" + links[i].getHref());
2199 * get real bounds of a RangeType's specification. start and end are an
2200 * inclusive range within which all segments and positions lie. TODO: refactor
2204 * @return int[] { start, end}
2206 private int[] getBounds(RangeType dseta)
2211 if (dseta.getSegCount() > 0 && dseta.getPosCount() > 0)
2214 "Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
2216 if (dseta.getSegCount() > 0)
2218 se = getSegRange(dseta.getSeg(0), true);
2219 for (int s = 1, sSize = dseta.getSegCount(); s < sSize; s++)
2221 int nse[] = getSegRange(dseta.getSeg(s), true);
2232 if (dseta.getPosCount() > 0)
2234 // could do a polarity for pos range too. and pass back indication of
2236 int pos = dseta.getPos(0).getI();
2239 for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
2241 pos = dseta.getPos(p).getI();
2258 * map from a rangeType's internal frame to the referenced object's coordinate
2262 * @return int [] { ref(pos)...} for all pos in rangeType's frame.
2264 private int[] getMapping(RangeType dseta)
2266 Vector posList = new Vector();
2270 if (dseta.getSegCount() > 0 && dseta.getPosCount() > 0)
2273 "Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
2275 if (dseta.getSegCount() > 0)
2277 for (int s = 0, sSize = dseta.getSegCount(); s < sSize; s++)
2279 se = getSegRange(dseta.getSeg(s), false);
2280 int se_end = se[1 - se[2]] + (se[2] == 0 ? 1 : -1);
2281 for (int p = se[se[2]]; p != se_end; p += se[2] == 0 ? 1 : -1)
2283 posList.add(new Integer(p));
2287 else if (dseta.getPosCount() > 0)
2289 int pos = dseta.getPos(0).getI();
2291 for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
2293 pos = dseta.getPos(p).getI();
2294 posList.add(new Integer(pos));
2298 if (posList != null && posList.size() > 0)
2300 int[] range = new int[posList.size()];
2301 for (int i = 0; i < range.length; i++)
2303 range[i] = ((Integer) posList.elementAt(i)).intValue();
2314 * where the from range is the local mapped range, and the to
2315 * range is the 'mapped' range in the MapRangeType
2322 private jalview.util.MapList parsemapType(MapType maprange, int localu,
2325 jalview.util.MapList ml = null;
2326 int[] localRange = getMapping(maprange.getLocal());
2327 int[] mappedRange = getMapping(maprange.getMapped());
2328 long lu = maprange.getLocal().hasUnit() ? maprange.getLocal().getUnit()
2330 long mu = maprange.getMapped().hasUnit() ? maprange.getMapped()
2331 .getUnit() : mappedu;
2332 ml = new jalview.util.MapList(localRange, mappedRange, (int) lu,
2338 * initialise a range type object from a set of start/end inclusive intervals
2343 private void initRangeType(RangeType mrt, int[] range)
2345 for (int i = 0; i < range.length; i += 2)
2347 Seg vSeg = new Seg();
2348 vSeg.setStart(range[i]);
2349 vSeg.setEnd(range[i + 1]);
2355 * initialise a MapType object from a MapList object.
2361 private void initMapType(MapType maprange, jalview.util.MapList ml,
2364 maprange.setLocal(new Local());
2365 maprange.setMapped(new Mapped());
2366 initRangeType(maprange.getLocal(), ml.getFromRanges());
2367 initRangeType(maprange.getMapped(), ml.getToRanges());
2370 maprange.getLocal().setUnit(ml.getFromRatio());
2371 maprange.getLocal().setUnit(ml.getToRatio());
2376 * not needed now. Provenance getVamsasProvenance(jalview.datamodel.Provenance
2377 * jprov) { jalview.datamodel.ProvenanceEntry[] entries = null; // TODO: fix
2378 * App and Action here. Provenance prov = new Provenance();
2379 * org.exolab.castor.types.Date date = new org.exolab.castor.types.Date( new
2380 * java.util.Date()); Entry provEntry;
2382 * if (jprov != null) { entries = jprov.getEntries(); for (int i = 0; i <
2383 * entries.length; i++) { provEntry = new Entry(); try { date = new
2384 * org.exolab.castor.types.Date(entries[i].getDate()); } catch (Exception ex) {
2385 * ex.printStackTrace();
2387 * date = new org.exolab.castor.types.Date(entries[i].getDate()); }
2388 * provEntry.setDate(date); provEntry.setUser(entries[i].getUser());
2389 * provEntry.setAction(entries[i].getAction()); prov.addEntry(provEntry); } }
2390 * else { provEntry = new Entry(); provEntry.setDate(date);
2391 * provEntry.setUser(System.getProperty("user.name")); // TODO: ext string
2392 * provEntry.setApp("JVAPP"); // TODO: ext string provEntry.setAction(action);
2393 * prov.addEntry(provEntry); }
2397 jalview.datamodel.Provenance getJalviewProvenance(Provenance prov)
2399 // TODO: fix App and Action entries and check use of provenance in jalview.
2400 jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();
2401 for (int i = 0; i < prov.getEntryCount(); i++)
2403 jprov.addEntry(prov.getEntry(i).getUser(), prov.getEntry(i)
2404 .getAction(), prov.getEntry(i).getDate(), prov.getEntry(i)
2413 * @return default initial provenance list for a Jalview created vamsas
2416 Provenance dummyProvenance()
2418 return dummyProvenance(null);
2421 Entry dummyPEntry(String action)
2423 Entry entry = new Entry();
2424 entry.setApp(this.provEntry.getApp());
2427 entry.setAction(action);
2431 entry.setAction("created.");
2433 entry.setDate(new java.util.Date());
2434 entry.setUser(this.provEntry.getUser());
2438 Provenance dummyProvenance(String action)
2440 Provenance prov = new Provenance();
2441 prov.addEntry(dummyPEntry(action));
2445 Entry addProvenance(Provenance p, String action)
2447 Entry dentry = dummyPEntry(action);
2452 public Entry getProvEntry()
2457 public IClientDocument getClientDocument()
2462 public IdentityHashMap getJvObjectBinding()
2467 public Hashtable getVamsasObjectBinding()
2472 public void storeSequenceMappings(AlignViewport viewport, String title)
2475 AlignViewport av = viewport;
2478 jalview.datamodel.AlignmentI jal = av.getAlignment();
2479 // /////////////////////////////////////////
2481 DataSet dataset = null;
2482 if (jal.getDataset() == null)
2484 Cache.log.warn("Creating new dataset for an alignment.");
2485 jal.setDataset(null);
2487 dataset = (DataSet) getjv2vObj(jal.getDataset());
2488 // Store any sequence mappings.
2489 if (av.getAlignment().getCodonFrames() != null
2490 && av.getAlignment().getCodonFrames().length > 0)
2492 jalview.datamodel.AlignedCodonFrame[] cframes = av.getAlignment()
2494 for (int cf = 0; cf < cframes.length; cf++)
2496 if (cframes[cf].getdnaSeqs().length > 0)
2498 jalview.datamodel.SequenceI[] dmps = cframes[cf].getdnaSeqs();
2499 jalview.datamodel.Mapping[] mps = cframes[cf].getProtMappings();
2500 for (int smp = 0; smp < mps.length; smp++)
2502 uk.ac.vamsas.objects.core.SequenceType mfrom = (SequenceType) getjv2vObj(dmps[smp]);
2505 new jalview.io.vamsas.Sequencemapping(this, mps[smp],
2511 .warn("NO Vamsas Binding for local sequence! NOT CREATING MAPPING FOR "
2512 + dmps[smp].getDisplayId(true)
2514 + mps[smp].getTo().getName());
2520 } catch (Exception e)
2522 throw new Exception("Couldn't store sequence mappings for " + title,