Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…form.js into use-jmes-path
  • Loading branch information
tokebe committed Oct 4, 2023
2 parents 7df862e + d6723f5 commit 14c50b0
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 41 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 jq_tf(input, {type: "biothings"});
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
4 changes: 3 additions & 1 deletion src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,9 @@ export class Record {
}
}
returnValue.push({
resource_id: "infores:biothings-explorer",
resource_id: this.config.provenanceUsesServiceProvider
? "infores:service-provider-trapi"
: "infores:biothings-explorer",
resource_role: "aggregator_knowledge_source",
upstream_resource_ids: [this.apiInforesCurie],
});
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 @@ -62,20 +62,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
29 changes: 16 additions & 13 deletions src/transformers/trapi_transformer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BaseTransformer from "./transformer";
import { Record } from "../record";
import { JSONDoc } from "../json_transform/types";

export default class TRAPITransformer extends BaseTransformer {
_getUniqueEdges() {
Expand All @@ -11,19 +12,21 @@ export default class TRAPITransformer extends BaseTransformer {
) {
this.data.response.message.results.forEach(result => {
result.analyses.forEach(analysis => {
const edgeID = analysis.edge_bindings.e01[0].id;
const edge =
"message" in this.data.response ? this.data.response.message.knowledge_graph.edges[edgeID] : undefined;
const edgeHasSupportGraph = edge.attributes.some(attribute => {
if (attribute.attribute_type_id === "biolink:support_graphs" && attribute.value?.length) {
return true;
}
});
if (edgeHasSupportGraph) return;
edges[edgeID] = {
subject: result.node_bindings.n0[0].id,
object: result.node_bindings.n1[0].id,
};
analysis?.edge_bindings?.e01?.forEach(binding => {
const edgeID = binding?.id;
const edge =
"message" in this.data.response && edgeID ? this.data.response.message.knowledge_graph.edges[edgeID] : undefined;
const edgeHasSupportGraph = edge.attributes.some(attribute => {
if (attribute.attribute_type_id === "biolink:support_graphs" && attribute.value?.length) {
return true;
}
});
if (edgeHasSupportGraph || !edgeID) return;
edges[edgeID] = {
subject: (this.data.response as JSONDoc).message.knowledge_graph.edges[edgeID].subject ?? result.node_bindings.n0[0].id,
object: (this.data.response as JSONDoc).message.knowledge_graph.edges[edgeID].object ?? result.node_bindings.n1[0].id,
};
})
});
});
}
Expand Down

0 comments on commit 14c50b0

Please sign in to comment.