pybedtools.bedtool.BedTool.flank

BedTool.flank(*args, **kwargs)[source]

Wraps bedtools flank.

Example usage:

>>> a = pybedtools.example_bedtool('a.bed')
>>> print(a.flank(genome='hg19', b=100)) 
chr1    0       1       feature1        0       +
chr1    100     200     feature1        0       +
chr1    0       100     feature2        0       +
chr1    200     300     feature2        0       +
chr1    50      150     feature3        0       -
chr1    500     600     feature3        0       -
chr1    800     900     feature4        0       +
chr1    950     1050    feature4        0       +

For convenience, the file or stream this BedTool points to is implicitly passed as the -i argument to flankBed

There are two alternatives for supplying a genome. Use g="genome.filename" if you have a genome’s chrom sizes saved as a file. This is the what BEDTools expects when using it from the command line. Alternatively, use the genome="assembly.name" (for example, genome="hg19") to use chrom sizes for that assembly without having to manage a separate file. The genome argument triggers a call pybedtools.chromsizes, so see that method for more details.

Original BEDTools help::

Tool:    bedtools flank (aka flankBed)
Version: v2.31.1
Summary: Creates flanking interval(s) for each BED/GFF/VCF feature.

Usage:   bedtools flank [OPTIONS] -i <bed/gff/vcf> -g <genome> [-b <int> or (-l and -r)]

Options: 
        -b      Create flanking interval(s) using -b base pairs in each direction.
                - (Integer) or (Float, e.g. 0.1) if used with -pct.

        -l      The number of base pairs that a flank should start from
                orig. start coordinate.
                - (Integer) or (Float, e.g. 0.1) if used with -pct.

        -r      The number of base pairs that a flank should end from
                orig. end coordinate.
                - (Integer) or (Float, e.g. 0.1) if used with -pct.

        -s      Define -l and -r based on strand.
                E.g. if used, -l 500 for a negative-stranded feature, 
                it will start the flank 500 bp downstream.  Default = false.

        -pct    Define -l and -r as a fraction of the feature's length.
                E.g. if used on a 1000bp feature, -l 0.50, 
                will add 500 bp "upstream".  Default = false.

        -header Print the header from the input file prior to results.

Notes: 
        (1)  Starts will be set to 0 if options would force it below 0.
        (2)  Ends will be set to the chromosome length if requested flank would
        force it above the max chrom length.
        (3)  In contrast to slop, which **extends** intervals, bedtools flank
        creates new intervals from the regions just up- and down-stream
        of your existing intervals.
        (4)  The genome file should tab delimited and structured as follows:

        <chromName><TAB><chromSize>

        For example, Human (hg19):
        chr1    249250621
        chr2    243199373
        ...
        chr18**gl000207**random 4262

Tip 1. Use samtools faidx to create a genome file from a FASTA: 
        One can the samtools faidx command to index a FASTA file.
        The resulting .fai index is suitable as a genome file, 
        as bedtools will only look at the first two, relevant columns
        of the .fai file.

        For example:
        samtools faidx GRCh38.fa
        bedtools flank -i my.bed -g GRCh38.fa.fai

Tip 2. Use UCSC Table Browser to create a genome file: 
        One can use the UCSC Genome Browser's MySQL database to extract
        chromosome sizes. For example, H. sapiens:

        mysql --user=genome --host=genome-mysql.cse.ucsc.edu -A -e \
        "select chrom, size from hg19.chromInfo"  > hg19.genome