in reply to Count the occurance of a words in a string

What do you think my @words = qw ($chromosomefilename); does? I am pretty sure it is not doing what you think it should do.

The following contains a subroutine that takes a filename and a sequence to search for and returns the number of matches found.

use Modern::Perl; use autodie; sub count_seq { my ($file, $search_seq) = @_; my $sequence; { local $/; # slurp mode open my $fh, '<', $file; $sequence = <$fh>; } $sequence =~ s/[\s\n]//g; return scalar (()=$sequence=~ m/$search_seq/g); } say count_seq('test.file', 'TA');
The subroutine builds one big string and eliminates all spaces and EOL characters. I am not sure that is a good idea, but I guess it will depend on your fileformat. By putting everything in one big string you could inadvertently construct not existing sequences. Suppose one sequence ends with "AT" and the next one starts with "CG". If you are looking for "ATCG", you will now find this combination in the end-of-a-sequence + beginning-of-another-sequence combination.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics