9b395f5cc3
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
Build Chrome Extension / build (push) Has been cancelled
Trigger Website Rebuild (Docs Updated) / dispatch (push) Has been cancelled
46 lines
2.0 KiB
JavaScript
46 lines
2.0 KiB
JavaScript
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
import { EmptyResultError } from '@jackwener/opencli/errors';
|
|
import { RELATED_COLUMNS, eutilsFetch, fetchSummaryRows, requireBoundedInt, requirePmid } from './utils.js';
|
|
|
|
cli({
|
|
site: 'pubmed',
|
|
name: 'related',
|
|
access: 'read',
|
|
description: 'Find articles related to a PubMed article',
|
|
domain: 'pubmed.ncbi.nlm.nih.gov',
|
|
strategy: Strategy.PUBLIC,
|
|
browser: false,
|
|
args: [
|
|
{ name: 'pmid', positional: true, required: true, help: 'PubMed ID, e.g. 37780221' },
|
|
{ name: 'limit', type: 'int', default: 20, help: 'Max results (1-100)' },
|
|
{ name: 'score', type: 'boolean', default: false, help: 'Show similarity scores when available' },
|
|
],
|
|
columns: RELATED_COLUMNS,
|
|
func: async (args) => {
|
|
const pmid = requirePmid(args.pmid);
|
|
const limit = requireBoundedInt(args.limit, 20, 100);
|
|
const result = await eutilsFetch('elink', {
|
|
id: pmid,
|
|
dbfrom: 'pubmed',
|
|
cmd: 'neighbor_score',
|
|
linkname: 'pubmed_pubmed',
|
|
}, { label: 'pubmed related' });
|
|
const rawLinks = result?.linksets?.[0]?.linksetdbs?.[0]?.links;
|
|
if (!Array.isArray(rawLinks) || rawLinks.length === 0) {
|
|
throw new EmptyResultError('pubmed related', `No related articles found for PMID ${pmid}.`);
|
|
}
|
|
const links = rawLinks
|
|
.map(link => typeof link === 'string' ? { id: link, score: null } : { id: String(link?.id ?? ''), score: Number.isFinite(Number(link?.score)) ? Number(link.score) : null })
|
|
.filter(link => link.id && link.id !== pmid)
|
|
.slice(0, limit);
|
|
if (links.length === 0) {
|
|
throw new EmptyResultError('pubmed related', `No related articles found for PMID ${pmid}.`);
|
|
}
|
|
const rows = await fetchSummaryRows(links.map(link => link.id), 'pubmed related summary');
|
|
return rows.map((row, index) => ({
|
|
...row,
|
|
score: args.score ? links[index].score : null,
|
|
}));
|
|
},
|
|
});
|