Example usage for org.dom4j Node selectNodes

List of usage examples for org.dom4j Node selectNodes

Introduction

In this page you can find the example usage for org.dom4j Node selectNodes.

Prototype

List<Node> selectNodes(String xpathExpression);

Source Link

Document

selectNodes evaluates an XPath expression and returns the result as a List of Node instances or String instances depending on the XPath expression.

Usage

From source file:edu.scripps.fl.pubchem.EUtilsFactory.java

License:Apache License

public List<Relation> getRelations(Document document) {
    String fromDb = document.selectSingleNode("/eLinkResult/LinkSet/DbFrom").getText();
    String idStr = document.selectSingleNode("/eLinkResult/LinkSet/IdList/Id").getText();
    Long id = Long.parseLong(idStr);
    List<Node> linkSetDbs = document.selectNodes("/eLinkResult/LinkSet/LinkSetDb");
    ArrayList<Relation> list = new ArrayList<Relation>();
    for (Node linkSetDb : linkSetDbs) {
        String toDb = linkSetDb.selectSingleNode("DbTo").getText();
        String linkName = linkSetDb.selectSingleNode("LinkName").getText();
        List<Node> ids = linkSetDb.selectNodes("Link/Id");
        list.ensureCapacity(list.size() + ids.size());
        for (Node idNode : ids) {
            long relatedId = Long.parseLong(idNode.getText());
            if (id == relatedId)
                continue;
            Relation relation = new Relation();
            relation.setRelationName(linkName);
            relation.setFromDb(fromDb);/*from   w  w w  .  j av  a2  s  .co  m*/
            relation.setFromId(id);
            relation.setToDb(toDb);
            relation.setToId(relatedId);
            list.add(relation);
        }
    }
    return list;
}

From source file:edu.scripps.fl.pubchem.EUtilsFactory.java

License:Apache License

public Collection<Long> getIds(Long id, String fromDb, String toDb) throws Exception {
    Document document = EUtilsFactory.getInstance().getDocument(
            "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi", "dbfrom", fromDb, "db", toDb, "id",
            "" + id);
    List<Node> linkSetDbs = document.selectNodes("/eLinkResult/LinkSet/LinkSetDb");
    Set<Long> relatedIds = new HashSet<Long>();
    for (Node linkSetDb : linkSetDbs) {
        String linkName = linkSetDb.selectSingleNode("LinkName").getText();
        List<Node> ids = linkSetDb.selectNodes("Link/Id");
        for (Node idNode : ids) {
            long relatedId = Long.parseLong(idNode.getText());
            if (id.equals(relatedId))
                continue;
            relatedIds.add(relatedId);/*from   w  ww .  j a v  a 2s  . c om*/
        }
    }
    return relatedIds;
}

From source file:edu.scripps.fl.pubchem.PubChemFactory.java

License:Apache License

public void populateAssayFromSummaryDocument(PCAssay assay, Node docSumNode) throws Exception {
    Node errorNode = docSumNode.selectSingleNode("error");
    if (errorNode != null)
        throw new Exception("Entrez error: " + errorNode.getText());

    List<Node> list = docSumNode.selectNodes("*");
    String uid = docSumNode.valueOf("@uid");
    assay.setAID(Integer.parseInt(uid));
    for (Node node : list) {
        String name = node.getName();
        Object value = node.getText();
        if (node.selectNodes("*").size() > 0) {

        } else {/*from  w ww .  ja  v  a 2s .  co  m*/
            String property = propMap.getProperty(name);
            if (null != property) {
                Class clazz = PropertyUtils.getPropertyType(assay, property);
                if (clazz.isAssignableFrom(Date.class))
                    value = parseDate(value);
                BeanUtils.setProperty(assay, property, value);
            } else {
                if (!unprocessedProperties.containsKey(name)) {
                    unprocessedProperties.put(name, "");
                    log.warn(String.format("Cannot determine PCAssay bean property '%s'", name));
                }
            }
        }
    }
    String desc = assay.getDescription();
    // eutils summary description doesn't contain new lines 
    // so don't update it if it already has a value (when we populate via xml first).
    if (desc == null || "".equals(desc) || !desc.contains("\n")) {
        Node node = docSumNode.selectSingleNode("AssayDescription");
        assay.setDescription(node.getText());
    }
    return;
}

