Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Reverse complement

by bluray (Sexton)
on Oct 31, 2011 at 16:32 UTC ( [id://934917]=perlquestion: print w/replies, xml ) Need Help??

bluray has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perlmonks,

Recently, I posted a question about creating a column of frequencies for the unique entries of other column (http://www.perlmonks.com/?node_id=934469). Thanks for the valuable inputs. In the same script, I did some tweaking to reverse complement the sequence under $1 and then print it like before. I am able to get the results, but I am curious if there is another neat way to do the step.

#!usr/bin/perl use strict; use warnings; my $sequence='ABCD'; my @headings= qw/ Tags Frequency /; my $headings=join("\t",@headings); my @input_files=<*.seq>; foreach my $input_file (@input_files) { open INPUT, "<", $input_file or die "Cannot open file \"$input_fil +e\". $!"; (my $outfile = $input_file) =~ s/.seq/\.tag\.txt/i; my %freq; while (my $line=<INPUT>) { if ($line=~m/$sequence(.{11})(.{11})$sequence/i){ my $revcomp=reverse($1); $revcomp=~tr/ACGTacgt/TGCAtgca/; $freq{$_}++ for $revcomp, $2; } } close INPUT or die "Cannot close file \"$input_file\". $!"; open OUTPUT, ">", $outfile or die "Cannot open file \"$outfile\". + $!"; print OUTPUT $headings, "\n"; for my $tag (sort {$freq{$b} <=> $freq{$a}} keys %freq) { print OUTPUT $tag,"\t",$freq{$tag},"\n"; } close OUTPUT or die "Unable to close \"$outfile\". $!"; }

Replies are listed 'Best First'.
Re: Reverse complement
by Marshall (Canon) on Oct 31, 2011 at 17:00 UTC
    Hi bluray!

    It looks to me like you paid attention to the suggestions in the previous post! I don't see anything glaring wrong here. If this does what you want functionally, I don't see a reason to keep tweaking. (well, I would spiff up the indenting) - but structurally, you have sound code.

    If you are asking if

    my $revcomp=reverse($1); $revcomp=~tr/ACGTacgt/TGCAtgca/;
    is fine, it is. reverse() is fast and tr is much faster than any other way to do a one->one character mapping. If you want to convert to all uppercase at the same time, $revcomp =~ tr/ACGTacgt/TGCATGCA/;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://934917]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 17:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found