Skip to content

Commit

Permalink
Debugging coordination_correction_for_nonhaptic
Browse files Browse the repository at this point in the history
  • Loading branch information
choglass committed Jan 3, 2025
1 parent 253c2ff commit bc12822
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
4 changes: 4 additions & 0 deletions cell2mol/connectivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,9 @@ def split_group(original_group, conn_idx, final_ligand_indices, debug: int=0):
splitted_groups = []

if debug > 1: print(f"GROUP.SPLIT_GROUP: {conn_idx=}")
if debug > 1: print(f"GROUP.SPLIT_GROUP: {original_group.labels=}")
if debug > 1: print(f"GROUP.SPLIT_GROUP: {original_group.coord=}")
if debug > 1: print(f"GROUP.SPLIT_GROUP: {original_group.atoms=}")
conn_labels = extract_from_list(conn_idx, original_group.labels, dimension=1)
conn_coord = extract_from_list(conn_idx, original_group.coord, dimension=1)
conn_frac_coord = extract_from_list(conn_idx, original_group.frac_coord, dimension=1)
Expand All @@ -576,6 +579,7 @@ def split_group(original_group, conn_idx, final_ligand_indices, debug: int=0):
gr_atoms = extract_from_list(b, conn_atoms, dimension=1)
# Create Group Object
newgroup = group(gr_labels, gr_coord, gr_frac_coord, radii=gr_radii)
if debug > 1: print(f"GROUP.SPLIT_GROUP: {newgroup.labels=}")
# For debugging
newgroup.origin = "split_group"
# Define the GROUP as parent of the group. Bottom-Up hierarchy
Expand Down
17 changes: 10 additions & 7 deletions cell2mol/coordination_sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ def define_coordination_geometry (metal: object, coord_group: list, debug: int=0

symbols.append(metal.label)
positions.append(metal.coord)

if debug >= 2 : print(f"DEFINE_coordination_geometry: {metal.label} {metal.coord}")
print(f"DEFINE_coordination_geometry: {[group.formula for group in coord_group]}")
print(f"DEFINE_coordination_geometry: {[group.is_haptic for group in coord_group]}")
print(f"DEFINE_coordination_geometry: {[[a.label for a in group.atoms] for group in coord_group]}")

for group in coord_group:
if group.is_haptic == False:
for atom in group.atoms:
symbols.append(atom.label)
positions.append(atom.coord)
#if debug >= 2 : print("DEFINE_coordination_geometry:", atom.label, atom.coord)
if debug >= 2 : print("DEFINE_coordination_geometry:", atom.label, atom.coord)
else :
if debug >= 2 : print(f"DEFINE_coordination_geometry: {group.haptic_type=}")
#if debug >= 2 : print(f"DEFINE_coordination_geometry: {[atom.coord for atom in group.atoms]}")
Expand Down Expand Up @@ -372,8 +376,7 @@ def coordination_correction_for_nonhaptic(group: object, debug: int=0):

if debug > 0: print("Entering COORD_CORR_NONHAPTIC:")
if not hasattr(group,"metals"): group.get_connected_metals()


print(f"group: {[atom.label for atom in group.atoms]}")
# Pair each atom with its index in the original list
indexed_atoms = list(enumerate(group.atoms))

Expand All @@ -384,9 +387,6 @@ def coordination_correction_for_nonhaptic(group: object, debug: int=0):
sorted_atoms = [atom[1] for atom in sorted_indexed_atoms]
original_indices = [atom[0] for atom in sorted_indexed_atoms]

# Update the group's atoms list to the sorted atoms
group.atoms = sorted_atoms

## First Correction (former verify_connectivity)
conn_idx = []
final_ligand_indices = []
Expand Down Expand Up @@ -422,6 +422,7 @@ def coordination_correction_for_nonhaptic(group: object, debug: int=0):
# group.remove_atom(idx, debug=debug)

conn_idx = sorted(list(set(conn_idx)))

return group, conn_idx, final_ligand_indices


Expand Down Expand Up @@ -456,6 +457,8 @@ def coordination_correction_for_haptic (group: object, debug: int=0):
else :
conn_idx.append(idx)
final_ligand_indices.append(atom.get_parent_index("ligand"))

conn_idx = sorted(list(set(conn_idx)))

return group, conn_idx, final_ligand_indices
#######################################################
27 changes: 19 additions & 8 deletions cell2mol/missingH.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_missingH(Z, valence, center, charge, edges, points):


##############################
def get_missingH_from_adjacency(Z, center, points):
def get_missingH_from_adjacency(Z, center, points, bonded_atom_labels):
missingH = False

num_adj_atoms = len(points)
Expand All @@ -52,9 +52,16 @@ def get_missingH_from_adjacency(Z, center, points):

# Evaluates geometry
val_e = num_adj_atoms
if val_e < shapeval: missingH = True
if val_e == shapeval: missingH = False
if val_e > shapeval: missingH = True
if val_e == 1:
if bonded_atom_labels[0] == "O" or bonded_atom_labels[0] == "N": # CO or CN
missingH = False
else:
shapeval = "more than 1 (possibly 3 in missing H in methyl)"
missingH = True
elif val_e == shapeval :
missingH = False
else :
missingH = True

# Saves report
# print(f"Summary of facts:\n -Atom has {num_adj_atoms} adjacent atoms \n -with total bond order {sum_bond_order} \n -arranged in a shape {shape} that suggests coordination {shapeval} \n -with formal charge {charge} \n -valence {valence} and {lonepairs} lone pairs.")
Expand Down Expand Up @@ -117,6 +124,8 @@ def check_missingH(refmoleclist: list, debug: int=0):
if ref.natoms == 1 and "O" in ref.labels:
Missing_H_in_CoordWater = True
if debug >= 1: print(f"WARNING found isolated O atom in the cell. This tends to be a water with missing H, so stopping")
elif ref.formula == "CO" or ref.formula == "CN":
pass
else:
for kdx, a in enumerate(ref.atoms):
if not hasattr(a,"adjacency"): continue
Expand All @@ -128,13 +137,13 @@ def check_missingH(refmoleclist: list, debug: int=0):
bonded_atom_coord.append(ref.coord[adj])
bonded_atom_labels.append(ref.atoms[adj].label)
if debug >= 2: print("Adjacency", a.adjacency, bonded_atom_labels)
ismissingH, report = get_missingH_from_adjacency(a.atnum, a.coord, bonded_atom_coord)
ismissingH, report = get_missingH_from_adjacency(a.atnum, a.coord, bonded_atom_coord, bonded_atom_labels)
if ismissingH:
for label, coord in zip(bonded_atom_labels, bonded_atom_coord):
print("Dist", f"{a.label}-{label}", get_dist(a.coord, coord))
if ismissingH:
if debug >= 1: print("")
if debug >= 1: print(f"WARNING in Missing H function for: {ref.type}, {idx}, {ref.labels}")
if debug >= 1: print(f"WARNING in Missing H function for: {ref.type}, molecule index {idx}, {ref.formula}")
if debug >= 1: print(f"C Atom {kdx} {a.get_parent_index('molecule')} has missing H atoms")
if debug >= 1: print(report)
Missing_H_in_C = True
Expand All @@ -146,6 +155,8 @@ def check_missingH(refmoleclist: list, debug: int=0):
Missing_H_in_CoordWater = True
if debug >= 1: print("")
if debug >= 1: print("WARNING in Missing H function for ligand",lig.natoms,lig.labels)
elif lig.formula == "CO" or lig.formula == "CN":
pass
else:
for kdx, a in enumerate(lig.atoms):
if a.label == "C" and a.mconnec == 0:
Expand All @@ -156,10 +167,10 @@ def check_missingH(refmoleclist: list, debug: int=0):
bonded_atom_coord.append(lig.get_parent("molecule").coord[adj])
bonded_atom_labels.append(lig.get_parent("molecule").atoms[adj].label)
if debug >= 2: print("Adjacency", a.adjacency, bonded_atom_labels)
ismissingH, report = get_missingH_from_adjacency(a.atnum, a.coord, bonded_atom_coord)
ismissingH, report = get_missingH_from_adjacency(a.atnum, a.coord, bonded_atom_coord, bonded_atom_labels)
if ismissingH:
if debug >= 1: print("")
if debug >= 1: print(f"WARNING in Missing H function for: {ref.type}, {idx}, {jdx}, {lig.labels}")
if debug >= 1: print(f"WARNING in Missing H function for: {ref.type}, molecule index {idx}, ligand index {jdx}, {lig.formula}")
if debug >= 1: print(f"C Atom {kdx} {a.get_parent_index('molecule')} has missing H atoms")
if debug >= 1: print(report)
Missing_H_in_C = True
Expand Down
3 changes: 2 additions & 1 deletion cell2mol/refcell.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def process_refcell(input_path, name, current_dir, debug=0):

# Finalize and save the reference cell object if no errors
if refcell.error_case == 0:
get_unique_species_in_reference(refcell, debug)
pass
# get_unique_species_in_reference(refcell, debug)
else:
print(f"Error occurred in processing reference cell: error case {refcell.error_case}")
refcell.save(ref_cell_fname)
Expand Down

0 comments on commit bc12822

Please sign in to comment.