Synapse Factory Operations¶
Contained within this file are experimental interfaces for working with the Synapse Python Client. Unless otherwise noted these interfaces are subject to change at any time. Use at your own risk.
API Reference¶
synapseclient.operations.get
¶
get(synapse_id: Optional[str] = None, *, entity_name: Optional[str] = None, parent_id: Optional[str] = None, version_number: Optional[int] = None, activity_options: Optional[ActivityOptions] = None, file_options: Optional[FileOptions] = None, table_options: Optional[TableOptions] = None, link_options: Optional[LinkOptions] = None, synapse_client: Optional[Synapse] = None) -> Union[Dataset, DatasetCollection, EntityView, File, Folder, Link, MaterializedView, Project, SubmissionView, Table, VirtualTable]
Factory method to retrieve any Synapse entity by its ID or by name and parent ID.
This method serves as a unified interface for retrieving any type of Synapse entity without needing to know the specific entity type beforehand. It automatically determines the entity type and returns the appropriate model instance.
You can retrieve entities in two ways:
- By providing a synapse_id directly
- By providing entity_name and optionally parent_id for lookup
| PARAMETER | DESCRIPTION |
|---|---|
synapse_id
|
The Synapse ID of the entity to retrieve (e.g., 'syn123456'). Mutually exclusive with entity_name. |
entity_name
|
The name of the entity to find. Must be used with this approach instead of synapse_id. When looking up projects, parent_id should be None. |
parent_id
|
The parent entity ID when looking up by name. Set to None when looking up projects by name. Only used with entity_name. |
version_number
|
The specific version number of the entity to retrieve. Only applies to versionable entities (File, Table, Dataset). If not specified, the most recent version will be retrieved. Ignored for other entity types. |
activity_options
|
Activity-specific configuration options. Can be applied to any entity type to include activity information.
TYPE:
|
file_options
|
File-specific configuration options. Only applies to File entities. Ignored for other entity types.
TYPE:
|
table_options
|
Table-specific configuration options. Only applies to Table-like entities (Table, Dataset, EntityView, MaterializedView, SubmissionView, VirtualTable, DatasetCollection). Ignored for other entity types.
TYPE:
|
link_options
|
Link-specific configuration options. Only applies when the entity is a Link. Ignored for other entity types.
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
Union[Dataset, DatasetCollection, EntityView, File, Folder, Link, MaterializedView, Project, SubmissionView, Table, VirtualTable]
|
The appropriate Synapse entity model instance based on the entity type. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If both synapse_id and entity_name are provided, or if neither is provided. |
ValueError
|
If entity_name is provided without this being a valid lookup scenario. |
ValueError
|
If the synapse_id is not a valid Synapse ID format. |
Note
When using entity_name lookup:
- For projects: leave parent_id=None
- For all other entities: provide the parent_id of the containing folder/project
Retrieving entities by ID
Get any entity by Synapse ID:
from synapseclient import Synapse
from synapseclient.models import File, Project
from synapseclient.operations import get
syn = Synapse()
syn.login()
# Works for any entity type
entity = get(synapse_id="syn123456")
# The returned object will be the appropriate type
if isinstance(entity, File):
print(f"File: {entity.name}")
elif isinstance(entity, Project):
print(f"Project: {entity.name}")
Retrieving entities by name
Get an entity by name and parent:
from synapseclient import Synapse
from synapseclient.operations import get
syn = Synapse()
syn.login()
# Get a file by name within a folder
entity = get(
entity_name="my_file.txt",
parent_id="syn123456"
)
# Get a project by name (parent_id=None)
project = get(
entity_name="My Project",
parent_id=None
)
Retrieving a specific version
Get a specific version of a versionable entity:
from synapseclient import Synapse
from synapseclient.operations import get
syn = Synapse()
syn.login()
entity = get(synapse_id="syn123456", version_number=2)
Retrieving a file with custom options
Get file metadata with specific download options:
from synapseclient import Synapse
from synapseclient.operations import get, FileOptions, ActivityOptions
syn = Synapse()
syn.login()
file_entity = get(
synapse_id="syn123456",
activity_options=ActivityOptions(include_activity=True),
file_options=FileOptions(
download_file=False
)
)
Retrieving a table with activity and columns
Get table with activity and column information:
from synapseclient import Synapse
from synapseclient.operations import get, ActivityOptions, TableOptions
syn = Synapse()
syn.login()
table_entity = get(
synapse_id="syn123456",
activity_options=ActivityOptions(include_activity=True),
table_options=TableOptions(include_columns=True)
)
Following links
Get the target of a link entity:
from synapseclient import Synapse
from synapseclient.operations import get, LinkOptions
syn = Synapse()
syn.login()
target_entity = get(
synapse_id="syn123456",
link_options=LinkOptions(follow_link=True)
)
Working with Link entities
Get a Link entity without following it:
from synapseclient import Synapse
from synapseclient.operations import get, LinkOptions
syn = Synapse()
syn.login()
# Get the link entity itself
link_entity = get(
synapse_id="syn123456", # Example link ID
link_options=LinkOptions(follow_link=False)
)
print(f"Link: {link_entity.name} -> {link_entity.target_id}")
# Then follow the link to get the target
target_entity = get(
synapse_id="syn123456",
link_options=LinkOptions(follow_link=True)
)
print(f"Target: {target_entity.name} (type: {type(target_entity).__name__})")
Comprehensive File options
Download file with custom location and collision handling:
from synapseclient import Synapse
from synapseclient.operations import get, FileOptions
syn = Synapse()
syn.login()
file_entity = get(
synapse_id="syn123456",
file_options=FileOptions(
download_file=True,
download_location="/path/to/download/",
if_collision="overwrite.local"
)
)
print(f"Downloaded file: {file_entity.name} to {file_entity.path}")
Table options for table-like entities
Get table entities with column information:
from synapseclient import Synapse
from synapseclient.operations import get, TableOptions
syn = Synapse()
syn.login()
# Works for Table, Dataset, EntityView, MaterializedView,
# SubmissionView, VirtualTable, and DatasetCollection
table_entity = get(
synapse_id="syn123456", # Example table ID
table_options=TableOptions(include_columns=True)
)
print(f"Table: {table_entity.name} with {len(table_entity.columns)} columns")
Combining multiple options
Get a File with both activity and custom download options:
from synapseclient import Synapse
from synapseclient.operations import get, FileOptions, ActivityOptions
syn = Synapse()
syn.login()
file_entity = get(
synapse_id="syn123456",
activity_options=ActivityOptions(include_activity=True),
file_options=FileOptions(
download_file=False
)
)
print(f"File: {file_entity.name} (activity included: {file_entity.activity is not None})")
Working with entity instances
Pass an existing entity instance to refresh or apply new options:
from synapseclient import Synapse
from synapseclient.operations import get, FileOptions
syn = Synapse()
syn.login()
# Get an entity first
entity = get(synapse_id="syn123456")
print(f"Original entity: {entity.name}")
# Then use the entity instance to get it again with different options
refreshed_entity = get(
entity,
file_options=FileOptions(download_file=False)
)
print(f"Refreshed entity: {refreshed_entity.name} (download_file: {refreshed_entity.download_file})")
Source code in synapseclient/operations/factory_operations.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 | |
synapseclient.operations.FileOptions
dataclass
¶
Configuration options specific to File entities when using the factory methods.
This dataclass allows you to customize how File entities are handled during retrieval, including download behavior, file location, and collision handling.
| ATTRIBUTE | DESCRIPTION |
|---|---|
download_file |
Whether to automatically download the file content when retrieving the File entity. If True, the file will be downloaded to the local filesystem. If False, only the metadata will be retrieved. Default is True.
TYPE:
|
download_location |
The local directory path where the file should be downloaded. If None, the file will be downloaded to the default Synapse cache location. If specified, must be a valid directory path. Default is None. |
if_collision |
Strategy to use when a file with the same name already exists at the download location. Valid options are: - "keep.both": Keep both files by appending a number to the new file - "overwrite.local": Overwrite the existing local file - "keep.local": Keep the existing local file and skip download Default is "keep.both".
TYPE:
|
Example
Configure file download options:
from synapseclient.operations import FileOptions
# Download file to specific location with overwrite
file_options = FileOptions(
download_file=True,
download_location="/path/to/downloads/",
if_collision="overwrite.local"
)
# Only retrieve metadata, don't download file content
metadata_only = FileOptions(download_file=False)
Source code in synapseclient/operations/factory_operations.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
synapseclient.operations.ActivityOptions
dataclass
¶
Configuration options for entities that support activity/provenance tracking.
This dataclass controls whether activity information (provenance data) should be included when retrieving entities. Activity information tracks the computational steps, data sources, and relationships that led to the creation of an entity.
| ATTRIBUTE | DESCRIPTION |
|---|---|
include_activity |
Whether to include activity/provenance information when retrieving the entity. If True, the returned entity will have its activity attribute populated with provenance data (if available). If False, the activity attribute will be None. Including activity may result in additional API calls and slower retrieval times. Default is False.
TYPE:
|
Example
Configure activity inclusion:
from synapseclient.operations import ActivityOptions
# Include activity information
with_activity = ActivityOptions(include_activity=True)
# Skip activity information (faster retrieval)
without_activity = ActivityOptions(include_activity=False)
Note
Activity information is only available for entities that support provenance tracking (File, Table, Dataset, etc...). For other entity types, this option is ignored.
Source code in synapseclient/operations/factory_operations.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
synapseclient.operations.TableOptions
dataclass
¶
Configuration options for table-like entities when using the factory methods.
This dataclass controls how table-like entities (Table, Dataset, EntityView, MaterializedView, SubmissionView, VirtualTable, and DatasetCollection) are retrieved, particularly whether column schema information should be included.
| ATTRIBUTE | DESCRIPTION |
|---|---|
include_columns |
Whether to include column schema information when retrieving table-like entities. If True, the returned entity will have its columns attribute populated with Column objects containing schema information (name, column_type, etc.). If False, the columns attribute will be an empty dict. Including columns may result in additional API calls but provides complete table structure information. Default is True.
TYPE:
|
Example
Configure table column inclusion:
from synapseclient.operations import TableOptions
# Include column schema information
with_columns = TableOptions(include_columns=True)
# Skip column information (faster retrieval)
without_columns = TableOptions(include_columns=False)
Source code in synapseclient/operations/factory_operations.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
synapseclient.operations.LinkOptions
dataclass
¶
Configuration options specific to Link entities when using the factory methods.
This dataclass controls how Link entities are handled during retrieval, particularly whether the link should be followed to return the target entity or if the Link entity itself should be returned.
| ATTRIBUTE | DESCRIPTION |
|---|---|
follow_link |
Whether to follow the link and return the target entity instead of the Link entity itself. If True, the factory method will return the entity that the Link points to (e.g., if a Link points to a File, a File object will be returned). If False, the Link entity itself will be returned, allowing you to inspect the link's properties such as target_id, target_version, etc. Default is True.
TYPE:
|
Example
Configure link following behavior:
from synapseclient.operations import LinkOptions
# Follow the link and return the target entity
follow_target = LinkOptions(follow_link=True)
# Return the Link entity itself
return_link = LinkOptions(follow_link=False)
Note
- When follow_link=True, the returned entity type depends on what the Link points to (could be File, Project, Folder, etc.)
- When follow_link=False, a Link entity is always returned
Source code in synapseclient/operations/factory_operations.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
synapseclient.operations.delete
¶
delete(entity: Union[str, CurationTask, Dataset, DatasetCollection, EntityView, Evaluation, File, Folder, Grid, JSONSchema, MaterializedView, Project, RecordSet, SchemaOrganization, SubmissionView, Table, Team, VirtualTable], version: Optional[Union[int, str]] = None, version_only: bool = False, *, synapse_client: Optional[Synapse] = None) -> None
Factory method to delete any Synapse entity.
This method serves as a unified interface for deleting any type of Synapse entity. It automatically applies the appropriate delete logic based on the entity type.
Version Deletion Behavior
The function supports deleting specific versions of entities with the following precedence order:
-
version parameter (highest priority): When provided, this version number will be used for deletion, overriding any version information from the entity object or the entity ID string.
-
Entity's version_number attribute (secondary): If the entity object has a
version_numberattribute set and no explicitversionparameter is provided, this version will be used. -
version_only parameter: When set to True, indicates that only a specific version should be deleted (not the entire entity). This parameter is only meaningful when combined with a version number from either the
versionparameter or the entity'sversion_numberattribute.
Supported for version-specific deletion:
- String ID with version (e.g., "syn123.4")
- File, RecordSet (use version_only=True)
- Table, Dataset, DatasetCollection, EntityView, MaterializedView, SubmissionView, VirtualTable (use version_only=True)
Not supported for version-specific deletion:
- Project, Folder, Evaluation, Team, SchemaOrganization, CurationTask, Grid
| PARAMETER | DESCRIPTION |
|---|---|
entity
|
The entity instance to delete, or a Synapse ID string (e.g., "syn123456" or "syn123456.4" for a specific version). Must be one of the supported entity types or a valid Synapse ID.
TYPE:
|
version
|
Optional version number to delete. Takes precedence over any version
information in the entity object or ID string. When provided with
|
version_only
|
If True, only the specified version will be deleted, not the
entire entity. Requires a version number from either the
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
None |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the entity is not a supported type or not a valid Synapse ID. |
ValueError
|
If version_only is True but no version number is available. |
Deleting a file by object
Delete a file from Synapse:
from synapseclient import Synapse
from synapseclient.models import File
from synapseclient.operations import delete
syn = Synapse()
syn.login()
file = File(id="syn123456")
delete(file)
print("File deleted successfully")
Deleting a specific version of a file
Delete only version 2 of a file, keeping other versions:
from synapseclient import Synapse
from synapseclient.models import File
from synapseclient.operations import delete
syn = Synapse()
syn.login()
# Method 1: Using version parameter (highest priority)
file = File(id="syn123456")
delete(file, version=2, version_only=True)
# Method 2: Using entity's version_number attribute
file = File(id="syn123456", version_number=2)
delete(file, version_only=True)
# Method 3: Using synapse ID with version
delete("syn123456.2", version_only=True)
print("File version 2 deleted successfully")
Deleting a file by ID string
Delete a file from Synapse using just its ID:
from synapseclient import Synapse
from synapseclient.operations import delete
syn = Synapse()
syn.login()
delete("syn123456")
print("Entity deleted successfully")
Deleting a project
Delete a project from Synapse:
from synapseclient import Synapse
from synapseclient.models import Project
from synapseclient.operations import delete
syn = Synapse()
syn.login()
project = Project(id="syn123456")
delete(project)
print("Project deleted successfully")
Deleting a specific version of a RecordSet
Delete only a specific version of a RecordSet:
from synapseclient import Synapse
from synapseclient.models import RecordSet
from synapseclient.operations import delete
syn = Synapse()
syn.login()
record_set = RecordSet(id="syn123456", version_number=3)
delete(record_set, version_only=True)
print("RecordSet version 3 deleted successfully")
Deleting a table
Delete a table from Synapse:
from synapseclient import Synapse
from synapseclient.models import Table
from synapseclient.operations import delete
syn = Synapse()
syn.login()
table = Table(id="syn123456")
delete(table)
print("Table deleted successfully")
Deleting a team
Delete a team from Synapse:
from synapseclient import Synapse
from synapseclient.models import Team
from synapseclient.operations import delete
syn = Synapse()
syn.login()
team = Team(id="123456")
delete(team)
print("Team deleted successfully")
Deleting a curation task
Delete a curation task from Synapse:
from synapseclient import Synapse
from synapseclient.models import CurationTask
from synapseclient.operations import delete
syn = Synapse()
syn.login()
task = CurationTask(task_id=123)
delete(task)
print("Curation task deleted successfully")
Source code in synapseclient/operations/delete_operations.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
synapseclient.operations.store
¶
store(entity: Union[AgentSession, CurationTask, Dataset, DatasetCollection, EntityView, Evaluation, File, Folder, FormData, FormGroup, Grid, JSONSchema, Link, MaterializedView, Project, RecordSet, SchemaOrganization, SubmissionView, Table, Team, VirtualTable], parent: Optional[Union[Folder, Project]] = None, *, file_options: Optional[StoreFileOptions] = None, container_options: Optional[StoreContainerOptions] = None, table_options: Optional[StoreTableOptions] = None, json_schema_options: Optional[StoreJSONSchemaOptions] = None, grid_options: Optional[StoreGridOptions] = None, synapse_client: Optional[Synapse] = None) -> Union[AgentSession, CurationTask, Dataset, DatasetCollection, EntityView, Evaluation, File, Folder, FormData, FormGroup, Grid, JSONSchema, Link, MaterializedView, Project, RecordSet, SchemaOrganization, SubmissionView, Table, Team, VirtualTable]
Factory method to store any Synapse entity.
This method serves as a unified interface for storing any type of Synapse entity. It automatically applies the appropriate store logic based on the entity type and the options provided.
| PARAMETER | DESCRIPTION |
|---|---|
entity
|
The entity instance to store. Must be one of the supported entity types.
TYPE:
|
parent
|
The parent folder or project to store the entity in. Only applicable for File, Folder, Link, and RecordSet entities. Ignored for other entity types. |
file_options
|
File-specific configuration options. Only applies to File and RecordSet entities. Ignored for other entity types.
TYPE:
|
container_options
|
Container-specific configuration options. Only applies to Project and Folder entities. Ignored for other entity types.
TYPE:
|
table_options
|
Table-specific configuration options. Only applies to Table-like entities (Table, Dataset, EntityView, MaterializedView, SubmissionView, VirtualTable, DatasetCollection). Ignored for other entity types.
TYPE:
|
json_schema_options
|
JSONSchema-specific configuration options. Only applies to JSONSchema entities. Required for JSONSchema. Ignored for other entity types.
TYPE:
|
grid_options
|
Grid-specific configuration options. Only applies to Grid entities. Ignored for other entity types.
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
Union[AgentSession, CurationTask, Dataset, DatasetCollection, EntityView, Evaluation, File, Folder, FormData, FormGroup, Grid, JSONSchema, Link, MaterializedView, Project, RecordSet, SchemaOrganization, SubmissionView, Table, Team, VirtualTable]
|
The stored Synapse entity model instance. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the entity is not a supported type. |
Storing a file
Store a file to Synapse:
from synapseclient import Synapse
from synapseclient.models import File
from synapseclient.operations import store
syn = Synapse()
syn.login()
file = File(path="/path/to/file.txt", parent_id="syn123456")
stored_file = store(file)
print(f"Stored file: {stored_file.name} (ID: {stored_file.id})")
Storing a file with custom options
Store a file with custom options:
from synapseclient import Synapse
from synapseclient.models import File
from synapseclient.operations import store, StoreFileOptions
syn = Synapse()
syn.login()
file = File(path="/path/to/file.txt", parent_id="syn123456")
stored_file = store(
file,
file_options=StoreFileOptions(
synapse_store=True,
content_type="text/plain",
merge_existing_annotations=True
)
)
print(f"Stored file: {stored_file.name}")
Storing a folder
Store a folder to Synapse:
from synapseclient import Synapse
from synapseclient.models import Folder
from synapseclient.operations import store
syn = Synapse()
syn.login()
folder = Folder(name="My Folder", parent_id="syn123456")
stored_folder = store(folder)
print(f"Stored folder: {stored_folder.name} (ID: {stored_folder.id})")
Storing a project
Store a project to Synapse:
from synapseclient import Synapse
from synapseclient.models import Project
from synapseclient.operations import store
syn = Synapse()
syn.login()
project = Project(name="My Project")
stored_project = store(project)
print(f"Stored project: {stored_project.name} (ID: {stored_project.id})")
Storing a table
Store a table to Synapse:
from synapseclient import Synapse
from synapseclient.models import Table, Column, ColumnType
from synapseclient.operations import store, StoreTableOptions
syn = Synapse()
syn.login()
table = Table(
name="My Table",
parent_id="syn123456",
columns=[Column(name="col1", column_type=ColumnType.STRING, maximum_size=50)]
)
stored_table = store(
table,
table_options=StoreTableOptions(dry_run=False, job_timeout=600)
)
print(f"Stored table: {stored_table.name} (ID: {stored_table.id})")
Storing a link
Store a link to Synapse:
from synapseclient import Synapse
from synapseclient.models import Link
from synapseclient.operations import store
syn = Synapse()
syn.login()
link = Link(name="My Link", parent_id="syn123456", target_id="syn789")
stored_link = store(link)
print(f"Stored link: {stored_link.name} (ID: {stored_link.id})")
Storing with parent parameter
Store an entity by passing the parent as a parameter:
from synapseclient import Synapse
from synapseclient.models import File, Folder
from synapseclient.operations import store
syn = Synapse()
syn.login()
parent_folder = Folder(id="syn123456")
file = File(path="/path/to/file.txt")
stored_file = store(file, parent=parent_folder)
print(f"Stored file: {stored_file.name} in folder {parent_folder.id}")
Storing an Evaluation
Store an evaluation (challenge queue):
from synapseclient import Synapse
from synapseclient.models import Evaluation
from synapseclient.operations import store
syn = Synapse()
syn.login()
evaluation = Evaluation(
name="My Challenge Evaluation",
description="Evaluation for my data challenge",
content_source="syn123456"
)
stored_evaluation = store(evaluation)
print(f"Stored evaluation: {stored_evaluation.name} (ID: {stored_evaluation.id})")
Creating a Team
Create a new team (note: Teams use create, not update):
from synapseclient import Synapse
from synapseclient.models import Team
from synapseclient.operations import store
syn = Synapse()
syn.login()
team = Team(
name="My Research Team",
description="A team for collaborative research"
)
created_team = store(team)
print(f"Created team: {created_team.name} (ID: {created_team.id})")
Storing a CurationTask
Store a curation task:
from synapseclient import Synapse
from synapseclient.models import CurationTask, FileBasedMetadataTaskProperties
from synapseclient.operations import store
syn = Synapse()
syn.login()
file_properties = FileBasedMetadataTaskProperties(
upload_folder_id="syn1234567",
file_view_id="syn2345678"
)
task = CurationTask(
project_id="syn9876543",
data_type="genomics_data",
instructions="Upload your genomics files",
task_properties=file_properties
)
stored_task = store(task)
print(f"Stored curation task: {stored_task.task_id}")
Source code in synapseclient/operations/store_operations.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 | |
synapseclient.operations.StoreFileOptions
dataclass
¶
Options for storing File entities.
| ATTRIBUTE | DESCRIPTION |
|---|---|
synapse_store |
Whether to store the file in Synapse or use an external URL. |
content_type |
The content type of the file. |
merge_existing_annotations |
If True, merge existing annotations with new ones. If False, replace all annotations. |
associate_activity_to_new_version |
If True, associate activity with new version. If False, do not associate activity. |
Source code in synapseclient/operations/store_operations.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
synapseclient.operations.StoreContainerOptions
dataclass
¶
Options for storing container entities (Project, Folder).
| ATTRIBUTE | DESCRIPTION |
|---|---|
failure_strategy |
Strategy for handling failures when storing child entities. Can be either a FailureStrategy enum value or a string. Valid string values: "LOG_EXCEPTION", "RAISE_EXCEPTION". Valid enum values: FailureStrategy.LOG_EXCEPTION, FailureStrategy.RAISE_EXCEPTION.
TYPE:
|
Source code in synapseclient/operations/store_operations.py
55 56 57 58 59 60 61 62 63 64 65 66 | |
synapseclient.operations.StoreTableOptions
dataclass
¶
Options for storing Table-like entities.
| ATTRIBUTE | DESCRIPTION |
|---|---|
dry_run |
If True, will not actually store but will log what would be done.
TYPE:
|
job_timeout |
Maximum time to wait for table schema update job to complete.
TYPE:
|
Source code in synapseclient/operations/store_operations.py
69 70 71 72 73 74 75 76 77 78 79 | |
synapseclient.operations.StoreJSONSchemaOptions
dataclass
¶
Options for storing JSONSchema entities.
| ATTRIBUTE | DESCRIPTION |
|---|---|
schema_body |
The body of the JSONSchema to store.
TYPE:
|
version |
The version of the JSONSchema body to store. |
dry_run |
Whether or not to do a dry-run.
TYPE:
|
Source code in synapseclient/operations/store_operations.py
82 83 84 85 86 87 88 89 90 91 92 93 94 | |
synapseclient.operations.StoreGridOptions
dataclass
¶
Options for storing Grid entities.
| ATTRIBUTE | DESCRIPTION |
|---|---|
attach_to_previous_session |
If True and using record_set_id, will attach to an existing active session if one exists.
TYPE:
|
timeout |
The number of seconds to wait for the job to complete.
TYPE:
|
Source code in synapseclient/operations/store_operations.py
97 98 99 100 101 102 103 104 105 106 107 108 | |
synapseclient.operations.find_entity_id
¶
find_entity_id(name: str, parent: Optional[Union[str, Dataset, DatasetCollection, EntityView, File, Folder, Link, MaterializedView, Project, RecordSet, SubmissionView, Table, VirtualTable]] = None, *, synapse_client: Optional[Synapse] = None) -> Optional[str]
Find an Entity given its name and parent.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Name of the entity to find.
TYPE:
|
parent
|
An Entity object (Dataset, DatasetCollection, EntityView, File, Folder, Link, MaterializedView, Project, RecordSet, SubmissionView, Table, VirtualTable) or the Id of an entity as a string. Omit if searching for a Project by name.
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
Optional[str]
|
The Entity ID or None if not found. |
Finding a project by name
Find a project using only its name:
from synapseclient import Synapse
from synapseclient.operations import find_entity_id
syn = Synapse()
syn.login()
project_id = find_entity_id(name="My Project")
if project_id:
print(f"Found project: {project_id}")
else:
print("Project not found")
Finding an entity within a parent
Find a file within a specific folder:
from synapseclient import Synapse
from synapseclient.operations import find_entity_id
syn = Synapse()
syn.login()
file_id = find_entity_id(
name="my_data.csv",
parent="syn123456" # Parent folder ID
)
if file_id:
print(f"Found file: {file_id}")
else:
print("File not found in folder")
Using with entity objects
Find an entity using an entity object as the parent:
from synapseclient import Synapse
from synapseclient.models import Folder
from synapseclient.operations import find_entity_id
syn = Synapse()
syn.login()
parent_folder = Folder(id="syn123456")
entity_id = find_entity_id(
name="analysis_results.txt",
parent=parent_folder
)
print(f"Entity ID: {entity_id}")
Source code in synapseclient/operations/utility_operations.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
synapseclient.operations.is_synapse_id
¶
Check if given synID is valid (attached to actual entity).
| PARAMETER | DESCRIPTION |
|---|---|
syn_id
|
A Synapse ID to validate.
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the Synapse ID is valid. |
Validating a Synapse ID
Check if a Synapse ID exists:
from synapseclient import Synapse
from synapseclient.operations import is_synapse_id
syn = Synapse()
syn.login()
if is_synapse_id("syn123456"):
print("Valid Synapse ID")
else:
print("Invalid or non-existent Synapse ID")
Validating multiple IDs
Check multiple Synapse IDs in a loop:
from synapseclient import Synapse
from synapseclient.operations import is_synapse_id
syn = Synapse()
syn.login()
ids_to_check = ["syn123456", "syn999999", "syn789012"]
for synapse_id in ids_to_check:
if is_synapse_id(synapse_id):
print(f"{synapse_id} is valid")
else:
print(f"{synapse_id} is invalid or does not exist")
Using in conditional logic
Use ID validation before attempting operations:
from synapseclient import Synapse
from synapseclient.operations import is_synapse_id, get
syn = Synapse()
syn.login()
user_input = "syn123456"
if is_synapse_id(user_input):
entity = get(synapse_id=user_input)
print(f"Retrieved entity: {entity.name}")
else:
print("Please provide a valid Synapse ID")
Source code in synapseclient/operations/utility_operations.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
synapseclient.operations.onweb
¶
onweb(entity: Union[str, Dataset, DatasetCollection, EntityView, File, Folder, Link, MaterializedView, Project, RecordSet, SubmissionView, Table, VirtualTable], subpage_id: Optional[str] = None, *, synapse_client: Optional[Synapse] = None) -> str
Open up a browser window to the entity page or wiki-subpage.
| PARAMETER | DESCRIPTION |
|---|---|
entity
|
Either an Entity object (Dataset, DatasetCollection, EntityView, File, Folder, Link, MaterializedView, Project, RecordSet, SubmissionView, Table, VirtualTable) or a Synapse ID string.
TYPE:
|
subpage_id
|
(Optional) ID of one of the wiki's sub-pages. |
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The URL that was opened in the browser. |
Opening an entity in browser
Open an entity's page in the default web browser:
from synapseclient import Synapse
from synapseclient.operations import onweb
syn = Synapse()
syn.login()
# Open by Synapse ID
url = onweb("syn123456")
print(f"Opened: {url}")
Opening with an entity object
Open an entity using an entity object:
from synapseclient import Synapse
from synapseclient.models import File
from synapseclient.operations import onweb, get
syn = Synapse()
syn.login()
file = get(synapse_id="syn123456")
url = onweb(file)
print(f"Opened file: {url}")
Opening a wiki subpage
Open a specific wiki subpage:
from synapseclient import Synapse
from synapseclient.operations import onweb
syn = Synapse()
syn.login()
# Open a specific wiki subpage
url = onweb("syn123456", subpage_id="12345")
print(f"Opened wiki subpage: {url}")
Source code in synapseclient/operations/utility_operations.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |
synapseclient.operations.md5_query
¶
Find the Entities which have attached file(s) which have the given MD5 hash.
| PARAMETER | DESCRIPTION |
|---|---|
md5
|
The MD5 hash to query for (hexadecimal string).
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
List[dict]
|
A list of Entity headers matching the MD5 hash. |
Finding entities by MD5 hash
Search for entities with a specific MD5 hash:
from synapseclient import Synapse
from synapseclient.operations import md5_query
syn = Synapse()
syn.login()
md5_hash = "1234567890abcdef1234567890abcdef"
results = md5_query(md5_hash)
print(f"Found {len(results)} entities with MD5: {md5_hash}")
for entity in results:
print(f"- {entity['id']}: {entity['name']}")
Checking for duplicate files
Use MD5 query to find duplicate files before uploading:
from synapseclient import Synapse
from synapseclient.operations import md5_query
import hashlib
syn = Synapse()
syn.login()
# Calculate MD5 of a local file
def calculate_md5(file_path):
hash_md5 = hashlib.md5()
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
file_path = "/path/to/file.txt"
file_md5 = calculate_md5(file_path)
# Check if file already exists in Synapse
existing_entities = md5_query(file_md5)
if existing_entities:
print(f"File already exists in Synapse:")
for entity in existing_entities:
print(f"- {entity['id']}: {entity['name']}")
else:
print("File is unique, safe to upload")
Finding all versions of a file
Find all entities that share the same content (MD5):
from synapseclient import Synapse
from synapseclient.operations import md5_query, get
syn = Synapse()
syn.login()
# Get MD5 from a known file
file = get(synapse_id="syn123456")
if hasattr(file, 'file_handle') and file.file_handle:
file_md5 = file.file_handle.content_md5
# Find all entities with the same MD5
matching_entities = md5_query(file_md5)
print(f"Found {len(matching_entities)} entities with same content:")
for entity in matching_entities:
print(f"- {entity['id']}: {entity['name']} in {entity.get('parentId', 'unknown')}")
Source code in synapseclient/operations/utility_operations.py
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | |
synapseclient.operations.print_entity
¶
print_entity(entity: Union[str, dict, Dataset, DatasetCollection, EntityView, File, Folder, Link, MaterializedView, Project, RecordSet, SubmissionView, Table, VirtualTable], ensure_ascii: bool = True, *, synapse_client: Optional[Synapse] = None) -> None
Pretty print an Entity as JSON.
| PARAMETER | DESCRIPTION |
|---|---|
entity
|
Either an Entity object (Dataset, DatasetCollection, EntityView, File, Folder, Link, MaterializedView, Project, RecordSet, SubmissionView, Table, VirtualTable), a Synapse ID string, or a dictionary representation of an entity.
TYPE:
|
ensure_ascii
|
If True, escapes all non-ASCII characters in the output. Defaults to True.
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
None. Prints the entity to the logger. |
Printing an entity by ID
Print an entity using its Synapse ID:
from synapseclient import Synapse
from synapseclient.operations import print_entity
syn = Synapse()
syn.login()
# Print entity by Synapse ID
print_entity("syn123456")
Printing an entity object
Print an entity object:
from synapseclient import Synapse
from synapseclient.models import File
from synapseclient.operations import print_entity, get
syn = Synapse()
syn.login()
# Get and print a file entity
file = get(synapse_id="syn123456")
print_entity(file)
Controlling ASCII output
Print an entity with non-ASCII characters:
from synapseclient import Synapse
from synapseclient.operations import print_entity
syn = Synapse()
syn.login()
# Print with unicode characters preserved
print_entity("syn123456", ensure_ascii=False)
Source code in synapseclient/operations/utility_operations.py
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 | |