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