Skip to content

Commit 6f0f746

Browse files
authored
Merge pull request #29 from PathwayCommons/suhyma
Implementing keyword search JS script
2 parents 07ba9fa + 775d08a commit 6f0f746

4 files changed

Lines changed: 100 additions & 15 deletions

File tree

.vscode/launch.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@
5252
"--pretty"
5353
],
5454
"console": "integratedTerminal"
55+
},
56+
{
57+
"type": "node",
58+
"request": "launch",
59+
"name": "DataSearch",
60+
"skipFiles": [
61+
"<node_internals>/**"
62+
],
63+
"program": "${workspaceFolder}/src/data-search.js",
64+
"console": "integratedTerminal"
5565
}
5666
]
5767
}

scripts/date.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { format, sub } from 'date-fns';
22

3-
const startOffset = { months: 1 };
3+
const startOffset = { days: 1 };
44
const arg = process.argv[2];
55
const now = new Date();
66

src/cli.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,43 @@ const writeText = async (text, file) => await writeFile(file, text);
1717
const printText = text => console.log(text);
1818
const getPrettyText = (articles, queryString, options) => prettyArticles(articles, queryString, options);
1919

20-
async function search (queryString, options) {
21-
const searcher = new Search();
20+
export async function search (queryString, options) {
21+
try {
22+
const searcher = new Search();
2223

23-
const articles = await getInput(options);
24+
const articles = await getInput(options);
2425

25-
await searcher.articles(articles);
26+
await searcher.articles(articles);
2627

27-
const res = await searcher.search(queryString, {
28-
combineWith: options.strict ? 'AND' : 'OR'
29-
});
28+
const res = await searcher.search(queryString, {
29+
combineWith: options.strict ? 'AND' : 'OR'
30+
});
3031

31-
await sendOutput(res, options, queryString);
32+
await sendOutput(res, options, queryString);
33+
34+
return res;
35+
} catch (err) {
36+
console.error(`Error in search: ${err}`);
37+
throw err;
38+
}
3239
}
3340

34-
async function download (startDate, endDate, options) {
35-
const source = options.source ?? 'biorxiv';
41+
export async function download (startDate, endDate, options) {
42+
try {
43+
const source = options.source ?? 'biorxiv';
3644

37-
const res = await performDownload(source, startDate, endDate);
45+
const res = await performDownload(source, startDate, endDate);
3846

39-
await sendOutput(res, options);
47+
await sendOutput(res, options);
48+
49+
return res;
50+
} catch (err) {
51+
console.error(`Error in download: ${err}`);
52+
throw err;
53+
}
4054
}
4155

42-
async function sendOutput (res, options, queryString) {
56+
export async function sendOutput (res, options, queryString) {
4357
if (options.reverse) {
4458
res = res.reverse();
4559
}
@@ -103,4 +117,4 @@ async function main () {
103117
await program.parseAsync();
104118
}
105119

106-
main();
120+
// main();

src/data-search.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#! /usr/bin/env node
2+
3+
// here we will convert the search.sh script into JS
4+
import { format, sub } from 'date-fns';
5+
import fs from 'fs/promises';
6+
import { download, search, sendOutput } from './cli.js';
7+
8+
const CATEGORY_ID = 'alzheimers-disease';
9+
const DATA_DIRECTORY = 'example-data';
10+
11+
const MEDRXIV_SOURCE = 'medrxiv';
12+
const BIORXIV_SOURCE = 'biorxiv';
13+
14+
const now = new Date();
15+
const startOffset = { days: 1 };
16+
const START_DATE = format(sub(now, startOffset), 'yyyy-MM-dd');
17+
const END_DATE = format(now, 'yyyy-MM-dd');
18+
19+
const BIORXIV_FILE = `${DATA_DIRECTORY}/${END_DATE}_${BIORXIV_SOURCE}.json`;
20+
const MEDRXIV_FILE = `${DATA_DIRECTORY}/${END_DATE}_${MEDRXIV_SOURCE}.json`;
21+
const COMBINED_FILE = `${DATA_DIRECTORY}/${END_DATE}.json`;
22+
const OUTPUT_FILE = `${DATA_DIRECTORY}/${CATEGORY_ID}.json`;
23+
24+
// Getting all latest articles from BiorXiv
25+
console.log(`Fetching from ${BIORXIV_SOURCE} between ${START_DATE} and ${END_DATE}`);
26+
fs.open(BIORXIV_FILE, 'w');
27+
const bioOptions = {
28+
source: BIORXIV_SOURCE,
29+
output: BIORXIV_FILE
30+
};
31+
const bioData = await download(START_DATE, END_DATE, bioOptions);
32+
33+
// Getting all latest articles from MedrXiv
34+
console.log(`Fetching from ${MEDRXIV_SOURCE} between ${START_DATE} and ${END_DATE}`);
35+
fs.open(MEDRXIV_FILE, 'w');
36+
const medOptions = {
37+
source: MEDRXIV_SOURCE,
38+
output: MEDRXIV_FILE
39+
};
40+
const medData = await download(START_DATE, END_DATE, medOptions);
41+
42+
// Creating a JSON with all the results, both sources combined
43+
console.log('Combining results...');
44+
fs.open(COMBINED_FILE, 'w');
45+
const combinedData = bioData.concat(medData);
46+
const combinedOptions = {
47+
output: COMBINED_FILE
48+
};
49+
await sendOutput(combinedData, combinedOptions);
50+
51+
// Search for the QUERY keyword in all the downloaded articles & compile the related articles
52+
const QUERY = 'alzheimer';
53+
fs.open(OUTPUT_FILE, 'w');
54+
const outputOptions = {
55+
input: COMBINED_FILE,
56+
output: OUTPUT_FILE
57+
};
58+
console.log(`Searching for ${QUERY}`);
59+
const searchHits = await search(QUERY, outputOptions);
60+
const numSearchHits = searchHits.length;
61+
console.log(`Found ${numSearchHits} hits`);

0 commit comments

Comments
 (0)