This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Printing Reverse Complement Of Dna In Single-Line Perl

I want to write a quick single-line perl script to produce the reverse complement of a sequence of DNA. The following isn't working for me, however:

$ cat sample.dna.sequence.txt | perl -ne '{while (<>) {$seq = $_; $seq =~ tr /atcgATCG/tagcTAGC/; $revComp = reverse($seq); print $revComp;}}'

Any suggestions?

perl bash sequence

The simpler version is: cat test.txt | perl -pe 'chomp;tr/ACGTacgt/TGCAtgca/;$_=reverse."\n"'

an even simpler version is cat test.txt | perl -pe 'tr/ACGTacgt/TGCAtgca/;reverse'

6 answers

Why use single line. keep the code to your script fold and use these routine script everyday.

Like: perl /home/usr/bin/rcdna.pl ATCGATGCG

#!/usr/bin/perl
use strict;
my $dna=shift @ARGV;
my $rcdna= & reverse_complement_IUPAC($dna);
print "$rcdna\n";

sub reverse_complement_IUPAC {
        my $dna = shift;

        # reverse the DNA sequence
        my $revcomp = reverse($dna);

        # complement the reversed DNA sequence
        $revcomp =~ tr/ABCDGHMNRSTUVWXYabcdghmnrstuvwxy/TVGHCDKNYSAABWXRtvghcdknysaabwxr/;
        return $revcomp;
}

shell script? you don't need perl:

tr -d "\n " < input.txt | tr "[ATGCatgcNn]" "[TACGtacgNn]" | rev

Say I want to do it with perl, for illustrative purposes. How do I go about that? And why doesn't my code work?

you need to concatenate all the strings in the while lool. Once everything is read, call tr , reverse and print.

Here is the modified solution that can handle IUPAC degeneracies:

tr -d "\n" < input.txt | tr "[ATGCUatgcuNnYyRrSsWwKkMmBbDdHhVv]" "[TACGAtacgaNnRrYySsWwMmKkVvHhDdBb]" | rev

If you want to use Perl:

$ echo "CAAT" | perl -le '{$seq = <STDIN>; chomp $seq; $seq =~ tr /atcgATCG/tagcTAGC/; $seq = reverse($seq); print $seq; }'
ATTG

STDIN is a default handle, so you could just take that out for brevity. I like being explicit if it isn't too wordy, though.

for this matter I usually go for this compact yet self-explanatory code inside my scripts

echo -n "CAAT" | perl -pe '{tr/atcgATCG/tagcTAGC/; $_=reverse}'

although I would agree that if reverse complementing is the only thing you want to do, you'll probably find shell scripting faster.

This will give you the reverse complement of each line, try something like:

$tail -n +2 seq.fa | tr -d "\n" |tr "acgtACGT" "tgcaTGCA" |rev
perl -0777pe's/\n //g; tr/ATGCatgcNn/TACGtacgNn/; $_ = reverse $_;' sample.sequence.txt

Hi , maybe you can write a perl script like the following ,

  use Bio::Seq;

  $seqobj = Bio::Seq->new(-seq => $seq);
  $Query = $seqobj->revcom()->seq;

Good luck !

Log in to answer this question.