The following contains a subroutine that takes a filename and a sequence to search for and returns the number of matches found.
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.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');
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 DeltronicsIn reply to Re: Count the occurance of a words in a string
by CountZero
in thread Count the occurance of a words in a string
by Pratyusha Reddy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |