-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
559 lines (475 loc) · 18 KB
/
utils.py
File metadata and controls
559 lines (475 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
"""
Utitilies -- how to intuitively distinguish it from analysisTools.py?
"""
from openmm.app import *
import openmm.unit as unit
import argparse
import os
import csv
import numpy as np
import time
import Bio.PDB # biopython
import mdtraj as md
import MDAnalysis as mda
from pdbfixersource import PDBFixer # related to openmm. prepare PDB files for molecular simulations. https://openmm.org/ecosystem
from collections import Counter
from PeptideBuilder import Geometry # pip install PeptideBuilder
import PeptideBuilder
# I/O
def get_input():
"""
get the command line in put for the run num. defaulting to a new run (0)
:return:
"""
# Two examples of commands:
# python main.py --run_num=1 --mode='free aptamer' --aptamerSeq='TAATGTTAATTG' --ligand='False' --ligandType='' --ligandSeq=''
# python main.py --run_num=2 --mode='full dock' --aptamerSeq='TAATGTTAATTG' --ligand='YQTQ.pdb' --ligandType='peptide' --ligandSeq='YQTQTNSPRRAR'
parser = argparse.ArgumentParser()
parser.add_argument('--run_num', type=int, default=0)
parser.add_argument('--mode', type=str, default='simulation_mode')
parser.add_argument('--aptamerSeq', type=str, default='NOSEQUENCE')
parser.add_argument('--ligand', type=str, default='False')
parser.add_argument('--ligandType', type=str, default='')
parser.add_argument('--ligandSeq', type=str, default='')
cmd_line_input = parser.parse_args()
run_num = cmd_line_input.run_num
mode = cmd_line_input.mode
aptamerSeq = cmd_line_input.aptamerSeq
ligand = cmd_line_input.ligand
ligandType = cmd_line_input.ligandType
ligandSeq = cmd_line_input.ligandSeq
return [run_num, mode, aptamerSeq, ligand, ligandType, ligandSeq]
def recenterDCD(topology, trajectory):
"""
topology as pdb
trajectory as dcd
creates a new dcd without periodic artifacts
"""
traj = md.load(trajectory, top = topology)
traj.image_molecules()
traj.save(trajectory.split('.')[0] + '_recentered.dcd')
class Timer:
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, *args):
self.end = time.time()
self.interval = self.end - self.start
def printRecord(statement, filepath=""):
"""
print a string to command line output and a text file
:param statement:
:return:
"""
print(statement)
if os.path.exists(filepath + 'record.txt'):
with open(filepath + 'record.txt', 'a') as file:
file.write('\n' + statement)
else:
with open(filepath + 'record.txt', 'w') as file:
file.write('\n' + statement)
def prepPDB(file, boxOffset, pH, ionicStrength, MMBCORRECTION=False, waterBox=True):
"""
Soak pdb file in water box
:param file:
:param boxOffset:
:param pH:
:param ionicStrength:
:param MMBCORRECTION: if the input pdb file is an MMB output, we need to apply a correction, since MMB is a little weird formatting-wise
https://simtk.org/plugins/phpBB/viewtopicPhpbb.php?f=359&t=13397&p=0&start=0&view=&sid=bc6c1b9005122914ec7d572999ba945b
:param waterBox:
:return:
"""
if MMBCORRECTION:
replaceText(file, '*', "'") # due to a bug in this version of MMB - structures are encoded improperly - this fixes it
fixer = PDBFixer(filename=file)
padding, boxSize, boxVectors = None, None, None
geompadding = float(boxOffset) * unit.nanometer # TODO what does unit.nanometer do
boxMode = 'cubic' # TODO toggle for box type - look at openmm-setup source code for other box types
if boxMode == 'cubic':
padding = geompadding # for cubic box
elif boxMode == 'rectangular prism':
# or we can make a rectangular prism which (maybe) cuts off sides of the cube
u = mda.Universe(file)
coords = u.atoms.positions
xrange = np.ptp(coords[:, 0]) # get maximum dimension
yrange = np.ptp(coords[:, 1])
zrange = np.ptp(coords[:, 2])
maxsize = max([xrange, yrange, zrange])
# minimum dimension is half the longest TODO why?
# also convert to nanometer
xrange = max([xrange, maxsize / 2]) / 10
yrange = max([yrange, maxsize / 2]) / 10
zrange = max([zrange, maxsize / 2]) / 10
# TODO may also need an EWALD offset
xrange = xrange + 2 * boxOffset
yrange = yrange + 2 * boxOffset
zrange = zrange + 2 * boxOffset
boxSize = [xrange, yrange, zrange] * unit.nanometer # for rectangular prism
fixer.findMissingResidues()
fixer.findMissingAtoms()
fixer.addMissingAtoms() # may need to optimize bonding here
fixer.addMissingHydrogens(pH=pH) # add missing hydrogens
# TODO: Don't understand these above yet
if waterBox == True:
ionicStrength = float(ionicStrength) * unit.molar
positiveIon = 'Na+' # params['positiveion']+'+'
negativeIon = 'Cl-' # params['negativeion']+'-'
fixer.addSolvent(boxSize, padding, boxVectors, positiveIon, negativeIon, ionicStrength)
PDBFile.writeFile(fixer.topology, fixer.positions, open(file.split('.pdb')[0] + '_processed.pdb', 'w'))
def replaceText(file, old_string, new_string):
# search and replace string in a text file, then save the new version
f = open(file, 'r')
text = f.read()
f.close()
text = text.replace(old_string, new_string)
f = open(file, 'w')
f.write(text)
f.close()
def findLine(file, string):
"""
return the line number of a given string in a text file, indexing from 1
:param file:
:param string:
:return lineNum:
"""
f = open(file, 'r')
text = f.read()
f.close()
text = text.split('\n')
try:
lineInd = 1
for line in text:
if line == string:
lineNum = lineInd # index from 1
lineInd += 1
return lineNum
except:
raise ValueError("String not found in the file!")
def addLine(file, string, line_number):
"""
add a new line of text to a file
:param file:
:param string:
:param line_number:
:return:
"""
f = open(file, 'r')
text = f.read()
f.close()
lines = text.split('\n')
lines.insert(line_number - 1, string)
text = "\n".join(lines)
f = open(file, 'w')
f.write(text)
f.close()
def cleanTrajectory(structure, trajectory):
"""
Remove water, salt from trajectory
:param structure:
:param trajectory:
:return:
"""
u = mda.Universe(structure, trajectory)
# TODO: if u.segments.n_segments > 2: # if > 2 segments, then there must be solvent and salts (assuming nonzero salt concentration)
goodStuff = u.segments[:-2].atoms # cut out salts and solvents
goodStuff.write("clean_" + structure) # write topology
with mda.Writer("clean_" + trajectory, goodStuff.n_atoms) as W:
for ts in u.trajectory: # indexing over the trajectory
W.write(goodStuff)
def extractFrame(structure, trajectory, frame, outFileName):
"""
Save a given trajectory frame as a separate pdb file
:param structure:
:param trajectory:
:param frame:
:param outFileName:
:return:
"""
u = mda.Universe(structure, trajectory) # load up the whole trajectory
u.trajectory[frame] # Michael: this indexes the trajectory up to the desired frame (weird syntax, I think) # Me: this takes out the desired frame
if u.segments.n_segments > 2: # if there are more than 2 segments, then there must be solvent and salts (assuming nonzero salt concentration)
atoms = u.segments[:-2].atoms # omit solvent and salts
else:
atoms = u.atoms
atoms.write(outFileName)
if frame == -1:
printRecord('MDAnalysis: save the last frame into: {}'.format(outFileName))
def findAngles():
"""
Reads the angles required to constrain the dihedrals of the peptide backbone from the backbone_dihedrals.csv file.
For more info, see README_CONSTRAINTS.md
:param:
:return angles_to_constrain, a list that contains the numerical values for angles to constrain:
"""
angles_to_constrain = []
resdict = {"ALA": "A", "CYS": "C", "ASP": "D", "GLU": "E", "PHE": "F",
"GLY": "G", "HIS": "H", "ILE": "I", "LYS": "K", "LEU": "L",
"MET": "M", "ASN": "N", "PRO": "P", "GLN": "Q", "ARG": "R",
"SER": "S", "THR": "T", "VAL": "V", "TRP": "W", "TYR": "Y"}
resdict_inv = {one_let: three_let for three_let, one_let in
resdict.items()} # 3-letter a.a. code easier to work with for OpenMM
with open("backbone_dihedrals.csv") as csv_file:
printRecord("Reading CSV file...")
read_csv = csv.reader(csv_file, delimiter=",")
residue_nums = []
rows = []
row_lengths = set()
for row in read_csv:
rows.append(row)
residue_nums.append(row[0])
row_lengths.add(len(row))
# if len(rows) == 1 and params['peptide backbone constraint constant'] != 0:
# printRecord("ERROR: Backbone angles file does not have any values, but the constraint constant in main.py is not zero. Exiting run.")
# exit()
if len(row_lengths) != 1: # won't work if there is 1 more faulty input for line 1, and 4 inputs for line 2
rows_unequal = []
for i in range(len(rows)):
if len(rows[i]) != 4:
rows_unequal.append(i + 1)
printRecord("ERROR: Incorrect number of inputs for rows:")
for unequal_row in rows_unequal:
printRecord(unequal_row)
printRecord("Exiting run.")
exit()
elif mode(residue_nums) != residue_nums:
printRecord("ERROR: More than one input row for a residue_num in backbone_dihedrals.csv; exiting run.")
exit()
else: # everything should be correct here
printRecord("Finding angles to constrain...")
angles_to_constrain = []
for i in range(len(rows)):
if i > 0:
angles_to_constrain.append(rows[i])
return angles_to_constrain
def mode(lst):
"""
Returns the statistical mode(s) of a given list, only used for checking backbone_dihedrals
Source: https://stackabuse.com/calculating-mean-median-and-mode-in-python/
:param lst:
:return mode_list, a list of the mode(s) found in the input list:
"""
c = Counter(lst)
mode_list = [k for k, v in c.items() if v == c.most_common(1)[0][1]]
return mode_list
def buildPeptide(peptideSeq, peptidePDB, customAngles=False):
"""
Construct a peptide with optionally custom angles and constraints
:param peptide:
:param customAngles:
:return:
"""
print('custom angles=', customAngles)
geo = Geometry.geometry(peptideSeq[0])
# angles_to_constrain = findAngles() # all values in the list are strings
# printRecord("Found angles_to_constrain successfully, beginning to constrain...\n")
if customAngles:
printRecord("CustomAngles on\n")
angles_to_constrain = findAngles() # all values in the list are strings
printRecord("Found angles_to_constrain successfully, beginning to constrain...\n")
phis = {row[0]: float(row[1]) for row in angles_to_constrain}
psis = {row[0]: float(row[2]) for row in angles_to_constrain}
for row in angles_to_constrain:
if int(row[0]) == 0:
printRecord('phi[0] and psi[0]:', phis[row[0]], psis[row[0]], "\n") # only used for debugging
geo.phi, geo.psi = phis[row[0]], psis[row[0]]
structure = PeptideBuilder.initialize_res(peptideSeq[0])
for i in range(1, len(peptideSeq)):
geo = Geometry.geometry(peptideSeq[i])
if customAngles:
for row in angles_to_constrain:
if int(row[0]) == i:
printRecord(f'phi[{i}] and psi[{i}]: {phis[str(i)]}, {psis[str(i)]}\n') # only used for debugging
geo.phi, geo.psi = phis[str(i)], psis[str(i)]
printRecord("Adding Residue...\n")
PeptideBuilder.add_residue(structure, geo)
if customAngles:
constrain_str = " with custom angles & constraints"
else:
constrain_str = ""
printRecord("Successfully built peptide" + constrain_str + "\n")
# PeptideBuilder.add_terminal_OXT(structure) # OpenMM will not run without this, but LightDock will not run with it. Solution, add terminal oxygen in prepPDB after docking
out = Bio.PDB.PDBIO()
out.set_structure(structure)
out.save(peptidePDB)
def killH(structure):
"""
Use simTk modeller to delete all atoms with "H" in the name
:param structure:
:return:
"""
pdb = PDBFile(structure)
topology = pdb.topology
positions = pdb.positions
modeller = Modeller(topology, positions)
modeller.delete(atom for atom in topology.atoms() if "H" in atom.name)
PDBFile.writeFile(modeller.topology, modeller.positions, open(structure.split('.')[0] + '_noH.pdb', 'w'))
def addH(structure, pH):
"""
Protonate a given structure
:param structure:
:param pH:
:return:
"""
pdb = PDBFile(structure)
topology = pdb.topology
positions = pdb.positions
modeller = Modeller(topology, positions)
modeller.addHydrogens(pH=pH)
PDBFile.writeFile(modeller.topology, modeller.positions, open(structure.split('.')[0] + '_H.pdb', 'w'))
def changeSegment(structure, oldSeg, newSeg):
"""
Change the segment ID for all molecule(s) in a pdb file
:param structure:
:param oldSeg:
:param newSeg:
:return:
"""
replaceText(structure, ' ' + oldSeg + ' ', ' ' + newSeg + ' ')
def readInitialLines(file, lines):
# Return the first N lines of a text file
f = open(file, 'r')
text = f.read()
f.close()
text = text.split('\n')
initialLines = text[:lines]
return initialLines
def readFinalLines(file, lines):
# Return the final N lines of a text file
f = open(file, 'r')
text = f.read()
f.close()
text = text.split('\n')
finalLines = text[-(lines + 1):] # TODO: this will return "lines+1" rows from the end of the file
return finalLines
def appendTrajectory(topology, original, new):
"""
Use mda to combine old and new MD trajectories into one nice video
"""
trajectories = [original, new]
u = mda.Universe(topology, trajectories)
with mda.Writer('combinedTraj.dcd', u.atoms.n_atoms) as W:
for ts in u.trajectory:
W.write(u)
def removeLine(file, string):
"""
remove every line containing given string from a file
:param file: the file
:param string: the string to look for
:return:
"""
f = open(file, 'r')
text = f.read()
f.close()
lines = text.split('\n')
try:
for i in range(len(lines)):
if string in lines[i]:
lines.pop(i)
except:
pass
text = "\n".join(lines)
f = open(file, 'w')
f.write(text)
f.close()
def appendLine(file, string):
# add a new line of end of a file
f = open(file, 'r')
text = f.read()
f.close()
lines = text.split('\n')
lines.insert(len(lines) - 1, string) # replace line ### with the string, indexing from 1
text = "\n".join(lines)
f = open(file, 'w')
f.write(text)
f.close()
# # Doesn't seem to be used?
# def writeCheckpoint(text):
# """
# write some output to the checkpoint file
# :return:
# """
# f = open('checkpoint.txt', 'a')
# f.write('\n' + text)
# f.close()
#
# def combinePDB(file1, file2):
# """
# combine 2 pdb files into one
# some special formatting for MDA outputs in particular
# :param file1:
# :param file2:
# :return:
# """
# filenames = [file1, file2]
# for file in filenames: # remove title, periodic box, endpoints
# removeLine(file, 'CRYST1')
# removeLine(file, 'TITLE')
# removeLine(file, 'END')
# if 'repStructure' in file:
# appendLine(file, 'TER')
#
# with open('combined.pdb', 'w') as outfile:
# for fname in filenames:
# with open(fname) as infile:
# for line in infile:
# outfile.write(line)
#
#
# def fullPipelineTrajectory(ind1,ind2):
# '''
# combine folding, smoothing, sampling, docking and binding trajectories into one nice video
# '''
#
# # this needs to be updated with the new file formatting system
# trajectories = []
# # fold
# dir = './mmbFiles_%d'%ind1
# copyfile(dir + '/last.1.pdb','foldFrame1.pdb')
# dirList = os.listdir(dir)
#
# filenames = []
# for file in dirList:
# if 'trajectory' in file:
# filenames.append(dir + '/' +file)
#
# with open('foldingTraj_%d'%ind1 + '.pdb', 'w') as outfile:
# for fname in filenames:
# with open(fname) as infile:
# for line in infile:
# outfile.write(line)
#
# replaceText('foldingTraj_%d'%ind1 + '.pdb', '*', "'") # due to a bug in this version of MMB - structures are encoded improperly - this fixes it
#
# u = mda.Universe('foldingTraj_%d'%ind1 + '.pdb')
# with mda.Writer('foldingTraj_%d'%ind1 +'.dcd', u.atoms.n_atoms) as W:
# for ts in u.trajectory:
# W.write(u)
#
# trajectories.append('foldingTraj_%d'%ind1 + '.dcd')
#
# # initial relaxation
# trajectories.append('smoothed_sequence_%d'%ind1 + '.dcd')
#
# # free aptamer
# trajectories.append('clean_finished_sequence_%d'%ind1 + '.dcd')
#
# u = mda.Universe('foldFrame1.pdb', trajectories)
#
# with mda.Writer('fullPipeTraj.dcd', u.atoms.n_atoms) as W:
# for ts in u.trajectory:
# W.write(u)
#
#
# # docking
#
# # binding
# trajectories.append('clean_finished_complex_%d'%ind1 + '_%d'%ind2 + '.dcd')
#
#
# def copyLine(file, line_number):
# # copy a line of text from a file and return it
# f = open(file, 'r')
# text = f.read()
# f.close()
# return text.split('\n')[line_number - 1] # copy a line from the file, indexing from 1