Skip to content

Commit

Permalink
web: pubmedtool now checks if IDs are being reused
Browse files Browse the repository at this point in the history
  • Loading branch information
liamappelbe committed Jul 20, 2024
1 parent 4cd6749 commit 78cf9be
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
39 changes: 32 additions & 7 deletions vetupdates/pubmedtool.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</script>
<script src="vetupdates_search.js"></script>
<script>
const {metadataRequest, kMajorTags} = module.exports;
const {metadataRequest, kMajorTags, getIdsRequest} = module.exports;

let domWrap = null;
let domLoad = null;
Expand Down Expand Up @@ -138,10 +138,12 @@
}

class ArticleResult {
constructor(id, article, err) {
constructor(id, aid, article, err) {
this.id = id;
this.aid = aid;
this.article = article;
this.err = err;
this.seen = false;
}
}

Expand All @@ -156,12 +158,34 @@
try {
const aid = await asyncPubMedConvertId(id);
const art = await getArticleImpl(aid);
return new ArticleResult(id, new ArticleInfo(
return new ArticleResult(id, aid, new ArticleInfo(
aid, art.title, art.authors, art.getDateText(), art.doi,
art.journalName, art.journalAbbr, art.getLink()), null);
} catch (err) {
return new ArticleResult(id, null, err);
return new ArticleResult(id, null, null, err);
}
}

async function checkUsed(results) {
const pmids = [];
const pmcids = [];
for (const ar of results) {
if (ar.aid == null) continue;
if (ar.aid.pmid != null) {
pmids.push(ar.aid.pmid);
} else if (ar.aid.pmcid != null) {
pmcids.push(ar.aid.pmcid);
}
}
const known = new Set(await getIdsRequest(pmids, pmcids));
for (const ar of results) {
if (ar.aid == null) continue;
if (known.has(parseInt(ar.aid.pmid)) ||
known.has(-parseInt(ar.aid.pmcid))) {
ar.seen = true;
}
}
return results;
}

domWrap.classList.add('loading');
Expand All @@ -173,15 +197,16 @@
state.errorText = '';
saveState();

const results = await Promise.all(state.ids.map(getArticle));
const results = await checkUsed(await Promise.all(state.ids.map(getArticle)));
domWrap.classList.remove('loading');
domLoad.classList.remove('loading');

for (const ar of results) {
if (ar.err != null) {
addError(`Invalid ID: ${ar.id}`);
console.error(ar.id, ar.err);
continue;
} else if (ar.seen) {
addError(`Reused ID: ${ar.id}`);
}
}

Expand Down Expand Up @@ -519,7 +544,7 @@ <h1>PubMed Formatter</h1>
<br/>
Errors:
<br/>
<textarea id="errors" rows="6" cols="40"></textarea>
<textarea id="errors" rows="8" cols="40"></textarea>
</div>
<div class="middle">
<span>Elements: <a onclick="selectTextArea('tags')">[SELECT]</a></span>
Expand Down
13 changes: 13 additions & 0 deletions vetupdates/vetupdates_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const kApiEndpoint =
kLocalDev ? 'http://localhost:8080' : 'https://squeakysqueenoctopus.com';
const kAdvancedSearchApi = kApiEndpoint + '/zpqk';
const kSearchApi = kApiEndpoint + '/gqmo';
const kGetIdsApi = kApiEndpoint + '/rmdl';
const kMetadataApi = kApiEndpoint + '/meta';
const kRetries = 10;
const kMajorTagText = 'Topics';
Expand Down Expand Up @@ -104,6 +105,12 @@ async function searchRequest(q) {
return JSON.parse(await asyncRequest(kSearchApi + '?' + q));
}

async function getIdsRequest(pmids, pmcids) {
const q = [encodeIdQuery('p', pmids), encodeIdQuery('c', pmcids)];
return JSON.parse(await asyncRequest(
kGetIdsApi + '?' + q.filter(w => w.length > 0).join('&')));
}

function getUniqueElementId() {
while (true) {
const newId = `id_${Math.floor(1e9 * Math.random())}`;
Expand Down Expand Up @@ -240,6 +247,11 @@ function cleanMetadata(a) {
return b;
}

function encodeIdQuery(q, v) {
if (v.length == 0) return '';
return q + '=' + encodeURIComponent(v.join(','));
}

function encodeTextQuery(q, t) {
if (t.value.length == 0) return '';
return q + '=' +
Expand Down Expand Up @@ -396,6 +408,7 @@ if (typeof (module) != 'undefined') {
metadataRequest,
advancedSearchRequest,
searchRequest,
getIdsRequest,
kMajorTags,
};
}
Expand Down

0 comments on commit 78cf9be

Please sign in to comment.