JAL-2847 small things, filled in actionperformeds
[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.io.FileNotFoundException;
12 import java.io.IOException;
13 import java.net.URL;
14 import java.util.Map;
15
16 import org.forester.archaeopteryx.AptxUtil;
17 import org.forester.archaeopteryx.Archaeopteryx;
18 import org.forester.archaeopteryx.Configuration;
19 import org.forester.archaeopteryx.MainFrame;
20 import org.forester.io.parsers.nhx.NHXParser;
21 import org.forester.phylogeny.Phylogeny;
22 import org.forester.phylogeny.PhylogenyNode;
23
24 /**
25  * Static class for creating Archaeopteryx tree viewer instances from calculated
26  * trees and letting them be bound to Jalview.
27  * 
28  * @author kjvanderheide
29  *
30  */
31 public final class AptxInit
32 {
33
34   private final static Configuration APTX_CONFIG = new Configuration(
35           "_aptx_jalview_configuration_file", false, false, false);
36
37   private final static boolean VALIDATE_PHYLOXML_XSD = APTX_CONFIG
38           .isValidatePhyloXmlAgainstSchema();
39
40   private final static boolean REPLACE_NHX_UNDERSCORES = APTX_CONFIG
41           .isReplaceUnderscoresInNhParsing();
42
43   private final static boolean INTERNAL_NUMBERS_AS_CONFIDENCE = APTX_CONFIG
44           .isInternalNumberAreConfidenceForNhParsing();
45
46   private final static boolean MIDPOINT_REROOT = APTX_CONFIG
47           .isMidpointReroot();
48
49   private final static NHXParser.TAXONOMY_EXTRACTION TAXONOMY_EXTRACTION = APTX_CONFIG
50           .getTaxonomyExtraction();
51
52   /**
53    * Test method, should generally not be used as it does not bind the tree to
54    * its alignment
55    * 
56    * @param aptxTrees
57    * @return
58    */
59   public static MainFrame createUnboundInstance(final Phylogeny aptxTree)
60   {
61     Phylogeny[] aptxTrees = { aptxTree };
62     return createAptxFrame(aptxTrees);
63   }
64
65   // public static MainFrame createInstance(final Phylogeny[] aptxTrees,
66   // AlignmentViewport jalviewAlignmentView)
67   // {
68   // return createAptxFrameInJalview(aptxTrees);
69   //
70   // }
71   //
72   // public static MainFrame createInstance(final Phylogeny aptxTree,
73   // final AlignmentViewport jalviewAlignmentView)
74   // {
75   // Phylogeny[] aptxTrees = { aptxTree }; // future possibility to load in
76   // // several trees simultaneously
77   // return createAptxFrameInJalview(aptxTrees);
78   //
79   // }
80
81   public static MainFrame createInstance(
82           final TreeBuilder calculatedTree) // very dense method, to be split up
83   {
84     ExternalTreeBuilderI<Phylogeny, PhylogenyNode> aptxTreeBuilder = new AptxTreeBuilder(
85             calculatedTree);
86
87     Phylogeny aptxTree = aptxTreeBuilder.buildTree();
88     Phylogeny[] aptxTrees = { aptxTree }; // future possibility to load in
89                                           // several trees simultaneously
90
91     MainFrame aptxApp = createAptxFrame(aptxTrees);
92             
93     bindNodesToJalviewSequences(aptxApp, calculatedTree.getAvport(),
94             aptxTreeBuilder.getAlignmentBoundNodes(),
95             aptxTreeBuilder.getNodesBoundAlignment());
96
97     return bindFrameToJalview(aptxApp);
98
99   }
100
101   public static MainFrame createInstanceFromFile(String filePath,
102           AlignmentViewport viewport)
103   {
104     String[] AptxArgs = new String[] { "-c",
105         APTX_CONFIG.getConfigFilename(), filePath };
106     MainFrame aptxApp = Archaeopteryx.main(AptxArgs);
107
108     int tabCount = aptxApp.getMainPanel().getTabbedPane().getTabCount();
109
110     for (int i = 0; i < tabCount; i++)
111     {
112       // roundabout way to select each tree because getComponentAt(i) requires
113       // casting to TreePanel which doesn't work
114       aptxApp.getMainPanel().getTabbedPane().setSelectedIndex(i);
115       Phylogeny tree = aptxApp.getMainPanel().getCurrentTreePanel()
116               .getPhylogeny();
117
118     LoadedTreeAssociation bindAptxNodes = new LoadedTreeAssociation(
119             viewport.getAlignment().getSequencesArray(),
120               tree);
121
122     bindAptxNodes.associateLeavesToSequences();
123
124     bindNodesToJalviewSequences(aptxApp, viewport,
125             bindAptxNodes.getAlignmentWithNodes(),
126             bindAptxNodes.getNodesWithAlignment());
127     }
128     return bindFrameToJalview(aptxApp);
129   }
130
131   public static MainFrame createInstanceFromUrl(URL treeUrl,
132           AlignmentViewport viewport)
133           throws FileNotFoundException, IOException, RuntimeException
134   {
135     String treeTitle = treeUrl.getFile();
136     Phylogeny[] trees = AptxUtil.readPhylogeniesFromUrl(treeUrl,
137             VALIDATE_PHYLOXML_XSD,
138              REPLACE_NHX_UNDERSCORES, INTERNAL_NUMBERS_AS_CONFIDENCE,
139             TAXONOMY_EXTRACTION, MIDPOINT_REROOT);
140     MainFrame aptxApp = Archaeopteryx.createApplication(trees, APTX_CONFIG,
141             treeTitle);
142
143     for (Phylogeny tree : trees)
144     {
145       LoadedTreeAssociation bindAptxNodes = new LoadedTreeAssociation(
146               viewport.getAlignment().getSequencesArray(), tree);
147
148       bindAptxNodes.associateLeavesToSequences();
149       bindNodesToJalviewSequences(aptxApp, viewport,
150               bindAptxNodes.getAlignmentWithNodes(),
151               bindAptxNodes.getNodesWithAlignment());
152
153     }
154
155     return bindFrameToJalview(aptxApp);
156
157   }
158
159
160
161
162   public static MainFrame createAptxFrame(
163           final Phylogeny[] aptxTrees)
164   {
165     MainFrame aptxApp = Archaeopteryx.createApplication(aptxTrees,
166             APTX_CONFIG, null);
167     return aptxApp;
168   }
169
170
171   public static ExternalTreeViewerBindingI<?> bindNodesToJalviewSequences(
172           final MainFrame aptxApp,
173           final AlignmentViewport jalviewAlignViewport,
174           final Map<SequenceI, PhylogenyNode> alignMappedToNodes,
175           final Map<PhylogenyNode, SequenceI> nodesMappedToAlign)
176   {
177     return new JalviewBinding(aptxApp, jalviewAlignViewport,
178             alignMappedToNodes, nodesMappedToAlign);
179   }
180
181
182   public static MainFrame bindFrameToJalview(final MainFrame aptxApp)
183   {
184     int width = 400;
185     int height = 550;
186     aptxApp.setMinimumSize(new Dimension(width, height));
187     // aptxApp.setFont(Desktop.instance.getFont());
188     // aptxApp.getMainPanel().setFont(Desktop.instance.getFont());
189
190     Desktop.addInternalFrame(aptxApp, "Archaeopteryx Tree View", true,
191             width, height, true, true);
192
193     return aptxApp;
194
195   }
196
197
198
199 }