1
#!/usr/bin/env python
2 60
"""
3
utils.py
4

5
Simple tools for use with the quanformer package
6

7
Version:    Mar 29 2019
8
By:         Victoria T. Lim
9

10
"""
11 60
import openeye.oechem as oechem
12 60
import quanformer.reader as reader
13

14 60
def convert_extension(infile, outfile, canonical=False):
15
    """
16
    Convert one molecule file format into another using OpenEye tools.
17
    The user may also assign canonical smiles as name before writing output.
18

19
    """
20
    # open input file
21 60
    mols = reader.read_mols(infile)
22

23
    # open output file
24 60
    ofs = oechem.oemolostream()
25 60
    if not ofs.open(outfile):
26 0
        oechem.OEThrow.Fatal("Unable to open %s for writing" % outfile)
27

28
    # write to output
29 60
    for mol in mols:
30 60
        if canonical:
31 60
            smi = oechem.OEMolToSmiles(mol)
32 60
        for conf in mol.GetConfs():
33 60
            if canonical:
34 60
                conf.SetTitle(smi)
35 60
            oechem.OEWriteConstMolecule(ofs, conf)
36

37
    # close filestreams
38 60
    ofs.close()
39

40 60
def convert_extension_separate(infile, presuffix, canonical=False, separate='mol'):
41
    """
42
    Convert one molecule file format into another using OpenEye tools.
43
    The user may also assign canonical smiles as name before writing output.
44
    Separate output into (each mol with all confs) or (each conf).
45

46
    presuffix : list
47
        first item contains prefix of output name, last item contains extension
48
        ex. ['alkyl', '.xyz']
49
    separate : string
50
        'mol' or 'conf'
51

52
    """
53
    # open input file
54 0
    mols = reader.read_mols(infile)
55

56
    # write to output
57 0
    for i, mol in enumerate(mols):
58

59
        # open output file
60 0
        if separate=='mol':
61 0
            ofs = oechem.oemolostream()
62 0
            if not ofs.open('{}_{}{}'.format(presuffix[0], str(i), presuffix[1])):
63 0
                oechem.OEThrow.Fatal("Unable to open %s for writing" % outfile)
64 0
        if canonical:
65 0
            smi = oechem.OEMolToSmiles(mol)
66

67 0
        for j, conf in enumerate(mol.GetConfs()):
68
            # open output file
69 0
            if separate=='conf':
70 0
                ofs = oechem.oemolostream()
71 0
                if not ofs.open('{}_{}_{}{}'.format(presuffix[0], str(i), str(j), presuffix[1])):
72 0
                    oechem.OEThrow.Fatal("Unable to open %s for writing" % outfile)
73 0
            if canonical:
74 0
                conf.SetTitle(smi)
75 0
            oechem.OEWriteConstMolecule(ofs, conf)
76

77
    # close filestreams
78 0
    ofs.close()

Read our documentation on viewing source code .

Loading