diff --git a/__test__/base_transformer.test.ts b/__test__/base_transformer.test.ts index aa0bf0b..95172bd 100644 --- a/__test__/base_transformer.test.ts +++ b/__test__/base_transformer.test.ts @@ -57,41 +57,41 @@ describe("test base transformer", () => { test("Test _updatePublications function if pubmed id is prefixed", () => { const tf = new base_tf(input, {}); const fake = { - pubmed: "PMID:1233" + ref_pmid: "PMID:1233" } const res = tf._updatePublications(fake); - expect(res).not.toHaveProperty('pubmed'); + expect(res).not.toHaveProperty('ref_pmid'); expect(res.publications).toEqual(["PMID:1233"]); }) test("Test _updatePublications function if pubmed id is NOT prefixed", () => { const tf = new base_tf(input, {}); const fake = { - pubmed: 1233 + ref_pmid: 1233 } const res = tf._updatePublications(fake); - expect(res).not.toHaveProperty('pubmed'); + expect(res).not.toHaveProperty('ref_pmid'); expect(res.publications).toEqual(["PMID:1233"]) }) test("Test _updatePublications function if pmc id is prefixed", () => { const tf = new base_tf(input, {}); const fake = { - pmc: "PMC:1233" + ref_pmcid: "PMCID:1233" } const res = tf._updatePublications(fake); - expect(res).not.toHaveProperty('pmc'); - expect(res.publications).toEqual(["PMC:1233"]); + expect(res).not.toHaveProperty('ref_pmcid'); + expect(res.publications).toEqual(["PMCID:1233"]); }) test("Test _updatePublications function if pmc id is NOT prefixed", () => { const tf = new base_tf(input, {}); const fake = { - pmc: 123 + ref_pmcid: 123 } const res = tf._updatePublications(fake); - expect(res).not.toHaveProperty('pmc'); - expect(res.publications).toEqual(["PMC:123"]) + expect(res).not.toHaveProperty('ref_pmcid'); + expect(res.publications).toEqual(["PMCID:123"]) }) test("Test extractObjectIDs function if output id type not in result", () => { diff --git a/__test__/biothings_transformer.test.ts b/__test__/biothings_transformer.test.ts index 6308e39..93821c4 100644 --- a/__test__/biothings_transformer.test.ts +++ b/__test__/biothings_transformer.test.ts @@ -58,7 +58,7 @@ describe("test biothings transformer", () => { let tf = new biothings_tf(input, {}); let res = await tf.transform(); expect(res).toHaveLength(27); - expect(res[0]).not.toHaveProperty('pubmed'); + expect(res[0]).not.toHaveProperty('ref_pmid'); expect(res[0]).toHaveProperty('publications', ["PMID:21873635"]); }) }) diff --git a/src/transformers/biolink_transformer.ts b/src/transformers/biolink_transformer.ts index 29bef56..d1d6b12 100644 --- a/src/transformers/biolink_transformer.ts +++ b/src/transformers/biolink_transformer.ts @@ -15,12 +15,18 @@ export default class BiolinkTransformer extends BaseTransformer { rec['object'][prefix] = rec.object.id; } } - if (rec.publications === undefined || rec.publications.length === 0 || !(rec.publications[0]['id'].startsWith("PMID"))) { + if (rec.publications === undefined || rec.publications.length === 0) { delete rec.publications } else { - rec.publications = rec.publications.map(pub => { - return { "id": pub.id.split(':').slice(-1)[0] } - }) + const oldPublications = rec.publications; + rec.publications = []; + for (let oldPub of oldPublications) { + if (!oldPub?.id?.startsWith?.("PMID:")) { + continue; + } + + rec.publications.push({ id: oldPub.id.split(':').slice(-1)[0] }); + } } if (!("provided_by" in rec)) { delete rec.provided_by diff --git a/src/transformers/ctd_transformer.ts b/src/transformers/ctd_transformer.ts index 0b02ac1..e639e80 100644 --- a/src/transformers/ctd_transformer.ts +++ b/src/transformers/ctd_transformer.ts @@ -7,9 +7,15 @@ export default class CTDTransformer extends BaseTransformer { if (typeof item.PubMedIDs === "string") { item.PubMedIDs = item.PubMedIDs.split('|'); } + if (typeof item.PubMedIds === "string") { + item.PubMedIds = item.PubMedIds.split('|'); + } if (typeof item.DiseaseID === "string") { item.DiseaseID = item.DiseaseID.split(':').slice(-1)[0]; } + if (typeof item.DiseaseId === "string") { + item.DiseaseId = item.DiseaseId.split(':').slice(-1)[0]; + } return item; }); return { data: res }; diff --git a/src/transformers/transformer.ts b/src/transformers/transformer.ts index adb7fc3..21e3dcb 100644 --- a/src/transformers/transformer.ts +++ b/src/transformers/transformer.ts @@ -54,20 +54,84 @@ export default class BaseTransformer { } _updatePublications(mappedResponse: any) { - if ("pubmed" in mappedResponse) { - mappedResponse.pubmed = toArray(mappedResponse.pubmed); - mappedResponse.publications = mappedResponse.pubmed.map(item => - typeof item === "string" && item.toUpperCase().startsWith("PMID:") ? item.toUpperCase() : "PMID:" + item, - ); - delete mappedResponse.pubmed; + if (!Array.isArray(mappedResponse.publications)) { + mappedResponse.publications = []; } - if ("pmc" in mappedResponse) { - mappedResponse.pmc = toArray(mappedResponse.pmc); - mappedResponse.publications = mappedResponse.pmc.map(item => - typeof item === "string" && item.toUpperCase().startsWith("PMC:") ? item.toUpperCase() : "PMC:" + item, - ); - delete mappedResponse.pmc; + + const publicationTypes = [ + {prop: "ref_pmid", prefix: "PMID:", urls: ["http://www.ncbi.nlm.nih.gov/pubmed/", "http://europepmc.org/abstract/MED/", "https://www.ncbi.nlm.nih.gov/pubmed/"]}, + {prop: "ref_pmcid", prefix: "PMCID:", urls: ["http://www.ncbi.nlm.nih.gov/pmc/articles/", "http://europepmc.org/articles/"]}, + {prop: "ref_clinicaltrials", prefix: "clinicaltrials:", urls: ["https://clinicaltrials.gov/ct2/show/", "https://www.clinicaltrials.gov/ct2/show/"]}, + {prop: "ref_doi", prefix: "doi:", urls: ["https://doi.org/", "http://www.nejm.org/doi/full/", "https://www.tandfonline.com/doi/abs/", "http://onlinelibrary.wiley.com/doi/"]}, + {prop: "ref_isbn", prefix: "isbn:", urls: ["https://www.isbn-international.org/identifier/"]} + ] + + // handle URLs (which could be CURIEs) + if ("ref_url" in mappedResponse) { + for (let publication of toArray(mappedResponse.ref_url)) { + if (typeof publication !== "string" || publication.length === 0) { + continue; + } + + let isCurie = false; + for (let publicationType of publicationTypes) { + for (let url of publicationType.urls) { + if (publication.startsWith(url)) { + isCurie = true; + + if (!mappedResponse[publicationType.prop]) { + mappedResponse[publicationType.prop] = []; + } + else if (!Array.isArray(mappedResponse[publicationType.prop])) { + mappedResponse[publicationType.prop] = toArray(mappedResponse[publicationType.prop]); + } + + mappedResponse[publicationType.prop].push(publication.slice(url.length)); + + break; + } + } + + if (isCurie) { + break; + } + } + + if (!isCurie) { + mappedResponse.publications.push(publication); + } + } } + delete mappedResponse.ref_url; + + for (let publicationType of publicationTypes) { + if (publicationType.prop in mappedResponse) { + for (let publication of toArray(mappedResponse[publicationType.prop])) { + // handle numbers + if (typeof publication === "number") { + publication = publication.toString(); + } + + if (typeof publication !== "string" || publication.length === 0) { + continue; + } + + if (publication.toUpperCase().startsWith(publicationType.prefix.toUpperCase())) { + mappedResponse.publications.push(publicationType.prefix + publication.slice(publicationType.prefix.length)); + } + else { + mappedResponse.publications.push(publicationType.prefix + publication); + } + } + + delete mappedResponse[publicationType.prop]; + } + } + + if (mappedResponse.publications.length === 0) { + delete mappedResponse.publications; + } + return mappedResponse; }