Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How can I count the types of changes in 1st, 2nd and 3rd positions of all 3-letter words in comparing two separate strings of unequal length?

by supriyoch_2008 (Scribe)
on May 13, 2012 at 10:39 UTC ( #970267=perlquestion: print w/ replies, xml ) Need Help??
supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perlmonks,

I am a beginner in perl programming. I have two strings of unequal sizes (15 & 12 letters) i.e. $a="AGAGATATTCAAATT"; and $b="TCGAACGTCGAG"; In comparing the two given strings $a and $b, I am interested to count the types of changes (A to T i.e. A2T, G2A, A2G etc.) for 1st, 2nd and 3rd positions of all 3-letter words between two strings. Use of hash and dumper together appears very complicated to me. I donot know which perl code I should use in such counting. Since the two strings are small, one can do it manually. I wish if perl code could do the same task then it can be used in comparing two strings as long as 100 letters (which is difficult to do manually). May I expect to get help from Perlmonks in counting the types of changes between two strings?

#!/usr/bin/perl-w use strict; use Data::Dump qw[ pp ]; my $a="AGAGATATTCAAATT"; my $b="TCGAACGTCGAG"; my (%first,%second,%third); my @trilet1 = $a =~ /.../g; my @trilet2 = $b =~ /.../g; use Data::Dump qw[ pp ]; Code????? for my $p ( 0 .. length( $seq1 ) - 1 ) { my $c1 = substr $seq1, $p, 1; my $c2 = substr $seq2, $p, 1; next if $c1 eq $c2; ++$subs{ $c1 . '2' . $c2 }; } pp \%first,%second, %third; exit; <p>The result should look like:</p> <code>Counting Results: { For 1st position of all 3-letter words: A2T=> 1, G2A=> 1, A2G=> 1, C2G=> 1, For 2nd position of all 3-letter words: G2C=> 1, For 3rd position of all 3-letter words: A2G=> 2, T2C=> 2, }

Comment on How can I count the types of changes in 1st, 2nd and 3rd positions of all 3-letter words in comparing two separate strings of unequal length?
Download Code
Re: How can I count the types of changes in 1st, 2nd and 3rd positions of all 3-letter words in comparing two separate strings of unequal length?
by baxy77bax (Chaplain) on May 13, 2012 at 11:06 UTC
    Ok, so if i understood you correctly you have two problems here:
    1. You need to know which codons are homologous and which are not. Is that correct ??
    2. After you figure out the first you wish to know where are the changes within the proper reading frame ???

    Are my assumptions correct ?? Or the sequences are already aligned and you just wish to count the differences ?

    In both cases, when alignment is done to count the diff you just iterate through both arrays count triplets, hash them and the for every triplet make a subhash that will record the type and the count of a specific change.

    Example:

    use strict; use Data::Dumper; my $r = 'AAATGTGATGTGAACGT'; my $t = 'AATGTGTCGT-TG-ATG'; my @a = split('',$r); my @v = split('',$t); my %hash =(); my $tt = @a>@v ? @a : @v; for(my $i = 0 ; $i<$tt;$i++){ my $z = 1+$i %3; # Update - suggested by Perlbotics, better ! unless ($a[$i] eq $v[$i]){ $hash{$z}->{"$a[$i]2$v[$i]"}++; } } print Dumper(\%hash);
    Result:
    $VAR1 = { '1' => { 'G2T' => 3, 'T2G' => 1, 'A2G' => 1 }, '3' => { 'G2T' => 1, 'C2A' => 1, 'T2G' => 2, 'A2T' => 1 }, '2' => { 'G2T' => 1, 'T2G' => 1, 'A2-' => 1, 'A2C' => 1, 'T2-' => 1 } };
Re: How can I count the types of changes in 1st, 2nd and 3rd positions of all 3-letter words in comparing two separate strings of unequal length?
by ww (Bishop) on May 13, 2012 at 12:14 UTC
    You say a very basic Perl operation "appears very complicated" and then repeat the tactics you've used to get others to do your work -- posting a code sample that's essentially no more than a few lines of boilerplate.

    This time, what you're looking for is scarcely more than an extension or adaptation of the advice (and solutions) you've been given previously.

    You'll do better to start now to try to solve your own homework(?) or $work(?). Twenty-one posts with little evidence that you've made a serious attempt to think through your own questions is not a good reflection on your desire to learn... which is what PM is largely about.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-09-19 17:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (143 votes), past polls