|
| 1 | +""" |
| 2 | +Here is where you'll find the code for the Activity/Provenance tutorial. |
| 3 | +""" |
| 4 | + |
| 5 | +# Step 1: Add a new Activity to your File |
| 6 | +# --8<-- [start:retrieve_project_folder_file] |
| 7 | +import os |
| 8 | +import tempfile |
| 9 | + |
| 10 | +import synapseclient |
| 11 | +from synapseclient.models import Activity, File, Folder, Project, UsedEntity, UsedURL |
| 12 | + |
| 13 | +syn = synapseclient.login() |
| 14 | + |
| 15 | +# Set project and folder name that exists within the project |
| 16 | +PROJECT_NAME = "Dark Side Of The Moon" |
| 17 | +FOLDER_NAME = "biospecimen_experiment_1" |
| 18 | + |
| 19 | +# Retrieve the project and folder IDs |
| 20 | +my_project_id = Project(name=PROJECT_NAME).get().id |
| 21 | + |
| 22 | +biospecimen_experiment_1_folder = Folder( |
| 23 | + name=FOLDER_NAME, parent_id=my_project_id |
| 24 | +).get() |
| 25 | + |
| 26 | +with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as tmp: |
| 27 | + tmp.write("First biospecimen data - post-QC analysis results") |
| 28 | + tmp_path = tmp.name |
| 29 | +# Store a first version of the file in Synapse |
| 30 | +my_file = File( |
| 31 | + path=tmp_path, |
| 32 | + name="biospecimen_data.txt", |
| 33 | + parent_id=biospecimen_experiment_1_folder.id, |
| 34 | +) |
| 35 | +my_file.store() |
| 36 | + |
| 37 | +# --8<-- [end:retrieve_project_folder_file] |
| 38 | + |
| 39 | +# --8<-- [start:create_activity] |
| 40 | +# Create an Activity describing the analysis step that produced this file |
| 41 | +analysis_activity = Activity( |
| 42 | + name="Quality Control Analysis", |
| 43 | + description="Initial QC analysis of biospecimen data using the FastQC pipeline.", |
| 44 | + used=[ |
| 45 | + UsedURL( |
| 46 | + name="FastQC v0.12.1", |
| 47 | + url="https://github.com/s-andrews/FastQC/releases/tag/v0.12.1", |
| 48 | + ), |
| 49 | + UsedEntity(target_id=my_project_id), |
| 50 | + ], |
| 51 | + executed=[ |
| 52 | + UsedURL( |
| 53 | + name="QC Analysis Script", |
| 54 | + url="https://github.com/Sage-Bionetworks/analysis-scripts/blob/v1.0/qc_analysis.py", |
| 55 | + ), |
| 56 | + ], |
| 57 | +) |
| 58 | + |
| 59 | +# Attach the activity to the file and store it |
| 60 | +my_file.activity = analysis_activity |
| 61 | +my_file = my_file.store() |
| 62 | + |
| 63 | +first_version_number = my_file.version_number |
| 64 | +print( |
| 65 | + f"Stored file: {my_file.name} (version {first_version_number}) " |
| 66 | + f"with activity: {my_file.activity.name}" |
| 67 | +) |
| 68 | +# --8<-- [end:create_activity] |
| 69 | + |
| 70 | +# --8<-- [start:add_activity_to_version] |
| 71 | +# Step 2: Add a new Activity to a specific version of your File |
| 72 | +# Each time you store an updated file, Synapse creates a new version. |
| 73 | +# You can track a different activity for each version to capture the |
| 74 | +# full history of what was done to produce each version of the file. |
| 75 | + |
| 76 | +# Create a dummy file and upload it as a new version |
| 77 | +with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as tmp: |
| 78 | + tmp.write("Updated biospecimen data - post-QC analysis results") |
| 79 | + tmp_path = tmp.name |
| 80 | + |
| 81 | +updated_file = File( |
| 82 | + path=tmp_path, |
| 83 | + name="biospecimen_data.txt", |
| 84 | + parent_id=biospecimen_experiment_1_folder.id, |
| 85 | +) |
| 86 | +updated_file.store() |
| 87 | +second_version_number = updated_file.version_number |
| 88 | + |
| 89 | +downstream_activity = Activity( |
| 90 | + name="Downstream Analysis", |
| 91 | + description="Downstream analysis of QC-passed biospecimen samples.", |
| 92 | + used=[ |
| 93 | + UsedURL( |
| 94 | + name="Seurat v5.0.0", |
| 95 | + url="https://github.com/satijalab/seurat/releases/tag/v5.0.0", |
| 96 | + ), |
| 97 | + UsedEntity( |
| 98 | + target_id=my_file.id, |
| 99 | + target_version_number=first_version_number, |
| 100 | + ), |
| 101 | + ], |
| 102 | + executed=[ |
| 103 | + UsedURL( |
| 104 | + name="Downstream Analysis Script", |
| 105 | + url="https://github.com/Sage-Bionetworks/analysis-scripts/blob/v1.0/downstream_analysis.py", |
| 106 | + ), |
| 107 | + ], |
| 108 | +) |
| 109 | + |
| 110 | +# Store the activity on the new version using Activity.store() |
| 111 | +downstream_activity.store(parent=updated_file) |
| 112 | +print( |
| 113 | + f"Stored activity '{downstream_activity.name}' on file " |
| 114 | + f"{updated_file.name} (version {second_version_number})" |
| 115 | +) |
| 116 | +# --8<-- [end:add_activity_to_version] |
| 117 | + |
| 118 | +# --8<-- [start:print_activities] |
| 119 | +# Step 3: Print stored activities on your File |
| 120 | +# Retrieve and print the activity on the latest version of the file |
| 121 | +current_activity = Activity.from_parent(parent=my_file) |
| 122 | +print(f"\nActivity on latest version (v{my_file.version_number}):") |
| 123 | +print(f" Name: {current_activity.name}") |
| 124 | +print(f" Description: {current_activity.description}") |
| 125 | +for item in current_activity.used: |
| 126 | + print(f" Used: {item}") |
| 127 | +for item in current_activity.executed: |
| 128 | + print(f" Executed: {item}") |
| 129 | + |
| 130 | +# Retrieve and print the activity for the first version |
| 131 | +first_activity = Activity.from_parent( |
| 132 | + parent=my_file, |
| 133 | + parent_version_number=first_version_number, |
| 134 | +) |
| 135 | +print(f"\nActivity on version {first_version_number}:") |
| 136 | +print(f" Name: {first_activity.name}") |
| 137 | +print(f" Description: {first_activity.description}") |
| 138 | +# --8<-- [end:print_activities] |
| 139 | + |
| 140 | +# --8<-- [start:delete_activity] |
| 141 | +# Step 4: Delete an activity |
| 142 | +# Deleting an activity disassociates it from the entity and removes it from |
| 143 | +# Synapse once it is no longer referenced by any entity. |
| 144 | + |
| 145 | +current_activity.disassociate_from_entity(parent=updated_file) |
| 146 | +current_activity.delete(parent=updated_file) |
| 147 | +print( |
| 148 | + f"\nDeleted activity from: {updated_file.name} (version {updated_file.version_number})" |
| 149 | +) |
| 150 | + |
| 151 | +# Verify the activity was removed |
| 152 | +deleted_activity = Activity.from_parent( |
| 153 | + parent=updated_file, parent_version_number=updated_file.version_number |
| 154 | +) |
| 155 | +print(f"Activity after deletion: {deleted_activity}") |
| 156 | +# --8<-- [end:delete_activity] |
0 commit comments