This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Perl Pattern Matching Compare 2 Strings

I have 2 strings as follows: $a='ccgctaccgcgatac' and $b='ccgatacgcatcacata'

I have to get all the common substring of a determined length $c

i.e if $c =3 results is {ccg,cgc,ata,tac...}

 while($a=~/([actg][actg][actg])/g)
      {
         if($b=~/($1)/)
           { 
            print $1."\n"; 
           }
      }

the result : ccg ccg cga tac it didnt give back all the matched substrings of length 3 because it take 3 by 3 characters in $a and do the matches based on each 3 I need to take all matches taking the overlaping into consideration

perl

what have you tried? how long are your strings?

there is a solution on stackoverflow. I am not sure to put that link, as this feels like a homework question.

strings are extracted from fasta files about 1500 characters. I have no idea

Strings are extracted from fasta file about 1500 character i've tried this as example when $c=3 it didnt give me all matched substrings. while ($a=~/([actg][actg][actg])/g){if($b=~/($1)/){print $1."n"; }}

It's not clear what you want to do or why you want to do it. Are the substrings of the string overlapping? For example from $a, are you interested in ccg, cgc, gct etc.?

You can use a sliding window of 3 bases across the first string. Save each 3 base combination in a dictionary so you have redundancy. Same sliding window for the second string, check each window against the dictionary. If it exist in the dictionary, print it.

You can use a sliding window of 3 bases across the first string. Save each 3 base combination in a dictionary so you don't have redundancy. Do the same sliding window for the second string, check each window against the dictionary. If it exist in the dictionary, print it.

You can use a sliding window of 3 bases across the first string. Save each 3 base combination in a dictionary so you don't have redundancy. Do the same sliding window for the second string, check each window against the dictionary. If it exist in the dictionary, print it.

Don't you know stackoverflow is more appropriate for such questions?

Decided to close this. It's a basic Perl programming question, not a bioinformatics problem.

2 answers

perl -e 'my $a="ccgctaccgcgatac";$b="ccgatacgcatcacata";foreach (my $i=0;$i<length($a)-3;$i+=1){my $c=substr($a,$i,3);if ($b=~/$c/){print "$cn";}}'

if you need non-redundant elements:

perl -e 'my $a="ccgctaccgcgatac";$b="ccgatacgcatcacata";foreach (my $i=0;$i<length($a)-3;$i+=1){my $c=substr($a,$i,3);if ($b=~/$c/){print "$c\n";}}' |sort -u

or

perl -e 'my %d=();my $a="ccgctaccgcgatac";$b="ccgatacgcatcacata";foreach (my $i=0;$i<length($a)-3;$i+=1){my $c=substr($a,$i,3);if ($b=~/$c/){$d{$c}++;}}foreach (keys %d){print "$_\n"}'

the answer on stackoverflow: solution

Log in to answer this question.