1
$\begingroup$

I am trying to split my .bed/.fa file genomic segments into arbitrary smaller overlapping intervals:

Consider a typical line/row in my *.bed file as follows:

chrom. $\ \ \ \ \ $ start $\ \ \ \ \ \ \ \ \ \ \ \ $ end

chr7 $\ \ \ $ 127471196 $\ \ \ $ 127472363$\ \ \ $ ...

The start and end genomic positions include 1167 base pairs for this line. I need to extend this line, for example, into multiple lines where each line covers exactly 500bp like below:

chr7 $\ \ \ $ 127471196 $\ \ \ $ 127471696 $\ \ \ $ ...

chr7 $\ \ \ $ 127471863 $\ \ \ $ 127472363 $\ \ \ $ ...

![enter image description here

While each line covers 500pb, between the two new lines they both include the same "range" of genomic positions as the original line including some overlap which is fine (and desired)

Is there an existing tool (perhaps part of bedtools?) that can do something like this?

I know there is the bedops tool that does ALMOST this using the --chop option, where it takes the original segment x1:x2 and chops the original segment into specified fixed bp length intervals, however, if there is any left over towards the end is creates a new non-standard length segment (i.e. x1:x2=10bp and chop length=3, it will give you 4 segments that are length 3, 3, 3 and 1 - what I am looking for is something that adjusts for the last remainder and outputs segments of length 3,3,3 and 3 where the last length 3 segment is right adjusted, while the first three segments are left adjusted or better yet it gives the user the choice of precise redefinition of segment coordinates y1,y2,y3 and y4). Thank you.

$\endgroup$

1 Answer 1

2
$\begingroup$

You can run --chop with -x to excise the last piece, adding the --stagger to size the chopped pieces as needed, e.g.:

$ echo -e 'chr1\t0\t10' | bedops --chop 3 --stagger 2 -x -
chr1    0       3
chr1    2       5
chr1    4       7
chr1    6       9

Adjust parameters to taste.

https://bedops.readthedocs.io/en/latest/content/reference/set-operations/bedops.html#chop-w-chop

https://bedops.readthedocs.io/en/latest/content/reference/set-operations/bedops.html#stagger-stagger

https://bedops.readthedocs.io/en/latest/content/reference/set-operations/bedops.html#exclude-x

$\endgroup$
2
  • $\begingroup$ Thank you @alex - the specific issue with bedops --stagger option is that I have not been able to feed it a negative number, that's what would allow for "overlaps" which is critical! $\endgroup$
    – Zebra Fish
    Sep 19, 2022 at 17:33
  • 2
    $\begingroup$ As shown in the example above, --stagger can give you overlapping elements. However, you also can pipe results to bedops --everything --range N to extend edges by N bases, e.g. ... | bedops --chop X -x - | bedops --everything --range Y - > answer.bed. You'd probably need to use coordinates from fetchChromSizes or similar to trim edge-of-chromosome windows, but that is easy to do via piping to awk or the like. $\endgroup$ Sep 19, 2022 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.