Exemple #1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        public static EcellModel Convert(string filename)
        {
            if (!File.Exists(filename))
                throw new EcellException(string.Format(MessageResources.ErrFindFile, filename));

            string sbml = File.ReadAllText(filename);
            SBMLReader reader = new SBMLReader();
            SBMLDocument document = reader.readSBMLFromString(sbml);
            Model model = document.getModel();
            if (model == null)
            {
                throw new EcellException(string.Format("{0} has no model.", filename));
            }
            SBML_Model theModel = new SBML_Model(model);
            SBML_Compartment theCompartment = new SBML_Compartment(theModel);
            SBML_Parameter theParameter = new SBML_Parameter( theModel );
            SBML_Species theSpecies = new SBML_Species( theModel );
            SBML_Rule theRule = new SBML_Rule( theModel );
            SBML_Reaction theReaction = new SBML_Reaction(theModel);
            SBML_Event theEvent = new SBML_Event(theModel);

            document.Dispose();

            //Eml eml = new Eml(null);
            string modelId = Path.GetFileNameWithoutExtension(filename);
            EcellModel modelObject = (EcellModel)EcellObject.CreateObject(modelId, "", Constants.xpathModel, "", new List<EcellData>());
            // Set Layer
            List<EcellLayer> layers = new List<EcellLayer>();
            layers.Add(new EcellLayer(SbmlConstant.GlobalParameters, true));
            modelObject.Layers = layers;

            //
            // Set Stepper.
            //
            EcellObject stepper = EcellObject.CreateObject(modelId, "DE", EcellObject.STEPPER, "FixedODE1Stepper", new List<EcellData>());
            modelObject.Children.Add(stepper);

            //
            // Set Compartment ( System ).
            //
            EcellSystem rootSystem = (EcellSystem)EcellObject.CreateObject(modelId, "/", EcellObject.SYSTEM, EcellObject.SYSTEM, new List<EcellData>());
            rootSystem.SetEcellValue("StepperID", new EcellValue("DE"));
            rootSystem.SetEcellValue("Name", new EcellValue("Default"));
            modelObject.Children.Add(rootSystem);

            EcellSystem system = rootSystem;
            foreach (CompartmentStruct aCompartment in theModel.CompartmentList)
            {
                // getPath
                string aPath = "";
                if ( theModel.Level == 1 )
                    aPath = theModel.getPath( aCompartment.Name );
                else if ( theModel.Level == 2 )
                    aPath = theModel.getPath( aCompartment.ID );

                // setFullID
                if( aPath != "/" )
                {
                    system = (EcellSystem)EcellObject.CreateObject(modelId, aPath, EcellObject.SYSTEM, EcellObject.SYSTEM, new List<EcellData>());
                    modelObject.Children.Add(system);
                }

                // setStepper
                system.SetEcellValue("StepperID", new EcellValue("DE"));

                // setName( default = [] )
                if (theModel.Level == 2)
                    if (aCompartment.Name != "")
                        system.SetEcellValue("Name", new EcellValue(aCompartment.Name));

                // setDimensions( default = 3 )
                EcellObject dimension = EcellObject.CreateObject(modelId, aPath + ":Dimensions", EcellObject.VARIABLE, EcellObject.VARIABLE, new List<EcellData>());
                dimension.SetEcellValue("Value", new EcellValue((int)aCompartment.SpatialDimension));
                system.Children.Add(dimension);

                // setSIZE
                EcellObject size = EcellObject.CreateObject(modelId, aPath + ":SIZE", EcellObject.VARIABLE, EcellObject.VARIABLE, new List<EcellData>());
                size.SetEcellValue("Value", new EcellValue((double)theCompartment.getCompartmentSize(aCompartment)));
                system.Children.Add(size);
            }

            // Set GlobalParameter ( Variable )
            if ( theModel.ParameterList.Count > 0)
            {
                foreach(ParameterStruct aParameter in theModel.ParameterList)
                {
                    // setFullID
                    string parameterKey = theParameter.getParameterID( aParameter );
                    EcellObject parameter = EcellObject.CreateObject(modelId, parameterKey, EcellObject.VARIABLE, EcellObject.VARIABLE, new List<EcellData>());
                    // Set Layer
                    parameter.Layer = SbmlConstant.GlobalParameters;
                    // setName
                    if ( aParameter.Name != "" )
                        parameter.SetEcellValue("Name", new EcellValue(aParameter.Name));

                    // setValue
                    parameter.SetEcellValue("Value", new EcellValue(aParameter.Value));

                    // setFixed ( default = 1 )
                    if ( aParameter.Constant)
                        parameter.SetEcellValue("Fixed", new EcellValue(1));

                    // set to system.
                    rootSystem.Children.Add(parameter);
                }
            }

            // Set Species ( Variable )
            foreach(SpeciesStruct aSpecies in theModel.SpeciesList)
            {
                // Create
                string aSpeciesID = theSpecies.getSpeciesID( aSpecies );
                EcellObject variable = EcellObject.CreateObject(modelId, aSpeciesID, EcellObject.VARIABLE, EcellObject.VARIABLE, new List<EcellData>());
                // setName
                if( theModel.Level == 2 )
                    if ( aSpecies.Name != "" )
                        variable.SetEcellValue("Name", new EcellValue(aSpecies.Name));

                // setValue
                variable.SetEcellValue("Value", new EcellValue(theSpecies.getSpeciesValue( aSpecies )));

                // setFixed
                variable.SetEcellValue("Fixed", new EcellValue(theSpecies.getConstant( aSpecies )));

                // add
                modelObject.AddEntity(variable);
            }

            // Set Rule ( Process )
            if ( theModel.RuleList.Count > 0)
            {
                // make Rule System
                string aSystemKey = "/SBMLRule";
                system = (EcellSystem)EcellObject.CreateObject(modelId, aSystemKey, EcellObject.SYSTEM, EcellObject.SYSTEM, new List<EcellData>());
                system.SetEcellValue("Name", new EcellValue("System for SBML Rule"));
                system.SetEcellValue("StepperID", new EcellValue("DE"));
                modelObject.Children.Add(system);

                foreach(RuleStruct aRule in theModel.RuleList)
                {
                    theRule.initialize();

                    // setFullID
                    string aRuleID = theRule.getRuleID();
                    EcellObject process = EcellObject.CreateObject(modelId, aRuleID, EcellObject.PROCESS, EcellObject.PROCESS, new List<EcellData>());
                    modelObject.AddEntity(process);

                    // Algebraic Rule
                    if ( aRule.RuleType == libsbml.libsbml.SBML_ALGEBRAIC_RULE )
                    {
                        process.Classname = "ExpressionAlgebraicProcess";
                    }
                    // Assignment Rule
                    else if (aRule.RuleType == libsbml.libsbml.SBML_ASSIGNMENT_RULE ||
                           aRule.RuleType == libsbml.libsbml.SBML_SPECIES_CONCENTRATION_RULE ||
                           aRule.RuleType == libsbml.libsbml.SBML_COMPARTMENT_VOLUME_RULE ||
                           aRule.RuleType == libsbml.libsbml.SBML_PARAMETER_RULE)
                    {
                        process.Classname = "ExpressionAssignmentProcess";

                        int aVariableType = theRule.getVariableType( aRule.Variable );

                        if (aVariableType == libsbml.libsbml.SBML_SPECIES)
                            theRule.setSpeciesToVariableReference( aRule.Variable, 1 );
                        else if (aVariableType == libsbml.libsbml.SBML_PARAMETER)
                            theRule.setParameterToVariableReference( aRule.Variable, 1 );
                        else if (aVariableType == libsbml.libsbml.SBML_COMPARTMENT)
                            theRule.setCompartmentToVariableReference( aRule.Variable, 1 );
                        else
                            throw new EcellException("Variable type must be Species, Parameter, or Compartment");
                    }
                    // Rate Rule
                    else if (aRule.RuleType == libsbml.libsbml.SBML_RATE_RULE)
                    {
                        process.Classname = "ExpressionFluxProcess";

                        int aVariableType = theRule.getVariableType( aRule.Variable );

                        if (aVariableType == libsbml.libsbml.SBML_SPECIES)
                            theRule.setSpeciesToVariableReference( aRule.Variable, 1 );
                        else if (aVariableType == libsbml.libsbml.SBML_PARAMETER)
                            theRule.setParameterToVariableReference( aRule.Variable, 1 );
                        else if (aVariableType == libsbml.libsbml.SBML_COMPARTMENT)
                            theRule.setCompartmentToVariableReference( aRule.Variable, 1 );
                        else
                            throw new EcellException("Variable type must be Species, Parameter, or Compartment");
                    }
                    else
                        throw new EcellException(" The type of Rule must be Algebraic, Assignment or Rate Rule");

                    // convert SBML formula to E-Cell formula
                    string convertedFormula = theRule.convertRuleFormula( aRule.Formula );

                    // set Expression Property
                    process.SetEcellValue("Expression", new EcellValue(convertedFormula));

                    // setVariableReferenceList
                    process.SetEcellValue("VariableReferenceList", new EcellValue(theRule.GetVariableReferenceList()));
                }
            }

            // Set Reaction ( Process )
            foreach(ReactionStruct aReaction in theModel.ReactionList)
            {
                theReaction.initialize();

                // setFullID
                string aReactionID = theReaction.getReactionID( aReaction );
                EcellProcess process = (EcellProcess)EcellObject.CreateObject(modelId, aReactionID, EcellObject.PROCESS, "ExpressionFluxProcess", new List<EcellData>());

                // setName
                if ( theModel.Level == 2 )
                    if( aReaction.Name != "" )
                        process.SetEcellValue("Name",new EcellValue(aReaction.Name));

                // setSubstrate
                foreach(ReactantStruct aSubstrate in aReaction.Reactants)
                {
                    string name = "S" + theReaction.SubstrateNumber.ToString();
                    string aSubstrateID = theModel.getSpeciesReferenceID( aSubstrate.Species );
                    if ( aSubstrateID == null )
                        throw new EcellException("Species "+aSubstrate.Species+" not found");

                    if ( aSubstrate.Denominator != 1 )
                        throw new EcellException("Stoichiometry Error : E-Cell System can't set a floating Stoichiometry");
                    int coefficient = -1 * theReaction.getStoichiometry(aSubstrate.Species, aSubstrate.Stoichiometry );

                    VariableReferenceStruct aSubstrateList = new VariableReferenceStruct(name, "Variable:" + aSubstrateID, coefficient);
                    theReaction.VariableReferenceList.Add( aSubstrateList );
                    theReaction.SubstrateNumber = theReaction.SubstrateNumber + 1;
                }

                // setProduct
                foreach(ProductStruct aProduct in aReaction.Products)
                {
                    string name = 'P' + theReaction.ProductNumber.ToString();
                    string aProductID = theModel.getSpeciesReferenceID( aProduct.Species );
                    if ( aProductID == "" )
                        throw new EcellException("Species "+aProduct.Species+" not found");

                    if ( aProduct.Denominator != 1 )
                        throw new EcellException("Stoichiometry Error : E-Cell System can't set a floating Stoichiometry");

                    int coefficient = 1 * theReaction.getStoichiometry(aProduct.Species,  aProduct.Stoichiometry );

                    VariableReferenceStruct aProductList = new VariableReferenceStruct(name, "Variable:" + aProductID, coefficient);
                    theReaction.VariableReferenceList.Add( aProductList );
                    theReaction.ProductNumber = theReaction.ProductNumber + 1;
                }

                // setCatalyst
                foreach(string aModifier in aReaction.Modifiers)
                {
                    string name = "C" + theReaction.ModifierNumber.ToString();
                    string aModifierID = theModel.getSpeciesReferenceID( aModifier );
                    if ( aModifierID == "" )
                        throw new EcellException("Species "+aModifier+" not found");

                    VariableReferenceStruct aModifierList = new VariableReferenceStruct(name, "Variable:" + aModifierID, 0);
                    theReaction.VariableReferenceList.Add( aModifierList );
                    theReaction.ModifierNumber = theReaction.ModifierNumber + 1;

                }

                // setProperty
                foreach(KineticLawStruct kineticLaw in aReaction.KineticLaws)
                {
                    foreach(ParameterStruct aParameter in kineticLaw.Parameters)
                    {
                        if (theModel.Level == 1)
                            process.SetEcellValue(aParameter.Name, new EcellValue(aParameter.Value));
                        else if (theModel.Level == 2)
                            process.SetEcellValue(aParameter.ID, new EcellValue(aParameter.Value));
                    }

                    // set "Expression" Property
                    // convert SBML format formula to E-Cell format formula
                    if( kineticLaw.Formula != "" )
                    {
                        string anExpression = theReaction.convertKineticLawFormula( kineticLaw.Formula );

                        // set Expression Property for ExpressionFluxProcess
                        process.SetEcellValue("Expression", new EcellValue(anExpression));

                        // setVariableReferenceList
                        process.SetEcellValue("VariableReferenceList", new EcellValue(theReaction.GetVariableReferenceList()));
                    }
                }
                // Set Parent System.
                string sysKey = process.ParentSystemID;
                foreach (EcellReference er in process.ReferenceList)
                {
                    if (er.Coefficient == -1)
                        sysKey = er.Key.Split(':')[0];
                }
                foreach (EcellReference er in process.ReferenceList)
                {
                    if (er.Coefficient == 1)
                        sysKey = er.Key.Split(':')[0];
                }
                process.Key = sysKey + ":" + process.LocalID;
                // Update EntityPath.
                foreach (EcellData data in process.Value)
                {
                    data.EntityPath = process.FullID + ":" + data.Name;
                }

                modelObject.AddEntity(process);

            }

            return modelObject;
        }
