Search in sources :

Example 1 with InvalidQueryException

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

the class TumorTypesUtil method findTumorType.

private List<TumorType> findTumorType(TumorType allTumorTypes, TumorType currentTumorType, List<TumorType> matchedTumorTypes, String key, String keyword, Boolean exactMatch, Boolean includeParent) throws InvalidQueryException {
    Map<String, TumorType> childrenTumorTypes = currentTumorType.getChildren();
    Boolean match = false;
    Map<String, List<String>> externalReferences = currentTumorType.getExternalReferences();
    if (includeParent == null) {
        includeParent = false;
    }
    if (exactMatch == null) {
        exactMatch = true;
    }
    switch(key) {
        case "code":
            if (exactMatch) {
                match = currentTumorType.getCode() == null ? false : currentTumorType.getCode().equalsIgnoreCase(keyword);
            } else {
                match = currentTumorType.getCode() == null ? false : StringUtils.containsIgnoreCase(currentTumorType.getCode(), keyword);
            }
            break;
        case "color":
            if (exactMatch) {
                match = currentTumorType.getColor() == null ? false : currentTumorType.getColor().equalsIgnoreCase(keyword);
            } else {
                match = currentTumorType.getColor() == null ? false : StringUtils.containsIgnoreCase(currentTumorType.getColor(), keyword);
            }
            break;
        case "name":
            if (exactMatch) {
                match = currentTumorType.getName() == null ? false : currentTumorType.getName().equalsIgnoreCase(keyword);
            } else {
                match = currentTumorType.getName() == null ? false : StringUtils.containsIgnoreCase(currentTumorType.getName(), keyword);
            }
            break;
        case "nci":
            if (exactMatch) {
                match = externalReferences.containsKey("NCI") ? ListUtil.hasMatchingElementIgnoreCase(externalReferences.get("NCI"), keyword) : false;
            } else {
                match = externalReferences.containsKey("NCI") ? ListUtil.hasElementWhichContainsStringIgnoreCase(externalReferences.get("NCI"), keyword) : false;
            }
            break;
        case "umls":
            if (exactMatch) {
                match = externalReferences.containsKey("UMLS") ? ListUtil.hasMatchingElementIgnoreCase(externalReferences.get("UMLS"), keyword) : false;
            } else {
                match = externalReferences.containsKey("UMLS") ? ListUtil.hasElementWhichContainsStringIgnoreCase(externalReferences.get("UMLS"), keyword) : false;
            }
            break;
        case "maintype":
            if (exactMatch) {
                match = currentTumorType == null ? false : (currentTumorType.getMainType() == null ? false : currentTumorType.getMainType().equals(keyword));
            } else {
                match = currentTumorType == null ? false : (currentTumorType.getMainType() == null ? false : StringUtils.containsIgnoreCase(currentTumorType.getMainType(), keyword));
            }
            break;
        case "level":
            try {
                Integer keywordAsInteger = Integer.parseInt(keyword);
                match = currentTumorType == null ? false : (currentTumorType.getLevel() == null ? false : (currentTumorType.getLevel() == null ? false : currentTumorType.getLevel().equals(keywordAsInteger)));
            } catch (NumberFormatException e) {
                throw new InvalidQueryException("'" + keyword + "' is not a valid level.  Level must be an integer.");
            }
            break;
        default:
            // we should never get here because we have already checked if this is a valid key
            throw new InvalidQueryException(buildInvalidQueryTypeError(key));
    }
    if (match) {
        TumorType tumorType = new TumorType();
        tumorType.setCode(currentTumorType.getCode());
        tumorType.setColor(currentTumorType.getColor());
        tumorType.setName(currentTumorType.getName());
        tumorType.setMainType(currentTumorType.getMainType());
        tumorType.setExternalReferences(currentTumorType.getExternalReferences());
        tumorType.setTissue(currentTumorType.getTissue());
        // the results of search operations are flat lists of TumorTypes .. no nested children so do not setChildren()
        tumorType.setParent(currentTumorType.getParent());
        tumorType.setHistory(currentTumorType.getHistory());
        tumorType.setRevocations(currentTumorType.getRevocations());
        tumorType.setPrecursors(currentTumorType.getPrecursors());
        tumorType.setLevel(currentTumorType.getLevel());
        matchedTumorTypes.add(tumorType);
        if (includeParent) {
            String code = currentTumorType.getParent();
            List<TumorType> parentTumorTypes = findTumorType(allTumorTypes, allTumorTypes, new ArrayList<TumorType>(), "code", code, true, true);
            if (parentTumorTypes != null && parentTumorTypes.size() > 0) {
                TumorType parentNode = parentTumorTypes.get(0);
                matchedTumorTypes.add(parentNode);
                if (parentNode.getParent() != null) {
                    matchedTumorTypes = findTumorType(allTumorTypes, allTumorTypes, matchedTumorTypes, "code", parentNode.getParent(), true, true);
                }
            }
        }
    }
    if (childrenTumorTypes.size() > 0) {
        Iterator it = childrenTumorTypes.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            matchedTumorTypes = findTumorType(allTumorTypes, (TumorType) pair.getValue(), matchedTumorTypes, key, keyword, exactMatch, includeParent);
        }
    }
    return new ArrayList<>(new LinkedHashSet<>(matchedTumorTypes));
}
Also used : TumorType(org.mskcc.oncotree.model.TumorType) InvalidQueryException(org.mskcc.oncotree.error.InvalidQueryException)

