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