This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to rename first column of bedgraph file based on another reference table?

I have a bedgraph file that looks like this:

chr1    0   35  0
chr1    35  56  0.18636
chr3    56  73  0.37273
chr3    73  123 0.55909
chr4    123 142 0.37273
chr4    142 144 0.55909
chr5    144 161 0.37273

And I have another reference table in a tab delimited format that has alternative values for the first column:

chr1    scaffold_A
chr2    scaffold_B
chr3    scaffold_C
chr4    scaffold_D
chr5    scaffold_E

How can I take the values in the reference table to replace values in the first column of the bedgraph file so the final output is:

scaffold_A  0   35  0
scaffold_A  35  56  0.18636
scaffold_C  56  73  0.37273
scaffold_C  73  123 0.55909
scaffold_D  123 142 0.37273
scaffold_D  142 144 0.55909
scaffold_E  144 161 0.37273
chip-seq bedgraph

1 answer

not tested, but it could be something like:

sed -f <(awk '{printf("s/^%s\t/%s\t/\n",$1,$2);}' file2.txt) file1.txt

or

join -t $'\t' -o '2.2,1.2,1.3,1.4' -1 1 -2 1 <(sort -t $'\t' -k1,1 file1.txt) <(sort -t $'\t' -k1,1 file2.txt)

Log in to answer this question.