-
Notifications
You must be signed in to change notification settings - Fork 4
/
findControlledBy.py
259 lines (214 loc) · 8.57 KB
/
findControlledBy.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
import requests
import json
import argparse
import os.path
HEADERS = {'content-type': 'application/json'}
DEBUG_ON = False
SERVER = 'http://www.encodeproject.org'
AUTHID = 'default provided by keypairs.json'
AUTHPW = 'default provided by keypairs.json'
EPILOG = ''' Provide a list of experiments that need a
controlled_by for thier fastqs. Assumptions are:
If the file has a value in controlled_by then leave it alone.
To use a different key from the default keypair file:
%(prog)s --key submit
'''
def set_ENCODE_keys(keyfile, key):
'''
Set the global authentication keyds
'''
keysf = open(keyfile, 'r')
keys_json_string = keysf.read()
keysf.close()
keys = json.loads(keys_json_string)
key_dict = keys[key]
global AUTHID
global AUTHPW
global SERVER
AUTHID = key_dict['key']
AUTHPW = key_dict['secret']
SERVER = key_dict['server']
if not SERVER.endswith("/"):
SERVER += "/"
return
def get_ENCODE(obj_id):
'''GET an ENCODE object as JSON and return as dict
'''
# url = SERVER+obj_id+'?limit=all&datastore=database'
url = SERVER+obj_id+'?frame=embedded'
if DEBUG_ON:
print "DEBUG: GET %s" % (url)
response = requests.get(url, auth=(AUTHID, AUTHPW), headers=HEADERS)
if DEBUG_ON:
print "DEBUG: GET RESPONSE code %s" % (response.status_code)
try:
if response.json():
print "DEBUG: GET RESPONSE JSON"
print json.dumps(
response.json(),
indent=4,
separators=(',', ': ')
)
except:
print "DEBUG: GET RESPONSE text %s" % (response.text)
if not response.status_code == requests.codes.ok:
return ''
response.raise_for_status()
return response.json()
def get_experiment_list(file, search):
objList = []
if search == "NULL":
f = open(file)
objList = f.readlines()
for i in range(0, len(objList)):
objList[i] = objList[i].strip()
else:
set = get_ENCODE(search+'&limit=all&frame=embedded')
for i in range(0, len(set['@graph'])):
objList.append(set['@graph'][i]['accession'])
# objList.append(set['@graph'][i]['uuid'])
return objList
def get_fastq_dictionary(exp):
controlfiles = {}
control = get_ENCODE(exp)
for ff in control['files']:
if ff.get('file_format') != 'fastq':
continue
if 'replicate' not in ff:
print "Missing replicate error"
continue
biorep = str(ff['replicate']['biological_replicate_number'])
# techrep = str(ff['replicate']['technical_replicate_number'])
pair = str(ff.get('paired_end'))
# rep = biorep + '-' + techrep
# key = rep + '-' + pair
biokey = biorep + '-' + pair
if biokey not in controlfiles:
controlfiles[biokey] = [ff['accession']]
else:
print "error: replicate-pair has multiple files"
controlfiles[biokey].append(ff['accession'])
# controlfiles[key].append('multiple-files-error')
return controlfiles
def get_HAIB_fastq_dictionary(controls):
controlfiles = {}
for control in controls:
for ff in control['files']:
ff_obj = get_ENCODE(ff)
if ff_obj.get('file_format') != 'fastq':
continue
if 'replicate' not in ff_obj:
print "Missing replicate error"
continue
try:
lib = get_ENCODE(ff_obj['replicate']['library'])
biosample = lib['biosample']['accession']
except:
print 'Cannot find biosample'
biosample = ''
key = biosample
if key not in controlfiles:
controlfiles[key] = [ff_obj['accession']]
else:
print "error: biosample has multiple files"
controlfiles[key].append(ff_obj['accession'])
controlfiles[key].append('same-biosample-error')
print controlfiles
return controlfiles
def main():
parser = argparse.ArgumentParser(
description=__doc__, epilog=EPILOG,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
'--infile', '-i',
default='infile',
help="File containing a list of ENCSRs.")
parser.add_argument(
'--search',
default='NULL',
help="The search parameters.")
parser.add_argument(
'--key',
default='default',
help="The keypair identifier from the keyfile.")
parser.add_argument(
'--keyfile',
default=os.path.expanduser("~/keypairs.json"),
help="The keypair file. Default is --keyfile=%s" % (os.path.expanduser("~/keypairs.json")))
parser.add_argument(
'--debug',
default=False,
action='store_true',
help="Print debug messages. Default is False.")
parser.add_argument(
'--existing',
default=False,
action='store_true',
help="Print debug messages. Default is False.")
args = parser.parse_args()
DEBUG_ON = args.debug
set_ENCODE_keys(args.keyfile, args.key)
objList = get_experiment_list(args.infile, args.search)
for item in objList:
print "Experiment:" + item
obj = get_ENCODE(item)
controlfiles = {}
#for i in obj.get('possible_controls'):
# controlIds.append(i['accession'])
# Missing possible controls, bail out
if 'possible_controls' not in obj or len(obj['possible_controls']) == 0:
print 'error: {} has no possible_controls'.format(obj['accession'])
continue
# If it is HAIB
elif obj['lab']['name'] == 'richard-myers':
controlfiles = get_HAIB_fastq_dictionary(obj['possible_controls'])
# Single possible control
elif len(obj['possible_controls']) == 1:
controlId = obj['possible_controls'][0]['accession']
controlfiles = get_fastq_dictionary(controlId)
# Double possible controls
#elif len(obj['possible_controls']) == 2:
# controlId1 = obj['possible_controls'][0]['accession']
# controlfiles1 = get_fastq_dictionary(controlId1)
# controlId2 = obj['possible_controls'][1]['accession']
# controlfiles2 = get_fastq_dictionary(controlId2)
# for x in controlfiles1:
# if x in controlfiles2:
# controlfiles[x] = controlfiles1[x] + controlfiles2[x]
# else:
# controlfiles[x] = controlfiles1[x]
# More than 1 possible controls
else:
print 'error: {} has more than 1 possible_controls'.format(obj['accession'])
continue
if DEBUG_ON:
print controlfiles
# Now find the experiment fastqs
for ff in obj['files']:
findold = args.existing or (ff.get('controlled_by') == [] or ff.get('controlled_by') is None)
if ff.get('file_format') == 'fastq' and findold:
biorep = str(ff['replicate']['biological_replicate_number'])
techrep = str(ff['replicate']['technical_replicate_number'])
pair = str(ff.get('paired_end'))
rep = biorep + '-' + techrep + '-' + pair
biokey = biorep + '-' + pair
if ff['lab']['name'] == 'richard-myers':
if 'replicate' in ff:
if 'library' in ff['replicate']:
if 'biosample' in ff['replicate']['library']:
biokey = ff['replicate']['library']['biosample']['accession']
print ff['replicate']['library']['biosample']['accession']
else:
biokey = None
if rep in controlfiles:
answer = controlfiles[rep]
biokey = rep
elif biokey in controlfiles:
answer = controlfiles[biokey]
else:
answer = 'error: control had no corresponding replicate'
print biokey, ff['accession'], answer
print ''
if __name__ == '__main__':
main()