-
Notifications
You must be signed in to change notification settings - Fork 1
/
autocompleteFunctions.js
104 lines (91 loc) · 3.8 KB
/
autocompleteFunctions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// import { handleFetchErrors } from "./utilities";
// const handleNodeNormError = (response, setterFunction) => {
// if(response.status === 502)
// setterFunction(['node_norm_error']);
// else
// setterFunction([]);
// }
// Returns array of terms based on user input
export const getAutocompleteTerms = (inputText, setLoadingAutocomplete, setAutoCompleteItems,
autocompleteFunctions, limitType = '', limitPrefixes = [], endpoint) => {
if(inputText) {
console.log(`fetching '${inputText}'`);
setLoadingAutocomplete(true);
const formatData = { input: inputText.toLowerCase() };
newFetchNodesFromInputText(inputText, limitType, limitPrefixes, endpoint)
.then(response => response.json())
.then(nodes => {
let newNodes = {};
for(const node of nodes) {
newNodes[node.curie] = node.synonyms;
}
formatData.resolved = newNodes;
return nodes;
})
.then(normalizedNodes => autocompleteFunctions.annotate(normalizedNodes))
.then(annotatedNodes => autocompleteFunctions.format(annotatedNodes, formatData))
.then(autocompleteItems => {
// Truncate items in case of too many matches
autocompleteItems = autocompleteItems.slice(0,40);
console.log('formatted autocomplete items:', autocompleteItems)
setAutoCompleteItems(autocompleteItems);
setLoadingAutocomplete(false);
})
.catch((error) => {
console.log(error)
setLoadingAutocomplete(false);
});
}
}
// Do a node search based on user input text
const newFetchNodesFromInputText = async (inputText, type, prefixes, endpoint) => {
let prefixString = "&only_prefixes=";
if(prefixes.length > 0) {
prefixes = prefixes.join('|');
prefixString = `${prefixString}${prefixes}`;
} else {
prefixString = "";
}
const nameResolverRequestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
};
return fetch(`${endpoint}?string=${inputText}&offset=0&limit=100&biolink_type=${type}${prefixString}`, nameResolverRequestOptions)
}
export const filterAndSortExamples = (arr, type, direction = null) => {
if(direction) {
return arr.filter((query)=>query.type === type && query.direction === direction)
.sort((a, b) => (a.name > b.name) ? 1: -1);
}
return arr.filter((query)=>query.type === type).sort((a, b) => (a.name > b.name) ? 1: -1);
}
// Do a node search based on user input text
// const fetchNodesFromInputText = async (inputText) => {
// const nameResolverRequestOptions = {
// method: 'POST',
// headers: { 'Content-Type': 'application/json' },
// };
// return fetch(`https://name-resolution-sri.renci.org/lookup?string=${inputText}&offset=0&limit=200`, nameResolverRequestOptions)
// }
// Normalize nodes to get the canonical CURIE associated with the node
// const fetchNormalizedNodesFromNodes = async (nodes) => {
// const curies = nodesToCuries(nodes);
// console.log('Curies returned from Name resolver:', curies);
// const body = {
// curies: curies,
// conflate: true
// }
// const nodeNormalizerRequestOptions = {
// method: 'POST',
// headers: { 'Content-Type': 'application/json' },
// body: JSON.stringify(body)
// };
// return fetch('https://nodenorm.transltr.io/1.3/get_normalized_nodes', nodeNormalizerRequestOptions);
// }
// Filter normalized nodes based on the filter for the query type
// const filterNormalizedNodes = async (normalizedNodes, autocompleteFilter) => {
// console.log(Object.keys(normalizedNodes).length, 'full data from node normalizer:', normalizedNodes);
// return Promise.resolve(Object.values(normalizedNodes).filter(autocompleteFilter));
// }
// Get the CURIEs associated with an object of the form {<CURIE>: <NODE>}
// const nodesToCuries = (nodes) => { return Object.keys(nodes); }