Find for all features in {{file_1}}.bed the closest one in {{file_2}}.bed and write their distance in an extra column (input files must be sorted)

bedtools

  • A swiss-army knife of tools for genomic-analysis tasks.
    Used to intersect, group, convert and count data in BAM, BED, GFF/GTF, VCF format.
    More information: <https://bedtools.readthedocs.io>.
  • Trick copyright: tl;dr; <https://github.com/tldr-pages/tldr>

bedtools closest -a {{path/to/file_1}}.bed -b {{path/to/file_2}}.bed -d

click the source code to copy install bedtools on any operating system with command-not-found.com