how to change column header based on the second file column
1
0
Entering edit mode
3.6 years ago
Bioinfonext ▴ 460

Hi,

I have got two tab demilated files; count.txt and metadata.txt. I would like to renmae the count.txt header based on the metadata.txt 5th Column (SampleType). Could anyone please suggest how I can do it by using R or unix command?

Count .txt

                                       Root-T1    Root-T2     Root-T3     Root-T4
8e60d301122d7aa359eb6b0b00f37f62    10.99317665 9.83612554  10.0140148  12.93594085
16c306238059e7942361b356cb1fe8e0    8.536081053 9.830638671 9.823501073 11.76397582
bfad6370d28182cc6304844e9bec7fb6    5.39148115  4.246835906 2.154775982 7.668303753
97aecc1f35cc1f50db507ad71dd22367    1.683437866 11.0066574  2.743835676 7.427173277
85ecffe953eb7a94dd582cd4437a79d8    3.075677166 2.785500037 2.487627557 3.343912342
4e6308265ca4d8eaf48d14ddedf31033    1.661037976 1.671665813 2.645294092 3.017676992

Metadata.txt

                     Treatment  Batch   Compartment SampleType
Root-T1                 T1      FIRST     Root       Irrigated
Root-T2                 T1      FIRST     Root       Irrigated
Root-T3                 T1      FIRST     Root       Irrigated
Root-T4                 T1      FIRST     Root       Irrigated

Many thanks,

R BASH LINUX UNIX • 828 views
ADD COMMENT
3
Entering edit mode
3.6 years ago

Generate a sed pattern with awk . something like:

sed -f <(awk '/^[^ \t]/ {printf("/^[ \\t]/s/%s/%s/\n",$1,$5);}' Metadata.txt) Count.txt

please note that using sed may have side effects if your header have samples likes Root-T1 and Root-T11 (thet both match the pattern Root-T1 )

ADD COMMENT
0
Entering edit mode

thanks Pierre,

For 4th column, this command working perfectly, but for 5th column result file look like below;

Rainfed                     
    Rainfed                     
    Rainfed                     
    Rainfed                     
8e60d301122d7aa359eb6b0b00f37f62    10.99317665 9.83612554  10.0140148  12.93594085 14.35852601 12.98159128 12.00400185
16c306238059e7942361b356cb1fe8e0    8.536081053 9.830638671 9.823501073 11.76397582 11.30787403 13.18144472 12.17792827
bfad6370d28182cc6304844e9bec7fb6    5.39148115  4.246835906 2.154775982 7.668303753 5.638125709 0.928208203 2.15791904
97aecc1f35cc1f50db507ad71dd22367    1.683437866 11.0066574  2.743835676 7.427173277 3.294332112 0.106588502 2.053255349
ADD REPLY
0
Entering edit mode

If I open the result file based on 5th column with vi command it like this; I am not sure how it got ^M character.

Irrigated^M     Irrigated^M     Irrigated^M     Irrigated^M     Irrigated^M     Rainfed^M       Rainfed^M       Rainfed^M       Rainfed^M       Rainfed^M       Irrigated^M     Irrigated^M     Irrigated^M     Irrigated^M     Irrigated^M     Rainfed^M       Rainfed^M       Rainfed^M       Rainfed^M       Rainfed^M       Irrigated^M     Irrigated^M     Irrigated^M     Irrigated^M     Irrigated^M     Rainfed^M       Rainfed^M       Rainfed^M       Rainfed^M       Rainfed^M
8e60d301122d7aa359eb6b0b00f37f62        10.99317665     9.83612554      10.0140148      12.93594085     14.35852601     12.98159128     12.00400185     12.93086801     14.34132974     15.25886789     14.11885883     11.78132013     11.21429411     13.31574455     12.49416052     14.83416736     13.801829       12.51858404     14.10324552     13.57821028     13.54810873     14.55657019
ADD REPLY
0
Entering edit mode

it works perfectly after I have corrected the metadata file by using below command.

cat metadata.txt | tr "\r" "\n" > metadata.txt

Many thanks

ADD REPLY
0
Entering edit mode

Is it also possible to arrange all Irrigated column together and then rainfed?

Many thanks

ADD REPLY

Login before adding your answer.

Traffic: 1517 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