-
Notifications
You must be signed in to change notification settings - Fork 2
/
bb.fastaconsensus
executable file
·643 lines (564 loc) · 20.9 KB
/
bb.fastaconsensus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#!/usr/bin/perl
#----------------------------------------------------------#
# Author: Douglas Senalik [email protected] #
#----------------------------------------------------------#
# "Black Box" program series
=bb
Generate a consensus of a multi-fasta alignment
=cut bb
use strict;
use warnings;
use Getopt::Long; # for getting command line parameters
# 1.1 add --dropinvariant and some error checking
# 1.2 May 29, 2016, add --table, fix lower case problem
my $ver="1.3"; # 1.3 July 12, 2016, add --gapfraction
############################################################
# configuration variables
############################################################
my $defaultunknown = '?*-';
my $defaultwrap = 0;
my $defaultmajority = 20;
my $defaultgapfraction = 1;
my %degen = ( # lookup for degenerate bases
'A' => 'A',
'AC' => 'M',
'ACG' => 'V',
'ACGNT'=> 'N',
'ACGT' => 'N',
'ACN' => 'M',
'ACT' => 'H',
'AG' => 'R',
'AGN' => 'R',
'AGT' => 'D',
'AN' => 'A',
'AT' => 'W',
'ANT' => 'W',
'C' => 'C',
'CG' => 'S',
'CGN' => 'S',
'CGT' => 'B',
'CN' => 'C',
'CT' => 'Y',
'CNT' => 'Y',
'G' => 'G',
'GN' => 'G',
'GNT' => 'K',
'GT' => 'K',
'N' => 'N',
'NT' => 'T',
'T' => 'T' );
############################################################
# global variables
############################################################
my $ansiup = "\033[1A"; # terminal control
(my $prognopath = $0) =~ s/^.*[\/\\]//;
my @seq; # sequences loaded from $infilename
my %unk; # unknown base index from $unknown
my @counts;
my %allbases;
############################################################
# command line parameters
############################################################
my $infilename = ""; # input file name
my $outfilename = ""; # output file name
my $consensusonly;
my $unknown = $defaultunknown;
my $majority = $defaultmajority;
my $gapfraction = $defaultgapfraction;
my $nodegen = 0;
my $wrap = $defaultwrap;
my $report;
my $dropinvariant;
my $table;
my $help = 0; # print help and exit
my $quiet = 0; # only show errors
my $debug = 0; # print extra debugging information
GetOptions (
"infile=s" => \$infilename, # string
"outfile=s" => \$outfilename, # string
"majority=s" => \$majority, # string(real)
"gapfraction=s" => \$gapfraction, # string(real)
"nodegenerate" => \$nodegen, # flag
"consensusonly" => \$consensusonly, # flag
"report:s" => \$report, # flag|filename
"table=s" => \$table, # filename
"unknown=s" => \$unknown, # string(char list)
"wrap=i" => \$wrap, # integer
"dropinvariant" => \$dropinvariant, # flag
"help" => \$help, # flag
"quiet" => \$quiet, # flag
"debug" => \$debug); # flag
# debug implies not quiet
if ( $debug ) { $quiet = 0; }
unless ( $infilename ) { $help = 1 }
unless ( $outfilename ) { $help = 1 }
if ( $outfilename eq "-" ) { $quiet = 1 }
############################################################
# print help screen
############################################################
if ( $help )
{
print "$prognopath version $ver
This program takes an aligned multi-FASTA file as input, and generates
a consensus sequence based on the most abundant nucleotide
Required parameters:
--infile=xxx input multi-FASTA file name
--outfile=xxx output file name, consensus will be added as the
first sequence
filenames can be .gz compressed, or use - for STDIN/STDOUT
Optional parameters:
--majority=xxx if second most abundant base is within
this percentage of most abundant, then
generate a degenerate base. Default = $defaultmajority,
use --majority=0 to only select most abundant base
--gapfraction=xxx value between 0 and 1, if >= this fraction of sequences have
gap character, make consensus gap also. Default = $defaultgapfraction
--nodegen do not use degenerate base codes in the consensus
sequence. If equal frequencies, choose one at random
--consensusonly only output the consensus sequence
--unknown=xxx characters representing unknow bases which are ignored
for creating the consensus
default = \"$defaultunknown\"
--wrap=xxx wrap output sequences to this length,
default is no wrap
--report[=xxx] print a summary of base frequencies
optional value is output file name, default is STDOUT
--table=xxx save a table with all variant locations relative to
consensus: 'D' for deletion, 'I' for insertion,
'P' for polymorphism, '.' for same as consensus
--dropinvariant all locations where all sequences are identical are
eliminated, and sequences will therefore be shortened
--help print this screen
--quiet only print error messages
--debug print extra debugging information
";
exit 1;
} # if ( $help )
############################################################
# generate unknown base hash
############################################################
{
for my $i ( 0 .. length($unknown)-1 )
{ $unk{substr($unknown, $i, 1)} = 1; }
# to all degenerate combinations, add an additional one with gap character included
my @tmp = keys( %degen );
foreach my $u ( keys %unk )
{
$degen{$u} = $u;
foreach my $t ( @tmp )
{
# characters in key must be in alphabetical order
my $sortkey = join( '', sort( split(//, $u.$t ) ) );
$degen{$sortkey} = $degen{$t};
} # for $t
} # for $u
}
############################################################
# load input file
############################################################
debugmsg ( "Loading input file \"$infilename\"" );
@seq = loadfasta ( $infilename, 1 );
# do analysis in all upper case
foreach my $aseq ( @seq )
{ $aseq->{seq} = uc($aseq->{seq}); }
unless ( $quiet ) { print "Loaded ".commify(scalar @seq)." sequences\n"; }
############################################################
# error checking
############################################################
unless ( @seq ) { die "Error, no sequences loaded from \"$infilename\"\n"; }
unless ( ( scalar @seq ) > 1 ) { die "Error, only one sequence loaded from \"$infilename\"\n"; }
my $lzero = length( $seq[0]->{seq} );
my $okay = 1;
for my $i ( 1..$#seq )
{
my $li = length( $seq[$i]->{seq} );
if ( $li != $lzero )
{
$okay = 0;
print "Error, length of sequence #".($i+1).":$li is"
. " not equal to length of sequence #1:$lzero\n";
}
}
unless ( $okay ) { exit 1; }
############################################################
# optional --dropinvariant
############################################################
if ( $dropinvariant )
{
debugmsg ( "Drop invariant" );
my @newseq;
my $newlen = 0;
for my $i ( 0..$#seq )
{ $newseq[$i]->{hdr} = $seq[$i]->{hdr}; }
for my $i ( 0 .. length($seq[0]->{seq})-1 )
{
my %counts;
foreach my $aseq ( @seq )
{
my $base = substr($aseq->{seq}, $i, 1);
$counts{$base}++;
}
# will be only one key in %counts if all bases were identical
if ( ( scalar keys %counts ) > 1 )
{
for my $j ( 0..$#seq )
{
my $base = substr($seq[$j]->{seq}, $i, 1);
$newseq[$j]->{seq} .= $base;
}
$newlen++;
}
} # for $i
unless( $newlen )
{ die "Error, all sequences were identical and --dropinvariant was specified\n"; }
unless( $quiet )
{ print "Sequence length after removal of invariant sites is ".commify($newlen)."\n"; }
@seq = @newseq;
} # if ( $dropinvariant )
############################################################
# main processing loop
############################################################
{
debugmsg ( "Counting bases" );
foreach my $aseq ( @seq )
{
for my $i ( 0 .. length($aseq->{seq})-1 )
{
my $base = substr($aseq->{seq}, $i, 1);
# unless ( $unk{$base} ) # skip gaps
# {
$counts[$i]->{$base}++;
$allbases{$base}++;
# }
}
} # for $aseq
debugmsg ( "Generating consensus" );
my %consseq = ( 'seq' => '', 'hdr' => 'Consensus' );
my $ngapsincons = 0;
for my $i ( 0 .. $#counts )
{
# rank bases by frequency
my @byfreq = sort { $counts[$i]->{$b} <=> $counts[$i]->{$a} } keys %{$counts[$i]};
my $totalbases = 0;
foreach my $base ( keys %{$counts[$i]} )
{ $totalbases += $counts[$i]->{$base}; }
### Gap to consensus
if ( ( ( $counts[$i]->{'-'} // 0 ) / $totalbases ) >= $gapfraction )
{
$consseq{seq} .= '-';
$ngapsincons++;
}
else
{
### code for degenerate
my @pct;
my @deg;
for my $j ( 0 .. $#byfreq )
{
$pct[$j] = $counts[$i]->{$byfreq[$j]} * 100 / $totalbases;
# skip gaps in consensus, --gapfraction does that
unless ( $unk{$byfreq[$j]} )
{
unless ( @deg ) # first entry in list
{ push ( @deg, $byfreq[$j] ); }
else
{
if ( ( $pct[$j] + $majority ) >= $pct[0] )
{ push ( @deg, $byfreq[$j] ); }
else
{ last; }
} # else after first entry
} # unless gap character
} # for $j
my $degenstr = join ( '', sort ( @deg ) ); # alphabetical to simplify degenerate hash
if ( $nodegen )
{
if ( ( $degenstr =~ m/[\?N\-]/ ) and ( substr($degenstr,1,1) !~ m/[\?N\-]/ ))
{ $degenstr = substr($degenstr,1,1); }
else
{ $degenstr = substr($degenstr,0,1); }
}
my $db = $degen{$degenstr};
if ( $unk{$db} ) { $ngapsincons++; }
unless ( $db )
{
$db = "N";
print "Warning, cannot lookup degenerate base for \"$degenstr\" position $i totalbases=$totalbases, converting to \"N\"\n";
}
$consseq{seq} .= $db;
} # else not >= $gapfraction
} # for $i
unshift ( @seq, \%consseq );
unless ( $quiet ) { print commify($ngapsincons)." gaps in consensus\n"; }
}
############################################################
# save output file
############################################################
debugmsg ( "Saving output file \"$outfilename\"" );
if ( $consensusonly ) { @seq = $seq[0]; }
savefasta ( $outfilename, \@seq, $wrap );
############################################################
# --report
############################################################
if ( defined $report )
{
unless ( $report ) { $report = '-'; }
my $OUTF = stdopen ( ">", $report );
print $OUTF "Pos";
for my $base ( sort keys %allbases )
{ print $OUTF "\t", $base; }
print $OUTF "\n";
for my $i ( 0 .. $#counts )
{
print $OUTF $i+1;
for my $base ( sort keys %allbases )
{ print $OUTF "\t", ( $counts[$i]->{$base} // 0 ); }
print $OUTF "\n";
}
stdclose ( $OUTF );
} # if $report
############################################################
# --table
############################################################
if ( $table )
{
my $OUTF = stdopen ( ">", $table );
# header line
print $OUTF join( "\t", 'Start', 'End' );
foreach my $aseq ( @seq )
{
( my $id = $aseq->{hdr} ) =~ s/\s.*$//;
print $OUTF "\t", $id;
}
print $OUTF "\n";
# table body - record SNPs relative to consensus, and deletions
my %events; # 3-D hash, first index is start, second is end, third is genotype index, value is 'D' or 'P'
for my $i ( 0 .. $#seq )
{
my $status = '.';
my $coord;
for my $j ( 0 .. length($seq[$i]->{seq})-1 )
{
my $nt = substr($seq[$i]->{seq},$j,1);
my $connt = substr($seq[0]->{seq},$j,1);
if ( $nt eq $connt )
{
if ( $status ne '.' )
{ $events{$coord}->{$j-1}->[$i] = $status; }
$status = '.';
$coord = undef;
}
elsif ( $nt eq '-' )
{
if ( $status ne 'D' )
{
if ( $status ne '.' )
{ $events{$coord}->{$j-1}->[$i] = $status; }
$status = 'D';
$coord = $j;
}
}
elsif ( $connt eq '-' )
{
if ( $status ne 'I' )
{
if ( $status ne '.' )
{ $events{$coord}->{$j-1}->[$i] = $status; }
$status = 'I';
$coord = $j;
}
}
else
{
if ( $status ne 'P' )
{
if ( $status ne '.' )
{ $events{$coord}->{$j-1}->[$i] = $status; }
$status = 'P';
$coord = $j;
}
}
} # for $j
if ( $status ne '.' )
{ $events{$coord}->{length($seq[$i]->{seq})-1}->[$i] = $status; }
} # for $i
for my $i ( sort{$a<=>$b} keys %events ) # start coordinate
{
for my $j ( sort{$a<=>$b} keys %{$events{$i}} ) # end coordinate
{
print $OUTF join( "\t", $i+1, $j+1 );
for my $k ( 0 .. $#seq )
{
my $state = '.';
if ( ( $events{$i} ) and ( $events{$i}->{$j} ) and ( $events{$i}->{$j}->[$k] ) )
{ $state = $events{$i}->{$j}->[$k]; }
print $OUTF "\t", $state;
} # for $k
print $OUTF "\n";
} # for $j
} # for $i
} # if $table
############################################################
# cleanup and end program
############################################################
unless ( $quiet ) { print "$0 Done\n"; }
exit 0;
############################################################
sub debugmsg { my ( $text, $noreturn, $nolinenum ) = @_;
############################################################
if ( $debug )
{
my ($package, $filename, $line, $sub) = caller(0);
unless ( $nolinenum ) { $text = "Line $line: " . $text; }
if ( ! ( $noreturn ) ) { $text .= "\n"; }
print $text;
} # if ( $debug )
} # sub debugmsg
###############################################################
sub timestr {
###############################################################
@_ = localtime(shift || time);
return(sprintf("%04d/%02d/%02d %02d:%02d", $_[5]+1900, $_[4]+1, $_[3], @_[2,1]));
} # sub timestr
###############################################################
sub commify {
###############################################################
# http://perldoc.perl.org/perlfaq5.html#How-can-I-output-my-numbers-with-commas
local $_ = shift;
1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
return $_;
} # commify
###############################################################
sub stdopen { my ( $mode, $filename, $extratext ) = @_;
###############################################################
# a replacement for the three-parameter open which also allows
# the use of "-" as the file name to mean STDIN or STDOUT
my $fh; # the file handle
if ( $filename eq "-" ) # only exact match to "-" has special meaning
{
if ( $mode =~ m/>/ )
{ $fh = *STDOUT }
else
{ $fh = *STDIN }
}
else
{
# supplemental passed text for error messages, need one more space
if ( defined $extratext )
{ $extratext .= " " }
else
{ $extratext = "" }
my $text; # this is only used for error message
if ( $mode =~ m/^\+?>>/ ) # ">>" or "+>>"
{ $text = "append" }
elsif ( $mode =~ m/^\+?>/ ) # ">" or "+>"
{ $text = "output" }
elsif ( $mode =~ m/^\+?</ ) # "<" or "+<"
{ $text = "input" }
elsif ( $mode eq "-|" )
{ $text = "piped input" }
elsif ( $mode eq "|-" )
{ $text = "piped output" }
else
{ die "Error, unsupported file mode \"$mode\" specified to stdopen( $mode, $filename, $extratext )\n"; }
# if file name ends in ".gz", gzip compression is assumed, and handle it transparently
if ( $filename =~ m/\.gz$/ )
{
if ( $mode =~ m/^>$/ ) # output mode
{ $mode = "|-"; $filename = "gzip -c > \"$filename\""; }
elsif ( $mode =~ m/^<$/ ) # input mode
{ $mode = "-|"; $filename = "gunzip -c \"$filename\""; }
elsif ( $mode =~ m/^>>$/ ) # append mode
{ $mode = "|-"; $filename = "gzip -c >> \"$filename\""; }
else
{ die "Error, can't handle gzip compression with mode \"$mode\" for file \"filename\"\n"; }
} # if gzip compressed file
elsif ( $filename =~ m/\.bz2$/ )
{
if ( $mode =~ m/^>$/ ) # output mode
{ $mode = "|-"; $filename = "bzip2 -c > \"$filename\""; }
elsif ( $mode =~ m/^<$/ ) # input mode
{ $mode = "-|"; $filename = "bunzip2 -c \"$filename\""; }
else
{ die "Error, can't handle bzip2 compression with mode \"$mode\" for file \"filename\"\n"; }
}
open ( $fh, $mode, $filename ) or die ( "Error opening ${extratext}file \"$filename\" for $text: $!\n" );
}
# return the opened file handle to the caller
return $fh;
} # sub stdopen
###############################################################
sub stdclose { my ( $fh ) = @_;
###############################################################
# same as built-in close, except in case of STDIN or STDOUT,
# and in this case the file handle is not closed
unless ( fileno($fh) <= 2 ) # if file number is this low, is stdin or stdout or stderr
{ close ( $fh ) or die ( "Error closing file handle: $!\n" ); }
} # sub stdclose
###############################################################
sub loadfasta { my ( $infilename, $stripreturns ) = @_;
###############################################################
# load a single FASTA file into an array of hashes to separate header and sequence portion
# e.g. $seq[3]->{hdr} contains the header of the fourth sequence, $seq[3]->{seq} contains the sequence
# and the ">" is stripped off of the header
my @seq;
my $count = 0; # number of sequences loaded
my $mode = "<";
if ( $infilename =~ m/\.gz$/ )
{ $mode = "-|"; $infilename = "gunzip -c \"$infilename\""; }
my $INF = stdopen ( $mode, $infilename, 'input FASTA' );
while ( my $aline = <$INF> )
{
if ( $aline =~ m/^#/ ) # ignore comment lines
{ }
elsif ( $aline =~ m/^>(.*)$/ )
{
$aline =~ s/[\r\n]//g;
$aline =~ s/^>//;
$count++;
$seq[$count-1]->{hdr} = $aline;
}
else
{
if ( $stripreturns ) { $aline =~ s/[\r\n]//g; }
$seq[$count-1]->{seq} .= $aline;
}
} # while ( my $aline = <$INF> )
stdclose ( $INF );
return @seq;
} # sub loadfasta
###############################################################
sub savefasta { my ( $outfilename, $arrayref, $addreturns ) = @_;
###############################################################
# save a FASTA file in a hash as described by loadfasta above
# addreturns is the desired wrap length, or undefined or zero for no wrapping
my $OUTF = stdopen ( ">", $outfilename, 'output FASTA' );
foreach my $seqref ( @{$arrayref} )
{
next unless ( defined $seqref );
print $OUTF ">", $seqref->{"hdr"}, "\n";
my $s = $seqref->{"seq"};
if ( $addreturns )
{ $s =~ s/(.{$addreturns})/$1\n/g; }
print $OUTF $s;
unless ( $s =~ m/\n$/ ) { print $OUTF "\n"; }
} # foreach my $seqref ( @{$arrayref} )
stdclose ( $OUTF );
} # sub savefasta
###############################################################
sub indexfasta { my ( $seqref, $stripheaders ) = @_;
###############################################################
# if $stripheaders is true, remove anything in header after
# the first white space. Returns a hash reference, key is
# header, value is array index
my %index;
for my $i ( 0..$#{$seqref} )
{
my $hdr = $seqref->[$i]->{'hdr'};
if ( $stripheaders ) { $hdr =~ s/\s.*$//; }
if ( defined $index{$hdr} ) { die "Error, duplicate header \"$hdr\" while indexing\n"; }
$index{$hdr} = $i;
}
return \%index
} # sub indexfasta
# eof