Search in sources :

Example 1 with UncertainSequence

use of dr.evolution.sequence.UncertainSequence in project beast-mcmc by beast-dev.

the class SimpleAlignment method getUncertainSitePattern.

@Override
public double[][] getUncertainSitePattern(int siteIndex) {
    if (areUncertain()) {
        double[][] pattern = new double[getSequenceCount()][];
        for (int i = 0; i < getSequenceCount(); ++i) {
            Sequence seq = getSequence(i);
            if (siteIndex > seq.getLength()) {
                pattern[i] = new double[dataType.getStateCount()];
                Arrays.fill(pattern[i], 1.0);
            } else {
                if (seq instanceof UncertainSequence) {
                    pattern[i] = ((UncertainSequence) seq).getUncertainPattern(siteIndex);
                } else {
                    pattern[i] = new double[dataType.getStateCount()];
                    int[] states = dataType.getStates(seq.getState(siteIndex));
                    for (int state : states) {
                        pattern[i][state] = 1.0;
                    }
                }
            }
        }
        return pattern;
    } else {
        throw new UnsupportedOperationException("getUncertainSitePattern not implemented yet");
    }
}
Also used : UncertainSequence(dr.evolution.sequence.UncertainSequence) Sequence(dr.evolution.sequence.Sequence) UncertainSequence(dr.evolution.sequence.UncertainSequence)

Example 2 with UncertainSequence

use of dr.evolution.sequence.UncertainSequence in project beast-mcmc by beast-dev.

the class AlignmentParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    final SimpleAlignment alignment = new SimpleAlignment();
    final DataType dataType = DataTypeUtils.getDataType(xo);
    if (dataType == null) {
        throw new XMLParseException("dataType attribute expected for alignment element");
    }
    alignment.setDataType(dataType);
    for (int i = 0; i < xo.getChildCount(); i++) {
        final Object child = xo.getChild(i);
        if (child instanceof UncertainSequence) {
            alignment.addSequence((UncertainSequence) child);
        } else if (child instanceof Sequence) {
            alignment.addSequence((Sequence) child);
        } else if (child instanceof DataType) {
        // already dealt with
        } else {
            throw new XMLParseException("Unknown child element found in alignment");
        }
    }
    final Logger logger = Logger.getLogger("dr.evoxml");
    logger.info("\nRead alignment" + (xo.hasAttribute(XMLParser.ID) ? ": " + xo.getId() : "") + "\n  Sequences = " + alignment.getSequenceCount() + "\n      Sites = " + alignment.getSiteCount() + "\n   Datatype = " + alignment.getDataType().getDescription());
    return alignment;
}
Also used : SimpleAlignment(dr.evolution.alignment.SimpleAlignment) DataType(dr.evolution.datatype.DataType) UncertainSequence(dr.evolution.sequence.UncertainSequence) Sequence(dr.evolution.sequence.Sequence) UncertainSequence(dr.evolution.sequence.UncertainSequence) Logger(java.util.logging.Logger)

Example 3 with UncertainSequence

use of dr.evolution.sequence.UncertainSequence 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 {
    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!");
    }
    Sequence sequence;
    if (UncertainSequence.containsWeights(sequenceString)) {
        sequence = new UncertainSequence();
    } else {
        sequence = new Sequence();
    }
    if (dataType != null) {
        sequence.setDataType(dataType);
    }
    sequence.setSequenceString(sequenceString);
    sequence.setTaxon(taxon);
    return sequence;
}
Also used : StringTokenizer(java.util.StringTokenizer) Taxon(dr.evolution.util.Taxon) UncertainSequence(dr.evolution.sequence.UncertainSequence) Sequence(dr.evolution.sequence.Sequence) UncertainSequence(dr.evolution.sequence.UncertainSequence)

Aggregations

Sequence (dr.evolution.sequence.Sequence)3 UncertainSequence (dr.evolution.sequence.UncertainSequence)3 SimpleAlignment (dr.evolution.alignment.SimpleAlignment)1 DataType (dr.evolution.datatype.DataType)1 Taxon (dr.evolution.util.Taxon)1 StringTokenizer (java.util.StringTokenizer)1 Logger (java.util.logging.Logger)1