Exemple #2
0
 public void TestSBML_Rule()
 {
     SBMLReader reader = new SBMLReader();
     SBMLDocument document = reader.readSBML(TestConstant.SBML_BIOMD0000000003);
     Model sbmlModel = document.getModel();
     SBML_Model model = new SBML_Model(sbmlModel);
     SBML_Compartment sc = new SBML_Compartment(model);
     SBML_Species species = new SBML_Species(model);
     SBML_Rule rule = new SBML_Rule(model);
 }
Exemple #3
0
        public void TestSBML_Species()
        {
            SBMLReader reader = new SBMLReader();
            SBMLDocument document = reader.readSBML(TestConstant.SBML_Oscillation);

            SBML_Model model = new SBML_Model(document.getModel());
            SBML_Compartment sc = new SBML_Compartment(model);
            SBML_Species species = new SBML_Species(model);

            // getConstant
            SpeciesStruct ss = new SpeciesStruct();
            int i = species.getConstant(ss);
            Assert.AreEqual(0, i, "getConstant returns unexpected value.");

            ss.Constant = true;
            i = species.getConstant(ss);
            Assert.AreEqual(1, i, "getConstant returns unexpected value.");

            model.Level = 1;
            i = species.getConstant(ss);
            Assert.AreEqual(0, i, "getConstant returns unexpected value.");

            ss.BoundaryCondition = true;
            i = species.getConstant(ss);
            Assert.AreEqual(1, i, "getConstant returns unexpected value.");

            try
            {
                model.Level = 0;
                i = species.getConstant(ss);
            }
            catch (Exception)
            {
            }

            // getSpeciesValue
            double d = species.getSpeciesValue(ss);
            Assert.AreEqual(0.0, d, "getSpeciesValue returns unexpected value.");

            model.Level = 2;
            ss.InitialAmount = double.NaN;
            ss.Compartment = "cell";
            d = species.getSpeciesValue(ss);
            Assert.AreEqual(0.0, d, "getSpeciesValue returns unexpected value.");

            try
            {
                ss.InitialConcentration = double.NaN;
                d = species.getSpeciesValue(ss);
            }
            catch (Exception)
            {
            }

            // getSpeciesID
            ss.ID = "Test1";
            string id = species.getSpeciesID(ss);
            Assert.AreEqual("/cell:Test1", id, "getSpeciesID returns unexpected value.");

            try
            {
                model.Level = 1;
                ss.Name = "Test2";
                id = species.getSpeciesID(ss);
            }
            catch (Exception)
            {
            }

            try
            {
                model.Level = 0;
                id = species.getSpeciesID(ss);
            }
            catch (Exception)
            {
            }

            try
            {
                model.Level = 2;
                ss.Compartment = "";
                id = species.getSpeciesID(ss);
            }
            catch (Exception)
            {
            }
        }
