Merge column headers and transpose


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge column headers and transpose
# 1  
Old 11-29-2012
Merge column headers and transpose

Hello Everyone!
I am new on this forum and this is my first post. I wish to apologize for my, not canonical, English.
I would like to solve this problem but I have no clue of how do it!I will be grateful if someone could help me!
I have a table like this:
Code:
gene TF1 TF2 TF3 TF4
gene1 1 2 3 4
gene2 1 1 4 4
gene3 1 2 3 3
gene4 1 2 3 4
gene5 1 2 1 3
gene6 2 2 1 2
gene7 3 3 1 3
gene8 2 3 1 4

Each row is a gene and each column a transcription factor.The number in the intersection means if the TF there is, there is not, is upregulated, is downregulated.
Now what I would like to obtain is something like that:
Code:
gene TF
gene1TF1 1
gene1TF2 2
gene1TF3 3
gene1TF4 4
gene2TF1 1
gene2TF2 1
gene2TF3 4
gene2TF4 4
and so on

Adding at each gene the TF and the relative number!I get crazy!
I really hope that someone could help me!
Thanks a lot

Giuliano

Last edited by joeyg; 11-29-2012 at 02:29 PM.. Reason: corrected spelling
# 2  
Old 11-29-2012
Code:
awk '
NR==1 {
  n=split($0,h,FS)
  print $1, "TF"
  next
}
{
  for(i=2;i<=n;i++)
    print $1 h[i], $i
}' myFile

# 3  
Old 11-29-2012
Thaks a lot but in the output the gene is not repeated for each trasnscription factor(TF).Its shown just the first TFfor each gene!!
Other suggestion?
# 4  
Old 11-29-2012
Code:
awk '
 NR==1 {   n=split($0,h,FS)   print $1, "TF"   next } 
{   for(i=2;i<=n;i++)     print $1 h[i], $i }' myFile

and with myFile:
Code:
gene TF1 TF2 TF3 TF4
gene1 1 2 3 4
gene2 1 1 4 4
gene3 1 2 3 3
gene4 1 2 3 4
gene5 1 2 1 3
gene6 2 2 1 2
gene7 3 3 1 3
gene8 2 3 1 4

produces the following:
Code:
gene TS
gene1TF1 1
gene1TF2 2
gene1TF3 3
gene1TF4 4
gene2TF1 1
gene2TF2 1
gene2TF3 4
gene2TF4 4
gene3TF1 1
gene3TF2 2
gene3TF3 3
gene3TF4 3
gene4TF1 1
gene4TF2 2
gene4TF3 3
gene4TF4 4
gene5TF1 1
gene5TF2 2
gene5TF3 1
gene5TF4 3
gene6TF1 2
gene6TF2 2
gene6TF3 1
gene6TF4 2
gene7TF1 3
gene7TF2 3
gene7TF3 1
gene7TF4 3
gene8TF1 2
gene8TF2 3
gene8TF3 1
gene8TF4 4

this matches exactly your desired output.
# 5  
Old 11-30-2012
Also Try..

Code:
awk 'NR==1{split($0,P);print $1,substr($2,1,2)}
    NR>1{for(i=2;i<=NF;i++){print $1""P[i],$i}}' file

# 6  
Old 11-30-2012
Ok it works.I made I mistake!!
Thanks a lot!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Transpose from row to column using timestamp in first column

Gents, Transpose from row to column, taking in consideration the first column, which contends the date. Input file 72918,111000009,111000009,111000009,111000009,111000009,111000009,111000009,111000009,111000009 72918,2356,2357,2358,2359,2360,2361,2362,2363,2364 72918,0,0,0,0,0,0,0,0,0... (12 Replies)
Discussion started by: jiam912
12 Replies

2. Shell Programming and Scripting

Merge csvs with column headers

hello gurus, Somebody must have done this before, I couldn't find anything. Please redirect me if this was solved before, and if not please help. To the problem now, I have multiple csv files (about 1000) which I need to concatenate by column header. The final file should have a superset... (4 Replies)
Discussion started by: abh.kumar
4 Replies

3. Shell Programming and Scripting

Transpose Second column only

Hi Folks, My input file is like this cat input abcd:efgh:jklm 123,456,67,78,89,90 hi:kil:op 76,78,12,3456, unix:linux:shell:bash 111,111 My expected output abcd:efgh:jklm hi:kil:op unix:linux:shell:bash 123 76 111 456 78 111 67 12 78 3456 89 90 (5 Replies)
Discussion started by: jacobs.smith
5 Replies

4. Shell Programming and Scripting

Transpose from 2nd column till the last column

Hi I have 5 columns like this a b c d e f g h i j k l m n o From 2nd column till the 5th column of every record, I would like to transpose them as rows, so my output file contains only one row a b c d e f g h i j (9 Replies)
Discussion started by: jacobs.smith
9 Replies

5. Shell Programming and Scripting

Transpose field names from column headers to values in one column

Hi All, I'm looking for a script which can transpose field names from column headers to values in one column. for example, the input is: IDa;IDb;IDc;PARAM1;PARAM2;PARAM3; a;b;c;p1val;p2val;p3val; d;e;f;p4val;p5val;p6val; g;h;i;p7val;p8val;p9val; into the output like this: ... (6 Replies)
Discussion started by: popesk
6 Replies

6. Shell Programming and Scripting

Merge Multiple Files and Transpose

Looking to join three files and then transpose some columns from multiple rows into a single row. File Info: FIELD TERMINATED BY '^' ENCLOSED BY '~' LINE TERMINATED BY '\r\n' FIRST FILE (FOOD_DES.txt) ~01001~^~0100~^~Butter, salted~^~BUTTER,WITH... (2 Replies)
Discussion started by: mkastin
2 Replies

7. Shell Programming and Scripting

Transpose multipe columns to rows and adding headers

Hi, I found the following awk script to transpose multiple (3) columns to multiple rows: #=== BEGIN {FS=","} { for (i=1;i<=NF;i++) { arr=$i; if(nf<= NF) nf=NF; } nr=NR } END { for(i=1;i<=nf;i++) { (8 Replies)
Discussion started by: Gery
8 Replies

8. UNIX for Dummies Questions & Answers

Sort by Column Headers

Hi All, I am new to UNIX can you please help me to sort a file with different columns my file looks like this $ cat gaut.txt UID PID PPID PGID SID C STIME TTY TIME CMD liveuser 3008 2892 3008 3008 0 11:58 ? 00:00:00 gnome-session liveuser 3019 1 ... (8 Replies)
Discussion started by: cgk1983
8 Replies

9. Shell Programming and Scripting

Excel Column Headers

cat ABC.log | egrep "Error 500" >> /tmp/Logs.log egrep "<Mango>.*<.Mango>" Logs.log | sed -e "s/^.*<Mango/<Mango/" | cut -f2 -d">"| cut -f1 -d"<" >> /tmp/temp1.xls egrep "<Apple>.*<.Apple>" Logs.log | sed -e "s/^.*<Apple/<Apple/" | cut -f2 -d">"| cut -f1 -d"<" >> /tmp/temp2.xls print Heading1,... (1 Reply)
Discussion started by: pk_eee
1 Replies

10. Shell Programming and Scripting

Removing Headers and a Column

I have a text file in unix with a layout like this Column 1 - 1-12 Column 2 - 13-39 Column 3 - 40-58 Column 4 - 59-85 Column 5 - 86-120 Columbn 6 - 121-131 The file also has a header on the first 6 lines of each page. Each page is 51 lines long. So I want to remove the header from each... (30 Replies)
Discussion started by: DerangedNick
30 Replies
Login or Register to Ask a Question