-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcount_cat_tax.py
executable file
·67 lines (56 loc) · 2.52 KB
/
count_cat_tax.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
#! /usr/bin/env python
"""
Adds taxonomy information from TSV files to count tables
Copyright:
count_cat_tax.py Combine taxonomy file and count table
Copyright (C) 2016 William Brazelton, Alex Hyer
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import argparse
import sys
def main():
otus = {}
header = ['taxonomy']
with open(args.tsv, 'rU') as tsv_handle:
for line in tsv_handle:
columns = line.strip().split('\t')
otus[columns[0]] = [';'.join(columns[1:])]
with open(args.count_table, 'rU') as count_handle:
header = count_handle.readline().strip().split('\t') + header
for line in count_handle:
columns = line.strip().split('\t')
otus[columns[0]] = columns[1:] + otus[columns[0]]
with open(args.output, 'w') as out_handle:
out_handle.write('Representative Sequences\t'.join(header) + '\n')
for key in otus.keys():
if len(otus[key]) == 1:
print('WARNING: {0} may not be in {1}'.format(
key, args.count_table))
otus[key] = ['NA' for i in range(len(header[1:]))] + otus[key]
output = key + '\t' + '\t'.join(otus[key]) + '\n'
out_handle.write(output)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.
RawDescriptionHelpFormatter)
parser.add_argument('-t', '--tsv',
required=True,
help='taxonomy TSV file from taxonomy2tsv.py')
parser.add_argument('-c', '--count_table',
required=True,
help='Count table file from Mothur')
parser.add_argument('-o', '--output',
required=True,
help='File to write new table to')
args = parser.parse_args()
main()
sys.exit(0)