0

大家好,我有一些来自 blat 工具的数据,它给出了对齐的输出,如下所示:

contig30
chromosome 1
000000001 gctctgc.tctggggacgctcgcagcgctcggcgcctggcccag 000000043
>>>>>>>>> ||||||| |||||||||||||||||||||||||||||||||||| >>>>>>>>>
123368567 gctctgcatctggggacgctcgcagcgctcggcgcctggcccag 123368610

000000044 tttctttgacaatgtctaccgttcatgaaattctgtgcaagctcagcttg 000000093
>>>>>>>>> |||||||||||||||||||||||||||||||||||||||||||||||||| >>>>>>>>>
123380141 tttctttgacaatgtctaccgttcatgaaattctgtgcaagctcagcttg 123380190

contig35
chromosome 1
000000001 gctctgc.tctggggacgctcgcagcgctcggcgcctggcccag 000000043
>>>>>>>>> ||||||| |||||||||||||||||||||||||||||||||||| >>>>>>>>>
123368567 gctctgcatctggggacgctcgcagcgctcggcgcctggcccag 123368610

我有这个数据的文本文件。

我想要做的是按以下方式打印输出:

contig 30 chromosome 1 000000001-123368567
contig 30 chromosome 1 000000002-123368568
contig 30 chromosome 1 000000003-123368569

 -
 -
 upto
 contig 30 chromosome 1 000000093-123380190

和下一个条目类似。我的输入文本文件中有多个此类条目。

4

2 回答 2

1

你可以寻找这样的东西:

#!/usr/bin/env perl

use strict;
use warnings;
use utf8;

my $content = do {
    local $/;
    <DATA>
};

while (
    $content =~ /   
                        (contig)(30)\n
                        (chromosome\ 1)\n
                        (\d+).*\n
                        .*\n
                        (\d+).*\n

                    /gmx
  )
{
    print $1, " ", $2, " ", $3, " ", $4, "-", $5, "\n";
}

__DATA__
contig30
chromosome 1
000000001 gctctgc.tctggggacgctcgcagcgctcggcgcctggcccag 000000043
>>>>>>>>> ||||||| |||||||||||||||||||||||||||||||||||| >>>>>>>>>
123368567 gctctgcatctggggacgctcgcagcgctcggcgcctggcccag 123368610

000000044 tttctttgacaatgtctaccgttcatgaaattctgtgcaagctcagcttg 000000093
>>>>>>>>> |||||||||||||||||||||||||||||||||||||||||||||||||| >>>>>>>>>
123380141 tttctttgacaatgtctaccgttcatgaaattctgtgcaagctcagcttg 123380190

contig35
chromosome 1
000000001 gctctgc.tctggggacgctcgcagcgctcggcgcctggcccag 000000043
>>>>>>>>> ||||||| |||||||||||||||||||||||||||||||||||| >>>>>>>>>
123368567 gctctgcatctggggacgctcgcagcgctcggcgcctggcccag 123368610

contig30
chromosome 1
000000002 gctctgc.tctggggacgctcgcagcgctcggcgcctggcccag 000000043
>>>>>>>>> ||||||| |||||||||||||||||||||||||||||||||||| >>>>>>>>>
123368568 gctctgcatctggggacgctcgcagcgctcggcgcctggcccag 123368610

这里重要的是,它DATA$content. 这意味着文件的全部内容被存储到$content包括所有换行符等。为了

将文件镜像到变量中后,您可以对其执行多行搜索。为了通知 perl 这样做,您必须将m修饰符添加到正则表达式(添加 x 修饰符是为了通过描述模式的底层结构来提高正则表达式的易读性)。

于 2013-08-02T08:46:35.687 回答
0

这似乎可以完成这项工作:

#!/usr/bin/env perl
use strict;
use warnings;

my $contig;
my $chromo;

while (<>)
{
    chomp;
    if (/^contig(\d+)/)
    {
        $contig = $1;
    }
    elsif (/^chromosome (\d+)/)
    {
        $chromo = $1;
    }
    elsif (/^(\d+) [acgt.]+ (\d+)/)
    {
        my $b1 = $1;
        my $e1 = $2;
        my $junk = <>;
        my $line = <>;
        next unless $junk =~ m/^[<>]+ [ |]+ [<>]+$/; # See other question
        my($b2, $e2) = $line =~ m/^(\d+) [acgt.]+ (\d+)/;
        for (my $i = 0; $i < $e1 - $b1; $i++)
        {
            printf "contig %d chromosome %d %.9d-%.9d\n", $contig, $chromo, $b1+$i, $b2+$i;
        }
    }
    # else blank?  Ignore it, anyway.
}

另一个问题中的数据<不是>在行的开头,所以我使用该模式[<>]来匹配填充行的开头和结尾。本题中间部分也有空格。

样本输出:

contig 30 chromosome 1 000000001-123368567
contig 30 chromosome 1 000000002-123368568
contig 30 chromosome 1 000000003-123368569
...
contig 30 chromosome 1 000000040-123368606
contig 30 chromosome 1 000000041-123368607
contig 30 chromosome 1 000000042-123368608
contig 30 chromosome 1 000000044-123380141
contig 30 chromosome 1 000000045-123380142
contig 30 chromosome 1 000000046-123380143
...
contig 35 chromosome 1 000000001-123368567
contig 35 chromosome 1 000000002-123368568
contig 35 chromosome 1 000000003-123368569
...
contig 35 chromosome 1 000000040-123368606
contig 35 chromosome 1 000000041-123368607
contig 35 chromosome 1 000000042-123368608
于 2013-08-02T09:32:23.190 回答