Search in sources :

Example 31 with Locus

use of bacter.Locus 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 32 with Locus

use of bacter.Locus in project bacter by tgvaughan.

the class CFOperator method expandConversions.

/**
 * Take length of new edge above srcNode that is greater than the
 * original height of srcNode.parent and shifts a random fraction of
 * conversion attachments to it from the lineage above destNode.
 *
 * In the case that destNode was the root, the conversions starting
 * above destNode are drawn from the prior.
 *
 * Assumes topology has not yet been altered.
 *
 * @param srcNode source node for the move
 * @param destNode dest node for the move
 * @param destTime new time drawn for srcNode.P.
 * @return log probability of new conversion configuration.
 */
protected double expandConversions(Node srcNode, Node destNode, double destTime) {
    double logP = 0.0;
    double volatileHeight = acg.getRoot().getHeight();
    boolean forwardRootMove = destTime > volatileHeight;
    Node node = srcNode.getParent();
    while (node != null) {
        for (Locus locus : acg.getLoci()) {
            for (Conversion conv : acg.getConversions(locus)) {
                if (conv.getNode1() == node && conv.getHeight1() < destTime) {
                    if (Randomizer.nextBoolean())
                        conv.setNode1(srcNode);
                    logP += Math.log(0.5);
                }
                if (conv.getNode2() == node && conv.getHeight2() < destTime) {
                    if (Randomizer.nextBoolean())
                        conv.setNode2(srcNode);
                    logP += Math.log(0.5);
                }
            }
        }
        node = node.getParent();
    }
    // Apply topology modifications.
    disconnectEdge(srcNode);
    connectEdge(srcNode, destNode, destTime);
    // this was a forward root move
    if (forwardRootMove) {
        acg.setRoot(srcNode.getParent());
        double L = 2.0 * (destTime - volatileHeight);
        double Nexp = L * rhoInput.get().getValue() * (acg.getTotalSequenceLength() + acg.getLoci().size() * (deltaInput.get().getValue() - 1.0));
        int N = (int) Randomizer.nextPoisson(Nexp);
        // Factorial cancels
        logP += -Nexp + N * Math.log(Nexp);
        for (int i = 0; i < N; i++) {
            Conversion conv = new Conversion();
            double u = Randomizer.nextDouble() * L;
            if (u < 0.5 * L) {
                conv.setNode1(destNode);
                conv.setHeight1(volatileHeight + u);
            } else {
                conv.setNode1(srcNode);
                conv.setHeight1(volatileHeight + u - 0.5 * L);
            }
            logP += Math.log(1.0 / L) + drawAffectedRegion(conv) + coalesceEdge(conv);
            acg.addConversion(conv);
        }
    }
    return logP;
}
Also used : Node(beast.evolution.tree.Node) Locus(bacter.Locus) Conversion(bacter.Conversion)

Example 33 with Locus

use of bacter.Locus in project bacter by tgvaughan.

the class ConversionCreationOperator method drawAffectedRegionRestricted.

/**
 * Select affected region when region endpoint restriction is in place.
 *
 * @param conv Conversion object where these sites are stored.
 * @return log probability density of chosen attachment.
 */
protected double drawAffectedRegionRestricted(Conversion conv) {
    int locusIdx = Randomizer.randomChoicePDF(relativeLocusSizes);
    Locus locus = acg.getLoci().get(locusIdx);
    conv.setLocus(locus);
    conv.setStartSite(0);
    conv.setEndSite(locus.getSiteCount() - 1);
    return Math.log(relativeLocusSizes[locusIdx]);
}
Also used : Locus(bacter.Locus)

Example 34 with Locus

use of bacter.Locus in project bacter by tgvaughan.

the class ConversionGraphStatsLogger method init.

@Override
public void init(PrintStream out) {
    String id = acg.getID();
    if (id == null || id.matches("\\s*"))
        id = "acg";
    out.print(id + ".CFheight\t" + id + ".CFlength\t" + id + ".nConv\t" + id + ".nUselessConvs\t" + id + ".meanEdgeLength\t" + id + ".meanDepartureHeight\t");
    for (Locus loci : acg.getLoci()) {
        String lid = id + "." + loci.getID();
        out.print(lid + ".meanTractLength\t" + lid + ".meanRegionLength\t" + lid + ".meanStartSite\t" + lid + ".meanEndSite\t" + lid + ".randomStartSite\t" + lid + ".randomEndSite\t");
    }
}
Also used : Locus(bacter.Locus)

Example 35 with Locus

use of bacter.Locus in project bacter by tgvaughan.

the class ConversionGraphStatsLogger method getMeanDepartureHeight.

/**
 * Obtain mean height of point of departure of converted edges
 * in ACG.
 *
 * @param acg
 * @return mean height, or NaN if ACG has no converted edges
 */
public static double getMeanDepartureHeight(ConversionGraph acg) {
    if (acg.getTotalConvCount() < 1)
        return Double.NaN;
    double mean = 0.0;
    for (Locus locus : acg.getLoci()) {
        for (Conversion conv : acg.getConversions(locus)) {
            mean += conv.getHeight1();
        }
    }
    mean /= acg.getTotalConvCount();
    return mean;
}
Also used : Locus(bacter.Locus) Conversion(bacter.Conversion)

Aggregations

Locus (bacter.Locus)37 Conversion (bacter.Conversion)27 Node (beast.evolution.tree.Node)17 ConversionGraph (bacter.ConversionGraph)10 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 RealParameter (beast.core.parameter.RealParameter)6 TaxonSet (beast.evolution.alignment.TaxonSet)5 SiteModel (beast.evolution.sitemodel.SiteModel)5 JukesCantor (beast.evolution.substitutionmodel.JukesCantor)5 ConstantPopulation (beast.evolution.tree.coalescent.ConstantPopulation)5 PrintStream (java.io.PrintStream)4 ClusterTree (beast.util.ClusterTree)3 SimulatedACG (bacter.model.SimulatedACG)2 NexusBuilder (feast.nexus.NexusBuilder)2 TaxaBlock (feast.nexus.TaxaBlock)2 FileNotFoundException (java.io.FileNotFoundException)2 Iterator (java.util.Iterator)2 CFEventList (bacter.CFEventList)1 ACGCoalescent (bacter.model.ACGCoalescent)1