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