Search in sources :

Example 1 with InvalidOncoTreeDataException

use of org.mskcc.oncotree.error.InvalidOncoTreeDataException in project oncotree by cBioPortal.

the class CacheUtil method resetCache.

public void resetCache() throws FailedCacheRefreshException {
    logger.info("resetCache() -- refilling tumor types cache");
    Map<Version, Map<String, TumorType>> latestTumorTypesCache = new HashMap<>();
    ArrayList<Version> oncoTreeVersions = new ArrayList<Version>();
    ArrayList<String> failedVersions = new ArrayList<String>();
    // use this to store and look up previous oncoTree codes
    HashMap<String, ArrayList<String>> topBraidURIsToOncotreeCodes = new HashMap<String, ArrayList<String>>();
    boolean failedOncoTreeVersionsCacheRefresh = false;
    boolean failedVersionedOncoTreeNodesCacheRefresh = false;
    // update EHCache with newest versions available
    try {
        oncoTreePersistentCache.updateOncoTreeVersionsInPersistentCache();
    } catch (TopBraidException exception) {
        logger.error("resetCache() -- failed to pull versions from repository");
        failedOncoTreeVersionsCacheRefresh = true;
    }
    // attmpt to get versions from EHCache -- RuntimeException thrown when ALL options are exhausted (ehcache, topbraid, backup)
    try {
        oncoTreeVersions = oncoTreePersistentCache.getOncoTreeVersionsFromPersistentCache();
    } catch (RuntimeException e) {
        throw new FailedCacheRefreshException("No data found in specified backup cache location...");
    }
    if (!failedOncoTreeVersionsCacheRefresh) {
        try {
            oncoTreePersistentCache.backupOncoTreeVersionsPersistentCache(oncoTreeVersions);
        } catch (Exception e) {
            logger.error("Unable to backup versions EHCache");
            slackUtil.sendSlackNotification("*OncoTree Error* - OncoTreeVersionsCache backup failed." + e.getMessage());
        }
    }
    // versions are ascending by release date
    for (Version version : oncoTreeVersions) {
        Map<String, TumorType> latestTumorTypes = new HashMap<String, TumorType>();
        ArrayList<OncoTreeNode> oncoTreeNodes = new ArrayList<OncoTreeNode>();
        failedVersionedOncoTreeNodesCacheRefresh = false;
        if (version != null) {
            try {
                oncoTreePersistentCache.updateOncoTreeNodesInPersistentCache(version);
            } catch (TopBraidException e) {
                logger.error("resetCache() -- failed to pull tumor types for version '" + version.getVersion() + "' from repository : " + e.toString());
                failedVersionedOncoTreeNodesCacheRefresh = true;
            }
            // store versions for which nodes cannot be successfully loaded (either due to inaccessible data or invalid data)
            try {
                oncoTreeNodes = oncoTreePersistentCache.getOncoTreeNodesFromPersistentCache(version);
            } catch (RuntimeException e) {
                failedVersions.add(version.getVersion());
                logger.warn("resetCache() -- failed to retrieve version '" + version.getVersion() + "'");
                continue;
            }
            if (!failedVersionedOncoTreeNodesCacheRefresh) {
                try {
                    oncoTreePersistentCache.backupOncoTreeNodesPersistentCache(oncoTreeNodes, version);
                } catch (Exception e) {
                    logger.error("Unable to backup oncotree nodes EHCache");
                    slackUtil.sendSlackNotification("*OncoTree Error* - OncoTreeNodesCache backup failed." + e.getMessage());
                }
            }
            try {
                latestTumorTypes = tumorTypesUtil.getAllTumorTypesFromOncoTreeNodes(oncoTreeNodes, version, topBraidURIsToOncotreeCodes);
            } catch (InvalidOncoTreeDataException exception) {
                logger.error("Unable to get tumor types from oncotree nodes");
                failedVersions.add(version.getVersion());
                logger.warn("resetCache() -- failed to retrieve version : " + version.getVersion() + " : " + exception.toString());
                continue;
            }
        }
        latestTumorTypesCache.put(version, latestTumorTypes);
    }
    // Fail the cache refresh if required oncotree version cannot be constructed or if no versions can be constructed
    if (latestTumorTypesCache.keySet().size() == 0) {
        logger.error("resetCache() -- failed to pull a single valid OncoTree version");
        throw new FailedCacheRefreshException("Failed to refresh cache - no versions constructed");
    }
    if (failedVersions.contains(requiredOncotreeVersion)) {
        logger.error("resetCache() -- failed to pull required oncotree version: " + requiredOncotreeVersion);
        throw new FailedCacheRefreshException("Failed to refresh cache - required version not constructed");
    }
    if (failedVersions.size() > 0) {
        slackUtil.sendSlackNotification("OncoTree successfully recached `" + requiredOncotreeVersion + "`, but ran into issues with the following versions: " + String.join(", ", failedVersions));
    }
    tumorTypesCache = latestTumorTypesCache;
    // cache is filled, but indicate to endpoint that we did not successfully pull updated data from TopBraid
    if (failedOncoTreeVersionsCacheRefresh || failedVersionedOncoTreeNodesCacheRefresh) {
        throw new FailedCacheRefreshException("Failed to refresh cache - composite error");
    } else {
        dateOfLastCacheRefresh = new Date();
        logger.info("resetCache() -- successfully reset cache from repository");
    }
}
Also used : InvalidOncoTreeDataException(org.mskcc.oncotree.error.InvalidOncoTreeDataException) TopBraidException(org.mskcc.oncotree.topbraid.TopBraidException) OncoTreeNode(org.mskcc.oncotree.topbraid.OncoTreeNode) TopBraidException(org.mskcc.oncotree.topbraid.TopBraidException) InvalidVersionException(org.mskcc.oncotree.error.InvalidVersionException) InvalidOncoTreeDataException(org.mskcc.oncotree.error.InvalidOncoTreeDataException) TumorType(org.mskcc.oncotree.model.TumorType) Version(org.mskcc.oncotree.model.Version)

