-
Notifications
You must be signed in to change notification settings - Fork 13
/
ENCODE_controlled_by_backfill.py
executable file
·395 lines (360 loc) · 17.2 KB
/
ENCODE_controlled_by_backfill.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env python3
# -*- coding: latin-1 -*-
import argparse
import os.path
import encodedcc
import sys
EPILOG = '''
Script to fix the controlled_by backfill problems
This is a dryrun default script, run with '--update' to PATCH data
Useage:
%(prog)s --infile MyFile.txt
%(prog)s --infile ENCSR000AAA
%(prog)s --infile ENCSR000AAA,ENCSR000AAB,ENCSR000AAC
%(prog)s --query "/search/?type=Experiment"
Script will take a file with single column list of accessions
Can also take a single accession or comma separated list of accessions
A query from which to gather accessions
%(prog)s --method single
%(prog)s --method multi
%(prog)s --method biosample
There are three methods to pick from
"single" assumes one replicate in the control
"multi" assumes one control with number of replicates equal to number of replicates in experiment
"biosample" assumes multiple controls that should be matched on biosample
***By NOT selecting the '--method' option the script will try to guess at what the correct method is***
%(prog)s --ignore_runtype
This makes the script ignore the value of the paired ends, default is off
%(prog)s --missing
Script will print out only the names of files missing controlled_by
For more details:
%(prog)s --help
'''
def getArgs():
parser = argparse.ArgumentParser(
description=__doc__, epilog=EPILOG,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument('--method',
help="'single' = there is only one replicate in the control, \
'multi' = one control with same number of replicates as experiment has replicates, \
'biosample' = multiple controls should be matched on the biosample",
choices=["single", "multi", "biosample"])
parser.add_argument('--ignore_runtype',
help="Ignores value of paired-end. Default is off",
default=False,
action='store_true')
parser.add_argument('--infile',
help="file containing single column list of object accessions,\
single accession, or comma separated list of accessions")
parser.add_argument('--query',
help="query of objects you want to process")
parser.add_argument('--key',
default='default',
help="The keypair identifier from the keyfile. \
Default is --key=default")
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 off.")
parser.add_argument('--update',
default=False,
action='store_true',
help="Let the script PATCH the data. Default is off")
parser.add_argument('--missing',
default=False,
action='store_true',
help="Only print files that are missing controlled_by.\
Default is off")
args = parser.parse_args()
return args
class BackFill:
def __init__(self, connection, debug=False, missing=False, update=False, ignore_runtype=False):
self.connection = connection
self.DEBUG = debug
self.MISSING = missing
self.update = update
self.ignore_runtype = ignore_runtype
self.dataList = []
def updater(self, exp, con):
''' helper function runs the update step'''
temp = encodedcc.get_ENCODE(
exp + '?datastore=database', self.connection).get("controlled_by", [])
if con not in temp:
control = temp + [con]
patch_dict = {"controlled_by": control}
print("patching experiment file {} with controlled_by {}".format(exp, con))
encodedcc.patch_ENCODE(exp, self.connection, patch_dict)
else:
print("ERROR: controlled_by for experiment file {} already contains {}".format(
exp, con))
def single_rep(self, obj):
'''one control with one replicate in control,
multiple replicates in experiment'''
control_files = encodedcc.get_ENCODE(
obj["possible_controls"][0]["accession"], self.connection, frame="embedded").get("files", [])
if len(control_files) == 0:
if self.DEBUG:
print("Control object {} has no files".format(
obj["possible_controls"][0]["accession"]), file=sys.stderr)
return
for c in control_files:
if c.get("file_type", "") == "fastq":
exp_list = []
for e in obj["files"]:
if e.get("file_type", "") == "fastq":
if not self.MISSING or (self.MISSING and not e.get("controlled_by")):
exp_list.append(e["accession"])
for exp in exp_list:
temp = {"ExpAcc": obj["accession"], "Method": "Single",
"ExpFile": exp, "ConFile": c["accession"]}
self.dataList.append(temp)
if self.update:
self.updater(exp, c["accession"])
if self.DEBUG:
print("ExpFile: {}, ConFile: {}".format(
temp["ExpFile"], temp["ConFile"]))
def pair_dict_maker(self, x_data, x):
''' helper function makes the exp_data
and con_data dictionaries'''
x_file_bio_num = x.get("biological_replicates")
x_file_paired = x.get("paired_end")
x_file_acc = x["accession"]
if self.ignore_runtype:
x_file_paired = None
x_pair = str(x_file_bio_num[0]) + "-" + str(x_file_paired)
x_data[x_file_acc] = x_pair
def multi_rep(self, obj):
'''one control, with one replicate in
control per replicate in experiment'''
control_files = encodedcc.get_ENCODE(
obj["possible_controls"][0]["accession"], self.connection, frame="embedded").get("files", [])
control_replicates = obj["possible_controls"][0].get("replicates", [])
exp_data = {}
con_data = {}
if len(control_replicates) != len(obj["replicates"]):
if self.DEBUG:
print("Control has {} replicates and experiment has {} replicates".format(
len(control_replicates), len(obj["replicates"])), file=sys.stderr)
return
if len(control_files) == 0:
if self.DEBUG:
print("Control {} has no files".format(
obj["possible_controls"][0]["accession"]), file=sys.stderr)
return
for e in obj["files"]:
if e.get("file_type", "") == "fastq":
if not self.MISSING or (self.MISSING and not e.get("controlled_by")):
self.pair_dict_maker(exp_data, e)
for c in control_files:
if c.get("file_type", "") == "fastq":
self.pair_dict_maker(con_data, c)
if self.ignore_runtype:
self.mini(exp_data, con_data, obj)
else:
self.mini(con_data, exp_data, obj)
def mini(self, x_data, y_data, obj):
''' just a helper function
does all the fancy sorting for multi rep
'''
for x_key in x_data.keys():
temp_list = []
for y_key in y_data.keys():
if x_data[x_key] == y_data[y_key]:
temp_list.append(y_key)
if self.ignore_runtype:
for t in temp_list:
temp = {
"ExpAcc": obj["accession"], "Method": "Multi-runtype ignored", "ExpFile": x_key, "ConFile": t}
self.dataList.append(temp)
if self.update:
self.updater(x_key, t)
if self.DEBUG:
print("ExpFile: {}, ConFile: {}".format(
temp["ExpFile"], temp["ConFile"]))
else:
for t in temp_list:
temp = {
"ExpAcc": obj["accession"], "Method": "Multi", "ExpFile": t, "ConFile": x_key}
self.dataList.append(temp)
if self.update:
self.updater(t, x_key)
if self.DEBUG:
print("ExpFile: {}, ConFile: {}".format(
temp["ExpFile"], temp["ConFile"]))
def multi_control(self, obj):
'''multiple controls, match on biosample'''
con_data = {}
val = True
for con in obj["possible_controls"]:
c = encodedcc.get_ENCODE(
con["accession"], self.connection, frame="embedded")
if c.get("replicates"):
for rep in c["replicates"]:
if c.get("files"):
con_bio_acc = rep["library"]["biosample"]["accession"]
con_bio_num = rep["biological_replicate_number"]
for f in c["files"]:
if f.get("file_type", "") == "fastq":
con_file_bio_num = f["biological_replicates"]
if con_bio_num in con_file_bio_num:
con_file_acc = f["accession"]
con_data[con_bio_acc] = con_file_acc
else:
if self.DEBUG:
print("No files found for control {}".format(
con["accession"]), file=sys.stderr)
val = False
else:
if self.DEBUG:
print("No replicates found in control {}".format(
con["accession"]), file=sys.stderr)
val = False
if val:
exp_data = {}
for e in obj["replicates"]:
exp_bio_acc = e["library"]["biosample"]["accession"]
exp_bio_num = e["biological_replicate_number"]
for f in obj["files"]:
if f.get("file_type", "") == "fastq":
if not self.MISSING or (self.MISSING and not f.get("controlled_by")):
exp_file_bio_num = f["biological_replicates"]
if exp_bio_num in exp_file_bio_num:
exp_file_acc = f["accession"]
exp_data[exp_bio_acc] = exp_file_acc
for key in exp_data.keys():
if con_data.get(key):
temp = {"ExpAcc": obj["accession"], "Method": "Biosample",
"ExpFile": exp_data[key], "ConFile": con_data[key]}
self.dataList.append(temp)
if self.update:
self.updater(exp_data[key], con_data[key])
if self.DEBUG:
print("Biosample: {}, ExpFile: {}, ConFile: {}".format(
key, temp["ExpFile"], temp["ConFile"]))
def main():
args = getArgs()
key = encodedcc.ENC_Key(args.keyfile, args.key)
connection = encodedcc.ENC_Connection(key)
accessions = []
if args.update:
print("This is an UPDATE run data will be PATCHed")
else:
print("This is a dryrun, no data will be changed")
if args.infile:
if os.path.isfile(args.infile):
accessions = [line.rstrip('\n') for line in open(args.infile)]
else:
accessions = args.infile.split(",")
elif args.query:
if "search" in args.query:
temp = encodedcc.get_ENCODE(
args.query, connection).get("@graph", [])
else:
temp = [encodedcc.get_ENCODE(args.query, connection)]
if any(temp):
for obj in temp:
if obj.get("accession"):
accessions.append(obj["accession"])
elif obj.get("uuid"):
accessions.append(obj["uuid"])
elif obj.get("@id"):
accessions.append(obj["@id"])
elif obj.get("aliases"):
accessions.append(obj["aliases"][0])
if len(accessions) == 0:
# if something happens and we end up with no accessions stop
print("ERROR: object has no identifier", file=sys.stderr)
sys.exit(1)
else:
for acc in accessions:
obj = encodedcc.get_ENCODE(acc, connection, frame="embedded")
isValid = True
check = ["replicates", "files"]
for c in check:
if not obj.get(c):
if args.debug:
print("Missing {} for {}".format(
c, acc), file=sys.stderr)
isValid = False
if obj.get("possible_controls"):
for p in obj["possible_controls"]:
for c in check:
if not obj.get(c):
if args.debug:
print("Missing {} for {}".format(
c, p["accession"]), file=sys.stderr)
isValid = False
else:
isValid = False
if args.debug:
print("Missing possible_controls for {}".format(
acc), file=sys.stderr)
if isValid:
backfill = BackFill(connection, debug=args.debug, missing=args.missing,
update=args.update, ignore_runtype=args.ignore_runtype)
if args.method == "single":
if args.debug:
print("SINGLE REP {}".format(acc))
backfill.single_rep(obj)
elif args.method == "multi":
if args.debug:
print("MULTI REP {}".format(acc))
backfill.multi_rep(obj)
elif args.method == "biosample":
if args.debug:
print("BIOSAMPLE {}".format(acc))
backfill.multi_control(obj)
else:
exp_rep = len(obj["replicates"])
exp_con = len(obj["possible_controls"])
if exp_con == 1:
# one possible control
con_rep = len(obj["possible_controls"]
[0]["replicates"])
if con_rep == exp_rep:
# same number experiment replicates as control replicates
# method is multi
if args.debug:
print("MULTI REP {}".format(acc))
backfill.multi_rep(obj)
elif con_rep == 1:
# one control replicate and multiple experiment replicates
# method is single
if args.debug:
print("SINGLE REP {}".format(acc))
backfill.single_rep(obj)
else:
if args.debug:
print("Experiment {} contains {} experiment replicates and {} control replicates and so does not fit the current pattern!".format(
acc, exp_rep, con_rep))
elif exp_con > 1:
# more than one possible control
con_reps = 0
for con in obj["possible_controls"]:
if len(con["replicates"]) == 1:
con_reps += 1
if con_reps == exp_rep:
# same number of controls with one replicate as number of experiment replicates
# method is biosample
if args.debug:
print("BIOSAMPLE {}".format(acc))
backfill.multi_control(obj)
else:
if args.debug:
print("Experiment {} contains {} experiment replicates and {} control replicates between {} total controls and so does not fit the current pattern!".format(
acc, exp_rep, con_rep, exp_con))
else:
if args.debug:
print(
"Experiment {} does not fit any of the current patterns!".format(acc))
if len(backfill.dataList) > 0:
print("Experiment\tMethod\tExperimentFile\tControlFile")
for data in backfill.dataList:
print("{ExpAcc}\t{Method}\t{ExpFile}\t{ConFile}".format(
ExpAcc=data["ExpAcc"], Method=data["Method"], ExpFile=data["ExpFile"], ConFile=data["ConFile"]))
if __name__ == '__main__':
main()