From source file:edu.scripps.fl.pubchem.PubChemFactory.java

License:Apache License

public int populateRelations(Session session, Document document) throws IOException, DocumentException {
    String fromDb = document.selectSingleNode("/eLinkResult/LinkSet/DbFrom").getText();
    String idStr = document.selectSingleNode("/eLinkResult/LinkSet/IdList/Id").getText();
    Long id = Long.parseLong(idStr);
    List<Node> linkSetDbs = document.selectNodes("/eLinkResult/LinkSet/LinkSetDb");

    Transaction trx = session.beginTransaction();
    int counter = 0;
    for (Node linkSetDb : linkSetDbs) {
        String toDb = linkSetDb.selectSingleNode("DbTo").getText();
        String linkName = linkSetDb.selectSingleNode("LinkName").getText();
        List<Node> ids = linkSetDb.selectNodes("Link/Id");
        for (Node idNode : ids) {
            long relatedId = Long.parseLong(idNode.getText());
            if (id == relatedId)
                continue;
            Relation relation = new Relation();
            relation.setRelationName(linkName);
            relation.setFromDb(fromDb);//w w  w.  jav a  2  s  . co  m
            relation.setFromId(id);
            relation.setToDb(toDb);
            relation.setToId(relatedId);
            session.save(relation);
            if (counter++ % 100 == 0) {
                trx.commit();
                session.clear(); // cache grows hugely during very large
                // inserts
                trx = session.beginTransaction();
            }
        }
    }
    trx.commit();
    return counter;
}

From source file:edu.scripps.fl.pubchem.PubChemFactory.java

License:Apache License

public Collection<Long> getNeighbors(Long id) throws Exception {
    Document document = EUtilsWebSession.getInstance().getDocument(
            "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi", "dbfrom", "pcassay", "id", "" + id,
            "linkname", "pcassay_pcassay_neighbor_list");
    List<Node> linkSetDbs = document.selectNodes("/eLinkResult/LinkSet/LinkSetDb");
    Set<Long> relatedIds = new HashSet();
    int counter = 0;
    for (Node linkSetDb : linkSetDbs) {
        String linkName = linkSetDb.selectSingleNode("LinkName").getText();
        List<Node> ids = linkSetDb.selectNodes("Link/Id");
        for (Node idNode : ids) {
            Long relatedId = Long.parseLong(idNode.getText());
            relatedIds.add(relatedId);//  w w w .j  a v  a2s . co m
        }
    }
    return relatedIds;
}

From source file:edu.scripps.fl.pubchem.PubChemFactory.java

License:Apache License

public List<Relation> getRelations(Document document) {
    String fromDb = document.selectSingleNode("/eLinkResult/LinkSet/DbFrom").getText();
    String idStr = document.selectSingleNode("/eLinkResult/LinkSet/IdList/Id").getText();
    Long id = Long.parseLong(idStr);
    List<Node> linkSetDbs = document.selectNodes("/eLinkResult/LinkSet/LinkSetDb");
    ArrayList<Relation> list = new ArrayList();
    for (Node linkSetDb : linkSetDbs) {
        String toDb = linkSetDb.selectSingleNode("DbTo").getText();
        String linkName = linkSetDb.selectSingleNode("LinkName").getText();
        List<Node> ids = linkSetDb.selectNodes("Link/Id");
        list.ensureCapacity(list.size() + ids.size());
        for (Node idNode : ids) {
            long relatedId = Long.parseLong(idNode.getText());
            if (id == relatedId)
                continue;
            Relation relation = new Relation();
            relation.setRelationName(linkName);
            relation.setFromDb(fromDb);//from ww  w. j  ava 2 s  .co m
            relation.setFromId(id);
            relation.setToDb(toDb);
            relation.setToId(relatedId);
            list.add(relation);
        }
    }
    return list;
}

