Skip to content

Commit 59d8c1c

Browse files
authored
Merge pull request #74 from CAnBioNet/fix_bibc_modularity_crash
Fixed crash and also added option for BiBC on multiple groups
2 parents 6229bcf + 7329427 commit 59d8c1c

File tree

1 file changed

+83
-34
lines changed

1 file changed

+83
-34
lines changed

analysis/calc_network_properties.py

Lines changed: 83 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,10 @@ def create_node_dict(node_list_file, gc_nodes):
242242

243243
return(subnet_dict)
244244

245-
245+
def louv(nodes_from_gc):
246+
part = community_louvain.best_partition(nodes_from_gc)
247+
return(part)
248+
246249
# .
247250
def bibc_mod(nodes_from_gc):
248251
'''
@@ -528,33 +531,74 @@ def node_groups_from_list(fname):
528531
################################################################################
529532
########################## Calculate subnetwork properties #####################
530533
################################################################################
531-
with open(outdir + "subnetwork_properties.txt", "w") as file:
532-
533-
file.write("\n### Subnetwork properties ###\n")
534-
print("Finding subnetwork mean degree...")
534+
if args.bibc:
535+
if bibc_choice == "node_types" or bibc_choice == "node_groups_list":
536+
with open(outdir + "subnetwork_properties.txt", "w") as file:
535537

536-
# Find only the giant component
537-
gc = max(connected_component_subgraphs(G), key=len)
538-
gc_nodes = gc.nodes()
539-
540-
# Assign nodes to subnetworks from the mapping file
541-
subnets = create_node_dict(node_input_file, gc_nodes)
542-
543-
meandeg_dict = {}
544-
for k,v in subnets.items():
545-
total_degree = 0
546-
sg = G.subgraph(v)
547-
for i in sg.nodes():
548-
total_degree += sg.degree[i]
549-
mean_degree = total_degree / nx.number_of_nodes(sg)
550-
meandeg_dict[k] = mean_degree # Make a new key with the name of k and the value of mean_degree
551-
file.write(str(k) + "_mean_degree\t" + str(mean_degree) + "\n")
552-
553-
# Overwrite any previous file with same name instead of appending
554-
file.truncate()
538+
file.write("### Subnetwork properties ###\n")
539+
print("Finding subnetwork mean degree...")
555540

556-
file.close()
541+
# Find only the giant component
542+
gc = max(connected_component_subgraphs(G), key=len)
543+
gc_nodes = gc.nodes()
544+
545+
# Assign nodes to subnetworks from the mapping file
546+
subnets = create_node_dict(node_input_file, gc_nodes)
547+
548+
meandeg_dict = {}
549+
for k,v in subnets.items():
550+
print(k,v)
551+
total_degree = 0
552+
sg = G.subgraph(v)
553+
for i in sg.nodes():
554+
total_degree += sg.degree[i]
555+
mean_degree = total_degree / nx.number_of_nodes(sg)
556+
meandeg_dict[k] = mean_degree # Make a new key with the name of k and the value of mean_degree
557+
file.write(str(k) + "_mean_degree\t" + str(mean_degree) + "\n")
558+
559+
# Overwrite any previous file with same name instead of appending
560+
file.truncate()
561+
562+
file.close()
563+
564+
elif bibc_choice == "modularity":
565+
with open(outdir + "subnetwork_properties.txt", "w") as file:
557566

567+
file.write("### Subnetwork properties ###\n")
568+
print("Finding subnetwork mean degree...")
569+
570+
# Find only the giant component
571+
gc = max(connected_component_subgraphs(G), key=len)
572+
gc_nodes = gc.nodes()
573+
574+
# Find modular regions of the network and assign nodes to them
575+
partitions = louv(gc)
576+
meandeg_dict = {}
577+
578+
partitions_flipped = {}
579+
for k,v in partitions.items():
580+
if v in partitions_flipped:
581+
partitions_flipped[v].append(k)
582+
else:
583+
partitions_flipped[v] = [k]
584+
585+
for k,v in partitions_flipped.items():
586+
total_degree = 0
587+
sg = G.subgraph(v)
588+
for i in sg.nodes():
589+
total_degree += sg.degree[i]
590+
mean_degree = total_degree / nx.number_of_nodes(sg)
591+
meandeg_dict[k] = mean_degree # Make a new key with the name of k and the value of mean_degree
592+
file.write(str(k) + "_mean_degree\t" + str(mean_degree) + "\n")
593+
594+
# Overwrite any previous file with same name instead of appending
595+
file.truncate()
596+
597+
file.close()
598+
else:
599+
with open(outdir + "subnetwork_properties.txt", "w") as file:
600+
file.write("### Subnetwork properties ###\n")
601+
file.write("Subnetwork properties were not calculated. These properties are only calculated if the --bibc flag is used.")
558602
################################################################################
559603
########################## Calculate node properties ###########################
560604
################################################################################
@@ -727,7 +771,7 @@ def parse_RBC_results(rbc_list, otu_pv_list, otu_pv_str):
727771

728772
return(otu_pv_str)
729773

730-
def BiBC(choice, node_file, nodes_in_gc, giantcomp, calc_type, otu_pv_list, otu_pv_str, type1, type2):
774+
def BiBC(choice, nodes_in_gc, giantcomp, calc_type, otu_pv_list, otu_pv_str, t1 = None, t2 = None, mapfile = None):
731775
'''
732776
Calculates RBC/BiBC on the specified nodes
733777
@@ -738,20 +782,19 @@ def BiBC(choice, node_file, nodes_in_gc, giantcomp, calc_type, otu_pv_list, otu_
738782
# If the user wants to calculate based on pre-defined node types
739783
if choice == "node_types":
740784
# Pass the gc nodes to the function that will assign the correct node types to each of the nodes
741-
assigned_types = assign_node_type(node_file, nodes_in_gc, type1, type2)
785+
assigned_types = assign_node_type(mapfile, nodes_in_gc, t1, t2)
742786

743787
# Calculate rbc using the above function which takes the outputs of assign_node_type
744788
rbc = restricted_betweenness_centrality(giantcomp, assigned_types['Type1'], assigned_types['Type2'], bibc_calc_type)
745789

746790
parse_rbc_str = parse_RBC_results(rbc, otu_pv_list, otu_pv_str)
747-
return_str = "BiBC" + "_" + type1 + "_" + type2 + "\t" + parse_rbc_str + "\n"
791+
return_str = "BiBC" + "_" + t1 + "_" + t2 + "\t" + parse_rbc_str + "\n"
748792

749793
return(return_str)
750794

751795
# Otherwise, if they wish to use modularity as the BiBC parameter...
752796
elif choice == "modularity":
753-
nodes_in_gc_for_bibc_mod = max(connected_component_subgraphs(G), key=len)
754-
nodes_from_bibc_mod = bibc_mod(nodes_in_gc_for_bibc_mod)
797+
nodes_from_bibc_mod = bibc_mod(giantcomp)
755798
rbc = restricted_betweenness_centrality(giantcomp, nodes_from_bibc_mod['mod1'], nodes_from_bibc_mod['mod2'], bibc_calc_type)
756799
parse_rbc_str = parse_RBC_results(rbc, otu_pv_list, otu_pv_str)
757800
return_str = "BiBC_between_modular_regions\t" + parse_rbc_str + "\n"
@@ -765,7 +808,7 @@ def BiBC(choice, node_file, nodes_in_gc, giantcomp, calc_type, otu_pv_list, otu_
765808
multi_groups_out_str = ""
766809

767810
for i in all_group_pairs:
768-
assigned_types = assign_node_type(node_file, nodes_in_gc, i[0], i[1])
811+
assigned_types = assign_node_type(mapfile, nodes_in_gc, i[0], i[1])
769812

770813
rbc = restricted_betweenness_centrality(giantcomp, assigned_types['Type1'], assigned_types['Type2'], bibc_calc_type)
771814

@@ -803,14 +846,20 @@ def BiBC(choice, node_file, nodes_in_gc, giantcomp, calc_type, otu_pv_list, otu_
803846
# Otherwise, calculate BiBC
804847
elif (len(subg[0]) != len(subg[1])):
805848
print("BiBC/RBC being calculated for giant component.")
806-
output_str = BiBC(bibc_choice, node_input_file, gc_nodes, gc, bibc_calc_type, otu_pheno_value_list, otu_pheno_value_str, node_type1, node_type2)
849+
if bibc_choice == "node_types" or bibc_choice == "node_groups_list":
850+
output_str = BiBC(bibc_choice, gc_nodes, gc, bibc_calc_type, otu_pheno_value_list, otu_pheno_value_str, t1 = node_type1, t2 = node_type2, mapfile = node_input_file)
851+
elif bibc_choice == "modularity":
852+
output_str = BiBC(bibc_choice, gc_nodes, gc, bibc_calc_type, otu_pheno_value_list, otu_pheno_value_str)
807853
file.write(output_str)
808854

809855
# If there is only one giant comp, then compute BiBC on whole graph.
810856
else:
811857
print("There is only one component. BiBC being calculated on the entire graph.")
812-
output_str = BiBC(bibc_choice, node_input_file, gc_nodes, gc, bibc_calc_type, otu_pheno_value_list, otu_pheno_value_str, node_type1, node_type2)
813-
#print(output_str)
858+
if bibc_choice == "node_types" or bibc_choice == "node_groups_list":
859+
output_str = BiBC(bibc_choice, gc_nodes, gc, bibc_calc_type, otu_pheno_value_list, otu_pheno_value_str, t1 = node_type1, t2 = node_type2, mapfile = node_input_file)
860+
elif bibc_choice == "modularity":
861+
output_str = BiBC(bibc_choice, gc_nodes, gc, bibc_calc_type, otu_pheno_value_list, otu_pheno_value_str)
862+
814863
file.write(output_str)
815864

816865
# Overwrite any previous file with same name instead of appending

0 commit comments

Comments
 (0)