Search in sources :

Example 36 with DataType

use of dr.evolution.datatype.DataType 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);
    DataType dataType = null;
    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");
    }
    if (xo.hasAttribute(DATA_TYPE)) {
        dataType = DataTypeUtils.getDataType(xo);
    }
    TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
    GammaSiteRateModel siteModel = (GammaSiteRateModel) xo.getChild(GammaSiteRateModel.class);
    // FrequencyModel freqModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
    FrequencyModel freqModel;
    List<FrequencyModel> freqModels = new ArrayList<FrequencyModel>();
    for (int i = 0; i < xo.getChildCount(); i++) {
        Object cxo = xo.getChild(i);
        if (cxo instanceof FrequencyModel) {
            freqModels.add((FrequencyModel) cxo);
        }
    }
    if (freqModels.size() == 1) {
        freqModel = freqModels.get(0);
    } else {
        double[] freqParameter = new double[freqModels.size() * freqModels.get(0).getFrequencyCount()];
        int index = 0;
        for (int i = 0; i < freqModels.size(); i++) {
            for (int j = 0; j < freqModels.get(i).getFrequencyCount(); j++) {
                freqParameter[index] = (freqModels.get(i).getFrequency(j)) / freqModels.size();
                index++;
            }
        }
        freqModel = new FrequencyModel(dataType, freqParameter);
    }
    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, dataType);
    if (rootSequence != null) {
        partition.setRootSequence(rootSequence);
    }
    return partition;
}
Also used : FrequencyModel(dr.evomodel.substmodel.FrequencyModel) Partition(dr.app.beagle.tools.Partition) ArrayList(java.util.ArrayList) HomogeneousBranchModel(dr.evomodel.branchmodel.HomogeneousBranchModel) GammaSiteRateModel(dr.evomodel.siteratemodel.GammaSiteRateModel) Sequence(dr.evolution.sequence.Sequence) BranchModel(dr.evomodel.branchmodel.BranchModel) HomogeneousBranchModel(dr.evomodel.branchmodel.HomogeneousBranchModel) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) TreeModel(dr.evomodel.tree.TreeModel) BranchRateModel(dr.evomodel.branchratemodel.BranchRateModel) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) DataType(dr.evolution.datatype.DataType) XMLObject(dr.xml.XMLObject) XMLParseException(dr.xml.XMLParseException)

Example 37 with DataType

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);
}
Also used : Attribute(dr.util.Attribute) DataType(dr.evolution.datatype.DataType)

Example 38 with DataType

use of dr.evolution.datatype.DataType in project beast-mcmc by beast-dev.

the class SubstitutionModelGenerator method writeSubstitutionSiteModel.

/**
 * Writes the substitution model to XML.
 *
 * @param writer the writer
 * @param model  the partition model to write in BEAST XML
 */
