Пример #1
0
def _examine(V1, V2, variants, logprior=None, _calc_lh=None):
    E, Es = lh.calc_lh(
        *[
            common.convert_variant_dict_to_tuple(V)
            for V in (variants[V1], variants[V2])
        ], _calc_lh)
    Es -= np.max(Es, axis=1)[:, None]
    sep = np.nan * np.ones(len(variants[V1]['var_reads']))[:, None]
    persamp = np.hstack((
        variants[V1]['var_reads'][:, None],
        variants[V1]['total_reads'][:, None],
        variants[V1]['vaf'][:, None],
        variants[V1]['omega_v'][:, None],
        variants[V1]['vaf'][:, None] / variants[V1]['omega_v'][:, None],
        sep,
        variants[V2]['var_reads'][:, None],
        variants[V2]['total_reads'][:, None],
        variants[V2]['vaf'][:, None],
        variants[V2]['omega_v'][:, None],
        variants[V2]['vaf'][:, None] / variants[V2]['omega_v'][:, None],
        sep,
        Es,
    ))

    post = _calc_posterior(E, _complete_logprior(logprior))
    return (persamp, E, post)
Пример #2
0
def calc_posterior(variants, logprior, rel_type, parallel=1):
    M = len(variants)
    # Allow Numba use by converting to namedtuple.
    vids = common.extract_vids(variants)
    variants = [
        common.convert_variant_dict_to_tuple(variants[V]) for V in vids
    ]

    posterior = mutrel.init_mutrel(vids)
    evidence = mutrel.init_mutrel(vids)
    pairs = list(itertools.combinations(range(M), 2)) + [(V, V)
                                                         for V in range(M)]

    _compute = lambda pbar: _compute_pairs(
        pairs,
        variants,
        logprior,
        posterior,
        evidence,
        pbar,
        parallel,
    )

    if parallel > 0:
        with tqdm.tqdm(total=len(pairs),
                       desc='Computing %s relations' % rel_type,
                       unit='pair',
                       dynamic_ncols=True) as pbar:
            posterior, evidence = _compute(pbar)
    else:
        posterior, evidence = _compute(None)
    return (posterior, evidence)
Пример #3
0
def add_variants(vids_to_add, variants, mutrel_posterior, mutrel_evidence,
                 logprior, pbar, parallel):
    for vid in vids_to_add:
        assert vid in variants

    new_vids = mutrel_posterior.vids + vids_to_add
    # M: number of variants we have now
    # A: number of variants we added
    M = len(new_vids)
    A = len(vids_to_add)
    assert len(mutrel_posterior.vids) == len(mutrel_evidence.vids) == M - A
    variants = [
        common.convert_variant_dict_to_tuple(variants[V]) for V in new_vids
    ]

    new_posterior = mutrel.init_mutrel(new_vids)
    new_evidence = mutrel.init_mutrel(new_vids)
    new_posterior.rels[:-A, :-A] = mutrel_posterior.rels
    new_evidence.rels[:-A, :-A] = mutrel_evidence.rels

    pairs = [(I, J) for I in range(M) for J in range(M - A, M)]

    return _compute_pairs(
        pairs,
        variants,
        logprior,
        new_posterior,
        new_evidence,
        pbar,
        parallel,
    )
Пример #4
0
def create_vars():
    variants = {
        #'V1': {'var_reads': [18], 'total_reads': [100]},
        #'V2': {'var_reads': [10], 'total_reads': [100]},
        'V1': {
            'var_reads': [500],
            'total_reads': [1000]
        },
        'V2': {
            'var_reads': [100],
            'total_reads': [1000]
        },

        #'V1': {'var_reads': [1702], 'total_reads': [4069]},
        #'V2': {'var_reads': [2500], 'total_reads': [19100]},

        #'V1': {'var_reads': [0], 'total_reads': [200]},
        #'V2': {'var_reads': [179], 'total_reads': [356]},
    }
    print(sorted(variants.items()))

    S = 1
    for vid, V in variants.items():
        for K in ('var_reads', 'total_reads'):
            V[K] = np.array(S * V[K]).astype(np.int)
        V['id'] = vid
        V['ref_reads'] = V['total_reads'] - V['var_reads']
        V['vaf'] = V['var_reads'].astype(np.float) / V['total_reads']
        V['omega_v'] = np.array(S * [0.5])

    variants = {
        vid: common.convert_variant_dict_to_tuple(V)
        for vid, V in variants.items()
    }
    return (variants['V1'], variants['V2'])