Beispiel #1
0
def dbsearch(rundir, args, globs):
    cwd = os.getcwd()
    flag = False

    obab = 'obabel'
    if args.gui:
        from molSimplify.Classes.mWidgets import mQDialogErr
        from molSimplify.Classes.mWidgets import mQDialogWarn
        from molSimplify.Classes.mWidgets import mQDialogInf
    ### in any case do similarity search over indexed db ###
    outf = args.dbfname if args.dbfname else 'simres.smi'  # output file
    ### convert to SMILES/SMARTS if file
    if not args.dbbase:
        if args.gui:
            qqb = mQDialogWarn('Warning', "No database file found within " + globs.chemdbdir + '. Search not possible.')
            qqb.setParent(args.gui.DBWindow)
        print "No database file found within " + globs.chemdbdir + '. Search not possible.'
        return True
    # if args.dbsim:
    # print('similarity searching')
    # if '.smi' in args.dbsim:
    # if glob.glob(args.dbsim):
    # f = open(args.dbsim,'r')
    # smistr = f.read()
    # f.close()
    # else:
    # print 'File '+args.dbsim+' not existing. Check your input.'
    # print 'Similarity search terminating..'
    # return True
    # elif ('.mol' in args.dbsim or '.xyz' in args.dbsim):
    # if glob.glob(args.dbsim):
    # ftype = args.dbsim.split('.')[-1]
    # obConversion = openbabel.OBConversion()
    # obConversion.SetInFormat(ftype)
    # OBMol = openbabel.OBMol()
    # obConversion.ReadFile(OBMol,args.dbsim)
    # smistr = pybel.write("smi")
    # else:
    # print 'File '+args.dbsim+' not existing. Check your input.'
    # print 'Similarity search terminating..'
    # return True
    # else:
    # smistr = args.dbsim
    # print smistr
    if args.dbsmarts:
        if '.smi' in args.dbsmarts:
            if glob.glob(args.dbsmarts):
                f = open(args.dbsmarts, 'r')
                smistr = f.read()
                f.close()
            else:
                print 'File ' + args.dbsmarts + ' does not exist. Check your input.'
                print 'Substructure search terminating..'
                return 1
        elif ('.mol' in args.dbsmarts or '.xyz' in args.dbsmarts):
            if glob.glob(args.dbsmarts):
                ftype = args.dbsmarts.split('.')[-1]

                smistr = pymol.write("smi")
            else:
                print 'File ' + args.dbsmarts + ' does not exist. Check your input.'
                print 'Substructure search terminating..'
                return True
        else:
            smistr = args.dbsmarts
    elif args.dbhuman:
        smistr = []
        denticity = args.dbvdent if args.dbvdent else '1'
        coordatoms = args.dbvconns if args.dbvconns else 'N'
        hyb = args.dbvhyb if args.dbvhyb else '3'
        nlinks = args.dbvlinks if args.dbvlinks else '2'
        monod = ['1', 'mono', 'Mono', 'monodentate', 'Monodentate']
        bid = ['2', 'bi', 'Bi', 'bidentate', 'Bidentate']
        if args.debug:
            print('dbhuman conversion')
            print('dbhuman coordatoms ' + str(coordatoms))
            print('dbhuman nlinks ' + str(nlinks))
            print('dbhuman hyb ' + str(hyb))
        if denticity in monod:
            smistr = '[#' + str(amassdict[coordatoms[0]][1]) + '^' + hyb[0] + ';!+]'
        elif denticity in bid:
            smistr = '[#' + str(amassdict[coordatoms[0]][1]) + '^' + hyb[0] + ';!+]'
            for i in range(int(nlinks)):
                smistr = smistr + '[#6;R0]'
            print(coordatoms, hyb)
            smistr = smistr + '[#' + str(amassdict[coordatoms[1]][1]) + '^' + hyb[1] + ';!+]'
        print('setting smistr from dbhuman ' + smistr)

    # else:
    ## get database
    # [dbsdf,dbfs] = setupdb(args.dbbase)
    ## convert to smiles and print to output
    # if globs.osx:
    # cmd = "/usr/local/bin/obabel "+dbsdf+" -f0 -l100 -o"+outf[-3:]+" -O "+outf
    # else:
    # cmd = obab+" "+dbsdf+" -f0 -l100 -o"+outf[-3:]+" -O "+outf
    # t = mybash(cmd)
    # os.rename(outf,args.rundir+'/'+outf)
    # print t
    # return False
    ### parse filters
    squery = checkscr(args)
    if args.debug:
        print("squery is " + str(squery))

    if args.dbmaxsmartsmatches:
        plugin_path = plugin_defs()
        shutil.copy(plugin_path, 'plugindefines.txt')
        cmd = "sed -i '/nsmartsmatches/!b;n;c" + smistr + "' " + 'plugindefines.txt'
        mybash(cmd)
    ### run substructure search ###
    nmols = '10000' if not args.dbnsearch else args.dbnsearch
    finger = 'FP2' if not args.dbfinger else args.dbfinger
    if int(nmols) > 3000 and args.gui:
        qqb = mQDialogInf('Warning', "Database search is going to take a few minutes. Please wait..OK?")
        qqb.setParent(args.gui.DBWindow)
    if args.dbsmarts or args.dbhuman or args.dbsim:
        outputf, flag = getsimilar(smistr, nmols, args.dbbase, finger, squery, args)
        try:
            shutil.copy('simres.smi', outf)
        except:
            pass

    if args.debug:
        print('after similarity search, outf is ' + str(outputf))
    if flag:
        if args.gui:
            qqb = mQDialogWarn('Warning', "No matches found in search..")
            qqb.setParent(args.gui.DBWindow)
        print "No matches found in search.."
        return True
    # strip metals and clean-up, remove duplicates etc
    # print('mb ' +  str(mybash('cat '+outf)))
    if args.dbsmarts or args.dbhuman:
        print('Stripping salts and removing duplicates')

        print('number of smiles strings BEFORE salt stripping: ' + mybash("cat " + outf + '| wc -l'))
        flag = stripsalts(outf)
        print('number of smiles strings AFTER salt stripping: ' + mybash("cat " + outf + '| wc -l'))
        # print('flag from salt stripping: ' + str(flag))
        print('number of smiles strings BEFORE unique: ' + mybash("cat " + outf + '| wc -l'))
        cmd = obab + " -ismi " + outf + " -osmi -O " + outf + " --unique"
        # print('running:' + str(cmd))
        shutil.copy(outf, 'afterstrippingsalts.smi')
        t = mybash(cmd)
        print('number of smiles strings AFTER unique: ' + mybash("cat " + outf + '| wc -l'))

    # print 't (ret from bash) is  '+ str(t)
    # filter results containing elements that aren't allowed
    if args.dballowedels:
        if args.dballowedels == 'organic':  # HCNO only
            allowedels = ['H', 'C', 'N', 'O']
        elif args.dballowedels == 'organohalides':
            allowedels = ['H', 'C', 'N', 'O', 'F', 'Cl', 'Br', 'I']
        elif args.dballowedels == 'common':
            allowedels = ['H', 'C', 'N', 'O', 'F', 'Cl', 'Br', 'I', 'P', 'S']
        else:
            allowedels = args.dballowedels
        print('number of smiles strings BEFORE element filter: ' + mybash("cat " + outf + '| wc -l'))
        checkels(outf, allowedels)
        print('number of smiles strings AFTER element filter unique: ' + mybash("cat " + outf + '| wc -l'))

        shutil.copy(outf, 'afterfilteringels.smi')
    # check if defined connection atoms
    if args.dbcatoms:
        catoms = [int(a) for a in args.dbcatoms]
    else:
        catoms = [1]
    # do pattern matching
    nres = 50 if not args.dbresults else int(args.dbresults)
    if args.dbsmarts or args.dbhuman:
        print('number of smiles strings BEFORE SMARTS filter: ' + mybash("cat " + outf + '| wc -l'))
        flag = matchsmarts(smistr, outf, catoms, args)
        print('number of smiles strings AFTER SMARTS filter: ' + mybash("cat " + outf + '| wc -l'))
    if args.debug:
        print('outf is ' + str(outf))
    ### maximal dissimilarity search
    if args.dbdissim:
        dissim(outf, int(args.dbdissim))
    if args.rundir:
        print('writing output to ' + str(args.rundir) + '/' + str(outf) + '\n')
        os.rename(outf, args.rundir + '/' + outf)
    else:
        print('writing output to ' + str(cwd) + '/' + str(outf) + '\n')
    # os.chdir(cwd)
    return False
