-
Notifications
You must be signed in to change notification settings - Fork 0
/
USI_submission.py
executable file
·292 lines (241 loc) · 9.05 KB
/
USI_submission.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
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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
USI submission pipeline
Features:
- Creating a new submission
- Adding samples, projects, studies to submission
- check validation status
PreRequest:
The user MUST get an AAP account at https://explore.aai.ebi.ac.uk/home
Example:
python3 USI_submission -flags arguments
Note:
This script doesn't support validation right now. The provided input data
need to be
Options:
* Compulsory
-u Username *
-sp SampleFile
TODO -p projectFile
TODO -r sequencingRunFile
TODO -d study
"""
import json
import requests
import getpass
import argparse
#username = sys.argv[1]
def cli():
my_parser = argparse.ArgumentParser(description='Easier USI submission')
parser = argparse.ArgumentParser(prog=f'\nData submission Portal API tool. \n\nTODO add delete,update actions\n')
#subparsers = parser.add_subparsers(help='execution modes', dest='exec_mode')
# ----- Mode 'create' -----
#sub = subparsers.add_parser('create', help='create a team/submission/sample/proj ect,etc')
parser.add_argument('-u','--username', metavar='<Username>', type=str, required=True,
help='AAP username, case-sensitive')
parser.add_argument('-t', '--team', metavar='<Teamname>', type=str, required=True,
help='create a unique teamname')
parser.add_argument('-td', '--team_descirption', type=str,
help='enter team description')
parser.add_argument('-n', '--new-submission', default=True, action="store_true", required=True,
help='Create a new submission. Requested for every data submission step')
parser.add_argument('-p', '--project',
help='submit a new project')
parser.add_argument('-sp', '--sample',
help='Submit a new sample')
parser.add_argument('-seq', '--seqRun',
help='Submit a new sequence run')
parser.add_argument('-a', '--assay',
help='Submit a new assay')
parser.add_argument('-ad', '--assay_data',
help='Submit a new assay data')
args = parser.parse_args()
# ------- Mode 'delete' ----
#sub = subparsers.add_parser('delete', help='delete a submission TODO')
# ----- Mode 'update' -----
#sub = subparsers.add_parser('update', help=' TODO')
parser.print_help()
return args
def authenticate():
"""
Get token for API submission
PreRequest: the user must get a valid username from https://explore.api.aai.ebi.ac.uk/auth
Note:
1. username is case-sensitive
2. The token is only valid for one hour
3. Every time creating a new submission, the token has to be refreshed.
Output: if credientials correct, return token.
"""
r = requests.get('https://explore.api.aai.ebi.ac.uk/auth', auth=(username, password))
if r.status_code == requests.codes.ok:
return r.text
else:
raise r.raise_for_status()
def creat_team(team_name,team_description):
#TODO new team or check if teamname exist or not
header = {"Content-Type": "application/json",
"Authorization": "Bearer " + token,
"Accept": "application/hal+json"
}
team_info = {
"description":team_description,
"centreName":team_name
}
response = requests.post("https://submission-dev.ebi.ac.uk/api/user/teams",
headers = header,json = team_info)
if response.status_code == requests.codes.created:
response_json = response.json()
team = response_json["name"]
link_submissions_create = response_json['_links']["submissions:create"]['href']
return team,link_submissions_create
else:
raise response.raise_for_status()
def create_submission(link,token):
header = {"Content-Type": "application/json;charset=UTF-8",
"Authorization": "Bearer " + token,
"Accept": "application/hal+json"
}
data = {}
response = requests.post(link,headers = header, json = data)
if response.status_code == requests.codes.created:
response_json = response.json()
return response_json
else:
raise response.raise_for_status()
def load_data(filename):
with open(filename) as f:
data = json.load(f)
return data
def update_sample(token, link, data):
url = link
header = {
"Content-Type": "application/json;charset=UTF-8",
"Accept": "application/hal+json",
"Authorization": "Bearer " + token
}
response = requests.post(url, headers=header, json=data)
if response.status_code == requests.codes.created:
pass
else:
raise response.raise_for_status()
# Collect links in the content for next submission
def get_content(link,token):
header = {"Content-Type": "application/json;charset=UTF-8",
"Authorization": "Bearer " + token,
"Accept": "application/hal+json"
}
data = {}
response = requests.post(eval("link_contents"),headers = header, json = data)
# test
a = response.json()
return a
'''
if response.status_code == requests.codes.created:
print(response.json())
response_json = response.json()
return response_json
else:
raise response.raise_for_status()
'''
def create_domainData(domain,data):
"""
Create a data in one USI submission. the domains are e.g. samples,sequencingRuns,etc
:param domain:eg samples, projects,sequencingRuns
:param data: data in json format, should follow corresponding domain-specific schema requirement
:return:Data submission response
"""
header = {"Content-Type": "application/json;charset=UTF-8",
"Authorization": "Bearer " + token,
"Accept": "application/hal+json"
}
response = requests.post(eval("link_"+domain), headers=header, json=data)
if response.status_code == requests.codes.created:
response_json = response.json()
print(domain+" submit status: ")
print(response.status_code)
return response_json
else:
raise response.raise_for_status()
def submission_status(link,token):
header = {
"Accept": "application/hal+json",
"Authorization": "Bearer " + token
}
response = requests.get(link, headers=header)
if response.status_code == requests.codes.ok:
print("submission status")
print(response.status_code)
#print(response.json())
else:
raise response.raise_for_status()
def validation_status(link,token):
header = {
"Accept": "application/hal+json",
"Authorization": "Bearer " + token
}
response = requests.get(link, headers=header)
if response.status_code == requests.codes.ok:
print("validation results")
print(response.status_code)
responses = response.json()
#print(responses)
print(responses['_embedded']['validationResults'][0]['overallValidationOutcomeByAuthor'])
return responses
else:
raise response.raise_for_status()
# Start command line interface
args = cli()
# Getting arguments from command line
username = args.username
team_name = args.team
team_description = args.team_descirption
sample_file = args.sample
project_file = args.project
seq_file = args.seqRun
assay_file = args.assay
assay_data_file = args.assay_data
# Authentification
input("\nPress Enter to continue...\n")
print("Username(case sensitive: )"+username)
if args.username:
try:
password = getpass.getpass(prompt="AAP Password:",stream=None)
except Exception as error:
print('ERROR', error)
# get original token
token = authenticate()
# creating new team
if args.team:
teams,link_submission = creat_team(team_name,team_description="new ")
# Refresh token after created a new team
token = authenticate()
# creating new submission
if args.new_submission:
new_submission = create_submission(link_submission,token)
# Create link variable with the links in the submission
for i in list(new_submission['_links'].keys()):
# If link name includes ":", replace with "_"
if (":" in i):
new = i.replace(":","_")
vars()["link_"+new]= new_submission['_links'][i]['href']
else:
vars()["link_"+i]= new_submission['_links'][i]['href']
contents = get_content(eval("link_contents"),token)
# Create variable for link to different types of metadata
for i in list(contents['_links'].keys()):
# If link name includes ":", replace with "_"
if (":" in i):
new = i.replace(":","_")
vars()["link_"+new]= contents['_links'][i]['href']
else:
vars()["link_"+i]= contents['_links'][i]['href']
# TODO: connect args.sample/study/assay to link_samples/assay/study automatically.
if args.sample:
sample_filename = args.sample
samples = load_data(sample_filename)
print(samples)
domain = "samples"
create_domainData(domain,eval(domain))
submission_status(link_submissionStatus,token)
validation_status(link_validationResults,token)