Check hiddenSequence before placing in tmp
[jalview.git] / src / jalview / gui / Jalview2XML.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 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
22 import jalview.schemes.*;
23
24 import jalview.gui.*;
25
26 import java.io.*;
27
28 import java.net.*;
29
30 import java.util.*;
31
32 import java.util.jar.*;
33
34 import javax.swing.*;
35
36 import org.exolab.castor.xml.*;
37
38 import jalview.schemabinding.version2.*;
39
40
41
42
43 /**
44  * DOCUMENT ME!
45  *
46  * @author $author$
47  * @version $Revision$
48  */
49 public class Jalview2XML
50 {
51     // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE
52     public void SaveState(File statefile)
53     {
54         JInternalFrame[] frames = Desktop.desktop.getAllFrames();
55
56         if (frames == null)
57         {
58             return;
59         }
60
61         try
62         {
63             FileOutputStream fos = new FileOutputStream(statefile);
64             JarOutputStream jout = new JarOutputStream(fos);
65
66             //NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS
67             ////////////////////////////////////////////////////
68             PrintWriter out = new PrintWriter(new OutputStreamWriter(jout,
69                         "UTF-8"));
70
71             Vector shortNames = new Vector();
72
73             //REVERSE ORDER
74             for (int i = frames.length - 1; i > -1; i--)
75             {
76                 if (frames[i] instanceof AlignFrame)
77                 {
78                     AlignFrame af = (AlignFrame) frames[i];
79
80                     String shortName = af.getTitle();
81
82                     if (shortName.indexOf(File.separatorChar) > -1)
83                     {
84                         shortName = shortName.substring(shortName.lastIndexOf(
85                                     File.separatorChar) + 1);
86                     }
87
88                     int count = 1;
89
90                     while (shortNames.contains(shortName))
91                     {
92                         if (shortName.endsWith("_" + (count - 1)))
93                         {
94                             shortName = shortName.substring(0,
95                                     shortName.lastIndexOf("_"));
96                         }
97
98                         shortName = shortName.concat("_" + count);
99                         count++;
100                     }
101
102                     shortNames.addElement(shortName);
103
104                     if (!shortName.endsWith(".xml"))
105                     {
106                         shortName = shortName + ".xml";
107                     }
108
109                     int ap, apSize= af.alignPanels.size();
110                     for (ap = 0; ap < apSize; ap++)
111                     {
112                       AlignmentPanel apanel = (AlignmentPanel) af.alignPanels.
113                           elementAt(ap);
114
115                       if (apanel.av.explodedPosition == null)
116                         apanel.av.explodedPosition = af.getBounds();
117
118                       SaveState(apanel,
119                                 apSize == 1 ? shortName : ap+shortName,
120                                 jout, out);
121                     }
122                 }
123             }
124
125             out.close();
126             jout.close();
127         }
128         catch (Exception ex)
129         {
130             ex.printStackTrace();
131         }
132     }
133
134     // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW
135     public void SaveAlignment(AlignFrame af, String jarFile,
136         String fileName)
137     {
138         try
139         {
140           int ap, apSize= af.alignPanels.size();
141           FileOutputStream fos = new FileOutputStream(jarFile);
142           JarOutputStream jout = new JarOutputStream(fos);
143           PrintWriter out = new PrintWriter(new OutputStreamWriter(jout,
144                 "UTF-8"));
145           for( ap=0; ap<apSize; ap++)
146           {
147             AlignmentPanel apanel = (AlignmentPanel)af.alignPanels.elementAt(ap);
148
149             if (apanel.av.explodedPosition == null)
150               apanel.av.explodedPosition = af.getBounds();
151
152             SaveState(apanel,
153                       apSize==1?fileName:fileName+ap,
154                       jout, out);
155           }
156
157           out.close();
158           jout.close();
159         }
160         catch (Exception ex)
161         {
162           ex.printStackTrace();
163         }
164     }
165
166     /**
167      * DOCUMENT ME!
168      *
169      * @param af DOCUMENT ME!
170      * @param timeStamp DOCUMENT ME!
171      * @param fileName DOCUMENT ME!
172      * @param jout DOCUMENT ME!
173      * @param out DOCUMENT ME!
174      */
175     public void SaveState(AlignmentPanel ap,
176                           String fileName,
177                           JarOutputStream jout,
178                           PrintWriter out)
179     {
180       if (seqRefIds == null)
181         seqRefIds = new Hashtable();
182
183         Vector userColours = new Vector();
184
185         AlignViewport av = ap.av;
186
187         JalviewModel object = new JalviewModel();
188         object.setVamsasModel(new jalview.schemabinding.version2.VamsasModel());
189
190         object.setCreationDate(new java.util.Date(System.currentTimeMillis()));
191         object.setVersion(jalview.bin.Cache.getProperty("VERSION"));
192
193         jalview.datamodel.AlignmentI jal = av.alignment;
194
195         if(av.hasHiddenRows)
196         {
197           jal = jal.getHiddenSequences().getFullAlignment();
198         }
199
200         SequenceSet vamsasSet = new SequenceSet();
201         Sequence vamsasSeq;
202         JalviewModelSequence jms = new JalviewModelSequence();
203
204         vamsasSet.setGapChar(jal.getGapCharacter() + "");
205
206         JSeq jseq;
207         Vector pdbfiles = null;
208
209         //SAVE SEQUENCES
210         int id = 0;
211         for (int i = 0; i < jal.getHeight(); i++)
212         {
213             id = jal.getSequenceAt(i).hashCode();
214
215             if(seqRefIds.get(id+"")!=null)
216             {
217
218             }
219             else
220             {
221               vamsasSeq = new Sequence();
222               vamsasSeq.setId(id + "");
223               vamsasSeq.setName(jal.getSequenceAt(i).getName());
224               vamsasSeq.setSequence(jal.getSequenceAt(i).getSequence());
225               vamsasSeq.setDescription(jal.getSequenceAt(i).getDescription());
226
227               if (jal.getSequenceAt(i).getDatasetSequence().getDBRef() != null)
228               {
229                 jalview.datamodel.DBRefEntry[] dbrefs =
230                     jal.getSequenceAt(i).getDatasetSequence().getDBRef();
231
232                 for (int d = 0; d < dbrefs.length; d++)
233                 {
234                   DBRef dbref = new DBRef();
235                   dbref.setSource(dbrefs[d].getSource());
236                   dbref.setVersion(dbrefs[d].getVersion());
237                   dbref.setAccessionId(dbrefs[d].getAccessionId());
238                   vamsasSeq.addDBRef(dbref);
239                 }
240               }
241
242               vamsasSet.addSequence(vamsasSeq);
243               seqRefIds.put(id+"", vamsasSeq);
244             }
245
246             jseq = new JSeq();
247             jseq.setStart(jal.getSequenceAt(i).getStart());
248             jseq.setEnd(jal.getSequenceAt(i).getEnd());
249             jseq.setColour(jal.getSequenceAt(i).getColor().getRGB());
250
251             jseq.setId(id);
252
253             if (av.hasHiddenRows)
254             {
255               jseq.setHidden(av.alignment.getHiddenSequences().isHidden(
256                   jal.getSequenceAt(i)));
257
258               if(jal.getSequenceAt(i).getHiddenSequences()!=null)
259               {
260                 jalview.datamodel.SequenceI [] reps =
261                     jal.getSequenceAt(i).getHiddenSequences().getSequencesInOrder(jal);
262
263                 for(int h=0; h<reps.length; h++)
264                 {
265                   jseq.addHiddenSequences(
266                       jal.findIndex(reps[h])
267                       );
268                 }
269               }
270             }
271
272
273             if(jal.getSequenceAt(i).getDatasetSequence().getSequenceFeatures()!=null)
274             {
275               jalview.datamodel.SequenceFeature[] sf
276                   = jal.getSequenceAt(i).getDatasetSequence().getSequenceFeatures();
277               int index = 0;
278               while(index < sf.length)
279               {
280                 Features features = new Features();
281
282                 features.setBegin(sf[index].getBegin());
283                 features.setEnd(sf[index].getEnd());
284                 features.setDescription(sf[index].getDescription());
285                 features.setType(sf[index].getType());
286                 features.setFeatureGroup(sf[index].getFeatureGroup());
287                 features.setScore(sf[index].getScore());
288                 if(sf[index].links!=null)
289                 {
290                   for(int l=0; l<sf[index].links.size(); l++)
291                   {
292                     OtherData keyValue = new OtherData();
293                     keyValue.setKey("LINK_"+l);
294                     keyValue.setValue(sf[index].links.elementAt(l).toString());
295                     features.addOtherData(keyValue);
296                   }
297                 }
298                 if(sf[index].otherDetails!=null)
299                 {
300                   String key;
301                   Enumeration keys = sf[index].otherDetails.keys();
302                   while(keys.hasMoreElements())
303                   {
304                     key = keys.nextElement().toString();
305                     OtherData keyValue = new OtherData();
306                     keyValue.setKey( key );
307                     keyValue.setValue(
308                         sf[index].otherDetails.get(key).toString());
309                     features.addOtherData(keyValue);
310                   }
311                 }
312
313                 jseq.addFeatures(features);
314                 index ++;
315               }
316             }
317
318             if(jal.getSequenceAt(i).getDatasetSequence().getPDBId()!=null)
319             {
320               Enumeration en = jal.getSequenceAt(i).getDatasetSequence().getPDBId().elements();
321               while(en.hasMoreElements())
322               {
323                 Pdbids pdb = new Pdbids();
324                 jalview.datamodel.PDBEntry entry
325                    = (jalview.datamodel.PDBEntry)en.nextElement();
326
327                 pdb.setId(entry.getId());
328                 pdb.setType(entry.getType());
329
330                 if(entry.getFile()!=null)
331                 {
332                   if(pdbfiles==null)
333                     pdbfiles = new Vector();
334
335
336                   if(!pdbfiles.contains(entry.getId()))
337                   {
338                     pdbfiles.addElement(entry.getId());
339                     try
340                     {
341                       File file = new File(entry.getFile());
342                       if(file.exists())
343                       {
344                         byte[] data = new byte[ (int) file.length()];
345                         jout.putNextEntry(new JarEntry(entry.getId()));
346                         DataInputStream dis = new DataInputStream(new
347                             FileInputStream(file));
348                         dis.readFully(data);
349
350                         DataOutputStream dout = new DataOutputStream(jout);
351                         dout.write(data, 0, data.length);
352                         jout.closeEntry();
353                       }
354                     }
355                     catch (Exception ex)
356                     {
357                       ex.printStackTrace();
358                     }
359                   }
360                 }
361
362
363                 if(entry.getProperty()!=null)
364                 {
365                   PdbentryItem item = new PdbentryItem();
366                   Hashtable properties = entry.getProperty();
367                   Enumeration en2 = properties.keys();
368                   while(en2.hasMoreElements())
369                   {
370                     Property prop = new Property();
371                     String key = en2.nextElement().toString();
372                     prop.setName(key);
373                     prop.setValue( properties.get(key).toString() );
374                     item.addProperty(prop);
375                   }
376                   pdb.addPdbentryItem(item);
377                 }
378
379                 jseq.addPdbids(pdb);
380               }
381             }
382
383             jms.addJSeq(jseq);
384         }
385
386         if(av.hasHiddenRows)
387           jal = av.alignment;
388
389
390         //SAVE TREES
391         ///////////////////////////////////
392         if (av.currentTree != null)
393         {
394           // FIND ANY ASSOCIATED TREES
395           // NOT IMPLEMENTED FOR HEADLESS STATE AT PRESENT
396           if (Desktop.desktop != null)
397           {
398             JInternalFrame[] frames = Desktop.desktop.getAllFrames();
399
400             for (int t = 0; t < frames.length; t++)
401             {
402               if (frames[t] instanceof TreePanel)
403               {
404                 TreePanel tp = (TreePanel) frames[t];
405
406                 if (tp.treeCanvas.av.alignment == jal)
407                 {
408                   Tree tree = new Tree();
409                   tree.setTitle(tp.getTitle());
410                   tree.setCurrentTree( (av.currentTree == tp.getTree()));
411                   tree.setNewick(tp.getTree().toString());
412                   tree.setThreshold(tp.treeCanvas.threshold);
413
414                   tree.setFitToWindow(tp.fitToWindow.getState());
415                   tree.setFontName(tp.getTreeFont().getName());
416                   tree.setFontSize(tp.getTreeFont().getSize());
417                   tree.setFontStyle(tp.getTreeFont().getStyle());
418                   tree.setMarkUnlinked(tp.placeholdersMenu.getState());
419
420                   tree.setShowBootstrap(tp.bootstrapMenu.getState());
421                   tree.setShowDistances(tp.distanceMenu.getState());
422
423                   tree.setHeight(tp.getHeight());
424                   tree.setWidth(tp.getWidth());
425                   tree.setXpos(tp.getX());
426                   tree.setYpos(tp.getY());
427
428                   jms.addTree(tree);
429                 }
430               }
431             }
432           }
433         }
434
435         //SAVE ANNOTATIONS
436         if (jal.getAlignmentAnnotation() != null)
437         {
438             jalview.datamodel.AlignmentAnnotation[] aa = jal.getAlignmentAnnotation();
439
440             for (int i = 0; i < aa.length; i++)
441             {
442                 Annotation an = new Annotation();
443
444                 if (aa[i].label.equals("Quality") ||
445                         aa[i].label.equals("Conservation") ||
446                         aa[i].label.equals("Consensus"))
447                 {
448                     an.setLabel(aa[i].label);
449                     an.setGraph(true);
450                     vamsasSet.addAnnotation(an);
451                     continue;
452                 }
453
454
455                 an.setDescription(aa[i].description);
456
457                 if(aa[i].sequenceRef!=null)
458                  {
459                    an.setSequenceRef(aa[i].sequenceRef.getName());
460                  }
461
462                 if(aa[i].graph>0)
463                 {
464                   an.setGraph(true);
465                   an.setGraphType(aa[i].graph);
466                   an.setGraphGroup(aa[i].graphGroup);
467                   if(aa[i].getThreshold()!=null)
468                   {
469                     ThresholdLine line = new ThresholdLine();
470                     line.setLabel(aa[i].getThreshold().label);
471                     line.setValue(aa[i].getThreshold().value);
472                     line.setColour(aa[i].getThreshold().colour.getRGB());
473                     an.setThresholdLine(line);
474                   }
475                 }
476                 else
477                   an.setGraph(false);
478
479                 an.setLabel(aa[i].label);
480
481                 AnnotationElement ae;
482
483                 for (int a = 0; a < aa[i].annotations.length; a++)
484                 {
485                     if ((aa[i] == null) || (aa[i].annotations[a] == null))
486                     {
487                         continue;
488                     }
489
490                     ae = new AnnotationElement();
491                     ae.setDescription(aa[i].annotations[a].description);
492                     ae.setDisplayCharacter(aa[i].annotations[a].displayCharacter);
493                     ae.setValue(aa[i].annotations[a].value);
494                     ae.setPosition(a);
495                     ae.setSecondaryStructure(aa[i].annotations[a].secondaryStructure +
496                         "");
497
498                     if(aa[i].annotations[a].colour!=java.awt.Color.black)
499                       ae.setColour(aa[i].annotations[a].colour.getRGB());
500
501                     an.addAnnotationElement(ae);
502                 }
503
504                 vamsasSet.addAnnotation(an);
505             }
506         }
507
508         //SAVE GROUPS
509         if (jal.getGroups() != null)
510         {
511             JGroup[] groups = new JGroup[jal.getGroups().size()];
512
513             for (int i = 0; i < groups.length; i++)
514             {
515                 groups[i] = new JGroup();
516
517                 jalview.datamodel.SequenceGroup sg = (jalview.datamodel.SequenceGroup) jal.getGroups()
518                                                                                           .elementAt(i);
519                 groups[i].setStart(sg.getStartRes());
520                 groups[i].setEnd(sg.getEndRes());
521                 groups[i].setName(sg.getName());
522                 if(sg.cs!=null)
523                 {
524                   if (sg.cs.conservationApplied())
525                   {
526                     groups[i].setConsThreshold(sg.cs.getConservationInc());
527
528                     if (sg.cs instanceof jalview.schemes.UserColourScheme)
529                     {
530                       groups[i].setColour(SetUserColourScheme(sg.cs,
531                           userColours,
532                           jms));
533                     }
534                     else
535                     {
536                       groups[i].setColour(ColourSchemeProperty.getColourName(sg.
537                           cs));
538                     }
539                   }
540                   else if(sg.cs instanceof jalview.schemes.AnnotationColourGradient)
541                   {
542                     groups[i].setColour(
543                         ColourSchemeProperty.getColourName(
544                       ( (jalview.schemes.AnnotationColourGradient) sg.cs).getBaseColour()));
545                   }
546                   else if (sg.cs instanceof jalview.schemes.UserColourScheme)
547                   {
548                     groups[i].setColour(SetUserColourScheme(sg.cs, userColours,
549                         jms));
550                   }
551                   else
552                   {
553                     groups[i].setColour(ColourSchemeProperty.getColourName(
554                         sg.cs));
555                   }
556
557                   groups[i].setPidThreshold(sg.cs.getThreshold());
558                 }
559
560                 groups[i].setOutlineColour(sg.getOutlineColour().getRGB());
561                 groups[i].setDisplayBoxes(sg.getDisplayBoxes());
562                 groups[i].setDisplayText(sg.getDisplayText());
563                 groups[i].setColourText(sg.getColourText());
564
565                 for (int s = 0; s < sg.getSize(false); s++)
566                 {
567                     jalview.datamodel.Sequence seq =
568                         (jalview.datamodel.Sequence) sg.getSequenceAt(s);
569                     groups[i].addSeq(seq.hashCode());
570                 }
571             }
572
573             jms.setJGroup(groups);
574         }
575
576
577         ///////////SAVE VIEWPORT
578         Viewport view = new Viewport();
579         view.setTitle(ap.alignFrame.getTitle());
580         view.setSequenceSetId(av.getSequenceSetId());
581         view.setViewName(av.viewName);
582         view.setGatheredViews(av.gatherViewsHere);
583
584         view.setXpos(av.explodedPosition.x);
585         view.setYpos(av.explodedPosition.y);
586         view.setWidth(av.explodedPosition.width);
587         view.setHeight(av.explodedPosition.height);
588         view.setStartRes(av.startRes);
589         view.setStartSeq(av.startSeq);
590
591         if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme)
592         {
593             view.setBgColour(SetUserColourScheme(av.getGlobalColourScheme(),
594                     userColours, jms));
595         }
596         else if(av.getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient)
597         {
598            jalview.schemes.AnnotationColourGradient acg
599               = (jalview.schemes.AnnotationColourGradient)av.getGlobalColourScheme();
600
601             AnnotationColours ac = new AnnotationColours();
602             ac.setAboveThreshold(acg.getAboveThreshold());
603             ac.setThreshold(acg.getAnnotationThreshold());
604             ac.setAnnotation(acg.getAnnotation());
605             if(acg.getBaseColour() instanceof jalview.schemes.UserColourScheme)
606               ac.setColourScheme(SetUserColourScheme(acg.getBaseColour(),
607                     userColours, jms));
608             else
609               ac.setColourScheme(ColourSchemeProperty.getColourName(acg.getBaseColour()));
610
611             ac.setMaxColour(acg.getMaxColour().getRGB());
612             ac.setMinColour(acg.getMinColour().getRGB());
613             view.setAnnotationColours(ac);
614             view.setBgColour("AnnotationColourGradient");
615         }
616         else
617         {
618             view.setBgColour(ColourSchemeProperty.getColourName(
619                     av.getGlobalColourScheme()));
620         }
621
622         ColourSchemeI cs = av.getGlobalColourScheme();
623
624         if(cs!=null)
625         {
626           if (cs.conservationApplied())
627           {
628             view.setConsThreshold(cs.getConservationInc());
629             if (cs instanceof jalview.schemes.UserColourScheme)
630               view.setBgColour(SetUserColourScheme(cs, userColours, jms));
631           }
632
633           if (cs instanceof ResidueColourScheme)
634           {
635             view.setPidThreshold(cs.getThreshold());
636           }
637         }
638
639         view.setConservationSelected(av.getConservationSelected());
640         view.setPidSelected(av.getAbovePIDThreshold());
641         view.setFontName(av.font.getName());
642         view.setFontSize(av.font.getSize());
643         view.setFontStyle(av.font.getStyle());
644         view.setRenderGaps(av.renderGaps);
645         view.setShowAnnotation(av.getShowAnnotation());
646         view.setShowBoxes(av.getShowBoxes());
647         view.setShowColourText(av.getColourText());
648         view.setShowFullId(av.getShowJVSuffix());
649         view.setShowSequenceFeatures(av.showSequenceFeatures);
650         view.setShowText(av.getShowText());
651         view.setWrapAlignment(av.getWrapAlignment());
652
653         if(av.featuresDisplayed!=null)
654         {
655           jalview.schemabinding.version2.FeatureSettings fs
656               = new jalview.schemabinding.version2.FeatureSettings();
657
658           String [] renderOrder =
659               ap.seqPanel.seqCanvas.getFeatureRenderer().renderOrder;
660
661           Vector settingsAdded = new Vector();
662           for(int ro=0; ro<renderOrder.length; ro++)
663           {
664             Setting setting = new Setting();
665             setting.setType(renderOrder[ro]);
666             setting.setColour(
667                 ap.seqPanel.seqCanvas.getFeatureRenderer().getColour(renderOrder[ro]).getRGB()
668                 );
669
670             setting.setDisplay(
671                 av.featuresDisplayed.containsKey(renderOrder[ro])
672                 );
673
674             fs.addSetting(setting);
675             settingsAdded.addElement(renderOrder[ro]);
676           }
677
678           //Make sure we save none displayed feature settings
679           Enumeration en =
680               ap.seqPanel.seqCanvas.getFeatureRenderer().featureColours.keys();
681           while(en.hasMoreElements())
682           {
683             String key = en.nextElement().toString();
684             if(settingsAdded.contains(key))
685               continue;
686
687             Setting setting = new Setting();
688             setting.setType(key);
689             setting.setColour(
690                 ap.seqPanel.seqCanvas.getFeatureRenderer().getColour(key).getRGB()
691                 );
692
693             setting.setDisplay(false);
694
695             fs.addSetting(setting);
696             settingsAdded.addElement(key);
697           }
698
699           jms.setFeatureSettings(fs);
700
701         }
702
703         if(av.hasHiddenColumns)
704         {
705           for(int c=0; c<av.getColumnSelection().getHiddenColumns().size(); c++)
706           {
707             int [] region = (int[]) av.getColumnSelection().getHiddenColumns().elementAt(c);
708             HiddenColumns hc = new HiddenColumns();
709             hc.setStart(region[0]);
710             hc.setEnd(region[1]);
711             view.addHiddenColumns(hc);
712           }
713         }
714
715         jms.addViewport(view);
716
717
718         object.setJalviewModelSequence(jms);
719         object.getVamsasModel().addSequenceSet(vamsasSet);
720
721         try
722         {
723             if (!fileName.endsWith(".xml"))
724             {
725                 fileName = fileName + ".xml";
726             }
727
728             JarEntry entry = new JarEntry(fileName);
729             jout.putNextEntry(entry);
730
731             object.marshal(out);
732         }
733         catch (Exception ex)
734         {
735             ex.printStackTrace();
736         }
737     }
738
739     String SetUserColourScheme(jalview.schemes.ColourSchemeI cs,
740         Vector userColours, JalviewModelSequence jms)
741     {
742         String id = null;
743         jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs;
744
745         if (!userColours.contains(ucs))
746         {
747             userColours.add(ucs);
748
749             java.awt.Color[] colours = ucs.getColours();
750             jalview.schemabinding.version2.UserColours uc = new jalview.schemabinding.version2.UserColours();
751             jalview.schemabinding.version2.UserColourScheme jbucs = new jalview.schemabinding.version2.UserColourScheme();
752
753             for (int i = 0; i < colours.length; i++)
754             {
755                 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
756                 col.setName(ResidueProperties.aa[i]);
757                 col.setRGB(jalview.util.Format.getHexString(colours[i]));
758                 jbucs.addColour(col);
759             }
760             if(ucs.getLowerCaseColours()!=null)
761             {
762               colours = ucs.getLowerCaseColours();
763               for (int i = 0; i < colours.length; i++)
764              {
765                  jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
766                  col.setName(ResidueProperties.aa[i].toLowerCase());
767                  col.setRGB(jalview.util.Format.getHexString(colours[i]));
768                  jbucs.addColour(col);
769              }
770             }
771
772             id = "ucs" + userColours.indexOf(ucs);
773             uc.setId(id);
774             uc.setUserColourScheme(jbucs);
775             jms.addUserColours(uc);
776         }
777
778         return id;
779     }
780
781    jalview.schemes.UserColourScheme GetUserColourScheme(
782         JalviewModelSequence jms, String id)
783     {
784         UserColours[] uc = jms.getUserColours();
785         UserColours colours = null;
786
787         for (int i = 0; i < uc.length; i++)
788         {
789             if (uc[i].getId().equals(id))
790             {
791                 colours = uc[i];
792
793                 break;
794             }
795         }
796
797         java.awt.Color[] newColours = new java.awt.Color[24];
798
799         for (int i = 0; i < 24; i++)
800         {
801             newColours[i] = new java.awt.Color(Integer.parseInt(
802                         colours.getUserColourScheme().getColour(i).getRGB(), 16));
803         }
804
805         jalview.schemes.UserColourScheme ucs =
806             new jalview.schemes.UserColourScheme(newColours);
807
808         if (colours.getUserColourScheme().getColourCount() > 24)
809         {
810           newColours = new java.awt.Color[23];
811           for (int i = 0; i < 23; i++)
812           {
813             newColours[i] = new java.awt.Color(Integer.parseInt(
814                 colours.getUserColourScheme().getColour(i+24).getRGB(), 16));
815           }
816           ucs.setLowerCaseColours(newColours);
817         }
818
819         return ucs;
820     }
821
822     Hashtable seqRefIds;
823
824     /**
825      * DOCUMENT ME!
826      *
827      * @param file DOCUMENT ME!
828      */
829     public AlignFrame LoadJalviewAlign(final String file)
830     {
831         jalview.gui.AlignFrame af = null;
832
833         seqRefIds = new Hashtable();
834         Vector gatherToThisFrame= new Vector();
835
836         try
837         {
838             //UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING
839             URL url = null;
840
841             if (file.startsWith("http://"))
842             {
843                 url = new URL(file);
844             }
845
846             JarInputStream jin = null;
847             JarEntry jarentry = null;
848             int entryCount = 1;
849
850             do
851             {
852                 if (url != null)
853                 {
854                     jin = new JarInputStream(url.openStream());
855                 }
856                 else
857                 {
858                     jin = new JarInputStream(new FileInputStream(file));
859                 }
860
861                 for (int i = 0; i < entryCount; i++)
862                 {
863                     jarentry = jin.getNextJarEntry();
864                 }
865
866                 if (jarentry != null && jarentry.getName().endsWith(".xml"))
867                 {
868                     InputStreamReader in = new InputStreamReader(jin, "UTF-8");
869                     JalviewModel object = new JalviewModel();
870
871                     Unmarshaller unmar = new Unmarshaller(object);
872                     unmar.setValidation(false);
873                     object = (JalviewModel) unmar.unmarshal( in );
874
875                     af = LoadFromObject(object, file);
876                     if(af.viewport.gatherViewsHere)
877                     {
878                       gatherToThisFrame.add(af);
879                     }
880                     entryCount++;
881                 }
882                 else if (jarentry != null)
883                 {
884                   //Some other file here.
885                   entryCount++;
886                 }
887             }
888             while (jarentry != null);
889         }
890         catch(java.net.UnknownHostException ex)
891         {
892           ex.printStackTrace();
893           System.err.println("Couldn't locate Jalview XML file : " +
894               ex + "\n");
895
896           javax.swing.SwingUtilities.invokeLater(new Runnable()
897           {
898             public void run()
899             {
900               JOptionPane.showInternalMessageDialog(Desktop.desktop,
901                   "Couldn't locate " + file,
902                   "URL not found",
903                   JOptionPane.WARNING_MESSAGE);
904             }
905           });
906         }
907         catch (Exception ex)
908         {
909           //Is Version 1 Jar file?
910           af =  new Jalview2XML_V1().LoadJalviewAlign(file);
911
912           if(af!=null)
913           {
914             System.out.println("Successfully loaded archive file");
915             return af;
916           }
917
918             System.err.println("Exception whilst loading jalview XML file : " +
919                 ex + "\n");
920             javax.swing.SwingUtilities.invokeLater(new Runnable()
921             {
922               public void run()
923               {
924
925                 JOptionPane.showInternalMessageDialog(Desktop.desktop,
926                     "Error loading  " + file,
927                     "Error loading Jalview file",
928                     JOptionPane.WARNING_MESSAGE);
929               }});
930         }
931
932         if (Desktop.instance != null)
933           Desktop.instance.stopLoading();
934
935         for (int i = 0; i < gatherToThisFrame.size(); i++)
936         {
937           Desktop.instance.gatherViews(
938               (AlignFrame) gatherToThisFrame.elementAt(i));
939         }
940
941         return af;
942     }
943
944     String loadPDBFile(String file, String pdbId)
945     {
946       try
947       {
948         JarInputStream jin = null;
949
950         if (file.startsWith("http://"))
951         {
952           jin = new JarInputStream(new URL(file).openStream());
953         }
954         else
955         {
956           jin = new JarInputStream(new FileInputStream(file));
957         }
958
959         JarEntry entry = null;
960         do
961         {
962           entry = jin.getNextJarEntry();
963         }
964         while (!entry.getName().equals(pdbId));
965
966         BufferedReader in = new BufferedReader(new InputStreamReader(jin));
967         File outFile = File.createTempFile("jalview_pdb", ".txt");
968         outFile.deleteOnExit();
969         PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
970         String data;
971
972         while ( (data = in.readLine()) != null)
973         {
974           out.println(data);
975         }
976         out.close();
977         return outFile.getAbsolutePath();
978
979       }
980       catch (Exception ex)
981       {
982         ex.printStackTrace();
983       }
984
985       return null;
986     }
987
988
989     AlignFrame LoadFromObject(JalviewModel object, String file)
990     {
991         SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0);
992         Sequence[] vamsasSeq = vamsasSet.getSequence();
993
994         JalviewModelSequence jms = object.getJalviewModelSequence();
995
996         Viewport[] views = jms.getViewport();
997         Viewport view = views[0]; // DEAL WITH MULTIPLE VIEWPORTS LATER
998
999         //////////////////////////////////
1000         //LOAD SEQUENCES
1001
1002         Vector hiddenSeqs = null;
1003         jalview.datamodel.Sequence jseq;
1004
1005         ArrayList tmpseqs = new ArrayList();
1006
1007
1008         boolean multipleView = false;
1009
1010         JSeq[] JSEQ = object.getJalviewModelSequence().getJSeq();
1011         for (int i = 0; i < JSEQ.length; i++)
1012         {
1013           String seqId = JSEQ[i].getId() + "";
1014
1015           if (seqRefIds.get(seqId) != null)
1016           {
1017             tmpseqs.add( (jalview.datamodel.Sequence) seqRefIds.get(seqId));
1018             multipleView = true;
1019           }
1020           else
1021           {
1022             jseq = new jalview.datamodel.Sequence(vamsasSeq[i].getName(),
1023                 vamsasSeq[i].getSequence());
1024             jseq.setDescription(vamsasSeq[i].getDescription());
1025             jseq.setStart(JSEQ[i].getStart());
1026             jseq.setEnd(JSEQ[i].getEnd());
1027             jseq.setColor(new java.awt.Color(JSEQ[i].getColour()));
1028             seqRefIds.put(vamsasSeq[i].getId(), jseq);
1029             tmpseqs.add( jseq );
1030           }
1031
1032
1033           if (JSEQ[i].getHidden())
1034           {
1035             if (hiddenSeqs == null)
1036               hiddenSeqs = new Vector();
1037
1038
1039             hiddenSeqs.addElement(
1040                 (jalview.datamodel.Sequence) seqRefIds.get(seqId));
1041           }
1042
1043         }
1044
1045         ///SequenceFeatures are added to the DatasetSequence,
1046         // so we must create the dataset before loading features
1047         /////////////////////////////////
1048
1049
1050       jalview.datamodel.Sequence[] orderedSeqs = new jalview.datamodel.Sequence[
1051           tmpseqs.size()];
1052
1053        tmpseqs.toArray(orderedSeqs) ;
1054
1055
1056         jalview.datamodel.Alignment al =
1057             new jalview.datamodel.Alignment(orderedSeqs);
1058
1059         al.setDataset(null);
1060         /////////////////////////////////
1061
1062
1063         Hashtable pdbloaded = new Hashtable();
1064         if(!multipleView)
1065         {
1066           for (int i = 0; i < vamsasSeq.length; i++)
1067           {
1068             if (JSEQ[i].getFeaturesCount() > 0)
1069             {
1070               Features[] features = JSEQ[i].getFeatures();
1071               for (int f = 0; f < features.length; f++)
1072               {
1073                 jalview.datamodel.SequenceFeature sf
1074                     = new jalview.datamodel.SequenceFeature(features[f].getType(),
1075                     features[f].getDescription(), features[f].getStatus(),
1076                     features[f].getBegin(), features[f].getEnd(),
1077                     features[f].getFeatureGroup());
1078
1079                 sf.setScore(features[f].getScore());
1080                 for (int od = 0; od < features[f].getOtherDataCount(); od++)
1081                 {
1082                   OtherData keyValue = features[f].getOtherData(od);
1083                   if (keyValue.getKey().startsWith("LINK"))
1084                     sf.addLink(keyValue.getValue());
1085                   else
1086                     sf.setValue(keyValue.getKey(), keyValue.getValue());
1087
1088                 }
1089
1090                 al.getSequenceAt(i).getDatasetSequence().addSequenceFeature(sf);
1091               }
1092             }
1093             if (JSEQ[i].getPdbidsCount() > 0)
1094             {
1095               Pdbids[] ids = JSEQ[i].getPdbids();
1096               for (int p = 0; p < ids.length; p++)
1097               {
1098                 jalview.datamodel.PDBEntry entry = new jalview.datamodel.
1099                     PDBEntry();
1100                 entry.setId(ids[p].getId());
1101                 entry.setType(ids[p].getType());
1102                 if (ids[p].getFile() != null)
1103                 {
1104                   if (!pdbloaded.containsKey(ids[p].getFile()))
1105                   {
1106                     String tmppdb = loadPDBFile(file, ids[p].getId());
1107                     entry.setFile(tmppdb);
1108                     pdbloaded.put(ids[p].getId(), tmppdb);
1109                   }
1110                   else
1111                     entry.setFile(pdbloaded.get(ids[p].getId()).toString());
1112                 }
1113
1114                 al.getSequenceAt(i).getDatasetSequence().addPDBId(entry);
1115               }
1116             }
1117             if (vamsasSeq[i].getDBRefCount() > 0)
1118             {
1119               for (int d = 0; d < vamsasSeq[i].getDBRefCount(); d++)
1120               {
1121                 jalview.datamodel.DBRefEntry entry =
1122                     new jalview.datamodel.DBRefEntry(
1123                         vamsasSeq[i].getDBRef(d).getSource(),
1124                         vamsasSeq[i].getDBRef(d).getVersion(),
1125                         vamsasSeq[i].getDBRef(d).getAccessionId()
1126                     );
1127                 al.getSequenceAt(i).getDatasetSequence().addDBRef(entry);
1128               }
1129
1130             }
1131           }
1132         }
1133
1134
1135          /////////////////////////////////
1136         //////////////////////////////////
1137         //LOAD ANNOTATIONS
1138         boolean hideQuality = true,
1139             hideConservation = true,
1140             hideConsensus = true;
1141
1142         if (vamsasSet.getAnnotationCount()>0)
1143         {
1144             Annotation[] an = vamsasSet.getAnnotation();
1145
1146             for (int i = 0; i < an.length; i++)
1147             {
1148                if (an[i].getLabel().equals("Quality"))
1149                 {
1150                  hideQuality = false;
1151                  continue;
1152                 }
1153                else if(an[i].getLabel().equals("Conservation"))
1154                {
1155                  hideConservation = false;
1156                  continue;
1157                }
1158                else if(an[i].getLabel().equals("Consensus"))
1159                {
1160                  hideConsensus = false;
1161                  continue;
1162                }
1163
1164                 AnnotationElement[] ae = an[i].getAnnotationElement();
1165                 jalview.datamodel.Annotation[] anot = new jalview.datamodel.Annotation[al.getWidth()];
1166
1167                 for (int aa = 0; aa < ae.length; aa++)
1168                 {
1169                     anot[ae[aa].getPosition()] = new jalview.datamodel.Annotation(ae[aa].getDisplayCharacter(),
1170                             ae[aa].getDescription(),
1171                             ae[aa].getSecondaryStructure().length()==0?' ':ae[aa].getSecondaryStructure().charAt(0),
1172                             ae[aa].getValue());
1173                     anot[ae[aa].getPosition()].colour = new java.awt.Color( ae[aa].getColour() );
1174                 }
1175
1176                 jalview.datamodel.AlignmentAnnotation jaa = null;
1177
1178                 if (an[i].getGraph())
1179                 {
1180                   jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
1181                       an[i].getDescription(), anot, 0, 0,
1182                       an[i].getGraphType());
1183
1184                   jaa.graphGroup = an[i].getGraphGroup();
1185
1186                   if (an[i].getThresholdLine() != null)
1187                   {
1188                     jaa.setThreshold(new jalview.datamodel.GraphLine(
1189                                   an[i].getThresholdLine().getValue(),
1190                                   an[i].getThresholdLine().getLabel(),
1191                                 new java.awt.Color(an[i].getThresholdLine().getColour()))
1192                              );
1193
1194                   }
1195
1196                 }
1197                 else
1198                 {
1199                     jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
1200                             an[i].getDescription(), anot);
1201                 }
1202
1203                 if(an[i].getSequenceRef()!=null)
1204                 {
1205                   jaa.createSequenceMapping(
1206                       al.findName(an[i].getSequenceRef()), 1, true
1207                       );
1208                   al.findName(an[i].getSequenceRef()).addAlignmentAnnotation(jaa);
1209                 }
1210
1211                 al.addAnnotation(jaa);
1212             }
1213         }
1214
1215
1216         //  af.changeColour() );
1217         /////////////////////////
1218         //LOAD GROUPS
1219         if (jms.getJGroupCount() > 0)
1220         {
1221             JGroup[] groups = jms.getJGroup();
1222
1223             for (int i = 0; i < groups.length; i++)
1224             {
1225                 ColourSchemeI cs = null;
1226
1227                 if (groups[i].getColour() != null)
1228                 {
1229                     if (groups[i].getColour().startsWith("ucs"))
1230                     {
1231                         cs = GetUserColourScheme(jms, groups[i].getColour());
1232                     }
1233                     else
1234                     {
1235                         cs = ColourSchemeProperty.getColour(al,
1236                                 groups[i].getColour());
1237                     }
1238
1239                     if(cs!=null)
1240                       cs.setThreshold(groups[i].getPidThreshold(), true);
1241                 }
1242
1243                 Vector seqs = new Vector();
1244
1245                 for (int s = 0; s < groups[i].getSeqCount(); s++)
1246                 {
1247                     String seqId = groups[i].getSeq(s)+"";
1248                     seqs.addElement((jalview.datamodel.SequenceI) seqRefIds.get(seqId));
1249                 }
1250
1251                 jalview.datamodel.SequenceGroup sg = new jalview.datamodel.SequenceGroup(seqs,
1252                         groups[i].getName(), cs, groups[i].getDisplayBoxes(),
1253                         groups[i].getDisplayText(), groups[i].getColourText(),
1254                         groups[i].getStart(), groups[i].getEnd());
1255
1256                 sg.setOutlineColour(new java.awt.Color(
1257                         groups[i].getOutlineColour()));
1258
1259                 if (groups[i].getConsThreshold() != 0)
1260                 {
1261                     jalview.analysis.Conservation c = new jalview.analysis.Conservation("All",
1262                             ResidueProperties.propHash, 3, sg.getSequences(false), 0,
1263                             sg.getWidth() - 1);
1264                     c.calculate();
1265                     c.verdict(false, 25);
1266                     sg.cs.setConservation(c);
1267                 }
1268
1269                 al.addGroup(sg);
1270             }
1271         }
1272
1273
1274         /////////////////////////////////
1275         // LOAD VIEWPORT
1276
1277         AlignFrame af = new AlignFrame(al,
1278                                        view.getWidth(),
1279                                        view.getHeight() );
1280
1281         af.viewport.sequenceSetID = view.getSequenceSetId();
1282
1283         af.viewport.gatherViewsHere = view.getGatheredViews();
1284
1285         if (view.getSequenceSetId() != null)
1286           PaintRefresher.Register(af.alignPanel, view.getSequenceSetId());
1287
1288         if(hiddenSeqs!=null)
1289         {
1290           for(int s=0; s<JSEQ.length; s++)
1291           {
1292             for(int r=0; r<JSEQ[s].getHiddenSequencesCount(); r++)
1293             {
1294               al.getSequenceAt(s).addHiddenSequence(
1295                   al.getSequenceAt( JSEQ[s].getHiddenSequences(r)  )
1296                   );
1297             }
1298           }
1299
1300           jalview.datamodel.SequenceI [] hseqs = new
1301               jalview.datamodel.SequenceI[hiddenSeqs.size()];
1302
1303           for(int s=0; s<hiddenSeqs.size(); s++)
1304             hseqs[s] = (jalview.datamodel.SequenceI)hiddenSeqs.elementAt(s);
1305
1306           af.viewport.hideSequence( hseqs );
1307
1308         }
1309
1310
1311         if((hideConsensus || hideQuality || hideConservation)
1312             && al.getAlignmentAnnotation()!=null)
1313         {
1314           int hSize = al.getAlignmentAnnotation().length;
1315           for (int h = 0; h < hSize; h++)
1316           {
1317             if (
1318                 (hideConsensus &&
1319                  al.getAlignmentAnnotation()[h].label.equals("Consensus"))
1320                 ||
1321                 (hideQuality &&
1322                  al.getAlignmentAnnotation()[h].label.equals("Quality"))
1323                 ||
1324                 (hideConservation &&
1325                  al.getAlignmentAnnotation()[h].label.equals("Conservation")))
1326             {
1327               al.deleteAnnotation(al.getAlignmentAnnotation()[h]);
1328               hSize--;
1329               h--;
1330             }
1331           }
1332           af.alignPanel.adjustAnnotationHeight();
1333         }
1334
1335         af.viewport.viewName = view.getViewName();
1336         af.setBounds(view.getXpos(), view.getYpos(), view.getWidth(),
1337             view.getHeight());
1338         af.viewport.setStartRes(view.getStartRes());
1339         af.viewport.setStartSeq(view.getStartSeq());
1340         af.viewport.setShowAnnotation(view.getShowAnnotation());
1341         af.viewport.showConservation = !hideConservation;
1342         af.viewport.showQuality = !hideQuality;
1343         af.viewport.showIdentity = !hideConsensus;
1344
1345         af.viewport.setAbovePIDThreshold(view.getPidSelected());
1346         af.abovePIDThreshold.setSelected(view.getPidSelected());
1347         af.viewport.setColourText(view.getShowColourText());
1348         af.colourTextMenuItem.setSelected(view.getShowColourText());
1349         af.viewport.setConservationSelected(view.getConservationSelected());
1350         af.conservationMenuItem.setSelected(view.getConservationSelected());
1351
1352         af.viewport.setShowJVSuffix(view.getShowFullId());
1353         af.seqLimits.setSelected(view.getShowFullId());
1354
1355         af.viewport.setFont(new java.awt.Font(view.getFontName(),
1356                 view.getFontStyle(), view.getFontSize()));
1357         af.alignPanel.fontChanged();
1358
1359         af.viewport.setRenderGaps(view.getRenderGaps());
1360         af.renderGapsMenuItem.setSelected(view.getRenderGaps());
1361
1362         af.viewport.setWrapAlignment(view.getWrapAlignment());
1363         af.wrapMenuItem.setSelected(view.getWrapAlignment());
1364
1365         af.alignPanel.setWrapAlignment(view.getWrapAlignment());
1366
1367         af.annotationPanelMenuItem.setState(view.getShowAnnotation());
1368         af.viewport.setShowAnnotation(view.getShowAnnotation());
1369         af.alignPanel.setAnnotationVisible(view.getShowAnnotation());
1370
1371         af.viewport.setShowBoxes(view.getShowBoxes());
1372         af.viewBoxesMenuItem.setSelected(view.getShowBoxes());
1373         af.viewport.setShowText(view.getShowText());
1374         af.viewTextMenuItem.setSelected(view.getShowText());
1375
1376         ColourSchemeI cs = null;
1377
1378         if (view.getBgColour() != null)
1379         {
1380             if (view.getBgColour().startsWith("ucs"))
1381             {
1382                 cs = GetUserColourScheme(jms, view.getBgColour());
1383             }
1384             else if(view.getBgColour().startsWith("Annotation"))
1385             {
1386               //int find annotation
1387               for (int i = 0; i < af.viewport.alignment.getAlignmentAnnotation().length; i++)
1388               {
1389                 if (af.viewport.alignment.getAlignmentAnnotation()[i].label.
1390                     equals(view.getAnnotationColours().getAnnotation()))
1391                 {
1392                   if (af.viewport.alignment.getAlignmentAnnotation()[i].
1393                       getThreshold() == null)
1394                   {
1395                     af.viewport.alignment.getAlignmentAnnotation()[i].
1396                         setThreshold(
1397                             new jalview.datamodel.GraphLine(
1398                                 view.getAnnotationColours().getThreshold(),
1399                                 "Threshold", java.awt.Color.black)
1400
1401                         );
1402                   }
1403
1404
1405                   if (view.getAnnotationColours().getColourScheme().equals(
1406                       "None"))
1407                   {
1408                     cs = new AnnotationColourGradient(
1409                         af.viewport.alignment.getAlignmentAnnotation()[i],
1410                         new java.awt.Color(view.getAnnotationColours().
1411                                            getMinColour()),
1412                         new java.awt.Color(view.getAnnotationColours().
1413                                            getMaxColour()),
1414                         view.getAnnotationColours().getAboveThreshold());
1415                   }
1416                   else if (view.getAnnotationColours().getColourScheme().
1417                            startsWith("ucs"))
1418                   {
1419                      cs = new AnnotationColourGradient(
1420                          af.viewport.alignment.getAlignmentAnnotation()[i],
1421                          GetUserColourScheme(jms, view.getAnnotationColours().
1422                                                         getColourScheme()),
1423                          view.getAnnotationColours().getAboveThreshold()
1424                          );
1425                    }
1426                    else
1427                    {
1428                      cs = new AnnotationColourGradient(
1429                          af.viewport.alignment.getAlignmentAnnotation()[i],
1430                          ColourSchemeProperty.getColour(al,
1431                          view.getAnnotationColours().getColourScheme()),
1432                          view.getAnnotationColours().getAboveThreshold()
1433                          );
1434                    }
1435
1436                   // Also use these settings for all the groups
1437                   if (al.getGroups() != null)
1438                     for (int g = 0; g < al.getGroups().size(); g++)
1439                     {
1440                       jalview.datamodel.SequenceGroup sg
1441                           = (jalview.datamodel.SequenceGroup)al.getGroups().elementAt(g);
1442
1443                       if(sg.cs == null)
1444                         continue;
1445
1446
1447                   /*    if (view.getAnnotationColours().getColourScheme().equals("None"))
1448                       {
1449                         sg.cs = new AnnotationColourGradient(
1450                             af.viewport.alignment.getAlignmentAnnotation()[i],
1451                             new java.awt.Color(view.getAnnotationColours().
1452                                                getMinColour()),
1453                             new java.awt.Color(view.getAnnotationColours().
1454                                                getMaxColour()),
1455                             view.getAnnotationColours().getAboveThreshold());
1456                       }
1457                       else*/
1458                       {
1459                         sg.cs = new AnnotationColourGradient(
1460                             af.viewport.alignment.getAlignmentAnnotation()[i],
1461                             sg.cs,
1462                             view.getAnnotationColours().getAboveThreshold()
1463                             );
1464                       }
1465
1466                     }
1467
1468
1469                   break;
1470                 }
1471
1472               }
1473             }
1474             else
1475             {
1476                 cs = ColourSchemeProperty.getColour(al, view.getBgColour());
1477             }
1478
1479             if(cs!=null)
1480             {
1481               cs.setThreshold(view.getPidThreshold(), true);
1482               cs.setConsensus(af.viewport.hconsensus);
1483             }
1484         }
1485
1486         af.setColourSelected(view.getBgColour());
1487         af.viewport.setGlobalColourScheme(cs);
1488         af.viewport.setColourAppliesToAllGroups(false);
1489         af.changeColour(cs);
1490         if (view.getConservationSelected() && cs!=null)
1491         {
1492           cs.setConservationInc(view.getConsThreshold());
1493         }
1494
1495         af.viewport.setColourAppliesToAllGroups(true);
1496
1497         if (view.getShowSequenceFeatures())
1498         {
1499            af.viewport.showSequenceFeatures = true;
1500            af.showSeqFeatures.setSelected(true);
1501         }
1502
1503         if(jms.getFeatureSettings()!=null)
1504         {
1505           af.viewport.featuresDisplayed = new Hashtable();
1506           String [] renderOrder = new String[jms.getFeatureSettings().getSettingCount()];
1507          for(int fs=0; fs<jms.getFeatureSettings().getSettingCount(); fs++)
1508          {
1509            Setting setting = jms.getFeatureSettings().getSetting(fs);
1510
1511            af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setColour(setting.getType(),
1512                new java.awt.Color(setting.getColour()));
1513
1514            renderOrder[fs] = setting.getType();
1515
1516            if(setting.getDisplay())
1517              af.viewport.featuresDisplayed.put(
1518                  setting.getType(), new Integer(setting.getColour()));
1519          }
1520          af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().renderOrder = renderOrder;
1521         }
1522
1523         if (view.getHiddenColumnsCount() > 0)
1524         {
1525           for (int c = 0; c < view.getHiddenColumnsCount(); c++)
1526           {
1527               af.viewport.hideColumns(
1528                 view.getHiddenColumns(c).getStart(),
1529                 view.getHiddenColumns(c).getEnd() //+1
1530                 );
1531           }
1532         }
1533
1534
1535         Desktop.addInternalFrame(af, view.getTitle(),
1536              view.getWidth(), view.getHeight());
1537
1538         //LOAD TREES
1539         ///////////////////////////////////////
1540         if (jms.getTreeCount() > 0)
1541         {
1542             try
1543             {
1544                 for (int t = 0; t < jms.getTreeCount(); t++)
1545                 {
1546
1547                     Tree tree = jms.getTree(t);
1548
1549                     TreePanel tp = af.ShowNewickTree(new jalview.io.NewickFile(
1550                                 tree.getNewick()), tree.getTitle(),
1551                                 tree.getWidth(), tree.getHeight(),
1552                                 tree.getXpos(), tree.getYpos());
1553
1554                     tp.fitToWindow.setState(tree.getFitToWindow());
1555                     tp.fitToWindow_actionPerformed(null);
1556
1557                     if(tree.getFontName()!=null)
1558                       tp.setTreeFont(new java.awt.Font(tree.getFontName(),
1559                                           tree.getFontStyle(),
1560                                           tree.getFontSize()));
1561                     else
1562                       tp.setTreeFont(new java.awt.Font(view.getFontName(),
1563                                             view.getFontStyle(),
1564                                             tree.getFontSize()));
1565
1566                     tp.showPlaceholders(tree.getMarkUnlinked());
1567                     tp.showBootstrap(tree.getShowBootstrap());
1568                     tp.showDistances(tree.getShowDistances());
1569
1570                     tp.treeCanvas.threshold = tree.getThreshold();
1571
1572                     if (tree.getCurrentTree())
1573                       af.viewport.setCurrentTree(tp.getTree());
1574                 }
1575
1576             }
1577             catch (Exception ex)
1578             {
1579                 ex.printStackTrace();
1580             }
1581
1582         }
1583
1584         return af;
1585     }
1586 }
1587