From source file:edu.scripps.fl.pubchem.PubChemXMLParserFactory.java

License:Apache License

protected void populateAssayResultsFromXML(PCAssay assay, Node assayDescriptionNode) throws Exception {
    List<PCAssayResult> results = null;
    List<Node> assayResultNodes = assayDescriptionNode
            .selectNodes("../../../PC-AssaySubmit_data/PC-AssayResults");
    if (assayResultNodes.size() == 0)
        return;/*from w  w  w.j ava2s . c  om*/
    else
        results = new ArrayList<PCAssayResult>(assayResultNodes.size());

    List<PCAssayColumn> testedCols = assay.getTestedColumns();
    PCAssayColumn activeColumn = assay.getActiveColumn();

    for (Node resultNode : assayResultNodes) {
        PCAssayResult result = new PCAssayResult();

        String val = resultNode.selectSingleNode("PC-AssayResults_sid").valueOf("text()");
        result.setSID(Long.parseLong(val));

        val = resultNode.selectSingleNode("PC-AssayResults_outcome").valueOf("@value");
        val = val.substring(0, 1).toUpperCase() + val.substring(1);
        result.setOutcome(val);

        val = resultNode.selectSingleNode("PC-AssayResults_rank").valueOf("text()");
        result.setRankScore(Integer.parseInt(val));

        List<Node> assayDataNodes = resultNode.selectNodes("PC-AssayResults_data/PC-AssayData");

        List<String> all = GrowthList.decorate(new ArrayList(assay.getColumns().size() - 2));
        result.setAllValues(all);
        for (Node node : assayDataNodes) {
            val = node.valueOf("PC-AssayData_tid/text()");
            int index = Integer.parseInt(val) - 1;
            val = node.selectSingleNode(".//*[starts-with(name(),'PC-AssayData_value_')]").getText();
            all.set(index, val);
        }

        // if a dose response assay with a marked activeConcentration
        if ("confirmatory".equals(assay.getActivityOutcomeMethod()) && activeColumn != null) {
            String actConc = all.get(activeColumn.getTID() - 1);
            if (null != actConc && !"".equals(actConc)) {
                result.setPrimaryValue(Double.valueOf(actConc));
                result.setPrimaryColumn(activeColumn);

                PCAssayColumn qualCol = assay.getQualifierColumn();
                if (qualCol != null) {
                    String qual = all.get(qualCol.getTID() - 1);
                    if (!"".equals(qual))
                        result.setQualifier(qual);
                }
            }

        } else if ("screening".equals(assay.getActivityOutcomeMethod()) && testedCols.size() > 0) {
            PCAssayColumn testedCol = testedCols.get(0);
            String value = all.get(testedCol.getTID() - 1);
            result.setPrimaryColumn(testedCol);
            if (null != value && !"".equals(value))
                if ("float".equals(testedCol.getType()) || "int".equals(testedCol.getType()))
                    result.setPrimaryValue(Double.parseDouble(value));
                else
                    result.setPrimaryValueAsString(value);
        }

        // put all testedConcentration columns into an ordered array. Interested in numbers here only.
        result.getTestedValues().clear();
        for (int ii = 0; ii < testedCols.size(); ii++) {
            PCAssayColumn testedCol = testedCols.get(ii);
            String value = all.get(testedCol.getTID() - 1);
            if (null != value && !"".equals(value)) {
                try {
                    Double dbl = Double.parseDouble(value);
                    result.getTestedValues().set(ii, dbl);
                } catch (NumberFormatException ex) {
                    // if not a number then don't worry about it.
                }
            }
        }
        assay.getResults().add(result);
    }

}

From source file:edu.scripps.fl.pubchem.PubChemXMLParserFactory.java

License:Apache License

