[SOLVED] Converting a file with Rows and Columns to just Columns
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I haven't tried anything specific yet. I found a few methods to convert rows to columns, most using sed, but I am at a loss for how apply that to what I am working with. I basically would like to convert the A, C, G, and T rows to columns, then have their entries line up to the positions.
Also, yes, these are nucleotide frequencies at a given position.
Is this the fixed format of the data? Its repeated EXACTLY like this over and over(allowing for different data obviously)? Like this, in perpetuity:
Code:
Pos
148
A 0
C 0
G 0.081985
T 0.918015
207
A 0.021697
C 0.978303
G 0
T 0
Pos
148
A 0
C 0
G 0.081985
T 0.918015
207
A 0.021697
C 0.978303
G 0
T 0
Pos
148
A 0
C 0
G 0.081985
T 0.918015
207
A 0.021697
C 0.978303
G 0
T 0
and so on
If not, can you post a selection of the actual data? EXACTLY how it is in your file?
Last edited by szboardstretcher; 03-04-2014 at 08:38 PM.
Try searching for columns to rows on this site as this has been done multiple times. I would include the key word 'awk' as well as columnized data is better suited to this command than sed
And, without diving into a code excercise, and sticking to bash alone:
Requirements:
remove pos at beginning of file
structure stays the same throughout the file
Code:
while read line1; do #first line pos
read line2 # blank
read line3 # A
read line4 # C
read line5 # G
read line6 # T
echo $line1 ${line3:1} ${line4:1} ${line5:1} ${line6:1} >> output_file
done < data
Last edited by szboardstretcher; 03-04-2014 at 10:04 PM.
Yea, the file had to be altered slightly for it to work. Based on the requirements szboardstretcher outlined, I removed the first line containing "Pos" and "Freq". I can't attest to whether or not its the most efficient way, but I basically did it in two steps as shown below:
Code:
more +2 oldfile > newfile
while read line1; do #first line pos
read line2 # blank
read line3 # A
read line4 # C
read line5 # G
read line6 # T
echo $line1 ${line3:1} ${line4:1} ${line5:1} ${line6:1} >> output_file
done < newfile
The columns don't have labels, but I will be working with these files in R so will take care of that when I read them in.
Edit: You are using the original data. OP provided another snippet further down in the post.
Code:
# The data
[root@dev ~]# cat data
148
A 0.000000
C 0.000000
G 0.081985
T 0.918015
207
A 0.021697
C 0.978303
G 0.000000
T 0.000000
208
A 0.979209
C 0.000000
G 0.020791
T 0.000000
Code:
# the script
[root@dev ~]# cat read_data.sh
while read line1; do #first line pos
read line2 # blank
read line3 # A
read line4 # C
read line5 # G
read line6 # T
echo $line1 ${line3:1} ${line4:1} ${line5:1} ${line6:1}
done < data
Just to finesse this a little for posterity's sake, grail has a point about the substring indexing (which actually starts at zero) in the parameter expansions used in the echo command. It works with :1 as the leading blank is skipped.
Using the data in post#6
Pos A C G T
148 0.000000 0.000000 0.081985 0.918015
Pos A C G T
207 0.021697 0.978303 0.000000 0.000000
Pos A C G T
208 0.979209 0.000000 0.020791 0.000000
Last edited by allend; 03-05-2014 at 09:02 AM.
Reason: Must keep our nucleotides in order!
@szboardstretcher - you are right that I am not sure what was happening?? I have now run the same code at home and all is fine ... go figure ... sorry for the confusion.
I should give a solution for all those shenanigans:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.