This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Counting Features In A Bed File

I have a file in the following BED format

Chr1 1022071 1022105  +      
Chr1 1022071 1022105  +
Chr1 1022072 1022106  -  
Chr1 1022072 1022106  - 
Chr1 1022072 1022106  -
Chr1 1022072 1022106  -

I am trying get the counts of each feature represented in this file.

mergeBed -i R5_chr.bed -n -s -d 0 > Output/R5_chr_counts.bed

I am interested in the counts of the features and I do not want to merge features by any number of base pairs. Then the output should be as follows

Chr1 1022071 1022105 2 +
Chr1 1022072 1022106 4 +

Any suggestions on how to achieve this using bedtools or in bash or awk? Thanks in advance!

bedtools bash awk

2 answers

Based on the example you've given this should work:

sort R5_chr.bed | uniq -c | awk '{ print $2,$3,$4,$1,$5}' > Output/R5_chr_counts.bed

Giving this output:

Chr1 1022071 1022105 2 +
Chr1 1022072 1022106 4 -

If the BED file is already sorted you can omit the initial sort command:

uniq -c R5_chr.bed | awk '{ print $2,$3,$4,$1,$5}' > Output/R5_chr_counts.bed

Thank you very much!! This worked perfectly to my need :)

sort <file> | uniq --count

Find duplicate lines in a file and count how many time each line was duplicated: http://stackoverflow.com/questions/6712437/find-duplicate-lines-in-a-file-and-count-how-many-time-each-line-was-duplicated

Log in to answer this question.