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