-
Notifications
You must be signed in to change notification settings - Fork 2
/
rxncon2reactiongraph.py
executable file
·156 lines (117 loc) · 5.1 KB
/
rxncon2reactiongraph.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/python3
import logging
import os
import sys
import click
import click_log
import colorama
from rxncon.input.excel_book.excel_book import ExcelBook
from rxncon.visualization.graphML import XGMML
from rxncon.visualization.graphML import map_layout2xgmml
from rxncon.visualization.reaction_graph import rxngraph_from_rxncon_system
logger = logging.getLogger(__name__)
colorama.init()
def _file_path_existence(file_path):
"""
Checking if the file path already exists.
Note:
It is supposed to be possible to overwrite existing files.
Args:
file_path: File path.
Returns:
None
Raises:
FileExistsError: If file exists.
NotADirectoryError: If directory does not exists.
"""
path, file = os.path.split(file_path)
if path and os.path.exists(path) and os.path.isfile(file_path):
raise FileExistsError("{0} exists! remove file and run again".format(file_path))
elif not path and os.path.isfile(file):
raise FileExistsError("{0} exists! remove file and run again".format(file_path))
elif path and not os.path.exists(path):
raise NotADirectoryError("Path {0} does not exists.".format(path))
def write_xgmml(excel_filename: str, output=None, layout_template_file=None):
"""
creating the xgmml file from an excel input and writing it into a new file.
Args:
excel_filename: Name of the excel input file.
output: Name of the new output.
layout_template_file: Name of the layout template file.
Returns:
None
"""
if not output:
output = os.path.splitext(os.path.basename(excel_filename))[0]
base_path = os.path.dirname(excel_filename)
suffix = '_rg'
if not output.endswith('.xgmml'):
output = '{0}{1}.xgmml'.format(output, suffix)
else:
base_name = output.split('.xgmml')[0]
output = "{0}{1}.{2}".format(base_name, suffix, 'xgmml')
graph_filename = os.path.join(base_path, '{0}'.format(output))
print('graph_filename: ', graph_filename)
_file_path_existence(graph_filename)
print('Reading in Excel file [{}] ...'.format(excel_filename))
excel_book = ExcelBook(excel_filename)
rxncon_system = excel_book.rxncon_system
print('Constructed rxncon system: [{} reactions], [{} contingencies]'
.format(len(rxncon_system.reactions), len(rxncon_system.contingencies)))
print('Generating reaction graph output...')
rxngraph_system = rxngraph_from_rxncon_system(rxncon_system)
graph = rxngraph_system.reaction_graph
if layout_template_file:
print('Writing layout information from [{0}] to graph file [{1}] ...'.format(layout_template_file,
graph_filename))
gml_system = XGMML(graph, "{}".format(output))
graph = map_layout2xgmml(gml_system.to_string(), layout_template_file)
print('Writing reaction graph file [{}] ...'.format(graph_filename))
with open(graph_filename, "w") as graph_handle:
graph_handle.write(graph)
else:
print('Writing reaction graph file [{}] ...'.format(graph_filename))
gml_system = XGMML(graph, "{}".format(output))
gml_system.to_file(graph_filename)
@click.command()
@click.option('--output', default=None,
help='Base name for output files. Default: \'fn\' for input file \'fn.xls\'')
@click.option('--layout', default=None, nargs=1, type=click.Path(exists=True),
help='xgmml file containing layout information, which should be transferred to the new file.')
@click.argument('excel_file')
@click_log.simple_verbosity_option(default='WARNING')
@click_log.init()
def run(output, excel_file, layout):
write_xgmml(excel_file, output, layout)
def setup_logging_colors():
click_log.ColorFormatter.colors = {
'error': dict(fg='red'),
'exception': dict(fg='red'),
'critical': dict(fg='red'),
'debug': dict(fg='yellow'),
'warning': dict(fg='yellow'),
'info': dict(fg='yellow')
}
def format(self, record):
if not record.exc_info:
level = record.levelname.lower()
if level in self.colors:
padding_size = 7 # Assume just INFO / DEBUG entries.
prefix = click.style('{}: '.format(level).ljust(padding_size),
**self.colors[level])
prefix += click.style('{} '.format(record.name), fg='blue')
msg = record.msg
if isinstance(msg, bytes):
msg = msg.decode(sys.getfilesystemencoding(),
'replace')
elif not isinstance(msg, str):
msg = str(msg)
record.msg = '\n'.join(prefix + x for x in msg.splitlines())
return logging.Formatter.format(self, record)
click_log.ColorFormatter.format = format
if __name__ == '__main__':
try:
setup_logging_colors()
run()
except Exception as e:
print('ERROR: {}\n{}\nPlease re-run this command with the \'-v DEBUG\' option.'.format(type(e), e))