java - 在 Java 中查找简单序列重复的有效方法

标签 java regex bioinformatics dna-sequence

我必须找到简单的序列重复,并且必须存储每个唯一的重复及其位置。我已经编写了一个 Perl 代码来做到这一点(其中确实有大量的 if 和 for 来查找重复直到五聚体)。我的问题是,在java中是否有一些更简单的方法可以做到这一点,比如一些正则表达式或搜索字符串并返回连续重复和位置的计数的东西,这不涉及很多控制语句和迭代。

更新:简单序列重复(SSR)只是一个不间断重复的字符串,从二聚体(即两个不同的字符重复在一起)开始。就像一个词在一个句子中不间断地不断重复。如果是 DNA,它看起来像

AATTAAAATTTTAAAAAAAAGGGCCCTTTAA[ATATATATATATAT]AAGGGATTTAAGGAATTAAGA[TGATGATGATGATGA]TGGTAG

此处 AT 和 TGA 是简单序列重复,AT 是二聚体,TGA 是三聚体。 我必须找到的是序列重复的起始位置,它被重复多少次(即长度)以及它是哪个序列(即AT例如从位置6开始,它被重复10次,TGA是从位置 25 开始,依此类推)

我的 Perl 代码:(它有点错误)

my $i=0;
my $j=0;
my $dna;
my $m2=0;
my $m4=0;
my $m3=0;
my $m5=0;
my $temp1;
my $temp2;
my $min;

print "Please enter DNA sequence : ";
$dna=<>;
my $firstdi;
my $seconddi;
my $thirddi;
my $fourthdi;
my $fifthtet;
my $firsttet;
my $secondtet;
my $thirdtet;
my $fourthtet;
my $fifthtet;
my $firsttri;
my $secondtri;
my $thirdtri;
my $fourttri;
my $fifthtri;
my $firstpent;
my $secondpent;
my $thirdpent;
my $fourtpent;
my $fifthpent;
print "\n";
print "Please enter the length to search for : ";
$motif=<>;
print "\n";
print "Please enter the minimum number of motif repeats : " ;
$min=<>;
chomp($dna);
chomp($motif);
chomp($min);
my @output;
my @codearr = split //, $dna;
print "\n";print @codearr;print "\n";
my $arrsize=@codearr;
print "\nSize : ";
print "\n";print $arrsize; print "\n";
print "Output : ";
my $total=0;


if($motif==2)
{
for($i=0;$i<($arrsize-2);$i=$i+$motif)
{
    if($codearr[$i] ne $codearr[$i+1])
    {
  $temp1 = join( "",$codearr[$i],$codearr[$i+1]);
  $temp2 = join( "",$codearr[$i+2],$codearr[$i+3]);
   if($temp1 eq $temp2)
 {
 if($m2==0)
 {
 $ms1=$i;
 }
 $total++;
 $m2++;
 }
}
}
}



if($motif==3)
{
for($i=0;$i<($arrsize-2);$i=$i+$motif)
{if($codearr[$i] ne $codearr[$i+1])
    {
  $temp1 = join( "",$codearr[$i],$codearr[$i+1]);
  $temp2 = join( "",$codearr[$i+2],$codearr[$i+3]);
   if($temp1 eq $temp2)
 {
  if($m2==0)
 {
 $ms1=$i;
 }
 $m2++;
  $total++;
 }
}
}

for($i=0;$i<($arrsize-3);$i=$i+$motif)
{if($codearr[$i] ne $codearr[$i+1])
    {
$temp1 = join( "",$codearr[$i],$codearr[$i+1],$codearr[$i+2]);
$temp2 = join( "",$codearr[$i+3],$codearr[$i+4],$codearr[$i+5]);
 if($temp1 eq $temp2)
 {
  if($m3==0)
 {
 $ms3=$i;
 }
 $m3++;
  $total++;
 }
}
}
}