public void writeSubstitutionSiteModel(PartitionSubstitutionModel model, XMLWriter writer) {
    DataType dataType = model.getDataType();
    String dataTypeDescription = dataType.getDescription();
    switch(dataType.getType()) {
        case DataType.NUCLEOTIDES:
            if (model.isUnlinkedSubstitutionModel()) {
                for (int i = 1; i <= model.getCodonPartitionCount(); i++) {
                    switch(model.getNucSubstitutionModel()) {
                        case JC:
                            writeJCModel(i, writer, model);
                            break;
                        case HKY:
                            writeHKYModel(i, writer, model);
                            break;
                        case TN93:
                            writeTN93Model(i, writer, model);
                            break;
                        case GTR:
                            writeGTRModel(i, writer, model);
                            break;
                        default:
                            throw new IllegalArgumentException("unknown substition model type");
                    }
                }
            } else {
                switch(model.getNucSubstitutionModel()) {
                    case JC:
                        writeJCModel(-1, writer, model);
                        break;
                    case HKY:
                        writeHKYModel(-1, writer, model);
                        break;
                    case TN93:
                        writeTN93Model(-1, writer, model);
                        break;
                    case GTR:
                        writeGTRModel(-1, writer, model);
                        break;
                    default:
                        throw new IllegalArgumentException("unknown substitution model type");
                }
            }
            // ****************** Site Model *****************
            if (model.getCodonPartitionCount() > 1) {
                // model.getCodonHeteroPattern() != null) {
                for (int i = 1; i <= model.getCodonPartitionCount(); i++) {
                    writeNucSiteModel(i, writer, model);
                }
                writer.println();
            } else {
                writeNucSiteModel(-1, writer, model);
            }
            break;
        case DataType.AMINO_ACIDS:
            // Amino Acid model
            String aaModel = model.getAaSubstitutionModel().getXMLName();
            writer.writeComment("The " + aaModel + " substitution model");
            writer.writeTag(EmpiricalAminoAcidModelParser.EMPIRICAL_AMINO_ACID_MODEL, new Attribute[] { new Attribute.Default<String>(XMLParser.ID, model.getPrefix() + "aa"), new Attribute.Default<String>("type", aaModel) }, true);
            // ****************** Site Model *****************
            writeAASiteModel(writer, model);
            break;
        case DataType.TWO_STATES:
        case DataType.COVARION:
            switch(model.getBinarySubstitutionModel()) {
                case BIN_DOLLO:
                    return;
                case BIN_SIMPLE:
                    writeBinarySimpleModel(writer, model);
                    break;
                case BIN_COVARION:
                    writeBinaryCovarionModel(writer, model);
                    break;
            }
            // ****************** Site Model *****************
            writeTwoStateSiteModel(writer, model);
            break;
        case DataType.GENERAL:
        case DataType.CONTINUOUS:
            // handled by component
            break;
        case DataType.MICRO_SAT:
            writeMicrosatSubstModel(model, writer);
            break;
        default:
            throw new IllegalArgumentException("Unknown data type");
    }
}
Also used : Attribute(dr.util.Attribute) DataType(dr.evolution.datatype.DataType)

Example 39 with DataType

use of dr.evolution.datatype.DataType in project beast-mcmc by beast-dev.

the class BeagleSeqSimTest method ancestralSequenceTree.

// END: simulateRandomBranchAssignment
static void ancestralSequenceTree() {
    try {
        LinkedHashMap<NodeRef, int[]> sequenceMap = new LinkedHashMap<NodeRef, int[]>();
        DataType dataType = Nucleotides.INSTANCE;
        // create tree
        NewickImporter importer = new NewickImporter("(SimSeq1:73.7468,(SimSeq2:25.256989999999995,SimSeq3:45.256989999999995):18.48981);");
        Tree tree = importer.importTree(null);
        DefaultTreeModel treeModel = new DefaultTreeModel(tree);
        for (NodeRef node : treeModel.getNodes()) {
            if (treeModel.isExternal(node)) {
                int[] seq = new int[] { 1, 1, 1 };
                sequenceMap.put(node, seq);
            } else {
                int[] seq = new int[] { 2, 2, 2 };
                sequenceMap.put(node, seq);
            }
        }
        // END: nodes loop
        AncestralSequenceTrait ancestralSequence = new AncestralSequenceTrait(sequenceMap, dataType);
        TreeTraitProvider[] treeTraitProviders = new TreeTraitProvider[] { ancestralSequence };
        StringBuffer buffer = new StringBuffer();
        NumberFormat format = NumberFormat.getNumberInstance(Locale.ENGLISH);
        boolean useTipLabels = true;
        // 
        TreeUtils.newick(// 
        treeModel, // 
        treeModel.getRoot(), // 
        useTipLabels, // 
        TreeUtils.BranchLengthType.LENGTHS_AS_TIME, // 
        format, // 
        null, // 
        treeTraitProviders, null, buffer);
        System.out.println(buffer);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ImportException e) {
        e.printStackTrace();
    }
// END: try-catch
}
Also used : TreeTraitProvider(dr.evolution.tree.TreeTraitProvider) DefaultTreeModel(dr.evomodel.tree.DefaultTreeModel) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) ImportException(dr.evolution.io.Importer.ImportException) NodeRef(dr.evolution.tree.NodeRef) NewickImporter(dr.evolution.io.NewickImporter) DataType(dr.evolution.datatype.DataType) Tree(dr.evolution.tree.Tree) NumberFormat(java.text.NumberFormat)

