CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
new line after comma

 



cmccabe1
Novice

Jan 23, 2014, 7:14 AM

Post #1 of 4 (1619 views)
new line after comma Can't Post

I use the code below to output the attached output. My question is if there are multiple hits separated by a comma, how can they be each a new row?
For example,
chr2 39222264 39222264 - A 39222271 G 39222507 T 39222423 would be:
chr2 39222264 39222264 - A 39222271
chr2 39222264 39222264 - G 39222507
chr2 39222264 39222264 - T 39222423


Code
cat sequences_4homopolymers.txt | perl -aF/\\t/ -lne '($chr,$st)=$F[0]=~/(chr\w+)\:(\d+)/ && ($1, $2);%k=map $F[1]=~m/$_{4}/i?($_,length($`)):(), qw(A G C T); print join("\t",$chr,$st,$st,"-",map "$_\t".($st+$k{$_}),sort keys %k)' >4hp.txt

Thank you.
Attachments: 4hp.txt (8.12 KB)


Chris Charley
User

Jan 23, 2014, 1:24 PM

Post #2 of 4 (1597 views)
Re: [cmccabe1] new line after comma [In reply to] Can't Post

I see no commas in your input or desired output. There are only tabs in your sample file '4hp.txt'.


BillKSmith
Veteran

Jan 23, 2014, 8:38 PM

Post #3 of 4 (1573 views)
Re: [Chris Charley] new line after comma [In reply to] Can't Post

I believe that 4php.txt contains neither he input nor the desired output. It is the output of the very long one-liner in the original post. The example in the post shows one line from 4php.txt and the same data the way it should be printed.

The new fields and newlines must be added to the map block in the print statement. I will not attempt to post an untested version and testing is impossible without an input file.

I strongly recommend against implementing this task as a one-liner. A well formatted (and possibly commented) script would be a huge first step toward making the required changes.
Good Luck,
Bill


BillKSmith
Veteran

Jan 25, 2014, 1:43 PM

Post #4 of 4 (1459 views)
Re: [cmccabe1] new line after comma [In reply to] Can't Post

I believe that I have reverse engineered an input file (see attached file in.txt) which corresponds to lines 1 and 6 of 4hp.txt. Note: 'x' is used to represent characters that I cannot infer. The following script processes it into the specified form.


Code
#!perl -nlaF/\t/ 
use strict;
use warnings;
my ( $chr, $st ) = $F[0] =~ /(chr\w+)\:(\d+)/;
my %k = map $F[1] =~ m/$_{4}/i ? ( $_, length($`) ) : (), qw(A G C T);

foreach (sort keys %k){
local $, = "\t";
print $chr, $st, $st, '-', $_, ($st + $k{$_});
}



OUTPUT:

Code

chr1 15251156 15251156 - A 15251257
chr1 15251156 15251156 - T 15251216
chr2 39212965 39212965 - A 39213290
chr2 39212965 39212965 - G 39213202
chr2 39212965 39212965 - T 39213033

Code
Good Luck, 
Bill
Attachments: in.txt (0.46 KB)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives