Search in sources :

Example 6 with MCMC

use of beast.core.MCMC in project beast2 by CompEvol.

the class BEASTObjectDialog method main.

/**
 * rudimentary test *
 */
public static void main(String[] args) {
    BEASTObjectDialog dlg = null;
    try {
        if (args.length == 0) {
            dlg = new BEASTObjectDialog(new BEASTObjectPanel(new MCMC(), Runnable.class, null), null);
        } else if (args[0].equals("-x")) {
            StringBuilder text = new StringBuilder();
            String NL = System.getProperty("line.separator");
            Scanner scanner = new Scanner(new File(args[1]));
            try {
                while (scanner.hasNextLine()) {
                    text.append(scanner.nextLine() + NL);
                }
            } finally {
                scanner.close();
            }
            BEASTInterface beastObject = new beast.util.XMLParser().parseBareFragment(text.toString(), false);
            dlg = new BEASTObjectDialog(new BEASTObjectPanel(beastObject, beastObject.getClass(), null), null);
        } else if (args.length == 1) {
            dlg = new BEASTObjectDialog(new BEASTObjectPanel((BEASTInterface) Class.forName(args[0]).newInstance(), Class.forName(args[0]), null), null);
        } else if (args.length == 2) {
            dlg = new BEASTObjectDialog(new BEASTObjectPanel((BEASTInterface) Class.forName(args[0]).newInstance(), Class.forName(args[1]), null), null);
        } else {
            throw new IllegalArgumentException("Incorrect number of arguments");
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Usage: " + BEASTObjectDialog.class.getName() + " [-x file ] [class [type]]\n" + "where [class] (optional, default MCMC) is a BEASTObject to edit\n" + "and [type] (optional only if class is specified, default Runnable) the type of the BEASTObject.\n" + "for example\n" + "");
        System.exit(1);
    }
    dlg.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    if (dlg.showDialog()) {
        BEASTInterface beastObject = dlg.m_panel.m_beastObject;
        String xml = new XMLProducer().modelToXML(beastObject);
        System.out.println(xml);
    }
}
Also used : Scanner(java.util.Scanner) XMLProducer(beast.util.XMLProducer) MCMC(beast.core.MCMC) BEASTInterface(beast.core.BEASTInterface) File(java.io.File)

Example 7 with MCMC

use of beast.core.MCMC in project beast2 by CompEvol.

the class BEASTObjectPanel method main.

/**
 * rudimentary test *
 */
public static void main(String[] args) {
    init();
    BEASTObjectPanel pluginPanel = null;
    try {
        if (args.length == 0) {
            pluginPanel = new BEASTObjectPanel(new MCMC(), Runnable.class, null);
        } else if (args[0].equals("-x")) {
            StringBuilder text = new StringBuilder();
            String NL = System.getProperty("line.separator");
            Scanner scanner = new Scanner(new File(args[1]));
            try {
                while (scanner.hasNextLine()) {
                    text.append(scanner.nextLine() + NL);
                }
            } finally {
                scanner.close();
            }
            BEASTInterface beastObject = new beast.util.XMLParser().parseBareFragment(text.toString(), false);
            pluginPanel = new BEASTObjectPanel(beastObject, beastObject.getClass(), null);
        } else if (args.length == 1) {
            pluginPanel = new BEASTObjectPanel((BEASTInterface) Class.forName(args[0]).newInstance(), Class.forName(args[0]), null);
        } else if (args.length == 2) {
            pluginPanel = new BEASTObjectPanel((BEASTInterface) Class.forName(args[0]).newInstance(), Class.forName(args[1]), null);
        } else {
            throw new IllegalArgumentException("Incorrect number of arguments");
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Usage: " + BEASTObjectPanel.class.getName() + " [-x file ] [class [type]]\n" + "where [class] (optional, default MCMC) is a BEASTObject to edit\n" + "and [type] (optional only if class is specified, default Runnable) the type of the BEASTObject.\n" + "for example\n" + "");
        System.exit(1);
    }
    pluginPanel.setVisible(true);
    if (pluginPanel.m_bOK) {
        BEASTInterface beastObject = pluginPanel.m_beastObject;
        String xml = new XMLProducer().modelToXML(beastObject);
        System.out.println(xml);
    }
}
Also used : Scanner(java.util.Scanner) XMLProducer(beast.util.XMLProducer) MCMC(beast.core.MCMC) BEASTInterface(beast.core.BEASTInterface) File(java.io.File)

Example 8 with MCMC

use of beast.core.MCMC in project MultiTypeTree by tgvaughan.

the class NSR_Test method test.

@Test
public void test() throws Exception {
    System.out.println("NSR test");
    // Fix seed.
    Randomizer.setSeed(42);
    // Assemble migration model:
    RealParameter rateMatrix = new RealParameter("0.1 0.1");
    RealParameter popSizes = new RealParameter("7.0 7.0");
    SCMigrationModel migModel = new SCMigrationModel();
    migModel.initByName("rateMatrix", rateMatrix, "popSizes", popSizes, "typeSet", new TypeSet("A", "B"));
    // Assemble initial MultiTypeTree
    MultiTypeTree mtTree = new StructuredCoalescentMultiTypeTree();
    mtTree.initByName("typeLabel", "deme", "migrationModel", migModel, "leafTypes", "1 0");
    // Set up state:
    State state = new State();
    state.initByName("stateNode", mtTree);
    // Assemble distribution:
    StructuredCoalescentTreeDensity distribution = new StructuredCoalescentTreeDensity();
    distribution.initByName("migrationModel", migModel, "multiTypeTree", mtTree);
    // Set up operators:
    Operator operatorNSR = new NodeShiftRetype();
    operatorNSR.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel);
    // Set up stat analysis logger:
    MultiTypeTreeStatLogger logger = new MultiTypeTreeStatLogger();
    logger.initByName("multiTypeTree", mtTree, "burninFrac", 0.1, "logEvery", 100);
    // Set up MCMC:
    MCMC mcmc = new MCMC();
    mcmc.initByName("chainLength", "1000000", "state", state, "distribution", distribution, "operator", operatorNSR, "logger", logger);
    // Run MCMC:
    mcmc.run();
    System.out.format("height mean = %s\n", logger.getHeightMean());
    System.out.format("height var = %s\n", logger.getHeightVar());
    System.out.format("height ESS = %s\n", logger.getHeightESS());
    // Compare analysis results with truth:
    boolean withinTol = (logger.getHeightESS() > 2000) && (Math.abs(logger.getHeightMean() - 19.0) < 0.5) && (Math.abs(logger.getHeightVar() - 291) < 30);
    Assert.assertTrue(withinTol);
}
Also used : Operator(beast.core.Operator) MultiTypeTreeStatLogger(multitypetree.util.MultiTypeTreeStatLogger) State(beast.core.State) TypeSet(beast.evolution.tree.TypeSet) StructuredCoalescentTreeDensity(multitypetree.distributions.StructuredCoalescentTreeDensity) MCMC(beast.core.MCMC) RealParameter(beast.core.parameter.RealParameter) StructuredCoalescentMultiTypeTree(beast.evolution.tree.StructuredCoalescentMultiTypeTree) MultiTypeTree(beast.evolution.tree.MultiTypeTree) SCMigrationModel(beast.evolution.tree.SCMigrationModel) StructuredCoalescentMultiTypeTree(beast.evolution.tree.StructuredCoalescentMultiTypeTree) Test(org.junit.Test)

Example 9 with MCMC

use of beast.core.MCMC in project MultiTypeTree by tgvaughan.

the class STXR_NRR_MTU_TS_Test method test.

@Test
public void test() throws Exception {
    System.out.println("STXR_NRR_MTU_TS test");
    // Fix seed.
    Randomizer.setSeed(42);
    // Assemble migration model:
    RealParameter rateMatrix = new RealParameter("0.1 0.1");
    RealParameter popSizes = new RealParameter("7.0 7.0");
    SCMigrationModel migModel = new SCMigrationModel();
    migModel.initByName("rateMatrix", rateMatrix, "popSizes", popSizes, "typeSet", new TypeSet("A", "B"));
    // Assemble initial MultiTypeTree
    MultiTypeTree mtTree = new StructuredCoalescentMultiTypeTree();
    mtTree.initByName("typeLabel", "deme", "migrationModel", migModel, "leafTypes", "1 1 0 0");
    // Set up state:
    State state = new State();
    state.initByName("stateNode", mtTree);
    // Assemble distribution:
    StructuredCoalescentTreeDensity distribution = new StructuredCoalescentTreeDensity();
    distribution.initByName("migrationModel", migModel, "multiTypeTree", mtTree);
    // Set up operators:
    Operator operatorSTXR = new TypedSubtreeExchangeRandom();
    operatorSTXR.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "mu", 0.2);
    Operator operatorNRR = new NodeRetypeRandom();
    operatorNRR.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "mu", 0.2);
    Operator operatorMTU = new MultiTypeUniform();
    operatorMTU.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel);
    Operator operatorMTTS = new MultiTypeTreeScale();
    operatorMTTS.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "scaleFactor", 1.5, "useOldTreeScaler", false);
    // Set up stat analysis logger:
    MultiTypeTreeStatLogger logger = new MultiTypeTreeStatLogger();
    logger.initByName("multiTypeTree", mtTree, "burninFrac", 0.1, "logEvery", 1000);
    // Set up MCMC:
    MCMC mcmc = new MCMC();
    mcmc.initByName("chainLength", "10000000", "state", state, "distribution", distribution, "operator", operatorSTXR, "operator", operatorNRR, "operator", operatorMTU, "operator", operatorMTTS, "logger", logger);
    // Run MCMC:
    mcmc.run();
    System.out.format("height mean = %s\n", logger.getHeightMean());
    System.out.format("height var = %s\n", logger.getHeightVar());
    System.out.format("height ESS = %s\n", logger.getHeightESS());
    // Compare analysis results with truth:
    boolean withinTol = (logger.getHeightESS() > 2000) && (Math.abs(logger.getHeightMean() - 25.8) < 0.5) && (Math.abs(logger.getHeightVar() - 320) < 30);
    Assert.assertTrue(withinTol);
}
Also used : Operator(beast.core.Operator) MultiTypeTreeStatLogger(multitypetree.util.MultiTypeTreeStatLogger) MCMC(beast.core.MCMC) RealParameter(beast.core.parameter.RealParameter) StructuredCoalescentMultiTypeTree(beast.evolution.tree.StructuredCoalescentMultiTypeTree) MultiTypeTree(beast.evolution.tree.MultiTypeTree) SCMigrationModel(beast.evolution.tree.SCMigrationModel) State(beast.core.State) TypeSet(beast.evolution.tree.TypeSet) StructuredCoalescentTreeDensity(multitypetree.distributions.StructuredCoalescentTreeDensity) StructuredCoalescentMultiTypeTree(beast.evolution.tree.StructuredCoalescentMultiTypeTree) Test(org.junit.Test)

