@Description( "Find taxonomic names matching the passed in prefix. Optimized for use with autocomplete boxes on webforms.") @PluginTarget(GraphDatabaseService.class) @Deprecated public Representation autocompleteQuery( @Source GraphDatabaseService graphDb, @Description( "A string containing a single name (or partial name prefix) to be queried against the db") @Parameter(name = "queryString") String queryString, @Description("The name of the taxonomic context to be searched") @Parameter(name = "contextName", optional = true) String contextName, @Description( "A boolean indicating whether or not suppressed taxa should be included in the results. Defaults to false.") @Parameter(name = "includeDubious", optional = true) Boolean includeDubious) throws ContextNotFoundException, ParseException { includeDubious = includeDubious == null ? false : includeDubious; GraphDatabaseAgent gdb = new GraphDatabaseAgent(graphDb); Taxonomy taxonomy = new Taxonomy(gdb); // attempt to get the named context, will throw exception if a name is supplied but no // corresponding context can be found TaxonomyContext context = null; if (contextName != null) { context = taxonomy.getContextByName(contextName); } SingleNamePrefixQuery query = new SingleNamePrefixQuery(taxonomy, context); query.setIncludeDubious(includeDubious); TNRSResults results = query.setQueryString(queryString).runQuery().getResults(); // return results if they exist, if (results.iterator().hasNext()) { List<TNRSMatch> matches = results.iterator().next().getMatches().getMatchList(); if (!matches.isEmpty()) { Collections.sort(matches, new MatchComparator()); return TNRSResultsRepresentation_v1.getMatchSetRepresentationForAutocompleteBox( matches.iterator()); } } // else just return an empty JSON array return OTRepresentationConverter.convert(new LinkedList<String>()); }
@Description( "Find the least inclusive taxonomic context defined for the provided set of taxon names.") @PluginTarget(GraphDatabaseService.class) @Deprecated public Representation getContextForNames( @Source GraphDatabaseService graphDb, @Description("An array of taxon names to be queried.") @Parameter(name = "names", optional = false) String[] names) { // initialize objects GraphDatabaseAgent gdb = new GraphDatabaseAgent(graphDb); Taxonomy taxonomy = new Taxonomy(gdb); MultiNameContextQuery tnrs = new MultiNameContextQuery(taxonomy); // format input for the TNRS query object Map<Object, String> idNameMap = new HashMap<Object, String>(); for (String name : names) { idNameMap.put(name, name); } // call TNRS query, remembering names that could not be matched Collection<String> namesNotMatched = tnrs.setSearchStrings(idNameMap).inferContextAndReturnAmbiguousNames().values(); // if (nameIdsNotMatched.isEmpty()) { // nameIdsNotMatched = (Collection<Object>) new HashSet<Object>(); // } // get the inferred context TaxonomyContext inferredContext = tnrs.getContext(); // format results and finish ContextResult contextResult = new ContextResult(inferredContext, namesNotMatched); gdb.shutdownDb(); // return OpentreeRepresentationConverter.convert(contextResult); return TNRSResultsRepresentation_v1.getContextRepresentation(contextResult); }
@Description( "Return information about potential matches to a set of taxonomic names. This service uses taxonomic contexts to narrow search parameters and help disambiguate synonyms and homonyms. ") @PluginTarget(GraphDatabaseService.class) @Deprecated public Representation contextQuery( @Source GraphDatabaseService graphDb, @Description( "A comma-delimited string of taxon names to be queried against the taxonomy db. This is an alternative to the use of the 'names' parameter") @Parameter(name = "queryString", optional = true) String queryString, @Description("The name of the taxonomic context to be searched") @Parameter(name = "contextName", optional = true) String contextName, @Description( "An array of taxon names to be queried. This is an alternative to the use of the 'queryString' parameter") @Parameter(name = "names", optional = true) String[] names, @Description( "An array of ids to use for identifying names. These will be set in the id field of each name result. If this parameter is used, ids will be treated as strings.") @Parameter(name = "idStrings", optional = true) String[] idStrings, @Description( "An array of ids to use for identifying names. These will be set in the id field of each name result. If this parameter is used, ids will be treated as ints.") @Parameter(name = "idInts", optional = true) Long[] idInts, @Description("A boolean indicating whether or not to include deprecated taxa in the search.") @Parameter(name = "includeDeprecated", optional = true) Boolean includeDeprecated, @Description( "A boolean indicating whether or not to perform approximate string (a.k.a. \"fuzzy\") matching. Will greatly improve speed if this is turned OFF (false). By default, however, it is on (true).") @Parameter(name = "doApproximateMatching", optional = true) Boolean doFuzzyMatching, @Description( "Whether to include so-called 'dubious' taxa--those which are not accepted by OTT.") @Parameter(name = "includeDubious", optional = true) Boolean includeDubious) throws ContextNotFoundException { GraphDatabaseAgent gdb = new GraphDatabaseAgent(graphDb); Taxonomy taxonomy = new Taxonomy(gdb); // including deprecated and dubious names are turned OFF by default includeDeprecated = includeDeprecated == null ? false : includeDeprecated; includeDubious = includeDubious == null ? false : includeDubious; // fuzzy matching is turned ON by default doFuzzyMatching = doFuzzyMatching == null ? true : doFuzzyMatching; // check valid input on names String[] searchStrings = null; if (queryString != null && names == null) { searchStrings = queryString.split("\\s*\\,\\s*"); } else if (names != null && queryString == null) { searchStrings = names; } else { throw new IllegalArgumentException( "You must provide exactly one of either the 'queryString' or 'names' parameters"); } // check valid input on ids Object[] ids = null; if (idInts != null && idStrings == null) { ids = new Object[idInts.length]; int i = 0; for (Long id : idInts) { ids[i++] = id; } } else if (idStrings != null && idInts == null) { ids = idStrings; } else if (idStrings != null && idInts != null) { throw new IllegalArgumentException( "You may provide at most one of the 'idStrings' an 'idInts' parameters"); } else { ids = searchStrings; } Map<Object, String> idNameMap = new HashMap<Object, String>(); if (ids != null) { int i = 0; for (String name : searchStrings) { try { idNameMap.put(ids[i++], name); } catch (ArrayIndexOutOfBoundsException ex) { throw new IllegalArgumentException("There are more names than ids"); } } if (ids.length > i) { throw new IllegalArgumentException("There are more ids than names"); } } else { for (String name : searchStrings) { idNameMap.put(name, name); } } if (idNameMap.keySet().size() > MAX_QUERY_STRINGS) { HashMap<String, Object> results = new HashMap<String, Object>(); results.put("status", "error"); results.put( "message", "Queries containing more than " + MAX_QUERY_STRINGS + " strings are not supported. You may submit multiple" + "smaller queries to avoid this limit."); return OTRepresentationConverter.convert(results); } // attempt to get the named context, will throw exception if a name is supplied but no // corresponding context can be found TaxonomyContext context = null; boolean useAutoInference = true; if (contextName != null) { context = taxonomy.getContextByName(contextName); useAutoInference = false; } MultiNameContextQuery mncq = new MultiNameContextQuery(taxonomy); TNRSResults results = mncq.setSearchStrings(idNameMap) .setContext(context) .setAutomaticContextInference(useAutoInference) .setIncludeDubious(includeDubious) .setIncludeDeprecated(includeDeprecated) .setDoFuzzyMatching(doFuzzyMatching) .runQuery() .getResults(); gdb.shutdownDb(); return TNRSResultsRepresentation_v1.getResultsRepresentation(results); }