Search in sources :

Example 1 with DataType

use of beast.evolution.datatype.DataType in project beast2 by CompEvol.

the class AlignmentViewer method updateTableData.

private char[] updateTableData() {
    int siteCount = m_alignment.getSiteCount();
    int taxonCount = m_alignment.getTaxonCount();
    // set up table content
    DataType dataType = m_alignment.getDataType();
    char[] headerChar = new char[siteCount];
    Object[][] colorMap = setupColorMap();
    Object[][] integerColorMap = setupIntegerColorMap();
    try {
        for (int i = 0; i < siteCount; i++) {
            int patternIndex_ = m_alignment.getPatternIndex(i);
            int[] pattern = m_alignment.getPattern(patternIndex_);
            String patternString = dataType.state2string(pattern);
            if (patternString.contains(",")) {
                // We have a string of comma separated values.
                String[] patternAtTaxon = patternString.split(",");
                headerChar[i] = mostFrequentCharInPattern(patternAtTaxon);
                for (int j = 0; j < taxonCount; j++) {
                    try {
                        int code = Integer.valueOf(patternAtTaxon[j]);
                        if (code + '0' == headerChar[i]) {
                            // Prevent Exception when accessing integerColorMap
                            code = Math.min(Math.max(code, 0), integerColorMap[0].length - 1);
                            tableData[j][i + 1] = integerColorMap[0][code];
                        } else {
                            code = Math.min(Math.max(code, 0), integerColorMap[0].length - 1);
                            tableData[j][i + 1] = integerColorMap[1][code];
                        }
                    } catch (NumberFormatException e) {
                        // State cannot be interpreted as a number.
                        // Assume it is a special (ambiguous) character.
                        tableData[j][i + 1] = useColor ? ' ' : patternAtTaxon[j];
                    }
                }
            } else {
                headerChar[i] = mostFrequentCharInPattern(patternString);
                for (int j = 0; j < taxonCount; j++) {
                    char c = patternString.charAt(j);
                    if (c == headerChar[i]) {
                        tableData[j][i + 1] = colorMap[0][c];
                    } else {
                        tableData[j][i + 1] = colorMap[1][c];
                    }
                }
            }
        }
    } catch (Exception e) {
    // ignore
    }
    return headerChar;
}
Also used : DataType(beast.evolution.datatype.DataType)

Example 2 with DataType

use of beast.evolution.datatype.DataType in project beast2 by CompEvol.

the class Frequencies method estimateFrequencies.

// update
/**
 * Estimate from sequence alignment.
 * This version matches the implementation in Beast 1 & PAUP  *
 */
void estimateFrequencies() {
    Alignment alignment = dataInput.get();
    DataType dataType = alignment.getDataType();
    int stateCount = alignment.getMaxStateCount();
    freqs = new double[stateCount];
    Arrays.fill(freqs, 1.0 / stateCount);
    int attempts = 0;
    double difference;
    do {
        double[] tmpFreq = new double[stateCount];
        double total = 0.0;
        for (int i = 0; i < alignment.getPatternCount(); i++) {
            int[] pattern = alignment.getPattern(i);
            double weight = alignment.getPatternWeight(i);
            for (int value : pattern) {
                int[] codes = dataType.getStatesForCode(value);
                double sum = 0.0;
                for (int codeIndex : codes) {
                    sum += freqs[codeIndex];
                }
                for (int codeIndex : codes) {
                    double tmp = (freqs[codeIndex] * weight) / sum;
                    tmpFreq[codeIndex] += tmp;
                    total += tmp;
                }
            }
        }
        difference = 0.0;
        for (int i = 0; i < stateCount; i++) {
            difference += Math.abs((tmpFreq[i] / total) - freqs[i]);
            freqs[i] = tmpFreq[i] / total;
        }
        attempts++;
    } while (difference > 1E-8 && attempts < 1000);
    // Alignment alignment = m_data.get();
    // m_fFreqs = new double[alignment.getMaxStateCount()];
    // for (int i = 0; i < alignment.getPatternCount(); i++) {
    // int[] pattern = alignment.getPattern(i);
    // double weight = alignment.getPatternWeight(i);
    // DataType dataType = alignment.getDataType();
    // for (int value : pattern) {
    // if (value < 4) {
    // int [] codes = dataType.getStatesForCode(value);
    // for (int codeIndex : codes) {
    // m_fFreqs[codeIndex] += weight / codes.length;
    // }
    // }
    // //                if (value < m_fFreqs.length) { // ignore unknowns
    // //                    m_fFreqs[value] += weight;
    // //                }
    // }
    // }
    // // normalize
    // double sum = 0;
    // for (double f : m_fFreqs) {
    // sum += f;
    // }
    // for (int i = 0; i < m_fFreqs.length; i++) {
    // m_fFreqs[i] /= sum;
    // }
    Log.info.println("Starting frequencies: " + Arrays.toString(freqs));
}
Also used : Alignment(beast.evolution.alignment.Alignment) DataType(beast.evolution.datatype.DataType)

