Question: (Closed) converting rows into columns and vice versa
1
gravatar for Ana
12 months ago by
Ana100
Ana100 wrote:

I have a table file (including 11 million SNPs) and simply I want to convert rows into columns and column into rows. something like this, this is my unput

7 2 2 0 6  
0 2 0 0 0 
2 2 0 0 2

this is my desired output

7  0  2
2  2  2
2  0  0
0  0  0
6  0  2

normally I can do it so easily with R by using as.data.frame(t(df)), but it does not work with my data set maybe because file is so big! I could find this little Python script but does not work as well

with open('in.txt') as f:
  lis = [x.split() for x in f]

for x in zip(*lis):
  for y in x:
    print(y+'\t', end='')
  print('\n')

when I run it I get this error message

File "./row_to_column_transposition.py", line 8
    print(y+'\t', end='')
                     ^
SyntaxError: invalid syntax

I have tried other ways like awk but it does not give me my desired format! Can someone help me to fix it, I would appreciate that.

colums rows python table • 583 views
ADD COMMENTlink modified 12 months ago by EagleEye5.3k • written 12 months ago by Ana100

How many samples do you have for your 11 million SNPs? You tried this?

awk ' { 
    for (i=1; i<=NF; i++)  {
        a[NR,i] = $i
    } } NF>p { p = NF } END {    
    for(j=1; j<=p; j++) {
        str=a[1,j]
        for(i=2; i<=NR; i++){
            str=str" "a[i,j];
        }
        print str
    } }' FILE1.txt > FILE2.txt
ADD REPLYlink modified 12 months ago • written 12 months ago by Floris Brenk830

You may want to deep-link relevant post in future instead of just copying/pasting the code. Use the https link that shows up in address bar after you click the "link" button in any post and paste in new post.

ADD REPLYlink written 12 months ago by genomax49k
1

@genomax, when someone posts a link (instead of copy-pasting the code from other page) where the solution is already available, it gives OP a feeling that with some effort they can find solution instead of asking for help, which is very good.

ADD REPLYlink written 12 months ago by venu5.3k

Hello Ana!

Previous solutions exist and have been posted in this thread.

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

ADD REPLYlink modified 12 months ago • written 12 months ago by genomax49k
1
gravatar for venu
12 months ago by
venu5.3k
Germany
venu5.3k wrote:

Transposing rows and columns: 3 methods

Just a tip: Do not spend too much time on writing scripts when the solutions are already available (unless it is for learning purpose). Consider a google search before starting to write a script.

ADD COMMENTlink written 12 months ago by venu5.3k
0
gravatar for EagleEye
12 months ago by
EagleEye5.3k
Sweden
EagleEye5.3k wrote:

Solution is here,

awk '
{ 
    for (i=1; i<=NF; i++)  {
        a[NR,i] = $i
    }
}
NF>p { p = NF }
END {    
    for(j=1; j<=p; j++) {
        str=a[1,j]
        for(i=2; i<=NR; i++){
            str=str"\t"a[i,j];
        }
        print str
    }
}' YOURFILE.TXT
ADD COMMENTlink modified 12 months ago • written 12 months ago by EagleEye5.3k

I suggest that you paste the code referred to in this answer and then delete the answer you have referred to (from other thread) since it was not appropriate for the question asked there.

ADD REPLYlink written 12 months ago by genomax49k

Just noticed that the AWK code mentioned in this post is same as mine from previous post. That was the reason I did not repeat it.

ADD REPLYlink written 12 months ago by EagleEye5.3k
1

Hmm. Not nice of @Floris to paste it here without proper attribution.

ADD REPLYlink modified 12 months ago • written 12 months ago by genomax49k

Here is it first mentioned in 2009 -> https://stackoverflow.com/questions/1729824/an-efficient-way-to-transpose-a-file-in-bash

ADD REPLYlink modified 12 months ago • written 12 months ago by Floris Brenk830

Aha .. I retracted my statement (leaving it in place since if I delete it it will remove all daughter nodes). Thank you @Floris for providing the original link.

ADD REPLYlink modified 12 months ago • written 12 months ago by genomax49k
Please log in to add an answer.
The thread is closed. No new answers may be added.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1976 users visited in the last hour