IndraNet Graphs (indra.assemblers.indranet
)¶
The IndraNet assembler creates multiple different types of networkx graphs from INDRA Statements. It also allows exporting binary Statement information as a pandas DataFrame.
- class indra.assemblers.indranet.net.IndraNet(incoming_graph_data=None, **attr)[source]¶
A Networkx representation of INDRA Statements.
- classmethod digraph_from_df(df, flattening_method=None, weight_mapping=None)[source]¶
Create a digraph from a pandas DataFrame.
- Parameters
df (pd.DataFrame) – The dataframe to build the graph from.
flattening_method (str or function(networkx.DiGraph, edge)) – The method to use when updating the belief for the flattened edge.
weight_mapping (function(networkx.DiGraph)) – A function taking at least the graph G as an argument and returning G after adding edge weights as an edge attribute to the flattened edges using the reserved keyword ‘weight’.
- Returns
An IndraNet graph flattened to a DiGraph
- Return type
IndraNet(nx.DiGraph)
- classmethod from_df(df)[source]¶
Create an IndraNet MultiDiGraph from a pandas DataFrame.
Returns an instance of IndraNet with graph data filled out from a dataframe containing pairwise interactions.
- Parameters
df (pd.DataFrame) –
A
pandas.DataFrame
with each row containing node and edge data for one edge. Indices are used to distinguish multiedges between a pair of nodes. Any columns not part of the below mentioned mandatory columns are considered extra attributes. Columns starting with ‘agA_’ or ‘agB_’ (excluding the agA/B_name) will be added to its respective nodes as node attributes. Any other columns will be added as edge attributes.Mandatory columns are : agA_name, agB_name, agA_ns, agA_id, agB_ns, agB_id, stmt_type, evidence_count, stmt_hash, belief and source_counts.
- Returns
An IndraNet object
- Return type
- classmethod signed_from_df(df, sign_dict=None, flattening_method=None, weight_mapping=None)[source]¶
Create a signed graph from a pandas DataFrame.
- Parameters
df (pd.DataFrame) – The dataframe to build the signed graph from.
sign_dict (dict) – A dictionary mapping a Statement type to a sign to be used for the edge. By default only Activation and IncreaseAmount are added as positive edges and Inhibition and DecreaseAmount are added as negative edges, but a user can pass any other Statement types in a dictionary.
flattening_method (str or function(networkx.DiGraph, edge)) – The method to use when updating the belief for the flattened edge.
weight_mapping (function(networkx.DiGraph)) – A function taking at least the graph G as an argument and returning G after adding edge weights as an edge attribute to the flattened edges using the reserved keyword ‘weight’.
- Returns
An IndraNet graph flattened to a signed graph
- Return type
IndraNet(nx.MultiDiGraph)
- to_digraph(flattening_method=None, weight_mapping=None)[source]¶
Flatten the IndraNet to a DiGraph
- Parameters
flattening_method (str|function) – The method to use when updating the belief for the flattened edge
weight_mapping (function) – A function taking at least the graph G as an argument and returning G after adding edge weights as an edge attribute to the flattened edges using the reserved keyword ‘weight’.
- Returns
G – An IndraNet graph flattened to a DiGraph
- Return type
IndraNet(nx.DiGraph)
- to_signed_graph(sign_dict=None, flattening_method=None, weight_mapping=None)[source]¶
Flatten the IndraNet to a signed graph.
- Parameters
sign_dict (dict) – A dictionary mapping a Statement type to a sign to be used for the edge. By default only Activation and IncreaseAmount are added as positive edges and Inhibition and DecreaseAmount are added as negative edges, but a user can pass any other Statement types in a dictionary.
flattening_method (str or function(networkx.DiGraph, edge)) –
The method to use when updating the belief for the flattened edge.
If a string is provided, it must be one of the predefined options ‘simple_scorer’ or ‘complementary_belief’.
If a function is provided, it must take the flattened graph ‘G’ and an edge ‘edge’ to perform the belief flattening on and return a number:
>>> def flattening_function(G, edge): ... # Return the average belief score of the constituent edges ... all_beliefs = [s['belief'] ... for s in G.edges[edge]['statements']] ... return sum(all_beliefs)/len(all_beliefs)
weight_mapping (function(networkx.DiGraph)) –
A function taking at least the graph G as an argument and returning G after adding edge weights as an edge attribute to the flattened edges using the reserved keyword ‘weight’.
Example:
>>> def weight_mapping(G): ... # Sets the flattened weight to the average of the ... # inverse source count ... for edge in G.edges: ... w = [1/s['evidence_count'] ... for s in G.edges[edge]['statements']] ... G.edges[edge]['weight'] = sum(w)/len(w) ... return G
- Returns
SG – An IndraNet graph flattened to a signed graph
- Return type
IndraNet(nx.MultiDiGraph)
- class indra.assemblers.indranet.assembler.IndraNetAssembler(statements=None)[source]¶
Assembler to create an IndraNet object from a list of INDRA statements.
- Parameters
statements (list[indra.statements.Statement]) – A list of INDRA Statements to be assembled.
- add_statements(stmts)[source]¶
Add INDRA Statements to the assembler’s list of statements.
- Parameters
stmts (list[indra.statements.Statement]) – A list of
indra.statements.Statement
to be added to the statement list of the assembler.
- make_df(exclude_stmts=None, complex_members=3, extra_columns=None, keep_self_loops=True)[source]¶
Create a dataframe containing information extracted from assembler’s list of statements necessary to build an IndraNet.
- Parameters
exclude_stmts (list[str]) – A list of statement type names to not include in the dataframe.
complex_members (int) – Maximum allowed size of a complex to be included in the data frame. All complexes larger than complex_members will be rejected. For accepted complexes, all permutations of their members will be added as dataframe records. Default is 3.
extra_columns (list[tuple(str, function)]) – A list of tuples defining columns to add to the dataframe in addition to the required columns. Each tuple contains the column name and a function to generate a value from a statement.
keep_self_loops (Optional[bool]) – Whether to keep the self-loops when constructing the graph.
- Returns
df – Pandas DataFrame object containing information extracted from statements. It contains the following columns:
- agA_name
The first Agent’s name.
- agA_ns
The first Agent’s identifier namespace as per db_refs.
- agA_id
The first Agent’s identifier as per db_refs
- ags_ns, agB_name, agB_id
As above for the second agent. Note that the Agent may be None (and these fields left empty) if the Statement consists only of a single Agent (e.g., SelfModification, ActiveForm, or Translocation statement).
- stmt_type
Statement type, given by the name of the class in indra.statements.
- evidence_count
Number of evidences for the statement.
- stmt_hash
An unique long integer hash identifying the content of the statement.
- belief
The belief score associated with the statement.
- source_counts
The number of evidences per input source for the statement.
- residue
If applicable, the amino acid residue being modified. NaN if if it is unknown or unspecified/not applicable.
- position
If applicable, the position of the modified amino acid. NaN if it is unknown or unspecified/not applicable.
- initial_sign
The default sign (polarity) associated with the given statement if the statement type has implied polarity. To facilitate weighted path finding, the sign is represented as 0 for positive polarity and 1 for negative polarity.
More columns can be added by providing the extra_columns parameter.
- Return type
pd.DataFrame
- make_model(method='preassembly', exclude_stmts=None, complex_members=3, graph_type='multi_graph', sign_dict=None, belief_flattening=None, belief_scorer=None, weight_flattening=None, extra_columns=None, keep_self_loops=True)[source]¶
Assemble an IndraNet graph object.
- Parameters
method (str) – Method for assembling an IndraNet graph. Accepted values: df and preassembly. With the df method, the statements are converted into pandas DataFrame first where each row corresponds to an edge in unflattened MultiDiGraph IndraNet. Then the IndraNet can be flattened into signed and unsigned graphs. The beliefs can be calculated on the new edges by providing belief_flattening function. With the preassembly option, the statements are merged together and the beliefs are calculated by leveraging the preassembly functionality with custom matches functions (the matches functions are applied depending on the graph type). This method ensures the more robust belief calculation.
exclude_stmts (list[str]) – A list of statement type names to not include in the graph.
complex_members (int) – Maximum allowed size of a complex to be included in the graph. All complexes larger than complex_members will be rejected. For accepted complexes, all permutations of their members will be added as edges. Default is 3.
graph_type (str) – Specify the type of graph to assemble. Chose from ‘multi_graph’ (default), ‘digraph’, ‘signed’. Default is multi_graph.
sign_dict (dict) – A dictionary mapping a Statement type to a sign to be used for the edge. This parameter is only used with the ‘signed’ option. See IndraNet.to_signed_graph for more info.
belief_flattening (str or function(networkx.DiGraph, edge)) –
Only needed when method is set to df. The method to use when updating the belief for the flattened edge.
If a string is provided, it must be one of the predefined options ‘simple_scorer’ or ‘complementary_belief’.
If a function is provided, it must take the flattened graph ‘G’ and an edge ‘edge’ to perform the belief flattening on and return a number:
>>> def belief_flattening(G, edge): ... # Return the average belief score of the constituent edges ... all_beliefs = [s['belief'] ... for s in G.edges[edge]['statements']] ... return sum(all_beliefs)/len(all_beliefs)
belief_scorer (Optional[indra.belief.BeliefScorer]) – Only needed when method is set to preassembly. Instance of BeliefScorer class to use in calculating edge probabilities. If None is provided (default), then the default scorer is used.
weight_flattening (function(networkx.DiGraph)) –
A function taking at least the graph G as an argument and returning G after adding edge weights as an edge attribute to the flattened edges using the reserved keyword ‘weight’.
Example:
>>> def weight_flattening(G): ... # Sets the flattened weight to the average of the ... # inverse source count ... for edge in G.edges: ... w = [1/s['evidence_count'] ... for s in G.edges[edge]['statements']] ... G.edges[edge]['weight'] = sum(w)/len(w) ... return G
keep_self_loops (Optional[bool]) – Whether to keep the self-loops when constructing the graph.
- Returns
model – IndraNet graph object.
- Return type
- make_model_by_preassembly(exclude_stmts=None, complex_members=3, graph_type='multi_graph', sign_dict=None, belief_scorer=None, weight_flattening=None, extra_columns=None, keep_self_loops=True)[source]¶
Assemble an IndraNet graph object by preassembling the statements according to selected graph type.
- Parameters
exclude_stmts (list[str]) – A list of statement type names to not include in the graph.
complex_members (int) – Maximum allowed size of a complex to be included in the graph. All complexes larger than complex_members will be rejected. For accepted complexes, all permutations of their members will be added as edges. Default is 3.
graph_type (str) – Specify the type of graph to assemble. Chose from ‘multi_graph’ (default), ‘digraph’, ‘signed’. Default is multi_graph.
sign_dict (dict) – A dictionary mapping a Statement type to a sign to be used for the edge. This parameter is only used with the ‘signed’ option. See IndraNet.to_signed_graph for more info.
belief_scorer (Optional[indra.belief.BeliefScorer]) – Instance of BeliefScorer class to use in calculating edge probabilities. If None is provided (default), then the default scorer is used.
weight_flattening (function(networkx.DiGraph)) –
A function taking at least the graph G as an argument and returning G after adding edge weights as an edge attribute to the flattened edges using the reserved keyword ‘weight’.
Example:
>>> def weight_flattening(G): ... # Sets the flattened weight to the average of the ... # inverse source count ... for edge in G.edges: ... w = [1/s['evidence_count'] ... for s in G.edges[edge]['statements']] ... G.edges[edge]['weight'] = sum(w)/len(w) ... return G
keep_self_loops (Optional[bool]) – Whether to keep the self-loops when constructing the graph.
- Returns
model – IndraNet graph object.
- Return type
- make_model_from_df(exclude_stmts=None, complex_members=3, graph_type='multi_graph', sign_dict=None, belief_flattening=None, weight_flattening=None, extra_columns=None, keep_self_loops=True)[source]¶
Assemble an IndraNet graph object by constructing a pandas Dataframe first.
- Parameters
exclude_stmts (list[str]) – A list of statement type names to not include in the graph.
complex_members (int) – Maximum allowed size of a complex to be included in the graph. All complexes larger than complex_members will be rejected. For accepted complexes, all permutations of their members will be added as edges. Default is 3.
graph_type (str) – Specify the type of graph to assemble. Chose from ‘multi_graph’ (default), ‘digraph’, ‘signed’. Default is multi_graph.
sign_dict (dict) – A dictionary mapping a Statement type to a sign to be used for the edge. This parameter is only used with the ‘signed’ option. See IndraNet.to_signed_graph for more info.
belief_flattening (str or function(networkx.DiGraph, edge)) –
The method to use when updating the belief for the flattened edge.
If a string is provided, it must be one of the predefined options ‘simple_scorer’ or ‘complementary_belief’.
If a function is provided, it must take the flattened graph ‘G’ and an edge ‘edge’ to perform the belief flattening on and return a number:
>>> def belief_flattening(G, edge): ... # Return the average belief score of the constituent edges ... all_beliefs = [s['belief'] ... for s in G.edges[edge]['statements']] ... return sum(all_beliefs)/len(all_beliefs)
weight_flattening (function(networkx.DiGraph)) –
A function taking at least the graph G as an argument and returning G after adding edge weights as an edge attribute to the flattened edges using the reserved keyword ‘weight’.
Example:
>>> def weight_flattening(G): ... # Sets the flattened weight to the average of the ... # inverse source count ... for edge in G.edges: ... w = [1/s['evidence_count'] ... for s in G.edges[edge]['statements']] ... G.edges[edge]['weight'] = sum(w)/len(w) ... return G
keep_self_loops (Optional[bool]) – Whether to keep the self-loops when constructing the graph.
- Returns
model – IndraNet graph object.
- Return type