-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathconvert_table.py
executable file
·100 lines (74 loc) · 4.04 KB
/
convert_table.py
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
#!/usr/bin/env python
import argparse
from importlib.metadata import version
from picrust2.util import (check_files_exist, contrib_to_legacy,
convert_humann2_to_picrust2,
convert_picrust2_to_humann2,
convert_picrust2_to_humann2_merged)
CONVERSION_CHOICES = ['contrib_to_legacy',
'humann2_strat_to_picrust2',
'humann2_unstrat_to_picrust2',
'picrust2_unstrat_to_humann2_split',
'picrust2_strat_to_humann2_split',
'picrust2_to_humann2_merged']
parser = argparse.ArgumentParser(
description='Converts to and from PICRUSt2 function abundance table. '
'Currently supports converting to legacy contributional format and to and '
'from HUMAnN2 function tables. Both stratified and unstratified tables '
'are supported. Note that the categories like \"UNMAPPED\" in the HUMAnN2 '
'tables will be removed if they have values of 0. The PICRUSt2 '
'\"strat\" format refers to the deprecated wide-table stratified table '
'format only.',
epilog='''
Usage example:
convert_table.py -o output.tsv -c humann2_strat_to_picrust2 humann2_out/*.tsv
''', formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('input_files', metavar='INPUT', type=str, nargs='+',
help='Input table to convert. If there are multiple input '
'files (e.g. if multiple HUMAnN2 gene tables for '
'different samples should be converted to a single '
'PICRUSt2 table) then specify them all: file1 file2 '
'file3...')
parser.add_argument('-o', '--output', metavar='OUTPUT', required=True,
type=str,
help='Path to output. Corresponds to folder name if '
'multiple files are output, otherwise it will be a '
'filename.')
parser.add_argument('-c', '--conversion', metavar='CONVERSION', required=True,
choices=CONVERSION_CHOICES,
help='Type of conversion to perform '
'(\'contrib_to_legacy\', '
'\'humann2_unstrat_to_picrust2\', '
'\'humann2_strat_to_picrust2\', '
'\'picrust2_unstrat_to_humann2_split\', '
'\'picrust2_strat_to_humann2_split\', or '
'\'picrust2_to_humann2_merged\').')
parser.add_argument('--raw_abun', default=False, action='store_true',
help='When converting to legacy contributional table '
'output abundance (i.e. OTUAbundanceInSample column) '
'as raw abundance rather than relative abundances '
'(relative abundances are the default).')
parser.add_argument('-v', '--version', default=False, action='version',
version="PICRUSt2 " + version('PICRUSt2'))
def main():
args = parser.parse_args()
# Check that input files exist.
check_files_exist(args.input_files)
if args.conversion == 'contrib_to_legacy':
if args.raw_abun:
rel_abun_set = False
else:
rel_abun_set = True
contrib_to_legacy(args.input_files, args.output, rel_abun_set)
elif args.conversion == 'humann2_unstrat_to_picrust2':
convert_humann2_to_picrust2(args.input_files, args.output, False)
elif args.conversion == 'humann2_strat_to_picrust2':
convert_humann2_to_picrust2(args.input_files, args.output, True)
elif args.conversion == 'picrust2_unstrat_to_humann2_split':
convert_picrust2_to_humann2(args.input_files, args.output, False)
elif args.conversion == 'picrust2_strat_to_humann2_split':
convert_picrust2_to_humann2(args.input_files, args.output, True)
elif args.conversion == 'picrust2_to_humann2_merged':
convert_picrust2_to_humann2_merged(args.input_files, args.output)
if __name__ == '__main__':
main()