def processPathwayBKF(self, exhaustiveOr=False):
        assert len(self.pathways) > 0, "Have not processed pathways yet"
        for pathway in tqdm.tqdm(self.pathways,
                                 desc='Processing pathway BKFs'):
            bkf = BKB(name=pathway.pathwayID)

            pathwayActiveComp_idx = bkf.addComponent('{}_active='.format(
                pathway.pathwayID))
            pathwayActiveTrue_idx = bkf.addComponentState(
                pathwayActiveComp_idx, 'True')

            if exhaustiveOr:
                print("not implemented")
            else:
                geneSelectorComp_idx = bkf.addComponent("Gene_combo=")
                for gene in pathway.genes:  #gene[0] = geneName, gene[1] = low value gene[2] = high value
                    geneCombo_idx = bkf.addComponentState(
                        geneSelectorComp_idx, gene[0])

                    statConditionComp_idx = bkf.addComponent(
                        'mu-STD<={}<=mu+STD='.format(gene[0]))
                    statConditionTrue_idx = bkf.addComponentState(
                        statConditionComp_idx, 'True')

                    bkf.addSNode(
                        BKB_S_node(statConditionComp_idx,
                                   statConditionTrue_idx, 1.0))

                    bkf.addSNode(
                        BKB_S_node(
                            geneSelectorComp_idx, geneCombo_idx, 1.0,
                            [(statConditionComp_idx, statConditionTrue_idx)]))

                    bkf.addSNode(
                        BKB_S_node(pathwayActiveComp_idx,
                                   pathwayActiveTrue_idx, 1.0,
                                   [(geneSelectorComp_idx, geneCombo_idx)]))

                self.bkfs.append(bkf)
        assert len(self.pathways) > 0, "Have not processed pathways yet"
Ejemplo n.º 2
0
    for gene2 in GENES:
        count = 0
        for patient in PATIENTS:
            if patient_data[patient]['Genes'][gene1] and patient_data[patient][
                    'Genes'][gene2]:
                count += 1
    counts[(gene1, gene2)] = float(count / len(PATIENTS)) / priors[gene2]

#-- Make BKB Patient frags
for patient in PATIENTS:
    bkf = BKB()

    for gene in patient_data[patient]['Genes']:
        #-- Setup Gene i component.
        geneComp_idx = bkf.addComponent(gene)
        geneStateTrue_idx = bkf.addComponentState(geneComp_idx, 'True')

        s_node_prior = BKB_S_node(init_component_index=geneComp_idx,
                                  init_state_index=geneStateTrue_idx,
                                  init_probability=1)
        bkf.addSNode(s_node_prior)

    patient_bkfs.append(bkf)

#-- Define Sample Pathway
PATHWAYS = ['Pathway{}'.format(i) for i in range(1)]
pathway_data = {
    pathway: {
        gene: random.random() / 10
        for gene in set([random.choice(GENES) for _ in range(len(GENES))])
    }
Ejemplo n.º 3
0
        'Age': random.randrange(AGE_RANGE_YRS[0], AGE_RANGE_YRS[1]),
        'Survival': random.randrange(SURIVAL_YRS[0], SURIVAL_YRS[1])
    }
    for patient in PATIENTS
}
bkfs = list()

