Skip to content

Commit

Permalink
Merge pull request #1417 from dandi/gh-1308
Browse files Browse the repository at this point in the history
Convert DOI URLs in `related_publications` to related resources
  • Loading branch information
yarikoptic authored Mar 25, 2024
2 parents 74897b4 + b0bf065 commit d5e2a76
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
try:
u = URL(v)
except ValueError:
continue
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 @@ -332,7 +332,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 @@ -871,6 +873,7 @@ def test_nwb2asset(simple2_nwb: Path) -> None:
variableMeasured=[],
measurementTechnique=[],
approach=[],
relatedResource=[],
)


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

0 comments on commit d5e2a76

Please sign in to comment.