Search in sources :

Example 1 with Identifiable

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

the class GeneralDataTypeParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    List<String> states = new ArrayList<String>();
    for (int i = 0; i < xo.getChildCount(); i++) {
        if (xo.getChild(i) instanceof XMLObject) {
            XMLObject cxo = (XMLObject) xo.getChild(i);
            if (cxo.getName().equals(STATE)) {
                states.add(cxo.getStringAttribute(CODE));
            } else if (cxo.getName().equals(ALIAS)) {
            // Do nothing just now
            } else if (cxo.getName().equals(AMBIGUITY)) {
            // Do nothing just now
            } else {
                throw new XMLParseException("illegal element, " + cxo.getName() + ", in " + getParserName() + " element");
            }
        } else if (xo.getChild(i) instanceof Identifiable) {
            states.add(((Identifiable) xo.getChild(i)).getId());
        } else {
            throw new XMLParseException("illegal element in " + getParserName() + " element");
        }
    }
    if (states.size() == 0) {
        throw new XMLParseException("No state elements defined in " + getParserName() + " element");
    } else if (states.size() < 2) {
        throw new XMLParseException("Less than two state elements defined in " + getParserName() + " element");
    }
    GeneralDataType dataType = new GeneralDataType(states);
    for (int i = 0; i < xo.getChildCount(); i++) {
        if (xo.getChild(i) instanceof XMLObject) {
            XMLObject cxo = (XMLObject) xo.getChild(i);
            if (cxo.getName().equals(ALIAS)) {
                String alias = cxo.getStringAttribute(CODE);
                //                    if (alias.length() != 1) {
                //                        throw new XMLParseException("State alias codes in " + getParserName() + " element must be exactly one character");
                //                    }
                String state = cxo.getStringAttribute(STATE);
                try {
                    dataType.addAlias(alias, state);
                } catch (IllegalArgumentException iae) {
                    throw new XMLParseException(iae.getMessage() + "in " + getParserName() + " element");
                }
            } else if (cxo.getName().equals(AMBIGUITY)) {
                String code = cxo.getStringAttribute(CODE);
                //                    if (code.length() != 1) {
                //                        throw new XMLParseException("State ambiguity codes in " + getParserName() + " element must be exactly one character");
                //                    }
                String[] ambiguities = cxo.getStringArrayAttribute(STATES);
                if (ambiguities.length == 1) {
                    String codes = ambiguities[0];
                    if (codes.length() < 2) {
                        throw new XMLParseException("States for ambiguity code in " + getParserName() + " element are not ambiguous");
                    }
                    ambiguities = new String[codes.length()];
                    for (int j = 0; j < codes.length(); j++) {
                        ambiguities[j] = String.valueOf(codes.charAt(j));
                    }
                }
                try {
                    dataType.addAmbiguity(code, ambiguities);
                } catch (IllegalArgumentException iae) {
                    throw new XMLParseException(iae.getMessage() + "in " + getParserName() + " element");
                }
            }
        }
    }
    return dataType;
}
Also used : ArrayList(java.util.ArrayList) GeneralDataType(dr.evolution.datatype.GeneralDataType) Identifiable(dr.util.Identifiable)

Example 2 with Identifiable

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

the class LoggerParser method parseXMLObject.

/**
     * @return an object based on the XML element it was passed.
     */
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    // You must say how often you want to log
    final int logEvery = xo.getIntegerAttribute(LOG_EVERY);
    final PrintWriter pw = getLogFile(xo, getParserName());
    final LogFormatter formatter = new TabDelimitedFormatter(pw);
    boolean performanceReport = false;
    if (!xo.hasAttribute(FILE_NAME)) {
        // is a screen log
        performanceReport = true;
    }
    // added a performance measurement delay to avoid the full evaluation period.
    final MCLogger logger = new MCLogger(formatter, logEvery, performanceReport, 10000);
    String title = null;
    if (xo.hasAttribute(TITLE)) {
        title = xo.getStringAttribute(TITLE);
    }
    String header = null;
    if (xo.hasAttribute(HEADER)) {
        header = xo.getStringAttribute(HEADER);
    }
    if (title == null) {
        final BeastVersion version = new BeastVersion();
        title = "BEAST " + version.getVersionString() + "\n" + (header != null ? header + "\n" : "") + "Generated " + (new Date()).toString() + " [seed=" + MathUtils.getSeed() + "]\n" + System.getProperty("command_line", "");
    } else {
        if (header != null) {
            title += "\n" + header;
        }
    }
    logger.setTitle(title);
    for (int i = 0; i < xo.getChildCount(); i++) {
        final Object child = xo.getChild(i);
        if (child instanceof Columns) {
            logger.addColumns(((Columns) child).getColumns());
        } else if (child instanceof Loggable) {
            logger.add((Loggable) child);
        } else if (child instanceof Identifiable) {
            logger.addColumn(new LogColumn.Default(((Identifiable) child).getId(), child));
        } else if (child instanceof Property) {
            logger.addColumn(new LogColumn.Default(((Property) child).getAttributeName(), child));
        } else {
            logger.addColumn(new LogColumn.Default(child.getClass().toString(), child));
        }
    }
    return logger;
}
Also used : Date(java.util.Date) Identifiable(dr.util.Identifiable) BeastVersion(dr.app.beast.BeastVersion) Property(dr.util.Property) PrintWriter(java.io.PrintWriter)

