Search in sources :

Example 16 with Alignment

use of dr.evolution.alignment.Alignment in project beast-mcmc by beast-dev.

the class BEAUTiImporter method importFastaFile.

// FASTA
private void importFastaFile(File file) throws IOException, ImportException {
    try {
        FileReader reader = new FileReader(file);
        FastaImporter importer = new FastaImporter(reader, Nucleotides.INSTANCE);
        Alignment alignment = importer.importAlignment();
        reader.close();
        setData(file.getName(), alignment, alignment, null, null, null, null);
    } catch (ImportException e) {
        throw new ImportException(e.getMessage());
    } catch (IOException e) {
        throw new IOException(e.getMessage());
    }
}
Also used : ImportException(dr.evolution.io.Importer.ImportException) Alignment(dr.evolution.alignment.Alignment) SimpleAlignment(dr.evolution.alignment.SimpleAlignment) FastaImporter(dr.evolution.io.FastaImporter)

Example 17 with Alignment

use of dr.evolution.alignment.Alignment in project beast-mcmc by beast-dev.

the class NexusImporter method importAlignment.

// **************************************************************
// SequenceImporter IMPLEMENTATION
// **************************************************************
/**
     * importAlignment.
     */
public Alignment importAlignment() throws IOException, Importer.ImportException {
    boolean done = false;
    TaxonList taxonList = null;
    Alignment alignment = null;
    while (!done) {
        try {
            NexusImporter.NexusBlock block = findNextBlock();
            if (block == NexusImporter.TAXA_BLOCK) {
                taxonList = readTaxaBlock();
            } else if (block == NexusImporter.CALIBRATION_BLOCK) {
                if (taxonList == null) {
                    throw new MissingBlockException("TAXA block is missing");
                }
                readCalibrationBlock(taxonList);
            } else if (block == NexusImporter.CHARACTERS_BLOCK) {
                if (taxonList == null) {
                    throw new MissingBlockException("TAXA block is missing");
                }
                alignment = readCharactersBlock(taxonList);
                done = true;
            } else if (block == NexusImporter.DATA_BLOCK) {
                // A data block doesn't need a taxon block before it
                // but if one exists then it will use it.
                alignment = readDataBlock();
                done = true;
            } else {
            // Ignore the block..
            }
        } catch (EOFException ex) {
            done = true;
        }
    }
    if (alignment == null) {
        throw new MissingBlockException("DATA or CHARACTERS block is missing");
    }
    return alignment;
}
Also used : Alignment(dr.evolution.alignment.Alignment) SimpleAlignment(dr.evolution.alignment.SimpleAlignment)

Example 18 with Alignment

use of dr.evolution.alignment.Alignment in project beast-mcmc by beast-dev.

the class BEAUTiImporter method importBEASTFile.

// xml
private void importBEASTFile(File file) throws IOException, ImportException, JDOMException {
    try {
        FileReader reader = new FileReader(file);
        BeastImporter importer = new BeastImporter(reader);
        List<TaxonList> taxonLists = new ArrayList<TaxonList>();
        List<Alignment> alignments = new ArrayList<Alignment>();
        importer.importBEAST(taxonLists, alignments);
        TaxonList taxa = taxonLists.get(0);
        int count = 1;
        for (Alignment alignment : alignments) {
            String name = file.getName();
            if (alignment.getId() != null && alignment.getId().length() > 0) {
                name = alignment.getId();
            } else {
                if (alignments.size() > 1) {
                    name += count;
                }
            }
            setData(name, taxa, alignment, null, null, null, null);
            count++;
        }
        // assume that any additional taxon lists are taxon sets...
        for (int i = 1; i < taxonLists.size(); i++) {
            Taxa taxonSet = (Taxa) taxonLists.get(i);
            options.taxonSets.add(taxonSet);
            options.taxonSetsMono.put(taxonSet, false);
            options.taxonSetsIncludeStem.put(taxonSet, false);
            options.taxonSetsTreeModel.put(taxonSet, options.getPartitionTreeModels().get(0));
        }
        reader.close();
    } catch (JDOMException e) {
        throw new JDOMException(e.getMessage());
    } catch (ImportException e) {
        throw new ImportException(e.getMessage());
    } catch (IOException e) {
        throw new IOException(e.getMessage());
    }
}
Also used : TaxonList(dr.evolution.util.TaxonList) ArrayList(java.util.ArrayList) JDOMException(org.jdom.JDOMException) ImportException(dr.evolution.io.Importer.ImportException) Taxa(dr.evolution.util.Taxa) Alignment(dr.evolution.alignment.Alignment) SimpleAlignment(dr.evolution.alignment.SimpleAlignment)

Example 19 with Alignment

use of dr.evolution.alignment.Alignment in project beast-mcmc by beast-dev.

the class PatternListGenerator method writePatternList.

/**
     * Write a single pattern list
     *
     * @param partition the partition to write a pattern list for
     * @param offset    offset by
     * @param every     skip every
     * @param writer    the writer
     */
