JAL-1803 restore property array for a sequence’s associated PDBEntry from project.
[jalview.git] / src / jalview / gui / Jalview2XML.java
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.gui;
22
23 import jalview.api.FeatureColourI;
24 import jalview.api.ViewStyleI;
25 import jalview.api.structures.JalviewStructureDisplayI;
26 import jalview.bin.Cache;
27 import jalview.datamodel.AlignedCodonFrame;
28 import jalview.datamodel.Alignment;
29 import jalview.datamodel.AlignmentAnnotation;
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.PDBEntry;
32 import jalview.datamodel.RnaViewerModel;
33 import jalview.datamodel.SequenceGroup;
34 import jalview.datamodel.SequenceI;
35 import jalview.datamodel.StructureViewerModel;
36 import jalview.datamodel.StructureViewerModel.StructureData;
37 import jalview.ext.varna.RnaModel;
38 import jalview.gui.StructureViewer.ViewerType;
39 import jalview.schemabinding.version2.AlcodMap;
40 import jalview.schemabinding.version2.AlcodonFrame;
41 import jalview.schemabinding.version2.Annotation;
42 import jalview.schemabinding.version2.AnnotationColours;
43 import jalview.schemabinding.version2.AnnotationElement;
44 import jalview.schemabinding.version2.CalcIdParam;
45 import jalview.schemabinding.version2.DBRef;
46 import jalview.schemabinding.version2.Features;
47 import jalview.schemabinding.version2.Group;
48 import jalview.schemabinding.version2.HiddenColumns;
49 import jalview.schemabinding.version2.JGroup;
50 import jalview.schemabinding.version2.JSeq;
51 import jalview.schemabinding.version2.JalviewModel;
52 import jalview.schemabinding.version2.JalviewModelSequence;
53 import jalview.schemabinding.version2.MapListFrom;
54 import jalview.schemabinding.version2.MapListTo;
55 import jalview.schemabinding.version2.Mapping;
56 import jalview.schemabinding.version2.MappingChoice;
57 import jalview.schemabinding.version2.OtherData;
58 import jalview.schemabinding.version2.PdbentryItem;
59 import jalview.schemabinding.version2.Pdbids;
60 import jalview.schemabinding.version2.Property;
61 import jalview.schemabinding.version2.RnaViewer;
62 import jalview.schemabinding.version2.SecondaryStructure;
63 import jalview.schemabinding.version2.Sequence;
64 import jalview.schemabinding.version2.SequenceSet;
65 import jalview.schemabinding.version2.SequenceSetProperties;
66 import jalview.schemabinding.version2.Setting;
67 import jalview.schemabinding.version2.StructureState;
68 import jalview.schemabinding.version2.ThresholdLine;
69 import jalview.schemabinding.version2.Tree;
70 import jalview.schemabinding.version2.UserColours;
71 import jalview.schemabinding.version2.Viewport;
72 import jalview.schemes.AnnotationColourGradient;
73 import jalview.schemes.ColourSchemeI;
74 import jalview.schemes.ColourSchemeProperty;
75 import jalview.schemes.FeatureColour;
76 import jalview.schemes.ResidueColourScheme;
77 import jalview.schemes.ResidueProperties;
78 import jalview.schemes.UserColourScheme;
79 import jalview.structure.StructureSelectionManager;
80 import jalview.structures.models.AAStructureBindingModel;
81 import jalview.util.MessageManager;
82 import jalview.util.Platform;
83 import jalview.util.StringUtils;
84 import jalview.util.jarInputStreamProvider;
85 import jalview.viewmodel.AlignmentViewport;
86 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
87 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
88 import jalview.ws.jws2.Jws2Discoverer;
89 import jalview.ws.jws2.dm.AAConSettings;
90 import jalview.ws.jws2.jabaws2.Jws2Instance;
91 import jalview.ws.params.ArgumentI;
92 import jalview.ws.params.AutoCalcSetting;
93 import jalview.ws.params.WsParamSetI;
94
95 import java.awt.Color;
96 import java.awt.Rectangle;
97 import java.io.BufferedReader;
98 import java.io.DataInputStream;
99 import java.io.DataOutputStream;
100 import java.io.File;
101 import java.io.FileInputStream;
102 import java.io.FileOutputStream;
103 import java.io.IOException;
104 import java.io.InputStreamReader;
105 import java.io.OutputStreamWriter;
106 import java.io.PrintWriter;
107 import java.lang.reflect.InvocationTargetException;
108 import java.net.MalformedURLException;
109 import java.net.URL;
110 import java.util.ArrayList;
111 import java.util.Arrays;
112 import java.util.Enumeration;
113 import java.util.HashMap;
114 import java.util.HashSet;
115 import java.util.Hashtable;
116 import java.util.IdentityHashMap;
117 import java.util.Iterator;
118 import java.util.LinkedHashMap;
119 import java.util.List;
120 import java.util.Map;
121 import java.util.Map.Entry;
122 import java.util.Set;
123 import java.util.Vector;
124 import java.util.jar.JarEntry;
125 import java.util.jar.JarInputStream;
126 import java.util.jar.JarOutputStream;
127
128 import javax.swing.JInternalFrame;
129 import javax.swing.JOptionPane;
130 import javax.swing.SwingUtilities;
131
132 import org.exolab.castor.xml.Marshaller;
133 import org.exolab.castor.xml.Unmarshaller;
134
135 /**
136  * Write out the current jalview desktop state as a Jalview XML stream.
137  * 
138  * Note: the vamsas objects referred to here are primitive versions of the
139  * VAMSAS project schema elements - they are not the same and most likely never
140  * will be :)
141  * 
142  * @author $author$
143  * @version $Revision: 1.134 $
144  */
145 public class Jalview2XML
146 {
147   private static final String VIEWER_PREFIX = "viewer_";
148
149   private static final String RNA_PREFIX = "rna_";
150
151   private static final String UTF_8 = "UTF-8";
152
153   // use this with nextCounter() to make unique names for entities
154   private int counter = 0;
155
156   /*
157    * SequenceI reference -> XML ID string in jalview XML. Populated as XML reps
158    * of sequence objects are created.
159    */
160   IdentityHashMap<SequenceI, String> seqsToIds = null;
161
162   /**
163    * jalview XML Sequence ID to jalview sequence object reference (both dataset
164    * and alignment sequences. Populated as XML reps of sequence objects are
165    * created.)
166    */
167   Map<String, SequenceI> seqRefIds = null;
168
169   Map<String, SequenceI> incompleteSeqs = null;
170
171   List<SeqFref> frefedSequence = null;
172
173   boolean raiseGUI = true; // whether errors are raised in dialog boxes or not
174
175   /*
176    * Map of reconstructed AlignFrame objects that appear to have come from
177    * SplitFrame objects (have a dna/protein complement view).
178    */
179   private Map<Viewport, AlignFrame> splitFrameCandidates = new HashMap<Viewport, AlignFrame>();
180
181   /*
182    * Map from displayed rna structure models to their saved session state jar
183    * entry names
184    */
185   private Map<RnaModel, String> rnaSessions = new HashMap<RnaModel, String>();
186
187   /**
188    * create/return unique hash string for sq
189    * 
190    * @param sq
191    * @return new or existing unique string for sq
192    */
193   String seqHash(SequenceI sq)
194   {
195     if (seqsToIds == null)
196     {
197       initSeqRefs();
198     }
199     if (seqsToIds.containsKey(sq))
200     {
201       return seqsToIds.get(sq);
202     }
203     else
204     {
205       // create sequential key
206       String key = "sq" + (seqsToIds.size() + 1);
207       key = makeHashCode(sq, key); // check we don't have an external reference
208       // for it already.
209       seqsToIds.put(sq, key);
210       return key;
211     }
212   }
213
214   void clearSeqRefs()
215   {
216     if (_cleartables)
217     {
218       if (seqRefIds != null)
219       {
220         seqRefIds.clear();
221       }
222       if (seqsToIds != null)
223       {
224         seqsToIds.clear();
225       }
226       if (incompleteSeqs != null)
227       {
228         incompleteSeqs.clear();
229       }
230       // seqRefIds = null;
231       // seqsToIds = null;
232     }
233     else
234     {
235       // do nothing
236       warn("clearSeqRefs called when _cleartables was not set. Doing nothing.");
237       // seqRefIds = new Hashtable();
238       // seqsToIds = new IdentityHashMap();
239     }
240   }
241
242   void initSeqRefs()
243   {
244     if (seqsToIds == null)
245     {
246       seqsToIds = new IdentityHashMap<SequenceI, String>();
247     }
248     if (seqRefIds == null)
249     {
250       seqRefIds = new HashMap<String, SequenceI>();
251     }
252     if (incompleteSeqs == null)
253     {
254       incompleteSeqs = new HashMap<String, SequenceI>();
255     }
256     if (frefedSequence == null)
257     {
258       frefedSequence = new ArrayList<SeqFref>();
259     }
260   }
261
262   public Jalview2XML()
263   {
264   }
265
266   public Jalview2XML(boolean raiseGUI)
267   {
268     this.raiseGUI = raiseGUI;
269   }
270
271   /**
272    * base class for resolving forward references to sequences by their ID
273    * 
274    * @author jprocter
275    *
276    */
277   abstract class SeqFref
278   {
279     String sref;
280
281     String type;
282
283     public SeqFref(String _sref, String type)
284     {
285       sref = _sref;
286       this.type = type;
287     }
288
289     public String getSref()
290     {
291       return sref;
292     }
293
294     public SequenceI getSrefSeq()
295     {
296       return seqRefIds.get(sref);
297     }
298
299     public boolean isResolvable()
300     {
301       return seqRefIds.get(sref) != null;
302     }
303
304     public SequenceI getSrefDatasetSeq()
305     {
306       SequenceI sq = seqRefIds.get(sref);
307       if (sq != null)
308       {
309         while (sq.getDatasetSequence() != null)
310         {
311           sq = sq.getDatasetSequence();
312         }
313       }
314       return sq;
315     }
316     /**
317      * @return true if the forward reference was fully resolved
318      */
319     abstract boolean resolve();
320
321     @Override
322     public String toString()
323     {
324       return type + " reference to " + sref;
325     }
326   }
327
328   /**
329    * create forward reference for a mapping
330    * 
331    * @param sref
332    * @param _jmap
333    * @return
334    */
335   public SeqFref newMappingRef(final String sref,
336           final jalview.datamodel.Mapping _jmap)
337   {
338     SeqFref fref = new SeqFref(sref, "Mapping")
339     {
340       public jalview.datamodel.Mapping jmap = _jmap;
341
342       @Override
343       boolean resolve()
344       {
345         SequenceI seq = getSrefDatasetSeq();
346         if (seq == null)
347         {
348           return false;
349         }
350         jmap.setTo(seq);
351         return true;
352       }
353     };
354     return fref;
355   }
356
357   public SeqFref newAlcodMapRef(final String sref,
358           final AlignedCodonFrame _cf, final jalview.datamodel.Mapping _jmap)
359   {
360
361     SeqFref fref = new SeqFref(sref, "Codon Frame")
362     {
363       AlignedCodonFrame cf = _cf;
364
365       public jalview.datamodel.Mapping mp = _jmap;
366
367       @Override
368       public boolean isResolvable()
369       {
370         return super.isResolvable() && mp.getTo() != null;
371       };
372
373       @Override
374       boolean resolve()
375       {
376         SequenceI seq = getSrefDatasetSeq();
377         if (seq == null)
378         {
379           return false;
380         }
381         cf.addMap(seq, mp.getTo(), mp.getMap());
382         return true;
383       }
384     };
385     return fref;
386   }
387
388   public void resolveFrefedSequences()
389   {
390     Iterator<SeqFref> nextFref=frefedSequence.iterator();
391     int toresolve=frefedSequence.size();
392     int unresolved=0,failedtoresolve=0;
393     while (nextFref.hasNext()) {
394       SeqFref ref = nextFref.next();
395       if (ref.isResolvable())
396       {
397         try {
398           if (ref.resolve())
399           {
400             nextFref.remove();
401           } else {
402             failedtoresolve++;
403           }
404         } catch (Exception x) {
405           System.err.println("IMPLEMENTATION ERROR: Failed to resolve forward reference for sequence "+ref.getSref());
406           x.printStackTrace();
407           failedtoresolve++;
408         } 
409       } else {
410         unresolved++;
411       }
412     }
413     if (unresolved>0)
414     {
415       System.err.println("Jalview Project Import: There were " + unresolved
416               + " forward references left unresolved on the stack.");
417     }
418     if (failedtoresolve>0)
419     {
420       System.err.println("SERIOUS! " + failedtoresolve
421               + " resolvable forward references failed to resolve.");
422     }
423     if (incompleteSeqs != null && incompleteSeqs.size() > 0)
424     {
425       System.err.println("Jalview Project Import: There are "
426               + incompleteSeqs.size()
427               + " sequences which may have incomplete metadata.");
428       if (incompleteSeqs.size() < 10)
429       {
430         for (SequenceI s : incompleteSeqs.values())
431         {
432           System.err.println(s.toString());
433         }
434       }
435       else
436       {
437         System.err
438                 .println("Too many to report. Skipping output of incomplete sequences.");
439       }
440     }
441   }
442
443   /**
444    * This maintains a map of viewports, the key being the seqSetId. Important to
445    * set historyItem and redoList for multiple views
446    */
447   Map<String, AlignViewport> viewportsAdded = new HashMap<String, AlignViewport>();
448
449   Map<String, AlignmentAnnotation> annotationIds = new HashMap<String, AlignmentAnnotation>();
450
451   String uniqueSetSuffix = "";
452
453   /**
454    * List of pdbfiles added to Jar
455    */
456   List<String> pdbfiles = null;
457
458   // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE
459   public void saveState(File statefile)
460   {
461     FileOutputStream fos = null;
462     try
463     {
464       fos = new FileOutputStream(statefile);
465       JarOutputStream jout = new JarOutputStream(fos);
466       saveState(jout);
467
468     } catch (Exception e)
469     {
470       // TODO: inform user of the problem - they need to know if their data was
471       // not saved !
472       if (errorMessage == null)
473       {
474         errorMessage = "Couldn't write Jalview Archive to output file '"
475                 + statefile + "' - See console error log for details";
476       }
477       else
478       {
479         errorMessage += "(output file was '" + statefile + "')";
480       }
481       e.printStackTrace();
482     } finally
483     {
484       if (fos != null)
485       {
486         try
487         {
488           fos.close();
489         } catch (IOException e)
490         {
491           // ignore
492         }
493       }
494     }
495     reportErrors();
496   }
497
498   /**
499    * Writes a jalview project archive to the given Jar output stream.
500    * 
501    * @param jout
502    */
503   public void saveState(JarOutputStream jout)
504   {
505     AlignFrame[] frames = Desktop.getAlignFrames();
506
507     if (frames == null)
508     {
509       return;
510     }
511     saveAllFrames(Arrays.asList(frames), jout);
512   }
513
514   /**
515    * core method for storing state for a set of AlignFrames.
516    * 
517    * @param frames
518    *          - frames involving all data to be exported (including containing
519    *          splitframes)
520    * @param jout
521    *          - project output stream
522    */
523   private void saveAllFrames(List<AlignFrame> frames, JarOutputStream jout)
524   {
525     Hashtable<String, AlignFrame> dsses = new Hashtable<String, AlignFrame>();
526
527     /*
528      * ensure cached data is clear before starting
529      */
530     // todo tidy up seqRefIds, seqsToIds initialisation / reset
531     rnaSessions.clear();
532     splitFrameCandidates.clear();
533
534     try
535     {
536
537       // NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS
538       // //////////////////////////////////////////////////
539
540       List<String> shortNames = new ArrayList<String>();
541       List<String> viewIds = new ArrayList<String>();
542
543       // REVERSE ORDER
544       for (int i = frames.size() - 1; i > -1; i--)
545       {
546         AlignFrame af = frames.get(i);
547         // skip ?
548         if (skipList != null
549                 && skipList
550                         .containsKey(af.getViewport().getSequenceSetId()))
551         {
552           continue;
553         }
554
555         String shortName = makeFilename(af, shortNames);
556
557         int ap, apSize = af.alignPanels.size();
558
559         for (ap = 0; ap < apSize; ap++)
560         {
561           AlignmentPanel apanel = af.alignPanels.get(ap);
562           String fileName = apSize == 1 ? shortName : ap + shortName;
563           if (!fileName.endsWith(".xml"))
564           {
565             fileName = fileName + ".xml";
566           }
567
568           saveState(apanel, fileName, jout, viewIds);
569
570           String dssid = getDatasetIdRef(af.getViewport().getAlignment()
571                   .getDataset());
572           if (!dsses.containsKey(dssid))
573           {
574             dsses.put(dssid, af);
575           }
576         }
577       }
578
579       writeDatasetFor(dsses, "" + jout.hashCode() + " " + uniqueSetSuffix,
580               jout);
581
582       try
583       {
584         jout.flush();
585       } catch (Exception foo)
586       {
587       }
588       ;
589       jout.close();
590     } catch (Exception ex)
591     {
592       // TODO: inform user of the problem - they need to know if their data was
593       // not saved !
594       if (errorMessage == null)
595       {
596         errorMessage = "Couldn't write Jalview Archive - see error output for details";
597       }
598       ex.printStackTrace();
599     }
600   }
601
602   /**
603    * Generates a distinct file name, based on the title of the AlignFrame, by
604    * appending _n for increasing n until an unused name is generated. The new
605    * name (without its extension) is added to the list.
606    * 
607    * @param af
608    * @param namesUsed
609    * @return the generated name, with .xml extension
610    */
611   protected String makeFilename(AlignFrame af, List<String> namesUsed)
612   {
613     String shortName = af.getTitle();
614
615     if (shortName.indexOf(File.separatorChar) > -1)
616     {
617       shortName = shortName.substring(shortName
618               .lastIndexOf(File.separatorChar) + 1);
619     }
620
621     int count = 1;
622
623     while (namesUsed.contains(shortName))
624     {
625       if (shortName.endsWith("_" + (count - 1)))
626       {
627         shortName = shortName.substring(0, shortName.lastIndexOf("_"));
628       }
629
630       shortName = shortName.concat("_" + count);
631       count++;
632     }
633
634     namesUsed.add(shortName);
635
636     if (!shortName.endsWith(".xml"))
637     {
638       shortName = shortName + ".xml";
639     }
640     return shortName;
641   }
642
643   // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW
644   public boolean saveAlignment(AlignFrame af, String jarFile,
645           String fileName)
646   {
647     try
648     {
649       FileOutputStream fos = new FileOutputStream(jarFile);
650       JarOutputStream jout = new JarOutputStream(fos);
651       List<AlignFrame> frames = new ArrayList<AlignFrame>();
652
653       // resolve splitframes
654       if (af.getViewport().getCodingComplement() != null)
655       {
656         frames = ((SplitFrame) af.getSplitViewContainer()).getAlignFrames();
657       }
658       else
659       {
660         frames.add(af);
661       }
662       saveAllFrames(frames, jout);
663       try
664       {
665         jout.flush();
666       } catch (Exception foo)
667       {
668       }
669       ;
670       jout.close();
671       return true;
672     } catch (Exception ex)
673     {
674       errorMessage = "Couldn't Write alignment view to Jalview Archive - see error output for details";
675       ex.printStackTrace();
676       return false;
677     }
678   }
679
680   private void writeDatasetFor(Hashtable<String, AlignFrame> dsses,
681           String fileName, JarOutputStream jout)
682   {
683
684     for (String dssids : dsses.keySet())
685     {
686       AlignFrame _af = dsses.get(dssids);
687       String jfileName = fileName + " Dataset for " + _af.getTitle();
688       if (!jfileName.endsWith(".xml"))
689       {
690         jfileName = jfileName + ".xml";
691       }
692       saveState(_af.alignPanel, jfileName, true, jout, null);
693     }
694   }
695
696   /**
697    * create a JalviewModel from an alignment view and marshall it to a
698    * JarOutputStream
699    * 
700    * @param ap
701    *          panel to create jalview model for
702    * @param fileName
703    *          name of alignment panel written to output stream
704    * @param jout
705    *          jar output stream
706    * @param viewIds
707    * @param out
708    *          jar entry name
709    */
710   public JalviewModel saveState(AlignmentPanel ap, String fileName,
711           JarOutputStream jout, List<String> viewIds)
712   {
713     return saveState(ap, fileName, false, jout, viewIds);
714   }
715
716   /**
717    * create a JalviewModel from an alignment view and marshall it to a
718    * JarOutputStream
719    * 
720    * @param ap
721    *          panel to create jalview model for
722    * @param fileName
723    *          name of alignment panel written to output stream
724    * @param storeDS
725    *          when true, only write the dataset for the alignment, not the data
726    *          associated with the view.
727    * @param jout
728    *          jar output stream
729    * @param out
730    *          jar entry name
731    */
732   public JalviewModel saveState(AlignmentPanel ap, String fileName,
733           boolean storeDS, JarOutputStream jout, List<String> viewIds)
734   {
735     if (viewIds == null)
736     {
737       viewIds = new ArrayList<String>();
738     }
739
740     initSeqRefs();
741
742     List<UserColourScheme> userColours = new ArrayList<UserColourScheme>();
743
744     AlignViewport av = ap.av;
745
746     JalviewModel object = new JalviewModel();
747     object.setVamsasModel(new jalview.schemabinding.version2.VamsasModel());
748
749     object.setCreationDate(new java.util.Date(System.currentTimeMillis()));
750     object.setVersion(jalview.bin.Cache.getDefault("VERSION",
751             "Development Build"));
752
753     /**
754      * rjal is full height alignment, jal is actual alignment with full metadata
755      * but excludes hidden sequences.
756      */
757     jalview.datamodel.AlignmentI rjal = av.getAlignment(), jal = rjal;
758
759     if (av.hasHiddenRows())
760     {
761       rjal = jal.getHiddenSequences().getFullAlignment();
762     }
763
764     SequenceSet vamsasSet = new SequenceSet();
765     Sequence vamsasSeq;
766     JalviewModelSequence jms = new JalviewModelSequence();
767
768     vamsasSet.setGapChar(jal.getGapCharacter() + "");
769
770     if (jal.getDataset() != null)
771     {
772       // dataset id is the dataset's hashcode
773       vamsasSet.setDatasetId(getDatasetIdRef(jal.getDataset()));
774       if (storeDS)
775       {
776         // switch jal and the dataset
777         jal = jal.getDataset();
778         rjal = jal;
779       }
780     }
781     if (jal.getProperties() != null)
782     {
783       Enumeration en = jal.getProperties().keys();
784       while (en.hasMoreElements())
785       {
786         String key = en.nextElement().toString();
787         SequenceSetProperties ssp = new SequenceSetProperties();
788         ssp.setKey(key);
789         ssp.setValue(jal.getProperties().get(key).toString());
790         vamsasSet.addSequenceSetProperties(ssp);
791       }
792     }
793
794     JSeq jseq;
795     Set<String> calcIdSet = new HashSet<String>();
796     // record the set of vamsas sequence XML POJO we create.
797     HashMap<String,Sequence> vamsasSetIds = new HashMap<String,Sequence>(); 
798     // SAVE SEQUENCES
799     for (final SequenceI jds : rjal.getSequences())
800     {
801       final SequenceI jdatasq = jds.getDatasetSequence() == null ? jds
802               : jds.getDatasetSequence();
803       String id = seqHash(jds);
804       if (vamsasSetIds.get(id) == null)
805       {
806         if (seqRefIds.get(id) != null && !storeDS)
807         {
808           // This happens for two reasons: 1. multiple views are being
809           // serialised.
810           // 2. the hashCode has collided with another sequence's code. This
811           // DOES
812           // HAPPEN! (PF00072.15.stk does this)
813           // JBPNote: Uncomment to debug writing out of files that do not read
814           // back in due to ArrayOutOfBoundExceptions.
815           // System.err.println("vamsasSeq backref: "+id+"");
816           // System.err.println(jds.getName()+"
817           // "+jds.getStart()+"-"+jds.getEnd()+" "+jds.getSequenceAsString());
818           // System.err.println("Hashcode: "+seqHash(jds));
819           // SequenceI rsq = (SequenceI) seqRefIds.get(id + "");
820           // System.err.println(rsq.getName()+"
821           // "+rsq.getStart()+"-"+rsq.getEnd()+" "+rsq.getSequenceAsString());
822           // System.err.println("Hashcode: "+seqHash(rsq));
823         }
824         else
825         {
826           vamsasSeq = createVamsasSequence(id, jds);
827           vamsasSet.addSequence(vamsasSeq);
828           vamsasSetIds.put(id, vamsasSeq);
829           seqRefIds.put(id, jds);
830         }
831       }
832       jseq = new JSeq();
833       jseq.setStart(jds.getStart());
834       jseq.setEnd(jds.getEnd());
835       jseq.setColour(av.getSequenceColour(jds).getRGB());
836
837       jseq.setId(id); // jseq id should be a string not a number
838       if (!storeDS)
839       {
840         // Store any sequences this sequence represents
841         if (av.hasHiddenRows())
842         {
843           // use rjal, contains the full height alignment
844           jseq.setHidden(av.getAlignment().getHiddenSequences()
845                   .isHidden(jds));
846
847           if (av.isHiddenRepSequence(jds))
848           {
849             jalview.datamodel.SequenceI[] reps = av
850                     .getRepresentedSequences(jds)
851                     .getSequencesInOrder(rjal);
852
853             for (int h = 0; h < reps.length; h++)
854             {
855               if (reps[h] != jds)
856               {
857                 jseq.addHiddenSequences(rjal.findIndex(reps[h]));
858               }
859             }
860           }
861         }
862         // mark sequence as reference - if it is the reference for this view
863         if (jal.hasSeqrep())
864         {
865           jseq.setViewreference(jds == jal.getSeqrep());
866         }
867       }
868
869       // TODO: omit sequence features from each alignment view's XML dump if we
870       // are storing dataset
871       if (jds.getSequenceFeatures() != null)
872       {
873         jalview.datamodel.SequenceFeature[] sf = jds.getSequenceFeatures();
874         int index = 0;
875         while (index < sf.length)
876         {
877           Features features = new Features();
878
879           features.setBegin(sf[index].getBegin());
880           features.setEnd(sf[index].getEnd());
881           features.setDescription(sf[index].getDescription());
882           features.setType(sf[index].getType());
883           features.setFeatureGroup(sf[index].getFeatureGroup());
884           features.setScore(sf[index].getScore());
885           if (sf[index].links != null)
886           {
887             for (int l = 0; l < sf[index].links.size(); l++)
888             {
889               OtherData keyValue = new OtherData();
890               keyValue.setKey("LINK_" + l);
891               keyValue.setValue(sf[index].links.elementAt(l).toString());
892               features.addOtherData(keyValue);
893             }
894           }
895           if (sf[index].otherDetails != null)
896           {
897             String key;
898             Iterator<String> keys = sf[index].otherDetails.keySet()
899                     .iterator();
900             while (keys.hasNext())
901             {
902               key = keys.next();
903               OtherData keyValue = new OtherData();
904               keyValue.setKey(key);
905               keyValue.setValue(sf[index].otherDetails.get(key).toString());
906               features.addOtherData(keyValue);
907             }
908           }
909
910           jseq.addFeatures(features);
911           index++;
912         }
913       }
914
915       if (jdatasq.getAllPDBEntries() != null)
916       {
917         Enumeration en = jdatasq.getAllPDBEntries().elements();
918         while (en.hasMoreElements())
919         {
920           Pdbids pdb = new Pdbids();
921           jalview.datamodel.PDBEntry entry = (jalview.datamodel.PDBEntry) en
922                   .nextElement();
923
924           String pdbId = entry.getId();
925           pdb.setId(pdbId);
926           pdb.setType(entry.getType());
927
928           /*
929            * Store any structure views associated with this sequence. This
930            * section copes with duplicate entries in the project, so a dataset
931            * only view *should* be coped with sensibly.
932            */
933           // This must have been loaded, is it still visible?
934           JInternalFrame[] frames = Desktop.desktop.getAllFrames();
935           String matchedFile = null;
936           for (int f = frames.length - 1; f > -1; f--)
937           {
938             if (frames[f] instanceof StructureViewerBase)
939             {
940               StructureViewerBase viewFrame = (StructureViewerBase) frames[f];
941               matchedFile = saveStructureState(ap, jds, pdb, entry,
942                       viewIds, matchedFile, viewFrame);
943               /*
944                * Only store each structure viewer's state once in the project
945                * jar. First time through only (storeDS==false)
946                */
947               String viewId = viewFrame.getViewId();
948               if (!storeDS && !viewIds.contains(viewId))
949               {
950                 viewIds.add(viewId);
951                 try
952                 {
953                   String viewerState = viewFrame.getStateInfo();
954                   writeJarEntry(jout, getViewerJarEntryName(viewId),
955                           viewerState.getBytes());
956                 } catch (IOException e)
957                 {
958                   System.err.println("Error saving viewer state: "
959                           + e.getMessage());
960                 }
961               }
962             }
963           }
964
965           if (matchedFile != null || entry.getFile() != null)
966           {
967             if (entry.getFile() != null)
968             {
969               // use entry's file
970               matchedFile = entry.getFile();
971             }
972             pdb.setFile(matchedFile); // entry.getFile());
973             if (pdbfiles == null)
974             {
975               pdbfiles = new ArrayList<String>();
976             }
977
978             if (!pdbfiles.contains(pdbId))
979             {
980               pdbfiles.add(pdbId);
981               copyFileToJar(jout, matchedFile, pdbId);
982             }
983           }
984
985           if (entry.getProperty() != null && !entry.getProperty().isEmpty())
986           {
987             PdbentryItem item = new PdbentryItem();
988             Hashtable properties = entry.getProperty();
989             Enumeration en2 = properties.keys();
990             while (en2.hasMoreElements())
991             {
992               Property prop = new Property();
993               String key = en2.nextElement().toString();
994               prop.setName(key);
995               prop.setValue(properties.get(key).toString());
996               item.addProperty(prop);
997             }
998             pdb.addPdbentryItem(item);
999           }
1000
1001           jseq.addPdbids(pdb);
1002         }
1003       }
1004
1005       saveRnaViewers(jout, jseq, jds, viewIds, ap, storeDS);
1006
1007       jms.addJSeq(jseq);
1008     }
1009
1010     if (!storeDS && av.hasHiddenRows())
1011     {
1012       jal = av.getAlignment();
1013     }
1014     // SAVE MAPPINGS
1015     // FOR DATASET
1016     if (storeDS && jal.getCodonFrames() != null)
1017     {
1018       List<AlignedCodonFrame> jac = jal.getCodonFrames();
1019       for (AlignedCodonFrame acf : jac)
1020       {
1021         AlcodonFrame alc = new AlcodonFrame();
1022         if (acf.getProtMappings() != null
1023                 && acf.getProtMappings().length > 0)
1024         {
1025           boolean hasMap = false;
1026           SequenceI[] dnas = acf.getdnaSeqs();
1027           jalview.datamodel.Mapping[] pmaps = acf.getProtMappings();
1028           for (int m = 0; m < pmaps.length; m++)
1029           {
1030             AlcodMap alcmap = new AlcodMap();
1031             alcmap.setDnasq(seqHash(dnas[m]));
1032             alcmap.setMapping(createVamsasMapping(pmaps[m], dnas[m], null,
1033                     false));
1034             alc.addAlcodMap(alcmap);
1035             hasMap = true;
1036           }
1037           if (hasMap)
1038           {
1039             vamsasSet.addAlcodonFrame(alc);
1040           }
1041         }
1042         // TODO: delete this ? dead code from 2.8.3->2.9 ?
1043         // {
1044         // AlcodonFrame alc = new AlcodonFrame();
1045         // vamsasSet.addAlcodonFrame(alc);
1046         // for (int p = 0; p < acf.aaWidth; p++)
1047         // {
1048         // Alcodon cmap = new Alcodon();
1049         // if (acf.codons[p] != null)
1050         // {
1051         // // Null codons indicate a gapped column in the translated peptide
1052         // // alignment.
1053         // cmap.setPos1(acf.codons[p][0]);
1054         // cmap.setPos2(acf.codons[p][1]);
1055         // cmap.setPos3(acf.codons[p][2]);
1056         // }
1057         // alc.addAlcodon(cmap);
1058         // }
1059         // if (acf.getProtMappings() != null
1060         // && acf.getProtMappings().length > 0)
1061         // {
1062         // SequenceI[] dnas = acf.getdnaSeqs();
1063         // jalview.datamodel.Mapping[] pmaps = acf.getProtMappings();
1064         // for (int m = 0; m < pmaps.length; m++)
1065         // {
1066         // AlcodMap alcmap = new AlcodMap();
1067         // alcmap.setDnasq(seqHash(dnas[m]));
1068         // alcmap.setMapping(createVamsasMapping(pmaps[m], dnas[m], null,
1069         // false));
1070         // alc.addAlcodMap(alcmap);
1071         // }
1072         // }
1073       }
1074     }
1075
1076     // SAVE TREES
1077     // /////////////////////////////////
1078     if (!storeDS && av.currentTree != null)
1079     {
1080       // FIND ANY ASSOCIATED TREES
1081       // NOT IMPLEMENTED FOR HEADLESS STATE AT PRESENT
1082       if (Desktop.desktop != null)
1083       {
1084         JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1085
1086         for (int t = 0; t < frames.length; t++)
1087         {
1088           if (frames[t] instanceof TreePanel)
1089           {
1090             TreePanel tp = (TreePanel) frames[t];
1091
1092             if (tp.treeCanvas.av.getAlignment() == jal)
1093             {
1094               Tree tree = new Tree();
1095               tree.setTitle(tp.getTitle());
1096               tree.setCurrentTree((av.currentTree == tp.getTree()));
1097               tree.setNewick(tp.getTree().toString());
1098               tree.setThreshold(tp.treeCanvas.threshold);
1099
1100               tree.setFitToWindow(tp.fitToWindow.getState());
1101               tree.setFontName(tp.getTreeFont().getName());
1102               tree.setFontSize(tp.getTreeFont().getSize());
1103               tree.setFontStyle(tp.getTreeFont().getStyle());
1104               tree.setMarkUnlinked(tp.placeholdersMenu.getState());
1105
1106               tree.setShowBootstrap(tp.bootstrapMenu.getState());
1107               tree.setShowDistances(tp.distanceMenu.getState());
1108
1109               tree.setHeight(tp.getHeight());
1110               tree.setWidth(tp.getWidth());
1111               tree.setXpos(tp.getX());
1112               tree.setYpos(tp.getY());
1113               tree.setId(makeHashCode(tp, null));
1114               jms.addTree(tree);
1115             }
1116           }
1117         }
1118       }
1119     }
1120
1121     // SAVE ANNOTATIONS
1122     /**
1123      * store forward refs from an annotationRow to any groups
1124      */
1125     IdentityHashMap<SequenceGroup, String> groupRefs = new IdentityHashMap<SequenceGroup, String>();
1126     if (storeDS)
1127     {
1128       for (SequenceI sq : jal.getSequences())
1129       {
1130         // Store annotation on dataset sequences only
1131         AlignmentAnnotation[] aa = sq.getAnnotation();
1132         if (aa != null && aa.length > 0)
1133         {
1134           storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
1135                   vamsasSet);
1136         }
1137       }
1138     }
1139     else
1140     {
1141       if (jal.getAlignmentAnnotation() != null)
1142       {
1143         // Store the annotation shown on the alignment.
1144         AlignmentAnnotation[] aa = jal.getAlignmentAnnotation();
1145         storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
1146                 vamsasSet);
1147       }
1148     }
1149     // SAVE GROUPS
1150     if (jal.getGroups() != null)
1151     {
1152       JGroup[] groups = new JGroup[jal.getGroups().size()];
1153       int i = -1;
1154       for (jalview.datamodel.SequenceGroup sg : jal.getGroups())
1155       {
1156         JGroup jGroup = new JGroup();
1157         groups[++i] = jGroup;
1158
1159         jGroup.setStart(sg.getStartRes());
1160         jGroup.setEnd(sg.getEndRes());
1161         jGroup.setName(sg.getName());
1162         if (groupRefs.containsKey(sg))
1163         {
1164           // group has references so set its ID field
1165           jGroup.setId(groupRefs.get(sg));
1166         }
1167         if (sg.cs != null)
1168         {
1169           if (sg.cs.conservationApplied())
1170           {
1171             jGroup.setConsThreshold(sg.cs.getConservationInc());
1172
1173             if (sg.cs instanceof jalview.schemes.UserColourScheme)
1174             {
1175               jGroup.setColour(setUserColourScheme(sg.cs, userColours, jms));
1176             }
1177             else
1178             {
1179               jGroup.setColour(ColourSchemeProperty.getColourName(sg.cs));
1180             }
1181           }
1182           else if (sg.cs instanceof jalview.schemes.AnnotationColourGradient)
1183           {
1184             jGroup.setColour("AnnotationColourGradient");
1185             jGroup.setAnnotationColours(constructAnnotationColours(
1186                     (jalview.schemes.AnnotationColourGradient) sg.cs,
1187                     userColours, jms));
1188           }
1189           else if (sg.cs instanceof jalview.schemes.UserColourScheme)
1190           {
1191             jGroup.setColour(setUserColourScheme(sg.cs, userColours, jms));
1192           }
1193           else
1194           {
1195             jGroup.setColour(ColourSchemeProperty.getColourName(sg.cs));
1196           }
1197
1198           jGroup.setPidThreshold(sg.cs.getThreshold());
1199         }
1200
1201         jGroup.setOutlineColour(sg.getOutlineColour().getRGB());
1202         jGroup.setDisplayBoxes(sg.getDisplayBoxes());
1203         jGroup.setDisplayText(sg.getDisplayText());
1204         jGroup.setColourText(sg.getColourText());
1205         jGroup.setTextCol1(sg.textColour.getRGB());
1206         jGroup.setTextCol2(sg.textColour2.getRGB());
1207         jGroup.setTextColThreshold(sg.thresholdTextColour);
1208         jGroup.setShowUnconserved(sg.getShowNonconserved());
1209         jGroup.setIgnoreGapsinConsensus(sg.getIgnoreGapsConsensus());
1210         jGroup.setShowConsensusHistogram(sg.isShowConsensusHistogram());
1211         jGroup.setShowSequenceLogo(sg.isShowSequenceLogo());
1212         jGroup.setNormaliseSequenceLogo(sg.isNormaliseSequenceLogo());
1213         for (SequenceI seq : sg.getSequences())
1214         {
1215           jGroup.addSeq(seqHash(seq));
1216         }
1217       }
1218
1219       jms.setJGroup(groups);
1220     }
1221     if (!storeDS)
1222     {
1223       // /////////SAVE VIEWPORT
1224       Viewport view = new Viewport();
1225       view.setTitle(ap.alignFrame.getTitle());
1226       view.setSequenceSetId(makeHashCode(av.getSequenceSetId(),
1227               av.getSequenceSetId()));
1228       view.setId(av.getViewId());
1229       if (av.getCodingComplement() != null)
1230       {
1231         view.setComplementId(av.getCodingComplement().getViewId());
1232       }
1233       view.setViewName(av.viewName);
1234       view.setGatheredViews(av.isGatherViewsHere());
1235
1236       Rectangle size = ap.av.getExplodedGeometry();
1237       Rectangle position = size;
1238       if (size == null)
1239       {
1240         size = ap.alignFrame.getBounds();
1241         if (av.getCodingComplement() != null)
1242         {
1243           position = ((SplitFrame) ap.alignFrame.getSplitViewContainer())
1244                   .getBounds();
1245         }
1246         else
1247         {
1248           position = size;
1249         }
1250       }
1251       view.setXpos(position.x);
1252       view.setYpos(position.y);
1253
1254       view.setWidth(size.width);
1255       view.setHeight(size.height);
1256
1257       view.setStartRes(av.startRes);
1258       view.setStartSeq(av.startSeq);
1259
1260       if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme)
1261       {
1262         view.setBgColour(setUserColourScheme(av.getGlobalColourScheme(),
1263                 userColours, jms));
1264       }
1265       else if (av.getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient)
1266       {
1267         AnnotationColours ac = constructAnnotationColours(
1268                 (jalview.schemes.AnnotationColourGradient) av
1269                         .getGlobalColourScheme(),
1270                 userColours, jms);
1271
1272         view.setAnnotationColours(ac);
1273         view.setBgColour("AnnotationColourGradient");
1274       }
1275       else
1276       {
1277         view.setBgColour(ColourSchemeProperty.getColourName(av
1278                 .getGlobalColourScheme()));
1279       }
1280
1281       ColourSchemeI cs = av.getGlobalColourScheme();
1282
1283       if (cs != null)
1284       {
1285         if (cs.conservationApplied())
1286         {
1287           view.setConsThreshold(cs.getConservationInc());
1288           if (cs instanceof jalview.schemes.UserColourScheme)
1289           {
1290             view.setBgColour(setUserColourScheme(cs, userColours, jms));
1291           }
1292         }
1293
1294         if (cs instanceof ResidueColourScheme)
1295         {
1296           view.setPidThreshold(cs.getThreshold());
1297         }
1298       }
1299
1300       view.setConservationSelected(av.getConservationSelected());
1301       view.setPidSelected(av.getAbovePIDThreshold());
1302       view.setFontName(av.font.getName());
1303       view.setFontSize(av.font.getSize());
1304       view.setFontStyle(av.font.getStyle());
1305       view.setScaleProteinAsCdna(av.getViewStyle().isScaleProteinAsCdna());
1306       view.setRenderGaps(av.isRenderGaps());
1307       view.setShowAnnotation(av.isShowAnnotation());
1308       view.setShowBoxes(av.getShowBoxes());
1309       view.setShowColourText(av.getColourText());
1310       view.setShowFullId(av.getShowJVSuffix());
1311       view.setRightAlignIds(av.isRightAlignIds());
1312       view.setShowSequenceFeatures(av.isShowSequenceFeatures());
1313       view.setShowText(av.getShowText());
1314       view.setShowUnconserved(av.getShowUnconserved());
1315       view.setWrapAlignment(av.getWrapAlignment());
1316       view.setTextCol1(av.getTextColour().getRGB());
1317       view.setTextCol2(av.getTextColour2().getRGB());
1318       view.setTextColThreshold(av.getThresholdTextColour());
1319       view.setShowConsensusHistogram(av.isShowConsensusHistogram());
1320       view.setShowSequenceLogo(av.isShowSequenceLogo());
1321       view.setNormaliseSequenceLogo(av.isNormaliseSequenceLogo());
1322       view.setShowGroupConsensus(av.isShowGroupConsensus());
1323       view.setShowGroupConservation(av.isShowGroupConservation());
1324       view.setShowNPfeatureTooltip(av.isShowNPFeats());
1325       view.setShowDbRefTooltip(av.isShowDBRefs());
1326       view.setFollowHighlight(av.isFollowHighlight());
1327       view.setFollowSelection(av.followSelection);
1328       view.setIgnoreGapsinConsensus(av.isIgnoreGapsConsensus());
1329       if (av.getFeaturesDisplayed() != null)
1330       {
1331         jalview.schemabinding.version2.FeatureSettings fs = new jalview.schemabinding.version2.FeatureSettings();
1332
1333         String[] renderOrder = ap.getSeqPanel().seqCanvas
1334                 .getFeatureRenderer().getRenderOrder()
1335                 .toArray(new String[0]);
1336
1337         Vector<String> settingsAdded = new Vector<String>();
1338         if (renderOrder != null)
1339         {
1340           for (String featureType : renderOrder)
1341           {
1342             FeatureColourI fcol = ap.getSeqPanel().seqCanvas
1343                     .getFeatureRenderer()
1344                     .getFeatureStyle(featureType);
1345             Setting setting = new Setting();
1346             setting.setType(featureType);
1347             if (!fcol.isSimpleColour())
1348             {
1349               setting.setColour(fcol.getMaxColour().getRGB());
1350               setting.setMincolour(fcol.getMinColour().getRGB());
1351               setting.setMin(fcol.getMin());
1352               setting.setMax(fcol.getMax());
1353               setting.setColourByLabel(fcol.isColourByLabel());
1354               setting.setAutoScale(fcol.isAutoScaled());
1355               setting.setThreshold(fcol.getThreshold());
1356               // -1 = No threshold, 0 = Below, 1 = Above
1357               setting.setThreshstate(fcol.isAboveThreshold() ? 1
1358                       : (fcol.isBelowThreshold() ? 0 : -1));
1359             }
1360             else
1361             {
1362               setting.setColour(fcol.getColour().getRGB());
1363             }
1364
1365             setting.setDisplay(av.getFeaturesDisplayed().isVisible(
1366                     featureType));
1367             float rorder = ap.getSeqPanel().seqCanvas.getFeatureRenderer()
1368                     .getOrder(featureType);
1369             if (rorder > -1)
1370             {
1371               setting.setOrder(rorder);
1372             }
1373             fs.addSetting(setting);
1374             settingsAdded.addElement(featureType);
1375           }
1376         }
1377
1378         // is groups actually supposed to be a map here ?
1379         Iterator<String> en = ap.getSeqPanel().seqCanvas
1380                 .getFeatureRenderer()
1381                 .getFeatureGroups().iterator();
1382         Vector<String> groupsAdded = new Vector<String>();
1383         while (en.hasNext())
1384         {
1385           String grp = en.next();
1386           if (groupsAdded.contains(grp))
1387           {
1388             continue;
1389           }
1390           Group g = new Group();
1391           g.setName(grp);
1392           g.setDisplay(((Boolean) ap.getSeqPanel().seqCanvas
1393                   .getFeatureRenderer().checkGroupVisibility(grp, false))
1394                   .booleanValue());
1395           fs.addGroup(g);
1396           groupsAdded.addElement(grp);
1397         }
1398         jms.setFeatureSettings(fs);
1399       }
1400
1401       if (av.hasHiddenColumns())
1402       {
1403         if (av.getColumnSelection() == null
1404                 || av.getColumnSelection().getHiddenColumns() == null)
1405         {
1406           warn("REPORT BUG: avoided null columnselection bug (DMAM reported). Please contact Jim about this.");
1407         }
1408         else
1409         {
1410           for (int c = 0; c < av.getColumnSelection().getHiddenColumns()
1411                   .size(); c++)
1412           {
1413             int[] region = av.getColumnSelection().getHiddenColumns()
1414                     .get(c);
1415             HiddenColumns hc = new HiddenColumns();
1416             hc.setStart(region[0]);
1417             hc.setEnd(region[1]);
1418             view.addHiddenColumns(hc);
1419           }
1420         }
1421       }
1422       if (calcIdSet.size() > 0)
1423       {
1424         for (String calcId : calcIdSet)
1425         {
1426           if (calcId.trim().length() > 0)
1427           {
1428             CalcIdParam cidp = createCalcIdParam(calcId, av);
1429             // Some calcIds have no parameters.
1430             if (cidp != null)
1431             {
1432               view.addCalcIdParam(cidp);
1433             }
1434           }
1435         }
1436       }
1437
1438       jms.addViewport(view);
1439     }
1440     object.setJalviewModelSequence(jms);
1441     object.getVamsasModel().addSequenceSet(vamsasSet);
1442
1443     if (jout != null && fileName != null)
1444     {
1445       // We may not want to write the object to disk,
1446       // eg we can copy the alignViewport to a new view object
1447       // using save and then load
1448       try
1449       {
1450         System.out.println("Writing jar entry " + fileName);
1451         JarEntry entry = new JarEntry(fileName);
1452         jout.putNextEntry(entry);
1453         PrintWriter pout = new PrintWriter(new OutputStreamWriter(jout,
1454                 UTF_8));
1455         Marshaller marshaller = new Marshaller(pout);
1456         marshaller.marshal(object);
1457         pout.flush();
1458         jout.closeEntry();
1459       } catch (Exception ex)
1460       {
1461         // TODO: raise error in GUI if marshalling failed.
1462         ex.printStackTrace();
1463       }
1464     }
1465     return object;
1466   }
1467
1468   /**
1469    * Save any Varna viewers linked to this sequence. Writes an rnaViewer element
1470    * for each viewer, with
1471    * <ul>
1472    * <li>viewer geometry (position, size, split pane divider location)</li>
1473    * <li>index of the selected structure in the viewer (currently shows gapped
1474    * or ungapped)</li>
1475    * <li>the id of the annotation holding RNA secondary structure</li>
1476    * <li>(currently only one SS is shown per viewer, may be more in future)</li>
1477    * </ul>
1478    * Varna viewer state is also written out (in native Varna XML) to separate
1479    * project jar entries. A separate entry is written for each RNA structure
1480    * displayed, with the naming convention
1481    * <ul>
1482    * <li>rna_viewId_sequenceId_annotationId_[gapped|trimmed]</li>
1483    * </ul>
1484    * 
1485    * @param jout
1486    * @param jseq
1487    * @param jds
1488    * @param viewIds
1489    * @param ap
1490    * @param storeDataset
1491    */
1492   protected void saveRnaViewers(JarOutputStream jout, JSeq jseq,
1493           final SequenceI jds, List<String> viewIds, AlignmentPanel ap,
1494           boolean storeDataset)
1495   {
1496     if (Desktop.desktop == null)
1497     {
1498       return;
1499     }
1500     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1501     for (int f = frames.length - 1; f > -1; f--)
1502     {
1503       if (frames[f] instanceof AppVarna)
1504       {
1505         AppVarna varna = (AppVarna) frames[f];
1506         /*
1507          * link the sequence to every viewer that is showing it and is linked to
1508          * its alignment panel
1509          */
1510         if (varna.isListeningFor(jds) && ap == varna.getAlignmentPanel())
1511         {
1512           String viewId = varna.getViewId();
1513           RnaViewer rna = new RnaViewer();
1514           rna.setViewId(viewId);
1515           rna.setTitle(varna.getTitle());
1516           rna.setXpos(varna.getX());
1517           rna.setYpos(varna.getY());
1518           rna.setWidth(varna.getWidth());
1519           rna.setHeight(varna.getHeight());
1520           rna.setDividerLocation(varna.getDividerLocation());
1521           rna.setSelectedRna(varna.getSelectedIndex());
1522           jseq.addRnaViewer(rna);
1523
1524           /*
1525            * Store each Varna panel's state once in the project per sequence.
1526            * First time through only (storeDataset==false)
1527            */
1528           // boolean storeSessions = false;
1529           // String sequenceViewId = viewId + seqsToIds.get(jds);
1530           // if (!storeDataset && !viewIds.contains(sequenceViewId))
1531           // {
1532           // viewIds.add(sequenceViewId);
1533           // storeSessions = true;
1534           // }
1535           for (RnaModel model : varna.getModels())
1536           {
1537             if (model.seq == jds)
1538             {
1539               /*
1540                * VARNA saves each view (sequence or alignment secondary
1541                * structure, gapped or trimmed) as a separate XML file
1542                */
1543               String jarEntryName = rnaSessions.get(model);
1544               if (jarEntryName == null)
1545               {
1546
1547                 String varnaStateFile = varna.getStateInfo(model.rna);
1548                 jarEntryName = RNA_PREFIX + viewId + "_" + nextCounter();
1549                 copyFileToJar(jout, varnaStateFile, jarEntryName);
1550                 rnaSessions.put(model, jarEntryName);
1551               }
1552               SecondaryStructure ss = new SecondaryStructure();
1553               String annotationId = varna.getAnnotation(jds).annotationId;
1554               ss.setAnnotationId(annotationId);
1555               ss.setViewerState(jarEntryName);
1556               ss.setGapped(model.gapped);
1557               ss.setTitle(model.title);
1558               rna.addSecondaryStructure(ss);
1559             }
1560           }
1561         }
1562       }
1563     }
1564   }
1565
1566   /**
1567    * Copy the contents of a file to a new entry added to the output jar
1568    * 
1569    * @param jout
1570    * @param infilePath
1571    * @param jarEntryName
1572    */
1573   protected void copyFileToJar(JarOutputStream jout, String infilePath,
1574           String jarEntryName)
1575   {
1576     DataInputStream dis = null;
1577     try
1578     {
1579       File file = new File(infilePath);
1580       if (file.exists() && jout != null)
1581       {
1582         dis = new DataInputStream(new FileInputStream(file));
1583         byte[] data = new byte[(int) file.length()];
1584         dis.readFully(data);
1585         writeJarEntry(jout, jarEntryName, data);
1586       }
1587     } catch (Exception ex)
1588     {
1589       ex.printStackTrace();
1590     } finally
1591     {
1592       if (dis != null)
1593       {
1594         try
1595         {
1596           dis.close();
1597         } catch (IOException e)
1598         {
1599           // ignore
1600         }
1601       }
1602     }
1603   }
1604
1605   /**
1606    * Write the data to a new entry of given name in the output jar file
1607    * 
1608    * @param jout
1609    * @param jarEntryName
1610    * @param data
1611    * @throws IOException
1612    */
1613   protected void writeJarEntry(JarOutputStream jout, String jarEntryName,
1614           byte[] data) throws IOException
1615   {
1616     if (jout != null)
1617     {
1618       System.out.println("Writing jar entry " + jarEntryName);
1619       jout.putNextEntry(new JarEntry(jarEntryName));
1620       DataOutputStream dout = new DataOutputStream(jout);
1621       dout.write(data, 0, data.length);
1622       dout.flush();
1623       jout.closeEntry();
1624     }
1625   }
1626
1627   /**
1628    * Save the state of a structure viewer
1629    * 
1630    * @param ap
1631    * @param jds
1632    * @param pdb
1633    *          the archive XML element under which to save the state
1634    * @param entry
1635    * @param viewIds
1636    * @param matchedFile
1637    * @param viewFrame
1638    * @return
1639    */
1640   protected String saveStructureState(AlignmentPanel ap, SequenceI jds,
1641           Pdbids pdb, PDBEntry entry, List<String> viewIds,
1642           String matchedFile, StructureViewerBase viewFrame)
1643   {
1644     final AAStructureBindingModel bindingModel = viewFrame.getBinding();
1645
1646     /*
1647      * Look for any bindings for this viewer to the PDB file of interest
1648      * (including part matches excluding chain id)
1649      */
1650     for (int peid = 0; peid < bindingModel.getPdbCount(); peid++)
1651     {
1652       final PDBEntry pdbentry = bindingModel.getPdbEntry(peid);
1653       final String pdbId = pdbentry.getId();
1654       if (!pdbId.equals(entry.getId())
1655               && !(entry.getId().length() > 4 && entry.getId()
1656                       .toLowerCase().startsWith(pdbId.toLowerCase())))
1657       {
1658         /*
1659          * not interested in a binding to a different PDB entry here
1660          */
1661         continue;
1662       }
1663       if (matchedFile == null)
1664       {
1665         matchedFile = pdbentry.getFile();
1666       }
1667       else if (!matchedFile.equals(pdbentry.getFile()))
1668       {
1669         Cache.log
1670                 .warn("Probably lost some PDB-Sequence mappings for this structure file (which apparently has same PDB Entry code): "
1671                         + pdbentry.getFile());
1672       }
1673       // record the
1674       // file so we
1675       // can get at it if the ID
1676       // match is ambiguous (e.g.
1677       // 1QIP==1qipA)
1678
1679       for (int smap = 0; smap < viewFrame.getBinding().getSequence()[peid].length; smap++)
1680       {
1681         // if (jal.findIndex(jmol.jmb.sequence[peid][smap]) > -1)
1682         if (jds == viewFrame.getBinding().getSequence()[peid][smap])
1683         {
1684           StructureState state = new StructureState();
1685           state.setVisible(true);
1686           state.setXpos(viewFrame.getX());
1687           state.setYpos(viewFrame.getY());
1688           state.setWidth(viewFrame.getWidth());
1689           state.setHeight(viewFrame.getHeight());
1690           final String viewId = viewFrame.getViewId();
1691           state.setViewId(viewId);
1692           state.setAlignwithAlignPanel(viewFrame.isUsedforaligment(ap));
1693           state.setColourwithAlignPanel(viewFrame.isUsedforcolourby(ap));
1694           state.setColourByJmol(viewFrame.isColouredByViewer());
1695           state.setType(viewFrame.getViewerType().toString());
1696           pdb.addStructureState(state);
1697         }
1698       }
1699     }
1700     return matchedFile;
1701   }
1702
1703   private AnnotationColours constructAnnotationColours(
1704           AnnotationColourGradient acg, List<UserColourScheme> userColours,
1705           JalviewModelSequence jms)
1706   {
1707     AnnotationColours ac = new AnnotationColours();
1708     ac.setAboveThreshold(acg.getAboveThreshold());
1709     ac.setThreshold(acg.getAnnotationThreshold());
1710     ac.setAnnotation(acg.getAnnotation());
1711     if (acg.getBaseColour() instanceof jalview.schemes.UserColourScheme)
1712     {
1713       ac.setColourScheme(setUserColourScheme(acg.getBaseColour(),
1714               userColours, jms));
1715     }
1716     else
1717     {
1718       ac.setColourScheme(ColourSchemeProperty.getColourName(acg
1719               .getBaseColour()));
1720     }
1721
1722     ac.setMaxColour(acg.getMaxColour().getRGB());
1723     ac.setMinColour(acg.getMinColour().getRGB());
1724     ac.setPerSequence(acg.isSeqAssociated());
1725     ac.setPredefinedColours(acg.isPredefinedColours());
1726     return ac;
1727   }
1728
1729   private void storeAlignmentAnnotation(AlignmentAnnotation[] aa,
1730           IdentityHashMap<SequenceGroup, String> groupRefs,
1731           AlignmentViewport av, Set<String> calcIdSet, boolean storeDS,
1732           SequenceSet vamsasSet)
1733   {
1734
1735     for (int i = 0; i < aa.length; i++)
1736     {
1737       Annotation an = new Annotation();
1738
1739       AlignmentAnnotation annotation = aa[i];
1740       if (annotation.annotationId != null)
1741       {
1742         annotationIds.put(annotation.annotationId, annotation);
1743       }
1744
1745       an.setId(annotation.annotationId);
1746
1747       an.setVisible(annotation.visible);
1748
1749       an.setDescription(annotation.description);
1750
1751       if (annotation.sequenceRef != null)
1752       {
1753         // 2.9 JAL-1781 xref on sequence id rather than name
1754         an.setSequenceRef(seqsToIds.get(annotation.sequenceRef));
1755       }
1756       if (annotation.groupRef != null)
1757       {
1758         String groupIdr = groupRefs.get(annotation.groupRef);
1759         if (groupIdr == null)
1760         {
1761           // make a locally unique String
1762           groupRefs.put(
1763                   annotation.groupRef,
1764                   groupIdr = ("" + System.currentTimeMillis()
1765                           + annotation.groupRef.getName() + groupRefs
1766                           .size()));
1767         }
1768         an.setGroupRef(groupIdr.toString());
1769       }
1770
1771       // store all visualization attributes for annotation
1772       an.setGraphHeight(annotation.graphHeight);
1773       an.setCentreColLabels(annotation.centreColLabels);
1774       an.setScaleColLabels(annotation.scaleColLabel);
1775       an.setShowAllColLabels(annotation.showAllColLabels);
1776       an.setBelowAlignment(annotation.belowAlignment);
1777
1778       if (annotation.graph > 0)
1779       {
1780         an.setGraph(true);
1781         an.setGraphType(annotation.graph);
1782         an.setGraphGroup(annotation.graphGroup);
1783         if (annotation.getThreshold() != null)
1784         {
1785           ThresholdLine line = new ThresholdLine();
1786           line.setLabel(annotation.getThreshold().label);
1787           line.setValue(annotation.getThreshold().value);
1788           line.setColour(annotation.getThreshold().colour.getRGB());
1789           an.setThresholdLine(line);
1790         }
1791       }
1792       else
1793       {
1794         an.setGraph(false);
1795       }
1796
1797       an.setLabel(annotation.label);
1798
1799       if (annotation == av.getAlignmentQualityAnnot()
1800               || annotation == av.getAlignmentConservationAnnotation()
1801               || annotation == av.getAlignmentConsensusAnnotation()
1802               || annotation.autoCalculated)
1803       {
1804         // new way of indicating autocalculated annotation -
1805         an.setAutoCalculated(annotation.autoCalculated);
1806       }
1807       if (annotation.hasScore())
1808       {
1809         an.setScore(annotation.getScore());
1810       }
1811
1812       if (annotation.getCalcId() != null)
1813       {
1814         calcIdSet.add(annotation.getCalcId());
1815         an.setCalcId(annotation.getCalcId());
1816       }
1817       if (annotation.hasProperties())
1818       {
1819         for (String pr : annotation.getProperties())
1820         {
1821           Property prop = new Property();
1822           prop.setName(pr);
1823           prop.setValue(annotation.getProperty(pr));
1824           an.addProperty(prop);
1825         }
1826       }
1827
1828       AnnotationElement ae;
1829       if (annotation.annotations != null)
1830       {
1831         an.setScoreOnly(false);
1832         for (int a = 0; a < annotation.annotations.length; a++)
1833         {
1834           if ((annotation == null) || (annotation.annotations[a] == null))
1835           {
1836             continue;
1837           }
1838
1839           ae = new AnnotationElement();
1840           if (annotation.annotations[a].description != null)
1841           {
1842             ae.setDescription(annotation.annotations[a].description);
1843           }
1844           if (annotation.annotations[a].displayCharacter != null)
1845           {
1846             ae.setDisplayCharacter(annotation.annotations[a].displayCharacter);
1847           }
1848
1849           if (!Float.isNaN(annotation.annotations[a].value))
1850           {
1851             ae.setValue(annotation.annotations[a].value);
1852           }
1853
1854           ae.setPosition(a);
1855           if (annotation.annotations[a].secondaryStructure > ' ')
1856           {
1857             ae.setSecondaryStructure(annotation.annotations[a].secondaryStructure
1858                     + "");
1859           }
1860
1861           if (annotation.annotations[a].colour != null
1862                   && annotation.annotations[a].colour != java.awt.Color.black)
1863           {
1864             ae.setColour(annotation.annotations[a].colour.getRGB());
1865           }
1866
1867           an.addAnnotationElement(ae);
1868           if (annotation.autoCalculated)
1869           {
1870             // only write one non-null entry into the annotation row -
1871             // sufficient to get the visualization attributes necessary to
1872             // display data
1873             continue;
1874           }
1875         }
1876       }
1877       else
1878       {
1879         an.setScoreOnly(true);
1880       }
1881       if (!storeDS || (storeDS && !annotation.autoCalculated))
1882       {
1883         // skip autocalculated annotation - these are only provided for
1884         // alignments
1885         vamsasSet.addAnnotation(an);
1886       }
1887     }
1888
1889   }
1890
1891   private CalcIdParam createCalcIdParam(String calcId, AlignViewport av)
1892   {
1893     AutoCalcSetting settings = av.getCalcIdSettingsFor(calcId);
1894     if (settings != null)
1895     {
1896       CalcIdParam vCalcIdParam = new CalcIdParam();
1897       vCalcIdParam.setCalcId(calcId);
1898       vCalcIdParam.addServiceURL(settings.getServiceURI());
1899       // generic URI allowing a third party to resolve another instance of the
1900       // service used for this calculation
1901       for (String urls : settings.getServiceURLs())
1902       {
1903         vCalcIdParam.addServiceURL(urls);
1904       }
1905       vCalcIdParam.setVersion("1.0");
1906       if (settings.getPreset() != null)
1907       {
1908         WsParamSetI setting = settings.getPreset();
1909         vCalcIdParam.setName(setting.getName());
1910         vCalcIdParam.setDescription(setting.getDescription());
1911       }
1912       else
1913       {
1914         vCalcIdParam.setName("");
1915         vCalcIdParam.setDescription("Last used parameters");
1916       }
1917       // need to be able to recover 1) settings 2) user-defined presets or
1918       // recreate settings from preset 3) predefined settings provided by
1919       // service - or settings that can be transferred (or discarded)
1920       vCalcIdParam.setParameters(settings.getWsParamFile().replace("\n",
1921               "|\\n|"));
1922       vCalcIdParam.setAutoUpdate(settings.isAutoUpdate());
1923       // todo - decide if updateImmediately is needed for any projects.
1924
1925       return vCalcIdParam;
1926     }
1927     return null;
1928   }
1929
1930   private boolean recoverCalcIdParam(CalcIdParam calcIdParam,
1931           AlignViewport av)
1932   {
1933     if (calcIdParam.getVersion().equals("1.0"))
1934     {
1935       Jws2Instance service = Jws2Discoverer.getDiscoverer()
1936               .getPreferredServiceFor(calcIdParam.getServiceURL());
1937       if (service != null)
1938       {
1939         WsParamSetI parmSet = null;
1940         try
1941         {
1942           parmSet = service.getParamStore().parseServiceParameterFile(
1943                   calcIdParam.getName(), calcIdParam.getDescription(),
1944                   calcIdParam.getServiceURL(),
1945                   calcIdParam.getParameters().replace("|\\n|", "\n"));
1946         } catch (IOException x)
1947         {
1948           warn("Couldn't parse parameter data for "
1949                   + calcIdParam.getCalcId(), x);
1950           return false;
1951         }
1952         List<ArgumentI> argList = null;
1953         if (calcIdParam.getName().length() > 0)
1954         {
1955           parmSet = service.getParamStore()
1956                   .getPreset(calcIdParam.getName());
1957           if (parmSet != null)
1958           {
1959             // TODO : check we have a good match with settings in AACon -
1960             // otherwise we'll need to create a new preset
1961           }
1962         }
1963         else
1964         {
1965           argList = parmSet.getArguments();
1966           parmSet = null;
1967         }
1968         AAConSettings settings = new AAConSettings(
1969                 calcIdParam.isAutoUpdate(), service, parmSet, argList);
1970         av.setCalcIdSettingsFor(calcIdParam.getCalcId(), settings,
1971                 calcIdParam.isNeedsUpdate());
1972         return true;
1973       }
1974       else
1975       {
1976         warn("Cannot resolve a service for the parameters used in this project. Try configuring a JABAWS server.");
1977         return false;
1978       }
1979     }
1980     throw new Error(MessageManager.formatMessage(
1981             "error.unsupported_version_calcIdparam",
1982             new Object[] { calcIdParam.toString() }));
1983   }
1984
1985   /**
1986    * External mapping between jalview objects and objects yielding a valid and
1987    * unique object ID string. This is null for normal Jalview project IO, but
1988    * non-null when a jalview project is being read or written as part of a
1989    * vamsas session.
1990    */
1991   IdentityHashMap jv2vobj = null;
1992
1993   /**
1994    * Construct a unique ID for jvobj using either existing bindings or if none
1995    * exist, the result of the hashcode call for the object.
1996    * 
1997    * @param jvobj
1998    *          jalview data object
1999    * @return unique ID for referring to jvobj
2000    */
2001   private String makeHashCode(Object jvobj, String altCode)
2002   {
2003     if (jv2vobj != null)
2004     {
2005       Object id = jv2vobj.get(jvobj);
2006       if (id != null)
2007       {
2008         return id.toString();
2009       }
2010       // check string ID mappings
2011       if (jvids2vobj != null && jvobj instanceof String)
2012       {
2013         id = jvids2vobj.get(jvobj);
2014       }
2015       if (id != null)
2016       {
2017         return id.toString();
2018       }
2019       // give up and warn that something has gone wrong
2020       warn("Cannot find ID for object in external mapping : " + jvobj);
2021     }
2022     return altCode;
2023   }
2024
2025   /**
2026    * return local jalview object mapped to ID, if it exists
2027    * 
2028    * @param idcode
2029    *          (may be null)
2030    * @return null or object bound to idcode
2031    */
2032   private Object retrieveExistingObj(String idcode)
2033   {
2034     if (idcode != null && vobj2jv != null)
2035     {
2036       return vobj2jv.get(idcode);
2037     }
2038     return null;
2039   }
2040
2041   /**
2042    * binding from ID strings from external mapping table to jalview data model
2043    * objects.
2044    */
2045   private Hashtable vobj2jv;
2046
2047   private Sequence createVamsasSequence(String id, SequenceI jds)
2048   {
2049     return createVamsasSequence(true, id, jds, null);
2050   }
2051
2052   private Sequence createVamsasSequence(boolean recurse, String id,
2053           SequenceI jds, SequenceI parentseq)
2054   {
2055     Sequence vamsasSeq = new Sequence();
2056     vamsasSeq.setId(id);
2057     vamsasSeq.setName(jds.getName());
2058     vamsasSeq.setSequence(jds.getSequenceAsString());
2059     vamsasSeq.setDescription(jds.getDescription());
2060     jalview.datamodel.DBRefEntry[] dbrefs = null;
2061     if (jds.getDatasetSequence() != null)
2062     {
2063       vamsasSeq.setDsseqid(seqHash(jds.getDatasetSequence()));
2064     }
2065     else
2066     {
2067       // seqId==dsseqid so we can tell which sequences really are
2068       // dataset sequences only
2069       vamsasSeq.setDsseqid(id);
2070       dbrefs = jds.getDBRefs();
2071       if (parentseq == null)
2072       {
2073         parentseq = jds;
2074       }
2075     }
2076     if (dbrefs != null)
2077     {
2078       for (int d = 0; d < dbrefs.length; d++)
2079       {
2080         DBRef dbref = new DBRef();
2081         dbref.setSource(dbrefs[d].getSource());
2082         dbref.setVersion(dbrefs[d].getVersion());
2083         dbref.setAccessionId(dbrefs[d].getAccessionId());
2084         if (dbrefs[d].hasMap())
2085         {
2086           Mapping mp = createVamsasMapping(dbrefs[d].getMap(), parentseq,
2087                   jds, recurse);
2088           dbref.setMapping(mp);
2089         }
2090         vamsasSeq.addDBRef(dbref);
2091       }
2092     }
2093     return vamsasSeq;
2094   }
2095
2096   private Mapping createVamsasMapping(jalview.datamodel.Mapping jmp,
2097           SequenceI parentseq, SequenceI jds, boolean recurse)
2098   {
2099     Mapping mp = null;
2100     if (jmp.getMap() != null)
2101     {
2102       mp = new Mapping();
2103
2104       jalview.util.MapList mlst = jmp.getMap();
2105       List<int[]> r = mlst.getFromRanges();
2106       for (int[] range : r)
2107       {
2108         MapListFrom mfrom = new MapListFrom();
2109         mfrom.setStart(range[0]);
2110         mfrom.setEnd(range[1]);
2111         mp.addMapListFrom(mfrom);
2112       }
2113       r = mlst.getToRanges();
2114       for (int[] range : r)
2115       {
2116         MapListTo mto = new MapListTo();
2117         mto.setStart(range[0]);
2118         mto.setEnd(range[1]);
2119         mp.addMapListTo(mto);
2120       }
2121       mp.setMapFromUnit(mlst.getFromRatio());
2122       mp.setMapToUnit(mlst.getToRatio());
2123       if (jmp.getTo() != null)
2124       {
2125         MappingChoice mpc = new MappingChoice();
2126
2127         // check/create ID for the sequence referenced by getTo()
2128
2129         String jmpid = "";
2130         SequenceI ps = null;
2131         if (parentseq != jmp.getTo()
2132                 && parentseq.getDatasetSequence() != jmp.getTo())
2133         {
2134           // chaining dbref rather than a handshaking one
2135           jmpid = seqHash(ps = jmp.getTo());
2136         }
2137         else
2138         {
2139           jmpid = seqHash(ps = parentseq);
2140         }
2141         mpc.setDseqFor(jmpid);
2142         if (!seqRefIds.containsKey(mpc.getDseqFor()))
2143         {
2144           jalview.bin.Cache.log.debug("creatign new DseqFor ID");
2145           seqRefIds.put(mpc.getDseqFor(), ps);
2146         }
2147         else
2148         {
2149           jalview.bin.Cache.log.debug("reusing DseqFor ID");
2150         }
2151
2152         mp.setMappingChoice(mpc);
2153       }
2154     }
2155     return mp;
2156   }
2157
2158   String setUserColourScheme(jalview.schemes.ColourSchemeI cs,
2159           List<UserColourScheme> userColours, JalviewModelSequence jms)
2160   {
2161     String id = null;
2162     jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs;
2163     boolean newucs = false;
2164     if (!userColours.contains(ucs))
2165     {
2166       userColours.add(ucs);
2167       newucs = true;
2168     }
2169     id = "ucs" + userColours.indexOf(ucs);
2170     if (newucs)
2171     {
2172       // actually create the scheme's entry in the XML model
2173       java.awt.Color[] colours = ucs.getColours();
2174       jalview.schemabinding.version2.UserColours uc = new jalview.schemabinding.version2.UserColours();
2175       jalview.schemabinding.version2.UserColourScheme jbucs = new jalview.schemabinding.version2.UserColourScheme();
2176
2177       for (int i = 0; i < colours.length; i++)
2178       {
2179         jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
2180         col.setName(ResidueProperties.aa[i]);
2181         col.setRGB(jalview.util.Format.getHexString(colours[i]));
2182         jbucs.addColour(col);
2183       }
2184       if (ucs.getLowerCaseColours() != null)
2185       {
2186         colours = ucs.getLowerCaseColours();
2187         for (int i = 0; i < colours.length; i++)
2188         {
2189           jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
2190           col.setName(ResidueProperties.aa[i].toLowerCase());
2191           col.setRGB(jalview.util.Format.getHexString(colours[i]));
2192           jbucs.addColour(col);
2193         }
2194       }
2195
2196       uc.setId(id);
2197       uc.setUserColourScheme(jbucs);
2198       jms.addUserColours(uc);
2199     }
2200
2201     return id;
2202   }
2203
2204   jalview.schemes.UserColourScheme getUserColourScheme(
2205           JalviewModelSequence jms, String id)
2206   {
2207     UserColours[] uc = jms.getUserColours();
2208     UserColours colours = null;
2209
2210     for (int i = 0; i < uc.length; i++)
2211     {
2212       if (uc[i].getId().equals(id))
2213       {
2214         colours = uc[i];
2215
2216         break;
2217       }
2218     }
2219
2220     java.awt.Color[] newColours = new java.awt.Color[24];
2221
2222     for (int i = 0; i < 24; i++)
2223     {
2224       newColours[i] = new java.awt.Color(Integer.parseInt(colours
2225               .getUserColourScheme().getColour(i).getRGB(), 16));
2226     }
2227
2228     jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
2229             newColours);
2230
2231     if (colours.getUserColourScheme().getColourCount() > 24)
2232     {
2233       newColours = new java.awt.Color[23];
2234       for (int i = 0; i < 23; i++)
2235       {
2236         newColours[i] = new java.awt.Color(Integer.parseInt(colours
2237                 .getUserColourScheme().getColour(i + 24).getRGB(), 16));
2238       }
2239       ucs.setLowerCaseColours(newColours);
2240     }
2241
2242     return ucs;
2243   }
2244
2245   /**
2246    * contains last error message (if any) encountered by XML loader.
2247    */
2248   String errorMessage = null;
2249
2250   /**
2251    * flag to control whether the Jalview2XML_V1 parser should be deferred to if
2252    * exceptions are raised during project XML parsing
2253    */
2254   public boolean attemptversion1parse = true;
2255
2256   /**
2257    * Load a jalview project archive from a jar file
2258    * 
2259    * @param file
2260    *          - HTTP URL or filename
2261    */
2262   public AlignFrame loadJalviewAlign(final String file)
2263   {
2264
2265     jalview.gui.AlignFrame af = null;
2266
2267     try
2268     {
2269       // create list to store references for any new Jmol viewers created
2270       newStructureViewers = new Vector<JalviewStructureDisplayI>();
2271       // UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING
2272       // Workaround is to make sure caller implements the JarInputStreamProvider
2273       // interface
2274       // so we can re-open the jar input stream for each entry.
2275
2276       jarInputStreamProvider jprovider = createjarInputStreamProvider(file);
2277       af = loadJalviewAlign(jprovider);
2278
2279     } catch (MalformedURLException e)
2280     {
2281       errorMessage = "Invalid URL format for '" + file + "'";
2282       reportErrors();
2283     } finally
2284     {
2285       try
2286       {
2287         SwingUtilities.invokeAndWait(new Runnable()
2288         {
2289           @Override
2290           public void run()
2291           {
2292             setLoadingFinishedForNewStructureViewers();
2293           };
2294         });
2295       } catch (Exception x)
2296       {
2297         System.err.println("Error loading alignment: " + x.getMessage());
2298       }
2299     }
2300     return af;
2301   }
2302
2303   private jarInputStreamProvider createjarInputStreamProvider(
2304           final String file) throws MalformedURLException
2305   {
2306     URL url = null;
2307     errorMessage = null;
2308     uniqueSetSuffix = null;
2309     seqRefIds = null;
2310     viewportsAdded.clear();
2311     frefedSequence = null;
2312
2313     if (file.startsWith("http://"))
2314     {
2315       url = new URL(file);
2316     }
2317     final URL _url = url;
2318     return new jarInputStreamProvider()
2319     {
2320
2321       @Override
2322       public JarInputStream getJarInputStream() throws IOException
2323       {
2324         if (_url != null)
2325         {
2326           return new JarInputStream(_url.openStream());
2327         }
2328         else
2329         {
2330           return new JarInputStream(new FileInputStream(file));
2331         }
2332       }
2333
2334       @Override
2335       public String getFilename()
2336       {
2337         return file;
2338       }
2339     };
2340   }
2341
2342   /**
2343    * Recover jalview session from a jalview project archive. Caller may
2344    * initialise uniqueSetSuffix, seqRefIds, viewportsAdded and frefedSequence
2345    * themselves. Any null fields will be initialised with default values,
2346    * non-null fields are left alone.
2347    * 
2348    * @param jprovider
2349    * @return
2350    */
2351   public AlignFrame loadJalviewAlign(final jarInputStreamProvider jprovider)
2352   {
2353     errorMessage = null;
2354     if (uniqueSetSuffix == null)
2355     {
2356       uniqueSetSuffix = System.currentTimeMillis() % 100000 + "";
2357     }
2358     if (seqRefIds == null)
2359     {
2360       initSeqRefs();
2361     }
2362     AlignFrame af = null, _af = null;
2363     IdentityHashMap<AlignmentI, AlignmentI> importedDatasets = new IdentityHashMap<AlignmentI, AlignmentI>();
2364     Map<String, AlignFrame> gatherToThisFrame = new HashMap<String, AlignFrame>();
2365     final String file = jprovider.getFilename();
2366     try
2367     {
2368       JarInputStream jin = null;
2369       JarEntry jarentry = null;
2370       int entryCount = 1;
2371
2372       do
2373       {
2374         jin = jprovider.getJarInputStream();
2375         for (int i = 0; i < entryCount; i++)
2376         {
2377           jarentry = jin.getNextJarEntry();
2378         }
2379
2380         if (jarentry != null && jarentry.getName().endsWith(".xml"))
2381         {
2382           InputStreamReader in = new InputStreamReader(jin, UTF_8);
2383           JalviewModel object = new JalviewModel();
2384
2385           Unmarshaller unmar = new Unmarshaller(object);
2386           unmar.setValidation(false);
2387           object = (JalviewModel) unmar.unmarshal(in);
2388           if (true) // !skipViewport(object))
2389           {
2390             _af = loadFromObject(object, file, true, jprovider);
2391             if (_af != null
2392                     && object.getJalviewModelSequence().getViewportCount() > 0)
2393             {
2394               if (af == null)
2395               {
2396                 // store a reference to the first view
2397                 af = _af;
2398               }
2399               if (_af.viewport.isGatherViewsHere())
2400               {
2401                 // if this is a gathered view, keep its reference since
2402                 // after gathering views, only this frame will remain
2403                 af = _af;
2404                 gatherToThisFrame.put(_af.viewport.getSequenceSetId(), _af);
2405               }
2406               // Save dataset to register mappings once all resolved
2407               importedDatasets.put(af.viewport.getAlignment().getDataset(),
2408                       af.viewport.getAlignment().getDataset());
2409             }
2410           }
2411           entryCount++;
2412         }
2413         else if (jarentry != null)
2414         {
2415           // Some other file here.
2416           entryCount++;
2417         }
2418       } while (jarentry != null);
2419       resolveFrefedSequences();
2420     } catch (IOException ex)
2421     {
2422       ex.printStackTrace();
2423       errorMessage = "Couldn't locate Jalview XML file : " + file;
2424       System.err.println("Exception whilst loading jalview XML file : "
2425               + ex + "\n");
2426     } catch (Exception ex)
2427     {
2428       System.err.println("Parsing as Jalview Version 2 file failed.");
2429       ex.printStackTrace(System.err);
2430       if (attemptversion1parse)
2431       {
2432         // Is Version 1 Jar file?
2433         try
2434         {
2435           af = new Jalview2XML_V1(raiseGUI).LoadJalviewAlign(jprovider);
2436         } catch (Exception ex2)
2437         {
2438           System.err.println("Exception whilst loading as jalviewXMLV1:");
2439           ex2.printStackTrace();
2440           af = null;
2441         }
2442       }
2443       if (Desktop.instance != null)
2444       {
2445         Desktop.instance.stopLoading();
2446       }
2447       if (af != null)
2448       {
2449         System.out.println("Successfully loaded archive file");
2450         return af;
2451       }
2452       ex.printStackTrace();
2453
2454       System.err.println("Exception whilst loading jalview XML file : "
2455               + ex + "\n");
2456     } catch (OutOfMemoryError e)
2457     {
2458       // Don't use the OOM Window here
2459       errorMessage = "Out of memory loading jalview XML file";
2460       System.err.println("Out of memory whilst loading jalview XML file");
2461       e.printStackTrace();
2462     }
2463
2464     /*
2465      * Regather multiple views (with the same sequence set id) to the frame (if
2466      * any) that is flagged as the one to gather to, i.e. convert them to tabbed
2467      * views instead of separate frames. Note this doesn't restore a state where
2468      * some expanded views in turn have tabbed views - the last "first tab" read
2469      * in will play the role of gatherer for all.
2470      */
2471     for (AlignFrame fr : gatherToThisFrame.values())
2472     {
2473       Desktop.instance.gatherViews(fr);
2474     }
2475
2476     restoreSplitFrames();
2477     for (AlignmentI ds : importedDatasets.keySet())
2478     {
2479       if (ds.getCodonFrames() != null)
2480       {
2481         StructureSelectionManager.getStructureSelectionManager(
2482                 Desktop.instance).registerMappings(ds.getCodonFrames());
2483       }
2484     }
2485     if (errorMessage != null)
2486     {
2487       reportErrors();
2488     }
2489
2490     if (Desktop.instance != null)
2491     {
2492       Desktop.instance.stopLoading();
2493     }
2494
2495     return af;
2496   }
2497
2498   /**
2499    * Try to reconstruct and display SplitFrame windows, where each contains
2500    * complementary dna and protein alignments. Done by pairing up AlignFrame
2501    * objects (created earlier) which have complementary viewport ids associated.
2502    */
2503   protected void restoreSplitFrames()
2504   {
2505     List<SplitFrame> gatherTo = new ArrayList<SplitFrame>();
2506     List<AlignFrame> addedToSplitFrames = new ArrayList<AlignFrame>();
2507     Map<String, AlignFrame> dna = new HashMap<String, AlignFrame>();
2508
2509     /*
2510      * Identify the DNA alignments
2511      */
2512     for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
2513             .entrySet())
2514     {
2515       AlignFrame af = candidate.getValue();
2516       if (af.getViewport().getAlignment().isNucleotide())
2517       {
2518         dna.put(candidate.getKey().getId(), af);
2519       }
2520     }
2521
2522     /*
2523      * Try to match up the protein complements
2524      */
2525     for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
2526             .entrySet())
2527     {
2528       AlignFrame af = candidate.getValue();
2529       if (!af.getViewport().getAlignment().isNucleotide())
2530       {
2531         String complementId = candidate.getKey().getComplementId();
2532         // only non-null complements should be in the Map
2533         if (complementId != null && dna.containsKey(complementId))
2534         {
2535           final AlignFrame dnaFrame = dna.get(complementId);
2536           SplitFrame sf = createSplitFrame(dnaFrame, af);
2537           addedToSplitFrames.add(dnaFrame);
2538           addedToSplitFrames.add(af);
2539           if (af.viewport.isGatherViewsHere())
2540           {
2541             gatherTo.add(sf);
2542           }
2543         }
2544       }
2545     }
2546
2547     /*
2548      * Open any that we failed to pair up (which shouldn't happen!) as
2549      * standalone AlignFrame's.
2550      */
2551     for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
2552             .entrySet())
2553     {
2554       AlignFrame af = candidate.getValue();
2555       if (!addedToSplitFrames.contains(af))
2556       {
2557         Viewport view = candidate.getKey();
2558         Desktop.addInternalFrame(af, view.getTitle(), view.getWidth(),
2559                 view.getHeight());
2560         System.err.println("Failed to restore view " + view.getTitle()
2561                 + " to split frame");
2562       }
2563     }
2564
2565     /*
2566      * Gather back into tabbed views as flagged.
2567      */
2568     for (SplitFrame sf : gatherTo)
2569     {
2570       Desktop.instance.gatherViews(sf);
2571     }
2572
2573     splitFrameCandidates.clear();
2574   }
2575
2576   /**
2577    * Construct and display one SplitFrame holding DNA and protein alignments.
2578    * 
2579    * @param dnaFrame
2580    * @param proteinFrame
2581    * @return
2582    */
2583   protected SplitFrame createSplitFrame(AlignFrame dnaFrame,
2584           AlignFrame proteinFrame)
2585   {
2586     SplitFrame splitFrame = new SplitFrame(dnaFrame, proteinFrame);
2587     String title = MessageManager.getString("label.linked_view_title");
2588     int width = (int) dnaFrame.getBounds().getWidth();
2589     int height = (int) (dnaFrame.getBounds().getHeight()
2590             + proteinFrame.getBounds().getHeight() + 50);
2591
2592     /*
2593      * SplitFrame location is saved to both enclosed frames
2594      */
2595     splitFrame.setLocation(dnaFrame.getX(), dnaFrame.getY());
2596     Desktop.addInternalFrame(splitFrame, title, width, height);
2597
2598     /*
2599      * And compute cDNA consensus (couldn't do earlier with consensus as
2600      * mappings were not yet present)
2601      */
2602     proteinFrame.viewport.alignmentChanged(proteinFrame.alignPanel);
2603
2604     return splitFrame;
2605   }
2606
2607   /**
2608    * check errorMessage for a valid error message and raise an error box in the
2609    * GUI or write the current errorMessage to stderr and then clear the error
2610    * state.
2611    */
2612   protected void reportErrors()
2613   {
2614     reportErrors(false);
2615   }
2616
2617   protected void reportErrors(final boolean saving)
2618   {
2619     if (errorMessage != null)
2620     {
2621       final String finalErrorMessage = errorMessage;
2622       if (raiseGUI)
2623       {
2624         javax.swing.SwingUtilities.invokeLater(new Runnable()
2625         {
2626           @Override
2627           public void run()
2628           {
2629             JOptionPane.showInternalMessageDialog(Desktop.desktop,
2630                     finalErrorMessage, "Error "
2631                             + (saving ? "saving" : "loading")
2632                             + " Jalview file", JOptionPane.WARNING_MESSAGE);
2633           }
2634         });
2635       }
2636       else
2637       {
2638         System.err.println("Problem loading Jalview file: " + errorMessage);
2639       }
2640     }
2641     errorMessage = null;
2642   }
2643
2644   Map<String, String> alreadyLoadedPDB = new HashMap<String, String>();
2645
2646   /**
2647    * when set, local views will be updated from view stored in JalviewXML
2648    * Currently (28th Sep 2008) things will go horribly wrong in vamsas document
2649    * sync if this is set to true.
2650    */
2651   private final boolean updateLocalViews = false;
2652
2653   /**
2654    * Returns the path to a temporary file holding the PDB file for the given PDB
2655    * id. The first time of asking, searches for a file of that name in the
2656    * Jalview project jar, and copies it to a new temporary file. Any repeat
2657    * requests just return the path to the file previously created.
2658    * 
2659    * @param jprovider
2660    * @param pdbId
2661    * @return
2662    */
2663   String loadPDBFile(jarInputStreamProvider jprovider, String pdbId)
2664   {
2665     if (alreadyLoadedPDB.containsKey(pdbId))
2666     {
2667       return alreadyLoadedPDB.get(pdbId).toString();
2668     }
2669
2670     String tempFile = copyJarEntry(jprovider, pdbId, "jalview_pdb");
2671     if (tempFile != null)
2672     {
2673       alreadyLoadedPDB.put(pdbId, tempFile);
2674     }
2675     return tempFile;
2676   }
2677
2678   /**
2679    * Copies the jar entry of given name to a new temporary file and returns the
2680    * path to the file, or null if the entry is not found.
2681    * 
2682    * @param jprovider
2683    * @param jarEntryName
2684    * @param prefix
2685    *          a prefix for the temporary file name, must be at least three
2686    *          characters long
2687    * @return
2688    */
2689   protected String copyJarEntry(jarInputStreamProvider jprovider,
2690           String jarEntryName, String prefix)
2691   {
2692     BufferedReader in = null;
2693     PrintWriter out = null;
2694
2695     try
2696     {
2697       JarInputStream jin = jprovider.getJarInputStream();
2698       /*
2699        * if (jprovider.startsWith("http://")) { jin = new JarInputStream(new
2700        * URL(jprovider).openStream()); } else { jin = new JarInputStream(new
2701        * FileInputStream(jprovider)); }
2702        */
2703
2704       JarEntry entry = null;
2705       do
2706       {
2707         entry = jin.getNextJarEntry();
2708       } while (entry != null && !entry.getName().equals(jarEntryName));
2709       if (entry != null)
2710       {
2711         in = new BufferedReader(new InputStreamReader(jin, UTF_8));
2712         File outFile = File.createTempFile(prefix, ".tmp");
2713         outFile.deleteOnExit();
2714         out = new PrintWriter(new FileOutputStream(outFile));
2715         String data;
2716
2717         while ((data = in.readLine()) != null)
2718         {
2719           out.println(data);
2720         }
2721         out.flush();
2722         String t = outFile.getAbsolutePath();
2723         return t;
2724       }
2725       else
2726       {
2727         warn("Couldn't find entry in Jalview Jar for " + jarEntryName);
2728       }
2729     } catch (Exception ex)
2730     {
2731       ex.printStackTrace();
2732     } finally
2733     {
2734       if (in != null)
2735       {
2736         try
2737         {
2738           in.close();
2739         } catch (IOException e)
2740         {
2741           // ignore
2742         }
2743       }
2744       if (out != null)
2745       {
2746         out.close();
2747       }
2748     }
2749
2750     return null;
2751   }
2752
2753   private class JvAnnotRow
2754   {
2755     public JvAnnotRow(int i, AlignmentAnnotation jaa)
2756     {
2757       order = i;
2758       template = jaa;
2759     }
2760
2761     /**
2762      * persisted version of annotation row from which to take vis properties
2763      */
2764     public jalview.datamodel.AlignmentAnnotation template;
2765
2766     /**
2767      * original position of the annotation row in the alignment
2768      */
2769     public int order;
2770   }
2771
2772   /**
2773    * Load alignment frame from jalview XML DOM object
2774    * 
2775    * @param object
2776    *          DOM
2777    * @param file
2778    *          filename source string
2779    * @param loadTreesAndStructures
2780    *          when false only create Viewport
2781    * @param jprovider
2782    *          data source provider
2783    * @return alignment frame created from view stored in DOM
2784    */
2785   AlignFrame loadFromObject(JalviewModel object, String file,
2786           boolean loadTreesAndStructures, jarInputStreamProvider jprovider)
2787   {
2788     SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0);
2789     Sequence[] vamsasSeq = vamsasSet.getSequence();
2790
2791     JalviewModelSequence jms = object.getJalviewModelSequence();
2792
2793     Viewport view = (jms.getViewportCount() > 0) ? jms.getViewport(0)
2794             : null;
2795
2796     // ////////////////////////////////
2797     // LOAD SEQUENCES
2798
2799     List<SequenceI> hiddenSeqs = null;
2800
2801
2802     List<SequenceI> tmpseqs = new ArrayList<SequenceI>();
2803
2804     boolean multipleView = false;
2805     SequenceI referenceseqForView = null;
2806     JSeq[] jseqs = object.getJalviewModelSequence().getJSeq();
2807     int vi = 0; // counter in vamsasSeq array
2808     for (int i = 0; i < jseqs.length; i++)
2809     {
2810       String seqId = jseqs[i].getId();
2811
2812       SequenceI tmpSeq = seqRefIds.get(seqId);
2813       if (tmpSeq != null)
2814       {
2815         if (!incompleteSeqs.containsKey(seqId))
2816         {
2817           // may not need this check, but keep it for at least 2.9,1 release
2818           if (tmpSeq.getStart()!=jseqs[i].getStart() || tmpSeq.getEnd()!=jseqs[i].getEnd())
2819           { 
2820             System.err
2821                     .println("Warning JAL-2154 regression: updating start/end for sequence "
2822                             + tmpSeq.toString() + " to " + jseqs[i]);
2823           }
2824         } else {
2825           incompleteSeqs.remove(seqId);
2826         }
2827         if (vamsasSeq.length > vi && vamsasSeq[vi].getId().equals(seqId))
2828         {
2829           // most likely we are reading a dataset XML document so
2830           // update from vamsasSeq section of XML for this sequence
2831           tmpSeq.setName(vamsasSeq[vi].getName());
2832           tmpSeq.setDescription(vamsasSeq[vi].getDescription());
2833           tmpSeq.setSequence(vamsasSeq[vi].getSequence());
2834           vi++;
2835         }
2836         else
2837         {
2838           // reading multiple views, so vamsasSeq set is a subset of JSeq
2839           multipleView = true;
2840         }
2841         tmpSeq.setStart(jseqs[i].getStart());
2842         tmpSeq.setEnd(jseqs[i].getEnd());
2843         tmpseqs.add(tmpSeq);
2844       }
2845       else
2846       {
2847         tmpSeq = new jalview.datamodel.Sequence(vamsasSeq[vi].getName(),
2848                 vamsasSeq[vi].getSequence());
2849         tmpSeq.setDescription(vamsasSeq[vi].getDescription());
2850         tmpSeq.setStart(jseqs[i].getStart());
2851         tmpSeq.setEnd(jseqs[i].getEnd());
2852         tmpSeq.setVamsasId(uniqueSetSuffix + seqId);
2853         seqRefIds.put(vamsasSeq[vi].getId(), tmpSeq);
2854         tmpseqs.add(tmpSeq);
2855         vi++;
2856       }
2857
2858       if (jseqs[i].hasViewreference() && jseqs[i].getViewreference())
2859       {
2860         referenceseqForView = tmpseqs.get(tmpseqs.size() - 1);
2861       }
2862
2863       if (jseqs[i].getHidden())
2864       {
2865         if (hiddenSeqs == null)
2866         {
2867           hiddenSeqs = new ArrayList<SequenceI>();
2868         }
2869
2870         hiddenSeqs.add(tmpSeq);
2871       }
2872     }
2873
2874     // /
2875     // Create the alignment object from the sequence set
2876     // ///////////////////////////////
2877     SequenceI[] orderedSeqs = tmpseqs
2878             .toArray(new SequenceI[tmpseqs.size()]);
2879
2880     AlignmentI al = null;
2881     // so we must create or recover the dataset alignment before going further
2882     // ///////////////////////////////
2883     if (vamsasSet.getDatasetId() == null || vamsasSet.getDatasetId() == "")
2884     {
2885       // older jalview projects do not have a dataset - so creat alignment and
2886       // dataset
2887       al = new Alignment(orderedSeqs);
2888       al.setDataset(null);
2889     }
2890     else
2891     {
2892       boolean isdsal = object.getJalviewModelSequence().getViewportCount() == 0;
2893       if (isdsal)
2894       {
2895         // we are importing a dataset record, so
2896         // recover reference to an alignment already materialsed as dataset
2897         al = getDatasetFor(vamsasSet.getDatasetId());
2898       }
2899       if (al == null)
2900       {
2901         // materialse the alignment
2902         al = new Alignment(orderedSeqs);
2903       }
2904       if (isdsal)
2905       {
2906         addDatasetRef(vamsasSet.getDatasetId(), al);
2907       }
2908
2909       // finally, verify all data in vamsasSet is actually present in al
2910       // passing on flag indicating if it is actually a stored dataset
2911       recoverDatasetFor(vamsasSet, al, isdsal);
2912     }
2913
2914     if (referenceseqForView != null)
2915     {
2916       al.setSeqrep(referenceseqForView);
2917     }
2918     // / Add the alignment properties
2919     for (int i = 0; i < vamsasSet.getSequenceSetPropertiesCount(); i++)
2920     {
2921       SequenceSetProperties ssp = vamsasSet.getSequenceSetProperties(i);
2922       al.setProperty(ssp.getKey(), ssp.getValue());
2923     }
2924
2925     // ///////////////////////////////
2926
2927     Hashtable pdbloaded = new Hashtable(); // TODO nothing writes to this??
2928     if (!multipleView)
2929     {
2930       // load sequence features, database references and any associated PDB
2931       // structures for the alignment
2932       //
2933       // prior to 2.10, this part would only be executed the first time a
2934       // sequence was encountered, but not afterwards.
2935       // now, for 2.10 projects, this is also done if the xml doc includes
2936       // dataset sequences not actually present in any particular view.
2937       //
2938       for (int i = 0; i < vamsasSeq.length; i++)
2939       {
2940         if (jseqs[i].getFeaturesCount() > 0)
2941         {
2942           Features[] features = jseqs[i].getFeatures();
2943           for (int f = 0; f < features.length; f++)
2944           {
2945             jalview.datamodel.SequenceFeature sf = new jalview.datamodel.SequenceFeature(
2946                     features[f].getType(), features[f].getDescription(),
2947                     features[f].getStatus(), features[f].getBegin(),
2948                     features[f].getEnd(), features[f].getFeatureGroup());
2949
2950             sf.setScore(features[f].getScore());
2951             for (int od = 0; od < features[f].getOtherDataCount(); od++)
2952             {
2953               OtherData keyValue = features[f].getOtherData(od);
2954               if (keyValue.getKey().startsWith("LINK"))
2955               {
2956                 sf.addLink(keyValue.getValue());
2957               }
2958               else
2959               {
2960                 sf.setValue(keyValue.getKey(), keyValue.getValue());
2961               }
2962
2963             }
2964             // adds feature to datasequence's feature set (since Jalview 2.10)
2965             al.getSequenceAt(i).addSequenceFeature(sf);
2966           }
2967         }
2968         if (vamsasSeq[i].getDBRefCount() > 0)
2969         {
2970           // adds dbrefs to datasequence's set (since Jalview 2.10)
2971           addDBRefs(
2972                   al.getSequenceAt(i).getDatasetSequence() == null ? al.getSequenceAt(i)
2973                           : al.getSequenceAt(i).getDatasetSequence(),
2974                   vamsasSeq[i]);
2975         }
2976         if (jseqs[i].getPdbidsCount() > 0)
2977         {
2978           Pdbids[] ids = jseqs[i].getPdbids();
2979           for (int p = 0; p < ids.length; p++)
2980           {
2981             jalview.datamodel.PDBEntry entry = new jalview.datamodel.PDBEntry();
2982             entry.setId(ids[p].getId());
2983             if (ids[p].getType() != null)
2984             {
2985               if (ids[p].getType().equalsIgnoreCase("PDB"))
2986               {
2987                 entry.setType(PDBEntry.Type.PDB);
2988               }
2989               else
2990               {
2991                 entry.setType(PDBEntry.Type.FILE);
2992               }
2993             }
2994             if (ids[p].getFile() != null)
2995             {
2996               if (!pdbloaded.containsKey(ids[p].getFile()))
2997               {
2998                 entry.setFile(loadPDBFile(jprovider, ids[p].getId()));
2999               }
3000               else
3001               {
3002                 entry.setFile(pdbloaded.get(ids[p].getId()).toString());
3003               }
3004             }
3005             if (ids[p].getPdbentryItem() != null)
3006             {
3007               entry.setProperty(new Hashtable());
3008               for (PdbentryItem item : ids[p].getPdbentryItem())
3009               {
3010                 for (Property pr : item.getProperty())
3011                 {
3012                   entry.getProperty().put(pr.getName(), pr.getValue());
3013                 }
3014               }
3015             }
3016             StructureSelectionManager.getStructureSelectionManager(
3017                     Desktop.instance).registerPDBEntry(entry);
3018             // adds PDBEntry to datasequence's set (since Jalview 2.10)
3019             if (al.getSequenceAt(i).getDatasetSequence() != null)
3020             {
3021               al.getSequenceAt(i).getDatasetSequence().addPDBId(entry);
3022             }
3023             else
3024             {
3025               al.getSequenceAt(i).addPDBId(entry);
3026             }
3027           }
3028         }
3029       }
3030     } // end !multipleview
3031
3032     // ///////////////////////////////
3033     // LOAD SEQUENCE MAPPINGS
3034
3035     if (vamsasSet.getAlcodonFrameCount() > 0)
3036     {
3037       // TODO Potentially this should only be done once for all views of an
3038       // alignment
3039       AlcodonFrame[] alc = vamsasSet.getAlcodonFrame();
3040       for (int i = 0; i < alc.length; i++)
3041       {
3042         AlignedCodonFrame cf = new AlignedCodonFrame();
3043         if (alc[i].getAlcodMapCount() > 0)
3044         {
3045           AlcodMap[] maps = alc[i].getAlcodMap();
3046           for (int m = 0; m < maps.length; m++)
3047           {
3048             SequenceI dnaseq = seqRefIds.get(maps[m].getDnasq());
3049             // Load Mapping
3050             jalview.datamodel.Mapping mapping = null;
3051             // attach to dna sequence reference.
3052             if (maps[m].getMapping() != null)
3053             {
3054               mapping = addMapping(maps[m].getMapping());
3055               if (dnaseq != null && mapping.getTo() != null)
3056               {
3057                 cf.addMap(dnaseq, mapping.getTo(), mapping.getMap());
3058               }
3059               else
3060               {
3061                 // defer to later
3062                 frefedSequence.add(newAlcodMapRef(maps[m].getDnasq(), cf,
3063                         mapping));
3064               }
3065             }
3066           }
3067           al.addCodonFrame(cf);
3068         }
3069       }
3070     }
3071
3072     // ////////////////////////////////
3073     // LOAD ANNOTATIONS
3074     List<JvAnnotRow> autoAlan = new ArrayList<JvAnnotRow>();
3075
3076     /*
3077      * store any annotations which forward reference a group's ID
3078      */
3079     Map<String, List<AlignmentAnnotation>> groupAnnotRefs = new Hashtable<String, List<AlignmentAnnotation>>();
3080
3081     if (vamsasSet.getAnnotationCount() > 0)
3082     {
3083       Annotation[] an = vamsasSet.getAnnotation();
3084
3085       for (int i = 0; i < an.length; i++)
3086       {
3087         Annotation annotation = an[i];
3088
3089         /**
3090          * test if annotation is automatically calculated for this view only
3091          */
3092         boolean autoForView = false;
3093         if (annotation.getLabel().equals("Quality")
3094                 || annotation.getLabel().equals("Conservation")
3095                 || annotation.getLabel().equals("Consensus"))
3096         {
3097           // Kludge for pre 2.5 projects which lacked the autocalculated flag
3098           autoForView = true;
3099           if (!annotation.hasAutoCalculated())
3100           {
3101             annotation.setAutoCalculated(true);
3102           }
3103         }
3104         if (autoForView
3105                 || (annotation.hasAutoCalculated() && annotation
3106                         .isAutoCalculated()))
3107         {
3108           // remove ID - we don't recover annotation from other views for
3109           // view-specific annotation
3110           annotation.setId(null);
3111         }
3112
3113         // set visiblity for other annotation in this view
3114         String annotationId = annotation.getId();
3115         if (annotationId != null && annotationIds.containsKey(annotationId))
3116         {
3117           AlignmentAnnotation jda = annotationIds.get(annotationId);
3118           // in principle Visible should always be true for annotation displayed
3119           // in multiple views
3120           if (annotation.hasVisible())
3121           {
3122             jda.visible = annotation.getVisible();
3123           }
3124
3125           al.addAnnotation(jda);
3126
3127           continue;
3128         }
3129         // Construct new annotation from model.
3130         AnnotationElement[] ae = annotation.getAnnotationElement();
3131         jalview.datamodel.Annotation[] anot = null;
3132         java.awt.Color firstColour = null;
3133         int anpos;
3134         if (!annotation.getScoreOnly())
3135         {
3136           anot = new jalview.datamodel.Annotation[al.getWidth()];
3137           for (int aa = 0; aa < ae.length && aa < anot.length; aa++)
3138           {
3139             anpos = ae[aa].getPosition();
3140
3141             if (anpos >= anot.length)
3142             {
3143               continue;
3144             }
3145
3146             anot[anpos] = new jalview.datamodel.Annotation(
3147
3148             ae[aa].getDisplayCharacter(), ae[aa].getDescription(),
3149                     (ae[aa].getSecondaryStructure() == null || ae[aa]
3150                             .getSecondaryStructure().length() == 0) ? ' '
3151                             : ae[aa].getSecondaryStructure().charAt(0),
3152                     ae[aa].getValue()
3153
3154             );
3155             // JBPNote: Consider verifying dataflow for IO of secondary
3156             // structure annotation read from Stockholm files
3157             // this was added to try to ensure that
3158             // if (anot[ae[aa].getPosition()].secondaryStructure>' ')
3159             // {
3160             // anot[ae[aa].getPosition()].displayCharacter = "";
3161             // }
3162             anot[anpos].colour = new java.awt.Color(ae[aa].getColour());
3163             if (firstColour == null)
3164             {
3165               firstColour = anot[anpos].colour;
3166             }
3167           }
3168         }
3169         jalview.datamodel.AlignmentAnnotation jaa = null;
3170
3171         if (annotation.getGraph())
3172         {
3173           float llim = 0, hlim = 0;
3174           // if (autoForView || an[i].isAutoCalculated()) {
3175           // hlim=11f;
3176           // }
3177           jaa = new jalview.datamodel.AlignmentAnnotation(
3178                   annotation.getLabel(), annotation.getDescription(), anot,
3179                   llim, hlim, annotation.getGraphType());
3180
3181           jaa.graphGroup = annotation.getGraphGroup();
3182           jaa._linecolour = firstColour;
3183           if (annotation.getThresholdLine() != null)
3184           {
3185             jaa.setThreshold(new jalview.datamodel.GraphLine(annotation
3186                     .getThresholdLine().getValue(), annotation
3187                     .getThresholdLine().getLabel(), new java.awt.Color(
3188                     annotation.getThresholdLine().getColour())));
3189
3190           }
3191           if (autoForView || annotation.isAutoCalculated())
3192           {
3193             // Hardwire the symbol display line to ensure that labels for
3194             // histograms are displayed
3195             jaa.hasText = true;
3196           }
3197         }
3198         else
3199         {
3200           jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
3201                   an[i].getDescription(), anot);
3202           jaa._linecolour = firstColour;
3203         }
3204         // register new annotation
3205         if (an[i].getId() != null)
3206         {
3207           annotationIds.put(an[i].getId(), jaa);
3208           jaa.annotationId = an[i].getId();
3209         }
3210         // recover sequence association
3211         String sequenceRef = an[i].getSequenceRef();
3212         if (sequenceRef != null)
3213         {
3214           // from 2.9 sequenceRef is to sequence id (JAL-1781)
3215           SequenceI sequence = seqRefIds.get(sequenceRef);
3216           if (sequence == null)
3217           {
3218             // in pre-2.9 projects sequence ref is to sequence name
3219             sequence = al.findName(sequenceRef);
3220           }
3221           if (sequence != null)
3222           {
3223             jaa.createSequenceMapping(sequence, 1, true);
3224             sequence.addAlignmentAnnotation(jaa);
3225           }
3226         }
3227         // and make a note of any group association
3228         if (an[i].getGroupRef() != null && an[i].getGroupRef().length() > 0)
3229         {
3230           List<jalview.datamodel.AlignmentAnnotation> aal = groupAnnotRefs
3231                   .get(an[i].getGroupRef());
3232           if (aal == null)
3233           {
3234             aal = new ArrayList<jalview.datamodel.AlignmentAnnotation>();
3235             groupAnnotRefs.put(an[i].getGroupRef(), aal);
3236           }
3237           aal.add(jaa);
3238         }
3239
3240         if (an[i].hasScore())
3241         {
3242           jaa.setScore(an[i].getScore());
3243         }
3244         if (an[i].hasVisible())
3245         {
3246           jaa.visible = an[i].getVisible();
3247         }
3248
3249         if (an[i].hasCentreColLabels())
3250         {
3251           jaa.centreColLabels = an[i].getCentreColLabels();
3252         }
3253
3254         if (an[i].hasScaleColLabels())
3255         {
3256           jaa.scaleColLabel = an[i].getScaleColLabels();
3257         }
3258         if (an[i].hasAutoCalculated() && an[i].isAutoCalculated())
3259         {
3260           // newer files have an 'autoCalculated' flag and store calculation
3261           // state in viewport properties
3262           jaa.autoCalculated = true; // means annotation will be marked for
3263           // update at end of load.
3264         }
3265         if (an[i].hasGraphHeight())
3266         {
3267           jaa.graphHeight = an[i].getGraphHeight();
3268         }
3269         if (an[i].hasBelowAlignment())
3270         {
3271           jaa.belowAlignment = an[i].isBelowAlignment();
3272         }
3273         jaa.setCalcId(an[i].getCalcId());
3274         if (an[i].getPropertyCount() > 0)
3275         {
3276           for (jalview.schemabinding.version2.Property prop : an[i]
3277                   .getProperty())
3278           {
3279             jaa.setProperty(prop.getName(), prop.getValue());
3280           }
3281         }
3282         if (jaa.autoCalculated)
3283         {
3284           autoAlan.add(new JvAnnotRow(i, jaa));
3285         }
3286         else
3287         // if (!autoForView)
3288         {
3289           // add autocalculated group annotation and any user created annotation
3290           // for the view
3291           al.addAnnotation(jaa);
3292         }
3293       }
3294     }
3295     // ///////////////////////
3296     // LOAD GROUPS
3297     // Create alignment markup and styles for this view
3298     if (jms.getJGroupCount() > 0)
3299     {
3300       JGroup[] groups = jms.getJGroup();
3301       boolean addAnnotSchemeGroup = false;
3302       for (int i = 0; i < groups.length; i++)
3303       {
3304         JGroup jGroup = groups[i];
3305         ColourSchemeI cs = null;
3306         if (jGroup.getColour() != null)
3307         {
3308           if (jGroup.getColour().startsWith("ucs"))
3309           {
3310             cs = getUserColourScheme(jms, jGroup.getColour());
3311           }
3312           else if (jGroup.getColour().equals("AnnotationColourGradient")
3313                   && jGroup.getAnnotationColours() != null)
3314           {
3315             addAnnotSchemeGroup = true;
3316             cs = null;
3317           }
3318           else
3319           {
3320             cs = ColourSchemeProperty.getColour(al, jGroup.getColour());
3321           }
3322
3323           if (cs != null)
3324           {
3325             cs.setThreshold(jGroup.getPidThreshold(), true);
3326           }
3327         }
3328
3329         Vector<SequenceI> seqs = new Vector<SequenceI>();
3330
3331         for (int s = 0; s < jGroup.getSeqCount(); s++)
3332         {
3333           String seqId = jGroup.getSeq(s) + "";
3334           SequenceI ts = seqRefIds.get(seqId);
3335
3336           if (ts != null)
3337           {
3338             seqs.addElement(ts);
3339           }
3340         }
3341
3342         if (seqs.size() < 1)
3343         {
3344           continue;
3345         }
3346
3347         SequenceGroup sg = new SequenceGroup(seqs, jGroup.getName(), cs,
3348                 jGroup.getDisplayBoxes(), jGroup.getDisplayText(),
3349                 jGroup.getColourText(), jGroup.getStart(), jGroup.getEnd());
3350
3351         sg.setOutlineColour(new java.awt.Color(jGroup.getOutlineColour()));
3352
3353         sg.textColour = new java.awt.Color(jGroup.getTextCol1());
3354         sg.textColour2 = new java.awt.Color(jGroup.getTextCol2());
3355         sg.setShowNonconserved(jGroup.hasShowUnconserved() ? jGroup
3356                 .isShowUnconserved() : false);
3357         sg.thresholdTextColour = jGroup.getTextColThreshold();
3358         if (jGroup.hasShowConsensusHistogram())
3359         {
3360           sg.setShowConsensusHistogram(jGroup.isShowConsensusHistogram());
3361         }
3362         ;
3363         if (jGroup.hasShowSequenceLogo())
3364         {
3365           sg.setshowSequenceLogo(jGroup.isShowSequenceLogo());
3366         }
3367         if (jGroup.hasNormaliseSequenceLogo())
3368         {
3369           sg.setNormaliseSequenceLogo(jGroup.isNormaliseSequenceLogo());
3370         }
3371         if (jGroup.hasIgnoreGapsinConsensus())
3372         {
3373           sg.setIgnoreGapsConsensus(jGroup.getIgnoreGapsinConsensus());
3374         }
3375         if (jGroup.getConsThreshold() != 0)
3376         {
3377           jalview.analysis.Conservation c = new jalview.analysis.Conservation(
3378                   "All", ResidueProperties.propHash, 3,
3379                   sg.getSequences(null), 0, sg.getWidth() - 1);
3380           c.calculate();
3381           c.verdict(false, 25);
3382           sg.cs.setConservation(c);
3383         }
3384
3385         if (jGroup.getId() != null && groupAnnotRefs.size() > 0)
3386         {
3387           // re-instate unique group/annotation row reference
3388           List<AlignmentAnnotation> jaal = groupAnnotRefs.get(jGroup
3389                   .getId());
3390           if (jaal != null)
3391           {
3392             for (AlignmentAnnotation jaa : jaal)
3393             {
3394               jaa.groupRef = sg;
3395               if (jaa.autoCalculated)
3396               {
3397                 // match up and try to set group autocalc alignment row for this
3398                 // annotation
3399                 if (jaa.label.startsWith("Consensus for "))
3400                 {
3401                   sg.setConsensus(jaa);
3402                 }
3403                 // match up and try to set group autocalc alignment row for this
3404                 // annotation
3405                 if (jaa.label.startsWith("Conservation for "))
3406                 {
3407                   sg.setConservationRow(jaa);
3408                 }
3409               }
3410             }
3411           }
3412         }
3413         al.addGroup(sg);
3414         if (addAnnotSchemeGroup)
3415         {
3416           // reconstruct the annotation colourscheme
3417           sg.cs = constructAnnotationColour(jGroup.getAnnotationColours(),
3418                   null, al, jms, false);
3419         }
3420       }
3421     }
3422     if (view == null)
3423     {
3424       // only dataset in this model, so just return.
3425       return null;
3426     }
3427     // ///////////////////////////////
3428     // LOAD VIEWPORT
3429
3430     // If we just load in the same jar file again, the sequenceSetId
3431     // will be the same, and we end up with multiple references
3432     // to the same sequenceSet. We must modify this id on load
3433     // so that each load of the file gives a unique id
3434     String uniqueSeqSetId = view.getSequenceSetId() + uniqueSetSuffix;
3435     String viewId = (view.getId() == null ? null : view.getId()
3436             + uniqueSetSuffix);
3437     AlignFrame af = null;
3438     AlignViewport av = null;
3439     // now check to see if we really need to create a new viewport.
3440     if (multipleView && viewportsAdded.size() == 0)
3441     {
3442       // We recovered an alignment for which a viewport already exists.
3443       // TODO: fix up any settings necessary for overlaying stored state onto
3444       // state recovered from another document. (may not be necessary).
3445       // we may need a binding from a viewport in memory to one recovered from
3446       // XML.
3447       // and then recover its containing af to allow the settings to be applied.
3448       // TODO: fix for vamsas demo
3449       System.err
3450               .println("About to recover a viewport for existing alignment: Sequence set ID is "
3451                       + uniqueSeqSetId);
3452       Object seqsetobj = retrieveExistingObj(uniqueSeqSetId);
3453       if (seqsetobj != null)
3454       {
3455         if (seqsetobj instanceof String)
3456         {
3457           uniqueSeqSetId = (String) seqsetobj;
3458           System.err
3459                   .println("Recovered extant sequence set ID mapping for ID : New Sequence set ID is "
3460                           + uniqueSeqSetId);
3461         }
3462         else
3463         {
3464           System.err
3465                   .println("Warning : Collision between sequence set ID string and existing jalview object mapping.");
3466         }
3467
3468       }
3469     }
3470     /**
3471      * indicate that annotation colours are applied across all groups (pre
3472      * Jalview 2.8.1 behaviour)
3473      */
3474     boolean doGroupAnnColour = Jalview2XML.isVersionStringLaterThan(
3475             "2.8.1", object.getVersion());
3476
3477     AlignmentPanel ap = null;
3478     boolean isnewview = true;
3479     if (viewId != null)
3480     {
3481       // Check to see if this alignment already has a view id == viewId
3482       jalview.gui.AlignmentPanel views[] = Desktop
3483               .getAlignmentPanels(uniqueSeqSetId);
3484       if (views != null && views.length > 0)
3485       {
3486         for (int v = 0; v < views.length; v++)
3487         {
3488           if (views[v].av.getViewId().equalsIgnoreCase(viewId))
3489           {
3490             // recover the existing alignpanel, alignframe, viewport
3491             af = views[v].alignFrame;
3492             av = views[v].av;
3493             ap = views[v];
3494             // TODO: could even skip resetting view settings if we don't want to
3495             // change the local settings from other jalview processes
3496             isnewview = false;
3497           }
3498         }
3499       }
3500     }
3501
3502     if (isnewview)
3503     {
3504       af = loadViewport(file, jseqs, hiddenSeqs, al, jms, view,
3505               uniqueSeqSetId, viewId, autoAlan);
3506       av = af.viewport;
3507       ap = af.alignPanel;
3508     }
3509
3510     /*
3511      * Load any trees, PDB structures and viewers
3512      * 
3513      * Not done if flag is false (when this method is used for New View)
3514      */
3515     if (loadTreesAndStructures)
3516     {
3517       loadTrees(jms, view, af, av, ap);
3518       loadPDBStructures(jprovider, jseqs, af, ap);
3519       loadRnaViewers(jprovider, jseqs, ap);
3520     }
3521     // and finally return.
3522     return af;
3523   }
3524
3525   /**
3526    * Instantiate and link any saved RNA (Varna) viewers. The state of the Varna
3527    * panel is restored from separate jar entries, two (gapped and trimmed) per
3528    * sequence and secondary structure.
3529    * 
3530    * Currently each viewer shows just one sequence and structure (gapped and
3531    * trimmed), however this method is designed to support multiple sequences or
3532    * structures in viewers if wanted in future.
3533    * 
3534    * @param jprovider
3535    * @param jseqs
3536    * @param ap
3537    */
3538   private void loadRnaViewers(jarInputStreamProvider jprovider,
3539           JSeq[] jseqs, AlignmentPanel ap)
3540   {
3541     /*
3542      * scan the sequences for references to viewers; create each one the first
3543      * time it is referenced, add Rna models to existing viewers
3544      */
3545     for (JSeq jseq : jseqs)
3546     {
3547       for (int i = 0; i < jseq.getRnaViewerCount(); i++)
3548       {
3549         RnaViewer viewer = jseq.getRnaViewer(i);
3550         AppVarna appVarna = findOrCreateVarnaViewer(viewer,
3551                 uniqueSetSuffix, ap);
3552
3553         for (int j = 0; j < viewer.getSecondaryStructureCount(); j++)
3554         {
3555           SecondaryStructure ss = viewer.getSecondaryStructure(j);
3556           SequenceI seq = seqRefIds.get(jseq.getId());
3557           AlignmentAnnotation ann = this.annotationIds.get(ss
3558                   .getAnnotationId());
3559
3560           /*
3561            * add the structure to the Varna display (with session state copied
3562            * from the jar to a temporary file)
3563            */
3564           boolean gapped = ss.isGapped();
3565           String rnaTitle = ss.getTitle();
3566           String sessionState = ss.getViewerState();
3567           String tempStateFile = copyJarEntry(jprovider, sessionState,
3568                   "varna");
3569           RnaModel rna = new RnaModel(rnaTitle, ann, seq, null, gapped);
3570           appVarna.addModelSession(rna, rnaTitle, tempStateFile);
3571         }
3572         appVarna.setInitialSelection(viewer.getSelectedRna());
3573       }
3574     }
3575   }
3576
3577   /**
3578    * Locate and return an already instantiated matching AppVarna, or create one
3579    * if not found
3580    * 
3581    * @param viewer
3582    * @param viewIdSuffix
3583    * @param ap
3584    * @return
3585    */
3586   protected AppVarna findOrCreateVarnaViewer(RnaViewer viewer,
3587           String viewIdSuffix, AlignmentPanel ap)
3588   {
3589     /*
3590      * on each load a suffix is appended to the saved viewId, to avoid conflicts
3591      * if load is repeated
3592      */
3593     String postLoadId = viewer.getViewId() + viewIdSuffix;
3594     for (JInternalFrame frame : getAllFrames())
3595     {
3596       if (frame instanceof AppVarna)
3597       {
3598         AppVarna varna = (AppVarna) frame;
3599         if (postLoadId.equals(varna.getViewId()))
3600         {
3601           // this viewer is already instantiated
3602           // could in future here add ap as another 'parent' of the
3603           // AppVarna window; currently just 1-to-many
3604           return varna;
3605         }
3606       }
3607     }
3608
3609     /*
3610      * viewer not found - make it
3611      */
3612     RnaViewerModel model = new RnaViewerModel(postLoadId,
3613             viewer.getTitle(), viewer.getXpos(), viewer.getYpos(),
3614             viewer.getWidth(), viewer.getHeight(),
3615             viewer.getDividerLocation());
3616     AppVarna varna = new AppVarna(model, ap);
3617
3618     return varna;
3619   }
3620
3621   /**
3622    * Load any saved trees
3623    * 
3624    * @param jms
3625    * @param view
3626    * @param af
3627    * @param av
3628    * @param ap
3629    */
3630   protected void loadTrees(JalviewModelSequence jms, Viewport view,
3631           AlignFrame af, AlignViewport av, AlignmentPanel ap)
3632   {
3633     // TODO result of automated refactoring - are all these parameters needed?
3634     try
3635     {
3636       for (int t = 0; t < jms.getTreeCount(); t++)
3637       {
3638
3639         Tree tree = jms.getTree(t);
3640
3641         TreePanel tp = (TreePanel) retrieveExistingObj(tree.getId());
3642         if (tp == null)
3643         {
3644           tp = af.ShowNewickTree(
3645                   new jalview.io.NewickFile(tree.getNewick()),
3646                   tree.getTitle(), tree.getWidth(), tree.getHeight(),
3647                   tree.getXpos(), tree.getYpos());
3648           if (tree.getId() != null)
3649           {
3650             // perhaps bind the tree id to something ?
3651           }
3652         }
3653         else
3654         {
3655           // update local tree attributes ?
3656           // TODO: should check if tp has been manipulated by user - if so its
3657           // settings shouldn't be modified
3658           tp.setTitle(tree.getTitle());
3659           tp.setBounds(new Rectangle(tree.getXpos(), tree.getYpos(), tree
3660                   .getWidth(), tree.getHeight()));
3661           tp.av = av; // af.viewport; // TODO: verify 'associate with all
3662           // views'
3663           // works still
3664           tp.treeCanvas.av = av; // af.viewport;
3665           tp.treeCanvas.ap = ap; // af.alignPanel;
3666
3667         }
3668         if (tp == null)
3669         {
3670           warn("There was a problem recovering stored Newick tree: \n"
3671                   + tree.getNewick());
3672           continue;
3673         }
3674
3675         tp.fitToWindow.setState(tree.getFitToWindow());
3676         tp.fitToWindow_actionPerformed(null);
3677
3678         if (tree.getFontName() != null)
3679         {
3680           tp.setTreeFont(new java.awt.Font(tree.getFontName(), tree
3681                   .getFontStyle(), tree.getFontSize()));
3682         }
3683         else
3684         {
3685           tp.setTreeFont(new java.awt.Font(view.getFontName(), view
3686                   .getFontStyle(), tree.getFontSize()));
3687         }
3688
3689         tp.showPlaceholders(tree.getMarkUnlinked());
3690         tp.showBootstrap(tree.getShowBootstrap());
3691         tp.showDistances(tree.getShowDistances());
3692
3693         tp.treeCanvas.threshold = tree.getThreshold();
3694
3695         if (tree.getCurrentTree())
3696         {
3697           af.viewport.setCurrentTree(tp.getTree());
3698         }
3699       }
3700
3701     } catch (Exception ex)
3702     {
3703       ex.printStackTrace();
3704     }
3705   }
3706
3707   /**
3708    * Load and link any saved structure viewers.
3709    * 
3710    * @param jprovider
3711    * @param jseqs
3712    * @param af
3713    * @param ap
3714    */
3715   protected void loadPDBStructures(jarInputStreamProvider jprovider,
3716           JSeq[] jseqs, AlignFrame af, AlignmentPanel ap)
3717   {
3718     /*
3719      * Run through all PDB ids on the alignment, and collect mappings between
3720      * distinct view ids and all sequences referring to that view.
3721      */
3722     Map<String, StructureViewerModel> structureViewers = new LinkedHashMap<String, StructureViewerModel>();
3723
3724     for (int i = 0; i < jseqs.length; i++)
3725     {
3726       if (jseqs[i].getPdbidsCount() > 0)
3727       {
3728         Pdbids[] ids = jseqs[i].getPdbids();
3729         for (int p = 0; p < ids.length; p++)
3730         {
3731           final int structureStateCount = ids[p].getStructureStateCount();
3732           for (int s = 0; s < structureStateCount; s++)
3733           {
3734             // check to see if we haven't already created this structure view
3735             final StructureState structureState = ids[p]
3736                     .getStructureState(s);
3737             String sviewid = (structureState.getViewId() == null) ? null
3738                     : structureState.getViewId() + uniqueSetSuffix;
3739             jalview.datamodel.PDBEntry jpdb = new jalview.datamodel.PDBEntry();
3740             // Originally : ids[p].getFile()
3741             // : TODO: verify external PDB file recovery still works in normal
3742             // jalview project load
3743             jpdb.setFile(loadPDBFile(jprovider, ids[p].getId()));
3744             jpdb.setId(ids[p].getId());
3745
3746             int x = structureState.getXpos();
3747             int y = structureState.getYpos();
3748             int width = structureState.getWidth();
3749             int height = structureState.getHeight();
3750
3751             // Probably don't need to do this anymore...
3752             // Desktop.desktop.getComponentAt(x, y);
3753             // TODO: NOW: check that this recovers the PDB file correctly.
3754             String pdbFile = loadPDBFile(jprovider, ids[p].getId());
3755             jalview.datamodel.SequenceI seq = seqRefIds.get(jseqs[i]
3756                     .getId() + "");
3757             if (sviewid == null)
3758             {
3759               sviewid = "_jalview_pre2_4_" + x + "," + y + "," + width
3760                       + "," + height;
3761             }
3762             if (!structureViewers.containsKey(sviewid))
3763             {
3764               structureViewers.put(sviewid,
3765                       new StructureViewerModel(x, y, width, height, false,
3766                               false, true, structureState.getViewId(),
3767                               structureState.getType()));
3768               // Legacy pre-2.7 conversion JAL-823 :
3769               // do not assume any view has to be linked for colour by
3770               // sequence
3771             }
3772
3773             // assemble String[] { pdb files }, String[] { id for each
3774             // file }, orig_fileloc, SequenceI[][] {{ seqs_file 1 }, {
3775             // seqs_file 2}, boolean[] {
3776             // linkAlignPanel,superposeWithAlignpanel}} from hash
3777             StructureViewerModel jmoldat = structureViewers.get(sviewid);
3778             jmoldat.setAlignWithPanel(jmoldat.isAlignWithPanel()
3779                     | (structureState.hasAlignwithAlignPanel() ? structureState
3780                             .getAlignwithAlignPanel() : false));
3781
3782             /*
3783              * Default colour by linked panel to false if not specified (e.g.
3784              * for pre-2.7 projects)
3785              */
3786             boolean colourWithAlignPanel = jmoldat.isColourWithAlignPanel();
3787             colourWithAlignPanel |= (structureState
3788                     .hasColourwithAlignPanel() ? structureState
3789                     .getColourwithAlignPanel() : false);
3790             jmoldat.setColourWithAlignPanel(colourWithAlignPanel);
3791
3792             /*
3793              * Default colour by viewer to true if not specified (e.g. for
3794              * pre-2.7 projects)
3795              */
3796             boolean colourByViewer = jmoldat.isColourByViewer();
3797             colourByViewer &= structureState.hasColourByJmol() ? structureState
3798                     .getColourByJmol() : true;
3799             jmoldat.setColourByViewer(colourByViewer);
3800
3801             if (jmoldat.getStateData().length() < structureState
3802                     .getContent().length())
3803             {
3804               {
3805                 jmoldat.setStateData(structureState.getContent());
3806               }
3807             }
3808             if (ids[p].getFile() != null)
3809             {
3810               File mapkey = new File(ids[p].getFile());
3811               StructureData seqstrmaps = jmoldat.getFileData().get(mapkey);
3812               if (seqstrmaps == null)
3813               {
3814                 jmoldat.getFileData().put(
3815                         mapkey,
3816                         seqstrmaps = jmoldat.new StructureData(pdbFile,
3817                                 ids[p].getId()));
3818               }
3819               if (!seqstrmaps.getSeqList().contains(seq))
3820               {
3821                 seqstrmaps.getSeqList().add(seq);
3822                 // TODO and chains?
3823               }
3824             }
3825             else
3826             {
3827               errorMessage = ("The Jmol views in this project were imported\nfrom an older version of Jalview.\nPlease review the sequence colour associations\nin the Colour by section of the Jmol View menu.\n\nIn the case of problems, see note at\nhttp://issues.jalview.org/browse/JAL-747");
3828               warn(errorMessage);
3829             }
3830           }
3831         }
3832       }
3833     }
3834     // Instantiate the associated structure views
3835     for (Entry<String, StructureViewerModel> entry : structureViewers
3836             .entrySet())
3837     {
3838       try
3839       {
3840         createOrLinkStructureViewer(entry, af, ap, jprovider);
3841       } catch (Exception e)
3842       {
3843         System.err.println("Error loading structure viewer: "
3844                 + e.getMessage());
3845         // failed - try the next one
3846       }
3847     }
3848   }
3849
3850   /**
3851    * 
3852    * @param viewerData
3853    * @param af
3854    * @param ap
3855    * @param jprovider
3856    */
3857   protected void createOrLinkStructureViewer(
3858           Entry<String, StructureViewerModel> viewerData, AlignFrame af,
3859           AlignmentPanel ap, jarInputStreamProvider jprovider)
3860   {
3861     final StructureViewerModel stateData = viewerData.getValue();
3862
3863     /*
3864      * Search for any viewer windows already open from other alignment views
3865      * that exactly match the stored structure state
3866      */
3867     StructureViewerBase comp = findMatchingViewer(viewerData);
3868
3869     if (comp != null)
3870     {
3871       linkStructureViewer(ap, comp, stateData);
3872       return;
3873     }
3874
3875     /*
3876      * From 2.9: stateData.type contains JMOL or CHIMERA, data is in jar entry
3877      * "viewer_"+stateData.viewId
3878      */
3879     if (ViewerType.CHIMERA.toString().equals(stateData.getType()))
3880     {
3881       createChimeraViewer(viewerData, af, jprovider);
3882     }
3883     else
3884     {
3885       /*
3886        * else Jmol (if pre-2.9, stateData contains JMOL state string)
3887        */
3888       createJmolViewer(viewerData, af, jprovider);
3889     }
3890   }
3891
3892   /**
3893    * Create a new Chimera viewer.
3894    * 
3895    * @param data
3896    * @param af
3897    * @param jprovider
3898    */
3899   protected void createChimeraViewer(
3900           Entry<String, StructureViewerModel> viewerData, AlignFrame af,
3901           jarInputStreamProvider jprovider)
3902   {
3903     StructureViewerModel data = viewerData.getValue();
3904     String chimeraSessionFile = data.getStateData();
3905
3906     /*
3907      * Copy Chimera session from jar entry "viewer_"+viewId to a temporary file
3908      * 
3909      * NB this is the 'saved' viewId as in the project file XML, _not_ the
3910      * 'uniquified' sviewid used to reconstruct the viewer here
3911      */
3912     String viewerJarEntryName = getViewerJarEntryName(data.getViewId());
3913     chimeraSessionFile = copyJarEntry(jprovider, viewerJarEntryName,
3914             "chimera");
3915
3916     Set<Entry<File, StructureData>> fileData = data.getFileData()
3917             .entrySet();
3918     List<PDBEntry> pdbs = new ArrayList<PDBEntry>();
3919     List<SequenceI[]> allseqs = new ArrayList<SequenceI[]>();
3920     for (Entry<File, StructureData> pdb : fileData)
3921     {
3922       String filePath = pdb.getValue().getFilePath();
3923       String pdbId = pdb.getValue().getPdbId();
3924       // pdbs.add(new PDBEntry(filePath, pdbId));
3925       pdbs.add(new PDBEntry(pdbId, null, PDBEntry.Type.PDB, filePath));
3926       final List<SequenceI> seqList = pdb.getValue().getSeqList();
3927       SequenceI[] seqs = seqList.toArray(new SequenceI[seqList.size()]);
3928       allseqs.add(seqs);
3929     }
3930
3931     boolean colourByChimera = data.isColourByViewer();
3932     boolean colourBySequence = data.isColourWithAlignPanel();
3933
3934     // TODO use StructureViewer as a factory here, see JAL-1761
3935     final PDBEntry[] pdbArray = pdbs.toArray(new PDBEntry[pdbs.size()]);
3936     final SequenceI[][] seqsArray = allseqs.toArray(new SequenceI[allseqs
3937             .size()][]);
3938     String newViewId = viewerData.getKey();
3939
3940     ChimeraViewFrame cvf = new ChimeraViewFrame(chimeraSessionFile,
3941             af.alignPanel, pdbArray, seqsArray, colourByChimera,
3942             colourBySequence, newViewId);
3943     cvf.setSize(data.getWidth(), data.getHeight());
3944     cvf.setLocation(data.getX(), data.getY());
3945   }
3946
3947   /**
3948    * Create a new Jmol window. First parse the Jmol state to translate filenames
3949    * loaded into the view, and record the order in which files are shown in the
3950    * Jmol view, so we can add the sequence mappings in same order.
3951    * 
3952    * @param viewerData
3953    * @param af
3954    * @param jprovider
3955    */
3956   protected void createJmolViewer(
3957           final Entry<String, StructureViewerModel> viewerData,
3958           AlignFrame af, jarInputStreamProvider jprovider)
3959   {
3960     final StructureViewerModel svattrib = viewerData.getValue();
3961     String state = svattrib.getStateData();
3962
3963     /*
3964      * Pre-2.9: state element value is the Jmol state string
3965      * 
3966      * 2.9+: @type is "JMOL", state data is in a Jar file member named "viewer_"
3967      * + viewId
3968      */
3969     if (ViewerType.JMOL.toString().equals(svattrib.getType()))
3970     {
3971       state = readJarEntry(jprovider,
3972               getViewerJarEntryName(svattrib.getViewId()));
3973     }
3974
3975     List<String> pdbfilenames = new ArrayList<String>();
3976     List<SequenceI[]> seqmaps = new ArrayList<SequenceI[]>();
3977     List<String> pdbids = new ArrayList<String>();
3978     StringBuilder newFileLoc = new StringBuilder(64);
3979     int cp = 0, ncp, ecp;
3980     Map<File, StructureData> oldFiles = svattrib.getFileData();
3981     while ((ncp = state.indexOf("load ", cp)) > -1)
3982     {
3983       do
3984       {
3985         // look for next filename in load statement
3986         newFileLoc.append(state.substring(cp,
3987                 ncp = (state.indexOf("\"", ncp + 1) + 1)));
3988         String oldfilenam = state.substring(ncp,
3989                 ecp = state.indexOf("\"", ncp));
3990         // recover the new mapping data for this old filename
3991         // have to normalize filename - since Jmol and jalview do
3992         // filename
3993         // translation differently.
3994         StructureData filedat = oldFiles.get(new File(oldfilenam));
3995         newFileLoc.append(Platform.escapeString(filedat.getFilePath()));
3996         pdbfilenames.add(filedat.getFilePath());
3997         pdbids.add(filedat.getPdbId());
3998         seqmaps.add(filedat.getSeqList().toArray(new SequenceI[0]));
3999         newFileLoc.append("\"");
4000         cp = ecp + 1; // advance beyond last \" and set cursor so we can
4001                       // look for next file statement.
4002       } while ((ncp = state.indexOf("/*file*/", cp)) > -1);
4003     }
4004     if (cp > 0)
4005     {
4006       // just append rest of state
4007       newFileLoc.append(state.substring(cp));
4008     }
4009     else
4010     {
4011       System.err.print("Ignoring incomplete Jmol state for PDB ids: ");
4012       newFileLoc = new StringBuilder(state);
4013       newFileLoc.append("; load append ");
4014       for (File id : oldFiles.keySet())
4015       {
4016         // add this and any other pdb files that should be present in
4017         // the viewer
4018         StructureData filedat = oldFiles.get(id);
4019         newFileLoc.append(filedat.getFilePath());
4020         pdbfilenames.add(filedat.getFilePath());
4021         pdbids.add(filedat.getPdbId());
4022         seqmaps.add(filedat.getSeqList().toArray(new SequenceI[0]));
4023         newFileLoc.append(" \"");
4024         newFileLoc.append(filedat.getFilePath());
4025         newFileLoc.append("\"");
4026
4027       }
4028       newFileLoc.append(";");
4029     }
4030
4031     if (newFileLoc.length() == 0)
4032     {
4033       return;
4034     }
4035     int histbug = newFileLoc.indexOf("history = ");
4036     if (histbug > -1)
4037     {
4038       /*
4039        * change "history = [true|false];" to "history = [1|0];"
4040        */
4041       histbug += 10;
4042       int diff = histbug == -1 ? -1 : newFileLoc.indexOf(";", histbug);
4043       String val = (diff == -1) ? null : newFileLoc
4044               .substring(histbug, diff);
4045       if (val != null && val.length() >= 4)
4046       {
4047         if (val.contains("e")) // eh? what can it be?
4048         {
4049           if (val.trim().equals("true"))
4050           {
4051             val = "1";
4052           }
4053           else
4054           {
4055             val = "0";
4056           }
4057           newFileLoc.replace(histbug, diff, val);
4058         }
4059       }
4060     }
4061
4062     final String[] pdbf = pdbfilenames.toArray(new String[pdbfilenames
4063             .size()]);
4064     final String[] id = pdbids.toArray(new String[pdbids.size()]);
4065     final SequenceI[][] sq = seqmaps
4066             .toArray(new SequenceI[seqmaps.size()][]);
4067     final String fileloc = newFileLoc.toString();
4068     final String sviewid = viewerData.getKey();
4069     final AlignFrame alf = af;
4070     final Rectangle rect = new Rectangle(svattrib.getX(), svattrib.getY(),
4071             svattrib.getWidth(), svattrib.getHeight());
4072     try
4073     {
4074       javax.swing.SwingUtilities.invokeAndWait(new Runnable()
4075       {
4076         @Override
4077         public void run()
4078         {
4079           JalviewStructureDisplayI sview = null;
4080           try
4081           {
4082             sview = new StructureViewer(alf.alignPanel
4083                     .getStructureSelectionManager()).createView(
4084                     StructureViewer.ViewerType.JMOL, pdbf, id, sq,
4085                     alf.alignPanel, svattrib, fileloc, rect, sviewid);
4086             addNewStructureViewer(sview);
4087           } catch (OutOfMemoryError ex)
4088           {
4089             new OOMWarning("restoring structure view for PDB id " + id,
4090                     (OutOfMemoryError) ex.getCause());
4091             if (sview != null && sview.isVisible())
4092             {
4093               sview.closeViewer(false);
4094               sview.setVisible(false);
4095               sview.dispose();
4096             }
4097           }
4098         }
4099       });
4100     } catch (InvocationTargetException ex)
4101     {
4102       warn("Unexpected error when opening Jmol view.", ex);
4103
4104     } catch (InterruptedException e)
4105     {
4106       // e.printStackTrace();
4107     }
4108
4109   }
4110
4111   /**
4112    * Generates a name for the entry in the project jar file to hold state
4113    * information for a structure viewer
4114    * 
4115    * @param viewId
4116    * @return
4117    */
4118   protected String getViewerJarEntryName(String viewId)
4119   {
4120     return VIEWER_PREFIX + viewId;
4121   }
4122
4123   /**
4124    * Returns any open frame that matches given structure viewer data. The match
4125    * is based on the unique viewId, or (for older project versions) the frame's
4126    * geometry.
4127    * 
4128    * @param viewerData
4129    * @return
4130    */
4131   protected StructureViewerBase findMatchingViewer(
4132           Entry<String, StructureViewerModel> viewerData)
4133   {
4134     final String sviewid = viewerData.getKey();
4135     final StructureViewerModel svattrib = viewerData.getValue();
4136     StructureViewerBase comp = null;
4137     JInternalFrame[] frames = getAllFrames();
4138     for (JInternalFrame frame : frames)
4139     {
4140       if (frame instanceof StructureViewerBase)
4141       {
4142         /*
4143          * Post jalview 2.4 schema includes structure view id
4144          */
4145         if (sviewid != null
4146                 && ((StructureViewerBase) frame).getViewId()
4147                         .equals(sviewid))
4148         {
4149           comp = (StructureViewerBase) frame;
4150           break; // break added in 2.9
4151         }
4152         /*
4153          * Otherwise test for matching position and size of viewer frame
4154          */
4155         else if (frame.getX() == svattrib.getX()
4156                 && frame.getY() == svattrib.getY()
4157                 && frame.getHeight() == svattrib.getHeight()
4158                 && frame.getWidth() == svattrib.getWidth())
4159         {
4160           comp = (StructureViewerBase) frame;
4161           // no break in faint hope of an exact match on viewId
4162         }
4163       }
4164     }
4165     return comp;
4166   }
4167
4168   /**
4169    * Link an AlignmentPanel to an existing structure viewer.
4170    * 
4171    * @param ap
4172    * @param viewer
4173    * @param oldFiles
4174    * @param useinViewerSuperpos
4175    * @param usetoColourbyseq
4176    * @param viewerColouring
4177    */
4178   protected void linkStructureViewer(AlignmentPanel ap,
4179           StructureViewerBase viewer, StructureViewerModel stateData)
4180   {
4181     // NOTE: if the jalview project is part of a shared session then
4182     // view synchronization should/could be done here.
4183
4184     final boolean useinViewerSuperpos = stateData.isAlignWithPanel();
4185     final boolean usetoColourbyseq = stateData.isColourWithAlignPanel();
4186     final boolean viewerColouring = stateData.isColourByViewer();
4187     Map<File, StructureData> oldFiles = stateData.getFileData();
4188
4189     /*
4190      * Add mapping for sequences in this view to an already open viewer
4191      */
4192     final AAStructureBindingModel binding = viewer.getBinding();
4193     for (File id : oldFiles.keySet())
4194     {
4195       // add this and any other pdb files that should be present in the
4196       // viewer
4197       StructureData filedat = oldFiles.get(id);
4198       String pdbFile = filedat.getFilePath();
4199       SequenceI[] seq = filedat.getSeqList().toArray(new SequenceI[0]);
4200       binding.getSsm().setMapping(seq, null, pdbFile,
4201               jalview.io.AppletFormatAdapter.FILE);
4202       binding.addSequenceForStructFile(pdbFile, seq);
4203     }
4204     // and add the AlignmentPanel's reference to the view panel
4205     viewer.addAlignmentPanel(ap);
4206     if (useinViewerSuperpos)
4207     {
4208       viewer.useAlignmentPanelForSuperposition(ap);
4209     }
4210     else
4211     {
4212       viewer.excludeAlignmentPanelForSuperposition(ap);
4213     }
4214     if (usetoColourbyseq)
4215     {
4216       viewer.useAlignmentPanelForColourbyseq(ap, !viewerColouring);
4217     }
4218     else
4219     {
4220       viewer.excludeAlignmentPanelForColourbyseq(ap);
4221     }
4222   }
4223
4224   /**
4225    * Get all frames within the Desktop.
4226    * 
4227    * @return
4228    */
4229   protected JInternalFrame[] getAllFrames()
4230   {
4231     JInternalFrame[] frames = null;
4232     // TODO is this necessary - is it safe - risk of hanging?
4233     do
4234     {
4235       try
4236       {
4237         frames = Desktop.desktop.getAllFrames();
4238       } catch (ArrayIndexOutOfBoundsException e)
4239       {
4240         // occasional No such child exceptions are thrown here...
4241         try
4242         {
4243           Thread.sleep(10);
4244         } catch (InterruptedException f)
4245         {
4246         }
4247       }
4248     } while (frames == null);
4249     return frames;
4250   }
4251
4252   /**
4253    * Answers true if 'version' is equal to or later than 'supported', where each
4254    * is formatted as major/minor versions like "2.8.3" or "2.3.4b1" for bugfix
4255    * changes. Development and test values for 'version' are leniently treated
4256    * i.e. answer true.
4257    * 
4258    * @param supported
4259    *          - minimum version we are comparing against
4260    * @param version
4261    *          - version of data being processsed
4262    * @return
4263    */
4264   public static boolean isVersionStringLaterThan(String supported,
4265           String version)
4266   {
4267     if (supported == null || version == null
4268             || version.equalsIgnoreCase("DEVELOPMENT BUILD")
4269             || version.equalsIgnoreCase("Test")
4270             || version.equalsIgnoreCase("AUTOMATED BUILD"))
4271     {
4272       System.err.println("Assuming project file with "
4273               + (version == null ? "null" : version)
4274               + " is compatible with Jalview version " + supported);
4275       return true;
4276     }
4277     else
4278     {
4279       return StringUtils.compareVersions(version, supported, "b") >= 0;
4280     }
4281   }
4282
4283   Vector<JalviewStructureDisplayI> newStructureViewers = null;
4284
4285   protected void addNewStructureViewer(JalviewStructureDisplayI sview)
4286   {
4287     if (newStructureViewers != null)
4288     {
4289       sview.getBinding().setFinishedLoadingFromArchive(false);
4290       newStructureViewers.add(sview);
4291     }
4292   }
4293
4294   protected void setLoadingFinishedForNewStructureViewers()
4295   {
4296     if (newStructureViewers != null)
4297     {
4298       for (JalviewStructureDisplayI sview : newStructureViewers)
4299       {
4300         sview.getBinding().setFinishedLoadingFromArchive(true);
4301       }
4302       newStructureViewers.clear();
4303       newStructureViewers = null;
4304     }
4305   }
4306
4307   AlignFrame loadViewport(String file, JSeq[] JSEQ,
4308           List<SequenceI> hiddenSeqs, AlignmentI al,
4309           JalviewModelSequence jms, Viewport view, String uniqueSeqSetId,
4310           String viewId, List<JvAnnotRow> autoAlan)
4311   {
4312     AlignFrame af = null;
4313     af = new AlignFrame(al, view.getWidth(), view.getHeight(),
4314             uniqueSeqSetId, viewId);
4315
4316     af.setFileName(file, "Jalview");
4317
4318     for (int i = 0; i < JSEQ.length; i++)
4319     {
4320       af.viewport.setSequenceColour(af.viewport.getAlignment()
4321               .getSequenceAt(i), new java.awt.Color(JSEQ[i].getColour()));
4322     }
4323
4324     if (al.hasSeqrep())
4325     {
4326       af.getViewport().setColourByReferenceSeq(true);
4327       af.getViewport().setDisplayReferenceSeq(true);
4328     }
4329
4330     af.viewport.setGatherViewsHere(view.getGatheredViews());
4331
4332     if (view.getSequenceSetId() != null)
4333     {
4334       AlignmentViewport av = viewportsAdded.get(uniqueSeqSetId);
4335
4336       af.viewport.setSequenceSetId(uniqueSeqSetId);
4337       if (av != null)
4338       {
4339         // propagate shared settings to this new view
4340         af.viewport.setHistoryList(av.getHistoryList());
4341         af.viewport.setRedoList(av.getRedoList());
4342       }
4343       else
4344       {
4345         viewportsAdded.put(uniqueSeqSetId, af.viewport);
4346       }
4347       // TODO: check if this method can be called repeatedly without
4348       // side-effects if alignpanel already registered.
4349       PaintRefresher.Register(af.alignPanel, uniqueSeqSetId);
4350     }
4351     // apply Hidden regions to view.
4352     if (hiddenSeqs != null)
4353     {
4354       for (int s = 0; s < JSEQ.length; s++)
4355       {
4356         SequenceGroup hidden = new SequenceGroup();
4357         boolean isRepresentative = false;
4358         for (int r = 0; r < JSEQ[s].getHiddenSequencesCount(); r++)
4359         {
4360           isRepresentative = true;
4361           SequenceI sequenceToHide = al.getSequenceAt(JSEQ[s]
4362                   .getHiddenSequences(r));
4363           hidden.addSequence(sequenceToHide, false);
4364           // remove from hiddenSeqs list so we don't try to hide it twice
4365           hiddenSeqs.remove(sequenceToHide);
4366         }
4367         if (isRepresentative)
4368         {
4369           SequenceI representativeSequence = al.getSequenceAt(s);
4370           hidden.addSequence(representativeSequence, false);
4371           af.viewport.hideRepSequences(representativeSequence, hidden);
4372         }
4373       }
4374
4375       SequenceI[] hseqs = hiddenSeqs.toArray(new SequenceI[hiddenSeqs
4376               .size()]);
4377       af.viewport.hideSequence(hseqs);
4378
4379     }
4380     // recover view properties and display parameters
4381     if (view.getViewName() != null)
4382     {
4383       af.viewport.viewName = view.getViewName();
4384       af.setInitialTabVisible();
4385     }
4386     af.setBounds(view.getXpos(), view.getYpos(), view.getWidth(),
4387             view.getHeight());
4388
4389     af.viewport.setShowAnnotation(view.getShowAnnotation());
4390     af.viewport.setAbovePIDThreshold(view.getPidSelected());
4391
4392     af.viewport.setColourText(view.getShowColourText());
4393
4394     af.viewport.setConservationSelected(view.getConservationSelected());
4395     af.viewport.setShowJVSuffix(view.getShowFullId());
4396     af.viewport.setRightAlignIds(view.getRightAlignIds());
4397     af.viewport.setFont(
4398             new java.awt.Font(view.getFontName(), view.getFontStyle(), view
4399                     .getFontSize()), true);
4400     ViewStyleI vs = af.viewport.getViewStyle();
4401     vs.setScaleProteinAsCdna(view.isScaleProteinAsCdna());
4402     af.viewport.setViewStyle(vs);
4403     // TODO: allow custom charWidth/Heights to be restored by updating them
4404     // after setting font - which means set above to false
4405     af.viewport.setRenderGaps(view.getRenderGaps());
4406     af.viewport.setWrapAlignment(view.getWrapAlignment());
4407     af.viewport.setShowAnnotation(view.getShowAnnotation());
4408
4409     af.viewport.setShowBoxes(view.getShowBoxes());
4410
4411     af.viewport.setShowText(view.getShowText());
4412
4413     af.viewport.setTextColour(new java.awt.Color(view.getTextCol1()));
4414     af.viewport.setTextColour2(new java.awt.Color(view.getTextCol2()));
4415     af.viewport.setThresholdTextColour(view.getTextColThreshold());
4416     af.viewport.setShowUnconserved(view.hasShowUnconserved() ? view
4417             .isShowUnconserved() : false);
4418     af.viewport.setStartRes(view.getStartRes());
4419     af.viewport.setStartSeq(view.getStartSeq());
4420     af.alignPanel.updateLayout();
4421     ColourSchemeI cs = null;
4422     // apply colourschemes
4423     if (view.getBgColour() != null)
4424     {
4425       if (view.getBgColour().startsWith("ucs"))
4426       {
4427         cs = getUserColourScheme(jms, view.getBgColour());
4428       }
4429       else if (view.getBgColour().startsWith("Annotation"))
4430       {
4431         AnnotationColours viewAnnColour = view.getAnnotationColours();
4432         cs = constructAnnotationColour(viewAnnColour, af, al, jms, true);
4433
4434         // annpos
4435
4436       }
4437       else
4438       {
4439         cs = ColourSchemeProperty.getColour(al, view.getBgColour());
4440       }
4441
4442       if (cs != null)
4443       {
4444         cs.setThreshold(view.getPidThreshold(), true);
4445         cs.setConsensus(af.viewport.getSequenceConsensusHash());
4446       }
4447     }
4448
4449     af.viewport.setGlobalColourScheme(cs);
4450     af.viewport.setColourAppliesToAllGroups(false);
4451
4452     if (view.getConservationSelected() && cs != null)
4453     {
4454       cs.setConservationInc(view.getConsThreshold());
4455     }
4456
4457     af.changeColour(cs);
4458
4459     af.viewport.setColourAppliesToAllGroups(true);
4460
4461     af.viewport.setShowSequenceFeatures(view.getShowSequenceFeatures());
4462
4463     if (view.hasCentreColumnLabels())
4464     {
4465       af.viewport.setCentreColumnLabels(view.getCentreColumnLabels());
4466     }
4467     if (view.hasIgnoreGapsinConsensus())
4468     {
4469       af.viewport.setIgnoreGapsConsensus(view.getIgnoreGapsinConsensus(),
4470               null);
4471     }
4472     if (view.hasFollowHighlight())
4473     {
4474       af.viewport.setFollowHighlight(view.getFollowHighlight());
4475     }
4476     if (view.hasFollowSelection())
4477     {
4478       af.viewport.followSelection = view.getFollowSelection();
4479     }
4480     if (view.hasShowConsensusHistogram())
4481     {
4482       af.viewport.setShowConsensusHistogram(view
4483               .getShowConsensusHistogram());
4484     }
4485     else
4486     {
4487       af.viewport.setShowConsensusHistogram(true);
4488     }
4489     if (view.hasShowSequenceLogo())
4490     {
4491       af.viewport.setShowSequenceLogo(view.getShowSequenceLogo());
4492     }
4493     else
4494     {
4495       af.viewport.setShowSequenceLogo(false);
4496     }
4497     if (view.hasNormaliseSequenceLogo())
4498     {
4499       af.viewport.setNormaliseSequenceLogo(view.getNormaliseSequenceLogo());
4500     }
4501     if (view.hasShowDbRefTooltip())
4502     {
4503       af.viewport.setShowDBRefs(view.getShowDbRefTooltip());
4504     }
4505     if (view.hasShowNPfeatureTooltip())
4506     {
4507       af.viewport.setShowNPFeats(view.hasShowNPfeatureTooltip());
4508     }
4509     if (view.hasShowGroupConsensus())
4510     {
4511       af.viewport.setShowGroupConsensus(view.getShowGroupConsensus());
4512     }
4513     else
4514     {
4515       af.viewport.setShowGroupConsensus(false);
4516     }
4517     if (view.hasShowGroupConservation())
4518     {
4519       af.viewport.setShowGroupConservation(view.getShowGroupConservation());
4520     }
4521     else
4522     {
4523       af.viewport.setShowGroupConservation(false);
4524     }
4525
4526     // recover featre settings
4527     if (jms.getFeatureSettings() != null)
4528     {
4529       FeaturesDisplayed fdi;
4530       af.viewport.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
4531       String[] renderOrder = new String[jms.getFeatureSettings()
4532               .getSettingCount()];
4533       Map<String, FeatureColourI> featureColours = new Hashtable<String, FeatureColourI>();
4534       Map<String, Float> featureOrder = new Hashtable<String, Float>();
4535
4536       for (int fs = 0; fs < jms.getFeatureSettings().getSettingCount(); fs++)
4537       {
4538         Setting setting = jms.getFeatureSettings().getSetting(fs);
4539         if (setting.hasMincolour())
4540         {
4541           FeatureColourI gc = setting.hasMin() ? new FeatureColour(
4542                   new Color(setting.getMincolour()), new Color(
4543                           setting.getColour()), setting.getMin(),
4544                   setting.getMax()) : new FeatureColour(new Color(
4545                   setting.getMincolour()), new Color(setting.getColour()),
4546                   0, 1);
4547           if (setting.hasThreshold())
4548           {
4549             gc.setThreshold(setting.getThreshold());
4550             int threshstate = setting.getThreshstate();
4551             // -1 = None, 0 = Below, 1 = Above threshold
4552             if (threshstate == 0)
4553             {
4554               gc.setBelowThreshold(true);
4555             }
4556             else if (threshstate == 1)
4557             {
4558               gc.setAboveThreshold(true);
4559             }
4560           }
4561           gc.setAutoScaled(true); // default
4562           if (setting.hasAutoScale())
4563           {
4564             gc.setAutoScaled(setting.getAutoScale());
4565           }
4566           if (setting.hasColourByLabel())
4567           {
4568             gc.setColourByLabel(setting.getColourByLabel());
4569           }
4570           // and put in the feature colour table.
4571           featureColours.put(setting.getType(), gc);
4572         }
4573         else
4574         {
4575           featureColours.put(setting.getType(), new FeatureColour(
4576                   new Color(setting.getColour())));
4577         }
4578         renderOrder[fs] = setting.getType();
4579         if (setting.hasOrder())
4580         {
4581           featureOrder.put(setting.getType(), setting.getOrder());
4582         }
4583         else
4584         {
4585           featureOrder.put(setting.getType(), new Float(fs
4586                   / jms.getFeatureSettings().getSettingCount()));
4587         }
4588         if (setting.getDisplay())
4589         {
4590           fdi.setVisible(setting.getType());
4591         }
4592       }
4593       Map<String, Boolean> fgtable = new Hashtable<String, Boolean>();
4594       for (int gs = 0; gs < jms.getFeatureSettings().getGroupCount(); gs++)
4595       {
4596         Group grp = jms.getFeatureSettings().getGroup(gs);
4597         fgtable.put(grp.getName(), new Boolean(grp.getDisplay()));
4598       }
4599       // FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
4600       // fgtable, featureColours, jms.getFeatureSettings().hasTransparency() ?
4601       // jms.getFeatureSettings().getTransparency() : 0.0, featureOrder);
4602       FeatureRendererSettings frs = new FeatureRendererSettings(
4603               renderOrder, fgtable, featureColours, 1.0f, featureOrder);
4604       af.alignPanel.getSeqPanel().seqCanvas.getFeatureRenderer()
4605               .transferSettings(frs);
4606
4607     }
4608
4609     if (view.getHiddenColumnsCount() > 0)
4610     {
4611       for (int c = 0; c < view.getHiddenColumnsCount(); c++)
4612       {
4613         af.viewport.hideColumns(view.getHiddenColumns(c).getStart(), view
4614                 .getHiddenColumns(c).getEnd() // +1
4615                 );
4616       }
4617     }
4618     if (view.getCalcIdParam() != null)
4619     {
4620       for (CalcIdParam calcIdParam : view.getCalcIdParam())
4621       {
4622         if (calcIdParam != null)
4623         {
4624           if (recoverCalcIdParam(calcIdParam, af.viewport))
4625           {
4626           }
4627           else
4628           {
4629             warn("Couldn't recover parameters for "
4630                     + calcIdParam.getCalcId());
4631           }
4632         }
4633       }
4634     }
4635     af.setMenusFromViewport(af.viewport);
4636     af.setTitle(view.getTitle());
4637     // TODO: we don't need to do this if the viewport is aready visible.
4638     /*
4639      * Add the AlignFrame to the desktop (it may be 'gathered' later), unless it
4640      * has a 'cdna/protein complement' view, in which case save it in order to
4641      * populate a SplitFrame once all views have been read in.
4642      */
4643     String complementaryViewId = view.getComplementId();
4644     if (complementaryViewId == null)
4645     {
4646       Desktop.addInternalFrame(af, view.getTitle(), view.getWidth(),
4647               view.getHeight());
4648       // recompute any autoannotation
4649       af.alignPanel.updateAnnotation(false, true);
4650       reorderAutoannotation(af, al, autoAlan);
4651       af.alignPanel.alignmentChanged();
4652     }
4653     else
4654     {
4655       splitFrameCandidates.put(view, af);
4656     }
4657     return af;
4658   }
4659
4660   private ColourSchemeI constructAnnotationColour(
4661           AnnotationColours viewAnnColour, AlignFrame af, AlignmentI al,
4662           JalviewModelSequence jms, boolean checkGroupAnnColour)
4663   {
4664     boolean propagateAnnColour = false;
4665     ColourSchemeI cs = null;
4666     AlignmentI annAlignment = af != null ? af.viewport.getAlignment() : al;
4667     if (checkGroupAnnColour && al.getGroups() != null
4668             && al.getGroups().size() > 0)
4669     {
4670       // pre 2.8.1 behaviour
4671       // check to see if we should transfer annotation colours
4672       propagateAnnColour = true;
4673       for (jalview.datamodel.SequenceGroup sg : al.getGroups())
4674       {
4675         if (sg.cs instanceof AnnotationColourGradient)
4676         {
4677           propagateAnnColour = false;
4678         }
4679       }
4680     }
4681     // int find annotation
4682     if (annAlignment.getAlignmentAnnotation() != null)
4683     {
4684       for (int i = 0; i < annAlignment.getAlignmentAnnotation().length; i++)
4685       {
4686         if (annAlignment.getAlignmentAnnotation()[i].label
4687                 .equals(viewAnnColour.getAnnotation()))
4688         {
4689           if (annAlignment.getAlignmentAnnotation()[i].getThreshold() == null)
4690           {
4691             annAlignment.getAlignmentAnnotation()[i]
4692                     .setThreshold(new jalview.datamodel.GraphLine(
4693                             viewAnnColour.getThreshold(), "Threshold",
4694                             java.awt.Color.black)
4695
4696                     );
4697           }
4698
4699           if (viewAnnColour.getColourScheme().equals("None"))
4700           {
4701             cs = new AnnotationColourGradient(
4702                     annAlignment.getAlignmentAnnotation()[i],
4703                     new java.awt.Color(viewAnnColour.getMinColour()),
4704                     new java.awt.Color(viewAnnColour.getMaxColour()),
4705                     viewAnnColour.getAboveThreshold());
4706           }
4707           else if (viewAnnColour.getColourScheme().startsWith("ucs"))
4708           {
4709             cs = new AnnotationColourGradient(
4710                     annAlignment.getAlignmentAnnotation()[i],
4711                     getUserColourScheme(jms,
4712                             viewAnnColour.getColourScheme()),
4713                     viewAnnColour.getAboveThreshold());
4714           }
4715           else
4716           {
4717             cs = new AnnotationColourGradient(
4718                     annAlignment.getAlignmentAnnotation()[i],
4719                     ColourSchemeProperty.getColour(al,
4720                             viewAnnColour.getColourScheme()),
4721                     viewAnnColour.getAboveThreshold());
4722           }
4723           if (viewAnnColour.hasPerSequence())
4724           {
4725             ((AnnotationColourGradient) cs).setSeqAssociated(viewAnnColour
4726                     .isPerSequence());
4727           }
4728           if (viewAnnColour.hasPredefinedColours())
4729           {
4730             ((AnnotationColourGradient) cs)
4731                     .setPredefinedColours(viewAnnColour
4732                             .isPredefinedColours());
4733           }
4734           if (propagateAnnColour && al.getGroups() != null)
4735           {
4736             // Also use these settings for all the groups
4737             for (int g = 0; g < al.getGroups().size(); g++)
4738             {
4739               jalview.datamodel.SequenceGroup sg = al.getGroups().get(g);
4740
4741               if (sg.cs == null)
4742               {
4743                 continue;
4744               }
4745
4746               /*
4747                * if (viewAnnColour.getColourScheme().equals("None" )) { sg.cs =
4748                * new AnnotationColourGradient(
4749                * annAlignment.getAlignmentAnnotation()[i], new
4750                * java.awt.Color(viewAnnColour. getMinColour()), new
4751                * java.awt.Color(viewAnnColour. getMaxColour()),
4752                * viewAnnColour.getAboveThreshold()); } else
4753                */
4754               {
4755                 sg.cs = new AnnotationColourGradient(
4756                         annAlignment.getAlignmentAnnotation()[i], sg.cs,
4757                         viewAnnColour.getAboveThreshold());
4758                 if (cs instanceof AnnotationColourGradient)
4759                 {
4760                   if (viewAnnColour.hasPerSequence())
4761                   {
4762                     ((AnnotationColourGradient) cs)
4763                             .setSeqAssociated(viewAnnColour.isPerSequence());
4764                   }
4765                   if (viewAnnColour.hasPredefinedColours())
4766                   {
4767                     ((AnnotationColourGradient) cs)
4768                             .setPredefinedColours(viewAnnColour
4769                                     .isPredefinedColours());
4770                   }
4771                 }
4772               }
4773
4774             }
4775           }
4776
4777           break;
4778         }
4779
4780       }
4781     }
4782     return cs;
4783   }
4784
4785   private void reorderAutoannotation(AlignFrame af, AlignmentI al,
4786           List<JvAnnotRow> autoAlan)
4787   {
4788     // copy over visualization settings for autocalculated annotation in the
4789     // view
4790     if (al.getAlignmentAnnotation() != null)
4791     {
4792       /**
4793        * Kludge for magic autoannotation names (see JAL-811)
4794        */
4795       String[] magicNames = new String[] { "Consensus", "Quality",
4796           "Conservation" };
4797       JvAnnotRow nullAnnot = new JvAnnotRow(-1, null);
4798       Hashtable<String, JvAnnotRow> visan = new Hashtable<String, JvAnnotRow>();
4799       for (String nm : magicNames)
4800       {
4801         visan.put(nm, nullAnnot);
4802       }
4803       for (JvAnnotRow auan : autoAlan)
4804       {
4805         visan.put(auan.template.label
4806                 + (auan.template.getCalcId() == null ? "" : "\t"
4807                         + auan.template.getCalcId()), auan);
4808       }
4809       int hSize = al.getAlignmentAnnotation().length;
4810       List<JvAnnotRow> reorder = new ArrayList<JvAnnotRow>();
4811       // work through any autoCalculated annotation already on the view
4812       // removing it if it should be placed in a different location on the
4813       // annotation panel.
4814       List<String> remains = new ArrayList<String>(visan.keySet());
4815       for (int h = 0; h < hSize; h++)
4816       {
4817         jalview.datamodel.AlignmentAnnotation jalan = al
4818                 .getAlignmentAnnotation()[h];
4819         if (jalan.autoCalculated)
4820         {
4821           String k;
4822           JvAnnotRow valan = visan.get(k = jalan.label);
4823           if (jalan.getCalcId() != null)
4824           {
4825             valan = visan.get(k = jalan.label + "\t" + jalan.getCalcId());
4826           }
4827
4828           if (valan != null)
4829           {
4830             // delete the auto calculated row from the alignment
4831             al.deleteAnnotation(jalan, false);
4832             remains.remove(k);
4833             hSize--;
4834             h--;
4835             if (valan != nullAnnot)
4836             {
4837               if (jalan != valan.template)
4838               {
4839                 // newly created autoannotation row instance
4840                 // so keep a reference to the visible annotation row
4841                 // and copy over all relevant attributes
4842                 if (valan.template.graphHeight >= 0)
4843
4844                 {
4845                   jalan.graphHeight = valan.template.graphHeight;
4846                 }
4847                 jalan.visible = valan.template.visible;
4848               }
4849               reorder.add(new JvAnnotRow(valan.order, jalan));
4850             }
4851           }
4852         }
4853       }
4854       // Add any (possibly stale) autocalculated rows that were not appended to
4855       // the view during construction
4856       for (String other : remains)
4857       {
4858         JvAnnotRow othera = visan.get(other);
4859         if (othera != nullAnnot && othera.template.getCalcId() != null
4860                 && othera.template.getCalcId().length() > 0)
4861         {
4862           reorder.add(othera);
4863         }
4864       }
4865       // now put the automatic annotation in its correct place
4866       int s = 0, srt[] = new int[reorder.size()];
4867       JvAnnotRow[] rws = new JvAnnotRow[reorder.size()];
4868       for (JvAnnotRow jvar : reorder)
4869       {
4870         rws[s] = jvar;
4871         srt[s++] = jvar.order;
4872       }
4873       reorder.clear();
4874       jalview.util.QuickSort.sort(srt, rws);
4875       // and re-insert the annotation at its correct position
4876       for (JvAnnotRow jvar : rws)
4877       {
4878         al.addAnnotation(jvar.template, jvar.order);
4879       }
4880       af.alignPanel.adjustAnnotationHeight();
4881     }
4882   }
4883
4884   Hashtable skipList = null;
4885
4886   /**
4887    * TODO remove this method
4888    * 
4889    * @param view
4890    * @return AlignFrame bound to sequenceSetId from view, if one exists. private
4891    *         AlignFrame getSkippedFrame(Viewport view) { if (skipList==null) {
4892    *         throw new Error("Implementation Error. No skipList defined for this
4893    *         Jalview2XML instance."); } return (AlignFrame)
4894    *         skipList.get(view.getSequenceSetId()); }
4895    */
4896
4897   /**
4898    * Check if the Jalview view contained in object should be skipped or not.
4899    * 
4900    * @param object
4901    * @return true if view's sequenceSetId is a key in skipList
4902    */
4903   private boolean skipViewport(JalviewModel object)
4904   {
4905     if (skipList == null)
4906     {
4907       return false;
4908     }
4909     String id;
4910     if (skipList.containsKey(id = object.getJalviewModelSequence()
4911             .getViewport()[0].getSequenceSetId()))
4912     {
4913       if (Cache.log != null && Cache.log.isDebugEnabled())
4914       {
4915         Cache.log.debug("Skipping seuqence set id " + id);
4916       }
4917       return true;
4918     }
4919     return false;
4920   }
4921
4922   public void addToSkipList(AlignFrame af)
4923   {
4924     if (skipList == null)
4925     {
4926       skipList = new Hashtable();
4927     }
4928     skipList.put(af.getViewport().getSequenceSetId(), af);
4929   }
4930
4931   public void clearSkipList()
4932   {
4933     if (skipList != null)
4934     {
4935       skipList.clear();
4936       skipList = null;
4937     }
4938   }
4939
4940   private void recoverDatasetFor(SequenceSet vamsasSet, AlignmentI al,
4941           boolean ignoreUnrefed)
4942   {
4943     jalview.datamodel.AlignmentI ds = getDatasetFor(vamsasSet
4944             .getDatasetId());
4945     Vector dseqs = null;
4946     if (ds == null)
4947     {
4948       // create a list of new dataset sequences
4949       dseqs = new Vector();
4950     }
4951     for (int i = 0, iSize = vamsasSet.getSequenceCount(); i < iSize; i++)
4952     {
4953       Sequence vamsasSeq = vamsasSet.getSequence(i);
4954       ensureJalviewDatasetSequence(vamsasSeq, ds, dseqs, ignoreUnrefed, i);
4955     }
4956     // create a new dataset
4957     if (ds == null)
4958     {
4959       SequenceI[] dsseqs = new SequenceI[dseqs.size()];
4960       dseqs.copyInto(dsseqs);
4961       ds = new jalview.datamodel.Alignment(dsseqs);
4962       debug("Created new dataset " + vamsasSet.getDatasetId()
4963               + " for alignment " + System.identityHashCode(al));
4964       addDatasetRef(vamsasSet.getDatasetId(), ds);
4965     }
4966     // set the dataset for the newly imported alignment.
4967     if (al.getDataset() == null && !ignoreUnrefed)
4968     {
4969       al.setDataset(ds);
4970     }
4971   }
4972
4973   /**
4974    * 
4975    * @param vamsasSeq
4976    *          sequence definition to create/merge dataset sequence for
4977    * @param ds
4978    *          dataset alignment
4979    * @param dseqs
4980    *          vector to add new dataset sequence to
4981    * @param ignoreUnrefed
4982    *          - when true, don't create new sequences from vamsasSeq if it's id
4983    *          doesn't already have an asssociated Jalview sequence.
4984    * @param vseqpos
4985    *          - used to reorder the sequence in the alignment according to the
4986    *          vamsasSeq array ordering, to preserve ordering of dataset
4987    */
4988   private void ensureJalviewDatasetSequence(Sequence vamsasSeq,
4989           AlignmentI ds, Vector dseqs, boolean ignoreUnrefed, int vseqpos)
4990   {
4991     // JBP TODO: Check this is called for AlCodonFrames to support recovery of
4992     // xRef Codon Maps
4993     SequenceI sq = seqRefIds.get(vamsasSeq.getId());
4994     boolean reorder = false;
4995     SequenceI dsq = null;
4996     if (sq != null && sq.getDatasetSequence() != null)
4997     {
4998       dsq = sq.getDatasetSequence();
4999     }
5000     else
5001     {
5002       reorder = true;
5003     }
5004     if (sq == null && ignoreUnrefed)
5005     {
5006       return;
5007     }
5008     String sqid = vamsasSeq.getDsseqid();
5009     if (dsq == null)
5010     {
5011       // need to create or add a new dataset sequence reference to this sequence
5012       if (sqid != null)
5013       {
5014         dsq = seqRefIds.get(sqid);
5015       }
5016       // check again
5017       if (dsq == null)
5018       {
5019         // make a new dataset sequence
5020         dsq = sq.createDatasetSequence();
5021         if (sqid == null)
5022         {
5023           // make up a new dataset reference for this sequence
5024           sqid = seqHash(dsq);
5025         }
5026         dsq.setVamsasId(uniqueSetSuffix + sqid);
5027         seqRefIds.put(sqid, dsq);
5028         if (ds == null)
5029         {
5030           if (dseqs != null)
5031           {
5032             dseqs.addElement(dsq);
5033           }
5034         }
5035         else
5036         {
5037           ds.addSequence(dsq);
5038         }
5039       }
5040       else
5041       {
5042         if (sq != dsq)
5043         { // make this dataset sequence sq's dataset sequence
5044           sq.setDatasetSequence(dsq);
5045           // and update the current dataset alignment
5046           if (ds == null)
5047           {
5048             if (dseqs != null)
5049             {
5050               if (!dseqs.contains(dsq))
5051               {
5052                 dseqs.add(dsq);
5053               }
5054             }
5055             else
5056             {
5057               if (ds.findIndex(dsq) < 0)
5058               {
5059                 ds.addSequence(dsq);
5060               }
5061             }
5062           }
5063         }
5064       }
5065     }
5066     // TODO: refactor this as a merge dataset sequence function
5067     // now check that sq (the dataset sequence) sequence really is the union of
5068     // all references to it
5069     // boolean pre = sq.getStart() < dsq.getStart();
5070     // boolean post = sq.getEnd() > dsq.getEnd();
5071     // if (pre || post)
5072     if (sq != dsq)
5073     {
5074       // StringBuffer sb = new StringBuffer();
5075       String newres = jalview.analysis.AlignSeq.extractGaps(
5076               jalview.util.Comparison.GapChars, sq.getSequenceAsString());
5077       if (!newres.equalsIgnoreCase(dsq.getSequenceAsString())
5078               && newres.length() > dsq.getLength())
5079       {
5080         // Update with the longer sequence.
5081         synchronized (dsq)
5082         {
5083           /*
5084            * if (pre) { sb.insert(0, newres .substring(0, dsq.getStart() -
5085            * sq.getStart())); dsq.setStart(sq.getStart()); } if (post) {
5086            * sb.append(newres.substring(newres.length() - sq.getEnd() -
5087            * dsq.getEnd())); dsq.setEnd(sq.getEnd()); }
5088            */
5089           dsq.setSequence(newres);
5090         }
5091         // TODO: merges will never happen if we 'know' we have the real dataset
5092         // sequence - this should be detected when id==dssid
5093         System.err
5094                 .println("DEBUG Notice:  Merged dataset sequence (if you see this often, post at http://issues.jalview.org/browse/JAL-1474)"); // ("
5095         // + (pre ? "prepended" : "") + " "
5096         // + (post ? "appended" : ""));
5097       }
5098     }
5099     else
5100     {
5101       // sequence refs are identical. We may need to update the existing dataset
5102       // alignment with this one, though.
5103       if (ds != null && dseqs == null)
5104       {
5105         int opos = ds.findIndex(dsq);
5106         SequenceI tseq = null;
5107         if (opos != -1 && vseqpos != opos)
5108         {
5109           // remove from old position
5110           ds.deleteSequence(dsq);
5111         }
5112         if (vseqpos < ds.getHeight())
5113         {
5114           if (vseqpos != opos)
5115           {
5116             // save sequence at destination position
5117             tseq = ds.getSequenceAt(vseqpos);
5118             ds.replaceSequenceAt(vseqpos, dsq);
5119             ds.addSequence(tseq);
5120           }
5121         }
5122         else
5123         {
5124           ds.addSequence(dsq);
5125         }
5126       }
5127     }
5128   }
5129
5130   /*
5131    * TODO use AlignmentI here and in related methods - needs
5132    * AlignmentI.getDataset() changed to return AlignmentI instead of Alignment
5133    */
5134   Hashtable<String, AlignmentI> datasetIds = null;
5135
5136   IdentityHashMap<AlignmentI, String> dataset2Ids = null;
5137
5138   private AlignmentI getDatasetFor(String datasetId)
5139   {
5140     if (datasetIds == null)
5141     {
5142       datasetIds = new Hashtable<String, AlignmentI>();
5143       return null;
5144     }
5145     if (datasetIds.containsKey(datasetId))
5146     {
5147       return datasetIds.get(datasetId);
5148     }
5149     return null;
5150   }
5151
5152   private void addDatasetRef(String datasetId, AlignmentI dataset)
5153   {
5154     if (datasetIds == null)
5155     {
5156       datasetIds = new Hashtable<String, AlignmentI>();
5157     }
5158     datasetIds.put(datasetId, dataset);
5159   }
5160
5161   /**
5162    * make a new dataset ID for this jalview dataset alignment
5163    * 
5164    * @param dataset
5165    * @return
5166    */
5167   private String getDatasetIdRef(AlignmentI dataset)
5168   {
5169     if (dataset.getDataset() != null)
5170     {
5171       warn("Serious issue!  Dataset Object passed to getDatasetIdRef is not a Jalview DATASET alignment...");
5172     }
5173     String datasetId = makeHashCode(dataset, null);
5174     if (datasetId == null)
5175     {
5176       // make a new datasetId and record it
5177       if (dataset2Ids == null)
5178       {
5179         dataset2Ids = new IdentityHashMap<AlignmentI, String>();
5180       }
5181       else
5182       {
5183         datasetId = dataset2Ids.get(dataset);
5184       }
5185       if (datasetId == null)
5186       {
5187         datasetId = "ds" + dataset2Ids.size() + 1;
5188         dataset2Ids.put(dataset, datasetId);
5189       }
5190     }
5191     return datasetId;
5192   }
5193
5194   private void addDBRefs(SequenceI datasetSequence, Sequence sequence)
5195   {
5196     for (int d = 0; d < sequence.getDBRefCount(); d++)
5197     {
5198       DBRef dr = sequence.getDBRef(d);
5199       jalview.datamodel.DBRefEntry entry = new jalview.datamodel.DBRefEntry(
5200               sequence.getDBRef(d).getSource(), sequence.getDBRef(d)
5201                       .getVersion(), sequence.getDBRef(d).getAccessionId());
5202       if (dr.getMapping() != null)
5203       {
5204         entry.setMap(addMapping(dr.getMapping()));
5205       }
5206       datasetSequence.addDBRef(entry);
5207     }
5208   }
5209
5210   private jalview.datamodel.Mapping addMapping(Mapping m)
5211   {
5212     SequenceI dsto = null;
5213     // Mapping m = dr.getMapping();
5214     int fr[] = new int[m.getMapListFromCount() * 2];
5215     Enumeration f = m.enumerateMapListFrom();
5216     for (int _i = 0; f.hasMoreElements(); _i += 2)
5217     {
5218       MapListFrom mf = (MapListFrom) f.nextElement();
5219       fr[_i] = mf.getStart();
5220       fr[_i + 1] = mf.getEnd();
5221     }
5222     int fto[] = new int[m.getMapListToCount() * 2];
5223     f = m.enumerateMapListTo();
5224     for (int _i = 0; f.hasMoreElements(); _i += 2)
5225     {
5226       MapListTo mf = (MapListTo) f.nextElement();
5227       fto[_i] = mf.getStart();
5228       fto[_i + 1] = mf.getEnd();
5229     }
5230     jalview.datamodel.Mapping jmap = new jalview.datamodel.Mapping(dsto,
5231             fr, fto, (int) m.getMapFromUnit(), (int) m.getMapToUnit());
5232     if (m.getMappingChoice() != null)
5233     {
5234       MappingChoice mc = m.getMappingChoice();
5235       if (mc.getDseqFor() != null)
5236       {
5237         String dsfor = "" + mc.getDseqFor();
5238         if (seqRefIds.containsKey(dsfor))
5239         {
5240           /**
5241            * recover from hash
5242            */
5243           jmap.setTo(seqRefIds.get(dsfor));
5244         }
5245         else
5246         {
5247           frefedSequence.add(newMappingRef(dsfor, jmap));
5248         }
5249       }
5250       else
5251       {
5252         /**
5253          * local sequence definition
5254          */
5255         Sequence ms = mc.getSequence();
5256         SequenceI djs = null;
5257         String sqid = ms.getDsseqid();
5258         if (sqid != null && sqid.length() > 0)
5259         {
5260           /*
5261            * recover dataset sequence
5262            */
5263           djs = seqRefIds.get(sqid);
5264         }
5265         else
5266         {
5267           System.err
5268                   .println("Warning - making up dataset sequence id for DbRef sequence map reference");
5269           sqid = ((Object) ms).toString(); // make up a new hascode for
5270           // undefined dataset sequence hash
5271           // (unlikely to happen)
5272         }
5273
5274         if (djs == null)
5275         {
5276           /**
5277            * make a new dataset sequence and add it to refIds hash
5278            */
5279           djs = new jalview.datamodel.Sequence(ms.getName(),
5280                   ms.getSequence());
5281           djs.setStart(jmap.getMap().getToLowest());
5282           djs.setEnd(jmap.getMap().getToHighest());
5283           djs.setVamsasId(uniqueSetSuffix + sqid);
5284           jmap.setTo(djs);
5285           incompleteSeqs.put(sqid, djs);
5286           seqRefIds.put(sqid, djs);
5287
5288         }
5289         jalview.bin.Cache.log.debug("about to recurse on addDBRefs.");
5290         addDBRefs(djs, ms);
5291
5292       }
5293     }
5294     return (jmap);
5295
5296   }
5297
5298   public jalview.gui.AlignmentPanel copyAlignPanel(AlignmentPanel ap,
5299           boolean keepSeqRefs)
5300   {
5301     initSeqRefs();
5302     JalviewModel jm = saveState(ap, null, null, null);
5303
5304     if (!keepSeqRefs)
5305     {
5306       clearSeqRefs();
5307       jm.getJalviewModelSequence().getViewport(0).setSequenceSetId(null);
5308     }
5309     else
5310     {
5311       uniqueSetSuffix = "";
5312       jm.getJalviewModelSequence().getViewport(0).setId(null); // we don't
5313       // overwrite the
5314       // view we just
5315       // copied
5316     }
5317     if (this.frefedSequence == null)
5318     {
5319       frefedSequence = new Vector();
5320     }
5321
5322     viewportsAdded.clear();
5323
5324     AlignFrame af = loadFromObject(jm, null, false, null);
5325     af.alignPanels.clear();
5326     af.closeMenuItem_actionPerformed(true);
5327
5328     /*
5329      * if(ap.av.getAlignment().getAlignmentAnnotation()!=null) { for(int i=0;
5330      * i<ap.av.getAlignment().getAlignmentAnnotation().length; i++) {
5331      * if(!ap.av.getAlignment().getAlignmentAnnotation()[i].autoCalculated) {
5332      * af.alignPanel.av.getAlignment().getAlignmentAnnotation()[i] =
5333      * ap.av.getAlignment().getAlignmentAnnotation()[i]; } } }
5334      */
5335
5336     return af.alignPanel;
5337   }
5338
5339   /**
5340    * flag indicating if hashtables should be cleared on finalization TODO this
5341    * flag may not be necessary
5342    */
5343   private final boolean _cleartables = true;
5344
5345   private Hashtable jvids2vobj;
5346
5347   /*
5348    * (non-Javadoc)
5349    * 
5350    * @see java.lang.Object#finalize()
5351    */
5352   @Override
5353   protected void finalize() throws Throwable
5354   {
5355     // really make sure we have no buried refs left.
5356     if (_cleartables)
5357     {
5358       clearSeqRefs();
5359     }
5360     this.seqRefIds = null;
5361     this.seqsToIds = null;
5362     super.finalize();
5363   }
5364
5365   private void warn(String msg)
5366   {
5367     warn(msg, null);
5368   }
5369
5370   private void warn(String msg, Exception e)
5371   {
5372     if (Cache.log != null)
5373     {
5374       if (e != null)
5375       {
5376         Cache.log.warn(msg, e);
5377       }
5378       else
5379       {
5380         Cache.log.warn(msg);
5381       }
5382     }
5383     else
5384     {
5385       System.err.println("Warning: " + msg);
5386       if (e != null)
5387       {
5388         e.printStackTrace();
5389       }
5390     }
5391   }
5392
5393   private void debug(String string)
5394   {
5395     debug(string, null);
5396   }
5397
5398   private void debug(String msg, Exception e)
5399   {
5400     if (Cache.log != null)
5401     {
5402       if (e != null)
5403       {
5404         Cache.log.debug(msg, e);
5405       }
5406       else
5407       {
5408         Cache.log.debug(msg);
5409       }
5410     }
5411     else
5412     {
5413       System.err.println("Warning: " + msg);
5414       if (e != null)
5415       {
5416         e.printStackTrace();
5417       }
5418     }
5419   }
5420
5421   /**
5422    * set the object to ID mapping tables used to write/recover objects and XML
5423    * ID strings for the jalview project. If external tables are provided then
5424    * finalize and clearSeqRefs will not clear the tables when the Jalview2XML
5425    * object goes out of scope. - also populates the datasetIds hashtable with
5426    * alignment objects containing dataset sequences
5427    * 
5428    * @param vobj2jv
5429    *          Map from ID strings to jalview datamodel
5430    * @param jv2vobj
5431    *          Map from jalview datamodel to ID strings
5432    * 
5433    * 
5434    */
5435   public void setObjectMappingTables(Hashtable vobj2jv,
5436           IdentityHashMap jv2vobj)
5437   {
5438     this.jv2vobj = jv2vobj;
5439     this.vobj2jv = vobj2jv;
5440     Iterator ds = jv2vobj.keySet().iterator();
5441     String id;
5442     while (ds.hasNext())
5443     {
5444       Object jvobj = ds.next();
5445       id = jv2vobj.get(jvobj).toString();
5446       if (jvobj instanceof jalview.datamodel.Alignment)
5447       {
5448         if (((jalview.datamodel.Alignment) jvobj).getDataset() == null)
5449         {
5450           addDatasetRef(id, (jalview.datamodel.Alignment) jvobj);
5451         }
5452       }
5453       else if (jvobj instanceof jalview.datamodel.Sequence)
5454       {
5455         // register sequence object so the XML parser can recover it.
5456         if (seqRefIds == null)
5457         {
5458           seqRefIds = new HashMap<String, SequenceI>();
5459         }
5460         if (seqsToIds == null)
5461         {
5462           seqsToIds = new IdentityHashMap<SequenceI, String>();
5463         }
5464         seqRefIds.put(jv2vobj.get(jvobj).toString(), (SequenceI) jvobj);
5465         seqsToIds.put((SequenceI) jvobj, id);
5466       }
5467       else if (jvobj instanceof jalview.datamodel.AlignmentAnnotation)
5468       {
5469         String anid;
5470         AlignmentAnnotation jvann = (AlignmentAnnotation) jvobj;
5471         annotationIds.put(anid = jv2vobj.get(jvobj).toString(), jvann);
5472         if (jvann.annotationId == null)
5473         {
5474           jvann.annotationId = anid;
5475         }
5476         if (!jvann.annotationId.equals(anid))
5477         {
5478           // TODO verify that this is the correct behaviour
5479           this.warn("Overriding Annotation ID for " + anid
5480                   + " from different id : " + jvann.annotationId);
5481           jvann.annotationId = anid;
5482         }
5483       }
5484       else if (jvobj instanceof String)
5485       {
5486         if (jvids2vobj == null)
5487         {
5488           jvids2vobj = new Hashtable();
5489           jvids2vobj.put(jvobj, jv2vobj.get(jvobj).toString());
5490         }
5491       }
5492       else
5493       {
5494         Cache.log.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
5495       }
5496     }
5497   }
5498
5499   /**
5500    * set the uniqueSetSuffix used to prefix/suffix object IDs for jalview
5501    * objects created from the project archive. If string is null (default for
5502    * construction) then suffix will be set automatically.
5503    * 
5504    * @param string
5505    */
5506   public void setUniqueSetSuffix(String string)
5507   {
5508     uniqueSetSuffix = string;
5509
5510   }
5511
5512   /**
5513    * uses skipList2 as the skipList for skipping views on sequence sets
5514    * associated with keys in the skipList
5515    * 
5516    * @param skipList2
5517    */
5518   public void setSkipList(Hashtable skipList2)
5519   {
5520     skipList = skipList2;
5521   }
5522
5523   /**
5524    * Reads the jar entry of given name and returns its contents, or null if the
5525    * entry is not found.
5526    * 
5527    * @param jprovider
5528    * @param jarEntryName
5529    * @return
5530    */
5531   protected String readJarEntry(jarInputStreamProvider jprovider,
5532           String jarEntryName)
5533   {
5534     String result = null;
5535     BufferedReader in = null;
5536
5537     try
5538     {
5539       /*
5540        * Reopen the jar input stream and traverse its entries to find a matching
5541        * name
5542        */
5543       JarInputStream jin = jprovider.getJarInputStream();
5544       JarEntry entry = null;
5545       do
5546       {
5547         entry = jin.getNextJarEntry();
5548       } while (entry != null && !entry.getName().equals(jarEntryName));
5549
5550       if (entry != null)
5551       {
5552         StringBuilder out = new StringBuilder(256);
5553         in = new BufferedReader(new InputStreamReader(jin, UTF_8));
5554         String data;
5555
5556         while ((data = in.readLine()) != null)
5557         {
5558           out.append(data);
5559         }
5560         result = out.toString();
5561       }
5562       else
5563       {
5564         warn("Couldn't find entry in Jalview Jar for " + jarEntryName);
5565       }
5566     } catch (Exception ex)
5567     {
5568       ex.printStackTrace();
5569     } finally
5570     {
5571       if (in != null)
5572       {
5573         try
5574         {
5575           in.close();
5576         } catch (IOException e)
5577         {
5578           // ignore
5579         }
5580       }
5581     }
5582
5583     return result;
5584   }
5585
5586   /**
5587    * Returns an incrementing counter (0, 1, 2...)
5588    * 
5589    * @return
5590    */
5591   private synchronized int nextCounter()
5592   {
5593     return counter++;
5594   }
5595 }