43fd1fa564d95fc3eafc069124f90c4c4b57a420
[jalview.git] / src / jalview / ext / forester / io / TreeParser.java
1 package jalview.ext.forester.io;
2
3 import jalview.ext.archaeopteryx.AptxInit;
4 import jalview.gui.AlignViewport;
5 //import jalview.ext.treeviewer.ExternalTreeParserI;
6 import jalview.gui.Desktop;
7 import jalview.gui.JvOptionPane;
8 import jalview.io.DataSourceType;
9 import jalview.io.NewickFile;
10 import jalview.util.MessageManager;
11
12 import java.io.File;
13 import java.io.IOException;
14
15 import org.forester.util.ForesterUtil;
16
17 public class TreeParser // implements ExternalTreeParserI<MainFrame>
18 {
19   private final String filePath;
20
21   private final File file;
22
23   public TreeParser(final String treeFilePath)
24   {
25     final String possibleError = ForesterUtil.isReadableFile(treeFilePath);
26     if (possibleError != null)
27     {
28       JvOptionPane.showMessageDialog(Desktop.desktop, possibleError,
29               MessageManager.getString("label.problem_reading_tree_file"),
30               JvOptionPane.WARNING_MESSAGE);
31
32     }
33     filePath = treeFilePath;
34     file = new File(filePath);
35
36   }
37
38   public TreeParser(final File treeFile) throws IOException
39   {
40     final String possibleError = ForesterUtil.isReadableFile(treeFile);
41     if (possibleError != null)
42     {
43       JvOptionPane.showMessageDialog(Desktop.desktop, possibleError,
44               MessageManager.getString("label.problem_reading_tree_file"),
45               JvOptionPane.WARNING_MESSAGE);
46
47     }
48     file = treeFile;
49     filePath = file.getCanonicalPath();
50   }
51
52   public void loadTree(AlignViewport viewport)
53   {
54
55     NewickFile fin = null; // old tree
56       try
57       {
58         AptxInit.createInstanceFromFile(filePath, viewport);
59
60         fin = new NewickFile(filePath, DataSourceType.FILE);
61         viewport.setCurrentTree(viewport.getAlignPanel().alignFrame
62                 .showNewickTree(fin, filePath).getTree());
63
64       } catch (Exception ex)
65       {
66         JvOptionPane.showMessageDialog(Desktop.desktop, ex.getMessage(),
67                 MessageManager.getString("label.problem_reading_tree_file"),
68                 JvOptionPane.WARNING_MESSAGE);
69         ex.printStackTrace();
70       }
71       if (fin != null && fin.hasWarningMessage())
72       {
73         JvOptionPane.showMessageDialog(Desktop.desktop,
74                 fin.getWarningMessage(),
75                 MessageManager
76                         .getString("label.possible_problem_with_tree_file"),
77                 JvOptionPane.WARNING_MESSAGE);
78       }
79     }
80   }
81
82
83 //
84 // @Override
85 // public MainFrame loadTreeFile(AlignmentViewport viewport)
86 // {
87 // String[] AptxArgs = new String[] { "-c",
88 // "_aptx_jalview_configuration_file", filePath };
89 // MainFrame aptx = Archaeopteryx.main(AptxArgs);
90 //
91 // LoadedTreeAssociation bindAptxNodes = new LoadedTreeAssociation(
92 // viewport.getAlignment().getSequencesArray(),
93 // aptx.getMainPanel().getCurrentTreePanel().getPhylogeny());
94 //
95 // bindAptxNodes.associateLeavesToSequences();
96 //
97 // new JalviewBinding(aptx, viewport, bindAptxNodes.getAlignmentWithNodes(),
98 // bindAptxNodes.getNodesWithAlignment());
99 //
100 // AptxInit.bindFrameToJalview(aptx);
101 //
102 // return aptx;
103 //
104 //
105 // }
106 // //
107 // void readPhylogeniesFromURL() {
108 // URL url = null;
109 // Phylogeny[] phys = null;
110 // final String message = "Please enter a complete URL, for example
111 // \"http://purl.org/phylo/treebase/phylows/study/TB2:S15480?format=nexus\"";
112 // final String url_string = JOptionPane
113 // .showInputDialog( this,
114 // message,
115 // "Use URL/webservice to obtain a phylogeny",
116 // JOptionPane.QUESTION_MESSAGE );
117 // boolean nhx_or_nexus = false;
118 // if ( ( url_string != null ) && ( url_string.length() > 0 ) ) {
119 // try {
120 // url = new URL( url_string );
121 // PhylogenyParser parser = null;
122 // if ( url.getHost().toLowerCase().indexOf( "tolweb" ) >= 0 ) {
123 // parser = new TolParser();
124 // }
125 // else {
126 // parser = ParserUtils
127 // .createParserDependingOnUrlContents( url,
128 // getConfiguration().isValidatePhyloXmlAgainstSchema() );
129 // }
130 // if ( parser instanceof NexusPhylogeniesParser ) {
131 // nhx_or_nexus = true;
132 // }
133 // else if ( parser instanceof NHXParser ) {
134 // nhx_or_nexus = true;
135 // }
136 // if ( _mainpanel.getCurrentTreePanel() != null ) {
137 // _mainpanel.getCurrentTreePanel().setWaitCursor();
138 // }
139 // else {
140 // _mainpanel.setWaitCursor();
141 // }
142 // final PhylogenyFactory factory =
143 // ParserBasedPhylogenyFactory.getInstance();
144 // phys = factory.create( url.openStream(), parser );
145 // }
146 // catch ( final MalformedURLException e ) {
147 // JOptionPane.showMessageDialog( this,
148 // "Malformed URL: " + url + "\n" + e.getLocalizedMessage(),
149 // "Malformed URL",
150 // JOptionPane.ERROR_MESSAGE );
151 // }
152 // catch ( final IOException e ) {
153 // JOptionPane.showMessageDialog( this,
154 // "Could not read from " + url + "\n"
155 // + ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ),
156 // "Failed to read URL",
157 // JOptionPane.ERROR_MESSAGE );
158 // }
159 // catch ( final Exception e ) {
160 // JOptionPane.showMessageDialog( this,
161 // ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ),
162 // "Unexpected Exception",
163 // JOptionPane.ERROR_MESSAGE );
164 // }
165 // finally {
166 // if ( _mainpanel.getCurrentTreePanel() != null ) {
167 // _mainpanel.getCurrentTreePanel().setArrowCursor();
168 // }
169 // else {
170 // _mainpanel.setArrowCursor();
171 // }
172 // }
173 // if ( ( phys != null ) && ( phys.length > 0 ) ) {
174 // if ( nhx_or_nexus &&
175 // getOptions().isInternalNumberAreConfidenceForNhParsing() ) {
176 // for( final Phylogeny phy : phys ) {
177 // PhylogenyMethods.transferInternalNodeNamesToConfidence( phy, "" );
178 // }
179 // }
180 // AptxUtil.addPhylogeniesToTabs( phys,
181 // new File( url.getFile() ).getName(),
182 // new File( url.getFile() ).toString(),
183 // getConfiguration(),
184 // getMainPanel() );
185 // _mainpanel.getControlPanel().showWhole();
186 // }
187 // }
188 // activateSaveAllIfNeeded();
189 // System.gc();
190 // }
191 //
192 // }
193 //
194 //
195 //
196 //
197 //
198 //