LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-04-2014, 06:40 PM   #1
mphillips67
LQ Newbie
 
Registered: Jul 2013
Posts: 9

Rep: Reputation: Disabled
Converting a file with Rows and Columns to just Columns


I have a file with entries that look like this:

Pos
148

A 0
C 0
G 0.081985
T 0.918015
207

A 0.021697
C 0.978303
G 0
T 0

I need to convert this to something that looks like:

Pos A C G T
148 0 0 0.081985 0.918015
207 0.021697 0.978303 0 0


So, my "Pos" entries are more or less already in a column. However, I need to convert the A, C, G, T rows to columns.

Any help would be appreciated.
 
Old 03-04-2014, 07:44 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
What have you tried and where are you such (I mean stuck... stupid table) ?

I think awk would be useful here.

Last edited by schneidz; 03-04-2014 at 11:04 PM.
 
Old 03-04-2014, 08:27 PM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Question: Does this have something to do with Genomes?
 
Old 03-04-2014, 08:33 PM   #4
mphillips67
LQ Newbie
 
Registered: Jul 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-04-2014, 08:36 PM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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.
 
Old 03-04-2014, 08:49 PM   #6
mphillips67
LQ Newbie
 
Registered: Jul 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
That is almost exactly how it is, except the "Pos" and "freq" aren't repeated. Here is a copy of lines directly from the file:

Pos freq
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

Last edited by mphillips67; 03-04-2014 at 08:51 PM.
 
Old 03-04-2014, 09:30 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 03-04-2014, 10:01 PM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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.
 
2 members found this post helpful.
Old 03-04-2014, 10:26 PM   #9
mphillips67
LQ Newbie
 
Registered: Jul 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
That did the trick. Thank you!
 
Old 03-04-2014, 11:50 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
hmmmm ... I am curious how that did the trick for you?

Based on the data and format in post #6 and using the code from post #8, the output I get is:
Code:
Pos freq 0.000000 0.000000 0.081985
T 0.918015 0.021697 0.978303 0.000000
T 0.000000 0.979209 0.000000 0.020791
Now to me this does not look like your desired output?

Assuming you altered the snippet provided, maybe you could show your solution that does provide the output you were looking for, so others may benefit
 
Old 03-05-2014, 12:21 AM   #11
mphillips67
LQ Newbie
 
Registered: Jul 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-05-2014, 02:39 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Cheers

So now, using your addition, the correct output is:
Code:
148 0 0 0.081985 0.918015
207 0.021697 0.978303 0 0
48 0 0
G 0.081985 07 0.021697 0.978303
G 0 os 48
A 0 0.081985 0.918015 07
A 0.021697 0 0
 
Old 03-05-2014, 07:54 AM   #13
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Grail,. not sure what you are doing..

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
Code:
# the output
[root@dev ~]# ./read_data.sh
148 0.000000 0.000000 0.081985 0.918015
207 0.021697 0.978303 0.000000 0.000000
208 0.979209 0.000000 0.020791 0.000000

Last edited by szboardstretcher; 03-05-2014 at 07:55 AM.
 
Old 03-05-2014, 08:34 AM   #14
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748
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
Code:
tail -n +2 data.txt | \
while read line1; do
 read line2;
 read line3;
 read line4;
 read line5;
 read line6;
 echo "Pos ${line3:0:1} ${line4:0:1} ${line5:0:1} ${line6:0:1}";
 echo "$line1 ${line3:2} ${line4:2} ${line5:2} ${line6:2}";
done
Produces
Code:
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!
 
1 members found this post helpful.
Old 03-05-2014, 10:31 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@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:
Code:
awk '{ORS=/T/?"\n":OFS}!/^$/{print $NF}' file
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to convert words in a text file [columns to rows] naveenchandar Linux - Newbie 2 11-08-2012 01:00 AM
[SOLVED] Creating columns from every 4 rows kbensch Linux - General 7 05-19-2012 11:35 AM
convert columns to rows (tab separated file to csv) doug23 Programming 16 08-16-2009 09:14 PM
columns & rows Ammad Linux - General 1 08-08-2005 04:02 AM
rows and columns digitalgravy Linux - General 2 03-16-2004 06:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration