Beispiel #1
0
        public void TestBinaryDeserialize()
        {
            BinarySerial serializer = new BinarySerial();
            BinaryModel  model      = serializer.DeserializeBinaryData();

            Assert.AreEqual(model.Commmand, serializer.GetModel().Commmand);
        }
Beispiel #2
0
        /// <summary>
        ///     Populates the edges list based on the SBML Qual model content.
        /// </summary>
        /// <param name="model">The target binary model to populate</param>
        /// <param name="sbmlModel">The input SBML Qual model</param>
        private void PopulateEdges(BinaryModel model, Model sbmlModel)
        {
            var modelQualPlugin = (QualModelPlugin)sbmlModel.getPlugin("qual");
            var numTransitions  = modelQualPlugin.getNumTransitions();

            for (var i = 0; i < numTransitions; i++)
            {
                var transition = modelQualPlugin.getTransition(i);
                var numInputs  = transition.getNumInputs();
                var numOutputs = transition.getNumOutputs();

                for (var j = 0; j < numInputs; j++)
                {
                    var input = transition.getInput(j);
                    for (var k = 0; k < numOutputs; k++)
                    {
                        var output = transition.getOutput(k);
                        var edge   = new BinaryEdge(model, transition.getId(),
                                                    input.getSign() == libsbml.INPUT_SIGN_NEGATIVE ? EdgeType.Negative : EdgeType.Positive)
                        {
                            Input  = model.Nodes.FirstOrDefault(n => n.Id.Equals(input.getQualitativeSpecies())),
                            Output = model.Nodes.FirstOrDefault(n => n.Id.Equals(output.getQualitativeSpecies()))
                        };
                        model.Edges.Add(edge);
                    }
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Enriches the target model with the appropriate drugs having a positive effect on the target phenotype
        /// </summary>
        /// <param name="model">The target model</param>
        /// <param name="targetPhenotypes">The target phenotypes</param>
        /// <param name="drugsList">The available drugs list</param>
        /// <param name="drugNodes">The drug nodes that have been added to the model</param>
        /// <param name="drugEdges">The drug edges that have been added to the model</param>
        public void AddDrugsToModel(BinaryModel model, List <TargetPhenotype> targetPhenotypes, List <DrugDescription> drugsList, out List <BinaryNode> drugNodes, out List <BinaryEdge> drugEdges)
        {
            var phenotypes = GetTargetPhenotypes(model, targetPhenotypes);

            // Get drugs nodes and edges for each model
            drugNodes = new List <BinaryNode>();
            drugEdges = new List <BinaryEdge>();
            foreach (var phenotype in phenotypes)
            {
                var clonedModel = model.Clone();
                FindDrugs(clonedModel, phenotype.Key.Id, phenotype.Value, drugsList, out List <BinaryNode> intDrugNodes, out List <BinaryEdge> intDrugEdges);
                foreach (var drugNode in intDrugNodes)
                {
                    if (!drugNodes.Any(n => n.Id.Equals(drugNode.Id)))
                    {
                        drugNodes.Add(drugNode);
                    }
                }

                foreach (var drugEdge in intDrugEdges)
                {
                    if (!drugEdges.Any(e => e.Input.Id.Equals(drugEdge.Input.Id) && e.Output.Id.Equals(drugEdge.Output.Id) && e.Type == drugEdge.Type))
                    {
                        drugEdges.Add(drugEdge);
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        ///     Converts the current binary model to an SBML 3.1 Qual document and writes it to a file.
        /// </summary>
        public void WriteToSbmlFile(BinaryModel model, string outputPath)
        {
            var outputSbmlDocument = ToSbmlQualDocument(model);
            var writer             = new SBMLWriter();

            writer.writeSBML(outputSbmlDocument, outputPath);
        }
Beispiel #5
0
 /// <summary>
 /// Protects inputs and outputs
 /// </summary>
 /// <param name="model">The target model</param>
 public void ProtectInputsAndOutputs(BinaryModel model)
 {
     foreach (var binaryNode in model.Nodes)
     {
         if (binaryNode.IsInput() || binaryNode.IsOutput())
         {
             binaryNode.Protected = true;
         }
     }
 }
Beispiel #6
0
        /// <summary>
        ///     Initializes and returns a new binary model based on an SBML file.
        /// </summary>
        /// <param name="filePath">The SBML file path</param>
        /// <returns>A new <see cref="BinaryModel" /> instance initialized from <paramref name="filePath" /></returns>
        public BinaryModel ReadFromSbmlFile(string filePath)
        {
            var reader    = new SBMLReader();
            var sbmlDoc   = reader.readSBML(filePath);
            var sbmlModel = sbmlDoc.getModel();

            Console.WriteLine("Initializing boolean model");
            var binaryModel = new BinaryModel();

            InitFromSbmlQualModel(binaryModel, sbmlModel);
            return(binaryModel);
        }
Beispiel #7
0
        /// <summary>
        ///     Populates the model nodes list based on the SBML Qual model content.
        /// </summary>
        /// <param name="model">The target binary model to populate</param>
        /// <param name="sbmlModel">The input SBML Qual model</param>
        private void PopulateNodes(BinaryModel model, Model sbmlModel)
        {
            var modelQualPlugin       = (QualModelPlugin)sbmlModel.getPlugin("qual");
            var numQualitativeSpecies = modelQualPlugin.getNumQualitativeSpecies();

            for (var i = 0; i < numQualitativeSpecies; i++)
            {
                var qualitativeSpecies = modelQualPlugin.getQualitativeSpecies(i);
                var node = new BinaryNode(model, qualitativeSpecies.getId())
                {
                    Name = qualitativeSpecies.isSetName() ? qualitativeSpecies.getName() : string.Empty
                };
                model.Nodes.Add(node);
            }
        }
Beispiel #8
0
 /// <summary>
 /// Protects nodes based on a list.
 /// </summary>
 /// <param name="model">The target model</param>
 /// <param name="protectedNodes">The list of nodes to protect</param>
 /// <param name="drugsList">The list of drugs to protect</param>
 /// <param name="phenotypes">The list of phenotyeps to protect</param>
 public void ProtectNodes(BinaryModel model, List <string> protectedNodes, List <DrugDescription> drugsList, List <BinaryNode> phenotypes)
 {
     foreach (var binaryNode in model.Nodes)
     {
         if (protectedNodes.Contains(binaryNode.Id))
         {
             binaryNode.Protected = true;
         }
         else if (drugsList.Any(d => d.TargetId.Equals(binaryNode.Id)))
         {
             binaryNode.Protected = true;
         }
     }
     foreach (var binaryNode in phenotypes)
     {
         binaryNode.Protected = true;
     }
 }
Beispiel #9
0
        /// <summary>
        ///     Converts the current binary model to an SBML 3.1 Qual document.
        /// </summary>
        /// <returns>A new SBML Qual document based on curent binary model</returns>
        public SBMLDocument ToSbmlQualDocument(BinaryModel model)
        {
            var sbmlns   = new SBMLNamespaces(3, 1, "qual", 1);
            var document = new SBMLDocument(sbmlns);

            document.setPackageRequired("qual", true);
            var sbmlModel = document.createModel();
            var qualModel = (QualModelPlugin)sbmlModel.getPlugin("qual");

            var compartment = sbmlModel.createCompartment();

            compartment.setConstant(false);
            compartment.setId("Default");
            compartment.setName("Default");

            foreach (var binaryNode in model.Nodes)
            {
                var qualitativeSpecies = qualModel.createQualitativeSpecies();
                qualitativeSpecies.setConstant(false);
                qualitativeSpecies.setId(binaryNode.Id);
                qualitativeSpecies.setName(binaryNode.Name);
                qualitativeSpecies.setCompartment("Default");
            }

            foreach (var binaryEdge in model.Edges)
            {
                var transition = qualModel.createTransition();

                var input = transition.createInput();
                input.setQualitativeSpecies(binaryEdge.Input.Id);
                input.setTransitionEffect(libsbml.INPUT_TRANSITION_EFFECT_NONE);
                input.setSign(binaryEdge.IsPositive() ? libsbml.INPUT_SIGN_POSITIVE : libsbml.INPUT_SIGN_NEGATIVE);

                var output = transition.createOutput();
                output.setQualitativeSpecies(binaryEdge.Output.Id);
                output.setTransitionEffect(libsbml.OUTPUT_TRANSITION_EFFECT_PRODUCTION);
                output.setOutputLevel(1);

                var defaultTrem = transition.createDefaultTerm();
                defaultTrem.setResultLevel(1);
            }

            return(document);
        }
Beispiel #10
0
        /// <summary>
        /// In a target model, finds the drugs that could have the desired effect on the target node.
        /// </summary>
        /// <param name="model">The target model</param>
        /// <param name="nodeId">The Id of the target node</param>
        /// <param name="drugEffect">The desired effect</param>
        /// <param name="drugsList">The list of available drugs</param>
        /// <param name="drugNodes">The new nodes added for the drugs</param>
        /// <param name="drugsEdges">The new edges added for the drugs</param>
        public void FindDrugs(BinaryModel model, string nodeId, DrugEffect drugEffect, List <DrugDescription> drugsList, out List <BinaryNode> drugNodes, out List <BinaryEdge> drugsEdges)
        {
            drugNodes  = new List <BinaryNode>();
            drugsEdges = new List <BinaryEdge>();
            var node = model.GetNode(nodeId);

            if (node.State != State.Unset)
            {
                return;
            }
            node.State = drugEffect == DrugEffect.Activation ? State.Active : State.Inactive;
            var targetDrug = drugsList.FirstOrDefault(d => d.TargetId.Equals(nodeId) && d.Effect == drugEffect);

            if (targetDrug != null)
            {
                var drugNode = new BinaryNode(model, targetDrug.Id);
                drugNode.State = State.Active;
                drugNodes.Add(drugNode);
                var drugEdge = new BinaryEdge(model, string.Concat(targetDrug.Id, node.Id), targetDrug.Effect == DrugEffect.Activation ? EdgeType.Positive : EdgeType.Negative);
                drugEdge.Input  = drugNode;
                drugEdge.Output = node;
                drugsEdges.Add(drugEdge);
            }
            foreach (var edge in model.Edges.Where(e => e.Output == node))
            {
                var modelClone  = model.Clone();
                var inputEffect = DrugEffect.Activation;
                switch (edge.Type)
                {
                case EdgeType.Positive:
                    inputEffect = drugEffect;
                    break;

                case EdgeType.Negative:
                    inputEffect = drugEffect == DrugEffect.Activation
                            ? DrugEffect.Inhibition
                            : DrugEffect.Activation;
                    break;
                }
                FindDrugs(modelClone, edge.Input.Id, inputEffect, drugsList, out var newDrugNodes, out var newDrugEdges);
                drugNodes.AddRange(newDrugNodes);
                drugsEdges.AddRange(newDrugEdges);
            }
        }
Beispiel #11
0
 /// <summary>
 /// Adds new entities to the target model
 /// </summary>
 /// <param name="model">The target model</param>
 /// <param name="newNodes">The nodes to add</param>
 /// <param name="newEdges">The edges to add</param>
 /// <param name="protectNodes">If set to<c>true</c>, new nodes and nodes related to the added edges will be protected</param>
 public void AddEntitiesToModel(BinaryModel model, List <BinaryNode> newNodes, List <BinaryEdge> newEdges, bool protectNodes)
 {
     // Add them to the model without duplicates
     foreach (var drugNode in newNodes)
     {
         drugNode.Model     = model;
         drugNode.Protected = protectNodes;
     }
     model.Nodes.AddRange(newNodes);
     foreach (var fullDrugEdge in newEdges)
     {
         var newEdge = new BinaryEdge(model, fullDrugEdge.Id, fullDrugEdge.Type)
         {
             Input  = model.Nodes.First(n => n.Id.Equals(fullDrugEdge.Input.Id)),
             Output = model.Nodes.First(n => n.Id.Equals(fullDrugEdge.Output.Id))
         };
         newEdge.Input.Protected  = protectNodes;
         newEdge.Output.Protected = protectNodes;
         model.Edges.Add(newEdge);
     }
 }
Beispiel #12
0
        /// <summary>
        /// Simplifies the input model and extends it with drugs susceptible of having a posiive efect on the target phenotypes.
        /// </summary>
        /// <param name="model">The binary model to extend</param>
        /// <param name="protectedNodes">The nodes that should be protected</param>
        /// <param name="targetPhenotypes">The target phenotypes</param>
        public void SimplifyAndExtendModelWithDrugs(BinaryModel model, List <string> protectedNodes, List <TargetPhenotype> targetPhenotypes)
        {
            // Clone the model
            var modelClone = model.Clone();

            // Set inputs and outputs as protected
            _networkSimplifier.ProtectInputsAndOutputs(modelClone);

            // Get available drugs list
            var drugsList = _drugDatabase.GetAvailableDrugs();

            // Set protected nodes based on configuration
            _networkSimplifier.ProtectNodes(modelClone, protectedNodes, drugsList, new List <BinaryNode>());

            // Remove all intermediary nodes
            int removedNodes;

            do
            {
                removedNodes = _networkSimplifier.CleanupNodes(modelClone);
            } while (removedNodes != 0);

            // Add drugs
            AddDrugsToModel(modelClone, targetPhenotypes, drugsList, out var drugNodes, out var drugEdges);

            // Add the nodes to the original model
            AddEntitiesToModel(model, drugNodes, drugEdges, true);

            // Protect other nodes if nescessary
            // More than nodes impacted by drugs and mandatory nodes, protect target phenotypes nodes
            _networkSimplifier.ProtectNodes(model, protectedNodes, new List <DrugDescription>(), GetTargetPhenotypes(model, targetPhenotypes).Keys.ToList());

            // Remove all unnescesary nodes
            do
            {
                removedNodes = _networkSimplifier.CleanupNodes(model);
            } while (removedNodes != 0);
        }
Beispiel #13
0
 /// <summary>
 /// Checks if a model already contains an edge with the same input and output but a different type
 /// </summary>
 /// <param name="model">The target model</param>
 /// <param name="input">The input node</param>
 /// <param name="output">The output node</param>
 /// <param name="type">The edge type</param>
 /// <returns><c>true</c> if the model already contains the oposite edge, <c>false</c> otherwise</returns>
 private bool HasOpositeEdge(BinaryModel model, BinaryNode input, BinaryNode output, EdgeType type)
 {
     return(model.Edges.Any(e => e.Input == input && e.Output == output && e.Type != type));
 }
Beispiel #14
0
        /// <summary>
        /// Removes non protected nodes with only one input and one output and merges the appropriate edges.
        /// Loop nodes and nodes with no input and/or output are also removed.
        /// </summary>
        /// <param name="model">The binary model</param>
        /// <returns>The number of nodes removed</returns>
        public int CleanupNodes(BinaryModel model)
        {
            int entitiesRemoved = 0;
            var nodes           = model.Nodes.ToList();

            foreach (var node in nodes)
            {
                if (node.Protected)
                {
                    continue;
                }
                var canRemoveNode = true;

                var edges   = model.Edges.ToList();
                var inputs  = edges.Where(e => e.Output == node).ToList();
                var outputs = edges.Where(e => e.Input == node).ToList();

                // If a node has no imput nor output or is a loop, remove it
                if (inputs.Count == 0 || outputs.Count == 0)
                {
                    model.Nodes.Remove(node);
                    model.Edges.RemoveAll(e => inputs.Contains(e) || outputs.Contains(e));
                    entitiesRemoved++;
                }
                // Remove self regulations
                model.Edges.RemoveAll(e => e.Input == e.Output);

                var outputsToKeep = new List <BinaryEdge>();
                var inputsToKeep  = new List <BinaryEdge>();
                var newEdgesToAdd = new List <BinaryEdge>();
                foreach (var inputEdge in inputs)
                {
                    foreach (var outputEdge in outputs)
                    {
                        var newEdge = new BinaryEdge(model, string.Concat(inputEdge.Id, outputEdge.Id), GetMergedEdgeType(inputEdge, outputEdge))
                        {
                            Input  = inputEdge.Input,
                            Output = outputEdge.Output
                        };

                        if (HasOpositeEdge(model, newEdge.Input, newEdge.Output, newEdge.Type))
                        {
                            canRemoveNode = false;
                            inputsToKeep.Add(inputEdge);
                            outputsToKeep.Add(outputEdge);
                            continue;
                        }
                        if (newEdge.Input != newEdge.Output && !HasSameEdge(model, newEdge.Input, newEdge.Output, newEdge.Type))
                        {
                            newEdgesToAdd.Add(newEdge);
                        }
                    }
                }

                // Compute edges to remove
                var edgesToRemove = (model.Edges.Where(e => e.Input == node && !outputsToKeep.Contains(e) || e.Output == node && !inputsToKeep.Contains(e))).ToList();

                // Compare edges to add count with edges to remove
                var delta = newEdgesToAdd.Count - edgesToRemove.Count;
                if (delta <= 0)
                {
                    entitiesRemoved += model.Edges.RemoveAll(e => edgesToRemove.Contains(e));
                    model.Edges.AddRange(newEdgesToAdd);
                    if (!canRemoveNode)
                    {
                        continue;
                    }
                    model.Nodes.Remove(node);
                    entitiesRemoved++;
                }
            }
            return(entitiesRemoved);
        }
Beispiel #15
0
 /// <summary>
 ///     Creates a new binary model based onan SBML Qual model.
 /// </summary>
 public void InitFromSbmlQualModel(BinaryModel model, Model sbmlModel)
 {
     PopulateNodes(model, sbmlModel);
     PopulateEdges(model, sbmlModel);
 }
Beispiel #16
0
 /// <summary>
 /// Returns the desired phenotype nodes from a target model
 /// </summary>
 /// <param name="model">The target model</param>
 /// <param name="targetPhenotypes">The desired phenotypes</param>
 /// <returns></returns>
 private Dictionary <BinaryNode, DrugEffect> GetTargetPhenotypes(BinaryModel model, List <TargetPhenotype> targetPhenotypes)
 {
     // Store phenotypes and target action
     return(targetPhenotypes.ToDictionary(targetPhenotype => model.Nodes.First(n => n.Id.Equals(targetPhenotype.PhenotypeId)), targetPhenotype => targetPhenotype.Effect));
 }