Skip to content

Commit

Permalink
Merge pull request #386 from dandi/gh-385
Browse files Browse the repository at this point in the history
Populate wasDerivedFrom
  • Loading branch information
yarikoptic authored Feb 19, 2021
2 parents d138c6f + 10d2c21 commit 543e11b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
22 changes: 17 additions & 5 deletions dandi/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,6 @@ def extract_anatomy(metadata):
def extract_model(modelcls, metadata, **kwargs):
m = modelcls.unvalidated()
for field in m.__fields__.keys():
if modelcls == models.BioSample and field == "wasDerivedFrom":
continue
value = kwargs.get(field, extract_field(field, metadata))
if value is not Ellipsis:
setattr(m, field, value)
Expand All @@ -253,9 +251,23 @@ def func(metadata):
return func


extract_wasDerivedFrom = extract_model_list(
models.BioSample, "identifier", "tissue_sample_id"
)
def extract_wasDerivedFrom(metadata):
derived_from = None
for field, sample_name in [
("tissue_sample_id", "tissuesample"),
("slice_id", "slice"),
("cell_id", "cell"),
]:
if metadata.get(field) is not None:
derived_from = [
models.BioSample(
identifier=metadata[field],
wasDerivedFrom=derived_from,
sampleType=models.SampleType(name=sample_name),
)
]
return derived_from


extract_wasAttributedTo = extract_model_list(
models.Participant, "identifier", "subject_id"
Expand Down
34 changes: 31 additions & 3 deletions dandi/tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,32 @@ def test_metadata2asset(schema_dir):
},
"wasDerivedFrom": [
{
"identifier": "tissue03",
"schemaKey": "BioSample"
"identifier": "cell01",
"schemaKey": "BioSample",
"sampleType": {
"name": "cell",
"schemaKey": "SampleType"
},
"wasDerivedFrom": [
{
"identifier": "slice02",
"schemaKey": "BioSample",
"sampleType": {
"name": "slice",
"schemaKey": "SampleType"
},
"wasDerivedFrom": [
{
"identifier": "tissue03",
"schemaKey": "BioSample",
"sampleType": {
"name": "tissuesample",
"schemaKey": "SampleType"
}
}
]
}
]
}
],
"wasAttributedTo": [
Expand Down Expand Up @@ -213,7 +237,11 @@ def test_metadata2asset_simple1(schema_dir):
},
"wasDerivedFrom": [
{
"identifier": "tissue42"
"identifier": "tissue42",
"sampleType": {
"name": "tissuesample",
"schemaKey": "SampleType"
}
}
],
"wasGeneratedBy": [
Expand Down

0 comments on commit 543e11b

Please sign in to comment.