/// <summary> /// /// </summary> /// <param name="aSBMLmodel"></param> public SBML_Model(Model aSBMLmodel) { this.CompartmentSize = new Dictionary<string,double>(); this.CompartmentUnit = new Dictionary<string,string>(); this.FunctionDefinition = new Dictionary<string, string>(); this.Level = aSBMLmodel.getLevel(); this.Version = aSBMLmodel.getVersion(); this.CompartmentList = SbmlFunctions.getCompartment(aSBMLmodel); this.EventList = SbmlFunctions.getEvent(aSBMLmodel); this.FunctionDefinitionList = SbmlFunctions.getFunctionDefinition(aSBMLmodel); this.ParameterList = SbmlFunctions.getParameter(aSBMLmodel); this.ReactionList = SbmlFunctions.getReaction(aSBMLmodel); this.RuleList = SbmlFunctions.getRule(aSBMLmodel); this.SpeciesList = SbmlFunctions.getSpecies(aSBMLmodel); this.UnitDefinitionList = SbmlFunctions.getUnitDefinition(aSBMLmodel); this.InitialAssignmentList = SbmlFunctions.getInitialAssignments(aSBMLmodel); this.setFunctionDefinitionToDictionary(); }
/// <summary> /// [ ReactionStruct ] /// [[ Id , Name , [ KineticLawStruct ] , Reversible , Fast , [ ReactantStruct ] , [ ProductStruct ] , [ ModifierSpecies ] ]] /// </summary> /// <param name="aSBMLmodel"></param> /// <returns></returns> public static List<ReactionStruct> getReaction(Model aSBMLmodel) { List<ReactionStruct> list = new List<ReactionStruct>(); ListOfReactions reactions = aSBMLmodel.getListOfReactions(); for (int i = 0; i < reactions.size(); i++ ) { Reaction aReaction = aSBMLmodel.getReaction(i); string anId = aReaction.getId(); string aName =aReaction.getName(); //----------KineticLaw---------------------------------- List<KineticLawStruct> ListOfKineticLaw = new List<KineticLawStruct>(); if( aReaction.isSetKineticLaw()) { KineticLaw aKineticLaw = aReaction.getKineticLaw(); if( aKineticLaw != null) { string aFormula_KL; if( aKineticLaw.isSetFormula()) aFormula_KL = aKineticLaw.getFormula(); else aFormula_KL = ""; List<string> aString_KL = new List<string>(); if (aSBMLmodel.getLevel() == 1) { aString_KL.Add( "" ); } else { if (aKineticLaw.isSetMath()) { ASTNode anASTNode_KL = aKineticLaw.getMath(); aString_KL.Add( libsbml.libsbml.formulaToString( anASTNode_KL ) ); } else aString_KL.Add( "" ); } string aTimeUnit_KL = aKineticLaw.getTimeUnits(); string aSubstanceUnit_KL = aKineticLaw.getSubstanceUnits(); List<ParameterStruct> listOfParameters = new List<ParameterStruct>(); ListOfParameters parameters = aKineticLaw.getListOfParameters(); for (int j = 0; j < parameters.size(); j++ ) { Parameter aParameter = aKineticLaw.getParameter(j); if (aParameter == null) continue; string anId_KL_P = aParameter.getId(); string aName_KL_P = aParameter.getName(); double aValue_KL_P = aParameter.getValue(); string aUnit_KL_P = aParameter.getUnits(); bool aConstant_KL_P = aParameter.getConstant(); ParameterStruct parameter = new ParameterStruct( anId_KL_P, aName_KL_P, aValue_KL_P, aUnit_KL_P, aConstant_KL_P); listOfParameters.Add( parameter ); } XMLNode anExpressionAnnotation = aKineticLaw.getAnnotation(); KineticLawStruct kineticLaw = new KineticLawStruct( aFormula_KL, aString_KL, aTimeUnit_KL, aSubstanceUnit_KL, listOfParameters, anExpressionAnnotation ); ListOfKineticLaw.Add(kineticLaw); } } bool aReversible = aReaction.getReversible(); bool aFast = aReaction.getFast(); //----------Reactants---------------------------------- List<ReactantStruct> ListOfReactants = new List<ReactantStruct>(); ListOfSpeciesReferences reactants = aReaction.getListOfReactants(); for (int k = 0; k < reactants.size(); k++ ) { SpeciesReference aSpeciesReference = aReaction.getReactant(k); string aSpecies_R = aSpeciesReference.getSpecies(); int aStoichiometry_R = (int)aSpeciesReference.getStoichiometry(); string aString_R = GetStoichiometryMath(aSpeciesReference); int aDenominator_R = aSpeciesReference.getDenominator(); ReactantStruct reactant = new ReactantStruct( aSpecies_R, aStoichiometry_R, aString_R, aDenominator_R); ListOfReactants.Add( reactant ); } //----------Products---------------------------------- List<ProductStruct> ListOfProducts = new List<ProductStruct>(); ListOfSpeciesReferences products = aReaction.getListOfProducts(); long max = products.size(); for (int l = 0; l < max; l++) { SpeciesReference aSpeciesReference = aReaction.getProduct(l); string aSpecies_P = aSpeciesReference.getSpecies(); double aStoichiometry_P = aSpeciesReference.getStoichiometry(); string aString_P = GetStoichiometryMath(aSpeciesReference); int aDenominator_P = aSpeciesReference.getDenominator(); ProductStruct product = new ProductStruct( aSpecies_P, aStoichiometry_P, aString_P, aDenominator_P); ListOfProducts.Add( product ); } //----------Modifiers---------------------------------- List<string> ListOfModifiers = new List<string>(); ListOfSpeciesReferences modifiers = aReaction.getListOfModifiers(); for (long l = 0; l < modifiers.size(); l++ ) { ModifierSpeciesReference aSpeciesReference = aReaction.getModifier(l); string aSpecies_M = aSpeciesReference.getSpecies(); ListOfModifiers.Add( aSpecies_M ); } ReactionStruct reaction = new ReactionStruct( anId, aName, ListOfKineticLaw, aReversible, aFast, ListOfReactants, ListOfProducts, ListOfModifiers ); list.Add(reaction); } return list; }
public void test_Model_createWithNS() { XMLNamespaces xmlns = new XMLNamespaces(); xmlns.add( "http://www.sbml.org", "testsbml"); SBMLNamespaces sbmlns = new SBMLNamespaces(2,1); sbmlns.addNamespaces(xmlns); Model object1 = new Model(sbmlns); assertTrue( object1.getTypeCode() == libsbml.SBML_MODEL ); assertTrue( object1.getMetaId() == "" ); assertTrue( object1.getNotes() == null ); assertTrue( object1.getAnnotation() == null ); assertTrue( object1.getLevel() == 2 ); assertTrue( object1.getVersion() == 1 ); assertTrue( object1.getNamespaces() != null ); assertTrue( object1.getNamespaces().getLength() == 2 ); object1 = null; }
public void test_L3_Model_createWithNS() { XMLNamespaces xmlns = new XMLNamespaces(); xmlns.add( "http://www.sbml.org", "testsbml"); SBMLNamespaces sbmlns = new SBMLNamespaces(3,1); sbmlns.addNamespaces(xmlns); Model m = new Model(sbmlns); assertTrue( m.getTypeCode() == libsbml.SBML_MODEL ); assertTrue( m.getMetaId() == "" ); assertTrue( m.getNotes() == null ); assertTrue( m.getAnnotation() == null ); assertTrue( m.getLevel() == 3 ); assertTrue( m.getVersion() == 1 ); assertTrue( m.getNamespaces() != null ); assertTrue( m.getNamespaces().getLength() == 2 ); assertTrue( m.getId() == "" ); assertTrue( m.getName() == "" ); assertTrue( m.getSubstanceUnits() == "" ); assertTrue( m.getTimeUnits() == "" ); assertTrue( m.getVolumeUnits() == "" ); assertTrue( m.getAreaUnits() == "" ); assertTrue( m.getLengthUnits() == "" ); assertTrue( m.getConversionFactor() == "" ); assertEquals( false, m.isSetId() ); assertEquals( false, m.isSetName() ); assertEquals( false, m.isSetSubstanceUnits() ); assertEquals( false, m.isSetTimeUnits() ); assertEquals( false, m.isSetVolumeUnits() ); assertEquals( false, m.isSetAreaUnits() ); assertEquals( false, m.isSetLengthUnits() ); assertEquals( false, m.isSetConversionFactor() ); m = null; }
private static void setEssentialEntity(Model aSBMLModel) { // // set N_A Parameter // bool isAbogadroNumber = false; foreach (ParameterStruct aParameter in SbmlFunctions.getParameter(aSBMLModel)) { if (aSBMLModel.getLevel() == 1 && aParameter.Name == "N_A") isAbogadroNumber = true; else if (aSBMLModel.getLevel() == 2 && aParameter.ID == "N_A") isAbogadroNumber = true; } if ( !isAbogadroNumber ) { // create Parameter object Parameter aParameter = aSBMLModel.createParameter(); // set Parameter Name if (aSBMLModel.getLevel() == 1) aParameter.setName("N_A"); else if (aSBMLModel.getLevel() == 2) aParameter.setId("N_A"); // set Parameter Value aParameter.setValue(6.0221367e+23); // set Parameter Constant aParameter.setConstant(true); } // ------------ // set EmptySet // ------------ //bool isEmptySet = false; //foreach (SpeciesStruct aSpecies in SbmlFunctions.getSpecies(aSBMLModel)) //{ // if (aSBMLModel.getLevel() == 1 && aSpecies.Name == "EmptySet") // isEmptySet = true; // else if (aSBMLModel.getLevel() == 2 && aSpecies.ID == "EmptySet") // isEmptySet = true; //} //if (!isEmptySet) //{ // // create Species object // Species aSpecies = aSBMLModel.createSpecies(); // // set Species Name // if (aSBMLModel.getLevel() == 1) // aSpecies.setName("EmptySet"); // else if (aSBMLModel.getLevel() == 2) // aSpecies.setId("EmptySet"); // // set Species Compartment // aSpecies.setCompartment("default"); // // set Species Amount // aSpecies.setInitialAmount(0); // // set Species Constant // aSpecies.setConstant(true); //} }
private static void createSystem(EcellObject anEml, Model aSBMLModel) { if ( anEml.LocalID == "SBMLParameter" || anEml.LocalID == "SBMLRule" ) return; // create Compartment object Compartment aCompartment = aSBMLModel.createCompartment(); // set ID ROOT System and Other System string aCompartmentID = ""; if (anEml.LocalID == "/") aCompartmentID = "default"; // Root system else aCompartmentID = anEml.LocalID; // aCompartmentID = "default" + anEml.Key.Replace( "/", "__" ); ID_Namespace.Add( aCompartmentID ); if (aSBMLModel.getLevel() == 1) aCompartment.setName( aCompartmentID ); else if (aSBMLModel.getLevel() == 2) aCompartment.setId( aCompartmentID ); foreach(EcellObject child in anEml.Children) { if (!(child is EcellVariable)) continue; // set Size and constant of Compartment if( child.LocalID == "SIZE" ) { double size = ((EcellSystem)anEml).SizeInVolume; aCompartment.setSize(size); EcellValue value = anEml.GetEcellValue("Fixed"); if(value != null) aCompartment.setConstant(System.Convert.ToBoolean((int)value)); } // set Dimensions of Compartment else if( child.LocalID == "Dimensions" ) { int dimension = (int)child.GetEcellValue("Value"); aCompartment.setSpatialDimensions(dimension); } } // set Outside element of Compartment if( anEml.ParentSystemID == "/" && anEml.LocalID != "") { aCompartment.setOutside( "default" ); } else if (!string.IsNullOrEmpty(anEml.ParentSystemID)) { aCompartment.setOutside( getCurrentCompartment( anEml.ParentSystemID )); } }
private static void createSpecies(EcellObject anEml, Model aSBMLModel) { string aCurrentCompartment = getCurrentCompartment( anEml.ParentSystemID ); Compartment aCurrentCompartmentObj = aSBMLModel.getCompartment( aCurrentCompartment ); if( aCurrentCompartment == "SBMLParameter" ) { // ------------------------ // create Parameter object // ------------------------ Parameter aParameter = aSBMLModel.createParameter(); string aParameterID; if ( ID_Namespace.Contains(anEml.LocalID) == false ) aParameterID = anEml.LocalID; else aParameterID = "SBMLParamter__" + anEml.LocalID; // set Paramter ID to Id namespace ID_Namespace.Add( aParameterID ); // set Parameter ID if (aSBMLModel.getLevel() == 1) aParameter.setName( aParameterID ); else if (aSBMLModel.getLevel() == 2) aParameter.setId( aParameterID ); // set Parameter Name, Value and Constant foreach(EcellData aProperty in anEml.Value) { string aFullPN = anEml.FullID + ":" + aProperty.Name; // set Parameter Name if ( aProperty.Name == "Name" ) { if (aSBMLModel.getLevel() == 1) { } if (aSBMLModel.getLevel() == 2) { aParameter.setName( (string)aProperty.Value ); } } // set Parameter Value else if ( aProperty.Name == "Value" ) { aParameter.setValue( (double)aProperty.Value ); } // set Constant else if ( aProperty.Name == "Fixed" ) { aParameter.setConstant(System.Convert.ToBoolean((int)aProperty.Value)); } else { Trace.WriteLine("Unrepresentable property; " + aFullPN); //throw new EcellException("Unrepresentable property; " + aFullPN); } } } else { if( anEml.LocalID != "SIZE" && anEml.LocalID != "Dimensions" ) { // create Species object Species aSpecies = aSBMLModel.createSpecies(); // set Species ID string aSpeciesID; if ( ID_Namespace.Contains( anEml.LocalID) == false ) aSpeciesID = anEml.LocalID; else { if ( !anEml.ParentSystemID.Equals("/") ) aSpeciesID = anEml.LocalID; else aSpeciesID = anEml.LocalID; } ID_Namespace.Add( aSpeciesID ); if (aSBMLModel.getLevel() == 1) aSpecies.setName( aSpeciesID ); if (aSBMLModel.getLevel() == 2) aSpecies.setId( aSpeciesID ); // set Compartment of Species if( aCurrentCompartment == "" ) aSpecies.setCompartment( "default" ); else aSpecies.setCompartment( aCurrentCompartment ); // set Species Name, Value and Constant foreach(EcellData aProperty in anEml.Value) { string aFullPN = anEml.FullID + ":" + aProperty.Name; // set Species Name if ( aProperty.Name == "Name" ) { if (aSBMLModel.getLevel() == 1) { } else if (aSBMLModel.getLevel() == 2) { aSpecies.setName( (string)aProperty.Value ); } } // set Species Value else if ( aProperty.Name == "Value" ) { // aMolarValue = convertToMoleUnit( // anEml.getEntityProperty( aFullPN )[0] ) aSpecies.setInitialAmount( (double)aProperty.Value / 6.0221367e+23 ); } // set Species Constant else if ( aProperty.Name == "Fixed" ) { aSpecies.setConstant(System.Convert.ToBoolean((int)aProperty.Value)); } // set Concentration by rule else if ( aProperty.Name == "MolarConc" ) { // XXX: units are just eventually correct here, because // SBML falls back to mole and liter for substance and // volume of the species if these are unspecified. if (aSBMLModel.getLevel() == 1) { double compVol; if (aCurrentCompartmentObj != null) compVol = aCurrentCompartmentObj.getVolume(); else compVol = 1.0; double propValue = (double)aProperty.Value; aSpecies.setInitialAmount( compVol * propValue ); } else // SBML lv.2 { aSpecies.setInitialConcentration( (double)aProperty.Value ); } } else { Trace.WriteLine("Unrepresentable property; " + aFullPN); //throw new EcellException("Unrepresentable property: " + aFullPN); } } } } }
private static void createReaction(EcellObject anEml, Model aSBMLModel) { EcellProcess aProcess = (EcellProcess)anEml; List<EcellReference> aVariableReferenceList = aProcess.ReferenceList; string anExpression = aProcess.Expression; // ------------------ // make Rule object // ------------------ if ( anEml.ParentSystemID == "/SBMLRule" ) { // get Process Class anExpression = convertExpression( anExpression, aVariableReferenceList, aProcess.ParentSystemID); if( aProcess.Classname == ProcessConstants.ExpressionAlgebraicProcess ) { // create AlgebraicRule object AlgebraicRule anAlgebraicRule = aSBMLModel.createAlgebraicRule(); // set AlgebraicRule Formula anAlgebraicRule.setFormula( anExpression ); } else if( aProcess.Classname == ProcessConstants.ExpressionAssignmentProcess ) { foreach(EcellReference aVariableReference in aVariableReferenceList) { if ( aVariableReference.Coefficient != 0 ) { // create AssignmentRule object AssignmentRule anAssignmentRule =aSBMLModel.createAssignmentRule(); // set AssignmentRule Formula anAssignmentRule.setFormula( aVariableReference.Coefficient.ToString() + "* ( " + anExpression + ")" ); string aVariableID = getVariableReferenceId( aVariableReference.FullID, aProcess.ParentSystemID ); anAssignmentRule.setVariable( aVariableID ); } } } else if( aProcess.Classname == ProcessConstants.ExpressionFluxProcess ) { foreach(EcellReference aVariableReference in aVariableReferenceList) { if ( aVariableReference.Coefficient != 0 ) { // create AssignmentRule object RateRule aRateRule = aSBMLModel.createRateRule(); // set AssignmentRule Formula aRateRule.setFormula( aVariableReference.Coefficient + "* ( " + anExpression + ")" ); string aVariableID = getVariableReferenceId( aVariableReference.FullID, aProcess.ParentSystemID ); aRateRule.setVariable( aVariableID ); } } } else { throw new EcellException("The type of Process must be Algebraic, Assignment, Flux Processes: " + aProcess.Key); } } // ---------------------- // make Reaction object // ---------------------- else { // create Parameter object Reaction aReaction = aSBMLModel.createReaction(); // create KineticLaw Object KineticLaw aKineticLaw = aSBMLModel.createKineticLaw(); // set Reaction ID if (aSBMLModel.getLevel() == 1) aReaction.setName( anEml.LocalID ); if (aSBMLModel.getLevel() == 2) aReaction.setId( anEml.LocalID ); foreach(EcellData aProperty in anEml.Value) { if (!aProperty.Loadable) continue; string aFullPN = aProcess.FullID + ":" + aProperty.Name; // set Name property ( Name ) if ( aProperty.Name == "Name" ) { // set Reaction Name if (aSBMLModel.getLevel() == 1) { } else if (aSBMLModel.getLevel() == 2) aReaction.setName( (string)aProperty.Value ); } // set Expression property ( KineticLaw Formula ) else if ( aProperty.Name == "Expression") { // convert Expression of the ECELL format to // SBML kineticLaw formula // setExpressionAnnotation( aKineticLaw, anExpression ); bool aDelayFlag = false; anExpression = convertExpression(anExpression, aVariableReferenceList, aProcess.ParentSystemID); // get Current System Id string CompartmentOfReaction = ""; foreach (EcellReference aVariableReference in aVariableReferenceList) { if (aVariableReference.Coefficient == 0) continue; CompartmentOfReaction = getCompartmentID(aVariableReference.FullID, aProcess.ParentSystemID); } // set KineticLaw Formula if ( aDelayFlag == false ) aKineticLaw.setFormula( anExpression ); else { ASTNode anASTNode = libsbml.libsbml.parseFormula( anExpression ); anASTNode = setDelayType( anASTNode ); aKineticLaw.setMath( anASTNode ); } } // set VariableReference property ( SpeciesReference ) else if ( aProperty.Name == "VariableReferenceList" ) { // make a flag. Because SBML model is defined // both Product and Reactant. This flag is required // in order to judge whether the Product and the // Reactant are defined. bool aReactantFlag = false; bool aProductFlag = false; foreach(EcellReference aVariableReference in aVariableReferenceList) { // -------------------------------- // add Reactants to Reaction object // -------------------------------- if ( aVariableReference.Coefficient < 0 ) { // change the Reactant Flag aReactantFlag = true; // create Reactant object SpeciesReference aReactant = aSBMLModel.createReactant(); // set Species Id to Reactant object string aSpeciesReferenceId = getVariableReferenceId ( aVariableReference.FullID, aProcess.ParentSystemID ); aReactant.setSpecies( aSpeciesReferenceId ); // set Stoichiometry aReactant.setStoichiometry( -aVariableReference.Coefficient ); } // ------------------------------- // add Products to Reaction object // ------------------------------- else if ( aVariableReference.Coefficient > 0 ) { // change the Product Flag aProductFlag = true; // create Product object SpeciesReference aProduct = aSBMLModel.createProduct(); // set Species Id string aSpeciesReferenceId = getVariableReferenceId ( aVariableReference.FullID, aProcess.ParentSystemID ); aProduct.setSpecies( aSpeciesReferenceId ); // set Stoichiometry aProduct.setStoichiometry( aVariableReference.Coefficient ); } // -------------------------------- // add Modifiers to Reaction object // -------------------------------- else { // create Modifier object ModifierSpeciesReference aModifier = aSBMLModel.createModifier(); // set Species Id to Modifier object string aVariableReferenceId = getVariableReferenceId( aVariableReference.FullID, aProcess.ParentSystemID ); aModifier.setSpecies( aVariableReferenceId ); } } // set EmptySet Species, because if it didn"t define, // Reactant or Product can not be defined. //if (!aReactantFlag) //{ // // create Reactant object // SpeciesReference aReactant = aSBMLModel.createReactant(); // // set Species Id to Reactant object // aReactant.setSpecies("EmptySet"); // // set Stoichiometry // aReactant.setStoichiometry(0); //} //else if (!aProductFlag) //{ // // create Product object // SpeciesReference aProduct = aSBMLModel.createProduct(); // // set Species Id // aProduct.setSpecies("EmptySet"); // // set Stoichiometry // aProduct.setStoichiometry(0); //} } // These properties are not defined in SBML Lv2 else if ( aProperty.Name == "Priority" || aProperty.Name == "Activity" || aProperty.Name == "IsContinuous" || aProperty.Name == "StepperID" || aProperty.Name == "FireMethod" || aProperty.Name == "InitializeMethod" ) { continue; } else { // create Parameter Object (Local) Parameter aParameter = aSBMLModel.createKineticLawParameter(); // set Parameter ID aParameter.setId( aProperty.Name ); // set Parameter Value aParameter.setValue((double)aProperty.Value); } } // add KineticLaw Object to Reaction Object aReaction.setKineticLaw( aKineticLaw ); } }