|
4 | 4 |
|
5 | 5 | - [Overview](#overview) |
6 | 6 | - [Export Discovery Metadata into File](#export-discovery-metadata-from-file) |
7 | | -- [Publish Discovery Metadata from File]() |
| 7 | +- [Publish Discovery Metadata from File](#publish-discovery-metadata-from-file) |
8 | 8 | - [DOIs in Gen3](#dois-in-gen3-discovery-metadata-and-page-for-visualizing-public-doi-metadata) |
9 | 9 | - [dbGaP FHIR Metadata in Gen3 Discovery](#combine-dbgap-fhir-metadata-with-current-discovery-metadata) |
| 10 | +- [Publish Discovery Metadata Objects from File](#publish-discovery-metadata-objects-from-file) |
10 | 11 |
|
11 | 12 | ### Overview |
12 | 13 |
|
@@ -529,4 +530,56 @@ def main(): |
529 | 530 |
|
530 | 531 | if __name__ == "__main__": |
531 | 532 | main() |
| 533 | +``` |
| 534 | + |
| 535 | +### Publish Discovery Metadata Objects from File |
| 536 | +Gen3's SDK can be used to ingest data objects related to datasets in Gen3 environment from a file by using the `publish_discovery_object_metadata()` function. To obtain a file of existing metadata objects, use the `output_discovery_objects()` function. By default new objects published from a file are appended to a dataset in a Gen3 environment. If object guids from a file already exist for a dataset in the Gen3 environment, objects are updated. If the `overwrite` option is `True`, all current metadata objects related to a dataset are instead replaced. You can also use this functionality from the CLI. See `gen3 discovery objects --help` |
| 537 | + |
| 538 | +Example of usage: |
| 539 | +```python |
| 540 | +""" |
| 541 | +Example script showing reading Discovery Objects Metadata and then |
| 542 | +publishing it back, just to demonstrate the functions. |
| 543 | + |
| 544 | +Before running this, ensure your ~/.gen3/credentials.json contains |
| 545 | +an API key for a Gen3 instance to interact with and/or adjust the |
| 546 | +Gen3Auth logic to provide auth in another way |
| 547 | +""" |
| 548 | +from cdislogging import get_logger |
| 549 | + |
| 550 | +from gen3.tools.metadata.discovery_objects import ( |
| 551 | + publish_discovery_object_metadata, |
| 552 | + output_discovery_objects, |
| 553 | +) |
| 554 | +from gen3.utils import get_or_create_event_loop_for_thread |
| 555 | +from gen3.auth import Gen3Auth |
| 556 | + |
| 557 | +logging = get_logger("__name__") |
| 558 | + |
| 559 | +if __name__ == "__main__": |
| 560 | + auth = Gen3Auth() |
| 561 | + loop = get_or_create_event_loop_for_thread() |
| 562 | + logging.info(f"Reading discovery objects metadata from: {auth.endpoint}...") |
| 563 | + output_filename = loop.run_until_complete( |
| 564 | + output_discovery_objects( |
| 565 | + auth, |
| 566 | + output_format="tsv", |
| 567 | + ) |
| 568 | + ) |
| 569 | + logging.info(f"Output discovery objects metadata: {output_filename}") |
| 570 | + |
| 571 | + # Here you can modify the file by hand or in code and then publish to update |
| 572 | + # Alternatively, you can skip the read above and just provide a file with |
| 573 | + # the object metadata you want to publish |
| 574 | + |
| 575 | + logging.info( |
| 576 | + f"publishing discovery object metadata to: {auth.endpoint} from file: {output_filename}" |
| 577 | + ) |
| 578 | + loop.run_until_complete( |
| 579 | + publish_discovery_object_metadata( |
| 580 | + auth, |
| 581 | + output_filename, |
| 582 | + overwrite=False, |
| 583 | + ) |
| 584 | + ) |
532 | 585 | ``` |
0 commit comments