JAL-2805 JAL-2847 JAL-281 added file path to Aptx frame title
[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.MalformedURLException;
17 import java.net.URL;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.forester.archaeopteryx.AptxUtil;
22 import org.forester.archaeopteryx.Archaeopteryx;
23 import org.forester.archaeopteryx.Configuration;
24 import org.forester.archaeopteryx.MainFrame;
25 import org.forester.archaeopteryx.webservices.PhylogeniesWebserviceClient;
26 import org.forester.archaeopteryx.webservices.WebserviceUtil;
27 import org.forester.archaeopteryx.webservices.WebservicesManager;
28 import org.forester.io.parsers.PhylogenyParser;
29 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
30 import org.forester.io.parsers.nhx.NHXParser;
31 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
32 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
33 import org.forester.io.parsers.tol.TolParser;
34 import org.forester.io.parsers.util.ParserUtils;
35 import org.forester.phylogeny.Phylogeny;
36 import org.forester.phylogeny.PhylogenyMethods;
37 import org.forester.phylogeny.PhylogenyNode;
38 import org.forester.phylogeny.data.Identifier;
39 import org.forester.util.ForesterUtil;
40
41 /**
42  * Static class for creating Archaeopteryx tree viewer instances from calculated
43  * trees and letting them be bound to Jalview.
44  * 
45  * @author kjvanderheide
46  *
47  */
48 public final class AptxInit
49 {
50
51   private final static Configuration APTX_CONFIG = new Configuration(
52           "_aptx_jalview_configuration_file", false, false, false);
53
54   private final static boolean VALIDATE_PHYLOXML_XSD = APTX_CONFIG
55           .isValidatePhyloXmlAgainstSchema();
56
57   private final static boolean REPLACE_NHX_UNDERSCORES = APTX_CONFIG
58           .isReplaceUnderscoresInNhParsing();
59
60   private final static boolean INTERNAL_NUMBERS_AS_CONFIDENCE = APTX_CONFIG
61           .isInternalNumberAreConfidenceForNhParsing();
62
63   private final static boolean MIDPOINT_REROOT = APTX_CONFIG
64           .isMidpointReroot();
65
66   private final static NHXParser.TAXONOMY_EXTRACTION TAXONOMY_EXTRACTION = APTX_CONFIG
67           .getTaxonomyExtraction();
68
69   private static Map<MainFrame, JalviewBinding> activeAptx = new HashMap<>();
70
71
72
73   public static MainFrame createInstanceFromCalculation(
74           final TreeBuilder calculatedTree)
75   {
76     ExternalTreeBuilderI<Phylogeny, PhylogenyNode> aptxTreeBuilder = new AptxTreeBuilder(
77             calculatedTree);
78
79     Phylogeny aptxTree = aptxTreeBuilder.buildTree();
80
81     MainFrame aptxApp = createAptxFrame(aptxTree,
82             calculatedTree.getAvport(), null);
83             
84     return aptxApp;
85   }
86
87   /**
88    * Refactored from Archaeopteryx.main
89    * 
90    * @param filePath
91    * @param viewport
92    * @return
93    * @throws IOException
94    * @throws FileNotFoundException
95    */
96   public static MainFrame[] createInstancesFromFile(String filePath,
97           AlignmentViewport viewport)
98           throws FileNotFoundException, IOException
99   {
100     File treeFile = new File(filePath);
101     final String err = ForesterUtil.isReadableFile(treeFile);
102     if (!ForesterUtil.isEmpty(err))
103     {
104       JvOptionPane.showMessageDialog(Desktop.desktop, err,
105               MessageManager.getString("label.problem_reading_tree_file"),
106               JvOptionPane.WARNING_MESSAGE);
107     }
108
109     if (Desktop.instance != null)
110     {
111       Desktop.instance.startLoading(filePath);
112     }
113     boolean nhx_or_nexus = false;
114     final PhylogenyParser parser = ParserUtils.createParserDependingOnFileType(
115             treeFile, VALIDATE_PHYLOXML_XSD);
116     if (parser instanceof NHXParser)
117     {
118       nhx_or_nexus = true;
119       final NHXParser nhx = (NHXParser) parser;
120       nhx.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES);
121       nhx.setIgnoreQuotes(false);
122       nhx.setTaxonomyExtraction(TAXONOMY_EXTRACTION);
123     }
124     else if (parser instanceof NexusPhylogeniesParser)
125     {
126       nhx_or_nexus = true;
127       final NexusPhylogeniesParser nex = (NexusPhylogeniesParser) parser;
128       nex.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES);
129       nex.setIgnoreQuotes(false);
130     }
131     else if (parser instanceof PhyloXmlParser)
132     {
133       if ( VALIDATE_PHYLOXML_XSD ) {
134         JvOptionPane.showInternalMessageDialog( null,
135                                        ForesterUtil.wordWrap(
136                                                               "phyloXML XSD-based validation is turned off [enable with line 'validate_against_phyloxml_xsd_schem: true' in configuration file]",
137                                                               80 ),
138                                        "Warning",
139                                        JvOptionPane.WARNING_MESSAGE );
140       }
141     }
142     Phylogeny[] trees = PhylogenyMethods.readPhylogenies(parser, treeFile);
143     MainFrame[] aptxFrames = new MainFrame[trees.length];
144
145
146     for (int i = 0; i < trees.length; i++)
147       {
148       Phylogeny tree = trees[i];
149
150       if (nhx_or_nexus && INTERNAL_NUMBERS_AS_CONFIDENCE)
151       {
152         PhylogenyMethods.transferInternalNodeNamesToConfidence(tree, "");
153       }
154       String treeTitle = treeFile.getName() + "[" + i + "]";
155       tree.setName(treeTitle);
156       aptxFrames[i] = createAptxFrame(tree, viewport, treeTitle);
157
158     }
159     if (Desktop.instance != null)
160     {
161       Desktop.instance.stopLoading();
162     }
163     return aptxFrames;
164     }
165
166
167   public static MainFrame[] createInstancesFromUrl(URL treeUrl,
168           AlignmentViewport viewport)
169           throws FileNotFoundException, IOException, RuntimeException
170   {
171     
172     String treeTitle = treeUrl.getFile();
173     if (Desktop.instance != null)
174     {
175       Desktop.instance.startLoading(treeTitle);
176     }
177     Phylogeny[] trees = AptxUtil.readPhylogeniesFromUrl(treeUrl,
178             VALIDATE_PHYLOXML_XSD,
179              REPLACE_NHX_UNDERSCORES, INTERNAL_NUMBERS_AS_CONFIDENCE,
180             TAXONOMY_EXTRACTION, MIDPOINT_REROOT);
181
182     MainFrame[] aptxFrames = new MainFrame[trees.length];
183     for (int i = 0; i < trees.length; i++)
184     {
185       Phylogeny tree = trees[i];
186       aptxFrames[i] = createAptxFrame(tree, viewport, treeTitle);
187     }
188
189     if (Desktop.instance != null)
190     {
191       Desktop.instance.stopLoading();
192     }
193
194     return aptxFrames;
195
196   }
197
198   /**
199    * Refactored from Forester's UrlTreeReader, this can be more efficient
200    * 
201    * @param databaseIndex
202    * @param viewport
203    * @return
204    */
205   public static MainFrame[] createInstancesFromDb(int databaseIndex,
206           AlignmentViewport viewport)
207   {
208
209     URL url = null;
210     Phylogeny[] trees = null;
211     final WebservicesManager webservices_manager = WebservicesManager
212             .getInstance();
213     final PhylogeniesWebserviceClient client = webservices_manager
214             .getAvailablePhylogeniesWebserviceClient(databaseIndex);
215     String identifier = JvOptionPane.showInternalInputDialog(
216             Desktop.desktop,
217             client.getInstructions() + "\n(Reference: "
218                     + client.getReference() + ")",
219             client.getDescription(), JvOptionPane.QUESTION_MESSAGE);
220
221     if ((identifier != null) && (identifier.trim().length() > 0))
222     {
223       if (Desktop.instance != null)
224       {
225         Desktop.instance.startLoading(identifier);
226       }
227
228       identifier = identifier.trim();
229       if (client.isQueryInteger())
230       {
231         identifier = identifier.replaceAll("^\\D+", "");
232
233         int id;
234         try
235         {
236           id = Integer.parseInt(identifier);
237         } catch (final NumberFormatException e)
238         {
239           JvOptionPane.showInternalMessageDialog(Desktop.desktop,
240                   "Identifier is expected to be a number",
241                   "Can not open URL", JvOptionPane.ERROR_MESSAGE);
242           return new MainFrame[0];
243         }
244         identifier = id + "";
245       }
246       boolean exception = false;
247       try
248       {
249         String url_str = client.getUrl();
250         url_str = url_str.replaceFirst(
251                 PhylogeniesWebserviceClient.QUERY_PLACEHOLDER, identifier);
252         url = new URL(url_str);
253         PhylogenyParser parser = null;
254         switch (client.getReturnFormat())
255         {
256         case TOL_XML_RESPONSE:
257           parser = new TolParser();
258           break;
259         case NEXUS:
260           parser = new NexusPhylogeniesParser();
261           ((NexusPhylogeniesParser) parser).setReplaceUnderscores(true);
262           break;
263         case TREEBASE_TREE:
264           parser = new NexusPhylogeniesParser();
265           ((NexusPhylogeniesParser) parser).setReplaceUnderscores(true);
266           ((NexusPhylogeniesParser) parser)
267                   .setTaxonomyExtraction(NHXParser.TAXONOMY_EXTRACTION.NO);
268           break;
269         case TREEBASE_STUDY:
270           parser = new NexusPhylogeniesParser();
271           ((NexusPhylogeniesParser) parser).setReplaceUnderscores(true);
272           ((NexusPhylogeniesParser) parser)
273                   .setTaxonomyExtraction(NHXParser.TAXONOMY_EXTRACTION.NO);
274           break;
275         case NH:
276           parser = new NHXParser();
277           ((NHXParser) parser)
278                   .setTaxonomyExtraction(NHXParser.TAXONOMY_EXTRACTION.NO);
279           ((NHXParser) parser).setReplaceUnderscores(true);
280           ((NHXParser) parser).setGuessRootedness(true);
281           break;
282         case NH_EXTRACT_TAXONOMY:
283           parser = new NHXParser();
284           ((NHXParser) parser).setTaxonomyExtraction(
285                   NHXParser.TAXONOMY_EXTRACTION.AGGRESSIVE);
286           ((NHXParser) parser).setReplaceUnderscores(false);
287           ((NHXParser) parser).setGuessRootedness(true);
288           break;
289         case PFAM:
290           parser = new NHXParser();
291           ((NHXParser) parser).setTaxonomyExtraction(
292                   NHXParser.TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT);
293           ((NHXParser) parser).setReplaceUnderscores(false);
294           ((NHXParser) parser).setGuessRootedness(true);
295           break;
296         case NHX:
297           parser = new NHXParser();
298           ((NHXParser) parser)
299                   .setTaxonomyExtraction(NHXParser.TAXONOMY_EXTRACTION.NO);
300           ((NHXParser) parser).setReplaceUnderscores(false);
301           ((NHXParser) parser).setGuessRootedness(true);
302           break;
303         case PHYLOXML:
304           parser = PhyloXmlParser.createPhyloXmlParserXsdValidating();
305           break;
306         default:
307           throw new IllegalArgumentException(
308                   "unknown format: " + client.getReturnFormat());
309         }
310         //
311         // if (_main_frame.getMainPanel().getCurrentTreePanel() != null)
312         // {
313         // _main_frame.getMainPanel().getCurrentTreePanel().setWaitCursor();
314         // }
315         // else
316         // {
317         // _main_frame.getMainPanel().setWaitCursor();
318         // }
319         trees = ForesterUtil.readPhylogeniesFromUrl(url, parser);
320       } catch (final MalformedURLException e)
321       {
322         exception = true;
323         JvOptionPane.showInternalMessageDialog(Desktop.desktop,
324                 "Malformed URL: " + url + "\n" + e.getLocalizedMessage(),
325                 "Malformed URL", JvOptionPane.ERROR_MESSAGE);
326       } catch (final IOException e)
327       {
328         exception = true;
329         JvOptionPane.showInternalMessageDialog(Desktop.desktop,
330                 "Could not read from " + url + "\n"
331                         + e.getLocalizedMessage(),
332                 "Failed to read tree from " + client.getName() + " for "
333                         + identifier,
334                 JvOptionPane.ERROR_MESSAGE);
335       } catch (final NumberFormatException e)
336       {
337         exception = true;
338         JvOptionPane.showInternalMessageDialog(Desktop.desktop,
339                 "Could not read from " + url + "\n"
340                         + e.getLocalizedMessage(),
341                 "Failed to read tree from " + client.getName() + " for "
342                         + identifier,
343                 JvOptionPane.ERROR_MESSAGE);
344       } catch (final Exception e)
345       {
346         exception = true;
347         e.printStackTrace();
348         JvOptionPane.showInternalMessageDialog(Desktop.desktop,
349                 e.getLocalizedMessage(), "Unexpected Exception",
350                 JvOptionPane.ERROR_MESSAGE);
351       } finally
352       {
353         // if (_main_frame.getCurrentTreePanel() != null)
354         // {
355         // _main_frame.getCurrentTreePanel().setArrowCursor();
356         // }
357         // else
358         // {
359         // _main_frame.getMainPanel().setArrowCursor();
360         // }
361       }
362       if ((trees != null) && (trees.length > 0))
363       {
364         for (final Phylogeny phylogeny : trees)
365         {
366           if (!phylogeny.isEmpty())
367           {
368             if (client.getName().equals(WebserviceUtil.TREE_FAM_NAME))
369             {
370               phylogeny.setRerootable(false);
371               phylogeny.setRooted(true);
372             }
373             if (client.getProcessingInstructions() != null)
374             {
375               try
376               {
377                 WebserviceUtil.processInstructions(client, phylogeny);
378               } catch (final PhyloXmlDataFormatException e)
379               {
380                 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
381                         "Error:\n" + e.getLocalizedMessage(), "Error",
382                         JvOptionPane.ERROR_MESSAGE);
383               }
384             }
385             if (client.getNodeField() != null)
386             {
387               try
388               {
389                 PhylogenyMethods.transferNodeNameToField(phylogeny,
390                         client.getNodeField(), false);
391               } catch (final PhyloXmlDataFormatException e)
392               {
393                 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
394                         "Error:\n" + e.getLocalizedMessage(), "Error",
395                         JvOptionPane.ERROR_MESSAGE);
396               }
397             }
398             phylogeny.setIdentifier(
399                     new Identifier(identifier, client.getName()));
400             // _main_frame.getJMenuBar().remove(_main_frame.getHelpMenu());
401             // _main_frame.getMenuBarOfMainFrame()
402             // .add(_main_frame.getHelpMenu());
403             // _main_frame.getMainPanel().addPhylogenyInNewTab(phylogeny,
404             // _main_frame.getConfiguration(),
405             // new File(url.getFile()).getName(), url.toString());
406
407             MainFrame aptxApp = createAptxFrame(phylogeny, viewport,
408                     url.getFile());
409             String my_name_for_file = "";
410             if (!ForesterUtil.isEmpty(phylogeny.getName()))
411             {
412               my_name_for_file = new String(phylogeny.getName())
413                       .replaceAll(" ", "_");
414             }
415             else if (phylogeny.getIdentifier() != null)
416             {
417               final StringBuffer sb = new StringBuffer();
418               if (!ForesterUtil
419                       .isEmpty(phylogeny.getIdentifier().getProvider()))
420               {
421                 sb.append(phylogeny.getIdentifier().getProvider());
422                 sb.append("_");
423               }
424               sb.append(phylogeny.getIdentifier().getValue());
425               my_name_for_file = new String(
426                       sb.toString().replaceAll(" ", "_"));
427             }
428             // _main_frame.getMainPanel().getCurrentTreePanel()
429             // .setTreeFile(new File(my_name_for_file));
430             AptxUtil.lookAtSomeTreePropertiesForAptxControlSettings(
431                     phylogeny, aptxApp.getMainPanel().getControlPanel(),
432                     APTX_CONFIG);
433             // _main_frame.getMainPanel().getControlPanel().showWhole();
434
435             aptxApp.activateSaveAllIfNeeded();
436           }
437         }
438       }
439       else if (!exception)
440       {
441         JvOptionPane.showMessageDialog(null,
442                 ForesterUtil.wordWrap(
443                         "Failed to read in tree(s) from [" + url + "]", 80),
444                 "Error", JvOptionPane.ERROR_MESSAGE);
445       }
446       if ((trees != null) && (trees.length > 0))
447       {
448         try
449         {
450           JvOptionPane.showMessageDialog(null,
451                   ForesterUtil.wordWrap("Successfully read in "
452                           + trees.length + " tree(s) from [" + url + "]",
453                           80),
454                   "Success", JvOptionPane.INFORMATION_MESSAGE);
455         } catch (final Exception e)
456         {
457           // Not important if this fails, do nothing.
458         }
459         // _main_frame.getContentPane().repaint();
460       }
461     }
462
463     System.gc();
464
465
466     if (Desktop.instance != null)
467     {
468       Desktop.instance.stopLoading();
469     }
470     return null;
471
472
473   }
474
475
476
477
478
479   public static MainFrame createAptxFrame(
480           final Phylogeny aptxTree,
481           final AlignmentViewport jalviewAlignport, String treeTitle)
482   {
483     MainFrame aptxApp = Archaeopteryx.createApplication(aptxTree,
484             APTX_CONFIG, treeTitle);
485
486     LoadedTreeSequenceAssociation bindAptxNodes = new LoadedTreeSequenceAssociation(
487             jalviewAlignport.getAlignment().getSequencesArray(), aptxTree);
488     bindAptxNodes.associateLeavesToSequences();
489
490     bindNodesToJalviewSequences(aptxApp, jalviewAlignport,
491             bindAptxNodes.getAlignmentWithNodes(),
492             bindAptxNodes.getNodesWithAlignment());
493     bindTreeViewFrameToJalview(aptxApp);
494
495     return aptxApp;
496   }
497
498   // private static void addPartitioningSlider(MainFrame aptxFrame)
499   // {
500   // JSlider slider = new JSlider();
501   //
502   //
503   // }
504
505   public static ExternalTreeViewerBindingI<?> bindNodesToJalviewSequences(
506           final MainFrame aptxApp,
507           final AlignmentViewport jalviewAlignViewport,
508           final Map<SequenceI, PhylogenyNode> alignMappedToNodes,
509           final Map<PhylogenyNode, SequenceI> nodesMappedToAlign)
510   {
511     JalviewBinding treeBinding = new JalviewBinding(aptxApp,
512             jalviewAlignViewport,
513             alignMappedToNodes, nodesMappedToAlign);
514     activeAptx.put(aptxApp, treeBinding);
515     return treeBinding;
516   }
517
518
519   public static MainFrame bindTreeViewFrameToJalview(
520           final MainFrame aptxApp)
521   {
522     int width = 400;
523     int height = 550;
524     aptxApp.setMinimumSize(new Dimension(width, height));
525     // aptxApp.setFont(Desktop.instance.getFont());
526     // aptxApp.getMainPanel().setFont(Desktop.instance.getFont());
527     String frameTitle = MessageManager.getString("label.aptx_title");
528     File treeFile = aptxApp.getMainPanel().getCurrentTreePanel()
529             .getTreeFile();
530     if (treeFile != null)
531     {
532       frameTitle += MessageManager.formatMessage("label.aptx_title_append",
533               new String[]
534               { treeFile.getAbsolutePath() });
535     }
536     Desktop.addInternalFrame(aptxApp, frameTitle, true, width, height, true,
537             true);
538     return aptxApp;
539
540   }
541
542   public static Map<MainFrame, JalviewBinding> getAllAptxFrames()
543   {
544     return activeAptx;
545   }
546
547
548 }