自己动手,丰衣足食。
看不懂 的留言,讨论!
#!/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