pybedtools.bedtool.BedTool.each

BedTool.each(func, *args, **kwargs)[source]

Modify each feature with a user-defined function.

Applies user-defined function func to each feature. func must accept an Interval as its first argument; args and **kwargs will be passed to *func.

func must return an Interval object OR a value that evaluates to False, in which case the original feature will be removed from the output. This way, an additional “filter” call is not necessary.

>>> def truncate_feature(feature, limit=0):
...     feature.score = str(len(feature))
...     if len(feature) > limit:
...         feature.stop = feature.start + limit
...         feature.name = feature.name + '.short'
...     return feature
>>> a = pybedtools.example_bedtool('a.bed')
>>> b = a.each(truncate_feature, limit=100)
>>> print(b) 
chr1    1       100     feature1        99      +
chr1    100     200     feature2        100     +
chr1    150     250     feature3.short  350     -
chr1    900     950     feature4        50      +