Example 3 with DataType

use of beast.evolution.datatype.DataType in project beast2 by CompEvol.

the class UncertainAlignmentTest method testUncertainAlignment.

@Test
public void testUncertainAlignment() throws Exception {
    Alignment data = getUncertainAlignment();
    DataType dataType = data.getDataType();
    System.out.println("Tip likelihoods:");
    int sites = data.getCounts().get(0).size();
    for (int taxon = 0; taxon < data.getTaxonCount(); taxon++) {
        for (int i = 0; i < sites; i++) {
            double[] probs = data.getTipLikelihoods(taxon, i);
            for (int j = 0; j < probs.length; j++) {
                System.out.print(probs[j] + " ");
            }
            System.out.print("; ");
        }
        System.out.println();
    }
    System.out.println("Most likely sequences:");
    for (List<Integer> seq : data.getCounts()) {
        System.out.println(dataType.state2string(seq));
    }
    Alignment data2 = getAlignment();
    for (int taxon = 0; taxon < data.getTaxonCount(); taxon++) {
        assertEquals(data.getCounts().get(taxon), data2.getCounts().get(taxon));
    }
}
Also used : Alignment(beast.evolution.alignment.Alignment) DataType(beast.evolution.datatype.DataType) Test(org.junit.Test)

Example 4 with DataType

use of beast.evolution.datatype.DataType in project beast2 by CompEvol.

the class UncertainAlignmentTest method getUncertainAlignment.

public static Alignment getUncertainAlignment(boolean duplicate) throws Exception {
    String seq1Probs = new String("0.7,0.0,0.3,0.0; 0.0,0.3,0.0,0.7; 0.0,0.0,0.0,1.0;");
    String seq2Probs = new String("0.7,0.0,0.3,0.0; 0.0,0.3,0.0,0.7; 0.0,1.0,0.0,0.0;");
    String seq3Probs = new String("0.4,0.0,0.6,0.0; 0.0,0.6,0.0,0.4; 0.0,1.0,0.0,0.0;");
    if (duplicate) {
        seq1Probs += seq1Probs;
        seq2Probs += seq2Probs;
        seq3Probs += seq3Probs;
    }
    Sequence seq1 = new Sequence();
    seq1.initByName("taxon", "seq1", "value", seq1Probs, "uncertain", true);
    Sequence seq2 = new Sequence();
    seq2.initByName("taxon", "seq2", "value", seq2Probs, "uncertain", true);
    Sequence seq3 = new Sequence();
    seq3.initByName("taxon", "seq3", "value", seq3Probs, "uncertain", true);
    Alignment data = new Alignment();
    data.initByName("sequence", seq1, "sequence", seq2, "sequence", seq3, "dataType", "nucleotide");
    DataType dataType = data.getDataType();
    System.out.println("Most probable sequences:");
    for (List<Integer> seq : data.getCounts()) {
        System.out.println(dataType.state2string(seq));
    }
    return data;
}
Also used : Alignment(beast.evolution.alignment.Alignment) DataType(beast.evolution.datatype.DataType) Sequence(beast.evolution.alignment.Sequence)

Example 5 with DataType

use of beast.evolution.datatype.DataType in project bacter by tgvaughan.

the class SimulatedAlignment method grabDataType.

/**
 * HORRIBLE function to identify data type from given description.
 *
 * @return DataType instance (null if none found)
 */
private void grabDataType() {
    if (userDataTypeInput.get() != null) {
        dataType = userDataTypeInput.get();
    } else {
        List<String> dataTypeDescList = new ArrayList<>();
        List<String> classNames = PackageManager.find(beast.evolution.datatype.DataType.class, "beast.evolution.datatype");
        for (String className : classNames) {
            try {
                DataType thisDataType = (DataType) Class.forName(className).getDeclaredConstructor().newInstance();
                if (dataTypeInput.get().equals(thisDataType.getTypeDescription())) {
                    dataType = thisDataType;
                    break;
                }
                dataTypeDescList.add(thisDataType.getTypeDescription());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        if (dataType == null) {
            throw new IllegalArgumentException("Data type + '" + dataTypeInput.get() + "' cannot be found.  Choose one of " + Arrays.toString(dataTypeDescList.toArray(new String[0])));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) DataType(beast.evolution.datatype.DataType)

Aggregations

DataType (beast.evolution.datatype.DataType)8 Alignment (beast.evolution.alignment.Alignment)3 Sequence (beast.evolution.alignment.Sequence)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1