-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconfig_parser.py
140 lines (123 loc) · 4.88 KB
/
config_parser.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 3 18:36:01 2017
@author: bryce
"""
#==============================================================================
# Copyright (C) 2017 Bryce L. Kille
# University of Illinois
# Department of Chemistry
#
# Copyright (C) 2017 Christopher J. Schwalen
# University of Illinois
# Department of Chemistry
#
# Copyright (C) 2017 Douglas A. Mitchell
# University of Illinois
# Department of Chemistry
#
# License: GNU Affero General Public License v3 or later
# Complete license availabel in the accompanying LICENSE.txt.
# or <http://www.gnu.org/licenses/>.
#
# This file is part of RODEO2.
#
# RODEO2 is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RODEO2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#==============================================================================
#DELIMITERS
BEGIN_RIPP = ">"
END_RIPP = ">>"
BEGIN_COLORS = "#BEGIN_COLORS"
END_COLORS = "#END_COLORS"
BEGIN_VARIABLES = "#BEGIN_VARIABLES"
END_VARIABLES = "#END_VARIABLES"
def parse_config_file(filename):
conf_dict = {}
with open(filename) as config_handle:
ripp_type = ""
in_color = False
in_variable = False
in_ripp = False
for line in config_handle:
line = line.strip()
if line == "\n" or line == "":
continue
elif line[0] == BEGIN_RIPP:
in_ripp = True
ripp_type = line[1:].lower()
conf_dict[ripp_type] = {}
conf_dict[ripp_type]['pfam_colors'] = {}
conf_dict[ripp_type]['variables'] = {}
elif line == END_RIPP:
in_ripp = False
elif line == BEGIN_COLORS:
in_color = True
elif line == END_COLORS:
in_color = False
elif line == BEGIN_VARIABLES:
in_variable = True
elif line == END_VARIABLES:
in_variable = False
elif in_ripp:
line = line.split(' ')
key = line[0]
val = line[1]
if line[0] == 'int':
key = line[1]
val = int(line[2])
elif line[0] == 'bool':
key = line[1]
if line[2].lower() == "yes" or line[2].lower() == "true":
val = True
else:
val = False
if in_color:
conf_dict[ripp_type]['pfam_colors'][key.upper()] = val.lower()
elif in_variable:
conf_dict[ripp_type]['variables'][key.lower()] = val
return conf_dict
def merge_confs(conf_list):
master_conf_dict = conf_list[0]
for conf in conf_list[1:]:
for ripp in conf.keys():
current_dict = conf[ripp]
for entry_type in current_dict.keys():
current_dict_nested = current_dict[entry_type]
for key in current_dict_nested:
master_conf_dict[ripp][entry_type][key] = current_dict_nested[key]
return master_conf_dict
def merge_conf_and_arg(conf, args):
if args.precursor_max is not None:
conf['general']['variables']['precursor_max'] = args.precursor_max
for peptide_type in conf.keys():
conf[peptide_type]['variables']['precursor_max'] = args.precursor_max
if args.precursor_min is not None:
conf['general']['variables']['precursor_min'] = args.precursor_min
for peptide_type in conf.keys():
conf[peptide_type]['variables']['precursor_min'] = args.precursor_min
if args.overlap is not None:
conf['general']['variables']['overlap'] = args.overlap
if args.fetch_type is not None:
conf['general']['variables']['fetch_type'] = args.fetch_type
if args.fetch_n is not None:
conf['general']['variables']['fetch_n'] = args.fetch_n
if args.fetch_distance is not None:
conf['general']['variables']['fetch_distance'] = args.fetch_distance
if args.evaluate_all is not None:
conf['general']['variables']['evaluate_all'] = args.evaluate_all
if args.exhaustive is not None:
for peptide_type in conf.keys():
conf[peptide_type]['variables']['exhaustive'] = args.exhaustive
if args.print_precursors is not None:
for peptide_type in conf.keys():
conf[peptide_type]['variables']['print_precursors'] = args.print_precursors
return conf