+ // Store Jalview specific stuff in the Jalview appData
+ // not implemented in the SimpleDoc interface.
+ }
+
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+
+ }
+
+ /**
+ * remove docseqs from the given alignment marking provenance appropriately
+ * and removing any references to the sequences.
+ *
+ * @param alignment
+ * @param docseqs
+ */
+ private void removeValignmentSequences(Alignment alignment, Vector docseqs)
+ {
+ // delete these from document. This really needs to be a generic document
+ // API function derived by CASTOR.
+ Enumeration en = docseqs.elements();
+ while (en.hasMoreElements())
+ {
+ alignment.removeAlignmentSequence((AlignmentSequence) en
+ .nextElement());
+ }
+ Entry pe = addProvenance(alignment.getProvenance(), "Removed "
+ + docseqs.size() + " sequences");
+ en = alignment.enumerateAlignmentAnnotation();
+ Vector toremove = new Vector();
+ while (en.hasMoreElements())
+ {
+ uk.ac.vamsas.objects.core.AlignmentAnnotation alan = (uk.ac.vamsas.objects.core.AlignmentAnnotation) en
+ .nextElement();
+ if (alan.getSeqrefsCount() > 0)
+ {
+ int p = 0;
+ Vector storem = new Vector();
+ Enumeration sr = alan.enumerateSeqrefs();
+ while (sr.hasMoreElements())
+ {
+ Object alsr = sr.nextElement();
+ if (docseqs.contains(alsr))
+ {
+ storem.addElement(alsr);
+ }
+ }
+ // remove references to the deleted sequences
+ sr = storem.elements();
+ while (sr.hasMoreElements())
+ {
+ alan.removeSeqrefs(sr.nextElement());
+ }
+
+ if (alan.getSeqrefsCount() == 0)
+ {
+ // should then delete alan from dataset
+ toremove.addElement(alan);
+ }
+ }
+ }
+ // remove any annotation that used to be associated to a specific bunch of
+ // sequences
+ en = toremove.elements();
+ while (en.hasMoreElements())
+ {
+ alignment
+ .removeAlignmentAnnotation((uk.ac.vamsas.objects.core.AlignmentAnnotation) en
+ .nextElement());
+ }
+ // TODO: search through alignment annotations to remove any references to
+ // this alignment sequence
+ }
+
+ /**
+ * sync a jalview alignment seuqence into a vamsas alignment assumes all lock
+ * transformation/bindings have been sorted out before hand. creates/syncs the
+ * vamsas alignment sequence for jvalsq and adds it to the alignment if
+ * necessary. unbounddocseq is a duplicate of the vamsas alignment sequences
+ * and these are removed after being processed w.r.t a bound jvalsq
+ *
+ */
+ private boolean syncToAlignmentSequence(SequenceI jvalsq,
+ Alignment alignment, Vector unbounddocseq)
+ {
+ boolean modal = false;
+ // todo: islocked method here
+ boolean up2doc = false;
+ AlignmentSequence alseq = (AlignmentSequence) getjv2vObj(jvalsq);
+ if (alseq == null)
+ {
+ alseq = new AlignmentSequence();
+ up2doc = true;
+ }
+ else
+ {
+ if (unbounddocseq != null)
+ {
+ unbounddocseq.removeElement(alseq);
+ }
+ }
+ // boolean locked = (alignment.getModifiable()==null ||
+ // alignment.getModifiable().length()>0);
+ // TODO: VAMSAS: translate lowercase symbols to annotation ?
+ if (up2doc || !alseq.getSequence().equals(jvalsq.getSequenceAsString()))
+ {
+ alseq.setSequence(jvalsq.getSequenceAsString());
+ alseq.setStart(jvalsq.getStart());
+ alseq.setEnd(jvalsq.getEnd());
+ modal = true;
+ }
+ if (up2doc || !alseq.getName().equals(jvalsq.getName()))
+ {
+ modal = true;
+ alseq.setName(jvalsq.getName());
+ }
+ if (jvalsq.getDescription() != null
+ && (alseq.getDescription() == null || !jvalsq.getDescription()
+ .equals(alseq.getDescription())))
+ {
+ modal = true;
+ alseq.setDescription(jvalsq.getDescription());
+ }
+ if (getjv2vObj(jvalsq.getDatasetSequence()) == null)
+ {
+ Cache.log
+ .warn("Serious Implementation error - Unbound dataset sequence in alignment: "
+ + jvalsq.getDatasetSequence());
+ }
+ alseq.setRefid(getjv2vObj(jvalsq.getDatasetSequence()));
+ if (up2doc)
+ {
+
+ alignment.addAlignmentSequence(alseq);
+ bindjvvobj(jvalsq, alseq);
+ }
+ return up2doc || modal;
+ }
+
+ /**
+ * locally sync a jalview alignment seuqence from a vamsas alignment assumes
+ * all lock transformation/bindings have been sorted out before hand.
+ * creates/syncs the jvalsq from the alignment sequence
+ */
+ private boolean syncFromAlignmentSequence(AlignmentSequence valseq,
+ char valGapchar, char gapChar, Vector dsseqs)
+
+ {
+ boolean modal = false;
+ // todo: islocked method here
+ boolean upFromdoc = false;
+ jalview.datamodel.SequenceI alseq = (SequenceI) getvObj2jv(valseq);
+ if (alseq == null)
+ {
+ upFromdoc = true;
+ }
+ if (alseq != null)
+ {
+
+ // boolean locked = (alignment.getModifiable()==null ||
+ // alignment.getModifiable().length()>0);
+ // TODO: VAMSAS: translate lowercase symbols to annotation ?
+ if (upFromdoc
+ || !valseq.getSequence().equals(alseq.getSequenceAsString()))
+ {
+ // this might go *horribly* wrong
+ alseq.setSequence(new String(valseq.getSequence()).replace(
+ valGapchar, gapChar));
+ alseq.setStart((int) valseq.getStart());
+ alseq.setEnd((int) valseq.getEnd());
+ modal = true;
+ }
+ if (!valseq.getName().equals(alseq.getName()))
+ {
+ modal = true;
+ alseq.setName(valseq.getName());
+ }
+ if (alseq.getDescription() == null
+ || (valseq.getDescription() == null || alseq.getDescription()
+ .equals(valseq.getDescription())))
+ {
+ alseq.setDescription(valseq.getDescription());
+ modal = true;
+ }
+ if (modal && Cache.log.isDebugEnabled())
+ {
+ Cache.log.debug("Updating apparently edited sequence "
+ + alseq.getName());
+ }
+ }
+ else
+ {
+ alseq = new jalview.datamodel.Sequence(valseq.getName(), valseq
+ .getSequence().replace(valGapchar, gapChar), (int) valseq
+ .getStart(), (int) valseq.getEnd());
+
+ Vobject datsetseq = (Vobject) valseq.getRefid();
+ if (datsetseq != null)
+ {
+ alseq.setDatasetSequence((SequenceI) getvObj2jv(datsetseq)); // exceptions
+ // if
+ // AlignemntSequence
+ // reference
+ // isn't
+ // a
+ // simple
+ // SequenceI
+ }
+ else
+ {
+ Cache.log
+ .error("Invalid dataset sequence id (null) for alignment sequence "
+ + valseq.getVorbaId());
+ }
+ bindjvvobj(alseq, valseq);
+ alseq.setVamsasId(valseq.getVorbaId().getId());
+ dsseqs.add(alseq);
+ }
+ Vobject datsetseq = (Vobject) valseq.getRefid();
+ if (datsetseq != null)
+ {
+ if (datsetseq != alseq.getDatasetSequence())
+ {
+ modal = true;
+ }
+ alseq.setDatasetSequence((SequenceI) getvObj2jv(datsetseq)); // exceptions
+ }
+ return upFromdoc || modal;
+ }
+
+ private void initRangeAnnotationType(RangeAnnotation an,
+ AlignmentAnnotation alan, int[] gapMap)
+ {
+ Seg vSeg = new Seg();
+ vSeg.setStart(1);
+ vSeg.setInclusive(true);
+ vSeg.setEnd(gapMap.length);
+ an.addSeg(vSeg);
+
+ // LATER: much of this is verbatim from the alignmentAnnotation
+ // method below. suggests refactoring to make rangeAnnotation the
+ // base class
+ an.setDescription(alan.description);
+ an.setLabel(alan.label);
+ an.setGroup(Integer.toString(alan.graphGroup));
+ // // JBPNote -
+ // originally we
+ // were going to
+ // store
+ // graphGroup in
+ // the Jalview
+ // specific
+ // bits.
+ AnnotationElement ae;
+ for (int a = 0; a < alan.annotations.length; a++)
+ {
+ if (alan.annotations[a] == null)
+ {
+ continue;
+ }
+
+ ae = new AnnotationElement();
+ ae.setDescription(alan.annotations[a].description);
+ ae.addGlyph(new Glyph());
+ ae.getGlyph(0).setContent(alan.annotations[a].displayCharacter); // assume
+ // jax-b
+ // takes
+ // care
+ // of
+ // utf8
+ // translation
+ if (alan.graph != jalview.datamodel.AlignmentAnnotation.NO_GRAPH)
+ {
+ ae.addValue(alan.annotations[a].value);
+ }
+ ae.setPosition(gapMap[a] + 1); // position w.r.t. AlignmentSequence
+ // symbols
+ if (alan.annotations[a].secondaryStructure != ' ')
+ {
+ // we only write an annotation where it really exists.
+ Glyph ss = new Glyph();
+ ss
+ .setDict(uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE);
+ ss.setContent(String
+ .valueOf(alan.annotations[a].secondaryStructure));
+ ae.addGlyph(ss);
+ }
+ an.addAnnotationElement(ae);
+ }
+
+ }
+
+ private void saveDatasetSequenceAnnotation(HashMap AlSeqMaps,
+ uk.ac.vamsas.objects.core.Sequence sref, AlignmentAnnotation alan)
+ {
+ // {
+ // uk.ac.vamsas.
+ // objects.core.AlignmentSequence alsref = (uk.ac.vamsas.
+ // objects.core.AlignmentSequence) sref;
+ uk.ac.vamsas.objects.core.DataSetAnnotations an = (uk.ac.vamsas.objects.core.DataSetAnnotations) getjv2vObj(alan);
+ int[] gapMap = getGapMap(AlSeqMaps, alan);
+ if (an == null)
+ {
+ an = new uk.ac.vamsas.objects.core.DataSetAnnotations();
+ initRangeAnnotationType(an, alan, gapMap);
+
+ an.setProvenance(dummyProvenance()); // get provenance as user
+ // created, or jnet, or
+ // something else.
+ setAnnotationType(an, alan);
+ an.setGroup(Integer.toString(alan.graphGroup)); // // JBPNote -
+ // originally we
+ // were going to
+ // store
+ // graphGroup in
+ // the Jalview
+ // specific
+ // bits.
+ if (alan.getThreshold() != null && alan.getThreshold().displayed)
+ {
+ an.addProperty(newProperty(THRESHOLD, "float", ""
+ + alan.getThreshold().value));
+ if (alan.getThreshold().label != null)
+ an.addProperty(newProperty(THRESHOLD + "Name", "string", ""
+ + alan.getThreshold().label));
+ }
+ ((DataSet) sref.getV_parent()).addDataSetAnnotations(an);
+ bindjvvobj(alan, an);
+ }
+ else
+ {
+ // update reference sequence Annotation
+ if (an.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT LOCK
+ // METHODS)
+ {
+ // verify existing alignment sequence annotation is up to date
+ System.out.println("update dataset sequence annotation.");
+ }
+ else
+ {
+ // verify existing alignment sequence annotation is up to date
+ System.out
+ .println("make new alignment dataset sequence annotation if modification has happened.");
+ }
+ }
+
+ }
+
+ private int[] getGapMap(HashMap AlSeqMaps, AlignmentAnnotation alan)
+ {
+ int[] gapMap;
+ if (AlSeqMaps.containsKey(alan.sequenceRef))
+ {
+ gapMap = (int[]) AlSeqMaps.get(alan.sequenceRef);
+ }
+ else
+ {
+ gapMap = new int[alan.sequenceRef.getLength()];
+ // map from alignment position to sequence position.
+ int[] sgapMap = alan.sequenceRef.gapMap();
+ for (int a = 0; a < sgapMap.length; a++)
+ {
+ gapMap[sgapMap[a]] = a;
+ }
+ }
+ return gapMap;
+ }
+
+ private void saveAlignmentSequenceAnnotation(HashMap AlSeqMaps,
+ AlignmentSequence alsref, AlignmentAnnotation alan)
+ {
+ // {
+ // uk.ac.vamsas.
+ // objects.core.AlignmentSequence alsref = (uk.ac.vamsas.
+ // objects.core.AlignmentSequence) sref;
+ uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation an = (uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation) getjv2vObj(alan);
+ int[] gapMap = getGapMap(AlSeqMaps, alan);
+ if (an == null)
+ {
+ an = new uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation();
+ initRangeAnnotationType(an, alan, gapMap);
+ /**
+ * I mean here that we don't actually have a semantic 'type' for the
+ * annotation (this might be - score, intrinsic property, measurement,
+ * something extracted from another program, etc)
+ */
+ an.setType(JALVIEW_ANNOTATION_ROW); // TODO: better fix
+ // this rough guess ;)
+ alsref.addAlignmentSequenceAnnotation(an);
+ bindjvvobj(alan, an);
+ // These properties are directly supported by the
+ // AlignmentSequenceAnnotation type.
+ setAnnotationType(an, alan);
+ an.setProvenance(dummyProvenance()); // get provenance as user
+ // created, or jnet, or
+ // something else.
+ }
+ else
+ {
+ // update reference sequence Annotation
+ if (an.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT LOCK
+ // METHODS)
+ {
+ // verify existing alignment sequence annotation is up to date
+ System.out.println("update alignment sequence annotation.");
+ }
+ else
+ {
+ // verify existing alignment sequence annotation is up to date
+ System.out
+ .println("make new alignment sequence annotation if modification has happened.");
+ }
+ }
+ }
+
+ /**
+ * set vamsas annotation object type from jalview annotation
+ *
+ * @param an
+ * @param alan
+ */
+ private void setAnnotationType(RangeAnnotation an,
+ AlignmentAnnotation alan)
+ {
+ if (an instanceof AlignmentSequenceAnnotation)
+ {
+ if (alan.graph != AlignmentAnnotation.NO_GRAPH)
+ {
+ ((AlignmentSequenceAnnotation) an).setGraph(true);
+ }
+ else
+ {
+ ((AlignmentSequenceAnnotation) an).setGraph(false);
+ }
+ }
+ if (an instanceof uk.ac.vamsas.objects.core.AlignmentAnnotation)
+ {
+ if (alan.graph != AlignmentAnnotation.NO_GRAPH)
+ {
+ ((uk.ac.vamsas.objects.core.AlignmentAnnotation) an).setGraph(true);
+ }
+ else
+ {
+ ((uk.ac.vamsas.objects.core.AlignmentAnnotation) an)
+ .setGraph(false);
+ }