def create_merge_definition_list(gen_model_event,
                                  prefix=None,
                                  merge_singleton_events=False):
     genes = ir_genes.Genes()
     for realization in gen_model_event.realizations:
         r_name = realization.name.strip()
         gene = ir_gene.Gene(r_name, assume_01_allele=False)
         if len(gene.get_allele_text()) < 1:
             raise ValueError('Gene name does not contain allele')
         genes.add_gene(gene)
     unique_genes = {}
     for gene in genes:
         gene_def = gene.get_gene_text()
         unique_gene = unique_genes.get(gene_def, None)
         if unique_gene is None:
             unique_gene = ir_genes.GenePossibilities()
             unique_genes[gene_def] = unique_gene
         unique_gene.add_gene(gene, also_check_allele=True)
     merge_defs = []
     for gene_def, unique_gene in unique_genes.iteritems():
         # Singleton events (genes with one allele only) do not have to be merged; this is optional
         if len(unique_gene) > 1 or merge_singleton_events:
             merged_event_name = gene_def
             if prefix is not None:
                 merged_event_name = prefix + merged_event_name
             merge_def = EventMergeDefinition(merged_event_name)
             for g in unique_gene:
                 if prefix: s = prefix
                 s += str(g)
                 merge_def.add(s)
             merge_defs.append(merge_def)
     return merge_defs
Esempio n. 2
0
def test_merge_definitions_6__events_not_unique(bayesian_network_0):
    merge_def1 = EventMergeDefinition('V1')
    merge_def1.extend(['V1*01', 'V1*02', 'V1*03'])
    with pytest.raises(ValueError) as e:
        merge_defs = DistributionEventMergeDefinitions(
            'v_gene',
            bayesian_network_0,
            allow_unspecified_events=True,
            assert_merge_definitions=True)
    assert str(
        e.value
    ) == DistributionEventMergeDefinitions.ERR_NETWORK_HAS_DUPLICATED_EVENT_NAMES
Esempio n. 3
0
def test_merge_definitions_3__duplicated_events(bayesian_network_1):
    merge_def1 = EventMergeDefinition('V1')
    merge_def1.extend(['V1*01', 'V1*02', 'V1*02'])
    merge_defs = DistributionEventMergeDefinitions(
        'v_gene',
        bayesian_network_1,
        allow_unspecified_events=True,
        assert_merge_definitions=True)
    with pytest.raises(ValueError) as e:
        merge_defs.set_merge_definitions([merge_def1])
    assert str(
        e.value) == DistributionEventMergeDefinitions.ERR_DUPLICATE_EVENT_MSG
Esempio n. 4
0
def test_merge_definitions_2__event_not_found(bayesian_network_1):
    merge_def1 = EventMergeDefinition('V1')
    merge_def1.extend(['V1*01', 'V1*02', 'V1*04'])
    merge_defs = DistributionEventMergeDefinitions(
        'v_gene',
        bayesian_network_1,
        allow_unspecified_events=True,
        assert_merge_definitions=True)
    with pytest.raises(ValueError) as e:
        merge_defs.set_merge_definitions([merge_def1])
    assert str(
        e.value) == DistributionEventMergeDefinitions.ERR_EVENT_NOT_FOUND
Esempio n. 5
0
def create_merg_defs(bn):
    merge_def1 = EventMergeDefinition('v_choice:TRBV7-2')
    merge_def1.extend([
        'v_choice:TRBV7-2*01', 'v_choice:TRBV7-2*02', 'v_choice:TRBV7-2*03',
        'v_choice:TRBV7-2*04'
    ])
    merge_defs = DistributionEventMergeDefinitions(
        'GeneChoice_V_gene_Undefined_side_prio7_size147',
        bn,
        allow_unspecified_events=True,
        assert_merge_definitions=True)
    merge_defs.set_merge_definitions([merge_def1])
    return merge_defs
Esempio n. 6
0
def test_merge_definitions_4__unspecified_event(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=False,
        assert_merge_definitions=True)
    with pytest.raises(ValueError) as e:
        merge_defs.set_merge_definitions([merge_def1])
    assert str(
        e.value
    ) == DistributionEventMergeDefinitions.ERR_UNSPECIFIED_EVENTS_NOT_ALLOWED_MSG
Esempio n. 7
0
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
Esempio n. 8
0
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)
Esempio n. 9
0
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)
Esempio n. 10
0
def test_merge_definitions_5__one_merge_def_only(bayesian_network_1):
    merge_def1 = EventMergeDefinition('V1')
    merge_def1.extend(['V1*01', 'V1*02', 'V1*03'])
    merge_def2 = EventMergeDefinition('V2')
    merge_def2.extend(['V2*01'])
    merge_defs = DistributionEventMergeDefinitions(
        'v_gene',
        bayesian_network_1,
        allow_unspecified_events=True,
        assert_merge_definitions=True)
    with pytest.raises(ValueError) as e:
        merge_defs.set_merge_definitions([merge_def1, merge_def2])
    assert str(
        e.value
    ) == DistributionEventMergeDefinitions.ERR_ONLY_ONE_MERGE_DEF_ALLOWED_MSG