@@ -297,45 +297,35 @@ def draw_links(interactions, color, object_name, coords, state):
297297def calculate_correlation (obj , frames , min_presence = 0.05 , max_presence = 0.95 , coeff_thresh = 0.5 , p_thresh = 0.3 ,
298298 int_type = "HBOND" ):
299299 all_cm = dict ()
300- try :
301- if int_type == "ALL" :
302- for interaction in intTypeMap .keys ():
303- all_cm [interaction ] = pd .read_csv ('/tmp/ring/md/{}.cm_{}' .format (obj , interaction ), sep = ' ' ,
304- header = None )
305- else :
306- all_cm [int_type ] = pd .read_csv ('/tmp/ring/md/{}.cm_{}' .format (obj , int_type ), sep = ' ' , header = None )
307- except FileNotFoundError :
308- return
300+ nodes = []
301+ if int_type == "ALL" :
302+ to_read = intTypeMap .keys ()
303+ else :
304+ to_read = [int_type ]
309305
310- contacts_sparse = dict ()
311- for j in range (1 , frames + 1 ):
312- if int_type == "ALL" :
313- interactions = intTypeMap .keys ()
314- else :
315- interactions = [int_type ]
316- for interaction in interactions :
317- df = all_cm [interaction ][all_cm [interaction ][0 ] == j ]
318- nodes = df [1 ]
306+ for interaction in to_read :
307+ all_cm [interaction ] = pd .read_csv ('/tmp/ring/md/{}.cm_{}' .format (obj , interaction ), sep = ' ' ,
308+ header = None )
309+ if len (nodes ) == 0 :
310+ nodes = all_cm [interaction ][all_cm [interaction ][0 ] == 1 ][1 ]
319311 nodes = [Node (x ) for x in nodes ]
312+
313+ conn_freq = get_freq (obj )
314+ contacts_sparse = dict ()
315+ for frame in range (0 , frames ):
316+ for interaction in to_read :
317+ df = all_cm [interaction ][all_cm [interaction ][0 ] == frame + 1 ]
320318 df = df .iloc [:, 2 :]
321319 matrix = df .values
322320 matrix [np .triu_indices (matrix .shape [0 ])] = 0
323- for i in np .argwhere (matrix > 0 ):
324- node1 = nodes [i [ 0 ] ]
325- node2 = nodes [i [ 1 ] ]
321+ for i , j in np .argwhere (matrix > 0 ):
322+ node1 = nodes [i ]
323+ node2 = nodes [j ]
326324 edge = Edge (sorted ([node1 , node2 ]))
327- contacts_sparse .setdefault (edge , dict ())
328- contacts_sparse [edge ].setdefault (j - 1 , 0 )
329- contacts_sparse [edge ][j - 1 ] += 1
330-
331- to_pop = []
332- for k , v in contacts_sparse .items ():
333- presence = len (v ) / frames
334- if not min_presence < presence < max_presence :
335- to_pop .append (k )
336-
337- for k in to_pop :
338- contacts_sparse .pop (k )
325+ if min_presence < conn_freq [interaction ][edge ] < max_presence :
326+ contacts_sparse .setdefault (edge , dict ())
327+ contacts_sparse [edge ].setdefault (frame , 0 )
328+ contacts_sparse [edge ][frame ] += 1
339329
340330 z = np .zeros ((len (contacts_sparse ), frames ))
341331 for i , contacts_for_frame in enumerate (contacts_sparse .values ()):
@@ -345,14 +335,15 @@ def calculate_correlation(obj, frames, min_presence=0.05, max_presence=0.95, coe
345335 coeffs_matr = np .ones ((z .shape [0 ], z .shape [0 ])) * np .nan
346336 p_matr = np .ones ((z .shape [0 ], z .shape [0 ])) * np .nan
347337
348- for i in range (z .shape [0 ]):
349- for j in range (z .shape [0 ]):
350- if i != j :
351- corr_coeff , p_val = pearsonr (z [i ], z [j ])
352- if p_val < p_thresh and (corr_coeff > coeff_thresh or corr_coeff < - coeff_thresh ):
353- coeffs_matr [i , j ] = corr_coeff
354- p_matr [i , j ] = p_val
338+ indexes = np .triu_indices (z .shape [0 ], k = 1 )
339+ for i , j in zip (indexes [0 ], indexes [1 ]):
355340
341+ corr_coeff , p_val = pearsonr (z [i ], z [j ])
342+ if p_val < p_thresh and (corr_coeff > coeff_thresh or corr_coeff < - coeff_thresh ):
343+ coeffs_matr [i , j ] = corr_coeff
344+ p_matr [i , j ] = p_val
345+ p_matr [np .tril_indices_from (p_matr )] = p_matr [np .triu_indices_from (p_matr )]
346+ coeffs_matr [np .tril_indices_from (coeffs_matr )] = coeffs_matr [np .triu_indices_from (coeffs_matr )]
356347 return list (contacts_sparse .keys ()), coeffs_matr , p_matr
357348
358349
@@ -476,4 +467,5 @@ def export_network_graph(model):
476467
477468
478469if __name__ == '__main__' :
479- export_network_graph ('2h9r' )
470+ # export_network_graph('2h9r')
471+ calculate_correlation ("trj_cl" , 20 )
0 commit comments