Beispiel #1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="aName"></param>
        /// <returns></returns>
        public string setCompartmentToVariableReference(string aName)
        {
            foreach(CompartmentStruct aCompartment in this.Model.CompartmentList)
            {
                if ( aCompartment.ID == aName || aCompartment.Name == aName )
                {
                    foreach (VariableReferenceStruct aVariableReference in this.VariableReferenceList)
                        if( aVariableReference.Variable.Split(':')[2] == "SIZE" )
                        {
                            string aCurrentPath = aVariableReference.Variable.Split(':')[1];
                            int aLastSlash = aCurrentPath.LastIndexOf( "/" );
                            return aVariableReference.Name;
                        }
                    VariableReferenceStruct varRef = new VariableReferenceStruct(
                        aName,
                        "Variable:" + this.Model.getPath( aName ) + ":SIZE",
                        0);
                    this.VariableReferenceList.Add( varRef );

                    return varRef.Name;
                }
            }
            return "";
        }
Beispiel #2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="anASTNode"></param>
        /// <returns></returns>
        private ASTNode convertVariableName(ASTNode anASTNode)
        {
            long aNumChildren = anASTNode.getNumChildren();

            if ( aNumChildren == 2 )
            {
                this.convertVariableName( anASTNode.getLeftChild() );
                this.convertVariableName( anASTNode.getRightChild() );

                return anASTNode;
            }
            else if ( aNumChildren == 1 )
            {
                this.convertVariableName( anASTNode.getLeftChild() );
                return anASTNode;
            }
            else if ( aNumChildren == 0 )
            {
                if ( anASTNode.isNumber() )
                {
                }
                else
                {
                    string aName = anASTNode.getName();
                    string variableName = "";

                    // Check Species
                    foreach(SpeciesStruct aSpecies in this.Model.SpeciesList)
                    {
                        if ( aSpecies.ID != aName && aSpecies.Name != aName)
                            continue;

                        // Check VariableReference
                        foreach(VariableReferenceStruct aVariableReference in this.VariableReferenceList)
                            if (aVariableReference.Variable.Split(':')[2] == aName)
                                variableName =  aVariableReference.Name;
                        if (variableName == "")
                        {
                            string aModifierID = this.Model.getSpeciesReferenceID(aName);

                            VariableReferenceStruct varRef = new VariableReferenceStruct(
                                "C" + this.ModifierNumber.ToString(),
                                "Variable:" + aModifierID,
                                0);
                            this.VariableReferenceList.Add(varRef);

                            variableName = varRef.Name;
                            this.ModifierNumber++;
                        }
                        //string compartmentName = this.setCompartmentToVariableReference( aSpecies.Compartment );
                        //anASTNode.setType( libsbml.libsbml.AST_DIVIDE );
                        //anASTNode.addChild( new ASTNode( libsbml.libsbml.AST_NAME ) );
                        //anASTNode.addChild( new ASTNode( libsbml.libsbml.AST_NAME ) );
                        //anASTNode.getLeftChild().setName( variableName + ".Value" );
                        //anASTNode.getRightChild().setName( compartmentName + ".Value" ) ;
                        anASTNode.setName(variableName + ".Value");
                        return anASTNode;
                    }

                    // Check Parameters.
                    foreach(ParameterStruct aParameter in this.Model.ParameterList)
                    {
                        if (aParameter.ID != aName && aParameter.Name != aName)
                            continue;
                        foreach(VariableReferenceStruct aVariableReference in this.VariableReferenceList)
                            if (aVariableReference.Variable.Split(':')[2] == aName)
                                variableName = aVariableReference.Name;

                        if( variableName == "" )
                        {
                            VariableReferenceStruct varRef = new VariableReferenceStruct(
                                aName,
                                "Variable:/:" + aName,
                                0 );
                            this.VariableReferenceList.Add( varRef );

                            this.ParameterNumber++;
                            variableName = varRef.Name;
                        }

                        anASTNode.setName( variableName + ".Value" );

                        return anASTNode;
                    }
            //                if variableName == '':
                    variableName = this.setCompartmentToVariableReference( aName );
                    if (variableName != "")
                        anASTNode.setName( variableName + ".Value" );
                }
            }
            return anASTNode;
        }
Beispiel #3
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;
        }
Beispiel #4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="aName"></param>
        /// <param name="aStoichiometry"></param>
        /// <returns></returns>
        public string setSpeciesToVariableReference(string aName, int aStoichiometry)
        {
            foreach(SpeciesStruct aSpecies in this.Model.SpeciesList)
            {
                if ( ( this.Model.Level == 1 && aSpecies.Name == aName ) ||
                     ( this.Model.Level == 2 && aSpecies.ID == aName ) )
                {
                    string compartmentName;
                    foreach(VariableReferenceStruct aVariableReference in this.VariableReferenceList)
                    {
                        if (aVariableReference.Variable.Split(':')[2] == aName)
                        {
                            if (aStoichiometry != 0)
                                aVariableReference.Coefficient = aStoichiometry;

                            compartmentName = this.setCompartmentToVariableReference(aSpecies.Compartment, 0);
                            return aVariableReference.Name;
                        }
                    }
                    List<string> aVariableList = new List<string>();

                    string variableName = "V" + this.VariableNumber.ToString();
                    string aVariableID = this.Model.getSpeciesReferenceID( aName );

                    VariableReferenceStruct varRef = new VariableReferenceStruct(
                        variableName,
                        "Variable:" + aVariableID,
                        aStoichiometry );

                    this.VariableReferenceList.Add( varRef );
                    this.VariableNumber++;

                    compartmentName = this.setCompartmentToVariableReference(aSpecies.Compartment, 0);

                    return variableName;
                }
            }
            throw new EcellException("Error set species to VariableReference");
        }
