2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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
21 import jalview.bin.Cache;
22 import jalview.datamodel.AlignmentAnnotation;
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.AlignmentView;
25 import jalview.datamodel.DBRefEntry;
26 import jalview.datamodel.GraphLine;
27 import jalview.datamodel.SequenceFeature;
28 import jalview.datamodel.SequenceI;
29 import jalview.gui.AlignFrame;
30 import jalview.gui.AlignViewport;
31 import jalview.gui.Desktop;
32 import jalview.gui.TreePanel;
33 import jalview.io.vamsas.DatastoreItem;
34 import jalview.io.vamsas.Rangetype;
36 import java.util.Enumeration;
37 import java.util.HashMap;
38 import java.util.Hashtable;
39 import java.util.IdentityHashMap;
40 import java.util.Vector;
42 import uk.ac.vamsas.client.*;
43 import uk.ac.vamsas.objects.core.*;
48 * org.exolab.castor.util.LocalConfiguration.getInstance().getProperties().setProperty(
49 * "org.exolab.castor.serializer", "org.apache.xml.serialize.XMLSerilazizer"); }
53 public class VamsasAppDatastore
56 * Type used for general jalview generated annotation added to vamsas document
58 public static final String JALVIEW_ANNOTATION_ROW = "JalviewAnnotation";
61 * AlignmentAnnotation property to indicate that values should not be
64 public static final String DISCRETE_ANNOTATION = "discrete";
67 * continuous property - optional to specify that annotation should be
68 * represented as a continous graph line
70 private static final String CONTINUOUS_ANNOTATION = "continuous";
72 private static final String THRESHOLD = "threshold";
74 Entry provEntry = null;
80 IdentityHashMap jv2vobj;
82 Hashtable alignRDHash;
84 public VamsasAppDatastore(IClientDocument cdoc, Hashtable vobj2jv,
85 IdentityHashMap jv2vobj, Entry provEntry, Hashtable alignRDHash)
88 this.vobj2jv = vobj2jv;
89 this.jv2vobj = jv2vobj;
90 this.provEntry = provEntry;
91 this.alignRDHash = alignRDHash;
95 * @return the Vobject bound to Jalview datamodel object
97 protected Vobject getjv2vObj(Object jvobj)
99 if (jv2vobj.containsKey(jvobj))
101 return cdoc.getObject((VorbaId) jv2vobj.get(jvobj));
103 if (Cache.log.isDebugEnabled())
105 Cache.log.debug("Returning null VorbaID binding for jalview object "
114 * @return Jalview datamodel object bound to the vamsas document object
116 protected Object getvObj2jv(uk.ac.vamsas.client.Vobject vobj)
118 VorbaId id = vobj.getVorbaId();
121 id = cdoc.registerObject(vobj);
123 .debug("Registering new object and returning null for getvObj2jv");
126 if (vobj2jv.containsKey(vobj.getVorbaId()))
128 return vobj2jv.get(vobj.getVorbaId());
133 protected void bindjvvobj(Object jvobj, uk.ac.vamsas.client.Vobject vobj)
135 VorbaId id = vobj.getVorbaId();
138 id = cdoc.registerObject(vobj);
139 if (id == null || vobj.getVorbaId() == null
140 || cdoc.getObject(id) != vobj)
142 Cache.log.error("Failed to get id for "
143 + (vobj.isRegisterable() ? "registerable"
144 : "unregisterable") + " object " + vobj);
148 if (vobj2jv.containsKey(vobj.getVorbaId())
149 && !((VorbaId) vobj2jv.get(vobj.getVorbaId())).equals(jvobj))
152 "Warning? Overwriting existing vamsas id binding for "
153 + vobj.getVorbaId(), new Exception(
154 "Overwriting vamsas id binding."));
156 else if (jv2vobj.containsKey(jvobj)
157 && !((VorbaId) jv2vobj.get(jvobj)).equals(vobj.getVorbaId()))
160 "Warning? Overwriting existing jalview object binding for "
161 + jvobj, new Exception(
162 "Overwriting jalview object binding."));
165 * Cache.log.error("Attempt to make conflicting object binding! "+vobj+" id "
166 * +vobj.getVorbaId()+" already bound to "+getvObj2jv(vobj)+" and "+jvobj+"
167 * already bound to "+getjv2vObj(jvobj),new Exception("Excessive call to
170 // we just update the hash's regardless!
171 Cache.log.debug("Binding " + vobj.getVorbaId() + " to " + jvobj);
172 vobj2jv.put(vobj.getVorbaId(), jvobj);
173 // JBPNote - better implementing a hybrid invertible hash.
174 jv2vobj.put(jvobj, vobj.getVorbaId());
178 * put the alignment viewed by AlignViewport into cdoc.
181 * alignViewport to be stored
183 * title for alignment
185 public void storeVAMSAS(AlignViewport av, String aFtitle)
189 jalview.datamodel.AlignmentI jal = av.getAlignment();
191 VAMSAS root = null; // will be resolved based on Dataset Parent.
192 // /////////////////////////////////////////
194 DataSet dataset = null;
195 if (jal.getDataset() == null)
197 Cache.log.warn("Creating new dataset for an alignment.");
198 jal.setDataset(null);
200 dataset = (DataSet) getjv2vObj(jal.getDataset());
203 // it might be that one of the dataset sequences does actually have a
204 // binding, so search for it indirectly.
205 jalview.datamodel.SequenceI[] jdatset = jal.getDataset()
206 .getSequencesArray();
207 for (int i = 0; i < jdatset.length; i++)
209 Vobject vbound = getjv2vObj(jdatset[i]);
212 if (vbound instanceof uk.ac.vamsas.objects.core.Sequence)
216 dataset = (DataSet) vbound.getV_parent();
220 if (dataset != vbound.getV_parent())
223 "IMPLEMENTATION ERROR: Cannot map an alignment of sequences from datasets into the vamsas document.");
224 // This occurs because the dataset for the alignment we are
235 // we create a new dataset on the default vamsas root.
236 root = cdoc.getVamsasRoots()[0]; // default vamsas root for modifying.
237 dataset = new DataSet();
238 root.addDataSet(dataset);
239 bindjvvobj(jal.getDataset(), dataset);
240 dataset.setProvenance(dummyProvenance());
241 // dataset.getProvenance().addEntry(provEntry);
246 root = (VAMSAS) dataset.getV_parent();
251 // set new dataset and alignment sequences based on alignment Nucleotide
253 // this *will* break when alignment contains both nucleotide and amino
255 String dict = jal.isNucleotide() ? uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA
256 : uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_AA;
257 Vector dssmods = new Vector();
258 for (int i = 0; i < jal.getHeight(); i++)
260 SequenceI sq = jal.getSequenceAt(i).getDatasetSequence(); // only insert
264 sequence = (Sequence) getjv2vObj(sq);
265 if (sequence == null)
267 sequence = new Sequence();
268 bindjvvobj(sq, sequence);
269 sq.setVamsasId(sequence.getVorbaId().getId());
270 sequence.setSequence(sq.getSequenceAsString());
271 sequence.setDictionary(dict);
272 sequence.setName(sq.getName());
273 sequence.setStart(sq.getStart());
274 sequence.setEnd(sq.getEnd());
275 sequence.setDescription(sq.getDescription());
276 dataset.addSequence(sequence);
277 dssmods.addElement(dssmods);
281 boolean dsmod = false;
282 // verify and update principal attributes.
283 if (sq.getDescription() != null
284 && (sequence.getDescription() == null || !sequence
285 .getDescription().equals(sq.getDescription())))
287 sequence.setDescription(sq.getDescription());
290 if (sequence.getSequence() == null
291 || !sequence.getSequence().equals(
292 sq.getSequenceAsString()))
294 if (sequence.getStart() != sq.getStart()
295 || sequence.getEnd() != sq.getEnd())
297 // update modified sequence.
298 sequence.setSequence(sq.getSequenceAsString());
299 sequence.setStart(sq.getStart());
300 sequence.setEnd(sq.getEnd());
304 if (!dict.equals(sequence.getDictionary()))
306 sequence.setDictionary(dict);
309 if (!sequence.getName().equals(sq.getName()))
311 sequence.setName(sq.getName());
316 dssmods.addElement(sequence);
319 // add or update any new features/references on dataset sequence
320 if (sq.getSequenceFeatures() != null)
322 int sfSize = sq.getSequenceFeatures().length;
324 for (int sf = 0; sf < sfSize; sf++)
326 // TODO: update/modifiable synchronizer
327 jalview.datamodel.SequenceFeature feature = (jalview.datamodel.SequenceFeature) sq
328 .getSequenceFeatures()[sf];
330 DataSetAnnotations dsa = (DataSetAnnotations) getjv2vObj(feature);
333 dsa = (DataSetAnnotations) getDSAnnotationFromJalview(
334 new DataSetAnnotations(), feature);
335 if (dsa.getProvenance() == null)
337 dsa.setProvenance(new Provenance());
339 addProvenance(dsa.getProvenance(), "created"); // JBPNote - need
341 dsa.addSeqRef(sequence); // we have just created this annotation
342 // - so safe to use this
343 bindjvvobj(feature, dsa);
344 dataset.addDataSetAnnotations(dsa);
348 // todo: verify and update dataset annotations for sequence
349 System.out.println("update dataset sequence annotations.");
353 if (sq.getDatasetSequence()==null && sq.getDBRef() != null)
355 // only sync database references for dataset sequences
356 DBRefEntry[] entries = sq.getDBRef();
357 jalview.datamodel.DBRefEntry dbentry;
358 for (int db = 0; db < entries.length; db++)
360 Rangetype dbr = new jalview.io.vamsas.Dbref(this,
361 dbentry = entries[db], sq, sequence);
366 if (dssmods.size() > 0)
370 Entry pentry = this.addProvenance(dataset.getProvenance(),
371 "updated sequences");
372 // pentry.addInput(vInput); could write in which sequences were
374 dssmods.removeAllElements();
377 // dataset.setProvenance(getVamsasProvenance(jal.getDataset().getProvenance()));
378 // ////////////////////////////////////////////
379 if (!av.getAlignment().isAligned())
380 return; // TODO: trees could be written - but for the moment we just
382 // ////////////////////////////////////////////
383 // Save the Alignments
385 Alignment alignment = (Alignment) getjv2vObj(av.getSequenceSetId()); // bind
398 if (alignment == null)
400 alignment = new Alignment();
401 bindjvvobj(av.getSequenceSetId(), alignment);
402 if (alignment.getProvenance() == null)
404 alignment.setProvenance(new Provenance());
406 addProvenance(alignment.getProvenance(), "added"); // TODO: insert some
409 dataset.addAlignment(alignment);
411 Property title = new Property();
412 title.setName("title");
413 title.setType("string");
414 title.setContent(aFtitle);
415 alignment.addProperty(title);
417 alignment.setGapChar(String.valueOf(av.getGapCharacter()));
418 for (int i = 0; i < jal.getHeight(); i++)
420 syncToAlignmentSequence(jal.getSequenceAt(i), alignment, null);
422 alignRDHash.put(av.getSequenceSetId(), av.getUndoRedoHash());
426 // always prepare to clone the alignment
427 boolean alismod = av.isUndoRedoHashModified((long[]) alignRDHash
428 .get(av.getSequenceSetId()));
429 // todo: verify and update mutable alignment props.
430 // TODO: Use isLocked methods
431 if (alignment.getModifiable() == null
432 || alignment.getModifiable().length() == 0)
433 // && !alignment.isDependedOn())
435 boolean modified = false;
436 // check existing sequences in local and in document.
437 Vector docseqs = new Vector(alignment
438 .getAlignmentSequenceAsReference());
439 for (int i = 0; i < jal.getHeight(); i++)
441 modified |= syncToAlignmentSequence(jal.getSequenceAt(i),
444 if (docseqs.size() > 0)
446 // removeValignmentSequences(alignment, docseqs);
447 docseqs.removeAllElements();
449 .println("Sequence deletion from alignment is not implemented.");
457 addProvenance(alignment.getProvenance(), "Edited"); // TODO:
466 addProvenance(alignment.getProvenance(), "Attributes Edited"); // TODO:
476 System.out.println("update alignment in document.");
480 System.out.println("alignment in document left unchanged.");
485 // unbind alignment from view.
486 // create new binding and new alignment.
487 // mark trail on new alignment as being derived from old ?
489 .println("update edited alignment to new alignment in document.");
492 // ////////////////////////////////////////////
493 // SAVE Alignment Sequence Features
494 for (int i = 0, iSize = alignment.getAlignmentSequenceCount(); i < iSize; i++)
496 AlignmentSequence valseq;
497 SequenceI alseq = (SequenceI) getvObj2jv(valseq = alignment
498 .getAlignmentSequence(i));
499 if (alseq != null && alseq.getSequenceFeatures() != null)
502 * We do not put local Alignment Sequence Features into the vamsas
506 * jalview.datamodel.SequenceFeature[] features = alseq
507 * .getSequenceFeatures(); for (int f = 0; f < features.length; f++) {
508 * if (features[f] != null) { AlignmentSequenceAnnotation valseqf = (
509 * AlignmentSequenceAnnotation) getjv2vObj(features[i]); if (valseqf ==
512 * valseqf = (AlignmentSequenceAnnotation) getDSAnnotationFromJalview(
513 * new AlignmentSequenceAnnotation(), features[i]);
514 * valseqf.setGraph(false);
515 * valseqf.addProperty(newProperty("jalview:feature","boolean","true"));
516 * if (valseqf.getProvenance() == null) { valseqf.setProvenance(new
517 * Provenance()); } addProvenance(valseqf.getProvenance(), "created"); //
518 * JBPNote - // need to // update bindjvvobj(features[i], valseqf);
519 * valseq.addAlignmentSequenceAnnotation(valseqf); } } }
524 // ////////////////////////////////////////////
526 if (jal.getAlignmentAnnotation() != null)
528 jalview.datamodel.AlignmentAnnotation[] aa = jal
529 .getAlignmentAnnotation();
530 java.util.HashMap AlSeqMaps = new HashMap(); // stores int maps from
531 // alignment columns to
532 // sequence positions.
533 for (int i = 0; i < aa.length; i++)
535 if (aa[i] == null || isJalviewOnly(aa[i]))
539 if (aa[i].sequenceRef != null)
541 // Deal with sequence associated annotation
542 Vobject sref = getjv2vObj(aa[i].sequenceRef);
543 if (sref instanceof uk.ac.vamsas.objects.core.AlignmentSequence)
545 saveAlignmentSequenceAnnotation(AlSeqMaps,
546 (AlignmentSequence) sref, aa[i]);
550 // first find the alignment sequence to associate this with.
551 SequenceI jvalsq = null;
552 Enumeration jval = av.getAlignment().getSequences()
554 while (jval.hasMoreElements())
556 jvalsq = (SequenceI) jval.nextElement();
557 // saveDatasetSequenceAnnotation(AlSeqMaps,(uk.ac.vamsas.objects.core.Sequence)
559 if (jvalsq.getDatasetSequence() == aa[i].sequenceRef)
561 Vobject alsref = getjv2vObj(jvalsq);
562 saveAlignmentSequenceAnnotation(AlSeqMaps,
563 (AlignmentSequence) alsref, aa[i]);
572 // add Alignment Annotation
573 uk.ac.vamsas.objects.core.AlignmentAnnotation an = (uk.ac.vamsas.objects.core.AlignmentAnnotation) getjv2vObj(aa[i]);
576 an = new uk.ac.vamsas.objects.core.AlignmentAnnotation();
577 an.setType(JALVIEW_ANNOTATION_ROW);
578 an.setDescription(aa[i].description);
579 alignment.addAlignmentAnnotation(an);
580 Seg vSeg = new Seg(); // TODO: refactor to have a default
581 // rangeAnnotationType initer/updater that
582 // takes a set of int ranges.
584 vSeg.setInclusive(true);
585 vSeg.setEnd(jal.getWidth());
589 an.setGraph(true); // aa[i].graph);
591 an.setLabel(aa[i].label);
592 an.setProvenance(dummyProvenance());
593 if (aa[i].graph != AlignmentAnnotation.NO_GRAPH)
595 an.setGroup(Integer.toString(aa[i].graphGroup)); // // JBPNote
610 AnnotationElement ae;
612 for (int a = 0; a < aa[i].annotations.length; a++)
614 if ((aa[i] == null) || (aa[i].annotations[a] == null))
619 ae = new AnnotationElement();
620 ae.setDescription(aa[i].annotations[a].description);
621 ae.addGlyph(new Glyph());
622 ae.getGlyph(0).setContent(
623 aa[i].annotations[a].displayCharacter); // assume
632 ae.addValue(aa[i].annotations[a].value);
634 ae.setPosition(a + 1);
635 if (aa[i].annotations[a].secondaryStructure != ' ')
637 Glyph ss = new Glyph();
639 .setDict(uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE);
642 .valueOf(aa[i].annotations[a].secondaryStructure));
645 an.addAnnotationElement(ae);
649 // an.addProperty(newProperty("jalview:editable", null,
651 // an.setModifiable(""); // TODO: This is not the way the
652 // modifiable flag is supposed to be used.
654 setAnnotationType(an, aa[i]);
656 if (aa[i].graph != jalview.datamodel.AlignmentAnnotation.NO_GRAPH)
659 an.setGroup(Integer.toString(aa[i].graphGroup));
660 if (aa[i].threshold != null && aa[i].threshold.displayed)
662 an.addProperty(newProperty(THRESHOLD, "float", ""
663 + aa[i].threshold.value));
664 if (aa[i].threshold.label != null)
666 an.addProperty(newProperty(THRESHOLD + "Name",
667 "string", "" + aa[i].threshold.label));
676 if (an.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT
679 // verify annotation - update (perhaps)
681 .info("update alignment sequence annotation. not yet implemented.");
685 // verify annotation - update (perhaps)
687 .info("updated alignment sequence annotation added.");
693 // /////////////////////////////////////////////////////
695 // //////////////////////////////////////////////
697 // /////////////////////////////////
698 // FIND ANY ASSOCIATED TREES
699 if (Desktop.desktop != null)
701 javax.swing.JInternalFrame[] frames = Desktop.instance
704 for (int t = 0; t < frames.length; t++)
706 if (frames[t] instanceof TreePanel)
708 TreePanel tp = (TreePanel) frames[t];
710 if (tp.getAlignment() == jal)
712 DatastoreItem vtree = new jalview.io.vamsas.Tree(this, tp,
718 // Store Jalview specific stuff in the Jalview appData
719 // not implemented in the SimpleDoc interface.
724 ex.printStackTrace();
730 * remove docseqs from the given alignment marking provenance appropriately
731 * and removing any references to the sequences.
736 private void removeValignmentSequences(Alignment alignment, Vector docseqs)
738 // delete these from document. This really needs to be a generic document
739 // API function derived by CASTOR.
740 Enumeration en = docseqs.elements();
741 while (en.hasMoreElements())
743 alignment.removeAlignmentSequence((AlignmentSequence) en
746 Entry pe = addProvenance(alignment.getProvenance(), "Removed "
747 + docseqs.size() + " sequences");
748 en = alignment.enumerateAlignmentAnnotation();
749 Vector toremove = new Vector();
750 while (en.hasMoreElements())
752 uk.ac.vamsas.objects.core.AlignmentAnnotation alan = (uk.ac.vamsas.objects.core.AlignmentAnnotation) en
754 if (alan.getSeqrefsCount() > 0)
757 Vector storem = new Vector();
758 Enumeration sr = alan.enumerateSeqrefs();
759 while (sr.hasMoreElements())
761 Object alsr = sr.nextElement();
762 if (docseqs.contains(alsr))
764 storem.addElement(alsr);
767 // remove references to the deleted sequences
768 sr = storem.elements();
769 while (sr.hasMoreElements())
771 alan.removeSeqrefs(sr.nextElement());
774 if (alan.getSeqrefsCount() == 0)
776 // should then delete alan from dataset
777 toremove.addElement(alan);
781 // remove any annotation that used to be associated to a specific bunch of
783 en = toremove.elements();
784 while (en.hasMoreElements())
787 .removeAlignmentAnnotation((uk.ac.vamsas.objects.core.AlignmentAnnotation) en
790 // TODO: search through alignment annotations to remove any references to
791 // this alignment sequence
795 * sync a jalview alignment seuqence into a vamsas alignment assumes all lock
796 * transformation/bindings have been sorted out before hand. creates/syncs the
797 * vamsas alignment sequence for jvalsq and adds it to the alignment if
798 * necessary. unbounddocseq is a duplicate of the vamsas alignment sequences
799 * and these are removed after being processed w.r.t a bound jvalsq
802 private boolean syncToAlignmentSequence(SequenceI jvalsq,
803 Alignment alignment, Vector unbounddocseq)
805 boolean modal = false;
806 // todo: islocked method here
807 boolean up2doc = false;
808 AlignmentSequence alseq = (AlignmentSequence) getjv2vObj(jvalsq);
811 alseq = new AlignmentSequence();
816 if (unbounddocseq != null)
818 unbounddocseq.removeElement(alseq);
821 // boolean locked = (alignment.getModifiable()==null ||
822 // alignment.getModifiable().length()>0);
823 // TODO: VAMSAS: translate lowercase symbols to annotation ?
824 if (up2doc || !alseq.getSequence().equals(jvalsq.getSequenceAsString()))
826 alseq.setSequence(jvalsq.getSequenceAsString());
827 alseq.setStart(jvalsq.getStart());
828 alseq.setEnd(jvalsq.getEnd());
831 if (up2doc || !alseq.getName().equals(jvalsq.getName()))
834 alseq.setName(jvalsq.getName());
836 if (jvalsq.getDescription() != null
837 && (alseq.getDescription() == null || !jvalsq.getDescription()
838 .equals(alseq.getDescription())))
841 alseq.setDescription(jvalsq.getDescription());
843 if (getjv2vObj(jvalsq.getDatasetSequence()) == null)
846 .warn("Serious Implementation error - Unbound dataset sequence in alignment: "
847 + jvalsq.getDatasetSequence());
849 alseq.setRefid(getjv2vObj(jvalsq.getDatasetSequence()));
853 alignment.addAlignmentSequence(alseq);
854 bindjvvobj(jvalsq, alseq);
856 return up2doc || modal;
860 * locally sync a jalview alignment seuqence from a vamsas alignment assumes
861 * all lock transformation/bindings have been sorted out before hand.
862 * creates/syncs the jvalsq from the alignment sequence
864 private boolean syncFromAlignmentSequence(AlignmentSequence valseq,
865 char valGapchar, char gapChar, Vector dsseqs)
868 boolean modal = false;
869 // todo: islocked method here
870 boolean upFromdoc = false;
871 jalview.datamodel.SequenceI alseq = (SequenceI) getvObj2jv(valseq);
879 // boolean locked = (alignment.getModifiable()==null ||
880 // alignment.getModifiable().length()>0);
881 // TODO: VAMSAS: translate lowercase symbols to annotation ?
883 || !valseq.getSequence().equals(alseq.getSequenceAsString()))
885 // this might go *horribly* wrong
886 alseq.setSequence(new String(valseq.getSequence()).replace(
887 valGapchar, gapChar));
888 alseq.setStart((int) valseq.getStart());
889 alseq.setEnd((int) valseq.getEnd());
892 if (!valseq.getName().equals(alseq.getName()))
895 alseq.setName(valseq.getName());
897 if (alseq.getDescription() == null
898 || (valseq.getDescription() == null || alseq.getDescription()
899 .equals(valseq.getDescription())))
901 alseq.setDescription(valseq.getDescription());
904 if (modal && Cache.log.isDebugEnabled())
906 Cache.log.debug("Updating apparently edited sequence "
912 alseq = new jalview.datamodel.Sequence(valseq.getName(), valseq
913 .getSequence().replace(valGapchar, gapChar), (int) valseq
914 .getStart(), (int) valseq.getEnd());
916 Vobject datsetseq = (Vobject) valseq.getRefid();
917 if (datsetseq != null)
919 alseq.setDatasetSequence((SequenceI) getvObj2jv(datsetseq)); // exceptions
931 .error("Invalid dataset sequence id (null) for alignment sequence "
932 + valseq.getVorbaId());
934 bindjvvobj(alseq, valseq);
935 alseq.setVamsasId(valseq.getVorbaId().getId());
938 Vobject datsetseq = (Vobject) valseq.getRefid();
939 if (datsetseq != null)
941 if (datsetseq != alseq.getDatasetSequence())
945 alseq.setDatasetSequence((SequenceI) getvObj2jv(datsetseq)); // exceptions
947 return upFromdoc || modal;
950 private void initRangeAnnotationType(RangeAnnotation an,
951 AlignmentAnnotation alan, int[] gapMap)
953 Seg vSeg = new Seg();
955 vSeg.setInclusive(true);
956 vSeg.setEnd(gapMap.length);
959 // LATER: much of this is verbatim from the alignmentAnnotation
960 // method below. suggests refactoring to make rangeAnnotation the
962 an.setDescription(alan.description);
963 an.setLabel(alan.label);
964 an.setGroup(Integer.toString(alan.graphGroup));
973 AnnotationElement ae;
974 for (int a = 0; a < alan.annotations.length; a++)
976 if (alan.annotations[a] == null)
981 ae = new AnnotationElement();
982 ae.setDescription(alan.annotations[a].description);
983 ae.addGlyph(new Glyph());
984 ae.getGlyph(0).setContent(alan.annotations[a].displayCharacter); // assume
991 if (alan.graph != jalview.datamodel.AlignmentAnnotation.NO_GRAPH)
993 ae.addValue(alan.annotations[a].value);
995 ae.setPosition(gapMap[a] + 1); // position w.r.t. AlignmentSequence
997 if (alan.annotations[a].secondaryStructure != ' ')
999 // we only write an annotation where it really exists.
1000 Glyph ss = new Glyph();
1002 .setDict(uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE);
1003 ss.setContent(String
1004 .valueOf(alan.annotations[a].secondaryStructure));
1007 an.addAnnotationElement(ae);
1012 private void saveDatasetSequenceAnnotation(HashMap AlSeqMaps,
1013 uk.ac.vamsas.objects.core.Sequence sref, AlignmentAnnotation alan)
1017 // objects.core.AlignmentSequence alsref = (uk.ac.vamsas.
1018 // objects.core.AlignmentSequence) sref;
1019 uk.ac.vamsas.objects.core.DataSetAnnotations an = (uk.ac.vamsas.objects.core.DataSetAnnotations) getjv2vObj(alan);
1020 int[] gapMap = getGapMap(AlSeqMaps, alan);
1023 an = new uk.ac.vamsas.objects.core.DataSetAnnotations();
1024 initRangeAnnotationType(an, alan, gapMap);
1026 an.setProvenance(dummyProvenance()); // get provenance as user
1027 // created, or jnet, or
1029 setAnnotationType(an, alan);
1030 an.setGroup(Integer.toString(alan.graphGroup)); // // JBPNote -
1038 if (alan.getThreshold() != null && alan.getThreshold().displayed)
1040 an.addProperty(newProperty(THRESHOLD, "float", ""
1041 + alan.getThreshold().value));
1042 if (alan.getThreshold().label != null)
1043 an.addProperty(newProperty(THRESHOLD + "Name", "string", ""
1044 + alan.getThreshold().label));
1046 ((DataSet) sref.getV_parent()).addDataSetAnnotations(an);
1047 bindjvvobj(alan, an);
1051 // update reference sequence Annotation
1052 if (an.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT LOCK
1055 // verify existing alignment sequence annotation is up to date
1056 System.out.println("update dataset sequence annotation.");
1060 // verify existing alignment sequence annotation is up to date
1062 .println("make new alignment dataset sequence annotation if modification has happened.");
1068 private int[] getGapMap(HashMap AlSeqMaps, AlignmentAnnotation alan)
1071 if (AlSeqMaps.containsKey(alan.sequenceRef))
1073 gapMap = (int[]) AlSeqMaps.get(alan.sequenceRef);
1077 gapMap = new int[alan.sequenceRef.getLength()];
1078 // map from alignment position to sequence position.
1079 int[] sgapMap = alan.sequenceRef.gapMap();
1080 for (int a = 0; a < sgapMap.length; a++)
1082 gapMap[sgapMap[a]] = a;
1088 private void saveAlignmentSequenceAnnotation(HashMap AlSeqMaps,
1089 AlignmentSequence alsref, AlignmentAnnotation alan)
1093 // objects.core.AlignmentSequence alsref = (uk.ac.vamsas.
1094 // objects.core.AlignmentSequence) sref;
1095 uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation an = (uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation) getjv2vObj(alan);
1096 int[] gapMap = getGapMap(AlSeqMaps, alan);
1099 an = new uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation();
1100 initRangeAnnotationType(an, alan, gapMap);
1102 * I mean here that we don't actually have a semantic 'type' for the
1103 * annotation (this might be - score, intrinsic property, measurement,
1104 * something extracted from another program, etc)
1106 an.setType(JALVIEW_ANNOTATION_ROW); // TODO: better fix
1107 // this rough guess ;)
1108 alsref.addAlignmentSequenceAnnotation(an);
1109 bindjvvobj(alan, an);
1110 // These properties are directly supported by the
1111 // AlignmentSequenceAnnotation type.
1112 setAnnotationType(an, alan);
1113 an.setProvenance(dummyProvenance()); // get provenance as user
1114 // created, or jnet, or
1119 // update reference sequence Annotation
1120 if (an.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT LOCK
1123 // verify existing alignment sequence annotation is up to date
1124 System.out.println("update alignment sequence annotation.");
1128 // verify existing alignment sequence annotation is up to date
1130 .println("make new alignment sequence annotation if modification has happened.");
1136 * set vamsas annotation object type from jalview annotation
1141 private void setAnnotationType(RangeAnnotation an,
1142 AlignmentAnnotation alan)
1144 if (an instanceof AlignmentSequenceAnnotation)
1146 if (alan.graph != AlignmentAnnotation.NO_GRAPH)
1148 ((AlignmentSequenceAnnotation) an).setGraph(true);
1152 ((AlignmentSequenceAnnotation) an).setGraph(false);
1155 if (an instanceof uk.ac.vamsas.objects.core.AlignmentAnnotation)
1157 if (alan.graph != AlignmentAnnotation.NO_GRAPH)
1159 ((uk.ac.vamsas.objects.core.AlignmentAnnotation) an).setGraph(true);
1163 ((uk.ac.vamsas.objects.core.AlignmentAnnotation) an)
1169 case AlignmentAnnotation.BAR_GRAPH:
1170 an.addProperty(newProperty(DISCRETE_ANNOTATION, "boolean", "true"));
1172 case AlignmentAnnotation.LINE_GRAPH:
1173 an.addProperty(newProperty(CONTINUOUS_ANNOTATION, "boolean", "true"));
1176 // don't add any kind of discrete or continous property info.
1180 private Property newProperty(String name, String type, String content)
1182 Property vProperty = new Property();
1183 vProperty.setName(name);
1186 vProperty.setType(type);
1190 vProperty.setType("String");
1192 vProperty.setContent(content);
1197 * correctly create a RangeAnnotation from a jalview sequence feature
1200 * (typically DataSetAnnotations or
1201 * AlignmentSequenceAnnotation)
1203 * (the feature to be mapped from)
1206 private RangeAnnotation getDSAnnotationFromJalview(RangeAnnotation dsa,
1207 SequenceFeature feature)
1209 dsa.setType(feature.getType());
1210 Seg vSeg = new Seg();
1211 vSeg.setStart(feature.getBegin());
1212 vSeg.setEnd(feature.getEnd());
1213 vSeg.setInclusive(true);
1215 dsa.setDescription(feature.getDescription());
1216 dsa.setStatus(feature.getStatus());
1217 if (feature.links != null && feature.links.size() > 0)
1219 for (int i = 0, iSize = feature.links.size(); i < iSize; i++)
1221 String link = (String) feature.links.elementAt(i);
1222 int sep = link.indexOf('|');
1225 Link vLink = new Link();
1228 vLink.setContent(link.substring(0, sep - 1));
1232 vLink.setContent("");
1234 vLink.setHref(link.substring(sep + 1)); // TODO: validate href.
1239 dsa.setGroup(feature.getFeatureGroup());
1244 * get start<end range of segment, adjusting for inclusivity flag and
1248 * @param ensureDirection
1249 * when true - always ensure start is less than end.
1250 * @return int[] { start, end, direction} where direction==1 for range running
1251 * from end to start.
1253 private int[] getSegRange(Seg visSeg, boolean ensureDirection)
1255 boolean incl = visSeg.getInclusive();
1256 // adjust for inclusive flag.
1257 int pol = (visSeg.getStart() <= visSeg.getEnd()) ? 1 : -1; // polarity of
1259 int start = visSeg.getStart() + (incl ? 0 : pol);
1260 int end = visSeg.getEnd() + (incl ? 0 : -pol);
1261 if (ensureDirection && pol == -1)
1263 // jalview doesn't deal with inverted ranges, yet.
1269 { start, end, pol < 0 ? 1 : 0 };
1275 * @return true if annotation is not to be stored in document
1277 private boolean isJalviewOnly(AlignmentAnnotation annotation)
1279 return annotation.label.equals("Quality")
1280 || annotation.label.equals("Conservation")
1281 || annotation.label.equals("Consensus");
1285 * This will return the first AlignFrame viewing AlignViewport av. It will
1286 * break if there are more than one AlignFrames viewing a particular av. This
1287 * also shouldn't be in the io package.
1290 * @return alignFrame for av
1292 public AlignFrame getAlignFrameFor(AlignViewport av)
1294 if (Desktop.desktop != null)
1296 javax.swing.JInternalFrame[] frames = Desktop.instance.getAllFrames();
1298 for (int t = 0; t < frames.length; t++)
1300 if (frames[t] instanceof AlignFrame)
1302 if (((AlignFrame) frames[t]).getViewport() == av)
1304 return (AlignFrame) frames[t];
1312 public void updateToJalview()
1314 VAMSAS _roots[] = cdoc.getVamsasRoots();
1316 for (int _root = 0; _root < _roots.length; _root++)
1318 VAMSAS root = _roots[_root];
1319 boolean newds = false;
1320 for (int _ds = 0, _nds = root.getDataSetCount(); _ds < _nds; _ds++)
1322 // ///////////////////////////////////
1324 DataSet dataset = root.getDataSet(_ds);
1325 int i, iSize = dataset.getSequenceCount();
1327 jalview.datamodel.Alignment jdataset = (jalview.datamodel.Alignment) getvObj2jv(dataset);
1329 if (jdataset == null)
1331 Cache.log.debug("Initialising new jalview dataset fields");
1333 dsseqs = new Vector();
1337 Cache.log.debug("Update jalview dataset from vamsas.");
1338 jremain = jdataset.getHeight();
1339 dsseqs = jdataset.getSequences();
1342 // TODO: test sequence merging - we preserve existing non vamsas
1343 // sequences but add in any new vamsas ones, and don't yet update any
1344 // sequence attributes
1345 for (i = 0; i < iSize; i++)
1347 Sequence vdseq = dataset.getSequence(i);
1348 jalview.datamodel.SequenceI dsseq = (SequenceI) getvObj2jv(vdseq);
1351 if (!dsseq.getSequenceAsString().equals(vdseq.getSequence()))
1354 "Broken! - mismatch of dataset sequence: and jalview internal dataset sequence.");
1360 dsseq = new jalview.datamodel.Sequence(dataset.getSequence(i)
1361 .getName(), dataset.getSequence(i).getSequence(),
1362 (int) dataset.getSequence(i).getStart(), (int) dataset
1363 .getSequence(i).getEnd());
1364 dsseq.setDescription(dataset.getSequence(i).getDescription());
1365 bindjvvobj(dsseq, dataset.getSequence(i));
1366 dsseq.setVamsasId(dataset.getSequence(i).getVorbaId().getId());
1369 if (vdseq.getDbRefCount() > 0)
1371 DbRef[] dbref = vdseq.getDbRef();
1372 for (int db = 0; db < dbref.length; db++)
1374 new jalview.io.vamsas.Dbref(this, dbref[db], vdseq, dsseq);
1377 dsseq.updatePDBIds();
1383 SequenceI[] seqs = new SequenceI[dsseqs.size()];
1384 for (i = 0, iSize = dsseqs.size(); i < iSize; i++)
1386 seqs[i] = (SequenceI) dsseqs.elementAt(i);
1387 dsseqs.setElementAt(null, i);
1389 jdataset = new jalview.datamodel.Alignment(seqs);
1390 Cache.log.debug("New vamsas dataset imported into jalview.");
1391 bindjvvobj(jdataset, dataset);
1394 // add any new dataset sequence feature annotations
1395 if (dataset.getDataSetAnnotations() != null)
1397 for (int dsa = 0; dsa < dataset.getDataSetAnnotationsCount(); dsa++)
1399 DataSetAnnotations dseta = dataset.getDataSetAnnotations(dsa);
1400 // TODO: deal with group annotation on datset sequences.
1401 if (dseta.getSeqRefCount() == 1)
1403 SequenceI dsSeq = (SequenceI) getvObj2jv((Vobject) dseta
1404 .getSeqRef(0)); // TODO: deal with group dataset
1408 jalview.bin.Cache.log
1409 .warn("Couldn't resolve jalview sequenceI for dataset object reference "
1410 + ((Vobject) dataset.getDataSetAnnotations(
1411 dsa).getSeqRef(0)).getVorbaId()
1416 if (dseta.getAnnotationElementCount() == 0)
1418 jalview.datamodel.SequenceFeature sf = (jalview.datamodel.SequenceFeature) getvObj2jv(dseta);
1422 .addSequenceFeature(sf = getJalviewSeqFeature(dseta));
1423 bindjvvobj(sf, dseta);
1428 // TODO: deal with alignmentAnnotation style annotation
1429 // appearing on dataset sequences.
1430 // JBPNote: we could just add them to all alignments but
1431 // that may complicate cross references in the jalview
1434 .warn("Ignoring dataset annotation with annotationElements. Not yet supported in jalview.");
1440 if (dataset.getAlignmentCount() > 0)
1442 // LOAD ALIGNMENTS from DATASET
1444 for (int al = 0, nal = dataset.getAlignmentCount(); al < nal; al++)
1446 uk.ac.vamsas.objects.core.Alignment alignment = dataset
1448 AlignViewport av = findViewport(alignment);
1450 jalview.datamodel.AlignmentI jal = null;
1453 jal = av.getAlignment();
1455 iSize = alignment.getAlignmentSequenceCount();
1456 boolean newal = (jal == null) ? true : false;
1457 boolean refreshal = false;
1458 Vector newasAnnots = new Vector();
1459 char gapChar = ' '; // default for new alignments read in from the
1463 dsseqs = jal.getSequences(); // for merge/update
1464 gapChar = jal.getGapCharacter();
1468 dsseqs = new Vector();
1470 char valGapchar = alignment.getGapChar().charAt(0);
1471 for (i = 0; i < iSize; i++)
1473 AlignmentSequence valseq = alignment.getAlignmentSequence(i);
1474 jalview.datamodel.Sequence alseq = (jalview.datamodel.Sequence) getvObj2jv(valseq);
1475 if (syncFromAlignmentSequence(valseq, valGapchar, gapChar,
1480 // updated to sequence from the document
1484 if (valseq.getAlignmentSequenceAnnotationCount() > 0)
1486 AlignmentSequenceAnnotation[] vasannot = valseq
1487 .getAlignmentSequenceAnnotation();
1488 for (int a = 0; a < vasannot.length; a++)
1490 jalview.datamodel.AlignmentAnnotation asa = (jalview.datamodel.AlignmentAnnotation) getvObj2jv(vasannot[a]); // TODO:
1498 int se[] = getBounds(vasannot[a]);
1499 asa = getjAlignmentAnnotation(jal, vasannot[a]);
1500 asa.setSequenceRef(alseq);
1501 asa.createSequenceMapping(alseq, se[0], false); // TODO:
1512 alseq.addAlignmentAnnotation(asa);
1513 bindjvvobj(asa, vasannot[a]);
1514 newasAnnots.add(asa);
1518 // update existing annotation - can do this in place
1519 if (vasannot[a].getModifiable() == null) // TODO: USE
1525 .info("UNIMPLEMENTED: not recovering user modifiable sequence alignment annotation");
1526 // TODO: should at least replace with new one - otherwise
1527 // things will break
1528 // basically do this:
1529 // int se[] = getBounds(vasannot[a]);
1530 // asa.update(getjAlignmentAnnotation(jal, vasannot[a]));
1531 // // update from another annotation object in place.
1532 // asa.createSequenceMapping(alseq, se[0], false);
1541 SequenceI[] seqs = new SequenceI[dsseqs.size()];
1542 for (i = 0, iSize = dsseqs.size(); i < iSize; i++)
1544 seqs[i] = (SequenceI) dsseqs.elementAt(i);
1545 dsseqs.setElementAt(null, i);
1547 jal = new jalview.datamodel.Alignment(seqs);
1548 Cache.log.debug("New vamsas alignment imported into jalview "
1549 + alignment.getVorbaId().getId());
1550 jal.setDataset(jdataset);
1552 if (newasAnnots != null && newasAnnots.size() > 0)
1554 // Add the new sequence annotations in to the alignment.
1555 for (int an = 0, anSize = newasAnnots.size(); an < anSize; an++)
1557 jal.addAnnotation((AlignmentAnnotation) newasAnnots
1559 // TODO: check if anything has to be done - like calling
1560 // adjustForAlignment or something.
1561 newasAnnots.setElementAt(null, an);
1565 // //////////////////////////////////////////
1566 // //LOAD ANNOTATIONS FOR THE ALIGNMENT
1567 // ////////////////////////////////////
1568 if (alignment.getAlignmentAnnotationCount() > 0)
1570 uk.ac.vamsas.objects.core.AlignmentAnnotation[] an = alignment
1571 .getAlignmentAnnotation();
1573 for (int j = 0; j < an.length; j++)
1575 jalview.datamodel.AlignmentAnnotation jan = (jalview.datamodel.AlignmentAnnotation) getvObj2jv(an[j]);
1578 // update or stay the same.
1579 // TODO: should at least replace with a new one - otherwise
1580 // things will break
1581 // basically do this:
1582 // jan.update(getjAlignmentAnnotation(jal, an[a])); // update
1583 // from another annotation object in place.
1586 .debug("update from vamsas alignment annotation to existing jalview alignment annotation.");
1587 if (an[j].getModifiable() == null) // TODO: USE VAMSAS
1588 // LIBRARY OBJECT LOCK
1591 // TODO: user defined annotation is totally mutable... - so
1592 // load it up or throw away if locally edited.
1594 .info("NOT IMPLEMENTED - Recovering user-modifiable annotation - yet...");
1596 // TODO: compare annotation element rows
1597 // TODO: compare props.
1601 jan = getjAlignmentAnnotation(jal, an[j]);
1602 jal.addAnnotation(jan);
1603 bindjvvobj(jan, an[j]);
1607 AlignFrame alignFrame;
1610 Cache.log.debug("New alignframe for alignment "
1611 + alignment.getVorbaId());
1612 // ///////////////////////////////
1613 // construct alignment view
1614 alignFrame = new AlignFrame(jal, AlignFrame.DEFAULT_WIDTH,
1615 AlignFrame.DEFAULT_HEIGHT);
1616 av = alignFrame.getViewport();
1617 String title = alignment.getProvenance().getEntry(
1618 alignment.getProvenance().getEntryCount() - 1)
1620 if (alignment.getPropertyCount() > 0)
1622 for (int p = 0, pe = alignment.getPropertyCount(); p < pe; p++)
1624 if (alignment.getProperty(p).getName().equals("title"))
1626 title = alignment.getProperty(p).getContent();
1630 // TODO: automatically create meaningful title for a vamsas
1631 // alignment using its provenance.
1632 if (Cache.log.isDebugEnabled())
1634 title = title + "(" + alignment.getVorbaId() + ")";
1637 jalview.gui.Desktop.addInternalFrame(alignFrame, title,
1638 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
1639 bindjvvobj(av.getSequenceSetId(), alignment);
1643 // find the alignFrame for jal.
1644 // TODO: fix this so we retrieve the alignFrame handing av
1646 alignFrame = getAlignFrameFor(av);
1649 av.alignmentChanged(alignFrame.alignPanel);
1653 // /////////////////////////////////////
1654 if (alignment.getTreeCount() > 0)
1657 for (int t = 0; t < alignment.getTreeCount(); t++)
1659 jalview.io.vamsas.Tree vstree = new jalview.io.vamsas.Tree(
1660 this, alignFrame, alignment.getTree(t));
1661 TreePanel tp = null;
1662 if (vstree.isValidTree())
1664 tp = alignFrame.ShowNewickTree(vstree.getNewickTree(),
1665 vstree.getTitle(), vstree.getInputData(), 600,
1666 500, t * 20 + 50, t * 20 + 50);
1671 bindjvvobj(tp, alignment.getTree(t));
1674 vstree.UpdateSequenceTreeMap(tp);
1675 } catch (RuntimeException e)
1677 Cache.log.warn("update of labels failed.", e);
1682 Cache.log.warn("Cannot create tree for tree " + t
1684 + alignment.getTree(t).getVorbaId());
1692 // we do sequenceMappings last because they span all datasets in a vamsas
1694 for (int _ds = 0, _nds = root.getDataSetCount(); _ds < _nds; _ds++)
1696 DataSet dataset = root.getDataSet(_ds);
1697 if (dataset.getSequenceMappingCount() > 0)
1699 for (int sm = 0, smCount = dataset.getSequenceMappingCount(); sm < smCount; sm++)
1701 Rangetype seqmap = new jalview.io.vamsas.Sequencemapping(this,
1702 dataset.getSequenceMapping(sm));
1709 public AlignViewport findViewport(Alignment alignment)
1711 AlignViewport av = null;
1712 AlignViewport[] avs = findViewportForSequenceSetId((String) getvObj2jv(alignment));
1720 private AlignViewport[] findViewportForSequenceSetId(String sequenceSetId)
1722 Vector viewp = new Vector();
1723 if (Desktop.desktop != null)
1725 javax.swing.JInternalFrame[] frames = Desktop.instance.getAllFrames();
1727 for (int t = 0; t < frames.length; t++)
1729 if (frames[t] instanceof AlignFrame)
1731 if (((AlignFrame) frames[t]).getViewport().getSequenceSetId()
1732 .equals(sequenceSetId))
1734 viewp.addElement(((AlignFrame) frames[t]).getViewport());
1738 if (viewp.size() > 0)
1740 AlignViewport[] vp = new AlignViewport[viewp.size()];
1748 // bitfields - should be a template in j1.5
1749 private static int HASSECSTR = 0;
1751 private static int HASVALS = 1;
1753 private static int HASHPHOB = 2;
1755 private static int HASDC = 3;
1757 private static int HASDESCSTR = 4;
1759 private static int HASTWOSTATE = 5; // not used yet.
1762 * parses the AnnotationElements - if they exist - into
1763 * jalview.datamodel.Annotation[] rows Two annotation rows are made if there
1764 * are distinct annotation for both at 'pos' and 'after pos' at any particular
1768 * @return { boolean[static int constants ], int[ae.length] - map to annotated
1769 * object frame, jalview.datamodel.Annotation[],
1770 * jalview.datamodel.Annotation[] (after)}
1772 private Object[] parseRangeAnnotation(
1773 uk.ac.vamsas.objects.core.RangeAnnotation annotation)
1775 // set these attributes by looking in the annotation to decide what kind of
1776 // alignment annotation rows will be made
1777 // TODO: potentially we might make several annotation rows from one vamsas
1778 // alignment annotation. the jv2Vobj binding mechanism
1779 // may not quite cope with this (without binding an array of annotations to
1780 // a vamsas alignment annotation)
1781 // summary flags saying what we found over the set of annotation rows.
1782 boolean[] AeContent = new boolean[]
1783 { false, false, false, false, false };
1784 int[] rangeMap = getMapping(annotation);
1785 jalview.datamodel.Annotation[][] anot = new jalview.datamodel.Annotation[][]
1786 { new jalview.datamodel.Annotation[rangeMap.length],
1787 new jalview.datamodel.Annotation[rangeMap.length] };
1788 boolean mergeable = true; // false if 'after positions cant be placed on
1789 // same annotation row as positions.
1791 if (annotation.getAnnotationElementCount() > 0)
1793 AnnotationElement ae[] = annotation.getAnnotationElement();
1794 for (int aa = 0; aa < ae.length; aa++)
1796 int pos = (int) ae[aa].getPosition() - 1; // pos counts from 1 to
1797 // (|seg.start-seg.end|+1)
1798 if (pos >= 0 && pos < rangeMap.length)
1800 int row = ae[aa].getAfter() ? 1 : 0;
1801 if (anot[row][pos] != null)
1803 // only time this should happen is if the After flag is set.
1804 Cache.log.debug("Ignoring duplicate annotation site at " + pos);
1807 if (anot[1 - row][pos] != null)
1812 if (ae[aa].getDescription() != null)
1814 desc = ae[aa].getDescription();
1815 if (desc.length() > 0)
1817 // have imported valid description string
1818 AeContent[HASDESCSTR] = true;
1821 String dc = null; // ae[aa].getDisplayCharacter()==null ? "dc" :
1822 // ae[aa].getDisplayCharacter();
1823 String ss = null; // ae[aa].getSecondaryStructure()==null ? "ss" :
1824 // ae[aa].getSecondaryStructure();
1825 java.awt.Color colour = null;
1826 if (ae[aa].getGlyphCount() > 0)
1828 Glyph[] glyphs = ae[aa].getGlyph();
1829 for (int g = 0; g < glyphs.length; g++)
1834 uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE))
1836 ss = glyphs[g].getContent();
1837 AeContent[HASSECSTR] = true;
1842 uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_HD_HYDRO))
1844 Cache.log.debug("ignoring hydrophobicity glyph marker.");
1845 AeContent[HASHPHOB] = true;
1846 char c = (dc = glyphs[g].getContent()).charAt(0);
1847 // dc may get overwritten - but we still set the colour.
1848 colour = new java.awt.Color(c == '+' ? 255 : 0,
1849 c == '.' ? 255 : 0, c == '-' ? 255 : 0);
1852 else if (glyphs[g].getDict().equals(
1853 uk.ac.vamsas.objects.utils.GlyphDictionary.DEFAULT))
1855 dc = glyphs[g].getContent();
1856 AeContent[HASDC] = true;
1861 .debug("IMPLEMENTATION TODO: Ignoring unknown glyph type "
1862 + glyphs[g].getDict());
1867 if (ae[aa].getValueCount() > 0)
1869 AeContent[HASVALS] = true;
1870 if (ae[aa].getValueCount() > 1)
1872 Cache.log.warn("ignoring additional "
1873 + (ae[aa].getValueCount() - 1)
1874 + "values in annotation element.");
1876 val = ae[aa].getValue(0);
1880 anot[row][pos] = new jalview.datamodel.Annotation(
1881 (dc != null) ? dc : "", desc, (ss != null) ? ss
1882 .charAt(0) : ' ', val);
1886 anot[row][pos] = new jalview.datamodel.Annotation(
1887 (dc != null) ? dc : "", desc, (ss != null) ? ss
1888 .charAt(0) : ' ', val, colour);
1893 Cache.log.warn("Ignoring out of bound annotation element " + aa
1894 + " in " + annotation.getVorbaId().getId());
1897 // decide on how many annotation rows are needed.
1900 for (int i = 0; i < anot[0].length; i++)
1902 if (anot[1][i] != null)
1904 anot[0][i] = anot[1][i];
1905 anot[0][i].description = anot[0][i].description + " (after)";
1906 AeContent[HASDESCSTR] = true; // we have valid description string
1915 for (int i = 0; i < anot[0].length; i++)
1917 anot[1][i].description = anot[1][i].description + " (after)";
1921 { AeContent, rangeMap, anot[0], anot[1] };
1925 // no annotations to parse. Just return an empty annotationElement[]
1928 { AeContent, rangeMap, anot[0], anot[1] };
1935 * the jalview alignment to which the annotation will be
1936 * attached (ideally - freshly updated from corresponding
1939 * @return unbound jalview alignment annotation object.
1941 private jalview.datamodel.AlignmentAnnotation getjAlignmentAnnotation(
1942 jalview.datamodel.AlignmentI jal,
1943 uk.ac.vamsas.objects.core.RangeAnnotation annotation)
1945 if (annotation == null)
1950 // hasSequenceRef=annotation.getClass().equals(uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation.class);
1951 // boolean hasProvenance=hasSequenceRef ||
1952 // (annotation.getClass().equals(uk.ac.vamsas.objects.core.AlignmentAnnotation.class));
1954 * int se[] = getBounds(annotation); if (se==null) se=new int[]
1955 * {0,jal.getWidth()-1};
1957 Object[] parsedRangeAnnotation = parseRangeAnnotation(annotation);
1958 String a_label = annotation.getLabel();
1959 String a_descr = annotation.getDescription();
1960 GraphLine gl = null;
1962 boolean interp = true; // cleared if annotation is DISCRETE
1963 // set type and other attributes from properties
1964 if (annotation.getPropertyCount() > 0)
1966 // look for special jalview properties
1967 uk.ac.vamsas.objects.core.Property[] props = annotation.getProperty();
1968 for (int p = 0; p < props.length; p++)
1970 if (props[p].getName().equalsIgnoreCase(DISCRETE_ANNOTATION))
1972 type = AlignmentAnnotation.BAR_GRAPH;
1975 else if (props[p].getName().equalsIgnoreCase(CONTINUOUS_ANNOTATION))
1977 type = AlignmentAnnotation.LINE_GRAPH;
1979 else if (props[p].getName().equalsIgnoreCase(THRESHOLD))
1984 val = new Float(props[p].getContent());
1985 } catch (Exception e)
1987 Cache.log.warn("Failed to parse threshold property");
1992 gl = new GraphLine(val.floatValue(), "", java.awt.Color.black);
1996 gl.value = val.floatValue();
1999 else if (props[p].getName().equalsIgnoreCase(THRESHOLD + "Name"))
2002 gl = new GraphLine(0, "", java.awt.Color.black);
2003 gl.label = props[p].getContent();
2007 jalview.datamodel.AlignmentAnnotation jan = null;
2008 if (a_label == null || a_label.length() == 0)
2010 a_label = annotation.getType();
2011 if (a_label.length() == 0)
2013 a_label = "Unamed annotation";
2016 if (a_descr == null || a_descr.length() == 0)
2018 a_descr = "Annotation of type '" + annotation.getType() + "'";
2020 if (parsedRangeAnnotation == null)
2023 .debug("Inserting empty annotation row elements for a whole-alignment annotation.");
2027 if (parsedRangeAnnotation[3] != null)
2029 Cache.log.warn("Ignoring 'After' annotation row in "
2030 + annotation.getVorbaId());
2032 jalview.datamodel.Annotation[] arow = (jalview.datamodel.Annotation[]) parsedRangeAnnotation[2];
2033 boolean[] has = (boolean[]) parsedRangeAnnotation[0];
2034 // VAMSAS: getGraph is only on derived annotation for alignments - in this
2035 // way its 'odd' - there is already an existing TODO about removing this
2036 // flag as being redundant
2039 * ((annotation.getClass().equals(uk.ac.vamsas.objects.core.AlignmentAnnotation.class) &&
2040 * ((uk.ac.vamsas.objects.core.AlignmentAnnotation)annotation).getGraph()) ||
2041 * (hasSequenceRef=true &&
2042 * ((uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation)annotation).getGraph())) {
2048 type = jalview.datamodel.AlignmentAnnotation.BAR_GRAPH; // default
2054 // no hints - so we ensure HPHOB display is like this.
2055 type = jalview.datamodel.AlignmentAnnotation.BAR_GRAPH;
2058 // make bounds and automatic description strings for jalview user's
2059 // benefit (these shouldn't be written back to vamsas document)
2060 boolean first = true;
2061 float min = 0, max = 1;
2063 for (int i = 0; i < arow.length; i++)
2065 if (arow[i] != null)
2067 if (i - lastval > 1 && interp)
2069 // do some interpolation *between* points
2070 if (arow[lastval] != null)
2072 float interval = arow[i].value - arow[lastval].value;
2073 interval /= i - lastval;
2074 float base = arow[lastval].value;
2075 for (int ip = lastval + 1, np = 0; ip < i; np++, ip++)
2077 arow[ip] = new jalview.datamodel.Annotation("", "", ' ',
2078 interval * np + base);
2079 // NB - Interpolated points don't get a tooltip and
2085 // check range - shouldn't we have a min and max property in the
2086 // annotation object ?
2089 min = max = arow[i].value;
2094 if (arow[i].value < min)
2096 min = arow[i].value;
2098 else if (arow[i].value > max)
2100 max = arow[i].value;
2103 // make tooltip and display char value
2104 if (!has[HASDESCSTR])
2106 arow[i].description = arow[i].value + "";
2112 if (arow[i].description != null
2113 && arow[i].description.length() < 3)
2115 // copy over the description as the display char.
2116 arow[i].displayCharacter = new String(arow[i].description);
2121 // mark the position as a point used for the interpolation.
2122 arow[i].displayCharacter = arow[i].value + "";
2127 jan = new jalview.datamodel.AlignmentAnnotation(a_label, a_descr,
2128 arow, min, max, type);
2132 if (annotation.getAnnotationElementCount() == 0)
2134 // empty annotation array
2135 // TODO: alignment 'features' compare rangeType spec to alignment
2136 // width - if it is not complete, then mark regions on the annotation
2139 jan = new jalview.datamodel.AlignmentAnnotation(a_label, a_descr,
2141 jan.setThreshold(null);
2143 if (annotation.getLinkCount() > 0)
2145 Cache.log.warn("Ignoring " + annotation.getLinkCount()
2146 + "links added to AlignmentAnnotation.");
2148 if (annotation.getModifiable() == null
2149 || annotation.getModifiable().length() == 0) // TODO: USE VAMSAS
2153 jan.editable = true;
2157 if (annotation.getGroup() != null
2158 && annotation.getGroup().length() > 0)
2160 jan.graphGroup = Integer.parseInt(annotation.getGroup()); // TODO:
2168 } catch (Exception e)
2171 .info("UNIMPLEMENTED : Couldn't parse non-integer group value for setting graphGroup correctly.");
2180 private SequenceFeature getJalviewSeqFeature(RangeAnnotation dseta)
2182 int[] se = getBounds(dseta);
2183 SequenceFeature sf = new jalview.datamodel.SequenceFeature(dseta
2184 .getType(), dseta.getDescription(), dseta.getStatus(), se[0],
2185 se[1], dseta.getGroup());
2186 if (dseta.getLinkCount() > 0)
2188 Link[] links = dseta.getLink();
2189 for (int i = 0; i < links.length; i++)
2191 sf.addLink(links[i].getContent() + "|" + links[i].getHref());
2198 * get real bounds of a RangeType's specification. start and end are an
2199 * inclusive range within which all segments and positions lie. TODO: refactor
2203 * @return int[] { start, end}
2205 private int[] getBounds(RangeType dseta)
2210 if (dseta.getSegCount() > 0 && dseta.getPosCount() > 0)
2213 "Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
2215 if (dseta.getSegCount() > 0)
2217 se = getSegRange(dseta.getSeg(0), true);
2218 for (int s = 1, sSize = dseta.getSegCount(); s < sSize; s++)
2220 int nse[] = getSegRange(dseta.getSeg(s), true);
2231 if (dseta.getPosCount() > 0)
2233 // could do a polarity for pos range too. and pass back indication of
2235 int pos = dseta.getPos(0).getI();
2238 for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
2240 pos = dseta.getPos(p).getI();
2257 * map from a rangeType's internal frame to the referenced object's coordinate
2261 * @return int [] { ref(pos)...} for all pos in rangeType's frame.
2263 private int[] getMapping(RangeType dseta)
2265 Vector posList = new Vector();
2269 if (dseta.getSegCount() > 0 && dseta.getPosCount() > 0)
2272 "Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
2274 if (dseta.getSegCount() > 0)
2276 for (int s = 0, sSize = dseta.getSegCount(); s < sSize; s++)
2278 se = getSegRange(dseta.getSeg(s), false);
2279 int se_end = se[1 - se[2]] + (se[2] == 0 ? 1 : -1);
2280 for (int p = se[se[2]]; p != se_end; p += se[2] == 0 ? 1 : -1)
2282 posList.add(new Integer(p));
2286 else if (dseta.getPosCount() > 0)
2288 int pos = dseta.getPos(0).getI();
2290 for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
2292 pos = dseta.getPos(p).getI();
2293 posList.add(new Integer(pos));
2297 if (posList != null && posList.size() > 0)
2299 int[] range = new int[posList.size()];
2300 for (int i = 0; i < range.length; i++)
2302 range[i] = ((Integer) posList.elementAt(i)).intValue();
2313 * where the from range is the local mapped range, and the to
2314 * range is the 'mapped' range in the MapRangeType
2321 private jalview.util.MapList parsemapType(MapType maprange, int localu,
2324 jalview.util.MapList ml = null;
2325 int[] localRange = getMapping(maprange.getLocal());
2326 int[] mappedRange = getMapping(maprange.getMapped());
2327 long lu = maprange.getLocal().hasUnit() ? maprange.getLocal().getUnit()
2329 long mu = maprange.getMapped().hasUnit() ? maprange.getMapped()
2330 .getUnit() : mappedu;
2331 ml = new jalview.util.MapList(localRange, mappedRange, (int) lu,
2337 * initialise a range type object from a set of start/end inclusive intervals
2342 private void initRangeType(RangeType mrt, int[] range)
2344 for (int i = 0; i < range.length; i += 2)
2346 Seg vSeg = new Seg();
2347 vSeg.setStart(range[i]);
2348 vSeg.setEnd(range[i + 1]);
2354 * initialise a MapType object from a MapList object.
2360 private void initMapType(MapType maprange, jalview.util.MapList ml,
2363 maprange.setLocal(new Local());
2364 maprange.setMapped(new Mapped());
2365 initRangeType(maprange.getLocal(), ml.getFromRanges());
2366 initRangeType(maprange.getMapped(), ml.getToRanges());
2369 maprange.getLocal().setUnit(ml.getFromRatio());
2370 maprange.getLocal().setUnit(ml.getToRatio());
2375 * not needed now. Provenance getVamsasProvenance(jalview.datamodel.Provenance
2376 * jprov) { jalview.datamodel.ProvenanceEntry[] entries = null; // TODO: fix
2377 * App and Action here. Provenance prov = new Provenance();
2378 * org.exolab.castor.types.Date date = new org.exolab.castor.types.Date( new
2379 * java.util.Date()); Entry provEntry;
2381 * if (jprov != null) { entries = jprov.getEntries(); for (int i = 0; i <
2382 * entries.length; i++) { provEntry = new Entry(); try { date = new
2383 * org.exolab.castor.types.Date(entries[i].getDate()); } catch (Exception ex) {
2384 * ex.printStackTrace();
2386 * date = new org.exolab.castor.types.Date(entries[i].getDate()); }
2387 * provEntry.setDate(date); provEntry.setUser(entries[i].getUser());
2388 * provEntry.setAction(entries[i].getAction()); prov.addEntry(provEntry); } }
2389 * else { provEntry = new Entry(); provEntry.setDate(date);
2390 * provEntry.setUser(System.getProperty("user.name")); // TODO: ext string
2391 * provEntry.setApp("JVAPP"); // TODO: ext string provEntry.setAction(action);
2392 * prov.addEntry(provEntry); }
2396 jalview.datamodel.Provenance getJalviewProvenance(Provenance prov)
2398 // TODO: fix App and Action entries and check use of provenance in jalview.
2399 jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();
2400 for (int i = 0; i < prov.getEntryCount(); i++)
2402 jprov.addEntry(prov.getEntry(i).getUser(), prov.getEntry(i)
2403 .getAction(), prov.getEntry(i).getDate(), prov.getEntry(i)
2412 * @return default initial provenance list for a Jalview created vamsas
2415 Provenance dummyProvenance()
2417 return dummyProvenance(null);
2420 Entry dummyPEntry(String action)
2422 Entry entry = new Entry();
2423 entry.setApp(this.provEntry.getApp());
2426 entry.setAction(action);
2430 entry.setAction("created.");
2432 entry.setDate(new java.util.Date());
2433 entry.setUser(this.provEntry.getUser());
2437 Provenance dummyProvenance(String action)
2439 Provenance prov = new Provenance();
2440 prov.addEntry(dummyPEntry(action));
2444 Entry addProvenance(Provenance p, String action)
2446 Entry dentry = dummyPEntry(action);
2451 public Entry getProvEntry()
2456 public IClientDocument getClientDocument()
2461 public IdentityHashMap getJvObjectBinding()
2466 public Hashtable getVamsasObjectBinding()
2471 public void storeSequenceMappings(AlignViewport viewport, String title)
2474 AlignViewport av = viewport;
2477 jalview.datamodel.AlignmentI jal = av.getAlignment();
2478 // /////////////////////////////////////////
2480 DataSet dataset = null;
2481 if (jal.getDataset() == null)
2483 Cache.log.warn("Creating new dataset for an alignment.");
2484 jal.setDataset(null);
2486 dataset = (DataSet) getjv2vObj(jal.getDataset());
2487 // Store any sequence mappings.
2488 if (av.getAlignment().getCodonFrames() != null
2489 && av.getAlignment().getCodonFrames().length > 0)
2491 jalview.datamodel.AlignedCodonFrame[] cframes = av.getAlignment()
2493 for (int cf = 0; cf < cframes.length; cf++)
2495 if (cframes[cf].getdnaSeqs().length > 0)
2497 jalview.datamodel.SequenceI[] dmps = cframes[cf].getdnaSeqs();
2498 jalview.datamodel.Mapping[] mps = cframes[cf].getProtMappings();
2499 for (int smp = 0; smp < mps.length; smp++)
2501 uk.ac.vamsas.objects.core.SequenceType mfrom = (SequenceType) getjv2vObj(dmps[smp]);
2504 new jalview.io.vamsas.Sequencemapping(this, mps[smp],
2510 .warn("NO Vamsas Binding for local sequence! NOT CREATING MAPPING FOR "
2511 + dmps[smp].getDisplayId(true)
2513 + mps[smp].getTo().getName());
2519 } catch (Exception e)
2521 throw new Exception("Couldn't store sequence mappings for " + title,