是否可以使用正则表达式在字符串中搜索碱基对?

Is it possible to search a string for base pairs using regular expressions?

我正在尝试使用正则表达式在字符串中的位置查找 RNA 碱基对(即 AU、CG、GC、UA、GU、UG)。我正在选择一个 Perl 程序,该程序将提取特定位置的字符并使用我提供的正则表达式查找匹配项。例如,给定字符串 AUGCCGU,我可以提取第一个和最后一个字符 AU,然后检查它们是否与我提供的正则表达式匹配。 旁注:我只能提供一个正则表达式,不能提供其他代码。

在提取 2 个字符后测试单个碱基对非常简单:(AU)|(CG)|(U[AG])|(G[CU])UA = matchUC = no match 等。但是,我想知道是否有一种合理的方法来测试 4 个(或更多)字符并寻找 1 个(或更多)碱基对匹配. 换句话说:给定 4 个提取的字符,查找字符 1&4 和 2&3 之间的对,如果找到 1 个或多个对,则报告匹配: ACGU = match(2 对 - AU,CG),ACCU = match(1 对,AU),ACUC = no match(0 对)。

如有任何建议,我们将不胜感激。我觉得可能需要反向引用和条件的组合,但我真的很难弄清楚如何在此处应用它们。或者这甚至可能吗?

您可以尝试在给定一组模式和位置​​的情况下生成正则表达式。例如:

use strict;
use warnings;
use Regexp::Assemble;

my @patterns = qw( AU CG UA UG GC GU );
my @match_pos = qw( 14 23 );
my $pat_size = 4;
my $regex = build_regex( \@patterns, \@match_pos, $pat_size );

sub build_regex {
    my ( $patterns, $match_pos, $size ) = @_;
    my $ra = Regexp::Assemble->new();

    for my $pos_str ( @$match_pos ) {
        my @pos = map { $_ - 1 } split //, $pos_str;
        for my $pat_short ( @$patterns ) {
            my @pat = ('.') x $pat_size;
            my @chars = split //, $pat_short;
            @pat[@pos] = @chars;
            my $pat = join '', @pat;
            $ra->add($pat);
        }
    }
    my $regex = $ra->re;
    return $regex;
}

此正则表达式将匹配位置 1 和 4 或位置 2 或 3 的所有模式。

您似乎只想获取两列并查看它们是否与您的匹配
正则表达式对。

没有必要在一个正则表达式中尝试完成所有这些。

这是更快的方法。使用 substr() 来创建目标,然后
用正则表达式测试它。

将所有输入和输出放在一个结构中。
在下面的示例中,这是 Que 哈希。

没必要这么复杂。

Perl 代码

use strict;
use warnings;


my $FullSequence = 'AUGCCGU';

my %Que = (

 # structure:
 #  item =   col pair  ,  results ( target , match )
    '1' => [  1,    7,     '', '' ],
    '2' => [  2,    1,     '', '' ],
    '3' => [  2,    3,     '', '' ],
    '4' => [  5,    3,     '', '' ],
    '5' => [  5,    2,     '', '' ],
    '6' => [  2,    3,     '', '' ],

      # simple overlap test
    '7a' => [ 1,    2,     '', '' ],
    '7b' => [ 2,    3,     '', '' ],
    '7c' => [ 3,    4,     '', '' ],
    '7d' => [ 4,    5,     '', '' ],
    '7e' => [ 5,    6,     '', '' ],
    '7f' => [ 6,    7,     '', '' ],
);

# Process Que
  for my $key (keys %Que )
  {
     # Get target pair at column locations
      my $target = substr( $FullSequence, $Que{$key}->[0] - 1, 1 ) . substr( $FullSequence, $Que{$key}->[1] - 1, 1 );
      $Que{$key}->[2] = $target; 

     # Get match result of target
      if ( $target =~ /(AU|CG|U[AG]|G[CU])/ ) {
          $Que{$key}->[3] = ;
          next;
      }
      $Que{$key}->[3] = 'no match';
  }

# Print Que result
  for my $key ( sort (keys %Que) )
  {
      print "item $key = ";
      print "cols (" . $Que{$key}->[0] . "," . $Que{$key}->[1] . ") ";
      print "result (" . $Que{$key}->[2]. ")  = " . $Que{$key}->[3] . "\n";

  }

输出

item 1 = cols (1,7) result (AU)  = AU
item 2 = cols (2,1) result (UA)  = UA
item 3 = cols (2,3) result (UG)  = UG
item 4 = cols (5,3) result (CG)  = CG
item 5 = cols (5,2) result (CU)  = no match
item 6 = cols (2,3) result (UG)  = UG
item 7a = cols (1,2) result (AU)  = AU
item 7b = cols (2,3) result (UG)  = UG
item 7c = cols (3,4) result (GC)  = GC
item 7d = cols (4,5) result (CC)  = no match
item 7e = cols (5,6) result (CG)  = CG
item 7f = cols (6,7) result (GU)  = GU