Mercurial > repos > dannon > metaphlan
comparison metaphlan2krona.py @ 6:e951f9d38339 default tip
Added metaphlan2krona
| author | Dannon Baker <dannonbaker@me.com> |
|---|---|
| date | Tue, 08 Apr 2014 14:16:46 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 5:73f082e9fa2d | 6:e951f9d38339 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 # ============================================================================== | |
| 4 # Conversion script: from MetaPhlAn output to Krona text input file | |
| 5 # Author: Daniel Brami (daniel.brami@gmail.com) | |
| 6 # ============================================================================== | |
| 7 | |
| 8 import sys | |
| 9 import optparse | |
| 10 import re | |
| 11 | |
| 12 def main(): | |
| 13 #Parse Command Line | |
| 14 parser = optparse.OptionParser() | |
| 15 parser.add_option( '-p', '--profile', dest='profile', default='', action='store', help='The input file is the MetaPhlAn standard result file' ) | |
| 16 parser.add_option( '-k', '--krona', dest='krona', default='krona.out', action='store', help='the Krona output file name' ) | |
| 17 ( options, spillover ) = parser.parse_args() | |
| 18 | |
| 19 if not options.profile or not options.krona: | |
| 20 parser.print_help() | |
| 21 sys.exit() | |
| 22 | |
| 23 re_candidates = re.compile(r"s__|unclassified\t") | |
| 24 re_replace = re.compile(r"\w__") | |
| 25 re_bar = re.compile(r"\|") | |
| 26 | |
| 27 metaPhLan = list() | |
| 28 with open(options.profile,'r') as f: | |
| 29 metaPhLan = f.readlines() | |
| 30 f.close() | |
| 31 | |
| 32 krona_tmp = options.krona | |
| 33 metaPhLan_FH = open(krona_tmp, 'w') | |
| 34 | |
| 35 for aline in (metaPhLan): | |
| 36 if(re.search(re_candidates, aline)): | |
| 37 x=re.sub(re_replace, '\t', aline) | |
| 38 x=re.sub(re_bar, '', x) | |
| 39 | |
| 40 x_cells = x.split('\t') | |
| 41 lineage = '\t'.join(x_cells[0:(len(x_cells) -1)]) | |
| 42 abundance = float(x_cells[-1].rstrip('\n')) | |
| 43 | |
| 44 metaPhLan_FH.write('%s\n'%(str(abundance) + '\t' + lineage)) | |
| 45 | |
| 46 metaPhLan_FH.close() | |
| 47 | |
| 48 if __name__ == '__main__': | |
| 49 main() |
