-
Notifications
You must be signed in to change notification settings - Fork 74
[SYNPY-1375] Add Activity/Provenance tutorial #1351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8b2a4f0
[SYNPY-1375]: Add Activity/Provenance tutorial
thomasyu888 19475f1
Use --8<-- anchors to avoid having to maintain specific coding lines
thomasyu888 ebe4d41
Add activity to mkdocs
thomasyu888 f3eb52a
Clean up activity script
thomasyu888 943d045
Update activity
thomasyu888 d1b7da2
Update tutorial to create files
thomasyu888 a1727c0
Update docs
thomasyu888 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| """ | ||
| Here is where you'll find the code for the Activity/Provenance tutorial. | ||
| """ | ||
|
|
||
| # Step 1: Add a new Activity to your File | ||
| import synapseclient | ||
| from synapseclient.models import Activity, File, Folder, Project, UsedEntity, UsedURL | ||
|
|
||
| syn = synapseclient.login() | ||
|
|
||
| # Retrieve the project and folder IDs | ||
| my_project_id = ( | ||
| Project(name="My uniquely named project about Alzheimer's Disease").get().id | ||
|
thomasyu888 marked this conversation as resolved.
Outdated
|
||
| ) | ||
|
|
||
| biospecimen_experiment_1_folder = Folder( | ||
| name="biospecimen_experiment_1", parent_id=my_project_id | ||
| ).get() | ||
|
thomasyu888 marked this conversation as resolved.
|
||
|
|
||
| # Retrieve an existing file from the project | ||
| my_file = File( | ||
| name="fileA.txt", | ||
| parent_id=biospecimen_experiment_1_folder.id, | ||
| ).get() | ||
|
|
||
| # Create an Activity describing the analysis step that produced this file | ||
| analysis_activity = Activity( | ||
| name="Quality Control Analysis", | ||
| description="Initial QC analysis of biospecimen data using the FastQC pipeline.", | ||
| used=[ | ||
| UsedURL( | ||
| name="FastQC v0.12.1", | ||
| url="https://github.com/s-andrews/FastQC/releases/tag/v0.12.1", | ||
| ), | ||
| UsedEntity(target_id=my_project_id), | ||
| ], | ||
| executed=[ | ||
| UsedURL( | ||
| name="QC Analysis Script", | ||
| url="https://github.com/Sage-Bionetworks/analysis-scripts/blob/v1.0/qc_analysis.py", | ||
| ), | ||
| ], | ||
| ) | ||
|
|
||
| # Attach the activity to the file and store it | ||
| my_file.activity = analysis_activity | ||
| my_file = my_file.store() | ||
|
thomasyu888 marked this conversation as resolved.
|
||
|
|
||
| first_version_number = my_file.version_number | ||
| print( | ||
| f"Stored file: {my_file.name} (version {first_version_number}) " | ||
| f"with activity: {my_file.activity.name}" | ||
| ) | ||
|
|
||
| # Step 2: Add a new Activity to a specific version of your File | ||
| # Each time you store an updated file, Synapse creates a new version. | ||
| # You can track a different activity for each version to capture the | ||
| # full history of what was done to produce each version of the file. | ||
| downstream_activity = Activity( | ||
| name="Downstream Analysis", | ||
| description="Downstream analysis of QC-passed biospecimen samples.", | ||
| used=[ | ||
| UsedURL( | ||
| name="Seurat v5.0.0", | ||
| url="https://github.com/satijalab/seurat/releases/tag/v5.0.0", | ||
| ), | ||
| UsedEntity( | ||
| target_id=my_file.id, | ||
| target_version_number=first_version_number, | ||
| ), | ||
| ], | ||
| executed=[ | ||
| UsedURL( | ||
| name="Downstream Analysis Script", | ||
| url="https://github.com/Sage-Bionetworks/analysis-scripts/blob/v1.0/downstream_analysis.py", | ||
| ), | ||
| ], | ||
| ) | ||
|
|
||
| # Store the activity directly on the file using Activity.store() | ||
| second_version_file = File(id=my_file.id).get() | ||
| downstream_activity.store(parent=second_version_file) | ||
|
|
||
| second_version_number = second_version_file.version_number | ||
|
thomasyu888 marked this conversation as resolved.
Outdated
|
||
| print( | ||
| f"Stored activity '{downstream_activity.name}' on file " | ||
| f"{second_version_file.name} (version {second_version_number})" | ||
| ) | ||
|
thomasyu888 marked this conversation as resolved.
|
||
|
|
||
| # Step 3: Print stored activities on your File | ||
| # Retrieve and print the activity on the latest version of the file | ||
| current_activity = Activity.from_parent(parent=my_file) | ||
| print(f"\nActivity on latest version (v{my_file.version_number}):") | ||
| print(f" Name: {current_activity.name}") | ||
|
thomasyu888 marked this conversation as resolved.
|
||
| print(f" Description: {current_activity.description}") | ||
| for item in current_activity.used: | ||
| print(f" Used: {item}") | ||
| for item in current_activity.executed: | ||
| print(f" Executed: {item}") | ||
|
|
||
| # Retrieve and print the activity for the first version | ||
| first_activity = Activity.from_parent( | ||
| parent=my_file, | ||
| parent_version_number=first_version_number, | ||
| ) | ||
| print(f"\nActivity on version {first_version_number}:") | ||
| print(f" Name: {first_activity.name}") | ||
| print(f" Description: {first_activity.description}") | ||
|
|
||
| # Step 4: Delete an activity | ||
| # Deleting an activity disassociates it from the entity and removes it from | ||
| # Synapse once it is no longer referenced by any entity. | ||
| Activity.delete(parent=my_file) | ||
| print(f"\nDeleted activity from: {my_file.name} (version {my_file.version_number})") | ||
|
thomasyu888 marked this conversation as resolved.
Outdated
|
||
|
|
||
| # Verify the activity was removed | ||
| deleted_activity = Activity.from_parent(parent=my_file) | ||
| print(f"Activity after deletion: {deleted_activity}") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.