Refactor code for SBML2PRISM

This commit is contained in:
LukasBuecherl 2022-07-25 14:27:39 -06:00
parent a93e650de2
commit 4bf6c6e1e6

View file

@ -14,58 +14,23 @@
package edu.utah.ece.async.ibiosim.conversion;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.xml.stream.XMLStreamException;
import org.sbml.jsbml.Compartment;
import org.sbml.jsbml.Model;
import org.sbml.jsbml.ModifierSpeciesReference;
import org.sbml.jsbml.Parameter;
import org.sbml.jsbml.Reaction;
import org.sbml.jsbml.SBMLDocument;
import org.sbml.jsbml.SBMLReader;
import org.sbml.jsbml.Species;
import org.sbml.jsbml.SpeciesReference;
import org.sbml.jsbml.ext.comp.CompModelPlugin;
import org.sbml.jsbml.ext.comp.CompSBMLDocumentPlugin;
import org.sbml.jsbml.ext.comp.CompSBasePlugin;
import org.sbml.jsbml.ext.comp.ExternalModelDefinition;
import org.sbml.jsbml.ext.comp.ReplacedBy;
import org.sbml.jsbml.ext.comp.ReplacedElement;
import org.sbolstandard.core2.AccessType;
import org.sbolstandard.core2.ComponentDefinition;
import org.sbolstandard.core2.DirectionType;
import org.sbolstandard.core2.EDAMOntology;
import org.sbolstandard.core2.FunctionalComponent;
import org.sbolstandard.core2.Interaction;
import org.sbolstandard.core2.Module;
import org.sbolstandard.core2.ModuleDefinition;
import org.sbolstandard.core2.RefinementType;
import org.sbolstandard.core2.SBOLConversionException;
import org.sbolstandard.core2.SBOLDocument;
import org.sbolstandard.core2.SBOLReader;
import org.sbolstandard.core2.SBOLValidationException;
import org.sbolstandard.core2.Sequence;
import org.sbolstandard.core2.SequenceOntology;
import org.sbolstandard.core2.SystemsBiologyOntology;
import org.sbolstandard.core2.TopLevel;
import edu.utah.ece.async.ibiosim.dataModels.biomodel.annotation.AnnotationUtility;
import edu.utah.ece.async.ibiosim.dataModels.biomodel.parser.BioModel;
import edu.utah.ece.async.ibiosim.dataModels.biomodel.util.SBMLutilities;
import edu.utah.ece.async.ibiosim.dataModels.util.GlobalConstants;
/**
* Perform conversion from SBML to PRISM.
@ -76,10 +41,11 @@ import edu.utah.ece.async.ibiosim.dataModels.util.GlobalConstants;
* Contributors </a>
* @version %I%
*/
public class SBML2PRISM {
/*
* Convert SBML to Prism Input: SBMLDocument, File Output: void
* Convert SBML to Prism. Input: SBMLDocument, File, bound. Output: void
*
* Function takes in a SBML document and writes the prism conversion of the file
* in the same directory. The function also translates the constraint into a
@ -90,33 +56,41 @@ public class SBML2PRISM {
* To run the converter use the following command: java -jar
* conversion/target/iBioSim-conversion-3.1.0-SNAPSHOT-jar-with-dependencies.jar
* -l PRISM YOURSBMLFILE.xml
*
* The function also allows the translation into a bound model by simply adding the flag
* -bound
*/
public static void convertSBML2PRISM(SBMLDocument sbmlDoc, String filename, boolean unbound) throws IOException {
Model model = sbmlDoc.getModel();
File file = new File(filename.replace(".xml", ".sm"));
// Opening and writing preamble to the file
FileWriter out = new FileWriter(file);
out.write("// File generated by SBML-to-PRISM converter\n");
out.write("// Original file: " + filename + "\n");
out.write("// @GeneticLogicLab\n");
out.write("\n");
out.write("ctmc\n");
out.write("\n");
// Set bound limit if bound model is selected
if (unbound) {
convertSBML2PRISMUnbound(sbmlDoc, filename);
out.write("// const int MAX_AMOUNT = ADD VALUE \n");
out.write("\n");
} else {
convertSBML2PRISMbound(sbmlDoc, filename);
double maxAmount = 0.0;
for (int i = 0; i < model.getSpeciesCount(); i++) {
Species species = model.getSpecies(i);
if (species.getInitialAmount() > maxAmount) {
maxAmount = species.getInitialAmount();
}
}
out.write(" const int MAX_AMOUNT = " + (int) maxAmount + ";\n");
out.write("\n"); out.write("\n");
}
}
private static void convertSBML2PRISMUnbound(SBMLDocument sbmlDoc, String filename) throws IOException {
Model model = sbmlDoc.getModel();
File file = new File(filename.replace(".xml", ".sm"));
// Opening and writing preamble to the file
FileWriter out = new FileWriter(file);
out.write("// File generated by SBML-to-PRISM converter\n");
out.write("// Original file: " + filename + "\n");
out.write("// @GeneticLogicLab\n");
out.write("\n");
out.write("ctmc\n");
out.write("\n");
out.write("// const int MAX_AMOUNT = ADD VALUE \n");
out.write("\n");
// Identify compartments and their size
out.write("// Compartment size\n");
@ -124,7 +98,6 @@ public class SBML2PRISM {
Compartment compartment = model.getCompartment(i);
out.write("const double " + checkReservedKeywordPrism(compartment.getId()) + " = " + compartment.getSize()
+ ";\n");
}
out.write("\n");
@ -147,15 +120,28 @@ public class SBML2PRISM {
// For function checkReservedKeywordPrism see below
Species species = model.getSpecies(i);
out.write("// Species " + checkReservedKeywordPrism(species.getId()) + "\n");
out.write("// const int " + checkReservedKeywordPrism(species.getId()) + "_MAX = MAX_AMOUNT;\n");
out.write("module " + checkReservedKeywordPrism(species.getId()) + "\n");
out.write("\n");
out.write(" // " + checkReservedKeywordPrism(species.getId()) + " : " + "[0.."
+ checkReservedKeywordPrism(species.getId()) + "_MAX] init " + (int) species.getInitialAmount()
+ ";\n");
out.write(" " + checkReservedKeywordPrism(species.getId()) + " : " + "int init "
+ (int) (species.getInitialAmount()) + ";\n");
out.write("\n");
if (unbound) {
out.write("// const int " + checkReservedKeywordPrism(species.getId()) + "_MAX = MAX_AMOUNT;\n");
out.write("module " + checkReservedKeywordPrism(species.getId()) + "\n");
out.write("\n");
out.write(" // " + checkReservedKeywordPrism(species.getId()) + " : " + "[0.."
+ checkReservedKeywordPrism(species.getId()) + "_MAX] init " + (int) species.getInitialAmount()
+ ";\n");
out.write(" " + checkReservedKeywordPrism(species.getId()) + " : " + "int init "
+ (int) (species.getInitialAmount()) + ";\n");
out.write("\n");
} else {
out.write("const int " + checkReservedKeywordPrism(species.getId()) + "_MAX = MAX_AMOUNT;\n");
out.write("module " + checkReservedKeywordPrism(species.getId()) + "\n");
out.write("\n");
out.write(" " + checkReservedKeywordPrism(species.getId()) + " : " + "[0.."
+ checkReservedKeywordPrism(species.getId()) + "_MAX] init " + (int) species.getInitialAmount()
+ ";\n");
out.write(" // " + checkReservedKeywordPrism(species.getId()) + " : " + "int init "
+ (int) (species.getInitialAmount()) + ";\n");
out.write("\n");
}
// Iterate over reactions
for (int j = 0; j < model.getReactionCount(); j++) {
@ -174,12 +160,24 @@ public class SBML2PRISM {
+ checkReservedKeywordPrism(species.getId()) + "-" + (int) reactant.getStoichiometry()
+ ");\n");
} else if (product != null) {
out.write(" // " + checkReservedKeywordPrism(reaction.getId()) + "\n");
out.write(" [" + checkReservedKeywordPrism(reaction.getId()) + "] "
+ checkReservedKeywordPrism(species.getId()) + " >= " + "0 -> ("
+ checkReservedKeywordPrism(species.getId()) + "\'="
+ checkReservedKeywordPrism(species.getId()) + "+" + (int) product.getStoichiometry()
+ ");\n");
if (unbound) {
out.write(" // " + checkReservedKeywordPrism(reaction.getId()) + "\n");
out.write(" [" + checkReservedKeywordPrism(reaction.getId()) + "] "
+ checkReservedKeywordPrism(species.getId()) + " >= " + "0 -> ("
+ checkReservedKeywordPrism(species.getId()) + "\'="
+ checkReservedKeywordPrism(species.getId()) + "+" + (int) product.getStoichiometry()
+ ");\n");
} else {
out.write(" // " + checkReservedKeywordPrism(reaction.getId()) + "\n");
out.write(" [" + checkReservedKeywordPrism(reaction.getId()) + "] "
+ checkReservedKeywordPrism(species.getId()) + " <= " + checkReservedKeywordPrism(species.getId()) + "_MAX-"
+ (int) product.getStoichiometry() + " -> ("
+ checkReservedKeywordPrism(species.getId()) + "\'="
+ checkReservedKeywordPrism(species.getId()) + "+" + (int) product.getStoichiometry()
+ ");\n");
}
}
}
@ -235,169 +233,21 @@ public class SBML2PRISM {
out.close();
// Write Properties File
File property = new File(filename.replace(".xml", ".props"));
FileWriter property_out = new FileWriter(property);
// Property preamble
property_out.write("// File generated by SBML-to-PRISM converter\n");
property_out.write("// Original file: " + filename + "\n");
property_out.write("// @GeneticLogicLab\n");
property_out.write("\n");
for (int i = 0; i < model.getConstraintCount(); i++) {
// Get and write translation of constraint
property_out.write(checkReserveKeywordMath(
SBMLutilities.convertMath2PrismProperty(model.getConstraint(i).getMath()), model));
}
property_out.close();
writePRISMProperty(filename, model);
}
private static void convertSBML2PRISMbound(SBMLDocument sbmlDoc, String filename) throws IOException {
Model model = sbmlDoc.getModel();
File file = new File(filename.replace(".xml", ".sm"));
// Opening and writing preamble to the file
FileWriter out = new FileWriter(file);
out.write("// File generated by SBML-to-PRISM converter\n");
out.write("// Original file: " + filename + "\n");
out.write("// @GeneticLogicLab\n");
out.write("\n");
out.write("ctmc\n");
out.write("\n");
double maxAmount = 0.0;
for (int i = 0; i < model.getSpeciesCount(); i++) {
Species species = model.getSpecies(i);
if (species.getInitialAmount() > maxAmount) {
maxAmount = species.getInitialAmount();
}
}
out.write(" const int MAX_AMOUNT = " + (int) maxAmount + ";\n");
out.write("\n");
// Identify compartments and their size
out.write("// Compartment size\n");
for (int i = 0; i < model.getCompartmentCount(); i++) {
Compartment compartment = model.getCompartment(i);
out.write("const double " + checkReservedKeywordPrism(compartment.getId()) + " = " + compartment.getSize()
+ ";\n");
}
out.write("\n");
// Identify model parameters
out.write("// Model parameters\n");
for (int i = 0; i < model.getParameterCount(); i++) {
Parameter parameter = model.getParameter(i);
out.write("const double " + checkReservedKeywordPrism(parameter.getId()) + " = " + parameter.getValue()
+ "; // " + parameter.getName() + "\n"); // if not null name
}
out.write("\n");
// Identify model species
for (int i = 0; i < model.getSpeciesCount(); i++) {
// Write out syntax
// For function checkReservedKeywordPrism see below
Species species = model.getSpecies(i);
out.write("// Species " + checkReservedKeywordPrism(species.getId()) + "\n");
out.write("const int " + checkReservedKeywordPrism(species.getId()) + "_MAX = MAX_AMOUNT;\n");
out.write("module " + checkReservedKeywordPrism(species.getId()) + "\n");
out.write("\n");
out.write(" " + checkReservedKeywordPrism(species.getId()) + " : " + "[0.."
+ checkReservedKeywordPrism(species.getId()) + "_MAX] init " + (int) species.getInitialAmount()
+ ";\n");
out.write(" // " + checkReservedKeywordPrism(species.getId()) + " : " + "int init "
+ (int) (species.getInitialAmount()) + ";\n");
out.write("\n");
// Iterate over reactions
for (int j = 0; j < model.getReactionCount(); j++) {
Reaction reaction = model.getReaction(j);
// Identify reactants and products
SpeciesReference reactant = reaction.getReactantForSpecies(species.getId());
SpeciesReference product = reaction.getProductForSpecies(species.getId());
if (reactant != null) {
out.write(" // " + checkReservedKeywordPrism(reaction.getId()) + "\n");
out.write(" [" + checkReservedKeywordPrism(reaction.getId()) + "] "
+ checkReservedKeywordPrism(species.getId()) + " > "
+ (int) (reactant.getStoichiometry() - 1) + " -> ("
+ checkReservedKeywordPrism(species.getId()) + "\'="
+ checkReservedKeywordPrism(species.getId()) + "-" + (int) reactant.getStoichiometry()
+ ");\n");
} else if (product != null) {
out.write(" // " + checkReservedKeywordPrism(reaction.getId()) + "\n");
out.write(" [" + checkReservedKeywordPrism(reaction.getId()) + "] "
+ checkReservedKeywordPrism(species.getId()) + " <= " + checkReservedKeywordPrism(species.getId()) + "_MAX-"
+ (int) product.getStoichiometry() + " -> ("
+ checkReservedKeywordPrism(species.getId()) + "\'="
+ checkReservedKeywordPrism(species.getId()) + "+" + (int) product.getStoichiometry()
+ ");\n");
}
}
out.write("\n");
out.write("endmodule\n");
out.write("\n");
}
// Identify reaction rate
out.write("// Reaction rates\n");
out.write("module reaction_rates\n");
out.write("\n");
for (int i = 0; i < model.getReactionCount(); i++) {
Reaction reaction = model.getReaction(i);
// Write state transitions
out.write(" // " + checkReservedKeywordPrism(reaction.getId()) + ": -> ");
for (int j = 0; j < reaction.getProductCount(); j++) {
out.write(checkReservedKeywordPrism(reaction.getProduct(j).getSpecies()) + " ");
}
out.write("\n");
// Get the math for the reaction rate
out.write(" [" + checkReservedKeywordPrism(reaction.getId()) + "] "
+ checkReserveKeywordMath(
SBMLutilities.convertMath2PrismProperty(reaction.getKineticLaw().getMath()), model)
+ " > 0 -> " + "("
+ checkReserveKeywordMath(
SBMLutilities.convertMath2PrismProperty(reaction.getKineticLaw().getMath()), model)
+ ") : true;\n");
out.write("\n");
}
out.write("endmodule\n");
out.write("\n");
// Identify rewards
out.write("// Reward structures (one per species)");
out.write("\n");
for (int i = 0; i < model.getSpeciesCount(); i++) {
Species species = model.getSpecies(i);
out.write("// Reward " + (i + 1) + ": " + checkReservedKeywordPrism(species.getId()) + "\n");
out.write("rewards " + "\"" + checkReservedKeywordPrism(species.getId()) + "\" true : "
+ checkReservedKeywordPrism(species.getId()) + "; endrewards\n");
}
out.close();
/*
* Writes PRISM property Input: (String) filename, (Model) model
* Output: void
*
* Function checks the constraints of a SBML model and translates it
* into the PRISM syntax. The properties are then written into a
* .props file.
*/
private static void writePRISMProperty(String filename, Model model) throws IOException
{
// Write Properties File
File property = new File(filename.replace(".xml", ".props"));
FileWriter property_out = new FileWriter(property);
@ -415,7 +265,6 @@ public class SBML2PRISM {
}
property_out.close();
}
/*
@ -500,11 +349,3 @@ public class SBML2PRISM {
}
}
/*
BioModel bioModel = BioModel.createBioModel(properties.getDirectory(), this);
bioModel.load(filename);
if (bioModel.flattenModel(true) != null) {}
bioModel.getSBMLDocument()
*/