Ejemplo n.º 1
0
def estimate_confidence_bound_with_cvx(f_mat, num_reads_in_bams, fixed_i,
                                       mle_estimate, bound_type, alpha):
    if bound_type == 'lb': bound_type = 'LOWER'
    if bound_type == 'ub': bound_type = 'UPPER'
    assert bound_type in ('LOWER',
                          'UPPER'), ("Improper bound type '%s'" % bound_type)
    expected_array, observed_array = f_mat.expected_and_observed(
        bam_cnts=num_reads_in_bams)

    from cvxpy import matrix, variable, geq, log, eq, program, maximize, minimize, sum

    mle_log_lhd = calc_lhd(mle_estimate, observed_array, expected_array)

    lower_lhd_bound = mle_log_lhd - chi2.ppf(1 - alpha, 1) / 2.
    free_indices = set(range(expected_array.shape[1])) - set((fixed_i, ))

    Xs = matrix(observed_array)
    ps = matrix(expected_array)
    thetas = variable(ps.shape[1])
    constraints = [
        geq(Xs * log(ps * thetas), lower_lhd_bound),
        eq(sum(thetas), 1),
        geq(thetas, 0)
    ]

    if bound_type == 'UPPER':
        p = program(maximize(thetas[fixed_i, 0]), constraints)
    else:
        p = program(minimize(thetas[fixed_i, 0]), constraints)
    p.options['maxiters'] = 1500
    value = p.solve(quiet=not DEBUG_OPTIMIZATION)
    thetas_values = numpy.array(thetas.value.T.tolist()[0])
    log_lhd = calc_lhd(thetas_values, observed_array, expected_array)

    return chi2.sf(2 * (mle_log_lhd - log_lhd), 1), value
Ejemplo n.º 2
0
def mincost_maxflow( flownx, cost='cost', DELTA=None ) :
    if DELTA is None : DELTA = DEFAULT_DELTA
    
    total_flow = compute_totalflow( flownx )
    total_cost = compute_totalcost( flownx, cost=cost )
    constraints = collect_constraints( flownx )
    
    prog1 = cvxpy.program( cvxpy.maximize( total_flow ), constraints )
    max_flow = prog1.solve()
    
    constraints2 = [ c for c in constraints ]
    constraints2.append( cvxpy.geq( total_flow, max_flow - DELTA ) )
    prog2 = cvxpy.program( cvxpy.minimize( total_cost ), constraints2 )
    res = prog2.solve()
    return res
Ejemplo n.º 3
0
def estimate_transcript_frequencies_with_cvxopt(observed_array,
                                                expected_array,
                                                sparse_penalty,
                                                sparse_index,
                                                verbose=False):
    from cvxpy import matrix, variable, geq, log, eq, program, maximize, \
        minimize, sum, quad_over_lin

    Xs = matrix(observed_array)
    ps = matrix(expected_array)
    thetas = variable(ps.shape[1])
    constraints = [eq(sum(thetas), 1), geq(thetas, 0)]

    if sparse_penalty == None:
        p = program(maximize(Xs * log(ps * thetas)), constraints)
    else:
        p = program(
            maximize(Xs * log(ps * thetas) - sparse_penalty *
                     quad_over_lin(1., thetas[sparse_index, 0])), constraints)

    p.options['maxiters'] = 1500
    value = p.solve(quiet=not verbose)
    thetas_values = numpy.array(thetas.value.T.tolist()[0])
    return thetas_values
Ejemplo n.º 4
0
 def _FindMtdf(self, c_range=(1e-6, 1e-2), bounds=None,
               normalization=DeltaGNormalization.DEFAULT):
     """Find the MTDF (Maximal Thermodynamic Driving Force).
     
     Args:
         c_range: a tuple (min, max) for concentrations (in M).
         bounds: a list of (lower bound, upper bound) tuples for compound
             concentrations.
     
     Returns:
         A 3 tuple (optimal dGfs, optimal concentrations, optimal mtdf).
     """
     ln_conc, motive_force_lb, constraints = self._MakeMtdfProblem(
                                             c_range, bounds)
     program = cvxpy.program(cvxpy.maximize(motive_force_lb), constraints)
     program.solve(quiet=True)
     return ln_conc.value, program.objective.value
