Skip to content

Commit

Permalink
Merge pull request #53 from biothings/publication-refactor
Browse files Browse the repository at this point in the history
Publication refactor
  • Loading branch information
tokebe authored Aug 11, 2023
2 parents 85bba2a + ae3a5f4 commit d6723f5
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 27 deletions.
20 changes: 10 additions & 10 deletions __test__/base_transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
2 changes: 1 addition & 1 deletion __test__/biothings_transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
})
})
Expand Down
14 changes: 10 additions & 4 deletions src/transformers/biolink_transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/transformers/ctd_transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
88 changes: 76 additions & 12 deletions src/transformers/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit d6723f5

Please sign in to comment.