Skip to content

Commit

Permalink
type hints for rungen
Browse files Browse the repository at this point in the history
  • Loading branch information
ralf-meyer committed Aug 26, 2023
1 parent fb50e10 commit 944f9c6
Showing 1 changed file with 40 additions and 35 deletions.
75 changes: 40 additions & 35 deletions molSimplify/Scripts/rungen.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import sys
from collections import Counter
import glob
from typing import List, Union
from typing import List, Union, Tuple

from argparse import Namespace
from molSimplify.Classes.globalvars import globalvars
Expand All @@ -33,20 +33,20 @@
multitcgen)
from molSimplify.Scripts.structgen import (structgen)

###################################################################
### define input for cross-compatibility between python 2 and 3 ###
###################################################################
# ##################################################################
# ## define input for cross-compatibility between python 2 and 3 ###
# ##################################################################
get_input = input
if sys.version_info[:2] <= (2, 7):
get_input = raw_input # noqa: F821

#####################################
### constrained random generation ###
#####################################
# ####################################
# ## constrained random generation ###
# ####################################


def constrgen(rundir, args):
emsg = False
def constrgen(rundir: str, args: Namespace) -> Tuple[Union[Namespace, bool], str]:
emsg = ""

Check warning on line 49 in molSimplify/Scripts/rungen.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Scripts/rungen.py#L49

Added line #L49 was not covered by tests
# load global variables
licores = getlicores()
print('Random generation started..\n\n')
Expand Down Expand Up @@ -147,9 +147,9 @@ def constrgen(rundir, args):
return args, emsg


###############################################
### get sample aggreeing to the constraints ###
###############################################
# ##############################################
# ## get sample aggreeing to the constraints ###
# ##############################################


def getconstsample(no_rgen, args, licores, coord):
Expand Down Expand Up @@ -304,14 +304,14 @@ def multigenruns(rundir, args, write_files=True):
# @return Ligand list, connecting atoms, multiple ligand flag


def checkmultilig(ligs: List[str]):
def checkmultilig(ligs: List[str]) -> Tuple[List[List[str]], List[List[str]], int]:
mligs = []
tcats = []
multidx = -1
# loop over ligands

for i, lig in enumerate(ligs):
connatoms: List[Union[str, bool]] = []
connatoms: List[str] = []
if '.smi' in lig:
if '~' in lig:
lig = lig.replace('~', os.path.expanduser("~"))
Expand All @@ -323,12 +323,9 @@ def checkmultilig(ligs: List[str]):
for ss in s:
ss = ss.replace('\t', ' ')
sf = [_f for _f in ss.split(' ') if _f]
print(sf)
if len(sf) > 0:
connatoms.append(sf[-1])
multidx = i
else:
connatoms.append(False)
if len(s) > 1:
mligs.append(s)
else:
Expand All @@ -339,6 +336,7 @@ def checkmultilig(ligs: List[str]):
mligs.append([lig])
tcats.append(connatoms)
ligandslist = list(itertools.product(*mligs))

# convert tuple to list
llist = []
for l0 in ligandslist:
Expand All @@ -355,13 +353,14 @@ def checkmultilig(ligs: List[str]):
# @param rundir Run directory


def draw_supervisor(args, rundir):
def draw_supervisor(args: Namespace, rundir: str):
if args.lig:
print('Due to technical limitations, we will draw only the first ligand.')
print('To view multiple ligands at once, consider using the GUI instead.')
li = args.lig[0]
lig, emsg = lig_load(li)
lig.draw_svg(li)
if lig is not None:
lig.draw_svg(li)

Check warning on line 363 in molSimplify/Scripts/rungen.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Scripts/rungen.py#L362-L363

Added lines #L362 - L363 were not covered by tests
elif args.core:
if len(args.core) > 1:
print('Due to technical limitations, we will draw only the first core.')
Expand All @@ -370,14 +369,16 @@ def draw_supervisor(args, rundir):
print('Due to technical limitations, we can draw only one structure per run. '
'To draw the substrate, run the program again.')
cc, emsg = core_load(args.core[0])
cc.draw_svg(args.core[0])
if cc is not None:
cc.draw_svg(args.core[0])

Check warning on line 373 in molSimplify/Scripts/rungen.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Scripts/rungen.py#L372-L373

Added lines #L372 - L373 were not covered by tests
elif args.substrate:
if len(args.substrate) > 1:
print('Due to technical limitations, we will draw only the first substrate.')
print('Drawing the substrate.')
print((args.substrate[0]))
substrate, subcatoms, emsg = substr_load(args.substrate[0], 0, args.subcatoms)
substrate.draw_svg(args.substrate[0])
if substrate is not None:
substrate.draw_svg(args.substrate[0])

