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