Skip to content

Commit 790ca7e

Browse files
authored
Merge pull request #46 from rsdefever/futurize-scripts
Add python2/python3 support to scripts with futurize
2 parents 26d06d5 + 686c9ed commit 790ca7e

3 files changed

Lines changed: 117 additions & 84 deletions

File tree

Documentation/Tex/utilities.tex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
\chapter{Utilities}
22

3+
All Python utility scripts require the \texttt{future} package
4+
and \texttt{numpy} package, as listed in \texttt{requirements-py.txt}.
5+
Both packages are available via \texttt{pip} or \texttt{conda}.
6+
7+
Please note that \texttt{python2} support is deprecated and may be removed
8+
in a future release.
9+
310
%\section{Input File GUI}
411
%\label{sec:GUI}
512
%This script provides a graphical user interface (GUI) with which the user
@@ -52,7 +59,7 @@ \chapter{Utilities}
5259
\section{Generate a Molecular Connectivity File}
5360
\label{sec:mcfgen}
5461

55-
The script \texttt{mcf\_gen.py} is a tool that aims to ease the setup of molecular connectivity files from scratch (see section \ref{sec:MCF_File} to learn more
62+
The script \texttt{mcfgen.py} is a tool that aims to ease the setup of molecular connectivity files from scratch (see section \ref{sec:MCF_File} to learn more
5663
about MCFs), as the generation of these files by hand can be error prone.
5764
In this section, a pentane MCF will be generated to demonstrate the use of this tool.
5865
The Transferable Potentials for Phase Equilibria (TraPPE) force field will be used to represent the pentane molecular interactions.

Scripts/Frag_Library_Setup/library_setup.py

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@
4545
#*******************************************************************************
4646
# IMPORT MODULES
4747
#*******************************************************************************
48+
from __future__ import print_function
49+
from builtins import input
50+
from builtins import str
51+
from builtins import range
4852
import sys, math, subprocess, os, linecache, argparse, re
53+
import warnings
4954

5055
#*******************************************************************************
5156
# ARGUMENT PARSE
@@ -131,10 +136,10 @@ def cml_to_pdb(infilename):
131136
if '</atomArray>' in line:
132137
cml_end_atom = line_nbr + 1
133138

134-
for i in xrange(cml_start_atom+1, cml_end_atom):
139+
for i in range(cml_start_atom+1, cml_end_atom):
135140
cml_atom_info.append(re.findall('"([^"]*)"',linecache.getline(infilename, i)))
136141

137-
for i in xrange(cml_start_bonds+1, cml_end_bonds):
142+
for i in range(cml_start_bonds+1, cml_end_bonds):
138143
cml_bond_info.append(re.findall('"([^"]*)"',linecache.getline(infilename, i))[0].split())
139144

140145
temp=[]
@@ -254,9 +259,9 @@ def read_pdb(pdb_file):
254259
normal = '\033[0m'
255260
gcmc_flag = 0
256261

257-
print bold+"\n********************** Cassandra Setup *********************\n"+normal
258-
print bold+"Cassandra location: "+normal+cassandra_path
259-
print bold+"Scanning input file"+normal
262+
print(bold+"\n********************** Cassandra Setup *********************\n"+normal)
263+
print(bold+"Cassandra location: "+normal+cassandra_path)
264+
print(bold+"Scanning input file"+normal)
260265

261266
#Locate line number for the following keywords:
262267
# Nbr_Species
@@ -314,23 +319,23 @@ def read_pdb(pdb_file):
314319
else:
315320
vdw_style = []
316321
charge_style = []
317-
for i in xrange(1,nbr_boxes+1):
322+
for i in range(1,nbr_boxes+1):
318323
vdw_style.append(linecache.getline(input_file, vdw_style_line+i).split()[0])
319324
if charge_style_line:
320325
charge_style.append(linecache.getline(input_file, charge_style_line+i).split()[0])
321326
if vdw_style[1] != vdw_style[0]:
322-
vdw_style = raw_input("VDW_Style for boxes don't match. " +
327+
vdw_style = input("VDW_Style for boxes don't match. " +
323328
"Enter the VDW_Style to use (" + vdw_style[0] + "/" +
324329
vdw_style[i-1] + "):")
325330
else:
326331
vdw_style = vdw_style[0]
327332
if vdw_style != 'LJ' and vdw_style != 'lj' and \
328333
vdw_style != 'Mie' and vdw_style != 'mie':
329-
print "Only 'LJ' and 'Mie' VDW_Styles are supported"
334+
print("Only 'LJ' and 'Mie' VDW_Styles are supported")
330335
quit()
331336
if charge_style_line:
332337
if charge_style[1] != charge_style[0]:
333-
charge_style = raw_input("Charge_Style for boxes don't match. " +
338+
charge_style = input("Charge_Style for boxes don't match. " +
334339
"Enter the Charge_Style to use (" + charge_style[0] + "/" +
335340
charge_style[i-1] + "):")
336341
else:
@@ -341,12 +346,12 @@ def read_pdb(pdb_file):
341346

342347
#Obtain number of species
343348
nbr_species = int(linecache.getline(input_file,nbr_species_line+1))
344-
print bold+" Number of species found: " + normal + str(nbr_species)
349+
print(bold+" Number of species found: " + normal + str(nbr_species))
345350

346351
#Intra_Scaling
347352
vdw_scaling = []
348353
charge_scaling = []
349-
for i in xrange(1,nbr_species+1):
354+
for i in range(1,nbr_species+1):
350355
if intra_scaling_line:
351356
if charge_style_line and charge_style == 'coul':
352357
vdw_scaling.append(linecache.getline(input_file,intra_scaling_line+2*i-1))
@@ -384,15 +389,15 @@ def read_pdb(pdb_file):
384389

385390
#Look for MCF files
386391
mcf_files = []
387-
for i in xrange(0,nbr_species):
392+
for i in range(0,nbr_species):
388393
mcf_files.append(linecache.getline(input_file,molec_files_line+i+1).split()[0])
389-
print bold+" The MCF file number " + str(i+1) + " is: " + normal + mcf_files[i]
394+
print(bold+" The MCF file number " + str(i+1) + " is: " + normal + mcf_files[i])
390395

391396
#Open the MCF files to record where atom, bond and fragment info is
392397
line_where_atom_info = []
393398
line_where_bond_info = []
394399
line_where_fragment_info = []
395-
for i in xrange(0,len(mcf_files)):
400+
for i in range(0,len(mcf_files)):
396401
current_mcf = open(mcf_files[i],'r')
397402
for line_number_mcf, line_mcf in enumerate(current_mcf):
398403
if not line_mcf.strip():
@@ -411,11 +416,11 @@ def read_pdb(pdb_file):
411416
which_atoms_are_ring = []
412417
temp_list = []
413418
nbr_atoms = []
414-
for i in xrange(0, nbr_species):
419+
for i in range(0, nbr_species):
415420
nbr_atoms.append(int(linecache.getline(mcf_files[i],
416421
line_where_atom_info[i]+1).split()[0]))
417422
atom_type_list[i] = [0]*nbr_atoms[i]
418-
for j in xrange(0,nbr_atoms[i]):
423+
for j in range(0,nbr_atoms[i]):
419424
atom_type = linecache.getline(mcf_files[i],
420425
line_where_atom_info[i]+2+j).split()[1]
421426
#remove old '_s?' flags if present
@@ -435,12 +440,12 @@ def read_pdb(pdb_file):
435440
#Find how many fragments there are and get a list of them.
436441
nbr_fragments = []
437442
fragment_list = []
438-
for i in xrange(0, nbr_species):
443+
for i in range(0, nbr_species):
439444
nbr_fragments.append(int(linecache.getline(mcf_files[i],
440445
line_where_fragment_info[i]+1).split()[0]))
441-
print bold+" Species "+str(i+1)+" has "+str(nbr_fragments[i])+ " fragments" + normal
446+
print(bold+" Species "+str(i+1)+" has "+str(nbr_fragments[i])+ " fragments" + normal)
442447
temp_list=[]
443-
for j in xrange(0,nbr_fragments[i]):
448+
for j in range(0,nbr_fragments[i]):
444449
temp_list.append(linecache.getline(mcf_files[i],
445450
line_where_fragment_info[i]+2+j).split()[2:])
446451
fragment_list.append(temp_list)
@@ -466,9 +471,9 @@ def read_pdb(pdb_file):
466471
temp_exoring = []
467472

468473
if any([any(has_ring) for has_ring in fragment_has_ring]):
469-
print bold+"Molecules with rings found. These are:" + normal
474+
print(bold+"Molecules with rings found. These are:" + normal)
470475
for i,species in enumerate(fragment_has_ring):
471-
print bold+"Species "+str(i+1)+" has "+str(sum(species))+ " rings."+normal
476+
print(bold+"Species "+str(i+1)+" has "+str(sum(species))+ " rings."+normal)
472477

473478
#We know what fragments are ring for each species. Copy only those config_files
474479
#for the species that have rings
@@ -489,16 +494,16 @@ def read_pdb(pdb_file):
489494
temperature = float(linecache.getline(input_file,temp_line+1).split()[0])
490495

491496
#Rewrite the MCF files to append an '_s1' to the atom type
492-
for i in xrange(0,len(mcf_files)):
497+
for i in range(0,len(mcf_files)):
493498
current_mcf = open(mcf_files[i],'r')
494499
new_mcf_file = open(mcf_files[i]+"temp",'w')
495500
total_lines = len(current_mcf.readlines())
496501
current_mcf.close()
497502
stride = 0
498-
for line_number in xrange(1,total_lines+1):
503+
for line_number in range(1,total_lines+1):
499504
if line_number + stride > line_where_atom_info[i]+1 and \
500505
line_number + stride <= line_where_atom_info[i]+1 + nbr_atoms[i]:
501-
for j in xrange(0,nbr_atoms[i]):
506+
for j in range(0,nbr_atoms[i]):
502507
this_line = linecache.getline(mcf_files[i],line_number+j+stride).split()
503508
this_line[1] = atom_type_list[i][j]
504509
new_mcf_file.write(' '.join(this_line)+'\n')
@@ -522,31 +527,31 @@ def read_pdb(pdb_file):
522527
#/species?/fragments
523528
#/species?/frag?
524529

525-
for i in xrange(0, nbr_species):
530+
for i in range(0, nbr_species):
526531

527-
for j in xrange(0,nbr_fragments[i]):
532+
for j in range(0,nbr_fragments[i]):
528533
os.system("mkdir -p species"+str(i+1)+"/frag"+str(j+1))
529534

530535
if i not in pdb_without_conect:
531536
os.system("mkdir -p species"+str(i+1)+"/fragments/")
532537

533538
#Now, create input files for fragment MCF generation
534-
for i in xrange(0, nbr_species):
539+
for i in range(0, nbr_species):
535540

536541
if i in pdb_without_conect:
537-
print "\n\n" + bold + "MCF generation file not created for species "+str(i+1)+normal
542+
print("\n\n" + bold + "MCF generation file not created for species "+str(i+1)+normal)
538543
if nbr_fragments[i] == 0:
539-
print bold+"No fragment configuration needed."+normal
544+
print(bold+"No fragment configuration needed."+normal)
540545
else:
541-
print bold+"Fragment configuration will be taken from PDB file."+normal
546+
print(bold+"Fragment configuration will be taken from PDB file."+normal)
542547
continue
543548

544549
if nbr_atoms[i] >= 3:
545550
for element in files_ring_to_copy:
546551
if str(i) in element[0]:
547552
os.system("cp "+element[1]+" species"+str(i+1)+"/fragments/molecule.pdb")
548553

549-
print "\n\n"+bold+"Creating input MCF generation file for species "+str(i+1)+" "+normal
554+
print("\n\n"+bold+"Creating input MCF generation file for species "+str(i+1)+" "+normal)
550555
input_mcf_gen = open("species"+str(i+1)+"/fragments/species"+str(i+1)+"_mcf_gen.inp",'w')
551556
input_mcf_gen.write("# Run_Name\nspecies"+str(i+1)+"_mcf_gen")
552557
input_mcf_gen.write("\n\n")
@@ -570,7 +575,7 @@ def read_pdb(pdb_file):
570575
input_mcf_gen.write("# Box_Info\n1\nCUBIC\n30.0 30.0 30.0\n\nEND")
571576
input_mcf_gen.close()
572577

573-
print bold+"Running Cassandra to generate MCF files"+normal
578+
print(bold+"Running Cassandra to generate MCF files"+normal)
574579
sys.stdout.flush()
575580
os.chdir('./species'+str(i+1)+'/fragments/')
576581
subprocess.call([cassandra_path,'species'+str(i+1)+'_mcf_gen.inp'])
@@ -580,7 +585,7 @@ def read_pdb(pdb_file):
580585
#Test if fragment and/or ring is rigid
581586
fragment_is_rigid = [] # Boolean entry for each i,j
582587
ring_is_rigid = [] # frag_id's for each frag that has a rigid ring
583-
for i in xrange(0, nbr_species):
588+
for i in range(0, nbr_species):
584589

585590
if nbr_atoms[i] < 3:
586591

@@ -595,7 +600,7 @@ def read_pdb(pdb_file):
595600
else:
596601
temp_rigid = []
597602
temp_ring_rigid = []
598-
for j in xrange(0,nbr_fragments[i]):
603+
for j in range(0,nbr_fragments[i]):
599604
#Read mcf to see how many angles are fixed
600605
frag_atoms_are_ring = []
601606
angle_parms = {}
@@ -606,14 +611,14 @@ def read_pdb(pdb_file):
606611
while line:
607612
if "# Atom_Info" in line:
608613
nbr_frag_atoms = int(frag_mcf.readline().split()[0])
609-
for k in xrange(0,nbr_frag_atoms):
614+
for k in range(0,nbr_frag_atoms):
610615
line = frag_mcf.readline()
611616
atom_ID = int(line.split()[0])
612617
if line.split()[-1] == 'ring':
613618
frag_atoms_are_ring.append(atom_ID)
614619
elif "# Angle_Info" in line:
615620
nbr_angles = int(frag_mcf.readline().split()[0])
616-
for k in xrange(0,nbr_angles):
621+
for k in range(0,nbr_angles):
617622
line = frag_mcf.readline()
618623
angle_ID = tuple([int(x) for x in line.split()[1:4]])
619624
angle_type = line.split()[4]
@@ -629,7 +634,7 @@ def read_pdb(pdb_file):
629634
if temp_rigid[j]:
630635
#If the whole frag is rigid, the ring must be rigid
631636
temp_ring_rigid.append(j)
632-
elif any([angle_type == 'fixed' for angle_type in angle_parms.values()]):
637+
elif any([angle_type == 'fixed' for angle_type in list(angle_parms.values())]):
633638
#Must have some 'fixed' angles to have a rigid ring
634639
nbr_ring_angles_fixed = 0
635640
for angle_ID in angle_parms:
@@ -644,8 +649,8 @@ def read_pdb(pdb_file):
644649

645650
#Create input file for each fragment of each species
646651

647-
for i in xrange(0, nbr_species):
648-
for j in xrange(0,nbr_fragments[i]):
652+
for i in range(0, nbr_species):
653+
for j in range(0,nbr_fragments[i]):
649654
if fragment_is_rigid[i][j]:
650655
#Read PDB
651656
atom_coords = read_pdb(pdb_files[i])
@@ -661,8 +666,8 @@ def read_pdb(pdb_file):
661666
output_frag.close()
662667
else:
663668
if fragment_has_ring[i][j]:
664-
print bold+"Generating RING FRAGMENT library species "+str(i+1)+\
665-
" fragment "+str(j+1)+normal
669+
print(bold+"Generating RING FRAGMENT library species "+str(i+1)+\
670+
" fragment "+str(j+1)+normal)
666671
input_frag = open("species"+str(i+1)+"/frag"+str(j+1)+"/frag"+str(j+1)+
667672
".inp",'w')
668673
input_frag.write("# Run_Name\nfrag"+str(j+1)+"\n\n")
@@ -714,8 +719,8 @@ def read_pdb(pdb_file):
714719
subprocess.call([cassandra_path,'frag'+str(j+1)+'.inp'])
715720
os.chdir('../../')
716721
else:
717-
print bold+"Generating fragment library species "+str(i+1)+\
718-
" fragment " + str(j+1)+normal
722+
print(bold+"Generating fragment library species "+str(i+1)+\
723+
" fragment " + str(j+1)+normal)
719724
input_frag = open("species"+str(i+1)+"/frag"+str(j+1)+"/frag"+str(j+1)+
720725
".inp",'w')
721726
input_frag.write("# Run_Name\nfrag"+str(j+1)+"\n\n")
@@ -764,13 +769,13 @@ def read_pdb(pdb_file):
764769
total_lines = len(in_file.readlines())
765770
in_file.close()
766771
omit = False
767-
for line_number in xrange(1,total_lines+1):
772+
for line_number in range(1,total_lines+1):
768773

769774
if line_number == frag_files_line:
770775
new_file.write("# Fragment_Files\n")
771776
total_frag_counter = 0
772-
for i in xrange(0, nbr_species):
773-
for j in xrange(0,nbr_fragments[i]):
777+
for i in range(0, nbr_species):
778+
for j in range(0,nbr_fragments[i]):
774779
total_frag_counter += 1
775780
new_file.write("species"+str(i+1)+"/frag"+str(j+1)+"/frag"+str(j+1)+
776781
".dat " + str(total_frag_counter)+"\n")
@@ -788,6 +793,13 @@ def read_pdb(pdb_file):
788793
in_file.close()
789794
new_file.close()
790795

791-
print bold+"Removing temporary input file"+normal
796+
print(bold+"Removing temporary input file"+normal)
792797
os.system("rm "+ input_file+"; mv "+input_file+"temp "+input_file)
793-
print bold+"Finished"+normal
798+
print(bold+"Finished"+normal)
799+
800+
# Python 2.x deprecation warning
801+
if (sys.version_info < (3,0)):
802+
warnings.showwarning("\n\nSupport for Python2 is deprecated in "
803+
"Cassandra and will be removed in a future release. Please "
804+
"consider switching to Python3.\n\n", DeprecationWarning,
805+
'library_setup.py', 801)

0 commit comments

Comments
 (0)