// Path: src/feast/nexus/NexusWriter.java
// public class NexusWriter {
//     
//     /**
//      * Write an alignment and/or one or more trees to the provided print stream
//      * in Nexus format.
//      * 
//      * @param alignment Alignment to write (may be null)
//      * @param trees Zero or more trees with taxa corresponding to alignment. (May be null)
//      * @param pstream Print stream where output is sent
//      */
//     public static void write(Alignment alignment, List<Tree> trees,
//             PrintStream pstream) {
//         
//         TaxonSet taxonSet = null;
//         if (alignment != null) {
//             taxonSet = new TaxonSet(alignment);
//         } else {
//             if (trees != null && !trees.isEmpty())
//                 taxonSet = trees.get(0).getTaxonset();
//         }
// 
//         NexusBuilder nb = new NexusBuilder();
//         
//         if (taxonSet != null) {
//             nb.append(new TaxaBlock(taxonSet));
//         }
//         
//         if (alignment != null)
//             nb.append(new CharactersBlock(alignment));
//         
//         if (trees != null && !trees.isEmpty())
//             nb.append(new TreesBlock(trees));
//         
//         nb.write(pstream);
//     }
//     
// }


import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import beast.core.BEASTObject;
import beast.core.Input;
import beast.core.Runnable;
import beast.evolution.alignment.Alignment;
import beast.evolution.alignment.FilteredAlignment;
import beast.util.XMLParser;
import feast.nexus.NexusWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/*
 * Copyright (C) 2014 Tim Vaughan <tgvaughan@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package feast.fileio;


/**
 * Simple tool for extracting a sequence alignment from a BEAST 2 input file
 * and writing it out as a Nexus file.  The input file must be a valid
 * BEAST 2 input file, not simply a fragment.  This unfortunately means that
 * we can't handle the XML output of SequenceSimulator.
 *
 * @author Tim Vaughan
 */
public class SequenceExtractor {
    
    public static void main(String[] args) {

        if (args.length<2) {
            System.out.println("Usage: SequenceExtractor "
                    + "beast_input_file.xml output_file.nexus");
            System.exit(0);
        }
            
        try {
            XMLParser parser = new XMLParser();
            Runnable runnable = parser.parseFile(new File(args[0]));
            Set<Alignment> alignments = findAlignments(runnable);
            if (alignments.isEmpty()) {
                System.out.println("No alignments found!");
                System.exit(1);
            }
            if (alignments.size()>1) {
                String prefix;
                int extIndex = args[0].lastIndexOf('.');
                if (extIndex<0) {
                    // No extension
                    prefix = args[0];
                } else {
                    prefix = args[0].substring(0, extIndex);
                }

                for (Alignment alignment : alignments) {
                    if (alignment instanceof FilteredAlignment)
                        continue;

// Path: src/feast/nexus/NexusWriter.java
// public class NexusWriter {
//     
//     /**
//      * Write an alignment and/or one or more trees to the provided print stream
//      * in Nexus format.
//      * 
//      * @param alignment Alignment to write (may be null)
//      * @param trees Zero or more trees with taxa corresponding to alignment. (May be null)
//      * @param pstream Print stream where output is sent
//      */
//     public static void write(Alignment alignment, List<Tree> trees,
//             PrintStream pstream) {
//         
//         TaxonSet taxonSet = null;
//         if (alignment != null) {
//             taxonSet = new TaxonSet(alignment);
//         } else {
//             if (trees != null && !trees.isEmpty())
//                 taxonSet = trees.get(0).getTaxonset();
//         }
// 
//         NexusBuilder nb = new NexusBuilder();
//         
//         if (taxonSet != null) {
//             nb.append(new TaxaBlock(taxonSet));
//         }
//         
//         if (alignment != null)
//             nb.append(new CharactersBlock(alignment));
//         
//         if (trees != null && !trees.isEmpty())
//             nb.append(new TreesBlock(trees));
//         
//         nb.write(pstream);
//     }
//     
// }


// Path: src/feast/fileio/SequenceExtractor.java
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import beast.core.BEASTObject;
import beast.core.Input;
import beast.core.Runnable;
import beast.evolution.alignment.Alignment;
import beast.evolution.alignment.FilteredAlignment;
import beast.util.XMLParser;
import feast.nexus.NexusWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/*
 * Copyright (C) 2014 Tim Vaughan <tgvaughan@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package feast.fileio;


/**
 * Simple tool for extracting a sequence alignment from a BEAST 2 input file
 * and writing it out as a Nexus file.  The input file must be a valid
 * BEAST 2 input file, not simply a fragment.  This unfortunately means that
 * we can't handle the XML output of SequenceSimulator.
 *
 * @author Tim Vaughan
 */
public class SequenceExtractor {
    
    public static void main(String[] args) {

        if (args.length<2) {
            System.out.println("Usage: SequenceExtractor "
                    + "beast_input_file.xml output_file.nexus");
            System.exit(0);
        }
            
        try {
            XMLParser parser = new XMLParser();
            Runnable runnable = parser.parseFile(new File(args[0]));
            Set<Alignment> alignments = findAlignments(runnable);
            if (alignments.isEmpty()) {
                System.out.println("No alignments found!");
                System.exit(1);
            }
            if (alignments.size()>1) {
                String prefix;
                int extIndex = args[0].lastIndexOf('.');
                if (extIndex<0) {
                    // No extension
                    prefix = args[0];
                } else {
                    prefix = args[0].substring(0, extIndex);
                }

                for (Alignment alignment : alignments) {
                    if (alignment instanceof FilteredAlignment)
                        continue;

                    NexusWriter.write(alignment, null,