Ejemplo n.º 5
0
 def _FindMtdf(self,
               c_range=(1e-6, 1e-2),
               bounds=None,
               normalization=DeltaGNormalization.DEFAULT):
     """Find the MTDF (Maximal Thermodynamic Driving Force).
     
     Args:
         c_range: a tuple (min, max) for concentrations (in M).
         bounds: a list of (lower bound, upper bound) tuples for compound
             concentrations.
     
     Returns:
         A 3 tuple (optimal dGfs, optimal concentrations, optimal mtdf).
     """
     ln_conc, motive_force_lb, constraints = self._MakeMtdfProblem(
         c_range, bounds)
     program = cvxpy.program(cvxpy.maximize(motive_force_lb), constraints)
     program.solve(quiet=True)
     return ln_conc.value, program.objective.value
            def f_Gamma(c):
                # For fixed c solve the semidefinite program for Algorithm 4
                
                # Variable Gamma_plus (for \Gamma_{i+1})
                d_plus = variable(2*n, 1, name='d_plus')
                
                # Constraints
                constr = []
                for j in range(2*n):
                    ctt =  less_equals(d_plus[j,0], 0)
                    constr.append( less_equals(-d_plus[j,0], 0))
                    constr.append( less_equals( d_plus[j,0], d[j,0]))
                    constr.append( less_equals( J[j,j]*d_plus[j,0] + sum([abs(J[i,j])*d_plus[i,0] for i in range(2*n)]) - abs(J[j,j])*d_plus[j,0], c* d_plus[j,0]))
                
                # Objective function
                obj = geo_mean(d_plus)
                # Find solution
                p = program(maximize(obj), constr)

                return d_plus.value
Ejemplo n.º 7
0
 def GetTotalReactionEnergy(self, min_driving_force=0, maximize=True):
     """
         Maximizes the total pathway dG' (i.e. minimize energetic cost).
         Arguments:
             min_driving_force - the lower limit on each reaction's driving force
                                 (it is common to provide the optimize driving force
                                 in order to find the concentrations that minimize the
                                 cost, without affecting the MTDF).
             maximize          - if True then finds the maximal total dG.
                                 if False then finds the minimal total dG.
     """
     ln_conc, constraints, total_g = self._GetTotalReactionEnergy(
                             self.c_range, self.bounds, min_driving_force)
     
     if maximize:
         objective = cvxpy.maximize(total_g)
     else:
         objective = cvxpy.minimize(total_g)
     
     program = cvxpy.program(objective, constraints)
     program.solve(quiet=True)
     return ln_conc.value, program.objective.value
Ejemplo n.º 8
0
    def GetTotalReactionEnergy(self, min_driving_force=0, maximize=True):
        """
            Maximizes the total pathway dG' (i.e. minimize energetic cost).
            Arguments:
                min_driving_force - the lower limit on each reaction's driving force
                                    (it is common to provide the optimize driving force
                                    in order to find the concentrations that minimize the
                                    cost, without affecting the MTDF).
                maximize          - if True then finds the maximal total dG.
                                    if False then finds the minimal total dG.
        """
        ln_conc, constraints, total_g = self._GetTotalReactionEnergy(
            self.c_range, self.bounds, min_driving_force)

        if maximize:
            objective = cvxpy.maximize(total_g)
        else:
            objective = cvxpy.minimize(total_g)

        program = cvxpy.program(objective, constraints)
        program.solve(quiet=True)
        return ln_conc.value, program.objective.value
