<commit_before>class Geometry:
    file_store_path = 'QMfiles'
    
    def __init__( uniqueID, rmg_molecule ):
        self.uniqueID = uniqueID
        self.rmg_molecule = rmg_molecule
    
    def generateRDKitGeometries(boundsMatrix=None):
        """
        Use RDKit to guess geometry.
        
        Save mol files of both crude and refined.
        Saves coordinates on atoms.
        """
        rdmol = self.rd_embed(boundsMatrix)
        rdmol = self.rd_optimize(rdmol, boundsMatrix)
        self.save_coordinates(rdmol)

    def rd_embed(boundsMatrix=None):
        "Create rdmol from rmg_molecule"
        "Embed in 3D using rdkit"
        "Save crude geom in mol file, named  uniqueID.crude.mol"
        return rdmol

    def rd_optimize(rdmol, boundsMatrix=None):
        "Optimize using UFF in RDKit"
        "Save optimized geom in mol file, named after uniqueID.refined.mol"
        
        return rdmol
    
    def save_coordinates(rdmol):
        "Save xyz coordinates on each atom in rmg_molecule"


class QM_Molecule:
    def __init__( rmg_molecule ):
        self.rmg_molecule = rmg_molecule
        
    def createGeometry(self):
        """
        Creates self.geometry with RDKit geometries
        """
        uniqueID = self.generateInChiAug()
        self.geometry = Geometry( uniqueID, rmg_molecule )
        self.geometry.generateRDKitGeometries()
        

    def generateThermoData(self, option):
        
        self.createGeometry()
        
        if option.method == 'mopac':
            method = qm.methods.MopacPM3 # for example
        elif option.method == 'gaussian03':
            method = qm.methods.Gaussian03PM3
        else:
            logging.info('Unknown QM Method')
        
        success = False
        for attempt in range(method.max_attempts):
            method.writeInputFile(self.geometry, attempt)
            success = method.runJob()
            method.parseResult()
            if success: break
        else:
            raise Exception("Couldn't generate thermo data.")
        thermoData = method.processResult()
        
        return thermoData
        
    def getInChiAug(self):
        """
        Returns the augmented InChI from self.rmg_molecule 
        """
        return augmented_InChI
        
        <commit_msg>Completed the embedding and optimization methods in Geometry.<commit_after>class Geometry:
    file_store_path = 'QMfiles'
    if not os.path.exists(file_store_path):
        logging.info("Creating directory %s for mol files."%os.path.abspath(file_store_path))
        os.makedirs(file_store_path)
    
    def __init__(self, uniqueID, rmg_molecule ):
        self.uniqueID = uniqueID
        self.rmg_molecule = rmg_molecule
        
    def generateRDKitGeometries(boundsMatrix=None):
        """
        Use RDKit to guess geometry.
        
        Save mol files of both crude and refined.
        Saves coordinates on atoms.
        """
        rdmol, rdAtIdx = self.rd_embed(boundsMatrix)
        rdmol = self.rd_optimize(rdmol, boundsMatrix)
        self.save_coordinates(rdmol, rdAtIdx)

    def rd_embed(boundsMatrix=None):
        """
        Import rmg molecule and create rdkit molecule with the same atom labeling.
        """
        rdAtomIdx = {} # dictionary of rdkit atom indices
        # Initialize a blank Editable molecule and add all the atoms from RMG molecule
        rdMol = AllChem.rdchem.EditableMol(AllChem.rdchem.Mol())
        for index, atom in enumerate(self.molecule.vertices):
            rdAtom = AllChem.rdchem.Atom(atom.element.symbol)
            rdAtom.SetNumRadicalElectrons(atom.radicalElectrons)
            rdMol.AddAtom(rdAtom)
            rdAtomIdx[atom] = index
        
        # Add the bonds
        for atom1 in self.molecule.vertices:
            for atom2, bond in atom1.edges.items():
                index1 = rdAtomIdx[atom1] # atom1.sortingLabel
                index2 = rdAtomIdx[atom2] # atom2.sortingLabel
                if index1 > index2:
                    # Check the RMG bond order and add the appropriate rdkit bond.
                    if bond.order == 'S':
                        rdBond = AllChem.rdchem.BondType.SINGLE
                    elif bond.order == 'D':
                        rdBond = AllChem.rdchem.BondType.DOUBLE
                    elif bond.order == 'T':
                        rdBond = AllChem.rdchem.BondType.TRIPLE
                    elif bond.order == 'B':
                        rdBond = AllChem.rdchem.BondType.AROMATIC
                    else:
                        print "Unknown bond order"
                    rdMol.AddBond(index1, index2, rdBond)
        
        # Make editable mol into a mol and rectify the molecule
        rdMol = rdMol.GetMol()
        Chem.SanitizeMol(rdMol)
        
        AllChem.EmbedMultipleConfs(rdMol, numConfAttempts,randomSeed=1)
        crude = Chem.Mol(rdMol.ToBinary()) 
        
        with open(uniqueID + '.crude.mol', 'w') as out3Dcrude:
            out3Dcrude.write(Chem.MolToMolBlock(crude,confId=minEid))
            
        return rdMol, rdAtIdx
        
    def rd_optimize(rdmol, boundsMatrix=None):
        
        energy=0.0
        minEid=0;
        lowestE=9.999999e99;#start with a very high number, which would never be reached
        
        for i in range(rdmol.GetNumConformers()):
            AllChem.UFFOptimizeMolecule(rdmol,confId=i)
            energy=AllChem.UFFGetMoleculeForceField(rdmol,confId=i).CalcEnergy()
            if energy < lowestE:
                minEid = i
                lowestE = energy
        with open(uniqueID + '.refined.mol', 'w') as out3D:
            out3D.write(Chem.MolToMolBlock(rdmol,confId=minEid))
        
        return rdmol
    
    def save_coordinates(rdmol, rdAtIdx):
        "Save xyz coordinates on each atom in rmg_molecule"


class QM_Molecule:
    def __init__( rmg_molecule ):
        self.rmg_molecule = rmg_molecule
        
    def createGeometry(self):
        """
        Creates self.geometry with RDKit geometries
        """
        uniqueID = self.generateInChiAug()
        self.geometry = Geometry( uniqueID, rmg_molecule )
        self.geometry.generateRDKitGeometries()
        

    def generateThermoData(self, option):
        
        self.createGeometry()
        
        if option.program == 'mopac':
            method = qm.methods.MopacPM3 # for example
        elif option.program == 'gaussian03':
            method = qm.methods.Gaussian03PM3
        else:
            logging.info('Unknown QM Method')
        
        success = False
        for attempt in range(method.max_attempts):
            method.writeInputFile(self.geometry, attempt)
            success = method.runJob()
            method.parseResult()
            if success: break
        else:
            raise Exception("Couldn't generate thermo data.")
        thermoData = method.processResult()
        
        return thermoData
        
    def getInChiAug(self):
        """
        Returns the augmented InChI from self.rmg_molecule 
        """
        return augmented_InChI
        
        
{'fraction_non_alphanumeric': 0.07842586085734364, 'fraction_numerical': 0.006043569922698524, 'mean_word_length': 1.8624296057924377, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4462997', 'n_tokens_mistral': 2226, 'n_tokens_neox': 2042, 'n_words': 475}