Skip to content

Commit

Permalink
Merge pull request #85 from hjkgrp/code_quality
Browse files Browse the repository at this point in the history
Code quality
  • Loading branch information
ralf-meyer committed Jul 1, 2022
2 parents a8144b6 + c967a97 commit 5a017bf
Show file tree
Hide file tree
Showing 16 changed files with 627 additions and 417 deletions.
2 changes: 1 addition & 1 deletion molSimplify/Classes/atom3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def distancev(self, atom2):
dz = xyz[2]-point[2]
return [dx, dy, dz]

def ismetal(self, transition_metals_only=True):
def ismetal(self, transition_metals_only: bool = True) -> bool:
""" Identify whether an atom is a metal.
Parameters
Expand Down
27 changes: 13 additions & 14 deletions molSimplify/Classes/mol3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from math import sqrt
import numpy as np
import openbabel
from typing import List
from scipy.spatial import ConvexHull

from molSimplify.Classes.atom3D import atom3D
Expand Down Expand Up @@ -82,7 +83,7 @@ def __init__(self, name='ABC', loc='', use_atom_specific_cutoffs=False):
# Holder for molecular group
self.grps = False
# Holder for metals
self.metals = False
self.metals = None
# Conformation (empty string if irrelevant)
self.loc = loc
# Temporary list for storing conformations
Expand Down Expand Up @@ -213,7 +214,7 @@ def ACM_axis(self, idx1, idx2, axis, angle):
xyz = submol_to_move.getAtomCoords(i)
self.atoms[atidx].__init__(Sym=asym, xyz=xyz)

def addAtom(self, atom, index=None, auto_populate_BO_dict=True):
def addAtom(self, atom: atom3D, index: int = None, auto_populate_BO_dict: bool = True):
"""Adds an atom to the atoms attribute, which contains a list of
atom3D class instances.
Expand Down Expand Up @@ -279,7 +280,7 @@ def addAtom(self, atom, index=None, auto_populate_BO_dict=True):
self.natoms += 1
self.mass += atom.mass
self.size = self.molsize()
self.metal = False
self.metals = None

def assign_graph_from_net(self, path_to_net, return_graph=False):
"""
Expand Down Expand Up @@ -610,7 +611,7 @@ def convert2mol3D(self):
self.addAtom(atom3D(sym, pos))
# self.atoms = atom3D_list
# reset metal ID
self.metal = False
self.metals = None

def convert2OBMol(self, force_clean=False, ignoreX=False):
"""Converts mol3D class instance to OBMol class instance.
Expand Down Expand Up @@ -785,7 +786,7 @@ def combine(self, mol, bond_to_add=[], dirty=False):
cmol.OBMol.AddBond(i + 1, j + 1, int(jointBOMat[i][j]))
# reset graph
cmol.graph = []
self.metal = False
self.metals = None
return cmol

def coords(self):
Expand Down Expand Up @@ -957,7 +958,7 @@ def deleteatom(self, atomIdx):
if len(self.graph):
self.graph = np.delete(
np.delete(self.graph, atomIdx, 0), atomIdx, 1)
self.metal = False
self.metals = None
del (self.atoms[atomIdx])

def deleteatoms(self, Alist):
Expand Down Expand Up @@ -995,7 +996,7 @@ def deleteatoms(self, Alist):
del (self.atoms[h])
if len(self.graph):
self.graph = np.delete(np.delete(self.graph, Alist, 0), Alist, 1)
self.metal = False
self.metals = None

def freezeatom(self, atomIdx):
"""Set the freeze attribute to be true for a given atom3D class.
Expand Down Expand Up @@ -1259,12 +1260,10 @@ def findcloseMetal(self, atom0):
index of the nearest metal, or heaviest atom if no metal found.
"""
if not self.metals:
self.findMetal()

close_metal = False
mindist = 1000
for i in enumerate(self.metals):
for i in enumerate(self.findMetal()):
atom = self.getAtom(i)
if distance(atom.coords(), atom0.coords()) < mindist:
mindist = distance(atom.coords(), atom0.coords())
Expand All @@ -1277,7 +1276,7 @@ def findcloseMetal(self, atom0):
close_metal = i
return close_metal

def findMetal(self, transition_metals_only=True):
def findMetal(self, transition_metals_only: bool = True) -> List[int]:
"""Find metal(s) in a mol3D class.
Parameters
Expand All @@ -1291,15 +1290,15 @@ def findMetal(self, transition_metals_only=True):
List of indices of metal atoms in mol3D.
"""
if not self.metals:
if self.metals is None:
metal_list = []
for i, atom in enumerate(self.atoms):
if atom.ismetal(transition_metals_only=transition_metals_only):
metal_list.append(i)
self.metals = metal_list
return (self.metals)
return self.metals