Beispiel #2
0
def dbsearch(rundir, args, globs):
    cwd = os.getcwd()
    flag = False

    obab = 'obabel'
    if args.gui:
        from molSimplify.Classes.mWidgets import mQDialogWarn
        from molSimplify.Classes.mWidgets import mQDialogInf
    ### in any case do similarity search over indexed db ###
    outf = args.dbfname if args.dbfname else 'simres.smi'  # output file
    # convert to SMILES/SMARTS if file
    if not args.dbbase:
        if args.gui:
            qqb = mQDialogWarn(
                'Warning', "No database file found within " + globs.chemdbdir +
                '. Search not possible.')
            qqb.setParent(args.gui.DBWindow)
        print(("No database file found within " + globs.chemdbdir +
               '. Search not possible.'))
        return True
    # if args.dbsim:
    # print('similarity searching')
    # if '.smi' in args.dbsim:
    # if glob.glob(args.dbsim):
    # f = open(args.dbsim,'r')
    # smistr = f.read()
    # f.close()
    # else:
    # print 'File '+args.dbsim+' not existing. Check your input.'
    # print 'Similarity search terminating..'
    # return True
    # elif ('.mol' in args.dbsim or '.xyz' in args.dbsim):
    # if glob.glob(args.dbsim):
    # ftype = args.dbsim.split('.')[-1]
    # obConversion = openbabel.OBConversion()
    # obConversion.SetInFormat(ftype)
    # OBMol = openbabel.OBMol()
    # obConversion.ReadFile(OBMol,args.dbsim)
    # smistr = pybel.write("smi")
    # else:
    # print 'File '+args.dbsim+' not existing. Check your input.'
    # print 'Similarity search terminating..'
    # return True
    # else:
    # smistr = args.dbsim
    # print smistr
    if args.dbsmarts:
        if '.smi' in args.dbsmarts:
            if glob.glob(args.dbsmarts):
                f = open(args.dbsmarts, 'r')
                smistr = f.read()
                f.close()
            else:
                print(('File ' + args.dbsmarts +
                       ' does not exist. Check your input.'))
                print('Substructure search terminating..')
                return 1
        elif ('.mol' in args.dbsmarts or '.xyz' in args.dbsmarts):
            if glob.glob(args.dbsmarts):
                ftype = args.dbsmarts.split('.')[-1]

                smistr = pymol.write("smi")
            else:
                print(('File ' + args.dbsmarts +
                       ' does not exist. Check your input.'))
                print('Substructure search terminating..')
                return True
        else:
            smistr = args.dbsmarts
    elif args.dbhuman:
        smistr = []
        denticity = args.dbvdent if args.dbvdent else '1'
        coordatoms = args.dbvconns if args.dbvconns else 'N'
        hyb = args.dbvhyb if args.dbvhyb else '3'
        nlinks = args.dbvlinks if args.dbvlinks else '2'
        monod = ['1', 'mono', 'Mono', 'monodentate', 'Monodentate']
        bid = ['2', 'bi', 'Bi', 'bidentate', 'Bidentate']
        if args.debug:
            print('dbhuman conversion')
            print(('dbhuman coordatoms ' + str(coordatoms)))
            print(('dbhuman nlinks ' + str(nlinks)))
            print(('dbhuman hyb ' + str(hyb)))
        if denticity in monod:
            smistr = '[#' + str(
                amassdict[coordatoms[0]][1]) + '^' + hyb[0] + ';!+]'
        elif denticity in bid:
            smistr = '[#' + str(
                amassdict[coordatoms[0]][1]) + '^' + hyb[0] + ';!+]'
            for i in range(int(nlinks)):
                smistr = smistr + '[#6;R0]'
            print((coordatoms, hyb))
            smistr = smistr + \
                '[#' + str(amassdict[coordatoms[1]][1]) + '^' + hyb[1] + ';!+]'
        print(('setting smistr from dbhuman ' + smistr))

    # else:
    # get database
    # [dbsdf,dbfs] = setupdb(args.dbbase)
    # convert to smiles and print to output
    # if globs.osx:
    # cmd = "/usr/local/bin/obabel "+dbsdf+" -f0 -l100 -o"+outf[-3:]+" -O "+outf
    # else:
    # cmd = obab+" "+dbsdf+" -f0 -l100 -o"+outf[-3:]+" -O "+outf
    # t = mybash(cmd)
    # os.rename(outf,args.rundir+'/'+outf)
    # print t
    # return False
    # parse filters
    squery = checkscr(args)
    if args.debug:
        print(("squery is " + str(squery)))

    if args.dbmaxsmartsmatches:
        plugin_path = plugin_defs()
        shutil.copy(plugin_path, 'plugindefines.txt')
        cmd = "sed -i '/nsmartsmatches/!b;n;c" + smistr + "' " + 'plugindefines.txt'
        mybash(cmd)
    ### run substructure search ###
    nmols = '10000' if not args.dbnsearch else args.dbnsearch
    finger = 'FP2' if not args.dbfinger else args.dbfinger
    if int(nmols) > 3000 and args.gui:
        qqb = mQDialogInf(
            'Warning',
            "Database search is going to take a few minutes. Please wait..OK?")
        qqb.setParent(args.gui.DBWindow)
    if args.dbsmarts or args.dbhuman or args.dbsim:
        outputf, flag = getsimilar(smistr, nmols, args.dbbase, finger, squery,
                                   args)
        try:
            shutil.copy('simres.smi', outf)
        except:
            pass

    if args.debug:
        print(('after similarity search, outf is ' + str(outputf)))
    if flag:
        if args.gui:
            qqb = mQDialogWarn('Warning', "No matches found in search..")
            qqb.setParent(args.gui.DBWindow)
        print("No matches found in search..")
        return True
    # strip metals and clean-up, remove duplicates etc
    # print('mb ' +  str(mybash('cat '+outf)))
    if args.dbsmarts or args.dbhuman:
        print('Stripping salts and removing duplicates')

        print(('number of smiles strings BEFORE salt stripping: ' +
               mybash("cat " + outf + '| wc -l')))
        flag = stripsalts(outf)
        print(('number of smiles strings AFTER salt stripping: ' +
               mybash("cat " + outf + '| wc -l')))
        # print('flag from salt stripping: ' + str(flag))
        print(('number of smiles strings BEFORE unique: ' +
               mybash("cat " + outf + '| wc -l')))
        cmd = obab + " -ismi " + outf + " -osmi -O " + outf + " --unique"
        # print('running:' + str(cmd))
        shutil.copy(outf, 'afterstrippingsalts.smi')
        t = mybash(cmd)
        print(('number of smiles strings AFTER unique: ' +
               mybash("cat " + outf + '| wc -l')))

    # print 't (ret from bash) is  '+ str(t)
    # filter results containing elements that aren't allowed
    if args.dballowedels:
        if args.dballowedels == 'organic':  # HCNO only
            allowedels = ['H', 'C', 'N', 'O']
        elif args.dballowedels == 'organohalides':
            allowedels = ['H', 'C', 'N', 'O', 'F', 'Cl', 'Br', 'I']
        elif args.dballowedels == 'common':
            allowedels = ['H', 'C', 'N', 'O', 'F', 'Cl', 'Br', 'I', 'P', 'S']
        else:
            allowedels = args.dballowedels
        print(('number of smiles strings BEFORE element filter: ' +
               mybash("cat " + outf + '| wc -l')))
        checkels(outf, allowedels)
        print(('number of smiles strings AFTER element filter unique: ' +
               mybash("cat " + outf + '| wc -l')))

        shutil.copy(outf, 'afterfilteringels.smi')
    # check if defined connection atoms
    if args.dbcatoms:
        catoms = [int(a) for a in args.dbcatoms]
    else:
        catoms = [1]
    # do pattern matching
    nres = 50 if not args.dbresults else int(args.dbresults)
    if args.dbsmarts or args.dbhuman:
        print(('number of smiles strings BEFORE SMARTS filter: ' +
               mybash("cat " + outf + '| wc -l')))
        flag = matchsmarts(smistr, outf, catoms, args)
        print(('number of smiles strings AFTER SMARTS filter: ' +
               mybash("cat " + outf + '| wc -l')))
    if args.debug:
        print(('outf is ' + str(outf)))
    # maximal dissimilarity search
    if args.dbdissim:
        dissim(outf, int(args.dbdissim))
    if args.rundir:
        print(
            ('writing output to ' + str(args.rundir) + '/' + str(outf) + '\n'))
        os.rename(outf, args.rundir + '/' + outf)
    else:
        print(('writing output to ' + str(cwd) + '/' + str(outf) + '\n'))
    # os.chdir(cwd)
    return False
