Napistu provides powerful functionality to merge multiple pathway models from different databases into a single consensus network. This process resolves entity conflicts, merges identical components, and creates a unified representation while preserving source information.
SBML_dfs: Represents individual pathway models as collections of pandas DataFramesPWIndex: Manages metadata about pathway files and their locationsIdentifiers: Handles systematic identifiers for biological entitiesSource: Tracks provenance information for merged entitiesThe primary function for consensus building is construct_consensus_model() in the consensus.py module.
Create a pathway index that describes all the models you want to merge:
from napistu import indices
# Create pathway index from model definitions
pw_index_df = indices.create_pathway_index_df(
model_keys={"human": "recon3", "mouse": "iMM1415"},
model_urls={"human": "path/to/recon3.xml", "mouse": "path/to/iMM1415.xml"},
model_species={"human": "Homo sapiens", "mouse": "Mus musculus"},
base_path="./models/",
source_name="metabolic_models",
file_extension=".xml"
)
# Create PWIndex object
pw_index = indices.PWIndex(pw_index_df)Convert your pathway files into SBML_dfs objects:
from napistu import consensus
# Load all models from the pathway index
sbml_dfs_dict = consensus.construct_sbml_dfs_dict(
pw_index=pw_index.index,
strict=True # Set to False to skip problematic files
)Create the consensus network by merging shared entities:
# Build consensus model
consensus_model = consensus.construct_consensus_model(
sbml_dfs_dict=sbml_dfs_dict,
pw_index=pw_index,
dogmatic=True # True: keep genes/transcripts/proteins separate
# False: merge them when possible
)The consensus building process follows a hierarchical approach:
Two modes control how entities are merged:
Dogmatic Mode (dogmatic=True):
BQB_IS or BQB_IS_HOMOLOG_TO relationshipsNon-Dogmatic Mode (dogmatic=False):
BQB_IS_ENCODED_BY and BQB_ENCODESCheck ontology compatibility before merging:
# Check shared ontologies across models
shared_ontologies, ontology_summary = consensus.pre_consensus_ontology_check(
sbml_dfs_dict,
tablename="species"
)
# Check shared compartments
shared_compartments, compartment_summary = consensus.pre_consensus_compartment_check(
sbml_dfs_dict,
tablename="compartments"
)Analyze the merged model:
# Check ontologies in consensus model
consensus_ontologies = consensus.post_consensus_species_ontology_check(consensus_model)
# Get network summary statistics
network_stats = consensus_model.get_network_summary()
# Check source coverage
reactions_sources = consensus.post_consensus_source_check(
consensus_model,
table_name="reactions"
) consensus_model.infer_uncompartmentalized_species_location() consensus_model.infer_sbo_terms() consensus_model.name_compartmentalized_species()The consensus model includes automatic validation:
# Validate and attempt automatic fixes
consensus_model.validate_and_resolve()
# Manual validation (raises errors without fixes)
consensus_model.validate()# Export to various formats
consensus_model.export_sbml_dfs(
model_prefix="consensus_",
outdir="./output/",
overwrite=True,
dogmatic=True
)# Get reaction formulas
formulas = consensus_model.reaction_formulas()
# Search by species name
matching_species = consensus_model.search_by_name(
name="glucose",
entity_type="species"
)
# Get species participation in reactions
species_status = consensus_model.species_status("SPEC_00001")
# Get characteristic identifiers
species_ids = consensus_model.get_characteristic_species_ids(dogmatic=True)from napistu import consensus, indices
def create_consensus_network(model_configs, output_dir):
"""
Complete workflow for creating a consensus network
Parameters:
- model_configs: dict with model metadata
- output_dir: where to save results
"""
# Step 1: Create pathway index
pw_index_df = indices.create_pathway_index_df(**model_configs)
pw_index = indices.PWIndex(pw_index_df)
# Step 2: Load models
print("Loading individual models...")
sbml_dfs_dict = consensus.construct_sbml_dfs_dict(
pw_index=pw_index.index,
strict=False # Skip problematic files
)
print(f"Loaded {len(sbml_dfs_dict)} models")
# Step 3: Pre-consensus analysis
print("Analyzing model compatibility...")
species_ontologies, _ = consensus.pre_consensus_ontology_check(
sbml_dfs_dict, "species"
)
print(f"Shared species ontologies: {species_ontologies}")
# Step 4: Build consensus
print("Building consensus model...")
consensus_model = consensus.construct_consensus_model(
sbml_dfs_dict=sbml_dfs_dict,
pw_index=pw_index,
dogmatic=True
)
# Step 5: Validate and export
print("Validating consensus model...")
consensus_model.validate_and_resolve()
print("Exporting results...")
consensus_model.export_sbml_dfs(
model_prefix="consensus_",
outdir=output_dir,
overwrite=True
)
# Step 6: Summary statistics
stats = consensus_model.get_network_summary()
print(f"Consensus model contains:")
print(f" - {stats['n_species']} species")
print(f" - {stats['n_reactions']} reactions")
print(f" - {stats['n_compartments']} compartments")
return consensus_model
# Usage example
model_config = {
"model_keys": {"recon": "recon3", "bigg": "iHuman"},
"model_urls": {"recon": "recon3.xml", "bigg": "iHuman.xml"},
"model_species": {"recon": "Homo sapiens", "bigg": "Homo sapiens"},
"base_path": "./models/",
"source_name": "human_metabolism"
}
consensus_net = create_consensus_network(model_config, "./consensus_output/")strict=False to skip problematic files during loadingdogmatic mode based on your analysis goalsThe consensus building process preserves all source information, allowing you to trace any entity back to its original model(s) and understand how the merger was performed.