Search in sources :

Example 91 with Taxon

use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.

the class FitchParsimony method main.

public static void main(String[] argv) {
    FlexibleNode tip1 = new FlexibleNode(new Taxon("tip1"));
    FlexibleNode tip2 = new FlexibleNode(new Taxon("tip2"));
    FlexibleNode tip3 = new FlexibleNode(new Taxon("tip3"));
    FlexibleNode tip4 = new FlexibleNode(new Taxon("tip4"));
    FlexibleNode tip5 = new FlexibleNode(new Taxon("tip5"));
    FlexibleNode node1 = new FlexibleNode();
    node1.addChild(tip1);
    node1.addChild(tip2);
    FlexibleNode node2 = new FlexibleNode();
    node2.addChild(tip4);
    node2.addChild(tip5);
    FlexibleNode node3 = new FlexibleNode();
    node3.addChild(tip3);
    node3.addChild(node2);
    FlexibleNode root = new FlexibleNode();
    root.addChild(node1);
    root.addChild(node3);
    FlexibleTree tree = new FlexibleTree(root);
    Patterns patterns = new Patterns(Nucleotides.INSTANCE, tree);
    patterns.addPattern(new int[] { 1, 0, 1, 2, 2 });
    FitchParsimony fitch = new FitchParsimony(patterns, false);
    System.out.println("No. Steps = " + fitch.getScore(tree));
    System.out.println(" state(node1) = " + fitch.getStates(tree, node1)[0]);
    System.out.println(" state(node2) = " + fitch.getStates(tree, node2)[0]);
    System.out.println(" state(node3) = " + fitch.getStates(tree, node3)[0]);
    System.out.println(" state(root) = " + fitch.getStates(tree, root)[0]);
    System.out.println("\nParsimony static methods:");
    System.out.println("No. Steps = " + Parsimony.getParsimonySteps(tree, patterns));
    Parsimony.reconstructParsimonyStates(tree, patterns);
    System.out.println(" state(node1) = " + tree.getNodeAttribute(node1, "rstate1"));
    System.out.println(" state(node2) = " + tree.getNodeAttribute(node2, "rstate1"));
    System.out.println(" state(node3) = " + tree.getNodeAttribute(node3, "rstate1"));
    System.out.println(" state(root) = " + tree.getNodeAttribute(root, "rstate1"));
}
Also used : Taxon(dr.evolution.util.Taxon) Patterns(dr.evolution.alignment.Patterns)

Example 92 with Taxon

use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.

the class SankoffParsimony method main.

public static void main(String[] argv) {
    FlexibleNode tip1 = new FlexibleNode(new Taxon("tip1"));
    FlexibleNode tip2 = new FlexibleNode(new Taxon("tip2"));
    FlexibleNode tip3 = new FlexibleNode(new Taxon("tip3"));
    FlexibleNode tip4 = new FlexibleNode(new Taxon("tip4"));
    FlexibleNode tip5 = new FlexibleNode(new Taxon("tip5"));
    FlexibleNode node1 = new FlexibleNode();
    node1.addChild(tip1);
    node1.addChild(tip2);
    FlexibleNode node2 = new FlexibleNode();
    node2.addChild(tip4);
    node2.addChild(tip5);
    FlexibleNode node3 = new FlexibleNode();
    node3.addChild(tip3);
    node3.addChild(node2);
    FlexibleNode root = new FlexibleNode();
    root.addChild(node1);
    root.addChild(node3);
    FlexibleTree tree = new FlexibleTree(root);
    Patterns patterns = new Patterns(Nucleotides.INSTANCE, tree);
    //patterns.addPattern(new int[] {1, 0, 1, 2, 2});
    //patterns.addPattern(new int[] {2, 1, 1, 1, 2});
    patterns.addPattern(new int[] { 2, 3, 1, 3, 3 });
    FitchParsimony fitch = new FitchParsimony(patterns, false);
    SankoffParsimony sankoff = new SankoffParsimony(patterns);
    for (int i = 0; i < patterns.getPatternCount(); i++) {
        double[] scores = fitch.getSiteScores(tree);
        System.out.println("Pattern = " + i);
        System.out.println("Fitch:");
        System.out.println("  No. Steps = " + scores[i]);
        System.out.println("    state(node1) = " + fitch.getStates(tree, node1)[i]);
        System.out.println("    state(node2) = " + fitch.getStates(tree, node2)[i]);
        System.out.println("    state(node3) = " + fitch.getStates(tree, node3)[i]);
        System.out.println("    state(root) = " + fitch.getStates(tree, root)[i]);
        scores = sankoff.getSiteScores(tree);
        System.out.println("Sankoff:");
        System.out.println("  No. Steps = " + scores[i]);
        System.out.println("    state(node1) = " + sankoff.getStates(tree, node1)[i]);
        System.out.println("    state(node2) = " + sankoff.getStates(tree, node2)[i]);
        System.out.println("    state(node3) = " + sankoff.getStates(tree, node3)[i]);
        System.out.println("    state(root) = " + sankoff.getStates(tree, root)[i]);
        System.out.println();
    }
}
Also used : Taxon(dr.evolution.util.Taxon) Patterns(dr.evolution.alignment.Patterns)

