Code:
|
chemAtomVars = self.chemAtoms
chemAtomSets = set(self.chemComp.chemAtomSets)
# remove ChemAtomSets that do not have all atoms within ChemCompVar
for ca in self.chemComp.chemAtoms:
if isinstance(ca, ChemAtom) and ca not in chemAtomVars:
cas = ca.chemAtomSet
while cas in chemAtomSets:
chemAtomSets.remove(cas)
cas = cas.chemAtomSet
# remove ChemAtomSets with duplicate names
casByName = {}
for cas in chemAtomSets.copy():
name = cas.name
cas0 = casByName.get(name)
if cas0 is None:
casByName[name] = cas
else:
set1 = cas.chemAtomSets
if set1:
set0 = cas0.chemAtomSets
else:
set1 = cas.chemAtoms
set0 = cas0.chemAtoms
if set1 < set0:
casx = cas
if not set1:
raise ApiError("%s found ChemAtomSets %s and %s that are incompatible" % (self, cas, cas0))
elif set0 < set1:
casByName[name] = cas
casx = cas0
if not set0:
raise ApiError("%s found ChemAtomSets %s and %s that are incompatible" % (self, cas, cas0))
else:
raise ApiError("%s found ChemAtomSets %s and %s that are not subsets of each other" % (self, cas, cas0))
while casx in chemAtomSets:
chemAtomSets.remove(casx)
casx = casx.chemAtomSet
#
result = frozenset(chemAtomSets)
|