Example 2 with InvalidQueryException

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

the class TumorTypesApi method tumorTypesSearchTypeQueryQueryGet.

@ApiOperation(value = "Tumor Types", notes = "", response = TumorType.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "An array of tumor types"), @ApiResponse(code = 400, message = "Bad request"), @ApiResponse(code = 404, message = "Could not find tumor types"), @ApiResponse(code = 503, message = "Required data source unavailable") })
@RequestMapping(value = "/search/{type}/{query}", produces = { APPLICATION_JSON_VALUE }, method = RequestMethod.GET)
public Iterable<TumorType> tumorTypesSearchTypeQueryQueryGet(@ApiParam(value = "Query type. It could be 'code', 'name', 'mainType', 'level', 'nci', 'umls' or 'color'.", required = true) @PathVariable("type") String type, @ApiParam(value = "The query content", required = true) @PathVariable("query") String query, @ApiParam(value = "The version of tumor types. For example, " + VersionUtil.DEFAULT_VERSION + ". Please see the versions api documentation for released versions.") @RequestParam(value = "version", required = false) String version, @ApiParam(value = "If it sets to true, it will only return one element array.", defaultValue = "true") @RequestParam(value = "exactMatch", required = false, defaultValue = "true") Boolean exactMatch, @ApiParam(value = "Tumor type levels. 1-5. By default, it doesn't includes tissue which is the primary level.", defaultValue = "2,3,4,5") @RequestParam(value = "levels", required = false, defaultValue = "1,2,3,4,5") String levels) {
    List<TumorType> matchedTumorTypes = new ArrayList<>();
    Version v = (version == null) ? versionUtil.getDefaultVersion() : versionUtil.getVersion(version);
    // Cache in tumor types in case no data present
    cacheUtil.getTumorTypesByVersion(v);
    matchedTumorTypes = v == null ? new ArrayList<TumorType>() : tumorTypesUtil.findTumorTypesByVersion(type, query, exactMatch, v, false);
    // check that user did not do a "level" query, but is doing a "levels" filter
    if (!type.toLowerCase().equals("level") && !StringUtils.isBlank(levels)) {
        List<String> ls = Arrays.asList(levels.split(","));
        List<Integer> levelList = new ArrayList<>();
        for (String l : ls) {
            try {
                Integer level = new Integer(Integer.parseInt(l.trim()));
                levelList.add(level);
            } catch (NumberFormatException e) {
                throw new InvalidQueryException("'" + l + "' is not a valid level.  Level must be an integer.");
            }
        }
        matchedTumorTypes = tumorTypesUtil.filterTumorTypesByLevel(matchedTumorTypes, levelList);
    }
    if (matchedTumorTypes.isEmpty()) {
        throw new TumorTypesNotFoundException("No tumor types found matching supplied query");
    }
    return matchedTumorTypes;
}
Also used : TumorTypesNotFoundException(org.mskcc.oncotree.error.TumorTypesNotFoundException) InvalidQueryException(org.mskcc.oncotree.error.InvalidQueryException) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

InvalidQueryException (org.mskcc.oncotree.error.InvalidQueryException)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 TumorTypesNotFoundException (org.mskcc.oncotree.error.TumorTypesNotFoundException)1 TumorType (org.mskcc.oncotree.model.TumorType)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1