|
| 1 | +"""Tools for working with NIDM-Experiment files""" |
| 2 | + |
| 3 | +import click |
| 4 | +from rdflib import Graph, Literal, Namespace, URIRef, util |
| 5 | +from rdflib.namespace import RDF, split_uri |
| 6 | +from nidm.core import Constants |
| 7 | +from nidm.experiment.Core import getUUID |
| 8 | +from nidm.experiment.Query import GetProjectsMetadata, getProjectAcquisitionObjects |
| 9 | +from nidm.experiment.tools.click_base import cli |
| 10 | + |
| 11 | + |
| 12 | +# adding click argument parsing |
| 13 | +@cli.command() |
| 14 | +@click.option( |
| 15 | + "--nidm_file_list", |
| 16 | + "-nl", |
| 17 | + required=True, |
| 18 | + help="A comma separated list of NIDM files with full path", |
| 19 | +) |
| 20 | +def update(nidm_file_list): |
| 21 | + """ |
| 22 | + This function will update simple 2 NIDM files. It will essentially move some of the metadata in the project activity |
| 23 | + to a prov:Collection entity and then include all nidm:AcquisitionObject entities as prov:hadMember entries in this |
| 24 | + new prov:Collection |
| 25 | + """ |
| 26 | + BIDS = Namespace(Constants.BIDS) |
| 27 | + PROV = Namespace(Constants.PROV) |
| 28 | + NIIRI = Namespace(Constants.NIIRI) |
| 29 | + |
| 30 | + # for each nidm file provided |
| 31 | + for nidm_file in nidm_file_list.split(","): |
| 32 | + # create empty graph |
| 33 | + graph_orig = Graph() |
| 34 | + |
| 35 | + # bind namespaces |
| 36 | + graph_orig.bind("bids", BIDS) |
| 37 | + graph_orig.bind("prov", PROV) |
| 38 | + graph_orig.bind("niiri", NIIRI) |
| 39 | + |
| 40 | + # load graph |
| 41 | + graph_orig.parse(nidm_file, format=util.guess_format(nidm_file)) |
| 42 | + |
| 43 | + # create a new prov:Collection, prov:Entity to store metadata about project |
| 44 | + collection_uuid = getUUID() |
| 45 | + graph_orig.add((NIIRI.term(collection_uuid), RDF.type, BIDS.Dataset)) |
| 46 | + graph_orig.add((NIIRI.term(collection_uuid), RDF.type, PROV.Collection)) |
| 47 | + graph_orig.add((NIIRI.term(collection_uuid), RDF.type, PROV.Entity)) |
| 48 | + |
| 49 | + # query for all metadata in nidm:Project activity |
| 50 | + proj_metadata = GetProjectsMetadata([nidm_file]) |
| 51 | + |
| 52 | + # loop through projects and get associated acquisition objects |
| 53 | + for proj_uuid in proj_metadata["projects"].keys(): |
| 54 | + # for all project metadata, only leave dcmitype:title, otherwise move to collection_uuid |
| 55 | + for metadata_key in proj_metadata["projects"][proj_uuid].keys(): |
| 56 | + if (metadata_key != str(RDF.type)) and ( |
| 57 | + metadata_key != "dctypes:title" |
| 58 | + ): |
| 59 | + # get namespace and term from metadata_key |
| 60 | + nm, term = split_uri(metadata_key) |
| 61 | + |
| 62 | + # add RDF namespace |
| 63 | + temp_nm = Namespace(str(Constants.namespaces[nm.split(":", 1)[0]])) |
| 64 | + graph_orig.bind(nm.split(":", 1)[0], temp_nm) |
| 65 | + |
| 66 | + if (metadata_key == "dcat:creator") or ( |
| 67 | + metadata_key == "prov:Location" |
| 68 | + ): |
| 69 | + # now add this metadata to the collection_uuid |
| 70 | + graph_orig.add( |
| 71 | + ( |
| 72 | + NIIRI.collection_uuid, |
| 73 | + temp_nm.term(term), |
| 74 | + URIRef( |
| 75 | + proj_metadata["projects"][proj_uuid][metadata_key] |
| 76 | + ), |
| 77 | + ) |
| 78 | + ) |
| 79 | + else: |
| 80 | + # now add this metadata to the collection_uuid |
| 81 | + graph_orig.add( |
| 82 | + ( |
| 83 | + NIIRI.collection_uuid, |
| 84 | + temp_nm.term(term), |
| 85 | + Literal( |
| 86 | + proj_metadata["projects"][proj_uuid][metadata_key] |
| 87 | + ), |
| 88 | + ) |
| 89 | + ) |
| 90 | + |
| 91 | + # get acquisition objects |
| 92 | + acq_objects = getProjectAcquisitionObjects( |
| 93 | + [nidm_file], project_id=str(proj_uuid.split(":", 1)[1]) |
| 94 | + ) |
| 95 | + |
| 96 | + for acq_obj in acq_objects.keys(): |
| 97 | + print(acq_obj) |
| 98 | + |
| 99 | + # graph.serialize(out_file, format="turtle") |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + update() |
0 commit comments