| 1 |
|
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use diagnostics; |
| 5 |
|
| 6 |
use Getopt::Long; |
| 7 |
use Pod::Usage; |
| 8 |
|
| 9 |
|
| 10 |
sub uniq { |
| 11 |
my @list = @_; |
| 12 |
my @out = (); |
| 13 |
my %seen = (); |
| 14 |
foreach my $item(@list) { |
| 15 |
push @out, $item unless $seen{$item}++; |
| 16 |
} |
| 17 |
return @out; |
| 18 |
} |
| 19 |
|
| 20 |
my ($infile, $outfile, $gofile, $help, $man); |
| 21 |
my $exclude = "GO:0005575"; |
| 22 |
|
| 23 |
GetOptions( 'infile|i=s' => \$infile, |
| 24 |
'outfile|o=s' => \$outfile, |
| 25 |
'gofile|g=s' => \$gofile, |
| 26 |
'exclude|x=s' => \$exclude, |
| 27 |
'help|h|?' => \$help, |
| 28 |
'man' => \$man, |
| 29 |
) or pod2usage(2); |
| 30 |
|
| 31 |
pod2usage(1) if $help; |
| 32 |
pod2usage(-exitstatus => 0, -verbose => 2) if $man; |
| 33 |
|
| 34 |
if(!$infile) { |
| 35 |
print STDERR "No input file specified!\n"; |
| 36 |
pod2usage(2); |
| 37 |
} |
| 38 |
|
| 39 |
if(!$outfile) { |
| 40 |
print STDERR "No output file specified!\n"; |
| 41 |
pod2usage(2); |
| 42 |
} |
| 43 |
|
| 44 |
if(!$gofile) { |
| 45 |
print STDERR "No GO hierarchy file specified!\n"; |
| 46 |
pod2usage(2); |
| 47 |
} |
| 48 |
|
| 49 |
if(!$exclude) { |
| 50 |
print STDERR "No clude term specified!\n"; |
| 51 |
pod2usage(2); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
open(GO, "< $gofile") or die("Could not open $gofile for reading!"); |
| 56 |
|
| 57 |
# load go hash: $go{$id} -> list of is_a terms |
| 58 |
my %go = (); |
| 59 |
while(my $line = <GO>) { |
| 60 |
chomp $line; |
| 61 |
|
| 62 |
next if substr($line,0,1) eq "#"; |
| 63 |
|
| 64 |
my @l = split(/\t/, $line); |
| 65 |
|
| 66 |
my $id = shift @l; |
| 67 |
$go{$id} = \@l; |
| 68 |
} |
| 69 |
|
| 70 |
close(GO); |
| 71 |
|
| 72 |
open(IN, "< $infile") or die("Could not open $infile for reading!"); |
| 73 |
open(OUT, "> $outfile") or die("Could not open $outfile for writing!"); |
| 74 |
|
| 75 |
my $skip = 0; |
| 76 |
while(my $line = <IN>) { |
| 77 |
chomp $line; |
| 78 |
|
| 79 |
# are we in an identifier line? |
| 80 |
if(substr($line,0,1) eq ">") { |
| 81 |
# yes, so filter out terms |
| 82 |
|
| 83 |
|
| 84 |
# get all GO terms |
| 85 |
my @ids = split(",",substr($line,1)); |
| 86 |
|
| 87 |
my $done = 0; |
| 88 |
while (!$done) { |
| 89 |
my @new_ids = (); |
| 90 |
|
| 91 |
$done = 1; |
| 92 |
foreach my $id (@ids) { |
| 93 |
# travel up one hierarchy level |
| 94 |
|
| 95 |
if(not defined($go{$id})) { |
| 96 |
push @new_ids, $id; |
| 97 |
next; |
| 98 |
} |
| 99 |
|
| 100 |
my @is_a = @{$go{$id}}; |
| 101 |
# are we at the top yet? |
| 102 |
if(scalar @is_a > 0) { |
| 103 |
# no, so add nodes of new level |
| 104 |
push @new_ids, @is_a; |
| 105 |
$done = 0; |
| 106 |
} else { |
| 107 |
# yes, so keep top level nodes |
| 108 |
push @new_ids, $id; |
| 109 |
} |
| 110 |
} |
| 111 |
@ids = @new_ids; |
| 112 |
} |
| 113 |
|
| 114 |
my @uniq_ids = &uniq(@ids); |
| 115 |
|
| 116 |
$skip = ((scalar @uniq_ids) == 1) && ($uniq_ids[0] eq $exclude); |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
# it's sequence, so print out unless we're skipping an entry |
| 121 |
print OUT "$line\n" unless $skip; |
| 122 |
} |
| 123 |
|
| 124 |
close(IN); |
| 125 |
close(OUT); |
| 126 |
|
| 127 |
exit 0; |
| 128 |
|
| 129 |
=pod |
| 130 |
|
| 131 |
=head1 TITLE |
| 132 |
|
| 133 |
filter_fasta.pl - Filter FASTA file by GO hierarchy |
| 134 |
|
| 135 |
=head1 SYNOPSIS |
| 136 |
|
| 137 |
filter_fasta.pl -i infile.f -o outfile.f -g go_hierarchy.txt -x 0005575 |
| 138 |
|
| 139 |
=head1 DESCRIPTION |
| 140 |
|
| 141 |
-i Infile in FASTA format, with GO identifiers as sequence names |
| 142 |
-o Outfile in FASTA format, in the same format, but excluding paths that lead to -x |
| 143 |
-x Exclude this GO term |
| 144 |
|
| 145 |
=cut |