-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateEMPBenchmark.py
executable file
·1454 lines (1245 loc) · 62.6 KB
/
CreateEMPBenchmark.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
#!/usr/bin/env python3.5
# general imports
import argparse
import os
import sys
import time
import copy
import shutil
import subprocess
from scipy.signal import argrelextrema
# 3rd party imports
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from collections import OrderedDict
# from Equation import Expression
from scipy import interpolate
# specific imports
from RosettaFilter import score2dict, score_dict2df, df2boxplots
import RosettaFilter as Rf
from Logger import Logger
mpl.rc_context(fname="/home/labs/fleishman/jonathaw/.matplotlib/publishable_matplotlibrc")
LSF_USERNAME = 'jonatha'
# PWD = os.getcwd() + '/'
# all_configurations_path = PWD + 'all_configs/'
ROSETTA_EXECUTABLES_PATH = '/home/labs/fleishman/jonathaw/Rosetta/main/source/bin/'
ROSETTA_SCRIPTS_EXEC_PATH = 'rosetta_scripts.default.linuxgccrelease'
ROSETTA_DATABASE_PATH = '/home/labs/fleishman/jonathaw/Rosetta/main/database/'
ROSETTA_MEM_POTENTIAL_PATH = ROSETTA_DATABASE_PATH + 'scoring/score_functions/MembranePotential/'
PROTOCOLS_PATH = '/home/labs/fleishman/jonathaw/elazaridis/protocols/'
ELAZAR_POLYVAL_PATH = '/home/labs/fleishman/jonathaw/membrane_prediciton/mother_fucker_MET_LUE_VAL_sym_k_neg.txt'
MPMutateRelax_XML = 'MPMutateRelax.xml'
MPFilterScan_XML = 'MPFilterScan_ELazaridis.xml'
MPFilterScanDifferentSFAAs = 'MPFilterScanDifferentSFAAs.xml'
RMSD_THRESHOLD = 0.5
NUM_AAS = 26
POLY_A_NAME = 'polyA'
NSTRUCT = 1 # 0
MEMBRANE_HALF_WIDTH = 15
TOTAL_HALF_WIDTH = 134.5/2 # 50
LAZARIDIS_POLY_DEG = 4
FLANK_SIZE = 30 # 6
TOTAL_AAS = NUM_AAS + (FLANK_SIZE * 2)
Z = np.linspace(-MEMBRANE_HALF_WIDTH, MEMBRANE_HALF_WIDTH, num=NUM_AAS)
Z_total = np.linspace(-TOTAL_HALF_WIDTH, TOTAL_HALF_WIDTH, num=TOTAL_AAS)
POS_Z_DICT_total = {i + 1: z for i, z in enumerate(Z_total)}
POS_Z_DICT = {i + 1: z for i, z in enumerate(Z)}
POS_RANGE = range(1, TOTAL_AAS+1)
aas_names = ['ALA', 'CYS', 'ASP', 'GLU', 'PHE', 'GLY', 'HIS', 'ILE', 'LYS', 'LEU', 'MET', 'ASN', 'PRO', 'GLN', 'ARG',
'SER', 'THR', 'VAL', 'TRP', 'TYR']
aas_3_1 = {'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'}
aas_1_3 = {v: k for k, v in aas_3_1.items()}
AAs = list('ACDEFGHIKLMNPQRSTVWY')
skip_aas = ['P'] #, 'T', 'S']
COLOR_MAP = {'full': 'blue', 'no_Menv': 'grey', 'ResSolv': 'purple',
'fullCEN': 'blue', 'no_MenvCEN': 'grey', 'ResSolvCEN': 'purple',
'elazar': 'red', 'diff_ips': 'orange', 'diff_ips_CEN': 'orange',
'fa_intra_rep': 'green', 'fa_mpsolv': 'pink', 'fa_rep': 'black',
'p_aa_pp': 'blue', 'rama': 'brown', 'no_res_solv': 'blue', 'beta': 'blue',
'beta_no_res_solv': 'black'}
RMSD_ITERATIONS = {aa: [] for aa in AAs}
z_range_aa = {aa: [-20, +20] if aa not in ['R', 'K', 'H'] else [-23, +20] for
aa in AAs}
SPLINE_SMOOTHNESS = 0
SPLINE_LIM = 25 # 30 1Nov
class InsertionProfile:
"""
a single residue insertion profile described by either a sorted list of PoZEnergy instances (poz_energy) or by
polynum
"""
def __init__(self, aa: str, pos_score: dict, membrane_half_depth: int=MEMBRANE_HALF_WIDTH,
residue_num: int=NUM_AAS, adjust_extra_membranal=True, poly_edges: list=[]):
"""
"""
self.AA = aa
self.membrane_half_depth = membrane_half_depth
self.residue_num = residue_num
# self.extra_membrane_adjusted = adjust_extra_membranal
# if adjust_extra_membranal:
# for pos in pos_score.keys():
# if -20 > POS_Z_DICT_total[pos] or +20 < POS_Z_DICT_total[pos]:
# pos_score[pos] = 0.0
self.pos_score = pos_score
# self.poz_energy = pos_energy_dict_to_PoZEnergy_list(pos_score)
# self.polynom = np.polyfit(Z_total, [x.energy for x in self.poz_energy], LAZARIDIS_POLY_DEG)
self.extramembrane_adjusted = False
self.poly_edges = poly_edges
def __repr__(self):
res = '<InsertionProfile for %s>\n' % self.AA
res += '\t<poz_energy>\n'
for a in self.poz_energy:
res += '\t\t<%s/>\n' % a
res += '\t<poz_energy/>\n'
print('ddd', self.polynom)
try:
res += '<polynom: %.2f*z^4 + %.2f*z^3 %.2f*z^2 %.2f*z + %.2f />' % self.polynom
except:
pass
res += '<InsertionProfile for %s/>\n' % self.AA
return res
def polynom_at_z(self, z: float) -> float:
"""
returns the polynoms value at z
:type z: float
"""
return np.polyval(self.polynom, z)
def format_polyval(self):
"""
print the polyval values for table
"""
return ' '.join([str(a) for a in self.polynom])
def format_spline_energies(self):
"""
:return: string of all energies separated by spaces
"""
# if self.AA not in ['R', 'K', 'H', 'P']:
# return ' '.join(
# str(self.pos_score[pos] if -15 <= POS_Z_DICT_total[pos] <= 15 else 0.0) for pos in POS_RANGE)
# elif self.AA == 'P':
# return ' '.join("0.0" for pos in POS_RANGE)
# else:
# return ' '.join(
# str(self.pos_score[pos] if -23 <= POS_Z_DICT_total[pos] <= 15 else 0.0) for pos in POS_RANGE)
if self.AA not in skip_aas:
# return ' '.join(str(self.pos_score[pos] if z_range_aa[self.AA][0]
# <= POS_Z_DICT_total[pos] <=
# z_range_aa[self.AA][1] else 0.0) for pos in POS_RANGE)
return ' '.join(str(self.pos_score[pos] if -SPLINE_LIM <= POS_Z_DICT_total[pos] <= SPLINE_LIM else 0.0) for pos in POS_RANGE)
else:
logger.log("creating %s spline as 0.0" % self.AA)
return ' '.join("0.0" for pos in POS_RANGE)
def rmsd_ips(self, other) -> float:
"""
retruns the difference between the IPs calculated as by RMSD over Z
"""
res = 0.0
for pos in range(1, TOTAL_AAS+1):
if self.poly_edges[0] <= POS_Z_DICT_total[pos] <= self.poly_edges[1]:
res += (self.pos_score[pos] - other.pos_score[pos])**2
return np.sqrt(np.mean(res))
def adjust_exta_membrane(self):
"""
set all positions outside [-20, 20] to 0. use only for setting splines in Rosetta
:return:
"""
self.extramembrane_adjusted = True
print('ADJUSTING !!! !STOP ME !!!!')
sys.exit()
for pos in POS_RANGE:
if -15 > POS_Z_DICT_total[pos] or +15 < POS_Z_DICT_total[pos]:
self.pos_score[pos] = 0
def pos_energy_dict_to_PoZEnergy_list(pos_energy_dict: dict) -> list():
"""
creates an ordered list of PoZEnergy instances corresponding to their positions
"""
result = []
for pos in range(1, TOTAL_AAS+1):
result.append(PoZEnergy(pos, POS_Z_DICT_total[pos], pos_energy_dict[pos]))
return result
def subtract_IP_from_IP(ip1: InsertionProfile, ip2: InsertionProfile, verbose: bool = False, smooth: bool=True) -> InsertionProfile:
"""
"""
new_pos_score = {}
if not smooth:
for pos in POS_RANGE:
new_pos_score[pos] = ip1.pos_score[pos] - ip2.pos_score[pos]
else:
# smooth the transition from water to membrane between +/-15A to +/-25A for resulting splines
y, x = [], []
for pos in POS_RANGE:
if -SPLINE_LIM > POS_Z_DICT_total[pos] or POS_Z_DICT_total[pos] > SPLINE_LIM:
y.append(0.0)
x.append(pos)
elif ip1.poly_edges[0] <= POS_Z_DICT_total[pos] <= ip1.poly_edges[1]:
y.append(ip1.pos_score[pos] - ip2.pos_score[pos])
x.append(pos)
tck = interpolate.splrep(x, y, s=SPLINE_SMOOTHNESS)
new_pos_score = {pos: interpolate.splev(pos, tck)
if -SPLINE_LIM <= POS_Z_DICT_total[pos] <= +SPLINE_LIM else 0.0
for pos in POS_RANGE}
return InsertionProfile(ip1.AA, new_pos_score)#, adjust_extra_membranal=ip1.extra_membrane_adjusted)
def add_IP_to_IP(ip1: InsertionProfile, ip2: InsertionProfile, verbose: bool = False) -> InsertionProfile:
"""
"""
new_pos_score = {}
for pos in POS_RANGE:
new_pos_score = ip1.pos_score[pos] = ip2.pos_score[pos]
if verbose:
print(pos, ip1.pos_score[pos], ip2.pos_score[pos], ip1.pos_score[pos]+ip2.pos_score[pos])
return InsertionProfile(ip1.AA, new_pos_score)#, adjust_extra_membranal=ip1.extra_membrane_adjusted)
def calibrate_energy_functions(args):
"""
:param args:
:return:
"""
global PWD
# fa_cen_for_scores = OrderedDict(
# dict(score0='centroid', score1='centroid', score2='centroid', score3='centroid', score5='centroid',
# talaris2014='fa_standard'))
# fa_cen_for_scores = OrderedDict(dict(score5='centroid', talaris2014='fa_standard'))
fa_cen_for_scores = OrderedDict(dict(score5='centroid', beta_nov16='fa_standard'))
score_funcs_to_calibrate = list(fa_cen_for_scores.keys())
original_dir = os.getcwd()
logger.log("will calibrate the score functions %r" % score_funcs_to_calibrate)
for en_func in score_funcs_to_calibrate:
if en_func != 'beta_nov16':
continue
# if en_func != 'talaris2014': continue
os.mkdir('%s/%s' % (original_dir, en_func))
os.chdir('%s/%s' % (original_dir, en_func))
logger.create_header("calibrating %s" % en_func)
PWD = '%s/%s/' % (original_dir, en_func)
calibrate_function(en_func + '_elazaridis', fa_cen=fa_cen_for_scores[en_func])
os.chdir('%s/' % original_dir)
def calibrate_function(score_func='talaris2014_elazaridis', fa_cen='fa_standard'):
# create files for running benchmark
if args['full']:
if not args['use_made_pdb']:
create_polyA_fasta()
sequence_to_idealized_helix()
else:
copy_path = '/home/labs/fleishman/jonathaw/elazaridis/file_safe/polyA_inMemb.pdb'
logger.log('USING THE PREMADE SAVE PDB !!! from %s' % copy_path)
shutil.copy(copy_path, 'polyA.pdb')
# create_spanfile()
# trunctate_2nd_mem_res()
# os.system('/home/labs/fleishman/jonathaw/elazaridis/protocols/make_csts.sh %s > %s' % (PWD + POLY_A_NAME + '.pdb', PWD+POLY_A_NAME+'.cst'))
# elazar_ips = create_elazar_ips()
# first FilterScan run. using null ResSolv
full_ips = filterscan_analysis_energy_func(score_func, res_solv_weight=0.0, fa_cen=fa_cen, residues_to_test=AAs,
print_xml=True, adjust_extra_membranal=False)
logger.create_header('creating and adjusting elazar profiles')
elazar_ips = create_elazar_ips()
if False:
for aa in ['V', 'M', 'G', 'T', 'S']:
if aa not in ['S']:
rhs_avg = np.mean([full_ips[aa].pos_score[pos] for pos in range(6, 17)])
elif aa == 'S':
rhs_avg = 0.5 # local maxima in elazar S profile
logger.log('for res %s found mean to be %.2f' % (aa, rhs_avg))
for pos in POS_RANGE:
if aa in ['V', 'M']:
if elazar_ips[aa].pos_score[pos] > rhs_avg:
elazar_ips[aa].pos_score[pos] = rhs_avg
if aa in ['G', 'T']:
if elazar_ips[aa].pos_score[pos] < rhs_avg:
elazar_ips[aa].pos_score[pos] = rhs_avg
if aa in ['S']:
elazar_ips[aa].pos_score[pos] = rhs_avg
else:
for aa in AAs:
rhs_avg = np.mean([full_ips[aa].pos_score[pos] for pos in range(6, 17)])
logger.log('for %s found RHS mean to be %.2f' % (aa, rhs_avg))
for pos in POS_RANGE:
elazar_ips[aa].pos_score[pos] += rhs_avg
if aa in 'FVMTS':
logger.log('bounding %s to %.2f' % (aa, rhs_avg))
for pos in POS_RANGE:
if aa in 'FVM':
if elazar_ips[aa].pos_score[pos] > rhs_avg:
elazar_ips[aa].pos_score[pos] = rhs_avg
if aa in 'T':
if elazar_ips[aa].pos_score[pos] < rhs_avg:
elazar_ips[aa].pos_score[pos] = rhs_avg
if aa == 'S':
elazar_ips[aa].pos_score[pos] = rhs_avg + 0.84
# calc the difference InsertionProfiles between Elazar and Rosetta. assign them as the polynom table
diff_ips = {0: {k: subtract_IP_from_IP(elazar_ips[k], full_ips[k]) for k in AAs}}
create_spline_table(diff_ips[0], 'spline_%s_fa.txt' % score_func, 'spline_test_%s.txt' % ('fa' if fa_cen == 'fa_standard' else 'cen'), args['note'])
# analyse Rosetta again.
MPResSolv_current_ips = {
0: filterscan_analysis_energy_func(score_func, res_solv_weight=1.0, fa_cen=fa_cen, residues_to_test=AAs,
adjust_extra_membranal=False, to_dump_pdbs=False)}
for aa in AAs:
rmsd = elazar_ips[aa].rmsd_ips(MPResSolv_current_ips[0][aa])
RMSD_ITERATIONS[aa].append(rmsd)
logger.log('the RMSD between Elazar and ResSolv for %s is %.2f' % (aa, rmsd))
# as long as one residue's RMSD is higher than threshold, keep iterating
if args['improve']:
args['full'] = True
rmsds = {aa: elazar_ips[aa].rmsd_ips(MPResSolv_current_ips[0][aa]) for
aa in AAs if aa not in skip_aas}
fixed_ips = {}
iter_num = 1
while any([rmsd > RMSD_THRESHOLD for rmsd in rmsds.values()]) and iter_num < 10:
aas_improve = [aa for aa in AAs if aa not in skip_aas if rmsds[aa] > RMSD_THRESHOLD]
if aas_improve == []:
logger.create_header('finished improving')
break
logger.log('starting round %i for AAs %s' % (iter_num, aas_improve))
diff_ips[iter_num] = {'A': diff_ips[iter_num-1]['A']}
# check which residues are "good enough" by RMSD, and fix them.
for aa in AAs:
if aa in skip_aas:
continue
RMSD_ITERATIONS[aa].append(rmsds[aa])
if aa not in fixed_ips.keys() and aa not in aas_improve:
logger.log('fixing %s on round %i' % (aa, iter_num))
fixed_ips[aa] = diff_ips[iter_num-1][aa]
if aa not in aas_improve:
diff_ips[iter_num][aa] = fixed_ips[aa]
for aa in aas_improve:
if aa in skip_aas:
continue
logger.log('improve %s at %.2f round %i' % (aa, rmsds[aa], iter_num))
# create a spline that describes the required profile. in the elazar range (-23, 15 or -15, 15)
# it will be what is required to get to elazar within the membrane.
# in (inf, -25) and (+25, inf) it is 0.
y, x = [], []
for pos in POS_RANGE:
if -SPLINE_LIM > POS_Z_DICT_total[pos] or POS_Z_DICT_total[pos] > SPLINE_LIM:
y.append(0.0)
x.append(pos)
elif elazar_ips[aa].poly_edges[0] <= POS_Z_DICT_total[pos] <= elazar_ips[aa].poly_edges[1]:
# train the spline on the difference between the profile from the previous iteration
# and what is reuqired to pull it closer to the Elazar
y.append(diff_ips[iter_num-1][aa].pos_score[pos] + elazar_ips[aa].pos_score[pos]
- MPResSolv_current_ips[iter_num-1][aa].pos_score[pos])
x.append(pos)
tck = interpolate.splrep(x, y, s=SPLINE_SMOOTHNESS)
diff_ips[iter_num][aa] = InsertionProfile(aa, {pos: interpolate.splev(pos, tck)
if -SPLINE_LIM <= POS_Z_DICT_total[pos] <= +SPLINE_LIM else 0.0
for pos in POS_RANGE})
create_spline_table(diff_ips[iter_num], 'spline_%s_fa_%i.txt' % (score_func, iter_num),
'spline_test_%s.txt' % ('fa' if fa_cen == 'fa_standard' else 'cen'), args['note'])
MPResSolv_current_ips[iter_num] = filterscan_analysis_energy_func(score_func, res_solv_weight=1.0,
fa_cen=fa_cen,
residues_to_test=AAs,
adjust_extra_membranal=False,
to_dump_pdbs=False)
rmsds = {aa:
elazar_ips[aa].rmsd_ips(MPResSolv_current_ips[iter_num][aa])
for aa in AAs if aa != 'P'}
iter_num += 1
draw_rmsd_plots()
draw_filterscan_profiles(OrderedDict({'ResSolv': MPResSolv_current_ips[iter_num-1],
'elazar': elazar_ips,
'beta_no_res_solv': full_ips}),
cen_fa='%s_%s' % (score_func, fa_cen))
logger.log('finished calibrating %s %s' % (score_func, fa_cen))
logger.log('got these RMSDs:')
for aa in AAs:
if aa not in skip_aas:
logger.log('for %s got %.2f after %i rounds' % (aa, rmsds[aa], iter_num))
def follow_ip(aa, ips, title):
points = [43] # [10, 35, 43, 53, 76]
print('flw %s %s, %s' % (title, aa, ', '.join('%i: %.2f' % (i, ips[aa].pos_score[i]) for i in points)))
def draw_rosetta_profiles(args):
### create files for running benchmark
if args['full']:
create_polyA_fasta()
sequence_to_idealized_helix()
create_spanfile()
trunctate_2nd_mem_res()
###
elazar_ips = create_elazar_ips()
full_ips = filterscan_analysis_energy_func('full', residues_to_test=AAs)
fullCEN_ips = filterscan_analysis_energy_func('fullCEN', residues_to_test=AAs)
MPResSolv_current_ips = filterscan_analysis_energy_func('talaris2014_elazaridis', residues_to_test=AAs, to_dump_pdbs=True)
MPResSolvCEN_current_ips = filterscan_analysis_energy_func('ResSolvCEN', residues_to_test=AAs, to_dump_pdbs=True)
# e_term_ips = create_e_term_specific_profiles('./', args['e_terms'])
dct = OrderedDict({'ResSolv': MPResSolv_current_ips, 'ResSolvCEN': MPResSolvCEN_current_ips,
'elazar': elazar_ips, 'full': full_ips})
# for e_term in args['e_terms']:
# dct[e_term] = e_term_ips[e_term]
draw_filterscan_profiles(dct)
plt.show()
def draw_rosetta_profiles_fa_cen(args):
global PWD
PWD = os.getcwd()+'/'
### create files for running benchmark
if args['full']:
if args['use_made_pdb']:
copy_path = '/home/labs/fleishman/jonathaw/elazaridis/file_safe/polyA_inMemb.pdb'
logger.log('USING THE PREMADE SAVE PDB !!! from %s' % copy_path)
shutil.copy(copy_path, 'polyA.pdb')
else:
create_polyA_fasta()
sequence_to_idealized_helix()
create_spanfile()
trunctate_2nd_mem_res()
###
elazar_ips = create_elazar_ips()
# caluclate and draw full-atom level terms
# fa_terms = ['fa_atr', 'fa_rep', 'fa_intra_rep', 'fa_pair', 'fa_dun', 'hbond_lr_bb', 'hbond_sr_bb', 'hbond_sc', 'p_aa_pp'] # canceled fa_sol, fa_plane, weight=0, 'fa_mpsolv', 'fa_mpenv','fa_mpenv_smooth' , 'ref, 'hbond_bb_sc'
# full_ips = filterscan_analysis_energy_func(args['energy_func_fa'], residues_to_test=AAs, to_dump_pdbs=True)
fa_e_term_ips, fa_terms = create_e_term_specific_profiles(args, './', args['energy_func_fa'])
print('GOT HESES TERMS', fa_terms)
for term in fa_terms:
plt.figure()
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.15, hspace=0.45)
for i, aa in enumerate(AAs):
plt.subplot(5, 4, 1 + i)
# if aa != 'P': continue
# plt.plot(Z_total, [x.energy for x in fa_e_term_ips[term][aa].poz_energy], color='k', label=term)
plt.plot(Z_total, [fa_e_term_ips[term][aa].pos_score[pos] for pos in POS_RANGE], color='k', label=term)
plt.title(aa.upper())
# if aa == 'P':
# plt.ylim([-12, 5])
# else:
# plt.ylim([-5, 5])
plt.suptitle(term)
plt.savefig('fa_%s.png' % term)
plt.close()
def create_e_term_specific_profiles(args, path_pdbs: str, energy_func, res_solv_weight: float=0.0) -> dict:
"""
creates residue specific insertion profiles for every e_term in the list
"""
global PWD
PWD = os.getcwd()+'/'
if args['full']:
if args['use_made_pdb']:
copy_path = '/home/labs/fleishman/jonathaw/elazaridis/file_safe/polyA_inMemb.pdb'
logger.log('USING THE PREMADE SAVE PDB !!! from %s' % copy_path)
shutil.copy(copy_path, 'polyA.pdb')
else:
create_polyA_fasta()
sequence_to_idealized_helix()
create_spanfile()
trunctate_2nd_mem_res()
filterscan_analysis_energy_func('beta_nov16',
res_solv_weight=0.0,
residues_to_test=AAs,
to_dump_pdbs=True,
fa_cen='fa_standard',
)
# get energy terms for all res / position combinations
all_results, e_terms = get_all_e_terms(path_pdbs)
# create DataFrame
df_ = pd.DataFrame()
for aa in AAs:
for pos in range(1, TOTAL_AAS + 1):
dct = {'pos': pos, 'aa': aa}
for e_term in e_terms:
# print('a', all_results[aa][pos].keys())
dct[e_term] = all_results[aa][pos][e_term]
df_ = df_.append(dct, ignore_index=True)
# each res / position combination has a row with values of all e_terms
e_term_ips = {}
for e_term in e_terms:
# find Ala mean
A_mean = df_[df_['aa'] == 'A'][e_term].mean()
# normalise by Ala mean
df_['%s_normed' % e_term] = df_[e_term] - A_mean
# create insertion profiles for e_term
e_term_ips[e_term] = create_insertion_profiles(df_, '%s_normed' % e_term, adjust_extra_membranal=False, smooth=res_solv_weight == 1.0)
return e_term_ips, e_terms
def get_all_e_terms(path_pdbs: str) -> (dict, list):
"""
goes over all residue/position combiantions, and gets the total energy terms out of their files
"""
result = {a: {} for a in AAs}
all_terms = []
for res in sorted(aas_3_1.keys()):
for ind in range(1, TOTAL_AAS+1):
for l in open('polyA.pdbALA%i%s.pdb' % (ind, res), 'r'):
if 'TOTAL_WTD' in l:
s = l.split()
result[aas_3_1[res]][ind] = {s[i].replace(':', ''): float(s[i+1]) for i in range(1, len(s), 2)}
if not all_terms:
all_terms = [s[i].replace(':', '') for i in range(1, len(s), 2)]
return result, all_terms
def create_polyval_table(diff_ips: dict, file_name: str, rosetta_table_name) -> None:
"""
creates a file_name with polynom values for all residues:
A float float float float (highest power first)
"""
with open(PWD+file_name, 'w+') as fout:
for aa in AAs:
fout.write('%s %s\n' % (aa, diff_ips[aa].format_polyval()))
logger.log('created table at %s' % PWD + file_name)
if args['change_rosetta_table']:
shutil.copy(PWD + file_name, ROSETTA_MEM_POTENTIAL_PATH + rosetta_table_name)
logger.log('copied table to %s' % ROSETTA_MEM_POTENTIAL_PATH + rosetta_table_name)
def create_spline_table(diff_ips_: dict, file_name: str, rosetta_spline_table_name, note=None) -> None:
"""
:param diff_ips_: {AA: IP}
:param file_name: local file in which to create the table
:param rosetta_spline_table_name: destination to which place the table if "change_rosetta_table"
:return: None
"""
with open(PWD+file_name, 'w+') as fout:
fout.write('# splines generated on %s\n' % time.strftime("%H_%M_%d_%b"))
if note is not None:
fout.write('# NOTE: %s\n' % note)
for aa in AAs:
if aa in skip_aas:
fout.write('%s %s\n' % (aa, InsertionProfile(aa, {}).format_spline_energies()))
else:
fout.write('%s %s\n' % (aa, diff_ips_[aa].format_spline_energies()))
logger.log('created table at %s' % PWD + file_name)
if args['change_rosetta_table']:
shutil.copy(PWD + file_name, ROSETTA_MEM_POTENTIAL_PATH + rosetta_spline_table_name)
logger.log('copied table to %s' % ROSETTA_MEM_POTENTIAL_PATH + rosetta_spline_table_name)
def just_draw_current_profiles():
global PWD
args['full'] = True
PWD = os.getcwd()+'/'
if not args['use_made_pdb']:
logger.log('making a new polyA.pdb!!!!!!')
create_polyA_fasta()
sequence_to_idealized_helix()
create_polyA_fasta()
sequence_to_idealized_helix()
else:
copy_path = '/home/labs/fleishman/jonathaw/elazaridis/file_safe/polyA_inMemb.pdb'
logger.log('USING THE PREMADE SAVE PDB !!! from %s' % copy_path)
shutil.copy(copy_path, 'polyA.pdb')
# create_spanfile()
# trunctate_2nd_mem_res()
elazar_ips = create_elazar_ips()
MPResSolv_current_ips = filterscan_analysis_energy_func('beta_nov16_elazaridis',
0.0,
'fa_standard',
residues_to_test=AAs,
adjust_extra_membranal=False,
to_dump_pdbs=False)
with_ressolv = filterscan_analysis_energy_func('beta_nov16_elazaridis',
1.0,
'fa_standard',
residues_to_test=AAs,
adjust_extra_membranal=False,
to_dump_pdbs=False)
dct = {'elazar': elazar_ips, 'beta': with_ressolv, 'beta_no_res_solv': MPResSolv_current_ips}
draw_filterscan_profiles(dct, show=True)
def draw_filterscan_profiles(ips_dict: OrderedDict, cen_fa='fa', show: bool = False) -> None:
"""
draws all profiles
"""
plt.figure()
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.15, hspace=0.45)
for i, aa in enumerate(AAs):
plt.subplot(5, 4, 1+i)
for name, ips in ips_dict.items():
plt.plot(Z_total, [ips[aa].pos_score[pos] for pos in POS_RANGE], color=COLOR_MAP[name], label=name)
plt.title(aa.upper())
# if aa != 'P':
# plt.ylim([-5, 5])
plt.xlim([-50, 50])
plt.axvline(z_range_aa[aa][0], color='grey')
plt.axvline(z_range_aa[aa][1], color='grey')
plt.axvline(-SPLINE_LIM, color='blue')
plt.axvline(+SPLINE_LIM, color='blue')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
file_location = '%sprofile_comparison_%s.png' % (PWD, cen_fa)
plt.savefig(file_location, dpi=600)
logger.log('saving profile comparison figure to %s' % file_location)
if show:
plt.show()
else:
plt.close()
def draw_sigmoidal_profiles(elazar_ips: dict) -> None:
from mpl_toolkits.mplot3d import axes3d
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x_neighbors = np.array(range(0, 41))
z_Z = Z
y_energy = [[sigmoid(n, 0.5, -4) * pze.energy for pze in elazar_ips['W'].poz_energy] for n in x_neighbors]
print(x_neighbors)
print(z_Z)
print(y_energy)
ax.plot_wireframe(z_Z, x_neighbors, y_energy, rstride=10, cstride=10)
def sigmoid(x, slope: float, offset: float) -> float:
return 1. / (1 + np.exp(slope*x+offset))
def create_elazar_ips() -> dict:
"""
creates {AA: ip} for the Elazar scale
"""
logger.log('creating InsertionProfiles for Elazar')
elazar_polyval = MakeHydrophobicityGrade()
result = {}
original_ips = {}
for aa in AAs:
pos_score = {i+1: a for i, a in enumerate([np.polyval(elazar_polyval[aas_1_3[aa]], z)
if -20 <= z <= +20 else 0.0 for z in Z_total])}
# pos_score = {i+1: a for i, a in enumerate([np.polyval(elazar_polyval[aas_1_3[aa]], z)
# if -MEMBRANE_HALF_WIDTH <= z <= MEMBRANE_HALF_WIDTH else 0.0
# for z in Z_total])}
# extend positive inside rule into the IN side
if aa in ['R', 'K', 'H']:
edge_score = np.polyval(elazar_polyval[aas_1_3[aa]], -20)
logger.log('adjusting %s -23 <= z <= -20 to be %.2f' % (aa, edge_score))
for pos in POS_RANGE:
if -23 <= POS_Z_DICT_total[pos] <= -20:
pos_score[pos] = edge_score
# force DEQN profiles to be linear, equivalent to the membrane core energy
if aa in ['D', 'E', 'Q', 'N']:
core_avg = np.mean([pos_score[pos] for pos in POS_RANGE if -10 <= POS_Z_DICT_total[pos] <= 10])
for pos in POS_RANGE:
if -20 <= POS_Z_DICT_total[pos] <= 20:
pos_score[pos] = core_avg
# force H to be linear in (0, +20)
if aa == 'H':
zero_to_twenty_avg = np.max([pos_score[pos] for pos in POS_RANGE if -MEMBRANE_HALF_WIDTH <= POS_Z_DICT_total[pos] <= MEMBRANE_HALF_WIDTH])
for pos in POS_RANGE:
if 0 <= POS_Z_DICT_total[pos] <= 20:
pos_score[pos] = zero_to_twenty_avg
# for all other AAs, stop polynom influence at furthest max/min points. this
# prevents the tiny troffs created by the polynom min points
if aa not in ['D', 'Q', 'N', 'E', 'H', 'A', 'T', 'G', 'K', 'R']:
x = np.array([pos_score[pos] for pos in POS_RANGE])
max_pnts = [a+1 for a in argrelextrema(x, np.greater)[0]]
min_pnts = [a+1 for a in argrelextrema(x, np.less)[0]]
# for I, there's a minimum at position 56 which is WRONG, skip it...
if aa == 'I':
min_pnts = [min_pnts[0]]
edge_pnts = [POS_Z_DICT_total[np.min(max_pnts+min_pnts)], POS_Z_DICT_total[np.max(max_pnts+min_pnts)]]
if not any([a > 10 for a in edge_pnts]):
edge_pnts[1] = z_range_aa[aa][1]
if not any([a < -10 for a in edge_pnts]):
edge_pnts[0] = z_range_aa[aa][0]
else:
edge_pnts = [z_range_aa[aa][0], z_range_aa[aa][1]]
logger.log('for %s setting the poly edges at %.2f, %.2f' % (aa, edge_pnts[0], edge_pnts[1]))
original_ips[aa] = InsertionProfile(aa, pos_score, edge_pnts) # for use only to create profiles in kcal/mol
# adjust kcal/mol to REUs according to kcal/mol=0.57REU.
# suggested by "Role of conformational sampling in computing mutation-induced changes in protein structure
# and stability."
# pos_score = {k: v/0.57 for k, v in pos_score.items()}
pos_score = {k: v*2.94 for k, v in pos_score.items()}
ip = InsertionProfile(aa, pos_score=pos_score, poly_edges=edge_pnts)
result[aa] = ip
logger.log('adjusting kcal/mol to REU by REU=kcal/mol / 0.57')
if args['mode'] == 'elazar_profiles':
logger.log('returning original profiles, in kcal/mol')
return original_ips
else:
return result
def create_original_ips_table(args):
ips = create_elazar_ips()
polynoms = {}
plt.figure()
i = 0
pos_in_memb = [pos for pos in POS_RANGE if -15 <= POS_Z_DICT_total[pos] <= 15]
# z_in_memb = np.arane(-15, 15, 100)
for aa, ip in ips.items():
plt.subplot(5, 4, 1+i)
polynoms[aa] = np.polyfit([POS_Z_DICT_total[pos] for pos in pos_in_memb], [ip.pos_score[pos] for pos in pos_in_memb], LAZARIDIS_POLY_DEG)
plt.scatter([POS_Z_DICT_total[pos] for pos in pos_in_memb], [ip.pos_score[pos] for pos in pos_in_memb])
plt.plot([POS_Z_DICT_total[pos] for pos in pos_in_memb], [np.polyval(polynoms[aa], POS_Z_DICT_total[pos]) for pos in pos_in_memb])
plt.title(aa)
plt.ylim([-2, 2])
i += 1
plt.savefig('original_scatter_and_plot.png')
plt.show()
with open('original_ips.txt', 'w+') as fout:
for aa, pln in polynoms.items():
fout.write('%s %f %f %f %f %f\n' % (aa, pln[0], pln[1], pln[2], pln[3], pln[4]))
def create_insertion_profiles(df: pd.DataFrame, column: str, adjust_extra_membranal: bool=True, smooth: bool=False) -> dict:
"""
creates {AA: InsertionProfile} using energy column in df
"""
logger.log('creating InsertionProfiles for %s' % column)
result = {}
for aa in AAs:
# pos_score = {i: df[((df['aa'] == aa) & (df['pos'] == i))][column].values[0] for i in range(1, NUM_AAS + 1)}
pos_score = {i: df[((df['aa'] == aa) & (df['pos'] == i))][column].values[0] for i in POS_RANGE} # switched for -50 to 50
ip = InsertionProfile(aa, pos_score=pos_score, adjust_extra_membranal=adjust_extra_membranal)
result[aa] = ip
return result
def filterscan_analysis() -> pd.DataFrame:
"""
run the FilteScan protocol on the ployA and return results in data frame
"""
# run FilterScan protocol to create sclog files for both full and no M env score functions
if args['full']: # -unmute core.scoring.membrane.MPResSolvEnergy
command = '%s%s -database %s -parser:protocol %s -s %s -mp:setup:spanfiles %s -nstruct %i -overwrite ' \
'-mp:scoring:hbond -mute all' % (ROSETTA_EXECUTABLES_PATH, ROSETTA_SCRIPTS_EXEC_PATH,
ROSETTA_DATABASE_PATH, PROTOCOLS_PATH + MPFilterScan_XML,
PWD + POLY_A_NAME + '.pdb', PWD + POLY_A_NAME + '.span', NSTRUCT)
logger.log('running FilterScan protocol, issuing command:\n%s' % command)
os.system(command)
logger.log('ran FilterScan. finished at %s' % time.strftime("%H:%M_%d%b"))
# parse both sclog files to {pos: {AA: score}}
full_fs_log = parse_filterscan_log('memb_hires_full.sclog')
noMenv_fs_log = parse_filterscan_log('memb_hires_no_Menv.sclog')
MPResSolv_fs_log = parse_filterscan_log('memb_hires_ResSolv.sclog')
# create DataFrame
df = pd.DataFrame()
for aa in AAs:
for pos in range(7, 33+1):
df = df.append({'pos': pos-6, 'aa': aa,
'full': full_fs_log[pos][aa],
'noMenv': noMenv_fs_log[pos][aa],
'res_solv': MPResSolv_fs_log[pos][aa]},
ignore_index=True)
# calculate polyA scores in both score funcitons
full_A_mean = df[df['aa'] == 'A']['full'].mean()
noMenv_A_mean = df[df['aa'] == 'A']['noMenv'].mean()
MPResSolv_A_mean = df[df['aa'] == 'A']['res_solv'].mean()
# calcualte delta of every mutant and the polyA for both score functions
df['full_normed'] = df['full']-full_A_mean
df['noMenv_normed'] = df['noMenv'] - noMenv_A_mean
df['ResSolv_normed'] = df['res_solv'] - MPResSolv_A_mean
logger.log('mean of A at full score is %f' % full_A_mean)
logger.log('mean of A at no M env is %f' % noMenv_A_mean)
logger.log('mean of A at MPResSolv env is %f' % MPResSolv_A_mean)
return df
def filterscan_analysis_energy_func(energy_function: str, res_solv_weight: float, fa_cen: str,
residues_to_test: list=AAs, to_dump_pdbs: bool=False,
adjust_extra_membranal: bool=True, print_xml: bool=False) -> dict:
"""
energy_function = which energy function to calibrate
run the FilteScan protocol on the ployA and return InsertionProfiles dict for energy_function
"""
span_ins_weight = 1
# run FilterScan protocol to create sclog files for both full and no M env score functions
if args['full']: # -unmute core.scoring.membrane.MPResSolvEnergy
if energy_function is None:
print('no energy function provided!')
sys.exit() # -corrections::beta_nov15 -score::elec_memb_sig_die
command = '%s%s -database %s -parser:protocol %s -s %s -nstruct %i -overwrite ' \
'-mp:scoring:hbond -mute all -ex1 -ex2 -ex3 -ex4 -parser:script_vars energy_function=%s ' \
'residues_to_test=%s to_dump=%i res_solv_weight=%.2f fa_or_cen=%s span_ins_weight=%.2f' % \
(ROSETTA_EXECUTABLES_PATH, ROSETTA_SCRIPTS_EXEC_PATH, ROSETTA_DATABASE_PATH,
PROTOCOLS_PATH + MPFilterScanDifferentSFAAs, PWD + POLY_A_NAME + '.pdb', NSTRUCT,
energy_function, ''.join(residues_to_test), 1 if to_dump_pdbs else 0, res_solv_weight, fa_cen, span_ins_weight)
# -mp:setup:spanfiles %s , PWD + POLY_A_NAME + '.span'
if 'beta' in energy_function:
command += ' -corrections::beta_nov16 '
logger.log('ADDING CORRECTIONS FOR beta')
if args['elec_memb_sig_die'] and 'beta' in energy_function:
command += ' -score::elec_memb_sig_die '
logger.log('ADD ELEC_MEMB_SIG_DIE')
if args['memb_fa_sol']:
command += ' -score:memb_fa_sol '
logger.log('USING MEMB_FA_SOL')
logger.log('running FilterScan for the energy function %s protocol, issuing command:\n%s' %
(energy_function, command))
logger.log_text_file('%s' % str(PROTOCOLS_PATH + MPFilterScanDifferentSFAAs), to_print=print_xml)
os.system(command)
logger.log('ran FilterScan for energy function %s. finished at %s' %
(energy_function, time.strftime("%H:%M_%d%b")))
# parse both sclog files to {pos: {AA: score}}
if args['full']:
shutil.move('temp.sclog', '%s.sclog' % energy_function)
logger.log('saved the FilterScan sclog to %s.sclog' % energy_function)
temp_fs_log = parse_filterscan_log('%s.sclog' % energy_function)
# create DataFrame
df = pd.DataFrame()
for aa in AAs:
# for pos in range(FLANK_SIZE+1, NUM_AAS+FLANK_SIZE+1+1):
# df = df.append({'pos': pos-FLANK_SIZE, 'aa': aa, energy_function: temp_fs_log[pos][aa]}, ignore_index=True) # switched for -50 to 50
for pos in range(1, TOTAL_AAS+1):
df = df.append({'pos': pos, 'aa': aa, energy_function: temp_fs_log[pos][aa]}, ignore_index=True)
# calculate polyA scores in both score funcitons
temp_A_mean = df[df['aa'] == 'A'][energy_function].mean()
# calcualte delta of every mutant and the polyA for both score functions
df['%s_normed' % energy_function] = df[energy_function]-temp_A_mean
logger.log('mean of A for %s is %f' % (energy_function, temp_A_mean))
ips = create_insertion_profiles(df, '%s_normed' % energy_function, adjust_extra_membranal, smooth=adjust_extra_membranal)
return ips
def filterscan_parallel(energy_function: str, res_solv_weight: float, fa_cen: str, residues_to_test:
list=AAs, to_dump_pdbs: bool=False, adjust_extra_membranal: bool=True,
print_xml: bool=False, iteration=0) -> dict:
PWD = os.getcwd() + '/'
# run FilterScan protocol to create sclog files for both full and no M env score functions
if args['full']: # -unmute core.scoring.membrane.MPResSolvEnergy
if energy_function is None:
print('no energy function provided!')
sys.exit()
cmd = open('command', 'w+')
if 'A' in residues_to_test:
residues_to_test = [aa for aa in residues_to_test if aa != 'A']
logger.log('creating jobs for residues %s, %i AAs' % (''.join(residues_to_test), len(residues_to_test)))
num = 1
iter_sclog_files = []
for pos in POS_RANGE:
for aa_ in residues_to_test:
aa = aa_ if aa_ != residues_to_test[0] else residues_to_test[0] + 'A'
name = '%s%i' % (aa, pos)
command = '%s%s -database %s -parser:protocol %s -s %s -nstruct %i -overwrite -mp:scoring:hbond -mute all -ex1 -ex2 -ex3 -ex4 -parser:script_vars energy_function=%s residues_to_test=%s to_dump=%i res_solv_weight=%.2f fa_or_cen=%s res_num=%i sclog=%s cst_file=%s' % (ROSETTA_EXECUTABLES_PATH, ROSETTA_SCRIPTS_EXEC_PATH, ROSETTA_DATABASE_PATH, PROTOCOLS_PATH + 'MPFilterScanDifferentSFAAsPos.xml', PWD + POLY_A_NAME + '.pdb', NSTRUCT, energy_function, ''.join(aa), 1 if to_dump_pdbs else 0, res_solv_weight, fa_cen, pos, './pos_%s_%i.sclog' % (aa, pos), PWD+POLY_A_NAME+'.cst')
iter_sclog_files.append('./pos_%s_%i.sclog' % (aa, pos))
with open('job.%s' % name, 'w+') as job:
job.write('#!/bin/bash\n. /usr/share/lsf/conf/profile.lsf\ncd %s\n'
% PWD)
os.system('chmod +x job.%s' % name)
job.write('%s\n' % command)
res = subprocess.check_output('bsub -N -u /dev/null -G fleishman-wx-grp-lsf -q fleishman -o /dev/null 2>&1 -e /dev/null 2>&1 %sjob.%s' % (PWD, name), shell=True)#, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
# print('[%s%s]\r' % ('-'*num, ' '*(len(POS_RANGE)*len(residues_to_test)-num)))
num += 1
cmd.write('bsub -N -u /dev/null -G fleishman-wx-grp-lsf -q fleishman -o /dev/null 2>&1 -e /dev/null 2>&1 %sjob.%s\n' % (PWD, name))
cmd.close()
logger.log('finished submitting jobs')
logger.log('running FilterScan for the energy function %s protocol, issuing command:' % energy_function)
logger.log_text_file('%s' % str(PROTOCOLS_PATH + MPFilterScanDifferentSFAAs), to_print=print_xml)
sclog_files = [a for a in os.listdir('./') if '.sclog' in a and 'pos' in a]
while len(sclog_files) < len(POS_RANGE)*len(residues_to_test):
logger.log('not enough finished %i' % len(sclog_files))
time.sleep(10)
sclog_files = [a for a in os.listdir('./') if '.sclog' in a and 'pos' in a]
for sclog in iter_sclog_files:
os.system('cat %s >> %s_iter_%i.sclog' % (sclog, energy_function, iteration))
temp_fs_log = parse_filterscan_log('%s_iter_%i.sclog' % (energy_function, iteration))
# create DataFrame
df = pd.DataFrame()
for aa in AAs:
for pos in range(1, TOTAL_AAS+1):
df = df.append({'pos': pos, 'aa': aa, energy_function: temp_fs_log[pos][aa]}, ignore_index=True)
# calculate polyA scores in both score funcitons
temp_A_mean = df[df['aa'] == 'A'][energy_function].mean()
# calcualte delta of every mutant and the polyA for both score functions
df['%s_normed' % energy_function] = df[energy_function]-temp_A_mean
logger.log('mean of A for %s is %f' % (energy_function, temp_A_mean))
ips = create_insertion_profiles(df, '%s_normed' % energy_function, adjust_extra_membranal)
logger.log('erasing err.* out.* job.* command and pos_*sclog')
os.system('rm -rf job.* err.* out.* command pos_*sclog')
return ips
def draw_rmsd_plots() -> None:
"""
draw the rmsd over iteration plots
"""
i = 0
plt.figure()
for aa, rmsd_list in RMSD_ITERATIONS.items():
plt.subplot(5, 4, 1 + i)
plt.plot(range(len(rmsd_list)), rmsd_list)
plt.title(aa)
i += 1
plt.savefig('rmsd_plt.png')
plt.close()
def parse_filterscan_log(file_name) -> dict:
"""
parse a FilterScan run log file, returns dict {position: {AA: score}}
"""
result = {i: {aa: None for aa in AAs} for i in range(1, TOTAL_AAS + 1)}
for l in open(file_name, 'r'):
s = l.split()
if len(s) >= 4:
result[int(s[0])][s[2]] = float(s[3])
return result
def analyse_all_configuration():
"""
goes over all configuration score files, returns DF of scores
"""
sc_files = [a for a in os.listdir(PWD + 'all_configs') if '.sc' in a]
for sc_file in sc_files:
sc_df = score_dict2df(score2dict(PWD + 'all_configs/' + sc_file))
print(sc_df)
def run_all_configurations():
"""
:return:
"""
os.mkdir(PWD + 'all_configs')
os.chdir(PWD + 'all_configs')
make_all_configurations_jobs()
os.system('sh command')
sleep_until_jobs_finish()
create_profiles()
os.chdir(PWD)
def create_profiles():
# profiles = {aa: {pos: None for pos in range(FLANK_SIZE + 1, FLANK_SIZE + NUM_AAS + 1)} for aa in aas_names}
profiles = {aa: {pos: None for pos in range(1, TOTAL_AAS + 1)} for aa in aas_names} # switched to this for -50 to 50
sc_files = [a for a in os.listdir(all_configurations_path) if '.sc' in a]
for sc_file in sc_files:
sc = score2dict(all_configurations_path+sc_file)
df = score_dict2df(sc)
aa = [a for a in aas_names if a in sc_file][0]
pos = int(sc_file.split('_')[2].split('.')[0])
profiles[aa][pos] = np.mean(df['score']) - polyA_total_mean
print('for res %s in pos %i found %.3f mean with %.3f std' % (aa, pos, np.mean(df['score']), np.std(df['score'])))
membrane_position = np.linspace(-TOTAL_HALF_WIDTH, TOTAL_HALF_WIDTH, endpoint=True, num=NUM_AAS)
mm_profiles = {aa: OrderedDict((membrane_position[pos - FLANK_SIZE - 1], profiles[aa][pos])
for pos in range(FLANK_SIZE + 1, FLANK_SIZE + NUM_AAS + 1)) for aa in aas_names}
mm_polys = {aa: np.polyfit(list(mm_profiles[aa].keys()), list(mm_profiles[aa].values()), LAZARIDIS_POLY_DEG)
for aa in aas_names}
mm_eqs = {aa: Expression('+'.join('%f*z^%i' % (n, i) for i, n in enumerate(mm_polys[aa][::-1])),
argorder=['z']) for aa, val in mm_polys.items()}