-
Notifications
You must be signed in to change notification settings - Fork 58
/
cleanup_duplicate_genewise.pl
160 lines (157 loc) · 3.96 KB
/
cleanup_duplicate_genewise.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use Bio::Range;
my $version_out = 2;
my $debug = 0;
my $score = 10;
my $minlen = 50;
# assumes GFF3 input
GetOptions(
'gffv|gff:s' => \$version_out,
'v|debug!' => \$debug,
's|score:s' => \$score,
'm|minlen:i' => \$minlen,
);
if( $version_out == 3 ) {
print "##gff-version 3\n";
}
my %genes;
my $gene;
while(<>) {
next if /^\#/;
chomp;
my @row = split(/\t/,$_);
my %group = map { split( /=/,$_) } split(/;/,pop @row);
my $name;
if( $row[2] eq 'gene' ) {
push @{$genes{$gene->{'Name'}}}, $gene if $gene;
$gene = undef;
} elsif( $row[2] eq 'mRNA' ) {
$name = $group{'ID'};
$gene = { 'Name' => $name,
'mRNA' => [@row],
'CDS'=> [],
};
} elsif( $row[2] eq 'CDS' ) {
push @{$gene->{'CDS'}}, [@row,$group{'Target'}];
}
}
# fencepost
push @{$genes{$gene->{'Name'}}}, $gene if $gene;
my %unique;
for my $gene ( sort keys %genes ) {
warn("gene is $gene\n") if $debug;
my %chrom;
for my $g ( @{$genes{$gene}} ) {
my $seqid = $g->{'mRNA'}->[0];
my $start = $g->{'mRNA'}->[3];
my $end = $g->{'mRNA'}->[4];
my $range = Bio::Range->new(-start => $start,
-end => $end);
# Technically this range includes introns, but let's not worry
# about it here, if there is a splice-site supported that is probably
# okay information to include
next if $range->length < $minlen;
push @{$chrom{$seqid}}, [$range,$g];
}
for my $chrom ( keys %chrom ) {
my @sets = @{$chrom{$chrom}};
if( @sets > 1 ) {
if( $debug ) {
warn "BEFORE:\n";
for my $s ( @sets ) {
my $s = $s->[1];
my $name = $s->{'Name'};
if( $version_out == 2 ) {
warn join("\t", @{$s->{'mRNA'}},
sprintf("GenePrediction %s",
$name)),"\n";
} else {
my @r = @{$s->{'mRNA'}};
$r[2] = 'gene';
warn join("\t",@r ,
sprintf("ID=%s.gene;Name=%s.gene",
$name,$name)),"\n";
warn join("\t", @{$s->{'mRNA'}},
sprintf("ID=%s;Parent=%s.gene;Name=%s",
$name,$name,$name)),"\n";
}
for my $cds ( @{$s->{'CDS'}} ) {
my @cds_i = @$cds;
my $target = pop @cds_i;
if( $version_out == 2) {
warn join("\t", @cds_i,
sprintf("GenePrediction %s ; Target \"%s\"",
$name,$target)),"\n";
} else {
warn join("\t", @cds_i,
sprintf("Parent=%s;Target=%s",
$name,$target)),"\n";
}
}
}
}
# check for overlaps
# take longest one
my $changes = 1;
while( $changes ) {
$changes = 0;
LOOP1: for( my $i = 0; $i < scalar @sets; $i++ ) {
my $s1 = $sets[$i]->[0];
for( my $j = $i+1; $j < scalar @sets; $j++ ) {
my $s2 = $sets[$j]->[0];
if( $s1->overlaps($s2) ) {
if( $s1->length > $s2->length ) {
splice(@sets,$j,1);
} else {
splice(@sets,$i,1);
}
$changes = 1;
last LOOP1;
}
}
}
}
for my $s ( @sets ) {
$s->[1]->{'Name'} .= "-$chrom";
if( $unique{$s->[1]->{'Name'}}++ ) {
$s->[1]->{'Name'} .=
sprintf(".copy%d",$unique{$s->[1]->{'Name'}});
}
}
} else {
$sets[0]->[1]->{'Name'} .= "-$chrom";
}
warn "AFTER:\n" if $debug;
for my $s ( @sets ) {
$s = $s->[1];
my $name = $s->{'Name'};
if( $version_out == 2 ) {
print join("\t", @{$s->{'mRNA'}}, sprintf("GenePrediction %s",
$name)),"\n";
} else {
my @r = @{$s->{'mRNA'}};
$r[2] = 'gene';
print join("\t",@r ,
sprintf("ID=%s.gene;Name=%s.gene",
$name,$name)),"\n";
print join("\t", @{$s->{'mRNA'}},
sprintf("ID=%s;Parent=%s.gene;Name=%s",
$name,$name,$name)),"\n";
}
for my $cds ( @{$s->{'CDS'}} ) {
my $target = pop @$cds;
if( $version_out == 2) {
print join("\t", @$cds,
sprintf("GenePrediction %s ; Target \"%s\"",
$name,$target)),"\n";
} else {
print join("\t", @$cds,
sprintf("Parent=%s;Target=%s",
$name,$target)),"\n";
}
}
}
}
}