forked from broadinstitute/dockstore-tool-cms2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
norm_and_collate_block.py
executable file
·429 lines (356 loc) · 19 KB
/
norm_and_collate_block.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/usr/bin/env python3
"""Normalize component scores, and collate the results.
"""
import argparse
import csv
import collections
import concurrent.futures
import contextlib
import copy
import functools
import glob
import gzip
import io
import json
import logging
import multiprocessing
import os
import os.path
import pathlib
import random
import re
import shutil
import subprocess
import sys
import tempfile
import time
import pandas as pd
# * Utils
_log = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(message)s')
MAX_INT32 = (2 ** 31)-1
def dump_file(fname, value):
"""store string in file"""
with open(fname, 'w') as out:
out.write(str(value))
def _pretty_print_json(json_val, sort_keys=True):
"""Return a pretty-printed version of a dict converted to json, as a string."""
return json.dumps(json_val, indent=4, separators=(',', ': '), sort_keys=sort_keys)
def _write_json(fname, json_val):
dump_file(fname=fname, value=_pretty_print_json(json_val))
def _load_dict_sorted(d):
return collections.OrderedDict(sorted(d.items()))
def _json_loads(s):
return json.loads(s.strip(), object_hook=_load_dict_sorted, object_pairs_hook=collections.OrderedDict)
def _json_loadf(fname):
return _json_loads(slurp_file(fname))
def slurp_file(fname, maxSizeMb=50):
"""Read entire file into one string. If file is gzipped, uncompress it on-the-fly. If file is larger
than `maxSizeMb` megabytes, throw an error; this is to encourage proper use of iterators for reading
large files. If `maxSizeMb` is None or 0, file size is unlimited."""
fileSize = os.path.getsize(fname)
if maxSizeMb and fileSize > maxSizeMb*1024*1024:
raise RuntimeError('Tried to slurp large file {} (size={}); are you sure? Increase `maxSizeMb` param if yes'.
format(fname, fileSize))
with open_or_gzopen(fname) as f:
return f.read()
def open_or_gzopen(fname, *opts, **kwargs):
mode = 'r'
open_opts = list(opts)
assert type(mode) == str, "open mode must be of type str"
# 'U' mode is deprecated in py3 and may be unsupported in future versions,
# so use newline=None when 'U' is specified
if len(open_opts) > 0:
mode = open_opts[0]
if sys.version_info[0] == 3:
if 'U' in mode:
if 'newline' not in kwargs:
kwargs['newline'] = None
open_opts[0] = mode.replace("U","")
# if this is a gzip file
if fname.endswith('.gz'):
# if text read mode is desired (by spec or default)
if ('b' not in mode) and (len(open_opts)==0 or 'r' in mode):
# if python 2
if sys.version_info[0] == 2:
# gzip.open() under py2 does not support universal newlines
# so we need to wrap it with something that does
# By ignoring errors in BufferedReader, errors should be handled by TextIoWrapper
return io.TextIOWrapper(io.BufferedReader(gzip.open(fname)))
# if 't' for text mode is not explicitly included,
# replace "U" with "t" since under gzip "rb" is the
# default and "U" depends on "rt"
gz_mode = str(mode).replace("U","" if "t" in mode else "t")
gz_opts = [gz_mode]+list(opts)[1:]
return gzip.open(fname, *gz_opts, **kwargs)
else:
return open(fname, *open_opts, **kwargs)
def available_cpu_count():
"""
Return the number of available virtual or physical CPUs on this system.
The number of available CPUs can be smaller than the total number of CPUs
when the cpuset(7) mechanism is in use, as is the case on some cluster
systems.
Adapted from http://stackoverflow.com/a/1006301/715090
"""
cgroup_cpus = MAX_INT32
try:
def get_cpu_val(name):
return float(slurp_file('/sys/fs/cgroup/cpu/cpu.'+name).strip())
cfs_quota = get_cpu_val('cfs_quota_us')
if cfs_quota > 0:
cfs_period = get_cpu_val('cfs_period_us')
_log.debug('cfs_quota %s, cfs_period %s', cfs_quota, cfs_period)
cgroup_cpus = max(1, int(cfs_quota / cfs_period))
except Exception as e:
pass
proc_cpus = MAX_INT32
try:
with open('/proc/self/status') as f:
status = f.read()
m = re.search(r'(?m)^Cpus_allowed:\s*(.*)$', status)
if m:
res = bin(int(m.group(1).replace(',', ''), 16)).count('1')
if res > 0:
proc_cpus = res
except IOError:
pass
_log.debug('cgroup_cpus %d, proc_cpus %d, multiprocessing cpus %d',
cgroup_cpus, proc_cpus, multiprocessing.cpu_count())
return min(cgroup_cpus, proc_cpus, multiprocessing.cpu_count())
def execute(action, **kw):
succeeded = False
try:
_log.debug('Running command: %s', action)
subprocess.check_call(action, shell=True, **kw)
succeeded = True
finally:
_log.debug('Returned from running command: succeeded=%s, command=%s', succeeded, action)
def chk(cond, msg='condition failed'):
if not cond:
raise RuntimeError(f'Error: {msg}')
# * Parsing args
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--input-json', required=True, help='inputs passed as json')
#parser.add_argument('--replica-id-str', required=True, help='replica id string')
#parser.add_argument('--out-normed-collated', required=True, help='output file for normed and collated results')
return parser.parse_args()
# * orig_main
def orig_main(args):
cmsdir = args.cmsdir # "/data/ilya-work/proj/cms2-work/cms/cms/cms/"
writedir = args.writedir # "/data/mytmp/run5/"
#"/idi/sabeti-scratch/jvitti/remodel/run2/"
simRecomFile = args.simRecomFile # "/idi/sabeti-scratch/jvitti/params/test_recom.recom"
pops = args.pops or [1,2,3,4]
#basedir = writedir + model + "_" + regime + "_sel" + str(selpop) + "/"
basedir = writedir + model + "_" + regime + "/"
tped_dir = basedir + "tpeds/"
thispop = selpop
replicate_idstring = "rep" + str(irep)
replicate_idstring2 = replicate_idstring + "_pop" + str(thispop)
tped_filename = tped_dir + replicate_idstring + "_0_" + str(selpop) + ".tped"
if not os.path.isfile(tped_filename):
tped_filename += ".gz"
if not os.path.isfile(tped_filename):
print(('missing: ', tped_filename))
sys.exit(0)
ihh12_commandstring = "/ilya/miniconda3/envs/selscan-env/bin/selscan"
#"python " + cmsdir + "scans.py selscan_ihh12"
ihh12_unnormedfileprefix = basedir + "ihh12/" + replicate_idstring2
#ihh12_argstring = tped_filename + " " + ihh12_unnormedfileprefix + " --threads 7 "
ihh12_unnormedfilename = ihh12_unnormedfileprefix + ".ihh12.out"
ihh12_argstring = " --ihh12 --tped " + tped_filename + " --out " + ihh12_unnormedfileprefix + " --threads 8"
ihh12_fullcmd = ihh12_commandstring + " " + ihh12_argstring
print(ihh12_fullcmd)
if not os.path.isfile(ihh12_unnormedfilename) and not os.path.isfile(ihh12_unnormedfilename + ".gz"):
execute(ihh12_fullcmd)
ihs_commandstring = "/ilya/miniconda3/envs/selscan-env/bin/selscan"
#ihs_commandstring = "python " + cmsdir + "scans.py selscan_ihs"
ihs_outfileprefix = basedir + "ihs/" + replicate_idstring2
ihs_unnormedfile = ihs_outfileprefix + ".ihs.out"
#ihs_argstring = tped_filename + " " + ihs_outfileprefix + " --threads 7 "
ihs_argstring = " --ihs --ihs-detail --tped " + tped_filename + " --out " + ihs_outfileprefix + " --threads 8"
ihs_fullcmd = ihs_commandstring + " " + ihs_argstring
#ihs_normedfile = ihs_unnormedfile + ".norm"
print(ihs_fullcmd)
if not os.path.isfile(ihs_unnormedfile) and not os.path.isfile(ihs_unnormedfile + ".gz"):
execute(ihs_fullcmd)
delihh_commandstring = "python " + cmsdir + "composite.py delihh_from_ihs"
delihh_unnormedfile = basedir + "delihh/" + replicate_idstring2
delihh_argstring = ihs_unnormedfile + " "+ delihh_unnormedfile
delihh_fullcmd = delihh_commandstring + " " + delihh_argstring
delihh_normedfile = delihh_unnormedfile + ".norm"
print(delihh_fullcmd)
if not os.path.isfile(delihh_unnormedfile) and not os.path.isfile(delihh_unnormedfile + ".gz"):
execute(delihh_fullcmd)
nsl_commandstring = "/ilya/miniconda3/envs/selscan-env/bin/selscan"
#nsl_commandstring = "python " + cmsdir + "scans.py selscan_nsl"
nsl_unnormedfileprefix = basedir + "nsl/" + replicate_idstring2
#nsl_argstring = tped_filename + " " + nsl_unnormedfileprefix
nsl_argstring = " --nsl --tped " + tped_filename + " --out " + nsl_unnormedfileprefix + " --threads 8"
nsl_fullcmd = nsl_commandstring + " " + nsl_argstring
nsl_unnormedfilename = nsl_unnormedfileprefix + ".nsl.out"
print(nsl_fullcmd)
if not os.path.isfile(nsl_unnormedfilename) and not os.path.isfile(nsl_unnormedfilename + ".gz"):
execute(nsl_fullcmd)
tpeddir = tped_dir
altpops = pops[:]
altpops.remove(int(thispop))
for altpop in altpops:
#xpehh_commandstring = "python " + cmsdir + "scans.py selscan_xpehh --threads 7"
xpehh_commandstring = "/ilya/miniconda3/envs/selscan-env/bin/selscan"
#tped2 = tpeddir + "rep" + str(irep) + "_" + str(altpop) + ".tped"
#tped_filename2 = get_tped_filename(selpop, irep, ancestralpop, altpop, model, tped_dir)
tped_filename2 = tped_dir + replicate_idstring + "_0_" + str(altpop) + ".tped"
if not os.path.isfile(tped_filename2):
tped_filename2 += ".gz"
xpehh_outfileprefix = basedir + "xpehh/" + replicate_idstring2 + "_vs" + str(altpop)
xpehh_unnormedfile = basedir + "xpehh/" + replicate_idstring2 + "_vs" + str(altpop) + ".xpehh.out"
#xpehh_argumentstring = tped_filename + " " + xpehh_outfileprefix + " " + tped_filename2 + " --threads 8"
xpehh_argumentstring = " --xpehh --tped " + tped_filename + " --out " + xpehh_outfileprefix + " --threads 8 --tped-ref " + tped_filename2
xpehh_fullcmd = xpehh_commandstring + " " + xpehh_argumentstring
print(xpehh_fullcmd)
if not os.path.isfile(xpehh_unnormedfile) and not os.path.isfile(xpehh_unnormedfile + ".gz"):
execute(xpehh_fullcmd)
#fstdeldaf_commandstring = "python " + cmsdir + "composite.py freqscores"
#fstdeldaf_outfilename = basedir + "freqs/" + replicate_idstring2 + "_vs" + str(altpop)
#fstdeldaf_argumentstring = tped_filename + " " + tped_filename2 + " " + simRecomFile + " " + fstdeldaf_outfilename
#fstdeldaf_fullcmd = fstdeldaf_commandstring + " " + fstdeldaf_argumentstring
#print(fstdeldaf_fullcmd)
#if not os.path.isfile(fstdeldaf_outfilename):
# execute(fstdeldaf_fullcmd)
def normalize_and_collate_scores_orig(inps, inps_idx):
def descr_df(df, msg):
"""Describe a DataFrame"""
return
print('===BEG=======================\n', msg)
print(df.describe(), '\n')
df.info(verbose=True, null_counts=True)
print('===END=======================\n', msg)
def make_local(inp):
"""Creates a symlink to the input file *inp* in the current directory,
and returns the symlink. Necessary because the program 'norm' (part of selscan)
writes the normalized output file to the same directory as its input file,
and the directory containing the original imput file may not be writable,
while the current directory (execution directory) is guaranteed to be writable."""
if isinstance(inps[inp], list):
new_inps = []
for f in inps[inp]:
local_fname = os.path.basename(f)
if os.path.exists(local_fname):
raise RuntimeError(f'make_local: error localizing {f} to {local_fname} -- already exists')
os.symlink(f, local_fname)
new_inps.append(local_fname)
inps[inp] = new_inps
else:
local_fname = os.path.basename(inps[inp])
if os.path.exists(local_fname):
raise RuntimeError(f'make_local: error localizing {f} to {local_fname} -- already exists')
os.symlink(inps[inp], local_fname)
inps[inp] = local_fname
return inps[inp]
for component in ('ihs_out', 'delihh_out', 'nsl_out', 'ihh12_out', 'xpehh_out', 'derFreq_out', 'iSAFE_out'):
make_local(component)
def chk_idx(pd, name):
chk(pd.index.is_unique, f'Bad {name} index: has non-unique values')
chk(pd.index.is_monotonic_increasing, f'Bad {name} index: not monotonically increasing')
descr_df(pd, name)
collated = None
derFreq = pd.read_table(inps["derFreq_out"], low_memory=False, index_col='pos')
chk_idx(derFreq, 'derFreq')
collated = derFreq if collated is None else collated.join(derFreq, how='outer')
execute(f'norm --ihs --bins {inps["component_computation_params"]["n_bins_ihs"]} --load-bins {inps["norm_bins_ihs"]} '
f'--files {inps["ihs_out"]} '
f'--log {inps["ihs_out"]}.{inps["component_computation_params"]["n_bins_ihs"]}bins.norm.log ')
ihs_normed = pd.read_table(f'{inps["ihs_out"]}.{inps["component_computation_params"]["n_bins_ihs"]}bins.norm',
names='id pos p1 ihh1 ihh2 ihs ihsnormed ihs_outside_cutoff'.split(),
index_col='pos',
low_memory=False).add_prefix('ihs_')
chk_idx(ihs_normed, 'ihs_normed')
collated = collated.join(ihs_normed, how='outer')
execute(f'norm --ihs --bins {inps["component_computation_params"]["n_bins_delihh"]} --load-bins {inps["norm_bins_delihh"]} '
f'--files {inps["delihh_out"]} '
f'--log {inps["delihh_out"]}.{inps["component_computation_params"]["n_bins_delihh"]}bins.norm.log ')
delihh_normed = pd.read_table(f'{inps["delihh_out"]}.{inps["component_computation_params"]["n_bins_delihh"]}bins.norm',
names='id pos p1 ihh1 ihh2 delihh delihhnormed delihh_outside_cutoff'.split(),
index_col='pos',
low_memory=False).add_prefix('delihh_')
chk_idx(delihh_normed, 'delihh_normed')
collated = collated.join(delihh_normed, how='outer')
execute(f'norm --nsl --bins {inps["component_computation_params"]["n_bins_nsl"]} --load-bins {inps["norm_bins_nsl"]} '
f'--files {inps["nsl_out"]} '
f'--log {inps["nsl_out"]}.{inps["component_computation_params"]["n_bins_nsl"]}bins.norm.log ')
nsl_normed = pd.read_table(f'{inps["nsl_out"]}.{inps["component_computation_params"]["n_bins_nsl"]}bins.norm',
names='id pos p1 ihh1_nsl ihh2_nsl nsl nslnormed nsl_outside_cutoff'.split(),
index_col='pos',
low_memory=False).add_prefix('nsl_')
chk_idx(nsl_normed, 'nsl_normed')
collated = collated.join(nsl_normed, how='outer')
chk_idx(collated, 'collated after nsl_normed')
execute(f'norm --ihh12 --bins 1 --load-bins {inps["norm_bins_ihh12"]} '
f'--files {inps["ihh12_out"]} '
f'--log {inps["ihh12_out"]}.norm.log ')
ihh12_normed = pd.read_table(f'{inps["ihh12_out"]}.norm', index_col='pos',
low_memory=False).add_prefix('ihh12_')
chk_idx(ihh12_normed, 'ihh12_normed')
collated = collated.join(ihh12_normed, how='outer')
chk_idx(collated, 'collated after ihh12_normed')
for other_pop_idx, (xpehh_out, norm_bins_xpehh) in enumerate(zip(inps["xpehh_out"], inps["norm_bins_xpehh"])):
execute(f'norm --xpehh --bins 1 --load-bins {norm_bins_xpehh} --files {xpehh_out} '
f'--log {xpehh_out}.norm.log ')
xpehh_normed = pd.read_table(xpehh_out+".norm", index_col='pos',
low_memory=False).add_suffix(f'_{other_pop_idx}').add_prefix('xpop_')
chk_idx(xpehh_normed, f'xpehh_normed_{other_pop_idx}')
collated = collated.join(xpehh_normed, how='outer')
chk_idx(collated, f'collated after xpehh_normed_{other_pop_idx}')
for other_pop_idx, fst_and_delDAF_out in enumerate(inps["fst_and_delDAF_out"]):
execute(f'norm --xpehh --bins 1 --load-bins {norm_bins_xpehh} --files {xpehh_out} '
f'--log {xpehh_out}.norm.log ')
fst_and_delDAF_tsv = \
pd.read_table(fst_and_delDAF_out, index_col='physPos',
low_memory=False).rename_axis('pos').add_suffix(f'_{other_pop_idx}')\
.add_prefix('fst_and_delDAF_')
chk_idx(fst_and_delDAF_tsv, f'fst_and_delDAF_tsv_{other_pop_idx}')
collated = collated.join(fst_and_delDAF_tsv, how='outer')
chk_idx(collated, f'collated after fst_and_delDAF_tsv_{other_pop_idx}')
collated['max_xpehh'] = collated.filter(like='normxpehh').max(axis='columns')
collated['mean_fst'] = collated.filter(like='Fst').mean(axis='columns')
collated['mean_delDAF'] = collated.filter(like='delDAF').mean(axis='columns')
isafe = pd.read_table(f'{inps["iSAFE_out"]}',
index_col='POS',
low_memory=False).rename_axis('pos').add_prefix('iSAFE_')
chk_idx(isafe, 'isafe')
collated = collated.join(isafe, how='outer')
replica_id_str = os.path.basename(inps['ihs_out'])
if replica_id_str.endswith('.ihs.out'):
replica_id_str = replica_id_str[:-len('.ihs.out')]
collated['hapset_id'] = replica_id_str
descr_df(collated, 'collated final')
collated.reset_index().to_csv(f'{inps_idx:06}.{replica_id_str}.normed_and_collated.tsv', sep='\t', na_rep='nan', index=False)
# end: def normalize_and_collate_scores_orig(inps)
def normalize_and_collate_scores(args):
inps_orig = _json_loadf(args.input_json)
for i in range(len(inps_orig['replica_info'])):
inps = copy.deepcopy(inps_orig)
inps_i = dict(replica_info=inps['replica_info'][i],
sel_pop=inps['sel_pop'],
ihs_out=inps['ihs_out'][i],
nsl_out=inps['nsl_out'][i],
ihh12_out=inps['ihh12_out'][i],
delihh_out=inps['delihh_out'][i],
derFreq_out=inps['derFreq_out'][i],
iSAFE_out=inps['iSAFE_out'][i],
xpehh_out=[v[i] for v in inps['xpehh_out']],
fst_and_delDAF_out=[v[i] for v in inps['fst_and_delDAF_out']],
norm_bins_ihs=inps['norm_bins_ihs'],
norm_bins_nsl=inps['norm_bins_nsl'],
norm_bins_ihh12=inps['norm_bins_ihh12'],
norm_bins_delihh=inps['norm_bins_delihh'],
norm_bins_xpehh=inps['norm_bins_xpehh'],
component_computation_params=inps['component_computation_params'])
_log.info(f'calling normalize_and_collate_scores_orig {i}: {inps_i}')
normalize_and_collate_scores_orig(inps_i, i)
if __name__=='__main__':
normalize_and_collate_scores(parse_args())