Beispiel #3
0
def postproc(rundir, args, globs):
    globs = globalvars()
    if args.gui:
        from molSimplify.Classes.mWidgets import mQDialogInf
        choice = mQDialogInf(
            'Post processing',
            'Parsing the results will take a while..Please be patient. Start?')
        choice.setParent(args.gui.pWindow)
    # locate output files
    pdir = args.postdir if args.postdir else globs.rundir
    cmd = "find '" + pdir + "' -name *out"
    t = mybash(cmd)
    resf = t.splitlines()
    logfile = pdir + "/post.log"
    if not os.path.isdir(pdir):
        print(('\nSpecified directory ' + pdir + ' does not exist..\n\n'))
        if args.gui:
            args.gui.iWtxt.setText('\nSpecified directory ' + pdir +
                                   ' does not exist.\n\n' +
                                   args.gui.iWtxt.toPlainText())
        return
    flog = open(logfile, 'a')
    flog.write('\n\n\n##### Date: ' + time.strftime('%m/%d/%Y %H:%M') +
               '#####\n\n')
    # run summary report
    if args.pres:
        print('\nGetting runs summary..\n\n')
        flog.write('\nGetting runs summary..\n\n')
        if args.gui:
            args.gui.iWtxt.setText('\nGetting runs summary..\n\n' +
                                   args.gui.iWtxt.toPlainText())
        terapost(resf, pdir, args.gui, flog)
        gampost(resf, pdir, args.gui, flog)
    # run nbo analysis
    if args.pnbo:
        print('\nGetting NBO summary..\n\n')
        flog.write('\nGetting NBO summary..\n\n')
        if args.gui:
            args.gui.iWtxt.setText('\nGetting NBO summary..\n\n' +
                                   args.gui.iWtxt.toPlainText())
        nbopost(resf, pdir, args.gui, flog)
    # locate molden files
    cmd = "find " + "'" + pdir + "'" + " -name *molden"
    t = mybash(cmd)
    molf = t.splitlines()
    # parse molecular orbitals
    if args.porbinfo:
        print('\nGetting MO information..\n\n')
        flog.write('\nGetting MO information..\n\n')
        if args.gui:
            args.gui.iWtxt.setText('\nGetting MO information..\n\n' +
                                   args.gui.iWtxt.toPlainText())
        if not os.path.isdir(pdir + '/MO_files'):
            os.mkdir(pdir + '/MO_files')
        moldpost(molf, pdir, args.gui, flog)
    # calculate delocalization indices
    if args.pdeloc:
        print('\nCalculating delocalization indices..\n\n')
        flog.write('\nCalculating delocalization indices..\n\n')
        if args.gui:
            args.gui.iWtxt.setText(
                '\nCalculating delocalization indices..\n\n' +
                args.gui.iWtxt.toPlainText())
        if not os.path.isdir(pdir + '/Deloc_files'):
            os.mkdir(pdir + '/Deloc_files')
        deloc(molf, pdir, args.gui, flog)
    # calculate charges
    if args.pcharge:
        print('\nCalculating charges..\n\n')
        flog.write('\nCalculating charges..\n\n')
        if args.gui:
            args.gui.iWtxt.setText('\nCalculating charges..\n\n' +
                                   args.gui.iWtxt.toPlainText())
        if not os.path.isdir(pdir + '/Charge_files'):
            os.mkdir(pdir + '/Charge_files')
        getcharges(molf, pdir, args.gui, flog)
    # parse wavefunction
    if args.pwfninfo:
        print('\nCalculating wavefunction properties..\n\n')
        flog.write('\nCalculating wavefunction properties..\n\n')
        if args.gui:
            args.gui.iWtxt.setText(
                '\nCalculating wavefunction properties..\n\n' +
                args.gui.iWtxt.toPlainText())
        if not os.path.isdir(pdir + '/Wfn_files'):
            os.mkdir(pdir + '/Wfn_files')
        if not os.path.isdir(pdir + '/Cube_files'):
            os.mkdir(pdir + '/Cube_files')
        getcubes(molf, pdir, args.gui, flog)
        getwfnprops(molf, pdir, args.gui, flog)
        if not args.pgencubes and os.path.isdir(pdir + '/Cube_files'):
            shutil.rmtree(pdir + '/Cube_files')
    # generate cube files
    if args.pgencubes:
        print('\nGenerating cube files..\n\n')
        flog.write('\nGenerating cube files..\n\n')
        if args.gui:
            args.gui.iWtxt.setText('\nGenerating cube files..\n\n' +
                                   args.gui.iWtxt.toPlainText())
        if not os.path.isdir(pdir + '/Cube_files'):
            os.mkdir(pdir + '/Cube_files')
        getcubes(molf, pdir, args.gui, flog)
    flog.close()