Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

find reverse complement of every 2nd line

by bingalee (Acolyte)
on Jul 09, 2013 at 22:04 UTC ( #1043386=perlquestion: print w/replies, xml ) Need Help??
bingalee has asked for the wisdom of the Perl Monks concerning the following question:

I have a file and i need to find the reverse complement only of every 2nd line- basically of every even line. How do i go about it?

Here"s what the file looks like


Thanks in advance guys


I'm facing specific problems in performing the reverse complement only on every other lines.

Actually I need to find reverse complement of every second line and JUST THE REVERSE of the fourth line

Replies are listed 'Best First'.
Re: find reverse complement of every 2nd line
by choroba (Bishop) on Jul 09, 2013 at 23:49 UTC
    Looking at the sample, you want the reverse complement of the second line, plus four, plus four... You can use $. which keeps the line number and the modulus operator %:
    use feature 'say'; while (<>) { chomp; if ($. % 4 == 2 ) { $_ = reverse; y/ACTG/TGAC/; } say; }
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: find reverse complement of every 2nd line
by davido (Archbishop) on Jul 09, 2013 at 22:20 UTC

    To someone who isn't an expert in Bioinformatics, can you please tell me how you would compute the reverse complement in the first place? Explain that, and show what code you have so far, and I or someone else here will help you find a solution.


      Hey Dave. For the reverse complement the code will be something like this

      my $raw=<IN>; $rev= reverse $raw; $rev=~tr/ATGCatgc/TACGtacg/;

      my problem actually is doing this only to every other line and not the entire file

      this is what i came up with, but it doesnt work

      while(@inf=<IN>) { if(i%2==1) { $inf[i]=reverse $inf[i]; } }

      In this i was just trying to find the reverse to see if this code performs the action on the lines I want it to.. but no luck. Im still thinking of any other way. Any suggestions?

        variables in Perl need sigils, so it must be $i not just i

        (using strict and warnings should have prevented you)

        and you need to increment it with ++$i within the loop.

        As an alternative you could just test $. % 2 since $. automatically holds the line-number.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        my problem actually is doing this only to every other line and not the entire file

        The special variable $. (documented in perldoc perlvar) keeps count of the line numbers, so you can do something like:
        while(<IN>) { next if $. % 2; # skip odd lines # do the processing }
Re: find reverse complement of every 2nd line
by rjt (Deacon) on Jul 09, 2013 at 23:39 UTC

    This was discussed on SoPW recently. Here's one solution (mine! what a coincidence): Re: Reverse Complement.

    And a link to the entire thread: Reverse Complement.

    To process every other line, just check the input line number with $. when you read your file in a while loop, something like this:

    while (<DATA>) { chomp; for ($_) { say scalar reverse when $. == 4; say uc scalar reverse when $. % 2 == 0; default { say }; } }

    (Note the above does not calculate the reverse compliment; check the provided links for that. This is just meant to show you a straightforward way to process the lines you desire.)

    With a little adaptation, you should have no problem coming up with a solution to your problem.

Re: find reverse complement of every 2nd line
by BrowserUk (Pope) on Jul 09, 2013 at 22:42 UTC

    For the pure ACGTacgt lines it is simple:


    Extending that to deal with the other letters and characters is just a case of knowing the complement rules -- which you presumably do -- and then extending the tr/// conversion strings appropriately.

    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.
Re: find reverse complement of every 2nd line
by jeffa (Bishop) on Jul 09, 2013 at 22:44 UTC

    I love Perlmonks but Google is good for searching too, for example a search for "perl bioinformatics cpan reverse complement" yielded this page, which appears to "calculate the reverse complement of a strand of DNA" ... whatever that is. :)


    (the triplet paradiddle with high-hat)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1043386]
Approved by BrowserUk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2017-10-23 11:01 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (279 votes). Check out past polls.