Clover icon

Coverage Report

  1. Project Clover database Wed Dec 3 2025 17:03:17 GMT
  2. Package jalview.io

File VamsasAppDatastore.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
60% of files have more coverage

Code metrics

544
896
53
1
2,844
2,045
374
0.42
16.91
53
7.06

Classes

Class Line # Actions
VamsasAppDatastore 89 896 374
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.io;
22   
23    import jalview.bin.Console;
24    import jalview.datamodel.AlignedCodonFrame;
25    import jalview.datamodel.AlignmentAnnotation;
26    import jalview.datamodel.GraphLine;
27    import jalview.datamodel.SequenceI;
28    import jalview.gui.AlignFrame;
29    import jalview.gui.AlignViewport;
30    import jalview.gui.Desktop;
31    import jalview.gui.TreePanel;
32    import jalview.io.vamsas.Datasetsequence;
33    import jalview.io.vamsas.DatastoreItem;
34    import jalview.io.vamsas.DatastoreRegistry;
35    import jalview.io.vamsas.Rangetype;
36    import jalview.project.Jalview2XML;
37    import jalview.util.MessageManager;
38    import jalview.util.jarInputStreamProvider;
39    import jalview.viewmodel.AlignmentViewport;
40   
41    import java.io.File;
42    import java.io.IOException;
43    import java.util.Enumeration;
44    import java.util.HashMap;
45    import java.util.Hashtable;
46    import java.util.IdentityHashMap;
47    import java.util.Iterator;
48    import java.util.List;
49    import java.util.Vector;
50    import java.util.jar.JarInputStream;
51    import java.util.jar.JarOutputStream;
52   
53    import uk.ac.vamsas.client.IClientAppdata;
54    import uk.ac.vamsas.client.IClientDocument;
55    import uk.ac.vamsas.client.Vobject;
56    import uk.ac.vamsas.client.VorbaId;
57    import uk.ac.vamsas.objects.core.Alignment;
58    import uk.ac.vamsas.objects.core.AlignmentSequence;
59    import uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation;
60    import uk.ac.vamsas.objects.core.AnnotationElement;
61    import uk.ac.vamsas.objects.core.DataSet;
62    import uk.ac.vamsas.objects.core.DataSetAnnotations;
63    import uk.ac.vamsas.objects.core.DbRef;
64    import uk.ac.vamsas.objects.core.Entry;
65    import uk.ac.vamsas.objects.core.Glyph;
66    import uk.ac.vamsas.objects.core.Local;
67    import uk.ac.vamsas.objects.core.MapType;
68    import uk.ac.vamsas.objects.core.Mapped;
69    import uk.ac.vamsas.objects.core.Property;
70    import uk.ac.vamsas.objects.core.Provenance;
71    import uk.ac.vamsas.objects.core.RangeAnnotation;
72    import uk.ac.vamsas.objects.core.RangeType;
73    import uk.ac.vamsas.objects.core.Seg;
74    import uk.ac.vamsas.objects.core.Sequence;
75    import uk.ac.vamsas.objects.core.SequenceType;
76    import uk.ac.vamsas.objects.core.VAMSAS;
77    import uk.ac.vamsas.objects.utils.Properties;
78   
79    /*
80    *
81    * static {
82    * org.exolab.castor.util.LocalConfiguration.getInstance().getProperties().setProperty(
83    * "org.exolab.castor.serializer", "org.apache.xml.serialize.XMLSerilazizer"); }
84    *
85    */
86    /*
87    * TODO: check/verify consistency for vamsas sync with group associated alignment annotation
88    */
 