Exemple #4
0
        public void TestSBML_Compartment()
        {
            SBMLReader reader = new SBMLReader();
            SBMLDocument document = reader.readSBML(TestConstant.SBML_Oscillation);

            SBML_Model model = new SBML_Model(document.getModel());
            SBML_Compartment c = new SBML_Compartment(model);

            CompartmentStruct cs = new CompartmentStruct();
            try
            {
                c.getCompartmentID(cs);
            }
            catch (Exception)
            {
            }
            string id;

            cs.Outside = "";
            cs.ID = "Test1";
            id = c.getCompartmentID(cs);
            Assert.AreEqual("System:/:Test1", id, "getCompartmentID returns unexpected value.");

            try
            {
                model.Level = 1;
                id = c.getCompartmentID(cs);
            }
            catch (Exception)
            {
            }

            try
            {
                model.Level = 0;
                id = c.getCompartmentID(cs);
            }
            catch (Exception)
            {
            }

            model.Level = 2;
            cs.Outside = "cell";
            id = c.getCompartmentID(cs);
            Assert.AreEqual("System:/cell:Test1", id, "getCompartmentID returns unexpected value.");

            try
            {
                model.Level = 1;
                id = c.getCompartmentID(cs);
            }
            catch (Exception)
            {
            }

            try
            {
                model.Level = 0;
                id = c.getCompartmentID(cs);
            }
            catch (Exception)
            {
            }

            // GetModuleType
            Type type = c.GetType();
            MethodInfo info1 = type.GetMethod("setSizeToDictionary", BindingFlags.NonPublic | BindingFlags.Instance);
            MethodInfo info2 = type.GetMethod("setUnitToDictionary", BindingFlags.NonPublic | BindingFlags.Instance);
            try
            {
                info1.Invoke(c, new object[] { cs });
            }
            catch (Exception)
            {
            }
            try
            {
                info2.Invoke(c, new object[] { cs });
            }
            catch (Exception)
            {
            }
            model.Level = 1;
            cs.Volume = double.NaN;
            cs.Name = "Test1";
            info1.Invoke(c, new object[] { cs });
            info2.Invoke(c, new object[] { cs });

            // getOutsideSize
            model.Level = 2;
            MethodInfo info3 = type.GetMethod("getOutsideSize", BindingFlags.NonPublic | BindingFlags.Instance);
            double size;
            size = (double)info3.Invoke(c, new object[] { "" });
            Assert.AreEqual(1.0d, size, "getOutsideSize returns unexpected value.");

            size = (double)info3.Invoke(c, new object[] { "cell" });
            Assert.AreEqual(1.0d, size, "getOutsideSize returns unexpected value.");

            // getOutsideUnit
            MethodInfo info4 = type.GetMethod("getOutsideUnit", BindingFlags.NonPublic | BindingFlags.Instance);
            string unit;
            unit = (string)info4.Invoke(c, new object[] { "" });
            Assert.AreEqual("", unit, "getCompartmentSize returns unexpected value.");

            unit = (string)info4.Invoke(c, new object[] { "cell" });
            Assert.AreEqual("", unit, "getCompartmentSize returns unexpected value.");

            // getCompartmentSize
            size = c.getCompartmentSize(cs);
            Assert.AreEqual(1.0d, size, "getCompartmentSize returns unexpected value.");

            model.Level = 1;
            size = c.getCompartmentSize(cs);
            Assert.AreEqual(1.0d, size, "getCompartmentSize returns unexpected value.");

            try
            {
                model.Level = 0;
                size = c.getCompartmentSize(cs);
            }
            catch (Exception)
            {
            }

            // getCompartmentUnit
            model.Level = 2;
            id = c.getCompartmentUnit(cs);
            Assert.AreEqual(null, id, "getCompartmentUnit returns unexpected value.");

            model.Level = 1;
            id = c.getCompartmentUnit(cs);
            Assert.AreEqual(null, id, "getCompartmentUnit returns unexpected value.");

            try
            {
                model.Level = 0;
                id = c.getCompartmentUnit(cs);
            }
            catch (Exception)
            {
            }
        }