2

向全世界的 Perl 大师致敬!

我有一个像这样的文件要解析并想要制作......

从第一列开始,ID、外显子信息、起始位置、结束位置和方向。ID 遇到一个数时加 1。

1   9239    712 8571    +
1   start_codon 712 714 +
1   stop_codon  8569    8571    +
2   3882    24137   24264   +
2   start_codon 24137   24139   +
3   3882    24322   24391   +
4   3882    24490   26064   +
4   stop_codon  26062   26064   +
5   4972    26704   26740   +
5   start_codon 26704   26706   +
6   4972    26814   27170   +
7   4972    27257   27978   +
7   stop_codon  27976   27978   +
8   10048   40161   41114   -
8   start_codon 41112   41114   -
8   stop_codon  40161   40163   -
9   272 43167   43629   -
9   stop_codon  43167   43169   -
10  272 43755   44059   -
10  start_codon 44057   44059   -

像这样 ....

1   9239    *712*   *8571*  +
1   start_codon 712 714 +
1   stop_codon  8569    8571    +
*X  9239    712 8571    +*
2   3882    *24137* 24264   +
2   start_codon 24137   24139   +
3   3882    24322   24391   +
4   3882    24490   *26064* +
4   stop_codon  26062   26064   +
*X  3882    24173   26064   +*
5   4972    *26704* 26740   +
5   start_codon 26704   26706   +
6   4972    26814   27170   +
7   4972    27257   *27978* +
7   stop_codon  27976   27978   +
*X  4972    26704   27978 +*
8   10048   *40161* *41114* -
8   start_codon 41112   41114   -
8   stop_codon  40161   40163   -
*X  10048   40161   41114   -*
9   272 *43167* 43629   -
9   stop_codon  43167   43169   -
10  272 43755   *44059* -
10  start_codon 44057   44059   -
*X  272 43167   44059   -*