Check warning on line 381 in molSimplify/Scripts/rungen.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Scripts/rungen.py#L380-L381

Added lines #L380 - L381 were not covered by tests
else:
print('You have not specified anything to draw. Currently supported: ligand, core, substrate')

Expand All @@ -388,26 +389,26 @@ def draw_supervisor(args, rundir):
# @return Error messages


def rungen(rundir, args, chspfname=None, write_files=True):
# (rundir, args: Namespace, chspfname=None, write_files: bool = True)
def rungen(rundir, args: Namespace, chspfname=None, write_files: bool = True):
emsg = ''
globs = globalvars()
globs.nosmiles = 0 # reset smiles ligands for each run
# check for specified ligands/functionalization
ligocc = []
# check for files specified for multiple ligands
mligs, catoms = [False], [False]
mligs: List[List[str]] = [[]]
catoms: List[List[str]] = [[]]
if args.lig is not None:
if args.lig:
mligs, catoms, multidx = checkmultilig(args.lig)
if args.debug:
print('after checking for multiple ligs, we found '
f'{multidx} ligands')
# save initial
smicat0 = [ss for ss in args.smicat] if args.smicat else False
smicat0 = [ss for ss in args.smicat] if args.smicat else []
# loop over ligands
for mcount, mlig in enumerate(mligs):
args.smicat = [ss for ss in smicat0] if smicat0 else False
args.smicat = [ss for ss in smicat0] if smicat0 else []
args.checkdir, skip = False, False # initialize flags
if len(mligs) > 0 and mligs[0]:
args.lig = mlig # get combination
Expand Down Expand Up @@ -450,8 +451,8 @@ def rungen(rundir, args, chspfname=None, write_files=True):
else:
ligands = []
lig = ''
ligocc = ''
# fetch smart name
ligocc = []

Check warning on line 454 in molSimplify/Scripts/rungen.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Scripts/rungen.py#L454

Added line #L454 was not covered by tests
# fetch smart name
fname = name_complex(rundir, args.core, args.geometry, ligands, ligocc,
mcount, args, nconf=False, sanity=False, bind=args.bind, bsmi=args.nambsmi)
if args.tsgen:
Expand All @@ -461,8 +462,10 @@ def rungen(rundir, args, chspfname=None, write_files=True):
subcatoms = args.subcatoms
mlig = args.mlig
mligcatoms = args.mligcatoms
fname = name_ts_complex(rundir, args.core, args.geometry, ligands, ligocc, substrate, subcatoms,
mlig, mligcatoms, mcount, args, nconf=False, sanity=False, bind=args.bind, bsmi=args.nambsmi)
fname = name_ts_complex(rundir, args.core, args.geometry, ligands, ligocc,

Check warning on line 465 in molSimplify/Scripts/rungen.py

View check run for this annotation

Codecov / codecov/patch

molSimplify/Scripts/rungen.py#L465

Added line #L465 was not covered by tests
substrate, subcatoms, mlig, mligcatoms, mcount,
args, nconf=False, sanity=False, bind=args.bind,
bsmi=args.nambsmi)
if args.debug:
print(('fname is ' + str(fname)))
rootdir = fname
Expand Down Expand Up @@ -490,7 +493,8 @@ def rungen(rundir, args, chspfname=None, write_files=True):
else:
flagdir = 'replace'
else:
# qqb = qBoxFolder(args.gui.wmain,'Folder exists','Directory '+rootcheck+' already exists. What do you want to do?')
# qqb = qBoxFolder(args.gui.wmain,'Folder exists',
# 'Directory '+rootcheck+' already exists. What do you want to do?')
# flagdir = qqb.getaction()
flagdir = 'replace'
# replace existing directory
Expand Down Expand Up @@ -529,7 +533,8 @@ def rungen(rundir, args, chspfname=None, write_files=True):
else:
flagdir = 'replace'
else:
# qqb = qBoxFolder(args.gui.wmain,'Folder exists','Directory '+rootdir+' already exists. What do you want to do?')
# qqb = qBoxFolder(args.gui.wmain,'Folder exists',
# 'Directory '+rootdir+' already exists. What do you want to do?')
# flagdir = qqb.getaction()
flagdir = 'replace'
# replace existing directory
Expand All @@ -551,9 +556,9 @@ def rungen(rundir, args, chspfname=None, write_files=True):
if write_files:
args.checkdirb = True
os.mkdir(rootdir)
####################################
############ GENERATION ############
####################################
# ###################################
# ########### GENERATION ############
# ###################################
if not skip:
# check for generate all
if args.genall:
Expand Down

0 comments on commit 944f9c6

Please sign in to comment.