The regex also has to match /N+/ sequences, so that is added as a special case (Update: Adding "NN" to @allowed would also have the same effect).my @allowed = qw[ AA AG GC GT CA CG TT TC ]; my $allowed = join "|", @allowed; my $regex = qr/ N+ | (?: (?=$allowed) . )* . /x; my $data = "CTGTCAGCNNNCCGGTTTTCAAGNNGAGCACACACCAAAAATGCACCAAAGCTTNACA +TCCATACAAA"; print "$_\n" for $data =~ m/$regex/g;
This doesn't work for multi-line data. You can either first remove all the newlines from the data, or if you don't want to have the whole file in memory at once, you can do this "inching along" process manually. Take one character at a time, keeping track of the last one you've seen as well. If the last two characters are an allowed sequence, then add the new character to a buffer. If the last two characters are disallowed, then print the buffer (it is a maximal allowed string), and restart the buffer starting with this new character.
Update: Something like this:
my @allowed = qw[ AA AG GC GT CA CG TT TC NN ]; my %allowed = map { $_ => 1 } @allowed; my $buf; while ( get next input character as $c ) { if ($allowed{ substr($buf,-1).$c }) { $buf .= $c; } else { print "$buf\n"; $buf = $c; } } print "$buf\n"; # don't forget last one
blokhead
In reply to Re: Extracting a substring according to some criteria
by blokhead
in thread Extracting a substring according to some criteria
by Benson
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |