Skip to content

Commit

Permalink
added doi and pubmed helper functions to LDPropertyValue and LDSchola…
Browse files Browse the repository at this point in the history
…rlyArticle converters
  • Loading branch information
HLWeil committed Mar 7, 2025
1 parent f997440 commit 0d82e3c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 43 deletions.
53 changes: 14 additions & 39 deletions src/ARCtrl/Conversion.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,41 +1000,6 @@ type PersonConversion =

type ScholarlyArticleConversion =


static member doiKey = "DOI"

static member doiURL = "http://purl.obolibrary.org/obo/OBI_0002110"

static member pubmedIDKey = "PubMedID"

static member pubmedIDURL = "http://purl.obolibrary.org/obo/OBI_0001617"

static member composeDOI (doi : string) : LDNode =
LDPropertyValue.create(name = ScholarlyArticleConversion.doiKey, value = doi, propertyID = ScholarlyArticleConversion.doiURL)

static member tryDecomposeDOI (doi : LDNode, ?context : LDContext) : string option =
match
LDPropertyValue.tryGetNameAsString(doi, ?context = context),
LDPropertyValue.tryGetValueAsString(doi, ?context = context),
LDPropertyValue.tryGetPropertyIDAsString(doi, ?context = context)
with
| Some name, Some value, Some id when name = ScholarlyArticleConversion.doiKey && id = ScholarlyArticleConversion.doiURL ->
Some value
| _ -> None

static member composePubMedID (pubMedID : string) : LDNode =
LDPropertyValue.create(name = ScholarlyArticleConversion.pubmedIDKey, value = pubMedID, propertyID = ScholarlyArticleConversion.pubmedIDURL)

static member tryDecomposePubMedID (pubMedID : LDNode, ?context : LDContext) : string option =
match
LDPropertyValue.tryGetNameAsString(pubMedID, ?context = context),
LDPropertyValue.tryGetValueAsString(pubMedID, ?context = context),
LDPropertyValue.tryGetPropertyIDAsString(pubMedID, ?context = context)
with
| Some name, Some value, Some id when name = ScholarlyArticleConversion.pubmedIDKey && id = ScholarlyArticleConversion.pubmedIDURL ->
Some value
| _ -> None

