forked from lh3/bioawk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bioawk.1
784 lines (779 loc) · 16.2 KB
/
bioawk.1
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
.de EX
.nf
.ft CW
..
.de EE
.br
.fi
.ft 1
..
bioawk
.TH BIOAWK 1
.CT 1 files prog_other
.SH NAME
bioawk \- pattern-directed scanning and processing language for biological formats
.SH SYNOPSIS
.B bioawk
[
.BI \-F
.I fs
|
.BI \-c
{
.I hdr,bed,gff,vcf,sam,fastx
}
]
[
.BI \-v
.I var=value
]
[
.I 'prog'
|
.BI \-f
.I progfile
]
[
.I file ...
]
.SH DESCRIPTION
.I bioawk
is an extension to Brian Kernighan's awk, adding the support for
several common biological data formats, including optionally gzip'ed
.I BED
,
.I GFF
,
.I SAM
,
.I VCF
,
.I FASTA/Q
and
.I TAB-delimited
formats with column names. It also adds a
few built-in functions and an command line option to use TAB as the
input/output delimiter. When the new functionality is not used, bioawk is
intended to behave exactly the same as the
.I awk
.
.PP
.I Bioawk
scans each input
.I file
for lines that match any of a set of patterns specified literally in
.IR prog
or in one or more files
specified as
.B \-f
.IR progfile .
With each pattern
there can be an associated action that will be performed
when a line of a
.I file
matches the pattern.
Each line is matched against the
pattern portion of every pattern-action statement;
the associated action is performed for each matched pattern.
The file name
.B \-
means the standard input.
Any
.IR file
of the form
.I var=value
is treated as an assignment, not a filename,
and is executed at the time it would have been opened if it were a filename.
The option
.B \-v
followed by
.I var=value
is an assignment to be done before
.I prog
is executed;
any number of
.B \-v
options may be present.
The
.B \-F
.IR fs
option defines the input field separator to be the regular expression
.IR fs.
.PP
An input line is normally made up of fields separated by white space,
or by regular expression
.BR FS .
The fields are denoted
.BR $1 ,
.BR $2 ,
\&..., while
.B $0
refers to the entire line.
If
.BR FS
is null, the input line is split into one field per character.
.PP
A pattern-action statement has the form:
.IP
.IB pattern " { " action " }
.PP
A missing
.BI { " action " }
means print the line;
a missing pattern always matches.
Pattern-action statements are separated by newlines or semicolons.
.PP
An action is a sequence of statements.
A statement can be one of the following:
.PP
.EX
.ta \w'\f(CWdelete array[expression]\fR'u
.RS
.nf
.ft CW
if(\fI expression \fP)\fI statement \fP\fR[ \fPelse\fI statement \fP\fR]\fP
while(\fI expression \fP)\fI statement\fP
for(\fI expression \fP;\fI expression \fP;\fI expression \fP)\fI statement\fP
for(\fI var \fPin\fI array \fP)\fI statement\fP
do\fI statement \fPwhile(\fI expression \fP)
break
continue
{\fR [\fP\fI statement ... \fP\fR] \fP}
\fIexpression\fP #\fR commonly\fP\fI var = expression\fP
print\fR [ \fP\fIexpression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fP
printf\fI format \fP\fR[ \fP,\fI expression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fP
return\fR [ \fP\fIexpression \fP\fR]\fP
next #\fR skip remaining patterns on this input line\fP
nextfile #\fR skip rest of this file, open next, start at top\fP
delete\fI array\fP[\fI expression \fP] #\fR delete an array element\fP
delete\fI array\fP #\fR delete all elements of array\fP
exit\fR [ \fP\fIexpression \fP\fR]\fP #\fR exit immediately; status is \fP\fIexpression\fP
.fi
.RE
.EE
.DT
.PP
Statements are terminated by
semicolons, newlines or right braces.
An empty
.I expression-list
stands for
.BR $0 .
String constants are quoted \&\f(CW"\ "\fR,
with the usual C escapes recognized within.
Expressions take on string or numeric values as appropriate,
and are built using the operators
.B + \- * / % ^
(exponentiation), and concatenation (indicated by white space).
The operators
.B
! ++ \-\- += \-= *= /= %= ^= > >= < <= == != ?:
are also available in expressions.
Variables may be scalars, array elements
(denoted
.IB x [ i ] \fR)
or fields.
Variables are initialized to the null string.
Array subscripts may be any string,
not necessarily numeric;
this allows for a form of associative memory.
Multiple subscripts such as
.B [i,j,k]
are permitted; the constituents are concatenated,
separated by the value of
.BR SUBSEP .
.PP
The
.B print
statement prints its arguments on the standard output
(or on a file if
.BI > " file
or
.BI >> " file
is present or on a pipe if
.BI | " cmd
is present), separated by the current output field separator,
and terminated by the output record separator.
.I file
and
.I cmd
may be literal names or parenthesized expressions;
identical string values in different statements denote
the same open file.
The
.B printf
statement formats its expression list according to the
.I format
(see
.IR printf (3)).
The built-in function
.BI close( expr )
closes the file or pipe
.IR expr .
The built-in function
.BI fflush( expr )
flushes any buffered output for the file or pipe
.IR expr .
.PP
The mathematical functions
.BR exp ,
.BR log ,
.BR sqrt ,
.BR sin ,
.BR cos ,
and
.BR atan2
are built in.
Other built-in functions:
.TF length
.TP
.B length
the length of its argument
taken as a string,
number of elements in an array for an array argument,
or length of
.B $0
if no argument.
.TP
.B rand
random number on [0,1).
.TP
.B srand
sets seed for
.B rand
and returns the previous seed.
.TP
.B int
truncates to an integer value.
.TP
\fBsubstr(\fIs\fB, \fIm\fR [\fB, \fIn\^\fR]\fB)\fR
the
.IR n -character
substring of
.I s
that begins at position
.I m
counted from 1.
If no
.IR n ,
use the rest of the string.
.TP
.BI index( s , " t" )
the position in
.I s
where the string
.I t
occurs, or 0 if it does not.
.TP
.BI match( s , " r" )
the position in
.I s
where the regular expression
.I r
occurs, or 0 if it does not.
The variables
.B RSTART
and
.B RLENGTH
are set to the position and length of the matched string.
.TP
\fBsplit(\fIs\fB, \fIa \fR[\fB, \fIfs\^\fR]\fB)\fR
splits the string
.I s
into array elements
.IB a [1] \fR,
.IB a [2] \fR,
\&...,
.IB a [ n ] \fR,
and returns
.IR n .
The separation is done with the regular expression
.I fs
or with the field separator
.B FS
if
.I fs
is not given.
An empty string as field separator splits the string
into one array element per character.
.TP
\fBsub(\fIr\fB, \fIt \fR[, \fIs\^\fR]\fB)
substitutes
.I t
for the first occurrence of the regular expression
.I r
in the string
.IR s .
If
.I s
is not given,
.B $0
is used.
.TP
\fBgsub(\fIr\fB, \fIt \fR[, \fIs\^\fR]\fB)
same as
.B sub
except that all occurrences of the regular expression
are replaced;
.B sub
and
.B gsub
return the number of replacements.
.TP
.BI sprintf( fmt , " expr" , " ...\fB)
the string resulting from formatting
.I expr ...
according to the
.IR printf (3)
format
.IR fmt .
.TP
.BI system( cmd )
executes
.I cmd
and returns its exit status. This will be \-1 upon error,
.IR cmd 's
exit status upon a normal exit,
256 +
.I sig
upon death-by-signal, where
.I sig
is the number of the murdering signal,
or 512 +
.I sig
if there was a core dump.
.TP
.BI tolower( str )
returns a copy of
.I str
with all upper-case characters translated to their
corresponding lower-case equivalents.
.TP
.BI toupper( str )
returns a copy of
.I str
with all lower-case characters translated to their
corresponding upper-case equivalents.
.PD
.PP
The ``function''
.B getline
sets
.B $0
to the next input record from the current input file;
.B getline
.BI < " file
sets
.B $0
to the next record from
.IR file .
.B getline
.I x
sets variable
.I x
instead.
Finally,
.IB cmd " | getline
pipes the output of
.I cmd
into
.BR getline ;
each call of
.B getline
returns the next line of output from
.IR cmd .
In all cases,
.B getline
returns 1 for a successful input,
0 for end of file, and \-1 for an error.
.PP
Patterns are arbitrary Boolean combinations
(with
.BR "! || &&" )
of regular expressions and
relational expressions.
Regular expressions are as in
.IR egrep ;
see
.IR grep (1).
Isolated regular expressions
in a pattern apply to the entire line.
Regular expressions may also occur in
relational expressions, using the operators
.B ~
and
.BR !~ .
.BI / re /
is a constant regular expression;
any string (constant or variable) may be used
as a regular expression, except in the position of an isolated regular expression
in a pattern.
.PP
A pattern may consist of two patterns separated by a comma;
in this case, the action is performed for all lines
from an occurrence of the first pattern
though an occurrence of the second.
.PP
A relational expression is one of the following:
.IP
.I expression matchop regular-expression
.br
.I expression relop expression
.br
.IB expression " in " array-name
.br
.BI ( expr , expr,... ") in " array-name
.PP
where a
.I relop
is any of the six relational operators in C,
and a
.I matchop
is either
.B ~
(matches)
or
.B !~
(does not match).
A conditional is an arithmetic expression,
a relational expression,
or a Boolean combination
of these.
.PP
The special patterns
.B BEGIN
and
.B END
may be used to capture control before the first input line is read
and after the last.
.B BEGIN
and
.B END
do not combine with other patterns.
They may appear multiple times in a program and execute
in the order they are read by
.IR awk .
.PP
Variable names with special meanings:
.TF FILENAME
.TP
.B ARGC
argument count, assignable.
.TP
.B ARGV
argument array, assignable;
non-null members are taken as filenames.
.TP
.B CONVFMT
conversion format used when converting numbers
(default
.BR "%.6g" ).
.TP
.B ENVIRON
array of environment variables; subscripts are names.
.TP
.B FILENAME
the name of the current input file.
.TP
.B FNR
ordinal number of the current record in the current file.
.TP
.B FS
regular expression used to separate fields; also settable
by option
.BI \-F fs\fR.
.TP
.BR NF
number of fields in the current record.
.TP
.B NR
ordinal number of the current record.
.TP
.B OFMT
output format for numbers (default
.BR "%.6g" ).
.TP
.B OFS
output field separator (default space).
.TP
.B ORS
output record separator (default newline).
.TP
.B RLENGTH
the length of a string matched by
.BR match .
.TP
.B RS
input record separator (default newline).
If empty, blank lines separate records.
If more than one character long,
.B RS
is treated as a regular expression, and records are
separated by text matching the expression.
.TP
.B RSTART
the start position of a string matched by
.BR match .
.TP
.B SUBSEP
separates multiple subscripts (default 034).
.PD
.PP
Functions may be defined (at the position of a pattern-action statement) thus:
.IP
.B
function foo(a, b, c) { ...; return x }
.PP
Parameters are passed by value if scalar and by reference if array name;
functions may be called recursively.
Parameters are local to the function; all other variables are global.
Thus local variables may be created by providing excess parameters in
the function definition.
.SH ENVIRONMENT VARIABLES
If
.B POSIXLY_CORRECT
is set in the environment, then
.I awk
follows the POSIX rules for
.B sub
and
.B gsub
with respect to consecutive backslashes and ampersands.
.SH EXAMPLES
.TP
.EX
length($0) > 72
.EE
Print lines longer than 72 characters.
.TP
.EX
{ print $2, $1 }
.EE
Print first two fields in opposite order.
.PP
.EX
BEGIN { FS = ",[ \et]*|[ \et]+" }
{ print $2, $1 }
.EE
.ns
.IP
Same, with input fields separated by comma and/or spaces and tabs.
.PP
.EX
.nf
{ s += $1 }
END { print "sum is", s, " average is", s/NR }
.fi
.EE
.ns
.IP
Add up first column, print sum and average.
.TP
.EX
/start/, /stop/
.EE
Print all lines between start/stop pairs.
.PP
.EX
.nf
BEGIN { # Simulate echo(1)
for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i]
printf "\en"
exit }
.fi
.EE
.SH BIOAWK EXTENSIONS
.PP
Bioawk adds a new
.B \-c
.IR fmt
option that specifies the input format. The behavior
of bioawk may vary depending on the value of
.IR fmt .
When
.I fmt
is
.IR header " or " hdr ,
bioawk parses named columns. It automatically adds variables whose names are
taken from the first line and values from the column index. Special characters
are converted to a underscore. When
.I fmt
is
.IR sam , " vcf" , " bed " or " gff" ,
it sets predefined column names. The column names for the different formats are as follows:
.IP bed
1:chrom 2:start 3:end 4:name 5:score 6:strand 7:thickstart 8:thickend 9:rgb 10:blockcount 11:blocksizes 12:blockstarts
.IP sam
1:qname 2:flag 3:rname 4:pos 5:mapq 6:cigar 7:rnext 8:pnext 9:tlen 10:seq 11:qual 12:tags
.IP vcf
1:chrom 2:pos 3:id 4:ref 5:alt 6:qual 7:filter 8:info
.IP gff
1:seqname 2:source 3:feature 4:start 5:end 6:score 7:strand 8:frame 9:attribute
.IP fastx
1:name 2:seq 3:qual 4:comment
.PP
Users may check out the predefined column names by:
.B -c
.IR help .
.PP
Note that when
.B -c
.I fmt
is in use, the input file can be optionally gzip'ed.
.PP
Bioawk also adds more built-in functions:
.TF length
.TP
.BI gc( s )
return the GC-content of a nucleotide string
.I s
.TP
.BI gffattr( s , " a" )
split the gff attribute string
.I s
into the array
.I a.
The value of
.I s
must be a properly formatted gff attribute string such as ID=PX161;Product=gene_name;EC_number=1.1.1.1. The resulting array
.I a
will have the keys ID, Product and EC_number. And the values will be PD161, gene_name, and 1.1.1.1.
If a key has multiple values they will not be split into a multidimentional array.
Furthermore, if a key is listed more than once, only the value of the second key will be saved.
.TP
.BI meanqual( s )
calculate the mean quality score from a string
.I s
that is using Sanger fastq encoding for phred scores.
.TP
.BI medianqual( s )
calculate the median quality score from a string
.I s
that is using Sanger fastq encoding for phred scores.
.TP
.BI maxqual( s )
calculate the max quality score from a string
.I s
that is using Sanger fastq encoding for phred scores.
.TP
.BI minqual( s )
calculate the min quality score from a string
.I s
that is using Sanger fastq encoding for phred scores.
.TP
.BI reverse( s )
reverse string
.I s
.TP
.BI revcomp( s )
reverse complement a nucleotide string
.I s
in the IUB encoding.
.TP
\fBtranslate(\fIs\fR [\fB, \fItable\^\fR]\fB)\fR
translate nucleotide sequence
.I s
into its amino acid counterpart. The
.I table
parameter is optional, if not given the standard (code 1) translation table is used.
Returns the translation of the nucleotide sequence.
.TP
\fBtrimq(\fIqual\fB, \fIbeg\fR, \fIend\fR [\fB, \fIparam\^\fR]\fB)\fR
trim quality string
.I qual
in the Sanger scale using Richard Mott's algorithm (used in Phred). The
0-based beginning and ending positions are written to
.IR beg " and " end ,
respectively. The last arguement
.I param
is the single parameter used in the algorithm, which is optional and defaults 0.05.
.TP
.BI and( x , " y" )
bit AND operation (& in C)
.TP
.BI or( x , " y" )
bit OR operation (| in C)
.TP
.BI xor( x , " y" )
bit XOR operation (^ in C)
.TP
.BI lshift( x , " y" )
bit left shift operation (<< in C)
.TP
.BI rshift( x , " y" )
bit right shift operation (>> in C)
.TP
.BI compl( x )
bit complement operation (~ in C)
.SH SEE ALSO
.IR awk (1),
.IR grep (1),
.IR lex (1),
.IR sed (1)
.br
A. V. Aho, B. W. Kernighan, P. J. Weinberger,
.IR "The AWK Programming Language" ,
Addison-Wesley, 1988. ISBN 0-201-07981-X.
.SH BUGS
There are no explicit conversions between numbers and strings.
To force an expression to be treated as a number add 0 to it;
to force it to be treated as a string concatenate
\&\f(CW""\fP to it.
.PP
The scope rules for variables in functions are a botch;
the syntax is worse.
.PP
Only eight-bit characters sets are handled correctly.
.SH UNUSUAL FLOATING-POINT VALUES
.I Awk
was designed before IEEE 754 arithmetic defined Not-A-Number (NaN)
and Infinity values, which are supported by all modern floating-point
hardware.
.PP
Because
.I awk
uses
.IR strtod (3)
and
.IR atof (3)
to convert string values to double-precision floating-point values,
modern C libraries also convert strings starting with
.B inf
and
.B nan
into infinity and NaN values respectively. This led to strange results,
with something like this:
.PP
.EX
.nf
echo nancy | awk '{ print $1 + 0 }'
.fi
.EE
.PP
printing
.B nan
instead of zero.
.PP
.I Awk
now follows GNU AWK, and prefilters string values before attempting
to convert them to numbers, as follows:
.TP
.I "Hexadecimal values"
Hexadecimal values (allowed since C99) convert to zero, as they did
prior to C99.
.TP
.I "NaN values"
The two strings
.B +nan
and
.B \-nan
(case independent) convert to NaN. No others do.
(NaNs can have signs.)
.TP
.I "Infinity values"
The two strings
.B +inf
and
.B \-inf
(case independent) convert to positive and negative infinity, respectively.
No others do.