Example 10 with MCMC

use of beast.core.MCMC in project MultiTypeTree by tgvaughan.

the class TWBR_TS_Test method test2.

@Test
public void test2() throws Exception {
    System.out.println("TWBR_test 2");
    // Fix seed.
    Randomizer.setSeed(42);
    // Assemble initial MultiTypeTree
    String newickStr = "((1[&deme=1]:1,2[&deme=0]:1)[&deme=0]:1," + "3[&deme=0]:2)[&deme=0]:0;";
    MultiTypeTreeFromNewick mtTree = new MultiTypeTreeFromNewick();
    mtTree.initByName("value", newickStr, "typeLabel", "deme");
    // Assemble migration model:
    RealParameter rateMatrix = new RealParameter("0.1 0.1");
    RealParameter popSizes = new RealParameter("7.0 7.0");
    SCMigrationModel migModel = new SCMigrationModel();
    migModel.initByName("rateMatrix", rateMatrix, "popSizes", popSizes, "typeSet", new TypeSet("A", "B"));
    // Assemble distribution:
    StructuredCoalescentTreeDensity distribution = new StructuredCoalescentTreeDensity();
    distribution.initByName("migrationModel", migModel, "multiTypeTree", mtTree);
    // Set up state:
    State state = new State();
    state.initByName("stateNode", mtTree);
    // Set up operator:
    TypedWilsonBaldingRandom operatorTWBR = new TypedWilsonBaldingRandom();
    operatorTWBR.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "mu", 0.2, "alpha", 0.2);
    Operator operatorMTTS = new MultiTypeTreeScale();
    operatorMTTS.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "scaleFactor", 0.8, "useOldTreeScaler", false);
    // Set up stat analysis logger:
    MultiTypeTreeStatLogger logger = new MultiTypeTreeStatLogger();
    logger.initByName("multiTypeTree", mtTree, "burninFrac", 0.1, "logEvery", 1000);
    // Set up MCMC:
    MCMC mcmc = new MCMC();
    mcmc.initByName("chainLength", "1000000", "state", state, "distribution", distribution, "operator", operatorTWBR, "operator", operatorMTTS, "logger", logger);
    // Run MCMC:
    mcmc.run();
    System.out.format("height mean = %s\n", logger.getHeightMean());
    System.out.format("height var = %s\n", logger.getHeightVar());
    System.out.format("height ESS = %s\n", logger.getHeightESS());
    // Compare analysis results with truth:
    boolean withinTol = (logger.getHeightESS() > 200) && (Math.abs(logger.getHeightMean() - 23) < 1) && (Math.abs(logger.getHeightVar() - 300) < 30);
    Assert.assertTrue(withinTol);
}
Also used : Operator(beast.core.Operator) MultiTypeTreeStatLogger(multitypetree.util.MultiTypeTreeStatLogger) MCMC(beast.core.MCMC) RealParameter(beast.core.parameter.RealParameter) SCMigrationModel(beast.evolution.tree.SCMigrationModel) State(beast.core.State) MultiTypeTreeFromNewick(beast.evolution.tree.MultiTypeTreeFromNewick) TypeSet(beast.evolution.tree.TypeSet) StructuredCoalescentTreeDensity(multitypetree.distributions.StructuredCoalescentTreeDensity) Test(org.junit.Test)

