Skip to content

Commit

Permalink
Merge pull request #36 from iManGHD/master
Browse files Browse the repository at this point in the history
Parallelization of ReversibilityCorrection
  • Loading branch information
mtefagh authored Apr 20, 2023
2 parents 5f62db4 + 91d8929 commit db341ff
Show file tree
Hide file tree
Showing 8 changed files with 895 additions and 382 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "sparseQFCA"
uuid = "077cf62c-ad2d-5edd-99ec-638372f8b004"
authors = ["Mojtaba Tefagh <[email protected]>", "Iman Ghadimi <[email protected]>"]
version = "1.4.0"
version = "1.5.0"

[deps]
COBREXA = "babc4406-5200-4a30-9033-bf5ae714c842"
Expand Down
431 changes: 431 additions & 0 deletions example/disteibutedQFCA.ipynb

Large diffs are not rendered by default.

72 changes: 44 additions & 28 deletions src/Consistency Checking/SwiftCC.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#-------------------------------------------------------------------------------------------
#=
Purpose: Finding blocked reactions in metabolic network
Author: Iman Ghadimi, Mojtaba Tefagh - Sharif University of Technology - Iran
Purpose: Identifying blocked reactions in metabolic networks using linear programming(1LP) and Gaussian elimination
Author: Iman Ghadimi, Mojtaba Tefagh - Sharif University of Technology
Date: July 2022
=#
#-------------------------------------------------------------------------------------------

module SwiftCC

export MyModel, myModel_Constructor, swiftCC

using GLPK, JuMP, COBREXA, LinearAlgebra, SparseArrays, Distributed
Expand All @@ -15,7 +16,6 @@ include("../Data Processing/pre_processing.jl")

using .pre_processing


