JAL-281 option pane with error handling for selecting an URL
[jalview.git] / src / jalview / ext / archaeopteryx / AptxInit.java
1 package jalview.ext.archaeopteryx;
2
3 import jalview.analysis.TreeBuilder;
4 import jalview.datamodel.SequenceI;
5 import jalview.ext.treeviewer.ExternalTreeBuilderI;
6 import jalview.ext.treeviewer.ExternalTreeViewerBindingI;
7 import jalview.gui.Desktop;
8 import jalview.viewmodel.AlignmentViewport;
9
10 import java.awt.Dimension;
11 import java.net.URL;
12 import java.util.Map;
13
14 import org.forester.archaeopteryx.Archaeopteryx;
15 import org.forester.archaeopteryx.MainFrame;
16 import org.forester.phylogeny.Phylogeny;
17 import org.forester.phylogeny.PhylogenyNode;
18
19 /**
20  * Static class for creating Archaeopteryx tree viewer instances from calculated
21  * trees and letting them be bound to Jalview.
22  * 
23  * @author kjvanderheide
24  *
25  */
26 public final class AptxInit
27 {
28   /**
29    * Test method, should generally not be used as it does not bind the tree to
30    * its alignment
31    * 
32    * @param aptxTrees
33    * @return
34    */
35   public static MainFrame createUnboundInstance(final Phylogeny aptxTree)
36   {
37     Phylogeny[] aptxTrees = { aptxTree };
38     return createAptxFrame(aptxTrees);
39   }
40
41   // public static MainFrame createInstance(final Phylogeny[] aptxTrees,
42   // AlignmentViewport jalviewAlignmentView)
43   // {
44   // return createAptxFrameInJalview(aptxTrees);
45   //
46   // }
47   //
48   // public static MainFrame createInstance(final Phylogeny aptxTree,
49   // final AlignmentViewport jalviewAlignmentView)
50   // {
51   // Phylogeny[] aptxTrees = { aptxTree }; // future possibility to load in
52   // // several trees simultaneously
53   // return createAptxFrameInJalview(aptxTrees);
54   //
55   // }
56
57   public static MainFrame createInstance(
58           final TreeBuilder calculatedTree) // very dense method, to be split up
59   {
60     ExternalTreeBuilderI<Phylogeny, PhylogenyNode> aptxTreeBuilder = new AptxTreeBuilder(
61             calculatedTree);
62
63     Phylogeny aptxTree = aptxTreeBuilder.buildTree();
64     Phylogeny[] aptxTrees = { aptxTree }; // future possibility to load in
65                                           // several trees simultaneously
66
67     MainFrame aptxApp = createAptxFrame(aptxTrees);
68             
69     bindNodesToJalviewSequences(aptxApp, calculatedTree.getAvport(),
70             aptxTreeBuilder.getAlignmentBoundNodes(),
71             aptxTreeBuilder.getNodesBoundAlignment());
72
73     return bindFrameToJalview(aptxApp);
74
75   }
76
77   public static MainFrame createInstanceFromFile(String filePath,
78           AlignmentViewport viewport)
79   {
80     String[] AptxArgs = new String[] { "-c",
81         "_aptx_jalview_configuration_file", filePath };
82     MainFrame aptxApp = Archaeopteryx.main(AptxArgs);
83
84     LoadedTreeAssociation bindAptxNodes = new LoadedTreeAssociation(
85             viewport.getAlignment().getSequencesArray(),
86             aptxApp.getMainPanel().getCurrentTreePanel().getPhylogeny());
87
88     bindAptxNodes.associateLeavesToSequences();
89
90     bindNodesToJalviewSequences(aptxApp, viewport,
91             bindAptxNodes.getAlignmentWithNodes(),
92             bindAptxNodes.getNodesWithAlignment());
93
94     return bindFrameToJalview(aptxApp);
95   }
96
97   public static MainFrame createInstanceFromUrl(URL url)
98   {
99
100     return null;
101     // void readPhylogeniesFromURL() {
102     // URL url = null;
103     // Phylogeny[] phys = null;
104     // final String message = "Please enter a complete URL, for example
105     // \"http://purl.org/phylo/treebase/phylows/study/TB2:S15480?format=nexus\"";
106     // final String url_string = JOptionPane
107     // .showInputDialog( this,
108     // message,
109     // "Use URL/webservice to obtain a phylogeny",
110     // JOptionPane.QUESTION_MESSAGE );
111     // boolean nhx_or_nexus = false;
112     // if ( ( url_string != null ) && ( url_string.length() > 0 ) ) {
113     // try {
114     // url = new URL( url_string );
115     // PhylogenyParser parser = null;
116     // if ( url.getHost().toLowerCase().indexOf( "tolweb" ) >= 0 ) {
117     // parser = new TolParser();
118     // }
119     // else {
120     // parser = ParserUtils
121     // .createParserDependingOnUrlContents( url,
122     // getConfiguration().isValidatePhyloXmlAgainstSchema() );
123     // }
124     // if ( parser instanceof NexusPhylogeniesParser ) {
125     // nhx_or_nexus = true;
126     // }
127     // else if ( parser instanceof NHXParser ) {
128     // nhx_or_nexus = true;
129     // }
130     // if ( _mainpanel.getCurrentTreePanel() != null ) {
131     // _mainpanel.getCurrentTreePanel().setWaitCursor();
132     // }
133     // else {
134     // _mainpanel.setWaitCursor();
135     // }
136     // final PhylogenyFactory factory =
137     // ParserBasedPhylogenyFactory.getInstance();
138     // phys = factory.create( url.openStream(), parser );
139     // }
140     // catch ( final MalformedURLException e ) {
141     // JOptionPane.showMessageDialog( this,
142     // "Malformed URL: " + url + "\n" + e.getLocalizedMessage(),
143     // "Malformed URL",
144     // JOptionPane.ERROR_MESSAGE );
145     // }
146     // catch ( final IOException e ) {
147     // JOptionPane.showMessageDialog( this,
148     // "Could not read from " + url + "\n"
149     // + ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ),
150     // "Failed to read URL",
151     // JOptionPane.ERROR_MESSAGE );
152     // }
153     // catch ( final Exception e ) {
154     // JOptionPane.showMessageDialog( this,
155     // ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ),
156     // "Unexpected Exception",
157     // JOptionPane.ERROR_MESSAGE );
158     // }
159     // finally {
160     // if ( _mainpanel.getCurrentTreePanel() != null ) {
161     // _mainpanel.getCurrentTreePanel().setArrowCursor();
162     // }
163     // else {
164     // _mainpanel.setArrowCursor();
165     // }
166     // }
167     // if ( ( phys != null ) && ( phys.length > 0 ) ) {
168     // if ( nhx_or_nexus &&
169     // getOptions().isInternalNumberAreConfidenceForNhParsing() ) {
170     // for( final Phylogeny phy : phys ) {
171     // PhylogenyMethods.transferInternalNodeNamesToConfidence( phy, "" );
172     // }
173     // }
174     // AptxUtil.addPhylogeniesToTabs( phys,
175     // new File( url.getFile() ).getName(),
176     // new File( url.getFile() ).toString(),
177     // getConfiguration(),
178     // getMainPanel() );
179     // _mainpanel.getControlPanel().showWhole();
180     // }
181     // }
182     // activateSaveAllIfNeeded();
183     // System.gc();
184     // }
185
186   }
187
188
189
190   public static MainFrame createAptxFrame(
191           final Phylogeny[] aptxTrees)
192   {
193     MainFrame aptxApp = Archaeopteryx.createApplication(aptxTrees,
194             "_aptx_jalview_configuration_file", null);
195     return aptxApp;
196   }
197
198
199   public static ExternalTreeViewerBindingI<?> bindNodesToJalviewSequences(
200           final MainFrame aptxApp,
201           final AlignmentViewport jalviewAlignViewport,
202           final Map<SequenceI, PhylogenyNode> alignMappedToNodes,
203           final Map<PhylogenyNode, SequenceI> nodesMappedToAlign)
204   {
205     return new JalviewBinding(aptxApp, jalviewAlignViewport,
206             alignMappedToNodes, nodesMappedToAlign);
207   }
208
209
210   public static MainFrame bindFrameToJalview(final MainFrame aptxApp)
211   {
212     int width = 400;
213     int height = 550;
214     aptxApp.setMinimumSize(new Dimension(width, height));
215     // aptxApp.setFont(Desktop.instance.getFont());
216     // aptxApp.getMainPanel().setFont(Desktop.instance.getFont());
217
218     Desktop.addInternalFrame(aptxApp, "Archaeopteryx Tree View", true,
219             width, height, true, true);
220
221     return aptxApp;
222
223   }
224
225
226 }