if($motif==4)
{
for($i=0;$i<($arrsize-2);$i=$i+$motif)
{
    if($codearr[$i] ne $codearr[$i+1])
    {
  $temp1 = join( "",$codearr[$i],$codearr[$i+1]);
  $temp2 = join( "",$codearr[$i+2],$codearr[$i+3]);
   if($temp1 eq $temp2)
 {
  if($m2==0)
 {
 $ms1=$i;
 }
 $m2++;
  $total++;
 }
}
}
for($i=0;$i<($arrsize-3);$i=$i+$motif)
{
    if($codearr[$i] ne $codearr[$i+1])
    {
$temp1 = join( "",$codearr[$i],$codearr[$i+1],$codearr[$i+2]);
$temp2 = join( "",$codearr[$i+3],$codearr[$i+4],$codearr[$i+5]);
 if($temp1 eq $temp2)
 {
  if($m3==0)
 {
 $ms3=$i;
 }
 $m3++;
  $total++;
 }
}
}
for($i=0;$i<($arrsize-4);$i=$i+$motif)
{
    if($codearr[$i] ne $codearr[$i+1])
    {
$temp1 = join( "",$codearr[$i],$codearr[$i+1],$codearr[$i+2],$codearr[$i+3]);
$temp2 = join( "",$codearr[$i+4],$codearr[$i+5],$codearr[$i+6],$codearr[$i+7]);
 if($temp1 eq $temp2)
 {
  if($m4==0)
 {
 $ms4=$i;
 }
 $m4++;
  $total++;
 }
}
}
}



if($motif==5)
{
for($i=0;$i<($arrsize-2);$i=$i+$motif)
{if($codearr[$i] ne $codearr[$i+1])
    {
  $temp1 = join( "",$codearr[$i],$codearr[$i+1]);
  $temp2 = join( "",$codearr[$i+2],$codearr[$i+3]);
   if($temp1 eq $temp2)
 {
  if($m2==0)
 {
 $ms1=$i;
 }
  $total++;
 $m2++;
 }
}  }
for($i=0;$i<($arrsize-3);$i=$i+$motif)
{
    if($codearr[$i] ne $codearr[$i+1])
    {
$temp1 = join( "",$codearr[$i],$codearr[$i+1],$codearr[$i+2]);
$temp2 = join( "",$codearr[$i+3],$codearr[$i+4],$codearr[$i+5]);
 if($temp1 eq $temp2)
 {
  if($m3==0)
 {
 $ms3=$i;
 }
  $total++;
 $m3++;
 }
}
}
for($i=0;$i<($arrsize-4);$i=$i+$motif)
{
    if($codearr[$i] ne $codearr[$i+1])
    {
$temp1 = join( "",$codearr[$i],$codearr[$i+1],$codearr[$i+2],$codearr[$i+3]);
$temp2 = join( "",$codearr[$i+4],$codearr[$i+5],$codearr[$i+6],$codearr[$i+7]);
 if($temp1 eq $temp2)
 {
  if($m4==0)
 {
 $ms4=$i;
 }
  $total++;
 $m4++;
 }
}
}
for($i=0;$i<($arrsize-5);$i=$i+$motif)
{if($codearr[$i] ne $codearr[$i+1])
    {
 $temp1 = join( "",$codearr[$i],$codearr[$i+1],$codearr[$i+2],$codearr[$i+3],$codearr[$i+4]);
 $temp2 = join( "",$codearr[$i+5],$codearr[$i+6],$codearr[$i+7],$codearr[$i+8],$codearr[$i+9]);
 if($temp1 eq $temp2)
 {
  if($m5==0)
 {
 $ms5=$i;
 }
  $total++;
 $m5++;
 }
}
}
}


if($motif==2)
{
  if($min<$total)
  {
print"Number of Dimer repeats : ";
    print $m2;
    print"\n";
    print"First position : ";
    print $ms1;
    print "\n";
    print "Sequence Lenght : ";
    print $arrsize;
    print "\n";
}
else
  {
  print "No or less than minimum SSRs found";}
}

if($motif==3)
{
  if($min<$total)
  {
print"Number of Dimer repeats : ";
    print $m2;
    print"\n";
    print"First position : ";
    print $ms1;
    print "\n";
    print "Sequence Lenght : ";
    print $arrsize;
    print "\n";
print"Number of Trimer repeats : ";
    print $m3;
    print"\n";
    print"First position : ";
    print $ms3;
    print "\n";
    print "Sequence Lenght : ";
    print $arrsize;
    print "\n";
}
else
  {
  print "No or less than minimum SSRs found";}
}

