x, y, z = map(float, (x, y, z)) res.append([path, Atom(sym, coords=(x, y, z))]) # new key for each coord found = False counter = 0 for line in read_file(logfile): if 'NET CHARGES:' in line: found = True if 'RMS DEVIATION' in line: break if found: if re.search(charge_regex, line): res[counter].append(float(line.split()[1])) counter += 1 coords = [atom[1] for atom in res] mol = Molecule(atoms=coords) mol.separate() # bad practice, should use for i, j in zip(mol.coords, res): for atom, r in zip(mol.coords, res): path, _, geodesic_charge = r results.append([ path, atom.index, atom.symbol, geodesic_charge, atom.x, atom.y, atom.z, f"{mol.fragments[atom.mol]['name']}_{atom.mol}" ]) write_csv_from_nested(results, col_names=('Path', 'Index', 'Element', 'Geodesic', 'Rx', 'Ry', 'Rz', 'Fragment'), filename='charges.csv')
#!/usr/bin/env python3 from autochem import Molecule, write_xyz from glob import glob import os import sys from tqdm import tqdm base = os.path.basename(__file__) if len(sys.argv) != 2 or sys.argv[1] == '-h': print( 'Extract all ions of a particular name from all xyz files in the current directory.' ) print( f'i.e. `{base} dhp` pulls out all dhp anions from all xyz files and place them in a subdirectory' ) print(f'Syntax: {base} molecule') sys.exit(1) if not os.path.isdir('extracted'): os.mkdir('extracted') for f in tqdm(glob('*xyz')): mol = Molecule(f) newname = os.path.join(os.getcwd(), 'extracted', f) extracted = [atom for atom in mol.coords if sys.argv[1] in atom.fragment] write_xyz(extracted, newname)
#!/usr/bin/env python3 from autochem import Molecule import sys if len(sys.argv) not in [2, 3] or '-h' in sys.argv: print(f'Syntax: {sys.argv[0]} xyzfile') sys.exit(1) try: output = sys.argv[2] except IndexError: output = 'connected.in' mol = Molecule(sys.argv[1]) bonds = [] for atom in mol.coords: for a in atom.connected_atoms: if f'{a.symbol}{a.index} - {atom.symbol}{atom.index}' not in bonds: bonds.append(f'{atom.symbol}{atom.index} - {a.symbol}{a.index}') for b in bonds: print(b)
from collections import namedtuple info = namedtuple("info", "xyz atom1 mol1 atom2 mol2 dist") def distances(namedtup, molecule): return [ namedtup( molecule.xyz, f"{atom1.symbol}_{atom1.index}", atom1.fragment, f"{atom2.symbol}_{atom2.index}", atom2.fragment, atom1.distance_to(atom2), ) for frag1, frag2 in itertools.combinations( molecule.fragments.values(), 2) for atom1 in frag1["atoms"] for atom2 in frag2["atoms"] ] dists = [] for xyz in glob('*xyz'): mol = Molecule(xyz) dists += distances(info, mol) df = df_from_namedtuples(info, dists) mindists = df.groupby(["xyz", "mol1", "mol2"])["dist"].min().reset_index().sort_values( ["xyz", "mol1", "mol2"]) mindists.to_csv('min_dists_between_frags.csv', index=False)