From 5b2ab491e801cb866a305630c78b5f264d682a99 Mon Sep 17 00:00:00 2001
From: Claus Jonathan Fritzemeier <clausjonathan.fritzemeier@hhu.de>
Date: Mon, 19 Oct 2015 16:31:19 +0200
Subject: [PATCH] easyconstraint documentation and implementation

---
 DESCRIPTION                               |  10 +-
 R/sysBiolAlg_fbaEasyConstraintClass.R     |  55 ++++++--
 man/sysBiolAlg_fbaEasyConstraint-class.Rd | 152 ++++++++++++++++++++++
 3 files changed, 200 insertions(+), 17 deletions(-)
 create mode 100644 man/sysBiolAlg_fbaEasyConstraint-class.Rd

diff --git a/DESCRIPTION b/DESCRIPTION
index 18a0246..833d777 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,8 +1,8 @@
 Package: sybil
 Type: Package
 Title: Efficient Constrained Based Modelling in R
-Version: 1.3.1
-Date: 2015-06-17
+Version: 1.3.2
+Date: 2015-10-19
 Authors@R: c(
 	     person(c("C.", "Jonathan"), "Fritzemeier", role = c("cre", "ctb"), email = "clausjonathan.fritzemeier@uni-duesseldorf.de"),
 	     person("Gabriel", "Gelius-Dietrich", role = c("aut")),
@@ -48,9 +48,11 @@ Collate: generics.R validmodelorg.R validoptsol.R validreactId.R
         optsol_optimizeProbClass.R optsol_fluxVarClass.R
         optsol_fluxdelClass.R optsol_robAnaClass.R optsol_phppClass.R
         optsol_genedelClass.R checksolClass.R summaryOptsolClass.R
-        sysBiolAlgClass.R sysBiolAlg_fbaClass.R sysBiolAlg_fvClass.R
+        sysBiolAlgClass.R sysBiolAlg_fbaClass.R
+        sysBiolAlg_fbaEasyConstraintClass.R sysBiolAlg_fvClass.R
         sysBiolAlg_lmomaClass.R sysBiolAlg_momaClass.R
-        sysBiolAlg_mtfClass.R sysBiolAlg_roomClass.R sybilLogClass.R
+        sysBiolAlg_mtfClass.R sysBiolAlg_mtfEasyConstraintClass.R
+        sysBiolAlg_roomClass.R sybilLogClass.R
 Packaged: 2014-11-19 20:39:37 UTC; gabriel
 Author: C. Jonathan Fritzemeier [cre, ctb],
   Gabriel Gelius-Dietrich [aut],
diff --git a/R/sysBiolAlg_fbaEasyConstraintClass.R b/R/sysBiolAlg_fbaEasyConstraintClass.R
index bdd97cb..676240b 100644
--- a/R/sysBiolAlg_fbaEasyConstraintClass.R
+++ b/R/sysBiolAlg_fbaEasyConstraintClass.R
@@ -100,32 +100,61 @@ setMethod(f = "initialize",
                   
                   mat <- S(model)
                   rtype <- rep("E", nRows)
+                  rlb <- rep(0, nRows)
+                  rub <- rep(0, nRows)
                   
                   #add easyConstraints:
                   if(!is.null(easyConstraint)){
-                  	if(		length(easyConstraint$nz) == length(easyConstraint$x)
-                  		| 	length(easyConstraint$nz) == length(easyConstraint$rtype)){
+                  	if(		length(easyConstraint$react) != length(easyConstraint$x)
+                  		| 	length(easyConstraint$react) != length(easyConstraint$rtype)
+                  		){
                   		stop("easyConstraint elements have to have equal lengths")
                   	}
-                  	stopifnot(is.list(easyConstraints$nz))
-                  	stopifnot(is.list(easyConstraints$x))
-                  	stopifnot(all(easyConstraints$rtype %in% c("F", "L", "U", "D", "E")))
+                  	stopifnot(is.list(easyConstraint$react))
+                  	stopifnot(is.list(easyConstraint$x))
+                  	stopifnot(all(easyConstraint$rtype %in% c("F", "L", "U", "D", "E")))
                   	
-                  	m <- Matrix(0, ncol=nCols, nrow=length(easyConstraint))
+                  	# setting and checking rlb
+                  	if(is.null(easyConstraint$lb)){
+                  		rlb <- c(rlb, rep(0, length(easyConstraint$react)))
+                  	}else{
+                  		if(length(easyConstraint$react) != length(easyConstraint$lb)){
+                  			stop("easyConstraint$lb length has to match length of react argument")
+                  		}else{
+                  			stopifnot(is.numeric(easyConstraint$lb))
+                  			rlb <- c(rlb, easyConstraint$lb)
+                  		}
+                  	}
+                  	
+                  	# setting and checking rub
+                  	if(is.null(easyConstraint$ub)){
+                  		rub <- c(rub, rep(0, length(easyConstraint$react)))
+                  	}else{
+                  		if(length(easyConstraint$react) != length(easyConstraint$ub)){
+                  			stop("easyConstraint$ub length has to match length of react argument")
+                  		}else{
+                  			stopifnot(is.numeric(easyConstraint$ub))
+                  			rub <- c(rub, easyConstraint$ub)
+                  		}
+                  	}
+                  	browser()
+                  	
+                  	m <- Matrix(0, ncol=nCols, nrow=length(easyConstraint$react))
                   	
-                  	for(i in 1:length(easyConstraint)){
-                  		m[i, easyConstraint$nz] <- easyConstraint$x
+                  	for(i in 1:length(easyConstraint$react)){
+                  		m[i, easyConstraint$react[[i]]] <- easyConstraint$x[[i]]
                   	}
                   	
+                  	
                   	mat <- rbind2(mat, m)
                   	rtype <- c(rtype, easyConstraint$rtype)
-                  	nRow <- nRow + length(easyConstraint)
+                  	nRows <- nRows + length(easyConstraint$react)
                   	if(!is.null(rowNames)){
-                  		c(rowNames, paste0("easyConstraint", 1:length(easyConstraint)))
+                  		c(rowNames, paste0("easyConstraint", 1:length(easyConstraint$react)))
                   	}
                   	
                   }
-
+                  
                   # generate problem object
                   .Object <- callNextMethod(.Object,
                                             sbalg      = "fba",
@@ -138,10 +167,10 @@ setMethod(f = "initialize",
                                             ub         = uppbnd(model),
                                             lb         = lowbnd(model),
                                             obj        = obj_coef(model),
-                                            rlb        = rep(0, nRows),
+                                            rlb        = rlb,
                                             rtype      = rtype,
                                             lpdir      = lpdir,
-                                            rub        = NULL,
+                                            rub        = rub,
                                             ctype      = NULL,
                                             cnames     = colNames,
                                             rnames     = rowNames,
diff --git a/man/sysBiolAlg_fbaEasyConstraint-class.Rd b/man/sysBiolAlg_fbaEasyConstraint-class.Rd
new file mode 100644
index 0000000..00f9381
--- /dev/null
+++ b/man/sysBiolAlg_fbaEasyConstraint-class.Rd
@@ -0,0 +1,152 @@
+\name{sysBiolAlg_fba-class}
+
+\Rdversion{1.1}
+\encoding{utf8}
+
+\docType{class}
+
+\alias{sysBiolAlg_fbaEasyConstraint-class}
+\alias{sysBiolAlg_fbaEasyConstraint}
+\alias{sysBiolAlg_mtfEasyConstraint-class}
+\alias{sysBiolAlg_mtfEasyConstraint}
+\alias{mtfEasyConstraint}
+\alias{fbaEasyConstraint}
+
+\title{Class \code{"sysBiolAlg_fbaEasyConstraint"} and \code{"sysBiolAlg_mtfEasyConstraint"}}
+
+\description{
+  The classes \code{sysBiolAlg_fbaEasyConstraint} \code{sysBiolAlg_mtfEasyConstraint} hold an object of class
+  \code{\linkS4class{optObj}} which is generated to meet the
+  requirements of the FBA/MTF algorithm. \cr
+  In Addition to this, it is very easy to add
+  additional linear constraints to that linear problem. Each constraints is 
+  defined by the affected reaction, the coefficient, lower and upper bounds, and
+  the constraint type.
+}
+
+\section{Objects from the Class}{
+  Objects can be created by calls of the form
+  
+  \code{sysBiolAlg(model, algorithm = "fba", ...)}.
+  
+  Arguments to \code{...} which are passed to method \code{initialize} of class
+  \code{sysBiolAlg_fba} are described in the Details section.
+}
+
+\section{Slots}{
+  \describe{
+    Slots are the same as in the original MTF/FBA classes.
+    
+    In addition, this slot is implemented:
+    
+    \item{\code{easyConstraint}:}{
+    	List holding the information for the constraints (see details):
+		\itemize{
+			\item{\code{react}}{
+				List of numeric vectors. Values indicate, to which reaction the 
+				constraint applys.
+			}
+			\item{\code{x}}{
+				List of numeric vectors. Values indicate coefficients of the 
+				constraint. Lengths have to be equal to \code{react}-field.
+			}
+			\item{\code{lb}}{
+				Numeric vector of lower bounds for constraints. If not given, a 
+				default bound of 0 will be used.
+			}
+			\item{\code{ub}}{
+				Numeric vector of lower bounds for constraints. If not given, a 
+				default bound of 0 will be used. Only needed for constraints, 
+				that need two bounds.
+			}
+			\item{\code{rtype}}{
+				Character vector defining the type of constraint.
+				
+				\tabular{lll}{
+					  \code{"F"}: \tab free constraint (GLPK only)          \tab \eqn{-\infty < x < \infty}{-INF < x < INF}               \cr
+					  \code{"L"}: \tab constraint with lower bound          \tab \eqn{\mathrm{lb} \leq x < \infty}{lb <= x < INF}         \cr
+					  \code{"U"}: \tab constraint with upper bound          \tab \eqn{-\infty < x \leq \mathrm{ub}}{-INF < x <= ub}       \cr
+					  \code{"D"}: \tab double-bounded (ranged) constraint   \tab \eqn{\mathrm{lb} \leq x \leq \mathrm{ub}}{lb <= x <= ub} \cr
+					  \code{"E"}: \tab fixed (equality) constraint          \tab \eqn{\mathrm{lb} = x = \mathrm{ub}}{lb = x = ub}         \cr
+				}
+				If \code{rtype[i]} is not one of \code{"F"}, \code{"L"}, \code{"U"},
+				\code{"D"} or \code{"E"}, the value of \code{rtype[i]} will be set to
+				\code{"E"}. See Details of \code{\link{loadLPprob}}.
+			}
+		}
+    }
+  }
+}
+
+\section{Extends}{
+  Class \code{"\linkS4class{sysBiolAlg}"}, directly.
+}
+
+\section{Methods}{
+  No methods defined with class "sysBiolAlg_fbaEasyConstraint" in the signature.
+}
+
+\details{
+  The problem object is built to be capable to perform flux balance analysis
+  (FBA) with a given model, which is basically the solution of a linear
+  programming problem
+  \deqn{%
+      \begin{array}{rll}%
+          \max            & \mbox{\boldmath$c$\unboldmath}^{\mathrm{T}}
+                                         \mbox{\boldmath$v$\unboldmath} \\[1ex]
+          \mathrm{s.\,t.} & \mbox{\boldmath$Sv$\unboldmath} = 0         \\[1ex]
+                          & \alpha_i \leq v_i \leq \beta_i
+                            & \quad \forall i \in \{1, \ldots, n\}      \\[1ex]
+      \end{array}%
+  }{
+      max  c^T v
+      s.t. Sv = 0
+           a_i <= v_i <= b_i  for i = 1, ..., n
+  }
+  with \eqn{\bold{S}}{S} being the stoichiometric matrix, \eqn{\alpha_i}{a_i}
+  and \eqn{\beta_i}{b_i} being the lower and upper bounds for flux (variable)
+  \eqn{i} respectively.  The total number of variables of the optimization
+  problem is denoted by \eqn{n}.  The solution of the optimization is a flux
+  distribution maximizing the objective function
+  \eqn{
+      \mbox{\boldmath$c$\unboldmath}^{\mathrm{T}}
+      \mbox{\boldmath$v$\unboldmath}
+  }{c^T v} under the a given environment and the assumption of steady state.
+  The optimization can be executed by using \code{\link{optimizeProb}}.
+  
+  The additional i-th EasyConstraint will be added as follows to the problem: \emph{to be checked.}
+  \deqn{
+  	\begin{array}{l}%
+  		\code{lb} <= v_\code{react\[\[i\]\]} * (x_i)^T <= \code{ub}
+  	\end{array}%
+  }{
+  	\code{lb} <= v_{\code{react\[\[i\]\]}} * (x_i)^T <= \code{ub}
+  }
+  
+}
+
+\references{
+  Edwards, J. S., Covert, M and Palsson, B. Ø. (2002) Metabolic modelling of
+  microbes: the flux-balance approach. \emph{Environ Microbiol} \bold{4},
+  133--140.
+  
+  Edwards, J. S., Ibarra, R. U. and Palsson, B. Ø. (2001) In silico predictions
+  of \emph{Escherichia coli} metabolic capabilities are consistent with
+  experimental data. \emph{Nat Biotechnol} \bold{19}, 125--130.
+}
+
+\author{
+ 	Claus Jonathan Fritzemeier <clausjonathan.fritzemeier@uni-duesseldorf.de>
+}
+
+
+\seealso{
+  Constructor function \code{\link{sysBiolAlg}} and
+  superclass \code{\linkS4class{sysBiolAlg}}.
+}
+
+\examples{
+  showClass("sysBiolAlg_fba")
+}
+
+\keyword{classes}
-- 
GitLab