def test_expand_90191(self): csmile_90191 = "[H][O][C]([H])([H])[C]([H])([O][H])[C]([H])([H])[H]" compound_1 = Compound("[H+]", name="1") compound_6 = Compound( "[H][N]=[C]([O][H])[C]1=[C]([H])[N]([C]2([H])[O][C]([H])([C]([H])([H])[O][P](=[O])([O][H])[O][P](=[O])([O][H])[O][C]([H])([H])[C]3([H])[O][C]([H])([n]4[c]([H])[n][c]5[c]([N]([H])[H])[n][c]([H])[n][c]54)[C]([H])([O][P](=[O])([O][H])[O][H])[C]3([H])[O][H])[C]([H])([O][H])[C]2([H])[O][H])[C]([H])=[C]([H])[C]1([H])[H]", name='6') compound_10 = Compound( "[H][N]=[C]([O][H])[C]1=[C]([H])[N]([C]2([H])[O][C]([H])([C]([H])([H])[O][P](=[O])([O][H])[O][P](=[O])([O][H])[O][C]([H])([H])[C]3([H])[O][C]([H])([n]4[c]([H])[n][c]5[c]([N]([H])[H])[n][c]([H])[n][c]54)[C]([H])([O][H])[C]3([H])[O][H])[C]([H])([O][H])[C]2([H])[O][H])[C]([H])=[C]([H])[C]1([H])[H]", name="10") compound_909 = Compound("[H][O][C]([H])([C]([H])=[O])[C]([H])([H])[H]", name=909) # compound_2345 = Compound("[H][C](=[O])[C]([H])=[C]([H])[H]", name = "2345") compound_90191 = Compound(csmile_90191) resulting_state_95713 = ChemicalCompoundState( [compound_1, compound_10, compound_909], organism=Test_organism_H) resulting_state_903108 = ChemicalCompoundState( [compound_1, compound_6, compound_909], organism=Test_organism_H) root_state = ChemicalCompoundState(compound_90191, organism=Test_organism_H) root_node = MCTS_node(root_state) root_node.expand() result_95713 = (root_node.children[0].state == resulting_state_95713 or root_node.children[0].state == resulting_state_903108) result_903108 = (root_node.children[1].state == resulting_state_903108 or root_node.children[1].state == resulting_state_95713) assert (result_95713 and result_903108)
def test_add_compound_to_state_no(self): csmile = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" compound = Compound(csmile) compound_2 = Compound(csmile) state = ChemicalCompoundState(compound, organism=Test_organism_H) state._add_compound_to_state(compound_2) assert len(state) == 1
def test_GetMoves_state_and_compound(self): compound_10 = Compound( "[H][N]=[C]([O][H])[C]1=[C]([H])[N]([C]2([H])[O][C]([H])([C]([H])([H])[O][P](=[O])([O][H])[O][P](=[O])([O][H])[O][C]([H])([H])[C]3([H])[O][C]([H])([n]4[c]([H])[n][c]5[c]([N]([H])[H])[n][c]([H])[n][c]54)[C]([H])([O][H])[C]3([H])[O][H])[C]([H])([O][H])[C]2([H])[O][H])[C]([H])=[C]([H])[C]1([H])[H]" ) state_test_10 = ChemicalCompoundState( compound_10, organism=Test_organism_H, available_rules=applicable_rules_mixed_dict) moves_compound = compound_10.obtain_applicable_transformation_with_move( available_rules=applicable_rules_mixed_dict) print("moves_compound: {}".format(moves_compound)) del compound_10 compound_10 = Compound( "[H][N]=[C]([O][H])[C]1=[C]([H])[N]([C]2([H])[O][C]([H])([C]([H])([H])[O][P](=[O])([O][H])[O][P](=[O])([O][H])[O][C]([H])([H])[C]3([H])[O][C]([H])([n]4[c]([H])[n][c]5[c]([N]([H])[H])[n][c]([H])[n][c]54)[C]([H])([O][H])[C]3([H])[O][H])[C]([H])([O][H])[C]2([H])[O][H])[C]([H])=[C]([H])[C]1([H])[H]" ) state_test_10 = ChemicalCompoundState( compound_10, organism=Test_organism_H, available_rules=applicable_rules_mixed_dict) moves_state = state_test_10.GetMoves(top_x=10) print("moves_state: {}".format(moves_state)) identicial_moves = len(moves_state) == len(moves_compound) for move in moves_state: identicial_moves = identicial_moves and move.in_list( moves_compound, main_layer=False) assert identicial_moves
def test_expand_821(self): csmile_821 = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" csmile_90191 = "[H][O][C]([H])([H])[C]([H])([O][H])[C]([H])([H])[H]" compound = Compound(csmile_821, "821") compound_1 = Compound("[H+]", name="1") compound_6 = Compound( "[H][N]=[C]([O][H])[C]1=[C]([H])[N]([C]2([H])[O][C]([H])([C]([H])([H])[O][P](=[O])([O][H])[O][P](=[O])([O][H])[O][C]([H])([H])[C]3([H])[O][C]([H])([n]4[c]([H])[n][c]5[c]([N]([H])[H])[n][c]([H])[n][c]54)[C]([H])([O][P](=[O])([O][H])[O][H])[C]3([H])[O][H])[C]([H])([O][H])[C]2([H])[O][H])[C]([H])=[C]([H])[C]1([H])[H]", name='6') compound_2345 = Compound("[H][C](=[O])[C]([H])=[C]([H])[H]", name="2345") compound_90191 = Compound(csmile_90191, "90191") resulting_state_94682 = ChemicalCompoundState([compound_90191], organism=Test_organism_H) resulting_state_117465 = ChemicalCompoundState( [compound_1, compound_6, compound_2345], organism=Test_organism_H) root_state = ChemicalCompoundState(compound, organism=Test_organism_H) root_node = MCTS_node(root_state, chemical_score=True) root_node.expand() for child in root_node.children: if child.move.rid == "MNXR94682_MNXM821": i_94682 = root_node.children.index(child) if child.move.rid == "MNXR117465_MNXM821": i_117465 = root_node.children.index(child) result_94682 = ( root_node.children[i_94682].state == resulting_state_94682) result_117465 = ( root_node.children[i_117465].state == resulting_state_117465) assert (result_94682 and result_117465)
def test_compound_in_state_yes(self): csmile = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" unstandardised = "C(=O)CC" compound = Compound(csmile) compound_ns = Compound(unstandardised) state = ChemicalCompoundState( [compound], organism=Test_organism_H) # state is not sanitised assert state.compound_in_state(compound_ns)
def test_compound_in_state_no(self): csmile = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" csmile_2 = "[H][O][C]([H])([H])[C]([H])([O][H])[C]([H])([H])[H]" compound = Compound(csmile) compound_2 = Compound(csmile_2) state = ChemicalCompoundState( [compound], organism=Test_organism_H) # state is not sanitised assert not state.compound_in_state(compound_2)
def test_cloning(self): csmile = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" compound = Compound(csmile) state = ChemicalCompoundState(compound) cloned_state = state.clone() different_python_object = (id(state) != id(cloned_state)) identical_chemical_object = (state == cloned_state) assert (different_python_object and identical_chemical_object)
def test_check_sanitized_state_yes(self): csmile = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" csmile_2 = "[H][O][C]([H])([H])[C]([H])([O][H])[C]([H])([H])[H]" compound = Compound(csmile) compound_2 = Compound(csmile_2) state = ChemicalCompoundState([compound, compound_2]) # state is not sanitised assert state._check_sanitized_state()
def test_check_sanitized_state_not(self): csmile = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" unstandardised = "C(=O)CC" compound = Compound(csmile) compound_ns = Compound(unstandardised) state = ChemicalCompoundState( [compound, compound_ns], main_layer=True) # state is not sanitised assert state._check_sanitized_state()
def test_GetResults_from_InChI_Key_fraction(self): csmile = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" chassis_2_smile = "[H][O][C](=[O])[C](=[O])[C]([H])([H])[C]([H])([O][H])[C]([H])([O][H])[C]([H])([H])[H]" outside_compound = Compound(csmile) chassis_2 = Compound(chassis_2_smile) state_chassis = ChemicalCompoundState([chassis_2, outside_compound], organism=Test_organism_H) assert state_chassis.GetResults_from_InChI_Keys( rewarding=Basic_Rollout_Reward) == 1 / 2
def test_node_rollout_possible(self): csmile_821 = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" compound_821 = Compound(csmile_821, "821") state_1 = ChemicalCompoundState([compound_821], organism=Test_organism_H) node_1 = MCTS_node(state_1) moves = state_1.GetMoves(top_x=2, chemical_score=False) rolled_state = node_1.rollout() assert (node_1.state != rolled_state)
def test_GetResultsForBiosensors_absent(self): chassis_1_smile = "[H+]" chassis_1 = Compound(chassis_1_smile) benzoate_chassis = ChemicalCompoundState([chassis_1], organism=detectable_cmpds_H) retrosynthesis = benzoate_chassis.GetResults_from_InChI_Keys( rewarding=Basic_Rollout_Reward) biosensor = benzoate_chassis.GetResultsForBiosensors( rewarding=Basic_Rollout_Reward) assert retrosynthesis == -1 and biosensor == -1
def test_GetResults_from_InChI_Key_reward(self): chassis_1_smile = "[H+]" chassis_2_smile = "[H][O][C](=[O])[C](=[O])[C]([H])([H])[C]([H])([O][H])[C]([H])([O][H])[C]([H])([H])[H]" chassis_1 = Compound(chassis_1_smile) chassis_2 = Compound(chassis_2_smile) state_chassis = ChemicalCompoundState([chassis_1, chassis_2], organism=Test_organism_H) assert state_chassis.GetResults_from_InChI_Keys( rewarding=Basic_Rollout_Reward ) == Basic_Rollout_Reward.full_state_reward
def test_equal_two(slef): csmile = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" csmile_2 = "[H][O][C]([H])([H])[C]([H])([O][H])[C]([H])([H])[H]" compound = Compound(csmile) compound_2 = Compound(csmile_2) state = ChemicalCompoundState([compound, compound_2], organism=Test_organism_H) state_2 = ChemicalCompoundState([compound, compound_2], organism=Test_organism_H) assert state == state_2
def test_compound_in_chassis_state(self): compound_1 = Compound("[H+]") compound_6 = Compound( "[H][N]=[C]([O][H])[C]1=[C]([H])[N]([C]2([H])[O][C]([H])([C]([H])([H])[O][P](=[O])([O][H])[O][P](=[O])([O][H])[O][C]([H])([H])[C]3([H])[O][C]([H])([n]4[c]([H])[n][c]5[c]([N]([H])[H])[n][c]([H])[n][c]54)[C]([H])([O][P](=[O])([O][H])[O][H])[C]3([H])[O][H])[C]([H])([O][H])[C]2([H])[O][H])[C]([H])=[C]([H])[C]1([H])[H]" ) compound_3459 = Compound( "[H][O][C](=[O])[C](=[O])[C]([H])([H])[C]([H])([O][H])[C]([H])([O][H])[C]([H])([H])[H]" ) chassis_metabolites = ChemicalCompoundState( [compound_1, compound_6, compound_3459], organism=Test_organism_H) assert chassis_metabolites.compound_in_state(compound_1)
def test_GetResultsForBiosensors_present(self): chassis_1_smile = "[H+]" chassis_1 = Compound(chassis_1_smile) benzoate = Compound( InChI="InChI=1S/C7H6O2/c8-7(9)6-4-2-1-3-5-6/h1-5H,(H,8,9)/p-1") benzoate_chassis = ChemicalCompoundState([benzoate, chassis_1], organism=detectable_cmpds_H) retrosynthesis = benzoate_chassis.GetResults_from_InChI_Keys( rewarding=Basic_Rollout_Reward) biosensor = benzoate_chassis.GetResultsForBiosensors( rewarding=Basic_Rollout_Reward) assert retrosynthesis == 0.5 and biosensor == 2
def test_saving_and_loading(self): compound_1 = Compound("[H+]") compound_10 = Compound( "[H][N]=[C]([O][H])[C]1=[C]([H])[N]([C]2([H])[O][C]([H])([C]([H])([H])[O][P](=[O])([O][H])[O][P](=[O])([O][H])[O][C]([H])([H])[C]3([H])[O][C]([H])([n]4[c]([H])[n][c]5[c]([N]([H])[H])[n][c]([H])[n][c]54)[C]([H])([O][H])[C]3([H])[O][H])[C]([H])([O][H])[C]2([H])[O][H])[C]([H])=[C]([H])[C]1([H])[H]" ) compound_909 = Compound("[H][O][C]([H])([C]([H])=[O])[C]([H])([H])[H]") chassis_1_10_909 = ChemicalCompoundState( [compound_1, compound_10, compound_909], organism=Test_organism_H) chassis_1_10_909.save(folder_address="tests/generated_jsons/pickles") chassis_load = unpickle("BOPG_BSAB_GPRL", type="state", folder_address="tests/data") assert chassis_1_10_909 == chassis_load
def test_GetMoves(self): # will have to be modified once we have proper scores # random.seed(42) csmile = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" csmile_2 = "[H][O][C]([H])([H])[C]([H])([O][H])[C]([H])([H])[H]" compound = Compound(csmile) compound_2 = Compound(csmile_2) state_2 = ChemicalCompoundState([compound, compound_2], organism=Test_organism_H) moves = state_2.GetMoves(top_x=2, chemical_score=False) for move in moves: print(move.rid) assert set([move.rid for move in moves ]) == set(['MNXR103108_MNXM90191', 'MNXR95713_MNXM90191'])
def test_proper_child_addition_821_rule_94682_moves(self): # random.seed(42) csmile = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" csmile_2 = "[H][O][C]([H])([H])[C]([H])([O][H])[C]([H])([H])[H]" compound = Compound(csmile, "821") compound_2 = Compound(csmile_2, "90191") known_result_state = ChemicalCompoundState([compound_2], organism=Test_organism_H) state_apply_move = ChemicalCompoundState(compound, organism=Test_organism_H) moves = state_apply_move.GetMoves(top_x=2) # state_apply_move.ApplyMove(list(moves.keys())[0]) assert set([move.rid for move in moves ]) == set(['MNXR94682_MNXM821', 'MNXR117465_MNXM821'])
def test_equality_statement_not_expanded(self): csmile = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" compound = Compound(csmile, name="821") state = ChemicalCompoundState( [compound], organism=Test_organism_H, representation=Test_representation) # state is not sanitised state_bis = ChemicalCompoundState( [compound], organism=Test_organism_H, representation=Test_representation) # state is not sanitised test_Tree = Tree(root_state=state, itermax=100) test_Tree_bis = Tree(root_state=state_bis, itermax=100) assert test_Tree == test_Tree_bis
def test_differing_compound_not_in_other(self): compound_1 = Compound("[H+]") compound_6 = Compound( "[H][N]=[C]([O][H])[C]1=[C]([H])[N]([C]2([H])[O][C]([H])([C]([H])([H])[O][P](=[O])([O][H])[O][P](=[O])([O][H])[O][C]([H])([H])[C]3([H])[O][C]([H])([n]4[c]([H])[n][c]5[c]([N]([H])[H])[n][c]([H])[n][c]54)[C]([H])([O][P](=[O])([O][H])[O][H])[C]3([H])[O][H])[C]([H])([O][H])[C]2([H])[O][H])[C]([H])=[C]([H])[C]1([H])[H]" ) compound_2345 = Compound("[H][C](=[O])[C]([H])=[C]([H])[H]", name="2345") compound_3459 = Compound( "[H][O][C](=[O])[C](=[O])[C]([H])([H])[C]([H])([O][H])[C]([H])([O][H])[C]([H])([H])[H]" ) state_1 = ChemicalCompoundState( [compound_1, compound_6, compound_2345]) state_2 = ChemicalCompoundState( [compound_1, compound_6, compound_3459]) assert state_1.not_in_other(state_2)[0] == compound_2345
def import_organism_from_csv(csv_file, add_Hs=True): with open(csv_file) as csv_handle: dict_reader = csv.DictReader(csv_handle, delimiter=',') compound_list = [] for row in dict_reader: name = row["name"] inchi = row["inchi"] if inchi is None or inchi == "None" or inchi == "": pass else: try: if name.startswith("InChI"): compound = Compound(InChI=inchi, heavy_standardisation=True, force_add_H=add_Hs) else: compound = Compound(InChI=inchi, name=name, heavy_standardisation=True, force_add_H=add_Hs) if not compound.in_list(compound_list, main_layer=False): compound_list.append(compound) except ChemConversionError as e: logging.error( "For compound {} with inchi {}: error ChemConversionError" .format(name, inchi)) organism = ChemicalCompoundState(compound_list, main_layer=False) return (organism)
def test_node_equality_true(self): compound_6 = Compound( "[H][N]=[C]([O][H])[C]1=[C]([H])[N]([C]2([H])[O][C]([H])([C]([H])([H])[O][P](=[O])([O][H])[O][P](=[O])([O][H])[O][C]([H])([H])[C]3([H])[O][C]([H])([n]4[c]([H])[n][c]5[c]([N]([H])[H])[n][c]([H])[n][c]54)[C]([H])([O][P](=[O])([O][H])[O][H])[C]3([H])[O][H])[C]([H])([O][H])[C]2([H])[O][H])[C]([H])=[C]([H])[C]1([H])[H]", name='6') compound_10 = Compound( "[H][N]=[C]([O][H])[C]1=[C]([H])[N]([C]2([H])[O][C]([H])([C]([H])([H])[O][P](=[O])([O][H])[O][P](=[O])([O][H])[O][C]([H])([H])[C]3([H])[O][C]([H])([n]4[c]([H])[n][c]5[c]([N]([H])[H])[n][c]([H])[n][c]54)[C]([H])([O][H])[C]3([H])[O][H])[C]([H])([O][H])[C]2([H])[O][H])[C]([H])=[C]([H])[C]1([H])[H]", name="10") compound_909 = Compound("[H][O][C]([H])([C]([H])=[O])[C]([H])([H])[H]", name=909) state_1 = ChemicalCompoundState( [compound_6, compound_10, compound_909], organism=Test_organism_H) state_2 = ChemicalCompoundState( [compound_6, compound_10, compound_909], organism=Test_organism_H) node_1 = MCTS_node(state_1) node_2 = MCTS_node(state_2) assert (node_1 == node_2)
def test_proper_root_initialisation(self): csmile = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" compound = Compound(csmile, name="821") state = ChemicalCompoundState( [compound], organism=Test_organism_H) # state is not sanitised node = MCTS_node(state) assert node._verif_initilisation()
def test_terminal_state_false(self): csmile = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" compound = Compound(csmile, name="821") state = ChemicalCompoundState( [compound], organism=Test_organism_H) # state is not sanitised node = MCTS_node(state) assert node.terminal == False
def test_expand_1_6_2345_same_node(self): compound_1 = Compound("[H+]", name="1") compound_6 = Compound( "[H][N]=[C]([O][H])[C]1=[C]([H])[N]([C]2([H])[O][C]([H])([C]([H])([H])[O][P](=[O])([O][H])[O][P](=[O])([O][H])[O][C]([H])([H])[C]3([H])[O][C]([H])([n]4[c]([H])[n][c]5[c]([N]([H])[H])[n][c]([H])[n][c]54)[C]([H])([O][P](=[O])([O][H])[O][H])[C]3([H])[O][H])[C]([H])([O][H])[C]2([H])[O][H])[C]([H])=[C]([H])[C]1([H])[H]", name='6') compound_2345 = Compound("[H][C](=[O])[C]([H])=[C]([H])[H]", name="2345") state = ChemicalCompoundState( [compound_1, compound_6, compound_2345], organism=Test_organism_H) # state is not sanitised node = MCTS_node(state) node.expand() state_ue = ChemicalCompoundState( [compound_1, compound_6, compound_2345], organism=Test_organism_H) # state is not sanitised node_ue = MCTS_node(state_ue) assert node == node_ue
def test_ApplyMoves(self): csmile = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" csmile_2 = "[H][O][C]([H])([H])[C]([H])([O][H])[C]([H])([H])[H]" compound = Compound(csmile) compound_2 = Compound(csmile_2) known_result_state = ChemicalCompoundState([compound_2], organism=Test_organism_H) state_apply_move = ChemicalCompoundState([compound], organism=Test_organism_H) moves = state_apply_move.GetMoves(top_x=2) # move = moves[list(moves.keys())[0]] for move in moves: if move.rid == "MNXR94682_MNXM821": i = moves.index(move) move = moves[i] state_apply_move.ApplyMove(move) assert state_apply_move == known_result_state
def test_equal_two_from_addition(self): csmile = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" csmile_2 = "[H][O][C]([H])([H])[C]([H])([O][H])[C]([H])([H])[H]" compound = Compound(csmile) compound_2 = Compound(csmile_2) state = ChemicalCompoundState(compound, organism=Test_organism_H) state_2 = ChemicalCompoundState(compound_2, organism=Test_organism_H) state._add_compound_to_state(compound_2) state_2._add_compound_to_state(compound) assert state == state_2
def test_node_equality_false_expanded_updated(self): csmile_821 = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" compound_821 = Compound(csmile_821, "821") compound_10 = Compound( "[H][N]=[C]([O][H])[C]1=[C]([H])[N]([C]2([H])[O][C]([H])([C]([H])([H])[O][P](=[O])([O][H])[O][P](=[O])([O][H])[O][C]([H])([H])[C]3([H])[O][C]([H])([n]4[c]([H])[n][c]5[c]([N]([H])[H])[n][c]([H])[n][c]54)[C]([H])([O][H])[C]3([H])[O][H])[C]([H])([O][H])[C]2([H])[O][H])[C]([H])=[C]([H])[C]1([H])[H]", name="10") compound_909 = Compound("[H][O][C]([H])([C]([H])=[O])[C]([H])([H])[H]", name="909") state_1 = ChemicalCompoundState( [compound_821, compound_10, compound_909], organism=Test_organism_H) state_2 = ChemicalCompoundState( [compound_821, compound_10, compound_909], organism=Test_organism_H) node_1 = MCTS_node(state_1) node_2 = MCTS_node(state_2) node_2.update(10, solved=True) assert (node_1 != node_2)
def test_proper_child_addition_821_rule_94682_moves_state(self): # random.seed(42) csmile = "[H][C](=[O])[C]([H])([H])[C]([H])([H])[H]" csmile_2 = "[H][O][C]([H])([H])[C]([H])([O][H])[C]([H])([H])[H]" compound = Compound(csmile, "821") compound_2 = Compound(csmile_2, "90191") known_result_state = ChemicalCompoundState([compound_2], organism=Test_organism_H) state_apply_move = ChemicalCompoundState(compound, organism=Test_organism_H) moves = state_apply_move.GetMoves(top_x=2) for move in moves: print(move.rid) if move.rid == "MNXR94682_MNXM821": i = moves.index(move) # print(moves) move_94682 = moves[i] state_apply_move.ApplyMove(move_94682) assert state_apply_move == known_result_state