-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathgrid_session_operations.py
More file actions
80 lines (62 loc) · 2.4 KB
/
grid_session_operations.py
File metadata and controls
80 lines (62 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
71
72
73
74
75
76
77
78
79
80
"""
Script: Working with Grid sessions.
Covers creating sessions, importing CSV data, downloading data,
synchronizing changes, and listing/deleting sessions.
"""
from synapseclient import Synapse
from synapseclient.models import Grid, Query
syn = Synapse()
syn.login()
# Create a Grid session from a RecordSet
grid = Grid(record_set_id="syn987654321")
grid = grid.create()
print(f"Grid session: {grid.session_id}")
# Or create a Grid session from an EntityView query
grid_from_query = Grid(initial_query=Query(sql="SELECT * FROM syn123456789"))
grid_from_query = grid_from_query.create()
# Import a CSV from a local file path.
# Column names are read from the CSV header and types are resolved
# from the JSON schema bound to the grid session automatically.
grid = grid.import_csv(path="path/to/metadata.csv")
print(f"Imported {grid.csv_import_total_count} rows")
print(f" Created: {grid.csv_import_created_count}")
print(f" Updated: {grid.csv_import_updated_count}")
# Or import directly from a pandas DataFrame
import pandas as pd
df = pd.DataFrame(
{
"individualID": ["ANIMAL001", "ANIMAL002"],
"species": ["Mouse", "Mouse"],
"sex": ["female", "male"],
"genotype": ["5XFAD", "APOE4KI"],
}
)
grid = grid.import_csv(dataframe=df)
# You can also provide an explicit schema to override auto-derivation:
from synapseclient.models import Column, ColumnType
schema = [
Column(name="individualID", column_type=ColumnType.STRING),
Column(name="species", column_type=ColumnType.STRING),
Column(name="sex", column_type=ColumnType.STRING),
Column(name="genotype", column_type=ColumnType.STRING),
]
grid = grid.import_csv(path="path/to/metadata.csv", schema=schema)
# Download grid data as a local CSV file
file_path = grid.download_csv(download_location="/tmp")
print(f"Downloaded grid data to: {file_path}")
# Synchronize grid changes with the data source
grid = grid.synchronize()
if grid.synchronize_error_messages:
print("Synchronization errors:")
for msg in grid.synchronize_error_messages:
print(f" - {msg}")
else:
print("Synchronization successful")
# List all active grid sessions
for session in Grid.list():
print(f"Session: {session.session_id}, Source: {session.source_entity_id}")
# List sessions for a specific source
for session in Grid.list(source_id="syn987654321"):
print(f"Session: {session.session_id}")
# Delete a grid session
grid.delete()