Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Harikrishna Sahu authored and Harikrishna Sahu committed Feb 23, 2022
1 parent 6a111d7 commit 5d86ecf
Show file tree
Hide file tree
Showing 8 changed files with 5,707 additions and 42 deletions.
4,973 changes: 4,973 additions & 0 deletions Colab_notebook/psp_Colab_notebook.ipynb

Large diffs are not rendered by default.

635 changes: 635 additions & 0 deletions Colab_notebook/psp_colab_notebook.py

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions psp/AmorphousBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,13 @@ def get_opls(self, output_fname='amor_opls.lmps'):

try:
print("LigParGen working on {}.pdb".format(output_prefix))
Converter.convert(pdb=os.path.join(self.OutDir_xyz, output_prefix + '.pdb'),
resname=output_prefix, charge=0, opt=0, outdir='.')
Converter.convert(
pdb=os.path.join(self.OutDir_xyz, output_prefix + '.pdb'),
resname=output_prefix,
charge=0,
opt=0,
outdir='.',
)
os.rename(lig_output_fname, data_fname)
except BaseException:
print('problem running LigParGen for {}.pdb.'.format(output_prefix))
Expand Down
27 changes: 17 additions & 10 deletions psp/AmorphousBuilder2.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(
self.SMILES_col = SMILES_col
self.LeftCap = LeftCap
self.RightCap = RightCap
self.Nunits_col = Nunits_col
self.Nunits_col = Nunits_col
self.Mwt_col = Mwt_col
self.define_BB_col = define_BB_col
self.Loop_col = Loop_col
Expand Down Expand Up @@ -83,9 +83,9 @@ def Build(self):
box_type_ = "Rectangular"
print(self.rdkit_conf_param)

# NumConf = 1
# if 'numConfs' in self.rdkit_conf_param.keys():
# NumConf = int(self.rdkit_conf_param['numConfs'])
# NumConf = 1
# if 'numConfs' in self.rdkit_conf_param.keys():
# NumConf = int(self.rdkit_conf_param['numConfs'])

print(
"\n",
Expand Down Expand Up @@ -133,16 +133,16 @@ def Build(self):
Loop_col=self.Loop_col,
OutDir=self.OutDir_xyz,
NCores=self.NCores,
Output=['xyz','pdb','smi'],
Output=['xyz', 'pdb', 'smi'],
seed=self.seed,
rdkit_conf_param=self.rdkit_conf_param,
)
results = mol.Build()

path_to_file = self.OutDir_xyz + df[self.ID_col].values[0] + '_C1.smi'
with open(path_to_file) as f:
contents = f.readlines()
smi = contents[0].split()[0]
contents = f.readlines()
smi = contents[0].split()[0]

results['SMILES'] = smi
xyz_gen_pd = pd.concat([xyz_gen_pd, results])
Expand All @@ -158,7 +158,9 @@ def Build(self):
# Get a list of filenames for XYZ coordinates
XYZ_list_ind = glob.glob(self.OutDir_xyz + str(row[self.ID_col]) + "*.pdb")
XYZ_list.append(XYZ_list_ind)
NumConf = len(XYZ_list_ind) # Get number of conformer generated for each system
NumConf = len(
XYZ_list_ind
) # Get number of conformer generated for each system

# Get number of molecules for each conformer of molecules
NMol_list += [int(row[self.NumMole] / NumConf)] * NumConf
Expand Down Expand Up @@ -342,8 +344,13 @@ def get_opls(self, output_fname='amor_opls.lmps'):

try:
print("LigParGen working on {}.pdb".format(output_prefix))
Converter.convert(pdb=os.path.join(self.OutDir_xyz, output_prefix + '.pdb'),
resname=output_prefix, charge=0, opt=0, outdir='.')
Converter.convert(
pdb=os.path.join(self.OutDir_xyz, output_prefix + '.pdb'),
resname=output_prefix,
charge=0,
opt=0,
outdir='.',
)
os.rename(lig_output_fname, data_fname)
except BaseException:
print('problem running LigParGen for {}.pdb.'.format(output_prefix))
Expand Down
6 changes: 3 additions & 3 deletions psp/CopolymerBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(
LeftCapSMI_col='LeftCap',
RightCapSMI_col='RightCap',
Nunits_col='Nunits', # list of numbers (ratios); follow the order of building blocks
Tunits_col='Tunits', # Total number units in a polymer chain
Tunits_col='Tunits', # Total number units in a polymer chain
Mwt_col='Mwt_polymer', # if > 0, then Nunits will be determined from molar mass of BB and Mwt
Copoly_type_col='Copoly_type', # 'homo', # homo, alternating, block, graft, random
define_BB_col='define_BB',
Expand Down Expand Up @@ -55,8 +55,8 @@ def __init__(
self.IrrStruc = IrrStruc
self.GAFF2_atom_typing = GAFF2_atom_typing
self.Subscript = Subscript
self.seed = seed,
self.rdkit_conf_param = rdkit_conf_param,
self.seed = (seed,)
self.rdkit_conf_param = (rdkit_conf_param,)

# list of molecules name and CORRECT/WRONG
def Build(self):
Expand Down
12 changes: 3 additions & 9 deletions psp/CrystalBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ def BuildCrystal(self):
self.NumCandidate,
NCores_opt,
)
for VaspInp in tqdm(
self.VaspInp_list, desc="Building models ..."
)
for VaspInp in tqdm(self.VaspInp_list, desc="Building models ...")
)
else:
result = Parallel(n_jobs=NCores)(
Expand All @@ -148,9 +146,7 @@ def BuildCrystal(self):
self.NumCandidate,
NCores_opt,
)
for VaspInp in tqdm(
self.VaspInp_list, desc="Building models ..."
)
for VaspInp in tqdm(self.VaspInp_list, desc="Building models ...")
)

