net — network structure
Make or modify network structure. Exposed as bobaT.net.
Network construction and feature selection utilities for bobaT.net module.
This module provides functions for: - Building gene regulatory networks from DIRECT-NET and FIGR data - LASSO-based feature selection for network pruning - Network comparison and analysis
- bobaT.make_tf_network.prune(G_orig, prune_sources=True, prune_sinks=True)[source]
Prune graph to remove nodes with no incoming or outgoing edges
- Parameters:
G_orig (networkx.DiGraph) – NetworkX graph to prune
prune_sources (bool, optional) – Remove nodes with no incoming edges, defaults to True
prune_sinks (bool, optional) – Remove nodes with no outgoing edges, defaults to True
- Returns:
Pruned network
- Return type:
- bobaT.make_tf_network.prune_info(G_orig, prune_self_loops=True)[source]
Prune graph to only include edges with evidence in multiple databases
- Parameters:
G_orig (networkx.DiGraph) – NetworkX graph to prune
prune_self_loops (bool, optional) – Whether to prune self-loops in the network (edges where parent node == child node), defaults to True
- Returns:
Pruned network
- Return type:
- bobaT.make_tf_network.prune_to_chea(G_orig, prune_self_loops=True)[source]
Prune graph to only include edges with evidence in ChEA databases
- Parameters:
G_orig (networkx.DiGraph) – NetworkX graph to prune
prune_self_loops (bool, optional) – Whether to prune self-loops in the network (edges where parent node == child node), defaults to True
- Returns:
Pruned network
- Return type:
- bobaT.make_tf_network.make_network(tfs, outdir='', do_prune=True, prune_sinks=True, prune_sources=True, do_prune_info=True, prune_self_loops=True, do_prune_to_chea=True, save_unfiltered=False, network_name='network')[source]
Make network from list of tfs and save as csv files with various levels of pruning.
- Parameters:
tfs (List[str]) – List of transcription factor gene names that will be searched in enrichR databases.
outdir (str or None) – Output directory where network csvs will be saved, defaults to “”
do_prune (bool, optional) – Prune network to remove nodes with no sinks and/or sources and generate a new network file called <network_name>_pruned.csv, defaults to True
prune_sinks (bool, optional) – Whether to prune sink nodes from the network (no outgoing nodes), defaults to True
prune_sources (bool, optional) – Whether to prune source nodes the network (no incoming nodes), defaults to True
do_prune_info (bool, optional) – Whether to prune the network to edges with evidence in more than one database from enrichR and generate a new network file called <network_name>_high_evidence.csv, defaults to True
prune_self_loops (bool, optional) – Whether to prune self-loops in the network (edges where parent node == child node), defaults to True
do_prune_to_chea (bool, optional) – Whether to prune the network to only edges with evidence in ChEA databases and generate a new network file called <network_name>_chea.csv, defaults to True
save_unfiltered (bool, optional) – Whether to save the unfiltered network before pruning, defaults to False. Note that if all pruning options are False and this is set to False, no network will be saved.
network_name (str) – Prefix name of network csv files, defaults to network
- Returns:
Network with highest level of pruning
- Return type:
NetworkX Graph
- bobaT.make_tf_network.save_network(network_file, G, attributes=True, overwrite=True)[source]
Save a NetworkX graph to a CSV file.
- Parameters:
network_file (str) – Path to output file
G (networkx.DiGraph) – Network graph to save
attributes (bool, default=True) – Whether to save edge attributes
overwrite (bool, default=True) – Whether to overwrite existing file
- Returns:
True if saved, False if file exists and overwrite=False
- Return type:
- bobaT.make_tf_network.add_connections(G, net_df, threshold_var=None, threshold=0, parent_node='TF motif', child_node='Target_gene', add_weight=True, weight='motif score', evidence='direct-net')[source]
Add edges to a NetworkX graph from a dataframe.
- Parameters:
G (networkx.DiGraph) – Network graph to add edges to
net_df (pd.DataFrame) – DataFrame containing edge information
threshold_var (str, optional) – Column name to threshold on
threshold (float, default=0) – Threshold value for filtering edges
parent_node (str, default='TF motif') – Column name for source nodes
child_node (str, default='Target_gene') – Column name for target nodes
add_weight (bool, default=True) – Whether to add edge weights
weight (str, default='motif score') – Column name for edge weights
evidence (str, default='direct-net') – Evidence type for edge annotation
- Returns:
Updated graph with new edges
- Return type:
- bobaT.make_tf_network.build_network_from_directnet(direct_net_df, threshold=0, threshold_var='motif score')[source]
Build a gene regulatory network from DIRECT-NET data.
- Parameters:
- Returns:
Gene regulatory network
- Return type:
- bobaT.make_tf_network.lasso_feature_selection(network, data, network_name, output_dir, alphas=None, save_network=True, plot=True)[source]
Perform LASSO regression for feature selection on network edges.
For each target gene, use LASSO to identify the most important transcription factor regulators from the network.
- Parameters:
network (pd.DataFrame) – Network dataframe with columns: ‘source’, ‘target’, ‘weight’, ‘evidence’
data (pd.DataFrame) – Expression data with genes as columns
network_name (str) – Name for output files
output_dir (str) – Directory for saving results
alphas (list, optional) – Alpha values to test. Default: [0.00001, 0.0001, 0.001, 0.01, 0.1, 1]
save_network (bool, default=True) – Whether to save filtered networks
plot (bool, default=True) – Whether to generate coefficient plots
- Returns:
Dictionary mapping alpha values to best scores
- Return type:
- bobaT.make_tf_network.compare_networks(original_network, filtered_network, alpha, save=True, save_dir='')[source]
Compare original network to filtered network and visualize differences.
- Parameters:
original_network (pd.DataFrame) – Original network with ‘source’ and ‘target’ columns
filtered_network (pd.DataFrame) – Filtered network with ‘source’ and ‘target’ columns
alpha (float) – Alpha value used for filtering
save (bool, default=True) – Whether to save the histogram
save_dir (str, default="") – Directory for saving plots
- Returns:
Summary statistics about the network comparison
- Return type:
- bobaT.make_tf_network.adaptive_lasso_pruning(network, output_dir, network_name, max_parents=8, alphas=None)[source]
Adaptively select alpha for each target to achieve desired max parents.
For targets with more than max_parents regulators, find the smallest alpha that reduces regulators to max_parents or fewer.
- Parameters:
network (pd.DataFrame) – Network dataframe with ‘source’ and ‘target’ columns
output_dir (str) – Directory containing LASSO results
network_name (str) – Name of the network
max_parents (int, default=8) – Maximum number of regulators per target
alphas (list, optional) – Alpha values to search. Default: [0.00001, 0.0001, 0.001, 0.01, 0.1, 1]
- Returns:
pd.DataFrame – Pruned network with at most max_parents regulators per target
dict – Dictionary mapping each target to its selected alpha