Ejemplo n.º 9
0
    def FindMTDF(self, concentration_bounds=None, normalization=None):
        """Finds the MTDF.
        
        Args:
            bounds: the Bounds objects setting concentration bounds.
        """
        my_bounds = concentration_bounds or self.DefaultConcentrationBounds()
        normalization = normalization or self.DeltaGNormalization.DEFAULT

        # Constrain concentrations
        ln_conc = cvxpy.variable(m=1, n=self.Ncompounds, name='lnC')
        ln_conc_lb, ln_conc_ub = my_bounds.GetLnBounds(self.compounds)
        constr = [
            cvxpy.geq(ln_conc, cvxpy.matrix(ln_conc_lb)),
            cvxpy.leq(ln_conc, cvxpy.matrix(ln_conc_ub))
        ]

        # Make the objective
        motive_force_lb = cvxpy.variable(name='B')
        my_dG0_r_primes = np.matrix(self.dG0_r_prime)

        # Make flux-based constraints on reaction free energies.
        # All reactions must have negative dGr in the direction of the flux.
        # Reactions with a flux of 0 must be in equilibrium.
        S = np.matrix(self.S)

        for i, flux in enumerate(self.fluxes):

            curr_dg0 = my_dG0_r_primes[0, i]
            # if the dG0 is unknown, this reaction imposes no new constraints
            if np.isnan(curr_dg0):
                continue

            rcol = cvxpy.matrix(S[:, i])
            curr_dgr = curr_dg0 + RT * ln_conc * rcol
            if flux == 0:
                constr.append(cvxpy.eq(curr_dgr, 0))
            else:
                motive_force = self.DeltaGNormalization.NormalizeDGByFlux(
                    curr_dgr, flux, normalization)

                constr.append(cvxpy.geq(motive_force, motive_force_lb))

        objective = cvxpy.maximize(motive_force_lb)
        problem = cvxpy.program(objective, constr, name='MTDF_OPT')

        problem.solve(quiet=True)
        """
        if status != 'optimal':
            status = optimized_pathway.OptimizationStatus.Infeasible(
                'Pathway infeasible given bounds.')
            return MTDFOptimizedPathway(
                self._model, self._thermo,
                my_bounds, optimization_status=status)
        """

        mtdf = float(motive_force_lb.value)
        opt_ln_conc = np.matrix(np.array(ln_conc.value))
        result = MTDFOptimizedPathway(
            self._model,
            self._thermo,
            my_bounds,
            optimal_value=mtdf,
            optimal_ln_metabolite_concentrations=opt_ln_conc)
        result.SetNormalization(normalization)
        return result
Ejemplo n.º 10
0
def maxflow( flownx ) :
    total_flow = compute_totalflow( flownx )
    constraints = collect_constraints( flownx )
    prog = cvxpy.program( cvxpy.maximize( total_flow ), constraints )
    max_flow = prog.solve()
    return max_flow
Ejemplo n.º 11
0
    def FindMTDF(self, concentration_bounds=None, normalization=None):
        """Finds the MTDF.
        
        Args:
            bounds: the Bounds objects setting concentration bounds.
        """        
        my_bounds = concentration_bounds or self.DefaultConcentrationBounds()
        normalization = normalization or self.DeltaGNormalization.DEFAULT
                
        # Constrain concentrations
        ln_conc = cvxpy.variable(m=1, n=self.Ncompounds, name='lnC')
        ln_conc_lb, ln_conc_ub = my_bounds.GetLnBounds(self.compounds)
        constr = [cvxpy.geq(ln_conc, cvxpy.matrix(ln_conc_lb)),
                  cvxpy.leq(ln_conc, cvxpy.matrix(ln_conc_ub))]
        
        # Make the objective
        motive_force_lb = cvxpy.variable(name='B')
        my_dG0_r_primes = np.matrix(self.dG0_r_prime)

        
        # Make flux-based constraints on reaction free energies.
        # All reactions must have negative dGr in the direction of the flux.
        # Reactions with a flux of 0 must be in equilibrium.
        S = np.matrix(self.S)
        
        for i, flux in enumerate(self.fluxes):
            
            curr_dg0 = my_dG0_r_primes[0, i]
            # if the dG0 is unknown, this reaction imposes no new constraints
            if np.isnan(curr_dg0):
                continue
            
            rcol = cvxpy.matrix(S[:, i])
            curr_dgr = curr_dg0 + RT * ln_conc * rcol
            if flux == 0:
                constr.append(cvxpy.eq(curr_dgr, 0))
            else:
                motive_force = self.DeltaGNormalization.NormalizeDGByFlux(
                    curr_dgr, flux, normalization)
                
                constr.append(cvxpy.geq(motive_force, motive_force_lb))
        
        objective = cvxpy.maximize(motive_force_lb)
        problem = cvxpy.program(objective, constr, name='MTDF_OPT')
        
        problem.solve(quiet=True)
        """
        if status != 'optimal':
            status = optimized_pathway.OptimizationStatus.Infeasible(
                'Pathway infeasible given bounds.')
            return MTDFOptimizedPathway(
                self._model, self._thermo,
                my_bounds, optimization_status=status)
        """
        
        mtdf = float(motive_force_lb.value)
        opt_ln_conc = np.matrix(np.array(ln_conc.value))
        result = MTDFOptimizedPathway(
            self._model, self._thermo,
            my_bounds, optimal_value=mtdf,
            optimal_ln_metabolite_concentrations=opt_ln_conc)
        result.SetNormalization(normalization)
        return result