@@ -242,7 +242,10 @@ def create_node_dict(node_list_file, gc_nodes):
242
242
243
243
return (subnet_dict )
244
244
245
-
245
+ def louv (nodes_from_gc ):
246
+ part = community_louvain .best_partition (nodes_from_gc )
247
+ return (part )
248
+
246
249
# .
247
250
def bibc_mod (nodes_from_gc ):
248
251
'''
@@ -528,33 +531,74 @@ def node_groups_from_list(fname):
528
531
################################################################################
529
532
########################## Calculate subnetwork properties #####################
530
533
################################################################################
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 :
535
537
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..." )
555
540
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 :
557
566
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." )
558
602
################################################################################
559
603
########################## Calculate node properties ###########################
560
604
################################################################################
@@ -727,7 +771,7 @@ def parse_RBC_results(rbc_list, otu_pv_list, otu_pv_str):
727
771
728
772
return (otu_pv_str )
729
773
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 ):
731
775
'''
732
776
Calculates RBC/BiBC on the specified nodes
733
777
@@ -738,20 +782,19 @@ def BiBC(choice, node_file, nodes_in_gc, giantcomp, calc_type, otu_pv_list, otu_
738
782
# If the user wants to calculate based on pre-defined node types
739
783
if choice == "node_types" :
740
784
# 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 )
742
786
743
787
# Calculate rbc using the above function which takes the outputs of assign_node_type
744
788
rbc = restricted_betweenness_centrality (giantcomp , assigned_types ['Type1' ], assigned_types ['Type2' ], bibc_calc_type )
745
789
746
790
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 "
748
792
749
793
return (return_str )
750
794
751
795
# Otherwise, if they wish to use modularity as the BiBC parameter...
752
796
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 )
755
798
rbc = restricted_betweenness_centrality (giantcomp , nodes_from_bibc_mod ['mod1' ], nodes_from_bibc_mod ['mod2' ], bibc_calc_type )
756
799
parse_rbc_str = parse_RBC_results (rbc , otu_pv_list , otu_pv_str )
757
800
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_
765
808
multi_groups_out_str = ""
766
809
767
810
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 ])
769
812
770
813
rbc = restricted_betweenness_centrality (giantcomp , assigned_types ['Type1' ], assigned_types ['Type2' ], bibc_calc_type )
771
814
@@ -803,14 +846,20 @@ def BiBC(choice, node_file, nodes_in_gc, giantcomp, calc_type, otu_pv_list, otu_
803
846
# Otherwise, calculate BiBC
804
847
elif (len (subg [0 ]) != len (subg [1 ])):
805
848
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 )
807
853
file .write (output_str )
808
854
809
855
# If there is only one giant comp, then compute BiBC on whole graph.
810
856
else :
811
857
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
+
814
863
file .write (output_str )
815
864
816
865
# Overwrite any previous file with same name instead of appending
0 commit comments