Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _read_bigwig_as_wig(filepath, chrom, start=None, end=None, cachedir=None):
# https://sebastienvigneau.wordpress.com/2014/01/10/bigwig-to-bedgraph-to-wig/
# http://redmine.soe.ucsc.edu/forum/index.php?t=msg&goto=5492&S=2925a24be1c20bb064fc09bd054f862d
cmd = ['bigWigToWig',
'-chrom={}'.format(chrom)]
if start is not None:
cmd += ['-start={}'.format(start)]
if end is not None:
cmd += ['-end={}'.format(end)]
if cachedir is not None:
cmd += ['-udcDir={}'.format(cachedir)]
with tempfile.NamedTemporaryFile('w+t') as fh:
cmd += [filepath, fh.name]
run(cmd, raises=True)
fh.flush()
trackline = fh.readline().split()
if trackline[0] == '#bedGraph':
info = {'type': 'bedGraph'}
out = pd.read_csv(fh, sep='\t', names=['chrom', 'start', 'end', 'value'])
else:
tracktype = trackline[0]
info = dict([kv.split('=') for kv in trackline[1:]])
info['type'] = tracktype
for key in ['start', 'step', 'span']:
if key in info: info[key] = int(info[key])
if tracktype == 'fixedStep':
out = pd.read_csv(fh, sep='\t', names=['value'])
else:
out = pd.read_csv(fh, sep='\t', names=['start', 'value'])
aggfunc : str, optional
Aggregation method (summary statistic). One of:
'mean' - average value in region (default)
'std' - standard deviation in region
'min' - minimum value in region
'max' - maximum value in region
'coverage' - % of region that is covered
"""
cmd = ['bigWigSummary',
filepath, chrom, str(start+1), str(end), str(nbins)]
if aggfunc is not None:
cmd += ['-type={}'.format(aggfunc)]
if cachedir is not None:
cmd += ['-udcDir={}'.format(cachedir)]
out = run(cmd, raises=True)
return pd.read_csv(io.StringIO(out), sep='\t', na_values='n/a', header=None).iloc[0].values
elif isinstance(kwargs[arg], pd.DataFrame):
tmp_file = tsv(kwargs[arg])
pandas_inputs[arg] = tmp_file
kwargs[arg] = tmp_file.name
cmd = ['bedtools', name]
for k, v in kwargs.items():
if isinstance(v, bool):
if not v: continue
cmd.append('-{}'.format(k))
else:
cmd.append('-{}'.format(k))
cmd.append(str(v))
try:
out = run(cmd, **run_kws)
finally:
for tmp_file in pandas_inputs.values():
tmp_file.close()
if not len(out):
return pd.DataFrame(columns=columns)
return to_dataframe(out, columns=columns)
with tempfile.NamedTemporaryFile(suffix='.bg') as f, \
tempfile.NamedTemporaryFile('wt', suffix='.chrom.sizes') as cs:
chromsizes.to_csv(cs, sep='\t', header=False)
cs.flush()
bg.to_csv(
f.name,
sep='\t',
columns=columns,
index=False,
header=False,
na_rep='nan')
run(['bedGraphToBigWig', f.name, cs.name, outpath],
print_cmd=True)