fixed indentations and ported python 2 code to python 3

This commit is contained in:
alphataubio
2024-01-12 18:19:50 -05:00
parent 7ca2dcac62
commit 5b05112aab
2 changed files with 216 additions and 221 deletions

View File

@ -33,7 +33,7 @@ which needs to be provided
try: try:
import numpy as np import numpy as np
except: except:
print >> sys.stderr, "numpy not found. Exiting." print("numpy not found. Exiting.", file=sys.stderr)
sys.exit(1) sys.exit(1)
""" """
@ -45,8 +45,8 @@ try:
box_length = float(sys.argv[2]) box_length = float(sys.argv[2])
infile = sys.argv[3] infile = sys.argv[3]
except: except:
print >> sys.stderr, "Usage: %s <%s> <%s> <%s>" % (sys.argv[0], \ print( "Usage: %s <%s> <%s> <%s>" % (sys.argv[0], \
"box offset", "box length", "file with sequences") "box offset", "box length", "file with sequences"), file=sys.stderr)
sys.exit(1) sys.exit(1)
box = np.array ([box_length, box_length, box_length]) box = np.array ([box_length, box_length, box_length])
@ -57,8 +57,7 @@ try:
inp = open (infile, 'r') inp = open (infile, 'r')
inp.close() inp.close()
except: except:
print >> sys.stderr, "Could not open file '%s' for reading. \ print( "Could not open file '%s' for reading. Aborting." % infile, file=sys.stderr)
Aborting." % infile
sys.exit(2) sys.exit(2)
# return parts of a string # return parts of a string
@ -174,21 +173,21 @@ def add_strands (mynewpositions, mynewa1s, mynewa3s):
# placed particles i we check whether it overlaps with any of the # placed particles i we check whether it overlaps with any of the
# newly created particles j # newly created particles j
print >> sys.stdout, "## Checking for overlaps" print( "## Checking for overlaps", file=sys.stdout)
for i in xrange(len(positions)): for i in range(len(positions)):
p = positions[i] p = positions[i]
pa1 = a1s[i] pa1 = a1s[i]
for j in xrange (len(mynewpositions)): for j in range (len(mynewpositions)):
q = mynewpositions[j] q = mynewpositions[j]
qa1 = mynewa1s[j] qa1 = mynewa1s[j]
# skip particles that are anyway too far away # skip particles that are anyway too far away
dr = p - q dr = p - q
dr -= box * np.rint (dr / box) dr -= box * np.rint(dr / box)
if np.dot(dr, dr) > RC2: if np.dot(dr, dr) > RC2:
continue continue
@ -200,13 +199,13 @@ def add_strands (mynewpositions, mynewa1s, mynewa3s):
# check for no overlap between the two backbone sites # check for no overlap between the two backbone sites
dr = p_pos_back - q_pos_back dr = p_pos_back - q_pos_back
dr -= box * np.rint (dr / box) dr -= box * np.rint(dr / box)
if np.dot(dr, dr) < RC2_BACK: if np.dot(dr, dr) < RC2_BACK:
overlap = True overlap = True
# check for no overlap between the two base sites # check for no overlap between the two base sites
dr = p_pos_base - q_pos_base dr = p_pos_base - q_pos_base
dr -= box * np.rint (dr / box) dr -= box * np.rint(dr / box)
if np.dot(dr, dr) < RC2_BASE: if np.dot(dr, dr) < RC2_BASE:
overlap = True overlap = True
@ -238,7 +237,7 @@ def add_strands (mynewpositions, mynewa1s, mynewa3s):
for p in mynewa3s: for p in mynewa3s:
a3s.append (p) a3s.append (p)
# calculate quaternion from local body frame and append # calculate quaternion from local body frame and append
for ia in xrange(len(mynewpositions)): for ia in range(len(mynewpositions)):
mynewquaternions = exyz_to_quat(mynewa1s[ia],mynewa3s[ia]) mynewquaternions = exyz_to_quat(mynewa1s[ia],mynewa3s[ia])
quaternions.append(mynewquaternions) quaternions.append(mynewquaternions)
@ -301,13 +300,12 @@ def generate_strand(bp, sequence=None, start_pos=np.array([0, 0, 0]), \
elif len(sequence) != bp: elif len(sequence) != bp:
n = bp - len(sequence) n = bp - len(sequence)
sequence += np.random.randint(1, 5, n) sequence += np.random.randint(1, 5, n)
print >> sys.stderr, "sequence is too short, adding %d random bases" % n print( "sequence is too short, adding %d random bases" % n, file=sys.stderr)
# normalize direction # normalize direction
dir_norm = np.sqrt(np.dot(dir,dir)) dir_norm = np.sqrt(np.dot(dir,dir))
if dir_norm < 1e-10: if dir_norm < 1e-10:
print >> sys.stderr, "direction must be a valid vector, \ print( "direction must be a valid vector, defaulting to (0, 0, 1)", file=sys.stderr)
defaulting to (0, 0, 1)"
dir = np.array([0, 0, 1]) dir = np.array([0, 0, 1])
else: dir /= dir_norm else: dir /= dir_norm
@ -391,7 +389,7 @@ def read_strands(filename):
try: try:
infile = open (filename) infile = open (filename)
except: except:
print >> sys.stderr, "Could not open file '%s'. Aborting." % filename print( "Could not open file '%s'. Aborting." % filename, file=sys.stderr )
sys.exit(2) sys.exit(2)
# This block works out the number of nucleotides and strands by reading # This block works out the number of nucleotides and strands by reading
@ -406,28 +404,27 @@ def read_strands(filename):
if line[:6] == 'DOUBLE': if line[:6] == 'DOUBLE':
line = line.split()[1] line = line.split()[1]
length = len(line) length = len(line)
print >> sys.stdout, "## Found duplex of %i base pairs" % length print( "## Found duplex of %i base pairs" % length, file=sys.stdout)
nnucl += 2*length nnucl += 2*length
nstrands += 2 nstrands += 2
nbonds += (2*length-2) nbonds += (2*length-2)
else: else:
line = line.split()[0] line = line.split()[0]
length = len(line) length = len(line)
print >> sys.stdout, \ print( "## Found single strand of %i bases" % length, file=sys.stdout)
"## Found single strand of %i bases" % length
nnucl += length nnucl += length
nstrands += 1 nstrands += 1
nbonds += length-1 nbonds += length-1
# rewind the sequence input file # rewind the sequence input file
infile.seek(0) infile.seek(0)
print >> sys.stdout, "## nstrands, nnucl = ", nstrands, nnucl print( "## nstrands, nnucl = ", nstrands, nnucl, file=sys.stdout)
# generate the data file in LAMMPS format # generate the data file in LAMMPS format
try: try:
out = open ("data.oxdna", "w") out = open ("data.oxdna", "w")
except: except:
print >> sys.stderr, "Could not open data file for writing. Aborting." print( "Could not open data file for writing. Aborting.", file=sys.stderr)
sys.exit(2) sys.exit(2)
lines = infile.readlines() lines = infile.readlines()
@ -452,11 +449,11 @@ def read_strands(filename):
seq = [(base_to_number[x]) for x in line] seq = [(base_to_number[x]) for x in line]
myns += 1 myns += 1
for b in xrange(length): for b in range(length):
basetype.append(seq[b]) basetype.append(seq[b])
strandnum.append(myns) strandnum.append(myns)
for b in xrange(length-1): for b in range(length-1):
bondpair = [noffset + b, noffset + b + 1] bondpair = [noffset + b, noffset + b + 1]
bonds.append(bondpair) bonds.append(bondpair)
noffset += length noffset += length
@ -467,16 +464,16 @@ def read_strands(filename):
seq2.reverse() seq2.reverse()
myns += 1 myns += 1
for b in xrange(length): for b in range(length):
basetype.append(seq2[b]) basetype.append(seq2[b])
strandnum.append(myns) strandnum.append(myns)
for b in xrange(length-1): for b in range(length-1):
bondpair = [noffset + b, noffset + b + 1] bondpair = [noffset + b, noffset + b + 1]
bonds.append(bondpair) bonds.append(bondpair)
noffset += length noffset += length
print >> sys.stdout, "## Created duplex of %i bases" % (2*length) print( "## Created duplex of %i bases" % (2*length), file=sys.stdout)
# generate random position of the first nucleotide # generate random position of the first nucleotide
cdm = box_offset + np.random.random_sample(3) * box cdm = box_offset + np.random.random_sample(3) * box
@ -499,10 +496,10 @@ def read_strands(filename):
axis /= np.sqrt(np.dot(axis, axis)) axis /= np.sqrt(np.dot(axis, axis))
newpositions, newa1s, newa3s = generate_strand(len(line), \ newpositions, newa1s, newa3s = generate_strand(len(line), \
sequence=seq, dir=axis, start_pos=cdm, double=True) sequence=seq, dir=axis, start_pos=cdm, double=True)
print >> sys.stdout, "## Trying %i" % i print( "## Trying %i" % i, file=sys.stdout)
end = timer() end = timer()
print >> sys.stdout, "## Added duplex of %i bases (line %i/%i) in %.2fs, now at %i/%i" % \ print( "## Added duplex of %i bases (line %i/%i) in %.2fs, now at %i/%i" % \
(2*length, i, nlines, end-start, len(positions), nnucl) (2*length, i, nlines, end-start, len(positions), nnucl), file=sys.stdout)
# block for single strands: last argument of the generate function # block for single strands: last argument of the generate function
# is set to 'False' # is set to 'False'
@ -511,11 +508,11 @@ def read_strands(filename):
seq = [(base_to_number[x]) for x in line] seq = [(base_to_number[x]) for x in line]
myns += 1 myns += 1
for b in xrange(length): for b in range(length):
basetype.append(seq[b]) basetype.append(seq[b])
strandnum.append(myns) strandnum.append(myns)
for b in xrange(length-1): for b in range(length-1):
bondpair = [noffset + b, noffset + b + 1] bondpair = [noffset + b, noffset + b + 1]
bonds.append(bondpair) bonds.append(bondpair)
noffset += length noffset += length
@ -527,8 +524,7 @@ def read_strands(filename):
axis = np.random.random_sample(3) axis = np.random.random_sample(3)
axis /= np.sqrt(np.dot(axis, axis)) axis /= np.sqrt(np.dot(axis, axis))
print >> sys.stdout, \ print("## Created single strand of %i bases" % length, file=sys.stdout)
"## Created single strand of %i bases" % length
newpositions, newa1s, newa3s = generate_strand(length, \ newpositions, newa1s, newa3s = generate_strand(length, \
sequence=seq, dir=axis, start_pos=cdm, double=False) sequence=seq, dir=axis, start_pos=cdm, double=False)
@ -541,14 +537,14 @@ def read_strands(filename):
sequence=seq, dir=axis, start_pos=cdm, double=False) sequence=seq, dir=axis, start_pos=cdm, double=False)
print >> sys.stdout, "## Trying %i" % (i) print >> sys.stdout, "## Trying %i" % (i)
end = timer() end = timer()
print >> sys.stdout, "## Added single strand of %i bases (line %i/%i) in %.2fs, now at %i/%i" % \ print( "## Added single strand of %i bases (line %i/%i) in %.2fs, now at %i/%i" % \
(length, i, nlines, end-start,len(positions), nnucl) (length, i, nlines, end-start,len(positions), nnucl), file=sys.stdout)
i += 1 i += 1
# sanity check # sanity check
if not len(positions) == nnucl: if not len(positions) == nnucl:
print len(positions), nnucl print( len(positions), nnucl )
raise AssertionError raise AssertionError
out.write('# LAMMPS data file\n') out.write('# LAMMPS data file\n')
@ -580,18 +576,16 @@ def read_strands(filename):
out.write('Atoms\n') out.write('Atoms\n')
out.write('\n') out.write('\n')
for i in xrange(nnucl): for i in range(nnucl):
out.write('%d %d %22.15le %22.15le %22.15le %d 1 1\n' \ out.write('%d %d %22.15le %22.15le %22.15le %d 1 1\n' \
% (i+1, basetype[i], \ % (i+1, basetype[i], positions[i][0], positions[i][1], positions[i][2], strandnum[i]))
positions[i][0], positions[i][1], positions[i][2], \
strandnum[i]))
out.write('\n') out.write('\n')
out.write('# Atom-ID, translational, rotational velocity\n') out.write('# Atom-ID, translational, rotational velocity\n')
out.write('Velocities\n') out.write('Velocities\n')
out.write('\n') out.write('\n')
for i in xrange(nnucl): for i in range(nnucl):
out.write("%d %22.15le %22.15le %22.15le %22.15le %22.15le %22.15le\n" \ out.write("%d %22.15le %22.15le %22.15le %22.15le %22.15le %22.15le\n" \
% (i+1,0.0,0.0,0.0,0.0,0.0,0.0)) % (i+1,0.0,0.0,0.0,0.0,0.0,0.0))
@ -600,9 +594,8 @@ def read_strands(filename):
out.write('Ellipsoids\n') out.write('Ellipsoids\n')
out.write('\n') out.write('\n')
for i in xrange(nnucl): for i in range(nnucl):
out.write(\ out.write("%d %22.15le %22.15le %22.15le %22.15le %22.15le %22.15le %22.15le\n" \
"%d %22.15le %22.15le %22.15le %22.15le %22.15le %22.15le %22.15le\n" \
% (i+1,1.1739845031423408,1.1739845031423408,1.1739845031423408, \ % (i+1,1.1739845031423408,1.1739845031423408,1.1739845031423408, \
quaternions[i][0],quaternions[i][1], quaternions[i][2],quaternions[i][3])) quaternions[i][0],quaternions[i][1], quaternions[i][2],quaternions[i][3]))
@ -611,13 +604,13 @@ def read_strands(filename):
out.write('Bonds\n') out.write('Bonds\n')
out.write('\n') out.write('\n')
for i in xrange(nbonds): for i in range(nbonds):
out.write("%d %d %d %d\n" % (i+1,1,bonds[i][0],bonds[i][1])) out.write("%d %d %d %d\n" % (i+1,1,bonds[i][0],bonds[i][1]))
out.close() out.close()
print >> sys.stdout, "## Wrote data to 'data.oxdna'" print("## Wrote data to 'data.oxdna'", file=sys.stdout)
print >> sys.stdout, "## DONE" print("## DONE", file=sys.stdout)
# call the above main() function, which executes the program # call the above main() function, which executes the program
read_strands (infile) read_strands (infile)
@ -627,4 +620,6 @@ runtime = end_time-start_time
hours = runtime/3600 hours = runtime/3600
minutes = (runtime-np.rint(hours)*3600)/60 minutes = (runtime-np.rint(hours)*3600)/60
seconds = (runtime-np.rint(hours)*3600-np.rint(minutes)*60)%60 seconds = (runtime-np.rint(hours)*3600-np.rint(minutes)*60)%60
print >> sys.stdout, "## Total runtime %ih:%im:%.2fs" % (hours,minutes,seconds) print( "## Total runtime %ih:%im:%.2fs" % (hours,minutes,seconds), file=sys.stdout)