JAL-2799 ALL trees are now loaded in as separate Aptx instances
[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.gui.JvOptionPane;
9 import jalview.util.MessageManager;
10 import jalview.viewmodel.AlignmentViewport;
11
12 import java.awt.Dimension;
13 import java.io.File;
14 import java.io.FileNotFoundException;
15 import java.io.IOException;
16 import java.net.URL;
17 import java.util.Map;
18
19 import org.forester.archaeopteryx.AptxUtil;
20 import org.forester.archaeopteryx.Archaeopteryx;
21 import org.forester.archaeopteryx.Configuration;
22 import org.forester.archaeopteryx.MainFrame;
23 import org.forester.io.parsers.PhylogenyParser;
24 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
25 import org.forester.io.parsers.nhx.NHXParser;
26 import org.forester.io.parsers.util.ParserUtils;
27 import org.forester.phylogeny.Phylogeny;
28 import org.forester.phylogeny.PhylogenyMethods;
29 import org.forester.phylogeny.PhylogenyNode;
30 import org.forester.util.ForesterUtil;
31
32 /**
33  * Static class for creating Archaeopteryx tree viewer instances from calculated
34  * trees and letting them be bound to Jalview.
35  * 
36  * @author kjvanderheide
37  *
38  */
39 public final class AptxInit
40 {
41
42   private final static Configuration APTX_CONFIG = new Configuration(
43           "_aptx_jalview_configuration_file", false, false, false);
44
45   private final static boolean VALIDATE_PHYLOXML_XSD = APTX_CONFIG
46           .isValidatePhyloXmlAgainstSchema();
47
48   private final static boolean REPLACE_NHX_UNDERSCORES = APTX_CONFIG
49           .isReplaceUnderscoresInNhParsing();
50
51   private final static boolean INTERNAL_NUMBERS_AS_CONFIDENCE = APTX_CONFIG
52           .isInternalNumberAreConfidenceForNhParsing();
53
54   private final static boolean MIDPOINT_REROOT = APTX_CONFIG
55           .isMidpointReroot();
56
57   private final static NHXParser.TAXONOMY_EXTRACTION TAXONOMY_EXTRACTION = APTX_CONFIG
58           .getTaxonomyExtraction();
59
60   /**
61    * Test method, should generally not be used as it does not bind the tree to
62    * its alignment
63    * 
64    * @param aptxTrees
65    * @return
66    */
67   public static MainFrame createUnboundInstance(final Phylogeny aptxTree)
68   {
69     return createAptxFrame(aptxTree);
70   }
71
72   // public static MainFrame createInstance(final Phylogeny[] aptxTrees,
73   // AlignmentViewport jalviewAlignmentView)
74   // {
75   // return createAptxFrameInJalview(aptxTrees);
76   //
77   // }
78   //
79   // public static MainFrame createInstance(final Phylogeny aptxTree,
80   // final AlignmentViewport jalviewAlignmentView)
81   // {
82   // Phylogeny[] aptxTrees = { aptxTree }; // future possibility to load in
83   // // several trees simultaneously
84   // return createAptxFrameInJalview(aptxTrees);
85   //
86   // }
87
88   public static MainFrame createInstance(
89           final TreeBuilder calculatedTree) // very dense method, to be split up
90   {
91     ExternalTreeBuilderI<Phylogeny, PhylogenyNode> aptxTreeBuilder = new AptxTreeBuilder(
92             calculatedTree);
93
94     Phylogeny aptxTree = aptxTreeBuilder.buildTree();
95
96     MainFrame aptxApp = createAptxFrame(aptxTree);
97             
98     bindNodesToJalviewSequences(aptxApp, calculatedTree.getAvport(),
99             aptxTreeBuilder.getAlignmentBoundNodes(),
100             aptxTreeBuilder.getNodesBoundAlignment());
101
102     return bindFrameToJalview(aptxApp);
103
104   }
105
106   /**
107    * Refactored from Archaeopteryx.main
108    * 
109    * @param filePath
110    * @param viewport
111    * @return
112    * @throws IOException
113    * @throws FileNotFoundException
114    */
115   public static MainFrame[] createInstancesFromFile(String filePath,
116           AlignmentViewport viewport)
117           throws FileNotFoundException, IOException
118   {
119     File treeFile = new File(filePath);
120     final String err = ForesterUtil.isReadableFile(treeFile);
121     if (!ForesterUtil.isEmpty(err))
122     {
123       JvOptionPane.showMessageDialog(Desktop.desktop, err,
124               MessageManager.getString("label.problem_reading_tree_file"),
125               JvOptionPane.WARNING_MESSAGE);
126     }
127     boolean nhx_or_nexus = false;
128     final PhylogenyParser p = ParserUtils.createParserDependingOnFileType(
129             treeFile, VALIDATE_PHYLOXML_XSD);
130     if (p instanceof NHXParser)
131     {
132       nhx_or_nexus = true;
133       final NHXParser nhx = (NHXParser) p;
134       nhx.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES);
135       nhx.setIgnoreQuotes(false);
136       nhx.setTaxonomyExtraction(TAXONOMY_EXTRACTION);
137     }
138     else if (p instanceof NexusPhylogeniesParser)
139     {
140       nhx_or_nexus = true;
141       final NexusPhylogeniesParser nex = (NexusPhylogeniesParser) p;
142       nex.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES);
143       nex.setIgnoreQuotes(false);
144     }
145 //    else if (p instanceof PhyloXmlParser)
146 //    {
147 //      MainFrameApplication.warnIfNotPhyloXmlValidation(APTX_CONFIG);
148 //    }
149     Phylogeny[] phylogenies = PhylogenyMethods.readPhylogenies(p, treeFile);
150     MainFrame[] aptxFrames = new MainFrame[phylogenies.length];
151     String treeTitle = treeFile.getName();
152
153     for (int i = 0; i < phylogenies.length; i++)
154       {
155       Phylogeny tree = phylogenies[i];
156       if (nhx_or_nexus && INTERNAL_NUMBERS_AS_CONFIDENCE)
157       {
158         PhylogenyMethods.transferInternalNodeNamesToConfidence(tree, "");
159       }
160
161       MainFrame aptxApp = Archaeopteryx.createApplication(tree, APTX_CONFIG,
162               treeTitle);
163       LoadedTreeAssociation bindAptxNodes = new LoadedTreeAssociation(
164               viewport.getAlignment().getSequencesArray(), tree);
165
166       bindAptxNodes.associateLeavesToSequences();
167       bindNodesToJalviewSequences(aptxApp, viewport,
168               bindAptxNodes.getAlignmentWithNodes(),
169               bindAptxNodes.getNodesWithAlignment());
170       bindFrameToJalview(aptxApp);
171       aptxFrames[i] = aptxApp;
172     }
173
174     return aptxFrames;
175     }
176
177
178   public static MainFrame[] createInstancesFromUrl(URL treeUrl,
179           AlignmentViewport viewport)
180           throws FileNotFoundException, IOException, RuntimeException
181   {
182     
183     String treeTitle = treeUrl.getFile();
184     Phylogeny[] trees = AptxUtil.readPhylogeniesFromUrl(treeUrl,
185             VALIDATE_PHYLOXML_XSD,
186              REPLACE_NHX_UNDERSCORES, INTERNAL_NUMBERS_AS_CONFIDENCE,
187             TAXONOMY_EXTRACTION, MIDPOINT_REROOT);
188
189     MainFrame[] aptxFrames = new MainFrame[trees.length];
190     for (int i = 0; i < trees.length; i++)
191     {
192       Phylogeny tree = trees[i];
193       MainFrame aptxApp = Archaeopteryx.createApplication(tree, APTX_CONFIG,
194               treeTitle);
195       LoadedTreeAssociation bindAptxNodes = new LoadedTreeAssociation(
196               viewport.getAlignment().getSequencesArray(), tree);
197
198       bindAptxNodes.associateLeavesToSequences();
199       bindNodesToJalviewSequences(aptxApp, viewport,
200               bindAptxNodes.getAlignmentWithNodes(),
201               bindAptxNodes.getNodesWithAlignment());
202       bindFrameToJalview(aptxApp);
203       aptxFrames[i] = aptxApp;
204     }
205
206     return aptxFrames;
207
208   }
209
210
211
212
213   public static MainFrame createAptxFrame(
214           final Phylogeny aptxTree)
215   {
216     MainFrame aptxApp = Archaeopteryx.createApplication(aptxTree,
217             APTX_CONFIG, null);
218     return aptxApp;
219   }
220
221
222   public static ExternalTreeViewerBindingI<?> bindNodesToJalviewSequences(
223           final MainFrame aptxApp,
224           final AlignmentViewport jalviewAlignViewport,
225           final Map<SequenceI, PhylogenyNode> alignMappedToNodes,
226           final Map<PhylogenyNode, SequenceI> nodesMappedToAlign)
227   {
228     return new JalviewBinding(aptxApp, jalviewAlignViewport,
229             alignMappedToNodes, nodesMappedToAlign);
230   }
231
232
233   public static MainFrame bindFrameToJalview(final MainFrame aptxApp)
234   {
235     int width = 400;
236     int height = 550;
237     aptxApp.setMinimumSize(new Dimension(width, height));
238     // aptxApp.setFont(Desktop.instance.getFont());
239     // aptxApp.getMainPanel().setFont(Desktop.instance.getFont());
240
241     Desktop.addInternalFrame(aptxApp, "Archaeopteryx Tree View", true,
242             width, height, true, true);
243
244     return aptxApp;
245
246   }
247
248
249
250 }