Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 是否可以使用正则表达式在字符串中搜索碱基对?_Regex_Perl_Bioinformatics - Fatal编程技术网

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

Regex 是否可以使用正则表达式在字符串中搜索碱基对?,regex,perl,bioinformatics,Regex,Perl,Bioinformatics,我试图使用正则表达式在字符串中的位置查找RNA碱基对(即AU、CG、GC、UA、GU、UG)。我选择了一个Perl程序,它将提取特定位置的字符,并使用我提供的正则表达式查找匹配项。例如,给定字符串augcgu,我可以提取第一个和最后一个字符AU,然后检查它们是否匹配我提供的正则表达式。 旁注:我只能提供一个正则表达式,没有其他代码 提取2个字符后测试单个碱基对非常简单:(AU)|(CG)|(U[AG])|(G[CU])UA=match,UC=no match,等等。但是,我想知道是否有一种合理的

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

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


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

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

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;
}

此regexp将匹配位置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] = $1;
          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      

我所说的站点只是指字符串中的一个给定位置。我告诉程序在不同的位置提取字符串中的字符,形成一个新的字符串。程序测试新字符串是否与正则表达式匹配。我已经更新了问题(希望)以提高清晰度。@sln我想这是我问题的一部分。如果有一种合理的方法来编写一个正则表达式来完成这项任务,那么我就不用从头开始编写脚本了(但我并不完全反对这样做)。我只是想看看这是否必要。在某种程度上,我通常只是好奇这是否是正则表达式可以实现的,或者这是否超出了正则表达式的能力范围。“我只能提供一个正则表达式,而没有其他代码”如果您有要匹配的字符串,为什么不可以按您的意愿将其拆分?请解释一下,我的意思是这很简单,不是吗。字符串中必须包含偶数字符:
12345678
。坐在一个环中,向中间剥离两端,
if($str=$a.$b)=~/(AU | CG | U[AG]| G[CU])/{print“在$loopvariable\r\n找到$1”}
@HåkonHæ。我可以向程序指出我感兴趣的职位,并给它一个正则表达式。然后程序逐行提取这些位置的字符。我假设用它创建一个新字符串,检查是否与正则表达式匹配,如果匹配发生,它将保留序列,然后移到下一行。所以我可以给它提供一个正则表达式,告诉它检查什么,但仅此而已。这将是一些工作,但最好的解决方案可能只是从头开始编写一个脚本,这样我就有了更多的功能。这让我大吃一惊。我甚至从未考虑过编写一个脚本来输出正则表达式的可能性。好主意!