Example 40 with DataType

use of dr.evolution.datatype.DataType in project beast-mcmc by beast-dev.

the class NexusExporter method exportAlignment.

public String exportAlignment(Alignment alignment) throws IOException, IllegalArgumentException {
    StringBuffer buffer = new StringBuffer();
    DataType dataType = null;
    int seqLength = 0;
    for (int i = 0; i < alignment.getSequenceCount(); i++) {
        Sequence sequence = alignment.getSequence(i);
        if (sequence.getLength() > seqLength) {
            seqLength = sequence.getLength();
        }
        if (dataType == null) {
            dataType = sequence.getDataType();
        } else if (dataType != sequence.getDataType()) {
            throw new RuntimeException("Sequences must have the same data type.");
        }
    // END: dataType check
    }
    // END: sequences loop
    buffer.append("#NEXUS\n");
    buffer.append("begin data;\n");
    buffer.append("\tdimensions" + " " + "ntax=" + alignment.getTaxonCount() + " " + "nchar=" + seqLength + ";\n");
    buffer.append("\tformat datatype=" + dataType.getDescription() + " missing=" + DataType.UNKNOWN_CHARACTER + " gap=" + DataType.GAP_CHARACTER + ";\n");
    buffer.append("\tmatrix\n");
    int maxRowLength = seqLength;
    for (int n = 0; n < Math.ceil((double) seqLength / maxRowLength); n++) {
        for (int i = 0; i < alignment.getSequenceCount(); i++) {
            Sequence sequence = alignment.getSequence(i);
            StringBuilder builder = new StringBuilder("\t");
            appendTaxonName(sequence.getTaxon(), builder);
            String sequenceString = sequence.getSequenceString();
            builder.append("\t").append(sequenceString.subSequence(n * maxRowLength, Math.min((n + 1) * maxRowLength, sequenceString.length())));
            int shortBy = Math.min(Math.min(n * maxRowLength, seqLength) - sequence.getLength(), maxRowLength);
            if (shortBy > 0) {
                for (int j = 0; j < shortBy; j++) {
                    builder.append(DataType.GAP_CHARACTER);
                }
            }
            buffer.append(builder + "\n");
        }
    // END: sequences loop
    }
    buffer.append(";\nend;");
    return buffer.toString();
}
Also used : DataType(dr.evolution.datatype.DataType) Sequence(dr.evolution.sequence.Sequence)

Aggregations

DataType (dr.evolution.datatype.DataType)66 Parameter (dr.inference.model.Parameter)26 FrequencyModel (dr.evomodel.substmodel.FrequencyModel)11 FrequencyModel (dr.oldevomodel.substmodel.FrequencyModel)11 ArrayList (java.util.ArrayList)8 Sequence (dr.evolution.sequence.Sequence)7 PartitionSubstitutionModel (dr.app.beauti.options.PartitionSubstitutionModel)4 PatternList (dr.evolution.alignment.PatternList)4 GeneralDataType (dr.evolution.datatype.GeneralDataType)4 Taxon (dr.evolution.util.Taxon)4 TaxonList (dr.evolution.util.TaxonList)4 GeneralSubstitutionModel (dr.evomodel.substmodel.GeneralSubstitutionModel)4 SVSComplexSubstitutionModel (dr.oldevomodel.substmodel.SVSComplexSubstitutionModel)4 Attribute (dr.util.Attribute)4 Codons (dr.evolution.datatype.Codons)3 HiddenDataType (dr.evolution.datatype.HiddenDataType)3 NodeRef (dr.evolution.tree.NodeRef)3 Tree (dr.evolution.tree.Tree)3 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)3 SubstitutionModel (dr.evomodel.substmodel.SubstitutionModel)3