This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to add a new column with consecutive integer number in a bed file?

How can I tranform the table 1 to table 2 by adding consecutive integer number in column 4? Thanks a lot.

Table 1:

1 1 16007
1 16008 24571
1 24572 27981
1 27982 30429
1 30430 32153
1 32154 32774
1 32775 37752
1 37753 38369
1 38370 38791
1 38792 39255

By adding 1, 2, 3, ….n on the forth column

Table 2:

1 1 16007 1
1 16008 24571 2
1 24572 27981 3
1 27982 30429 4
1 30430 32153 5
1 32154 32774 6
1 32775 37752 7
1 37753 38369 8
1 38370 38791 9
1 38792 39255 10
...  ...        ...       ...
r gene

4 answers

awk '{print $0,NR}' file > file2

Bed file is tab delimited, as @Alex wrote, in that case

awk '{print $0"\t"NR}' file > file2

You can still use the comma, but you might specify the output field separator parameter just to make sure:

$ awk 'BEGIN { OFS = "\t"; } { print $0,NR; }' in.bed > out.bed

Commas are easier to read and type; they don't require pairing quotes around tab characters. Either way works.

From the command line: seq 1 n | paste bedfile - > bedfileWithInteger

Thanks, while it prints "seq: invalid floating point argument: n"

n should be the number of rows of your input file.

Within R (since your question has the R tag):

> new_df <- cbind(old_df, 1:nrow(old_df))

With awk on the command line:

$ awk '{ print $0"\t"NR }' in.bed > out.bed

pure shell:

i=1; while read c1 c2 c3; do printf "$c1 $c2 $c3 $i\n"; ((i++)); done<file

Log in to answer this question.