Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Ref: How can I count the number and kinds of letters at 1st, 2nd and 3rd positions of 3-letter words in a string?

by supriyoch_2008 (Scribe)
on Apr 26, 2012 at 11:02 UTC ( #967308=perlquestion: print w/ replies, xml ) Need Help??
supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perlmonk,

This has reference to the question "How can I count the number and kinds of letters at 1st, 2nd and 3rd positions of 3-letter words in a string?" asked by me on 25 April 2012. Thank you for providing the code. In fact, I was interested in getting the numbers of A,T, G and C at first positions (in all 3-letter words) as scalar variables $A1, $T1, $G1 and $C1, respectively under the print command; similarly for 2nd position as $A2, $T2, $G2 and $C2 and for 3rd positions as $A3, $T3, $G3 and $C3, respectively. I want to apply these scalar values further in some basic arithmetic operations. So I modified your code to suit my needs. But I have got very erroneous results from the modified code even trying for 6 hours. In the mean time I have read the pages 60, 76, 111, 106,171, 329 for using 'foreach loop' in the book "Beginning Perl for Bioinformatics" but I didn't get any clue in those pages. I didn't write "use strict" at the beginning of the code intentionally. I am really confused where I am making the mistake in the code. Can you help me to sort out this problem?

My modified code goes as:

#!usr/bin/perl-w $seq="ATCGGCGCCTAT"; #Line 2 # Line 3 perlre @trilet= $seq =~ /.../g; # Line 5 perlsyn LOOP foreach $A1('A','T','G','C'){$first{$A1}=0;} foreach $A2('A','T','G','C'){$second{$A2}=0;} foreach $A3('A','T','G','C'){$third{$A3}=0;} foreach $T1('A','T','G','C' ){$first{$T1}=0;} foreach $T2('A','T','G','C'){$second{$T2}=0;} foreach $T3('A','T','G','C'){$third{$T3}=0;} foreach $G1('A','T','G','C'){$first{$G1}=0;} foreach $G2('A','T','G','C'){$second{$G2}=0;} foreach $G3('A','T','G','C'){$third{$G3}=0;} foreach $C1('A','T','G','C'){$first{$C1}=0;} foreach $C2('A','T','G','C'){$second{$C2}=0;} foreach $C3('A','T','G','C'){$third{$C3}=0;} foreach $tri(@trilet){ # Line 18 # Line 19 perlfunc: substr $first{substr $tri,0,1}++;# Line 20 $second{substr $tri,0,1}++;# Line 21 $third{substr $tri,0,1}++;} foreach $A1('A','T','G','C'){print"$A1=$first{$A1};\n";} foreach $A2('A','T','G','C'){print"$A2=$first{$A2};\n";} foreach $A3('A','T','G','C'){print"$A3=$first{$A3};\n";} foreach $T1('A','T','G','C'){print"$T1=$first{$T1};\n";} foreach $T2('A','T','G','C'){print"$T2=$first{$T2};\n";} foreach $T3('A','T','G','C'){print"$T3=$first{$T3};\n";} foreach $G1('A','T','G','C'){print"$G1=$first{$G1};\n";} foreach $G2('A','T','G','C'){print"$G2=$first{$G2};\n";} foreach $G3('A','T','G','C'){print"$G3=$first{$G3};\n";} foreach $C1('A','T','G','C'){print"$C1=$first{$C1};\n";} foreach $C2('A','T','G','C'){print"$C2=$first{$C2};\n";} foreach $C3('A','T','G','C'){print"$C3=$first{$C3};\n";} exit;

My Results:

Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\ xyz>cd desktop C:\Users\ xyz\Desktop>k3.pl A=1; T=1; G=2; C=0; A=1; T=1; G=2; C=0; A=1; T=1; G=2; C=0; A=1; T=1; G=2; C=0; A=1; T=1; G=2; C=0; A=1; T=1; G=2; C=0; A=1; T=1; G=2; C=0; A=1; T=1; G=2; C=0; A=1; T=1; G=2; C=0; A=1; T=1; G=2; C=0; A=1; T=1; G=2; C=0; A=1; T=1; G=2; C=0; C:\Users\ xyz\Desktop>

Comment on Ref: How can I count the number and kinds of letters at 1st, 2nd and 3rd positions of 3-letter words in a string?
Select or Download Code
Re: Ref: How can I count the number and kinds of letters at 1st, 2nd and 3rd positions of 3-letter words in a string?
by johngg (Abbot) on Apr 26, 2012 at 11:35 UTC

    The first thing that jumps out of the code is that you are initialising and incrementing elements of %first, %second and %third but only printing results from %first.

    Cheers,

    JohnGG

Re: Ref: How can I count the number and kinds of letters at 1st, 2nd and 3rd positions of 3-letter words in a string?
by ww (Bishop) on Apr 26, 2012 at 11:39 UTC

    You've been offered the tools to learn how to do this yourself; you've been given solutions to most of the many questions you've asked en route to this one. Please consider either using these gifts to good effect... or hire a programmer.

Re: Ref: How can I count the number and kinds of letters at 1st, 2nd and 3rd positions of 3-letter words in a string?
by brx (Pilgrim) on Apr 26, 2012 at 12:21 UTC

    It's difficult to explain in english that... I don't undertand what is really your problem. The code in Re: How can I count the number and kinds of letters at 1st, 2nd and 3rd positions of 3-letter words in a string? is not very nice but it is simple and I don't understand the aim of your corrections.

    You said: I didn't write "use strict" at the beginning of the code intentionally. -- Don't do that! It's baaaad! use strict; is your best friend!

    It seems you don't what to use an hash. Why ? $A1,$G1...$C3: 12 variables!!!

    Finnaly, my script was probably not so simple and I propose another one, with one hash and keys like "A1" or "G3".

    #!perl use strict; use warnings; my $seq = "ATCGGCGCCTAT" ; my %poscount; my @trilet = $seq =~ /.../g; #print join "\n",@trilet; foreach my $let ('A','T','G','C') { #init $poscount{$let.'1'}=0; $poscount{$let.'2'}=0; $poscount{$let.'3'}=0; } foreach my $tri (@trilet) { print "processing '$tri'\n"; my $let1 = substr $tri,0,1; $poscount{$let1.'1'}++; print $let1,"1++\t"; my $let2 = substr $tri,1,1; $poscount{$let2.'2'}++; print $let2,"2++\t"; my $let3 = substr $tri,2,1; $poscount{$let3.'3'}++; print $let3,"3++\n\n"; } foreach my $pos (1,2,3) { foreach my $let ('A','T','G','C') { my $letpos = $let.$pos; print "$letpos=$poscount{$letpos}; " } print "\n"; } __END__ processing 'ATC' A1++ T2++ C3++ processing 'GGC' G1++ G2++ C3++ processing 'GCC' G1++ C2++ C3++ processing 'TAT' T1++ A2++ T3++ A1=1; T1=1; G1=2; C1=0; A2=1; T2=1; G2=1; C2=1; A3=0; T3=1; G3=0; C3=3;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2014-10-01 21:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (40 votes), past polls