@jbrowse/plugin-linear-genome-view
Version:
JBrowse 2 linear genome view
71 lines (70 loc) • 2.81 kB
JavaScript
import BaseResult from '@jbrowse/core/TextSearch/BaseResults';
import { dedupe, getSession } from '@jbrowse/core/util';
export async function navToOption({ option, model, assemblyName, }) {
const location = option.getLocation();
const trackId = option.getTrackId();
if (location) {
await model.navToLocString(location, assemblyName, 0.2);
if (trackId) {
model.showTrack(trackId);
}
}
}
export async function handleSelectedRegion({ input, model, assembly, }) {
const allRefs = assembly.allRefNamesWithLowerCase || [];
const assemblyName = assembly.name;
if (input.split(' ').every(entry => checkRef(entry, allRefs))) {
await model.navToLocString(input, assembly.name);
}
else {
const searchScope = model.searchScope(assemblyName);
const { textSearchManager } = getSession(model);
const results = await fetchResults({
queryString: input,
searchType: 'exact',
searchScope,
rankSearchResults: model.rankSearchResults,
textSearchManager,
assembly,
});
if (results.length > 1) {
model.setSearchResults(results, input.toLowerCase(), assemblyName);
}
else if (results.length === 1) {
await navToOption({
option: results[0],
model,
assemblyName,
});
}
else {
await model.navToLocString(input, assemblyName);
}
}
}
export function checkRef(str, allRefs) {
const [ref, rest] = splitLast(str, ':');
return (allRefs.includes(str) ||
(allRefs.includes(ref) && !Number.isNaN(Number.parseInt(rest, 10))));
}
export async function fetchResults({ queryString, searchType, searchScope, rankSearchResults, textSearchManager, assembly, }) {
var _a;
if (!textSearchManager) {
console.warn('No text search manager');
}
const textSearchResults = await (textSearchManager === null || textSearchManager === void 0 ? void 0 : textSearchManager.search({
queryString,
searchType,
}, searchScope, rankSearchResults));
const refNameResults = (_a = assembly === null || assembly === void 0 ? void 0 : assembly.allRefNames) === null || _a === void 0 ? void 0 : _a.filter(ref => ref.toLowerCase().startsWith(queryString.toLowerCase())).slice(0, 10).map(r => new BaseResult({ label: r }));
return dedupe([...(refNameResults || []), ...(textSearchResults || [])], elt => elt.getId());
}
export function splitLast(str, split) {
const lastIndex = str.lastIndexOf(split);
if (lastIndex === -1) {
return [str, ''];
}
const before = str.slice(0, lastIndex);
const after = str.slice(lastIndex + 1);
return [before, after];
}