<commit_before>'''
Main entry point to access the on-the-fly estimation of 
thermochemical properties of species using quantum chemical packages such 
as G03, (Open)Mopac and MM4.

This module contains the driver class called QMTP with its principal method
 .generateQMThermoData that generates a ThermoData object.
 
A molfile class is a rather artificial and probably redundant class from RMG-Java
that collects information on paths to the 3D mol files.

ThreeDMolFileCreator is a wrapper class that:
-converts a RMG-Py molecule into a 2D mol file using OpenBabel
-generates 3D coordinates using RDKit 
'''

import os
import platform
from subprocess import Popen
from rmgpy.molecule import Molecule
import qmverifier as verif
import rmg_qm_parsers as pars
import qmtpjobs as job
import qminputwriters as writers
import symmetry as symm
import logging
import openbabel
from rdkit import Chem
from rdkit.Chem import AllChem

class molFile:
    '''
    molFile class to store and perform operations on mol files
    may eventually want to have a super class threeDGeom and say "public class molFile extends threeDGeom {"
    '''
    
    def __init__(self, molecule, name='', directory=''):
        #construct a molFile object while writing a file with text determined by chemGraph
        self.name = name
        self.directory = directory
        self.molecule = molecule
        self.path = os.path.join(self.directory,self.name +'.mol')
        self.crudepath = os.path.join(self.directory, self.name+'.cmol')
     
    
class ThreeDMolFileCreator:
    
    def __init__(self, name, directory, molecule):
        self.name = name
        self.directory = directory
        self.molecule = molecule
        
        self.mol2D = name + '.mol2D'
        self.mol3D = name + '.mol'
        self.mol3Dcrude = name + '.cmol'
        
        self.dir2D = os.path.join(directory,'2DMolfiles/')
        self.dir3D = os.path.join(directory,'3DMolfiles/')
        
    def create2D(self):
        '''
        #1. create a 2D file
        #use the absolute path for directory, so we can easily reference from other directories in command-line paths
        #can't use RMG_workingDirectory, since this basically holds the RMG environment variable, not the workingDirectory
        String directory = "2Dmolfiles/"
        '''
        obmol = self.molecule.toOBMol()
        obConversion = openbabel.OBConversion()
        obConversion.SetOutFormat('mol')
        output = obConversion.WriteString(obmol)
        with open(os.path.join(self.dir2D, self.mol2D),'w') as out:
            out.write(output)
        
    def embed3D(self, numConfAttempts):
        '''
        #embed a molecule in 3D, using RDKit
        '''<commit_msg>qmtp.ThreeDMolFileCreator creates working directories as needed.<commit_after>'''
Main entry point to access the on-the-fly estimation of 
thermochemical properties of species using quantum chemical packages such 
as G03, (Open)Mopac and MM4.

This module contains the driver class called QMTP with its principal method
 .generateQMThermoData that generates a ThermoData object.
 
A molfile class is a rather artificial and probably redundant class from RMG-Java
that collects information on paths to the 3D mol files.

ThreeDMolFileCreator is a wrapper class that:
-converts a RMG-Py molecule into a 2D mol file using OpenBabel
-generates 3D coordinates using RDKit 
'''

import os
import platform
from subprocess import Popen
from rmgpy.molecule import Molecule
import qmverifier as verif
import rmg_qm_parsers as pars
import qmtpjobs as job
import qminputwriters as writers
import symmetry as symm
import logging
import openbabel
from rdkit import Chem
from rdkit.Chem import AllChem

class molFile:
    '''
    molFile class to store and perform operations on mol files
    may eventually want to have a super class threeDGeom and say "public class molFile extends threeDGeom {"
    '''
    
    def __init__(self, molecule, name='', directory=''):
        #construct a molFile object while writing a file with text determined by chemGraph
        self.name = name
        self.directory = directory
        self.molecule = molecule
        self.path = os.path.join(self.directory,self.name +'.mol')
        self.crudepath = os.path.join(self.directory, self.name+'.cmol')
     
    
class ThreeDMolFileCreator:
    
    def __init__(self, name, directory, molecule):
        self.name = name
        self.directory = directory
        self.molecule = molecule
        
        self.mol2D = name + '.mol2D'
        self.mol3D = name + '.mol'
        self.mol3Dcrude = name + '.cmol'
        
        self.dir2D = os.path.join(directory,'2DMolfiles/')
        self.dir3D = os.path.join(directory,'3DMolfiles/')
        
        for directory in (self.dir2D, self.dir3D):
            if not os.path.exists(directory):
                logging.info("Creating directory %s for mol files."%os.path.abspath(directory))
                os.makedirs(directory)

    def create2D(self):
        '''
        #1. create a 2D file
        #use the absolute path for directory, so we can easily reference from other directories in command-line paths
        #can't use RMG_workingDirectory, since this basically holds the RMG environment variable, not the workingDirectory
        String directory = "2Dmolfiles/"
        '''
        obmol = self.molecule.toOBMol()
        obConversion = openbabel.OBConversion()
        obConversion.SetOutFormat('mol')
        output = obConversion.WriteString(obmol)
        with open(os.path.join(self.dir2D, self.mol2D),'w') as out:
            out.write(output)
        
    def embed3D(self, numConfAttempts):
        '''
        #embed a molecule in 3D, using RDKit
        '''
{'fraction_non_alphanumeric': 0.07431960921144452, 'fraction_numerical': 0.008025122121423587, 'mean_word_length': 2.87103308575287, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6677914', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1613, 'n_words': 578}