Example 93 with Taxon

use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.

the class MicroSatImporter method importPatterns.

public List<Patterns> importPatterns() throws IOException, Importer.ImportException {
    List<Patterns> microsatPatList = new ArrayList<Patterns>();
    // 1st List<String> is taxon names
    List<List<String>> data = new ArrayList<List<String>>();
    // microsatName[0] is keyword, microsatName[1] is name
    String[] microsatName = new String[2];
    microsatName[1] = "unnamed.microsat";
    String line = reader.readLine();
    while (line.startsWith("#")) {
        // comments
        if (line.toUpperCase().contains("NAME")) {
            microsatName = line.trim().split("[" + delimiter + " ]+");
            if (microsatName[1] == null || microsatName[1].length() < 1)
                throw new Importer.ImportException("Improper microsatellite name : " + microsatName[1]);
        }
        line = reader.readLine();
    }
    // read locus (microsat pattern) names in the 1st row after comments, where 1st element is id
    // trim trailing whitespace ?
    String[] names = line.trim().split("[" + delimiter + " ]+");
    // for validation
    int colLen = names.length;
    if (colLen < 2)
        throw new Importer.ImportException("Import file must have more than 1 columns : " + colLen);
    for (int i = 0; i < colLen; i++) {
        // init data
        List<String> l = new ArrayList<String>();
        data.add(l);
    }
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
    line = reader.readLine();
    while (line != null) {
        // read data
        String[] dataLine = line.trim().split("[" + delimiter + " ]+");
        if (dataLine.length != colLen)
            throw new Importer.ImportException("The number of name columns are different with values columns," + "\nplease use only letters or numbers in the name.");
        for (int i = 0; i < dataLine.length; i++) {
            data.get(i).add(dataLine[i]);
            if (i > 0) {
                int v = parseInt(dataLine[i]);
                if (v != Microsatellite.UNKNOWN_STATE_LENGTH) {
                    if (min > v)
                        min = v;
                    if (max < v)
                        max = v;
                }
            }
        }
        line = reader.readLine();
    }
    if (max < min)
        throw new Importer.ImportException("Importing invalid data: max < min !");
    //        if (min - 2 < 0) throw new Importer.ImportException("Importing invaild data: min-2 < 0 where min = " + min);
    // The min also = 1 and max should be the longest repeat length + 2.
    microsatellite = new Microsatellite(microsatName[1], 1, max + 2, 1);
    Taxa taxaHaploid = new Taxa();
    for (String name : data.get(0)) {
        Taxon t = new Taxon(name);
        taxaHaploid.addTaxon(t);
    }
    //        unionSetTaxonList.addTaxa(taxaHaploid);
    Patterns microsatPat;
    for (int i = 1; i < data.size(); i++) {
        // create pattern
        //            List<Integer> pattern = new ArrayList<Integer>();
        List<Integer> pattern;
        Taxa taxa = new Taxa();
        if ((i + 1 < data.size()) && names[i].equalsIgnoreCase(names[i + 1])) {
            // diploid: Locus2	Locus2
            Taxa taxaDiploid = new Taxa();
            for (String name : data.get(0)) {
                Taxon t = new Taxon(names[i] + "_1_" + name);
                taxaDiploid.addTaxon(t);
            }
            for (String name : data.get(0)) {
                Taxon t = new Taxon(names[i] + "_2_" + name);
                taxaDiploid.addTaxon(t);
            }
            if (unionSetTaxonList.containsAny(taxaDiploid))
                throw new Importer.ImportException("Importing invalid data: duplicate taxon name in this locus : " + names[i]);
            unionSetTaxonList.addTaxa(taxaDiploid);
            hasDifferentTaxon = true;
            pattern = new ArrayList<Integer>();
            String value;
            int size = data.get(i).size();
            for (int v = 0; v < size; v++) {
                value = data.get(i).get(v);
                //                    if (!isUnknownChar(value)) {
                Taxon t = taxaDiploid.getTaxon(v);
                if (!taxa.contains(t)) {
                    taxa.addTaxon(t);
                    //microsatellite.getState(value);
                    pattern.add(parseInt(value));
                    if (!unionSetTaxonList.contains(t)) {
                        unionSetTaxonList.addTaxon(t);
                        if (i > 1)
                            hasDifferentTaxon = true;
                    }
                }
            //                    }
            }
            for (int v = 0; v < data.get(i + 1).size(); v++) {
                value = data.get(i + 1).get(v);
                //                    if (!isUnknownChar(value)) {
                Taxon t = taxaDiploid.getTaxon(v + size);
                if (!taxa.contains(t)) {
                    taxa.addTaxon(t);
                    //microsatellite.getState(value);
                    pattern.add(parseInt(value));
                    if (!unionSetTaxonList.contains(t)) {
                        unionSetTaxonList.addTaxon(t);
                        if (i > 1)
                            hasDifferentTaxon = true;
                    }
                }
            //                    }
            }
            i++;
        } else {
            // haploid Locus1
            pattern = new ArrayList<Integer>();
            for (int v = 0; v < data.get(i).size(); v++) {
                String value = data.get(i).get(v);
                //                    if (!isUnknownChar(value)) {
                Taxon t = taxaHaploid.getTaxon(v);
                if (!taxa.contains(t)) {
                    taxa.addTaxon(t);
                    //microsatellite.getState(value);
                    pattern.add(parseInt(value));
                    if (!unionSetTaxonList.contains(t)) {
                        unionSetTaxonList.addTaxon(t);
                        if (i > 1)
                            hasDifferentTaxon = true;
                    }
                }
            //                    }
            }
        }
        int[] p = new int[pattern.size()];
        for (int v = 0; v < pattern.size(); v++) {
            p[v] = pattern.get(v);
        }
        microsatPat = new Patterns(microsatellite, taxa);
        microsatPat.addPattern(p);
        microsatPat.setId(names[i]);
        microsatPatList.add(microsatPat);
    }
    return microsatPatList;
}
Also used : Microsatellite(dr.evolution.datatype.Microsatellite) Taxon(dr.evolution.util.Taxon) ArrayList(java.util.ArrayList) Taxa(dr.evolution.util.Taxa) List(java.util.List) ArrayList(java.util.ArrayList) Patterns(dr.evolution.alignment.Patterns)

