Search in sources :

Example 1 with Alignment

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

the class BeagleTreeLikelihood method main.

public static void main(String[] args) {
    try {
        MathUtils.setSeed(666);
        System.out.println("Test case 1: simulateOnePartition");
        int sequenceLength = 1000;
        ArrayList<Partition> partitionsList = new ArrayList<Partition>();
        // create tree
        NewickImporter importer = new NewickImporter("(SimSeq1:73.7468,(SimSeq2:25.256989999999995,SimSeq3:45.256989999999995):18.48981);");
        Tree tree = importer.importTree(null);
        TreeModel treeModel = new TreeModel(tree);
        // create Frequency Model
        Parameter freqs = new Parameter.Default(new double[] { 0.25, 0.25, 0.25, 0.25 });
        FrequencyModel freqModel = new FrequencyModel(Nucleotides.INSTANCE, freqs);
        // create branch model
        Parameter kappa1 = new Parameter.Default(1, 1);
        Parameter kappa2 = new Parameter.Default(1, 1);
        HKY hky1 = new HKY(kappa1, freqModel);
        HKY hky2 = new HKY(kappa2, freqModel);
        HomogeneousBranchModel homogenousBranchSubstitutionModel = new HomogeneousBranchModel(hky1);
        List<SubstitutionModel> substitutionModels = new ArrayList<SubstitutionModel>();
        substitutionModels.add(hky1);
        substitutionModels.add(hky2);
        List<FrequencyModel> freqModels = new ArrayList<FrequencyModel>();
        freqModels.add(freqModel);
        Parameter epochTimes = new Parameter.Default(1, 20);
        // create branch rate model
        Parameter rate = new Parameter.Default(1, 0.001);
        BranchRateModel branchRateModel = new StrictClockBranchRates(rate);
        // create site model
        GammaSiteRateModel siteRateModel = new GammaSiteRateModel("siteModel");
        BranchModel homogeneousBranchModel = new HomogeneousBranchModel(hky1);
        BranchModel epochBranchModel = new EpochBranchModel(treeModel, substitutionModels, epochTimes);
        // create partition
        Partition partition1 = new //
        Partition(//
        treeModel, //
        homogenousBranchSubstitutionModel, //
        siteRateModel, //
        branchRateModel, //
        freqModel, // from
        0, // to
        sequenceLength - 1, // every
        1);
        partitionsList.add(partition1);
        // feed to sequence simulator and generate data
        BeagleSequenceSimulator simulator = new BeagleSequenceSimulator(partitionsList);
        Alignment alignment = simulator.simulate(false, false);
        BeagleTreeLikelihood nbtl = new BeagleTreeLikelihood(alignment, treeModel, homogeneousBranchModel, siteRateModel, branchRateModel, null, false, PartialsRescalingScheme.DEFAULT, false);
        System.out.println("nBTL(homogeneous) = " + nbtl.getLogLikelihood());
        nbtl = new BeagleTreeLikelihood(alignment, treeModel, epochBranchModel, siteRateModel, branchRateModel, null, false, PartialsRescalingScheme.DEFAULT, false);
        System.out.println("nBTL(epoch) = " + nbtl.getLogLikelihood());
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    }
// END: try-catch block
}
Also used : FrequencyModel(dr.evomodel.substmodel.FrequencyModel) Partition(dr.app.beagle.tools.Partition) EpochBranchModel(dr.evomodel.branchmodel.EpochBranchModel) HomogeneousBranchModel(dr.evomodel.branchmodel.HomogeneousBranchModel) GammaSiteRateModel(dr.evomodel.siteratemodel.GammaSiteRateModel) HomogeneousBranchModel(dr.evomodel.branchmodel.HomogeneousBranchModel) BranchModel(dr.evomodel.branchmodel.BranchModel) EpochBranchModel(dr.evomodel.branchmodel.EpochBranchModel) StrictClockBranchRates(dr.evomodel.branchratemodel.StrictClockBranchRates) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel) MarkovModulatedSubstitutionModel(dr.evomodel.substmodel.MarkovModulatedSubstitutionModel) BeagleSequenceSimulator(dr.app.beagle.tools.BeagleSequenceSimulator) TreeModel(dr.evomodel.tree.TreeModel) Alignment(dr.evolution.alignment.Alignment) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) BranchRateModel(dr.evomodel.branchratemodel.BranchRateModel) NewickImporter(dr.evolution.io.NewickImporter) HKY(dr.evomodel.substmodel.nucleotide.HKY) Tree(dr.evolution.tree.Tree) Parameter(dr.inference.model.Parameter)

Example 2 with Alignment

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

the class DataModelImporter method importFastaFile.

