forked from ENCODE-DCC/submission_sample_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ENCODETools.py
155 lines (140 loc) · 5.32 KB
/
ENCODETools.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
import os
import sys
import csv
import json
import jsonschema
import requests
from pyelasticsearch import ElasticSearch
import xlrd
import xlwt
from base64 import b64encode
# set headers. UNCLEAR IF THIS IS USED PROPERLY
HEADERS = {'content-type': 'application/json'}
# get object from server
def get_ENCODE(obj_id,keys):
'''GET an ENCODE object as JSON and return as dict'''
url = keys['server']+obj_id+'?limit=all'
response = requests.get(url, auth=(keys['authid'],keys['authpw']), headers=HEADERS)
if not response.status_code == 200:
print >> sys.stderr, response.text
return response.json()
# get object from server
def GetENCODE(object_id,keys):
'''GET an ENCODE object as JSON and return as dict'''
if type(object_id) is str:
url = keys['server']+object_id+'?limit=all'
#print(url)
try:
response = requests.get(url, auth=(keys['authid'],keys['authpw']), headers=HEADERS)
if not response.status_code == 200:
print >> sys.stderr, response.text
# no
except Exception as e:
print("Get request failed:")
#print(e)
# yes
else:
return response.json()
# patch object to server
def patch_ENCODE(obj_id,patch_json,keys):
'''PATCH an existing ENCODE object and return the response JSON'''
url = keys['server']+obj_id
json_payload = json.dumps(patch_json)
response = requests.patch(url, auth=(keys['authid'],keys['authpw']), data=json_payload)
print "Patch:"
print response.status_code
if not response.status_code == 200:
print >> sys.stderr, response.text
return response.json()
# post object to server
def new_ENCODE(collection_id, object_json):
'''POST an ENCODE object as JSON and return the resppnse JSON'''
url = SERVER+'/'+collection_id+'/'
json_payload = json.dumps(object_json)
response = requests.post(url, auth=(AUTHID, AUTHPW), headers=HEADERS, data=json_payload)
if not response.status_code == 201:
print >> sys.stderr, response.text
return response.json()
# get keys from file
def KeyENCODE(key_file,server_name):
key_open = open(key_file)
keys = csv.DictReader(key_open,delimiter = '\t')
for key in keys:
if key.get('Server') == server_name:
key_info = {}
key_info['user'] = key.get('User')
key_info['server'] = ('http://' + key.get('Server') + '.encodedcc.org')
key_info['authid'] = key.get('ID')
key_info['authpw'] = key.get('PW')
key_open.close()
return(key_info)
# read json objects from file
def ReadJSON(json_file):
json_load = open(json_file)
json_read = json.load(json_load)
json_load.close()
return json_read
# write new json obect. SHOULD BE MODIFIED TO CUSTOM OUTPUT FORMAT (FOR HUMAN VIEWING)
def WriteJSON(new_object,object_file):
with open(object_file, 'w') as outfile:
json.dump(new_object, outfile)
outfile.close()
# check json object for validity. SHOULD ONLY NEED OBJECT. NEED DEF TO EXTRACT VALUE (LIKE TYPE) FROM JSON OBJECT GRACEFULLY.
def ValidJSON(object_type,object_id,new_object):
#get the relevant schema
object_schema = get_ENCODE(('/profiles/' + object_type + '.json'))
# test the new object. SHOULD HANDLE ERRORS GRACEFULLY
try:
jsonschema.validate(new_object,object_schema)
# did not validate
except Exception as e:
print('Validation of ' + object_id + ' failed.')
print(e)
return False
# did validate
else:
# inform the user of the success
print('Validation of ' + object_id + ' succeeded.')
return True
# intended to fix invalid JSON. DOES NOT DO ANYTHING YET.
def CleanJSON(object_type,object_id,new_object,keys):
for key,value in new_object.items():
new_object.pop(key)
if not ValidJSON(object_type,object_id,new_object):
new_object[key] = value
else:
return True
# flatten embedded json objects to their ID
def FlatJSON(json_object,keys):
json_object = EmbedJSON(json_object,keys)
for key,value in json_object.items():
if type(value) is dict:
json_object[key] = json_object[key][u'@id']
if type(value) is list:
#print("Found List: " + key)
value_new = []
for value_check in value:
#print("Checking...")
if type(value_check) is dict:
#print("Found Object")
value_check = value_check[u'@id']
#print(value_check)
value_new.append(value_check)
json_object[key] = value_new
return json_object
# expand json object
def EmbedJSON(json_object,keys):
for key,value in json_object.items():
value_list = []
if type(value) is unicode:
value_list.append(value)
elif type(value) is list:
value_list = value
for value_check in value_list:
if type(value_check) is unicode:
if str(value_check[0]) == '/':
json_sub_object = GetENCODE(str(value_check),keys)
if type(json_sub_object) is dict:
#json_sub_object = EmbedJSON(json_sub_object,keys)
json_object[key] = json_sub_object
return json_object