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