By default, bedtools sorts by chr and start, ignoring the end-coordinate. Therefore, it might happen that you get something like this:
chr1 4 15
chr1 4 17
chr1 4 15
Uniq iterates from the top to the bottom of this file. Therefore, it recognizes duplicated reads only, if they appear right one below the other, which is not the case in the above example.
I recommend you not to use bedtools for sorting, as indicated on the bedtools::sort manual page. Unix sort is faster and more memory efficient. If you want to combine sorting of your bed with deduplication, you can use the following command:
sort -k1,1 -k2,2n -k3,3n -k6,6 -u input.bed > output.bed
This command takes chr, start, end and strand into account, which are the essential information to describe a unique fragment. The -u then acts on all the columns, that were provided in the command, generating a deduplicated file.
If you are on a Mac, you may want to get the GNU core utilities e.g. via homebrew, as GNU sort provides a nice --parallel option for multi-threading.
Have you tried to
diff
the results by capturing them to files to see where the differences are?I think you're onto something. I'm not familiar with diff but a quick read over the command I think
sortBed
is not sorting the end position as accurately?first file '<' is sortBed
second file '>' is sort -k1,1 -k2,2n
Looks like you are not sorting the third column in your unix sort. Is that always unique?
What answer does
sort-bed
return?