// FASTA
private void importFastaFile(File file, DateGuesser guesser, Map dataModel) throws IOException, ImportException {
    try {
        FileReader reader = new FileReader(file);
        FastaImporter importer = new FastaImporter(reader, Nucleotides.INSTANCE);
        Alignment alignment = importer.importAlignment();
        reader.close();
        setData(dataModel, guesser, file.getName(), alignment, null, alignment, 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 3 with Alignment

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

the class MicrosatelliteSimulator method simulateMsatPattern.

// intArray2Sequence
/**
     * Convert an alignment to a pattern
     */
public Patterns simulateMsatPattern() {
    Alignment align = simulate();
    int[] pattern = new int[align.getTaxonCount()];
    for (int i = 0; i < pattern.length; i++) {
        String taxonName = align.getSequence(i).getTaxon().getId();
        int index = taxa.getTaxonIndex(taxonName);
        pattern[index] = Integer.parseInt(align.getSequence(i).getSequenceString());
    }
    Patterns patterns = new Patterns(dataType, taxa);
    patterns.addPattern(pattern);
    for (int i = 0; i < pattern.length; i++) {
        System.out.print(pattern[i] + ",");
    }
    System.out.println();
    return patterns;
}
Also used : Alignment(dr.evolution.alignment.Alignment) Patterns(dr.evolution.alignment.Patterns)

Example 4 with Alignment

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

the class HiddenLinkageModel method getPartialsForGroupSizeOne.

private void getPartialsForGroupSizeOne(Taxon tax, double[] tipPartials) {
    Alignment aln = data.getAlignment();
    int sc = aln.getStateCount();
    int index = aln.getTaxonIndex(tax);
    int j = 0;
    for (int i = 0; i < aln.getSiteCount(); i++) {
        int s = aln.getState(index, i);
        if (s >= sc) {
            for (int k = 0; k < sc; k++) tipPartials[j + k] = 1.0;
        } else
            System.arraycopy(internalMatrix, s * sc, tipPartials, j, sc);
        j += sc;
    }
}
Also used : Alignment(dr.evolution.alignment.Alignment)

Example 5 with Alignment

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

the class LinkageGroupSwap method doOperation.

@Override
public double doOperation() {
    if (MathUtils.nextBoolean()) {
        // swap all taxa in one group to a new group
        int A = MathUtils.nextInt(groupCount);
        int B = MathUtils.nextInt(groupCount);
        HashSet<Taxon> aTaxa = new HashSet<Taxon>(hlm.getGroup(A));
        HashSet<Taxon> bTaxa = new HashSet<Taxon>(hlm.getGroup(B));
        for (Taxon taxon : aTaxa) {
            hlm.moveReadGroup(taxon, A, B);
        }
        for (Taxon taxon : bTaxa) {
            hlm.moveReadGroup(taxon, B, A);
        }
    } else {
        // pick two linkage groups uniformly A, B
        // pick an alignment column uniformly X
        // move all reads in group A that are defined in column X to group B, and
        // vice versa from B to A.
        // pick a read uniformly at random, add it to a linkage group uniformly at random
        ArrayList<Taxon> aTaxa = new ArrayList<Taxon>();
        ArrayList<Taxon> bTaxa = new ArrayList<Taxon>();
        int A = 0, B = 0;
        // choice of X and Y to groups that have something in col X.
        while (aTaxa.size() == 0 && bTaxa.size() == 0) {
            int X = MathUtils.nextInt(columnCount);
            A = MathUtils.nextInt(groupCount);
            B = MathUtils.nextInt(groupCount);
            if (A == B)
                // nothing to do.
                continue;
            // find all reads intersecting column X
            Alignment aln = hlm.getData().getAlignment();
            for (int i = 0; i < aln.getTaxonCount(); i++) {
                int state = aln.getPatternState(i, X);
                if (state == hlm.getDataType().getGapState() || state == hlm.getDataType().getUnknownState())
                    // seq undefined in this column
                    continue;
                if (hlm.getGroup(A).contains(aln.getTaxon(i))) {
                    aTaxa.add(aln.getTaxon(i));
                }
                if (hlm.getGroup(B).contains(aln.getTaxon(i))) {
                    bTaxa.add(aln.getTaxon(i));
                }
            }
        }
        // move taxa from A to B and from B to A
        for (Taxon taxon : aTaxa) {
            hlm.moveReadGroup(taxon, A, B);
        }
        for (Taxon taxon : bTaxa) {
            hlm.moveReadGroup(taxon, B, A);
        }
    }
    return 0;
}
Also used : Alignment(dr.evolution.alignment.Alignment) Taxon(dr.evolution.util.Taxon) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

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