fromneko._outputs.exportsimportExportsexporter=Exports(net)# Export to BNet format for MaBoSSexporter.export_bnet("my_model.bnet")# Export to SIF for Cytoscapeexporter.export_sif("my_network.sif")
Exporting directly from Network
The convenience wrappers on Network call Exports internally, so in most cases you do not need to instantiate Exports yourself:
This class implement many methods used to export the Network object in different format.
In particular the exports format will be methods-oriented (MaBoSS, Ginsim, cobrexa and so on...).
To start with, the user can export the Network in SIF and Bnet format.
In the future many more versatile methods will be implemented (SBML) and annotations will be included for each
interaction, including the DOI of the relative reference and annotations from each database
defexport_bnet(self,file_name="logic_model.bnet"):""" Function to export the network in bnet format, creating multiple files for bimodal interactions. """# Checks for nodes and interactions dataifnotisinstance(self.nodes,pd.DataFrame)orself.nodes.empty:print("Error: Nodes data is missing or empty.")returnifnotisinstance(self.interactions,pd.DataFrame)orself.interactions.empty:print("Error: Interactions data is missing or empty.")return# Identify undefined interactionsundefined_interactions=self.interactions.query("Effect == 'undefined'")ifnotundefined_interactions.empty:print(f"Warning: The network has {len(undefined_interactions)} UNDEFINED interaction(s).")print("Undefined interactions:")forindex,rowinundefined_interactions.iterrows():print(f"{row['source']} -> {row['target']}")print(f"Reference: {row['References']}")# Identify bimodal interactionsbimodal_interactions=self.interactions.query("Effect == 'bimodal'")ifnotbimodal_interactions.empty:print(f"Warning: The network has {len(bimodal_interactions)} BIMODAL interaction(s).")print("Bimodal interactions:")forindex,rowinbimodal_interactions.iterrows():print(f"{row['source']} -> {row['target']}")print(f"Reference: {row['References']}")# Generate permutations for bimodal interactionsbimodal_sources=bimodal_interactions['source'].tolist()bimodal_targets=bimodal_interactions['target'].tolist()permutations=list(itertools.product(['stimulation','inhibition'],repeat=len(bimodal_interactions)))# Create a directory for the BNet files if a directory is provideddirectory=os.path.dirname(file_name)ifdirectory:os.makedirs(directory,exist_ok=True)# Iterate through permutations and create a BNet file for eachfori,perminenumerate(permutations):# Create a copy of the interactions DataFrameinteractions_copy=self.interactions.copy()# Update bimodal interactions based on the current permutationforj,(source,target)inenumerate(zip(bimodal_sources,bimodal_targets)):interactions_copy.loc[(interactions_copy['source']==source)&(interactions_copy['target']==target),'Effect']=perm[j]# Pre-filter stimulations, inhibitions, and exclude undefined effectsstimulations=interactions_copy.query("Effect == 'stimulation'")inhibitions=interactions_copy.query("Effect == 'inhibition'")complex_formation=interactions_copy.query("Effect == 'form complex'")# Generate the file name for this permutationperm_file_name=f"{os.path.splitext(file_name)[0]}_{i+1}.bnet"withopen(perm_file_name,"w")asf:f.write("# model in BoolNet format\n")f.write("targets, factors\n")forentryinself.nodes.values:node=entry[0]# Replace special characters in node namesnode=re.sub(r"[\/\-\s\#]","_",node)formula_on=[re.sub(r"[\/\-\s\#]","_",src)forsrcinstimulations[stimulations["target"]==node]["source"].to_list()]formula_off=[re.sub(r"[\/\-\s\#]","_",src)forsrcininhibitions[inhibitions["target"]==node]["source"].to_list()]formula_complex=[re.sub(r"[\/\-\s\#]","_",src)forsrcincomplex_formation[complex_formation["target"]==node]["source"].to_list()]# Constructing the formulaformula_parts=[]ifformula_complex:formula_parts.append(f"({' & '.join(formula_complex)})")ifformula_on:formula_parts.append(f"({' | '.join(formula_on)})")ifformula_off:formula_parts.append("!({})".format(" | ".join(formula_off)))# Writing the node and its formula to the filef.write(f"{node}, {' & '.join(formula_parts)ifformula_partselsenode}\n")print(f"Created BNet file: {perm_file_name}")print(f"Generated {len(permutations)} BNet files.")
defexport_sif(self,file_name="logic_model.sif"):""" Function to export the network in SIF format """withopen(file_name,'w')asfile:forindex,rowinself.interactions.iterrows():# Use the Effect column directly assuming it contains "activate" or "inhibit"interaction_type=row['Effect']ifinteraction_type=="form complex":interaction_type="form_complex"# Reference for the interactioninteraction_reference=row['References']# Adjust column name if necessary# Write a comment line with the interaction referencefile.write(f"# Reference PMID: {interaction_reference}\n")# Write the formatted interaction to the .sif filefile.write(f"{row['source']}\t{interaction_type}\t{row['target']}\n")return