Example 2 with InvalidOncoTreeDataException

use of org.mskcc.oncotree.error.InvalidOncoTreeDataException in project oncotree by cBioPortal.

the class TumorTypesUtil method getAllTumorTypesFromOncoTreeNodes.

public Map<String, TumorType> getAllTumorTypesFromOncoTreeNodes(List<OncoTreeNode> oncoTreeNodes, Version version, HashMap<String, ArrayList<String>> topBraidURIsToOncotreeCodes) throws InvalidOncoTreeDataException {
    Map<String, TumorType> allNodes = new HashMap<>();
    HashSet<String> rootNodeCodeSet = new HashSet<>();
    HashSet<String> duplicateCodeSet = new HashSet<>();
    // construct basic nodes
    for (OncoTreeNode thisNode : oncoTreeNodes) {
        logger.debug("OncoTreeNode: code='" + thisNode.getCode() + "', name='" + thisNode.getName() + "'");
        TumorType tumorType = initTumorType(thisNode, version);
        String thisNodeCode = tumorType.getCode();
        if (allNodes.containsKey(thisNodeCode)) {
            duplicateCodeSet.add(thisNodeCode);
        }
        allNodes.put(thisNodeCode, tumorType);
        if (hasNoParent(tumorType)) {
            rootNodeCodeSet.add(thisNodeCode);
        }
        // get all codes defined so far for this topbraid uri and save in history
        if (topBraidURIsToOncotreeCodes.containsKey(thisNode.getURI())) {
            // do not add this code to the history, but add any others
            HashSet<String> allButThisNode = new HashSet<String>(topBraidURIsToOncotreeCodes.get(thisNode.getURI()));
            allButThisNode.remove(thisNode.getCode());
            tumorType.setHistory(new ArrayList<String>(allButThisNode));
        } else {
            topBraidURIsToOncotreeCodes.put(thisNode.getURI(), new ArrayList<String>());
        }
        for (String topBraidURI : thisNode.getRevocations()) {
            String fullTopBraidURI = TOPBRAID_BASE_URI + topBraidURI;
            if (topBraidURIsToOncotreeCodes.containsKey(fullTopBraidURI)) {
                ArrayList<String> nodeHistory = topBraidURIsToOncotreeCodes.get(fullTopBraidURI);
                // last node is most recent for this URI
                tumorType.addRevocations(nodeHistory.get(nodeHistory.size() - 1));
            } else {
                logger.error("loadFromRepository() -- unknown topBraidURI " + fullTopBraidURI + " in revocations field for topBraidURI " + thisNode.getURI());
                throw new InvalidOncoTreeDataException("Unknown topBraidURI " + fullTopBraidURI + " in revocations field for topBraidURI " + thisNode.getURI());
            }
        }
        for (String topBraidURI : thisNode.getPrecursors()) {
            String fullTopBraidURI = TOPBRAID_BASE_URI + topBraidURI;
            if (topBraidURIsToOncotreeCodes.containsKey(fullTopBraidURI)) {
                ArrayList<String> nodeHistory = topBraidURIsToOncotreeCodes.get(fullTopBraidURI);
                // last node is most recent for this URI
                tumorType.addPrecursors(nodeHistory.get(nodeHistory.size() - 1));
            } else {
                logger.error("loadFromRepository() -- unknown topBraidURI " + fullTopBraidURI + " in precursors field for topBraidURI " + thisNode.getURI());
                throw new InvalidOncoTreeDataException("Unknown topBraidURI " + fullTopBraidURI + " in precursors field for topBraidURI " + thisNode.getURI());
            }
        }
        // now save this as onoctree code history for this topbraid uri
        topBraidURIsToOncotreeCodes.get(thisNode.getURI()).add(thisNode.getCode());
    }
    validateOncoTreeOrThrowException(rootNodeCodeSet, duplicateCodeSet, allNodes);
    // also set NCI and UMLS codes
    for (TumorType tumorType : allNodes.values()) {
        String thisNodeCode = tumorType.getCode();
        MSKConcept mskConcept = mskConceptCache.get(thisNodeCode);
        if (mskConcept != null) {
            HashMap<String, List<String>> crosswalks = mskConcept.getCrosswalks();
            if (crosswalks != null && crosswalks.containsKey("NCI")) {
                tumorType.setExternalReference("NCI", crosswalks.get("NCI"));
            }
            if (mskConcept.getConceptIds() != null) {
                for (String mskConceptId : mskConcept.getConceptIds()) {
                    tumorType.addExternalReference("UMLS", mskConceptId.replace("MSK", "C"));
                }
            }
        }
        if (rootNodeCodeSet.contains(thisNodeCode)) {
            // root node has no parent
            continue;
        }
        TumorType parent = allNodes.get(tumorType.getParent());
        parent.addChild(tumorType);
    }
    // set depth and tissue properties (root has tissue = null)
    String rootCode = rootNodeCodeSet.iterator().next();
    TumorType rootNode = allNodes.get(rootCode);
    setDepthAndTissue(rootNode, 0, null);
    // now that all children have a path of references to them from the root, return only the root node.
    allNodes.clear();
    allNodes.put(rootCode, rootNode);
    return allNodes;
}
Also used : TumorType(org.mskcc.oncotree.model.TumorType) MSKConcept(org.mskcc.oncotree.crosswalk.MSKConcept) InvalidOncoTreeDataException(org.mskcc.oncotree.error.InvalidOncoTreeDataException) OncoTreeNode(org.mskcc.oncotree.topbraid.OncoTreeNode)

