debug messages and graceful recovery when structure state is corrupted. apply gpl...
[jalview.git] / src / jalview / gui / Jalview2XML.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3  * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.gui;
20
21 import java.awt.Rectangle;
22 import java.io.*;
23 import java.net.*;
24 import java.util.*;
25 import java.util.jar.*;
26
27 import javax.swing.*;
28
29 import org.exolab.castor.xml.*;
30
31 import uk.ac.vamsas.objects.utils.MapList;
32 import jalview.bin.Cache;
33 import jalview.datamodel.Alignment;
34 import jalview.datamodel.AlignmentI;
35 import jalview.datamodel.SequenceI;
36 import jalview.schemabinding.version2.*;
37 import jalview.schemes.*;
38 import jalview.structure.StructureSelectionManager;
39 import jalview.util.jarInputStreamProvider;
40
41 /**
42  * Write out the current jalview desktop state as a Jalview XML stream.
43  * 
44  * Note: the vamsas objects referred to here are primitive versions of the
45  * VAMSAS project schema elements - they are not the same and most likely never
46  * will be :)
47  * 
48  * @author $author$
49  * @version $Revision$
50  */
51 public class Jalview2XML
52 {
53   /**
54    * create/return unique hash string for sq
55    * 
56    * @param sq
57    * @return new or existing unique string for sq
58    */
59   String seqHash(SequenceI sq)
60   {
61     if (seqsToIds == null)
62     {
63       initSeqRefs();
64     }
65     if (seqsToIds.containsKey(sq))
66     {
67       return (String) seqsToIds.get(sq);
68     }
69     else
70     {
71       // create sequential key
72       String key = "sq" + (seqsToIds.size() + 1);
73       key = makeHashCode(sq, key); // check we don't have an external reference
74       // for it already.
75       seqsToIds.put(sq, key);
76       return key;
77     }
78   }
79
80   void clearSeqRefs()
81   {
82     if (_cleartables)
83     {
84       if (seqRefIds != null)
85       {
86         seqRefIds.clear();
87       }
88       if (seqsToIds != null)
89       {
90         seqsToIds.clear();
91       }
92       // seqRefIds = null;
93       // seqsToIds = null;
94     }
95     else
96     {
97       // do nothing
98       warn("clearSeqRefs called when _cleartables was not set. Doing nothing.");
99       // seqRefIds = new Hashtable();
100       // seqsToIds = new IdentityHashMap();
101     }
102   }
103
104   void initSeqRefs()
105   {
106     if (seqsToIds == null)
107     {
108       seqsToIds = new IdentityHashMap();
109     }
110     if (seqRefIds == null)
111     {
112       seqRefIds = new Hashtable();
113     }
114   }
115
116   /**
117    * SequenceI reference -> XML ID string in jalview XML. Populated as XML reps
118    * of sequence objects are created.
119    */
120   java.util.IdentityHashMap seqsToIds = null;
121
122   /**
123    * jalview XML Sequence ID to jalview sequence object reference (both dataset
124    * and alignment sequences. Populated as XML reps of sequence objects are
125    * created.)
126    */
127   java.util.Hashtable seqRefIds = null; // key->SequenceI resolution
128
129   Vector frefedSequence = null;
130
131   boolean raiseGUI = true; // whether errors are raised in dialog boxes or not
132
133   public Jalview2XML()
134   {
135   }
136
137   public Jalview2XML(boolean raiseGUI)
138   {
139     this.raiseGUI = raiseGUI;
140   }
141
142   public void resolveFrefedSequences()
143   {
144     if (frefedSequence.size() > 0)
145     {
146       int r = 0, rSize = frefedSequence.size();
147       while (r < rSize)
148       {
149         Object[] ref = (Object[]) frefedSequence.elementAt(r);
150         if (ref != null)
151         {
152           String sref = (String) ref[0];
153           if (seqRefIds.containsKey(sref))
154           {
155             if (ref[1] instanceof jalview.datamodel.Mapping)
156             {
157               SequenceI seq = (SequenceI) seqRefIds.get(sref);
158               while (seq.getDatasetSequence() != null)
159               {
160                 seq = seq.getDatasetSequence();
161               }
162               ((jalview.datamodel.Mapping) ref[1]).setTo(seq);
163             }
164             else
165             {
166               if (ref[1] instanceof jalview.datamodel.AlignedCodonFrame)
167               {
168                 SequenceI seq = (SequenceI) seqRefIds.get(sref);
169                 while (seq.getDatasetSequence() != null)
170                 {
171                   seq = seq.getDatasetSequence();
172                 }
173                 if (ref[2] != null
174                         && ref[2] instanceof jalview.datamodel.Mapping)
175                 {
176                   jalview.datamodel.Mapping mp = (jalview.datamodel.Mapping) ref[2];
177                   ((jalview.datamodel.AlignedCodonFrame) ref[1]).addMap(
178                           seq, mp.getTo(), mp.getMap());
179                 }
180                 else
181                 {
182                   System.err
183                           .println("IMPLEMENTATION ERROR: Unimplemented forward sequence references for AlcodonFrames involving "
184                                   + ref[2].getClass() + " type objects.");
185                 }
186               }
187               else
188               {
189                 System.err
190                         .println("IMPLEMENTATION ERROR: Unimplemented forward sequence references for "
191                                 + ref[1].getClass() + " type objects.");
192               }
193             }
194             frefedSequence.remove(r);
195             rSize--;
196           }
197           else
198           {
199             System.err
200                     .println("IMPLEMENTATION WARNING: Unresolved forward reference for hash string "
201                             + ref[0]
202                             + " with objecttype "
203                             + ref[1].getClass());
204             r++;
205           }
206         }
207         else
208         {
209           // empty reference
210           frefedSequence.remove(r);
211           rSize--;
212         }
213       }
214     }
215   }
216
217   /**
218    * This maintains a list of viewports, the key being the seqSetId. Important
219    * to set historyItem and redoList for multiple views
220    */
221   Hashtable viewportsAdded;
222
223   Hashtable annotationIds = new Hashtable();
224
225   String uniqueSetSuffix = "";
226
227   /**
228    * List of pdbfiles added to Jar
229    */
230   Vector pdbfiles = null;
231
232   // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE
233   public void SaveState(File statefile)
234   {
235     try
236     {
237       FileOutputStream fos = new FileOutputStream(statefile);
238       JarOutputStream jout = new JarOutputStream(fos);
239       SaveState(jout);
240
241     } catch (Exception e)
242     {
243       // TODO: inform user of the problem - they need to know if their data was
244       // not saved !
245       if (errorMessage == null)
246       {
247         errorMessage = "Couldn't write Jalview Archive to output file '"
248                 + statefile + "' - See console error log for details";
249       }
250       else
251       {
252         errorMessage += "(output file was '" + statefile + "')";
253       }
254       e.printStackTrace();
255     }
256     reportErrors();
257   }
258
259   /**
260    * Writes a jalview project archive to the given Jar output stream.
261    * 
262    * @param jout
263    */
264   public void SaveState(JarOutputStream jout)
265   {
266     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
267
268     if (frames == null)
269     {
270       return;
271     }
272
273     try
274     {
275
276       // NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS
277       // //////////////////////////////////////////////////
278       // NOTE ALSO new PrintWriter must be used for each new JarEntry
279       PrintWriter out = null;
280
281       Vector shortNames = new Vector();
282
283       // REVERSE ORDER
284       for (int i = frames.length - 1; i > -1; i--)
285       {
286         if (frames[i] instanceof AlignFrame)
287         {
288           AlignFrame af = (AlignFrame) frames[i];
289           // skip ?
290           if (skipList != null
291                   && skipList.containsKey(af.getViewport()
292                           .getSequenceSetId()))
293           {
294             continue;
295           }
296
297           String shortName = af.getTitle();
298
299           if (shortName.indexOf(File.separatorChar) > -1)
300           {
301             shortName = shortName.substring(shortName
302                     .lastIndexOf(File.separatorChar) + 1);
303           }
304
305           int count = 1;
306
307           while (shortNames.contains(shortName))
308           {
309             if (shortName.endsWith("_" + (count - 1)))
310             {
311               shortName = shortName
312                       .substring(0, shortName.lastIndexOf("_"));
313             }
314
315             shortName = shortName.concat("_" + count);
316             count++;
317           }
318
319           shortNames.addElement(shortName);
320
321           if (!shortName.endsWith(".xml"))
322           {
323             shortName = shortName + ".xml";
324           }
325
326           int ap, apSize = af.alignPanels.size();
327           for (ap = 0; ap < apSize; ap++)
328           {
329             AlignmentPanel apanel = (AlignmentPanel) af.alignPanels
330                     .elementAt(ap);
331             String fileName = apSize == 1 ? shortName : ap + shortName;
332             if (!fileName.endsWith(".xml"))
333             {
334               fileName = fileName + ".xml";
335             }
336
337             SaveState(apanel, fileName, jout);
338           }
339         }
340       }
341       try
342       {
343         jout.flush();
344       } catch (Exception foo)
345       {
346       }
347       ;
348       jout.close();
349     } catch (Exception ex)
350     {
351       // TODO: inform user of the problem - they need to know if their data was
352       // not saved !
353       if (errorMessage == null)
354       {
355         errorMessage = "Couldn't write Jalview Archive - see error output for details";
356       }
357       ex.printStackTrace();
358     }
359   }
360
361   // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW
362   public boolean SaveAlignment(AlignFrame af, String jarFile,
363           String fileName)
364   {
365     try
366     {
367       int ap, apSize = af.alignPanels.size();
368       FileOutputStream fos = new FileOutputStream(jarFile);
369       JarOutputStream jout = new JarOutputStream(fos);
370       for (ap = 0; ap < apSize; ap++)
371       {
372         AlignmentPanel apanel = (AlignmentPanel) af.alignPanels
373                 .elementAt(ap);
374         String jfileName = apSize == 1 ? fileName : fileName + ap;
375         if (!jfileName.endsWith(".xml"))
376         {
377           jfileName = jfileName + ".xml";
378         }
379         SaveState(apanel, jfileName, jout);
380       }
381
382       try
383       {
384         jout.flush();
385       } catch (Exception foo)
386       {
387       }
388       ;
389       jout.close();
390       return true;
391     } catch (Exception ex)
392     {
393       errorMessage = "Couldn't Write alignment view to Jalview Archive - see error output for details";
394       ex.printStackTrace();
395       return false;
396     }
397   }
398
399   /**
400    * create a JalviewModel from an algnment view and marshall it to a
401    * JarOutputStream
402    * 
403    * @param ap
404    *                panel to create jalview model for
405    * @param fileName
406    *                name of alignment panel written to output stream
407    * @param jout
408    *                jar output stream
409    * @param out
410    *                jar entry name
411    */
412   public JalviewModel SaveState(AlignmentPanel ap, String fileName,
413           JarOutputStream jout)
414   {
415     initSeqRefs();
416
417     Vector userColours = new Vector();
418
419     AlignViewport av = ap.av;
420
421     JalviewModel object = new JalviewModel();
422     object.setVamsasModel(new jalview.schemabinding.version2.VamsasModel());
423
424     object.setCreationDate(new java.util.Date(System.currentTimeMillis()));
425     object.setVersion(jalview.bin.Cache.getProperty("VERSION"));
426
427     jalview.datamodel.AlignmentI jal = av.alignment;
428
429     if (av.hasHiddenRows)
430     {
431       jal = jal.getHiddenSequences().getFullAlignment();
432     }
433
434     SequenceSet vamsasSet = new SequenceSet();
435     Sequence vamsasSeq;
436     JalviewModelSequence jms = new JalviewModelSequence();
437
438     vamsasSet.setGapChar(jal.getGapCharacter() + "");
439
440     if (jal.getDataset() != null)
441     {
442       // dataset id is the dataset's hashcode
443       vamsasSet.setDatasetId(getDatasetIdRef(jal.getDataset()));
444     }
445     if (jal.getProperties() != null)
446     {
447       Enumeration en = jal.getProperties().keys();
448       while (en.hasMoreElements())
449       {
450         String key = en.nextElement().toString();
451         SequenceSetProperties ssp = new SequenceSetProperties();
452         ssp.setKey(key);
453         ssp.setValue(jal.getProperties().get(key).toString());
454         vamsasSet.addSequenceSetProperties(ssp);
455       }
456     }
457
458     JSeq jseq;
459
460     // SAVE SEQUENCES
461     String id = "";
462     jalview.datamodel.SequenceI jds;
463     for (int i = 0; i < jal.getHeight(); i++)
464     {
465       jds = jal.getSequenceAt(i);
466       id = seqHash(jds);
467
468       if (seqRefIds.get(id) != null)
469       {
470         // This happens for two reasons: 1. multiple views are being serialised.
471         // 2. the hashCode has collided with another sequence's code. This DOES
472         // HAPPEN! (PF00072.15.stk does this)
473         // JBPNote: Uncomment to debug writing out of files that do not read
474         // back in due to ArrayOutOfBoundExceptions.
475         // System.err.println("vamsasSeq backref: "+id+"");
476         // System.err.println(jds.getName()+"
477         // "+jds.getStart()+"-"+jds.getEnd()+" "+jds.getSequenceAsString());
478         // System.err.println("Hashcode: "+seqHash(jds));
479         // SequenceI rsq = (SequenceI) seqRefIds.get(id + "");
480         // System.err.println(rsq.getName()+"
481         // "+rsq.getStart()+"-"+rsq.getEnd()+" "+rsq.getSequenceAsString());
482         // System.err.println("Hashcode: "+seqHash(rsq));
483       }
484       else
485       {
486         vamsasSeq = createVamsasSequence(id, jds);
487         vamsasSet.addSequence(vamsasSeq);
488         seqRefIds.put(id, jds);
489       }
490
491       jseq = new JSeq();
492       jseq.setStart(jds.getStart());
493       jseq.setEnd(jds.getEnd());
494       jseq.setColour(av.getSequenceColour(jds).getRGB());
495
496       jseq.setId(id); // jseq id should be a string not a number
497
498       if (av.hasHiddenRows)
499       {
500         jseq.setHidden(av.alignment.getHiddenSequences().isHidden(jds));
501
502         if (av.hiddenRepSequences != null
503                 && av.hiddenRepSequences.containsKey(jal.getSequenceAt(i)))
504         {
505           jalview.datamodel.SequenceI[] reps = ((jalview.datamodel.SequenceGroup) av.hiddenRepSequences
506                   .get(jal.getSequenceAt(i))).getSequencesInOrder(jal);
507
508           for (int h = 0; h < reps.length; h++)
509           {
510             if (reps[h] != jal.getSequenceAt(i))
511             {
512               jseq.addHiddenSequences(jal.findIndex(reps[h]));
513             }
514           }
515         }
516       }
517
518       if (jds.getDatasetSequence().getSequenceFeatures() != null)
519       {
520         jalview.datamodel.SequenceFeature[] sf = jds.getDatasetSequence()
521                 .getSequenceFeatures();
522         int index = 0;
523         while (index < sf.length)
524         {
525           Features features = new Features();
526
527           features.setBegin(sf[index].getBegin());
528           features.setEnd(sf[index].getEnd());
529           features.setDescription(sf[index].getDescription());
530           features.setType(sf[index].getType());
531           features.setFeatureGroup(sf[index].getFeatureGroup());
532           features.setScore(sf[index].getScore());
533           if (sf[index].links != null)
534           {
535             for (int l = 0; l < sf[index].links.size(); l++)
536             {
537               OtherData keyValue = new OtherData();
538               keyValue.setKey("LINK_" + l);
539               keyValue.setValue(sf[index].links.elementAt(l).toString());
540               features.addOtherData(keyValue);
541             }
542           }
543           if (sf[index].otherDetails != null)
544           {
545             String key;
546             Enumeration keys = sf[index].otherDetails.keys();
547             while (keys.hasMoreElements())
548             {
549               key = keys.nextElement().toString();
550               OtherData keyValue = new OtherData();
551               keyValue.setKey(key);
552               keyValue.setValue(sf[index].otherDetails.get(key).toString());
553               features.addOtherData(keyValue);
554             }
555           }
556
557           jseq.addFeatures(features);
558           index++;
559         }
560       }
561
562       if (jds.getDatasetSequence().getPDBId() != null)
563       {
564         Enumeration en = jds.getDatasetSequence().getPDBId().elements();
565         while (en.hasMoreElements())
566         {
567           Pdbids pdb = new Pdbids();
568           jalview.datamodel.PDBEntry entry = (jalview.datamodel.PDBEntry) en
569                   .nextElement();
570
571           pdb.setId(entry.getId());
572           pdb.setType(entry.getType());
573
574           AppJmol jmol;
575           // This must have been loaded, is it still visible?
576           JInternalFrame[] frames = Desktop.desktop.getAllFrames();
577           for (int f = frames.length - 1; f > -1; f--)
578           {
579             if (frames[f] instanceof AppJmol)
580             {
581               jmol = (AppJmol) frames[f];
582               if (!jmol.pdbentry.getId().equals(entry.getId()) 
583                       && !(entry.getId().length()>4 
584                               && entry.getId().startsWith(jmol.pdbentry.getId())))
585                 continue;
586
587               StructureState state = new StructureState();
588               state.setVisible(true);
589               state.setXpos(jmol.getX());
590               state.setYpos(jmol.getY());
591               state.setWidth(jmol.getWidth());
592               state.setHeight(jmol.getHeight());
593               state.setViewId(jmol.getViewId());
594               String statestring = jmol.viewer.getStateInfo();
595               if (state != null)
596               {
597                 state.setContent(statestring.replaceAll("\n", ""));
598               }
599               for (int s = 0; s < jmol.sequence.length; s++)
600               {
601                 if (jal.findIndex(jmol.sequence[s]) > -1)
602                 {
603                   pdb.addStructureState(state);
604                 }
605               }
606             }
607           }
608
609           if (entry.getFile() != null)
610           {
611             pdb.setFile(entry.getFile());
612             if (pdbfiles == null)
613             {
614               pdbfiles = new Vector();
615             }
616
617             if (!pdbfiles.contains(entry.getId()))
618             {
619               pdbfiles.addElement(entry.getId());
620               try
621               {
622                 File file = new File(entry.getFile());
623                 if (file.exists() && jout != null)
624                 {
625                   byte[] data = new byte[(int) file.length()];
626                   jout.putNextEntry(new JarEntry(entry.getId()));
627                   DataInputStream dis = new DataInputStream(
628                           new FileInputStream(file));
629                   dis.readFully(data);
630
631                   DataOutputStream dout = new DataOutputStream(jout);
632                   dout.write(data, 0, data.length);
633                   dout.flush();
634                   jout.closeEntry();
635                 }
636               } catch (Exception ex)
637               {
638                 ex.printStackTrace();
639               }
640
641             }
642           }
643
644           if (entry.getProperty() != null)
645           {
646             PdbentryItem item = new PdbentryItem();
647             Hashtable properties = entry.getProperty();
648             Enumeration en2 = properties.keys();
649             while (en2.hasMoreElements())
650             {
651               Property prop = new Property();
652               String key = en2.nextElement().toString();
653               prop.setName(key);
654               prop.setValue(properties.get(key).toString());
655               item.addProperty(prop);
656             }
657             pdb.addPdbentryItem(item);
658           }
659
660           jseq.addPdbids(pdb);
661         }
662       }
663
664       jms.addJSeq(jseq);
665     }
666
667     if (av.hasHiddenRows)
668     {
669       jal = av.alignment;
670     }
671     // SAVE MAPPINGS
672     if (jal.getCodonFrames() != null && jal.getCodonFrames().length > 0)
673     {
674       jalview.datamodel.AlignedCodonFrame[] jac = jal.getCodonFrames();
675       for (int i = 0; i < jac.length; i++)
676       {
677         AlcodonFrame alc = new AlcodonFrame();
678         vamsasSet.addAlcodonFrame(alc);
679         for (int p = 0; p < jac[i].aaWidth; p++)
680         {
681           Alcodon cmap = new Alcodon();
682           if (jac[i].codons[p]!=null)
683           {
684             // Null codons indicate a gapped column in the translated peptide alignment. 
685             cmap.setPos1(jac[i].codons[p][0]);
686             cmap.setPos2(jac[i].codons[p][1]);
687             cmap.setPos3(jac[i].codons[p][2]);
688           }
689           alc.addAlcodon(cmap);
690         }
691         if (jac[i].getProtMappings() != null
692                 && jac[i].getProtMappings().length > 0)
693         {
694           SequenceI[] dnas = jac[i].getdnaSeqs();
695           jalview.datamodel.Mapping[] pmaps = jac[i].getProtMappings();
696           for (int m = 0; m < pmaps.length; m++)
697           {
698             AlcodMap alcmap = new AlcodMap();
699             alcmap.setDnasq(seqHash(dnas[m])); 
700             alcmap.setMapping(createVamsasMapping(pmaps[m], dnas[m], null,
701                     false));
702             alc.addAlcodMap(alcmap);
703           }
704         }
705       }
706     }
707
708     // SAVE TREES
709     // /////////////////////////////////
710     if (av.currentTree != null)
711     {
712       // FIND ANY ASSOCIATED TREES
713       // NOT IMPLEMENTED FOR HEADLESS STATE AT PRESENT
714       if (Desktop.desktop != null)
715       {
716         JInternalFrame[] frames = Desktop.desktop.getAllFrames();
717
718         for (int t = 0; t < frames.length; t++)
719         {
720           if (frames[t] instanceof TreePanel)
721           {
722             TreePanel tp = (TreePanel) frames[t];
723
724             if (tp.treeCanvas.av.alignment == jal)
725             {
726               Tree tree = new Tree();
727               tree.setTitle(tp.getTitle());
728               tree.setCurrentTree((av.currentTree == tp.getTree()));
729               tree.setNewick(tp.getTree().toString());
730               tree.setThreshold(tp.treeCanvas.threshold);
731
732               tree.setFitToWindow(tp.fitToWindow.getState());
733               tree.setFontName(tp.getTreeFont().getName());
734               tree.setFontSize(tp.getTreeFont().getSize());
735               tree.setFontStyle(tp.getTreeFont().getStyle());
736               tree.setMarkUnlinked(tp.placeholdersMenu.getState());
737
738               tree.setShowBootstrap(tp.bootstrapMenu.getState());
739               tree.setShowDistances(tp.distanceMenu.getState());
740
741               tree.setHeight(tp.getHeight());
742               tree.setWidth(tp.getWidth());
743               tree.setXpos(tp.getX());
744               tree.setYpos(tp.getY());
745               tree.setId(makeHashCode(tp, null));
746               jms.addTree(tree);
747             }
748           }
749         }
750       }
751     }
752
753     // SAVE ANNOTATIONS
754     if (jal.getAlignmentAnnotation() != null)
755     {
756       jalview.datamodel.AlignmentAnnotation[] aa = jal
757               .getAlignmentAnnotation();
758
759       for (int i = 0; i < aa.length; i++)
760       {
761         Annotation an = new Annotation();
762
763         if (aa[i].annotationId != null)
764         {
765           annotationIds.put(aa[i].annotationId, aa[i]);
766         }
767
768         an.setId(aa[i].annotationId);
769
770         if (aa[i] == av.quality || aa[i] == av.conservation
771                 || aa[i] == av.consensus)
772         {
773           an.setLabel(aa[i].label);
774           an.setGraph(true);
775           vamsasSet.addAnnotation(an);
776           continue;
777         }
778
779         an.setVisible(aa[i].visible);
780
781         an.setDescription(aa[i].description);
782
783         if (aa[i].sequenceRef != null)
784         {
785           // TODO later annotation sequenceRef should be the XML ID of the
786           // sequence rather than its display name
787           an.setSequenceRef(aa[i].sequenceRef.getName());
788         }
789
790         if (aa[i].graph > 0)
791         {
792           an.setGraph(true);
793           an.setGraphType(aa[i].graph);
794           an.setGraphGroup(aa[i].graphGroup);
795           if (aa[i].getThreshold() != null)
796           {
797             ThresholdLine line = new ThresholdLine();
798             line.setLabel(aa[i].getThreshold().label);
799             line.setValue(aa[i].getThreshold().value);
800             line.setColour(aa[i].getThreshold().colour.getRGB());
801             an.setThresholdLine(line);
802           }
803         }
804         else
805         {
806           an.setGraph(false);
807         }
808
809         an.setLabel(aa[i].label);
810         if (aa[i].hasScore())
811         {
812           an.setScore(aa[i].getScore());
813         }
814         AnnotationElement ae;
815         if (aa[i].annotations != null)
816         {
817           an.setScoreOnly(false);
818           for (int a = 0; a < aa[i].annotations.length; a++)
819           {
820             if ((aa[i] == null) || (aa[i].annotations[a] == null))
821             {
822               continue;
823             }
824
825             ae = new AnnotationElement();
826             if (aa[i].annotations[a].description != null)
827               ae.setDescription(aa[i].annotations[a].description);
828             if (aa[i].annotations[a].displayCharacter != null)
829               ae.setDisplayCharacter(aa[i].annotations[a].displayCharacter);
830
831             if (!Float.isNaN(aa[i].annotations[a].value))
832               ae.setValue(aa[i].annotations[a].value);
833
834             ae.setPosition(a);
835             if (aa[i].annotations[a].secondaryStructure != ' '
836                     && aa[i].annotations[a].secondaryStructure != '\0')
837               ae
838                       .setSecondaryStructure(aa[i].annotations[a].secondaryStructure
839                               + "");
840
841             if (aa[i].annotations[a].colour != null
842                     && aa[i].annotations[a].colour != java.awt.Color.black)
843             {
844               ae.setColour(aa[i].annotations[a].colour.getRGB());
845             }
846
847             an.addAnnotationElement(ae);
848           }
849         }
850         else
851         {
852           an.setScoreOnly(true);
853         }
854         vamsasSet.addAnnotation(an);
855       }
856     }
857
858     // SAVE GROUPS
859     if (jal.getGroups() != null)
860     {
861       JGroup[] groups = new JGroup[jal.getGroups().size()];
862
863       for (int i = 0; i < groups.length; i++)
864       {
865         groups[i] = new JGroup();
866
867         jalview.datamodel.SequenceGroup sg = (jalview.datamodel.SequenceGroup) jal
868                 .getGroups().elementAt(i);
869         groups[i].setStart(sg.getStartRes());
870         groups[i].setEnd(sg.getEndRes());
871         groups[i].setName(sg.getName()); // TODO later sequence group should
872         // specify IDs of sequences, not just
873         // names
874         if (sg.cs != null)
875         {
876           if (sg.cs.conservationApplied())
877           {
878             groups[i].setConsThreshold(sg.cs.getConservationInc());
879
880             if (sg.cs instanceof jalview.schemes.UserColourScheme)
881             {
882               groups[i].setColour(SetUserColourScheme(sg.cs, userColours,
883                       jms));
884             }
885             else
886             {
887               groups[i]
888                       .setColour(ColourSchemeProperty.getColourName(sg.cs));
889             }
890           }
891           else if (sg.cs instanceof jalview.schemes.AnnotationColourGradient)
892           {
893             groups[i]
894                     .setColour(ColourSchemeProperty
895                             .getColourName(((jalview.schemes.AnnotationColourGradient) sg.cs)
896                                     .getBaseColour()));
897           }
898           else if (sg.cs instanceof jalview.schemes.UserColourScheme)
899           {
900             groups[i]
901                     .setColour(SetUserColourScheme(sg.cs, userColours, jms));
902           }
903           else
904           {
905             groups[i].setColour(ColourSchemeProperty.getColourName(sg.cs));
906           }
907
908           groups[i].setPidThreshold(sg.cs.getThreshold());
909         }
910
911         groups[i].setOutlineColour(sg.getOutlineColour().getRGB());
912         groups[i].setDisplayBoxes(sg.getDisplayBoxes());
913         groups[i].setDisplayText(sg.getDisplayText());
914         groups[i].setColourText(sg.getColourText());
915         groups[i].setTextCol1(sg.textColour.getRGB());
916         groups[i].setTextCol2(sg.textColour2.getRGB());
917         groups[i].setTextColThreshold(sg.thresholdTextColour);
918         groups[i].setShowUnconserved(sg.getShowunconserved());
919         for (int s = 0; s < sg.getSize(); s++)
920         {
921           jalview.datamodel.Sequence seq = (jalview.datamodel.Sequence) sg
922                   .getSequenceAt(s);
923           groups[i].addSeq(seqHash(seq));
924         }
925       }
926
927       jms.setJGroup(groups);
928     }
929
930     // /////////SAVE VIEWPORT
931     Viewport view = new Viewport();
932     view.setTitle(ap.alignFrame.getTitle());
933     view.setSequenceSetId(makeHashCode(av.getSequenceSetId(), av
934             .getSequenceSetId()));
935     view.setId(av.getViewId());
936     view.setViewName(av.viewName);
937     view.setGatheredViews(av.gatherViewsHere);
938
939     if (ap.av.explodedPosition != null)
940     {
941       view.setXpos(av.explodedPosition.x);
942       view.setYpos(av.explodedPosition.y);
943       view.setWidth(av.explodedPosition.width);
944       view.setHeight(av.explodedPosition.height);
945     }
946     else
947     {
948       view.setXpos(ap.alignFrame.getBounds().x);
949       view.setYpos(ap.alignFrame.getBounds().y);
950       view.setWidth(ap.alignFrame.getBounds().width);
951       view.setHeight(ap.alignFrame.getBounds().height);
952     }
953
954     view.setStartRes(av.startRes);
955     view.setStartSeq(av.startSeq);
956
957     if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme)
958     {
959       view.setBgColour(SetUserColourScheme(av.getGlobalColourScheme(),
960               userColours, jms));
961     }
962     else if (av.getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient)
963     {
964       jalview.schemes.AnnotationColourGradient acg = (jalview.schemes.AnnotationColourGradient) av
965               .getGlobalColourScheme();
966
967       AnnotationColours ac = new AnnotationColours();
968       ac.setAboveThreshold(acg.getAboveThreshold());
969       ac.setThreshold(acg.getAnnotationThreshold());
970       ac.setAnnotation(acg.getAnnotation());
971       if (acg.getBaseColour() instanceof jalview.schemes.UserColourScheme)
972       {
973         ac.setColourScheme(SetUserColourScheme(acg.getBaseColour(),
974                 userColours, jms));
975       }
976       else
977       {
978         ac.setColourScheme(ColourSchemeProperty.getColourName(acg
979                 .getBaseColour()));
980       }
981
982       ac.setMaxColour(acg.getMaxColour().getRGB());
983       ac.setMinColour(acg.getMinColour().getRGB());
984       view.setAnnotationColours(ac);
985       view.setBgColour("AnnotationColourGradient");
986     }
987     else
988     {
989       view.setBgColour(ColourSchemeProperty.getColourName(av
990               .getGlobalColourScheme()));
991     }
992
993     ColourSchemeI cs = av.getGlobalColourScheme();
994
995     if (cs != null)
996     {
997       if (cs.conservationApplied())
998       {
999         view.setConsThreshold(cs.getConservationInc());
1000         if (cs instanceof jalview.schemes.UserColourScheme)
1001         {
1002           view.setBgColour(SetUserColourScheme(cs, userColours, jms));
1003         }
1004       }
1005
1006       if (cs instanceof ResidueColourScheme)
1007       {
1008         view.setPidThreshold(cs.getThreshold());
1009       }
1010     }
1011
1012     view.setConservationSelected(av.getConservationSelected());
1013     view.setPidSelected(av.getAbovePIDThreshold());
1014     view.setFontName(av.font.getName());
1015     view.setFontSize(av.font.getSize());
1016     view.setFontStyle(av.font.getStyle());
1017     view.setRenderGaps(av.renderGaps);
1018     view.setShowAnnotation(av.getShowAnnotation());
1019     view.setShowBoxes(av.getShowBoxes());
1020     view.setShowColourText(av.getColourText());
1021     view.setShowFullId(av.getShowJVSuffix());
1022     view.setRightAlignIds(av.rightAlignIds);
1023     view.setShowSequenceFeatures(av.showSequenceFeatures);
1024     view.setShowText(av.getShowText());
1025     view.setShowUnconserved(av.getShowUnconserved());
1026     view.setWrapAlignment(av.getWrapAlignment());
1027     view.setTextCol1(av.textColour.getRGB());
1028     view.setTextCol2(av.textColour2.getRGB());
1029     view.setTextColThreshold(av.thresholdTextColour);
1030
1031     if (av.featuresDisplayed != null)
1032     {
1033       jalview.schemabinding.version2.FeatureSettings fs = new jalview.schemabinding.version2.FeatureSettings();
1034
1035       String[] renderOrder = ap.seqPanel.seqCanvas.getFeatureRenderer().renderOrder;
1036
1037       Vector settingsAdded = new Vector();
1038       for (int ro = 0; ro < renderOrder.length; ro++)
1039       {
1040         Setting setting = new Setting();
1041         setting.setType(renderOrder[ro]);
1042         setting.setColour(ap.seqPanel.seqCanvas.getFeatureRenderer()
1043                 .getColour(renderOrder[ro]).getRGB());
1044
1045         setting.setDisplay(av.featuresDisplayed
1046                 .containsKey(renderOrder[ro]));
1047         float rorder = ap.seqPanel.seqCanvas.getFeatureRenderer().getOrder(
1048                 renderOrder[ro]);
1049         if (rorder > -1)
1050         {
1051           setting.setOrder(rorder);
1052         }
1053         fs.addSetting(setting);
1054         settingsAdded.addElement(renderOrder[ro]);
1055       }
1056
1057       // Make sure we save none displayed feature settings
1058       Enumeration en = ap.seqPanel.seqCanvas.getFeatureRenderer().featureColours
1059               .keys();
1060       while (en.hasMoreElements())
1061       {
1062         String key = en.nextElement().toString();
1063         if (settingsAdded.contains(key))
1064         {
1065           continue;
1066         }
1067
1068         Setting setting = new Setting();
1069         setting.setType(key);
1070         setting.setColour(ap.seqPanel.seqCanvas.getFeatureRenderer()
1071                 .getColour(key).getRGB());
1072
1073         setting.setDisplay(false);
1074         float rorder = ap.seqPanel.seqCanvas.getFeatureRenderer().getOrder(
1075                 key);
1076         if (rorder > -1)
1077         {
1078           setting.setOrder(rorder);
1079         }
1080         fs.addSetting(setting);
1081         settingsAdded.addElement(key);
1082       }
1083       en = ap.seqPanel.seqCanvas.getFeatureRenderer().featureGroups.keys();
1084       Vector groupsAdded = new Vector();
1085       while (en.hasMoreElements())
1086       {
1087         String grp = en.nextElement().toString();
1088         if (groupsAdded.contains(grp))
1089         {
1090           continue;
1091         }
1092         Group g = new Group();
1093         g.setName(grp);
1094         g
1095                 .setDisplay(((Boolean) ap.seqPanel.seqCanvas
1096                         .getFeatureRenderer().featureGroups.get(grp))
1097                         .booleanValue());
1098         fs.addGroup(g);
1099         groupsAdded.addElement(grp);
1100       }
1101       jms.setFeatureSettings(fs);
1102
1103     }
1104
1105     if (av.hasHiddenColumns)
1106     {
1107       for (int c = 0; c < av.getColumnSelection().getHiddenColumns().size(); c++)
1108       {
1109         int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
1110                 .elementAt(c);
1111         HiddenColumns hc = new HiddenColumns();
1112         hc.setStart(region[0]);
1113         hc.setEnd(region[1]);
1114         view.addHiddenColumns(hc);
1115       }
1116     }
1117
1118     jms.addViewport(view);
1119
1120     object.setJalviewModelSequence(jms);
1121     object.getVamsasModel().addSequenceSet(vamsasSet);
1122
1123     if (jout != null && fileName != null)
1124     {
1125       // We may not want to write the object to disk,
1126       // eg we can copy the alignViewport to a new view object
1127       // using save and then load
1128       try
1129       {
1130         JarEntry entry = new JarEntry(fileName);
1131         jout.putNextEntry(entry);
1132         PrintWriter pout = new PrintWriter(new OutputStreamWriter(jout,
1133                 "UTF-8"));
1134         org.exolab.castor.xml.Marshaller marshaller = new org.exolab.castor.xml.Marshaller(
1135                 pout);
1136         marshaller.marshal(object);
1137         pout.flush();
1138         jout.closeEntry();
1139       } catch (Exception ex)
1140       {
1141         // TODO: raise error in GUI if marshalling failed.
1142         ex.printStackTrace();
1143       }
1144     }
1145     return object;
1146   }
1147
1148   /**
1149    * External mapping between jalview objects and objects yielding a valid and
1150    * unique object ID string. This is null for normal Jalview project IO, but
1151    * non-null when a jalview project is being read or written as part of a
1152    * vamsas session.
1153    */
1154   IdentityHashMap jv2vobj = null;
1155
1156   /**
1157    * Construct a unique ID for jvobj using either existing bindings or if none
1158    * exist, the result of the hashcode call for the object.
1159    * 
1160    * @param jvobj
1161    *                jalview data object
1162    * @return unique ID for referring to jvobj
1163    */
1164   private String makeHashCode(Object jvobj, String altCode)
1165   {
1166     if (jv2vobj != null)
1167     {
1168       Object id = jv2vobj.get(jvobj);
1169       if (id != null)
1170       {
1171         return id.toString();
1172       }
1173       // check string ID mappings
1174       if (jvids2vobj != null && jvobj instanceof String)
1175       {
1176         id = jvids2vobj.get(jvobj);
1177       }
1178       if (id != null)
1179       {
1180         return id.toString();
1181       }
1182       // give up and warn that something has gone wrong
1183       warn("Cannot find ID for object in external mapping : " + jvobj);
1184     }
1185     return altCode;
1186   }
1187
1188   /**
1189    * return local jalview object mapped to ID, if it exists
1190    * 
1191    * @param idcode
1192    *                (may be null)
1193    * @return null or object bound to idcode
1194    */
1195   private Object retrieveExistingObj(String idcode)
1196   {
1197     if (idcode != null && vobj2jv != null)
1198     {
1199       return vobj2jv.get(idcode);
1200     }
1201     return null;
1202   }
1203
1204   /**
1205    * binding from ID strings from external mapping table to jalview data model
1206    * objects.
1207    */
1208   private Hashtable vobj2jv;
1209
1210   private Sequence createVamsasSequence(String id, SequenceI jds)
1211   {
1212     return createVamsasSequence(true, id, jds, null);
1213   }
1214
1215   private Sequence createVamsasSequence(boolean recurse, String id,
1216           SequenceI jds, SequenceI parentseq)
1217   {
1218     Sequence vamsasSeq = new Sequence();
1219     vamsasSeq.setId(id);
1220     vamsasSeq.setName(jds.getName());
1221     vamsasSeq.setSequence(jds.getSequenceAsString());
1222     vamsasSeq.setDescription(jds.getDescription());
1223     jalview.datamodel.DBRefEntry[] dbrefs = null;
1224     if (jds.getDatasetSequence() != null)
1225     {
1226       vamsasSeq.setDsseqid(seqHash(jds.getDatasetSequence()));
1227       if (jds.getDatasetSequence().getDBRef() != null)
1228       {
1229         dbrefs = jds.getDatasetSequence().getDBRef();
1230       }
1231     }
1232     else
1233     {
1234       vamsasSeq.setDsseqid(id); // so we can tell which sequences really are
1235       // dataset sequences only
1236       dbrefs = jds.getDBRef();
1237     }
1238     if (dbrefs != null)
1239     {
1240       for (int d = 0; d < dbrefs.length; d++)
1241       {
1242         DBRef dbref = new DBRef();
1243         dbref.setSource(dbrefs[d].getSource());
1244         dbref.setVersion(dbrefs[d].getVersion());
1245         dbref.setAccessionId(dbrefs[d].getAccessionId());
1246         if (dbrefs[d].hasMap())
1247         {
1248           Mapping mp = createVamsasMapping(dbrefs[d].getMap(), parentseq,
1249                   jds, recurse);
1250           dbref.setMapping(mp);
1251         }
1252         vamsasSeq.addDBRef(dbref);
1253       }
1254     }
1255     return vamsasSeq;
1256   }
1257
1258   private Mapping createVamsasMapping(jalview.datamodel.Mapping jmp,
1259           SequenceI parentseq, SequenceI jds, boolean recurse)
1260   {
1261     Mapping mp = null;
1262     if (jmp.getMap() != null)
1263     {
1264       mp = new Mapping();
1265
1266       jalview.util.MapList mlst = jmp.getMap();
1267       int r[] = mlst.getFromRanges();
1268       for (int s = 0; s < r.length; s += 2)
1269       {
1270         MapListFrom mfrom = new MapListFrom();
1271         mfrom.setStart(r[s]);
1272         mfrom.setEnd(r[s + 1]);
1273         mp.addMapListFrom(mfrom);
1274       }
1275       r = mlst.getToRanges();
1276       for (int s = 0; s < r.length; s += 2)
1277       {
1278         MapListTo mto = new MapListTo();
1279         mto.setStart(r[s]);
1280         mto.setEnd(r[s + 1]);
1281         mp.addMapListTo(mto);
1282       }
1283       mp.setMapFromUnit(mlst.getFromRatio());
1284       mp.setMapToUnit(mlst.getToRatio());
1285       if (jmp.getTo() != null)
1286       {
1287         MappingChoice mpc = new MappingChoice();
1288         if (recurse
1289                 && (parentseq != jmp.getTo() || parentseq
1290                         .getDatasetSequence() != jmp.getTo()))
1291         {
1292           mpc.setSequence(createVamsasSequence(false, seqHash(jmp.getTo()),
1293                   jmp.getTo(), jds));
1294         }
1295         else
1296         {
1297           String jmpid = "";
1298           SequenceI ps = null;
1299           if (parentseq != jmp.getTo()
1300                   && parentseq.getDatasetSequence() != jmp.getTo())
1301           {
1302             // chaining dbref rather than a handshaking one
1303             jmpid = seqHash(ps = jmp.getTo());
1304           }
1305           else
1306           {
1307             jmpid = seqHash(ps = parentseq);
1308           }
1309           mpc.setDseqFor(jmpid);
1310           if (!seqRefIds.containsKey(mpc.getDseqFor()))
1311           {
1312             jalview.bin.Cache.log.debug("creatign new DseqFor ID");
1313             seqRefIds.put(mpc.getDseqFor(), ps);
1314           }
1315           else
1316           {
1317             jalview.bin.Cache.log.debug("reusing DseqFor ID");
1318           }
1319         }
1320         mp.setMappingChoice(mpc);
1321       }
1322     }
1323     return mp;
1324   }
1325
1326   String SetUserColourScheme(jalview.schemes.ColourSchemeI cs,
1327           Vector userColours, JalviewModelSequence jms)
1328   {
1329     String id = null;
1330     jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs;
1331
1332     if (!userColours.contains(ucs))
1333     {
1334       userColours.add(ucs);
1335
1336       java.awt.Color[] colours = ucs.getColours();
1337       jalview.schemabinding.version2.UserColours uc = new jalview.schemabinding.version2.UserColours();
1338       jalview.schemabinding.version2.UserColourScheme jbucs = new jalview.schemabinding.version2.UserColourScheme();
1339
1340       for (int i = 0; i < colours.length; i++)
1341       {
1342         jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
1343         col.setName(ResidueProperties.aa[i]);
1344         col.setRGB(jalview.util.Format.getHexString(colours[i]));
1345         jbucs.addColour(col);
1346       }
1347       if (ucs.getLowerCaseColours() != null)
1348       {
1349         colours = ucs.getLowerCaseColours();
1350         for (int i = 0; i < colours.length; i++)
1351         {
1352           jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
1353           col.setName(ResidueProperties.aa[i].toLowerCase());
1354           col.setRGB(jalview.util.Format.getHexString(colours[i]));
1355           jbucs.addColour(col);
1356         }
1357       }
1358
1359       id = "ucs" + userColours.indexOf(ucs);
1360       uc.setId(id);
1361       uc.setUserColourScheme(jbucs);
1362       jms.addUserColours(uc);
1363     }
1364
1365     return id;
1366   }
1367
1368   jalview.schemes.UserColourScheme GetUserColourScheme(
1369           JalviewModelSequence jms, String id)
1370   {
1371     UserColours[] uc = jms.getUserColours();
1372     UserColours colours = null;
1373
1374     for (int i = 0; i < uc.length; i++)
1375     {
1376       if (uc[i].getId().equals(id))
1377       {
1378         colours = uc[i];
1379
1380         break;
1381       }
1382     }
1383
1384     java.awt.Color[] newColours = new java.awt.Color[24];
1385
1386     for (int i = 0; i < 24; i++)
1387     {
1388       newColours[i] = new java.awt.Color(Integer.parseInt(colours
1389               .getUserColourScheme().getColour(i).getRGB(), 16));
1390     }
1391
1392     jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
1393             newColours);
1394
1395     if (colours.getUserColourScheme().getColourCount() > 24)
1396     {
1397       newColours = new java.awt.Color[23];
1398       for (int i = 0; i < 23; i++)
1399       {
1400         newColours[i] = new java.awt.Color(Integer.parseInt(colours
1401                 .getUserColourScheme().getColour(i + 24).getRGB(), 16));
1402       }
1403       ucs.setLowerCaseColours(newColours);
1404     }
1405
1406     return ucs;
1407   }
1408
1409   /**
1410    * contains last error message (if any) encountered by XML loader.
1411    */
1412   String errorMessage = null;
1413
1414   /**
1415    * flag to control whether the Jalview2XML_V1 parser should be deferred to if
1416    * exceptions are raised during project XML parsing
1417    */
1418   public boolean attemptversion1parse = true;
1419
1420   /**
1421    * Load a jalview project archive from a jar file
1422    * 
1423    * @param file -
1424    *                HTTP URL or filename
1425    */
1426   public AlignFrame LoadJalviewAlign(final String file)
1427   {
1428
1429     jalview.gui.AlignFrame af = null;
1430
1431     try
1432     {
1433       // UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING
1434       // Workaround is to make sure caller implements the JarInputStreamProvider
1435       // interface
1436       // so we can re-open the jar input stream for each entry.
1437
1438       jarInputStreamProvider jprovider = createjarInputStreamProvider(file);
1439       af = LoadJalviewAlign(jprovider);
1440     } catch (MalformedURLException e)
1441     {
1442       errorMessage = "Invalid URL format for '" + file + "'";
1443       reportErrors();
1444     }
1445     return af;
1446   }
1447
1448   private jarInputStreamProvider createjarInputStreamProvider(
1449           final String file) throws MalformedURLException
1450   {
1451     URL url = null;
1452     errorMessage = null;
1453     uniqueSetSuffix = null;
1454     seqRefIds = null;
1455     viewportsAdded = null;
1456     frefedSequence = null;
1457
1458     if (file.startsWith("http://"))
1459     {
1460       url = new URL(file);
1461     }
1462     final URL _url = url;
1463     return new jarInputStreamProvider()
1464     {
1465
1466       public JarInputStream getJarInputStream() throws IOException
1467       {
1468         if (_url != null)
1469         {
1470           return new JarInputStream(_url.openStream());
1471         }
1472         else
1473         {
1474           return new JarInputStream(new FileInputStream(file));
1475         }
1476       }
1477
1478       public String getFilename()
1479       {
1480         return file;
1481       }
1482     };
1483   }
1484
1485   /**
1486    * Recover jalview session from a jalview project archive. Caller may
1487    * initialise uniqueSetSuffix, seqRefIds, viewportsAdded and frefedSequence
1488    * themselves. Any null fields will be initialised with default values,
1489    * non-null fields are left alone.
1490    * 
1491    * @param jprovider
1492    * @return
1493    */
1494   public AlignFrame LoadJalviewAlign(final jarInputStreamProvider jprovider)
1495   {
1496     errorMessage = null;
1497     if (uniqueSetSuffix == null)
1498     {
1499       uniqueSetSuffix = System.currentTimeMillis() % 100000 + "";
1500     }
1501     if (seqRefIds == null)
1502     {
1503       seqRefIds = new Hashtable();
1504     }
1505     if (viewportsAdded == null)
1506     {
1507       viewportsAdded = new Hashtable();
1508     }
1509     if (frefedSequence == null)
1510     {
1511       frefedSequence = new Vector();
1512     }
1513
1514     jalview.gui.AlignFrame af = null;
1515     Hashtable gatherToThisFrame = new Hashtable();
1516     final String file = jprovider.getFilename();
1517     try
1518     {
1519       JarInputStream jin = null;
1520       JarEntry jarentry = null;
1521       int entryCount = 1;
1522
1523       do
1524       {
1525         jin = jprovider.getJarInputStream();
1526         for (int i = 0; i < entryCount; i++)
1527         {
1528           jarentry = jin.getNextJarEntry();
1529         }
1530
1531         if (jarentry != null && jarentry.getName().endsWith(".xml"))
1532         {
1533           InputStreamReader in = new InputStreamReader(jin, "UTF-8");
1534           JalviewModel object = new JalviewModel();
1535
1536           Unmarshaller unmar = new Unmarshaller(object);
1537           unmar.setValidation(false);
1538           object = (JalviewModel) unmar.unmarshal(in);
1539           if (true) // !skipViewport(object))
1540           {
1541             af = LoadFromObject(object, file, true, jprovider);
1542             if (af.viewport.gatherViewsHere)
1543             {
1544               gatherToThisFrame.put(af.viewport.getSequenceSetId(), af);
1545             }
1546           }
1547           entryCount++;
1548         }
1549         else if (jarentry != null)
1550         {
1551           // Some other file here.
1552           entryCount++;
1553         }
1554       } while (jarentry != null);
1555       resolveFrefedSequences();
1556     } catch (java.io.FileNotFoundException ex)
1557     {
1558       ex.printStackTrace();
1559       errorMessage = "Couldn't locate Jalview XML file : " + file;
1560       System.err.println("Exception whilst loading jalview XML file : "
1561               + ex + "\n");
1562     } catch (java.net.UnknownHostException ex)
1563     {
1564       ex.printStackTrace();
1565       errorMessage = "Couldn't locate Jalview XML file : " + file;
1566       System.err.println("Exception whilst loading jalview XML file : "
1567               + ex + "\n");
1568     } catch (Exception ex)
1569     {
1570       System.err.println("Parsing as Jalview Version 2 file failed.");
1571       ex.printStackTrace(System.err);
1572       if (attemptversion1parse)
1573       {
1574         // Is Version 1 Jar file?
1575         try
1576         {
1577           af = new Jalview2XML_V1(raiseGUI).LoadJalviewAlign(jprovider);
1578         } catch (Exception ex2)
1579         {
1580           System.err.println("Exception whilst loading as jalviewXMLV1:");
1581           ex2.printStackTrace();
1582           af = null;
1583         }
1584       }
1585       if (Desktop.instance != null)
1586       {
1587         Desktop.instance.stopLoading();
1588       }
1589       if (af != null)
1590       {
1591         System.out.println("Successfully loaded archive file");
1592         return af;
1593       }
1594       ex.printStackTrace();
1595
1596       System.err.println("Exception whilst loading jalview XML file : "
1597               + ex + "\n");
1598     } catch (OutOfMemoryError e)
1599     {
1600       new jalview.gui.OOMWarning("loading jalview XML file", e,
1601               Desktop.instance);
1602     }
1603
1604     if (Desktop.instance != null)
1605     {
1606       Desktop.instance.stopLoading();
1607     }
1608
1609     Enumeration en = gatherToThisFrame.elements();
1610     while (en.hasMoreElements())
1611     {
1612       Desktop.instance.gatherViews((AlignFrame) en.nextElement());
1613     }
1614     if (errorMessage != null)
1615     {
1616       reportErrors();
1617     }
1618     return af;
1619   }
1620
1621   /**
1622    * check errorMessage for a valid error message and raise an error box in the
1623    * GUI or write the current errorMessage to stderr and then clear the error
1624    * state.
1625    */
1626   protected void reportErrors()
1627   {
1628     reportErrors(false);
1629   }
1630
1631   protected void reportErrors(final boolean saving)
1632   {
1633     if (errorMessage != null)
1634     {
1635       final String finalErrorMessage = errorMessage;
1636       if (raiseGUI)
1637       {
1638         javax.swing.SwingUtilities.invokeLater(new Runnable()
1639         {
1640           public void run()
1641           {
1642             JOptionPane.showInternalMessageDialog(Desktop.desktop,
1643                     finalErrorMessage, "Error "
1644                             + (saving ? "saving" : "loading")
1645                             + " Jalview file", JOptionPane.WARNING_MESSAGE);
1646           }
1647         });
1648       }
1649       else
1650       {
1651         System.err.println("Problem loading Jalview file: " + errorMessage);
1652       }
1653     }
1654     errorMessage = null;
1655   }
1656
1657   Hashtable alreadyLoadedPDB;
1658
1659   /**
1660    * when set, local views will be updated from view stored in JalviewXML
1661    * Currently (28th Sep 2008) things will go horribly wrong in vamsas document
1662    * sync if this is set to true.
1663    */
1664   private boolean updateLocalViews = false;
1665
1666   String loadPDBFile(jarInputStreamProvider jprovider, String pdbId)
1667   {
1668     if (alreadyLoadedPDB == null)
1669       alreadyLoadedPDB = new Hashtable();
1670
1671     if (alreadyLoadedPDB.containsKey(pdbId))
1672       return alreadyLoadedPDB.get(pdbId).toString();
1673
1674     try
1675     {
1676       JarInputStream jin = jprovider.getJarInputStream();
1677       /*
1678        * if (jprovider.startsWith("http://")) { jin = new JarInputStream(new
1679        * URL(jprovider).openStream()); } else { jin = new JarInputStream(new
1680        * FileInputStream(jprovider)); }
1681        */
1682
1683       JarEntry entry = null;
1684       do
1685       {
1686         entry = jin.getNextJarEntry();
1687       } while (entry != null && !entry.getName().equals(pdbId));
1688       if (entry != null)
1689       {
1690         BufferedReader in = new BufferedReader(new InputStreamReader(jin));
1691         File outFile = File.createTempFile("jalview_pdb", ".txt");
1692         outFile.deleteOnExit();
1693         PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
1694         String data;
1695
1696         while ((data = in.readLine()) != null)
1697         {
1698           out.println(data);
1699         }
1700         try
1701         {
1702           out.flush();
1703         } catch (Exception foo)
1704         {
1705         }
1706         ;
1707         out.close();
1708
1709         alreadyLoadedPDB.put(pdbId, outFile.getAbsolutePath());
1710         return outFile.getAbsolutePath();
1711       }
1712       else
1713       {
1714         warn("Couldn't find PDB file entry in Jalview Jar for " + pdbId);
1715       }
1716     } catch (Exception ex)
1717     {
1718       ex.printStackTrace();
1719     }
1720
1721     return null;
1722   }
1723
1724   /**
1725    * Load alignment frame from jalview XML DOM object
1726    * 
1727    * @param object
1728    *                DOM
1729    * @param file
1730    *                filename source string
1731    * @param loadTreesAndStructures
1732    *                when false only create Viewport
1733    * @param jprovider
1734    *                data source provider
1735    * @return alignment frame created from view stored in DOM
1736    */
1737   AlignFrame LoadFromObject(JalviewModel object, String file,
1738           boolean loadTreesAndStructures, jarInputStreamProvider jprovider)
1739   {
1740     SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0);
1741     Sequence[] vamsasSeq = vamsasSet.getSequence();
1742
1743     JalviewModelSequence jms = object.getJalviewModelSequence();
1744
1745     Viewport view = jms.getViewport(0);
1746     // ////////////////////////////////
1747     // LOAD SEQUENCES
1748
1749     Vector hiddenSeqs = null;
1750     jalview.datamodel.Sequence jseq;
1751
1752     ArrayList tmpseqs = new ArrayList();
1753
1754     boolean multipleView = false;
1755
1756     JSeq[] JSEQ = object.getJalviewModelSequence().getJSeq();
1757     int vi = 0; // counter in vamsasSeq array
1758     for (int i = 0; i < JSEQ.length; i++)
1759     {
1760       String seqId = JSEQ[i].getId();
1761
1762       if (seqRefIds.get(seqId) != null)
1763       {
1764         tmpseqs.add((jalview.datamodel.Sequence) seqRefIds.get(seqId));
1765         multipleView = true;
1766       }
1767       else
1768       {
1769         jseq = new jalview.datamodel.Sequence(vamsasSeq[vi].getName(),
1770                 vamsasSeq[vi].getSequence());
1771         jseq.setDescription(vamsasSeq[vi].getDescription());
1772         jseq.setStart(JSEQ[i].getStart());
1773         jseq.setEnd(JSEQ[i].getEnd());
1774         jseq.setVamsasId(uniqueSetSuffix + seqId);
1775         seqRefIds.put(vamsasSeq[vi].getId(), jseq);
1776         tmpseqs.add(jseq);
1777         vi++;
1778       }
1779
1780       if (JSEQ[i].getHidden())
1781       {
1782         if (hiddenSeqs == null)
1783         {
1784           hiddenSeqs = new Vector();
1785         }
1786
1787         hiddenSeqs.addElement((jalview.datamodel.Sequence) seqRefIds
1788                 .get(seqId));
1789       }
1790
1791     }
1792
1793     // /
1794     // Create the alignment object from the sequence set
1795     // ///////////////////////////////
1796     jalview.datamodel.Sequence[] orderedSeqs = new jalview.datamodel.Sequence[tmpseqs
1797             .size()];
1798
1799     tmpseqs.toArray(orderedSeqs);
1800
1801     jalview.datamodel.Alignment al = new jalview.datamodel.Alignment(
1802             orderedSeqs);
1803
1804     // / Add the alignment properties
1805     for (int i = 0; i < vamsasSet.getSequenceSetPropertiesCount(); i++)
1806     {
1807       SequenceSetProperties ssp = vamsasSet.getSequenceSetProperties(i);
1808       al.setProperty(ssp.getKey(), ssp.getValue());
1809     }
1810
1811     // /
1812     // SequenceFeatures are added to the DatasetSequence,
1813     // so we must create or recover the dataset before loading features
1814     // ///////////////////////////////
1815     if (vamsasSet.getDatasetId() == null || vamsasSet.getDatasetId() == "")
1816     {
1817       // older jalview projects do not have a dataset id.
1818       al.setDataset(null);
1819     }
1820     else
1821     {
1822       recoverDatasetFor(vamsasSet, al);
1823     }
1824     // ///////////////////////////////
1825
1826     Hashtable pdbloaded = new Hashtable();
1827     if (!multipleView)
1828     {
1829       // load sequence features, database references and any associated PDB
1830       // structures for the alignment
1831       for (int i = 0; i < vamsasSeq.length; i++)
1832       {
1833         if (JSEQ[i].getFeaturesCount() > 0)
1834         {
1835           Features[] features = JSEQ[i].getFeatures();
1836           for (int f = 0; f < features.length; f++)
1837           {
1838             jalview.datamodel.SequenceFeature sf = new jalview.datamodel.SequenceFeature(
1839                     features[f].getType(), features[f].getDescription(),
1840                     features[f].getStatus(), features[f].getBegin(),
1841                     features[f].getEnd(), features[f].getFeatureGroup());
1842
1843             sf.setScore(features[f].getScore());
1844             for (int od = 0; od < features[f].getOtherDataCount(); od++)
1845             {
1846               OtherData keyValue = features[f].getOtherData(od);
1847               if (keyValue.getKey().startsWith("LINK"))
1848               {
1849                 sf.addLink(keyValue.getValue());
1850               }
1851               else
1852               {
1853                 sf.setValue(keyValue.getKey(), keyValue.getValue());
1854               }
1855
1856             }
1857
1858             al.getSequenceAt(i).getDatasetSequence().addSequenceFeature(sf);
1859           }
1860         }
1861         if (vamsasSeq[i].getDBRefCount() > 0)
1862         {
1863           addDBRefs(al.getSequenceAt(i).getDatasetSequence(), vamsasSeq[i]);
1864         }
1865         if (JSEQ[i].getPdbidsCount() > 0)
1866         {
1867           Pdbids[] ids = JSEQ[i].getPdbids();
1868           for (int p = 0; p < ids.length; p++)
1869           {
1870             jalview.datamodel.PDBEntry entry = new jalview.datamodel.PDBEntry();
1871             entry.setId(ids[p].getId());
1872             entry.setType(ids[p].getType());
1873             if (ids[p].getFile() != null)
1874             {
1875               if (!pdbloaded.containsKey(ids[p].getFile()))
1876               {
1877                 entry.setFile(loadPDBFile(jprovider, ids[p].getId()));
1878               }
1879               else
1880               {
1881                 entry.setFile(pdbloaded.get(ids[p].getId()).toString());
1882               }
1883             }
1884
1885             al.getSequenceAt(i).getDatasetSequence().addPDBId(entry);
1886           }
1887         }
1888       }
1889     } // end !multipleview
1890
1891     // ///////////////////////////////
1892     // LOAD SEQUENCE MAPPINGS
1893
1894     if (vamsasSet.getAlcodonFrameCount() > 0)
1895     {
1896       // TODO Potentially this should only be done once for all views of an
1897       // alignment
1898       AlcodonFrame[] alc = vamsasSet.getAlcodonFrame();
1899       for (int i = 0; i < alc.length; i++)
1900       {
1901         jalview.datamodel.AlignedCodonFrame cf = new jalview.datamodel.AlignedCodonFrame(
1902                 alc[i].getAlcodonCount());
1903         if (alc[i].getAlcodonCount() > 0)
1904         {
1905           Alcodon[] alcods = alc[i].getAlcodon();
1906           for (int p = 0; p < cf.codons.length; p++)
1907           {
1908             if (alcods[p].hasPos1() && alcods[p].hasPos2() && alcods[p].hasPos3())
1909             {
1910               // translated codons require three valid positions
1911               cf.codons[p] = new int[3];
1912               cf.codons[p][0] = (int) alcods[p].getPos1();
1913               cf.codons[p][1] = (int) alcods[p].getPos2();
1914               cf.codons[p][2] = (int) alcods[p].getPos3();
1915             } else {
1916               cf.codons[p] = null;
1917             }
1918           }
1919         }
1920         if (alc[i].getAlcodMapCount() > 0)
1921         {
1922           AlcodMap[] maps = alc[i].getAlcodMap();
1923           for (int m = 0; m < maps.length; m++)
1924           {
1925             SequenceI dnaseq = (SequenceI) seqRefIds
1926                     .get(maps[m].getDnasq());
1927             // Load Mapping
1928             jalview.datamodel.Mapping mapping = null;
1929             // attach to dna sequence reference.
1930             if (maps[m].getMapping() != null)
1931             {
1932               mapping = addMapping(maps[m].getMapping());
1933             }
1934             if (dnaseq != null)
1935             {
1936               cf.addMap(dnaseq, mapping.getTo(), mapping.getMap());
1937             }
1938             else
1939             {
1940               // defer to later
1941               frefedSequence.add(new Object[]
1942               { maps[m].getDnasq(), cf, mapping });
1943             }
1944           }
1945         }
1946         al.addCodonFrame(cf);
1947       }
1948
1949     }
1950
1951     // ////////////////////////////////
1952     // LOAD ANNOTATIONS
1953     boolean hideQuality = true, hideConservation = true, hideConsensus = true;
1954
1955     if (vamsasSet.getAnnotationCount() > 0)
1956     {
1957       Annotation[] an = vamsasSet.getAnnotation();
1958
1959       for (int i = 0; i < an.length; i++)
1960       {
1961         // set visibility for automatic annotation for this view
1962         if (an[i].getLabel().equals("Quality"))
1963         {
1964           hideQuality = false;
1965           continue;
1966         }
1967         else if (an[i].getLabel().equals("Conservation"))
1968         {
1969           hideConservation = false;
1970           continue;
1971         }
1972         else if (an[i].getLabel().equals("Consensus"))
1973         {
1974           hideConsensus = false;
1975           continue;
1976         }
1977         // set visiblity for other annotation in this view
1978         if (an[i].getId() != null
1979                 && annotationIds.containsKey(an[i].getId()))
1980         {
1981           jalview.datamodel.AlignmentAnnotation jda = (jalview.datamodel.AlignmentAnnotation) annotationIds
1982                   .get(an[i].getId());
1983           // in principle Visible should always be true for annotation displayed
1984           // in multiple views
1985           if (an[i].hasVisible())
1986             jda.visible = an[i].getVisible();
1987
1988           al.addAnnotation(jda);
1989
1990           continue;
1991         }
1992         // Construct new annotation from model.
1993         AnnotationElement[] ae = an[i].getAnnotationElement();
1994         jalview.datamodel.Annotation[] anot = null;
1995
1996         if (!an[i].getScoreOnly())
1997         {
1998           anot = new jalview.datamodel.Annotation[al.getWidth()];
1999
2000           for (int aa = 0; aa < ae.length && aa < anot.length; aa++)
2001           {
2002             if (ae[aa].getPosition() >= anot.length)
2003               continue;
2004
2005             anot[ae[aa].getPosition()] = new jalview.datamodel.Annotation(
2006
2007             ae[aa].getDisplayCharacter(), ae[aa].getDescription(), (ae[aa]
2008                     .getSecondaryStructure() == null || ae[aa]
2009                     .getSecondaryStructure().length() == 0) ? ' ' : ae[aa]
2010                     .getSecondaryStructure().charAt(0), ae[aa].getValue()
2011
2012             );
2013             // JBPNote: Consider verifying dataflow for IO of secondary
2014             // structure annotation read from Stockholm files
2015             // this was added to try to ensure that
2016             // if (anot[ae[aa].getPosition()].secondaryStructure>' ')
2017             // {
2018             // anot[ae[aa].getPosition()].displayCharacter = "";
2019             // }
2020             anot[ae[aa].getPosition()].colour = new java.awt.Color(ae[aa]
2021                     .getColour());
2022           }
2023         }
2024         jalview.datamodel.AlignmentAnnotation jaa = null;
2025
2026         if (an[i].getGraph())
2027         {
2028           jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
2029                   an[i].getDescription(), anot, 0, 0, an[i].getGraphType());
2030
2031           jaa.graphGroup = an[i].getGraphGroup();
2032
2033           if (an[i].getThresholdLine() != null)
2034           {
2035             jaa.setThreshold(new jalview.datamodel.GraphLine(an[i]
2036                     .getThresholdLine().getValue(), an[i]
2037                     .getThresholdLine().getLabel(), new java.awt.Color(
2038                     an[i].getThresholdLine().getColour())));
2039
2040           }
2041
2042         }
2043         else
2044         {
2045           jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
2046                   an[i].getDescription(), anot);
2047         }
2048         // register new annotation
2049         if (an[i].getId() != null)
2050         {
2051           annotationIds.put(an[i].getId(), jaa);
2052           jaa.annotationId = an[i].getId();
2053         }
2054         // recover sequence association
2055         if (an[i].getSequenceRef() != null)
2056         {
2057           if (al.findName(an[i].getSequenceRef()) != null)
2058           {
2059             jaa.createSequenceMapping(al.findName(an[i].getSequenceRef()),
2060                     1, true);
2061             al.findName(an[i].getSequenceRef()).addAlignmentAnnotation(jaa);
2062           }
2063         }
2064         if (an[i].hasScore())
2065         {
2066           jaa.setScore(an[i].getScore());
2067         }
2068
2069         if (an[i].hasVisible())
2070           jaa.visible = an[i].getVisible();
2071
2072         al.addAnnotation(jaa);
2073       }
2074     }
2075
2076     // ///////////////////////
2077     // LOAD GROUPS
2078     // Create alignment markup and styles for this view
2079     if (jms.getJGroupCount() > 0)
2080     {
2081       JGroup[] groups = jms.getJGroup();
2082
2083       for (int i = 0; i < groups.length; i++)
2084       {
2085         ColourSchemeI cs = null;
2086
2087         if (groups[i].getColour() != null)
2088         {
2089           if (groups[i].getColour().startsWith("ucs"))
2090           {
2091             cs = GetUserColourScheme(jms, groups[i].getColour());
2092           }
2093           else
2094           {
2095             cs = ColourSchemeProperty.getColour(al, groups[i].getColour());
2096           }
2097
2098           if (cs != null)
2099           {
2100             cs.setThreshold(groups[i].getPidThreshold(), true);
2101           }
2102         }
2103
2104         Vector seqs = new Vector();
2105
2106         for (int s = 0; s < groups[i].getSeqCount(); s++)
2107         {
2108           String seqId = groups[i].getSeq(s) + "";
2109           jalview.datamodel.SequenceI ts = (jalview.datamodel.SequenceI) seqRefIds
2110                   .get(seqId);
2111
2112           if (ts != null)
2113           {
2114             seqs.addElement(ts);
2115           }
2116         }
2117
2118         if (seqs.size() < 1)
2119         {
2120           continue;
2121         }
2122
2123         jalview.datamodel.SequenceGroup sg = new jalview.datamodel.SequenceGroup(
2124                 seqs, groups[i].getName(), cs, groups[i].getDisplayBoxes(),
2125                 groups[i].getDisplayText(), groups[i].getColourText(),
2126                 groups[i].getStart(), groups[i].getEnd());
2127
2128         sg
2129                 .setOutlineColour(new java.awt.Color(groups[i]
2130                         .getOutlineColour()));
2131
2132         sg.textColour = new java.awt.Color(groups[i].getTextCol1());
2133         sg.textColour2 = new java.awt.Color(groups[i].getTextCol2());
2134         sg.setShowunconserved(groups[i].hasShowUnconserved() ? groups[i].isShowUnconserved() : false);
2135         sg.thresholdTextColour = groups[i].getTextColThreshold();
2136
2137         if (groups[i].getConsThreshold() != 0)
2138         {
2139           jalview.analysis.Conservation c = new jalview.analysis.Conservation(
2140                   "All", ResidueProperties.propHash, 3, sg
2141                           .getSequences(null), 0, sg.getWidth() - 1);
2142           c.calculate();
2143           c.verdict(false, 25);
2144           sg.cs.setConservation(c);
2145         }
2146
2147         al.addGroup(sg);
2148       }
2149     }
2150
2151     // ///////////////////////////////
2152     // LOAD VIEWPORT
2153
2154     // If we just load in the same jar file again, the sequenceSetId
2155     // will be the same, and we end up with multiple references
2156     // to the same sequenceSet. We must modify this id on load
2157     // so that each load of the file gives a unique id
2158     String uniqueSeqSetId = view.getSequenceSetId() + uniqueSetSuffix;
2159     String viewId = (view.getId() == null ? null : view.getId()
2160             + uniqueSetSuffix);
2161     AlignFrame af = null;
2162     AlignViewport av = null;
2163     // now check to see if we really need to create a new viewport.
2164     if (multipleView && viewportsAdded.size() == 0)
2165     {
2166       // We recovered an alignment for which a viewport already exists.
2167       // TODO: fix up any settings necessary for overlaying stored state onto
2168       // state recovered from another document. (may not be necessary).
2169       // we may need a binding from a viewport in memory to one recovered from
2170       // XML.
2171       // and then recover its containing af to allow the settings to be applied.
2172       // TODO: fix for vamsas demo
2173       System.err
2174               .println("About to recover a viewport for existing alignment: Sequence set ID is "
2175                       + uniqueSeqSetId);
2176       Object seqsetobj = retrieveExistingObj(uniqueSeqSetId);
2177       if (seqsetobj != null)
2178       {
2179         if (seqsetobj instanceof String)
2180         {
2181           uniqueSeqSetId = (String) seqsetobj;
2182           System.err
2183                   .println("Recovered extant sequence set ID mapping for ID : New Sequence set ID is "
2184                           + uniqueSeqSetId);
2185         }
2186         else
2187         {
2188           System.err
2189                   .println("Warning : Collision between sequence set ID string and existing jalview object mapping.");
2190         }
2191
2192       }
2193     }
2194     AlignmentPanel ap = null;
2195     boolean isnewview = true;
2196     if (viewId != null)
2197     {
2198       // Check to see if this alignment already has a view id == viewId
2199       jalview.gui.AlignmentPanel views[] = Desktop
2200               .getAlignmentPanels(uniqueSeqSetId);
2201       if (views != null && views.length > 0)
2202       {
2203         for (int v = 0; v < views.length; v++)
2204         {
2205           if (views[v].av.getViewId().equalsIgnoreCase(viewId))
2206           {
2207             // recover the existing alignpanel, alignframe, viewport
2208             af = views[v].alignFrame;
2209             av = views[v].av;
2210             ap = views[v];
2211             // TODO: could even skip resetting view settings if we don't want to
2212             // change the local settings from other jalview processes
2213             isnewview = false;
2214           }
2215         }
2216       }
2217     }
2218
2219     if (isnewview)
2220     {
2221       af = loadViewport(file, JSEQ, hiddenSeqs, al, hideConsensus,
2222               hideQuality, hideConservation, jms, view, uniqueSeqSetId,
2223               viewId);
2224       av = af.viewport;
2225       ap = af.alignPanel;
2226     }
2227     // LOAD TREES
2228     // /////////////////////////////////////
2229     if (loadTreesAndStructures && jms.getTreeCount() > 0)
2230     {
2231       try
2232       {
2233         for (int t = 0; t < jms.getTreeCount(); t++)
2234         {
2235
2236           Tree tree = jms.getTree(t);
2237
2238           TreePanel tp = (TreePanel) retrieveExistingObj(tree.getId());
2239           if (tp == null)
2240           {
2241             tp = af.ShowNewickTree(new jalview.io.NewickFile(tree
2242                     .getNewick()), tree.getTitle(), tree.getWidth(), tree
2243                     .getHeight(), tree.getXpos(), tree.getYpos());
2244             if (tree.getId() != null)
2245             {
2246
2247             }
2248           }
2249           else
2250           {
2251             // update local tree attributes ?
2252             tp.setTitle(tree.getTitle());
2253             tp.setBounds(new Rectangle(tree.getXpos(), tree.getYpos(), tree
2254                     .getWidth(), tree.getHeight()));
2255             tp.av = av; // af.viewport; // TODO: verify 'associate with all
2256             // views'
2257             // works still
2258             tp.treeCanvas.av = av; // af.viewport;
2259             tp.treeCanvas.ap = ap; // af.alignPanel;
2260
2261           }
2262           if (tp==null)
2263           {
2264             warn("There was a problem recovering stored Newick tree: \n"+tree.getNewick());
2265             continue;
2266           }
2267
2268           tp.fitToWindow.setState(tree.getFitToWindow());
2269           tp.fitToWindow_actionPerformed(null);
2270
2271           if (tree.getFontName() != null)
2272           {
2273             tp.setTreeFont(new java.awt.Font(tree.getFontName(), tree
2274                     .getFontStyle(), tree.getFontSize()));
2275           }
2276           else
2277           {
2278             tp.setTreeFont(new java.awt.Font(view.getFontName(), view
2279                     .getFontStyle(), tree.getFontSize()));
2280           }
2281
2282           tp.showPlaceholders(tree.getMarkUnlinked());
2283           tp.showBootstrap(tree.getShowBootstrap());
2284           tp.showDistances(tree.getShowDistances());
2285
2286           tp.treeCanvas.threshold = tree.getThreshold();
2287
2288           if (tree.getCurrentTree())
2289           {
2290             af.viewport.setCurrentTree(tp.getTree());
2291           }
2292         }
2293
2294       } catch (Exception ex)
2295       {
2296         ex.printStackTrace();
2297       }
2298     }
2299
2300     // //LOAD STRUCTURES
2301     if (loadTreesAndStructures)
2302     {
2303       for (int i = 0; i < JSEQ.length; i++)
2304       {
2305         if (JSEQ[i].getPdbidsCount() > 0)
2306         {
2307           Pdbids[] ids = JSEQ[i].getPdbids();
2308           for (int p = 0; p < ids.length; p++)
2309           {
2310             for (int s = 0; s < ids[p].getStructureStateCount(); s++)
2311             {
2312               // check to see if we haven't already created this structure view
2313               String sviewid = (ids[p].getStructureState(s).getViewId() == null) ? null
2314                       : ids[p].getStructureState(s).getViewId()
2315                               + uniqueSetSuffix;
2316               jalview.datamodel.PDBEntry jpdb = new jalview.datamodel.PDBEntry();
2317               // Originally : ids[p].getFile()
2318               // : TODO: verify external PDB file recovery still works in normal
2319               // jalview project load
2320               jpdb.setFile(loadPDBFile(jprovider, ids[p].getId()));
2321               jpdb.setId(ids[p].getId());
2322
2323               int x = ids[p].getStructureState(s).getXpos();
2324               int y = ids[p].getStructureState(s).getYpos();
2325               int width = ids[p].getStructureState(s).getWidth();
2326               int height = ids[p].getStructureState(s).getHeight();
2327               AppJmol comp = null;
2328               JInternalFrame[] frames = null;
2329               do
2330               {
2331                 try
2332                 {
2333                   frames = Desktop.desktop.getAllFrames();
2334                 } catch (ArrayIndexOutOfBoundsException e)
2335                 {
2336                   // occasional No such child exceptions are thrown here...
2337                   frames = null;
2338                   try
2339                   {
2340                     Thread.sleep(10);
2341                   } catch (Exception f)
2342                   {
2343                   }
2344                   ;
2345                 }
2346               } while (frames == null);
2347               // search for any Jmol windows already open from other
2348               // alignment views that exactly match the stored structure state
2349               for (int f = 0; comp == null && f < frames.length; f++)
2350               {
2351                 if (frames[f] instanceof AppJmol)
2352                 {
2353                   if (sviewid != null
2354                           && ((AppJmol) frames[f]).getViewId().equals(
2355                                   sviewid))
2356                   {
2357                     // post jalview 2.4 schema includes structure view id
2358                     comp = (AppJmol) frames[f];
2359                   }
2360                   else if (frames[f].getX() == x && frames[f].getY() == y
2361                           && frames[f].getHeight() == height
2362                           && frames[f].getWidth() == width)
2363                   {
2364                     comp = (AppJmol) frames[f];
2365                   }
2366                 }
2367               }
2368               Desktop.desktop.getComponentAt(x, y);
2369               // TODO: NOW: check that this recovers the PDB file correctly.
2370               String pdbFile = loadPDBFile(jprovider, ids[p].getId());
2371
2372               jalview.datamodel.SequenceI[] seq = new jalview.datamodel.SequenceI[]
2373               { (jalview.datamodel.SequenceI) seqRefIds.get(JSEQ[i].getId()
2374                       + "") };
2375
2376               if (comp == null)
2377               {
2378                 // create a new Jmol window
2379                 String state = ids[p].getStructureState(s).getContent();
2380                 StringBuffer newFileLoc=null;
2381                 if (state.indexOf("load")>-1) {
2382                 newFileLoc = new StringBuffer(state.substring(
2383                         0, state.indexOf("\"", state.indexOf("load")) + 1));
2384
2385                 newFileLoc.append(jpdb.getFile());
2386                 newFileLoc.append(state.substring(state.indexOf("\"", state
2387                         .indexOf("load \"") + 6)));
2388                 } else {
2389                   System.err.println("Ignoring incomplete Jmol state for PDB "+ids[p].getId());
2390                   
2391                   newFileLoc = new StringBuffer(state);
2392                   newFileLoc.append("; load \"");
2393                   newFileLoc.append(jpdb.getFile());
2394                   newFileLoc.append("\";");
2395                 }
2396                 
2397                 if (newFileLoc!=null) {
2398                   new AppJmol(pdbFile, ids[p].getId(), seq, af.alignPanel,
2399                           newFileLoc.toString(), new java.awt.Rectangle(x, y,
2400                                 width, height), sviewid);
2401                 }
2402
2403               }
2404               else
2405               // if (comp != null)
2406               {
2407                 // NOTE: if the jalview project is part of a shared session then
2408                 // view synchronization should/could be done here.
2409
2410                 // add mapping for this sequence to the already open Jmol
2411                 // instance (if it doesn't already exist)
2412                 // These
2413                 StructureSelectionManager.getStructureSelectionManager()
2414                         .setMapping(seq, null, pdbFile,
2415                                 jalview.io.AppletFormatAdapter.FILE);
2416
2417                 ((AppJmol) comp).addSequence(seq);
2418               }
2419             }
2420           }
2421         }
2422       }
2423     }
2424
2425     return af;
2426   }
2427
2428   AlignFrame loadViewport(String file, JSeq[] JSEQ, Vector hiddenSeqs,
2429           Alignment al, boolean hideConsensus, boolean hideQuality,
2430           boolean hideConservation, JalviewModelSequence jms,
2431           Viewport view, String uniqueSeqSetId, String viewId)
2432   {
2433     AlignFrame af = null;
2434     af = new AlignFrame(al, view.getWidth(), view.getHeight(),
2435             uniqueSeqSetId, viewId);
2436
2437     af.setFileName(file, "Jalview");
2438
2439     for (int i = 0; i < JSEQ.length; i++)
2440     {
2441       af.viewport.setSequenceColour(af.viewport.alignment.getSequenceAt(i),
2442               new java.awt.Color(JSEQ[i].getColour()));
2443     }
2444
2445     af.viewport.gatherViewsHere = view.getGatheredViews();
2446
2447     if (view.getSequenceSetId() != null)
2448     {
2449       jalview.gui.AlignViewport av = (jalview.gui.AlignViewport) viewportsAdded
2450               .get(uniqueSeqSetId);
2451
2452       af.viewport.sequenceSetID = uniqueSeqSetId;
2453       if (av != null)
2454       {
2455         // propagate shared settings to this new view
2456         af.viewport.historyList = av.historyList;
2457         af.viewport.redoList = av.redoList;
2458       }
2459       else
2460       {
2461         viewportsAdded.put(uniqueSeqSetId, af.viewport);
2462       }
2463       // TODO: check if this method can be called repeatedly without
2464       // side-effects if alignpanel already registered.
2465       PaintRefresher.Register(af.alignPanel, uniqueSeqSetId);
2466     }
2467     // apply Hidden regions to view.
2468     if (hiddenSeqs != null)
2469     {
2470       for (int s = 0; s < JSEQ.length; s++)
2471       {
2472         jalview.datamodel.SequenceGroup hidden = new jalview.datamodel.SequenceGroup();
2473
2474         for (int r = 0; r < JSEQ[s].getHiddenSequencesCount(); r++)
2475         {
2476           hidden.addSequence(al
2477                   .getSequenceAt(JSEQ[s].getHiddenSequences(r)), false);
2478         }
2479         af.viewport.hideRepSequences(al.getSequenceAt(s), hidden);
2480       }
2481
2482       jalview.datamodel.SequenceI[] hseqs = new jalview.datamodel.SequenceI[hiddenSeqs
2483               .size()];
2484
2485       for (int s = 0; s < hiddenSeqs.size(); s++)
2486       {
2487         hseqs[s] = (jalview.datamodel.SequenceI) hiddenSeqs.elementAt(s);
2488       }
2489
2490       af.viewport.hideSequence(hseqs);
2491
2492     }
2493     // set visibility of annotation in view
2494     if ((hideConsensus || hideQuality || hideConservation)
2495             && al.getAlignmentAnnotation() != null)
2496     {
2497       int hSize = al.getAlignmentAnnotation().length;
2498       for (int h = 0; h < hSize; h++)
2499       {
2500         if ((hideConsensus && al.getAlignmentAnnotation()[h].label
2501                 .equals("Consensus"))
2502                 || (hideQuality && al.getAlignmentAnnotation()[h].label
2503                         .equals("Quality"))
2504                 || (hideConservation && al.getAlignmentAnnotation()[h].label
2505                         .equals("Conservation")))
2506         {
2507           al.deleteAnnotation(al.getAlignmentAnnotation()[h]);
2508           hSize--;
2509           h--;
2510         }
2511       }
2512       af.alignPanel.adjustAnnotationHeight();
2513     }
2514     // recover view properties and display parameters
2515     if (view.getViewName() != null)
2516     {
2517       af.viewport.viewName = view.getViewName();
2518       af.setInitialTabVisible();
2519     }
2520     af.setBounds(view.getXpos(), view.getYpos(), view.getWidth(), view
2521             .getHeight());
2522
2523     af.viewport.setShowAnnotation(view.getShowAnnotation());
2524     af.viewport.setAbovePIDThreshold(view.getPidSelected());
2525
2526     af.viewport.setColourText(view.getShowColourText());
2527
2528     af.viewport.setConservationSelected(view.getConservationSelected());
2529     af.viewport.setShowJVSuffix(view.getShowFullId());
2530     af.viewport.rightAlignIds = view.getRightAlignIds();
2531     af.viewport.setFont(new java.awt.Font(view.getFontName(), view
2532             .getFontStyle(), view.getFontSize()));
2533     af.alignPanel.fontChanged();
2534     af.viewport.setRenderGaps(view.getRenderGaps());
2535     af.viewport.setWrapAlignment(view.getWrapAlignment());
2536     af.alignPanel.setWrapAlignment(view.getWrapAlignment());
2537     af.viewport.setShowAnnotation(view.getShowAnnotation());
2538     af.alignPanel.setAnnotationVisible(view.getShowAnnotation());
2539
2540     af.viewport.setShowBoxes(view.getShowBoxes());
2541
2542     af.viewport.setShowText(view.getShowText());
2543
2544     af.viewport.textColour = new java.awt.Color(view.getTextCol1());
2545     af.viewport.textColour2 = new java.awt.Color(view.getTextCol2());
2546     af.viewport.thresholdTextColour = view.getTextColThreshold();
2547     af.viewport.setShowUnconserved(view.hasShowUnconserved() ? view.isShowUnconserved() : false);
2548     af.viewport.setStartRes(view.getStartRes());
2549     af.viewport.setStartSeq(view.getStartSeq());
2550
2551     ColourSchemeI cs = null;
2552     // apply colourschemes
2553     if (view.getBgColour() != null)
2554     {
2555       if (view.getBgColour().startsWith("ucs"))
2556       {
2557         cs = GetUserColourScheme(jms, view.getBgColour());
2558       }
2559       else if (view.getBgColour().startsWith("Annotation"))
2560       {
2561         // int find annotation
2562         for (int i = 0; i < af.viewport.alignment.getAlignmentAnnotation().length; i++)
2563         {
2564           if (af.viewport.alignment.getAlignmentAnnotation()[i].label
2565                   .equals(view.getAnnotationColours().getAnnotation()))
2566           {
2567             if (af.viewport.alignment.getAlignmentAnnotation()[i]
2568                     .getThreshold() == null)
2569             {
2570               af.viewport.alignment.getAlignmentAnnotation()[i]
2571                       .setThreshold(new jalview.datamodel.GraphLine(view
2572                               .getAnnotationColours().getThreshold(),
2573                               "Threshold", java.awt.Color.black)
2574
2575                       );
2576             }
2577
2578             if (view.getAnnotationColours().getColourScheme()
2579                     .equals("None"))
2580             {
2581               cs = new AnnotationColourGradient(af.viewport.alignment
2582                       .getAlignmentAnnotation()[i], new java.awt.Color(view
2583                       .getAnnotationColours().getMinColour()),
2584                       new java.awt.Color(view.getAnnotationColours()
2585                               .getMaxColour()), view.getAnnotationColours()
2586                               .getAboveThreshold());
2587             }
2588             else if (view.getAnnotationColours().getColourScheme()
2589                     .startsWith("ucs"))
2590             {
2591               cs = new AnnotationColourGradient(af.viewport.alignment
2592                       .getAlignmentAnnotation()[i], GetUserColourScheme(
2593                       jms, view.getAnnotationColours().getColourScheme()),
2594                       view.getAnnotationColours().getAboveThreshold());
2595             }
2596             else
2597             {
2598               cs = new AnnotationColourGradient(af.viewport.alignment
2599                       .getAlignmentAnnotation()[i], ColourSchemeProperty
2600                       .getColour(al, view.getAnnotationColours()
2601                               .getColourScheme()), view
2602                       .getAnnotationColours().getAboveThreshold());
2603             }
2604
2605             // Also use these settings for all the groups
2606             if (al.getGroups() != null)
2607             {
2608               for (int g = 0; g < al.getGroups().size(); g++)
2609               {
2610                 jalview.datamodel.SequenceGroup sg = (jalview.datamodel.SequenceGroup) al
2611                         .getGroups().elementAt(g);
2612
2613                 if (sg.cs == null)
2614                 {
2615                   continue;
2616                 }
2617
2618                 /*
2619                  * if
2620                  * (view.getAnnotationColours().getColourScheme().equals("None")) {
2621                  * sg.cs = new AnnotationColourGradient(
2622                  * af.viewport.alignment.getAlignmentAnnotation()[i], new
2623                  * java.awt.Color(view.getAnnotationColours(). getMinColour()),
2624                  * new java.awt.Color(view.getAnnotationColours().
2625                  * getMaxColour()),
2626                  * view.getAnnotationColours().getAboveThreshold()); } else
2627                  */
2628                 {
2629                   sg.cs = new AnnotationColourGradient(
2630                           af.viewport.alignment.getAlignmentAnnotation()[i],
2631                           sg.cs, view.getAnnotationColours()
2632                                   .getAboveThreshold());
2633                 }
2634
2635               }
2636             }
2637
2638             break;
2639           }
2640
2641         }
2642       }
2643       else
2644       {
2645         cs = ColourSchemeProperty.getColour(al, view.getBgColour());
2646       }
2647
2648       if (cs != null)
2649       {
2650         cs.setThreshold(view.getPidThreshold(), true);
2651         cs.setConsensus(af.viewport.hconsensus);
2652       }
2653     }
2654
2655     af.viewport.setGlobalColourScheme(cs);
2656     af.viewport.setColourAppliesToAllGroups(false);
2657
2658     if (view.getConservationSelected() && cs != null)
2659     {
2660       cs.setConservationInc(view.getConsThreshold());
2661     }
2662
2663     af.changeColour(cs);
2664
2665     af.viewport.setColourAppliesToAllGroups(true);
2666
2667     if (view.getShowSequenceFeatures())
2668     {
2669       af.viewport.showSequenceFeatures = true;
2670     }
2671     // recover featre settings
2672     if (jms.getFeatureSettings() != null)
2673     {
2674       af.viewport.featuresDisplayed = new Hashtable();
2675       String[] renderOrder = new String[jms.getFeatureSettings()
2676               .getSettingCount()];
2677       for (int fs = 0; fs < jms.getFeatureSettings().getSettingCount(); fs++)
2678       {
2679         Setting setting = jms.getFeatureSettings().getSetting(fs);
2680         if (setting.hasMincolour())
2681         {
2682           // TODO: determine how to set data independent bounds for a graduated colour scheme's range.
2683           GraduatedColor gc = new GraduatedColor(new java.awt.Color(setting.getMincolour()), new java.awt.Color(setting.getColour()),
2684                   0,1);
2685           if (setting.hasThreshold()) {
2686             gc.setThresh(setting.getThreshold());
2687             gc.setThreshType(setting.getThreshstate());
2688           }
2689         } else {
2690           af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setColour(
2691                   setting.getType(), new java.awt.Color(setting.getColour()));
2692         }
2693         renderOrder[fs] = setting.getType();
2694         if (setting.hasOrder())
2695           af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setOrder(
2696                   setting.getType(), setting.getOrder());
2697         else
2698           af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setOrder(
2699                   setting.getType(),
2700                   fs / jms.getFeatureSettings().getSettingCount());
2701         if (setting.getDisplay())
2702         {
2703           af.viewport.featuresDisplayed.put(setting.getType(), new Integer(
2704                   setting.getColour()));
2705         }
2706       }
2707       af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().renderOrder = renderOrder;
2708       Hashtable fgtable;
2709       af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().featureGroups = fgtable = new Hashtable();
2710       for (int gs = 0; gs < jms.getFeatureSettings().getGroupCount(); gs++)
2711       {
2712         Group grp = jms.getFeatureSettings().getGroup(gs);
2713         fgtable.put(grp.getName(), new Boolean(grp.getDisplay()));
2714       }
2715     }
2716
2717     if (view.getHiddenColumnsCount() > 0)
2718     {
2719       for (int c = 0; c < view.getHiddenColumnsCount(); c++)
2720       {
2721         af.viewport.hideColumns(view.getHiddenColumns(c).getStart(), view
2722                 .getHiddenColumns(c).getEnd() // +1
2723                 );
2724       }
2725     }
2726
2727     af.setMenusFromViewport(af.viewport);
2728     // TODO: we don't need to do this if the viewport is aready visible.
2729     Desktop.addInternalFrame(af, view.getTitle(), view.getWidth(), view
2730             .getHeight());
2731     return af;
2732   }
2733
2734   Hashtable skipList = null;
2735
2736   /**
2737    * TODO remove this method
2738    * 
2739    * @param view
2740    * @return AlignFrame bound to sequenceSetId from view, if one exists. private
2741    *         AlignFrame getSkippedFrame(Viewport view) { if (skipList==null) {
2742    *         throw new Error("Implementation Error. No skipList defined for this
2743    *         Jalview2XML instance."); } return (AlignFrame)
2744    *         skipList.get(view.getSequenceSetId()); }
2745    */
2746
2747   /**
2748    * Check if the Jalview view contained in object should be skipped or not.
2749    * 
2750    * @param object
2751    * @return true if view's sequenceSetId is a key in skipList
2752    */
2753   private boolean skipViewport(JalviewModel object)
2754   {
2755     if (skipList == null)
2756     {
2757       return false;
2758     }
2759     String id;
2760     if (skipList.containsKey(id = object.getJalviewModelSequence()
2761             .getViewport()[0].getSequenceSetId()))
2762     {
2763       if (Cache.log != null && Cache.log.isDebugEnabled())
2764       {
2765         Cache.log.debug("Skipping seuqence set id " + id);
2766       }
2767       return true;
2768     }
2769     return false;
2770   }
2771
2772   public void AddToSkipList(AlignFrame af)
2773   {
2774     if (skipList == null)
2775     {
2776       skipList = new Hashtable();
2777     }
2778     skipList.put(af.getViewport().getSequenceSetId(), af);
2779   }
2780
2781   public void clearSkipList()
2782   {
2783     if (skipList != null)
2784     {
2785       skipList.clear();
2786       skipList = null;
2787     }
2788   }
2789
2790   private void recoverDatasetFor(SequenceSet vamsasSet, Alignment al)
2791   {
2792     jalview.datamodel.Alignment ds = getDatasetFor(vamsasSet.getDatasetId());
2793     Vector dseqs = null;
2794     if (ds == null)
2795     {
2796       // create a list of new dataset sequences
2797       dseqs = new Vector();
2798     }
2799     for (int i = 0, iSize = vamsasSet.getSequenceCount(); i < iSize; i++)
2800     {
2801       Sequence vamsasSeq = vamsasSet.getSequence(i);
2802       ensureJalviewDatasetSequence(vamsasSeq, ds, dseqs);
2803     }
2804     // create a new dataset
2805     if (ds == null)
2806     {
2807       SequenceI[] dsseqs = new SequenceI[dseqs.size()];
2808       dseqs.copyInto(dsseqs);
2809       ds = new jalview.datamodel.Alignment(dsseqs);
2810       debug("Created new dataset "+vamsasSet.getDatasetId()+" for alignment "+System.identityHashCode(al));
2811       addDatasetRef(vamsasSet.getDatasetId(), ds);
2812     }
2813     // set the dataset for the newly imported alignment.
2814     if (al.getDataset() == null)
2815     {
2816       al.setDataset(ds);
2817     }
2818   }
2819
2820   /**
2821    * 
2822    * @param vamsasSeq
2823    *                sequence definition to create/merge dataset sequence for
2824    * @param ds
2825    *                dataset alignment
2826    * @param dseqs
2827    *                vector to add new dataset sequence to
2828    */
2829   private void ensureJalviewDatasetSequence(Sequence vamsasSeq,
2830           AlignmentI ds, Vector dseqs)
2831   {
2832     // JBP TODO: Check this is called for AlCodonFrames to support recovery of
2833     // xRef Codon Maps
2834     jalview.datamodel.Sequence sq = (jalview.datamodel.Sequence) seqRefIds
2835             .get(vamsasSeq.getId());
2836     jalview.datamodel.SequenceI dsq = null;
2837     if (sq != null && sq.getDatasetSequence() != null)
2838     {
2839       dsq = (jalview.datamodel.SequenceI) sq.getDatasetSequence();
2840     }
2841
2842     String sqid = vamsasSeq.getDsseqid();
2843     if (dsq == null)
2844     {
2845       // need to create or add a new dataset sequence reference to this sequence
2846       if (sqid != null)
2847       {
2848         dsq = (jalview.datamodel.SequenceI) seqRefIds.get(sqid);
2849       }
2850       // check again
2851       if (dsq == null)
2852       {
2853         // make a new dataset sequence
2854         dsq = sq.createDatasetSequence();
2855         if (sqid == null)
2856         {
2857           // make up a new dataset reference for this sequence
2858           sqid = seqHash(dsq);
2859         }
2860         dsq.setVamsasId(uniqueSetSuffix + sqid);
2861         seqRefIds.put(sqid, dsq);
2862         if (ds == null)
2863         {
2864           if (dseqs != null)
2865           {
2866             dseqs.addElement(dsq);
2867           }
2868         }
2869         else
2870         {
2871           ds.addSequence(dsq);
2872         }
2873       }
2874       else
2875       {
2876         if (sq != dsq)
2877         { // make this dataset sequence sq's dataset sequence
2878           sq.setDatasetSequence(dsq);
2879         }
2880       }
2881     }
2882     // TODO: refactor this as a merge dataset sequence function
2883     // now check that sq (the dataset sequence) sequence really is the union of
2884     // all references to it
2885     // boolean pre = sq.getStart() < dsq.getStart();
2886     // boolean post = sq.getEnd() > dsq.getEnd();
2887     // if (pre || post)
2888     if (sq != dsq)
2889     {
2890       StringBuffer sb = new StringBuffer();
2891       String newres = jalview.analysis.AlignSeq.extractGaps(
2892               jalview.util.Comparison.GapChars, sq.getSequenceAsString());
2893       if (!newres.equalsIgnoreCase(dsq.getSequenceAsString())
2894               && newres.length() > dsq.getLength())
2895       {
2896         // Update with the longer sequence.
2897         synchronized (dsq)
2898         {
2899           /*
2900            * if (pre) { sb.insert(0, newres .substring(0, dsq.getStart() -
2901            * sq.getStart())); dsq.setStart(sq.getStart()); } if (post) {
2902            * sb.append(newres.substring(newres.length() - sq.getEnd() -
2903            * dsq.getEnd())); dsq.setEnd(sq.getEnd()); }
2904            */
2905           dsq.setSequence(sb.toString());
2906         }
2907         // TODO: merges will never happen if we 'know' we have the real dataset
2908         // sequence - this should be detected when id==dssid
2909         System.err.println("DEBUG Notice:  Merged dataset sequence"); // ("
2910         // + (pre ? "prepended" : "") + " "
2911         // + (post ? "appended" : ""));
2912       }
2913     }
2914   }
2915
2916   java.util.Hashtable datasetIds = null;
2917   java.util.IdentityHashMap dataset2Ids = null;
2918   private Alignment getDatasetFor(String datasetId)
2919   {
2920     if (datasetIds == null)
2921     {
2922       datasetIds = new Hashtable();
2923       return null;
2924     }
2925     if (datasetIds.containsKey(datasetId))
2926     {
2927       return (Alignment) datasetIds.get(datasetId);
2928     }
2929     return null;
2930   }
2931
2932   private void addDatasetRef(String datasetId, Alignment dataset)
2933   {
2934     if (datasetIds == null)
2935     {
2936       datasetIds = new Hashtable();
2937     }
2938     datasetIds.put(datasetId, dataset);
2939   }
2940   /**
2941    * make a new dataset ID for this jalview dataset alignment
2942    * @param dataset
2943    * @return
2944    */
2945   private String getDatasetIdRef(jalview.datamodel.Alignment dataset)
2946   {
2947     if (dataset.getDataset()!=null)
2948     {
2949       warn("Serious issue!  Dataset Object passed to getDatasetIdRef is not a Jalview DATASET alignment...");
2950     }
2951     String datasetId=makeHashCode(dataset, null);
2952     if (datasetId==null)
2953     {
2954       // make a new datasetId and record it
2955       if (dataset2Ids == null)
2956       {
2957         dataset2Ids = new IdentityHashMap();
2958       } else {
2959         datasetId = (String) dataset2Ids.get(dataset);
2960       }
2961       if (datasetId==null)
2962       {
2963         datasetId = "ds"+dataset2Ids.size()+1;
2964         dataset2Ids.put(dataset,datasetId);
2965       }
2966     }
2967     return datasetId;
2968   }
2969   private void addDBRefs(SequenceI datasetSequence, Sequence sequence)
2970   {
2971     for (int d = 0; d < sequence.getDBRefCount(); d++)
2972     {
2973       DBRef dr = sequence.getDBRef(d);
2974       jalview.datamodel.DBRefEntry entry = new jalview.datamodel.DBRefEntry(
2975               sequence.getDBRef(d).getSource(), sequence.getDBRef(d)
2976                       .getVersion(), sequence.getDBRef(d).getAccessionId());
2977       if (dr.getMapping() != null)
2978       {
2979         entry.setMap(addMapping(dr.getMapping()));
2980       }
2981       datasetSequence.addDBRef(entry);
2982     }
2983   }
2984
2985   private jalview.datamodel.Mapping addMapping(Mapping m)
2986   {
2987     SequenceI dsto = null;
2988     // Mapping m = dr.getMapping();
2989     int fr[] = new int[m.getMapListFromCount() * 2];
2990     Enumeration f = m.enumerateMapListFrom();
2991     for (int _i = 0; f.hasMoreElements(); _i += 2)
2992     {
2993       MapListFrom mf = (MapListFrom) f.nextElement();
2994       fr[_i] = mf.getStart();
2995       fr[_i + 1] = mf.getEnd();
2996     }
2997     int fto[] = new int[m.getMapListToCount() * 2];
2998     f = m.enumerateMapListTo();
2999     for (int _i = 0; f.hasMoreElements(); _i += 2)
3000     {
3001       MapListTo mf = (MapListTo) f.nextElement();
3002       fto[_i] = mf.getStart();
3003       fto[_i + 1] = mf.getEnd();
3004     }
3005     jalview.datamodel.Mapping jmap = new jalview.datamodel.Mapping(dsto,
3006             fr, fto, (int) m.getMapFromUnit(), (int) m.getMapToUnit());
3007     if (m.getMappingChoice() != null)
3008     {
3009       MappingChoice mc = m.getMappingChoice();
3010       if (mc.getDseqFor() != null)
3011       {
3012         String dsfor = ""+mc.getDseqFor();
3013         if (seqRefIds.containsKey(dsfor))
3014         {
3015           /**
3016            * recover from hash
3017            */
3018           jmap.setTo((SequenceI) seqRefIds.get(dsfor));
3019         }
3020         else
3021         {
3022           frefedSequence.add(new Object[]
3023           { dsfor, jmap });
3024         }
3025       }
3026       else
3027       {
3028         /**
3029          * local sequence definition
3030          */
3031         Sequence ms = mc.getSequence();
3032         jalview.datamodel.Sequence djs = null;
3033         String sqid = ms.getDsseqid();
3034         if (sqid != null && sqid.length() > 0)
3035         {
3036           /*
3037            * recover dataset sequence
3038            */
3039           djs = (jalview.datamodel.Sequence) seqRefIds.get(sqid);
3040         }
3041         else
3042         {
3043           System.err
3044                   .println("Warning - making up dataset sequence id for DbRef sequence map reference");
3045           sqid = ((Object) ms).toString(); // make up a new hascode for
3046           // undefined dataset sequence hash
3047           // (unlikely to happen)
3048         }
3049
3050         if (djs == null)
3051         {
3052           /**
3053            * make a new dataset sequence and add it to refIds hash
3054            */
3055           djs = new jalview.datamodel.Sequence(ms.getName(), ms
3056                   .getSequence());
3057           djs.setStart(jmap.getMap().getToLowest());
3058           djs.setEnd(jmap.getMap().getToHighest());
3059           djs.setVamsasId(uniqueSetSuffix + sqid);
3060           jmap.setTo(djs);
3061           seqRefIds.put(sqid, djs);
3062
3063         }
3064         jalview.bin.Cache.log.debug("about to recurse on addDBRefs.");
3065         addDBRefs(djs, ms);
3066
3067       }
3068     }
3069     return (jmap);
3070
3071   }
3072
3073   public jalview.gui.AlignmentPanel copyAlignPanel(AlignmentPanel ap,
3074           boolean keepSeqRefs)
3075   {
3076     initSeqRefs();
3077     jalview.schemabinding.version2.JalviewModel jm = SaveState(ap, null,
3078             null);
3079
3080     if (!keepSeqRefs)
3081     {
3082       clearSeqRefs();
3083       jm.getJalviewModelSequence().getViewport(0).setSequenceSetId(null);
3084     }
3085     else
3086     {
3087       uniqueSetSuffix = "";
3088       jm.getJalviewModelSequence().getViewport(0).setId(null); // we don't overwrite the view we just copied
3089     }
3090     if (this.frefedSequence==null)
3091     {
3092       frefedSequence = new Vector();
3093     }
3094
3095     viewportsAdded = new Hashtable();
3096
3097     AlignFrame af = LoadFromObject(jm, null, false, null);
3098     af.alignPanels.clear();
3099     af.closeMenuItem_actionPerformed(true);
3100
3101     /*
3102      * if(ap.av.alignment.getAlignmentAnnotation()!=null) { for(int i=0; i<ap.av.alignment.getAlignmentAnnotation().length;
3103      * i++) { if(!ap.av.alignment.getAlignmentAnnotation()[i].autoCalculated) {
3104      * af.alignPanel.av.alignment.getAlignmentAnnotation()[i] =
3105      * ap.av.alignment.getAlignmentAnnotation()[i]; } } }
3106      */
3107
3108     return af.alignPanel;
3109   }
3110
3111   /**
3112    * flag indicating if hashtables should be cleared on finalization TODO this
3113    * flag may not be necessary
3114    */
3115   private boolean _cleartables = true;
3116
3117   private Hashtable jvids2vobj;
3118
3119   /*
3120    * (non-Javadoc)
3121    * 
3122    * @see java.lang.Object#finalize()
3123    */
3124   protected void finalize() throws Throwable
3125   {
3126     // really make sure we have no buried refs left.
3127     if (_cleartables)
3128     {
3129       clearSeqRefs();
3130     }
3131     this.seqRefIds = null;
3132     this.seqsToIds = null;
3133     super.finalize();
3134   }
3135
3136   private void warn(String msg)
3137   {
3138     warn(msg, null);
3139   }
3140
3141   private void warn(String msg, Exception e)
3142   {
3143     if (Cache.log != null)
3144     {
3145       if (e != null)
3146       {
3147         Cache.log.warn(msg, e);
3148       }
3149       else
3150       {
3151         Cache.log.warn(msg);
3152       }
3153     }
3154     else
3155     {
3156       System.err.println("Warning: " + msg);
3157       if (e != null)
3158       {
3159         e.printStackTrace();
3160       }
3161     }
3162   }
3163
3164   private void debug(String string)
3165   {
3166     debug(string,null);
3167   }
3168   private void debug(String msg, Exception e)
3169   {
3170     if (Cache.log != null)
3171     {
3172       if (e != null)
3173       {
3174         Cache.log.debug(msg, e);
3175       }
3176       else
3177       {
3178         Cache.log.debug(msg);
3179       }
3180     }
3181     else
3182     {
3183       System.err.println("Warning: " + msg);
3184       if (e != null)
3185       {
3186         e.printStackTrace();
3187       }
3188     }
3189   }
3190
3191   /**
3192    * set the object to ID mapping tables used to write/recover objects and XML
3193    * ID strings for the jalview project. If external tables are provided then
3194    * finalize and clearSeqRefs will not clear the tables when the Jalview2XML
3195    * object goes out of scope. - also populates the datasetIds hashtable with
3196    * alignment objects containing dataset sequences
3197    * 
3198    * @param vobj2jv
3199    *                Map from ID strings to jalview datamodel
3200    * @param jv2vobj
3201    *                Map from jalview datamodel to ID strings
3202    * 
3203    * 
3204    */
3205   public void setObjectMappingTables(Hashtable vobj2jv,
3206           IdentityHashMap jv2vobj)
3207   {
3208     this.jv2vobj = jv2vobj;
3209     this.vobj2jv = vobj2jv;
3210     Iterator ds = jv2vobj.keySet().iterator();
3211     String id;
3212     while (ds.hasNext())
3213     {
3214       Object jvobj = ds.next();
3215       id = jv2vobj.get(jvobj).toString();
3216       if (jvobj instanceof jalview.datamodel.Alignment)
3217       {
3218         if (((jalview.datamodel.Alignment) jvobj).getDataset() == null)
3219         {
3220           addDatasetRef(id, (jalview.datamodel.Alignment) jvobj);
3221         }
3222       }
3223       else if (jvobj instanceof jalview.datamodel.Sequence)
3224       {
3225         // register sequence object so the XML parser can recover it.
3226         if (seqRefIds == null)
3227         {
3228           seqRefIds = new Hashtable();
3229         }
3230         if (seqsToIds == null)
3231         {
3232           seqsToIds = new IdentityHashMap();
3233         }
3234         seqRefIds.put(jv2vobj.get(jvobj).toString(), jvobj);
3235         seqsToIds.put(jvobj, id);
3236       }
3237       else if (jvobj instanceof jalview.datamodel.AlignmentAnnotation)
3238       {
3239         if (annotationIds == null)
3240         {
3241           annotationIds = new Hashtable();
3242         }
3243         String anid;
3244         annotationIds.put(anid = jv2vobj.get(jvobj).toString(), jvobj);
3245         jalview.datamodel.AlignmentAnnotation jvann = (jalview.datamodel.AlignmentAnnotation) jvobj;
3246         if (jvann.annotationId == null)
3247         {
3248           jvann.annotationId = anid;
3249         }
3250         if (!jvann.annotationId.equals(anid))
3251         {
3252           // TODO verify that this is the correct behaviour
3253           this.warn("Overriding Annotation ID for " + anid
3254                   + " from different id : " + jvann.annotationId);
3255           jvann.annotationId = anid;
3256         }
3257       }
3258       else if (jvobj instanceof String)
3259       {
3260         if (jvids2vobj == null)
3261         {
3262           jvids2vobj = new Hashtable();
3263           jvids2vobj.put(jvobj, jv2vobj.get(jvobj).toString());
3264         }
3265       }
3266       else
3267         Cache.log.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
3268     }
3269   }
3270
3271   /**
3272    * set the uniqueSetSuffix used to prefix/suffix object IDs for jalview
3273    * objects created from the project archive. If string is null (default for
3274    * construction) then suffix will be set automatically.
3275    * 
3276    * @param string
3277    */
3278   public void setUniqueSetSuffix(String string)
3279   {
3280     uniqueSetSuffix = string;
3281
3282   }
3283
3284   /**
3285    * uses skipList2 as the skipList for skipping views on sequence sets
3286    * associated with keys in the skipList
3287    * 
3288    * @param skipList2
3289    */
3290   public void setSkipList(Hashtable skipList2)
3291   {
3292     skipList = skipList2;
3293   }
3294
3295 }