annotate kraken_taxonomy_report.py @ 0:3f1a0d47ea8d draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
author iuc
date Wed, 01 Jun 2016 17:25:40 -0400
parents
children b97694b21bc3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
1 #!/usr/bin/env python
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
2
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
3 # Reports a summary of Kraken's results
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
4 # and optionally creates a newick Tree
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
5 # Copyright (c) 2016 Daniel Blankenberg
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
6 # Licensed under the Academic Free License version 3.0
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
7 # https://github.com/blankenberg/Kraken-Taxonomy-Report
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
8
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
9 import sys
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
10 import os
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
11 import optparse
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
12 import re
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
13
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
14 __VERSION__ = '0.0.1'
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
15
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
16 __URL__ = "https://github.com/blankenberg/Kraken-Taxonomy-Report"
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
17
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
18 # Rank names were pulled from ncbi nodes.dmp on 02/02/2016
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
19 # cat nodes.dmp | cut -f 5 | sort | uniq
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
20 # "root" is added manually
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
21 NO_RANK_NAME = "no rank"
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
22 RANK_NAMES = [ NO_RANK_NAME,
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
23 "root",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
24 "superkingdom",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
25 "kingdom",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
26 "subkingdom",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
27 "superphylum",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
28 "phylum",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
29 "subphylum",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
30 "superclass",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
31 "class",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
32 "subclass",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
33 "infraclass",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
34 "superorder",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
35 "order",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
36 "suborder",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
37 "infraorder",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
38 "parvorder",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
39 "superfamily",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
40 "family",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
41 "subfamily",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
42 "tribe",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
43 "subtribe",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
44 "genus",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
45 "subgenus",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
46 "species group",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
47 "species subgroup",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
48 "species",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
49 "subspecies",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
50 "varietas",
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
51 "forma" ]
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
52 # NB: We put 'no rank' at top of list for generating trees, due to e.g.
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
53 # root (root) -> cellular organisms (no rank) -> bacteria (superkingdom)
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
54
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
55 RANK_NAME_TO_INTS = dict( [ (y, x) for (x, y) in enumerate( RANK_NAMES ) ] )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
56 RANK_NAMES_INTS = range( len( RANK_NAMES ) )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
57
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
58 NO_RANK_INT = RANK_NAMES.index( NO_RANK_NAME )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
59 NO_RANK_CODE = 'n'
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
60
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
61 PRIMARY_RANK_NAMES = [ 'species', 'genus', 'family', 'order', 'class', 'phylum', 'kingdom' ]
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
62 RANK_INT_TO_CODE = {}
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
63 for name in PRIMARY_RANK_NAMES:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
64 RANK_INT_TO_CODE[ RANK_NAMES.index( name ) ] = name[0]
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
65 RANK_INT_TO_CODE[ RANK_NAMES.index( 'superkingdom' ) ] = 'd'
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
66 PRIMARY_RANK_NAMES.append( 'superkingdom' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
67
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
68 NAME_STUB = "%s__%s"
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
69 NAME_RE = re.compile( "(\t| |\||\.;)" )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
70 NAME_REPL = "_"
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
71
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
72
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
73 def get_kraken_db_path( db ):
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
74 assert db, ValueError( "You must provide a kraken database" )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
75 k_db_path = os.getenv('KRAKEN_DB_PATH', None )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
76 if k_db_path:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
77 db = os.path.join( k_db_path, db )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
78 return db
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
79
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
80
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
81 def load_taxonomy( db_path, sanitize_names=False ):
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
82 child_lists = {}
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
83 name_map = {}
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
84 rank_map = {}
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
85 with open( os.path.join( db_path, "taxonomy/names.dmp" ) ) as fh:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
86 for line in fh:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
87 line = line.rstrip( "\n\r" )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
88 if line.endswith( "\t|" ):
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
89 line = line[:-2]
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
90 fields = line.split( "\t|\t" )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
91 node_id = fields[0]
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
92 name = fields[1]
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
93 if sanitize_names:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
94 name = NAME_RE.sub( NAME_REPL, name )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
95 name_type = fields[3]
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
96 if name_type == "scientific name":
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
97 name_map[ node_id ] = name
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
98
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
99 with open( os.path.join( db_path, "taxonomy/nodes.dmp" ) ) as fh:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
100 for line in fh:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
101 line = line.rstrip( "\n\r" )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
102 fields = line.split( "\t|\t" )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
103 node_id = fields[0]
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
104 parent_id = fields[1]
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
105 rank = RANK_NAME_TO_INTS.get( fields[2].lower(), None )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
106 if rank is None:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
107 # This should never happen, unless new taxonomy ranks are created
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
108 print >> sys.stderr, 'Unrecognized rank: Node "%s" is "%s", setting to "%s"' % ( node_id, fields[2], NO_RANK_NAME )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
109 rank = NO_RANK_INT
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
110 if node_id == '1':
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
111 parent_id = '0'
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
112 if parent_id not in child_lists:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
113 child_lists[ parent_id ] = []
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
114 child_lists[ parent_id ].append( node_id )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
115 rank_map[node_id] = rank
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
116 return ( child_lists, name_map, rank_map )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
117
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
118
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
119 def dfs_summation( node, counts, child_lists ):
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
120 children = child_lists.get( node, None )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
121 if children:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
122 for child in children:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
123 dfs_summation( child, counts, child_lists )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
124 counts[ node ] = counts.get( node, 0 ) + counts.get( child, 0 )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
125
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
126
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
127 def dfs_report( node, file_data, hit_taxa, rank_map, name_map, child_lists, output_lines, options, name=None, tax=None ):
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
128 if not options.summation and ( not options.show_zeros and node not in hit_taxa ):
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
129 return
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
130 rank_int = rank_map[node]
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
131 code = RANK_INT_TO_CODE.get( rank_int, NO_RANK_CODE )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
132 if ( code != NO_RANK_CODE or options.intermediate ) and ( options.show_zeros or node in hit_taxa):
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
133 if name is None:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
134 name = ""
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
135 else:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
136 name = "%s|" % name
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
137 if tax is None:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
138 tax = ''
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
139 else:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
140 tax = "%s;" % tax
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
141 sanitized_name = name_map[ node ]
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
142 name_stub = NAME_STUB % ( code, sanitized_name )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
143 name = name + name_stub
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
144 tax = tax + name_stub
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
145 if options.name_id:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
146 output = node
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
147 elif options.name_long:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
148 output = name
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
149 else:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
150 output = sanitized_name
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
151 for val in file_data:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
152 output = "%s\t%i" % ( output, val.get( node, 0 ) )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
153 if options.show_rank:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
154 output = "%s\t%s" % ( output, RANK_NAMES[ rank_int ] )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
155 if options.taxonomy:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
156 output = "%s\t%s" % ( output, tax )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
157 output_lines[ rank_int ].append( output )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
158 children = child_lists.get( node )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
159 if children:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
160 for child in children:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
161 dfs_report( child, file_data, hit_taxa, rank_map, name_map, child_lists, output_lines, options, name=name, tax=tax )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
162
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
163
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
164 def write_tree( child_lists, name_map, rank_map, options, branch_length=1 ):
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
165 # Uses Biopython, only load if making tree
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
166 import Bio.Phylo
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
167 from Bio.Phylo import BaseTree
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
168
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
169 def _get_name( node_id ):
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
170 if options.name_id:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
171 return node_id
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
172 return name_map[node_id]
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
173 nodes = {}
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
174 root_node_id = child_lists["0"][0]
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
175 nodes[root_node_id] = BaseTree.Clade( name=_get_name( root_node_id), branch_length=branch_length )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
176
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
177 def recurse_children( parent_id ):
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
178 if options.cluster is not None and rank_map[parent_id] == options.cluster:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
179 # Short circuit if we found our rank, prevents 'hanging' no ranks from being output
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
180 # e.g. clustering by "species" (Escherichia coli), but have "no rank" below (Escherichia coli K-12) in test_db
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
181 return
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
182 if parent_id not in nodes:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
183 nodes[parent_id] = BaseTree.Clade( name=_get_name( parent_id ), branch_length=branch_length )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
184 for child_id in child_lists.get( parent_id, [] ):
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
185 if options.cluster is None or ( rank_map[child_id] <= options.cluster ):
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
186 if child_id not in nodes:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
187 nodes[child_id] = BaseTree.Clade(name=_get_name( child_id ), branch_length=branch_length)
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
188 nodes[parent_id].clades.append(nodes[child_id])
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
189 recurse_children( child_id )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
190 recurse_children( root_node_id )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
191 tree = BaseTree.Tree(root=nodes[root_node_id])
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
192 Bio.Phylo.write( [tree], options.output_tree, 'newick' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
193
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
194
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
195 def __main__():
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
196 parser = optparse.OptionParser( usage="%prog [options] file1 file...fileN" )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
197 parser.add_option( '-v', '--version', dest='version', action='store_true', default=False, help='print version and exit' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
198 parser.add_option( '', '--show-zeros', dest='show_zeros', action='store_true', default=False, help='Show empty nodes' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
199 parser.add_option( '', '--header-line', dest='header_line', action='store_true', default=False, help='Provide a header on output' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
200 parser.add_option( '', '--intermediate', dest='intermediate', action='store_true', default=False, help='Intermediate Ranks' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
201 parser.add_option( '', '--name-id', dest='name_id', action='store_true', default=False, help='Use Taxa ID instead of Name' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
202 parser.add_option( '', '--name-long', dest='name_long', action='store_true', default=False, help='Use Long taxa ID instead of base name' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
203 parser.add_option( '', '--taxonomy', dest='taxonomy', action='store_true', default=False, help='Output taxonomy in last column' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
204 parser.add_option( '', '--cluster', dest='cluster', action='store', type="string", default=None, help='Cluster counts to specified rank' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
205 parser.add_option( '', '--summation', dest='summation', action='store_true', default=False, help='Add summation of child counts to each taxa' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
206 parser.add_option( '', '--sanitize-names', dest='sanitize_names', action='store_true', default=False, help='Replace special chars (\t| |\||\.;) with underscore (_)' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
207 parser.add_option( '', '--show-rank', dest='show_rank', action='store_true', default=False, help='Output column with Rank name' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
208 parser.add_option( '', '--db', dest='db', action='store', type="string", default=None, help='Name of Kraken database' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
209 parser.add_option( '', '--output', dest='output', action='store', type="string", default=None, help='Name of output file' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
210 parser.add_option( '', '--output-tree', dest='output_tree', action='store', type="string", default=None, help='Name of output file to place newick tree' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
211 (options, args) = parser.parse_args()
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
212 if options.version:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
213 print >> sys.stderr, "Kraken Taxonomy Report (%s) version %s" % ( __URL__, __VERSION__ )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
214 sys.exit()
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
215 if not args:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
216 print >> sys.stderr, parser.get_usage()
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
217 sys.exit()
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
218
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
219 if options.cluster:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
220 cluster_name = options.cluster.lower()
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
221 cluster = RANK_NAME_TO_INTS.get( cluster_name, None )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
222 assert cluster is not None, ValueError( '"%s" is not a valid rank for clustering.' % options.cluster )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
223 if cluster_name not in PRIMARY_RANK_NAMES:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
224 assert options.intermediate, ValueError( 'You cannot cluster by "%s", unless you enable intermediate ranks.' % options.cluster )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
225 ranks_to_report = [ cluster ]
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
226 options.cluster = cluster
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
227 # When clustering we need to do summatation
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
228 options.summation = True
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
229 else:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
230 options.cluster = None # make empty string into None
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
231 ranks_to_report = RANK_NAMES_INTS
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
232
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
233 if options.output:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
234 output_fh = open( options.output, 'wb+' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
235 else:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
236 output_fh = sys.stdout
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
237
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
238 db_path = get_kraken_db_path( options.db )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
239 ( child_lists, name_map, rank_map ) = load_taxonomy( db_path, sanitize_names=options.sanitize_names )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
240 file_data = []
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
241 hit_taxa = []
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
242 for input_filename in args:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
243 taxo_counts = {}
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
244 with open( input_filename ) as fh:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
245 for line in fh:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
246 fields = line.split( "\t" )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
247 taxo_counts[ fields[2] ] = taxo_counts.get( fields[2], 0 ) + 1
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
248 clade_counts = taxo_counts.copy() # fixme remove copying?
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
249 if options.summation:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
250 dfs_summation( '1', clade_counts, child_lists )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
251 for key, value in clade_counts.items():
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
252 if value and key not in hit_taxa:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
253 hit_taxa.append( key )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
254 file_data.append( clade_counts )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
255
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
256 if options.header_line:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
257 output_fh.write( "#ID\t" )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
258 output_fh.write( "\t".join( args ) )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
259 if options.show_rank:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
260 output_fh.write( "\trank" )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
261 if options.taxonomy:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
262 output_fh.write( "\ttaxonomy" )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
263 output_fh.write( '\n' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
264
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
265 output_lines = dict( [ ( x, [] ) for x in RANK_NAMES_INTS ] )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
266 dfs_report( '1', file_data, hit_taxa, rank_map, name_map, child_lists, output_lines, options, name=None, tax=None )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
267
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
268 for rank_int in ranks_to_report:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
269 for line in output_lines.get( rank_int, [] ):
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
270 output_fh.write( line )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
271 output_fh.write( '\n' )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
272 fh.close()
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
273 if options.output_tree:
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
274 write_tree( child_lists, name_map, rank_map, options )
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
275
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
276
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
277 if __name__ == "__main__":
3f1a0d47ea8d planemo upload for repository https://github.com/galaxyproject/tools-iuc/blob/master/tools/kraken_taxonomy_report/ commit 1c0a7aff7c5f6578a11e6e8e9bface8d02e7f8a1
iuc
parents:
diff changeset
278 __main__()