JAL-2799 rearranged AptxInit code
[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
62   public static MainFrame createInstanceFromCalculation(
63           final TreeBuilder calculatedTree) // very dense method, to be split up
64   {
65     ExternalTreeBuilderI<Phylogeny, PhylogenyNode> aptxTreeBuilder = new AptxTreeBuilder(
66             calculatedTree);
67
68     Phylogeny aptxTree = aptxTreeBuilder.buildTree();
69
70     MainFrame aptxApp = createAptxFrame(aptxTree,
71             calculatedTree.getAvport(), null);
72             
73     return aptxApp;
74   }
75
76   /**
77    * Refactored from Archaeopteryx.main
78    * 
79    * @param filePath
80    * @param viewport
81    * @return
82    * @throws IOException
83    * @throws FileNotFoundException
84    */
85   public static MainFrame[] createInstancesFromFile(String filePath,
86           AlignmentViewport viewport)
87           throws FileNotFoundException, IOException
88   {
89     File treeFile = new File(filePath);
90     final String err = ForesterUtil.isReadableFile(treeFile);
91     if (!ForesterUtil.isEmpty(err))
92     {
93       JvOptionPane.showMessageDialog(Desktop.desktop, err,
94               MessageManager.getString("label.problem_reading_tree_file"),
95               JvOptionPane.WARNING_MESSAGE);
96     }
97     boolean nhx_or_nexus = false;
98     final PhylogenyParser p = ParserUtils.createParserDependingOnFileType(
99             treeFile, VALIDATE_PHYLOXML_XSD);
100     if (p instanceof NHXParser)
101     {
102       nhx_or_nexus = true;
103       final NHXParser nhx = (NHXParser) p;
104       nhx.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES);
105       nhx.setIgnoreQuotes(false);
106       nhx.setTaxonomyExtraction(TAXONOMY_EXTRACTION);
107     }
108     else if (p instanceof NexusPhylogeniesParser)
109     {
110       nhx_or_nexus = true;
111       final NexusPhylogeniesParser nex = (NexusPhylogeniesParser) p;
112       nex.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES);
113       nex.setIgnoreQuotes(false);
114     }
115 //    else if (p instanceof PhyloXmlParser)
116 //    {
117 //      MainFrameApplication.warnIfNotPhyloXmlValidation(APTX_CONFIG);
118 //    }
119     Phylogeny[] phylogenies = PhylogenyMethods.readPhylogenies(p, treeFile);
120     MainFrame[] aptxFrames = new MainFrame[phylogenies.length];
121     String treeTitle = treeFile.getName();
122
123     for (int i = 0; i < phylogenies.length; i++)
124       {
125       Phylogeny tree = phylogenies[i];
126
127       if (nhx_or_nexus && INTERNAL_NUMBERS_AS_CONFIDENCE)
128       {
129         PhylogenyMethods.transferInternalNodeNamesToConfidence(tree, "");
130       }
131
132       aptxFrames[i] = createAptxFrame(tree, viewport, treeTitle);
133     }
134
135     return aptxFrames;
136     }
137
138
139   public static MainFrame[] createInstancesFromUrl(URL treeUrl,
140           AlignmentViewport viewport)
141           throws FileNotFoundException, IOException, RuntimeException
142   {
143     
144     String treeTitle = treeUrl.getFile();
145     Phylogeny[] trees = AptxUtil.readPhylogeniesFromUrl(treeUrl,
146             VALIDATE_PHYLOXML_XSD,
147              REPLACE_NHX_UNDERSCORES, INTERNAL_NUMBERS_AS_CONFIDENCE,
148             TAXONOMY_EXTRACTION, MIDPOINT_REROOT);
149
150     MainFrame[] aptxFrames = new MainFrame[trees.length];
151     for (int i = 0; i < trees.length; i++)
152     {
153       Phylogeny tree = trees[i];
154       aptxFrames[i] = createAptxFrame(tree, viewport, treeTitle);
155     }
156
157     return aptxFrames;
158
159   }
160
161
162
163
164
165   public static MainFrame createAptxFrame(
166           final Phylogeny aptxTree,
167           final AlignmentViewport jalviewAlignport, String treeTitle)
168   {
169     MainFrame aptxApp = Archaeopteryx.createApplication(aptxTree,
170             APTX_CONFIG, treeTitle);
171     LoadedTreeAssociation bindAptxNodes = new LoadedTreeAssociation(
172             jalviewAlignport.getAlignment().getSequencesArray(), aptxTree);
173
174     bindAptxNodes.associateLeavesToSequences();
175     bindNodesToJalviewSequences(aptxApp, jalviewAlignport,
176             bindAptxNodes.getAlignmentWithNodes(),
177             bindAptxNodes.getNodesWithAlignment());
178     bindFrameToJalview(aptxApp);
179     return aptxApp;
180   }
181
182
183   public static ExternalTreeViewerBindingI<?> bindNodesToJalviewSequences(
184           final MainFrame aptxApp,
185           final AlignmentViewport jalviewAlignViewport,
186           final Map<SequenceI, PhylogenyNode> alignMappedToNodes,
187           final Map<PhylogenyNode, SequenceI> nodesMappedToAlign)
188   {
189     return new JalviewBinding(aptxApp, jalviewAlignViewport,
190             alignMappedToNodes, nodesMappedToAlign);
191   }
192
193
194   public static MainFrame bindFrameToJalview(final MainFrame aptxApp)
195   {
196     int width = 400;
197     int height = 550;
198     aptxApp.setMinimumSize(new Dimension(width, height));
199     // aptxApp.setFont(Desktop.instance.getFont());
200     // aptxApp.getMainPanel().setFont(Desktop.instance.getFont());
201
202     Desktop.addInternalFrame(aptxApp, "Archaeopteryx Tree View", true,
203             width, height, true, true);
204
205     return aptxApp;
206
207   }
208
209
210
211 }