JAL-2089 fix broken help link
[jalview.git] / src / jalview / gui / Jalview2XML_V1.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.analysis.Conservation;
24 import jalview.binding.Annotation;
25 import jalview.binding.AnnotationElement;
26 import jalview.binding.Features;
27 import jalview.binding.JGroup;
28 import jalview.binding.JSeq;
29 import jalview.binding.JalviewModel;
30 import jalview.binding.JalviewModelSequence;
31 import jalview.binding.Pdbids;
32 import jalview.binding.Sequence;
33 import jalview.binding.SequenceSet;
34 import jalview.binding.Setting;
35 import jalview.binding.Tree;
36 import jalview.binding.UserColours;
37 import jalview.binding.Viewport;
38 import jalview.datamodel.PDBEntry;
39 import jalview.schemes.ColourSchemeI;
40 import jalview.schemes.ColourSchemeProperty;
41 import jalview.structure.StructureSelectionManager;
42 import jalview.util.MessageManager;
43 import jalview.util.jarInputStreamProvider;
44 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
45
46 import java.io.InputStreamReader;
47 import java.io.Reader;
48 import java.net.UnknownHostException;
49 import java.util.Hashtable;
50 import java.util.Vector;
51 import java.util.jar.JarEntry;
52 import java.util.jar.JarInputStream;
53
54 import javax.swing.JOptionPane;
55
56 /**
57  * DOCUMENT ME!
58  * 
59  * @author $author$
60  * @version $Revision$
61  */
62 public class Jalview2XML_V1
63 {
64   boolean raiseGUI = true;
65
66   public Jalview2XML_V1()
67   {
68
69   };
70
71   public Jalview2XML_V1(boolean raiseGUI)
72   {
73     this.raiseGUI = raiseGUI;
74   };
75
76   jalview.schemes.UserColourScheme GetUserColourScheme(
77           JalviewModelSequence jms, String id)
78   {
79     UserColours[] uc = jms.getUserColours();
80     UserColours colours = null;
81
82     for (int i = 0; i < uc.length; i++)
83     {
84       if (uc[i].getId().equals(id))
85       {
86         colours = uc[i];
87
88         break;
89       }
90     }
91
92     int csize = colours.getUserColourScheme().getColourCount();
93     java.awt.Color[] newColours = new java.awt.Color[csize];
94
95     for (int i = 0; i < csize; i++)
96     {
97       newColours[i] = new java.awt.Color(Integer.parseInt(colours
98               .getUserColourScheme().getColour(i).getRGB(), 16));
99     }
100
101     return new jalview.schemes.UserColourScheme(newColours);
102   }
103
104   /**
105    * DOCUMENT ME!
106    * 
107    * @param file
108    *          DOCUMENT ME!
109    */
110   public AlignFrame LoadJalviewAlign(final jarInputStreamProvider jprovider)
111   {
112     final String file = jprovider.getFilename();
113     jalview.gui.AlignFrame af = null;
114
115     try
116     {
117       JarInputStream jin = null;
118       JarEntry jarentry = null;
119       int entryCount = 1;
120
121       do
122       {
123         jin = jprovider.getJarInputStream();
124
125         for (int i = 0; i < entryCount; i++)
126         {
127           jarentry = jin.getNextJarEntry();
128         }
129
130         if (jarentry != null)
131         {
132           entryCount++;
133           if (jarentry.getName().endsWith(".xml"))
134           {
135             Reader in = new InputStreamReader(jin, "UTF-8");
136             JalviewModel object = new JalviewModel();
137
138             object = object.unmarshal(in);
139
140             af = LoadFromObject(object, file);
141           }
142         }
143         jin.close();
144       } while (jarentry != null);
145     } catch (final UnknownHostException ex)
146     {
147       ex.printStackTrace();
148       if (raiseGUI)
149       {
150         javax.swing.SwingUtilities.invokeLater(new Runnable()
151         {
152           @Override
153           public void run()
154           {
155
156             System.err.println("Couldn't locate Jalview XML file : " + ex
157                     + "\n");
158             JOptionPane.showInternalMessageDialog(Desktop.desktop,
159                     MessageManager.formatMessage("label.couldnt_locate",
160                             new String[] { file }), MessageManager
161                             .getString("label.url_not_found"),
162                     JOptionPane.WARNING_MESSAGE);
163           }
164         });
165       }
166       ;
167     } catch (Exception ex)
168     {
169       System.err.println("Exception whilst loading jalview XML file : ");
170       ex.printStackTrace();
171       if (raiseGUI)
172       {
173         javax.swing.SwingUtilities.invokeLater(new Runnable()
174         {
175           @Override
176           public void run()
177           {
178
179             JOptionPane.showInternalMessageDialog(Desktop.desktop,
180                     MessageManager.formatMessage(
181                             "label.error_loading_file_params", new String[]
182                             { file }), MessageManager
183                             .getString("label.error_loading_jalview_file"),
184                     JOptionPane.WARNING_MESSAGE);
185           }
186         });
187       }
188     }
189
190     return af;
191   }
192
193   AlignFrame LoadFromObject(JalviewModel object, String file)
194   {
195     Vector seqids = new Vector();
196     SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0);
197     Sequence[] vamsasSeq = vamsasSet.getSequence();
198
199     JalviewModelSequence jms = object.getJalviewModelSequence();
200
201     // ////////////////////////////////
202     // LOAD SEQUENCES
203     jalview.datamodel.Sequence[] jseqs = new jalview.datamodel.Sequence[vamsasSeq.length];
204     JSeq[] JSEQ = object.getJalviewModelSequence().getJSeq();
205     for (int i = 0; i < vamsasSeq.length; i++)
206     {
207       jseqs[i] = new jalview.datamodel.Sequence(vamsasSeq[i].getName(),
208               vamsasSeq[i].getSequence());
209       jseqs[i].setStart(JSEQ[i].getStart());
210       jseqs[i].setEnd(JSEQ[i].getEnd());
211       seqids.add(jseqs[i]);
212     }
213
214     // /SequenceFeatures are added to the DatasetSequence,
215     // so we must create the dataset before loading features
216     // ///////////////////////////////
217     jalview.datamodel.Alignment al = new jalview.datamodel.Alignment(jseqs);
218     al.setDataset(null);
219     // ///////////////////////////////
220
221     for (int i = 0; i < vamsasSeq.length; i++)
222     {
223       if (JSEQ[i].getFeaturesCount() > 0)
224       {
225         Features[] features = JSEQ[i].getFeatures();
226         for (int f = 0; f < features.length; f++)
227         {
228           jalview.datamodel.SequenceFeature sf = new jalview.datamodel.SequenceFeature(
229                   features[f].getType(), features[f].getDescription(),
230                   features[f].getStatus(), features[f].getBegin(),
231                   features[f].getEnd(), null);
232
233           al.getSequenceAt(i).getDatasetSequence().addSequenceFeature(sf);
234         }
235       }
236       if (JSEQ[i].getPdbidsCount() > 0)
237       {
238         Pdbids[] ids = JSEQ[i].getPdbids();
239         for (int p = 0; p < ids.length; p++)
240         {
241           jalview.datamodel.PDBEntry entry = new jalview.datamodel.PDBEntry();
242           entry.setId(ids[p].getId());
243           if (ids[p].getType() != null)
244           {
245             if (ids[p].getType().equalsIgnoreCase("PDB"))
246             {
247               entry.setType(PDBEntry.Type.PDB);
248             }
249             else
250             {
251               entry.setType(PDBEntry.Type.FILE);
252             }
253           }
254           al.getSequenceAt(i).getDatasetSequence().addPDBId(entry);
255           StructureSelectionManager.getStructureSelectionManager(
256                   Desktop.instance).registerPDBEntry(entry);
257         }
258
259       }
260     }
261
262     // ///////////////////////////////
263     // ////////////////////////////////
264     // LOAD ANNOTATIONS
265     if (vamsasSet.getAnnotation() != null)
266     {
267       Annotation[] an = vamsasSet.getAnnotation();
268
269       for (int i = 0; i < an.length; i++)
270       {
271         AnnotationElement[] ae = an[i].getAnnotationElement();
272         jalview.datamodel.Annotation[] anot = new jalview.datamodel.Annotation[al
273                 .getWidth()];
274
275         for (int aa = 0; aa < ae.length; aa++)
276         {
277           anot[ae[aa].getPosition()] = new jalview.datamodel.Annotation(
278                   ae[aa].getDisplayCharacter(), ae[aa].getDescription(),
279                   ae[aa].getSecondaryStructure().charAt(0),
280                   ae[aa].getValue());
281         }
282
283         jalview.datamodel.AlignmentAnnotation jaa = null;
284
285         if (an[i].getGraph())
286         {
287           jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
288                   an[i].getDescription(), anot, 0, 0,
289                   jalview.datamodel.AlignmentAnnotation.BAR_GRAPH);
290         }
291         else
292         {
293           jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
294                   an[i].getDescription(), anot);
295         }
296
297         al.addAnnotation(jaa);
298       }
299     }
300
301     // ///////////////////////////////
302     // LOAD VIEWPORT
303     Viewport[] views = jms.getViewport();
304     Viewport view = views[0]; // DEAL WITH MULTIPLE VIEWPORTS LATER
305
306     AlignFrame af = new AlignFrame(al, view.getWidth(), view.getHeight());
307
308     af.setFileName(file, "Jalview");
309
310     for (int i = 0; i < JSEQ.length; i++)
311     {
312       af.viewport.setSequenceColour(af.viewport.getAlignment()
313               .getSequenceAt(i), new java.awt.Color(JSEQ[i].getColour()));
314     }
315
316     // af.changeColour() );
317     // ///////////////////////
318     // LOAD GROUPS
319     if (jms.getJGroupCount() > 0)
320     {
321       JGroup[] groups = jms.getJGroup();
322
323       for (int i = 0; i < groups.length; i++)
324       {
325         ColourSchemeI cs = null;
326
327         if (groups[i].getColour() != null)
328         {
329           if (groups[i].getColour().startsWith("ucs"))
330           {
331             cs = GetUserColourScheme(jms, groups[i].getColour());
332           }
333           else
334           {
335             cs = ColourSchemeProperty.getColour(al, groups[i].getColour());
336           }
337
338           if (cs != null)
339           {
340             cs.setThreshold(groups[i].getPidThreshold(), true);
341           }
342
343         }
344
345         Vector seqs = new Vector();
346         int[] ids = groups[i].getSeq();
347
348         for (int s = 0; s < ids.length; s++)
349         {
350           seqs.addElement(seqids.elementAt(ids[s]));
351         }
352
353         jalview.datamodel.SequenceGroup sg = new jalview.datamodel.SequenceGroup(
354                 seqs, groups[i].getName(), cs, groups[i].getDisplayBoxes(),
355                 groups[i].getDisplayText(), groups[i].getColourText(),
356                 groups[i].getStart(), groups[i].getEnd());
357
358         sg.setOutlineColour(new java.awt.Color(groups[i].getOutlineColour()));
359
360         if (groups[i].getConsThreshold() != 0)
361         {
362           Conservation c = new Conservation("All", 3,
363                   sg.getSequences(null), 0, sg.getWidth() - 1);
364           c.calculate();
365           c.verdict(false, 25);
366           sg.cs.setConservation(c);
367         }
368
369         al.addGroup(sg);
370       }
371     }
372
373     af.setBounds(view.getXpos(), view.getYpos(), view.getWidth(),
374             view.getHeight());
375     af.viewport.setStartRes(view.getStartRes());
376     af.viewport.setStartSeq(view.getStartSeq());
377     af.viewport.setShowAnnotation(view.getShowAnnotation());
378     af.viewport.setAbovePIDThreshold(view.getPidSelected());
379     af.viewport.setColourText(view.getShowColourText());
380     af.viewport.setConservationSelected(view.getConservationSelected());
381     af.viewport.setShowJVSuffix(view.getShowFullId());
382     af.viewport.setFont(
383             new java.awt.Font(view.getFontName(), view.getFontStyle(), view
384                     .getFontSize()), true);
385
386     af.viewport.setRenderGaps(view.getRenderGaps());
387     af.viewport.setWrapAlignment(view.getWrapAlignment());
388
389     af.viewport.setShowAnnotation(view.isShowAnnotation());
390     af.viewport.setShowBoxes(view.getShowBoxes());
391     af.viewport.setShowText(view.getShowText());
392
393     ColourSchemeI cs = null;
394
395     if (view.getBgColour() != null)
396     {
397       if (view.getBgColour().startsWith("ucs"))
398       {
399         cs = GetUserColourScheme(jms, view.getBgColour());
400       }
401       else
402       {
403         cs = ColourSchemeProperty.getColour(al, view.getBgColour());
404       }
405
406       if (cs != null)
407       {
408         cs.setThreshold(view.getPidThreshold(), true);
409         cs.setConsensus(af.viewport.getSequenceConsensusHash());
410       }
411     }
412
413     af.viewport.setGlobalColourScheme(cs);
414     af.viewport.setColourAppliesToAllGroups(false);
415     af.alignPanel.updateLayout();
416     af.changeColour(cs);
417     if (view.getConservationSelected() && cs != null)
418     {
419       cs.setConservationInc(view.getConsThreshold());
420     }
421
422     af.viewport.setColourAppliesToAllGroups(true);
423     af.viewport.setShowSequenceFeatures(view.getShowSequenceFeatures());
424
425     if (jms.getFeatureSettings() != null)
426     {
427       Hashtable featuresDisplayed = new Hashtable();
428       Hashtable featureColours = new Hashtable();
429       String[] renderOrder = new String[jms.getFeatureSettings()
430               .getSettingCount()];
431       for (int fs = 0; fs < jms.getFeatureSettings().getSettingCount(); fs++)
432       {
433         Setting setting = jms.getFeatureSettings().getSetting(fs);
434
435         featureColours.put(setting.getType(),
436                 new java.awt.Color(setting.getColour()));
437
438         renderOrder[fs] = setting.getType();
439
440         if (setting.getDisplay())
441         {
442           featuresDisplayed.put(setting.getType(),
443                   new Integer(setting.getColour()));
444         }
445       }
446       FeatureRendererSettings frs = new FeatureRendererSettings(
447               renderOrder, new Hashtable(), featureColours, 1.0f, null);
448       af.alignPanel.getSeqPanel().seqCanvas.getFeatureRenderer()
449               .transferSettings(frs);
450     }
451
452     af.setMenusFromViewport(af.viewport);
453
454     Desktop.addInternalFrame(af, view.getTitle(), view.getWidth(),
455             view.getHeight());
456
457     // LOAD TREES
458     // /////////////////////////////////////
459     if (jms.getTreeCount() > 0)
460     {
461       try
462       {
463         for (int t = 0; t < jms.getTreeCount(); t++)
464         {
465
466           Tree tree = jms.getTree(t);
467
468           TreePanel tp = af.ShowNewickTree(
469                   new jalview.io.NewickFile(tree.getNewick()),
470                   tree.getTitle(), tree.getWidth(), tree.getHeight(),
471                   tree.getXpos(), tree.getYpos());
472
473           tp.fitToWindow.setState(tree.getFitToWindow());
474           tp.fitToWindow_actionPerformed(null);
475
476           if (tree.getFontName() != null)
477           {
478             tp.setTreeFont(new java.awt.Font(tree.getFontName(), tree
479                     .getFontStyle(), tree.getFontSize()));
480           }
481           else
482           {
483             tp.setTreeFont(new java.awt.Font(view.getFontName(), view
484                     .getFontStyle(), tree.getFontSize()));
485           }
486
487           tp.showPlaceholders(tree.getMarkUnlinked());
488           tp.showBootstrap(tree.getShowBootstrap());
489           tp.showDistances(tree.getShowDistances());
490
491           tp.treeCanvas.threshold = tree.getThreshold();
492
493           if (tree.getCurrentTree())
494           {
495             af.viewport.setCurrentTree(tp.getTree());
496           }
497         }
498
499       } catch (Exception ex)
500       {
501         ex.printStackTrace();
502       }
503
504     }
505
506     return af;
507   }
508 }