Example 3 with Identifiable

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

the class MLLoggerParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Likelihood likelihood = (Likelihood) xo.getElementFirstChild(LIKELIHOOD);
    // logEvery of zero only displays at the end
    int logEvery = xo.getAttribute(LOG_EVERY, 0);
    final PrintWriter pw = getLogFile(xo, getParserName());
    LogFormatter formatter = new TabDelimitedFormatter(pw);
    MLLogger logger = new MLLogger(likelihood, formatter, logEvery);
    if (xo.hasAttribute(TITLE)) {
        logger.setTitle(xo.getStringAttribute(TITLE));
    }
    for (int i = 0; i < xo.getChildCount(); i++) {
        Object child = xo.getChild(i);
        if (child instanceof Columns) {
            logger.addColumns(((Columns) child).getColumns());
        } else if (child instanceof Loggable) {
            logger.add((Loggable) child);
        } else if (child instanceof Identifiable) {
            logger.addColumn(new LogColumn.Default(((Identifiable) child).getId(), child));
        } else {
            logger.addColumn(new LogColumn.Default(child.getClass().toString(), child));
        }
    }
    return logger;
}
Also used : Likelihood(dr.inference.model.Likelihood) PrintWriter(java.io.PrintWriter) Identifiable(dr.util.Identifiable)

Example 4 with Identifiable

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

the class ColumnsParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    String label = xo.getAttribute(LABEL, "");
    final int sf = xo.getAttribute(SIGNIFICANT_FIGURES, -1);
    final int dp = xo.getAttribute(DECIMAL_PLACES, -1);
    final int width = xo.getAttribute(WIDTH, -1);
    String format = xo.getAttribute(FORMAT, "");
    ArrayList colList = new ArrayList();
    for (int i = 0; i < xo.getChildCount(); i++) {
        Object child = xo.getChild(i);
        LogColumn[] cols;
        if (child instanceof Loggable) {
            cols = ((Loggable) child).getColumns();
        } else if (child instanceof Identifiable) {
            cols = new LogColumn[] { new LogColumn.Default(((Identifiable) child).getId(), child) };
        } else {
            cols = new LogColumn[] { new LogColumn.Default(child.getClass().toString(), child) };
        }
        if (format.equals(PERCENT)) {
            for (int k = 0; k < cols.length; ++k) {
                if (cols[k] instanceof NumberColumn) {
                    cols[k] = new PercentColumn((NumberColumn) cols[k]);
                }
            }
        } else if (format.equals(BOOL)) {
            for (int k = 0; k < cols.length; ++k) {
                if (cols[k] instanceof NumberColumn) {
                    cols[k] = new BooleanColumn((NumberColumn) cols[k]);
                }
            }
        }
        for (int j = 0; j < cols.length; j++) {
            if (!label.equals("")) {
                if (cols.length > 1) {
                    cols[j].setLabel(label + Integer.toString(j + 1));
                } else {
                    cols[j].setLabel(label);
                }
            }
            if (cols[j] instanceof NumberColumn) {
                if (sf != -1) {
                    ((NumberColumn) cols[j]).setSignificantFigures(sf);
                }
                if (dp != -1) {
                    ((NumberColumn) cols[j]).setDecimalPlaces(dp);
                }
            }
            if (width > 0) {
                cols[j].setMinimumWidth(width);
            }
            colList.add(cols[j]);
        }
    }
    LogColumn[] columns = new LogColumn[colList.size()];
    colList.toArray(columns);
    return new Columns(columns);
}
Also used : ArrayList(java.util.ArrayList) Identifiable(dr.util.Identifiable)

Aggregations

Identifiable (dr.util.Identifiable)4 PrintWriter (java.io.PrintWriter)2 ArrayList (java.util.ArrayList)2 BeastVersion (dr.app.beast.BeastVersion)1 GeneralDataType (dr.evolution.datatype.GeneralDataType)1 Likelihood (dr.inference.model.Likelihood)1 Property (dr.util.Property)1 Date (java.util.Date)1