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