use of dr.evolution.datatype.DataType in project beast-mcmc by beast-dev.
the class AncestralStatesComponentGenerator method writeCountingParameter.
// END: generate
private void writeCountingParameter(XMLWriter writer, AbstractPartitionData partition, String prefix) {
AncestralStatesComponentOptions component = (AncestralStatesComponentOptions) options.getComponentOptions(AncestralStatesComponentOptions.class);
if (!component.isCountingStates(partition)) {
return;
}
StringBuilder matrix = new StringBuilder();
DataType dataType = partition.getDataType();
int stateCount = dataType.getStateCount();
if (dataType.getType() == DataType.GENERAL) {
PartitionSubstitutionModel substModel = partition.getPartitionSubstitutionModel();
stateCount = substModel.getDiscreteStateSet().size();
}
for (int i = 0; i < stateCount; i++) {
for (int j = 0; j < stateCount; j++) {
if (i == j) {
matrix.append(" 0.0");
} else {
matrix.append(" 1.0");
}
}
}
writer.writeTag("parameter", new Attribute[] { new Attribute.Default<String>("id", partition.getPrefix() + "count"), new Attribute.Default<String>("value", matrix.toString()) }, true);
}
use of dr.evolution.datatype.DataType in project beast-mcmc by beast-dev.
the class ExtractPairs method getPairAlignment.
public Alignment getPairAlignment(int x, int y) {
SimpleAlignment pairAlignment = new SimpleAlignment();
StringBuffer sequence0 = new StringBuffer();
StringBuffer sequence1 = new StringBuffer();
DataType dataType = alignment.getDataType();
int stateCount = dataType.getStateCount();
for (int i = 0; i < alignment.getSiteCount(); i++) {
int s0 = alignment.getState(x, i);
int s1 = alignment.getState(y, i);
char c0 = dataType.getChar(s0);
char c1 = dataType.getChar(s1);
if (s0 < stateCount || s1 < stateCount) {
sequence0.append(c0);
sequence1.append(c1);
}
}
// trim hanging ends on left
int left = 0;
while ((dataType.getState(sequence0.charAt(left)) >= stateCount) || (dataType.getState(sequence1.charAt(left)) >= stateCount)) {
left += 1;
}
// trim hanging ends on right
int right = sequence0.length() - 1;
while ((dataType.getState(sequence0.charAt(right)) >= stateCount) || (dataType.getState(sequence1.charAt(right)) >= stateCount)) {
right -= 1;
}
if (right < left)
return null;
String sequenceString0 = sequence0.substring(left, right + 1);
String sequenceString1 = sequence1.substring(left, right + 1);
pairAlignment.addSequence(new Sequence(alignment.getTaxon(x), sequenceString0));
pairAlignment.addSequence(new Sequence(alignment.getTaxon(y), sequenceString1));
return pairAlignment;
}
use of dr.evolution.datatype.DataType in project beast-mcmc by beast-dev.
the class GapStrippedAlignment method getSequence.
/**
* Very inefficient implementation, use sparingly
* @param sequenceIndex
* @return
*/
public final Sequence getSequence(int sequenceIndex) {
DataType dataType = getDataType();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < alignment.getSiteCount(); i++) {
if (!hasGap[i]) {
buffer.append(dataType.getChar(alignment.getState(sequenceIndex, i)));
}
}
return new Sequence(buffer.toString());
}
use of dr.evolution.datatype.DataType in project beast-mcmc by beast-dev.
the class ImportanceNarrowExchange method setTaxaWeights.
private void setTaxaWeights(PatternList patterns) throws Exception {
final DataType type = patterns.getDataType();
Map<Integer, Integer> counts = new HashMap<Integer, Integer>();
int[] taxaCounts = new int[patterns.getPatternLength()];
for (int nPat = 0; nPat < patterns.getPatternCount(); ++nPat) {
final int[] pattern = patterns.getPattern(nPat);
counts.clear();
for (int s : pattern) {
if (type.isGapState(s) || type.isAmbiguousState(s) || type.isUnknownState(s)) {
continue;
}
if (!counts.containsKey(s)) {
counts.put(s, 0);
}
counts.put(s, counts.get(s) + 1);
}
if (counts.size() <= 1) {
continue;
}
Map.Entry<Integer, Integer> m = null;
for (Map.Entry<Integer, Integer> e : counts.entrySet()) {
if (m == null || e.getValue() > m.getValue()) {
m = e;
}
}
assert m != null;
for (int i = 0; i < pattern.length; ++i) {
final int s = pattern[i];
if (!(type.isGapState(s) || type.isAmbiguousState(s) || type.isUnknownState(s))) {
if (s != m.getKey()) {
taxaCounts[i] += patterns.getPatternWeight(nPat);
}
}
}
}
nodeCounts = new int[tree.getNodeCount()];
Map<Taxon, Integer> taxaWeights = new HashMap<Taxon, Integer>();
for (int i = 0; i < taxaCounts.length; ++i) {
taxaWeights.put(patterns.getTaxon(i), taxaCounts[i]);
}
for (int i = 0; i < tree.getExternalNodeCount(); ++i) {
final NodeRef leaf = tree.getExternalNode(i);
final Taxon nodeTaxon = tree.getNodeTaxon(leaf);
// assert taxaWeights.containsKey(nodeTaxon) : nodeTaxon;
if (!taxaWeights.containsKey(nodeTaxon)) {
throw new Exception("" + nodeTaxon + " in tree " + tree.getId() + " not in patterns" + patterns.getId() + ".");
}
nodeCounts[leaf.getNumber()] = taxaWeights.get(nodeTaxon);
}
}
use of dr.evolution.datatype.DataType in project beast-mcmc by beast-dev.
the class BinarySubstitutionModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Parameter ratesParameter;
XMLObject cxo = xo.getChild(dr.oldevomodelxml.substmodel.GeneralSubstitutionModelParser.FREQUENCIES);
FrequencyModel freqModel = (FrequencyModel) cxo.getChild(FrequencyModel.class);
DataType dataType = freqModel.getDataType();
if (dataType != TwoStates.INSTANCE)
throw new XMLParseException("Frequency model must have binary (two state) data type.");
int relativeTo = 0;
ratesParameter = new Parameter.Default(0);
return new GeneralSubstitutionModel(getParserName(), dataType, freqModel, ratesParameter, relativeTo);
}
Aggregations