// Path: src/feast/nexus/BasicNexusParser.java
// public class BasicNexusParser {
// 
//     BufferedReader fileReader;
// 
//     public class NexusCommand {
//         public String name, args;
// 
//         public NexusCommand(String name, String args) {
//             this.name = name;
//             this.args = args;
//         }
//     }
// 
//     public class NexusBlock {
//         public String blockType;
//         public List<NexusCommand> commands;
// 
//         public NexusBlock(String blockType, List<NexusCommand> commands) {
//             this.blockType = blockType.toLowerCase();
//             this.commands = commands;
//         }
//     }
// 
//     public BasicNexusParser(BufferedReader fileReader) throws IOException {
//         this.fileReader = fileReader;
// 
//         String header = this.fileReader.readLine().trim().toLowerCase();
//         if (!header.equals("#nexus"))
//             throw new IllegalArgumentException("Input file is not a NEXUS file.");
//     }
// 
//     public NexusCommand getNextCommand() throws IOException {
//         StringBuilder cmdStrBuilder = new StringBuilder();
// 
//         while (true) {
//             int nextInt = (char)fileReader.read();
//             if (nextInt<0)
//                 return null;
// 
//             char nextChar = (char)nextInt;
//             if (nextChar == ';')
//                 break;
// 
//             cmdStrBuilder.append(nextChar);
//         }
// 
//         String fullCmdString = cmdStrBuilder.toString();
//         String cmdName, cmdArgs = null;
// 
//         int idx = fullCmdString.indexOf(" ");
//         if (idx<0)
//             cmdName = fullCmdString.trim().toLowerCase();
//         else {
//             cmdName = fullCmdString.substring(0, idx).trim().toLowerCase();
//             cmdArgs = fullCmdString.substring(idx + 1).trim();
//         }
// 
//         return new NexusCommand(cmdName, cmdArgs);
//     }
// 
//     public NexusBlock getNextBlock() throws IOException {
// 
//         NexusCommand command;
//         List<NexusCommand> blockCommandList = new ArrayList<>();
// 
//         command = getNextCommand();
//         while (!command.name.equals("begin")) {
//             command = getNextCommand();
// 
//             if (command == null)
//                 return null;
//         }
// 
//         String blockType = command.args;
// 
//         command = getNextCommand();
//         while (!command.name.equals("end")) {
//             blockCommandList.add(command);
//             command = getNextCommand();
// 
//             if (command == null)
//                 throw new RuntimeException("File ended in the middle of a NEXUS file block.");
//         }
// 
//         return new NexusBlock(blockType, blockCommandList);
//     }
// 
//     public NexusBlock getNextBlockMatching(String blockType) throws IOException {
//         NexusBlock block;
// 
//         do {
//             block = getNextBlock();
//         } while (block != null && !block.blockType.equals(blockType));
// 
//         return block;
//     }
// }


import beast.core.Description;
import beast.core.Input;
import beast.util.TreeParser;
import feast.nexus.BasicNexusParser;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
package feast.fileio;



/**
 * @author Tim Vaughan
 */
@Description("Wrapper around TreeParser to enable reading trees from Nexus files.")
public class TreeFromNexusFile extends TreeParser {

    public Input<String> fileNameInput = new Input<>("fileName", "Name of Nexus file "
            + "containing a tree block Nexus format.", Input.Validate.REQUIRED);

    public Input<Integer> treeIndexInput = new Input<>("treeIndex",
            "Index of tree in tree file (default 0).", 0);

    Map<String,String> translateMap;

    public TreeFromNexusFile() { }

    @Override
    public void initAndValidate() {

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(fileNameInput.get()));
        } catch (FileNotFoundException e) {
            throw new RuntimeException("Input file not found.");
        }

        try {
// Path: src/feast/nexus/BasicNexusParser.java
// public class BasicNexusParser {
// 
//     BufferedReader fileReader;
// 
//     public class NexusCommand {
//         public String name, args;
// 
//         public NexusCommand(String name, String args) {
//             this.name = name;
//             this.args = args;
//         }
//     }
// 
//     public class NexusBlock {
//         public String blockType;
//         public List<NexusCommand> commands;
// 
//         public NexusBlock(String blockType, List<NexusCommand> commands) {
//             this.blockType = blockType.toLowerCase();
//             this.commands = commands;
//         }
//     }
// 
//     public BasicNexusParser(BufferedReader fileReader) throws IOException {
//         this.fileReader = fileReader;
// 
//         String header = this.fileReader.readLine().trim().toLowerCase();
//         if (!header.equals("#nexus"))
//             throw new IllegalArgumentException("Input file is not a NEXUS file.");
//     }
// 
//     public NexusCommand getNextCommand() throws IOException {
//         StringBuilder cmdStrBuilder = new StringBuilder();
// 
//         while (true) {
//             int nextInt = (char)fileReader.read();
//             if (nextInt<0)
//                 return null;
// 
//             char nextChar = (char)nextInt;
//             if (nextChar == ';')
//                 break;
// 
//             cmdStrBuilder.append(nextChar);
//         }
// 
//         String fullCmdString = cmdStrBuilder.toString();
//         String cmdName, cmdArgs = null;
// 
//         int idx = fullCmdString.indexOf(" ");
//         if (idx<0)
//             cmdName = fullCmdString.trim().toLowerCase();
//         else {
//             cmdName = fullCmdString.substring(0, idx).trim().toLowerCase();
//             cmdArgs = fullCmdString.substring(idx + 1).trim();
//         }
// 
//         return new NexusCommand(cmdName, cmdArgs);
//     }
// 
//     public NexusBlock getNextBlock() throws IOException {
// 
//         NexusCommand command;
//         List<NexusCommand> blockCommandList = new ArrayList<>();
// 
//         command = getNextCommand();
//         while (!command.name.equals("begin")) {
//             command = getNextCommand();
// 
//             if (command == null)
//                 return null;
//         }
// 
//         String blockType = command.args;
// 
//         command = getNextCommand();
//         while (!command.name.equals("end")) {
//             blockCommandList.add(command);
//             command = getNextCommand();
// 
//             if (command == null)
//                 throw new RuntimeException("File ended in the middle of a NEXUS file block.");
//         }
// 
//         return new NexusBlock(blockType, blockCommandList);
//     }
// 
//     public NexusBlock getNextBlockMatching(String blockType) throws IOException {
//         NexusBlock block;
// 
//         do {
//             block = getNextBlock();
//         } while (block != null && !block.blockType.equals(blockType));
// 
//         return block;
//     }
// }


// Path: src/feast/fileio/TreeFromNexusFile.java
import beast.core.Description;
import beast.core.Input;
import beast.util.TreeParser;
import feast.nexus.BasicNexusParser;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

package feast.fileio;



/**
 * @author Tim Vaughan
 */
@Description("Wrapper around TreeParser to enable reading trees from Nexus files.")
public class TreeFromNexusFile extends TreeParser {

    public Input<String> fileNameInput = new Input<>("fileName", "Name of Nexus file "
            + "containing a tree block Nexus format.", Input.Validate.REQUIRED);

    public Input<Integer> treeIndexInput = new Input<>("treeIndex",
            "Index of tree in tree file (default 0).", 0);

    Map<String,String> translateMap;

    public TreeFromNexusFile() { }

    @Override
    public void initAndValidate() {

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(fileNameInput.get()));
        } catch (FileNotFoundException e) {
            throw new RuntimeException("Input file not found.");
        }

        try {
            BasicNexusParser nexusParser = new BasicNexusParser(reader);