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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
#########################################################################
# MacSyLib - Python library to detect macromolecular systems #
# in prokaryotes protein dataset using systems modelling #
# and similarity search. #
# #
# Authors: Sophie Abby, Bertrand Neron #
# Copyright (c) 2014-2025 Institut Pasteur (Paris) and CNRS. #
# See the COPYRIGHT file for details #
# #
# This file is part of MacSyLib package. #
# #
# MacSyLib is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# MacSyLib 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 General Public License for more details . #
# #
# You should have received a copy of the GNU General Public License #
# along with MacSyLib (COPYING). #
# If not, see <https://www.gnu.org/licenses/>. #
#########################################################################
import logging
import colorlog
import xml.etree.ElementTree as Et
import macsylib
from macsylib.error import MacsylibError
from macsylib.model_conf_parser import ModelConfParser
import macsylib.model_conf_parser
from tests import MacsyTest
class TestModelConfParser(MacsyTest):
def setUp(self) -> None:
macsylib.init_logger(name='macsylib')
macsylib.logger_set_level(name='macsylib', level=logging.INFO)
logger = colorlog.getLogger('macsylib')
macsylib.model_conf_parser._log = logger
def test_parse(self):
expected_conf = {'itself_weight': 11.0,
'exchangeable_weight': 12.0,
'mandatory_weight': 13.0,
'accessory_weight': 14.0,
'neutral_weight': 0.0,
'out_of_cluster_weight': 10.0,
'redundancy_penalty': 20.0,
'e_value_search': 0.12,
'i_evalue_sel': 0.012,
'coverage_profile': 0.55,
'cut_ga': False}
conf_file = self.find_data('conf_files', 'model_conf.xml')
mcp = ModelConfParser(conf_file)
test_conf = mcp.parse()
self.assertDictEqual(expected_conf, test_conf)
def test_parse_wo_filtering(self):
expected_conf = {'itself_weight': 11.0,
'exchangeable_weight': 12.0,
'mandatory_weight': 13.0,
'accessory_weight': 14.0,
'neutral_weight': 0.0,
'redundancy_penalty': 20.0,
'out_of_cluster_weight': 10.0}
conf_file = self.find_data('conf_files', 'model_conf_wo_filtering.xml')
mcp = ModelConfParser(conf_file)
test_conf = mcp.parse()
self.assertDictEqual(expected_conf, test_conf)
def test_parse_wo_weights(self):
expected_conf = {'e_value_search': 0.12,
'i_evalue_sel': 0.012,
'coverage_profile': 0.55,
'cut_ga': False}
conf_file = self.find_data('conf_files', 'model_conf_wo_weights.xml')
mcp = ModelConfParser(conf_file)
test_conf = mcp.parse()
self.assertDictEqual(expected_conf, test_conf)
def test_parse_bad_file(self):
conf_file = self.find_data('conf_files', 'project.conf')
mcp = ModelConfParser(conf_file)
with self.catch_log(log_name='macsylib'):
with self.assertRaises(MacsylibError) as ctx:
mcp.parse()
self.assertEqual(str(ctx.exception),
f"unable to parse model configuration '{conf_file}' : syntax error: line 2, column 0"
)
def test_parse_weights(self):
expected_weights = {'itself_weight': 11.0,
'exchangeable_weight': 12.0,
'mandatory_weight': 13.0,
'accessory_weight': 14.0,
'neutral_weight': 0.0,
'redundancy_penalty': 20.0,
'out_of_cluster_weight': 10.0,
}
conf_file = self.find_data('conf_files', 'model_conf.xml')
tree = Et.parse(conf_file)
model_node = tree.getroot()
mcp = ModelConfParser(conf_file)
recieved_weights = mcp.parse_weights(model_node.find("./weights"))
self.assertDictEqual(expected_weights, recieved_weights)
def test_parse_filtering(self):
expected_filters = {'e_value_search': 0.12,
'i_evalue_sel': 0.012,
'coverage_profile': 0.55,
'cut_ga': False
}
conf_file = self.find_data('conf_files', 'model_conf.xml')
tree = Et.parse(conf_file)
model_node = tree.getroot()
mcp = ModelConfParser(conf_file)
filtering_node = model_node.find("./filtering")
recieved_filters = mcp.parse_filtering(filtering_node)
self.assertDictEqual(recieved_filters, expected_filters)
# test cut_ga True
cut_ga_node = filtering_node.find('cut_ga')
cut_ga_node.text = 'True'
recieved_filters = mcp.parse_filtering(filtering_node)
self.assertTrue('cut_ga' in recieved_filters)
# test bad value for cut_ga
cut_ga_node.text = 'FOO'
with self.catch_log(log_name='macsylib'):
with self.assertRaises(MacsylibError) as ctx:
mcp.parse_filtering(filtering_node)
self.assertEqual(str(ctx.exception),
f"cannot parse 'cut_ga' element in '{conf_file}' expect True, 1, False, 0 got : 'FOO'"
)
# test no cut_ga element
filtering_node.remove(cut_ga_node)
recieved_filters = mcp.parse_filtering(filtering_node)
del(expected_filters['cut_ga'])
self.assertDictEqual(expected_filters, recieved_filters)
def test_parse_w_unkown_tag(self):
expected_filters = {'e_value_search': 0.12,
'i_evalue_sel': 0.012,
'coverage_profile': 0.55,
'cut_ga': False
}
conf_file = self.find_data('conf_files', 'model_conf.xml')
tree = Et.parse(conf_file)
model_node = tree.getroot()
mcp = ModelConfParser(conf_file)
filter_node = model_node.find("./filtering")
extra_elt = Et.SubElement(filter_node, 'nimportnaoik')
extra_elt.text = 'Foo'
with self.catch_log(log_name='macsylib') as log:
recieved_filters = mcp.parse_filtering(model_node.find("./filtering"))
log_msg = log.get_value().strip()
self.assertEqual(log_msg,
f"unknown element 'nimportnaoik' in '{conf_file}' ignore it."
)
self.assertDictEqual(recieved_filters, expected_filters)
def test_parse_w_bad_value_lxml(self):
conf_file = self.find_data('conf_files', 'model_conf.xml')
tree = Et.parse(conf_file)
model_node = tree.getroot()
mcp = ModelConfParser(conf_file)
filter_node = model_node.find("./filtering")
coverage_node = filter_node.find('coverage_profile')
coverage_node.text = "FOO"
with self.catch_log(log_name='macsylib'):
with self.assertRaises(MacsylibError) as ctx:
mcp.parse_filtering(model_node.find("./filtering"))
self.assertEqual(str(ctx.exception),
f"The model configuration file '{conf_file}' is not valid: could not convert string to float: 'FOO'"
)
|