protected PCAssay populateAssayFromXMLNode(Node topNode) throws Exception {
    //      String assayDescPath = "PC-AssaySubmit_assay/PC-AssaySubmit_assay_descr/PC-AssayDescription";
    Node assayDescNode = null;
    if (topNode.getName().equals("PC-AssayDescription"))
        assayDescNode = topNode;/*from  w w w .ja v  a  2s  .c o m*/
    else {
        assayDescNode = topNode.selectSingleNode(".//PC-AssayDescription");
    }
    if (assayDescNode == null)
        throw new Exception(
                String.format("Cannot find PC-AssayDescription node in provided node %s", topNode.getPath()));

    Node node = assayDescNode.selectSingleNode("PC-AssayDescription_aid/PC-ID/PC-ID_id");
    Integer aid = new Integer(node.getText());

    try {
        PCAssay assay = new PCAssay();
        if (aid > 0)
            assay.setAID(aid);

        node = assayDescNode.selectSingleNode("PC-AssayDescription_aid/PC-ID/PC-ID_version");
        Integer version = new Integer(node.getText());
        assay.setVersion(version);

        node = assayDescNode.selectSingleNode("PC-AssayDescription_revision");
        Integer revision = new Integer(node.getText());
        assay.setRevision(revision);

        Node trackingNode = assayDescNode
                .selectSingleNode("PC-AssayDescription_aid-source/PC-Source/PC-Source_db/PC-DBTracking");

        node = trackingNode.selectSingleNode("PC-DBTracking_name");
        assay.setSourceName(node.getText());

        node = trackingNode.selectSingleNode("PC-DBTracking_source-id/Object-id/Object-id_str");
        assay.setExtRegId(node.getText());

        // hold until date
        node = trackingNode.selectSingleNode("PC-DBTracking_date");
        if (node != null) {
            String year = node.selectSingleNode("Date/Date_std/Date-std/Date-std_year").getText();
            String month = node.selectSingleNode("Date/Date_std/Date-std/Date-std_month").getText();
            String day = node.selectSingleNode("Date/Date_std/Date-std/Date-std_day").getText();
            if (DEBUGGING)
                log.info("year: " + year + " month: " + month + " day: " + day);
            Calendar calendar = Calendar.getInstance();
            calendar.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));
            assay.setHoldUntilDate(calendar.getTime());
            if (DEBUGGING)
                log.info(calendar.getTime().toString());
        }

        node = assayDescNode.selectSingleNode("PC-AssayDescription_name");
        assay.setName(node.getText());

        List<Node> nodes = assayDescNode
                .selectNodes("PC-AssayDescription_description/PC-AssayDescription_description_E");
        assay.setDescription(join(nodes, separator));

        nodes = assayDescNode.selectNodes("PC-AssayDescription_protocol/PC-AssayDescription_protocol_E");
        assay.setProtocol(join(nodes, separator));

        nodes = assayDescNode.selectNodes("PC-AssayDescription_comment/PC-AssayDescription_comment_E");
        assay.setComment(join(nodes, separator));

        node = assayDescNode.selectSingleNode("PC-AssayDescription_activity-outcome-method");
        if (node != null)
            assay.setActivityOutcomeMethod(node.valueOf("@value"));

        node = assayDescNode
                .selectSingleNode("PC-AssayDescription_grant-number/PC-AssayDescription_grant-number_E");
        if (node != null)
            assay.setGrantNumber(node.getText());

        node = assayDescNode.selectSingleNode("PC-AssayDescription_project-category");
        if (node != null)
            assay.setProjectCategory(node.valueOf("@value"));

        assay.getAssayXRefs().removeAll(assay.getAssayXRefs());

        nodes = assayDescNode.selectNodes("PC-AssayDescription_xref/PC-AnnotatedXRef");
        handleXRefs(assay, null, nodes);

        nodes = assayDescNode.selectNodes("PC-AssayDescription_target/PC-AssayTargetInfo");
        handleTargetXRefs(assay, null, nodes);

        handlePanels(assay, assayDescNode);

        handleColumns(assay, assayDescNode);

        handleComments(assay, assayDescNode);

        return assay;
    } catch (Exception ex) {
        throw new RuntimeException("Problem with AID " + aid, ex);
    }
}

