-
Notifications
You must be signed in to change notification settings - Fork 14
/
quality_filter.pl
executable file
·175 lines (149 loc) · 4.62 KB
/
quality_filter.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/opt/hpc/pkg/perl-5.14/bin/perl -w
eval 'exec /opt/hpc/pkg/perl-5.14/bin/perl -w -S $0 ${1+"$@"}'
if 0; # not running under some shell
use strict;
#use lib ('/home/mcampbell/lib');
#use PostData;
use Getopt::Std;
use vars qw($opt_s $opt_d $opt_a $opt_p $opt_c $opt_m $opt_u);
getopts('sda:pcmu');
use FileHandle;
#-----------------------------------------------------------------------------
#----------------------------------- MAIN ------------------------------------
#-----------------------------------------------------------------------------
my $usage = "\n\nquality_filter.pl: generates defualt and standard
gene builds from a maker geneated gff3_file with
iprscan data pushed onto column 9 using ipr_update_gff.\n
USAGE: quality_filter.pl -[options]<gff3_file>\n
OPTIONS: -d Prints transcripts with an AED <1 (MAKER default)
-s Prints transcripts with an AED <1 and/or Pfam domain
if in gff3 (MAKER Standard)
-a <number between 0 and 1> Prints transcripts with an
AED < the given value\n\n";
my $FILE1 = $ARGV[0];
#my $FILE2 = $ARGV[1];
die($usage) unless $ARGV[0] && ($opt_a || $opt_s || $opt_d);
my %LU_G;
my %LU_T;
build_lus($FILE1);
#build_lu_tid($FILE2);
filter($FILE1);
#PostData(\%LU_G);
#PostData(\%LU_T);
#-----------------------------------------------------------------------------
#---------------------------------- SUBS -------------------------------------
#-----------------------------------------------------------------------------
sub build_lus{
my $file = shift;
my %data;
my $fh = new FileHandle;
$fh->open($file);
while (defined(my $line = <$fh>)){
chomp($line);
last if $line =~ /^\#\#FASTA/;
next if $line =~ /^\#/;
my @array = split(/\t/, $line);
next unless $array[2] =~ /mRNA/ || $array[2] =~ /tRNA/;
my ($tid) = $array[8] =~ /ID\=(.+?);.*/;
my ($gid) = $array[8] =~ /Parent\=(.+?);.*/;
my @c9 = split(/\;/, $array[8]);
foreach my $x (@c9){
my ($k,$v) = $x =~ /(.+)\=(.+)/;
$data{$k}=$v;
}
#load the LU
if ($opt_s &&
(($data{'Dbxref'} && $data{'Dbxref'} =~ /Pfam/) ||
$data{'_AED'} < 1)){
$LU_G{$gid}=1;
$LU_T{$tid}=1;
}
elsif ($opt_d && $data{'_AED'} < 1){
$LU_G{$gid}=1;
$LU_T{$tid}=1;
}
elsif ($opt_a && $data{'_AED'} < $opt_a){
$LU_G{$gid}=1;
$LU_T{$tid}=1;
}
elsif ($array[2] =~ /tRNA/){
$LU_G{$gid}=1;
$LU_T{$tid}=1;
}
undef %data;
}
}
#-----------------------------------------------------------------------------
sub filter{
my $file = shift;
my $fh = new FileHandle;
$fh->open($file);
print "##gff-version 3\n";
while (defined(my $line = <$fh>)){
chomp($line);
last if $line =~ /^\#\#FASTA/;
next if $line =~ /^\#/;
my @array = split(/\t/, $line);
if ($array[2] eq 'gene'){
my ($id) = $array[8] =~ /ID=(\S+?);/;
print $line."\n" if defined($LU_G{$id});
}
elsif ($array[2] eq 'mRNA'){
my ($id) = $array[8] =~ /ID=(\S+?);/;
print $line."\n" if defined($LU_T{$id});
}
elsif ($array[2] eq 'exon'|
$array[2] eq 'CDS'|
$array[2] eq 'three_prime_UTR'|
$array[2] eq 'five_prime_UTR'){
my $bool = 0;
my ($ids) = $array[8] =~ /Parent=(\S+);?/;
# my ($ids) = $array[8] =~ /Parent=(\S+?);/;
$ids =~ s/;//;
my @ids_array = split(/,/, $ids);
foreach my $x (@ids_array){
if (defined($LU_T{$x})){
$bool++;
}
else{
$line =~ s/$x[^:]//;
}
}
print $line."\n" if $bool;
}
else{print $line."\n"}
}
$fh->close();
}
#-----------------------------------------------------------------------------
sub build_lu_gid{
my $file = shift;
my $fh = new FileHandle;
$fh->open($file);
while (defined(my $line = <$fh>)){
chomp($line);
$LU_G{$line}=1;
}
$fh->close();
}
#-----------------------------------------------------------------------------
sub build_lu_tid{
my $file = shift;
my $fh = new FileHandle;
$fh->open($file);
while (defined(my $line = <$fh>)){
chomp($line);
last if $line =~ /^\#\#FASTA/;
next if $line =~ /^\#/;
my @array = split(/\t/, $line);
if ($array[2] =~ 'mRNA'){
my ($tid) = $line =~ /ID=(.+?);/;
my ($gid) = $line =~ /Parent=(.+?);/;
if (defined($LU_G{$gid})){
$LU_T{$tid}=1;
}
}
}
$fh->close();
}
#-----------------------------------------------------------------------------