必须添加以 X 开头的每一行,但以我的技能我不能... :(

问题是对于第二列中的每个外显子编号,忽略“start_codon”和“end_codon”,必须在星号 * 之间获得最小编号的外显子位置和最大编号的外显子位置。

这是我解析数据的基本代码......但我想,必须从头开始重新编码(我不知道如何插入行'X')

(对不起,我删除了代码,因为它不够好,可能会造成混乱......)

世界上的Perl大师,你能帮帮我吗???

谢谢!!

正如 TLP 所说,我把我的代码放回去了。虽然它的令人尴尬的代码

use strict;

if (@ARGV != 1) {
    print "Invalid arguments\n";
    print "Usage: perl min_max.pl [exon_output_file]\n";
    exit(0);
}

my $FILENAME = $ARGV[0];
    my  $exonid = 0;
    my  $exon = "";
    my  $startpos = 0;
    my  $endpos = 0;
    my  $strand = "";
    my  $min_pos = 0;
    my  $max_pos = 0;

open (DATA, $FILENAME);

while (my $line = <DATA>) {
    chomp $line;

    if ($line ne "") {
        if ($line =~ /^(.+)\t(.+)\t(.+)\t(.+)\t(.+)/) {
        $exonid = $1;
        $exon = $2;
        $startpos = $3;
        $endpos = $4;
        $strand = $5;
        }
        if ($exon =~ /\d+/) {
            print $exonid,"\t",$exon,"\t",$startpos,"\t",$endpos,"\t",$strand,"\n";
        } else {
            print $exonid,"\t",$exon,"\t",$startpos,"\t",$endpos,"\t",$strand,"\n";
        }
    }
}

close (DATA);
exit;

如何比较最大值和最小值....

4

2 回答 2

3

基本上你所做的就是遍历这些行,跳过你不想要的那些(即第 2 列中没有数字),记住同一组中每个新行的最小/最大值,以及当第 2 列数字更改时打印和重来。使用此解决方案,您还必须在最后手动打印最后一组。

此代码将内部DATA文件句柄用于演示数据。只需更改<DATA><>在目标输入文件上使用,如下所示:perl script.pl inputfile

use strict;
use warnings;
use List::Util qw(min max);

my $print;
my ($min, $max, $id);
while (<DATA>) {                   ###### change to <> to run on input file
    my @line = split;
    if ($line[1] !~ /^\d+$/) {                # if non-numbers in col 2
         print;                               # print line
         next;                                # skip to next line
    }
    if (!defined($id) or $id != $line[1]) {   # New dataset!
        say $print if $print;                 # Print and reset 
        $id = $line[1];
        $min = $max = undef;
    }
    $min = min($min // (), @line[2,3]);       # find min/max, skip undef
    $max = max($max // (), @line[2,3]);
    $print = join "\t", "X", $line[1], $min, $max;  # buffer the print
}
print $print;

__DATA__
1   9239    712 8571    +
1   start_codon 712 714 +
1   stop_codon  8569    8571    +
2   3882    24137   24264   +
2   start_codon 24137   24139   +
3   3882    24322   24391   +
4   3882    24490   26064   +
4   stop_codon  26062   26064   +
5   4972    26704   26740   +
5   start_codon 26704   26706   +
6   4972    26814   27170   +
7   4972    27257   27978   +
7   stop_codon  27976   27978   +
8   10048   40161   41114   -
8   start_codon 41112   41114   -
8   stop_codon  40161   40163   -
9   272 43167   43629   -
9   stop_codon  43167   43169   -
10  272 43755   44059   -
10  start_codon 44057   44059   -

输出:

9239    712     8571
3882    24137   26064
4972    26704   27978
10048   40161   41114
272     43167   44059
于 2012-12-26T13:09:56.043 回答
2

如果我理解正确,这里有一种方法(未经测试!)来做你似乎要求的事情:

use strict;
use warnings;
use feature 'say';

# read first line, initialize accumulators, print it back
chomp($_ = <>);
my ($last_id, $last_exon, $min_start, $max_end, $last_strand) = split /\t/;
say $_;

# loop over remaining lines
while (<>) {
    chomp;
    my ($exonid, $exon, $startpos, $endpos, $strand) = split /\t/;

    if ($exon !~ /\D/ and $exon != $last_exon) {
        # new exon found, print summary of last one...
        say join "\t", "X", $last_exon, $min_start, $max_end, $last_strand;
        # ...and reset accumulators
        ($last_id, $last_exon, $min_start, $max_end, $last_strand)
            = ($exonid, $exon, $startpos, $endpos, $strand);
    }
    else {
        # previous exon continues, just update accumulators
        $last_id     = $exonid;
        $last_exon   = $exon     if $exon !~ /\D/;
        $min_start   = $startpos if $min_start > $startpos;
        $max_end     = $endpos   if $max_end < $endpos;
        $last_strand = $strand;  # should not really be needed
    }
    # ...and don't forget to print the original line back again
    say $_;
}
# end of file, print summary of last exon
print join("\t", "X", $last_exon, $min_start, $max_end, $last_strand), "\n";

基本上,我假设您想要打印一个摘要行,X每当您在第二列中遇到一个与该列中的前一个数字不同的数字时,并且第二列中具有非数字值的行不应该触发总结。此外,您可能还希望在文件末尾有一个摘要行。

如果仅包含数字,则表达式$exon !~ /\D/返回 true 。$exon(具体来说,它测试它是否不包含数字字符,因此空字符串也会匹配。)

有很多边缘情况我没有考虑过,因为我不知道它们是否可能出现在您的数据中,以及如果确实发生了您希望如何处理它们。例如,为了小心,人们可能还希望打印一个摘要,以防万一链发生变化而外显子数保持不变。同样,细心的程序员可能想要考虑输入文件为空的可能性,或者第一行在第二列中包含非数字值的可能性。

至少use warnings如果我假设的任何值总是数字的结果不是这样,你会被提醒。

于 2012-12-26T12:34:10.453 回答