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