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"
for gene1 in GENES: 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))])
'Gender': random.choice(GENDER_OPTIONS), '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)]))
def read_expression_snodes(self, expression_levels_dict, tcga_expression_file, react_gene_file, pathways_file): #-- Process all gene_reaction s_nodes and assume there is a header with gene names. with open(react_gene_file, 'r') as csv_file: reader = csv.reader(csv_file) rows = [row for row in reader] #-- Process Gene Names from header: print(rows[0]) genes = rows[0][1:] genes = [genes[i][:-4] for i in range(0,len(genes),2)] print(genes) for row in rows[1:]: bkf = BKB(name=row[0] + ' BKF') reaction_head_name = row[0] react_component = BKB_component(reaction_head_name) i_node_true = BKB_I_node('True', react_component) i_node_false = BKB_I_node('False', react_component) react_component.addINode(i_node_true) react_component.addINode(i_node_false) bkf.addComponent(react_component) #-- Extract and Make I-nodes for gene_j, row_i in enumerate(range(1,len(row), 2)): gene_name = genes[gene_j] gene_low = row[row_i] gene_high = row[row_i + 1] if gene_low != '': print(gene_low, gene_high) num_levels = expression_levels_dict[gene_name] levels = [rag for rag in np.linspace(int(gene_low), int(gene_high), num_levels)] component_states = ['{} | {}'.format(levels[i], levels[i+1]) for i in range(len(levels)-1)] component = BKB_component(gene_name + ' ExpressionLv') for state in component_states: i_node = BKB_I_node(state, component) component.addINode(i_node) bkf.addComponent(component) for idx in range(bkf.findComponent(component.name).getNumberStates()): i_node = component.getState(idx) bkf.addSNode(BKB_S_node(component, i_node, random.random())) bkf.addSNode(BKB_S_node(react_component, i_node_true, random.random(), [(component, i_node)])) bkf.addSNode(BKB_S_node(react_component, i_node_false, random.random(), [(component, i_node)])) self.bkfs.append(bkf) print('Done') #-- Process Pathway BKFs with open(pathways_file, 'r') as csv_file: reader = csv.reader(csv_file) bkf = BKB() for row in reader: tail = row[0] head = row[1] states = ['True', 'False'] if bkf.findComponent(tail) == -1: component_tail = BKB_component(tail) for state in states: component_tail.addINode(BKB_I_node(state, component_tail)) bkf.addComponent(component_tail) else: component_tail = bkf.findComponent(tail) if bkf.findComponent(head) == -1: component_head = BKB_component(head) for state in states: component_head.addINode(BKB_I_node(state, component_head)) bkf.addComponent(component_head) else: component_head = bkf.findComponent(head) for state1 in states: for state2 in states: bkf.addSNode(BKB_S_node(component_head, component_head.findState(state1), random.random(), [(component_tail, component_tail.findState(state2))]) ) self.bkfs.append(bkf) print('Complete')
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())