/
validate_tree.py
41 lines (35 loc) · 1.36 KB
/
validate_tree.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
'''
Tests input sequence for whether it is a valid tree-sequence
'''
from parameter import ConsensusStatsCommandParser
from sequence import NeuriteSequence
import sys
version = 0.1
if __name__ == '__main__':
try:
sequence = sys.argv[1]
a_stack = [[1,0,1]]
for i in range(len(sequence)):
char = sequence[i]
#print(str(i)+" "+char)
if len(a_stack) == 0:
print("Sequence going beyond last complete tree at position "+str(i))
if char == 'A':
a_stack.append([0,1,0])
elif char == 'C':
a_stack[-1][a_stack[-1][0]+1] += 1
elif char == 'T':
a_stack[-1][a_stack[-1][0]+1] += 1
while len(a_stack) > 0 and a_stack[-1][0] == 1:
top = a_stack.pop()
if top[1] > top[2]+1:
print("First tree larger than second tree ending at position "+str(i))
if len(a_stack) > 0:
a_stack[-1][a_stack[-1][0]+1] += top[1]+top[2]
# After popping, remaining tree switches to second subtree
if len(a_stack) > 0:
a_stack[-1][0] = 1
if len(a_stack) > 0:
print("Tree is incomplete")
except (IOError, KeyboardInterrupt, IndexError) as e:
print(str(e))