用Perl语言写一个脚本实现滑动窗口(比如窗口100格每次滑动1格)统计一段DNA序列中的CG含量

如题,应该说的很明白了

use strict;

# $win_width: 窗口的宽度
# $pos: 当前窗口的位置,从1开始,$DNA_len-$win_width+1结束
# $DNA: DNA序列

my $DNA="agtgatgatagtagatagatagtataatagatagatagatatatgatagatagataaggaatagaagta";
my $win_width=10;

for(my $i=0;$i<length($DNA)-$win_width+1;$i++){
    print calc($DNA,$win_width,$i+1)."\n";
}

sub calc{
    my ($DNA,$win_width,$pos)=@_;
    $DNA=~s/\s+//g;  
    $DNA=uc($DNA);
    if($DNA=~/[^ATGC]/){
        print "非法字符";
        return;
    }
    my $sub_DNA=substr($DNA,$pos-1,$win_width);
    my @atgc=split("",$sub_DNA);
    my %atgc;  #atgc含量
    map{$atgc{$_}++}@atgc;
    return sprintf("%.2f",($atgc{"G"}+$atgc{"C"})/$win_width);
}

温馨提示:答案为网友推荐,仅供参考
相似回答