Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Trying to use substring to find a reverse complement in DNA,

by tryingnottofailmytes (Novice)
on Dec 05, 2015 at 22:59 UTC ( [id://1149482]=perlquestion: print w/replies, xml ) Need Help??

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

Hi. Obviously I'm new here. I'm a biology student playing in a CS class this semester and I have a final on Tuesday. Theres a lot of stuff I don't understand, so I'm hoping you guys can help me out. I'm trying to get the reverse complement of a string of DNA w/o using s// or tr/// and HAVE to use substring. Its a practice problem in our book that I'm working on. What I have so far
my $dna = 'atgAAAAAAAAAAGGGGGGGGGGTTTTTTTTCCCCCCCCCCCCagtcatc'; $dna = uc$dna; print "$dna\n"; my $revDNA= reverse $dna; print "$revDNA\n"; my $length = length$revDNA; my $complement = ''; for (my $i=0;$i>$length-1; $i++) { my $base= substr($revDNA,$i,1); if ($base eq 'A') { $base = 'T'; } elsif ($base eq 'T') { $base = 'U'; } elsif ($base eq 'G') { $base = 'C'; } elsif ($base eq 'C') { $base = 'G'; } $complement.=$base; } print $complement, "\n"; exit;
I don't really understand why this isn't working I'm not sure what the differences is between my and the posted solution, which has the for loop go backwards instead of forwards.... Can anyone offer any help?

Replies are listed 'Best First'.
Re: Trying to use substring to find a reverse complement in DNA,
by RichardK (Parson) on Dec 05, 2015 at 23:12 UTC

    The condition on your for loop is the wrong way round, you need to use less than not greater than, here:-

    $i>$length-1;

    If you want to know how to fix these things for yourself try the basic debugging checklist

      ... you need to use less than not greater than ...

      I would say tryingnottofailmytes needs to use less-than-or-equal
          $i <= $length-1
      as the for-loop condition in the OPed code.


      Give a man a fish:  <%-{-{-{-<

      THANK YOU SO MUCH!!!! This is my problem, I get really close in my code (at least I think I do) and I can't find the problems and I freak out and get a terrible score...
Re: Trying to use substring to find a reverse complement in DNA,
by johngg (Canon) on Dec 06, 2015 at 00:16 UTC

    Rather than the series of if { ... } elsif { ... } ... tests you could use a hash table to look up the base and use it as the fourth argument to substr to replace the original.

    $ perl -Mstrict -Mwarnings -E ' my $dna = q{AAGGTTCC}; my %compLU = ( A => q{T}, T => q{U}, G => q{C}, C => q{G}, ); my $complement = $dna; for my $idx ( 0 .. length( $complement ) - 1 ) { substr $complement, $idx, 1, $compLU{ substr $complement, $idx, 1 +}; } say $dna; say $complement;' AAGGTTCC TTCCUUGG $

    I hope this is of interest.

    Cheers,

    JohnGG

Re: Trying to use substring to find a reverse complement in DNA,
by Anonymous Monk on Dec 06, 2015 at 04:40 UTC
      Could also say:
      my $rc = reverse uc($dna) =~ tr/ATGC/TUCG/r;
      Laurent_R said Except that the OP said: without using s// or tr///. That's part of the assignment.

      Yes, my bad.

      Yeah, it seems much simpler and I would probably go for something along these lines.

      Except that the OP said: without using s// or tr///. That's part of the assignment.

Re: Trying to use substring to find a reverse complement in DNA,
by BillKSmith (Monsignor) on Dec 06, 2015 at 14:55 UTC
    UPDATE: Please disregard this suggestion. I did not notice the explicit reverse.

    Your book ran the loop backwards to reverse the string. You can run it forward, but you must reverse the concatenation:

    $complement = $base . $complement;
    Bill

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-16 15:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found