def findAtomsbySymbol(self, sym):
def findAtomsbySymbol(self, sym: str) -> List[int]:
"""Find all elements with a given symbol in a mol3D class.
Parameters
Expand Down
39 changes: 19 additions & 20 deletions molSimplify/Informatics/decoration_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
##########################################



def decorate_ligand(args,ligand_to_decorate,decoration,decoration_index):
def decorate_ligand(args, ligand_to_decorate, decoration, decoration_index):
# structgen depends on decoration_manager, and decoration_manager depends on structgen.ffopt
# Thus, this import needs to be placed here to avoid a circular dependence
from molSimplify.Scripts.structgen import ffopt
Expand All @@ -40,30 +39,30 @@ def decorate_ligand(args,ligand_to_decorate,decoration,decoration_index):
#if args.debug:
# print 'decorating ligand'
lig = ligand_to_decorate
## reorder to ensure highest atom index
## reorder to ensure highest atom index
## removed first
sort_order = [i[0] for i in sorted(enumerate(decoration_index), key=lambda x:x[1])]
sort_order = sort_order[::-1] ## reverse
sort_order = sort_order[::-1] # reverse

decoration_index = [decoration_index[i] for i in sort_order]
decoration=[decoration[i] for i in sort_order]
decoration = [decoration[i] for i in sort_order]
if args.debug:
print(('decoration_index is ' + str(decoration_index)))
licores = getlicores()
if not isinstance(lig, mol3D):
lig,emsg = lig_load(lig,licores)
lig, emsg = lig_load(lig, licores)
else:
lig.convert2OBMol()
lig.charge = lig.OBMol.GetTotalCharge()
lig.convert2mol3D() # convert to mol3D
lig.convert2mol3D() # convert to mol3D

## create new ligand
merged_ligand = mol3D()
merged_ligand.copymol3D(lig)
for i,dec in enumerate(decoration):
print(('** decoration number ' +str(i)+ ' attaching ' + dec + ' at site '+str(decoration_index[i])
+ '**\n'))
dec,emsg = lig_load(dec,licores)
for i, dec in enumerate(decoration):
print(('** decoration number ' + str(i) + ' attaching ' + dec + ' at site '+str(decoration_index[i])
+ '**\n'))
dec, emsg = lig_load(dec, licores)
# dec.OBMol.AddHydrogens()
dec.convert2mol3D() # convert to mol3D
if args.debug:
Expand Down Expand Up @@ -123,16 +122,16 @@ def decorate_ligand(args,ligand_to_decorate,decoration,decoration_index):
theta,urot = rotation_params(r1,merged_ligand.getAtom(decoration_index[i]).coords(),r2)
theta = vecangle(vecdiff(r0,merged_ligand.getAtom(decoration_index[i]).coords()),urot)
dec = rotate_around_axis(dec,r0,urot,theta)
## get the default distance between atoms in question

## get the default distance between atoms in question
connection_neighbours = merged_ligand.getAtom(merged_ligand.getBondedAtomsnotH(decoration_index[i])[0])
new_atom = dec.getAtom(decind)
target_distance = connection_neighbours.rad + new_atom.rad
target_distance = connection_neighbours.rad + new_atom.rad
position_to_place = vecdiff(new_atom.coords(),connection_neighbours.coords())
old_dist= norm(position_to_place)
missing = (target_distance - old_dist)/2
dec.translate([missing*position_to_place[j] for j in [0,1,2]])

r1 = dec.getAtom(decind).coords()
u = vecdiff(r1,merged_ligand.getAtom(decoration_index[i]).coords())
dtheta = 2
Expand Down Expand Up @@ -166,7 +165,7 @@ def decorate_ligand(args,ligand_to_decorate,decoration,decoration_index):
BO_mat = merged_ligand.populateBOMatrix()
row_deleted = BO_mat[decoration_index[i]]
bonds_to_add = []

# find where to put the new bonds ->>> Issue here.
for j,els in enumerate(row_deleted):
if els > 0:
Expand All @@ -181,7 +180,7 @@ def decorate_ligand(args,ligand_to_decorate,decoration,decoration_index):
bonds_to_add.append((bond_partner,(merged_ligand.natoms-1)+dec.cat[0],els))
else:
bonds_to_add.append((bond_partner,merged_ligand.natoms-1,els))

## perfrom delete
merged_ligand.deleteatom(decoration_index[i])

Expand All @@ -196,11 +195,11 @@ def decorate_ligand(args,ligand_to_decorate,decoration,decoration_index):
merged_ligand.writexyz('merged' + str(i) + '.xyz')
merged_ligand.printxyz()
print('************')

merged_ligand.convert2OBMol()
merged_ligand,emsg = ffopt('MMFF94',merged_ligand,[],0,[],False,[],100)
BO_mat = merged_ligand.populateBOMatrix()
if args.debug:
merged_ligand.writexyz('merged_relaxed.xyz')
print(BO_mat)
print(BO_mat)
return(merged_ligand)
Loading

0 comments on commit 5a017bf

Please sign in to comment.