Skip to content

Commit

Permalink
Make tool id searchable in side panel search
Browse files Browse the repository at this point in the history
Tool ids are now searchable with query: "id:sample_id" or "tool_id:sample_id"
  • Loading branch information
ahmedhamidawan committed Aug 11, 2023
1 parent 9fccbe2 commit c8e79cc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
26 changes: 24 additions & 2 deletions client/src/components/Panels/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import { orderBy } from "lodash";
import levenshteinDistance from "utils/levenshtein";

const TOOL_ID_KEYS = ["id", "tool_id"];
const TOOLS_RESULTS_SORT_LABEL = "apiSort";
const TOOLS_RESULTS_SECTIONS_HIDE = ["Expression Tools"];
const STRING_REPLACEMENTS = [" ", "-", "(", ")", "'", ":"];
const STRING_REPLACEMENTS = [" ", "-", "(", ")", "'", ":", `"`];

// Converts filterSettings { key: value } to query = "key:value"
export function createWorkflowQuery(filterSettings) {
Expand Down Expand Up @@ -132,6 +133,11 @@ export function hasResults(results) {
export function searchToolsByKeys(tools, keys, query, usesDL = false) {
let returnedTools = [];
let closestTerm = null;
const id = processForId(query, TOOL_ID_KEYS);
if (id) {
query = id;
keys = { id: 1 };
}
const queryValue = sanitizeString(query.trim().toLowerCase(), STRING_REPLACEMENTS);
const minimumQueryLength = 5; // for DL
for (const tool of tools) {
Expand Down Expand Up @@ -170,7 +176,7 @@ export function searchToolsByKeys(tools, keys, query, usesDL = false) {
}
}
// no results with string.match(): recursive call with usesDL
if (!usesDL && returnedTools.length == 0) {
if (!id && !usesDL && returnedTools.length == 0) {
return searchToolsByKeys(tools, keys, query, true);
}
// sorting results by indexed order of keys
Expand Down Expand Up @@ -279,6 +285,22 @@ function sanitizeString(value, targets = [], substitute = "") {
return sanitized.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

/**
* If the query is of the form "id:1234" (or "tool_id:1234"), return the id.
* Otherwise, return null.
* @param {String} query - the raw query
* @param {Array} keys - Optional: keys to check for (default: ["id"])
* @returns id or null
*/
function processForId(query, keys = ["id"]) {
for (const key of keys) {
if (query.includes(`${key}:`)) {
return query.split(`${key}:`)[1].trim();
}
}
return null;
}

function flattenToolsSection(section) {
const flattenTools = [];
if (section.elems) {
Expand Down
24 changes: 24 additions & 0 deletions client/src/components/Panels/utilities.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,30 @@ describe("test helpers in tool searching utilities", () => {
keys: { description: 1, name: 2 },
list: tempToolsList,
},
{
// id is not searchable if not identified by colon
q: "__ZIP_COLLECTION__",
expectedResults: [],
keys: { description: 1, name: 2 },
list: toolsList,
},
{
// id is searchable if provided "id:"
q: "id:__ZIP_COLLECTION__",
expectedResults: ["__ZIP_COLLECTION__"],
keys: { description: 1, name: 2 },
list: toolsList,
},
{
// id is searchable if provided "tool_id:"
q: "tool_id:umi_tools",
expectedResults: [
"toolshed.g2.bx.psu.edu/repos/iuc/umi_tools_extract/umi_tools_extract/1.1.2+galaxy2",
"umi_tools_reduplicate",
],
keys: { description: 1, name: 2 },
list: tempToolsList,
},
];
searches.forEach((search) => {
const { results } = searchToolsByKeys(flattenTools(search.list), search.keys, search.q);
Expand Down

0 comments on commit c8e79cc

Please sign in to comment.