Search in sources :

Example 11 with ConversionGraph

use of bacter.ConversionGraph in project bacter by tgvaughan.

the class SkylinePopulationFunction method main.

/**
 * Main method for testing.
 *
 * @param args command line arguments (unused)
 */
public static void main(String[] args) throws Exception {
    String acgString = "[&15,0,1.3905355989030808,31,770,1.597708055397074] " + "[&30,931,2.4351280458424904,36,2486,3.78055549386568] " + "[&15,941,2.0439957300083322,38,2364,6.911056700367016] " + "[&36,1091,4.285505683622974,38,2589,9.867725913197855] " + "((((10:0.5385300170206817,(17:0.116794353049212," + "((3:0.039229346597297564,12:0.039229346597297564)23:0.04582913870888949," + "13:0.08505848530618705)24:0.03173586774302495)26:0.4217356639714697)28:1.8114199763246093," + "((8:0.10883006062265468,2:0.10883006062265468)25:0.556428062025291," + "(6:0.5393311342677402,11:0.5393311342677402)29:0.12592698838020555)31:1.6846918706973453)34:1.4536824928125807," + "(1:0.47184545557390367,14:0.47184545557390367)27:3.331787030583968)37:2.9704369411362554," + "(((15:2.0624287390593707,((16:0.01825347077733299,19:0.01825347077733299)21:0.7668749128372041," + "(7:0.008018731329538273,9:0.008018731329538273)20:0.7771096522849988)32:1.2773003554448337)33:0.7487092404613747," + "4:2.8111379795207454)35:0.1331794525400949,((0:0.0243537216663141," + "5:0.0243537216663141)22:0.5681537100482162,18:0.5925074317145304)30:2.35181000034631)36:3.829751995233287)38:0.0";
    ConversionGraph acg = new ConversionGraph();
    acg.initByName("siteCount", 10000, "fromString", acgString);
    SkylinePopulationFunction skyline = new SkylinePopulationFunction();
    skyline.initByName("acg", acg, "popSizes", new RealParameter("1.0 1.0 5.0 1.0 2.0"), "groupSizes", new IntegerParameter("0"), "piecewiseLinear", true);
    try (PrintStream ps = new PrintStream("out.txt")) {
        ps.println("t N intensity intensityInv");
        double t = 0.0;
        while (t < 10) {
            ps.format("%g %g %g %g\n", t, skyline.getPopSize(t), skyline.getIntensity(t), skyline.getInverseIntensity(skyline.getIntensity(t)));
            t += 0.001;
        }
        ps.close();
    }
}
Also used : IntegerParameter(beast.core.parameter.IntegerParameter) PrintStream(java.io.PrintStream) RealParameter(beast.core.parameter.RealParameter) ConversionGraph(bacter.ConversionGraph)

Example 12 with ConversionGraph

use of bacter.ConversionGraph in project bacter by tgvaughan.

the class CFConvSwapExperiment method main.

public static void main(String[] args) throws Exception {
    // Load model
    XMLParser parser = new XMLParser();
    MCMC mcmc = (MCMC) parser.parseFile(new File("inferencePreSimulatedData.xml"));
    State state = mcmc.startStateInput.get();
    state.setStateFileName("problem.state");
    state.restoreFromFile();
    double oldPosterior = state.robustlyCalcPosterior(mcmc.posteriorInput.get());
    ConversionGraph acg = (ConversionGraph) state.getStateNode(0);
    PrintStream ps = new PrintStream("proposal.trees");
    ps.println(acg);
    Node srcNode = acg.getNode(3);
    Node srcNodeS = getSibling(srcNode);
    Node destNode = acg.getNode(6);
    double t_srcNodeP = srcNode.getParent().getHeight();
    disconnectEdge(acg, srcNode);
    Locus locus = acg.getLoci().get(0);
    Conversion convToReplace = acg.getConversions(locus).get(27);
    acg.deleteConversion(convToReplace);
    Node srcNodeP = srcNode.getParent();
    connectEdge(acg, srcNode, destNode, convToReplace.getHeight2());
    // Move Conversions
    for (Conversion conv : acg.getConversions(locus)) {
        boolean moved = false;
        if (conv.getNode1() == srcNode && conv.getHeight1() > srcNodeP.getHeight()) {
            conv.setNode1(destNode);
            moved = true;
        }
        if (conv.getNode2() == srcNode && conv.getHeight2() > srcNodeP.getHeight()) {
            conv.setNode2(destNode);
            moved = true;
        }
        if (moved) {
            while (conv.getHeight1() > conv.getNode1().getParent().getHeight()) conv.setNode1(conv.getNode1().getParent());
            while (conv.getHeight2() > conv.getNode2().getParent().getHeight()) conv.setNode2(conv.getNode2().getParent());
        }
    }
    Conversion convNew = new Conversion();
    convNew.setLocus(locus);
    convNew.setStartSite(0);
    convNew.setEndSite(4000);
    convNew.setNode1(srcNode);
    convNew.setNode2(srcNodeS);
    convNew.setHeight1(convToReplace.getHeight1());
    convNew.setHeight2(t_srcNodeP);
    acg.addConversion(convNew);
    ps.println(acg);
    double newPosterior = state.robustlyCalcPosterior(mcmc.posteriorInput.get());
    System.out.println(newPosterior - oldPosterior);
// state.setStateFileName("proposal.state");
// state.storeToFile(1);
// Open state file
}
Also used : PrintStream(java.io.PrintStream) Node(beast.evolution.tree.Node) XMLParser(beast.util.XMLParser) Locus(bacter.Locus) File(java.io.File) ConversionGraph(bacter.ConversionGraph) Conversion(bacter.Conversion)