"""
MyModel(S, Metabolites, Reactions, Genes, m, n, lb, ub)
Expand Down Expand Up @@ -43,14 +43,17 @@ mutable struct MyModel
ub ::Array{Float64,1}
end

#-------------------------------------------------------------------------------------------

"""
myModel_Constructor(ModelObject, S, Metabolites, Reactions, Genes, m, n, lb, ub)
A function that initializes a newly created object of MyModel.
The function takes in several arguments, including a ModelObject of type MyModel,
and assigns values to its fields based on the other arguments passed in.
# INPUTS
-'ModelObject' a newly object of MyModel.
-'ModelObject': A newly object of MyModel.
- `S`: LHS matrix (m x n)
- `Metabolites`: List of metabolic network metabolites.
- `Reactions`: List of metabolic network reactions.
Expand All @@ -73,10 +76,15 @@ function myModel_Constructor(ModelObject::MyModel, S::Union{SparseMatrixCSC{Floa
ModelObject.lb = lb
ModelObject.ub = ub
end

#-------------------------------------------------------------------------------------------

"""
swiftCC(ModelObject)
A function that finds blocked reactions for a metabolic network.
The function first exports data from ModelObject and performs some calculations to determine the reversibility of reactions
and the number of blocked reactions. It then creates an optimization model using the GLPK optimizer to identify irreversible blocked reactions,
and uses Gaussian elimination to identify blocked reversible reactions.
# INPUTS
Expand All @@ -85,10 +93,11 @@ A function that finds blocked reactions for a metabolic network.
# OPTIONAL INPUTS
- `Tolerance`: A small number that represents the level of error tolerance.
- `printLevel`: Verbose level (default: 1). Mute all output with `printLevel = 0`.
# OUTPUTS
- `blocked_index`: Index of blocked reactions.
- `blocked_index`: IDs of of blocked reactions.
- `dualVar`: Dual variables of a specific constraint.
# EXAMPLES
Expand All @@ -102,9 +111,9 @@ See also: `MyModel`, myModel_Constructor(), `reversibility()`, `homogenization()
"""

function swiftCC(ModelObject::MyModel, Tolerance::Float64=1e-6)
function swiftCC(ModelObject::MyModel, Tolerance::Float64=1e-6, printLevel::Int=1)

## Export data from ModelObject
## Extract relevant information from the input model object

S = ModelObject.S
Metabolites = ModelObject.Metabolites
Expand All @@ -114,12 +123,9 @@ function swiftCC(ModelObject::MyModel, Tolerance::Float64=1e-6)
lb = ModelObject.lb
ub = ModelObject.ub

## Determine the reversibility of a reaction

irreversible_reactions_id, reversible_reactions_id = reversibility(lb)

## Determine the number of irreversible and reversible reactions
## Identify which reactions are irreversible and which are reversible

irreversible_reactions_id, reversible_reactions_id = reversibility(lb, 0)
n_irr = length(irreversible_reactions_id)
n_rev = length(reversible_reactions_id)

Expand Down Expand Up @@ -168,46 +174,45 @@ function swiftCC(ModelObject::MyModel, Tolerance::Float64=1e-6)
end
end

# Determine the number of irreversible blocked reactions

# Determine the number of irreversible blocked reactions:
irr_blocked_num = length(irr_blocked_reactions)

## Find reversible blocked reactions

# Creating S_transpose:
# Create S_transpose:
S_transpose = S'

# Determining the dimensions of the S_transpose matrix:
# Determine the dimensions of the S_transpose matrix:
row_num_trans, col_num_trans = size(S_transpose)

# Removing irreversibly blocked reactions from the Stoichiometric Matrix:
# Remove irreversibly blocked reactions from the Stoichiometric Matrix:
S_transpose_noIrrBlocked = S_transpose[setdiff(1:end, irr_blocked_reactions), :]

# Creating the I_reversible Matrix:
# Create the I_reversible Matrix:
I_reversible = zeros(n, n_rev)

# Populating the I_reversible Matrix with 1 in the rows corresponding to reversible reactions:
# Populate the I_reversible Matrix with 1 in the rows corresponding to reversible reactions:
rev_id = 1
for col in eachcol(I_reversible)
col[reversible_reactions_id[rev_id]] = 1.0
rev_id += 1
end

# Removing irreversibly blocked reactions from the I_reversible Matrix:
# Remove irreversibe blocked reactions from the I_reversible Matrix:
I_reversible = I_reversible[setdiff(1:end, irr_blocked_reactions), :]

# Determining the dimensions of the S_transpose_noIrrBlocked and I_reversible matrices:
# Determine the dimensions of the S_transpose_noIrrBlocked and I_reversible matrices:
S_trans_row, S_trans_col = size(S_transpose_noIrrBlocked)
I_row, I_col = size(I_reversible)

# Solving the system of equations using Gaussian elimination to identify blocked reversible reactions:
# Solve the system of equations using Gaussian elimination to identify blocked reversible reactions:
X = S_transpose_noIrrBlocked \ I_reversible
Sol = (S_transpose_noIrrBlocked*X) - I_reversible

# Determining the dimensions of the Sol matrix:
# Determine the dimensions of the Sol matrix:
row_sol, col_sol = size(Sol)

# Finding columns in Sol that correspond to blocked reversible reactions:
# Find columns in Sol that correspond to blocked reversible reactions:
c = 0
rev_blocked_reactions_col = []
for col in eachcol(Sol)
Expand All @@ -222,14 +227,25 @@ function swiftCC(ModelObject::MyModel, Tolerance::Float64=1e-6)
append!(rev_blocked_reactions, reversible_reactions_id[i])
end

# Union reversbile blocked reactions and irreversible blocked reactions
## Union reversbile blocked reactions and irreversible blocked reactions

blocked_index = []
blocked_index = union(rev_blocked_reactions, irr_blocked_reactions)

# Returning a list consist of the Ids of the blocked reactions
## Returning a list consist of the Ids of the blocked reactions

blocked_index = sort(blocked_index)

## Print out results if requested

if printLevel > 0
printstyled("Tolerance = $Tolerance\n"; color=:magenta)
printstyled("Consistency_Checking(SwiftCC):\n"; color=:cyan)
println("Number of irreversible blocked reactions : $(length(irr_blocked_reactions))")
println("Number of reversible blocked reactions : $(length(rev_blocked_reactions))")
println("Number of blocked reactions : $(length(blocked_index))")
end

return blocked_index, dualVar
end

Expand Down
46 changes: 32 additions & 14 deletions src/Consistency Checking/TheNaiveApproach.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#-------------------------------------------------------------------------------------------
#=
Purpose: Finding blocked reactions in metabolic network
Author: Iman Ghadimi, Mojtaba Tefagh - Sharif University of Technology - Iran
Purpose: Identifying Blocked Reactions in a Metabolic Model using Linear Optimization
Author: Iman Ghadimi, Mojtaba Tefagh - Sharif University of Technology
Date: April 2022
=#
#-------------------------------------------------------------------------------------------

module TheNaiveApproach

export find_blocked_reactions

using GLPK, JuMP, COBREXA
Expand All @@ -18,7 +19,14 @@ using .pre_processing
"""
find_blocked_reactions(myModel)
A function that finds blocked reactions in metabolic network.
The function identifies blocked reactions in a metabolic model. Blocked reactions are reactions that cannot carry any flux,
i.e., the flux through the reaction is zero, due to the stoichiometry of the model and the constraints on the reaction rates.
The function first homogenizes the bounds on the reaction rates, and then identifies blocked irreversible reactions by optimizing
the flux through each reaction while constraining the flux to be less than or equal to 1. If the optimal flux is approximately zero,
the reaction is considered blocked. Then, the function identifies blocked reversible reactions by optimizing the flux through each
reaction in both directions, forward and backward, while constraining the flux to be less than or equal to 1 in the forward direction
and greater than or equal to -1 in the backward direction. If the optimal flux in both directions is approximately zero,the reaction
is considered blocked. The function returns the IDs of the blocked reactions.
# INPUTS
Expand All @@ -27,10 +35,11 @@ A function that finds blocked reactions in metabolic network.
# OPTIONAL INPUTS
- `Tolerance`: A small number that represents the level of error tolerance.
- `printLevel`: Verbose level (default: 1). Mute all output with `printLevel = 0`.
# OUTPUTS
- `blocked_index`: Index of blocked reactions.
- `blocked_index`: IDs of of blocked reactions.
# EXAMPLES
Expand All @@ -43,7 +52,7 @@ See also: `dataOfModel()`, `reversibility()`
"""

function find_blocked_reactions(myModel::StandardModel, Tolerance::Float64=1e-6)
function find_blocked_reactions(myModel::StandardModel, Tolerance::Float64=1e-6, printLevel::Int=1)

## Export data from model

Expand All @@ -55,11 +64,10 @@ function find_blocked_reactions(myModel::StandardModel, Tolerance::Float64=1e-6)

## Homogenize the upper_bound and lower_bound of reactions

# Set the maximum value for M

M = 1000.0
# Set the maximum value for M:
M = getM()

# Loop through each value in the array "lb" and "ub"
## Loop through each value in the array "lb" and "ub"

for i in 1:n
# If the lower bound is greater than zero, set it to zero:
Expand All @@ -80,7 +88,7 @@ function find_blocked_reactions(myModel::StandardModel, Tolerance::Float64=1e-6)
end
end

## Find Irreversible blocked reactions:
## Find Irreversible blocked reactions

# Create empty arrays to hold the IDs of blocked and unblocked irreversible reactions:
irreversible_blocked_reactions_id = []
Expand All @@ -95,7 +103,7 @@ function find_blocked_reactions(myModel::StandardModel, Tolerance::Float64=1e-6)
# Add a constraint to enforce that S*V = 0:
@constraint(model_irr, S * V .== 0)

# Loop through each irreversible reaction in the irreversible reactions array
## Loop through each irreversible reaction in the irreversible reactions array

for j in irreversible_reactions_id
# Set the objective to maximize the flux of the current reaction:
Expand Down Expand Up @@ -140,7 +148,7 @@ function find_blocked_reactions(myModel::StandardModel, Tolerance::Float64=1e-6)
# Loop through each reversible reaction in the reversible reactions array:
for j in reversible_reactions_id

# Forward direction
## Forward direction

@objective(model_rev, Max, V[j]) # Set the objective to maximize the flux of the current reaction
@constraint(model_rev, c1, V[j] <= 1) # Add a constraint to enforce that the flux of the current reaction is less than or equal to 1
Expand All @@ -150,7 +158,7 @@ function find_blocked_reactions(myModel::StandardModel, Tolerance::Float64=1e-6)
delete(model_rev, c1) # Remove the current constraint from the model
unregister(model_rev, :c1) # Unregister the current constraint from the model

# Backward direction
## Backward direction

@objective(model_rev, Min, V[j]) # Set the objective to minimize the flux of the current reaction
@constraint(model_rev, c2, V[j] >= -1) # Add a constraint to enforce that the flux of the current reaction is greater than or equal to -1
Expand All @@ -169,11 +177,21 @@ function find_blocked_reactions(myModel::StandardModel, Tolerance::Float64=1e-6)
end
end

## Combine the IDs of blocked irreversible and reversible reactions into a single array and Sort the array of blocked reaction IDs:
## Combine the IDs of blocked irreversible and reversible reactions into a single array and Sort the array of blocked reaction IDs

blocked_index = union(reversible_blocked_reactions_id, irreversible_blocked_reactions_id)
blocked_index = sort(blocked_index)

## Print out results if requested

if printLevel > 0
printstyled("Tolerance = $Tolerance\n"; color=:magenta)
printstyled("Consistency_Checking(TheNaiveApproch):\n"; color=:cyan)
println("Number of irreversible blocked reactions : $(length(irreversible_blocked_reactions_id))")
println("Number of reversible blocked reactions : $(length(reversible_blocked_reactions_id))")
println("Number of blocked reactions : $(length(blocked_index))")
end

return blocked_index

end
Expand Down
Loading

0 comments on commit db341ff

Please sign in to comment.