Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def validate_and_fill_unsettled_geometry(geom_unsettled, variables):
lgeom = [len(g) for g in geom_unsettled]
if lgeom[0] not in [0, 3]:
raise ValidationError("""First line must be Cartesian or single atom.""")
if any(l == 3 for l in lgeom) and not all((l in [3, 6]) for l in lgeom):
raise ValidationError(
"""Mixing Cartesian and Zmat formats must occur in just that order once absolute frame established."""
)
allowed_to_follow = {0: [2], 2: [4], 3: [3, 6], 4: [6], 6: [3, 6]}
for il in range(len(lgeom) - 1):
if lgeom[il + 1] not in allowed_to_follow[lgeom[il]]:
raise ValidationError(
"""This is not how a Zmat works - aim for lower triangular. Line len ({}) may be followed by line len ({}), not ({}).""".format(
lgeom[il], allowed_to_follow[lgeom[il]], lgeom[il + 1]
)
)
if not all(len(v) == 2 for v in variables):
raise ValidationError("""Variables should come in pairs: {}""".format(variables))
files = [f.lower() for f in fragment_files]
except AttributeError:
raise ValidationError("""fragment_files not strings: {}""".format(fragment_files))
if all(f in ["xyzabc", "points", "rotmat"] for f in hint_types):
types = hint_types
else:
raise ValidationError("""hint_types not among 'xyzabc', 'points', 'rotmat': {}""".format(hint_types))
hints = []
hlen = {"xyzabc": 6, "points": 9, "rotmat": 12}
for ifr, fr in enumerate(geom_hints):
try:
hint = [float(f) for f in fr]
except (ValueError, TypeError):
raise ValidationError("""Un float-able elements in geom_hints[{}]: {}""".format(ifr, fr))
htype = hint_types[ifr]
if len(hint) == hlen[htype]:
hints.append(hint)
else:
raise ValidationError("""EFP hint type {} not {} elements: {}""".format(htype, hlen[htype], hint))
return {"fragment_files": files, "hint_types": types, "geom_hints": hints}
# search pubchem for the provided string
try:
results = pubchem.get_pubchem_results(pubsearch)
except Exception as e:
raise ValidationError(e.message)
if pubsearch.endswith('*'):
pubsearch = pubsearch[:-1]
if len(results) == 1:
# There's only 1 result - use it
xyz = results[0].get_molecule_string()
processed['name'] = 'IUPAC {}'.format(results[0].name())
processed['molecular_charge'] = float(results[0].molecular_charge)
if 'Input Error' in xyz:
raise ValidationError(xyz)
else:
# There are multiple results -- print and exit
# * formerly, this checked for (then used) any exact match, but now (LAB; Sep 2018), disabling that
# since user explicitly added '*' char & "best match" (not available formerly) returned w/o '*'
msg = "\tPubchemError\n"
msg += "\tMultiple pubchem results were found. Replace\n\n\t\tpubchem:%s\n\n" % (pubsearch)
msg += "\twith the Chemical ID number or exact name from one of the following and re-run.\n\n"
msg += "\t Chemical ID IUPAC Name\n\n"
ematches = {}
for result in results:
msg += "%s" % (result)
ematches[result.cid] = result.iupac
raise ChoicesError(msg, ematches)
# remove PubchemInput first line and assert [A]
xyz = xyz.replace('PubchemInput', 'units ang')
)
)
header = False
assessment = [fn(cc, cfc, cm, cfm) for fn in cgmp_range]
sass = ["{:3}".format("T" if b else "") for b in assessment]
if log_full:
text.append("""Assess candidate {:}: {} --> {}""".format(candidate, " ".join(sass), all(assessment)))
if all(assessment):
return candidate
err = """Inconsistent or unspecified chg/mult: sys chg: {}, frag chg: {}, sys mult: {}, frag mult: {}""".format(
molecular_charge, fragment_charges, molecular_multiplicity, fragment_multiplicities
)
if verbose > -1:
print("\n\n" + "\n".join(text))
raise ValidationError(err)
try:
frc = [(f if f is None else float(f)) for f in fragment_charges]
except TypeError:
raise ValidationError("""fragment_charges not among None or float: {}""".format(fragment_charges))
if fragment_multiplicities is None:
frm = [None] * nfr
elif all(f is None or (isinstance(f, (int, np.integer)) and f >= 1) for f in fragment_multiplicities):
frm = fragment_multiplicities
else:
raise ValidationError(
"""fragment_multiplicities not among None or positive integer: {}""".format(fragment_multiplicities)
)
if not (len(frc) == len(frm) == len(frs) + 1):
raise ValidationError(
"""Dimension mismatch among fragment quantities: sep + 1 ({}), chg ({}), and mult({})""".format(
len(frs) + 1, len(frc), len(frm)
)
)
return {"fragment_separators": list(frs), "fragment_charges": frc, "fragment_multiplicities": frm}
def reorder(arr):
if nat != len(arr):
raise ValidationError("""wrong number of atoms in array: nat = {} != {}""".format(nat, len(arr)))
return np.concatenate([np.array(arr)[fr] for fr in frag_pattern], axis=0)
def get_sdf(self):
"""Function to return the SDF (structure-data file) of the PubChem object."""
from urllib.request import urlopen, Request
from urllib.parse import quote
from urllib.error import URLError
if (len(self.dataSDF) == 0):
url = 'https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/{}/SDF?record_type=3d'.format(
quote(str(self.cid)))
req = Request(url, headers={'Accept': 'chemical/x-mdl-sdfile'})
try:
self.dataSDF = urlopen(req).read().decode('utf-8')
except URLError as e:
msg = "Unable to open\n\n%s\n\ndue to the error\n\n%s\n\n" % (url, e)
msg += "It is possible that 3D information does not exist for this molecule in the PubChem database\n"
print(msg)
raise ValidationError(msg)
return self.dataSDF
Parameters
----------
ref : list
Hashes encoding distinguishable non-coord characteristics of reference
molecule. Namely, atomic symbol, mass, basis sets?.
current : list
Hashes encoding distinguishable non-coord characteristics of trial
molecule. Namely, atomic symbol, mass, basis sets?.
Returns
-------
iterator of tuples
"""
if sorted(ref) != sorted(current):
raise ValidationError(
"""ref and current can't map to each other.\n""" + "R: " + str(ref) + "\nC: " + str(current)
)
where = collections.defaultdict(list)
for iuq, uq in enumerate(ref):
where[uq].append(iuq)
cwhere = collections.defaultdict(list)
for iuq, uq in enumerate(current):
cwhere[uq].append(iuq)
connect = collections.OrderedDict()
for k in where:
connect[tuple(where[k])] = tuple(cwhere[k])
def filter_permutative(rgp, cgp):