private void writePatternList(final PartitionData partition, int offset, int every, final String codonPrefix, final boolean unique, final boolean strip, final XMLWriter writer) {
    Alignment alignment = partition.getAlignment();
    int from = partition.getFromSite();
    int to = partition.getToSite();
    int partEvery = partition.getEvery();
    if (partEvery > 1 && every > 1)
        throw new IllegalArgumentException();
    if (from < 1)
        from = 1;
    every = Math.max(partEvery, every);
    from += offset;
    // this object is created solely to calculate the number of patterns in the alignment
    SitePatterns patterns = new SitePatterns(alignment, null, from - 1, to - 1, every, strip, unique);
    writer.writeComment("The " + (unique ? "unique " : "") + "patterns from " + from + " to " + (to > 0 ? to : "end") + ((every > 1) ? " every " + every : ""), "npatterns=" + patterns.getPatternCount());
    List<Attribute> attributes = new ArrayList<Attribute>();
    // no 11 of 112 codon, which uses mergePatterns id instead
    if (codonPrefix != null) {
        attributes.add(new Attribute.Default<String>(XMLParser.ID, partition.getPrefix() + codonPrefix + SitePatternsParser.PATTERNS));
    }
    attributes.add(new Attribute.Default<String>("from", "" + from));
    if (to >= 0)
        attributes.add(new Attribute.Default<String>("to", "" + to));
    if (every > 1) {
        attributes.add(new Attribute.Default<String>("every", "" + every));
    }
    if (!unique) {
        attributes.add(new Attribute.Default<Boolean>(SitePatternsParser.UNIQUE, false));
    }
    if (strip) {
        // default true
        attributes.add(new Attribute.Default<Boolean>(SitePatternsParser.STRIP, false));
    }
    // generate <patterns>
    writer.writeOpenTag(SitePatternsParser.PATTERNS, attributes);
    writer.writeIDref(AlignmentParser.ALIGNMENT, alignment.getId());
    writer.writeCloseTag(SitePatternsParser.PATTERNS);
}
Also used : SitePatterns(dr.evolution.alignment.SitePatterns) Alignment(dr.evolution.alignment.Alignment) Attribute(dr.util.Attribute) ArrayList(java.util.ArrayList)

Example 20 with Alignment

use of dr.evolution.alignment.Alignment in project beast-mcmc by beast-dev.

the class SubstitutionModelGenerator method writeBinaryCovarionModel.

/**
     * Write the Binary covarion model XML block
     *
     * @param writer the writer
     * @param model  the partition model to write
     */
public void writeBinaryCovarionModel(XMLWriter writer, PartitionSubstitutionModel model) {
    //        String dataTypeDescription = TwoStateCovarion.INSTANCE.getDescription(); // dataType="twoStateCovarion" for COVARION_MODEL
    String prefix = model.getPrefix();
    writer.writeComment("The Binary covarion model");
    writer.writeOpenTag(BinaryCovarionModelParser.COVARION_MODEL, new Attribute[] { new Attribute.Default<String>(XMLParser.ID, prefix + "bcov") });
    if (model.getFrequencyPolicy() == FrequencyPolicyType.EMPIRICAL) {
        List<AbstractPartitionData> partitions = options.getDataPartitions(model);
        Alignment alignment = ((PartitionData) partitions.get(0)).getAlignment();
        //            Patterns patterns = new Patterns(partitions.get(0).getAlignment());
        //            for (int i = 1; i < partitions.size(); i++) {
        //                patterns.addPatterns(partitions.get(i).getAlignment());
        //            }
        double[] frequencies = alignment.getStateFrequencies();
        writer.writeOpenTag(FrequencyModelParser.FREQUENCIES);
        writer.writeTag(ParameterParser.PARAMETER, new Attribute[] { new Attribute.Default<String>(XMLParser.ID, prefix + "frequencies"), new Attribute.Default<String>(ParameterParser.VALUE, frequencies[0] + " " + frequencies[1]) }, true);
        writer.writeCloseTag(FrequencyModelParser.FREQUENCIES);
    } else {
        writeFrequencyModelBinary(writer, model, prefix);
    }
    writeParameter(BinaryCovarionModelParser.HIDDEN_FREQUENCIES, prefix + "hfrequencies", 2, 0.5, 0.0, 1.0, // hfrequencies also 0.5 0.5
    writer);
    writeParameter(BinaryCovarionModelParser.ALPHA, "bcov.alpha", model, writer);
    writeParameter(BinaryCovarionModelParser.SWITCHING_RATE, "bcov.s", model, writer);
    writer.writeCloseTag(BinaryCovarionModelParser.COVARION_MODEL);
}
Also used : Alignment(dr.evolution.alignment.Alignment) Attribute(dr.util.Attribute)

Aggregations

Alignment (dr.evolution.alignment.Alignment)29 SimpleAlignment (dr.evolution.alignment.SimpleAlignment)7 TaxonList (dr.evolution.util.TaxonList)7 TreeModel (dr.evomodel.tree.TreeModel)6 Parameter (dr.inference.model.Parameter)6 ImportException (dr.evolution.io.Importer.ImportException)5 Tree (dr.evolution.tree.Tree)5 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)5 GammaSiteRateModel (dr.evomodel.siteratemodel.GammaSiteRateModel)5 BeagleSequenceSimulator (dr.app.beagle.tools.BeagleSequenceSimulator)4 Partition (dr.app.beagle.tools.Partition)4 ConvertAlignment (dr.evolution.alignment.ConvertAlignment)4 SitePatterns (dr.evolution.alignment.SitePatterns)4 NewickImporter (dr.evolution.io.NewickImporter)4 HomogeneousBranchModel (dr.evomodel.branchmodel.HomogeneousBranchModel)4 FrequencyModel (dr.evomodel.substmodel.FrequencyModel)4 ArrayList (java.util.ArrayList)4 Taxon (dr.evolution.util.Taxon)3 DefaultBranchRateModel (dr.evomodel.branchratemodel.DefaultBranchRateModel)3 SubstitutionModel (dr.evomodel.substmodel.SubstitutionModel)3