Example 13 with ConversionGraph

use of bacter.ConversionGraph in project bacter by tgvaughan.

the class BacterACGLogReader method iterator.

/**
 * Retrieve an iterator for iterating over the ACGs represented
 * by this log file.  Important points
 *
 * 1. The iterator only iterates over as many (non-burnin) ACGs as exist
 * in the file when the ACGLogFileReader is constructed.  This is to avoid
 * problems associated with summarising ongoing analyses.
 *
 * 2. The iterator reuses a single ConversionGraph object during
 * the iteration.  This means that if you want to collect these
 * graphs as the iteration progresses you'll need to use
 * ConversionGraph::copy.
 *
 * @return ConversionGraph iterator
 */
@Override
public Iterator<ConversionGraph> iterator() {
    try {
        reset();
        skipBurnin();
    } catch (IOException e) {
        throw new IllegalStateException(e.getMessage());
    }
    ConversionGraph acg = new ConversionGraph();
    for (Locus locus : getLoci()) acg.lociInput.setValue(locus, acg);
    try {
        acg.initAndValidate();
    } catch (Exception e) {
        throw new IllegalStateException(e.getMessage());
    }
    return new Iterator<ConversionGraph>() {

        boolean lineConsumed = true;

        String nextLine = null;

        int current = 0;

        private String getNextLineNoConsume() {
            if (lineConsumed) {
                try {
                    nextLine = getNextTreeString();
                    lineConsumed = false;
                } catch (IOException e) {
                    throw new IllegalStateException(e.getMessage());
                }
            }
            return nextLine;
        }

        private void printProgressBar() {
            if (current == 0) {
                System.out.println("0%             25%            50%            75%           100%");
                System.out.println("|--------------|--------------|--------------|--------------|");
            }
            if (current < getCorrectedACGCount() - 1) {
                if (current % (int) Math.ceil(getCorrectedACGCount() / 61.0) == 0) {
                    System.out.print("\r");
                    for (int i = 0; i < Math.round(61.0 * current / getCorrectedACGCount()); i++) System.out.print("*");
                    System.out.flush();
                }
            } else {
                System.out.print("\r");
                for (int i = 0; i < 61; i++) System.out.print("*");
                System.out.println();
            }
        }

        @Override
        public boolean hasNext() {
            return current < getCorrectedACGCount() && getNextLineNoConsume() != null;
        }

        @Override
        public ConversionGraph next() {
            String result = getNextLineNoConsume();
            lineConsumed = true;
            acg.fromExtendedNewick(result);
            printProgressBar();
            current += 1;
            return acg;
        }
    };
}
Also used : Iterator(java.util.Iterator) IOException(java.io.IOException) Locus(bacter.Locus) ConversionGraph(bacter.ConversionGraph) IOException(java.io.IOException)

Example 14 with ConversionGraph

use of bacter.ConversionGraph in project bacter by tgvaughan.

the class ConvertedRegionLogger method init.

@Override
public void init(PrintStream out) {
    final ConversionGraph arg = acgInput.get();
    String mainID = (getID() == null || getID().matches(("\\s*"))) ? arg.getID() + ".converted" : getID();
    for (Locus locus : acgInput.get().getLoci()) out.print(mainID + "." + locus.getID() + "\t");
}
Also used : Locus(bacter.Locus) ConversionGraph(bacter.ConversionGraph)

Example 15 with ConversionGraph

use of bacter.ConversionGraph in project bacter by tgvaughan.

the class TrimmedACGLogger method log.

@Override
public void log(long nSample, PrintStream out) {
    ConversionGraph arg = acgInput.get();
    out.print("tree STATE_" + nSample + " = ");
    out.print(arg.getTrimmedExtendedNewick());
}
Also used : ConversionGraph(bacter.ConversionGraph)

Aggregations

ConversionGraph (bacter.ConversionGraph)15 Locus (bacter.Locus)10 Conversion (bacter.Conversion)7 Node (beast.evolution.tree.Node)6 RealParameter (beast.core.parameter.RealParameter)5 SiteModel (beast.evolution.sitemodel.SiteModel)5 JukesCantor (beast.evolution.substitutionmodel.JukesCantor)5 PrintStream (java.io.PrintStream)5 Test (org.junit.Test)5 TaxonSet (beast.evolution.alignment.TaxonSet)3 ConstantPopulation (beast.evolution.tree.coalescent.ConstantPopulation)3 ClusterTree (beast.util.ClusterTree)3 IntegerParameter (beast.core.parameter.IntegerParameter)2 Iterator (java.util.Iterator)2 ACGLogReader (bacter.util.ACGLogReader)1 BacterACGLogReader (bacter.util.BacterACGLogReader)1 COACGLogFileReader (bacter.util.COACGLogFileReader)1 State (beast.core.State)1 Taxon (beast.evolution.alignment.Taxon)1 XMLParser (beast.util.XMLParser)1