Aggregations

MCMC (beast.core.MCMC)22 State (beast.core.State)10 RealParameter (beast.core.parameter.RealParameter)10 Test (org.junit.Test)10 Operator (beast.core.Operator)9 SCMigrationModel (beast.evolution.tree.SCMigrationModel)9 TypeSet (beast.evolution.tree.TypeSet)9 StructuredCoalescentTreeDensity (multitypetree.distributions.StructuredCoalescentTreeDensity)9 MultiTypeTreeStatLogger (multitypetree.util.MultiTypeTreeStatLogger)9 BEASTInterface (beast.core.BEASTInterface)7 MultiTypeTreeFromNewick (beast.evolution.tree.MultiTypeTreeFromNewick)6 File (java.io.File)5 ArrayList (java.util.ArrayList)4 IntegerParameter (beast.core.parameter.IntegerParameter)3 CompoundDistribution (beast.core.util.CompoundDistribution)3 GenericTreeLikelihood (beast.evolution.likelihood.GenericTreeLikelihood)3 MultiTypeTree (beast.evolution.tree.MultiTypeTree)3 StructuredCoalescentMultiTypeTree (beast.evolution.tree.StructuredCoalescentMultiTypeTree)3 XMLParser (beast.util.XMLParser)3 Input (beast.core.Input)2