I have a huge file (20 GB
) which has a range of genomic locations, and for each location there is an identifier(4th column
), which is sometimes the same.
file1.txt
chr1 10 20 ABC
chr1 13 20 ABC
chr1 14 21 ABC
chr1 22 27 ABC
chr1 29 37 ABC
chr2 15 21 JJJ
chr2 21 31 JJJ
chr2 23 27 JJJ
chr2 35 56 JJJ
chr2 25 26 MMM
chr3 32 42 MMM
chr3 45 76 MMM
chr3 88 101 MMM
chr3 101 105 MMM
I am trying to merge the ranges of column 2
and 3
, if they are in the same chromosome and having the same identifier ( having the 1st
and 4th
columns the same).
I have tried to use bedtools
, as shown in this post, which looks very similar but not the same with mine, as they are looking for different outputs ( range vs merge )
So the answers in that post was with using groupby
in bedtools2,source code from github
.
I have applied the same as the following:
./groupBy -i ../file1.txt -g 1,4 -c 2,3 -o min,max | awk -v OFS='\t' '{print $1, $3, $4, $2}'
chr1 10 37 ABC
chr2 15 56 JJJ
chr2 25 26 MMM
chr3 32 105 MMM
But it only connects the ranges of identifiers.. It groups by, and does not merge.
And, if I use bedtools v2.26 merge
function :
>sort -k1,1 -k2,2n file1.txt > file2.txt
>cat file2.txt
chr1 10 20 ABC
chr1 13 20 ABC
chr1 14 21 ABC
chr1 22 27 ABC
chr1 29 37 ABC
chr2 15 21 JJJ
chr2 21 31 JJJ
chr2 23 27 JJJ
chr2 25 26 MMM
chr2 35 56 JJJ
chr3 32 42 MMM
chr3 45 76 MMM
chr3 88 101 MMM
chr3 101 105 MMM
bedtools merge -i file2.txt
chr1 10 21
chr1 22 27
chr1 29 37
chr2 15 31
chr2 35 56
chr3 32 42
chr3 45 76
chr3 88 105
Which, merges without seeing the identifiers as it also gives me this output.
What I want is to merge
the ranges if they overlap
, if having the same 1st and 4th column
as:
expected_output.txt
chr1 10 21 ABC
chr1 22 27 ABC
chr1 29 37 ABC
chr2 15 31 JJJ
chr2 35 56 JJJ
chr2 25 26 MMM
chr3 32 42 MMM
chr3 45 76 MMM
chr3 88 105 MMM
I only have access to 15GB of RAM, so I can't store the entire file in memory.