-
Notifications
You must be signed in to change notification settings - Fork 4
/
CLI.py
54 lines (40 loc) · 1.26 KB
/
CLI.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Example CLI usage of the newick validator library
run with python3 cli.py [input-file]
"""
from io import StringIO
from sys import argv
from Newick_Validator import is_newick
from Bio import Phylo
# read a file to a string
try:
with open(argv[1], 'r') as file:
content = file.read() # string
# file.close()
except (OSError, IndexError, UnicodeDecodeError):
print("File not found or wrong file format!")
exit()
# interrupts the execution if there are any issues with the file
# \n tree separator
forest = content.split("\n")
# calls function on each tree
for i in forest:
if not i or i.isspace():
print("\nFound an empty string.")
else:
print("\nParsing %s" % i)
newick = is_newick(i)
if newick:
print("\nFound a tree: \n%s\n" % i)
try:
t = Phylo.read(StringIO(i), "newick")
Phylo.draw_ascii(t)
# empty labels as clade
# Phylo.draw(t)
except Exception as e:
print("\nSomething went wrong, tree unrecognised by Pyhlo. ")
print("Exception: %s" % e)
else:
print("\nNot a valid Newick tree: \n%s\n" % i)