def main(): bb1 = stk.BuildingBlock('NCCN', [stk.PrimaryAminoFactory()]) bb2 = stk.BuildingBlock( smiles='O=CC(C=O)C=O', functional_groups=[stk.AldehydeFactory()], ) cage1 = stk.ConstructedMolecule(topology_graph=stk.cage.FourPlusSix( (bb1, bb2)), ) broken_bonds_by_id = [] disconnectors = [] for bi in cage1.get_bond_infos(): if bi.get_building_block() is None: a1id = bi.get_bond().get_atom1().get_id() a2id = bi.get_bond().get_atom2().get_id() broken_bonds_by_id.append(sorted((a1id, a2id))) disconnectors.extend((a1id, a2id)) print(broken_bonds_by_id) print(disconnectors) print('--') new_topology_graph = stko.TopologyExtractor() tg_info = new_topology_graph.extract_topology( molecule=cage1, broken_bonds_by_id=broken_bonds_by_id, disconnectors=set(disconnectors), ) print(tg_info.get_vertex_positions()) print(tg_info.get_connectivities()) print(tg_info.get_edge_pairs()) cage1.write(os.path.join('output_directory', 'tg_cage.mol')) tg_info.write(os.path.join('output_directory', 'tg_info.pdb'))
def main(): bb1 = stk.BuildingBlock('NCCNCCN', [stk.PrimaryAminoFactory()]) bb2 = stk.BuildingBlock('O=CCCC=O', [stk.AldehydeFactory()]) polymer = stk.ConstructedMolecule( stk.polymer.Linear(building_blocks=(bb1, bb2), repeating_unit="AB", orientations=[0, 0], num_repeating_units=1)) # Run calculations for bb. bb1.write('output_directory/tors_test_bb1.mol') tors_calculator = stko.TorsionCalculator() tors_results = tors_calculator.get_results(bb1) print(tors_results.get_molecule()) for t, ang in tors_results.get_torsion_angles(): print(t, ang, t.get_atom_ids()) # Run calculations for constructed molecule. polymer.write('output_directory/tors_test_polymer.mol') tors_calculator = stko.ConstructedMoleculeTorsionCalculator() tors_results = tors_calculator.get_results(polymer) print(tors_results.get_molecule()) for t, ang in tors_results.get_torsion_angles(): print(t, ang, t.get_atom_ids()) for t in tors_results.get_torsion_infos(): print( 'c', t.get_torsion(), t.get_building_block(), t.get_building_block_id(), t.get_building_block_torsion(), ) print(tors_results.get_torsion_infos_by_building_block()) for t in tors_results.get_torsion_infos(): print(get_torsion_info_angles(polymer, t))
def main(): bb1 = stk.BuildingBlock('NCCNCCN', [stk.PrimaryAminoFactory()]) bb2 = stk.BuildingBlock('O=CCCC=O', [stk.AldehydeFactory()]) polymer = stk.ConstructedMolecule( stk.polymer.Linear(building_blocks=(bb1, bb2), repeating_unit="AB", orientations=[0, 0], num_repeating_units=1)) examples_output = 'output_directory' if os.path.exists(examples_output): shutil.rmtree(examples_output) os.mkdir(examples_output) # Run optimisations. uff = stko.UFF() polymer = uff.optimize(polymer) polymer.write(f'{examples_output}/poly_uff.mol') mmff = stko.MMFF() polymer = mmff.optimize(polymer) polymer.write(f'{examples_output}/poly_mmff.mol') etkdg = stko.ETKDG() polymer = etkdg.optimize(polymer) polymer.write(f'{examples_output}/poly_etkdg.mol')
def main(): if len(sys.argv) > 1: orca_path = sys.argv[1] else: print('usage: orca_example.py orca_path') sys.exit() bb1 = stk.BuildingBlock('NCCN', [stk.PrimaryAminoFactory()]) bb2 = stk.BuildingBlock('O=CC=O', [stk.AldehydeFactory()]) polymer = stk.ConstructedMolecule( stk.polymer.Linear(building_blocks=(bb1, bb2), repeating_unit="AB", orientations=(0, 0), num_repeating_units=1)) examples_output = 'orca_output_directory' if not os.path.exists(examples_output): os.mkdir(examples_output) # Run optimisations. etkdg = stko.ETKDG() polymer = etkdg.optimize(polymer) orca_ey_1 = stko.OrcaEnergy( orca_path=orca_path, topline='! SP B97-3c', basename='example1', output_dir=f'{examples_output}/orca_e1_dir', ) print(orca_ey_1.get_energy(polymer)) uff = stko.UFF() polymer = uff.optimize(polymer) orca_ey_2 = stko.OrcaEnergy( orca_path=orca_path, topline='! SP B97-3c', basename='example2', output_dir=f'{examples_output}/orca_e2_dir', ) print(orca_ey_2.get_energy(polymer)) orca_ey_3 = stko.OrcaEnergy( orca_path=orca_path, topline='! SP B97-3c', basename='example3', output_dir=f'{examples_output}/orca_e3_dir', write_input_only=True, ) orca_ey_3.get_results(polymer)
def main(): examples_output = 'aligner_directory' if not os.path.exists(examples_output): os.mkdir(examples_output) bb1 = stk.BuildingBlock('NCCN', [stk.PrimaryAminoFactory()]) bb2 = stk.BuildingBlock( smiles='O=CC(C=O)C=O', functional_groups=[stk.AldehydeFactory()], ) cage = stk.ConstructedMolecule( topology_graph=stk.cage.FourPlusSix( (bb1, bb2), optimizer=stk.MCHammer(num_steps=2000), ), ) mol_list = [ (stk.BuildingBlock('NCCNCCN'), (('N', 'N'), ), True), ( stk.BuildingBlock('CN1C=NC2=C1C(=O)N(C(=O)N2C)C'), (('N', 'N'), ('O', 'O'), ), True, ), (stk.BuildingBlock('C1=CN=CN=C1'), (('N', 'N'), ), True), (stk.BuildingBlock('c1ccccc1'), (('C', 'C'), ), True), (stk.BuildingBlock('C1CCCCC1'), (('C', 'C'), ), True), (cage, (('N', 'N'), ), True), ] _opt = stko.UFF() for i, (mol, pairs, uff_opt) in enumerate(mol_list): initial = mol.with_rotation_about_axis( 1.34, np.array((0, 0, 1)), np.array((0, 0, 0)), ) mol.write(os.path.join(examples_output, f'unaligned_{i}.mol')) initial.write(os.path.join(examples_output, f'init_{i}.mol')) aligner = stko.Aligner( initial_molecule=initial, matching_pairs=pairs, ) if uff_opt: mol = aligner.optimize(_opt.optimize(mol)) else: mol = aligner.optimize(mol) mol.write(os.path.join(examples_output, f'aligned_{i}.mol')) rmsd_calculator = stko.RmsdMappedCalculator(initial) rmsd = rmsd_calculator.get_results(mol).get_rmsd() print(f'molecule {i} RMSD: {rmsd}')
def main(): bb1 = stk.BuildingBlock('NCCNCCN', [stk.PrimaryAminoFactory()]) bb2 = stk.BuildingBlock('O=CCCC=O', [stk.AldehydeFactory()]) polymer = stk.ConstructedMolecule( stk.polymer.Linear(building_blocks=(bb1, bb2), repeating_unit="AB", orientations=(0, 0), num_repeating_units=1)) examples_output = 'output_directory' if not os.path.exists(examples_output): os.mkdir(examples_output) print(stko.ZMatrix().get_zmatrix(bb1)) bb1.write(os.path.join(examples_output, 'bb1.mol')) print(stko.ZMatrix().get_zmatrix(bb2)) bb2.write(os.path.join(examples_output, 'bb2.mol')) print(stko.ZMatrix().get_zmatrix(polymer)) polymer.write(os.path.join(examples_output, 'polymer.mol'))
def test_torsion_matching(): """ Confirm torsions are appropriately mapped. Check corresponding torsion angles between building blocks and constructed molecules to confirm that the angle is the same in both. """ bb1 = stk.BuildingBlock('NCCNCCN', [stk.PrimaryAminoFactory()]) bb2 = stk.BuildingBlock('O=CCCC=O', [stk.AldehydeFactory()]) assert _polymer_angles_match(bb1, bb2) # Test case designed such that default rdkit torsion for # constructed molecule spans over both building blocks, requiring # stko to correctly clean this up. bonders3 = (stk.C(1), ) deleters3 = (stk.H(7), ) functional_group3 = stk.GenericFunctionalGroup(atoms=bonders3 + deleters3, bonders=bonders3, deleters=deleters3) bb3 = stk.BuildingBlock('CCCC', [functional_group3]) bonders4 = (stk.C(0), ) deleters4 = (stk.H(1), ) functional_group4 = stk.GenericFunctionalGroup(atoms=bonders4 + deleters4, bonders=bonders4, deleters=deleters4) bb4 = stk.BuildingBlock('C', [functional_group4]) assert _polymer_angles_match(bb3, bb4) # Test case for an exterior atom of a building block torsion being # deleted in construction. bonders5 = (stk.C(1), ) deleters5 = (stk.C(0), stk.H(4), stk.H(5), stk.H(6)) functional_group5 = stk.GenericFunctionalGroup(atoms=bonders5 + deleters5, bonders=bonders5, deleters=deleters5) bb5 = stk.BuildingBlock('CCCC', [functional_group5]) bb6 = bb4 assert not _polymer_angles_match(bb5, bb6)
def main(): bb1 = stk.BuildingBlock('NCCNCCN', [stk.PrimaryAminoFactory()]) bb2 = stk.BuildingBlock('O=CCCC=O', [stk.AldehydeFactory()]) polymer = stk.ConstructedMolecule( stk.polymer.Linear(building_blocks=(bb1, bb2), repeating_unit="AB", orientations=[0, 0], num_repeating_units=1)) # Zipping calculations together. mols = [ # Perfect linear short. stk.BuildingBlock('C#C'), # Kind of linear long. stk.BuildingBlock('C#CC#C'), stk.BuildingBlock('CCCC'), # Flat. stk.BuildingBlock('C1=CC=CC=C1'), # Less flat. stk.BuildingBlock('C1CCCCC1'), # Sphere - CH4. stk.BuildingBlock('C'), # Sphere - adamantane. stk.BuildingBlock('C1C3CC2CC(CC1C2)C3'), # Constructed Molecule. polymer, ] shape_calc = stko.ShapeCalculator() for mol in mols: print( mol, stko.ShapeResults( shape_calc.calculate(mol)).get_spherocity_index(), stko.ShapeResults(shape_calc.calculate(mol)).get_eccentricity(), )
atom2=stk.C(2), bonders=(stk.N(1), ), deleters=(stk.H(10), ), ), stk.SecondaryAmino( nitrogen=stk.N(4), hydrogen=stk.H(15), atom1=stk.C(3), atom2=stk.C(5), bonders=(stk.N(4), ), deleters=(stk.H(15), ), ), ), ), lambda: CaseData( factory=stk.AldehydeFactory(), molecule=stk.BuildingBlock('O=CCC=O'), functional_groups=( stk.Aldehyde( carbon=stk.C(1), oxygen=stk.O(0), hydrogen=stk.H(5), atom=stk.C(2), bonders=(stk.C(1), ), deleters=(stk.O(0), ), ), stk.Aldehyde( carbon=stk.C(3), oxygen=stk.O(4), hydrogen=stk.H(8), atom=stk.C(2),
and su_id != su ] for su_id, bb_id in has_same_bb_id: for i in subunits[su_id]: compound_subunit.add(i) taken_subunits.add(su_id) new_subunits[su] = compound_subunit return new_subunits # Building a cage from the examples on the stk docs. bb1 = stk.BuildingBlock( smiles='O=CC(C=O)C=O', functional_groups=[stk.AldehydeFactory()], ) bb2 = stk.BuildingBlock( smiles='O=CC(Cl)(C=O)C=O', functional_groups=[stk.AldehydeFactory()], ) bb3 = stk.BuildingBlock('NCCN', [stk.PrimaryAminoFactory()]) bb4 = stk.BuildingBlock.init_from_file( 'some_complex.mol', functional_groups=[stk.PrimaryAminoFactory()], ) bb5 = stk.BuildingBlock('NCCCCN', [stk.PrimaryAminoFactory()]) cage = stk.ConstructedMolecule( topology_graph=stk.cage.FourPlusSix( # building_blocks is now a dict, which maps building
def main(): bb1 = stk.BuildingBlock('NCCN', [stk.PrimaryAminoFactory()]) bb2 = stk.BuildingBlock('O=CCC=O', [stk.AldehydeFactory()]) polymer = stk.ConstructedMolecule( stk.polymer.Linear(building_blocks=(bb1, bb2), repeating_unit="AB", orientations=(0, 0), num_repeating_units=1)) bb2 = stk.BuildingBlock('O=CC(C=O)C=O', [stk.AldehydeFactory()]) cage = stk.ConstructedMolecule(topology_graph=stk.cage.FourPlusSix( (bb1, bb2)), ) cage2 = stk.ConstructedMolecule(topology_graph=stk.cage.FourPlusSix( building_blocks=(bb1, bb2), optimizer=stk.MCHammer(), ), ) # Produce a Fe+2 atom with 6 functional groups. iron_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]], ) # Define coordinating ligand with dummy bromine groups and # metal coordinating functional groups. oct_bb = stk.BuildingBlock( smiles='C1=NC(C=NBr)=CC=C1', functional_groups=[ stk.SmartsFunctionalGroupFactory( smarts='[#6]~[#7X2]~[#35]', bonders=(1, ), deleters=(), ), stk.SmartsFunctionalGroupFactory( smarts='[#6]~[#7X2]~[#6]', bonders=(1, ), deleters=(), ), ], ) # Build iron complex with delta stereochemistry. iron_oct_delta = stk.ConstructedMolecule( topology_graph=stk.metal_complex.OctahedralDelta( metals=iron_atom, ligands=oct_bb, optimizer=stk.MCHammer(), ), ) # Assign Bromo functional groups to the metal complex. iron_oct_delta = stk.BuildingBlock.init_from_molecule( molecule=iron_oct_delta, functional_groups=[stk.BromoFactory()], ) # Define spacer building block. bb3 = stk.BuildingBlock( smiles=('C1=CC(C2=CC=C(Br)C=C2)=C' 'C=C1Br'), functional_groups=[stk.BromoFactory()], ) # Build an M4L6 Tetrahedron with a spacer. moc = stk.ConstructedMolecule( topology_graph=stk.cage.M4L6TetrahedronSpacer( building_blocks=( iron_oct_delta, bb3, ), optimizer=stk.MCHammer(), ), ) host = 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()], ), ), optimizer=stk.MCHammer(), ), ) guest1 = stk.host_guest.Guest( building_block=stk.BuildingBlock('BrBr'), displacement=(0., 3., 0.), ) guest2 = stk.host_guest.Guest( building_block=stk.BuildingBlock('C1CCCC1'), ) hg_complex = stk.ConstructedMolecule(topology_graph=stk.host_guest.Complex( host=stk.BuildingBlock.init_from_molecule(host), guests=(guest1, guest2), ), ) cycle = stk.ConstructedMolecule(topology_graph=stk.macrocycle.Macrocycle( building_blocks=(stk.BuildingBlock( smiles='[Br]CC[Br]', functional_groups=[stk.BromoFactory()], ), ), repeating_unit='A', num_repeating_units=8, optimizer=stk.MCHammer(), ), ) axle = stk.ConstructedMolecule(topology_graph=stk.polymer.Linear( building_blocks=( stk.BuildingBlock('BrCCBr', [stk.BromoFactory()]), stk.BuildingBlock('BrCNCBr', [stk.BromoFactory()]), ), repeating_unit='AB', num_repeating_units=3, optimizer=stk.MCHammer(), )) rotaxane = stk.ConstructedMolecule(topology_graph=stk.rotaxane.NRotaxane( axle=stk.BuildingBlock.init_from_molecule(axle), cycles=(stk.BuildingBlock.init_from_molecule(cycle), ), repeating_unit='A', num_repeating_units=1, ), ) examples_output = 'output_directory' if not os.path.exists(examples_output): os.mkdir(examples_output) structures = [ ('bb1', bb1), ('bb2', bb2), ('bb3', bb3), ('polymer', polymer), ('cage', cage), ('cage2', cage2), ('m_iron_oct_delta', iron_oct_delta), ('m_moc', moc), ('hg_complex', hg_complex), ('cycle', cycle), ('axle', axle), ('rotaxane', rotaxane), ] # Run optimisations. for name, struct in structures: if 'm_' in name: rdkit_uff_energy = 'NA' else: rdkit_uff_energy = stko.UFFEnergy( ignore_inter_interactions=False).get_energy(struct) obabel_uff_energy = (stko.OpenBabelEnergy('uff').get_energy(struct)) struct.write(os.path.join(examples_output, f'obabel_{name}_unopt.mol')) opt = stko.OpenBabel( forcefield='uff', repeat_steps=10, sd_steps=20, cg_steps=20, ) opt_struct = opt.optimize(struct) opt_struct.write( os.path.join(examples_output, f'obabel_{name}_opt.mol')) if 'm_' in name: new_rdkit_uff_energy = 'NA' else: new_rdkit_uff_energy = stko.UFFEnergy( ignore_inter_interactions=False).get_energy(opt_struct) new_obabel_uff_energy = ( stko.OpenBabelEnergy('uff').get_energy(opt_struct)) print(f'{name}:\n' f'rdkit: {rdkit_uff_energy}, obabel: {obabel_uff_energy}\n' f'opt rdkit: {new_rdkit_uff_energy}, ' f'opt obabel: {new_obabel_uff_energy}\n')
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)