Debian Codegraph @

centrifuge-promote

raw
1 #!/usr/bin/env perl
2
3 use strict ;
4 use warnings ;
5
6 use File::Basename;
7 use Cwd;
8 use Cwd 'cwd' ;
9 use Cwd 'abs_path' ;
10
11
12 die "Usage: centrifuge-promote centrifuge_index_name centrifuge_output level > output\n\n".
13 "Promote the taxonomy id to specified level in Centrifuge output.\n".
14 "\tIf level equals \"lca\", this will merge the multiassignment to their lowest common ancestor.\n" if ( @ARGV == 0 ) ;
15
16 my $CWD = dirname( abs_path( $0 ) ) ;
17 # Go through the index to obtain the taxonomy tree
18 my %taxParent ;
19 my %taxIdToSeqId ;
20 my %taxLevel ;
21
22 my $centrifuge_index = $ARGV[0] ;
23 open FP1, "-|", "$CWD/centrifuge-inspect --taxonomy-tree $centrifuge_index" or die "can't open $!\n" ;
24 while ( <FP1> )
25 {
26 chomp ;
27 my @cols = split /\t\|\t/;
28 $taxParent{ $cols[0] } = $cols[1] ;
29 $taxLevel{ $cols[0] } = $cols[2] ;
30 }
31 close FP1 ;
32 open FP1, "-|", "$CWD/centrifuge-inspect --conversion-table $centrifuge_index" or die "can't open $!\n" ;
33 while ( <FP1> )
34 {
35 chomp ;
36 my @cols = split /\t/ ;
37 $taxIdToSeqId{ $cols[1] } = $cols[0] ;
38 }
39 close FP1 ;
40
41 # Go through the output of centrifuge
42 my $level = $ARGV[2] ;
43 sub PromoteTaxId
44 {
45 my $tid = $_[0] ;
46 return 0 if ( $tid <= 0 || !defined( $taxLevel{ $tid } ) ) ;
47
48 if ( $taxLevel{ $tid } eq $level )
49 {
50 return $tid ;
51 }
52 else
53 {
54 return 0 if ( $tid <= 1 ) ;
55 return PromoteTaxId( $taxParent{ $tid } ) ;
56 }
57 }
58
59 sub lca
60 {
61 my ($a, $b) = @_;
62 return $b if $a eq 0;
63 return $a if $b eq 0;
64 return $a if $a eq $b;
65 my %a_path;
66 while ($a ge 1)
67 {
68 $a_path{$a} = 1;
69 if (!defined $taxParent{$a}) {
70 print STDERR "Couldn't find parent of taxID $a - directly assigned to root.\n";
71 last;
72 }
73 last if $a eq $taxParent{$a};
74 $a = $taxParent{$a};
75 }
76
77 while ($b > 1)
78 {
79 return $b if (defined $a_path{$b});
80 if (!defined $taxParent{$b}) {
81 print STDERR "Couldn't find parent of taxID $b - directly assigned to root.\n";
82 last;
83 }
84 last if $b eq $taxParent{$b};
85 $b = $taxParent{$b};
86 }
87 return 1;
88 }
89
90 sub OutputPromotedLines
91 {
92 my @lines = @{ $_[0] } ;
93 return if ( scalar( @lines ) <= 0 ) ;
94
95 my @newLines ;
96 my $i ;
97 my $numMatches = 0 ;
98 my %showedUpTaxId ;
99 my $tab = sprintf( "\t" ) ;
100
101 if ( $level ne "lca" )
102 {
103 for ( $i = 0 ; $i < scalar( @lines ) ; ++$i )
104 {
105 my @cols = split /\t+/, $lines[ $i ] ;
106 my $newTid = PromoteTaxId( $cols[2] ) ;
107 if ( $newTid <= 1 )
108 {
109 $newTid = $cols[2] ;
110 }
111 my $newLevel = $cols[1] ;
112 $newLevel = $taxLevel{ $newTid } if ( $newTid >= 1 && defined $taxLevel{ $newTid } ) ;
113
114 next if ( defined $showedUpTaxId{ $newTid } ) ;
115
116 $showedUpTaxId{ $newTid } = 1 ;
117 ++$numMatches ;
118
119 $cols[2] = $newTid ;
120 $cols[1] = $newLevel ;
121 push @newLines, join( $tab, @cols ) ;
122 }
123 }
124 else
125 {
126 $numMatches = 1 ;
127 my @cols = split /\t+/, $lines[0] ;
128 my $l = $cols[2] ;
129 for ( $i = 1 ; $i < scalar( @lines ) ; ++$i )
130 {
131 @cols = split /\t+/, $lines[ $i ] ;
132
133 $l = lca( $l, $cols[2] ) ;
134 }
135
136 @cols = split /\t+/, $lines[0] ;
137 $cols[1] = $taxLevel{ $l } if ( $l ne $cols[2] ) ;
138 $cols[2] = $l ;
139 push @newLines, join( $tab, @cols ) ;
140 }
141
142 for ( $i = 0 ; $i < scalar( @newLines ) ; ++$i )
143 {
144 my @cols = split /\t+/, $newLines[$i] ;
145 $cols[-1] = $numMatches ;
146 print join( $tab, @cols ), "\n" ;
147 }
148 }
149
150 open FP1, $ARGV[1] ;
151 my $header = <FP1> ;
152 my $prevReadId = "" ;
153 my @lines ;
154
155 print $header ;
156 while ( <FP1> )
157 {
158 chomp ;
159 my @cols = split /\t/ ;
160 if ( $cols[0] eq $prevReadId )
161 {
162 push @lines, $_ ;
163 }
164 else
165 {
166 $prevReadId = $cols[0] ;
167
168 OutputPromotedLines( \@lines ) ;
169
170 undef @lines ;
171 push @lines, $_ ;
172 }
173 }
174 OutputPromotedLines( \@lines ) ;
175 close FP1 ;