-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoniom_inp_mod_aux.py
2996 lines (2540 loc) · 100 KB
/
oniom_inp_mod_aux.py
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
""""
authors: Jakub Baran, Paulina Miśkowiec, Tomasz Borowski
last update: 21 May 2024
"""
import re, math, scipy, string, scipy.spatial
import numpy as np
from copy import deepcopy
# CONSTANTS
letters = string.ascii_uppercase
digits = string.digits
vdw_radii = {\
'Sc':1.648, 'Ti':1.588, 'V':1.572, 'Cr':1.512, 'Mn':1.481, 'Fe':1.456, 'Co':1.436, 'Ni':1.417, 'Cu':1.748, 'Zn':1.382,\
'Y':1.673, 'Zr':1.562, 'Nb':1.583, 'Mo':1.526,'Tc':1.499, 'Ru':1.482, 'Rh':1.465, 'Pd':1.450, 'Ag':1.574, 'Cd':1.424,\
'La':1.761,'Hf':1.571, 'Ta':1.585, 'W':1.535, 'Re':1.477, 'Os':1.560, 'Ir':1.420, 'Pt':1.377, 'Au':1.647, 'Hg':1.353}
# values taken from the UFF force field: DOI: 10.1021/ja00051a040 (Table 1: nonbond distance/2.)
#######################
#### Class section ####
#######################
class point_charge:
__slots__ = ["charge", "coords"]
def __init__(self, charge, coords):
self.charge = charge # float
self.coords = coords # a list of 3 numbers
def __str__(self):
return str(self.coords[0]) + " " + str(self.coords[1]) + " " +\
str(self.coords[2]) + " " + str(self.charge)
def get_coords(self):
return self.coords
def get_charge(self):
return self.charge
def get_string(self):
x = '{:06.6f}'.format(self.coords[0])
y = '{:06.6f}'.format(self.coords[1])
z = '{:06.6f}'.format(self.coords[2])
q = '{:06.8f}'.format(self.charge)
if (self.coords[0] < 0.0):
s1 = ''
else:
s1 = ' '
if (self.coords[1] < 0.0):
s2 = ' '
else:
s2 = ' '
if (self.coords[2] < 0.0):
s3 = ' '
else:
s3 = ' '
if (self.charge < 0.0):
s4 = ' '
else:
s4 = ' '
return s1 + x + s2 + y + s3 + z + s4 + q
def set_charge(self, q):
self.charge = q
def set_coords(self, crd):
self.coords = crd
class atom:
__slots__ = ["index", "element", "at_charge", "coords", "at_type", "frozen", "oniom_layer",
"new_index", "new_at_type", "tree_chain_classification", "connect_list",
"in_mainchain", "name", "LAH"]
def __init__(self, index, element, at_charge, coords, at_type=None, frozen=None, oniom_layer='L'):
self.coords = coords
self.index = index # 0-based (to be consistent with vmd and python indexing)
self.new_index = None
self.element = element # element symbol
self.at_type = at_type
self.new_at_type = None
self.at_charge = at_charge
self.oniom_layer = oniom_layer # string, one from 'L', 'M' or 'H'
self.tree_chain_classification = '' #
self.connect_list = []
self.in_mainchain = False #
self.frozen = frozen # 0 - not frozen, -1 - frozen
self.name = None #
self.LAH = False # True if this atom is a Link Atom Host (LAH)
def __str__(self):
return str(self.index) + " " + str(self.element) + " " + str(self.at_type) + " " + str(self.at_charge) + " " \
+ str(self.frozen) + " " + str(self.coords) + " " + str(self.oniom_layer)
def get_coords(self):
return self.coords
def get_index(self):
return self.index
def get_new_index(self):
return self.new_index
def get_element(self):
return self.element
def get_type(self):
return self.at_type
def get_new_type(self):
return self.new_at_type
def get_at_charge(self):
return self.at_charge
def get_oniom_layer(self):
return self.oniom_layer
def get_tree_chain_classification(self):
return self.tree_chain_classification
def get_connect_list(self):
return self.connect_list
def get_in_mainchain(self):
return self.in_mainchain
def get_frozen(self):
return self.frozen
def get_name(self):
return self.name
def get_LAH(self):
return self.LAH
def set_coords(self, coords):
self.coords = coords
def set_type(self, at_type):
self.at_type = at_type
def set_new_type(self, new_at_type):
self.new_at_type = new_at_type
def set_at_charge(self, at_charge):
self.at_charge = at_charge
def set_oniom_layer(self, layer):
self.oniom_layer = layer
def set_tree_chain_classification(self, tree_chain_classification):
self.tree_chain_classification = tree_chain_classification
def set_connect_list(self, connect_list):
self.connect_list = connect_list
def set_in_mainchain(self, in_mainchain):
self.in_mainchain = in_mainchain
def set_frozen(self, frozen):
self.frozen = frozen
def set_new_index(self, new_index):
self.new_index = new_index
def set_name(self, name):
self.name = name
def set_LAH(self, LAH):
self.LAH = LAH
class link_atom(atom):
__slots__ = ["index", "element", "at_charge", "coords", "at_type", "frozen", "oniom_layer",
"new_index", "new_at_type", "tree_chain_classification", "connect_list",
"in_mainchain", "name", "LAH", "bonded_to", "H_coords", "mm_element"]
def __init__(self, coords, index, element,
at_type='', at_charge=0.0, bonded_to=None):
atom.__init__(self, index, element, at_charge, coords, at_type=None, frozen=None, oniom_layer='L')
self.at_type = at_type
self.bonded_to = bonded_to
self.H_coords = []
self.mm_element = ''
def __str__(self):
return str(self.index) + " " + str(self.element) + " " + str(self.at_type) + " " + str(self.at_charge) + " " \
+ " " + str(self.H_coords) + " " + str(self.bonded_to)
def get_type(self):
return self.at_type
def get_bonded_to(self):
return self.bonded_to
def get_MM_coords(self):
return self.coords
def get_H_coords(self):
return self.H_coords
def get_coords(self):
return self.H_coords
def get_mm_element(self):
return self.mm_element
def set_type(self, at_type):
self.at_type = at_type
def set_bonded_to(self, bonded_to):
self.bonded_to = bonded_to
def set_H_coords(self, H_coords):
self.H_coords = H_coords
def set_mm_element(self, mm_element):
self.mm_element = mm_element
class residue:
__slots__ = ["label", "index", "new_index", "in_protein", "atoms", "main_chain_atoms",
"trim", "chain"]
def __init__(self, label, index):
self.label = label
self.index = index # zero based - to be consistent with vmd and python
self.new_index = None
self.in_protein = False
self.atoms = [] # list of atom objects
self.main_chain_atoms = [] # list of atom objects
self.trim = False
self.chain = ''
def get_label(self):
return self.label
def get_index(self):
return self.index
def get_new_index(self):
return self.new_index
def get_in_protein(self):
return self.in_protein
def get_atoms(self):
return self.atoms
def get_main_chain_atoms(self):
return self.main_chain_atoms
def get_side_chain_atoms(self):
sc_atoms = self.atoms.copy()
for at in self.main_chain_atoms:
sc_atoms.remove(at)
return sc_atoms
def next_atom(self):
for atom in self.atoms:
yield atom
def get_trim(self):
return self.trim
def get_chain(self):
return self.chain
def set_in_protein(self,in_protein):
self.in_protein = in_protein
def add_atom(self,atom):
self.atoms.append(atom)
if atom.get_tree_chain_classification() == 'M':
self.main_chain_atoms.append(atom)
def add_main_chain_atom(self,atom):
self.main_chain_atoms.append(atom)
def set_trim(self,trim):
self.trim = trim
def set_new_index(self,new_index):
self.new_index = new_index
def set_chain(self,chain_id):
self.chain = chain_id
class peptide:
__slots__ = ["label", "index", "is_peptide", "residues"]
def __init__(self, label, index):
self.label = label
self.index = index # 0-based
self.is_peptide = False
self.residues = [] # list of residue objects
def get_label(self):
return self.label
def get_index(self):
return self.index
def get_is_peptide(self):
return self.is_peptide
def get_residues(self):
return self.residues
def get_last_residue(self):
if len(self.residues)>0:
return self.residues[-1]
else:
return None
def next_residue(self):
for residue in self.residues:
yield residue
def add_residue(self,residue):
self.residues.append(residue)
#########################
#### Read functions ####
#########################
def find_in_file(file):
"""
create a dictionary with pointers to specific sections in the input file
in order to have a easy access to them
:input: file - a file object
:returns: offsets - a dictionary
"""
offsets = {"chargeAndSpin": -1,
"atomInfo": -1,
"comment": -1,
"connectList": -1,
"header": 0,
"parm": -1,
"redundant": -1,
"p_charges": -1
}
empty_line = 0 # count empty line
file.seek(0, 2) # jump to the end of file
EOF = file.tell() # set end of file (EOF) location
file.seek(0) # jump to the beginning of the file
is_redundant_exists = False
while file.tell() != EOF:
previousLine = file.tell()
line = file.readline()
if empty_line == 1:
offsets["comment"] = previousLine
empty_line += 1 # avoid going again to this if statement
elif empty_line == 3:
offsets["chargeAndSpin"] = previousLine
offsets["atomInfo"] = file.tell()
empty_line += 1
elif empty_line == 5:
offsets["connectList"] = previousLine
empty_line += 1
elif empty_line == 7:
if re.match(r'^[0-9]+', line):
offsets["redundant"] = previousLine
is_redundant_exists = True
else:
offsets["parm"] = previousLine
empty_line += 1
elif empty_line == 9 and is_redundant_exists:
offsets["parm"] = previousLine
empty_line += 1
elif empty_line == 9:
if re.match(r'^[0-9]+', line):
offsets["p_charges"] = previousLine
empty_line += 1
elif empty_line == 11 and is_redundant_exists:
if re.match(r'^[0-9]+', line):
offsets["p_charges"] = previousLine
empty_line += 1
if line == '\n':
empty_line += 1
file.seek(0)
return offsets
def read_Chk(header):
"""
find Chk file name and Chk extension in input file
: header - a string with header from ONIOM input
:return:
list where [0] is file name and [1] is file extension
"""
try:
find_chk = re.search(r'%[Cc][Hh][Kk]=(.+)(\.[Cc][Hh][Kk]\s)', header)
chk_core = find_chk.group(1)
chk_extension = find_chk.group(2)
except AttributeError:
chk_core = ''
chk_extension = '\n' # if not found always go to next line
return chk_core, chk_extension
def read_from_to_empty_line(file, offset) -> str:
"""
save a file fragment between the place specified by offset to an empty line
to a string, which is returned
:input: file - file object
offset - int, position within the file, as returned by file.tell()
:returns:
str which contains the content of the header
"""
file.seek(offset)
header_line = ""
while True:
line = file.readline()
if line == '\n':
break
header_line += line
return header_line
def read_charge_spin(file, offset) -> dict:
"""
read from file information about charge and spin
:return:
dict which contains the content of the charge and spin section of oniom input file
"""
# 2-layers ONIOM
# chrgreal-low spinreal-low chrgmodel-high spinmodel-high chrgmodel-low spinmodel-low
# 3-layer ONIOM
# cRealL sRealL cIntM sIntM cIntL sIntL cModH sModH cModM sModM cModL sModL
two_layer_seq = ["ChrgRealLow", "SpinRealLow", "ChrgModelHigh", "SpinModelHigh",
"ChrgModelLow", "SpinModelLow"]
three_layer_seq = ["ChrgRealLow", "SpinRealLow", "ChrgIntMed", "SpinIntMed",
"ChrgIntLow", "SpinIntLow", "ChrgModelHigh", "SpinModelHigh",
"ChrgModelMed", "SpinModelMed", "ChrgModelLow", "SpinModelLow"]
file.seek(offset)
chargeAndSpin_dict = {"ChrgRealLow": 0, "SpinRealLow": 0,
"ChrgModelHigh": 0, "SpinModelHigh": 0,
"ChrgModelMed": 0, "SpinModelMed": 0,
"ChrgModelLow": 0, "SpinModelLow": 0,
"ChrgIntMed": 0, "SpinIntMed": 0,
"ChrgIntLow": 0, "SpinIntLow": 0,}
chargeAndSpin_list = file.readline().split()
n_items = len(chargeAndSpin_list)
if n_items == 6:
for key, value in zip(two_layer_seq, chargeAndSpin_list):
chargeAndSpin_dict[key] = int(value)
elif n_items == 12:
for key, value in zip(three_layer_seq, chargeAndSpin_list):
chargeAndSpin_dict[key] = int(value)
return chargeAndSpin_dict
def read_atom_inf(file, offset) -> tuple:
"""
create two list\n
*atomObject_list:\n
[0] - atom name\n
[1] - atom type\n
[2] - atom charge\n
[3] - atom frozen\n
[4] - atom x coords\n
[5] - atom y coords\n
[6] - atom z coords\n
[7] - atom oniom_layer\n
*linkObject_list:\n
[0] - link atom name\n
[1] - link atom type\n
[2] - link atom charge\n
[3] - link atom bonded_to\n
:return:
list where [0] is atom object and [1] is atom link object
"""
file.seek(offset)
atomObject_list = []
linkObject_list = []
oniom_layer_H_and_LAH_index_list = []
at_index = -1 # 0-based index
while True:
line = file.readline()
if line == '\n':
break
if check_if_line_correct(line):
at_index += 1
line = filter_line(line)
if len(line[3]) > 2: # if line[3] is frozen parameter, string has max 2 characters
line.insert(3, None)
element = line[0]
at_type = line[1]
at_charge = float(line[2])
if line[3] is None:
frozen = line[3]
else:
frozen = int(line[3])
coords = [float(line[4]), float(line[5]), float(line[6])]
onion_layer = line[7]
if onion_layer == 'H':
oniom_layer_H_and_LAH_index_list.append(at_index)
atomObject = atom(index=at_index, element=element, at_type=at_type, at_charge=at_charge,
frozen=frozen, coords=coords, oniom_layer=onion_layer)
if len(line) > 8: # atom has link section
atomObject.set_LAH(True)
oniom_layer_H_and_LAH_index_list.append(at_index)
link = link_atom(index=at_index, element=line[-4], at_type=line[-3], at_charge=float(line[-2]),
coords=coords)
bonded_to = int(line[-1]) - 1 # read value is 1-based, internal values are 0-based
link.set_bonded_to(bonded_to)
linkObject_list.append(link)
atomObject_list.append(atomObject)
for H_lk_atm in linkObject_list: # calculate the link atoms coords
qm_atom_index = H_lk_atm.get_bonded_to()
adjust_HLA_coords(H_lk_atm, atomObject_list[qm_atom_index]) # now 0-based index
return atomObject_list, linkObject_list, oniom_layer_H_and_LAH_index_list
def read_connect_list(file, offset) -> dict:
"""
create a dictionary where key is atom and value is a list of atoms(ints) connected to it
keys and values are 0-based
:return:
dict = { atom(int 0-based) : [connected_atom_1, connected_atom_2, ...],}
"""
file.seek(offset)
connect_dict = {}
while True:
line = file.readline().split()
if not line:
break
line = [int(float(elem)) for elem in line] # convert from str to int - int(float(i)) because int(i) cause
# problem when has to convert 1.0
if (line[0]-1) not in connect_dict:
connect_dict[(line[0]-1)] = []
for i in line[1::2]:
connect_dict[(line[0]-1)].append(i-1)
if i-1 not in connect_dict:
connect_dict[i-1] = []
connect_dict[i-1].append((line[0]-1))
return connect_dict
def read_p_charges(file, offset):
"""
read point charge section from the Gaussian input file
Parameters
----------
file : file objects
gaussian input file.
offset : INT
position in the file (as returned by .tell()) where
the point charge section begins.
Returns
-------
p_charges : LIST
a list of point_charge objects.
"""
file.seek(offset)
p_charges = []
while True:
line = file.readline().split()
if not line:
break
x = eval(line[0])
y = eval(line[1])
z = eval(line[2])
coords = [x, y, z]
charge = eval(line[3])
p_ch = point_charge(charge, coords)
p_charges.append(p_ch)
return p_charges
def filter_line(line: str) -> list:
"""
make from line(str) a list that contains only necessary information about atoms\n
example:\n
line = N-N3--0.1592 13.413952 49.941155 40.112556 L\n
after exec of this function\n
line = ['N', 'N3', '-0.1592', '13.413952', '49.941155', '40.112556', 'L']\n
:param line: line from file that contains information about atoms:
:return: a list where every list[index] contains a different information about atom
"""
for i in range(1, len(line)):
if line[i] == '-' and not re.match(r'\s+', line[i - 1]):
line = line[:i] + " " + line[i + 1:]
filterPattern = re.compile(r'\s+')
line = filterPattern.split(line)
line.remove('')
return line
def check_if_line_correct(line: str) -> bool:
"""
check if read line contains correct format information about atom\n
:param line: line to check
:return: True if line is correct False if line is incorrect
"""
pattern = r"[A-Z](.*[0-9]+[.][0-9]+){3}"
if re.match(pattern, line):
return True
else:
return False
def read_xyz_file(file):
"""
reads xyz file
file - file object
:returns: xyz_atoms - a list of lists, each [str - element symbol, float -x coord, float - y, float - z]
"""
xyz_atoms = []
file.seek(0)
line = file.readline() # in order to read number of atoms
line = line.split()
n_read = int(line[0]) # number of atoms read from xyz header
file.readline() # skip comment line
while True:
line = file.readline()
if line == '':
break
line = line.split()
element = line[0]
coords_x = float(line[1])
coords_y = float(line[2])
coords_z = float(line[3])
xyz_atoms.append([element, coords_x, coords_y, coords_z])
if n_read != len(xyz_atoms):
print("in function read_xyz_file: number of atoms read from file header: ", n_read, \
" does not match number of atoms found in the coordinate section: ", len(xyz_atoms))
return xyz_atoms
def read_qout_file(qout_f):
"""
qout_f - file object
:return: a list with atomic charges (floats)
"""
new_charge_list = qout_f.read().split()
new_charge_list = [float(i) for i in new_charge_list]
return new_charge_list
def read_single_number(file, flag_line):
"""reads a single number from file and returns it as a numerical value
file : file object
flag_line : string marker preceeding the value to be read"""
file.seek(0)
while True:
a = file.readline()
if not a:
break
match_flag=re.search(flag_line,a)
if match_flag:
if len(a.split())>1:
return eval(a.split()[1])
def read_single_string(file, flag_line):
"""reads a single string from file and returns it
file : file object
flag_line : string marker preceeding the string to be read"""
file.seek(0)
while True:
a = file.readline()
if not a:
break
match_flag=re.search(flag_line,a)
if match_flag:
if len(a.split())>1:
return a.split()[1]
def read_rsi_index(file, flag_line, end_line):
"""reads residue sidechain and index lines
contained between lines starting with flag_line
and end_line from file
---
file : file object
flag_line : string
end_line : string
---
Returns lists:
residue_index, sidechain_index and index_index
"""
residue_index = []
sidechain_index = []
index_index = []
file.seek(0)
while True:
a = file.readline()
if not a:
break
match_flag=re.search(flag_line,a)
if match_flag:
while True:
a = file.readline()
if not a:
break
elif re.search(end_line,a):
break
match_residue=re.search("residue",a)
match_sidechain=re.search("sidechain",a)
match_index=re.search("index",a)
if match_residue:
temp = a.split()
temp.remove('residue')
for item in temp:
residue_index.append(eval(item))
elif match_sidechain:
temp = a.split()
temp.remove('sidechain')
for item in temp:
sidechain_index.append(eval(item))
elif match_index:
temp = a.split()
temp.remove('index')
for item in temp:
index_index.append(eval(item))
for lista in [residue_index, sidechain_index, index_index]:
temp = set(lista)
temp_2 = list(temp)
lista = temp_2.sort()
return residue_index, sidechain_index, index_index
def read_rsi_names(file, flag_line, end_line):
"""reads residue names contained between lines starting with flag_line
and end_line from file
---
file : file object
flag_line : string
end_line : string
---
Returns a list:
residue_names
"""
residue_names = []
file.seek(0)
while True:
a = file.readline()
if not a:
break
match_flag=re.search(flag_line,a)
if match_flag:
while True:
a = file.readline()
if not a:
break
elif re.search(end_line,a):
break
match_residue=re.search("resname",a)
if match_residue:
temp = a.split()
temp.remove('resname')
for item in temp:
residue_names.append(item)
temp = set(residue_names)
temp_2 = list(temp)
temp_2.sort()
return residue_names
def input_read_qm_part(file, residues, atoms, layer="H"):
"""
Reads H_layer on M_layer from the input file
Sets the oniom_layer attribute of atoms to 'H' or 'M'
Returns a list qm_part with atom objects making the QM part (H_layer or M_layer)
(the list is sorted wrt atom index)
----
file : file object
residues : a list with residue obects for the system
atoms : a list with atom obects for the system
"""
assert (type(residues) == list), "residues must be a list of residue objects"
assert (type(atoms) == list), "atoms must be a list of atom objects"
qm_part = []
if layer == "H":
start_flag = "%H_layer"
end_flag = "%end_H_layer"
elif layer == "M":
start_flag = "%M_layer"
end_flag = "%end_M_layer"
residue_index,sidechain_index,index_index = read_rsi_index(file, start_flag, end_flag)
if (len(residue_index) + len(sidechain_index) + len(index_index))>0:
if layer == "H":
print('\nThe H-layer of the system will consist of: ')
elif layer == "M":
print('\nThe M-layer of the system will consist of: ')
print('residues: ',residue_index)
print('and sidechains: ',sidechain_index)
print('and atoms: ',index_index,'\n')
for i in residue_index:
if residues[i]:
for at in residues[i].get_atoms():
qm_part.append(at)
else:
print('\n Residue list does not have residue with index: ',i, '\n')
for i in sidechain_index:
if residues[i]:
for at in residues[i].get_atoms():
if not at.get_in_mainchain():
qm_part.append(at)
else:
print('\n Residue list does not have residue with index: ',i, '\n')
for i in index_index:
if atoms[i]:
qm_part.append(atoms[i])
else:
print('\n Atom list does not have atom with index: ',i,'\n')
temp = set(qm_part)
qm_part = list(temp)
qm_part.sort(key=lambda x: x.get_index(), reverse=True)
for at in qm_part:
if layer == "H":
at.set_oniom_layer('H')
elif layer == "M":
at.set_oniom_layer('M')
return qm_part
def input_read_link_atoms(file, atoms, border="HL"):
"""
Reads link_atoms section from the input file
Returns a list link_atoms with link_atom objects
(the list is sorted wrt atom index)
----
file : file object
atoms : a list with atom obects for the system
border : string, "HL", "HM" or "ML" - at which interface the link atoms are
"""
assert (type(atoms) == list), "atoms must be a list of atom objects"
MM_link_atoms = []
HLA_types = []
link_atoms = []
index_index = []
if border == "HL":
flag_line = "%H_L_link_atoms"
end_line = "%end_H_L_link_atoms"
info_str = " HL "
layer = "H"
elif border == "HM":
flag_line = "%H_M_link_atoms"
end_line = "%end_H_M_link_atoms"
info_str = " HM "
layer = "H"
elif border == "ML":
flag_line = "%M_L_link_atoms"
end_line = "%end_M_L_link_atoms"
info_str = " ML "
layer = "M"
file.seek(0)
while True:
a = file.readline()
if not a:
break
match_flag=re.search(flag_line,a)
if match_flag:
while True:
a = file.readline()
if not a:
break
elif re.search(end_line,a):
break
match_index=re.search("index",a)
match_type=re.search("type",a)
if match_index:
temp = a.split()
temp.remove('index')
for item in temp:
index_index.append(eval(item))
elif match_type:
temp = a.split()
temp.remove('type')
for item in temp:
HLA_types.append(item)
if len(index_index) == len(HLA_types) and len(index_index)>0 :
index_index, HLA_types = (list(t) for t in zip(*sorted(zip(index_index, HLA_types))))
print('\nThe atoms replaced by H-link atoms at the' + info_str + 'boarder are ')
print('atoms with index: ', index_index)
print('with H-link atom Amber atom types: ', HLA_types,'\n')
for i in index_index:
if atoms[i]:
MM_link_atoms.append(atoms[i])
else:
print('Atom list does not have atom with index: ',i,'\n')
for at,tp in zip(MM_link_atoms, HLA_types):
link_atoms.append(atom_to_link_atom(at, tp, 0.000001, layer))
return link_atoms
def input_read_freeze(file, residues, atoms):
"""
Reads freeze data from the input file
Sets the frozen atribute of atoms to be fixed to -1
Returns a list frozen with atom objects with frozen = -1
(the list is sorted wrt atom index)
----
file : file object
residues : a list with residue obects for the system
atoms : a list with atom objects for the system
"""
assert (type(residues) == list), "residues must be a list of residue objects"
assert (type(atoms) == list), "atoms must be a list of atom objects"
frozen = []
freeze_reference = []
r_free = read_single_number(file,"%r_free")
residue_index,sidechain_index,index_index = read_rsi_index(file, "%freeze_ref", "%end_freeze_ref")
if (len(residue_index) + len(sidechain_index) + len(index_index))>0:
print('\nThe reference with respect to which FREEZING will be done consist of: ')
print('residues: ',residue_index)
print('and sidechains: ',sidechain_index)
print('and atoms: ',index_index)
print('\nAll residues with at least one atom within: ',r_free,' A from the above reference will not be fixed \n')
for i in residue_index:
if residues[i]:
for at in residues[i].get_atoms():
freeze_reference.append(at)
else:
print('\n Residue list does not have residue with index: ',i, '\n')
for i in sidechain_index:
if residues[i]:
for at in residues[i].get_atoms():
if not at.get_in_mainchain():
freeze_reference.append(at)
else:
print('\n Residue list does not have residue with index: ',i, '\n')
for i in index_index:
if atoms[i]:
freeze_reference.append(atoms[i])
else:
print('\n Atom list does not have atom with index: ',i, '\n')
temp = set(freeze_reference)
freeze_reference = list(temp)
freeze_reference.sort(key=lambda x: x.get_index(), reverse=True)
within_resids_bool = residues_within_r_from_atom_list(residues, freeze_reference, r_free)
for w,resid in zip(within_resids_bool,residues):
if not w:
frozen.append(resid)
for at in resid.get_atoms():
at.set_frozen(-1)
return frozen
def read_pdb_file(file):
"""
reads content of the pdb file (in format as produced by prm2gaussian or oniom_inp_mod)
returns a list of residue objects and a list of atom objects
Parameters
----------
file : file object
file object with pdb file to be read.
Returns
-------
residue_list : LIST
a list of residue objects (their index runs from 0).
atom_list : LIST
a list of atom objects (their index runs from 0)
"""
residue_list = []
atom_list = []
new_resid = None
prev_resid_number = -1
file.seek(0)
while True:
line = file.readline()
if not line:
if new_resid:
if new_resid not in residue_list:
residue_list.append(new_resid)
break
line_split = line.split()
if line_split[0] == 'ATOM':
at_LAH = False