Is there a way of doing this ..., using only substr function?

As has been shown, you can. But why would you?

tr *ISN'T* a regular expression. It is a compile time constructed table lookup; designed for exactly this purpose; and it will do the job anywhere from 100x to 400 78x & 270 times faster than the other methods offered here:

#! perl -slw use strict; use Benchmark qw[ cmpthese ]; our $seq = "ACGGGAGGACGGGAAAATTACTACGGCATTAGCacgggaggacgggaaaattactacg +gcattagc"; our %xref = ( A => 'T', C => 'G', G => 'C', T => 'A', a => 't', c => 'g', g => 'c', t => 'a', ); our %rc = ( A => q{T}, T => q{A}, C => q{G}, G => q{C}, ); cmpthese -1, { tr => q[ ( my $revcmp = reverse $seq ) =~tr[ACGTacgt][tgcaTGCA]; ], davido => q[ my $seq = $seq; for(my $ix = 0; $ix < length $seq; ++$ix ) { substr( $seq, $ix, 1, $xref{ substr( $seq, $ix, 1 ) } ); } my $reverse = reverse($seq); ], atcroft => q[ my $complement; my @letters = split //, $seq; while ( @letters ) { my $l = uc pop @letters; $complement .= $rc{$l}; } ], hdb => q[ my $rev=''; my $n = length $seq; while( $n-- ){ $rev .= $_ for map chr( $_ & 2 ? $_^4: $_^21 ), ord substr + $seq, $n, 1 } ], } __END__ C:\test>junk60 Rate hdb atcroft davido tr hdb 9827/s -- -35% -71% -100% atcroft 15077/s 53% -- -55% -99% davido 33822/s 244% 124% -- -99% tr 2679052/s 27163% 17669% 7821% --

(I know you guys know this; but does the OP??? And if they do, why are they asking for this?)


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Create the reverse complement DNA sequence without pattern matching and reverse built-in function? by BrowserUk
in thread Create the reverse complement DNA sequence without pattern matching and reverse built-in function? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.