#-- Make BKB frags
for patient in PATIENTS:
    bkf = BKB()

    for gene in GENES:
        if random.choice([True, False]):
            #-- Setup Gene i component.
            comp_idx = bkf.addComponent('mut_{}'.format(gene))
            stateTrue_idx = bkf.addComponentState(comp_idx, 'True')

            bkf.addSNode(
                BKB_S_node(init_component_index=comp_idx,
                           init_state_index=stateTrue_idx,
                           init_probability=1))
            variant_comp_idx = bkf.addComponent('mut-var_{}'.format(gene))
            random_variant = random.choice(GENE_VARIANTS)
            variant_state_idx = bkf.addComponentState(variant_comp_idx,
                                                      random_variant)
            bkf.addSNode(
                BKB_S_node(init_component_index=variant_comp_idx,
                           init_state_index=variant_state_idx,
                           init_probability=1,
                           init_tail=[(comp_idx, stateTrue_idx)]))
            if 'Patient_Genes' not in patient_data[patient]:
    def processPathwayBKF(self, exhaustiveOr=False):
        assert len(self.pathways) > 0, "Have not processed pathways yet"

        for pathway in tqdm.tqdm(self.pathways,
                                 desc='Processing pathway BKFs'):
            bkf = BKB(name=pathway.ID)
            if "_hier" in pathway.ID:
                #head
                pathwayParentComp_idx = bkf.addComponent('{}_active='.format(
                    pathway.head))
                pathwayParentTrue_idx = bkf.addComponentState(
                    pathwayParentComp_idx, 'True')

                #tail - should only be 1 for _hier bkfs
                pathwayChildComp_idx = bkf.addComponent('{}_active='.format(
                    pathway.tails[0]))
                pathwayChildTrue_idx = bkf.addComponentState(
                    pathwayChildComp_idx, 'True')

                #S-node
                bkf.addSNode(
                    BKB_S_node(pathwayChildComp_idx, pathwayChildTrue_idx,
                               1.0))
                bkf.addSNode(
                    BKB_S_node(pathwayParentComp_idx, pathwayParentTrue_idx,
                               1.0,
                               [(pathwayChildComp_idx, pathwayChildTrue_idx)]))
            else:
                '''
                #head
                pathwayHierComp_idx = bkf.addComponent('{}_active='.format(pathway.head))
                pathwayHierTrue_idx = bkf.addComponentState(pathwayHierComp_idx, 'True')

                #tails
                for tail in pathway.tails:
                    statConditionComp_idx = bkf.addComponent('mu-STD<={}<=mu+STD'.format(tail))
                    statConditionTrue_idx = bkf.addComponentState(statConditionComp_idx, 'True')

                    bkf.addSNode(BKB_S_node(statConditionComp_idx, statConditionTrue_idx, 1.0))
                    bkf.addSNode(BKB_S_node(pathwayHierComp_idx, pathwayHierTrue_idx, 1.0, [(statConditionComp_idx, statConditionTrue_idx)]))

            self.bkfs.append(bkf)
                '''
                #=======

                if exhaustiveOr:
                    print("not implemented")
                else:
                    # pathway
                    pathwayReactionComp_idx = bkf.addComponent(pathway.head +
                                                               "_active=")
                    pathwayReactionTrue_idx = bkf.addComponentState(
                        pathwayReactionComp_idx, 'True')

                    # gene slector
                    geneSelectorComp_idx = bkf.addComponent("Gene_combo=")

                    #tails
                    for tail in pathway.tails:
                        geneCombo_idx = bkf.addComponentState(
                            geneSelectorComp_idx, tail)

                        statConditionComp = BKB_component("mu-STD>=" + tail +
                                                          "<=mu+STD=")
                        statConditionTrue = BKB_I_node('True',
                                                       statConditionComp)
                        statConditionComp_idx = bkf.addComponent("mu-STD>=" +
                                                                 tail +
                                                                 "<=mu+STD=")
                        statConditionTrue_idx = bkf.addComponentState(
                            statConditionComp_idx, 'True')

                        bkf.addSNode(
                            BKB_S_node(statConditionComp_idx,
                                       statConditionTrue_idx, 1.0))

                        bkf.addSNode(
                            BKB_S_node(geneSelectorComp_idx, geneCombo_idx,
                                       1.0, [(statConditionComp_idx,
                                              statConditionTrue_idx)]))

                        bkf.addSNode(
                            BKB_S_node(
                                pathwayReactionComp_idx,
                                pathwayReactionTrue_idx, 1.0,
                                [(geneSelectorComp_idx, geneCombo_idx)]))

            self.bkfs.append(bkf)
    def processPatientBKF(self):
        assert len(
            self.patients) > 0, "Have not processed Patient and gene data yet."
        #print("Forming Patient BKFs...")

        allGenes = list()
        for pat in tqdm.tqdm(self.patients, desc='Forming patient BKFs'):
            bkf = BKB(name=pat.patientID)
            variantCompIndices = list()
            variantCompNames = list()
            variantCompStateIndices = list()
            for idx, gene in enumerate(pat.mutatedGenes):
                allGenes.append(gene)
                # gene
                mutGeneComp_idx = bkf.addComponent('mut_{}='.format(gene))
                iNodeGeneMut_idx = bkf.addComponentState(
                    mutGeneComp_idx, 'True')
                # form SNode  o---->[mut_<genename>=True]
                bkf.addSNode(BKB_S_node(mutGeneComp_idx, iNodeGeneMut_idx,
                                        1.0))

                if 'var_' + pat.variants[idx] + '=' in variantCompNames:
                    cIdx = variantCompNames.index('var_{}='.format(
                        pat.variants[idx]))

                    #mut_var combo
                    mutVarComp_idx = bkf.addComponent('mut-var_{}='.format(
                        pat.mutatedGeneVariants[idx]))
                    iNodeMutVar_idx = bkf.addComponentState(
                        mutVarComp_idx, 'True')
                    #mut_var s-node
                    bkf.addSNode(
                        BKB_S_node(mutVarComp_idx, iNodeMutVar_idx, 1.0,
                                   [(mutGeneComp_idx, iNodeGeneMut_idx),
                                    (variantCompIndices[cIdx],
                                     variantCompStateIndices[cIdx])]))
                else:
                    #var
                    variantComp_idx = bkf.addComponent('var_{}='.format(
                        pat.variants[idx]))
                    iNodeVariant_idx = bkf.addComponentState(
                        variantComp_idx, 'True')
                    #var s-node
                    bkf.addSNode(
                        BKB_S_node(variantComp_idx, iNodeVariant_idx, 1.0))

                    variantCompIndices.append(variantComp_idx)
                    variantCompStateIndices.append(iNodeGeneMut_idx)
                    variantCompNames.append(
                        bkf.getComponentName(variantComp_idx))

                    #mut_var combo
                    mutVarComp_idx = bkf.addComponent('mut-var_{}='.format(
                        pat.mutatedGeneVariants[idx]))
                    iNodeMutVar_idx = bkf.addComponentState(
                        mutVarComp_idx, 'True')
                    #mut_var s-node
                    bkf.addSNode(
                        BKB_S_node(mutVarComp_idx, iNodeMutVar_idx, 1.0,
                                   [(mutGeneComp_idx, iNodeGeneMut_idx),
                                    (variantComp_idx, iNodeVariant_idx)]))
            self.bkfs.append(bkf)
from reasoner import Reasoner, _constructSNodesByHead
from query import Query

PATIENTS = ['Patient_{}'.format(i) for i in range(10)]
NUM_GENES = 5

bkfs = list()

#-- Make BKB frags
for j, _ in enumerate(PATIENTS):
    bkf = BKB()

    for i in range(NUM_GENES):
        #-- Setup Gene i component.
        comp_idx = bkf.addComponent('Gene{}_mutated'.format(i))
        stateTrue_idx = bkf.addComponentState(comp_idx, 'True')

        if random.choice([True, False]):
            bkf.addSNode(
                BKB_S_node(init_component_index=comp_idx,
                           init_state_index=stateTrue_idx,
                           init_probability=1))
    bkfs.append(bkf)

#-- Fuse patients together.
fused_bkb = fuse(bkfs, [1 for _ in range(len(PATIENTS))],
                 [str(hash(patient)) for patient in PATIENTS],
                 working_dir=os.getcwd())


#-- Impose Structure while maintiaining mutual exclusivity.