def create_merged_bayesian_network_for_event( event, prefix, bayesian_network, assert_merge_definitions=False, logger=StdOutLogger(verbose=False)): merge_def_list = BayesianNetworkMergeUtils.create_merge_definition_list( event, prefix, merge_singleton_events=False) logger.log('Merging...', includeTimestamp=True) logger.set_carriage_reset(True) count = len(merge_def_list) for index, merge_def in enumerate(merge_def_list): logger.log("\rMerging '%s' (%i of %i)" % (merge_def.get_merged_event(), index + 1, count), includeTimestamp=True) merge_defs = DistributionEventMergeDefinitions( event.name, bayesian_network, allow_unspecified_events=True, assert_merge_definitions=assert_merge_definitions) merge_defs.set_merge_definitions([merge_def]) bn_wrapper = BayesianNetworkWrapper(bayesian_network) bayesian_network = bn_wrapper.create_network_with_merged_events( merge_defs, bake=True) logger.set_carriage_reset(False) logger.log('Merging done!', includeTimestamp=True) return bayesian_network
def test_comparisons_2__compare_structure(bayesian_network_1, bayesian_network_2): wrapper1 = BayesianNetworkWrapper(bayesian_network_1) wrapper2 = BayesianNetworkWrapper(bayesian_network_2) with pytest.raises(Exception) as e: assert wrapper1.assert_structure_equality(wrapper2) assert str(e.value) == 'Number of states not equal'
def merge_alleles(self, events=None, prefixes=None, assert_merge_definitions=False): d_event = None if events is None: events = [] events.append(self.get_event_from_nickname(Nicknames.v_choice.value)) events.append(self.get_event_from_nickname(Nicknames.j_choice.value)) d_event = self.get_event_from_nickname(Nicknames.d_gene.value) if d_event is not None: events.append(d_event) if prefixes is None: prefixes = [] prefixes.append(Nicknames.v_choice.value + ':') prefixes.append(Nicknames.j_choice.value + ':') if d_event is not None: prefixes.append(Nicknames.d_gene.value + ':') pg_network = BayesianNetworkMergeUtils.create_merged_bayesian_network_for_events(events, prefixes, self._network_wrapper._network, assert_merge_definitions=assert_merge_definitions, logger=self._logger) self._network_wrapper = BayesianNetworkWrapper(pg_network)
def create_truncated_network(template): template_wrapper = BayesianNetworkWrapper(template) out = pom.BayesianNetwork() v_state = template_wrapper.get_state( 'GeneChoice_V_gene_Undefined_side_prio7_size147') #v_del_state = template_wrapper.get_state('Deletion_V_gene_Three_prime_prio5_size21') j_state = template_wrapper.get_state( 'GeneChoice_J_gene_Undefined_side_prio7_size16') #j_del_state = template_wrapper.get_state('Deletion_J_gene_Five_prime_prio5_size23') d_state = template_wrapper.get_state( 'GeneChoice_D_gene_Undefined_side_prio6_size3') out.add_states(v_state, j_state, d_state) #out.add_states(v_state, j_state, v_del_state, j_del_state, d_state) out.add_edge(v_state, j_state) #out.add_edge(v_state, v_del_state) #out.add_edge(j_state, j_del_state) out.add_edge(v_state, d_state) out.add_edge(j_state, d_state) out.bake() return out
def test_marginals_7__merge_all_alleles(bayesian_network_3): wrapper = BayesianNetworkWrapper(bayesian_network_3) merge_def1 = EventMergeDefinition('V1') merge_def1.extend(['V1*01', 'V1*02']) merge_defs = DistributionEventMergeDefinitions( 'v_gene', wrapper.get_network(), allow_unspecified_events=True, assert_merge_definitions=True) merge_defs.set_merge_definitions([merge_def1]) merged_network = wrapper.create_network_with_merged_events(merge_defs) assert (merged_network is not None) wrapper = BayesianNetworkWrapper(merged_network) margs = wrapper.get_probabilities( statename=None, probability_type=ProbabilityType.Conditional) assert len(margs) == 7
def test_marginals_5__merge_v_alleles(bayesian_network_1): wrapper = BayesianNetworkWrapper(bayesian_network_1) merge_def1 = EventMergeDefinition('V1') merge_def1.extend(['V1*01', 'V1*02', 'V1*03']) merge_defs = DistributionEventMergeDefinitions( 'v_gene', bayesian_network_1, allow_unspecified_events=True, assert_merge_definitions=True) merge_defs.set_merge_definitions([merge_def1]) merged_network = wrapper.create_network_with_merged_events(merge_defs) wrapper = BayesianNetworkWrapper(merged_network) margs = wrapper.get_probabilities( statename='v_gene', probability_type=ProbabilityType.Conditional) assert len(margs) == 2 assert utilities.almost_equal(margs.iloc[0, 0], 0.6, 0.0001) assert utilities.almost_equal(margs.iloc[1, 0], 0.4, 0.0001)
def test_marginals_4__merge_v_alleles(bayesian_network_1): wrapper = BayesianNetworkWrapper(bayesian_network_1) merge_def1 = EventMergeDefinition('V1') merge_def1.extend(['V1*01', 'V1*02', 'V1*03']) merge_defs = DistributionEventMergeDefinitions( 'v_gene', bayesian_network_1, allow_unspecified_events=True, assert_merge_definitions=True) merge_defs.set_merge_definitions([merge_def1]) merged_network = wrapper.create_network_with_merged_events(merge_defs) wrapper = BayesianNetworkWrapper(merged_network) margs = wrapper.get_probabilities( statename=None, probability_type=ProbabilityType.Marginal) assert len(margs) == 2 for key, value in margs.iteritems(): tot = value.sum() assert utilities.almost_equal(tot, 1.0, 0.0001)
out_marginals = 'C:/CiR/pTCR/IGOR_models/Unproductive_models/models_imgt_ref_dir_sep2019/HC/1365_TRB/1365_TRB_marginals.txt' out_params = 'C:/CiR/pTCR/IGOR_models/Unproductive_models/models_imgt_ref_dir_sep2019/HC/1365_TRB/1365_TRB_params.txt' gmw = GenModelWrapper(out_params, out_marginals, '1363_TRB_', StdOutLogger()) gmw.merge_alleles(assert_merge_definitions=True) bn = gmw.get_bayesian_network_wrapper().get_network() # truncate_bn = create_truncated_network(bn) # truncate_bn_wrapper = BayesianNetworkWrapper(truncate_bn) merge_defs = create_merg_defs(bn) merged_bayesian_network = gmw.get_bayesian_network_wrapper( ).create_network_with_merged_events(merge_defs, bake=False) merged_bayesian_network.bake() bnwrapper = BayesianNetworkWrapper(merged_bayesian_network) states = {} n = gmw.get_eventname_for_nickname('v_choice') state = bnwrapper.get_state(n) states[n] = state n = gmw.get_eventname_for_nickname('j_choice') state = bnwrapper.get_state(n) states[n] = state n = gmw.get_eventname_for_nickname('d_gene') state = bnwrapper.get_state(n) states[n] = state n = gmw.get_eventname_for_nickname('v_del3') state = bnwrapper.get_state(n)
def test_comparisons_1__compare_structure(bayesian_network_1): wrapper1 = BayesianNetworkWrapper(bayesian_network_1) wrapper2 = BayesianNetworkWrapper(bayesian_network_1) wrapper1.assert_structure_equality(wrapper2)
def test_marginals_2__assert_marginals(bayesian_network_1): wrapper = BayesianNetworkWrapper(bayesian_network_1) margs = wrapper.get_probabilities('j_gene', ProbabilityType.Marginal) assert len(margs) == 3
def _init_network_wrapper(self): importer = BayesianNetworkImporter(self._logger) pg_network = importer.create_network_from_genmodelwrapper(self) pg_network.bake() wrapper = BayesianNetworkWrapper(pg_network) return wrapper
class GenModelWrapper(object): _GENE_NAME_NICKNAMES = ['v_choice', 'j_choice'] _MARGINALS_DF_COL_NAME = 'Marginal' def __init__(self, params_fn, marginals_fn, name='', logger=StdOutLogger(verbose=False)): self._logger = logger self._params_fn = params_fn self._marginal_fn = marginals_fn self._name = name self._genmodel = GenModel(params_fn, marginals_fn) self._logger.log('GenModelWrapper.GenModel class loaded (%s, %s)' % (params_fn, marginals_fn), onlyIfVerbose=True) self._init_names_and_nicknames() self._insertion_compositions = self._init_insertion_compositions() self._insertion_lengths = self._init_insertion_lengths() self._network_wrapper = self._init_network_wrapper() self._logger.log('GenModelWrapper loaded', onlyIfVerbose=True) def _init_insertion_lengths(self): formatter = InsertionStatisticsFormatter(self) out = formatter.get_lengths_statistics() return out def get_insertion_lengths(self, nickname): out = self._insertion_lengths.get(nickname, None) return out def _init_insertion_compositions(self): calculator = SteadyStateCalculator(self._genmodel.marginals) calculator.calculate_for_key(Nicknames.vj_dinucl.value) calculator.calculate_for_key(Nicknames.vd_dinucl.value) calculator.calculate_for_key(Nicknames.dj_dinucl.value) out = calculator.get_matrices() return out def get_insertion_composition(self, nickname): out = self._insertion_compositions.get(nickname, None) return out def _init_network_wrapper(self): importer = BayesianNetworkImporter(self._logger) pg_network = importer.create_network_from_genmodelwrapper(self) pg_network.bake() wrapper = BayesianNetworkWrapper(pg_network) return wrapper def merge_alleles(self, events=None, prefixes=None, assert_merge_definitions=False): d_event = None if events is None: events = [] events.append(self.get_event_from_nickname(Nicknames.v_choice.value)) events.append(self.get_event_from_nickname(Nicknames.j_choice.value)) d_event = self.get_event_from_nickname(Nicknames.d_gene.value) if d_event is not None: events.append(d_event) if prefixes is None: prefixes = [] prefixes.append(Nicknames.v_choice.value + ':') prefixes.append(Nicknames.j_choice.value + ':') if d_event is not None: prefixes.append(Nicknames.d_gene.value + ':') pg_network = BayesianNetworkMergeUtils.create_merged_bayesian_network_for_events(events, prefixes, self._network_wrapper._network, assert_merge_definitions=assert_merge_definitions, logger=self._logger) self._network_wrapper = BayesianNetworkWrapper(pg_network) def _init_names_and_nicknames(self): self._names_for_nicknames = {} self._nicknames_for_names = {} for event in self._genmodel.events: self._names_for_nicknames[event.nickname] = event.name self._nicknames_for_names[event.name] = event.nickname def __str__(self): return self._name def get_GenModel(self): return self._genmodel def get_bayesian_network_wrapper(self): return self._network_wrapper def get_name(self): return self._name def set_name(self, name): self._name = name def get_eventname_for_nickname(self, nickname): return self._names_for_nicknames.get(nickname, None) def get_event_from_name(self, name): for event in self._genmodel.events: if event.name == name: return event return None def get_event_from_nickname(self, nickname): for event in self._genmodel.events: if event.nickname == nickname: return event return None def get_bayesian_network_wrapper(self): return self._network_wrapper def _get_v_gene_marginal_from_primitives(self, v_gene_name): nickname = str(Nicknames.v_choice.value) event = self.get_event_from_nickname(nickname) index = -1 for realization in event.realizations: if v_gene_name == realization.name: index = realization.index break margs = self._genmodel.marginals[0].get(nickname) out = margs[index] return out def assert_bayesian_network_structure_equality(self, gen_model_wrapper): try: self._network_wrapper.assert_structure_equality(gen_model_wrapper.get_bayesian_network_wrapper()) except AssertionError as error: id = self._name + 'vs. ' + gen_model_wrapper.get_name() + ': ' raise AssertionError(id + str(error))