output = []
Expand Down Expand Up @@ -569,9 +565,7 @@ def CrystalBuilderMain(
# digits = bd.len_digit_number(NSamples ** 8)

count = 0
for i in tqdm(
samp[0], desc=VaspInp + " Generating models"
): # Second poly
for i in tqdm(samp[0], desc=VaspInp + " Generating models"): # Second poly
for j in samp[2]: # Second poly
for k in samp[1]: # Second poly
for aX in samp[3]: # Second poly
Expand Down
83 changes: 67 additions & 16 deletions psp/PSP2_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ def build_copoly(
define_BB_ = df_smiles[df_smiles[ID] == unit_name][define_BB].values[0]
if define_BB_ == 'R':
define_BB_ = list(smiles_dict.keys())
define_BB_.sort() # to ensure it matches with Nunits
define_BB_.sort() # to ensure it matches with Nunits

# If Mwt is provided, update Nunits
if Mwt != 0:
Expand All @@ -491,7 +491,7 @@ def build_copoly(
Nunits = update_Nunits

elif Tunits != 0:
Nunits = [round(Tunits*item) for item in Nunits]
Nunits = [round(Tunits * item) for item in Nunits]

random_seq = []
for i in range(len(define_BB_)):
Expand Down Expand Up @@ -519,11 +519,11 @@ def build_copoly(

if Mwt != 0:
for i in range(len(unit_list)):
update_Nunits.append(
update_Nunits.append(
round((Nunits[i] * Mwt) / OBMol_Mwt_dict[unit_list[i]][0])
)
elif Tunits != 0:
update_Nunits = [round(Tunits*item) for item in Nunits]
update_Nunits = [round(Tunits * item) for item in Nunits]

BB_count = []
for key in blocks_dict:
Expand All @@ -532,7 +532,9 @@ def build_copoly(
BB_each_count.append(
update_Nunits[(ord(j) - 65)] * (1 / total_units.count(j))
)
BB_count.append(round(statistics.mean(BB_each_count))) # Take average
BB_count.append(
round(statistics.mean(BB_each_count))
) # Take average
Nunits = BB_count

else:
Expand All @@ -555,21 +557,41 @@ def build_copoly(
OBMol = optimize_geometry(OBMol)
result = 'SUCCESS'

if result == 'SUCCESS': # Fix seed issue

if result == 'SUCCESS': # Fix seed issue

list_conf = OBmolConfSearchRDkit(unit_name, OBMol, 'work_dir/', rdkit_conf_param)
list_conf = OBmolConfSearchRDkit(
unit_name, OBMol, 'work_dir/', rdkit_conf_param
)

n=1 # Conformer sl number
n = 1 # Conformer sl number
for OBconf in list_conf:
GenOutput(
unit_name, OBconf, output_files, out_dir, Inter_Mol_Dis, GAFF2_atom_typing, n
unit_name,
OBconf,
output_files,
out_dir,
Inter_Mol_Dis,
GAFF2_atom_typing,
n,
)
n += 1
return unit_name, result


def OBmolConfSearchRDkit(unitname, mol, work_dir, rdkit_conf_param, Nconf=1, NCores_opt=0, seed=None, NAttempt=10000, BasicKnowledge=True, RmsThresh=0.1, ExpTorsionAnglePref=True, enforceChirality=False):
def OBmolConfSearchRDkit(
unitname,
mol,
work_dir,
rdkit_conf_param,
Nconf=1,
NCores_opt=0,
seed=None,
NAttempt=10000,
BasicKnowledge=True,
RmsThresh=0.1,
ExpTorsionAnglePref=True,
enforceChirality=False,
):
list_conf = []

# Set parameters
Expand Down Expand Up @@ -614,8 +636,17 @@ def OBmolConfSearchRDkit(unitname, mol, work_dir, rdkit_conf_param, Nconf=1, NCo
m1 = Chem.MolFromMolFile(path_ini_geo)
m2 = Chem.AddHs(m1)
if m2 is not None:
cids = AllChem.EmbedMultipleConfs(m2, numConfs=Nconf, numThreads=NCores_opt, randomSeed=seed,
maxAttempts=NAttempt, useBasicKnowledge=BasicKnowledge, pruneRmsThresh=RmsThresh, useExpTorsionAnglePrefs=ExpTorsionAnglePref, enforceChirality=enforceChirality)
cids = AllChem.EmbedMultipleConfs(
m2,
numConfs=Nconf,
numThreads=NCores_opt,
randomSeed=seed,
maxAttempts=NAttempt,
useBasicKnowledge=BasicKnowledge,
pruneRmsThresh=RmsThresh,
useExpTorsionAnglePrefs=ExpTorsionAnglePref,
enforceChirality=enforceChirality,
)
if len(cids) < 1:
list_conf.append(mol)
print("Couldn't generate multiple conformers ............")
Expand All @@ -638,12 +669,19 @@ def OBmolConfSearchRDkit(unitname, mol, work_dir, rdkit_conf_param, Nconf=1, NCo
return list_conf
return list_conf


def GenOutput(
unitname, OBMol, list_output, output_dir, Inter_Mol_Dis, GAFF2_atom_typing, n
):
# Genrate an XYZ file
obConversion.SetOutFormat('xyz')
OBMol.SetTitle("Generated by OpenBabel@PSP --" + " Conformer: " + str(n) + " -- Output format: " + 'xyz')
OBMol.SetTitle(
"Generated by OpenBabel@PSP --"
+ " Conformer: "
+ str(n)
+ " -- Output format: "
+ 'xyz'
)
out_filename = os.path.join(output_dir, unitname + '_C' + str(n) + '.xyz')
obConversion.WriteFile(OBMol, out_filename)

Expand Down Expand Up @@ -699,7 +737,13 @@ def GenOutput(
def gen_opls_data(unit_name, OBMol, output_dir, unitname, n):
# Generate a pdb file
obConversion.SetOutFormat('pdb')
OBMol.SetTitle("Generated by OpenBabel@PSP --" + " Conformer: " + str(n) + " -- Output format: " + 'pdb')
OBMol.SetTitle(
"Generated by OpenBabel@PSP --"
+ " Conformer: "
+ str(n)
+ " -- Output format: "
+ 'pdb'
)
out_filename_pdb = os.path.join(output_dir, unitname + '_C' + str(n) + '.pdb')
obConversion.WriteFile(OBMol, out_filename_pdb)

Expand All @@ -718,7 +762,13 @@ def gen_opls_data(unit_name, OBMol, output_dir, unitname, n):
def GenOutputOB(unitname, OBMol, list_OB_output, output_dir, n):
for i in list_OB_output:
obConversion.SetOutFormat(i)
OBMol.SetTitle("Generated by OpenBabel@PSP --" + " Conformer: " + str(n) + " -- Output format: " + i)
OBMol.SetTitle(
"Generated by OpenBabel@PSP --"
+ " Conformer: "
+ str(n)
+ " -- Output format: "
+ i
)
out_filename = os.path.join(output_dir, unitname + '_C' + str(n) + '.' + i)
obConversion.WriteFile(OBMol, out_filename)

Expand Down Expand Up @@ -759,6 +809,7 @@ def combine_A_ntimes(OBMol, first, second, n):

return main_obmol, first, second


def combine_A_ntimes_pl(OBMol, first, second, n):
builder = ob.OBBuilder()
builder.SetKeepRings()
Expand Down
4 changes: 2 additions & 2 deletions psp/PSP_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2711,8 +2711,8 @@ def gen_conf_xyz_vasp(
maxAttempts=NAttempt,
)

# if len(cids) > 0:
# break
if len(cids) > 0:
break
n = 0
for cid in cids:
n += 1
Expand Down

0 comments on commit 5d86ecf

Please sign in to comment.