http://www.perlmonks.org?node_id=1009400

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

Hi, I have issues designing a code that scans 2 DNA sequences (taken as input from user, of same length) and then compares the two sequences, nucleotide by nucleotide (e.g. compare nt 1 from sequence 1, with nt 1 from sequence 2 etc). I'm new to perl, I've tried several different ways but I don't seem to get anything working. With the code that I've tried, nothing is printed out at all, so the nested foreach loops seem to get stuck somewhere. I would be greatful for some help/advice. PS I know I should use script, I deleted all that to simplify my problem and will add all that later on once I figured out this problem. Code:
#!/usr/bin/perl use warnings; print("Please enter the first DNA sequence: "); $DNAsequence1 = <STDIN>; chomp($DNAsequence1); + $DNAsequence1=uc($DNAsequence1); $lengthDNA1=length $DNAsequence1; print("\nPlease enter the second DNA sequenc (of same length as the fi +rst): "); $DNAsequence2 = <STDIN>; chomp($DNAsequence2); + $DNAsequence2=uc($DNAsequence2); $lengthDNA2=length $DNAsequence2; #print "The length of DNA sequence 2 is $lengthDNA2"; if ($lengthDNA1==$lengthDNA2){ print "DNA sequence 1: $DNAsequence1\nDNA sequence 2: $DNAsequence2\n" +; } else { print "\nThe DNA sequences are not of same lenght, please try aga +in!"; } @DNAsequence1=split(//,$DNAsequence1); @DNAsequence2=split(//,$DNAsequence2); $score=0; foreach $base1 (@$DNAsequence1) print $score; { foreach $base2(@$DNAsequence2) { if($base1->[0] eq $base2->[0]) { $score += 3; print $score; } else { $score=$score+1; } } } print $score;

Replies are listed 'Best First'.
Re: nested foreach loop, match DNA sequences and calculate score
by roboticus (Chancellor) on Dec 18, 2012 at 15:38 UTC

    NadjaPadjala:

    Your $base1 and $base2 variables aren't arrays, but you're treating them as such. Just try:

    if ($base1 eq $base2)

    Also, your print $score; statement on line 32 should be inside the curly braces. There may be more issues, but that's all I see at the moment.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: nested foreach loop, match DNA sequences and calculate score
by AnomalousMonk (Archbishop) on Dec 18, 2012 at 15:58 UTC

    Do yourself a big favor and use warnings and strict in your code at the beginning of each and every script:
        use warnings;
        use strict;

    Using warnings and, in particular, strictures would have alerted you to the misuse of  @$DNAsequence1 and  $base1->[0] et al.

Re: nested foreach loop, match DNA sequences and calculate score
by Cristoforo (Curate) on Dec 18, 2012 at 19:34 UTC
    To show another way to calculate the matches.
    #!/usr/bin/perl use strict; use warnings; my $fasta1 = 'GGGTATTCCTTCTCCACCTTGCAGCTAACATCAGTGTTTCGTCTACTCAAGCACGC +CAAC'; my $fasta2 = 'ACGCCCTAGAGCGCCCTGTCCAGGGGATGGCAACCAACTCTGACCCTGCAAGTGCA +GCAG'; my $matched = ($fasta1 ^ $fasta2) =~ tr/\0//; my $non_match = length($fasta1) - $matched; print "matched: $matched\n"; print "non_matched: $non_match\n";
    This prints:
    matched: 15 non_matched: 45
Re: nested foreach loop, match DNA sequences and calculate score
by NadjaPadjala (Initiate) on Dec 19, 2012 at 16:06 UTC
    Hi, Thank you all for your very fast and helpful advices! It helped me a lot, obviously I've been stuck with rather basic problems, not always seeming so logic when you're new to it. Thank you again! Nadja