-
Notifications
You must be signed in to change notification settings - Fork 6
/
spbsf.py
1331 lines (1168 loc) · 74 KB
/
spbsf.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
# This file is part of PySFD.
#
# Copyright (c) 2018 Sebastian Stolzenberg,
# Computational Molecular Biology Group,
# Freie Universitaet Berlin (GER)
#
# for any feedback or questions, please contact the author:
# Sebastian Stolzenberg <ss629@cornell.edu>
#
# PySFD is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
r"""
=======================================
PySFD - Significant Feature Differences Analyzer for Python
Sparse Pairwise Backbone Sidechain Features (contact frequencies and dwell times)
=======================================
"""
# only necessary for Python 2
from __future__ import print_function as _
from __future__ import division as _
from __future__ import absolute_import as _
import warnings as _warnings
import numpy as _np
import mdtraj as _md
import pandas as _pd
import subprocess as _subprocess
import shlex as _shlex
import glob as _glob
import itertools as _itertools
#import pickle as _pickle
#import os as _os
from pysfd.features import _feature_agent
class _sPBSF(_feature_agent.FeatureAgent):
"""
######################################
Parent Class _sPBSF
######################################
Intermediary class between a particular _sPBSF-derived feature class and
_feature_agent.FeatureAgent
in order to bundle common tasks
If coarse-graining (via df_rgn_seg_res_bb, see below) into regions,
by default aggregate via rgn_agg_func = "sum"
Parameters:
-----------
* error_type : str, default="std_err"
compute feature errors as ...
| "std_err" : ... standard errors
| "std_dev" : ... mean standard deviations
* max_mom_ord : int, default: 1
maximum ordinal of moment to compute
if max_mom_ord > 1, this will add additional entries
"mf.2", "sf.2", ..., "mf.%d" % max_mom_ord, "sf.%d" % max_mom_ord
to the feature tables
* df_rgn_seg_res_bb : optional pandas.DataFrame for coarse-graining that defines
regions by segIDs and resIDs, and optionally backbone/sidechain, e.g.
df_rgn_seg_res_bb = _pd.DataFrame({'rgn' : ["a1", "a2", "b1", "b2", "c"],
'seg' : ["A", "A", "B", "B", "C"],
'res' : [range(4,83), range(83,185), range(4,95), range(95,191), range(102,121)]})
if None, no coarse-graining is performed
* rgn_agg_func : function or str for coarse-graining, default = "sum"
function that defines how to aggregate from residues (backbone/sidechain) to regions in each frame
this function uses the coarse-graining mapping defined in df_rgn_seg_res_bb
- if a function, it has to be vectorized (i.e. able to be used by, e.g., a 1-dimensional numpy array)
- if a string, it has to be readable by the aggregate function of a pandas Data.Frame,
such as "mean", "std"
* df_hist_feats : pandas.DataFrame, default=None
data frame of features, for which to compute histograms.
.columns are self.l_lbl[self.feature_func_name] + ["dbin"], e.g.:
df_hist_feats = pd.DataFrame( { "seg1" : ["A", "A"],
"res1" : [5, 10],
"seg2" : ["A", "A"],
"res2" : [10, 15],
"dbin" : [0.1, 0.1] })
dbin is the histogram binning resolution in units of the feature type.
Only dbin values are allowed, which
sum exactly to the next significant digit's unit, e.g.:
for dbin = 0.02 = 2*10^-2 exists an n = 10, so that
n * dbin = 0.1 = 1*10^-1
Currently - for simplicity - dbin values have to be
the same for each feature.
If df_hist_feats == dbin (i.e. an int or float),
compute histograms for all features with
uniform histogram binning resolution dbin.
* is_with_dwell_times : bool, default=False
compute binary pairwise interactions with mean dwell times (t_on, t_off)?
* label : string, user-specific label for feature_name
"""
def __init__(self, feature_name, error_type, max_mom_ord, df_rgn_seg_res_bb, rgn_agg_func, is_with_dwell_times,
label, df_hist_feats = None, **params):
if rgn_agg_func is None:
rgn_agg_func = "sum"
params["is_with_dwell_times"] = is_with_dwell_times
params["_finish_traj_df"] = self._finish_traj_df
s_coarse = ""
if df_rgn_seg_res_bb is not None:
s_coarse = "coarse."
super(_sPBSF, self).__init__(feature_name = feature_name + s_coarse + error_type + label,
error_type = error_type,
max_mom_ord = max_mom_ord,
df_rgn_seg_res_bb = df_rgn_seg_res_bb,
rgn_agg_func = rgn_agg_func,
df_hist_feats = df_hist_feats,
**params)
@staticmethod
def _finish_traj_df(fself, l_lbl, traj_df, df_rgn_seg_res_bb, rgn_agg_func, df_hist_feats, is_with_dwell_times, is_correlation, r):
"""
helper function of _feature_func_engine:
finishes processing of traj_df in each of
the _feature_func_engine() in the spbsf module
Parameters
----------
* fself : self pointer to foreign master PySFD object
* l_lbl : list of str, feature label types, see below
* traj_df : pandas.DataFrame containing feature labels
* df_rgn_seg_res_bb : optional pandas.DataFrame for coarse-graining that defines
regions by segIDs and resIDs, and optionally backbone/sidechain, e.g.
df_rgn_seg_res_bb = _pd.DataFrame({'rgn' : ["a1", "a2", "b1", "b2", "c"],
'seg' : ["A", "A", "B", "B", "C"],
'res' : [range(4,83), range(83,185), range(4,95), range(95,191), range(102,121)]})
if None, no coarse-graining is performed
* rgn_agg_func : function or str for coarse-graining
function that defines how to aggregate from residues (backbone/sidechain) to regions in each frame
this function uses the coarse-graining mapping defined in df_rgn_seg_res_bb
- if a function, it has to be vectorized (i.e. able to be used by, e.g., a 1-dimensional numpy array)
- if a string, it has to be readable by the aggregate function of a pandas Data.Frame,
such as "mean", "std"
* df_hist_feats : pandas.DataFrame, default=None
data frame of features, for which to compute histograms.
.columns are self.l_lbl[self.feature_func_name] + ["dbin"], e.g.:
df_hist_feats = pd.DataFrame( { "seg1" : ["A", "A"],
"res1" : [5, 10],
"seg2" : ["A", "A"],
"res2" : [10, 15],
"dbin" : [0.1, 0.1] })
dbin is the histogram binning resolution in units of the feature type.
Only dbin values are allowed, which
sum exactly to the next significant digit's unit, e.g.:
for dbin = 0.02 = 2*10^-2 exists an n = 10, so that
n * dbin = 0.1 = 1*10^-1
Currently - for simplicity - dbin values have to be
the same for each feature.
If df_hist_feats == dbin (i.e. an int or float),
compute histograms for all features with
uniform histogram binning resolution dbin.
* is_with_dwell_times : bool
compute binary pairwise interactions with mean dwell times (t_on, t_off)?
* is_correlation : bool, optional, whether or not to output feature values
for a subsequent correlation analysis (e.g. pff.Feature_Correlation())
* r : int, replica index
Returns
-------
* traj_df : pandas.DataFrame, contains all the feature values accumulated for this replica
* dataflags : dict, contains flags with more information about the data in traj_df
"""
dataflags = { "error_type" : fself.error_type[fself._feature_func_name],
"is_with_dwell_times" : is_with_dwell_times }
if fself._feature_func_name in fself.max_mom_ord:
dataflags["max_mom_ord"] = fself.max_mom_ord[fself._feature_func_name]
if is_with_dwell_times and is_correlation:
raise ValueError("both is_with_dwell_times and is_correlation are True!")
def _comp_mean_dwell_times(a, is_std, mylen):
"""
Computes mean dwell times in units of input frames
(equivalent to mean first passage times between the "on" and "off"
states of an interaction
Parameters:
----------
a : sparse input array, including 0-indexed frames
in which the pairwise interaction exists
is_std : using standard deviations (True) or standard errors (False)
mylen : trajectory length
Returns:
----------
avg_t_on, avg_t_off
"""
if len(a) in [0, mylen]:
return None, None
x = _np.in1d(_np.arange(mylen), a).astype(int)
dx = x[1:] != x[:-1]
l = _np.append(_np.where(dx), len(x) - 1)
m = _np.diff(_np.append(-1, l))[:-1]
m1 = _np.mean(m[1::2]) if len(m[1::2]) > 0 else None
m2 = _np.mean(m[::2]) if len(m[::2]) > 0 else None
if is_std:
ms1 = _np.std(m[1::2]) if len(m[1::2]) > 0 else None
ms2 = _np.std(m[::2]) if len(m[::2]) > 0 else None
if x[0] == 0:
return m1, ms1, m2, ms2
elif x[0] == 1:
return m2, ms2, m1, ms1
else:
if x[0] == 0:
return m1, m2
elif x[0] == 1:
return m2, m1
traj_df.drop_duplicates(inplace = True)
numframes = traj_df['frame'].max() + 1
if is_correlation:
traj_df["feature"] = traj_df["seg1"] + "_" + \
traj_df["res1"].astype(str) + "_" + \
traj_df["bb1"].astype(str) + "_" + \
traj_df["seg2"] + "_" + \
traj_df["res2"].astype(str) + "_" + \
traj_df["bb2"].astype(str)
for mycol in ['seg1', 'rnm1', 'res1', 'bb1', 'seg2', 'rnm2', 'res2', 'bb2']:
del traj_df[mycol]
def sparse2full01(x, numframes):
b = _np.zeros(numframes)
b[x.values] = 1
# mark contacts with all "1" for deletion
if _np.all(b == 1):
return float('NaN')
else:
return list(b)
traj_df = traj_df.groupby("feature").agg({"frame" : lambda x : sparse2full01(x, numframes)}).dropna()
traj_df = _pd.DataFrame(_np.array(list(traj_df["frame"].values)), index = traj_df.index)
##traj_df_old = traj_df.copy()
#traj_df.set_index(["bspair", "frame"], inplace = True)
#traj_df = traj_df.unstack(fill_value=0)
#traj_df.columns = traj_df.columns.get_level_values(1)
#del traj_df.columns.name
## delete all rows that have only "1" entries
#traj_df = traj_df.loc[traj_df.sum(axis=1) != traj_df.shape[1]]
##print("len(full_traj):", len(full_traj), "len(traj_df_old):", len(traj_df_old), "len(traj_df):", len(traj_df))
##import pickle
##with open("test.pickle.%d.dat" % (r), "bw") as fout:
## pickle.dump([traj_df_old, traj_df, full_traj], fout)
dataflags["df_rgn_seg_res_bb"] = df_rgn_seg_res_bb
dataflags["l_lbl"] = ['seg1', 'res1', 'bb1', 'seg2', 'res2', 'bb2']
return traj_df, dataflags
if is_with_dwell_times:
if fself.error_type[fself._feature_func_name] == "std_dev":
traj_df = traj_df.groupby(l_lbl).agg({
'f' : 'sum',
'frame' : lambda x: _comp_mean_dwell_times(x, True, traj_df['frame'].max())})
traj_df['f'] /= 1. * numframes
traj_df[['ton', 'ston', 'tof', 'stof']] = traj_df['frame'].apply(_pd.Series)
traj_df['sf'] = _np.sqrt(traj_df['f'] * (1 - traj_df['f']) * numframes / (numframes - 1))
elif fself.error_type[fself._feature_func_name] == "std_err":
traj_df = traj_df.groupby(l_lbl).agg({
'f' : 'sum',
'frame' : lambda x: _comp_mean_dwell_times(x, False, traj_df['frame'].max())})
traj_df['f'] /= 1. * numframes
traj_df[['ton', 'tof']] = traj_df['frame'].apply(_pd.Series)
del traj_df['frame']
else:
def myhist(a_data, dbin):
if _np.any(a_data.isnull()):
return _np.float("NaN")
else:
prec = len(str(dbin).partition(".")[2])+1
a_data = _np.concatenate((a_data, (numframes-len(a_data)) * [0]))
a_bins =_np.arange(_np.floor(min(0,a_data.min()) / dbin),
_np.ceil(a_data.max() / dbin) + 1, 1) * dbin
a_hist = _np.histogram(a_data, bins = a_bins, density = True)
return tuple(list(a_hist))
if df_rgn_seg_res_bb is None:
# if include ALL feature entries for histogramming:
if isinstance(df_hist_feats, (int, float)):
dbin = df_hist_feats
traj_df_hist = traj_df.reset_index()
# elif include NO feature entries for histogramming:
elif df_hist_feats is None:
dbin = None
traj_df_hist = None
# else (if include SOME feature entries for histogramming):
elif isinstance(df_hist_feats, _pd.DataFrame):
dbin = df_hist_feats["dbin"][0]
traj_df_hist = traj_df.merge(df_hist_feats.drop(columns = "dbin"), how = "right")
if traj_df_hist is not None:
traj_df_hist = traj_df_hist.groupby(l_lbl).agg( { "f" : lambda x: myhist(x, dbin ) } ).reset_index()
traj_df_hist.rename(columns = { "f" : "fhist" }, inplace = True)
traj_df = traj_df.groupby(l_lbl).agg({ 'f' : 'sum' })
traj_df.rename(columns = { 'f' : 'N' }, inplace = True)
traj_df['f'] = traj_df['N'] / numframes
l_flbl = ['f']
#import scipy.stats as _scipy_stats
#import numpy as np
#numframes = 4
#print(numframes)
#a_data = np.random.randint(0,2,numframes)
#print(a_data)
#N = a_data.sum()
#print(N)
#f = a_data.mean()
#print(f)
#mymom = 4
#mymoment = ( N * (1 - f)**mymom + \
# (numframes - N) * (-f)**mymom) / \
# (numframes)
#print(mymoment)
#print(_scipy_stats.moment(a_data, moment = mymom))
for mymom in range(2, fself.max_mom_ord[fself._feature_func_name]+1):
l_flbl += ['f.%d' % mymom]
#traj_df['f.%d' % mymom] = _scipy_stats.moment(a_f, axis=0, moment = mymom)
traj_df['f.%d' % mymom] = (traj_df['N'] * (1 - traj_df['f'])**mymom + \
(numframes - traj_df['N']) * (-traj_df['f'])**mymom) / \
(numframes)
traj_df = traj_df[l_flbl].copy()
traj_df.reset_index(inplace = True)
if fself.error_type[fself._feature_func_name] == "std_dev":
traj_df['sf'] = _np.sqrt(traj_df['f'] * (1 - traj_df['f']) * numframes / (numframes - 1))
if traj_df_hist is not None:
traj_df = traj_df.merge(traj_df_hist, how = "outer")
traj_df.reset_index(drop = True, inplace = True)
elif df_rgn_seg_res_bb is not None:
if "bb" in df_rgn_seg_res_bb.columns:
traj_df_seg1_res1 = traj_df[["seg1", "res1", "bb1"]].drop_duplicates()
traj_df_seg1_res1.rename(columns={"seg1" : "seg", "res1" : "res", "bb1" : "bb"}, inplace = True)
traj_df_seg2_res2 = traj_df[["seg2", "res2", "bb2"]].drop_duplicates()
traj_df_seg2_res2.rename(columns={"seg2" : "seg", "res2" : "res", "bb2" : "bb"}, inplace = True)
df_rgn1_seg1_res1 = df_rgn_seg_res_bb.copy()
df_rgn1_seg1_res1.columns = ["rgn1", "seg1", "res1", "bb1"]
df_rgn2_seg2_res2 = df_rgn_seg_res_bb.copy()
df_rgn2_seg2_res2.columns = ["rgn2", "seg2", "res2", "bb2"]
else:
traj_df_seg1_res1 = traj_df[["seg1", "res1"]].drop_duplicates()
traj_df_seg1_res1.rename(columns={"seg1" : "seg", "res1" : "res"}, inplace = True)
traj_df_seg2_res2 = traj_df[["seg2", "res2"]].drop_duplicates()
traj_df_seg2_res2.rename(columns={"seg2" : "seg", "res2" : "res"}, inplace = True)
df_rgn1_seg1_res1 = df_rgn_seg_res_bb.copy()
df_rgn1_seg1_res1.columns = ["rgn1", "seg1", "res1"]
df_rgn2_seg2_res2 = df_rgn_seg_res_bb.copy()
df_rgn2_seg2_res2.columns = ["rgn2", "seg2", "res2"]
traj_df_seg_res = _pd.concat([traj_df_seg1_res1, traj_df_seg2_res2]).drop_duplicates()
df_merge = traj_df_seg_res.merge(df_rgn_seg_res_bb, how = "outer", copy = False, indicator = True)
df_merge = df_merge.query("_merge == 'right_only'")
if len(df_merge) > 0:
warnstr = "df_rgn_seg_res_bb, your coarse-graining definition, has resID entries that are not in your feature list:\n%s" % df_merge
_warnings.warn(warnstr)
traj_df = traj_df.merge(df_rgn1_seg1_res1, copy = False)
traj_df = traj_df.merge(df_rgn2_seg2_res2, copy = False)
#rgn1="a1H2"
#rgn2="a1H2"
#print(traj_df.query("rgn1 == '%s' and rgn2 == '%s'" % (rgn1, rgn2)))
traj_df = traj_df.groupby(["rgn1", "rgn2", "frame"]).agg( { "f" : rgn_agg_func } )
# if include ALL feature entries for histogramming:
if isinstance(df_hist_feats, (int, float)):
dbin = df_hist_feats
traj_df_hist = traj_df.reset_index()
# elif include NO feature entries for histogramming:
elif df_hist_feats is None:
dbin = None
traj_df_hist = None
# else (if include SOME feature entries for histogramming):
elif isinstance(df_hist_feats, _pd.DataFrame):
dbin = df_hist_feats["dbin"][0]
traj_df_hist = traj_df.reset_index().merge(df_hist_feats.drop(columns = "dbin"), how = "right")
if traj_df_hist is not None:
traj_df_hist = traj_df_hist.groupby(["rgn1", "rgn2"]).agg( { "f" : lambda x: myhist(x, dbin ) } )
traj_df_hist.rename(columns = { "f" : "fhist" }, inplace = True)
if fself.error_type[fself._feature_func_name] == "std_err":
traj_df = traj_df.groupby(['rgn1', 'rgn2']).agg( { 'f' : 'sum' } )
#print(traj_df.query("rgn1 == '%s' and rgn2 == '%s'" % (rgn1, rgn2)))
traj_df.rename(columns = { 'f' : 'N' }, inplace = True)
traj_df['f'] = traj_df['N'] / numframes
l_flbl = ['f']
for mymom in range(2, fself.max_mom_ord[fself._feature_func_name]+1):
l_flbl += ['f.%d' % mymom]
#traj_df['f.%d' % mymom] = _scipy_stats.moment(a_f, axis=0, moment = mymom)
traj_df['f.%d' % mymom] = (traj_df['N'] * (1 - traj_df['f'])**mymom + \
(numframes - traj_df['N']) * (-traj_df['f'])**mymom) / \
(numframes)
traj_df = traj_df[l_flbl].copy()
elif fself.error_type[fself._feature_func_name] == "std_dev":
#traj_df.set_index(['rgn1', 'rgn2'] + l_lbl + ['frame'], inplace = True)
#rgn1="a1H2"
#rgn2="a1H2"
#print(traj_df.query("rgn1 == '%s' and rgn2 == '%s'" % (rgn1, rgn2)))
# to sum up all the contacts between two regions:
#traj_df = traj_df.groupby(['rgn1', 'rgn2', 'frame']).agg( { 'f' : rgn_agg_func } )
#print(traj_df.query("rgn1 == '%s' and rgn2 == '%s'" % (rgn1, rgn2)))
# the following unusal way to compute mean/std frequencies
# for each pairwise interaction
# accounts for missing contact entries of
# zero-contact frames
mygroup = traj_df.groupby(['rgn1', 'rgn2'])
traj_df['mf'] = 1. * mygroup.transform('sum') / numframes
traj_df['sf'] = (traj_df['f'] - traj_df['mf']) ** 2
traj_df['sfcount'] = 1
traj_df = traj_df.groupby(['rgn1', 'rgn2']).agg({'mf' : lambda g: g.iloc[0],
'sf' : _np.sum,
'sfcount' : _np.sum})
# add contributions from missing contact entries,
# i.e. frames with zero contacts
traj_df['sf'] += (numframes - traj_df['sfcount']) * traj_df['mf'] ** 2
traj_df['sf'] = _np.sqrt(1. * traj_df['sf'] / (numframes - 1))
del traj_df['sfcount']
traj_df.rename(columns={'mf': 'f'}, inplace = True)
#print(traj_df.query("rgn1 == '%s' and rgn2 == '%s'" % (rgn1, rgn2)))
if traj_df_hist is not None:
traj_df = traj_df.merge(traj_df_hist, left_index = True, right_index = True, how = "outer")
traj_df.reset_index(inplace = True)
traj_df['r'] = r
#traj_df.reset_index(inplace = True)
return traj_df, dataflags
class HBond_mdtraj(_sPBSF):
"""
Computes hydrogen bonds via mdtraj.baker_hubbard()
(which is slow compared to hbond_vmd)
for a particular simulation with replica index r
Parameters
----------
(see in _sPBSF parent class docstring below)
"""
__doc__ = __doc__ + _sPBSF.__doc__
def __init__(self, error_type = "std_err", max_mom_ord = 1, df_rgn_seg_res_bb = None, rgn_agg_func = None, df_hist_feats = None, is_with_dwell_times = False, label = ""):
super(HBond_mdtraj, self).__init__(feature_name = "spbsf.HBond_mdtraj.",
error_type = error_type,
max_mom_ord = max_mom_ord,
df_rgn_seg_res_bb = df_rgn_seg_res_bb,
rgn_agg_func = rgn_agg_func,
df_hist_feats = df_hist_feats,
is_with_dwell_times = is_with_dwell_times,
label = label)
@staticmethod
def _feature_func_engine(args, params):
"""
Computes hydrogen bonds via mdtraj.baker_hubbard() (slow)
for a particular simulation with replica index r
Parameters
----------
* args : tuple (fself, myens, r):
* fself : self pointer to foreign master PySFD object
* myens : string
Name of simulated ensemble
* r : int, replica index
* params : dict, extra parameters as keyword arguments
* error_type : str
compute feature errors as ...
| "std_err" : ... standard errors
| "std_dev" : ... mean standard deviations
* max_mom_ord : int, default: 1
maximum ordinal of moment to compute
if max_mom_ord > 1, this will add additional entries
"mf.2", "sf.2", ..., "mf.%d" % max_mom_ord, "sf.%d" % max_mom_ord
to the feature tables
* df_rgn_seg_res_bb : optional pandas.DataFrame for coarse-graining that defines
regions by segIDs and resIDs, and optionally backbone/sidechain, e.g.
df_rgn_seg_res_bb = _pd.DataFrame({'rgn' : ["a1", "a2", "b1", "b2", "c"],
'seg' : ["A", "A", "B", "B", "C"],
'res' : [range(4,83), range(83,185), range(4,95), range(95,191), range(102,121)]})
if None, no coarse-graining is performed
* rgn_agg_func : function or str for coarse-graining
function that defines how to aggregate from residues (backbone/sidechain) to regions in each frame
this function uses the coarse-graining mapping defined in df_rgn_seg_res_bb
- if a function, it has to be vectorized (i.e. able to be used by, e.g., a 1-dimensional numpy array)
- if a string, it has to be readable by the aggregate function of a pandas Data.Frame,
such as "mean", "std"
* df_hist_feats : pandas.DataFrame, default=None
data frame of features, for which to compute histograms.
.columns are self.l_lbl[self.feature_func_name] + ["dbin"], e.g.:
df_hist_feats = pd.DataFrame( { "seg1" : ["A", "A"],
"res1" : [5, 10],
"seg2" : ["A", "A"],
"res2" : [10, 15],
"dbin" : [0.1, 0.1] })
dbin is the histogram binning resolution in units of the feature type.
Only dbin values are allowed, which
sum exactly to the next significant digit's unit, e.g.:
for dbin = 0.02 = 2*10^-2 exists an n = 10, so that
n * dbin = 0.1 = 1*10^-1
Currently - for simplicity - dbin values have to be
the same for each feature.
If df_hist_feats == dbin (i.e. an int or float),
compute histograms for all features with
uniform histogram binning resolution dbin.
"""
l_lbl1 = ["seg1", "res1", "rnm1", "bb1", "anm1"]
l_lbl2 = ["seg2", "res2", "rnm2", "bb2", "anm2"]
l_lbl = l_lbl1[:-1] + l_lbl2[:-1]
fself, myens, r = args
fself.error_type[fself._feature_func_name] = params["error_type"]
fself.max_mom_ord[fself._feature_func_name] = params["max_mom_ord"]
df_rgn_seg_res_bb = params["df_rgn_seg_res_bb"]
rgn_agg_func = params["rgn_agg_func"]
df_hist_feats = params["df_hist_feats"]
is_with_dwell_times = params["is_with_dwell_times"]
is_correlation = params.get("is_correlation", False)
_finish_traj_df = params["_finish_traj_df"]
instem = 'input/%s/r_%05d/%s.r_%05d.prot' % (myens, r, myens, r)
mytraj = _md.load('%s.%s' % (instem, fself.intrajformat), top='%s.pdb' % instem)
a_rnm = fself._get_raw_topology_ids('%s.pdb' % instem, "atom").rnm.values
a_atom = list(mytraj.topology.atoms)
a_seg = [a.segment_id for a in a_atom]
a_res = [a.residue.resSeq for a in a_atom]
#a_rnm = [a.residue.name for a in a_atom]
a_anm = [a.name for a in a_atom]
a_bb = [fself.is_bb(a.name) for a in a_atom]
df_lbl1 = _pd.DataFrame(data={'seg1': a_seg, 'res1': a_res, 'rnm1': a_rnm, 'bb1': a_bb, 'anm1': a_anm},
columns=l_lbl1)
df_lbl2 = _pd.DataFrame(data={'seg2': a_seg, 'res2': a_res, 'rnm2': a_rnm, 'bb2': a_bb, 'anm2': a_anm},
columns=l_lbl2)
traj_df = []
#picklefname = "output/tmp/%s.%s/%s/r_%05d/%s.%s.%s.r_%05d.pickle.dat" % (fself.feature_func_name, fself.intrajdatatype, myens, r, fself.feature_func_name, fself.intrajdatatype, myens, r)
#if not _os.path.isfile(picklefname):
if True:
for i in range(len(mytraj)):
pai_inds = _md.baker_hubbard(mytraj[i], exclude_water=False, periodic=False)
a = df_lbl1.iloc[pai_inds[:, 0]].reset_index(drop=True)
b = df_lbl2.iloc[pai_inds[:, 2]].reset_index(drop=True)
traj_df.append(_pd.concat([a, b], axis=1))
traj_df[-1]['frame'] = i
# if (i % 10) == 0:
# print(i)
traj_df = _pd.concat(traj_df, copy=False)
#_subprocess.Popen(_shlex.split("mkdir -p output/tmp/%s.%s/%s/r_%05d" % (fself.feature_func_name, fself.intrajdatatype, myens, r))).wait()
#with open(picklefname, "wb") as f:
# _pickle.dump(traj_df, f)
#else:
# with open(picklefname, "rb") as f:
# print("reloading %s" % picklefname)
# traj_df = _pickle.load(f)
if fself.maxnumframes > 0:
traj_df = traj_df.query("frame < %d" % fself.maxnumframes).copy()
for mycol in ['anm1', 'anm2']:
del traj_df[mycol]
traj_df.drop_duplicates(inplace = True)
traj_df = traj_df[l_lbl + ['frame']]
#traj_df['f'] = 1. / len(mytraj)
# now, number of contacts, later mean of number of contacts = frequency
traj_df['f'] = 1
return _finish_traj_df(fself, l_lbl, traj_df, df_rgn_seg_res_bb, rgn_agg_func, df_hist_feats, is_with_dwell_times, is_correlation, r)
class Hvvdwdist_VMD(_sPBSF):
"""
For a particular simulation with replica index r, compute non-covalent,
heavy atom van der Waals radius contacts with VMD
(see features/scripts/b1.1.a.compute_sPBSFs.hvvdwdist.tcl and
Venkatakrishnan, A., Deupi, X., Lebon, G., Tate, C. G., Schertler, G. F., and Babu, M. M. (2013)
Molecular signatures of g-protein-coupled receptors. Nature, 494(7436), 185–194.
for details)
Parameters
----------
* l_solv_rnm : optional list of additional solvent residue names (str, in VMD: "resname")
* l_anm : optional list of additional solvent atom names (str, in VMD: "name")
* l_rad : optional list of additional solvent vdW radii (float, in VMD: "name"), corresponds to l_anm
* solv_seg : str, segID name that will represent all solvent molecules
(see more in _sPBSF parent class docstring below)
"""
__doc__ = __doc__ + _sPBSF.__doc__
def __init__(self, error_type = "std_err", max_mom_ord = 1, df_rgn_seg_res_bb = None, rgn_agg_func = None, df_hist_feats = None, is_with_dwell_times = False, l_solv_rnm = None,
l_anm = None, l_rad = None, solv_seg = "X", label = ""):
if l_solv_rnm is None:
l_solv_rnm = ["\\\"Cl-\\\"", "\\\"Na\\\\+\\\"", "WAT"]
if l_anm is None:
l_anm = ["\\\"Cl-\\\"", "\\\"Na\\\\+\\\""]
if l_rad is None:
l_rad = [1.9, 1.5]
super(Hvvdwdist_VMD, self).__init__(feature_name = "spbsf.Hvvdwdist_VMD.",
error_type = error_type,
max_mom_ord = max_mom_ord,
df_rgn_seg_res_bb = df_rgn_seg_res_bb,
rgn_agg_func = rgn_agg_func,
df_hist_feats = df_hist_feats,
is_with_dwell_times = is_with_dwell_times,
label = label,
l_solv_rnm = l_solv_rnm,
l_anm = l_anm,
l_rad = l_rad,
solv_seg = solv_seg)
@staticmethod
def _feature_func_engine(args, params):
"""
For a particular simulation with replica index r, compute non-covalent,
heavy atom van der Waals radius contacts with VMD
(see features/scripts/b1.1.a.compute_sPBSFs.hvvdwdist.tcl and
Venkatakrishnan, A., Deupi, X., Lebon, G., Tate, C. G., Schertler, G. F., and Babu, M. M. (2013)
Molecular signatures of g-protein-coupled receptors. Nature, 494(7436), 185–194.
for details)
Parameters
----------
* args : tuple (fself, myens, r)
* fself : self pointer to foreign master PySFD object
* myens : string, Name of simulated ensemble
* r : int, replica index
* params : dict, extra parameters as keyword arguments
* l_solv_rnm : optional list of additional solvent residue names (str, in VMD: "resname")
* l_anm : optional list of additional solvent atom names (str, in VMD: "name")
* l_rad : optional list of additional solvent vdW radii (float, in VMD: "name"),
corresponds to l_anm
* solv_seg : str, segID name that will represent all solvent molecules
* error_type : str
compute feature errors as ...
| "std_err" : ... standard errors
| "std_dev" : ... mean standard deviations
* max_mom_ord : int, default: 1
maximum ordinal of moment to compute
if max_mom_ord > 1, this will add additional entries
"mf.2", "sf.2", ..., "mf.%d" % max_mom_ord, "sf.%d" % max_mom_ord
to the feature tables
* df_rgn_seg_res_bb : optional pandas.DataFrame for coarse-graining that defines
regions by segIDs and resIDs, and optionally backbone/sidechain, e.g.
df_rgn_seg_res_bb = _pd.DataFrame({'rgn' : ["a1", "a2", "b1", "b2", "c"],
'seg' : ["A", "A", "B", "B", "C"],
'res' : [range(4,83), range(83,185), range(4,95), range(95,191), range(102,121)]})
if None, no coarse-graining is performed
* rgn_agg_func : function or str for coarse-graining
function that defines how to aggregate from residues (backbone/sidechain) to regions in each frame
this function uses the coarse-graining mapping defined in df_rgn_seg_res_bb
- if a function, it has to be vectorized (i.e. able to be used by, e.g., a 1-dimensional numpy array)
- if a string, it has to be readable by the aggregate function of a pandas Data.Frame,
such as "mean", "std"
* df_hist_feats : pandas.DataFrame, default=None
data frame of features, for which to compute histograms.
.columns are self.l_lbl[self.feature_func_name] + ["dbin"], e.g.:
df_hist_feats = pd.DataFrame( { "seg1" : ["A", "A"],
"res1" : [5, 10],
"seg2" : ["A", "A"],
"res2" : [10, 15],
"dbin" : [0.1, 0.1] })
dbin is the histogram binning resolution in units of the feature type.
Only dbin values are allowed, which
sum exactly to the next significant digit's unit, e.g.:
for dbin = 0.02 = 2*10^-2 exists an n = 10, so that
n * dbin = 0.1 = 1*10^-1
Currently - for simplicity - dbin values have to be
the same for each feature.
If df_hist_feats == dbin (i.e. an int or float),
compute histograms for all features with
uniform histogram binning resolution dbin.
"""
l_lbl1 = ["seg1", "res1", "rnm1", "bb1", "anm1"]
l_lbl2 = ["seg2", "res2", "rnm2", "bb2", "anm2"]
l_lbl = l_lbl1[:-1] + l_lbl2[:-1]
fself, myens, r = args
l_solv_rnm = params["l_solv_rnm"]
l_anm = params["l_anm"]
l_rad = params["l_rad"]
solv_seg = params["solv_seg"]
fself.error_type[fself._feature_func_name] = params["error_type"]
fself.max_mom_ord[fself._feature_func_name] = params["max_mom_ord"]
df_rgn_seg_res_bb = params["df_rgn_seg_res_bb"]
rgn_agg_func = params["rgn_agg_func"]
df_hist_feats = params["df_hist_feats"]
is_with_dwell_times = params["is_with_dwell_times"]
is_correlation = params.get("is_correlation", False)
_finish_traj_df = params["_finish_traj_df"]
s_solv_seg = "_".join(solv_seg)
s_solv_rnm = "_".join(l_solv_rnm)
s_anm = "_".join(l_anm)
s_rad = "_".join([str(f) for f in l_rad])
indir = "input/%s/r_%05d" % (myens, r)
instem = "%s.r_%05d.noh" % (myens, r)
outdir = "output/%s/r_%05d/%s/%s" % (myens, r, fself.feature_func_name, fself.intrajdatatype)
_subprocess.Popen(_shlex.split("mkdir -p %s" % outdir)).wait()
mycmd = "vmd -dispdev text -e %s/features/scripts/compute_sPBSF.hvvdwdist.tcl -args %s %s %s %s %s %s %s %s" \
% (fself.pkg_dir, indir, instem, fself.intrajformat, outdir, s_solv_seg, s_solv_rnm, s_anm, s_rad)
outfile = open("%s/log.compute_sPBSFs.hvvdwdist.tcl.log" % outdir, "w")
myproc = _subprocess.Popen(_shlex.split(mycmd), stdout=outfile, stderr=outfile)
myproc.wait()
outfile.close()
traj_df = _pd.read_csv("%s/%s.sPBSF.hvvdwdist.dat" % (outdir, instem), sep=' ',
names=['frame'] + l_lbl)
#traj_df = traj_df.query("not ((seg1 == seg2) and (abs(res2 - res1) <= 4))").copy()
def order(df, blockpair):
# order contact pair IDs blockwise: (seg1,res1,rnm1,bb1)<->(seg2,res2,rnm2,bb2)
# by successively ordering as seg1<seg2, if "seg1==seg2": res1<res2,
# if "seg1==seg2" and "res1==res2": rnm1<rnm2,...
blockpair_01 = [ x for y in blockpair for x in y ]
blockpair_10 = [ x for y in blockpair[::-1] for x in y ]
pairs = _np.transpose(blockpair)
cumboolmask = _pd.Series(_np.ones(len(df), dtype="bool"))
for mypair in pairs:
boolmask = _np.logical_and((df[mypair[0]] >= df[mypair[1]]), cumboolmask)
if not boolmask.any():
break
#boolmask = _np.logical_and((df[mypair[0]] > df[mypair[1]]), cumboolmask)
df.loc[boolmask, blockpair_01] = df.loc[boolmask, blockpair_10].values
cumboolmask = _np.logical_and((df[mypair[0]] == df[mypair[1]]), cumboolmask)
blockpair = [ l_lbl1[:-1], l_lbl2[:-1] ]
order(traj_df, blockpair)
#traj_df['f'] = 1. / (traj_df['frame'].max() + 1)
# now, number of contacts, later mean of number of contacts = frequency
traj_df['f'] = 1.
return _finish_traj_df(fself, l_lbl, traj_df, df_rgn_seg_res_bb, rgn_agg_func, df_hist_feats, is_with_dwell_times, is_correlation, r)
class HBond_VMD(_sPBSF):
"""
Computes hydrogen bonds via VMD using standard parameters
(see features/scripts/b1.1.a.compute_sPBSFs.hbonds.tcl for details)
Parameters
----------
* cutoff_dist : optional, float, VMD hbond distance cutoff (in Angstrom)
* cutoff_angle : optional, float, VMD hbond distance cutoff (in Angstrom)
(see more in _sPBSF parent class docstring below)
"""
__doc__ = __doc__ + _sPBSF.__doc__
def __init__(self, error_type = "std_err", max_mom_ord = 1, df_rgn_seg_res_bb = None, rgn_agg_func = None, df_hist_feats = None, is_with_dwell_times = False, cutoff_dist = 3.0, cutoff_angle = 20, label = ""):
super(HBond_VMD, self).__init__(feature_name = "spbsf.HBond_VMD.",
error_type = error_type,
max_mom_ord = max_mom_ord,
df_rgn_seg_res_bb = df_rgn_seg_res_bb,
rgn_agg_func = rgn_agg_func,
df_hist_feats = df_hist_feats,
is_with_dwell_times = is_with_dwell_times,
label = label,
cutoff_dist = cutoff_dist,
cutoff_angle = cutoff_angle)
@staticmethod
def _feature_func_engine(args, params):
"""
Computes hydrogen bonds via VMD using standard parameters
(see features/scripts/b1.1.a.compute_sPBSFs.hbonds.tcl for details)
Parameters
----------
* args : tuple (fself, myens, r):
* fself : self pointer to foreign master PySFD object
* myens : string
Name of simulated ensemble
* r : int
replica index
* params : dict, extra parameters as keyword arguments
* cutoff_dist : optional, float, VMD hbond distance cutoff (in Angstrom)
* cutoff_angle : optional, float, VMD hbond distance cutoff (in angles)
* error_type : str
compute feature errors as ...
| "std_err" : ... standard errors
| "std_dev" : ... mean standard deviations
* max_mom_ord : int, default: 1
maximum ordinal of moment to compute
if max_mom_ord > 1, this will add additional entries
"mf.2", "sf.2", ..., "mf.%d" % max_mom_ord, "sf.%d" % max_mom_ord
to the feature tables
* df_rgn_seg_res_bb : optional pandas.DataFrame for coarse-graining that defines
regions by segIDs and resIDs, and optionally backbone/sidechain, e.g.
df_rgn_seg_res_bb = _pd.DataFrame({'rgn' : ["a1", "a2", "b1", "b2", "c"],
'seg' : ["A", "A", "B", "B", "C"],
'res' : [range(4,83), range(83,185), range(4,95), range(95,191), range(102,121)]})
if None, no coarse-graining is performed
* rgn_agg_func : function or str for coarse-graining
function that defines how to aggregate from residues (backbone/sidechain) to regions in each frame
this function uses the coarse-graining mapping defined in df_rgn_seg_res_bb
- if a function, it has to be vectorized (i.e. able to be used by, e.g., a 1-dimensional numpy array)
- if a string, it has to be readable by the aggregate function of a pandas Data.Frame,
such as "mean", "std"
* df_hist_feats : pandas.DataFrame, default=None
data frame of features, for which to compute histograms.
.columns are self.l_lbl[self.feature_func_name] + ["dbin"], e.g.:
df_hist_feats = pd.DataFrame( { "seg1" : ["A", "A"],
"res1" : [5, 10],
"seg2" : ["A", "A"],
"res2" : [10, 15],
"dbin" : [0.1, 0.1] })
dbin is the histogram binning resolution in units of the feature type.
Only dbin values are allowed, which
sum exactly to the next significant digit's unit, e.g.:
for dbin = 0.02 = 2*10^-2 exists an n = 10, so that
n * dbin = 0.1 = 1*10^-1
Currently - for simplicity - dbin values have to be
the same for each feature.
If df_hist_feats == dbin (i.e. an int or float),
compute histograms for all features with
uniform histogram binning resolution dbin.
"""
l_lbl1 = ["seg1", "res1", "rnm1", "bb1", "anm1"]
l_lbl2 = ["seg2", "res2", "rnm2", "bb2", "anm2"]
l_lbl = l_lbl1[:-1] + l_lbl2[:-1]
fself, myens, r = args
cutoff_dist = params["cutoff_dist"]
cutoff_angle = params["cutoff_angle"]
fself.error_type[fself._feature_func_name] = params["error_type"]
fself.max_mom_ord[fself._feature_func_name] = params["max_mom_ord"]
df_rgn_seg_res_bb = params["df_rgn_seg_res_bb"]
rgn_agg_func = params["rgn_agg_func"]
df_hist_feats = params["df_hist_feats"]
is_with_dwell_times = params["is_with_dwell_times"]
is_correlation = params.get("is_correlation", False)
_finish_traj_df = params["_finish_traj_df"]
indir = "input/%s/r_%05d" % (myens, r)
instem = "%s.r_%05d.prot" % (myens, r)
outdir = "output/%s/r_%05d/%s/%s" % (myens, r, fself.feature_func_name, fself.intrajdatatype)
_subprocess.Popen(_shlex.split("mkdir -p %s" % outdir)).wait()
_subprocess.Popen(_shlex.split("rm -rf %s/hbplus" % outdir)).wait()
mycmd = "vmd -dispdev text -e %s/features/scripts/compute_sPBSF.hbond.tcl -args %s %s %s %s %f %f" \
% (fself.pkg_dir, indir, instem, fself.intrajformat, outdir, cutoff_dist, cutoff_angle)
outfile = open("%s/log.compute_sPBSFs.hbond.tcl.log" % outdir, "w")
myproc = _subprocess.Popen(_shlex.split(mycmd), stdout=outfile, stderr=outfile)
myproc.wait()
outfile.close()
traj_df = _pd.read_csv("%s/%s.sPBSF.hbond.vmd.dat" % (outdir, instem), sep=' ',
names=['frame'] + l_lbl)
#traj_df['f'] = 1. / (traj_df['frame'].max() + 1)
# now, number of contacts, later mean of number of contacts = frequency
traj_df['f'] = 1.
return _finish_traj_df(fself, l_lbl, traj_df, df_rgn_seg_res_bb, rgn_agg_func, df_hist_feats, is_with_dwell_times, is_correlation, r)
class HBond_HBPLUS(_sPBSF):
"""
Computes hydrogren bonds via the HBPLUS program
(more refined than VMD or mdtraj, i.e. more hard-wired parameters
used to infer hydrogen bonds):
I.K. McDonald and J.M. Thornton (1994), "Satisfying Hydrogen Bonding Potential in Proteins", JMB 238:777-793.
how to obtain HBPLUS:
http://www.ebi.ac.uk/thornton-srv/software/HBPLUS
Before you instantiate this feature class, please double-check
the path in the "hbdir" environment variable in
scripts/compute_PI.hbplus.sh
, e.g.,
export hbdir=/home/sstolzen/mypackages/hbplus
, and make this file executable:
chmod +x scripts/compute_PI.hbplus.sh
Parameters
----------
(see in _sPBSF parent class docstring below)
"""
__doc__ = __doc__ + _sPBSF.__doc__
def __init__(self, error_type = "std_err", max_mom_ord = 1, df_rgn_seg_res_bb = None, rgn_agg_func = None, df_hist_feats = None, is_with_dwell_times = False, label = ""):
super(HBond_HBPLUS, self).__init__(feature_name = "spbsf.HBond_HBPLUS.",
error_type = error_type,
max_mom_ord = max_mom_ord,
df_rgn_seg_res_bb = df_rgn_seg_res_bb,
rgn_agg_func = rgn_agg_func,
df_hist_feats = df_hist_feats,
is_with_dwell_times = is_with_dwell_times,
label = label)
@staticmethod
def _feature_func_engine(args, params):
"""
Computes hydrogren bonds via the HBPLUS program (more refined than VMD or mdtraj:
I.K. McDonald and J.M. Thornton (1994), "Satisfying Hydrogen Bonding Potential in Proteins", JMB 238:777-793.
how to obtain HBPLUS:
http://www.ebi.ac.uk/thornton-srv/software/HBPLUS
Parameters
----------
* args : tuple (fself, myens, r)
* fself : self pointer to foreign master PySFD object
* myens : string, Name of simulated ensemble
* r : int, replica index
* params : dict, extra parameters as keyword arguments
* error_type : str
compute feature errors as ...
| "std_err" : ... standard errors
| "std_dev" : ... mean standard deviations
* max_mom_ord : int, default: 1
maximum ordinal of moment to compute
if max_mom_ord > 1, this will add additional entries
"mf.2", "sf.2", ..., "mf.%d" % max_mom_ord, "sf.%d" % max_mom_ord
to the feature tables
* df_rgn_seg_res_bb : optional pandas.DataFrame for coarse-graining that defines
regions by segIDs and resIDs, and optionally backbone/sidechain, e.g.
df_rgn_seg_res_bb = _pd.DataFrame({'rgn' : ["a1", "a2", "b1", "b2", "c"],
'seg' : ["A", "A", "B", "B", "C"],
'res' : [range(4,83), range(83,185), range(4,95), range(95,191), range(102,121)]})
if None, no coarse-graining is performed
* rgn_agg_func : function or str for coarse-graining
function that defines how to aggregate from residues (backbone/sidechain) to regions in each frame
this function uses the coarse-graining mapping defined in df_rgn_seg_res_bb
- if a function, it has to be vectorized (i.e. able to be used by, e.g., a 1-dimensional numpy array)
- if a string, it has to be readable by the aggregate function of a pandas Data.Frame,
such as "mean", "std"
* df_hist_feats : pandas.DataFrame, default=None
data frame of features, for which to compute histograms.
.columns are self.l_lbl[self.feature_func_name] + ["dbin"], e.g.:
df_hist_feats = pd.DataFrame( { "seg1" : ["A", "A"],
"res1" : [5, 10],
"seg2" : ["A", "A"],
"res2" : [10, 15],
"dbin" : [0.1, 0.1] })
dbin is the histogram binning resolution in units of the feature type.
Only dbin values are allowed, which
sum exactly to the next significant digit's unit, e.g.:
for dbin = 0.02 = 2*10^-2 exists an n = 10, so that
n * dbin = 0.1 = 1*10^-1
Currently - for simplicity - dbin values have to be
the same for each feature.
If df_hist_feats == dbin (i.e. an int or float),
compute histograms for all features with