Example 3 with InvalidOncoTreeDataException

use of org.mskcc.oncotree.error.InvalidOncoTreeDataException in project oncotree by cBioPortal.

the class TumorTypesUtil method validateOncoTreeOrThrowException.

private void validateOncoTreeOrThrowException(Set<String> rootNodeCodeSet, Set<String> duplicateCodeSet, Map<String, TumorType> allNodes) throws InvalidOncoTreeDataException {
    StringBuilder errorMessageBuilder = new StringBuilder();
    // check for one root node
    if (rootNodeCodeSet.size() == 0) {
        errorMessageBuilder.append("\toncotree has no root node (a node where parent is empty)\n");
    } else if (rootNodeCodeSet.size() > 1) {
        errorMessageBuilder.append("\toncotree has more than one root node (nodes where parent is empty):\n");
        for (String code : rootNodeCodeSet) {
            errorMessageBuilder.append("\t\t" + code + "\n");
        }
    }
    // check for no duplicated OncoTree codes
    if (duplicateCodeSet.size() > 0) {
        errorMessageBuilder.append("\tduplication : OncoTree has more than one node containing each of the following OncoTree codes:\n");
        for (String code : duplicateCodeSet) {
            errorMessageBuilder.append("\t\t" + code + "\n");
        }
    }
    // check that non-root nodes have a parent in the set of all nodes
    Set<String> allCodeSet = allNodes.keySet();
    for (TumorType tumorType : allNodes.values()) {
        String thisNodeCode = tumorType.getCode();
        String parentCode = tumorType.getParent();
        if (rootNodeCodeSet.contains(thisNodeCode)) {
            // by definition, root nodes have no parent
            continue;
        } else {
            if (!allCodeSet.contains(parentCode)) {
                errorMessageBuilder.append("\tnode " + thisNodeCode + " has parent code '" + parentCode + "', which is not a code for any node in the tree\n");
            }
        }
    }
    if (errorMessageBuilder.length() > 0) {
        throw new InvalidOncoTreeDataException("Invalid OncoTree received:\n" + errorMessageBuilder.toString());
    }
}
Also used : TumorType(org.mskcc.oncotree.model.TumorType) InvalidOncoTreeDataException(org.mskcc.oncotree.error.InvalidOncoTreeDataException)

Aggregations

InvalidOncoTreeDataException (org.mskcc.oncotree.error.InvalidOncoTreeDataException)3 TumorType (org.mskcc.oncotree.model.TumorType)3 OncoTreeNode (org.mskcc.oncotree.topbraid.OncoTreeNode)2 MSKConcept (org.mskcc.oncotree.crosswalk.MSKConcept)1 InvalidVersionException (org.mskcc.oncotree.error.InvalidVersionException)1 Version (org.mskcc.oncotree.model.Version)1 TopBraidException (org.mskcc.oncotree.topbraid.TopBraidException)1