annotate home/ubuntu/lefse_to_export/lefse2circlader.py @ 2:a31c10fe09c8 draft default tip

Fixed bug due to numerical approximation after normalization affecting root-level clades (e.g. "Bacteria" or "Archaea")
author george-weingart
date Tue, 07 Jul 2015 13:52:29 -0400
parents db64b6287cd6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
1 #!/usr/bin/env python
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
2
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
3 from __future__ import with_statement
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
4
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
5 import sys
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
6 import os
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
7 import argparse
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
8
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
9 def read_params(args):
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
10 parser = argparse.ArgumentParser(description='Convert LEfSe output to '
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
11 'Circlader input')
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
12 parser.add_argument( 'inp_f', metavar='INPUT_FILE', nargs='?',
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
13 default=None, type=str,
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
14 help="the input file [stdin if not present]")
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
15 parser.add_argument( 'out_f', metavar='OUTPUT_FILE', nargs='?',
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
16 default=None, type=str,
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
17 help="the output file [stdout if not present]")
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
18 parser.add_argument('-l', metavar='levels with label', default=0, type=int)
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
19
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
20 return vars(parser.parse_args())
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
21
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
22 def lefse2circlader(par):
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
23 finp,fout = bool(par['inp_f']), bool(par['out_f'])
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
24
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
25 with open(par['inp_f']) if finp else sys.stdin as inpf:
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
26 put_bm = (l.strip().split('\t') for l in inpf.readlines())
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
27 biomarkers = [p for p in put_bm if len(p) > 2]
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
28
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
29 circ = [ [ b[0],
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
30 "" if b[0].count('.') > par['l'] else b[0].split('.')[-1],
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
31 b[2],
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
32 b[2]+"_col" ] for b in biomarkers]
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
33
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
34 with open(par['out_f'],'w') if fout else sys.stdout as out_file:
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
35 for c in circ:
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
36 out_file.write( "\t".join( c ) + "\n" )
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
37
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
38 if __name__ == '__main__':
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
39 params = read_params(sys.argv)
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
40 lefse2circlader(params)
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
41
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
42
db64b6287cd6 Modified datatypes
george-weingart
parents:
diff changeset
43