Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ones as `unmapped`. If `unmapped` is None, then discards the unmapped
features.
`liftover_args` is a string of additional args that is passed,
verbatim, to liftOver.
Needs `liftOver` from UCSC to be on the path and a `chainfile`
downloaded from UCSC.
"""
result = BedTool._tmp()
if unmapped is None:
unmapped = BedTool._tmp()
cmds = ['liftOver', liftover_args, self.fn, chainfile, result,
unmapped]
os.system(' '.join(cmds))
return BedTool(result)
>>> len(b)
2
'''
idxs = range(len(self))
if seed is not None:
random.seed(seed)
random.shuffle(idxs)
idxs = idxs[:n]
tmpfn = self._tmp()
tmp = open(tmpfn, 'w')
for i, f in enumerate(self):
if i in idxs:
tmp.write(str(f) + '\n')
tmp.close()
return BedTool(tmpfn)
"""
Split each feature using a user-defined function.
Calls the provided function `func` with each interval. In contrast to
`each` (which does something similar), this method expects `func` to
return an *iterable* of Interval objects.
args and kwargs are passed directly to `func`.
Returns a new BedTool.
"""
def generator():
for orig_interval in self:
for interval in func(orig_interval, *args, **kwargs):
yield interval
return BedTool(generator())
for f in self:
TMP.write(str(f))
for other in other_beds:
for f in other:
TMP.write(str(f))
# otherwise, truncate
else:
for f in self:
TMP.write('%s\t%i\t%i\n' % (f.chrom, f.start, f.end))
for other in other_beds:
for f in other:
TMP.write('%s\t%i\t%i\n' % (f.chrom, f.start, f.end))
TMP.close()
c = BedTool(tmp)
if postmerge:
d = c.sort(stream=True).merge(**kwargs)
# Explicitly delete -- needed when using multiprocessing
os.unlink(tmp)
return d
else:
return c
cmds = [os.path.join(settings._samtools_path, 'samtools'),
'view',
'-S',
'-b',
'-t', kwargs['g'],
'-']
tmp = self._tmp()
p = subprocess.Popen(cmds,
stdout=open(tmp, 'w'),
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
bufsize=1)
for line in self:
p.stdin.write(str(line) + '\n')
stdout, stderr = p.communicate()
new_bedtool = BedTool(tmp)
new_bedtool._isbam = True
return new_bedtool
True.
*args and **kwargs are passed directly to *func*.
Returns a streaming BedTool; if you want the filename then use the
.saveas() method.
>>> a = pybedtools.example_bedtool('a.bed')
>>> subset = a.filter(lambda b: b.chrom == 'chr1' and b.start < 150)
>>> len(a), len(subset)
(4, 2)
so it has extracted 2 records from the original 4.
"""
return BedTool((f for f in self if func(f, *args, **kwargs)))
Example usage:
>>> # make a copy so we don't mess up the example file
>>> a = pybedtools.example_bedtool('a.bed').saveas()
>>> a_contents = str(a)
>>> b = a.moveto('other.bed')
>>> b.fn
'other.bed'
>>> b == a_contents
True
"""
if not isinstance(self.fn, basestring):
fn = self._collapse(self, fn=fn)
else:
shutil.move(self.fn, fn)
return BedTool(fn)
... postmerge=False) #doctest: +NORMALIZE_WHITESPACE
chr1 1 100 feature1 0 +
chr1 100 200 feature2 0 +
chr1 150 500 feature3 0 -
chr1 900 950 feature4 0 +
chr1 155 200 feature5 0 -
chr1 800 901 feature6 0 +
chr1 155 200 feature5 0 -
chr1 800 901 feature6 0 +
"""
assert len(others) > 0, 'You must specify at least one other bedfile!'
other_beds = []
for other in others:
if isinstance(other, basestring):
other = BedTool(other)
else:
assert isinstance(other, BedTool),\
'Either filename or another BedTool instance required'
other_beds.append(other)
tmp = self._tmp()
TMP = open(tmp, 'w')
# postmerge and force_trucate don't get passed on to merge
postmerge = kwargs.pop('postmerge', True)
force_truncate = kwargs.pop('force_truncate', False)
stream_merge = kwargs.get('stream', False)
if stream_merge and postmerge:
raise ValueError(
"The post-merge step in the `cat()` method "
"perfoms a sort, which uses stream=True. Using "
"""
if not self._tabixed():
raise ValueError(
"This BedTool has not been indexed for tabix "
"-- please use the .tabix() method")
# NOTE: tabix expects 1-based coords, but BEDTools works with
# zero-based.
interval = helpers.string_to_interval(interval_or_string)
coords = '%s:%s-%s' % (
interval.chrom,
interval.start + 1, # convert to 1-based coords
interval.stop)
cmds = ['tabix', self.fn, coords]
p = subprocess.Popen(cmds, stdout=subprocess.PIPE)
return BedTool(p.stdout)
>>> b = a.each(truncate_feature, limit=100)
>>> print b #doctest: +NORMALIZE_WHITESPACE
chr1 1 100 feature1 99 +
chr1 100 200 feature2 100 +
chr1 150 250 feature3.short 350 -
chr1 900 950 feature4 50 +
"""
def _generator():
for f in self:
result = func(f, *args, **kwargs)
if result:
yield result
return BedTool(_generator())