b0c2bdb69c0ca40f479b81268c5c253c8d569fb4
[jalview.git] / src / jalview / ext / forester / io / ForesterParser.java
1 package jalview.ext.forester.io;
2
3 import jalview.ext.archaeopteryx.AptxInit;
4 import jalview.ext.archaeopteryx.Tree;
5 import jalview.ext.treeviewer.TreeI;
6 import jalview.ext.treeviewer.TreeParserI;
7
8 import java.io.File;
9 import java.io.IOException;
10
11 import org.forester.io.parsers.PhylogenyParser;
12 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
13 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
14 import org.forester.io.parsers.util.PhylogenyParserException;
15 import org.forester.phylogeny.Phylogeny;
16
17 public class ForesterParser
18         implements TreeParserI
19 {
20   private final PhylogenyParser parser;
21
22   private TreeI[] parsedTrees;
23
24   protected ForesterParser(PhylogenyParser foresterParser, File file)
25           throws PhylogenyParserException, IOException
26   {
27     parser = foresterParser;
28     parser.setSource(file);
29   }
30
31   public static ForesterParser createNexusParser(File file)
32           throws PhylogenyParserException, IOException
33   {
34     NexusPhylogeniesParser nxParser = new NexusPhylogeniesParser();
35     nxParser.setReplaceUnderscores(
36             AptxInit.APTX_CONFIG.isReplaceUnderscoresInNhParsing());
37     nxParser.setIgnoreQuotes(false);
38     return new ForesterParser(nxParser, file);
39   }
40
41   public static ForesterParser createPhyloXmlParser(File file)
42           throws PhylogenyParserException, IOException
43   {
44     if (AptxInit.APTX_CONFIG.isValidatePhyloXmlAgainstSchema())
45     {
46     return new ForesterParser(
47               PhyloXmlParser.createPhyloXmlParserXsdValidating(), file);
48     }
49     else
50     {
51       return new ForesterParser(PhyloXmlParser.createPhyloXmlParser(),
52               file);
53     }
54   }
55
56   // ParserBasedPhylogenyFactory.getInstance().create(foresterParser, source)
57   @Override
58   public TreeI[] parse() throws IOException
59   {
60     Phylogeny[] foresterTrees = parser.parse();
61     parsedTrees = new TreeI[foresterTrees.length];
62
63     for (int i = 0; i < foresterTrees.length; i++)
64     {
65       parsedTrees[i] = new Tree(foresterTrees[i]);
66     }
67     return parsedTrees;
68
69   }
70
71   @Override
72   public void setSource(Object source) throws IOException
73   {
74     parser.setSource(source);
75
76   }
77
78   @Override
79   public String getName()
80   {
81     return parser.getName();
82   }
83
84
85   @Override
86   public TreeI[] getParsedTrees()
87   {
88     return parsedTrees;
89   }
90 }