Debian Codegraph @

debian/mcl_14-137_scripts/mcxdeblast

raw
1 #!/usr/bin/perl -w
2
3 use Getopt::Long;
4
5 # (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Stijn van Dongen
6 # (c) Copyright 2004 Jason Stajich (column output parsing)
7 #
8 # Other (C)ontributors;
9 # Dinakarpandian Deendayal (general comments)
10 # Abel Ureta-Vidal (general comments)
11 # Daniel Lundin (regexp tweak, warning)
12 #
13 # You can redistribute and/or modify this program under the terms of the GNU
14 # General Public License; either version 3 of the License or (at your option)
15 # any later version.
16
17 #
18 # TODO
19 #
20 # - Optionally read from STDIN
21 # - Require tab file for line-mode={123,packed}
22 #
23 # - Infer hsp_len from long output (?)
24 # - (Perhaps) enable section parsing ala tribe, for finer control.
25 # - $:: usage is arbitrary and ugly. pipe down.
26 # - Is the m9/seenlfgt combination correct?
27 # - Check whether m9 output state is correct if errors occur.
28
29
30 $^W = 1;
31 use strict;
32
33 $::mode_sort = 'o'; # a lphabetical
34 # o ccurrence
35
36 $::mode_score = 'e'; # e value
37 # b it
38
39 my $line_mode = ""; # default is raw mode, not line mode
40 $::name_out = '-';
41
42 $::bcut = 0;
43 $::ecut = 0;
44 $::rcut = 0;
45
46 my $blastfix = "";
47 my $addfix = "";
48
49 my $user_tabfile = "";
50
51 my %line_modes = qw ( abc 1 123 1);
52
53 my $obase = "";
54 my $stdhandler = 0;
55
56 my $help = 0;
57 my $m9 = 0;
58 my $abc = 0;
59
60
61
62 if
63 (! GetOptions
64 ( "help" => \$help
65 , "apropos" => \$help
66 , "sort=s" => \$::mode_sort
67 , "ecut=f" => \$::ecut
68 , "bcut=f" => \$::bcut
69 , "rcut=f" => \$::rcut
70 , "score=s" => \$::mode_score
71 , "m9" => \$m9
72 , "tab=s" => \$user_tabfile
73 , "xo-dat=s" => \$addfix
74 , "xi-dat=s" => \$blastfix
75 , "base=s" => \$obase
76 , "stdhandler" => \$stdhandler
77 , "line-mode=s" => \$line_mode
78 , "out=s" => \$::name_out
79 )
80 )
81 { print STDERR "option processing failed\n";
82 exit(1);
83 }
84
85 help() && exit(0) if $help;
86
87 die "unknown sort mode <$::mode_sort>"
88 unless $::mode_sort =~ /^[ao]$/;
89 die "unknown line mode <$line_mode>"
90 if ($line_mode && !defined($line_modes{$line_mode}));
91 die "unknown sort mode <$::mode_score>"
92 unless $::mode_score =~ /^[ebr]$/;
93
94
95 my $fname = shift || die "please submit name of blast file\n";
96 $obase = $fname unless $obase;
97
98
99 if ($blastfix) {
100 $obase = $fname;
101 if ($fname =~ /\Q.$blastfix\E$/) {
102 $obase =~ s/\Q.$blastfix\E$//;
103 }
104 else {
105 $fname .= ".$blastfix";
106 }
107 }
108 if ($addfix) {
109 $obase .= ".$addfix";
110 }
111
112 my ($gix, $giy);
113 $::seenlft = {};
114 $::seenrgt = {};
115 my $tagTocc = {};
116 my $me = "[$0] ";
117 my $lc = 0;
118 my $fhin = \*STDIN;
119
120 if ($fname ne '-') {
121 open(F_BLAST, "<$fname") || die "cannot open $fname\n";
122 $fhin = \*F_BLAST;
123 }
124
125 $::TAB_user = {};
126
127 if ($user_tabfile) {
128 read_tab($user_tabfile, $::TAB_user);
129 }
130
131
132 $::f_raw = undef;
133 my $f_err = undef;
134
135 if ($line_mode) {
136 if (!$::name_out || $::name_out eq '-') {
137 $::f_raw = \*STDOUT;
138 }
139 else {
140 open(F_RAW, ">$::name_out") || die "cannot open $::name_out";
141 $::f_raw = \*F_RAW;
142 }
143 $f_err = \*STDERR;
144
145 if ($line_mode eq 'abc') {
146 # print $::f_raw "#aa# (stream abc cookie)\n";
147 }
148 elsif ($line_mode eq '123') {
149 # print $::f_raw "#11# (stream abc cookie)\n";
150 }
151 }
152 else {
153 if ($stdhandler) {
154 $::f_raw = \*STDOUT;
155 $f_err = \*STDERR;
156 }
157 else {
158 open(F_RAW, ">$obase.raw") || die "cannot open $obase.raw\n";
159 $::f_raw = \*F_RAW;
160 open(F_ERR, ">$obase.err") || die "cannot open $obase.err\n";
161 $f_err = \*F_ERR;
162 }
163 }
164
165 $::ID = 0;
166
167 if ($m9) {
168 munge_linewise($fhin, $::f_raw, $line_mode);
169 }
170 else {
171 munge_long($fhin, $::f_raw, $line_mode);
172 }
173
174 close $::f_raw;
175 $::CT = scalar keys %$::seenrgt;
176
177
178 my $alnum = 0;
179 my $occTmisc = {};
180
181 if (!$user_tabfile && !$line_mode) {
182 open(F_HDR, ">$obase.hdr") || die "cannot open $obase.hdr\n"; # header
183 open(F_MAP, ">$obase.map") || die "cannot open $obase.idx\n"; # map file
184 open(F_TAB, ">$obase.tab") || die "cannot open $obase.idx\n"; # indices
185 print F_TAB "#<mapped index> <tag>\n";
186 if ($::mode_sort eq 'o') {
187 print F_TAB "# sort mode is by occurrence\n";
188 }
189 elsif ($::mode_sort eq 'a') {
190 print F_TAB "# sort mode is alphabetical\n";
191 }
192 if ($::mode_sort eq 'a') {
193 for (sort {$::a cmp $::b; } keys %$tagTocc) {
194 print F_TAB "$alnum $_\n";
195 $occTmisc->{$tagTocc->{$_}} = [ $alnum, $_ ];
196 $alnum++;
197 }
198 print STDERR "Index [$obase.tab] is sorted by alphabetic order\n";
199 }
200 elsif ($::mode_sort eq 'o' || 1) {
201 for (sort {$tagTocc->{$::a} <=> $tagTocc->{$::b}; } keys %$tagTocc) {
202 print F_TAB "$alnum $_\n";
203 $occTmisc->{$tagTocc->{$_}} = [ $alnum, $_ ];
204 $alnum++;
205 }
206 print STDERR "Index [$obase.tab] is sorted by occurrence order\n";
207 print STDERR "Primary and secondary occurrences are considered equal\n";
208 }
209 my $ct = keys %$occTmisc;
210 print F_MAP "(mclheader\nmcltype matrix\ndimensions $ct", 'x',
211 "$ct\n)\n(mclmatrix\nbegin\n";
212 for (sort {$::a <=> $::b; } keys %$occTmisc) {
213 # print F_MAP "$_ ", $occTmisc->{$_}[0], " ", $occTmisc->{$_}[1], "\n";
214 print F_MAP "$_ $occTmisc->{$_}[0] \$\n";
215 }
216 print F_MAP ")\n";
217
218 print F_HDR "(mclheader\nmcltype matrix\ndimensions ";
219 print F_HDR $::ID . 'x' . $::ID;
220 print F_HDR "\n)\n";
221 close F_TAB;
222 close F_HDR;
223 close F_MAP;
224 }
225
226
227 my $n_err = 0;
228 for (sort keys %$::seenrgt) {
229 if (!$::seenlft->{$_}) {
230 print $f_err "secondary element $_ not seen as primary element\n";
231 print $f_err "emergency measure: added the element to the primary list\n";
232 $n_err++;
233 }
234 }
235
236 if ($n_err) {
237 print STDERR $me, "$n_err secondary elements not seen as primary element\n";
238 print STDERR $me, "I added all of them\n";
239 print STDERR $me, "There were $::CT elements in all\n";
240 }
241 else {
242 print STDERR $me,
243 "all secondary elements were also seen as primary elements (check ok)\n";
244 }
245
246
247 sub munge_linewise {
248
249 my ($fh_in, $fh_raw, $line_mode) = @_;
250
251 my $gix_prev = "";
252
253 while (<$fh_in>) {
254
255 next if /^#/;
256 chomp;
257 my $sc_abc = 0;
258
259 my ($gix, $giy, $percent_id, $hsp_len, $mismatches, $gapsm,
260 $qstart, $qend,$hstart, $hend, $e, $b)
261 =
262 split;
263
264 my $s = 0;
265
266 my $idx = getid($gix, 1);
267 next unless $idx >= 0;
268
269 if ($gix_prev ne $gix) {
270 if (!$line_mode) {
271 if ($gix_prev) {
272 print $fh_raw "\$\n";
273 }
274 if( $idx >= 0 ) {
275 print $fh_raw "$idx ";
276 }
277 }
278 }
279 my $idy = getid($giy, 0);
280
281 $s = getscore($e, $b, $hsp_len);
282
283 if ($idy >= 0) {
284 if (!$line_mode) {
285 print $fh_raw "$idy:$s ";
286 }
287 else {
288 print $fh_raw "$gix\t$giy\t$s\n";
289 }
290 }
291 $gix_prev = $gix;
292 }
293
294 if ($gix_prev) {
295 print $fh_raw "\$\n" if !$line_mode;
296 }
297 }
298
299
300 sub munge_long {
301
302 my ($fh_in, $fh_raw, $line_mode) = @_;
303
304 my $need_query = 1;
305 my $need_hits = 2;
306 my $need_gi = 3;
307
308 my $state = $need_query;
309
310 while (<$fh_in>) {
311 $lc++;
312 chomp;
313 if (/^Query=\s+gi\|(\d+(_\d+)?)/ || /^Query=\s+(\S+).*$/) {
314 # warn "STATE query $1\n";
315 if ($state != $need_query) {
316 print STDERR "Unexpected 'Query=' line\n";
317 }
318 $gix = $1;
319 my $idx = getid($gix, 1);
320
321 if ($idx >= 0) {
322 print $fh_raw "$idx " if !$line_mode;
323 }
324 $state = $need_hits;
325 }
326 elsif (/^Query=/) {
327 print STDERR "Query string not recognized: $_\n";
328 }
329 elsif
330 ($state == $need_hits && /sequences producing significant alignments/i) {
331 $state = $need_gi;
332 # warn "STATE significant\n";
333 }
334 elsif ($state == $need_hits && /no hits found/i) {
335 print STDERR "no hits found for gi $gix\n";
336 print $fh_raw "\$\n" if !$line_mode;
337 $state = $need_query;
338 }
339 elsif
340 ( $state == $need_gi
341 && ! /^>/
342 && (/^gi\|(\d+(_\d+)?)/ || /^(\S+)\s+.*$/)
343 )
344 { $giy = $1;
345 my $idy = getid($giy, 0);
346 my ($s, $b, $e);
347
348 if (/(\S+)\s+(\S+)\s*$/) {
349 $b = +$1;
350 $e = +$2;
351 }
352 else {
353 print STDERR "no scores in line $lc [$_]!\n";
354 next;
355 }
356
357 $s = getscore($e, $b, 0);
358
359 if ($idy >= 0) { # fixme, void or explain.
360 if (!$line_mode) {
361 print $fh_raw "$idy:$s ";
362 }
363 else {
364 print $fh_raw "$giy\t$gix\t$s\n";
365 }
366 }
367 }
368 elsif (/^\s*$/) {
369 # paragraph skip does not change state, including the $need_gi case.
370 }
371 elsif (/(Statistics|Parameters):/) {
372 $state = $need_query;
373 # this provides WU-blast compatibility.
374 }
375 elsif ($state == $need_gi) {
376 print $fh_raw "\$\n" if !$line_mode;
377 $state = $need_query;
378 }
379 }
380
381 if ($state == $need_gi) {
382 print $f_err "run ended while expecting more secondary scores\n";
383 print STDERR "run ended while expecting more secondary scores\n";
384 print $fh_raw "\$\n" if !$line_mode;
385 }
386 }
387
388
389
390
391 sub read_tab {
392 my $file = shift;
393 my $tab = shift;
394 open (U_TAB, "<$file") || die "cannot open $file\n";
395 while (<U_TAB>) {
396 if (/^\s*#/) {
397 next;
398 }
399 else {
400 if (/^(\d+)\s+(.*)/) {
401 $tab->{$2} = $1;
402 }
403 else {
404 print STDERR "___ cannot parse line: $_";
405 }
406 }
407 }
408 }
409
410
411 sub getscore {
412 my ($e, $b, $hl) = @_;
413 my $s = 0;
414
415 if ($::mode_score eq 'e') {
416 $e = "1$e" if $e =~ /^e/;
417 $s = $e > 0 ? -log($e)/log(10) : 200;
418 if ($s > 200) {
419 $s = 200;
420 }
421 $s = $s > $::ecut ? $s : 0;
422 }
423 elsif ($::mode_score eq 'b') {
424 $s = $b > $::bcut ? $b : 0;
425 }
426 elsif ($::mode_score eq 'r' && $hl) {
427 $s = $b / $hl;
428 $s = $s > $::rcut ? $s : 0;
429 }
430 return $s;
431 }
432
433
434 sub getid {
435 my ($gi, $is_a_query) = @_;
436 my $id = -1;
437
438 if ($user_tabfile) {
439 if (defined($::TAB_user->{$gi})) {
440 $id = $::TAB_user->{$gi};
441 }
442 else {
443 print STDERR "___ no user tab entry for label <$gi>\n";
444 return -1;
445 }
446 }
447 else {
448 if (!exists($tagTocc->{$gi})) {
449 # warn "$is_a_query $gi <-> $::ID\n";
450 $tagTocc->{$gi} = $::ID++;
451 }
452 $id = $tagTocc->{$gi};
453 }
454
455 $::seenrgt->{$id}++;
456 if ($is_a_query) {
457 $::seenlft->{$id}++;
458 }
459 return $id;
460 }
461
462
463 sub help {
464 print <<_help_;
465 Usage: mcxdeblast <options> file-name
466 where file-name is in BLAST NCBI format.
467 mcxdeblast will create
468 base.hdr [to be read by mcxassemble]
469 base.raw [to be read by mcxassemble]
470 base.map [to be read by mcxassemble]
471 base.tab [to be read by clmformat]
472 base.err [error log]
473 where base is derived from or equal to file-name
474 Options:
475 --m9 Expect column (-m 9) input.
476 --line-mode=abc Output simple ID1 ID2 SCORE format.
477 --score=<b|e|r> Use bit scores, E-values,
478 or bit scores normalized by hsp-length
479 --sort=<a|o> Use alphabetic sorting (default) or occurrence.
480 --tab=<fname> Use user-supplied tab file.
481 --xi-dat=<suf> Strip <suf> from file-name to create output base name.
482 --xo-dat=<suf> Add <suf> to base name.
483 --bcut=<val> Ignore hits below bit score <val>
484 --ecut=<val> Ignore hits below E-value <val>
485 --rcut=<val> Ignore hits below raw value <val>
486 --out=<fname> Output file name ('-' for STDOUT)
487 _help_
488 }
489