Example 94 with Taxon

use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.

the class PhylipSequentialImporter method importAlignment.

/**
	 * importAlignment. 
	 */
public Alignment importAlignment() throws IOException, Importer.ImportException {
    SimpleAlignment alignment = null;
    try {
        int taxonCount = readInteger();
        int siteCount = readInteger();
        String firstSeq = null;
        for (int i = 0; i < taxonCount; i++) {
            StringBuffer name = new StringBuffer();
            char ch = read();
            int n = 0;
            while (!Character.isWhitespace(ch) && (maxNameLength < 1 || n < maxNameLength)) {
                name.append(ch);
                ch = read();
                n++;
            }
            StringBuffer seq = new StringBuffer(siteCount);
            readSequence(seq, dataType, "", siteCount, "-", "?", ".", firstSeq);
            if (firstSeq == null) {
                firstSeq = seq.toString();
            }
            if (alignment == null) {
                alignment = new SimpleAlignment();
            }
            alignment.addSequence(new Sequence(new Taxon(name.toString()), seq.toString()));
        }
    } catch (EOFException e) {
    }
    return alignment;
}
Also used : SimpleAlignment(dr.evolution.alignment.SimpleAlignment) Taxon(dr.evolution.util.Taxon) EOFException(java.io.EOFException) Sequence(dr.evolution.sequence.Sequence)

Example 95 with Taxon

use of dr.evolution.util.Taxon in project beast-mcmc by beast-dev.

the class NewickImporter method readExternalNode.

/**
     * Reads an external node in.
     */
private FlexibleNode readExternalNode(TaxonList taxonList) throws IOException, ImportException {
    FlexibleNode node = new FlexibleNode();
    String label = readToken(":(),;");
    Taxon taxon;
    if (taxonList != null) {
        // if a taxon list is given then the taxon must be in it...
        int index = taxonList.getTaxonIndex(label);
        if (index != -1) {
            taxon = taxonList.getTaxon(index);
        } else {
            throw new UnknownTaxonException("Taxon in tree, '" + label + "' is unknown");
        }
    } else {
        // No taxon list given so create new taxa
        taxon = new Taxon(label);
    }
    node.setTaxon(taxon);
    return node;
}
Also used : FlexibleNode(dr.evolution.tree.FlexibleNode) Taxon(dr.evolution.util.Taxon)

Aggregations

Taxon (dr.evolution.util.Taxon)151 Taxa (dr.evolution.util.Taxa)31 ArrayList (java.util.ArrayList)24 TaxonList (dr.evolution.util.TaxonList)19 NodeRef (dr.evolution.tree.NodeRef)18 Date (dr.evolution.util.Date)18 Tree (dr.evolution.tree.Tree)15 Sequence (dr.evolution.sequence.Sequence)12 TreeModel (dr.evomodel.tree.TreeModel)12 Parameter (dr.inference.model.Parameter)12 Attribute (dr.util.Attribute)11 SimpleAlignment (dr.evolution.alignment.SimpleAlignment)10 Patterns (dr.evolution.alignment.Patterns)9 NewickImporter (dr.evolution.io.NewickImporter)7 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)7 Microsatellite (dr.evolution.datatype.Microsatellite)6 IOException (java.io.IOException)5 HashSet (java.util.HashSet)5 PatternList (dr.evolution.alignment.PatternList)4 DataType (dr.evolution.datatype.DataType)4