def _get_fake_complex() -> stk.BuildingBlock: return stk.BuildingBlock(smiles='BrCc1cc(CBr)cc(CBr)c1', functional_groups=[stk.BromoFactory()])
import pytest import stk from .case_data import CaseData @pytest.fixture( scope='session', params=(lambda: CaseData( molecule=stk.BuildingBlock('BrCCBr', [stk.BromoFactory()]), writer=stk.MolWriter(), string=('\n RDKit 3D\n\n 0 0 0 0 0 0 0 0 ' ' 0 0999 V3000\nM V30 BEGIN CTAB\nM V30 COUNTS 8 7 ' '0 0 0\nM V30 BEGIN ATOM\nM V30 1 Br -1.4238 1.5615 ' '0.3223 0\nM V30 2 C -0.7405 -0.2573 0.1280 0\nM V30' ' 3 C 0.7148 -0.1157 -0.3383 0\nM V30 4 Br 1.6267 0.8' '896 1.0687 0\nM V30 5 H -1.3518 -0.8075 -0.5939 0\nM' ' V30 6 H -0.7769 -0.6964 1.1440 0\nM V30 7 H 0.7695' ' 0.5280 -1.2387 0\nM V30 8 H 1.1821 -1.1022 -0.4922 ' '0\nM V30 END ATOM\nM V30 BEGIN BOND\nM V30 1 1 1 2' '\nM V30 2 1 2 3\nM V30 3 1 3 4\nM V30 4 1 2 5\nM ' 'V30 5 1 2 6\nM V30 6 1 3 7\nM V30 7 1 3 8\nM V30 E' 'ND BOND\nM V30 END CTAB\nM END\n\n$$$$\n'), ), ), ) def case_data(request) -> CaseData: return request.param()
import pytest import stk import numpy as np @pytest.fixture(params=( stk.BuildingBlock('NCCN'), stk.BuildingBlock('Brc1ccc(Br)cc1Br', [stk.BromoFactory()]), stk.ConstructedMolecule(topology_graph=stk.polymer.Linear( building_blocks=( stk.BuildingBlock('BrCCBr', [stk.BromoFactory()]), stk.BuildingBlock('BrCNCCBr', [stk.BromoFactory()]), ), repeating_unit='AB', num_repeating_units=2, ), ), )) def molecule(request): """ A :class:`.Molecule` instance with at least 2 atoms. """ return request.param.clone() @pytest.fixture( params=[ np.array([1., 0., 0.]), np.array([0., 1., 0.]),
vertex1 = next(vertices) for id_, vertex2 in enumerate(vertices): yield stk.Edge(id_, vertex1, vertex2) vertex1 = vertex2 @pytest.fixture def building_block_vertices(building_block1, building_block2): return { building_block1: ( stk.Vertex(0, [0, 0, 0]), stk.Vertex(1, [10, 0, 0]), ), building_block2: ( stk.Vertex(2, [20, 0, 0]), stk.Vertex(3, [30, 0, 0]), ), } @pytest.fixture( params=(stk.BuildingBlock('BrCC', [stk.BromoFactory()]), ), ) def building_block1(request): return request.param @pytest.fixture( params=(stk.BuildingBlock('BrCCBr', [stk.BromoFactory()]), ), ) def building_block2(request): return request.param
import pytest import stk from ..case_data import CaseData def has_bromo(building_block): fg, = building_block.get_functional_groups(0) return fg.__class__ is stk.Bromo bb1 = stk.BuildingBlock('BrCCBr', [stk.BromoFactory()]) graph1 = stk.polymer.Linear((bb1, ), 'A', 2) bb2 = stk.BuildingBlock('BrCNCBr', [stk.BromoFactory()]) graph2 = stk.polymer.Linear((bb2, ), 'A', 2) bb3 = stk.BuildingBlock('BrCNNCCNCBr', [stk.BromoFactory()]) @pytest.fixture( params=(CaseData( mutator=stk.SimilarBuildingBlock( building_blocks=(bb2, bb3), is_replaceable=has_bromo, ), record=stk.MoleculeRecord(graph1), mutation_record=stk.MutationRecord( molecule_record=stk.MoleculeRecord(graph2), mutator_name='SimilarBuildingBlock', ),
import pytest import stk from .case_data import CaseData bb1 = stk.BuildingBlock('BrCCBr', [stk.BromoFactory()]) @pytest.fixture( params=( CaseData( molecule=bb1, writer=stk.XyzWriter(), string=( '8\n\nBr -1.423838 1.561473 0.322335\nC -0.740543 -0.2' '57311 0.127980\nC 0.714791 -0.115704 -0.338259\nBr 1.' '626726 0.889555 1.068701\nH -1.351758 -0.807456 -0.59' '3854\nH -0.776931 -0.696380 1.144036\nH 0.769475 0.52' '7986 -1.238698\nH 1.182078 -1.102163 -0.492240\n' ), ), ), ) def case_data(request): return request.param
def main(): parser = argparse.ArgumentParser() parser.add_argument('--mongodb_uri', help='The MongoDB URI for the database to connect to.', default='mongodb://localhost:27017/') args = parser.parse_args() logging.basicConfig(level=logging.INFO) # Use a random seed to get reproducible results. random_seed = 4 generator = np.random.RandomState(random_seed) logger.info('Making building blocks.') # Load the building block databases. fluoros = tuple( get_building_blocks( path=pathlib.Path(__file__).parent / 'fluoros.txt', functional_group_factory=stk.FluoroFactory(), )) bromos = tuple( get_building_blocks( path=pathlib.Path(__file__).parent / 'bromos.txt', functional_group_factory=stk.BromoFactory(), )) initial_population = tuple(get_initial_population(fluoros, bromos)) # Write the initial population. for i, record in enumerate(initial_population): write(record.get_molecule(), f'initial_{i}.mol') client = pymongo.MongoClient(args.mongodb_uri) db = stk.ConstructedMoleculeMongoDb(client) fitness_db = stk.ValueMongoDb(client, 'fitness_values') # Plot selections. generation_selector = stk.Best( num_batches=25, duplicate_molecules=False, ) stk.SelectionPlotter('generation_selection', generation_selector) mutation_selector = stk.Roulette( num_batches=5, random_seed=generator.randint(0, 1000), ) stk.SelectionPlotter('mutation_selection', mutation_selector) crossover_selector = stk.Roulette( num_batches=3, batch_size=2, random_seed=generator.randint(0, 1000), ) stk.SelectionPlotter('crossover_selection', crossover_selector) fitness_calculator = stk.PropertyVector( property_functions=( get_num_rotatable_bonds, get_complexity, get_num_bad_rings, ), input_database=fitness_db, output_database=fitness_db, ) fitness_normalizer = stk.NormalizerSequence( fitness_normalizers=( # Prevent division by 0 error in DivideByMean, by ensuring # a value of each property to be at least 1. stk.Add((1, 1, 1)), stk.DivideByMean(), # Obviously, because all coefficients are equal, the # Multiply normalizer does not need to be here. However, # it's here to show that you can easily change the relative # importance of each component of the fitness value, by # changing the values of the coefficients. stk.Multiply((1, 1, 1)), stk.Sum(), stk.Power(-1), ), ) ea = stk.EvolutionaryAlgorithm( num_processes=1, initial_population=initial_population, fitness_calculator=fitness_calculator, mutator=stk.RandomMutator( mutators=( stk.RandomBuildingBlock( building_blocks=fluoros, is_replaceable=is_fluoro, random_seed=generator.randint(0, 1000), ), stk.SimilarBuildingBlock( building_blocks=fluoros, is_replaceable=is_fluoro, random_seed=generator.randint(0, 1000), ), stk.RandomBuildingBlock( building_blocks=bromos, is_replaceable=is_bromo, random_seed=generator.randint(0, 1000), ), stk.SimilarBuildingBlock( building_blocks=bromos, is_replaceable=is_bromo, random_seed=generator.randint(0, 1000), ), ), random_seed=generator.randint(0, 1000), ), crosser=stk.GeneticRecombination(get_gene=get_functional_group_type, ), generation_selector=generation_selector, mutation_selector=mutation_selector, crossover_selector=crossover_selector, fitness_normalizer=fitness_normalizer, ) logger.info('Starting EA.') generations = [] for generation in ea.get_generations(50): for record in generation.get_molecule_records(): db.put(record.get_molecule()) generations.append(generation) # Write the final population. for i, record in enumerate(generation.get_molecule_records()): write(record.get_molecule(), f'final_{i}.mol') logger.info('Making fitness plot.') # Normalize the fitness values across the entire EA before # plotting the fitness values. generations = tuple( normalize_generations( fitness_calculator=fitness_calculator, fitness_normalizer=fitness_normalizer, generations=generations, )) fitness_progress = stk.ProgressPlotter( generations=generations, get_property=lambda record: record.get_fitness_value(), y_label='Fitness Value', ) fitness_progress.write('fitness_progress.png') fitness_progress.get_plot_data().to_csv('fitness_progress.csv') logger.info('Making rotatable bonds plot.') rotatable_bonds_progress = stk.ProgressPlotter( generations=generations, get_property=lambda record: get_num_rotatable_bonds(record. get_molecule()), y_label='Number of Rotatable Bonds', ) rotatable_bonds_progress.write('rotatable_bonds_progress.png')
import pytest import stk from ..case_data import CaseData topology_graph = stk.polymer.Linear( building_blocks=(stk.BuildingBlock('BrCCBr', [stk.BromoFactory()]), ), repeating_unit='A', num_repeating_units=2, ) @pytest.fixture( params=(CaseData( fitness_normalizer=stk.NullFitnessNormalizer(), population=( stk.MoleculeRecord( topology_graph=topology_graph, ).with_fitness_value(1), stk.MoleculeRecord( topology_graph=topology_graph, ).with_fitness_value(2), stk.MoleculeRecord( topology_graph=topology_graph, ).with_fitness_value(3), ), normalized=( stk.MoleculeRecord( topology_graph=topology_graph, ).with_fitness_value(1), stk.MoleculeRecord( topology_graph=topology_graph, ).with_fitness_value(2), stk.MoleculeRecord( topology_graph=topology_graph, ).with_fitness_value(3), ),
def get_case_data_2() -> CaseData: bb1 = stk.BuildingBlock('[C+2][N+]Br', [stk.BromoFactory()]) canonical_bb1 = bb1.with_canonical_atom_ordering() bb2 = stk.BuildingBlock('IS[O+]', [stk.IodoFactory()]) canonical_bb2 = bb2.with_canonical_atom_ordering() return CaseData( molecule=stk.ConstructedMolecule( topology_graph=stk.polymer.Linear( building_blocks=(bb1, bb2), repeating_unit='AB', num_repeating_units=1, ), ), result=stk.ConstructedMolecule.init( atoms=( stk.C(0, 2), stk.O(1, 1), stk.N(2, 1), stk.S(3), ), bonds=( stk.Bond(stk.C(0, 2), stk.N(2, 1), 1), stk.Bond(stk.O(1, 1), stk.S(3), 1), stk.Bond(stk.N(2, 1), stk.S(3), 1), ), position_matrix=np.array([]), atom_infos=( stk.AtomInfo( atom=stk.C(0, 2), building_block_atom=stk.C(0, 2), building_block=canonical_bb1, building_block_id=0, ), stk.AtomInfo( atom=stk.O(1, 1), building_block_atom=stk.O(0, 1), building_block=canonical_bb2, building_block_id=1, ), stk.AtomInfo( atom=stk.N(2, 1), building_block_atom=stk.N(2, 1), building_block=canonical_bb1, building_block_id=0, ), stk.AtomInfo( atom=stk.S(3), building_block_atom=stk.S(2), building_block=canonical_bb2, building_block_id=1, ), ), bond_infos=( stk.BondInfo( bond=stk.Bond(stk.C(0, 2), stk.N(2, 1), 1), building_block=canonical_bb1, building_block_id=0, ), stk.BondInfo( bond=stk.Bond(stk.O(1, 1), stk.S(3), 1), building_block=canonical_bb2, building_block_id=1, ), stk.BondInfo( bond=stk.Bond(stk.N(2, 1), stk.S(3), 1), building_block=None, building_block_id=None, ), ), num_building_blocks={ canonical_bb1: 1, canonical_bb2: 1, }, ), )
def main(): parser = argparse.ArgumentParser() parser.add_argument( '--mongodb_uri', help='The MongoDB URI for the database to connect to.', default='mongodb://localhost:27017/', ) args = parser.parse_args() logging.basicConfig(level=logging.INFO) # Use a random seed to get reproducible results. random_seed = 4 generator = np.random.RandomState(random_seed) logger.info('Making building blocks.') # Load the building block databases. fluoros = tuple( get_building_blocks( path=pathlib.Path(__file__).parent / 'fluoros.txt', functional_group_factory=stk.FluoroFactory(), )) bromos = tuple( get_building_blocks( path=pathlib.Path(__file__).parent / 'bromos.txt', functional_group_factory=stk.BromoFactory(), )) initial_population = tuple(get_initial_population(fluoros, bromos)) # Write the initial population. for i, record in enumerate(initial_population): write(record.get_molecule(), f'initial_{i}.mol') client = pymongo.MongoClient(args.mongodb_uri) db = stk.ConstructedMoleculeMongoDb(client) ea = stk.EvolutionaryAlgorithm( initial_population=initial_population, fitness_calculator=stk.FitnessFunction(get_fitness_value), mutator=stk.RandomMutator( mutators=( stk.RandomBuildingBlock( building_blocks=fluoros, is_replaceable=is_fluoro, random_seed=generator.randint(0, 1000), ), stk.SimilarBuildingBlock( building_blocks=fluoros, is_replaceable=is_fluoro, random_seed=generator.randint(0, 1000), ), stk.RandomBuildingBlock( building_blocks=bromos, is_replaceable=is_bromo, random_seed=generator.randint(0, 1000), ), stk.SimilarBuildingBlock( building_blocks=bromos, is_replaceable=is_bromo, random_seed=generator.randint(0, 1000), ), ), random_seed=generator.randint(0, 1000), ), crosser=stk.GeneticRecombination(get_gene=get_functional_group_type, ), generation_selector=stk.Best( num_batches=25, duplicate_molecules=False, ), mutation_selector=stk.Roulette( num_batches=5, random_seed=generator.randint(0, 1000), ), crossover_selector=stk.Roulette( num_batches=3, batch_size=2, random_seed=generator.randint(0, 1000), ), ) logger.info('Starting EA.') generations = [] for generation in ea.get_generations(50): for record in generation.get_molecule_records(): db.put(record.get_molecule()) generations.append(generation) # Write the final population. for i, record in enumerate(generation.get_molecule_records()): write(record.get_molecule(), f'final_{i}.mol') logger.info('Making fitness plot.') fitness_progress = stk.ProgressPlotter( generations=generations, get_property=lambda record: record.get_fitness_value(), y_label='Fitness Value', ) fitness_progress.write('fitness_progress.png') logger.info('Making rotatable bonds plot.') rotatable_bonds_progress = stk.ProgressPlotter( generations=generations, get_property=get_num_rotatable_bonds, y_label='Number of Rotatable Bonds', ) rotatable_bonds_progress.write('rotatable_bonds_progress.png')
def test_update_1(mongo_client): """ Test that existing entries are updated. """ database_name = '_test_update_1' mongo_client.drop_database(database_name) database = stk.ConstructedMoleculeMongoDb( mongo_client=mongo_client, database=database_name, put_lru_cache_size=0, get_lru_cache_size=0, ) jsonizer = stk.ConstructedMoleculeJsonizer() molecule = stk.BuildingBlock( smiles='BrCCBr', functional_groups=[stk.BromoFactory()], ).with_canonical_atom_ordering() polymer = stk.ConstructedMolecule( topology_graph=stk.polymer.Linear( # Use it as a building block twice, to make sure it is # not repeatedly added to the molecules database. building_blocks=(molecule, molecule), repeating_unit='AB', num_repeating_units=2, ), ).with_canonical_atom_ordering() json = jsonizer.to_json(polymer) database.put(polymer) assert_database_state( state1=get_database_state(database), state2=DatabaseState({ DatabaseEntry(**json['molecule']): 1, DatabaseEntry(**to_hashable_matrix(json['matrix'])): 1, DatabaseEntry(**json['buildingBlocks'][0]['molecule']): 1, DatabaseEntry(**to_hashable_matrix(json=json['buildingBlocks'][0]['matrix'], )): 1, DatabaseEntry(**to_hashable_constructed_molecule(json=json['constructedMolecule'], )): 1, }), ) polymer2 = polymer.with_position_matrix(position_matrix=np.zeros( (polymer.get_num_atoms(), 3)), ) json2 = jsonizer.to_json(polymer2) database.put(polymer2) assert_database_state( state1=get_database_state(database), state2=DatabaseState({ DatabaseEntry(**json['molecule']): 1, DatabaseEntry(**to_hashable_matrix(json2['matrix'])): 1, DatabaseEntry(**json['buildingBlocks'][0]['molecule']): 1, DatabaseEntry(**to_hashable_matrix(json=json['buildingBlocks'][0]['matrix'], )): 1, DatabaseEntry(**to_hashable_constructed_molecule(json=json['constructedMolecule'], )): 1, }), )
def test_update_3(mongo_client): """ Test that existing entries are updated. In this test, your first create one entry with two keys. Then update the entry with databases, each using 1 different key. No duplicate entries should be made in the database this way. """ database_name = '_test_update_3' mongo_client.drop_database(database_name) jsonizer1 = stk.ConstructedMoleculeJsonizer(key_makers=( stk.InchiKey(), stk.Smiles(), ), ) database1 = stk.ConstructedMoleculeMongoDb( mongo_client=mongo_client, database=database_name, put_lru_cache_size=0, get_lru_cache_size=0, jsonizer=jsonizer1, ) jsonizer2 = stk.ConstructedMoleculeJsonizer( key_makers=(stk.InchiKey(), ), ) database2 = stk.ConstructedMoleculeMongoDb( mongo_client=mongo_client, database=database_name, put_lru_cache_size=0, get_lru_cache_size=0, jsonizer=jsonizer2, ) jsonizer3 = stk.ConstructedMoleculeJsonizer(key_makers=(stk.Smiles(), ), ) database3 = stk.ConstructedMoleculeMongoDb( mongo_client=mongo_client, database=database_name, put_lru_cache_size=0, get_lru_cache_size=0, jsonizer=jsonizer3, ) molecule = stk.BuildingBlock( smiles='BrCCCBr', functional_groups=[stk.BromoFactory()], ).with_canonical_atom_ordering() polymer1 = stk.ConstructedMolecule( topology_graph=stk.polymer.Linear( # Use it as a building block twice, to make sure it is # not repeatedly added to the molecules database. building_blocks=(molecule, molecule), repeating_unit='AB', num_repeating_units=2, ), ).with_canonical_atom_ordering() json1 = jsonizer1.to_json(polymer1) database1.put(polymer1) assert_database_state( state1=get_database_state(database1), state2=DatabaseState({ DatabaseEntry(**json1['molecule']): 1, DatabaseEntry(**to_hashable_matrix(json1['matrix'])): 1, DatabaseEntry(**json1['buildingBlocks'][0]['molecule']): 1, DatabaseEntry(**to_hashable_matrix(json=json1['buildingBlocks'][0]['matrix'], )): 1, DatabaseEntry(**to_hashable_constructed_molecule(json=json1['constructedMolecule'], )): 1, }), ) # Should update the entry. polymer2 = polymer1.with_position_matrix(position_matrix=np.zeros( (polymer1.get_num_atoms(), 3)), ) json2 = jsonizer2.to_json(polymer2) json2['matrix'] = dict(json1['matrix']) json2['matrix']['m'] = jsonizer2.to_json(polymer2)['matrix']['m'] database2.put(polymer2) assert_database_state( state1=get_database_state(database1), state2=DatabaseState({ DatabaseEntry(**json1['molecule']): 1, DatabaseEntry(**to_hashable_matrix(json2['matrix'])): 1, DatabaseEntry(**json1['buildingBlocks'][0]['molecule']): 1, DatabaseEntry(**to_hashable_matrix(json=json1['buildingBlocks'][0]['matrix'], )): 1, DatabaseEntry(**to_hashable_constructed_molecule(json=json1['constructedMolecule'], )): 1, }), ) # Should also update the entry. polymer3 = polymer1.with_position_matrix(position_matrix=np.zeros( (polymer1.get_num_atoms(), 3)), ) json3 = jsonizer3.to_json(polymer3) json3['matrix'] = dict(json1['matrix']) json3['matrix']['m'] = jsonizer3.to_json(polymer3)['matrix']['m'] database3.put(polymer3) assert_database_state( state1=get_database_state(database1), state2=DatabaseState({ DatabaseEntry(**json1['molecule']): 1, DatabaseEntry(**to_hashable_matrix(json3['matrix'])): 1, DatabaseEntry(**json1['buildingBlocks'][0]['molecule']): 1, DatabaseEntry(**to_hashable_matrix(json=json1['buildingBlocks'][0]['matrix'], )): 1, DatabaseEntry(**to_hashable_constructed_molecule(json=json1['constructedMolecule'], )): 1, }), )
def add_mixed_entries( client, database, key_makers, ): constructed_molecule_db = stk.ConstructedMoleculeMongoDb( mongo_client=client, database=database, molecule_collection='molecules', position_matrix_collection='position_matrices', jsonizer=stk.ConstructedMoleculeJsonizer(key_makers=key_makers, ), ) num_atoms_db = stk.ValueMongoDb( mongo_client=client, collection='numAtoms', database=database, key_makers=key_makers, ) num_bonds_db = stk.ValueMongoDb( mongo_client=client, collection='numBonds', database=database, key_makers=key_makers, ) cage1 = stk.ConstructedMolecule(topology_graph=stk.cage.FourPlusSix( building_blocks=( stk.BuildingBlock( smiles='BrC1C(Br)CCCC1', functional_groups=[stk.BromoFactory()], ), stk.BuildingBlock( smiles='Brc1cc(Br)cc(Br)c1', functional_groups=[stk.BromoFactory()], ), ), ), ) constructed_molecule_db.put(cage1) num_atoms_db.put(cage1, cage1.get_num_atoms()) cage2 = stk.ConstructedMolecule(topology_graph=stk.cage.TwentyPlusThirty( building_blocks=( stk.BuildingBlock( smiles='BrC1C(Br)CCCC1', functional_groups=[stk.BromoFactory()], ), stk.BuildingBlock( smiles='Brc1cc(Br)cc(Br)c1', functional_groups=[stk.BromoFactory()], ), ), ), ) constructed_molecule_db.put(cage2) num_atoms_db.put(cage2, cage2.get_num_atoms()) macrocycle = stk.ConstructedMolecule( topology_graph=stk.macrocycle.Macrocycle( building_blocks=( stk.BuildingBlock( smiles='BrCCBr', functional_groups=[stk.BromoFactory()], ), stk.BuildingBlock( smiles='BrNNBr', functional_groups=[stk.BromoFactory()], ), stk.BuildingBlock( smiles='BrOOBr', functional_groups=[stk.BromoFactory()], ), ), repeating_unit='ABC', num_repeating_units=2, ), ) num_atoms_db.put(macrocycle, macrocycle.get_num_atoms()) polymer = stk.ConstructedMolecule(topology_graph=stk.polymer.Linear( building_blocks=( stk.BuildingBlock( smiles='BrCCBr', functional_groups=[stk.BromoFactory()], ), stk.BuildingBlock( smiles='BrNNBr', functional_groups=[stk.BromoFactory()], ), ), repeating_unit='AB', num_repeating_units=4, ), ) rotaxane = stk.ConstructedMolecule(topology_graph=stk.rotaxane.NRotaxane( axle=stk.BuildingBlock.init_from_molecule(polymer), cycles=(stk.BuildingBlock.init_from_molecule(macrocycle), ), repeating_unit='A', num_repeating_units=1, ), ) constructed_molecule_db.put(polymer) constructed_molecule_db.put(macrocycle) constructed_molecule_db.put(rotaxane) num_bonds_db.put(rotaxane, rotaxane.get_num_bonds())
import pytest import stk import numpy as np @pytest.fixture( scope='session', params=( lambda: stk.BuildingBlock('NCCN'), lambda: stk.BuildingBlock( smiles='Brc1ccc(Br)cc1Br', functional_groups=[stk.BromoFactory()], ), lambda: stk.ConstructedMolecule(topology_graph=stk.polymer.Linear( building_blocks=( stk.BuildingBlock('BrCCBr', [stk.BromoFactory()]), stk.BuildingBlock( smiles='BrCNCCBr', functional_groups=[stk.BromoFactory()], ), ), repeating_unit='AB', num_repeating_units=2, ), ), )) def molecule(request) -> stk.Molecule: """ A :class:`.Molecule` instance with at least 2 atoms. """
def main(): username = input('Username: '******'mongodb+srv://{username}:{password}@stk-vis-example.x4bkl.' 'mongodb.net/stk?retryWrites=true&w=majority') database = 'stk' client.drop_database(database) constructed_db = stk.ConstructedMoleculeMongoDb(client, database) atoms_db = stk.ValueMongoDb(client, 'Num Atoms') bonds_db = stk.ValueMongoDb(client, 'Num Bonds') energy_db = stk.ValueMongoDb(client, 'UFF Energy') macrocycle = uff( stk.ConstructedMolecule(topology_graph=stk.macrocycle.Macrocycle( building_blocks=( stk.BuildingBlock( smiles='BrCCBr', functional_groups=[stk.BromoFactory()], ), stk.BuildingBlock( smiles='BrNNBr', functional_groups=[stk.BromoFactory()], ), stk.BuildingBlock( smiles='BrOOBr', functional_groups=[stk.BromoFactory()], ), ), repeating_unit='ABC', num_repeating_units=2, ), )) atoms_db.put(macrocycle, macrocycle.get_num_atoms()) bonds_db.put(macrocycle, macrocycle.get_num_bonds()) energy_db.put(macrocycle, uff_energy(macrocycle)) constructed_db.put(macrocycle) polymer = uff( stk.ConstructedMolecule(topology_graph=stk.polymer.Linear( building_blocks=( stk.BuildingBlock( smiles='BrCCBr', functional_groups=[stk.BromoFactory()], ), stk.BuildingBlock( smiles='BrNNBr', functional_groups=[stk.BromoFactory()], ), ), repeating_unit='AB', num_repeating_units=4, ), )) atoms_db.put(polymer, polymer.get_num_atoms()) bonds_db.put(polymer, polymer.get_num_bonds()) energy_db.put(polymer, uff_energy(polymer)) constructed_db.put(polymer) rotaxane = uff( stk.ConstructedMolecule(topology_graph=stk.rotaxane.NRotaxane( axle=stk.BuildingBlock.init_from_molecule(polymer), cycles=(stk.BuildingBlock( smiles=('C1=CC2=CC3=CC=C(N3)C=C4C=CC(=N4)' 'C=C5C=CC(=N5)C=C1N2'), ), ), repeating_unit='A', num_repeating_units=1, ), )) atoms_db.put(rotaxane, rotaxane.get_num_atoms()) bonds_db.put(rotaxane, rotaxane.get_num_bonds()) energy_db.put(rotaxane, uff_energy(rotaxane)) constructed_db.put(rotaxane) kagome = uff( stk.ConstructedMolecule(topology_graph=stk.cof.Honeycomb( building_blocks=( stk.BuildingBlock('BrC=CBr', [stk.BromoFactory()]), stk.BuildingBlock( smiles='Brc1cc(Br)cc(Br)c1', functional_groups=[stk.BromoFactory()], ), ), lattice_size=(2, 2, 1)), )) atoms_db.put(kagome, kagome.get_num_atoms()) bonds_db.put(kagome, kagome.get_num_bonds()) energy_db.put(kagome, uff_energy(kagome)) constructed_db.put(kagome) cc3 = stk.ConstructedMolecule(topology_graph=stk.cage.FourPlusSix( building_blocks=( stk.BuildingBlock( smiles='NC1CCCCC1N', functional_groups=[stk.PrimaryAminoFactory()], ), stk.BuildingBlock( smiles='O=Cc1cc(C=O)cc(C=O)c1', functional_groups=[stk.AldehydeFactory()], ), ), ), ) cc3 = uff(cc3) atoms_db.put(cc3, cc3.get_num_atoms()) bonds_db.put(cc3, cc3.get_num_bonds()) energy_db.put(cc3, uff_energy(cc3)) constructed_db.put(cc3)
stk.Fluoro( fluorine=stk.F(3), atom=stk.C(2), bonders=(stk.C(2), ), deleters=(stk.F(3), ), ), stk.Fluoro( fluorine=stk.F(6), atom=stk.C(5), bonders=(stk.C(5), ), deleters=(stk.F(6), ), ), ), ), CaseData( factory=stk.BromoFactory(), molecule=stk.BuildingBlock('BrCC(Br)CCBr'), functional_groups=( stk.Bromo( bromine=stk.Br(0), atom=stk.C(1), bonders=(stk.C(1), ), deleters=(stk.Br(0), ), ), stk.Bromo( bromine=stk.Br(3), atom=stk.C(2), bonders=(stk.C(2), ), deleters=(stk.Br(3), ), ), stk.Bromo(
import pytest import stk from ....case_data import CaseData metal_atom = stk.BuildingBlock( smiles='[Fe+2]', functional_groups=(stk.SingleAtom(stk.Fe(0, charge=2)) for i in range(6)), position_matrix=([0, 0, 0], ), ) tritopic_linker = stk.BuildingBlock( smiles=('[H]C1=C([H])C(N(C2=C([H])C([H])=C(Br)C([H])=C2[H])C2=C([H])C(' '[H])=C(Br)C([H])=C2[H])=C([H])C([H])=C1Br'), functional_groups=[stk.BromoFactory()]) complex_ligand = stk.BuildingBlock( smiles='[H]C1=NC(C([H])=NBr)=C([H])C([H])=C1[H]', functional_groups=[ stk.SmartsFunctionalGroupFactory( smarts='[#6]~[#7X2]~[#35]', bonders=(1, ), deleters=(), ), stk.SmartsFunctionalGroupFactory( smarts='[#6]~[#7X2]~[#6]', bonders=(1, ), deleters=(), ), ]) iron_complex = stk.ConstructedMolecule( stk.metal_complex.OctahedralDelta( metals={metal_atom: 0},
def get_topology_graph(num_repeating_units): return stk.polymer.Linear( building_blocks=(stk.BuildingBlock('BrCCBr', [stk.BromoFactory()]), ), repeating_unit='A', num_repeating_units=num_repeating_units, )
import pytest import numpy as np import stk from .case_data import CaseData bb1 = stk.BuildingBlock('[C+2][N+]Br', [stk.BromoFactory()]) canonical_bb1 = bb1.with_canonical_atom_ordering() bb2 = stk.BuildingBlock('IS[O+]', [stk.IodoFactory()]) canonical_bb2 = bb2.with_canonical_atom_ordering() @pytest.fixture( params=( CaseData(molecule=stk.BuildingBlock( smiles='Br[C+2][N+]Cl', functional_groups=[stk.BromoFactory()], placer_ids=(0, ), ), result=stk.BuildingBlock.init( atoms=( stk.Cl(0), stk.Br(1), stk.C(2, 2), stk.N(3, 1), ), bonds=( stk.Bond(stk.Cl(0), stk.N(3, 1), 1), stk.Bond(stk.Br(1), stk.C(2, 2), 1), stk.Bond(stk.C(2, 2), stk.N(3, 1), 1),
def test_get_all(mongo_client): """ Test iteration over all molecules. """ database_name = '_test_get_entries_constructed_molecule' mongo_client.drop_database(database_name) key_maker = stk.Inchi() jsonizer = stk.ConstructedMoleculeJsonizer( key_makers=(key_maker, ) ) database = stk.ConstructedMoleculeMongoDb( mongo_client=mongo_client, database=database_name, jsonizer=jsonizer, put_lru_cache_size=0, get_lru_cache_size=0, ) molecules = [ stk.ConstructedMolecule( topology_graph=stk.polymer.Linear( building_blocks=(stk.BuildingBlock( smiles='BrCCCBr', functional_groups=[stk.BromoFactory()]), ), repeating_unit='A', num_repeating_units=3, ), ), stk.ConstructedMolecule( topology_graph=stk.polymer.Linear( building_blocks=( stk.BuildingBlock( smiles='BrCCBr', functional_groups=[stk.BromoFactory()] ), stk.BuildingBlock( smiles='BrCNCBr', functional_groups=[stk.BromoFactory()] ), ), repeating_unit='AB', num_repeating_units=2, ), ), ] molecules_by_key = { key_maker.get_key(molecule): molecule for molecule in molecules } for molecule in molecules: database.put(molecule) for i, retrieved in enumerate(database.get_all()): key = key_maker.get_key(retrieved) molecule = molecules_by_key[key] is_equivalent_constructed_molecule( molecule.with_canonical_atom_ordering(), retrieved.with_canonical_atom_ordering(), ) # Check number of molecules. assert i+1 == len(molecules)
@pytest.fixture def building_block(case_data): """ A :class:`.BuildingBlock` instance. """ return case_data.building_block @pytest.fixture( params=( lambda molecule: stk.BromoFactory().get_functional_groups(molecule), lambda molecule: stk.PrimaryAminoFactory().get_functional_groups(molecule), lambda molecule: it.chain( stk.PrimaryAminoFactory().get_functional_groups(molecule), stk.BromoFactory().get_functional_groups(molecule)), ) ) def get_functional_groups(request): """ Yield the functional groups of a `molecule`. Parameters ---------- molecule : :class:`.Molecule` The molecule whose functional groups should be gotten.
import pytest import stk from ....case_data import CaseData @pytest.fixture( scope='session', params=(lambda name: CaseData( molecule=stk.ConstructedMolecule( topology_graph=stk.cage.EightPlusSixteen(building_blocks=( stk.BuildingBlock( smiles='BrC1=C(Br)[C+]=N1', functional_groups=[stk.BromoFactory()], ), stk.BuildingBlock( smiles=('Br[C+]1C2(Br)[C+]=N[C+]2[C+](Br)[C+](' 'Br)[C+2]1'), functional_groups=[stk.BromoFactory()], ), ), ), ), smiles=('[C+]1=NC2=C1[C+]1[C+2][C+]3C4=C(N=[C+]4)[C+]4[C+2][C+' ']5C6=C([C+]=N6)[C+]6[C+2][C+]7C8=C([C+]=N8)C38[C+]=N[' 'C+]8[C+]1C1=C([C+]=N1)C13[C+]=N[C+]1[C+]1C8=C([C+]=N8' ')C89[C+]=N[C+]8[C+]8C%10=C([C+]=N%10)[C+]7[C+]7N=[C+]' 'C67C6=C(N=[C+]6)C67[C+]=N[C+]6[C+]6C%10=C([C+]=N%10)[' 'C+]5[C+]5N=[C+]C45C4=C([C+]=N4)[C+]4[C+]2[C+2][C+]2C5' '=C([C+]=N5)[C+]3[C+2][C+]1C1=C(N=[C+]1)[C+]1[C+2][C+]' '(C3=C(N=[C+]3)C23[C+]=N[C+]43)[C+]2C3=C([C+]=N3)[C+]6' '[C+2][C+]7C3=C([C+]=N3)[C+]8[C+2][C+]9C3=C(N=[C+]3)C1'
def main(): examples_output = 'splitter_output_directory' if not os.path.exists(examples_output): os.mkdir(examples_output) full_mol = stk.BuildingBlock('C1=CC=NC(=C1)C=NC2=CC=C(C=C2)Br') print(full_mol) full_mol.write(os.path.join(examples_output, 'original.mol')) splitter = stko.MoleculeSplitter( # This smarts corresponds to an imine bond: `CNCC`. breaker_smarts='[#6X3]~[#7X2]~[#6X3H1]~[#6X3!H1]', bond_deleter_ids=(0, 1), ) split_mols = splitter.split(full_mol) print(split_mols) for i, mol in enumerate(split_mols): print(mol) mol.write(os.path.join(examples_output, f'splits_{i}.xyz')) # Test it worked. assert len(split_mols) == 2 # Transform molecule. transformer = stko.MoleculeTransformer( replacer_smarts='[Br]', functional_groups=(stk.BromoFactory(), ), ) transformed_mols = tuple( transformer.transform(i) for i in split_mols ) count = 0 for i, mol in enumerate(transformed_mols): print(mol) mol.write(os.path.join(examples_output, f'transform_{i}.mol')) count += 1 # Test it worked. assert count == 2 # Reconstruct. polymer = stk.ConstructedMolecule( topology_graph=stk.polymer.Linear( building_blocks=(transformed_mols[0], transformed_mols[1]), repeating_unit='AB', num_repeating_units=1, ), ) polymer.write(os.path.join(examples_output, 'reconstructed.mol')) # Another more complex example. full_mol2 = stk.BuildingBlock( smiles=( 'C1=CC=C(C=C1)C#CC2=CC(=C(C=C2C#CC3=CC=CC=C3)C#CC4=CC=CC=' 'C4)C#CC5=CC=CC=C5' ), ) print(full_mol2) full_mol2.write(os.path.join(examples_output, 'original2.mol')) splitter = stko.MoleculeSplitter( breaker_smarts='[#6]~[#6]#[#6]', bond_deleter_ids=(0, 1), ) split_mols2 = splitter.split(full_mol2) for i, mol in enumerate(split_mols2): print(mol) mol.write(os.path.join(examples_output, f'splits2_{i}.xyz')) # Transform molecule. transformer = stko.MoleculeTransformer( replacer_smarts='[Br]', functional_groups=(stk.BromoFactory(), ), ) transformed_mols2 = tuple( transformer.transform(i) for i in split_mols2 ) for i, mol in enumerate(transformed_mols2): print(mol) mol.write(os.path.join(examples_output, f'transform2_{i}.mol'))