-
Notifications
You must be signed in to change notification settings - Fork 0
/
nwck.py
executable file
·61 lines (45 loc) · 1.38 KB
/
nwck.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
#! /usr/bin/python3
import sys
import argparse
import signal
import tools
def find_dist(root, u, v):
u = tools.newick.bfs(root, lambda n, _: n.label == u)
assert u is not None
path = None
def visitor(node, current_path):
nonlocal path
if node.label == v:
path = current_path
return True
v = tools.newick.bfs(u, visitor)
assert v is not None
def path_len(path, start):
dist = 0
while path[start] is not None:
dist += 1
start = path[start]
return dist
return path_len(path, v)
def main(args):
inputs = map(str.strip, args.input)
inputs = filter(bool, inputs) # remove blanks
inputs = tools.helpers.iter_chunks(inputs, 2)
distances = []
for string, pair in inputs:
tree = tools.newick.parse(string)
pair = pair.split()
distances.append(find_dist(tree, *pair))
print(*distances)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='description',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('input', nargs='?', type=argparse.FileType('r'),
default=sys.stdin)
args = parser.parse_args()
try:
main(args)
except BrokenPipeError:
sys.exit(128 + signal.SIGPIPE)
except KeyboardInterrupt:
sys.exit(128 + signal.SIGINT)