How to add a new column with consecutive integer number in a bed file?
4
0
Entering edit mode
8.6 years ago
bright602 ▴ 50

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 • 3.6k views
ADD COMMENT
7
Entering edit mode
8.6 years ago
awk '{print $0,NR}' file > file2
ADD COMMENT
0
Entering edit mode

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

awk '{print $0"\t"NR}' file > file2
ADD REPLY
0
Entering edit mode

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.

ADD REPLY
2
Entering edit mode
8.6 years ago
lkmklsmn ▴ 980

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

ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
2
Entering edit mode
8.6 years ago

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
ADD COMMENT
1
Entering edit mode
8.6 years ago
5heikki 11k

pure shell:

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

Login before adding your answer.

Traffic: 1942 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6