This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Difference between sortBed in bedtools and sort in unix

Hi all,

Do you know the difference between sortBed​ and sort ?theses two give different results.

cat file.sort.bed |uniq|wc -l

cat file.bed |sort|uniq|wc -l

Thanks

sequence chip-seq next-gen

There are many differences. If you specify exact commands you used, it would be easier to figure out why they are different for you.

for the first line I sort my bed file by sortbed from bedtools and then I used uniq and then count the lines

1) sortBed [OPTIONS] -i <bed gff="" vcf=""> 2) uniq 3) wc -l

while in second command I used directly bed file then used sort in unix script,uniq and finally count the lines.

You probably just did normal sort rather than sort -k1,1 k2,2n, which more similar to bedtools.

A bed file is in general a binary file and sort or cat on that file directly will probably not give you anything meaningful.

Whoa! Binary ped. I guess there are at least two beds in genomics then.

Unix's sort will not handle any headers of the bed file. It won't be able to handle properly any compressed form of bed, like bedgraph or bgzipp-ed bed. It would require to use the correct -n and V options to properly sort fields as numeric or characters.

Unix's sort will not handle any headers of the bed file. It won't be able to handle properly any compressed form of bed, like bedgraph or bgzipp-ed bed. It would require to use the correct -n and V options to properly sort fields as numeric or characters.

3 answers

Just guessing... sortBed may not break ties once it sorts by chrom, start end. I.e. duplicate lines having the same coordinates stay unsorted and uniq count them more than once. Unix sort by default sorts by additional fields to break ties. For example, given this file:

a   1
a   2
a   1
b   1
b   2
b   1

Unix sort without breaking ties (what sortBed might do):

sort -k1,1 -s test.txt | uniq | wc -l
6

Now with default, breaking ties:

sort -k1,1 test.txt | uniq | wc -l
4

Use sort-bed to sort BED files on all three relevant fields.

It runs on arbitrary BED input — it handles input with headers, for instance, or input with more than six columns — and it runs faster than Unix sort.

Then you don't have to worry about these issues!

linux sorting allows alphanumeric sorting as well now. You have to use the option V. If you want your bed files to be sorted chromosome wise then by region, use sort -k1,1V -k2,2n in.bed > out.sorted.bed. Without that option, sort will not perform an alphanumeric sort.

Log in to answer this question.