Search in sources :

Example 1 with OncoTreeNode

use of org.mskcc.oncotree.topbraid.OncoTreeNode 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 OncoTreeNode

use of org.mskcc.oncotree.topbraid.OncoTreeNode in project oncotree by cBioPortal.

the class MSKConceptCache method resetCache.

@EventListener(ApplicationReadyEvent.class)
// call every Sunday at 3am
@Scheduled(cron = "0 0 3 * * SUN")
private // the actual returned MSKConcept is not necessary for webapp deployment
void resetCache() throws Exception {
    logger.info("resetCache() -- attempting to refresh Crosswalk MSKConcept cache");
    HashMap<String, MSKConcept> latestOncoTreeCodesToMSKConcepts = new HashMap<String, MSKConcept>();
    ArrayList<Version> oncoTreeVersions = new ArrayList<Version>();
    try {
        oncoTreeVersions = oncoTreePersistentCache.getOncoTreeVersionsFromPersistentCache();
    } catch (RuntimeException e) {
        throw new FailedCacheRefreshException("Failed to refresh MSKConceptCache, unable to load verisons...");
    }
    for (Version version : oncoTreeVersions) {
        ArrayList<OncoTreeNode> oncoTreeNodes = new ArrayList<OncoTreeNode>();
        try {
            oncoTreeNodes = oncoTreePersistentCache.getOncoTreeNodesFromPersistentCache(version);
        } catch (RuntimeException e) {
            throw new FailedCacheRefreshException("Failed to refresh MSKConceptCache : " + e.toString());
        }
        for (OncoTreeNode node : oncoTreeNodes) {
            // skip querying repeated nodes/MSKConcepts
            if (!latestOncoTreeCodesToMSKConcepts.containsKey(node.getCode())) {
                // pull from crosswalk first
                oncoTreePersistentCache.updateMSKConceptInPersistentCache(node.getCode());
                MSKConcept concept = oncoTreePersistentCache.getMSKConceptFromPersistentCache(node.getCode());
                latestOncoTreeCodesToMSKConcepts.put(node.getCode(), concept);
            }
        }
    }
    // save all MSKConcepts at once
    try {
        oncoTreePersistentCache.backupMSKConceptPersistentCache(latestOncoTreeCodesToMSKConcepts);
    } catch (Exception e) {
        logger.error("Unable to backup MSKConcepts in EHCache");
        slackUtil.sendSlackNotification("*OncoTree Error* - MSKConceptCache backup failed." + e.getMessage());
    }
}
Also used : FailedCacheRefreshException(org.mskcc.oncotree.utils.FailedCacheRefreshException) Version(org.mskcc.oncotree.model.Version) OncoTreeNode(org.mskcc.oncotree.topbraid.OncoTreeNode) TopBraidException(org.mskcc.oncotree.topbraid.TopBraidException) FailedCacheRefreshException(org.mskcc.oncotree.utils.FailedCacheRefreshException) Scheduled(org.springframework.scheduling.annotation.Scheduled) EventListener(org.springframework.context.event.EventListener)

Example 3 with OncoTreeNode

use of org.mskcc.oncotree.topbraid.OncoTreeNode in project oncotree by cBioPortal.

the class OncotreeTestConfig method setupOncotreeRepositoryMockResponse.

private ArrayList<OncoTreeNode> setupOncotreeRepositoryMockResponse() {
    String[] rawTestValueSource = getRawTestValueSource();
    final int valuesPerCase = 6;
    if (rawTestValueSource.length % valuesPerCase != 0) {
        throw new RuntimeException("Error : malformed rawTestValueSource");
    }
    final int caseCount = rawTestValueSource.length / valuesPerCase;
    if (caseCount < 1) {
        throw new RuntimeException("Error : no test cases defined in rawTestValueSource");
    }
    ArrayList<OncoTreeNode> tmpOncoTreeRepositoryMockResponse = new ArrayList<>();
    for (int pos = 0; pos < rawTestValueSource.length; pos = pos + valuesPerCase) {
        OncoTreeNode nextNode = new OncoTreeNode();
        nextNode.setCode(rawTestValueSource[pos]);
        nextNode.setName(rawTestValueSource[pos + 1]);
        nextNode.setMainType(rawTestValueSource[pos + 2]);
        nextNode.setColor(rawTestValueSource[pos + 3]);
        nextNode.setParentCode(rawTestValueSource[pos + 4]);
        nextNode.setURI(rawTestValueSource[pos + 5]);
        tmpOncoTreeRepositoryMockResponse.add(nextNode);
    }
    return tmpOncoTreeRepositoryMockResponse;
}
Also used : OncoTreeNode(org.mskcc.oncotree.topbraid.OncoTreeNode)

Example 4 with OncoTreeNode

use of org.mskcc.oncotree.topbraid.OncoTreeNode in project oncotree by cBioPortal.

the class TumorTypesUtil method loadFromRepository.

private static Map<String, TumorType> loadFromRepository(Version version) throws InvalidOncoTreeDataException {
    List<OncoTreeNode> oncoTreeNodes = oncoTreeRepository.getOncoTree(version);
    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);
        }
    }
    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.setNCI(crosswalks.get("NCI"));
            }
            List<String> umlsIds = new ArrayList<String>();
            if (mskConcept.getConceptIds() != null) {
                for (String mskConceptId : mskConcept.getConceptIds()) {
                    umlsIds.add(mskConceptId.replace("MSK", "C"));
                }
            }
            tumorType.setUMLS(umlsIds);
            tumorType.setHistory(new ArrayList(mskConcept.getHistory()));
        }
        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 : MSKConcept(org.mskcc.oncotree.crosswalk.MSKConcept) OncoTreeNode(org.mskcc.oncotree.topbraid.OncoTreeNode) TumorType(org.mskcc.oncotree.model.TumorType)

Example 5 with OncoTreeNode

use of org.mskcc.oncotree.topbraid.OncoTreeNode 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)

Aggregations

OncoTreeNode (org.mskcc.oncotree.topbraid.OncoTreeNode)7 TumorType (org.mskcc.oncotree.model.TumorType)3 MSKConcept (org.mskcc.oncotree.crosswalk.MSKConcept)2 InvalidOncoTreeDataException (org.mskcc.oncotree.error.InvalidOncoTreeDataException)2 Version (org.mskcc.oncotree.model.Version)2 TopBraidException (org.mskcc.oncotree.topbraid.TopBraidException)2 ArrayList (java.util.ArrayList)1 CacheManager (javax.cache.CacheManager)1 InvalidVersionException (org.mskcc.oncotree.error.InvalidVersionException)1 FailedCacheRefreshException (org.mskcc.oncotree.utils.FailedCacheRefreshException)1 Cacheable (org.springframework.cache.annotation.Cacheable)1 EventListener (org.springframework.context.event.EventListener)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1