| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
# Give a Kraken-style report from a Centrifuge output |
| 4 |
# |
| 5 |
# Based on kraken-report by Derrick Wood |
| 6 |
# Copyright 2013-2016, Derrick Wood <[email protected]> |
| 7 |
# |
| 8 |
|
| 9 |
use strict; |
| 10 |
use warnings; |
| 11 |
use Getopt::Long; |
| 12 |
use File::Basename; |
| 13 |
use Cwd; |
| 14 |
use Cwd 'cwd' ; |
| 15 |
use Cwd 'abs_path' ; |
| 16 |
|
| 17 |
my ($centrifuge_index, $min_score, $min_length); |
| 18 |
my $no_lca = 0; |
| 19 |
my $show_zeros = 0; |
| 20 |
my $is_cnts_table = 0; |
| 21 |
my $PROG = "centrifuge-kreport"; |
| 22 |
my $CWD = dirname( abs_path( $0 ) ) ; |
| 23 |
|
| 24 |
GetOptions( |
| 25 |
"help" => \&display_help, |
| 26 |
"x=s" => \$centrifuge_index, |
| 27 |
"show-zeros" => \$show_zeros, |
| 28 |
"is-count-table" => \$is_cnts_table, |
| 29 |
"min-score=i" => \$min_score, |
| 30 |
"min-length=i"=> \$min_length, |
| 31 |
"no-lca" => \$no_lca |
| 32 |
) or usage(); |
| 33 |
|
| 34 |
usage() unless defined $centrifuge_index; |
| 35 |
if (!defined $ARGV[0]) { |
| 36 |
print STDERR "Reading centrifuge out file from STDIN ... \n"; |
| 37 |
} |
| 38 |
|
| 39 |
sub usage { |
| 40 |
my $exit_code = @_ ? shift : 64; |
| 41 |
print STDERR " |
| 42 |
Usage: centrifuge-kreport -x <index name> OPTIONS <centrifuge output file(s)> |
| 43 |
|
| 44 |
centrifuge-kreport creates Kraken-style reports from centrifuge out files. |
| 45 |
|
| 46 |
Options: |
| 47 |
-x INDEX (REQUIRED) Centrifuge index |
| 48 |
|
| 49 |
--no-lca Do not report the LCA of multiple assignments, but report count fractions at the taxa. |
| 50 |
--show-zeros Show clades that have zero reads, too |
| 51 |
--is-count-table The format of the file is 'taxID<tab>COUNT' instead of the standard |
| 52 |
Centrifuge output format |
| 53 |
|
| 54 |
--min-score SCORE Require a minimum score for reads to be counted |
| 55 |
--min-length LENGTH Require a minimum alignment length to the read |
| 56 |
|
| 57 |
"; |
| 58 |
exit $exit_code; |
| 59 |
} |
| 60 |
|
| 61 |
sub display_help { |
| 62 |
usage(0); |
| 63 |
} |
| 64 |
|
| 65 |
my (%child_lists, %name_map, %rank_map, %parent_map); |
| 66 |
print STDERR "Loading taxonomy ...\n"; |
| 67 |
load_taxonomy(); |
| 68 |
|
| 69 |
my %taxo_counts; |
| 70 |
my $seq_count = 0; |
| 71 |
$taxo_counts{0} = 0; |
| 72 |
if ($is_cnts_table) { |
| 73 |
while (<>) { |
| 74 |
my ($taxID,$count) = split; |
| 75 |
$taxo_counts{$taxID} = $count; |
| 76 |
$seq_count += $count; |
| 77 |
} |
| 78 |
} else { |
| 79 |
chomp(my $header = <>); |
| 80 |
my @cols = split /\t/, $header ; |
| 81 |
my %headerMap ; |
| 82 |
for ( my $i = 0 ; $i < scalar( @cols ) ; ++$i ) |
| 83 |
{ |
| 84 |
$headerMap{ $cols[$i] } = $i ; |
| 85 |
} |
| 86 |
|
| 87 |
my $prevReadID; |
| 88 |
my $prevTaxID; |
| 89 |
while (<>) { |
| 90 |
#my (undef,$seqID,$taxID,$score, undef, $hitLength, $queryLength, $numMatches) = split /\t/; |
| 91 |
my @cols = split /\t/ ; |
| 92 |
my $readID = $cols[ $headerMap{ "readID" } ] ; |
| 93 |
my $seqID = $cols[ $headerMap{ "seqID" } ] ; |
| 94 |
my $taxID = $cols[ $headerMap{ "taxID" } ] ; |
| 95 |
my $score = $cols[ $headerMap{ "score" } ] ; |
| 96 |
my $hitLength = $cols[ $headerMap{ "hitLength" } ] ; |
| 97 |
my $queryLength = $cols[ $headerMap{ "queryLength" } ] ; |
| 98 |
my $numMatches = $cols[ $headerMap{ "numMatches" } ] ; |
| 99 |
|
| 100 |
next if defined $min_length && $hitLength < $min_length; |
| 101 |
next if defined $min_score && $score < $min_score; |
| 102 |
|
| 103 |
$taxID = 1 if ( !isTaxIDInTree( $taxID ) ) ; |
| 104 |
|
| 105 |
if ($no_lca) { |
| 106 |
$taxo_counts{$taxID} += 1/$numMatches; |
| 107 |
$seq_count += 1/$numMatches; |
| 108 |
} else { |
| 109 |
if ( ( defined $prevReadID ) && ( $readID eq $prevReadID ) ) { |
| 110 |
--$taxo_counts{$prevTaxID}; |
| 111 |
$prevTaxID = lca($prevTaxID, $taxID); |
| 112 |
++$taxo_counts{$prevTaxID}; |
| 113 |
} else { |
| 114 |
++$taxo_counts{$taxID}; |
| 115 |
++$seq_count; |
| 116 |
$prevTaxID = $taxID; |
| 117 |
} |
| 118 |
} |
| 119 |
$prevReadID = $readID ; |
| 120 |
} |
| 121 |
} |
| 122 |
my $classified_count = $seq_count - $taxo_counts{0}; |
| 123 |
|
| 124 |
my %clade_counts = %taxo_counts; |
| 125 |
dfs_summation(1); |
| 126 |
|
| 127 |
for (keys %name_map) { |
| 128 |
$clade_counts{$_} ||= 0; |
| 129 |
} |
| 130 |
|
| 131 |
die "No sequence matches with given settings" unless $seq_count > 0; |
| 132 |
|
| 133 |
printf "%6.2f\t%d\t%d\t%s\t%d\t%s%s\n", |
| 134 |
$clade_counts{0} * 100 / $seq_count, |
| 135 |
$clade_counts{0}, $taxo_counts{0}, "U", |
| 136 |
0, "", "unclassified"; |
| 137 |
dfs_report(1, 0); |
| 138 |
|
| 139 |
sub dfs_report { |
| 140 |
my $node = shift; |
| 141 |
my $depth = shift; |
| 142 |
if (! $clade_counts{$node} && ! $show_zeros) { |
| 143 |
return; |
| 144 |
} |
| 145 |
printf "%6.2f\t%d\t%d\t%s\t%d\t%s%s\n", |
| 146 |
($clade_counts{$node} || 0) * 100 / $seq_count, |
| 147 |
($clade_counts{$node} || 0), |
| 148 |
($taxo_counts{$node} || 0), |
| 149 |
rank_code($rank_map{$node}), |
| 150 |
$node, |
| 151 |
" " x $depth, |
| 152 |
$name_map{$node}; |
| 153 |
my $children = $child_lists{$node}; |
| 154 |
if ($children) { |
| 155 |
my @sorted_children = sort { $clade_counts{$b} <=> $clade_counts{$a} } @$children; |
| 156 |
for my $child (@sorted_children) { |
| 157 |
dfs_report($child, $depth + 1); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
sub isTaxIDInTree { |
| 163 |
my $a = $_[0] ; |
| 164 |
|
| 165 |
while ( $a > 1 ) |
| 166 |
{ |
| 167 |
if ( !defined $parent_map{ $a } ) |
| 168 |
{ |
| 169 |
print STDERR "Couldn't find parent of taxID $a - directly assigned to root.\n"; |
| 170 |
return 0 ; |
| 171 |
} |
| 172 |
last if ( $a eq $parent_map{$a} ) ; |
| 173 |
$a = $parent_map{ $a } ; |
| 174 |
} |
| 175 |
return 1 ; |
| 176 |
} |
| 177 |
|
| 178 |
sub lca { |
| 179 |
my ($a, $b) = @_; |
| 180 |
return $b if $a eq 0; |
| 181 |
return $a if $b eq 0; |
| 182 |
return $a if $a eq $b; |
| 183 |
my %a_path; |
| 184 |
while ($a ge 1) { |
| 185 |
$a_path{$a} = 1; |
| 186 |
if (!defined $parent_map{$a}) { |
| 187 |
print STDERR "Couldn't find parent of taxID $a - directly assigned to root.\n"; |
| 188 |
last; |
| 189 |
} |
| 190 |
last if $a eq $parent_map{$a}; |
| 191 |
$a = $parent_map{$a}; |
| 192 |
} |
| 193 |
while ($b > 1) { |
| 194 |
return $b if (defined $a_path{$b}); |
| 195 |
if (!defined $parent_map{$b}) { |
| 196 |
print STDERR "Couldn't find parent of taxID $b - directly assigned to root.\n"; |
| 197 |
last; |
| 198 |
} |
| 199 |
last if $b eq $parent_map{$b}; |
| 200 |
$b = $parent_map{$b}; |
| 201 |
} |
| 202 |
return 1; |
| 203 |
} |
| 204 |
|
| 205 |
sub rank_code { |
| 206 |
my $rank = shift; |
| 207 |
for ($rank) { |
| 208 |
$_ eq "species" and return "S"; |
| 209 |
$_ eq "genus" and return "G"; |
| 210 |
$_ eq "family" and return "F"; |
| 211 |
$_ eq "order" and return "O"; |
| 212 |
$_ eq "class" and return "C"; |
| 213 |
$_ eq "phylum" and return "P"; |
| 214 |
$_ eq "kingdom" and return "K"; |
| 215 |
$_ eq "superkingdom" and return "D"; |
| 216 |
} |
| 217 |
return "-"; |
| 218 |
} |
| 219 |
|
| 220 |
sub dfs_summation { |
| 221 |
my $node = shift; |
| 222 |
my $children = $child_lists{$node}; |
| 223 |
if ($children) { |
| 224 |
for my $child (@$children) { |
| 225 |
dfs_summation($child); |
| 226 |
$clade_counts{$node} += ($clade_counts{$child} || 0); |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
sub load_taxonomy { |
| 232 |
|
| 233 |
print STDERR "Loading names file ...\n"; |
| 234 |
open NAMES, "-|", "$CWD/centrifuge-inspect --name-table $centrifuge_index" |
| 235 |
or die "$PROG: can't open names file: $!\n"; |
| 236 |
while (<NAMES>) { |
| 237 |
chomp; |
| 238 |
s/\t\|$//; |
| 239 |
my @fields = split /\t/; |
| 240 |
my ($node_id, $name) = @fields[0,1]; |
| 241 |
$name_map{$node_id} = $name; |
| 242 |
} |
| 243 |
close NAMES; |
| 244 |
|
| 245 |
print STDERR "Loading nodes file ...\n"; |
| 246 |
open NODES, "-|", "$CWD/centrifuge-inspect --taxonomy-tree $centrifuge_index" |
| 247 |
or die "$PROG: can't open nodes file: $!\n"; |
| 248 |
while (<NODES>) { |
| 249 |
chomp; |
| 250 |
my @fields = split /\t\|\t/; |
| 251 |
my ($node_id, $parent_id, $rank) = @fields[0,1,2]; |
| 252 |
if ($node_id == 1) { |
| 253 |
$parent_id = 0; |
| 254 |
} |
| 255 |
$child_lists{$parent_id} ||= []; |
| 256 |
push @{ $child_lists{$parent_id} }, $node_id; |
| 257 |
$rank_map{$node_id} = $rank; |
| 258 |
$parent_map{$node_id} = $parent_id; |
| 259 |
} |
| 260 |
close NODES; |
| 261 |
} |