Question: Counting Features In A Bed File
1
gravatar for k.nirmalraman
6.6 years ago by
k.nirmalraman990
Germany
k.nirmalraman990 wrote:

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 • 3.0k views
ADD COMMENTlink modified 6.6 years ago by Dave Richardson370 • written 6.6 years ago by k.nirmalraman990
5
gravatar for Dave Richardson
6.6 years ago by
Cambridge, UK
Dave Richardson370 wrote:

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
ADD COMMENTlink written 6.6 years ago by Dave Richardson370

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

ADD REPLYlink written 6.6 years ago by k.nirmalraman990
2
gravatar for zx8754
6.6 years ago by
zx87547.5k
London
zx87547.5k wrote:
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

ADD COMMENTlink written 6.6 years ago by zx87547.5k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1977 users visited in the last hour