in reply to Re: input record separator help
in thread input record separator help

thanks for your help Schiller, but I dont get how I can perform my various calculations on each sequence using your code. How can I push each extracted $dna into an array, which I can then iterate through for the calculations? This is the sort of thing I want to do for each sequence:
for (my $i=0; $i < @genome; $i++) { my $token = $genome[$i] . $genome[$i+1]; $freq{$token}++; $tt = $freq{tt}; $cg = $freq{cg}; $gc = $freq{gc}; $ta = $freq{ta}; $at = $freq{at}; $cc = $freq{cc}; $tg = $freq{tg}; $ag = $freq{ag}; $ac = $freq{ac}; $ga = $freq{ga}; $aa = $freq{aa}; $gg = $freq{gg}; $ca = $freq{ca}; $ct = $freq{ct}; $gt = $freq{gt}; $tc = $freq{tc}; }
I have tried just using a simple 'push', but this slowed the program down far too much. cheers x

Replies are listed 'Best First'.
Re: Re: Re: input record separator help
by Limbic~Region (Chancellor) on Mar 15, 2004 at 14:53 UTC
    Anonymous Monk,
    So my earlier assumption that you wanted to throw away non sequence lines was correct. I believe you can avoid an array all together.
    #!/usr/bin/perl use strict; use warnings; my $file = $ARGV[0] || 'default'; open (DNA, '<', $file) or die "Unable to open $file for reading : $!"; my %seq; while ( <DNA> ) { my $dna = <DNA>; my $pairs = "a2" x ((length $dna) / 2); $seq{$_}++ for unpack $pairs , $dna; } print "$_ : $seq{$_}\n" for keys %seq;
    Cheers - L~R
      hi L~R, thankyou for your help, would you mind exaplaining to me exactly what the code does? e.g. the 'a2' and 'unpack' bits? many thanks
        Anonymous Monk,
        You can check out the pack/unpack tutorial for more information, but here was my logic.
        my $pairs = "a2" x ((length $dna)/2); # The x operator (perldoc perlop) repeats LHS string times the RHS $seq{$_}++ for unpack $pairs, $dna; # Break string up into a list of 2 char strings # Increment the corresponding hash value by 1
        Cheers - L~R