Skip to content

Commit

Permalink
Merge pull request #165 from hjkgrp/pormake_fragment
Browse files Browse the repository at this point in the history
Pormake fragment
  • Loading branch information
gianmarco-terrones committed Jul 23, 2023
2 parents d979f26 + 8f38386 commit 6bec129
Show file tree
Hide file tree
Showing 7 changed files with 380 additions and 132 deletions.
4 changes: 2 additions & 2 deletions molSimplify/Informatics/MOF/MOF_RAC_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@
"wiggle_room = 1\n",
"\n",
"get_primitive(f'{working_path}/{cif_name}.cif', f'{working_path}/{cif_name}_primitive.cif')\n",
"overlap_removal(f'{working_path}/{cif_name}_primitive.cif', f'{working_path}/{cif_name}_no_overlap.cif', wiggle_room=1)\n",
"solvent_removal(f'{working_path}/{cif_name}_no_overlap.cif', f'{working_path}/{cif_name}_no_solvent.cif', wiggle_room=1)\n",
"overlap_removal(f'{working_path}/{cif_name}_primitive.cif', f'{working_path}/{cif_name}_no_overlap.cif', wiggle_room=wiggle_room)\n",
"solvent_removal(f'{working_path}/{cif_name}_no_overlap.cif', f'{working_path}/{cif_name}_no_solvent.cif', wiggle_room=wiggle_room)\n",
"\n",
"# Now you can featurize your \"clean\" MOF.\n",
"if not os.path.isdir(f'{working_path}/RACs'):\n",
Expand Down
10 changes: 5 additions & 5 deletions molSimplify/Informatics/MOF/MOF_descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def make_MOF_SBU_RACs(SBUlist, SBU_subgraph, molcif, depth, name, cell_v, anchor
anchoring_atoms : set
Linker atoms that are bonded to a metal.
sbupath : str
Path of the folder to make a csv file in.
Path of the folder to make a csv file in and TXT files containing connection index information.
linkeranchors_superlist : list of set
Coordinating atoms of linkers. Number of sets is the number of linkers.
connections_list : list of lists of int
Expand Down Expand Up @@ -344,7 +344,7 @@ def make_MOF_linker_RACs(linkerlist, linker_subgraphlist, molcif, depth, name, c
cell_v : numpy.ndarray
The three Cartesian vectors representing the edges of the crystal cell. Shape is (3,3).
linkerpath : str
Path of the folder to make a csv file in.
Path of the folder to make a csv file in and TXT files containing connection index information.
linkeranchors_superlist : list of set
Coordinating atoms of linkers. Number of sets is the number of linkers.
Expand Down Expand Up @@ -561,11 +561,11 @@ def bond_information_write(linker_list, linkeranchors_superlist, adj_matrix, mol
# Finding the optimal unit cell shift
molcif_cart_coords = np.array([atom.coords() for atom in molcif.atoms])
fcoords=frac_coord(molcif_cart_coords,cell_v) # fractional coordinates
fcoords[con_atom]+=compute_image_flag(cell_v,fcoords[con_point],fcoords[con_atom]) # Shifting the connected metal
fcoords[con_atom]+=compute_image_flag(cell_v,fcoords[con_point],fcoords[con_atom]) # Shifting the connected metal to get it close to the connection point
ccoords = fractional2cart(fcoords, cell_v)
shifted_con_atom3D = atom3D(Sym=con_atom3D.symbol(), xyz=list(ccoords[con_atom,:]))

bond_len = shifted_con_atom3D.distance(con_point3D)
bond_len = shifted_con_atom3D.distance(con_point3D) # Bond length between the connected metal and the connection point.
con_atom_radius = COVALENT_RADII[shifted_con_atom3D.symbol()]
con_point_radius = COVALENT_RADII[con_point3D.symbol()]
relative_bond_len = bond_len / (con_atom_radius + con_point_radius)
Expand Down Expand Up @@ -820,7 +820,7 @@ def detect_1D_rod(SBU_list, molcif, allatomtypes, cell_v, logpath, name):
cart_coords_sbus_with_shifts = np.array(cart_coords_sbus_with_shifts) # Converting nested list to a numpy array

distance_mat = dist_mat_comp(cart_coords_sbus_with_shifts)
adj_matrix, _ = compute_adj_matrix(distance_mat,allatomtypes_sbus_with_shifts, handle_overlap=True) # Ignoring overlap
adj_matrix, _ = compute_adj_matrix(distance_mat, allatomtypes_sbus_with_shifts, handle_overlap=True) # Ignoring overlap

# For each connected component, see how long it is
adj_matrix = sparse.csr_matrix(adj_matrix)
Expand Down
2 changes: 1 addition & 1 deletion molSimplify/Informatics/MOF/MOF_fragment_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"log_list = []\n",
"for cif_file in os.listdir(featurization_directory+'/cif/'): # assumes that all of the cifs are in a directory called cif inside of the parent directory.\n",
" print('now on', cif_file) \n",
" return_code = make_MOF_fragments(featurization_directory+'/cif/'+cif_file,3,\n",
" return_code = make_MOF_fragments(featurization_directory+'/cif/'+cif_file,\n",
" path=featurization_directory+'/',\n",
" xyzpath=featurization_directory+'/xyz/'+cif_file.replace('cif','xyz'))\n",
" if return_code == None:\n",
Expand Down
2 changes: 1 addition & 1 deletion molSimplify/Informatics/MOF/MOF_functionalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

# This script can only functionalize C-H bonds.

def functionalize_MOF(cif_file, path2write, functional_group = 'F', functionalization_limit = 1, path_between_functionalizations = 3, additional_atom_offset = 0):
def functionalize_MOF(cif_file, path2write, functional_group='F', functionalization_limit=1, path_between_functionalizations=3, additional_atom_offset=0):
"""
Functionalizes the provided MOF and writes the functionalized version to a cif file.
Loops through the atoms of a MOF and functionalizes at suitable carbon atoms.
Expand Down
54 changes: 27 additions & 27 deletions molSimplify/Informatics/MOF/PBC_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def readcif(name):
positions = np.array(positions)
return cpar, atomtypes, positions

def compute_image_flag(cell,fcoord1,fcoord2):
def compute_image_flag(cell, fcoord1, fcoord2):
"""
Calculates how to shift fcoord2 to get it as close as possible to fcoord1. Shift by the crystal cell vectors.
Expand Down Expand Up @@ -156,7 +156,7 @@ def compute_image_flag(cell,fcoord1,fcoord2):
return supercells[image]


def linker_length(adjmat,anchors):
def linker_length(adjmat, anchors):
"""
Computes the shortest and longest paths between anchors in a linker.
Expand All @@ -170,8 +170,8 @@ def linker_length(adjmat,anchors):
Returns
-------
(min_length,max_length) : tuple of ints
min_length is the shortest path between two anchors in a linker.
max_length is the longest path between two anchors in a linker.
min_length is the shortest path length between two anchors in a linker.
max_length is the longest path length between two anchors in a linker.
"""
rows, cols = np.where(adjmat == 1)
Expand All @@ -188,7 +188,7 @@ def linker_length(adjmat,anchors):
min_length=min(len(nx.shortest_path(gr,i,j))-1,min_length)
return (min_length,max_length)

def slice_mat(mat,atoms):
def slice_mat(mat, atoms):
"""
Slice the matrix mat.
Expand All @@ -208,7 +208,7 @@ def slice_mat(mat,atoms):
return np.array(mat[np.ix_(list(atoms),list(atoms))])


def ligand_detect(cell,cart_coords,adj_mat,anchorlist):
def ligand_detect(cell, cart_coords, adj_mat, anchorlist):
"""
Calculates how to shift anchor atoms so that they are close to atoms bonded to them.
I imagine this tackles the issue of two bonded atoms being on different sides of a crystal cell.
Expand Down Expand Up @@ -253,7 +253,7 @@ def ligand_detect(cell,cart_coords,adj_mat,anchorlist):
return np.array(periodic_images)


def XYZ_connected(cell,cart_coords,adj_mat):
def XYZ_connected(cell, cart_coords, adj_mat):
"""
Calculate fractional coordinates of atoms for the specified connected component, shifted by cell vectors to make the coordinates close to each other.
Expand Down Expand Up @@ -304,7 +304,7 @@ def XYZ_connected(cell,cart_coords,adj_mat):
counter+=1
return fcoords

def writeXYZfcoords(filename,atoms,cell,fcoords):
def writeXYZfcoords(filename, atoms, cell, fcoords):
"""
Write an XYZ file using fractional coordinates.
Expand All @@ -331,7 +331,7 @@ def writeXYZfcoords(filename,atoms,cell,fcoords):
s="%10.2f %10.2f %10.2f"%(cart_coord[0],cart_coord[1],cart_coord[2])
fo.write("%s %s\n"%(atoms[i],s))

def writeXYZandGraph(filename,atoms,cell,fcoords,molgraph):
def writeXYZandGraph(filename, atoms, cell, fcoords, molgraph):
"""
Write the xyz file for the MOF structure, as well as the net file containing the MOF's graph.
Expand Down Expand Up @@ -364,7 +364,7 @@ def writeXYZandGraph(filename,atoms,cell,fcoords,molgraph):
np.savetxt(filename[:-4]+".net",molgraph,fmt="%i",delimiter=",",header=tmpstr) # Save a net file.


def returnXYZandGraph(filename,atoms,cell,fcoords,molgraph):
def returnXYZandGraph(filename, atoms, cell, fcoords, molgraph):
"""
TODO
Expand Down Expand Up @@ -396,7 +396,7 @@ def returnXYZandGraph(filename,atoms,cell,fcoords,molgraph):
np.savetxt(filename[:-4]+".net",molgraph,fmt="%i",delimiter=",",header=tmpstr)
return coord_list, molgraph

def writeXYZcoords(filename,atoms,coords):
def writeXYZcoords(filename, atoms, coords):
"""
Write an XYZ file using Cartesian coordinates.
Expand All @@ -421,7 +421,7 @@ def writeXYZcoords(filename,atoms,coords):
fo.write("%s %s\n"%(atoms[i],s))
return

def writeXYZcoords_withcomment(filename,atoms,coords,comment):
def writeXYZcoords_withcomment(filename, atoms, coords, comment):
"""
Write an XYZ file using Cartesian coordinates, with a comment included.
Expand All @@ -445,7 +445,7 @@ def writeXYZcoords_withcomment(filename,atoms,coords,comment):
fo.write("%s %s\n"%(atoms[i],s))
return

def write2file(pt,fn,st):
def write2file(pt, fn, st):
"""
Writes the string st to a file.
Expand All @@ -466,7 +466,7 @@ def write2file(pt,fn,st):
with open(pt+fn, "a") as fo:
fo.write(st)

def write_cif(fname,cellprm,fcoords,atom_labels):
def write_cif(fname, cellprm, fcoords, atom_labels):
"""
Writes a cif file with the provided parameters.
Expand Down Expand Up @@ -545,7 +545,7 @@ def cell_to_cellpar(cell, radians=False):
angles = [angle * np.pi / 180 for angle in angles]
return np.array(lengths + angles)

def findPaths(G,u,n):
def findPaths(G, u, n):
"""
Finds paths between atom u and atoms n bonds away.
Expand Down Expand Up @@ -611,7 +611,7 @@ def frac_coord(coord, cell):
invcell=np.linalg.inv(cell)
return np.dot(coord,invcell)

def compute_distance_matrix3(cell, cart_coords, num_cells = 1):
def compute_distance_matrix3(cell, cart_coords, num_cells=1):
"""
Computes the pairwise distances between all atom pairs in the crystal cell.
Expand Down Expand Up @@ -649,7 +649,7 @@ def compute_distance_matrix3(cell, cart_coords, num_cells = 1):
distance_matrix = np.min(dist, axis=-1) # Consider the distance between two atoms at the crystal cell shift where they are closest.
return distance_matrix

def position_nearest_atom(cell, cart_coords, index_of_interest, num_cells = 1):
def position_nearest_atom(cell, cart_coords, index_of_interest, num_cells=1):
"""
Computes the pairwise distances between all atoms in the crystal cell to the atom specified by index_of_interest; returns the position of the nearest atom.
Expand Down Expand Up @@ -702,7 +702,7 @@ def position_nearest_atom(cell, cart_coords, index_of_interest, num_cells = 1):

return nearest_position, nearest_index, shift_for_nearest_atom

def make_graph_from_nodes_edges(nodes,edges,attribs):
def make_graph_from_nodes_edges(nodes, edges, attribs):
"""
TODO
Expand Down Expand Up @@ -757,7 +757,7 @@ def mkcell(cpar):
vectors = np.array([a_vec, b_vec, c_vec])
return vectors

def make_supercell(cell,atoms,fcoords,exp_coeff):
def make_supercell(cell, atoms, fcoords, exp_coeff):
"""
TODO
Expand Down Expand Up @@ -797,7 +797,7 @@ def make_supercell(cell,atoms,fcoords,exp_coeff):
return supercell,superatoms,superfcoords


def compute_adj_matrix(distance_mat,allatomtypes,wiggle_room=1,handle_overlap=False):
def compute_adj_matrix(distance_mat, allatomtypes, wiggle_room=1, handle_overlap=False):
"""
Calculates what atoms are bonded to each other.
Expand All @@ -818,7 +818,7 @@ def compute_adj_matrix(distance_mat,allatomtypes,wiggle_room=1,handle_overlap=Fa
Returns
-------
sparse.csr_matrix(adj_matrix) : scipy.sparse.csr.csr_matrix
1 represents a bond, 0 represents no bond. Shape is (number of atoms, number of atoms).
Adjacency matrix. 1 represents a bond, 0 represents no bond. Shape is (number of atoms, number of atoms).
overlap_atoms : list
Indices of atoms that overlap with any atom of a lower index.
Expand Down Expand Up @@ -902,19 +902,19 @@ def get_closed_subgraph(linkers, SBUlist, adj_matrix):
Parameters
----------
linkers : set
linkers : set of int
Indices corresponding to atoms in the linkers (or SBUs; see the summary part of this docstring) of the MOF. The part of the matrix to analyze.
SBUlist : set
SBUlist : set of numpy.int64
Indices corresponding to atoms in the SBUs (or linkers) of the MOF. The part of the matrix to ignore.
adj_matrix : scipy.sparse.csr.csr_matrix
1 represents a bond, 0 represents no bond. Shape is (number of atoms, number of atoms).
Adjacency matrix. 1 represents a bond, 0 represents no bond. Shape is (number of atoms, number of atoms).
Returns
-------
linker_list : list of lists of ints
Each inner list is its own separate linker. The ints are the atom indices of that linker. Length is # of linkers.
Each inner list is its own separate linker (or SBU). The ints are the atom indices of that linker (or SBU). Length is # of linkers (or SBUs).
linker_subgraphlist : list of scipy.sparse.csr.csr_matrix
The atom connections in the linker subgraph. Length is # of linkers.
The atom connections in the linker (or SBU) subgraph. Length is # of linkers (or SBUs).
"""

Expand Down Expand Up @@ -985,7 +985,7 @@ def include_extra_shells(SBUlists, subgraphlists, molcif, adjmat):
SBUs.append(list(SBUset))
subgraphs.append(adjmat[np.ix_(list(SBUset),list(SBUset))])

return SBUs,subgraphs
return SBUs, subgraphs

def disorder_detector(name):
"""
Expand Down
2 changes: 1 addition & 1 deletion molSimplify/Informatics/MOF/cluster_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def get_MOF_descriptors(data, depth, path=False, xyzpath = False):
tmpstr = "Failed to featurize %s: large primitive cell\n"%(name)
pbc_funs.write2file(path, "/FailedStructures.log", tmpstr)
return None, None
distance_mat = pbc_funs.compute_distance_matrix2(cell_v, cart_coords)
distance_mat = pbc_funs.compute_distance_matrix3(cell_v, cart_coords)
try:
adj_matrix, _ = pbc_funs.compute_adj_matrix(distance_mat, allatomtypes)
except NotImplementedError:
Expand Down
Loading

0 comments on commit 6bec129

Please sign in to comment.