-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathktoolu_taxonomy.py
106 lines (94 loc) · 3.89 KB
/
ktoolu_taxonomy.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
101
102
103
104
105
106
#!/usr/bin/env python
#
# Copyright (c) 2014-2016 Christian Schudoma, The Sainsbury Laboratory
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
import os
class ktTaxonomyTree(object):
def __init__(self, db):
self._readNames(os.path.join(db, 'taxonomy', 'names.dmp'))
self._readNodes(os.path.join(db, 'taxonomy', 'nodes.dmp'))
pass
def _readNames(self, fn):
assert os.path.exists(fn)
self.nodeNames = dict()
with open(fn) as fi:
for line in fi:
line = line.strip().strip('|').strip()
if not line:
break
line = line.split('\t|\t')
if line[3].strip() == 'scientific name':
self.nodeNames[int(line[0].strip())] = line[1].strip()
pass
def _readNodes(self, fn):
assert os.path.exists(fn)
self.nodeRanks, self.nodeChildren = dict(), dict()
with open(fn) as fi:
for line in fi:
line = line.strip().strip('|').strip()
if not line:
break
line = list(map(lambda x:x.strip(), line.split('\t|\t')))
# print(line, type(line))
line[:2] = list(map(int, line[:2]))
if line[0] == 1:
line[1] = 0
try:
self.nodeChildren[line[1]].add(line[0])
except:
self.nodeChildren[line[1]] = set([line[0]])
self.nodeRanks[line[0]] = line[2]
pass
def getDescendents(self, taxID):
assert taxID >= 0
assert self.nodeChildren
descendents, queue = set([taxID]), [taxID]
while queue:
children = self.nodeChildren.get(queue.pop(), set())
if children:
descendents.update(children)
queue.extend(children)
pass
return descendents
pass
def compileValidTaxIDs(db, wantedTaxIDs=[], unwantedTaxIDs=[], vipTaxIDs=[], logfile=None):
keepTaxIDs = set()
if logfile is not None:
[logfile.write('Reading taxonomy...\n'), logfile.flush()]
taxTree = ktTaxonomyTree(db)
if logfile is not None:
[logfile.write('Traversing requested taxonomy branch(es)...\n'), logfile.flush()]
# print(wantedTaxIDs)
for taxID in wantedTaxIDs:
keepTaxIDs.update(taxTree.getDescendents(abs(taxID)))
# print(unwantedTaxIDs)
for taxID in unwantedTaxIDs:
keepTaxIDs.difference_update(taxTree.getDescendents(abs(taxID)))
return keepTaxIDs.union(set(vipTaxIDs))
__author__ = "Christian Schudoma"
__copyright__ = "Copyright 2014-2016, Christian Schudoma, The Sainsbury Laboratory"
__credits__ = ["Pirasteh Pahlavan", "Agathe Jouet", "Yogesh Gupta"]
__license__ = "MIT"
__version__ = "1.0.1"
__maintainer__ = "Christian Schudoma"
__email__ = "[email protected]"
__status__ = "Development"