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

Re: What is wrong in this code???

by Kenosis (Priest)
on Dec 07, 2012 at 20:56 UTC ( #1007823=note: print w/replies, xml ) Need Help??


in reply to What is wrong in this code???

Try the following:

use strict; use warnings; my %sequences; while (<>) { for my $char ( split '', (split)[1] ) { $sequences{$char}++; } } print "$_ => $sequences{$_}", "\n" for keys %sequences;

Output on your data set:

- => 406 A => 1708 T => 1639 C => 2057 G => 1946

Replies are listed 'Best First'.
Re^2: What is wrong in this code???
by Anonymous Monk on Dec 07, 2012 at 21:06 UTC
    Thanks, but that is not what I need. What I must have, is the number of A, C, T, G and - per POSITION. So the output would be something like:
    POS #1 A:150 | T:50 | G:0 | C:20 | -:30 POS 21 A:80 | T:60 | G:40 | C:20 | -:0 etc

    So the numbers must add up the number of sequences, each time...

      My apologies; I did misunderstand. The following should work:

      use strict; use warnings; my %sequences; while (<>) { my $i = 1; for my $char ( split '', (split)[1] ) { $sequences{ $i++ }{$char}++; } } for my $pos ( sort { $a <=> $b } keys %sequences ) { my @charCount; while ( my ( $char, $count ) = each %{ $sequences{$pos} } ) { push @charCount, "$char:$count"; } print "POS #$pos " . ( join ' | ', @charCount ) . "\n"; }

      This is the output:

      POS #1 a:4 POS #2 q:1 | b:3 POS #3 c:4 POS #4 x:1 | d:2 | z:1 POS #5 e:4 POS #6 f:4 POS #7 c:1 | a:1 | b:1 | d:1 POS #8 h:4 POS #9 i:4 POS #10 j:4

      Run on this data:

      12 abcdefahij 12 abcdefbhij 12 aqcxefchij 12 abczefdhij
      use strict; use warnings; while (<>) { my %sequences; for my $char ( split '', (split)[1] ) { $sequences{$char}++; } print "$_ => $sequences{$_}", "\n" for keys %sequences; }

      Note how I declared the hash inside the loop. Scoping variables correctly is the easiest way to reset them.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007823]
help
Chatterbox?
[Discipulus]: i hope too, vacuum cleaner permitting, 2-3 afternoon to build. Or is the 1:1 serie? ;=)
[Tanktalus]: Sure, no one is active for hours and hours in the cb, so I go to upgrade the db, and then someone is active. Sheesh :)
[Discipulus]: anyway poppins probably died with the last night cold. But is not normal to see them in dec. they must pop in April
NodeReaper eyes the thorns in the side
Discipulus : the party puller!
[Discipulus]: I was trying to solve this but i'm not. my regex-fu is stuck at primary school

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2017-12-18 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (498 votes). Check out past polls.

    Notices?