96b57708618c4078e02efd5df7740f39fd8bff95
[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;
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       } catch (Exception ex)
64       {
65         JvOptionPane.showMessageDialog(Desktop.desktop, ex.getMessage(),
66                 MessageManager.getString("label.problem_reading_tree_file"),
67                 JvOptionPane.WARNING_MESSAGE);
68         ex.printStackTrace();
69       }
70       if (fin != null && fin.hasWarningMessage())
71       {
72         JvOptionPane.showMessageDialog(Desktop.desktop,
73                 fin.getWarningMessage(),
74                 MessageManager
75                         .getString("label.possible_problem_with_tree_file"),
76                 JvOptionPane.WARNING_MESSAGE);
77       }
78     }
79   }
80
81
82 //
83 // @Override
84 // public MainFrame loadTreeFile(AlignmentViewport viewport)
85 // {
86 // String[] AptxArgs = new String[] { "-c",
87 // "_aptx_jalview_configuration_file", filePath };
88 // MainFrame aptx = Archaeopteryx.main(AptxArgs);
89 //
90 // LoadedTreeAssociation bindAptxNodes = new LoadedTreeAssociation(
91 // viewport.getAlignment().getSequencesArray(),
92 // aptx.getMainPanel().getCurrentTreePanel().getPhylogeny());
93 //
94 // bindAptxNodes.associateLeavesToSequences();
95 //
96 // new JalviewBinding(aptx, viewport, bindAptxNodes.getAlignmentWithNodes(),
97 // bindAptxNodes.getNodesWithAlignment());
98 //
99 // AptxInit.bindFrameToJalview(aptx);
100 //
101 // return aptx;
102 //
103 //
104 // }
105 // //
106 // void readPhylogeniesFromURL() {
107 // URL url = null;
108 // Phylogeny[] phys = null;
109 // final String message = "Please enter a complete URL, for example
110 // \"http://purl.org/phylo/treebase/phylows/study/TB2:S15480?format=nexus\"";
111 // final String url_string = JOptionPane
112 // .showInputDialog( this,
113 // message,
114 // "Use URL/webservice to obtain a phylogeny",
115 // JOptionPane.QUESTION_MESSAGE );
116 // boolean nhx_or_nexus = false;
117 // if ( ( url_string != null ) && ( url_string.length() > 0 ) ) {
118 // try {
119 // url = new URL( url_string );
120 // PhylogenyParser parser = null;
121 // if ( url.getHost().toLowerCase().indexOf( "tolweb" ) >= 0 ) {
122 // parser = new TolParser();
123 // }
124 // else {
125 // parser = ParserUtils
126 // .createParserDependingOnUrlContents( url,
127 // getConfiguration().isValidatePhyloXmlAgainstSchema() );
128 // }
129 // if ( parser instanceof NexusPhylogeniesParser ) {
130 // nhx_or_nexus = true;
131 // }
132 // else if ( parser instanceof NHXParser ) {
133 // nhx_or_nexus = true;
134 // }
135 // if ( _mainpanel.getCurrentTreePanel() != null ) {
136 // _mainpanel.getCurrentTreePanel().setWaitCursor();
137 // }
138 // else {
139 // _mainpanel.setWaitCursor();
140 // }
141 // final PhylogenyFactory factory =
142 // ParserBasedPhylogenyFactory.getInstance();
143 // phys = factory.create( url.openStream(), parser );
144 // }
145 // catch ( final MalformedURLException e ) {
146 // JOptionPane.showMessageDialog( this,
147 // "Malformed URL: " + url + "\n" + e.getLocalizedMessage(),
148 // "Malformed URL",
149 // JOptionPane.ERROR_MESSAGE );
150 // }
151 // catch ( final IOException e ) {
152 // JOptionPane.showMessageDialog( this,
153 // "Could not read from " + url + "\n"
154 // + ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ),
155 // "Failed to read URL",
156 // JOptionPane.ERROR_MESSAGE );
157 // }
158 // catch ( final Exception e ) {
159 // JOptionPane.showMessageDialog( this,
160 // ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ),
161 // "Unexpected Exception",
162 // JOptionPane.ERROR_MESSAGE );
163 // }
164 // finally {
165 // if ( _mainpanel.getCurrentTreePanel() != null ) {
166 // _mainpanel.getCurrentTreePanel().setArrowCursor();
167 // }
168 // else {
169 // _mainpanel.setArrowCursor();
170 // }
171 // }
172 // if ( ( phys != null ) && ( phys.length > 0 ) ) {
173 // if ( nhx_or_nexus &&
174 // getOptions().isInternalNumberAreConfidenceForNhParsing() ) {
175 // for( final Phylogeny phy : phys ) {
176 // PhylogenyMethods.transferInternalNodeNamesToConfidence( phy, "" );
177 // }
178 // }
179 // AptxUtil.addPhylogeniesToTabs( phys,
180 // new File( url.getFile() ).getName(),
181 // new File( url.getFile() ).toString(),
182 // getConfiguration(),
183 // getMainPanel() );
184 // _mainpanel.getControlPanel().showWhole();
185 // }
186 // }
187 // activateSaveAllIfNeeded();
188 // System.gc();
189 // }
190 //
191 // }
192 //
193 //
194 //
195 //
196 //
197 //