#!/usr/bin/env python3
# (C) 2017 OpenEye Scientific Software Inc. All rights reserved.
#
# TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
# provided to current licensees or subscribers of OpenEye products or
# SaaS offerings (each a "Customer").
# Customer is hereby permitted to use, copy, and modify the Sample Code,
# subject to these terms. OpenEye claims no rights to Customer's
# modifications. Modification of Sample Code is at Customer's sole and
# exclusive risk. Sample Code may require Customer to have a then
# current license or subscription to the applicable OpenEye offering.
# THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED. OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall OpenEye be
# liable for any damages or liability in connection with the Sample Code
# or its use.
#############################################################################
# Randomly reorders molecules and optionally obtain a random subset
#############################################################################
import sys
from random import Random
from openeye import oechem
def WriteDatabase(moldb, ofs, mlist, size):
for molidx in mlist[:size]:
moldb.WriteMolecule(ofs, molidx)
def RandomizePercent(ifs, ofs, percent, rand):
moldb = oechem.OEMolDatabase(ifs)
indices = range(moldb.GetMaxMolIdx())
size = max(1, int(percent * 0.01 * moldb.GetMaxMolIdx()))
mlist = rand.sample(indices, size)
WriteDatabase(moldb, ofs, mlist, size)
def Randomize(ifs, ofs, rand):
wholedb = 100.0 # percent
RandomizePercent(ifs, ofs, wholedb, rand)
def RandomizeN(ifs, ofs, count, rand):
moldb = oechem.OEMolDatabase(ifs)
indices = range(moldb.GetMaxMolIdx())
mlist = rand.sample(indices, count)
WriteDatabase(moldb, ofs, mlist, count)
def main(argv=[__name__]):
itf = oechem.OEInterface(InterfaceData, argv)
if itf.HasFloat("-p") and itf.HasInt("-n"):
oechem.OEThrow.Usage("Give only one option, -p or -n")
ifs = oechem.oemolistream()
if not ifs.open(itf.GetString("-i")):
oechem.OEThrow.Fatal("Unable to open %s for reading" % itf.GetString("-i"))
ofs = oechem.oemolostream(".ism")
if itf.HasString("-o"):
if not ofs.open(itf.GetString("-o")):
oechem.OEThrow.Fatal("Unable to open %s for writing" % itf.GetString("-o"))
if itf.HasInt("-seed"):
rand = Random(itf.GetInt("-seed"))
else:
rand = Random()
if itf.HasInt("-n"):
RandomizeN(ifs, ofs, itf.GetInt("-n"), rand)
elif itf.HasFloat("-p"):
RandomizePercent(ifs, ofs, itf.GetFloat("-p"), rand)
else:
Randomize(ifs, ofs, rand)
InterfaceData = """
!BRIEF [-i] [-o]