-
Notifications
You must be signed in to change notification settings - Fork 5
/
post.py
executable file
·47 lines (38 loc) · 1.54 KB
/
post.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
#!/usr/bin/env python2
# -*- coding: latin-1 -*-
'''POST an object to an ENCODE server'''
import sys, requests, json
# Send and accept JSON format
HEADERS = {'content-type': 'application/json', 'accept': 'application/json'}
# Authentication is always required to PATCH ENCODE objects
AUTHID = "H7OL67B4" #<- Replace this with your keypair
AUTHPW = "lr5gz2fjowbaqox5" #<- Replace this with your keypair
# The URL is now the collection itself
URL = "http://test.encodedcc.org/experiments/"
# Build a Python dict with the experiment metadata
new_experiment = {
"description": "POST example experiment",
"assay_term_name": "ChIP-seq",
"biosample_term_name": "Stromal cell of bone marrow",
"target": "/targets/SMAD6-human/",
"award": "/awards/U41HG006992/",
"lab": "/labs/j-michael-cherry/",
"references": [
"PMID:12345",
"PMID:67890"
]
}
# Serialize the data structure as JSON
json_payload = json.dumps(new_experiment)
# POST the JSON and print the response
response = requests.post(URL, auth=(AUTHID, AUTHPW), headers=HEADERS, data=json_payload)
# If the POST succeeds, the response is the new object in JSON format
print json.dumps(response.json(), indent=4, separators=(',', ': '))
# Check the status code and if good, extract the accession number of the new object
if not response.status_code == 201:
print >> sys.stderr, response.text
else:
response_dict = response.json()
posted_experiment = response_dict['@graph'][0]
new_experiment_accession = posted_experiment['accession']
print "New ENCODE accession number: %s" %(new_experiment_accession)