实用便捷小程序--由从gff文件提取exon/intron坐标信息

自己动手,丰衣足食。
看不懂 的留言,讨论!

#!/usr/bin/perl -w
use strict;
use Data::Dumper;

die "Usage: perl $0 <file.gff> \n" if @ARGV < 1;

my (%strand, %exon, %hash);
open IN, $ARGV[0] || die $!;
while(<IN>){
    chomp;
    next if /^$|^#/;
    my @a = split /\t/, $_;
    if ($a[2] =~ /CDS/){
        my ($id) = $a[8] =~ /Parent=(\S+);/;
#        $strand{$a[0]}{$id} = $a[6];
#        push @{$exon{$id}}, $a[3]."-".$a[4];
        $hash{$id}{chrom} = $a[0];
        $hash{$id}{strand} = $a[6];

        push @{$exon{$id}}, @a[3,4];
    }else{
        next;
    }
}
close IN;
# print Dumper %exon;

foreach my $k (sort keys %hash){
    my $chrom = $hash{$k}{chrom};
    my $strand = $hash{$k}{strand};

    my @pos = sort {$a<=>$b} @{$exon{$k}};
    if ( @pos > 2 ){
       print join("\t", $chrom, $pos[0], $pos[1], $strand, "exon" )."\n";

       for (my $i=1;$i<@pos-2;$i+=2){
           my $in_ss = $pos[$i]+1;
           my $in_ed = $pos[$i+1]-1;

           print join("\t", $chrom, $in_ss, $in_ed, $strand, "intron" )."\n";
           print join("\t", $chrom, $pos[$i+1], $pos[$i+2], $strand, "exon")."\n";
        } 
    }else{
        print join("\t", $chrom, $pos[0], $pos[1], $strand, "exon" )."\n";
    }
}

__END__
Author: liupeng@genebang.com
Date : 2017-08-07
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 你藏不住的忧伤 是我来不及的彷徨
    千约NicE阅读 485评论 0 0
  • 概念 AOP(Aspect Oriented Programming)简而言之就是面向切面编程。它所要实现的目标就...
    seph_von阅读 788评论 0 0
  • 在大城市生活,离不开乘地铁。于是乎,也就产生了这门艺术。 据我个人观察、体验及推断,主要有听、看、感三种境界。 听...
    朱小虎XiaohuZhu阅读 346评论 0 1