use of dr.evolution.sequence.Sequence in project beast-mcmc by beast-dev.
the class BeautiAlignmentBuffer method getStates.
public void getStates(int sequenceIndex, int fromSite, int toSite, byte[] states) {
Sequence sequence = alignment.getSequence(sequenceIndex);
int j = 0;
for (int i = fromSite; i <= toSite; i++) {
states[j] = (byte) sequence.getState(i);
j++;
}
}
use of dr.evolution.sequence.Sequence in project beast-mcmc by beast-dev.
the class SequenceParser method parseXMLObject.
/**
* @return a sequence object based on the XML element it was passed.
*/
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Sequence sequence = new Sequence();
Taxon taxon = (Taxon) xo.getChild(Taxon.class);
DataType dataType = null;
if (xo.hasAttribute(DataType.DATA_TYPE)) {
String dataTypeStr = xo.getStringAttribute(DataType.DATA_TYPE);
if (dataTypeStr.equals(Nucleotides.DESCRIPTION)) {
dataType = Nucleotides.INSTANCE;
} else if (dataTypeStr.equals(AminoAcids.DESCRIPTION)) {
dataType = AminoAcids.INSTANCE;
} else if (dataTypeStr.equals(Codons.DESCRIPTION)) {
dataType = Codons.UNIVERSAL;
} else if (dataTypeStr.equals(TwoStates.DESCRIPTION)) {
dataType = TwoStates.INSTANCE;
}
}
StringBuffer seqBuf = new StringBuffer();
for (int i = 0; i < xo.getChildCount(); i++) {
Object child = xo.getChild(i);
if (child instanceof String) {
StringTokenizer st = new StringTokenizer((String) child);
while (st.hasMoreTokens()) {
seqBuf.append(st.nextToken());
}
}
}
// We really need to filter the input string to check for illegal characters.
// Perhaps sequence.setSequenceString could throw an exception if any characters
// don't fit the dataType.
String sequenceString = seqBuf.toString();
if (sequenceString.length() == 0) {
throw new XMLParseException("Sequence data missing from sequence element!");
}
if (dataType != null) {
sequence.setDataType(dataType);
}
sequence.setSequenceString(sequenceString);
sequence.setTaxon(taxon);
return sequence;
}
use of dr.evolution.sequence.Sequence in project beast-mcmc by beast-dev.
the class BeagleSequenceSimulator method compileAlignment.
// END: SimulatePartitionCallable class
private SimpleAlignment compileAlignment() {
SimpleAlignment simpleAlignment = new SimpleAlignment();
simpleAlignment.setReportCountStatistics(false);
simpleAlignment.setDataType(dataType);
LinkedHashMap<Taxon, int[]> alignmentMap = new LinkedHashMap<Taxon, int[]>();
// compile the alignment
for (Partition partition : partitions) {
Map<Taxon, int[]> sequenceMap = partition.getTaxonSequencesMap();
Iterator<Entry<Taxon, int[]>> iterator = sequenceMap.entrySet().iterator();
while (iterator.hasNext()) {
Entry<Taxon, int[]> pairs = (Entry<Taxon, int[]>) iterator.next();
Taxon taxon = pairs.getKey();
int[] partitionSequence = pairs.getValue();
if (alignmentMap.containsKey(taxon)) {
int j = 0;
for (int i = partition.from; i <= partition.to; i += partition.every) {
alignmentMap.get(taxon)[i] = partitionSequence[j];
j++;
}
// END: i loop
} else {
int[] sequence = new int[siteCount];
// dirty solution for gaps when taxa between the tree
// topologies don't match
Arrays.fill(sequence, gapFlag);
int j = 0;
for (int i = partition.from; i <= partition.to; i += partition.every) {
sequence[i] = partitionSequence[j];
j++;
}
// END: i loop
alignmentMap.put(taxon, sequence);
}
// END: key check
}
// END: iterate seqMap
}
// END: partitions loop
Iterator<Entry<Taxon, int[]>> iterator = alignmentMap.entrySet().iterator();
while (iterator.hasNext()) {
Entry<Taxon, int[]> pairs = (Entry<Taxon, int[]>) iterator.next();
Taxon taxon = (Taxon) pairs.getKey();
int[] intSequence = (int[]) pairs.getValue();
Sequence sequence = //
Utils.intArray2Sequence(//
taxon, //
intSequence, //
gapFlag, dataType);
// sequence.setDataType(dataType);
simpleAlignment.addSequence(sequence);
iterator.remove();
}
return simpleAlignment;
}
use of dr.evolution.sequence.Sequence in project beast-mcmc by beast-dev.
the class DataModelImporter method createAlignment.
private Map createAlignment(Alignment alignment) {
Map a = new HashMap();
a.put("id", (alignment.getId() != null ? alignment.getId() : "alignment"));
List<Map> ss = new ArrayList<Map>();
for (int i = 0; i < alignment.getSequenceCount(); i++) {
Sequence sequence = alignment.getSequence(i);
ss.add(createSequence(sequence));
}
a.put("sequences", ss);
return a;
}
use of dr.evolution.sequence.Sequence in project beast-mcmc by beast-dev.
the class PartitionParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
int from = 0;
int to = -1;
int every = xo.getAttribute(EVERY, 1);
if (xo.hasAttribute(FROM)) {
from = xo.getIntegerAttribute(FROM) - 1;
if (from < 0) {
throw new XMLParseException("Illegal 'from' attribute in patterns element");
}
}
if (xo.hasAttribute(TO)) {
to = xo.getIntegerAttribute(TO) - 1;
if (to < 0 || to < from) {
throw new XMLParseException("Illegal 'to' attribute in patterns element");
}
}
if (every <= 0) {
throw new XMLParseException("Illegal 'every' attribute in patterns element");
}
// END: every check
TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
GammaSiteRateModel siteModel = (GammaSiteRateModel) xo.getChild(GammaSiteRateModel.class);
FrequencyModel freqModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
Sequence rootSequence = (Sequence) xo.getChild(Sequence.class);
BranchRateModel rateModel = (BranchRateModel) xo.getChild(BranchRateModel.class);
if (rateModel == null) {
rateModel = new DefaultBranchRateModel();
}
BranchModel branchModel = (BranchModel) xo.getChild(BranchModel.class);
if (branchModel == null) {
SubstitutionModel substitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
branchModel = new HomogeneousBranchModel(substitutionModel);
}
Partition partition = new Partition(tree, branchModel, siteModel, rateModel, freqModel, from, to, every);
if (rootSequence != null) {
partition.setRootSequence(rootSequence);
}
return partition;
}
Aggregations