static member composeAuthor (author : string) : LDNode =
try
ARCtrl.Json.Decode.fromJsonString Json.LDNode.decoder author
Expand Down Expand Up @@ -1092,9 +1057,9 @@ type ScholarlyArticleConversion =
|> Option.fromSeq
let identifiers = ResizeArray [
if publication.DOI.IsSome && publication.DOI.Value <> "" then
ScholarlyArticleConversion.composeDOI publication.DOI.Value
LDPropertyValue.createDOI publication.DOI.Value
if publication.PubMedID.IsSome && publication.PubMedID.Value <> "" then
ScholarlyArticleConversion.composePubMedID publication.PubMedID.Value
LDPropertyValue.createPubMedID publication.PubMedID.Value
]
let status = publication.Status |> Option.map BaseTypes.composeDefinedTerm
let scholarlyArticle =
Expand All @@ -1121,8 +1086,18 @@ type ScholarlyArticleConversion =
LDScholarlyArticle.tryGetCreativeWorkStatus(sa, ?graph = graph, ?context = context)
|> Option.map (fun s -> BaseTypes.decomposeDefinedTerm(s, ?context = context))
let identifiers = LDScholarlyArticle.getIdentifiersAsPropertyValue(sa, ?graph = graph, ?context = context)
let pubMedID = identifiers |> ResizeArray.tryPick (fun i -> ScholarlyArticleConversion.tryDecomposePubMedID(i, ?context = context))
let doi = identifiers |> ResizeArray.tryPick (fun i -> ScholarlyArticleConversion.tryDecomposeDOI(i, ?context = context))
let doi =
identifiers
|> ResizeArray.tryPick (fun i -> LDPropertyValue.tryGetAsDOI(i, ?context = context))
|> function
| Some p -> Some p
| None -> LDScholarlyArticle.tryGetSameAsAsString(sa, ?context = context)
let pubMedID =
identifiers
|> ResizeArray.tryPick (fun i -> LDPropertyValue.tryGetAsPubMedID(i, ?context = context))
|> function
| Some p -> Some p
| None -> LDScholarlyArticle.tryGetUrlAsString(sa, ?context = context)
ARCtrl.Publication.create(
title = title,
?authors = authors,
Expand Down
50 changes: 49 additions & 1 deletion src/ROCrate/Generic/PropertyValue.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ type LDPropertyValue =

static member valueReference = "http://schema.org/valueReference"

static member doiKey = "DOI"

static member doiURL = "http://purl.obolibrary.org/obo/OBI_0002110"

static member pubmedIDKey = "PubMedID"

static member pubmedIDURL = "http://purl.obolibrary.org/obo/OBI_0001617"

static member tryGetNameAsString(pv : LDNode, ?context : LDContext) =
match pv.TryGetPropertyAsSingleton(LDPropertyValue.name, ?context = context) with
| Some (:? string as n) -> Some n
Expand Down Expand Up @@ -103,6 +111,28 @@ type LDPropertyValue =
LDPropertyValue.validate(pv, ?context = context)
&& pv.AdditionalType.Contains("FactorValue")

static member validateDOI (pv : LDNode, ?context : LDContext) =
LDPropertyValue.validate(pv, ?context = context)
&&
match
LDPropertyValue.tryGetNameAsString(pv, ?context = context),
LDPropertyValue.tryGetValueAsString(pv, ?context = context),
LDPropertyValue.tryGetPropertyIDAsString(pv, ?context = context)
with
| Some name, Some value, Some id when name = LDPropertyValue.doiKey && id = LDPropertyValue.doiURL -> true
| _ -> false

static member validatePubMedID (pv : LDNode, ?context : LDContext) =
LDPropertyValue.validate(pv, ?context = context)
&&
match
LDPropertyValue.tryGetNameAsString(pv, ?context = context),
LDPropertyValue.tryGetValueAsString(pv, ?context = context),
LDPropertyValue.tryGetPropertyIDAsString(pv, ?context = context)
with
| Some name, Some value, Some id when name = LDPropertyValue.pubmedIDKey && id = LDPropertyValue.pubmedIDURL -> true
| _ -> false

static member genId(name : string, ?value : string, ?propertyID : string, ?prefix) =
let prefix = Option.defaultValue "PV" prefix
match value,propertyID with
Expand Down Expand Up @@ -168,4 +198,22 @@ type LDPropertyValue =
| None -> LDPropertyValue.genIdFactorValue(name, ?value = value, ?propertyID = propertyID)
let fv = LDPropertyValue.create(name, id = id, ?value = value, ?propertyID = propertyID, ?unitCode = unitCode, ?unitText = unitText, ?valueReference = valueReference, ?context = context)
fv.AdditionalType <- ResizeArray ["FactorValue"]
fv
fv

static member createDOI(value, ?context : LDContext) =
let id = value
LDPropertyValue.create(name = LDPropertyValue.doiKey, value = value, id = id, propertyID = LDPropertyValue.doiURL, ?context = context)

static member createPubMedID(value, ?context : LDContext) =
let id = value
LDPropertyValue.create(name = LDPropertyValue.pubmedIDKey, value = value, id = id, propertyID = LDPropertyValue.pubmedIDURL, ?context = context)

static member tryGetAsDOI(pv : LDNode, ?context : LDContext) =
if LDPropertyValue.validateDOI(pv, ?context = context) then
Some (LDPropertyValue.getValueAsString(pv, ?context = context))
else None

static member tryGetAsPubMedID(pv : LDNode, ?context : LDContext) =
if LDPropertyValue.validatePubMedID(pv, ?context = context) then
Some (LDPropertyValue.getValueAsString(pv, ?context = context))
else None
10 changes: 7 additions & 3 deletions src/ROCrate/Generic/ScholarlyArticle.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type LDScholarlyArticle =

static member url = "http://schema.org/url"

static member sameAs = "http://schema.org/sameAs"

static member creativeWorkStatus = "http://schema.org/creativeWorkStatus"

static member comment = "http://schema.org/comment"
Expand Down Expand Up @@ -53,13 +55,15 @@ type LDScholarlyArticle =
static member setAuthors(s : LDNode, authors : ResizeArray<LDNode>, ?context : LDContext) =
s.SetProperty(LDScholarlyArticle.author, authors, ?context = context)

static member tryGetUrl(s : LDNode, ?context : LDContext) =
static member tryGetUrlAsString(s : LDNode, ?context : LDContext) =
match s.TryGetPropertyAsSingleton(LDScholarlyArticle.url, ?context = context) with
| Some (:? string as u) -> Some u
| _ -> None

static member setUrl(s : LDNode, u : string, ?context : LDContext) =
s.SetProperty(LDScholarlyArticle.url, u, ?context = context)
static member tryGetSameAsAsString(s : LDNode, ?context : LDContext) =
match s.TryGetPropertyAsSingleton(LDScholarlyArticle.sameAs, ?context = context) with
| Some (:? string as sa) -> Some sa
| _ -> None

static member tryGetCreativeWorkStatus(s : LDNode, ?graph : LDGraph, ?context : LDContext) =
match s.TryGetPropertyAsSingleNode(LDScholarlyArticle.creativeWorkStatus, ?graph = graph, ?context = context) with
Expand Down
11 changes: 11 additions & 0 deletions tests/ARCtrl/ROCrateConversion.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,17 @@ let tests_Publication =
let p' = ScholarlyArticleConversion.decomposeScholarlyArticle ro_Publication
Expect.equal p' p "Publication should match"
)
testCase "DOI_PubMedID_FromScaffold_ToDeprecatedROCrate" (fun () ->
let doi = "10.1234/5678"
let pubMedID = "12345678"
let p = ARCtrl.Publication.create(title = "My Paper", doi = doi, pubMedID = pubMedID)
let json =
Json.Publication.ROCrate.encoder p
|> Json.Encode.toJsonString 0
let ldNode = Json.Decode.fromJsonString Json.LDNode.decoder json
let p' = ScholarlyArticleConversion.decomposeScholarlyArticle ldNode
Expect.equal p' p "Publication should match"
)
testCase "Full_FromScaffold_Flattened" (fun () ->
let authors = "Lukas Weil, John Doe"
let comment = Comment("MyCommentKey","MyCommentValue")
Expand Down

0 comments on commit 0d82e3c

Please sign in to comment.