-
Notifications
You must be signed in to change notification settings - Fork 4
/
create_and_link_isa_datafile.py
188 lines (128 loc) · 6.28 KB
/
create_and_link_isa_datafile.py
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
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
# -*- coding: utf-8 -*-
"""create_and_link_isa_datafile.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Rai-RiGs30KeeaBgeUe3pL7pZxzINCpZ
Import the libraries so that they can be used within the notebook
* **requests** is used to make HTTP calls
* **json** is used to encode and decode strings into JSON
* **string** is used to perform text manipulation and checking
* **getpass** is used to do non-echoing password input
"""
import requests
import json
import string
import getpass
"""The **base_url** holds the URL to the SEEK instance that will be used in the notebook
**headers** holds the HTTP headers that will be sent with every HTTP call
* **Content-type: application/vnd.api+json** - indicates that any data sent will be in JSON API format
* **Accept: application/vnd.api+json** - indicates that the notebook expects any data returned to be in JSON API format
* **Accept-Charset: ISO-8859-1** - indicates that the notebook expects any text returned to be in ISO-8859-1 character set
"""
base_url = 'https://testing.sysmo-db.org'
headers = {"Content-type": "application/vnd.api+json",
"Accept": "application/vnd.api+json",
"Accept-Charset": "ISO-8859-1"}
"""Create a **requests** HTTP **Session**. A **Session** has re-usable settings such as **headers**
The **authorization** is username and password. The user is prompted for this information.
"""
session = requests.Session()
session.headers.update(headers)
session.auth = (input('Username: '), getpass.getpass('Password: '))
"""The **Investigation**, **Study** and **Assay** will be created within **Project** 33"""
containing_project_id = 33
"""Initialize an **investigation** as a hierarchical structure
The title of the **investigation** is input by the user
The **investigation** is linked to the containing **project**
"""
investigation = {}
investigation['data'] = {}
investigation['data']['type'] = 'investigations'
investigation['data']['attributes'] = {}
investigation['data']['attributes']['title'] = input('Please enter the name for the investigation: ')
investigation['data']['relationships'] = {}
investigation['data']['relationships']['projects'] = {}
investigation['data']['relationships']['projects']['data'] = [{'id' : containing_project_id, 'type' : 'projects'}]
"""**POST** the **investigation** to the SEEK instance
Check the status of the response
"""
r = session.post(base_url + '/investigations', json=investigation)
r.raise_for_status()
"""Extract the created **investigation** from JSON into **populated_investigation**"""
populated_investigation = r.json()
"""Extract the id and URL to the newly created **investigation**"""
investigation_id = populated_investigation['data']['id']
investigation_url = populated_investigation['data']['links']['self']
"""Initialize a **study** as a hierarchical structure
The title of the **study** is input by the user
The **study** is linked to the containing **investigation**
"""
study = {}
study['data'] = {}
study['data']['type'] = 'studies'
study['data']['attributes'] = {}
study['data']['attributes']['title'] = input('Please enter the name for the study: ')
study['data']['relationships'] = {}
study['data']['relationships']['investigation'] = {}
study['data']['relationships']['investigation']['data'] = {'id' : investigation_id, 'type' : 'investigations'}
"""**POST** the **study** to the SEEK instance
Check the status of the response
"""
r = session.post(base_url + '/studies', json=study)
r.raise_for_status()
"""Extract the created **study** from JSON into ***populated_study***"""
populated_study = r.json()
"""Extract the id and URL to the newly created **study**"""
study_id = populated_study['data']['id']
study_url = populated_study['data']['links']['self']
"""Initialize an **assay** as a hierarchical structure
The title of the **assay** is input by the user
The **assay** is linked to the containing **study**
"""
assay = {}
assay['data'] = {}
assay['data']['type'] = 'assays'
assay['data']['attributes'] = {}
assay['data']['attributes']['title'] = input('Please enter the name for the assay: ')
assay['data']['attributes']['assay_class'] = {'key' : 'EXP'}
assay['data']['attributes']['assay_type'] = {'uri' : 'http://jermontology.org/ontology/JERMOntology#Metabolomics'}
assay['data']['attributes']['technology_type'] = {'uri' : 'http://jermontology.org/ontology/JERMOntology#Electrophoresis'}
assay['data']['relationships'] = {}
assay['data']['relationships']['study'] = {}
assay['data']['relationships']['study']['data'] = {'id' : study_id, 'type' : 'studies'}
"""**POST** the **assay** to the SEEK instance
Check the status of the response
"""
r = session.post(base_url + '/assays', json=assay)
r.raise_for_status()
"""Extract the created **assay** from JSON into ***populated_assay***"""
populated_assay = r.json()
"""Extract the id and URL to the newly created **assay**"""
assay_id = populated_assay['data']['id']
assay_url = populated_assay['data']['links']['self']
"""Initialize a **data_file** as a hierarchical structure
The title of the **data_file** is input by the user
The **data_file** is linked to the containing **project** and to the newly created **assay**
"""
data_file = {}
data_file['data'] = {}
data_file['data']['type'] = 'data_files'
data_file['data']['attributes'] = {}
data_file['data']['attributes']['title'] = input('Please enter the name for the data_file: ')
remote_blob = {'url' : input('Please enter the URL for the remote data: ')}
data_file['data']['attributes']['content_blobs'] = [remote_blob]
data_file['data']['relationships'] = {}
data_file['data']['relationships']['projects'] = {}
data_file['data']['relationships']['projects']['data'] = [{'id' : containing_project_id, 'type' : 'projects'}]
data_file['data']['relationships']['assays'] = {}
data_file['data']['relationships']['assays']['data'] = [{'id' : assay_id, 'type' : 'assays'}]
"""**POST** the **data_file** to the SEEK instance
Check the status of the response
"""
r = session.post(base_url + '/data_files', json=data_file)
r.raise_for_status()
"""Extract the created **data_file** from JSON into **populated_data_file**"""
populated_data_file = r.json()
"""Extract the id and URL to the newly created **data_file**"""
data_file_id = populated_data_file['data']['id']
data_file_url = populated_data_file['data']['links']['self']