Beispiel #5
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="aName"></param>
        /// <param name="aStoichiometry"></param>
        /// <returns></returns>
        public string setParameterToVariableReference(string aName, int aStoichiometry)
        {
            foreach(ParameterStruct aParameter in this.Model.ParameterList)
            {
                if ( ( this.Model.Level == 1 && aParameter.Name == aName ) ||
                     ( this.Model.Level == 2 && aParameter.ID == aName ) )
                {
                    foreach(VariableReferenceStruct aVariableReference in this.VariableReferenceList)
                    {
                        if (aVariableReference.Variable.Split(':')[2] == aName)
                        {
                            if (aStoichiometry != 0)
                                aVariableReference.Coefficient = aStoichiometry;
                            return aVariableReference.Name;
                        }
                    }

                    string variableName = "P" + this.ParameterNumber.ToString();
                    this.ParameterNumber++;
                    VariableReferenceStruct varRef = new VariableReferenceStruct(
                        variableName,
                        "Variable:/:" + aName,
                        aStoichiometry );
                    this.VariableReferenceList.Add( varRef );

                    return variableName;
                }
            }
            throw new EcellException("Error set parameter to VariableReference");
        }
Beispiel #6
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="aName"></param>
        /// <param name="aStoichiometry"></param>
        /// <returns></returns>
        public string setCompartmentToVariableReference(string aName, int aStoichiometry)
        {
            foreach(CompartmentStruct aCompartment in this.Model.CompartmentList)
            {
                if ( ( this.Model.Level == 1 && aCompartment.Name == aName ) ||
                     ( this.Model.Level == 2 && aCompartment.ID == aName ) )
                {
                    foreach(VariableReferenceStruct aVariableReference in this.VariableReferenceList)
                    {
                        if( ( aVariableReference.Variable.Split(':')[1] == this.Model.getPath( aName ) )
                         && ( aVariableReference.Variable.Split(':')[2] == "SIZE" ) )
                        {
                            if (aStoichiometry != 0)
                                aVariableReference.Coefficient = aStoichiometry;

                            return aVariableReference.Name;
                        }
                    }
                    VariableReferenceStruct varRef = new VariableReferenceStruct(
                        aName,
                        "Variable:" + this.Model.getPath( aName ) + ":SIZE",
                        aStoichiometry );

                    this.VariableReferenceList.Add( varRef );

                    return aName;
                }
            }
            throw new EcellException("Error set compartment to VariableReference");
        }
Beispiel #7
0
 public void TestConstructorOfStructs()
 {
     //
     CompartmentStruct c = new CompartmentStruct(
         "ID",
         "Name",
         0,
         0.0,
         0.0,
         "Unit",
         "Parent",
         false);
     //
     EventStruct e = new EventStruct(
         "ID",
         "Name",
         "Trigger",
         "delay",
         "TimeUnit",
         new List<EventAssignmentStruct>());
     //
     EventAssignmentStruct ea = new EventAssignmentStruct(
         "Variable",
         "Formula");
     //
     FunctionDefinitionStruct fd = new FunctionDefinitionStruct(
         "ID",
         "Name",
         "Formula");
     //
     ParameterStruct p = new ParameterStruct(
         "ID",
         "Name",
         0.0,
         "Unit",
         false);
     //
     ReactionStruct r = new ReactionStruct(
         "ID",
         "Name",
         new List<KineticLawStruct>(),
         false,
         false,
         new List<ReactantStruct>(),
         new List<ProductStruct>(),
         new List<string>());
     //
     KineticLawStruct k = new KineticLawStruct(
         "Formula",
         new List<string>(),
         "TimeUnit",
         "Substance",
         new List<ParameterStruct>(),
         null);
     //
     ReactantStruct rs = new ReactantStruct(
         "Species",
         0,
         "Formula",
         0);
     //
     ProductStruct ps = new ProductStruct(
         "Species",
         0.0,
         "Formula",
         0);
     //
     RuleStruct rule = new RuleStruct(
         0,
         "Formula",
         "Variable");
     //
     SpeciesStruct s = new SpeciesStruct(
         "ID",
         "Name",
         "Parent",
         0.0,
         0.0,
         "Substance",
         "Spatial",
         "Unit",
         false,
         false,
         0,
         false);
     //
     UnitDefinitionStruct ud = new UnitDefinitionStruct(
         "ID",
         "Name",
         new List<UnitStruct>());
     //
     UnitStruct u = new UnitStruct(
         "Kind",
         0,
         0,
         0.0,
         0.0);
     //
     VariableReferenceStruct v = new VariableReferenceStruct(
         "Name",
         "Variable",
         0);
     //
     InitialAssignmentStruct i = new InitialAssignmentStruct(
         "Name",
         0.0);
 }