89    public class VamsasAppDatastore
90    {
91    /**
92    * Type used for general jalview generated annotation added to vamsas document
93    */
94    public static final String JALVIEW_ANNOTATION_ROW = "JalviewAnnotation";
95   
96    /**
97    * AlignmentAnnotation property to indicate that values should not be
98    * interpolated
99    */
100    public static final String DISCRETE_ANNOTATION = "discrete";
101   
102    /**
103    * continuous property - optional to specify that annotation should be
104    * represented as a continous graph line
105    */
106    private static final String CONTINUOUS_ANNOTATION = "continuous";
107   
108    private static final String THRESHOLD = "threshold";
109   
110    /**
111    * template for provenance entries written to vamsas session document
112    */
113    Entry provEntry = null;
114   
115    /**
116    * Instance of the session document being synchronized with
117    */
118    IClientDocument cdoc;
119   
120    /**
121    * map Vorba (vamsas object xml ref) IDs to live jalview object references
122    */
123    Hashtable vobj2jv;
124   
125    /**
126    * map live jalview object references to Vorba IDs
127    */
128    IdentityHashMap jv2vobj;
129   
130    /**
131    * map jalview sequence set ID (which is vorba ID for alignment) to last
132    * recorded hash value for the alignment viewport (the undo/redo hash value)
133    */
134    Hashtable alignRDHash;
135   
 
136  0 toggle public VamsasAppDatastore(IClientDocument cdoc, Hashtable vobj2jv,
137    IdentityHashMap jv2vobj, Entry provEntry, Hashtable alignRDHash)
138    {
139  0 this.cdoc = cdoc;
140  0 this.vobj2jv = vobj2jv;
141  0 this.jv2vobj = jv2vobj;
142  0 this.provEntry = provEntry;
143  0 this.alignRDHash = alignRDHash;
144  0 buildSkipList();
145    }
146   
147    /**
148    * the skipList used to skip over views from Jalview Appdata's that we've
149    * already syncrhonized
150    */
151    Hashtable skipList;
152   
 
153  0 toggle private void buildSkipList()
154    {
155  0 skipList = new Hashtable();
156  0 AlignFrame[] al = Desktop.getDesktopAlignFrames();
157  0 for (int f = 0; al != null && f < al.length; f++)
158    {
159  0 skipList.put(al[f].getViewport().getSequenceSetId(), al[f]);
160    }
161    }
162   
163    /**
164    * @return the Vobject bound to Jalview datamodel object
165    */
 
166  0 toggle protected Vobject getjv2vObj(Object jvobj)
167    {
168  0 if (jv2vobj.containsKey(jvobj))
169    {
170  0 return cdoc.getObject((VorbaId) jv2vobj.get(jvobj));
171    }
172    // check if we're working with a string - then workaround
173    // the use of IdentityHashTable because different strings
174    // have different object IDs.
175  0 if (jvobj instanceof String)
176    {
177  0 Object seqsetidobj = null;
178  0 seqsetidobj = getVamsasObjectBinding().get(jvobj);
179  0 if (seqsetidobj != null)
180    {
181  0 if (seqsetidobj instanceof String)
182    {
183    // what is expected. object returned by av.getSequenceSetId() -
184    // reverse lookup to get the 'registered' instance of this string
185  0 Vobject obj = getjv2vObj(seqsetidobj);
186  0 if (obj != null && !(obj instanceof Alignment))
187    {
188  0 Console.warn(
189    "IMPLEMENTATION ERROR?: Unexpected mapping for unmapped jalview string object content:"
190    + seqsetidobj + " to object " + obj);
191    }
192  0 return obj;
193    }
194    else
195    {
196  0 Console.warn("Unexpected mapping for Jalview String Object ID "
197    + seqsetidobj + " to another jalview dataset object "
198    + seqsetidobj);
199    }
200    }
201    }
202   
203  0 if (Console.isDebugEnabled())
204    {
205  0 Console.debug(
206    "Returning null VorbaID binding for jalview object " + jvobj);
207    }
208  0 return null;
209    }
210   
211    /**
212    *
213    * @param vobj
214    * @return Jalview datamodel object bound to the vamsas document object
215    */
 
216  0 toggle protected Object getvObj2jv(uk.ac.vamsas.client.Vobject vobj)
217    {
218  0 VorbaId id = vobj.getVorbaId();
219  0 if (id == null)
220    {
221  0 id = cdoc.registerObject(vobj);
222  0 Console.debug(
223    "Registering new object and returning null for getvObj2jv");
224  0 return null;
225    }
226  0 if (vobj2jv.containsKey(vobj.getVorbaId()))
227    {
228  0 return vobj2jv.get(vobj.getVorbaId());
229    }
230  0 return null;
231    }
232   
 
233  0 toggle protected void bindjvvobj(Object jvobj, uk.ac.vamsas.client.Vobject vobj)
234    {
235  0 VorbaId id = vobj.getVorbaId();
236  0 if (id == null)
237    {
238  0 id = cdoc.registerObject(vobj);
239  0 if (id == null || vobj.getVorbaId() == null
240    || cdoc.getObject(id) != vobj)
241    {
242  0 Console.error("Failed to get id for "
243  0 + (vobj.isRegisterable() ? "registerable"
244    : "unregisterable")
245    + " object " + vobj);
246    }
247    }
248   
249  0 if (vobj2jv.containsKey(vobj.getVorbaId())
250    && !((VorbaId) vobj2jv.get(vobj.getVorbaId())).equals(jvobj))
251    {
252  0 Console.debug(
253    "Warning? Overwriting existing vamsas id binding for "
254    + vobj.getVorbaId(),
255    new Exception(MessageManager.getString(
256    "exception.overwriting_vamsas_id_binding")));
257    }
258  0 else if (jv2vobj.containsKey(jvobj)
259    && !((VorbaId) jv2vobj.get(jvobj)).equals(vobj.getVorbaId()))
260    {
261  0 Console.debug(
262    "Warning? Overwriting existing jalview object binding for "
263    + jvobj,
264    new Exception("Overwriting jalview object binding."));
265    }
266    /*
267    * Cache.error("Attempt to make conflicting object binding! "+vobj+" id "
268    * +vobj.getVorbaId()+" already bound to "+getvObj2jv(vobj)+" and "+jvobj+"
269    * already bound to "+getjv2vObj(jvobj),new Exception("Excessive call to
270    * bindjvvobj")); }
271    */
272    // we just update the hash's regardless!
273  0 Console.debug("Binding " + vobj.getVorbaId() + " to " + jvobj);
274  0 vobj2jv.put(vobj.getVorbaId(), jvobj);
275    // JBPNote - better implementing a hybrid invertible hash.
276  0 jv2vobj.put(jvobj, vobj.getVorbaId());
277    }
278   
279    /**
280    * put the alignment viewed by AlignViewport into cdoc.
281    *
282    * @param av
283    * alignViewport to be stored
284    * @param aFtitle
285    * title for alignment
286    * @return true if alignment associated with viewport was stored/synchronized
287    * to document
288    */
 
289  0 toggle public boolean storeVAMSAS(AlignViewport av, String aFtitle)
290    {
291  0 try
292    {
293  0 jalview.datamodel.AlignmentI jal = av.getAlignment();
294  0 jalview.datamodel.AlignmentI jds = jal.getDataset();
295  0 boolean nw = false;
296  0 VAMSAS root = null; // will be resolved based on Dataset Parent.
297    // /////////////////////////////////////////
298    // SAVE THE DATASET
299  0 DataSet dataset = null;
300  0 if (jds == null)
301    {
302  0 Console.warn("Creating new dataset for an alignment.");
303  0 jal.setDataset(null);
304  0 jds = jal.getDataset();
305    }
306   
307    // try and get alignment and association for sequence set id
308   
309  0 Alignment alignment = (Alignment) getjv2vObj(av.getSequenceSetId());
310  0 if (alignment != null)
311    {
312  0 dataset = (DataSet) alignment.getV_parent();
313    }
314    else
315    {
316    // is the dataset already registered
317  0 dataset = (DataSet) getjv2vObj(jds);
318    }
319   
320  0 if (dataset == null)
321    {
322    // it might be that one of the dataset sequences does actually have a
323    // binding, so search for it indirectly. If it does, then the local
324    // jalview dataset
325    // must be merged with the existing vamsas dataset.
326  0 jalview.datamodel.SequenceI[] jdatset = jds.getSequencesArray();
327  0 for (int i = 0; i < jdatset.length; i++)
328    {
329  0 Vobject vbound = getjv2vObj(jdatset[i]);
330  0 if (vbound != null)
331    {
332  0 if (vbound instanceof uk.ac.vamsas.objects.core.Sequence)
333    {
334  0 if (dataset == null)
335    {
336  0 dataset = (DataSet) vbound.getV_parent();
337    }
338    else
339    {
340  0 if (vbound.getV_parent() != null
341    && dataset != vbound.getV_parent())
342    {
343  0 throw new Error(
344    "IMPLEMENTATION ERROR: Cannot map an alignment of sequences from different datasets into a single alignment in the vamsas document.");
345    }
346    }
347    }
348    }
349    }
350    }
351   
352  0 if (dataset == null)
353    {
354  0 Console.warn("Creating new vamsas dataset for alignment view "
355    + av.getSequenceSetId());
356    // we create a new dataset on the default vamsas root.
357  0 root = cdoc.getVamsasRoots()[0]; // default vamsas root for modifying.
358  0 dataset = new DataSet();
359  0 root.addDataSet(dataset);
360  0 bindjvvobj(jds, dataset);
361  0 dataset.setProvenance(dummyProvenance());
362    // dataset.getProvenance().addEntry(provEntry);
363  0 nw = true;
364    }
365    else
366    {
367  0 root = (VAMSAS) dataset.getV_parent();
368    }
369    // update dataset
370  0 Sequence sequence;
371    // set new dataset and alignment sequences based on alignment Nucleotide
372    // flag.
373    // this *will* break when alignment contains both nucleotide and amino
374    // acid sequences.
375  0 String dict = jal.isNucleotide()
376    ? uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_NA
377    : uk.ac.vamsas.objects.utils.SymbolDictionary.STANDARD_AA;
378  0 Vector dssmods = new Vector();
379  0 for (int i = 0; i < jal.getHeight(); i++)
380    {
381  0 SequenceI sq = jal.getSequenceAt(i).getDatasetSequence(); // only insert
382    // referenced
383    // sequences
384    // to dataset.
385  0 Datasetsequence dssync = new jalview.io.vamsas.Datasetsequence(this,
386    sq, dict, dataset);
387  0 sequence = (Sequence) dssync.getVobj();
388  0 if (dssync.getModified())
389    {
390  0 dssmods.addElement(sequence);
391    }
392  0 ;
393    }
394  0 if (dssmods.size() > 0)
395    {
396  0 if (!nw)
397    {
398  0 Entry pentry = this.addProvenance(dataset.getProvenance(),
399    "updated sequences");
400    // pentry.addInput(vInput); could write in which sequences were
401    // modified.
402  0 dssmods.removeAllElements();
403    }
404    }
405    // dataset.setProvenance(getVamsasProvenance(jal.getDataset().getProvenance()));
406    // ////////////////////////////////////////////
407  0 if (alignmentWillBeSkipped(av))
408    {
409    // TODO: trees could be written - but for the moment we just
410  0 addToSkipList(av);
411    // add to the JalviewXML skipList and ..
412  0 return false;
413    }
414   
415  0 if (alignment == null)
416    {
417  0 alignment = new Alignment();
418  0 bindjvvobj(av.getSequenceSetId(), alignment);
419  0 if (alignment.getProvenance() == null)
420    {
421  0 alignment.setProvenance(new Provenance());
422    }
423  0 addProvenance(alignment.getProvenance(), "added"); // TODO: insert some
424    // sensible source
425    // here
426  0 dataset.addAlignment(alignment);
427    {
428  0 Property title = new Property();
429  0 title.setName("title");
430  0 title.setType("string");
431  0 title.setContent(aFtitle);
432  0 alignment.addProperty(title);
433    }
434  0 alignment.setGapChar(String.valueOf(av.getGapCharacter()));
435  0 for (int i = 0; i < jal.getHeight(); i++)
436    {
437  0 syncToAlignmentSequence(jal.getSequenceAt(i), alignment, null);
438    }
439  0 alignRDHash.put(av.getSequenceSetId(), av.getUndoRedoHash());
440    }
441    else
442    {
443    // always prepare to clone the alignment
444  0 boolean alismod = av.isUndoRedoHashModified(
445    (long[]) alignRDHash.get(av.getSequenceSetId()));
446    // todo: verify and update mutable alignment props.
447    // TODO: Use isLocked methods
448  0 if (alignment.getModifiable() == null
449    || alignment.getModifiable().length() == 0)
450    // && !alignment.isDependedOn())
451    {
452  0 boolean modified = false;
453    // check existing sequences in local and in document.
454  0 Vector docseqs = new Vector(
455    alignment.getAlignmentSequenceAsReference());
456  0 for (int i = 0; i < jal.getHeight(); i++)
457    {
458  0 modified |= syncToAlignmentSequence(jal.getSequenceAt(i),
459    alignment, docseqs);
460    }
461  0 if (docseqs.size() > 0)
462    {
463    // removeValignmentSequences(alignment, docseqs);
464  0 docseqs.removeAllElements();
465  0 jalview.bin.Console.outPrintln(
466    "Sequence deletion from alignment is not implemented.");
467   
468    }
469  0 if (modified)
470    {
471  0 if (alismod)
472    {
473    // info in the undo
474  0 addProvenance(alignment.getProvenance(), "Edited"); // TODO:
475    // insert
476    // something
477    // sensible
478    // here again
479    }
480    else
481    {
482    // info in the undo
483  0 addProvenance(alignment.getProvenance(), "Attributes Edited"); // TODO:
484    // insert
485    // something
486    // sensible
487    // here
488    // again
489    }
490    }
491  0 if (alismod)
492    {
493  0 jalview.bin.Console.outPrintln("update alignment in document.");
494    }
495    else
496    {
497  0 jalview.bin.Console
498    .outPrintln("alignment in document left unchanged.");
499    }
500    }
501    else
502    {
503    // unbind alignment from view.
504    // create new binding and new alignment.
505    // mark trail on new alignment as being derived from old ?
506  0 jalview.bin.Console.outPrintln(
507    "update edited alignment to new alignment in document.");
508    }
509    }
510    // ////////////////////////////////////////////
511    // SAVE Alignment Sequence Features
512  0 for (int i = 0, iSize = alignment
513  0 .getAlignmentSequenceCount(); i < iSize; i++)
514    {
515  0 AlignmentSequence valseq;
516  0 SequenceI alseq = (SequenceI) getvObj2jv(
517    valseq = alignment.getAlignmentSequence(i));
518  0 if (alseq != null && alseq.getSequenceFeatures() != null)
519    {
520    /*
521    * We do not put local Alignment Sequence Features into the vamsas
522    * document yet.
523    *
524    *
525    * jalview.datamodel.SequenceFeature[] features = alseq
526    * .getSequenceFeatures(); for (int f = 0; f < features.length; f++) {
527    * if (features[f] != null) { AlignmentSequenceAnnotation valseqf = (
528    * AlignmentSequenceAnnotation) getjv2vObj(features[i]); if (valseqf
529    * == null) {
530    *
531    * valseqf = (AlignmentSequenceAnnotation) getDSAnnotationFromJalview(
532    * new AlignmentSequenceAnnotation(), features[i]);
533    * valseqf.setGraph(false);
534    * valseqf.addProperty(newProperty("jalview:feature"
535    * ,"boolean","true")); if (valseqf.getProvenance() == null) {
536    * valseqf.setProvenance(new Provenance()); }
537    * addProvenance(valseqf.getProvenance(), "created"); // JBPNote - //
538    * need to // update bindjvvobj(features[i], valseqf);
539    * valseq.addAlignmentSequenceAnnotation(valseqf); } } }
540    */
541    }
542    }
543   
544    // ////////////////////////////////////////////
545    // SAVE ANNOTATIONS
546  0 if (jal.getAlignmentAnnotation() != null)
547    {
548  0 jalview.datamodel.AlignmentAnnotation[] aa = jal
549    .getAlignmentAnnotation();
550  0 java.util.HashMap AlSeqMaps = new HashMap(); // stores int maps from
551    // alignment columns to
552    // sequence positions.
553  0 for (int i = 0; i < aa.length; i++)
554    {
555  0 if (aa[i] == null || isJalviewOnly(aa[i]))
556    {
557  0 continue;
558    }
559  0 if (aa[i].groupRef != null)
560    {
561    // TODO: store any group associated annotation references
562  0 Console.warn(
563    "Group associated sequence annotation is not stored in VAMSAS document.");
564  0 continue;
565    }
566  0 if (aa[i].sequenceRef != null)
567    {
568    // Deal with sequence associated annotation
569  0 Vobject sref = getjv2vObj(aa[i].sequenceRef);
570  0 if (sref instanceof uk.ac.vamsas.objects.core.AlignmentSequence)
571    {
572  0 saveAlignmentSequenceAnnotation(AlSeqMaps,
573    (AlignmentSequence) sref, aa[i]);
574    }
575    else
576    {
577    // first find the alignment sequence to associate this with.
578  0 for (SequenceI jvalsq : av.getAlignment().getSequences())
579    {
580    // saveDatasetSequenceAnnotation(AlSeqMaps,(uk.ac.vamsas.objects.core.Sequence)
581    // sref, aa[i]);
582  0 if (jvalsq.getDatasetSequence() == aa[i].sequenceRef)
583    {
584  0 Vobject alsref = getjv2vObj(jvalsq);
585  0 saveAlignmentSequenceAnnotation(AlSeqMaps,
586    (AlignmentSequence) alsref, aa[i]);
587  0 break;
588    }
589  0 ;
590    }
591    }
592    }
593    else
594    {
595    // add Alignment Annotation
596  0 uk.ac.vamsas.objects.core.AlignmentAnnotation an = (uk.ac.vamsas.objects.core.AlignmentAnnotation) getjv2vObj(
597    aa[i]);
598  0 if (an == null)
599    {
600  0 an = new uk.ac.vamsas.objects.core.AlignmentAnnotation();
601  0 an.setType(JALVIEW_ANNOTATION_ROW);
602  0 an.setDescription(aa[i].description);
603  0 alignment.addAlignmentAnnotation(an);
604  0 Seg vSeg = new Seg(); // TODO: refactor to have a default
605    // rangeAnnotationType initer/updater that
606    // takes a set of int ranges.
607  0 vSeg.setStart(1);
608  0 vSeg.setInclusive(true);
609  0 vSeg.setEnd(jal.getWidth());
610  0 an.addSeg(vSeg);
611  0 if (aa[i].graph > 0)
612    {
613  0 an.setGraph(true); // aa[i].graph);
614    }
615  0 an.setLabel(aa[i].label);
616  0 an.setProvenance(dummyProvenance());
617  0 if (aa[i].graph != AlignmentAnnotation.NO_GRAPH)
618    {
619  0 an.setGroup(Integer.toString(aa[i].graphGroup)); // // JBPNote
620    // -
621    // originally we
622    // were going to
623    // store
624    // graphGroup in
625    // the Jalview
626    // specific
627    // bits.
628  0 an.setGraph(true);
629    }
630    else
631    {
632  0 an.setGraph(false);
633    }
634  0 AnnotationElement ae;
635   
636  0 for (int a = 0; a < aa[i].annotations.length; a++)
637    {
638  0 if ((aa[i] == null) || (aa[i].annotations[a] == null))
639    {
640  0 continue;
641    }
642   
643  0 ae = new AnnotationElement();
644  0 ae.setDescription(aa[i].annotations[a].description);
645  0 ae.addGlyph(new Glyph());
646  0 ae.getGlyph(0)
647    .setContent(aa[i].annotations[a].displayCharacter); // assume
648    // jax-b
649    // takes
650    // care
651    // of
652    // utf8
653    // translation
654  0 if (an.isGraph())
655    {
656  0 ae.addValue(aa[i].annotations[a].value);
657    }
658  0 ae.setPosition(a + 1);
659  0 if (aa[i].annotations[a].secondaryStructure != ' ')
660    {
661  0 Glyph ss = new Glyph();
662  0 ss.setDict(
663    uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE);
664  0 ss.setContent(String.valueOf(
665    aa[i].annotations[a].secondaryStructure));
666  0 ae.addGlyph(ss);
667    }
668  0 an.addAnnotationElement(ae);
669    }
670  0 if (aa[i].editable)
671    {
672    // an.addProperty(newProperty("jalview:editable", null,
673    // "true"));
674    // an.setModifiable(""); // TODO: This is not the way the
675    // modifiable flag is supposed to be used.
676    }
677  0 setAnnotationType(an, aa[i]);
678   
679  0 if (aa[i].graph != jalview.datamodel.AlignmentAnnotation.NO_GRAPH)
680    {
681  0 an.setGraph(true);
682  0 an.setGroup(Integer.toString(aa[i].graphGroup));
683  0 if (aa[i].threshold != null && aa[i].threshold.displayed)
684    {
685  0 an.addProperty(Properties.newProperty(THRESHOLD,
686    Properties.FLOATTYPE,
687    "" + aa[i].threshold.value));
688  0 if (aa[i].threshold.label != null)
689    {
690  0 an.addProperty(Properties.newProperty(
691    THRESHOLD + "Name", Properties.STRINGTYPE,
692    "" + aa[i].threshold.label));
693    }
694    }
695    }
696   
697    }
698   
699    else
700    {
701  0 if (an.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT
702    // LOCK METHODS)
703    {
704    // verify annotation - update (perhaps)
705  0 Console.info(
706    "update alignment sequence annotation. not yet implemented.");
707    }
708    else
709    {
710    // verify annotation - update (perhaps)
711  0 Console.info(
712    "updated alignment sequence annotation added.");
713    }
714    }
715    }
716    }
717    }
718    // /////////////////////////////////////////////////////
719   
720    // //////////////////////////////////////////////
721    // /SAVE THE TREES
722    // /////////////////////////////////
723    // FIND ANY ASSOCIATED TREES
724  0 if (Desktop.getDesktopPane() != null)
725    {
726  0 javax.swing.JInternalFrame[] frames = Desktop.getInstance()
727    .getAllFrames();
728   
729  0 for (int t = 0; t < frames.length; t++)
730    {
731  0 if (frames[t] instanceof TreePanel)
732    {
733  0 TreePanel tp = (TreePanel) frames[t];
734   
735  0 if (tp.getViewPort().getSequenceSetId()
736    .equals(av.getSequenceSetId()))
737    {
738  0 DatastoreItem vtree = new jalview.io.vamsas.Tree(this, tp,
739    jal, alignment);
740    }
741    }
742    }
743    }
744    // Store Jalview specific stuff in the Jalview appData
745    // not implemented in the SimpleDoc interface.
746    }
747   
748    catch (Exception ex)
749    {
750  0 ex.printStackTrace();
751  0 return false;
752    }
753  0 return true;
754    }
755   
756    /**
757    * very quick test to see if the viewport would be stored in the vamsas
758    * document. Reasons for not storing include the unaligned flag being false
759    * (for all sequences, including the hidden ones!)
760    *
761    * @param av
762    * @return true if alignment associated with this view will be stored in
763    * document.
764    */
 
765  0 toggle public boolean alignmentWillBeSkipped(AlignmentViewport av)
766    {
767  0 return (!av.getAlignment().isAligned());
768    }
769   
 
770  0 toggle private void addToSkipList(AlignmentViewport av)
771    {
772  0 if (skipList == null)
773    {
774  0 skipList = new Hashtable();
775    }
776  0 skipList.put(av.getSequenceSetId(), av);
777    }
778   
779    /**
780    * remove docseqs from the given alignment marking provenance appropriately
781    * and removing any references to the sequences.
782    *
783    * @param alignment
784    * @param docseqs
785    */
 
786  0 toggle private void removeValignmentSequences(Alignment alignment,
787    Vector docseqs)
788    {
789    // delete these from document. This really needs to be a generic document
790    // API function derived by CASTOR.
791  0 Enumeration en = docseqs.elements();
792  0 while (en.hasMoreElements())
793    {
794  0 alignment.removeAlignmentSequence(
795    (AlignmentSequence) en.nextElement());
796    }
797  0 Entry pe = addProvenance(alignment.getProvenance(),
798    "Removed " + docseqs.size() + " sequences");
799  0 en = alignment.enumerateAlignmentAnnotation();
800  0 Vector toremove = new Vector();
801  0 while (en.hasMoreElements())
802    {
803  0 uk.ac.vamsas.objects.core.AlignmentAnnotation alan = (uk.ac.vamsas.objects.core.AlignmentAnnotation) en
804    .nextElement();
805  0 if (alan.getSeqrefsCount() > 0)
806    {
807  0 int p = 0;
808  0 Vector storem = new Vector();
809  0 Enumeration sr = alan.enumerateSeqrefs();
810  0 while (sr.hasMoreElements())
811    {
812  0 Object alsr = sr.nextElement();
813  0 if (docseqs.contains(alsr))
814    {
815  0 storem.addElement(alsr);
816    }
817    }
818    // remove references to the deleted sequences
819  0 sr = storem.elements();
820  0 while (sr.hasMoreElements())
821    {
822  0 alan.removeSeqrefs(sr.nextElement());
823    }
824   
825  0 if (alan.getSeqrefsCount() == 0)
826    {
827    // should then delete alan from dataset
828  0 toremove.addElement(alan);
829    }
830    }
831    }
832    // remove any annotation that used to be associated to a specific bunch of
833    // sequences
834  0 en = toremove.elements();
835  0 while (en.hasMoreElements())
836    {
837  0 alignment.removeAlignmentAnnotation(
838    (uk.ac.vamsas.objects.core.AlignmentAnnotation) en
839    .nextElement());
840    }
841    // TODO: search through alignment annotations to remove any references to
842    // this alignment sequence
843    }
844   
845    /**
846    * sync a jalview alignment seuqence into a vamsas alignment assumes all lock
847    * transformation/bindings have been sorted out before hand. creates/syncs the
848    * vamsas alignment sequence for jvalsq and adds it to the alignment if
849    * necessary. unbounddocseq is a duplicate of the vamsas alignment sequences
850    * and these are removed after being processed w.r.t a bound jvalsq
851    *
852    */
 
853  0 toggle private boolean syncToAlignmentSequence(SequenceI jvalsq,
854    Alignment alignment, Vector unbounddocseq)
855    {
856  0 boolean modal = false;
857    // todo: islocked method here
858  0 boolean up2doc = false;
859  0 AlignmentSequence alseq = (AlignmentSequence) getjv2vObj(jvalsq);
860  0 if (alseq == null)
861    {
862  0 alseq = new AlignmentSequence();
863  0 up2doc = true;
864    }
865    else
866    {
867  0 if (unbounddocseq != null)
868    {
869  0 unbounddocseq.removeElement(alseq);
870    }
871    }
872    // boolean locked = (alignment.getModifiable()==null ||
873    // alignment.getModifiable().length()>0);
874    // TODO: VAMSAS: translate lowercase symbols to annotation ?
875  0 if (up2doc || !alseq.getSequence().equals(jvalsq.getSequenceAsString()))
876    {
877  0 alseq.setSequence(jvalsq.getSequenceAsString());
878  0 alseq.setStart(jvalsq.getStart());
879  0 alseq.setEnd(jvalsq.getEnd());
880  0 modal = true;
881    }
882  0 if (up2doc || !alseq.getName().equals(jvalsq.getName()))
883    {
884  0 modal = true;
885  0 alseq.setName(jvalsq.getName());
886    }
887  0 if (jvalsq.getDescription() != null && (alseq.getDescription() == null
888    || !jvalsq.getDescription().equals(alseq.getDescription())))
889    {
890  0 modal = true;
891  0 alseq.setDescription(jvalsq.getDescription());
892    }
893  0 if (getjv2vObj(jvalsq.getDatasetSequence()) == null)
894    {
895  0 Console.warn(
896    "Serious Implementation error - Unbound dataset sequence in alignment: "
897    + jvalsq.getDatasetSequence());
898    }
899  0 alseq.setRefid(getjv2vObj(jvalsq.getDatasetSequence()));
900  0 if (up2doc)
901    {
902   
903  0 alignment.addAlignmentSequence(alseq);
904  0 bindjvvobj(jvalsq, alseq);
905    }
906  0 return up2doc || modal;
907    }
908   
909    /**
910    * locally sync a jalview alignment seuqence from a vamsas alignment assumes
911    * all lock transformation/bindings have been sorted out before hand.
912    * creates/syncs the jvalsq from the alignment sequence
913    */
 
914  0 toggle private boolean syncFromAlignmentSequence(AlignmentSequence valseq,
915    char valGapchar, char gapChar, List<SequenceI> dsseqs)
916   
917    {
918  0 boolean modal = false;
919    // todo: islocked method here
920  0 boolean upFromdoc = false;
921  0 jalview.datamodel.SequenceI alseq = (SequenceI) getvObj2jv(valseq);
922  0 if (alseq == null)
923    {
924  0 upFromdoc = true;
925    }
926  0 if (alseq != null)
927    {
928   
929    // boolean locked = (alignment.getModifiable()==null ||
930    // alignment.getModifiable().length()>0);
931    // TODO: VAMSAS: translate lowercase symbols to annotation ?
932  0 if (upFromdoc
933    || !valseq.getSequence().equals(alseq.getSequenceAsString()))
934    {
935    // this might go *horribly* wrong
936  0 alseq.setSequence(new String(valseq.getSequence())
937    .replace(valGapchar, gapChar));
938  0 alseq.setStart((int) valseq.getStart());
939  0 alseq.setEnd((int) valseq.getEnd());
940  0 modal = true;
941    }
942  0 if (!valseq.getName().equals(alseq.getName()))
943    {
944  0 modal = true;
945  0 alseq.setName(valseq.getName());
946    }
947  0 if (alseq.getDescription() == null || (valseq.getDescription() != null
948    && !alseq.getDescription().equals(valseq.getDescription())))
949    {
950  0 alseq.setDescription(valseq.getDescription());
951  0 modal = true;
952    }
953  0 if (modal && Console.isDebugEnabled())
954    {
955  0 Console.debug(
956    "Updating apparently edited sequence " + alseq.getName());
957    }
958    }
959    else
960    {
961  0 alseq = new jalview.datamodel.Sequence(valseq.getName(),
962    valseq.getSequence().replace(valGapchar, gapChar),
963    (int) valseq.getStart(), (int) valseq.getEnd());
964   
965  0 Vobject datsetseq = (Vobject) valseq.getRefid();
966  0 if (datsetseq != null)
967    {
968  0 alseq.setDatasetSequence((SequenceI) getvObj2jv(datsetseq)); // exceptions
969  0 if (valseq.getDescription() != null)
970    {
971  0 alseq.setDescription(valseq.getDescription());
972    }
973    else
974    {
975    // inherit description line from dataset.
976  0 if (alseq.getDatasetSequence().getDescription() != null)
977    {
978  0 alseq.setDescription(
979    alseq.getDatasetSequence().getDescription());
980    }
981    }
982    // if
983    // AlignemntSequence
984    // reference
985    // isn't
986    // a
987    // simple
988    // SequenceI
989    }
990    else
991    {
992  0 Console.error(
993    "Invalid dataset sequence id (null) for alignment sequence "
994    + valseq.getVorbaId());
995    }
996  0 bindjvvobj(alseq, valseq);
997  0 alseq.setVamsasId(valseq.getVorbaId().getId());
998  0 dsseqs.add(alseq);
999    }
1000  0 Vobject datsetseq = (Vobject) valseq.getRefid();
1001  0 if (datsetseq != null)
1002    {
1003  0 if (datsetseq != alseq.getDatasetSequence())
1004    {
1005  0 modal = true;
1006    }
1007  0 alseq.setDatasetSequence((SequenceI) getvObj2jv(datsetseq)); // exceptions
1008    }
1009  0 return upFromdoc || modal;
1010    }
1011   
 
1012  0 toggle private void initRangeAnnotationType(RangeAnnotation an,
1013    AlignmentAnnotation alan, int[] gapMap)
1014    {
1015  0 Seg vSeg = new Seg();
1016  0 vSeg.setStart(1);
1017  0 vSeg.setInclusive(true);
1018  0 vSeg.setEnd(gapMap.length);
1019  0 an.addSeg(vSeg);
1020   
1021    // LATER: much of this is verbatim from the alignmentAnnotation
1022    // method below. suggests refactoring to make rangeAnnotation the
1023    // base class
1024  0 an.setDescription(alan.description);
1025  0 an.setLabel(alan.label);
1026  0 an.setGroup(Integer.toString(alan.graphGroup));
1027    // // JBPNote -
1028    // originally we
1029    // were going to
1030    // store
1031    // graphGroup in
1032    // the Jalview
1033    // specific
1034    // bits.
1035  0 AnnotationElement ae;
1036  0 for (int a = 0; a < alan.annotations.length; a++)
1037    {
1038  0 if (alan.annotations[a] == null)
1039    {
1040  0 continue;
1041    }
1042   
1043  0 ae = new AnnotationElement();
1044  0 ae.setDescription(alan.annotations[a].description);
1045  0 ae.addGlyph(new Glyph());
1046  0 ae.getGlyph(0).setContent(alan.annotations[a].displayCharacter); // assume
1047    // jax-b
1048    // takes
1049    // care
1050    // of
1051    // utf8
1052    // translation
1053  0 if (alan.graph != jalview.datamodel.AlignmentAnnotation.NO_GRAPH)
1054    {
1055  0 ae.addValue(alan.annotations[a].value);
1056    }
1057  0 ae.setPosition(gapMap[a] + 1); // position w.r.t. AlignmentSequence
1058    // symbols
1059  0 if (alan.annotations[a].secondaryStructure != ' ')
1060    {
1061    // we only write an annotation where it really exists.
1062  0 Glyph ss = new Glyph();
1063  0 ss.setDict(
1064    uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE);
1065  0 ss.setContent(
1066    String.valueOf(alan.annotations[a].secondaryStructure));
1067  0 ae.addGlyph(ss);
1068    }
1069  0 an.addAnnotationElement(ae);
1070    }
1071   
1072    }
1073   
 
1074  0 toggle private void saveDatasetSequenceAnnotation(HashMap AlSeqMaps,
1075    uk.ac.vamsas.objects.core.Sequence sref, AlignmentAnnotation alan)
1076    {
1077    // {
1078    // uk.ac.vamsas.
1079    // objects.core.AlignmentSequence alsref = (uk.ac.vamsas.
1080    // objects.core.AlignmentSequence) sref;
1081  0 uk.ac.vamsas.objects.core.DataSetAnnotations an = (uk.ac.vamsas.objects.core.DataSetAnnotations) getjv2vObj(
1082    alan);
1083  0 int[] gapMap = getGapMap(AlSeqMaps, alan);
1084  0 if (an == null)
1085    {
1086  0 an = new uk.ac.vamsas.objects.core.DataSetAnnotations();
1087  0 initRangeAnnotationType(an, alan, gapMap);
1088   
1089  0 an.setProvenance(dummyProvenance()); // get provenance as user
1090    // created, or jnet, or
1091    // something else.
1092  0 setAnnotationType(an, alan);
1093  0 an.setGroup(Integer.toString(alan.graphGroup)); // // JBPNote -
1094    // originally we
1095    // were going to
1096    // store
1097    // graphGroup in
1098    // the Jalview
1099    // specific
1100    // bits.
1101  0 if (alan.getThreshold() != null && alan.getThreshold().displayed)
1102    {
1103  0 an.addProperty(Properties.newProperty(THRESHOLD,
1104    Properties.FLOATTYPE, "" + alan.getThreshold().value));
1105  0 if (alan.getThreshold().label != null)
1106    {
1107  0 an.addProperty(Properties.newProperty(THRESHOLD + "Name",
1108    Properties.STRINGTYPE, "" + alan.getThreshold().label));
1109    }
1110    }
1111  0 ((DataSet) sref.getV_parent()).addDataSetAnnotations(an);
1112  0 bindjvvobj(alan, an);
1113    }
1114    else
1115    {
1116    // update reference sequence Annotation
1117  0 if (an.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT LOCK
1118    // METHODS)
1119    {
1120    // verify existing alignment sequence annotation is up to date
1121  0 jalview.bin.Console
1122    .outPrintln("update dataset sequence annotation.");
1123    }
1124    else
1125    {
1126    // verify existing alignment sequence annotation is up to date
1127  0 jalview.bin.Console.outPrintln(
1128    "make new alignment dataset sequence annotation if modification has happened.");
1129    }
1130    }
1131   
1132    }
1133   
 
1134  0 toggle private int[] getGapMap(HashMap AlSeqMaps, AlignmentAnnotation alan)
1135    {
1136  0 int[] gapMap;
1137  0 if (AlSeqMaps.containsKey(alan.sequenceRef))
1138    {
1139  0 gapMap = (int[]) AlSeqMaps.get(alan.sequenceRef);
1140    }
1141    else
1142    {
1143  0 gapMap = new int[alan.sequenceRef.getLength()];
1144    // map from alignment position to sequence position.
1145  0 int[] sgapMap = alan.sequenceRef.gapMap();
1146  0 for (int a = 0; a < sgapMap.length; a++)
1147    {
1148  0 gapMap[sgapMap[a]] = a;
1149    }
1150    }
1151  0 return gapMap;
1152    }
1153   
 
1154  0 toggle private void saveAlignmentSequenceAnnotation(HashMap AlSeqMaps,
1155    AlignmentSequence alsref, AlignmentAnnotation alan)
1156    {
1157    // {
1158    // uk.ac.vamsas.
1159    // objects.core.AlignmentSequence alsref = (uk.ac.vamsas.
1160    // objects.core.AlignmentSequence) sref;
1161  0 uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation an = (uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation) getjv2vObj(
1162    alan);
1163  0 int[] gapMap = getGapMap(AlSeqMaps, alan);
1164  0 if (an == null)
1165    {
1166  0 an = new uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation();
1167  0 initRangeAnnotationType(an, alan, gapMap);
1168    /**
1169    * I mean here that we don't actually have a semantic 'type' for the
1170    * annotation (this might be - score, intrinsic property, measurement,
1171    * something extracted from another program, etc)
1172    */
1173  0 an.setType(JALVIEW_ANNOTATION_ROW); // TODO: better fix
1174    // this rough guess ;)
1175  0 alsref.addAlignmentSequenceAnnotation(an);
1176  0 bindjvvobj(alan, an);
1177    // These properties are directly supported by the
1178    // AlignmentSequenceAnnotation type.
1179  0 setAnnotationType(an, alan);
1180  0 an.setProvenance(dummyProvenance()); // get provenance as user
1181    // created, or jnet, or
1182    // something else.
1183    }
1184    else
1185    {
1186    // update reference sequence Annotation
1187  0 if (an.getModifiable() == null) // TODO: USE VAMSAS LIBRARY OBJECT LOCK
1188    // METHODS)
1189    {
1190    // verify existing alignment sequence annotation is up to date
1191  0 jalview.bin.Console
1192    .outPrintln("update alignment sequence annotation.");
1193    }
1194    else
1195    {
1196    // verify existing alignment sequence annotation is up to date
1197  0 jalview.bin.Console.outPrintln(
1198    "make new alignment sequence annotation if modification has happened.");
1199    }
1200    }
1201    }
1202   
1203    /**
1204    * set vamsas annotation object type from jalview annotation
1205    *
1206    * @param an
1207    * @param alan
1208    */
 
1209  0 toggle private void setAnnotationType(RangeAnnotation an,
1210    AlignmentAnnotation alan)
1211    {
1212  0 if (an instanceof AlignmentSequenceAnnotation)
1213    {
1214  0 if (alan.graph != AlignmentAnnotation.NO_GRAPH)
1215    {
1216  0 ((AlignmentSequenceAnnotation) an).setGraph(true);
1217    }
1218    else
1219    {
1220  0 ((AlignmentSequenceAnnotation) an).setGraph(false);
1221    }
1222    }
1223  0 if (an instanceof uk.ac.vamsas.objects.core.AlignmentAnnotation)
1224    {
1225  0 if (alan.graph != AlignmentAnnotation.NO_GRAPH)
1226    {
1227  0 ((uk.ac.vamsas.objects.core.AlignmentAnnotation) an).setGraph(true);
1228    }
1229    else
1230    {
1231  0 ((uk.ac.vamsas.objects.core.AlignmentAnnotation) an)
1232    .setGraph(false);
1233    }
1234    }
1235  0 switch (alan.graph)
1236    {
1237  0 case AlignmentAnnotation.BAR_GRAPH:
1238  0 an.addProperty(Properties.newProperty(DISCRETE_ANNOTATION,
1239    Properties.BOOLEANTYPE, "true"));
1240  0 break;
1241  0 case AlignmentAnnotation.LINE_GRAPH:
1242  0 an.addProperty(Properties.newProperty(CONTINUOUS_ANNOTATION,
1243    Properties.BOOLEANTYPE, "true"));
1244  0 break;
1245  0 default:
1246    // don't add any kind of discrete or continous property info.
1247    }
1248    }
1249   
1250    /**
1251    * get start<end range of segment, adjusting for inclusivity flag and
1252    * polarity.
1253    *
1254    * @param visSeg
1255    * @param ensureDirection
1256    * when true - always ensure start is less than end.
1257    * @return int[] { start, end, direction} where direction==1 for range running
1258    * from end to start.
1259    */
 
1260  0 toggle private int[] getSegRange(Seg visSeg, boolean ensureDirection)
1261    {
1262  0 boolean incl = visSeg.getInclusive();
1263    // adjust for inclusive flag.
1264  0 int pol = (visSeg.getStart() <= visSeg.getEnd()) ? 1 : -1; // polarity of
1265    // region.
1266  0 int start = visSeg.getStart() + (incl ? 0 : pol);
1267  0 int end = visSeg.getEnd() + (incl ? 0 : -pol);
1268  0 if (ensureDirection && pol == -1)
1269    {
1270    // jalview doesn't deal with inverted ranges, yet.
1271  0 int t = end;
1272  0 end = start;
1273  0 start = t;
1274    }
1275  0 return new int[] { start, end, pol < 0 ? 1 : 0 };
1276    }
1277   
1278    /**
1279    *
1280    * @param annotation
1281    * @return true if annotation is not to be stored in document
1282    */
 
1283  0 toggle private boolean isJalviewOnly(AlignmentAnnotation annotation)
1284    {
1285  0 return annotation.autoCalculated || annotation.label.equals("Quality")
1286    || annotation.label.equals("Conservation")
1287    || annotation.label.equals("Consensus");
1288    }
1289   
1290    boolean dojvsync = true;
1291   
1292    // boolean dojvsync = false; // disables Jalview AppData IO
1293    /**
1294    * list of alignment views created when updating Jalview from document.
1295    */
1296    private final Vector newAlignmentViews = new Vector();
1297   
1298    /**
1299    * update local jalview view settings from the stored appdata (if any)
1300    */
 
1301  0 toggle public void updateJalviewFromAppdata()
1302    {
1303    // recover any existing Jalview data from appdata
1304    // TODO: recover any PDB files stored as attachments in the vamsas session
1305    // and initialise the Jalview2XML.alreadyLoadedPDB hashtable with mappings
1306    // to temp files.
1307    {
1308  0 final IClientAppdata cappdata = cdoc.getClientAppdata();
1309  0 if (cappdata != null)
1310    {
1311  0 if (cappdata.hasClientAppdata())
1312    {
1313    // TODO: how to check version of Jalview client app data and whether
1314    // it has been modified
1315    // client data is shared over all app clients
1316  0 try
1317    {
1318    // jalview.gui.Jalview2XML fromxml = new jalview.gui.Jalview2XML();
1319  0 Jalview2XML fromxml = new Jalview2XML();
1320  0 fromxml.attemptversion1parse = false;
1321  0 fromxml.setUniqueSetSuffix("");
1322  0 fromxml.setObjectMappingTables(vobj2jv, jv2vobj); // mapKeysToString
1323    // and
1324    // mapValuesToString
1325  0 fromxml.setSkipList(skipList);
1326  0 jarInputStreamProvider jprovider = new jarInputStreamProvider()
1327    {
1328   
 
1329  0 toggle @Override
1330    public String getFilename()
1331    {
1332   
1333    // TODO Get the vamsas session ID here
1334  0 return "Jalview Vamsas Document Client Data";
1335    }
1336   
 
1337  0 toggle @Override
1338    public JarInputStream getJarInputStream() throws IOException
1339    {
1340  0 Console.debug(
1341    "Returning client input stream for Jalview from Vamsas Document.");
1342  0 return new JarInputStream(cappdata.getClientInputStream());
1343    }
1344   
 
1345  0 toggle @Override
1346    public File getFile()
1347    {
1348  0 return null;
1349    }
1350    };
1351  0 if (dojvsync)
1352    {
1353  0 fromxml.loadJalviewAlign(jprovider);
1354    }
1355    } catch (Exception e)
1356    {
1357   
1358    } catch (OutOfMemoryError e)
1359    {
1360   
1361    } catch (Error e)
1362    {
1363   
1364    }
1365    }
1366    }
1367  0 if (cappdata.hasUserAppdata())
1368    {
1369    // TODO: how to check version of Jalview user app data and whether it
1370    // has been modified
1371    // user data overrides data shared over all app clients ?
1372  0 try
1373    {
1374  0 Jalview2XML fromxml = new Jalview2XML();
1375  0 fromxml.attemptversion1parse = false;
1376  0 fromxml.setUniqueSetSuffix("");
1377  0 fromxml.setSkipList(skipList);
1378  0 fromxml.setObjectMappingTables(mapKeysToString(vobj2jv),
1379    mapValuesToString(jv2vobj));
1380  0 jarInputStreamProvider jarstream = new jarInputStreamProvider()
1381    {
1382   
 
1383  0 toggle @Override
1384    public String getFilename()
1385    {
1386   
1387    // TODO Get the vamsas session ID here
1388  0 return "Jalview Vamsas Document User Data";
1389    }
1390   
 
1391  0 toggle @Override
1392    public JarInputStream getJarInputStream() throws IOException
1393    {
1394  0 Console.debug(
1395    "Returning user input stream for Jalview from Vamsas Document.");
1396  0 return new JarInputStream(cappdata.getUserInputStream());
1397    }
1398   
 
1399  0 toggle @Override
1400    public File getFile()
1401    {
1402  0 return null;
1403    }
1404    };
1405  0 if (dojvsync)
1406    {
1407  0 fromxml.loadJalviewAlign(jarstream);
1408    }
1409    } catch (Exception e)
1410    {
1411   
1412    } catch (OutOfMemoryError e)
1413    {
1414   
1415    } catch (Error e)
1416    {
1417   
1418    }
1419    }
1420   
1421    }
1422  0 flushAlignViewports();
1423    }
1424   
1425    /**
1426    * remove any spurious views generated by document synchronization
1427    */
 
1428  0 toggle private void flushAlignViewports()
1429    {
1430    // remove any additional viewports originally recovered from the vamsas
1431    // document.
1432    // search for all alignframes containing viewports generated from document
1433    // sync,
1434    // and if any contain more than one view, then remove the one generated by
1435    // document update.
1436  0 AlignmentViewport views[], av = null;
1437  0 AlignFrame af = null;
1438  0 Iterator newviews = newAlignmentViews.iterator();
1439  0 while (newviews.hasNext())
1440    {
1441  0 av = (AlignmentViewport) newviews.next();
1442  0 af = Desktop.getAlignFrameFor(av);
1443    // TODO implement this : af.getNumberOfViews
1444  0 String seqsetidobj = av.getSequenceSetId();
1445  0 views = Desktop.getViewports(seqsetidobj);
1446  0 Console.debug("Found " + (views == null ? " no " : "" + views.length)
1447    + " views for '" + av.getSequenceSetId() + "'");
1448  0 if (views.length > 1)
1449    {
1450    // we need to close the original document view.
1451   
1452    // work out how to do this by seeing if the views are gathered.
1453    // pretty clunky but the only way to do this without adding more flags
1454    // to the align frames.
1455  0 boolean gathered = false;
1456  0 String newviewid = null;
1457  0 List<AlignedCodonFrame> mappings = av.getAlignment()
1458    .getCodonFrames();
1459  0 for (int i = 0; i < views.length; i++)
1460    {
1461  0 if (views[i] != av)
1462    {
1463  0 AlignFrame viewframe = Desktop.getAlignFrameFor(views[i]);
1464  0 if (viewframe == af)
1465    {
1466  0 gathered = true;
1467    }
1468  0 newviewid = views[i].getSequenceSetId();
1469    }
1470    else
1471    {
1472    // lose the reference to the vamsas document created view
1473  0 views[i] = null;
1474    }
1475    }
1476    // close the view generated by the vamsas document synchronization
1477  0 if (gathered)
1478    {
1479  0 af.closeView(av);
1480    }
1481    else
1482    {
1483  0 af.closeMenuItem_actionPerformed(false);
1484    }
1485  0 replaceJvObjMapping(seqsetidobj, newviewid);
1486  0 seqsetidobj = newviewid;
1487    // not sure if we need to do this:
1488   
1489  0 if (false) // mappings != null)
1490    {
1491    // ensure sequence mappings from vamsas document view still
1492    // active
1493  0 if (mappings != null)
1494    {
1495  0 jalview.structure.StructureSelectionManager
1496    .getStructureSelectionManager(Desktop.getInstance())
1497    .registerMappings(mappings);
1498    }
1499    }
1500    }
1501    // ensure vamsas object binds to the stored views retrieved from
1502    // Jalview appdata
1503    // jalview.structure.StructureSelectionManager
1504    // .getStructureSelectionManager()
1505    // .addStructureViewerListener(viewframe.alignPanel);
1506   
1507    }
1508   
1509  0 newviews = null;
1510  0 newAlignmentViews.clear();
1511    }
1512   
1513    /**
1514    * replaces oldjvobject with newjvobject in the Jalview Object <> VorbaID
1515    * binding tables
1516    *
1517    * @param oldjvobject
1518    * @param newjvobject
1519    * (may be null)
1520    */
 
1521  0 toggle private void replaceJvObjMapping(Object oldjvobject, Object newjvobject)
1522    {
1523  0 Object vobject = jv2vobj.remove(oldjvobject);
1524  0 if (vobject == null)
1525    {
1526    // NOTE: this happens if user deletes object in one session then updates
1527    // from another client
1528  0 throw new Error(MessageManager.formatMessage(
1529    "error.implementation_error_old_jalview_object_not_bound",
1530    new String[]
1531    { oldjvobject.toString() }));
1532    }
1533  0 if (newjvobject != null)
1534    {
1535  0 jv2vobj.put(newjvobject, vobject);
1536  0 vobj2jv.put(vobject, newjvobject);
1537    }
1538    }
1539   
1540    /**
1541    * Update the jalview client and user appdata from the local jalview settings
1542    */
 
1543  0 toggle public void updateJalviewClientAppdata()
1544    {
1545  0 final IClientAppdata cappdata = cdoc.getClientAppdata();
1546  0 if (cappdata != null)
1547    {
1548  0 try
1549    {
1550  0 Jalview2XML jxml = new Jalview2XML();
1551  0 jxml.setObjectMappingTables(mapKeysToString(vobj2jv),
1552    mapValuesToString(jv2vobj));
1553  0 jxml.setSkipList(skipList);
1554  0 if (dojvsync)
1555    {
1556  0 jxml.saveState(
1557    new JarOutputStream(cappdata.getClientOutputStream()));
1558    }
1559   
1560    } catch (Exception e)
1561    {
1562    // TODO raise GUI warning if user requests it.
1563  0 Console.error(
1564    "Couldn't update jalview client application data. Giving up - local settings probably lost.",
1565    e);
1566    }
1567    }
1568    else
1569    {
1570  0 Console.error(
1571    "Couldn't access client application data for vamsas session. This is probably a vamsas client bug.");
1572    }
1573    }
1574   
1575    /**
1576    * translate the Vobject keys to strings for use in Jalview2XML
1577    *
1578    * @param jv2vobj2
1579    * @return
1580    */
 
1581  0 toggle private IdentityHashMap mapValuesToString(IdentityHashMap jv2vobj2)
1582    {
1583  0 IdentityHashMap mapped = new IdentityHashMap();
1584  0 Iterator keys = jv2vobj2.keySet().iterator();
1585  0 while (keys.hasNext())
1586    {
1587  0 Object key = keys.next();
1588  0 mapped.put(key, jv2vobj2.get(key).toString());
1589    }
1590  0 return mapped;
1591    }
1592   
1593    /**
1594    * translate the Vobject values to strings for use in Jalview2XML
1595    *
1596    * @param vobj2jv2
1597    * @return hashtable with string values
1598    */
 
1599  0 toggle private Hashtable mapKeysToString(Hashtable vobj2jv2)
1600    {
1601  0 Hashtable mapped = new Hashtable();
1602  0 Iterator keys = vobj2jv2.keySet().iterator();
1603  0 while (keys.hasNext())
1604    {
1605  0 Object key = keys.next();
1606  0 mapped.put(key.toString(), vobj2jv2.get(key));
1607    }
1608  0 return mapped;
1609    }
1610   
1611    /**
1612    * synchronize Jalview from the vamsas document
1613    *
1614    * @return number of new views from document
1615    */
 
1616  0 toggle public int updateToJalview()
1617    {
1618  0 VAMSAS _roots[] = cdoc.getVamsasRoots();
1619   
1620  0 for (int _root = 0; _root < _roots.length; _root++)
1621    {
1622  0 VAMSAS root = _roots[_root];
1623  0 boolean newds = false;
1624  0 for (int _ds = 0, _nds = root.getDataSetCount(); _ds < _nds; _ds++)
1625    {
1626    // ///////////////////////////////////
1627    // ///LOAD DATASET
1628  0 DataSet dataset = root.getDataSet(_ds);
1629  0 int i, iSize = dataset.getSequenceCount();
1630  0 List<SequenceI> dsseqs;
1631  0 jalview.datamodel.Alignment jdataset = (jalview.datamodel.Alignment) getvObj2jv(
1632    dataset);
1633  0 int jremain = 0;
1634  0 if (jdataset == null)
1635    {
1636  0 Console.debug("Initialising new jalview dataset fields");
1637  0 newds = true;
1638  0 dsseqs = new Vector();
1639    }
1640    else
1641    {
1642  0 Console.debug("Update jalview dataset from vamsas.");
1643  0 jremain = jdataset.getHeight();
1644  0 dsseqs = jdataset.getSequences();
1645    }
1646   
1647    // TODO: test sequence merging - we preserve existing non vamsas
1648    // sequences but add in any new vamsas ones, and don't yet update any
1649    // sequence attributes
1650  0 for (i = 0; i < iSize; i++)
1651    {
1652  0 Sequence vdseq = dataset.getSequence(i);
1653  0 jalview.io.vamsas.Datasetsequence dssync = new Datasetsequence(
1654    this, vdseq);
1655   
1656  0 jalview.datamodel.SequenceI dsseq = (SequenceI) dssync.getJvobj();
1657  0 if (dssync.isAddfromdoc())
1658    {
1659  0 dsseqs.add(dsseq);
1660    }
1661  0 if (vdseq.getDbRefCount() > 0)
1662    {
1663  0 DbRef[] dbref = vdseq.getDbRef();
1664  0 for (int db = 0; db < dbref.length; db++)
1665    {
1666  0 new jalview.io.vamsas.Dbref(this, dbref[db], vdseq, dsseq);
1667   
1668    }
1669  0 dsseq.updatePDBIds();
1670    }
1671    }
1672   
1673  0 if (newds)
1674    {
1675  0 SequenceI[] seqs = new SequenceI[dsseqs.size()];
1676  0 for (i = 0, iSize = dsseqs.size(); i < iSize; i++)
1677    {
1678  0 seqs[i] = dsseqs.get(i);
1679  0 dsseqs.set(i, null);
1680    }
1681  0 jdataset = new jalview.datamodel.Alignment(seqs);
1682  0 Console.debug("New vamsas dataset imported into jalview.");
1683  0 bindjvvobj(jdataset, dataset);
1684    }
1685    // ////////
1686    // add any new dataset sequence feature annotations
1687  0 if (dataset.getDataSetAnnotations() != null)
1688    {
1689  0 for (int dsa = 0; dsa < dataset
1690    .getDataSetAnnotationsCount(); dsa++)
1691    {
1692  0 DataSetAnnotations dseta = dataset.getDataSetAnnotations(dsa);
1693    // TODO: deal with group annotation on datset sequences.
1694  0 if (dseta.getSeqRefCount() == 1)
1695    {
1696  0 SequenceI dsSeq = (SequenceI) getvObj2jv(
1697    (Vobject) dseta.getSeqRef(0)); // TODO: deal with group
1698    // dataset
1699    // annotations
1700  0 if (dsSeq == null)
1701    {
1702  0 Console.warn(
1703    "Couldn't resolve jalview sequenceI for dataset object reference "
1704    + ((Vobject) dataset
1705    .getDataSetAnnotations(dsa)
1706    .getSeqRef(0)).getVorbaId()
1707    .getId());
1708    }
1709    else
1710    {
1711  0 if (dseta.getAnnotationElementCount() == 0)
1712    {
1713  0 new jalview.io.vamsas.Sequencefeature(this, dseta, dsSeq);
1714   
1715    }
1716    else
1717    {
1718    // TODO: deal with alignmentAnnotation style annotation
1719    // appearing on dataset sequences.
1720    // JBPNote: we could just add them to all alignments but
1721    // that may complicate cross references in the jalview
1722    // datamodel
1723  0 Console.warn(
1724    "Ignoring dataset annotation with annotationElements. Not yet supported in jalview.");
1725    }
1726    }
1727    }
1728    else
1729    {
1730  0 Console.warn(
1731    "Ignoring multiply referenced dataset sequence annotation for binding to datsaet sequence features.");
1732    }
1733    }
1734    }
1735  0 if (dataset.getAlignmentCount() > 0)
1736    {
1737    // LOAD ALIGNMENTS from DATASET
1738   
1739  0 for (int al = 0, nal = dataset
1740  0 .getAlignmentCount(); al < nal; al++)
1741    {
1742  0 uk.ac.vamsas.objects.core.Alignment alignment = dataset
1743    .getAlignment(al);
1744    // TODO check this handles multiple views properly
1745  0 AlignmentViewport av = findViewport(alignment);
1746   
1747  0 jalview.datamodel.AlignmentI jal = null;
1748  0 if (av != null)
1749    {
1750    // TODO check that correct alignment object is retrieved when
1751    // hidden seqs exist.
1752  0 jal = (av.hasHiddenRows()) ? av.getAlignment()
1753    .getHiddenSequences().getFullAlignment()
1754    : av.getAlignment();
1755    }
1756  0 iSize = alignment.getAlignmentSequenceCount();
1757  0 boolean refreshal = false;
1758  0 Vector newasAnnots = new Vector();
1759  0 char gapChar = ' '; // default for new alignments read in from the
1760    // document
1761  0 if (jal != null)
1762    {
1763  0 dsseqs = jal.getSequences(); // for merge/update
1764  0 gapChar = jal.getGapCharacter();
1765    }
1766    else
1767    {
1768  0 dsseqs = new Vector();
1769    }
1770  0 char valGapchar = alignment.getGapChar().charAt(0);
1771  0 for (i = 0; i < iSize; i++)
1772    {
1773  0 AlignmentSequence valseq = alignment.getAlignmentSequence(i);
1774  0 jalview.datamodel.Sequence alseq = (jalview.datamodel.Sequence) getvObj2jv(
1775    valseq);
1776  0 if (syncFromAlignmentSequence(valseq, valGapchar, gapChar,
1777    dsseqs) && alseq != null)
1778    {
1779   
1780    // updated to sequence from the document
1781  0 jremain--;
1782  0 refreshal = true;
1783    }
1784  0 if (valseq.getAlignmentSequenceAnnotationCount() > 0)
1785    {
1786  0 AlignmentSequenceAnnotation[] vasannot = valseq
1787    .getAlignmentSequenceAnnotation();
1788  0 for (int a = 0; a < vasannot.length; a++)
1789    {
1790  0 jalview.datamodel.AlignmentAnnotation asa = (jalview.datamodel.AlignmentAnnotation) getvObj2jv(
1791    vasannot[a]); // TODO:
1792    // 1:many
1793    // jalview
1794    // alignment
1795    // sequence
1796    // annotations
1797  0 if (asa == null)
1798    {
1799  0 int se[] = getBounds(vasannot[a]);
1800  0 asa = getjAlignmentAnnotation(jal, vasannot[a]);
1801  0 asa.setSequenceRef(alseq);
1802  0 asa.createSequenceMapping(alseq, se[0], false); // TODO:
1803    // verify
1804    // that
1805    // positions
1806    // in
1807    // alseqAnnotation
1808    // correspond
1809    // to
1810    // ungapped
1811    // residue
1812    // positions.
1813  0 alseq.addAlignmentAnnotation(asa);
1814  0 bindjvvobj(asa, vasannot[a]);
1815  0 refreshal = true;
1816  0 newasAnnots.add(asa);
1817    }
1818    else
1819    {
1820    // update existing annotation - can do this in place
1821  0 if (vasannot[a].getModifiable() == null) // TODO: USE
1822    // VAMSAS LIBRARY
1823    // OBJECT LOCK
1824    // METHODS)
1825    {
1826  0 Console.info(
1827    "UNIMPLEMENTED: not recovering user modifiable sequence alignment annotation");
1828    // TODO: should at least replace with new one - otherwise
1829    // things will break
1830    // basically do this:
1831    // int se[] = getBounds(vasannot[a]);
1832    // asa.update(getjAlignmentAnnotation(jal, vasannot[a]));
1833    // // update from another annotation object in place.
1834    // asa.createSequenceMapping(alseq, se[0], false);
1835   
1836    }
1837    }
1838    }
1839    }
1840    }
1841  0 if (jal == null)
1842    {
1843  0 SequenceI[] seqs = new SequenceI[dsseqs.size()];
1844  0 for (i = 0, iSize = dsseqs.size(); i < iSize; i++)
1845    {
1846  0 seqs[i] = dsseqs.get(i);
1847  0 dsseqs.set(i, null);
1848    }
1849  0 jal = new jalview.datamodel.Alignment(seqs);
1850  0 Console.debug("New vamsas alignment imported into jalview "
1851    + alignment.getVorbaId().getId());
1852  0 jal.setDataset(jdataset);
1853    }
1854  0 if (newasAnnots != null && newasAnnots.size() > 0)
1855    {
1856    // Add the new sequence annotations in to the alignment.
1857  0 for (int an = 0, anSize = newasAnnots
1858  0 .size(); an < anSize; an++)
1859    {
1860  0 jal.addAnnotation(
1861    (AlignmentAnnotation) newasAnnots.elementAt(an));
1862    // TODO: check if anything has to be done - like calling
1863    // adjustForAlignment or something.
1864  0 newasAnnots.setElementAt(null, an);
1865    }
1866  0 newasAnnots = null;
1867    }
1868    // //////////////////////////////////////////
1869    // //LOAD ANNOTATIONS FOR THE ALIGNMENT
1870    // ////////////////////////////////////
1871  0 if (alignment.getAlignmentAnnotationCount() > 0)
1872    {
1873  0 uk.ac.vamsas.objects.core.AlignmentAnnotation[] an = alignment
1874    .getAlignmentAnnotation();
1875   
1876  0 for (int j = 0; j < an.length; j++)
1877    {
1878  0 jalview.datamodel.AlignmentAnnotation jan = (jalview.datamodel.AlignmentAnnotation) getvObj2jv(
1879    an[j]);
1880  0 if (jan != null)
1881    {
1882    // update or stay the same.
1883    // TODO: should at least replace with a new one - otherwise
1884    // things will break
1885    // basically do this:
1886    // jan.update(getjAlignmentAnnotation(jal, an[a])); // update
1887    // from another annotation object in place.
1888   
1889  0 Console.debug(
1890    "update from vamsas alignment annotation to existing jalview alignment annotation.");
1891  0 if (an[j].getModifiable() == null) // TODO: USE VAMSAS
1892    // LIBRARY OBJECT LOCK
1893    // METHODS)
1894    {
1895    // TODO: user defined annotation is totally mutable... - so
1896    // load it up or throw away if locally edited.
1897  0 Console.info(
1898    "NOT IMPLEMENTED - Recovering user-modifiable annotation - yet...");
1899    }
1900    // TODO: compare annotation element rows
1901    // TODO: compare props.
1902    }
1903    else
1904    {
1905  0 jan = getjAlignmentAnnotation(jal, an[j]);
1906    // TODO: ensure we add the alignment annotation before the
1907    // automatic annotation rows
1908  0 jal.addAnnotation(jan);
1909  0 bindjvvobj(jan, an[j]);
1910  0 refreshal = true;
1911    }
1912    }
1913    }
1914  0 AlignFrame alignFrame;
1915  0 if (av == null)
1916    {
1917  0 Console.debug("New alignframe for alignment "
1918    + alignment.getVorbaId());
1919    // ///////////////////////////////
1920    // construct alignment view
1921  0 alignFrame = new AlignFrame(jal, AlignFrame.DEFAULT_WIDTH,
1922    AlignFrame.DEFAULT_HEIGHT,
1923    alignment.getVorbaId().toString());
1924  0 av = alignFrame.getViewport();
1925  0 newAlignmentViews.addElement(av);
1926  0 String title = alignment.getProvenance()
1927    .getEntry(
1928    alignment.getProvenance().getEntryCount() - 1)
1929    .getAction();
1930  0 if (alignment.getPropertyCount() > 0)
1931    {
1932  0 for (int p = 0, pe = alignment
1933  0 .getPropertyCount(); p < pe; p++)
1934    {
1935  0 if (alignment.getProperty(p).getName().equals("title"))
1936    {
1937  0 title = alignment.getProperty(p).getContent();
1938    }
1939    }
1940    }
1941    // TODO: automatically create meaningful title for a vamsas
1942    // alignment using its provenance.
1943  0 if (Console.isDebugEnabled())
1944    {
1945  0 title = title + "(" + alignment.getVorbaId() + ")";
1946   
1947    }
1948  0 jalview.gui.Desktop.addInternalFrame(alignFrame, title,
1949    AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
1950  0 bindjvvobj(av.getSequenceSetId(), alignment);
1951    }
1952    else
1953    {
1954    // find the alignFrame for jal.
1955    // TODO: fix this so we retrieve the alignFrame handing av
1956    // *directly* (JBPNote - don't understand this now)
1957    // TODO: make sure all associated views are refreshed
1958  0 alignFrame = Desktop.getAlignFrameFor(av);
1959  0 if (refreshal)
1960    {
1961  0 av.alignmentChanged(alignFrame.alignPanel);
1962  0 alignFrame.alignPanel.adjustAnnotationHeight();
1963    }
1964    }
1965    // LOAD TREES
1966    // /////////////////////////////////////
1967  0 if (alignment.getTreeCount() > 0)
1968    {
1969   
1970  0 for (int t = 0; t < alignment.getTreeCount(); t++)
1971    {
1972  0 jalview.io.vamsas.Tree vstree = new jalview.io.vamsas.Tree(
1973    this, alignFrame, alignment.getTree(t));
1974  0 TreePanel tp = null;
1975  0 if (vstree.isValidTree())
1976    {
1977  0 tp = alignFrame.showNewickTree(vstree.getNewickTree(),
1978    vstree.getTitle(), vstree.getInputData(), 600,
1979    500, t * 20 + 50, t * 20 + 50);
1980   
1981    }
1982  0 if (tp != null)
1983    {
1984  0 bindjvvobj(tp, alignment.getTree(t));
1985  0 try
1986    {
1987  0 vstree.UpdateSequenceTreeMap(tp);
1988    } catch (RuntimeException e)
1989    {
1990  0 Console.warn("update of labels failed.", e);
1991    }
1992    }
1993    else
1994    {
1995  0 Console.warn("Cannot create tree for tree " + t
1996    + " in document ("
1997    + alignment.getTree(t).getVorbaId());
1998    }
1999   
2000    }
2001    }
2002    }
2003    }
2004    }
2005    // we do sequenceMappings last because they span all datasets in a vamsas
2006    // root
2007  0 for (int _ds = 0, _nds = root.getDataSetCount(); _ds < _nds; _ds++)
2008    {
2009  0 DataSet dataset = root.getDataSet(_ds);
2010  0 if (dataset.getSequenceMappingCount() > 0)
2011    {
2012  0 for (int sm = 0, smCount = dataset
2013  0 .getSequenceMappingCount(); sm < smCount; sm++)
2014    {
2015  0 Rangetype seqmap = new jalview.io.vamsas.Sequencemapping(this,
2016    dataset.getSequenceMapping(sm));
2017    }
2018    }
2019    }
2020    }
2021  0 return newAlignmentViews.size();
2022    }
2023   
 
2024  0 toggle public AlignmentViewport findViewport(Alignment alignment)
2025    {
2026  0 AlignmentViewport av = null;
2027  0 AlignmentViewport[] avs = Desktop
2028    .getViewports((String) getvObj2jv(alignment));
2029  0 if (avs != null)
2030    {
2031  0 av = avs[0];
2032    }
2033  0 return av;
2034    }
2035   
2036    // bitfields - should be a template in j1.5
2037    private static int HASSECSTR = 0;
2038   
2039    private static int HASVALS = 1;
2040   
2041    private static int HASHPHOB = 2;
2042   
2043    private static int HASDC = 3;
2044   
2045    private static int HASDESCSTR = 4;
2046   
2047    private static int HASTWOSTATE = 5; // not used yet.
2048   
2049    /**
2050    * parses the AnnotationElements - if they exist - into
2051    * jalview.datamodel.Annotation[] rows Two annotation rows are made if there
2052    * are distinct annotation for both at 'pos' and 'after pos' at any particular
2053    * site.
2054    *
2055    * @param annotation
2056    * @return { boolean[static int constants ], int[ae.length] - map to annotated
2057    * object frame, jalview.datamodel.Annotation[],
2058    * jalview.datamodel.Annotation[] (after)}
2059    */
 
2060  0 toggle private Object[] parseRangeAnnotation(
2061    uk.ac.vamsas.objects.core.RangeAnnotation annotation)
2062    {
2063    // set these attributes by looking in the annotation to decide what kind of
2064    // alignment annotation rows will be made
2065    // TODO: potentially we might make several annotation rows from one vamsas
2066    // alignment annotation. the jv2Vobj binding mechanism
2067    // may not quite cope with this (without binding an array of annotations to
2068    // a vamsas alignment annotation)
2069    // summary flags saying what we found over the set of annotation rows.
2070  0 boolean[] AeContent = new boolean[] { false, false, false, false,
2071    false };
2072  0 int[] rangeMap = getMapping(annotation);
2073  0 jalview.datamodel.Annotation[][] anot = new jalview.datamodel.Annotation[][] {
2074    new jalview.datamodel.Annotation[rangeMap.length],
2075    new jalview.datamodel.Annotation[rangeMap.length] };
2076  0 boolean mergeable = true; // false if 'after positions cant be placed on
2077    // same annotation row as positions.
2078   
2079  0 if (annotation.getAnnotationElementCount() > 0)
2080    {
2081  0 AnnotationElement ae[] = annotation.getAnnotationElement();
2082  0 for (int aa = 0; aa < ae.length; aa++)
2083    {
2084  0 int pos = (int) ae[aa].getPosition() - 1; // pos counts from 1 to
2085    // (|seg.start-seg.end|+1)
2086  0 if (pos >= 0 && pos < rangeMap.length)
2087    {
2088  0 int row = ae[aa].getAfter() ? 1 : 0;
2089  0 if (anot[row][pos] != null)
2090    {
2091    // only time this should happen is if the After flag is set.
2092  0 Console.debug("Ignoring duplicate annotation site at " + pos);
2093  0 continue;
2094    }
2095  0 if (anot[1 - row][pos] != null)
2096    {
2097  0 mergeable = false;
2098    }
2099  0 String desc = "";
2100  0 if (ae[aa].getDescription() != null)
2101    {
2102  0 desc = ae[aa].getDescription();
2103  0 if (desc.length() > 0)
2104    {
2105    // have imported valid description string
2106  0 AeContent[HASDESCSTR] = true;
2107    }
2108    }
2109  0 String dc = null; // ae[aa].getDisplayCharacter()==null ? "dc" :
2110    // ae[aa].getDisplayCharacter();
2111  0 String ss = null; // ae[aa].getSecondaryStructure()==null ? "ss" :
2112    // ae[aa].getSecondaryStructure();
2113  0 java.awt.Color colour = null;
2114  0 if (ae[aa].getGlyphCount() > 0)
2115    {
2116  0 Glyph[] glyphs = ae[aa].getGlyph();
2117  0 for (int g = 0; g < glyphs.length; g++)
2118    {
2119  0 if (glyphs[g].getDict().equals(
2120    uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_SS_3STATE))
2121    {
2122  0 ss = glyphs[g].getContent();
2123  0 AeContent[HASSECSTR] = true;
2124    }
2125  0 else if (glyphs[g].getDict().equals(
2126    uk.ac.vamsas.objects.utils.GlyphDictionary.PROTEIN_HD_HYDRO))
2127    {
2128  0 Console.debug("ignoring hydrophobicity glyph marker.");
2129  0 AeContent[HASHPHOB] = true;
2130  0 char c = (dc = glyphs[g].getContent()).charAt(0);
2131    // dc may get overwritten - but we still set the colour.
2132  0 colour = new java.awt.Color(c == '+' ? 255 : 0,
2133  0 c == '.' ? 255 : 0, c == '-' ? 255 : 0);
2134   
2135    }
2136  0 else if (glyphs[g].getDict().equals(
2137    uk.ac.vamsas.objects.utils.GlyphDictionary.DEFAULT))
2138    {
2139  0 dc = glyphs[g].getContent();
2140  0 AeContent[HASDC] = true;
2141    }
2142    else
2143    {
2144  0 Console.debug(
2145    "IMPLEMENTATION TODO: Ignoring unknown glyph type "
2146    + glyphs[g].getDict());
2147    }
2148    }
2149    }
2150  0 float val = 0;
2151  0 if (ae[aa].getValueCount() > 0)
2152    {
2153  0 AeContent[HASVALS] = true;
2154  0 if (ae[aa].getValueCount() > 1)
2155    {
2156  0 Console.warn(
2157    "ignoring additional " + (ae[aa].getValueCount() - 1)
2158    + " values in annotation element.");
2159    }
2160  0 val = ae[aa].getValue(0);
2161    }
2162  0 if (colour == null)
2163    {
2164  0 anot[row][pos] = new jalview.datamodel.Annotation(
2165  0 (dc != null) ? dc : "", desc,
2166  0 (ss != null) ? ss.charAt(0) : ' ', val);
2167    }
2168    else
2169    {
2170  0 anot[row][pos] = new jalview.datamodel.Annotation(
2171  0 (dc != null) ? dc : "", desc,
2172  0 (ss != null) ? ss.charAt(0) : ' ', val, colour);
2173    }
2174    }
2175    else
2176    {
2177  0 Console.warn("Ignoring out of bound annotation element " + aa
2178    + " in " + annotation.getVorbaId().getId());
2179    }
2180    }
2181    // decide on how many annotation rows are needed.
2182  0 if (mergeable)
2183    {
2184  0 for (int i = 0; i < anot[0].length; i++)
2185    {
2186  0 if (anot[1][i] != null)
2187    {
2188  0 anot[0][i] = anot[1][i];
2189  0 anot[0][i].description = anot[0][i].description + " (after)";
2190  0 AeContent[HASDESCSTR] = true; // we have valid description string
2191    // data
2192  0 anot[1][i] = null;
2193    }
2194    }
2195  0 anot[1] = null;
2196    }
2197    else
2198    {
2199  0 for (int i = 0; i < anot[0].length; i++)
2200    {
2201  0 anot[1][i].description = anot[1][i].description + " (after)";
2202    }
2203    }
2204  0 return new Object[] { AeContent, rangeMap, anot[0], anot[1] };
2205    }
2206    else
2207    {
2208    // no annotations to parse. Just return an empty annotationElement[]
2209    // array.
2210  0 return new Object[] { AeContent, rangeMap, anot[0], anot[1] };
2211    }
2212    // return null;
2213    }
2214   
2215    /**
2216    * @param jal
2217    * the jalview alignment to which the annotation will be attached
2218    * (ideally - freshly updated from corresponding vamsas alignment)
2219    * @param annotation
2220    * @return unbound jalview alignment annotation object.
2221    */
 
2222  0 toggle private jalview.datamodel.AlignmentAnnotation getjAlignmentAnnotation(
2223    jalview.datamodel.AlignmentI jal,
2224    uk.ac.vamsas.objects.core.RangeAnnotation annotation)
2225    {
2226  0 if (annotation == null)
2227    {
2228  0 return null;
2229    }
2230    // boolean
2231    // hasSequenceRef=annotation.getClass().equals(uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation.class);
2232    // boolean hasProvenance=hasSequenceRef ||
2233    // (annotation.getClass().equals(uk.ac.vamsas.objects.core.AlignmentAnnotation.class));
2234    /*
2235    * int se[] = getBounds(annotation); if (se==null) se=new int[]
2236    * {0,jal.getWidth()-1};
2237    */
2238  0 Object[] parsedRangeAnnotation = parseRangeAnnotation(annotation);
2239  0 String a_label = annotation.getLabel();
2240  0 String a_descr = annotation.getDescription();
2241  0 GraphLine gl = null;
2242  0 int type = 0;
2243  0 boolean interp = true; // cleared if annotation is DISCRETE
2244    // set type and other attributes from properties
2245  0 if (annotation.getPropertyCount() > 0)
2246    {
2247    // look for special jalview properties
2248  0 uk.ac.vamsas.objects.core.Property[] props = annotation.getProperty();
2249  0 for (int p = 0; p < props.length; p++)
2250    {
2251  0 if (props[p].getName().equalsIgnoreCase(DISCRETE_ANNOTATION))
2252    {
2253  0 type = AlignmentAnnotation.BAR_GRAPH;
2254  0 interp = false;
2255    }
2256  0 else if (props[p].getName().equalsIgnoreCase(CONTINUOUS_ANNOTATION))
2257    {
2258  0 type = AlignmentAnnotation.LINE_GRAPH;
2259    }
2260  0 else if (props[p].getName().equalsIgnoreCase(THRESHOLD))
2261    {
2262  0 Float val = null;
2263  0 try
2264    {
2265  0 val = Float.valueOf(props[p].getContent());
2266    } catch (Exception e)
2267    {
2268  0 Console.warn("Failed to parse threshold property");
2269    }
2270  0 if (val != null)
2271    {
2272  0 if (gl == null)
2273    {
2274  0 gl = new GraphLine(val.floatValue(), "",
2275    java.awt.Color.black);
2276    }
2277    else
2278    {
2279  0 gl.value = val.floatValue();
2280    }
2281    }
2282    }
2283  0 else if (props[p].getName().equalsIgnoreCase(THRESHOLD + "Name"))
2284    {
2285  0 if (gl == null)
2286    {
2287  0 gl = new GraphLine(0, "", java.awt.Color.black);
2288    }
2289  0 gl.label = props[p].getContent();
2290    }
2291    }
2292    }
2293  0 jalview.datamodel.AlignmentAnnotation jan = null;
2294  0 if (a_label == null || a_label.length() == 0)
2295    {
2296  0 a_label = annotation.getType();
2297  0 if (a_label.length() == 0)
2298    {
2299  0 a_label = "Unamed annotation";
2300    }
2301    }
2302  0 if (a_descr == null || a_descr.length() == 0)
2303    {
2304  0 a_descr = "Annotation of type '" + annotation.getType() + "'";
2305    }
2306  0 if (parsedRangeAnnotation == null)
2307    {
2308  0 Console.debug(
2309    "Inserting empty annotation row elements for a whole-alignment annotation.");
2310    }
2311    else
2312    {
2313  0 if (parsedRangeAnnotation[3] != null)
2314    {
2315  0 Console.warn("Ignoring 'After' annotation row in "
2316    + annotation.getVorbaId());
2317    }
2318  0 jalview.datamodel.Annotation[] arow = (jalview.datamodel.Annotation[]) parsedRangeAnnotation[2];
2319  0 boolean[] has = (boolean[]) parsedRangeAnnotation[0];
2320    // VAMSAS: getGraph is only on derived annotation for alignments - in this
2321    // way its 'odd' - there is already an existing TODO about removing this
2322    // flag as being redundant
2323    /*
2324    * if((annotation.getClass().equals(uk.ac.vamsas.objects.core.
2325    * AlignmentAnnotation.class) &&
2326    * ((uk.ac.vamsas.objects.core.AlignmentAnnotation)annotation).getGraph())
2327    * || (hasSequenceRef=true &&
2328    * ((uk.ac.vamsas.objects.core.AlignmentSequenceAnnotation
2329    * )annotation).getGraph())) {
2330    */
2331  0 if (has[HASVALS])
2332    {
2333  0 if (type == 0)
2334    {
2335  0 type = jalview.datamodel.AlignmentAnnotation.BAR_GRAPH; // default
2336    // type of
2337    // value
2338    // annotation
2339  0 if (has[HASHPHOB])
2340    {
2341    // no hints - so we ensure HPHOB display is like this.
2342  0 type = jalview.datamodel.AlignmentAnnotation.BAR_GRAPH;
2343    }
2344    }
2345    // make bounds and automatic description strings for jalview user's
2346    // benefit (these shouldn't be written back to vamsas document)
2347  0 boolean first = true;
2348  0 float min = 0, max = 1;
2349  0 int lastval = 0;
2350  0 for (int i = 0; i < arow.length; i++)
2351    {
2352  0 if (arow[i] != null)
2353    {
2354  0 if (i - lastval > 1 && interp)
2355    {
2356    // do some interpolation *between* points
2357  0 if (arow[lastval] != null)
2358    {
2359  0 float interval = arow[i].value - arow[lastval].value;
2360  0 interval /= i - lastval;
2361  0 float base = arow[lastval].value;
2362  0 for (int ip = lastval + 1, np = 0; ip < i; np++, ip++)
2363    {
2364  0 arow[ip] = new jalview.datamodel.Annotation("", "", ' ',
2365    interval * np + base);
2366    // NB - Interpolated points don't get a tooltip and
2367    // description.
2368    }
2369    }
2370    }
2371  0 lastval = i;
2372    // check range - shouldn't we have a min and max property in the
2373    // annotation object ?
2374  0 if (first)
2375    {
2376  0 min = max = arow[i].value;
2377  0 first = false;
2378    }
2379    else
2380    {
2381  0 if (arow[i].value < min)
2382    {
2383  0 min = arow[i].value;
2384    }
2385  0 else if (arow[i].value > max)
2386    {
2387  0 max = arow[i].value;
2388    }
2389    }
2390    // make tooltip and display char value
2391  0 if (!has[HASDESCSTR])
2392    {
2393  0 arow[i].description = arow[i].value + "";
2394    }
2395  0 if (!has[HASDC])
2396    {
2397  0 if (!interp)
2398    {
2399  0 if (arow[i].description != null
2400    && arow[i].description.length() < 3)
2401    {
2402    // copy over the description as the display char.
2403  0 arow[i].displayCharacter = new String(
2404    arow[i].description);
2405    }
2406    }
2407    else
2408    {
2409    // mark the position as a point used for the interpolation.
2410  0 arow[i].displayCharacter = arow[i].value + "";
2411    }
2412    }
2413    }
2414    }
2415  0 jan = new jalview.datamodel.AlignmentAnnotation(a_label, a_descr,
2416    arow, min, max, type);
2417    }
2418    else
2419    {
2420  0 if (annotation.getAnnotationElementCount() == 0)
2421    {
2422    // empty annotation array
2423    // TODO: alignment 'features' compare rangeType spec to alignment
2424    // width - if it is not complete, then mark regions on the annotation
2425    // row.
2426    }
2427  0 jan = new jalview.datamodel.AlignmentAnnotation(a_label, a_descr,
2428    arow);
2429  0 jan.setThreshold(null);
2430  0 jan.annotationId = annotation.getVorbaId().toString(); // keep all the
2431    // ids together.
2432    }
2433  0 if (annotation.getLinkCount() > 0)
2434    {
2435  0 Console.warn("Ignoring " + annotation.getLinkCount()
2436    + "links added to AlignmentAnnotation.");
2437    }
2438  0 if (annotation.getModifiable() == null
2439    || annotation.getModifiable().length() == 0) // TODO: USE VAMSAS
2440    // LIBRARY OBJECT
2441    // LOCK METHODS)
2442    {
2443  0 jan.editable = true;
2444    }
2445  0 try
2446    {
2447  0 if (annotation.getGroup() != null
2448    && annotation.getGroup().length() > 0)
2449    {
2450  0 jan.graphGroup = Integer.parseInt(annotation.getGroup()); // TODO:
2451    // group
2452    // similarly
2453    // named
2454    // annotation
2455    // together
2456    // ?
2457    }
2458    } catch (Exception e)
2459    {
2460  0 Console.info(
2461    "UNIMPLEMENTED : Couldn't parse non-integer group value for setting graphGroup correctly.");
2462    }
2463  0 return jan;
2464   
2465    }
2466   
2467  0 return null;
2468    }
2469   
2470    /**
2471    * get real bounds of a RangeType's specification. start and end are an
2472    * inclusive range within which all segments and positions lie. TODO: refactor
2473    * to vamsas utils
2474    *
2475    * @param dseta
2476    * @return int[] { start, end}
2477    */
 
2478  0 toggle private int[] getBounds(RangeType dseta)
2479    {
2480  0 if (dseta != null)
2481    {
2482  0 int[] se = null;
2483  0 if (dseta.getSegCount() > 0 && dseta.getPosCount() > 0)
2484    {
2485  0 throw new Error(MessageManager.getString(
2486    "error.invalid_vamsas_rangetype_cannot_resolve_lists"));
2487    }
2488  0 if (dseta.getSegCount() > 0)
2489    {
2490  0 se = getSegRange(dseta.getSeg(0), true);
2491  0 for (int s = 1, sSize = dseta.getSegCount(); s < sSize; s++)
2492    {
2493  0 int nse[] = getSegRange(dseta.getSeg(s), true);
2494  0 if (se[0] > nse[0])
2495    {
2496  0 se[0] = nse[0];
2497    }
2498  0 if (se[1] < nse[1])
2499    {
2500  0 se[1] = nse[1];
2501    }
2502    }
2503    }
2504  0 if (dseta.getPosCount() > 0)
2505    {
2506    // could do a polarity for pos range too. and pass back indication of
2507    // discontinuities.
2508  0 int pos = dseta.getPos(0).getI();
2509  0 se = new int[] { pos, pos };
2510  0 for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
2511    {
2512  0 pos = dseta.getPos(p).getI();
2513  0 if (se[0] > pos)
2514    {
2515  0 se[0] = pos;
2516    }
2517  0 if (se[1] < pos)
2518    {
2519  0 se[1] = pos;
2520    }
2521    }
2522    }
2523  0 return se;
2524    }
2525  0 return null;
2526    }
2527   
2528    /**
2529    * map from a rangeType's internal frame to the referenced object's coordinate
2530    * frame.
2531    *
2532    * @param dseta
2533    * @return int [] { ref(pos)...} for all pos in rangeType's frame.
2534    */
 
2535  0 toggle private int[] getMapping(RangeType dseta)
2536    {
2537  0 Vector posList = new Vector();
2538  0 if (dseta != null)
2539    {
2540  0 int[] se = null;
2541  0 if (dseta.getSegCount() > 0 && dseta.getPosCount() > 0)
2542    {
2543  0 throw new Error(MessageManager.getString(
2544    "error.invalid_vamsas_rangetype_cannot_resolve_lists"));
2545    }
2546  0 if (dseta.getSegCount() > 0)
2547    {
2548  0 for (int s = 0, sSize = dseta.getSegCount(); s < sSize; s++)
2549    {
2550  0 se = getSegRange(dseta.getSeg(s), false);
2551  0 int se_end = se[1 - se[2]] + (se[2] == 0 ? 1 : -1);
2552  0 for (int p = se[se[2]]; p != se_end; p += se[2] == 0 ? 1 : -1)
2553    {
2554  0 posList.add(Integer.valueOf(p));
2555    }
2556    }
2557    }
2558  0 else if (dseta.getPosCount() > 0)
2559    {
2560  0 int pos = dseta.getPos(0).getI();
2561   
2562  0 for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
2563    {
2564  0 pos = dseta.getPos(p).getI();
2565  0 posList.add(Integer.valueOf(pos));
2566    }
2567    }
2568    }
2569  0 if (posList != null && posList.size() > 0)
2570    {
2571  0 int[] range = new int[posList.size()];
2572  0 for (int i = 0; i < range.length; i++)
2573    {
2574  0 range[i] = ((Integer) posList.elementAt(i)).intValue();
2575    }
2576  0 posList.clear();
2577  0 return range;
2578    }
2579  0 return null;
2580    }
2581   
2582    /**
2583    *
2584    * @param maprange
2585    * where the from range is the local mapped range, and the to range
2586    * is the 'mapped' range in the MapRangeType
2587    * @param default
2588    * unit for local
2589    * @param default
2590    * unit for mapped
2591    * @return MapList
2592    */
 
2593  0 toggle private jalview.util.MapList parsemapType(MapType maprange, int localu,
2594    int mappedu)
2595    {
2596  0 jalview.util.MapList ml = null;
2597  0 int[] localRange = getMapping(maprange.getLocal());
2598  0 int[] mappedRange = getMapping(maprange.getMapped());
2599  0 long lu = maprange.getLocal().hasUnit() ? maprange.getLocal().getUnit()
2600    : localu;
2601  0 long mu = maprange.getMapped().hasUnit()
2602    ? maprange.getMapped().getUnit()
2603    : mappedu;
2604  0 ml = new jalview.util.MapList(localRange, mappedRange, (int) lu,
2605    (int) mu);
2606  0 return ml;
2607    }
2608   
2609    /**
2610    * initialise a range type object from a set of start/end inclusive intervals
2611    *
2612    * @param mrt
2613    * @param ranges
2614    */
 
2615  0 toggle private void initRangeType(RangeType mrt, List<int[]> ranges)
2616    {
2617  0 for (int[] range : ranges)
2618    {
2619  0 Seg vSeg = new Seg();
2620  0 vSeg.setStart(range[0]);
2621  0 vSeg.setEnd(range[1]);
2622  0 mrt.addSeg(vSeg);
2623    }
2624    }
2625   
2626    /**
2627    * initialise a MapType object from a MapList object.
2628    *
2629    * @param maprange
2630    * @param ml
2631    * @param setUnits
2632    */
 
2633  0 toggle private void initMapType(MapType maprange, jalview.util.MapList ml,
2634    boolean setUnits)
2635    {
2636  0 maprange.setLocal(new Local());
2637  0 maprange.setMapped(new Mapped());
2638  0 initRangeType(maprange.getLocal(), ml.getFromRanges());
2639  0 initRangeType(maprange.getMapped(), ml.getToRanges());
2640  0 if (setUnits)
2641    {
2642  0 maprange.getLocal().setUnit(ml.getFromRatio());
2643  0 maprange.getLocal().setUnit(ml.getToRatio());
2644    }
2645    }
2646   
2647    /*
2648    * not needed now. Provenance getVamsasProvenance(jalview.datamodel.Provenance
2649    * jprov) { jalview.datamodel.ProvenanceEntry[] entries = null; // TODO: fix
2650    * App and Action here. Provenance prov = new Provenance();
2651    * org.exolab.castor.types.Date date = new org.exolab.castor.types.Date( new
2652    * java.util.Date()); Entry provEntry;
2653    *
2654    * if (jprov != null) { entries = jprov.getEntries(); for (int i = 0; i <
2655    * entries.length; i++) { provEntry = new Entry(); try { date = new
2656    * org.exolab.castor.types.Date(entries[i].getDate()); } catch (Exception ex)
2657    * { ex.printStackTrace();
2658    *
2659    * date = new org.exolab.castor.types.Date(entries[i].getDate()); }
2660    * provEntry.setDate(date); provEntry.setUser(entries[i].getUser());
2661    * provEntry.setAction(entries[i].getAction()); prov.addEntry(provEntry); } }
2662    * else { provEntry = new Entry(); provEntry.setDate(date);
2663    * provEntry.setUser(System.getProperty("user.name")); // TODO: ext string
2664    * provEntry.setApp("JVAPP"); // TODO: ext string provEntry.setAction(action);
2665    * prov.addEntry(provEntry); }
2666    *
2667    * return prov; }
2668    */
 
2669  0 toggle jalview.datamodel.Provenance getJalviewProvenance(Provenance prov)
2670    {
2671    // TODO: fix App and Action entries and check use of provenance in jalview.
2672  0 jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();
2673  0 for (int i = 0; i < prov.getEntryCount(); i++)
2674    {
2675  0 jprov.addEntry(prov.getEntry(i).getUser(),
2676    prov.getEntry(i).getAction(), prov.getEntry(i).getDate(),
2677    prov.getEntry(i).getId());
2678    }
2679   
2680  0 return jprov;
2681    }
2682   
2683    /**
2684    *
2685    * @return default initial provenance list for a Jalview created vamsas
2686    * object.
2687    */
 
2688  0 toggle Provenance dummyProvenance()
2689    {
2690  0 return dummyProvenance(null);
2691    }
2692   
 
2693  0 toggle Entry dummyPEntry(String action)
2694    {
2695  0 Entry entry = new Entry();
2696  0 entry.setApp(this.provEntry.getApp());
2697  0 if (action != null)
2698    {
2699  0 entry.setAction(action);
2700    }
2701    else
2702    {
2703  0 entry.setAction("created.");
2704    }
2705  0 entry.setDate(new java.util.Date());
2706  0 entry.setUser(this.provEntry.getUser());
2707  0 return entry;
2708    }
2709   
 
2710  0 toggle Provenance dummyProvenance(String action)
2711    {
2712  0 Provenance prov = new Provenance();
2713  0 prov.addEntry(dummyPEntry(action));
2714  0 return prov;
2715    }
2716   
 
2717  0 toggle Entry addProvenance(Provenance p, String action)
2718    {
2719  0 Entry dentry = dummyPEntry(action);
2720  0 p.addEntry(dentry);
2721  0 return dentry;
2722    }
2723   
 
2724  0 toggle public Entry getProvEntry()
2725    {
2726  0 return provEntry;
2727    }
2728   
 
2729  0 toggle public IClientDocument getClientDocument()
2730    {
2731  0 return cdoc;
2732    }
2733   
 
2734  0 toggle public IdentityHashMap getJvObjectBinding()
2735    {
2736  0 return jv2vobj;
2737    }
2738   
 
2739  0 toggle public Hashtable getVamsasObjectBinding()
2740    {
2741  0 return vobj2jv;
2742    }
2743   
 
2744  0 toggle public void storeSequenceMappings(AlignmentViewport viewport,
2745    String title) throws Exception
2746    {
2747  0 AlignmentViewport av = viewport;
2748  0 try
2749    {
2750  0 jalview.datamodel.AlignmentI jal = av.getAlignment();
2751    // /////////////////////////////////////////
2752    // SAVE THE DATASET
2753  0 DataSet dataset = null;
2754  0 if (jal.getDataset() == null)
2755    {
2756  0 Console.warn("Creating new dataset for an alignment.");
2757  0 jal.setDataset(null);
2758    }
2759  0 dataset = (DataSet) ((Alignment) getjv2vObj(
2760    viewport.getSequenceSetId())).getV_parent(); // jal.getDataset());
2761  0 if (dataset == null)
2762    {
2763  0 dataset = (DataSet) getjv2vObj(jal.getDataset());
2764  0 Console.error(
2765    "Can't find the correct dataset for the alignment in this view. Creating new one.");
2766   
2767    }
2768    // Store any sequence mappings.
2769  0 List<AlignedCodonFrame> cframes = av.getAlignment().getCodonFrames();
2770  0 if (cframes != null)
2771    {
2772  0 for (AlignedCodonFrame acf : cframes)
2773    {
2774  0 if (acf.getdnaSeqs() != null && acf.getdnaSeqs().length > 0)
2775    {
2776  0 jalview.datamodel.SequenceI[] dmps = acf.getdnaSeqs();
2777  0 jalview.datamodel.Mapping[] mps = acf.getProtMappings();
2778  0 for (int smp = 0; smp < mps.length; smp++)
2779    {
2780  0 uk.ac.vamsas.objects.core.SequenceType mfrom = (SequenceType) getjv2vObj(
2781    dmps[smp]);
2782  0 if (mfrom != null)
2783    {
2784  0 new jalview.io.vamsas.Sequencemapping(this, mps[smp], mfrom,
2785    dataset);
2786    }
2787    else
2788    {
2789  0 Console.warn(
2790    "NO Vamsas Binding for local sequence! NOT CREATING MAPPING FOR "
2791    + dmps[smp].getDisplayId(true) + " to "
2792    + mps[smp].getTo().getName());
2793    }
2794    }
2795    }
2796    }
2797    }
2798    } catch (Exception e)
2799    {
2800  0 throw new Exception(MessageManager.formatMessage(
2801    "exception.couldnt_store_sequence_mappings", new String[]
2802    { title }), e);
2803    }
2804    }
2805   
 
2806  0 toggle public void clearSkipList()
2807    {
2808  0 if (skipList != null)
2809    {
2810  0 skipList.clear();
2811    }
2812    }
2813   
2814    /**
2815    * @return the skipList
2816    */
 
2817  0 toggle public Hashtable getSkipList()
2818    {
2819  0 return skipList;
2820    }
2821   
2822    /**
2823    * @param skipList
2824    * the skipList to set
2825    */
 
2826  0 toggle public void setSkipList(Hashtable skipList)
2827    {
2828  0 this.skipList = skipList;
2829    }
2830   
2831    /**
2832    * registry for datastoreItems
2833    */
2834    DatastoreRegistry dsReg = new DatastoreRegistry();
2835   
 
2836  0 toggle public DatastoreRegistry getDatastoreRegisty()
2837    {
2838  0 if (dsReg == null)
2839    {
2840  0 dsReg = new DatastoreRegistry();
2841    }
2842  0 return dsReg;
2843    }
2844    }