if($motif==4)
{
  if($min<$total)
  {
print"Number of Dimer repeats : ";
    print $m2;
    print"\n";
    print"First position : ";
    print $ms1;
    print "\n";
    print "Sequence Lenght : ";
    print $arrsize;
    print "\n";
print"Number of Trimer repeats : ";
    print $m3;
    print"\n";
    print"First position : ";
    print $ms3;
    print "\n";
    print "Sequence Lenght : ";
    print $arrsize;
    print "\n";
print"Number of Tetramer repeats : ";
    print $m4;
    print"\n";
    print"First position : ";
    print $ms4;
    print "\n";
    print "Sequence Lenght : ";
    print $arrsize;
    print "\n";
}
else
  {
  print "No or less than minimum SSRs found";}
}


if($motif==5)
{
  if($min<$total)
    {
print"Number of Dimer repeats : ";
    print $m2;
    print"\n";
    print"First position : ";
    print $ms1;
    print "\n";
    print "Sequence Lenght : ";
    print $arrsize;
    print "\n";
print"Number of Trimer repeats : ";
    print $m3;
    print"\n";
    print"First position : ";
    print $ms3;
    print "\n";
    print "Sequence Lenght : ";
    print $arrsize;
    print "\n";
print"Number of Tetramer repeats : ";
    print $m4;
    print"\n";
    print"First position : ";
    print $ms4;
    print "\n";
    print "Sequence Lenght : ";
    print $arrsize;
    print "\n";
print"Number of Pentamer repeats : ";
    print $m5;
    print"\n";
    print"First position : ";
    print $ms5;
    print "\n";
    print "Sequence Lenght : ";
    print $arrsize;
    print "\n";
  }
  else
  {
  print "No or less than minimum SSRs found";}
}

最佳答案

像正则表达式这样的东西应该可以帮助您开始这适用于您在问题中提到的“二聚体”,并且可以扩展以查找更长的序列。

String s = "AATTAAAATTTTAAAAAAAAGGGCCCTTTAAATATATATATATATAAGGGATTTAAGGAATTAAGATGATGATGATGATGATGGTAG";
Pattern pattern = Pattern.compile("([ATGC][ATGC])\\1+");
Matcher matcher = pattern.matcher(s);

while (matcher.find()) {
    System.out.print("Start index: " + matcher.start());
    System.out.print(" End index: " + matcher.end());
    System.out.println(" Found: " + matcher.group());
}

这给出了输出:

Start index: 4 End index: 8 Found: AAAA
Start index: 8 End index: 12 Found: TTTT
Start index: 12 End index: 20 Found: AAAAAAAA
Start index: 31 End index: 45 Found: ATATATATATATAT

词法分析库实际上可能会帮助您,因为您可以从这些正则表达式构建状态机,这有助于识别更复杂的模式。看看JLex .

<小时/>

编辑: 你说AA不算模式,它们必须是不同的字符。 您可以尝试this regex相反:

Pattern pattern = Pattern.compile("(?:([ATGC])(?!\\1)([ATGC])\\1\\2)+");

这有一个先行断言,确保它不匹配相同的字符。

关于java - 在 Java 中查找简单序列重复的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23557497/

相关文章:

python - 为什么我得到 BioPython HTTPError : HTTP Error 400: Bad Request when I use Esearch and Efetch

java - SoapFaultException - 提取代码和文本

python - 使用正则表达式排除引号内的案例

java - 我如何解析具有大量 Java 变体的人工格式化和键入的文本表,如果正则表达式是如何正确获取行值的答案?

javascript - 正则表达式,jquery问题

python - 如何在Python中获取序列中非共享插入和间隙的数量?

java - 或replaceAll中的条件

java - 从java应用程序调用acceleo模板

Java 8 流结合了两个不同的集合

bioinformatics - multiFASTA 文件处理