5
bp_classify_hits_kingdom - classify BLAST hits by taxonomic kingdom
9
bp_classify_hits_kingdom [-i tab_file] [-i second_BLAST_file] [-e evalue_cutoff]
10
[-t dir_where_TAXONOMY_files_are] [-g gi2taxid]
11
[-z PATH_TO_zcat] [-v]
15
Will print out the taxonomic distribution (at the kingdom level) for a
16
set of hits against the NR database. By default, this script assumes you
17
did a search against the protein database (gi_taxid_nuc.dump file).
19
This expects BLAST files in tabbed -m9 or -m8 format. Output with -m
20
8 or use blast2table.pl to convert (or fastam9_to_table.PLS if using
24
-t/--taxonomy Directory where the taxonomy .dmp files are (from NCBI)
25
-g/--gi File path of the gi2taxid file (gi_taxid_prot.dmp for proteins
26
or gi_taxid_nucl.dmp if the search was against a nucleid database)
27
-i/--in The name of the tab delimited -m8/-m9 output files to process
28
-e/--evalue Provide an E-value cutoff for hits to be considered
29
-z/--zcat Path to the 'zcat' executable, can also be 'gunzip -c'
30
if no zcat on your system.
32
-v/--verbose To turn on verbose messages
33
-h/--help Display this helpful information
35
This is intended to be useful starting script, but users may want to
36
customize the output and parameters. Note that I am summarizing the
37
kingdoms here and Eukaryota not falling into Metazoa, Viridiplantae, or
38
Fungi gets grouped into the general superkingdom Eukaryota for simplicity.
39
There are comments in the code directing you to where changes can be made
40
if you wanted to display hits by phylum for example. Note that you must
41
wipe out the cache file 'gi2class' that is created in your directory after
46
Jason Stajich jason_at_bioperl_dot_org
52
use Bio::DB::Taxonomy;
61
my $evalue_filter = 1e-3;
63
my $zcat = 'zcat'; # or gunzip -c
64
my $prefix = File::Spec->catfile($HOME,'taxonomy');
65
my $gi2taxidfile = "$prefix/gi_taxid_prot.dmp.gz";
66
my $force = 0; # don't use the cached gi2taxid file
68
'v|verbose|debug' => \$DEBUG,
72
'e|evalue:f' => \$evalue_filter,
73
't|taxonomy:s' => \$prefix,
74
'g|gi|gi2taxid:s' => \$gi2taxidfile,
75
'h|help' => sub { system('perldoc', $0); exit },
78
# insure idx location is created
79
mkdir(File::Spec->catfile($prefix,'idx'))
80
unless -d File::Spec->catfile($prefix,'idx');
82
# these files came from ftp://ftp.ncbi.nih.gov/pub/taxonomy
83
my $taxdb = Bio::DB::Taxonomy->new
84
(-source => 'flatfile',
85
-directory => File::Spec->catfile
87
-nodesfile => File::Spec->catfile($prefix,'nodes.dmp'),
88
-namesfile => File::Spec->catfile($prefix,'names.dmp')
92
my (%taxid4gi,%gi2node);
93
my $dbh = tie(%gi2node, 'DB_File', 'gi2class');
94
my $giidxfile = File::Spec->catfile($prefix,'idx','gi2taxid');
95
my $done = -f $giidxfile;
97
my $dbh2 = $dbh = DBI->connect("dbi:SQLite:dbname=$giidxfile","","");
99
$dbh2->do("CREATE TABLE gi2taxid ( gi integer PRIMARY KEY,
100
taxid integer NOT NULL)");
101
$dbh2->{AutoCommit} = 0;
103
# this file came from ftp://ftp.ncbi.nih.gov/pub/taxonomy
104
# I'm interested in protein hits therefor _prot file.
105
if (not -f $gi2taxidfile) {
106
die "Error: File $gi2taxidfile does not exist\n";
108
if( $gi2taxidfile =~ /\.gz$/ ) {
109
open($fh, "$zcat $gi2taxidfile |" ) || die "$zcat $gi2taxidfile: $!";
111
open($fh, $gi2taxidfile ) || die "Error: could not read file $gi2taxidfile: $!";
114
my $sth = $dbh2->prepare("INSERT INTO gi2taxid (gi,taxid) VALUES (?,?)");
117
my ($gi,$taxid) = split;
118
$sth->execute($gi,$taxid);
120
if( $i % 500000 == 0 ) {
122
warn("$i\n") if $DEBUG;
129
for my $file ( @files ) {
132
if( $file =~ /\.gz$/) {
135
my ($spname) = split(/\./,$file);
138
open($fh, "$zcat $file |") || die "$zcat $file: $!";
140
open($fh, $file) || die "$file: $!";
142
my $sth = $dbh->prepare("SELECT taxid from gi2taxid WHERE gi=?");
145
my ($qname,$hname,$pid,$qaln,$mismatch,$gaps,
146
$qstart,$qend,$hstart,$hend,
147
$evalue,$bits,$score) = split(/\t/,$_);
148
next if( $evalue > $evalue_filter );
149
if( ! exists $query{$spname}->{$qname} ) {
150
$query{$spname}->{$qname} = {};
153
if( $hname =~ /gi\|(\d+)/) {
155
if( ! $gi2node{$gi} ){ # see if we cached the results from before
158
$sth->bind_columns(\$taxid);
159
if( ! $sth->fetch ) {
160
warn("no taxid for $gi\n");
163
my $node = $taxdb->get_Taxonomy_Node($taxid);
165
warn("cannot find node for gi=$gi ($hname) (taxid=$taxid)\n");
168
my $parent = $taxdb->get_Taxonomy_Node($node->parent_id);
170
# THIS IS WHERE THE KINGDOM DECISION IS MADE
171
# DON'T FORGET TO WIPE OUT YOUR CACHE FILE
172
# gi2class after you make changes here
173
while( defined $parent && $parent->node_name ne 'root' ) {
174
# this is walking up the taxonomy hierarchy
175
# can be a little slow, but works...
176
#warn( "\t",$parent->rank, " ", $parent->node_name, "\n");
177
# deal with Eubacteria, Archea separate from
178
# Metazoa, Fungi, Viriplantae differently
179
# (everything else Eukaryotic goes in Eukaryota)
180
if( $parent->rank eq 'kingdom') {
182
($gi2node{$gi}) = $parent->node_name;
184
} elsif( $parent->rank eq 'superkingdom' ) {
186
($gi2node{$gi}) = $parent->node_name;
187
$gi2node{$gi} =~ s/ \<(bacteria|archaea)\>//g;
190
$parent = $taxdb->get_Taxonomy_Node($parent->parent_id);
193
my ($kingdom) = $gi2node{$gi};
194
#warn("$gi2node{$gi}\n");
195
unless( defined $kingdom && length($kingdom) ) {
196
#warn("no kingdom for $hname\n");
198
$query{$spname}->{$qname}->{$kingdom}++;
201
warn("no GI in $hname\n");
204
last if ( $DEBUG && $i++ > 10000);
208
# print out the taxonomic distribution
209
while( my ($sp,$d) = each %query ) {
210
my $total = scalar keys %$d;
211
print "$sp total=$total\n";
213
for my $v ( values %$d ) {
214
my $tag = join(",",sort keys %$v );
217
for my $t ( sort { $seen{$a} <=> $seen{$b} } keys %seen ) {
218
printf " %-20s\t%d\t%.2f%%\n",
219
$t,$seen{$t}, 100 * $seen{$t} / $total;