JAL-1759 corrected syntax for 'atom picked' callback (toggles label
[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.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.datamodel.PDBEntry;
38 import jalview.schemes.ColourSchemeI;
39 import jalview.schemes.ColourSchemeProperty;
40 import jalview.schemes.ResidueProperties;
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.util.Hashtable;
48 import java.util.Vector;
49 import java.util.jar.JarEntry;
50 import java.util.jar.JarInputStream;
51
52 import javax.swing.JOptionPane;
53
54 import org.exolab.castor.xml.IDResolver;
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         class NoDescIDResolver implements IDResolver
131         {
132           public Object resolve(String idref)
133           {
134             System.out.println(idref + " used");
135             return null;
136           }
137         }
138
139         if (jarentry != null)
140         {
141           InputStreamReader in = new InputStreamReader(jin, "UTF-8");
142           JalviewModel object = new JalviewModel();
143
144           object = object.unmarshal(in);
145
146           af = LoadFromObject(object, file);
147           entryCount++;
148         }
149       } while (jarentry != null);
150     } catch (final java.net.UnknownHostException ex)
151     {
152       ex.printStackTrace();
153       if (raiseGUI)
154       {
155         javax.swing.SwingUtilities.invokeLater(new Runnable()
156         {
157           public void run()
158           {
159
160             System.err.println("Couldn't locate Jalview XML file : " + ex
161                     + "\n");
162             JOptionPane.showInternalMessageDialog(Desktop.desktop,
163                     MessageManager.formatMessage("label.couldnt_locate",
164                             new String[]
165                             { file }), MessageManager
166                             .getString("label.url_not_found"),
167                     JOptionPane.WARNING_MESSAGE);
168           }
169         });
170       }
171       ;
172     } catch (Exception ex)
173     {
174       System.err.println("Exception whilst loading jalview XML file : ");
175       ex.printStackTrace();
176       if (raiseGUI)
177       {
178         javax.swing.SwingUtilities.invokeLater(new Runnable()
179         {
180           public void run()
181           {
182
183             JOptionPane.showInternalMessageDialog(Desktop.desktop,
184                     MessageManager.formatMessage("label.error_loading_file_params", new String[]{file}), MessageManager.getString("label.error_loading_jalview_file"),
185                     JOptionPane.WARNING_MESSAGE);
186           }
187         });
188       }
189     }
190
191     return af;
192   }
193
194   AlignFrame LoadFromObject(JalviewModel object, String file)
195   {
196     Vector seqids = new Vector();
197     SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0);
198     Sequence[] vamsasSeq = vamsasSet.getSequence();
199
200     JalviewModelSequence jms = object.getJalviewModelSequence();
201
202     // ////////////////////////////////
203     // LOAD SEQUENCES
204     jalview.datamodel.Sequence[] jseqs = new jalview.datamodel.Sequence[vamsasSeq.length];
205     JSeq[] JSEQ = object.getJalviewModelSequence().getJSeq();
206     for (int i = 0; i < vamsasSeq.length; i++)
207     {
208       jseqs[i] = new jalview.datamodel.Sequence(vamsasSeq[i].getName(),
209               vamsasSeq[i].getSequence());
210       jseqs[i].setStart(JSEQ[i].getStart());
211       jseqs[i].setEnd(JSEQ[i].getEnd());
212       seqids.add(jseqs[i]);
213     }
214
215     // /SequenceFeatures are added to the DatasetSequence,
216     // so we must create the dataset before loading features
217     // ///////////////////////////////
218     jalview.datamodel.Alignment al = new jalview.datamodel.Alignment(jseqs);
219     al.setDataset(null);
220     // ///////////////////////////////
221
222     for (int i = 0; i < vamsasSeq.length; i++)
223     {
224       if (JSEQ[i].getFeaturesCount() > 0)
225       {
226         Features[] features = JSEQ[i].getFeatures();
227         for (int f = 0; f < features.length; f++)
228         {
229           jalview.datamodel.SequenceFeature sf = new jalview.datamodel.SequenceFeature(
230                   features[f].getType(), features[f].getDescription(),
231                   features[f].getStatus(), features[f].getBegin(),
232                   features[f].getEnd(), null);
233
234           al.getSequenceAt(i).getDatasetSequence().addSequenceFeature(sf);
235         }
236       }
237       if (JSEQ[i].getPdbidsCount() > 0)
238       {
239         Pdbids[] ids = JSEQ[i].getPdbids();
240         for (int p = 0; p < ids.length; p++)
241         {
242           jalview.datamodel.PDBEntry entry = new jalview.datamodel.PDBEntry();
243           entry.setId(ids[p].getId());
244           if (ids[p].getType() != null)
245           {
246             if (ids[p].getType().equalsIgnoreCase("PDB"))
247             {
248               entry.setType(PDBEntry.Type.PDB);
249             }
250             else
251             {
252               entry.setType(PDBEntry.Type.FILE);
253             }
254           }
255           al.getSequenceAt(i).getDatasetSequence().addPDBId(entry);
256           StructureSelectionManager.getStructureSelectionManager(
257                   Desktop.instance).registerPDBEntry(entry);
258         }
259
260       }
261     }
262
263     // ///////////////////////////////
264     // ////////////////////////////////
265     // LOAD ANNOTATIONS
266     if (vamsasSet.getAnnotation() != null)
267     {
268       Annotation[] an = vamsasSet.getAnnotation();
269
270       for (int i = 0; i < an.length; i++)
271       {
272         AnnotationElement[] ae = an[i].getAnnotationElement();
273         jalview.datamodel.Annotation[] anot = new jalview.datamodel.Annotation[al
274                 .getWidth()];
275
276         for (int aa = 0; aa < ae.length; aa++)
277         {
278           anot[ae[aa].getPosition()] = new jalview.datamodel.Annotation(
279                   ae[aa].getDisplayCharacter(), ae[aa].getDescription(),
280                   ae[aa].getSecondaryStructure().charAt(0),
281                   ae[aa].getValue());
282         }
283
284         jalview.datamodel.AlignmentAnnotation jaa = null;
285
286         if (an[i].getGraph())
287         {
288           jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
289                   an[i].getDescription(), anot, 0, 0,
290                   jalview.datamodel.AlignmentAnnotation.BAR_GRAPH);
291         }
292         else
293         {
294           jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
295                   an[i].getDescription(), anot);
296         }
297
298         al.addAnnotation(jaa);
299       }
300     }
301
302     // ///////////////////////////////
303     // LOAD VIEWPORT
304     Viewport[] views = jms.getViewport();
305     Viewport view = views[0]; // DEAL WITH MULTIPLE VIEWPORTS LATER
306
307     AlignFrame af = new AlignFrame(al, view.getWidth(), view.getHeight());
308
309     af.setFileName(file, "Jalview");
310
311     for (int i = 0; i < JSEQ.length; i++)
312     {
313       af.viewport.setSequenceColour(af.viewport.getAlignment()
314               .getSequenceAt(i), new java.awt.Color(JSEQ[i].getColour()));
315     }
316
317     // af.changeColour() );
318     // ///////////////////////
319     // LOAD GROUPS
320     if (jms.getJGroupCount() > 0)
321     {
322       JGroup[] groups = jms.getJGroup();
323
324       for (int i = 0; i < groups.length; i++)
325       {
326         ColourSchemeI cs = null;
327
328         if (groups[i].getColour() != null)
329         {
330           if (groups[i].getColour().startsWith("ucs"))
331           {
332             cs = GetUserColourScheme(jms, groups[i].getColour());
333           }
334           else
335           {
336             cs = ColourSchemeProperty.getColour(al, groups[i].getColour());
337           }
338
339           if (cs != null)
340           {
341             cs.setThreshold(groups[i].getPidThreshold(), true);
342           }
343
344         }
345
346         Vector seqs = new Vector();
347         int[] ids = groups[i].getSeq();
348
349         for (int s = 0; s < ids.length; s++)
350         {
351           seqs.addElement(seqids
352                   .elementAt(ids[s]));
353         }
354
355         jalview.datamodel.SequenceGroup sg = new jalview.datamodel.SequenceGroup(
356                 seqs, groups[i].getName(), cs, groups[i].getDisplayBoxes(),
357                 groups[i].getDisplayText(), groups[i].getColourText(),
358                 groups[i].getStart(), groups[i].getEnd());
359
360         sg.setOutlineColour(new java.awt.Color(groups[i].getOutlineColour()));
361
362         if (groups[i].getConsThreshold() != 0)
363         {
364           jalview.analysis.Conservation c = new jalview.analysis.Conservation(
365                   "All", ResidueProperties.propHash, 3,
366                   sg.getSequences(null), 0, sg.getWidth() - 1);
367           c.calculate();
368           c.verdict(false, 25);
369           sg.cs.setConservation(c);
370         }
371
372         al.addGroup(sg);
373       }
374     }
375
376     af.setBounds(view.getXpos(), view.getYpos(), view.getWidth(),
377             view.getHeight());
378     af.viewport.setStartRes(view.getStartRes());
379     af.viewport.setStartSeq(view.getStartSeq());
380     af.viewport.setShowAnnotation(view.getShowAnnotation());
381     af.viewport.setAbovePIDThreshold(view.getPidSelected());
382     af.viewport.setColourText(view.getShowColourText());
383     af.viewport.setConservationSelected(view.getConservationSelected());
384     af.viewport.setShowJVSuffix(view.getShowFullId());
385     af.viewport.setFont(
386             new java.awt.Font(view.getFontName(), view.getFontStyle(), view
387                     .getFontSize()), true);
388
389     af.viewport.setRenderGaps(view.getRenderGaps());
390     af.viewport.setWrapAlignment(view.getWrapAlignment());
391
392     af.viewport.setShowAnnotation(view.isShowAnnotation());
393     af.viewport.setShowBoxes(view.getShowBoxes());
394     af.viewport.setShowText(view.getShowText());
395
396     ColourSchemeI cs = null;
397
398     if (view.getBgColour() != null)
399     {
400       if (view.getBgColour().startsWith("ucs"))
401       {
402         cs = GetUserColourScheme(jms, view.getBgColour());
403       }
404       else
405       {
406         cs = ColourSchemeProperty.getColour(al, view.getBgColour());
407       }
408
409       if (cs != null)
410       {
411         cs.setThreshold(view.getPidThreshold(), true);
412         cs.setConsensus(af.viewport.getSequenceConsensusHash());
413       }
414     }
415
416     af.viewport.setGlobalColourScheme(cs);
417     af.viewport.setColourAppliesToAllGroups(false);
418     af.alignPanel.updateLayout();
419     af.changeColour(cs);
420     if (view.getConservationSelected() && cs != null)
421     {
422       cs.setConservationInc(view.getConsThreshold());
423     }
424
425     af.viewport.setColourAppliesToAllGroups(true);
426     af.viewport.setShowSequenceFeatures(view.getShowSequenceFeatures());
427
428     if (jms.getFeatureSettings() != null)
429     {
430       Hashtable featuresDisplayed = new Hashtable();
431       Hashtable featureColours = new Hashtable();
432       String[] renderOrder = new String[jms.getFeatureSettings()
433               .getSettingCount()];
434       for (int fs = 0; fs < jms.getFeatureSettings().getSettingCount(); fs++)
435       {
436         Setting setting = jms.getFeatureSettings().getSetting(fs);
437
438         featureColours.put(
439                 setting.getType(), new java.awt.Color(setting.getColour()));
440
441         renderOrder[fs] = setting.getType();
442
443         if (setting.getDisplay())
444         {
445           featuresDisplayed.put(setting.getType(), new Integer(
446                   setting.getColour()));
447         }
448       }
449       FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder, new Hashtable(), featureColours, 1.0f, null);
450       af.alignPanel.getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(frs);
451     }
452
453     af.setMenusFromViewport(af.viewport);
454
455     Desktop.addInternalFrame(af, view.getTitle(), view.getWidth(),
456             view.getHeight());
457
458     // LOAD TREES
459     // /////////////////////////////////////
460     if (jms.getTreeCount() > 0)
461     {
462       try
463       {
464         for (int t = 0; t < jms.getTreeCount(); t++)
465         {
466
467           Tree tree = jms.getTree(t);
468
469           TreePanel tp = af.ShowNewickTree(
470                   new jalview.io.NewickFile(tree.getNewick()),
471                   tree.getTitle(), tree.getWidth(), tree.getHeight(),
472                   tree.getXpos(), tree.getYpos());
473
474           tp.fitToWindow.setState(tree.getFitToWindow());
475           tp.fitToWindow_actionPerformed(null);
476
477           if (tree.getFontName() != null)
478           {
479             tp.setTreeFont(new java.awt.Font(tree.getFontName(), tree
480                     .getFontStyle(), tree.getFontSize()));
481           }
482           else
483           {
484             tp.setTreeFont(new java.awt.Font(view.getFontName(), view
485                     .getFontStyle(), tree.getFontSize()));
486           }
487
488           tp.showPlaceholders(tree.getMarkUnlinked());
489           tp.showBootstrap(tree.getShowBootstrap());
490           tp.showDistances(tree.getShowDistances());
491
492           tp.treeCanvas.threshold = tree.getThreshold();
493
494           if (tree.getCurrentTree())
495           {
496             af.viewport.setCurrentTree(tp.getTree());
497           }
498         }
499
500       } catch (Exception ex)
501       {
502         ex.printStackTrace();
503       }
504
505     }
506
507     return af;
508   }
509 }