use of bacter.Conversion in project bacter by tgvaughan.
the class ConvertedEdgeSlide method proposal.
@Override
public double proposal() {
double logHR = 0.0;
if (acg.getTotalConvCount() == 0)
return Double.NEGATIVE_INFINITY;
// Select edge at random:
Conversion conv = chooseConversion();
// Decide whether to move departure or arrival point
boolean moveDeparture = Randomizer.nextBoolean();
// Get current (old) attachment height
double oldHeight;
if (moveDeparture) {
oldHeight = conv.getHeight1();
} else {
oldHeight = conv.getHeight2();
}
// Choose window:
double w = apertureSizeInput.get() * acg.getRoot().getHeight();
// Set new height
double newHeight = oldHeight + (Randomizer.nextDouble() - 0.5) * w;
// Check for boundary violation
if (moveDeparture) {
if (newHeight > conv.getHeight2() || newHeight > acg.getRoot().getHeight())
return Double.NEGATIVE_INFINITY;
} else {
if (newHeight < conv.getHeight1())
return Double.NEGATIVE_INFINITY;
}
// Get node below current (old) attachment point
Node nodeBelow;
if (moveDeparture)
nodeBelow = conv.getNode1();
else
nodeBelow = conv.getNode2();
// Choose node below new attachment point
if (newHeight < oldHeight) {
while (newHeight < nodeBelow.getHeight()) {
if (nodeBelow.isLeaf())
return Double.NEGATIVE_INFINITY;
if (Randomizer.nextBoolean())
nodeBelow = nodeBelow.getLeft();
else
nodeBelow = nodeBelow.getRight();
logHR += -Math.log(0.5);
}
} else {
while (!nodeBelow.isRoot() && newHeight > nodeBelow.getParent().getHeight()) {
nodeBelow = nodeBelow.getParent();
logHR += Math.log(0.5);
}
}
// Write changes back to recombination object
if (moveDeparture) {
conv.setHeight1(newHeight);
conv.setNode1(nodeBelow);
} else {
conv.setHeight2(newHeight);
conv.setNode2(nodeBelow);
}
return logHR;
}
use of bacter.Conversion in project bacter by tgvaughan.
the class ACGCladeSystem method mergeOverlappingConvs.
private List<Conversion> mergeOverlappingConvs(List<Conversion> conversions) {
List<Conversion> mergedList = new ArrayList<>();
List<Conversion> convOrderedByStart = new ArrayList<>(conversions);
convOrderedByStart.sort((o1, o2) -> o1.getStartSite() - o2.getStartSite());
List<Conversion> convOrderedByEnd = new ArrayList<>(conversions);
convOrderedByEnd.sort((o1, o2) -> o1.getEndSite() - o2.getEndSite());
int nActive = 0;
Conversion currentMergedConv = null;
int mergedConvCount = 0;
double mergedConvHeight1 = 0.0;
double mergedConvHeight2 = 0.0;
while (!convOrderedByStart.isEmpty() || !convOrderedByEnd.isEmpty()) {
int nextStart = convOrderedByStart.isEmpty() ? Integer.MAX_VALUE : convOrderedByStart.get(0).getStartSite();
int nextEnd = convOrderedByEnd.isEmpty() ? Integer.MAX_VALUE : convOrderedByEnd.get(0).getEndSite();
if (nextStart < nextEnd) {
nActive += 1;
if (nActive == 1) {
currentMergedConv = convOrderedByStart.get(0).getCopy();
currentMergedConv.acgIndex = convOrderedByStart.get(0).acgIndex;
mergedConvCount = 1;
mergedConvHeight1 = currentMergedConv.getHeight1();
mergedConvHeight2 = currentMergedConv.getHeight2();
} else {
mergedConvCount += 1;
mergedConvHeight1 += convOrderedByStart.get(0).getHeight1();
mergedConvHeight2 += convOrderedByStart.get(0).getHeight2();
}
convOrderedByStart.remove(0);
} else {
nActive -= 1;
if (nActive == 0) {
assert currentMergedConv != null;
currentMergedConv.setEndSite(nextEnd);
currentMergedConv.setHeight1(mergedConvHeight1 / mergedConvCount);
currentMergedConv.setHeight2(mergedConvHeight2 / mergedConvCount);
mergedList.add(currentMergedConv);
}
convOrderedByEnd.remove(0);
}
}
return mergedList;
}
use of bacter.Conversion in project bacter by tgvaughan.
the class ACGCladeSystem method getConversionSummaries.
/**
* Determine contiguous regions on specified locus where the fraction of
* ACGs having a conversion active is greater than the given threshold.
*
* @param from BitSet representing source clade
* @param to BitSet representing destination clade
* @param locus locus to consider
* @param threshold minimum fraction of sampled conversions included
* @return List of regions
*/
public List<ConversionSummary> getConversionSummaries(BitSet from, BitSet to, Locus locus, int nACGs, double threshold) {
BitSetPair bsPair = new BitSetPair(from, to);
List<ConversionSummary> convSummaryList = new ArrayList<>();
// Return empty list if on conversions meet the criteria.
if (!conversionLists.containsKey(bsPair) || !conversionLists.get(bsPair).containsKey(locus))
return convSummaryList;
int thresholdCount = (int) Math.ceil(nACGs * threshold);
List<Conversion> convOrderedByStart = new ArrayList<>();
convOrderedByStart.addAll(conversionLists.get(bsPair).get(locus));
convOrderedByStart.sort((Conversion o1, Conversion o2) -> o1.getStartSite() - o2.getStartSite());
List<Conversion> convOrderedByEnd = new ArrayList<>();
convOrderedByEnd.addAll(conversionLists.get(bsPair).get(locus));
convOrderedByEnd.sort((Conversion o1, Conversion o2) -> o1.getEndSite() - o2.getEndSite());
List<Conversion> activeConversions = new ArrayList<>();
ConversionSummary conversionSummary = null;
BitSet includedACGindices = new BitSet();
while (!convOrderedByStart.isEmpty() || !convOrderedByEnd.isEmpty()) {
int nextStart = convOrderedByStart.isEmpty() ? Integer.MAX_VALUE : convOrderedByStart.get(0).getStartSite();
int nextEnd = convOrderedByEnd.isEmpty() ? Integer.MAX_VALUE : convOrderedByEnd.get(0).getEndSite();
if (nextStart < nextEnd) {
activeConversions.add(convOrderedByStart.get(0));
if (activeConversions.size() >= thresholdCount) {
if (conversionSummary == null) {
conversionSummary = new ConversionSummary();
convSummaryList.add(conversionSummary);
conversionSummary.addConvs(activeConversions);
includedACGindices.clear();
for (Conversion conv : activeConversions) includedACGindices.set(conv.acgIndex);
} else {
conversionSummary.addConv(convOrderedByStart.get(0));
includedACGindices.set(convOrderedByStart.get(0).acgIndex);
}
}
convOrderedByStart.remove(0);
} else {
activeConversions.remove(convOrderedByEnd.get(0));
if (activeConversions.size() == thresholdCount - 1) {
assert conversionSummary != null;
conversionSummary.nIncludedACGs = includedACGindices.cardinality();
conversionSummary = null;
}
convOrderedByEnd.remove(0);
}
}
return convSummaryList;
}
use of bacter.Conversion in project bacter by tgvaughan.
the class CFConvSwapExperiment method connectEdge.
public static void connectEdge(ConversionGraph acg, Node node, Node destEdgeBase, double destTime) {
if (node.isRoot())
throw new IllegalArgumentException("Programmer error: " + "root argument passed to connectEdge().");
Node parent = node.getParent();
if (destEdgeBase.isRoot()) {
parent.addChild(destEdgeBase);
} else {
Node grandParent = destEdgeBase.getParent();
grandParent.removeChild(destEdgeBase);
grandParent.addChild(parent);
parent.addChild(destEdgeBase);
}
parent.setHeight(destTime);
for (Locus locus : acg.getLoci()) {
for (Conversion conv : acg.getConversions(locus)) {
if (conv.getNode1() == destEdgeBase && conv.getHeight1() > destTime)
conv.setNode1(parent);
if (conv.getNode2() == destEdgeBase && conv.getHeight2() > destTime)
conv.setNode2(parent);
}
}
}
use of bacter.Conversion in project bacter by tgvaughan.
the class ConversionGraphStatsLogger method getMeanTractLength.
/**
* Obtain mean length of converted regions described by ACG.
*
* @param acg
* @param locus
* @return mean length, or NaN if ACG has no converted edges.
*/
public static double getMeanTractLength(ConversionGraph acg, Locus locus) {
if (acg.getConvCount(locus) < 1)
return Double.NaN;
double mean = 0;
for (Conversion conv : acg.getConversions(locus)) mean += conv.getEndSite() - conv.getStartSite() + 1;
mean /= acg.getConvCount(locus);
return mean;
}
Aggregations