From source file:edu.scripps.fl.pubchem.PubChemXMLParserFactory.java

License:Apache License

protected void handleColumns(PCAssay assay, Node assayDescNode) {
    Map<Integer, PCAssayColumn> map = new HashMap();
    for (PCAssayColumn col : assay.getColumns())
        map.put(col.getTID(), col);//from  w w  w. j a v  a  2  s . c om

    ensureColumn(assay, -1, "Outcome", "string");
    ensureColumn(assay, 0, "Score", "float");

    Map<Integer, PCAssayPanel> mapPanels = new HashMap();
    for (PCAssayPanel panel : assay.getPanels())
        mapPanels.put(panel.getPanelNumber(), panel);

    List<Node> nodes = assayDescNode.selectNodes("PC-AssayDescription_results/PC-ResultType");
    for (Node n : nodes) {
        String tid = n.selectSingleNode("PC-ResultType_tid").getText();
        String name = n.selectSingleNode("PC-ResultType_name").getText();
        String type = n.selectSingleNode("PC-ResultType_type").valueOf("@value");
        PCAssayColumn column = ensureColumn(assay, Integer.parseInt(tid), name, type);

        Node node = n.selectSingleNode("PC-ResultType_unit");
        if (node != null)
            column.setUnit(node.valueOf("@value"));

        List<Node> descNodes = n.selectNodes("PC-ResultType_description/PC-ResultType_description_E");
        column.setDescription(join(descNodes, separator));
        if (DEBUGGING)
            log.info("Column description: " + join(descNodes, separator));

        node = n.selectSingleNode("PC-ResultType_ac");
        if (node != null)
            column.setActiveConcentration("true".equals(node.valueOf("@value")));

        node = n.selectSingleNode("PC-ResultType_tc");
        if (node != null) {
            Node node2 = node.selectSingleNode("PC-ConcentrationAttr/PC-ConcentrationAttr_dr-id");
            if (node2 != null) {
                column.setCurvePlotLabel(Integer.parseInt(node2.getText()));
            }
            String testedConc = node.selectSingleNode("PC-ConcentrationAttr/PC-ConcentrationAttr_concentration")
                    .getText();
            column.setTestedConcentration(Double.parseDouble(testedConc));
            String testedUnit = node.selectSingleNode("PC-ConcentrationAttr/PC-ConcentrationAttr_unit")
                    .valueOf("@value");
            column.setTestedConcentrationUnit(testedUnit);
        }

        node = n.selectSingleNode("PC-ResultType_panel-info/PC-AssayPanelTestResult");
        if (node != null) {
            String panelId = node.selectSingleNode("PC-AssayPanelTestResult_mid").getText();
            PCAssayPanel panel = mapPanels.get(Integer.parseInt(panelId));
            column.setPanel(panel);
            String panelColumnType = node.selectSingleNode("PC-AssayPanelTestResult_readout-annot")
                    .valueOf("@value");
            column.setPanelReadoutType(panelColumnType);
        }
    }
}

From source file:edu.scripps.fl.pubchem.web.pug.PUGRequest.java

License:Apache License

private static void setResponseIds2(Document document, Type type, Collection<Object> ids) throws Exception {
    Node node = document.selectSingleNode("//PCT-ID-List_db");
    node.setText(type.getDatabase());// w  ww  .ja  v  a  2s  .  c o  m
    node = document.selectSingleNode(".//PCT-ID-List_uids");
    for (Node child : (List<Node>) node.selectNodes("*"))
        child.detach();
    for (Object id : ids) {
        Element aidElem = DocumentHelper.createElement("PCT-ID-List_uids_E");
        aidElem.setText(id.toString());
        ((Element) node).add(aidElem);
    }
}