Skip to content

Commit

Permalink
Convert DOI URLs in related_publications to related resources
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Mar 7, 2024
1 parent ee18a15 commit b0bf065
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
25 changes: 25 additions & 0 deletions dandi/metadata/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from dandischema import models
import requests
import tenacity
from yarl import URL

from .. import __version__
from ..utils import ensure_datetime
Expand Down Expand Up @@ -583,6 +584,29 @@ def extract_digest(metadata: dict) -> dict[models.DigestType, str] | None:
return None


def extract_related_resource(metadata: dict) -> list[models.Resource] | None:
pubs = metadata.get("related_publications")
if not isinstance(pubs, (list, tuple)):
return None
related = []
for v in pubs:
if not isinstance(v, str):
continue

Check warning on line 594 in dandi/metadata/util.py

View check run for this annotation

Codecov / codecov/patch

dandi/metadata/util.py#L594

Added line #L594 was not covered by tests
try:
u = URL(v)
except ValueError:
continue

Check warning on line 598 in dandi/metadata/util.py

View check run for this annotation

Codecov / codecov/patch

dandi/metadata/util.py#L597-L598

Added lines #L597 - L598 were not covered by tests
if u.scheme not in ("http", "https") or u.host != "doi.org":
continue
related.append(
models.Resource(
identifier=v,
relation=models.RelationType.IsDescribedBy,
)
)
return related


FIELD_EXTRACTORS: dict[str, Callable[[dict], Any]] = {
"wasDerivedFrom": extract_wasDerivedFrom,
"wasAttributedTo": extract_wasAttributedTo,
Expand All @@ -595,6 +619,7 @@ def extract_digest(metadata: dict) -> dict[models.DigestType, str] | None:
"anatomy": extract_anatomy,
"digest": extract_digest,
"species": extract_species,
"relatedResource": extract_related_resource,
}


Expand Down
7 changes: 7 additions & 0 deletions dandi/tests/data/metadata/metadata2asset_3.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,12 @@
"name": "Cyperus bulbosus"
}
}
],
"relatedResource": [
{
"schemaKey": "Resource",
"identifier": "https://doi.org/10.48324/dandi.000027/0.210831.2033",
"relation": "dcite:IsDescribedBy"
}
]
}
3 changes: 2 additions & 1 deletion dandi/tests/data/metadata/metadata2asset_simple1.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@
"schemaKey": "Participant",
"identifier": "sub-01"
}
]
],
"relatedResource": []
}
6 changes: 5 additions & 1 deletion dandi/tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,9 @@ def test_timedelta2duration(td: timedelta, duration: str) -> None:
"institution": "University College",
"keywords": ["test", "sample", "example", "test-case"],
"lab": "Retriever Laboratory",
"related_publications": "A Brief History of Test Cases",
"related_publications": [
"https://doi.org/10.48324/dandi.000027/0.210831.2033"
],
"session_description": "Some test data",
"session_id": "XYZ789",
"session_start_time": "2020-08-31T15:58:28-04:00",
Expand Down Expand Up @@ -860,6 +862,7 @@ def test_nwb2asset(simple2_nwb: Path) -> None:
variableMeasured=[],
measurementTechnique=[],
approach=[],
relatedResource=[],
)


Expand Down Expand Up @@ -939,4 +942,5 @@ def test_nwb2asset_remote_asset(nwb_dandiset: SampleDandiset) -> None:
variableMeasured=[],
measurementTechnique=[],
approach=[],
relatedResource=[],
)

0 comments on commit b0bf065

Please sign in to comment.