Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions src/riptide.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import pandas
import warnings
from random import seed
from copy import deepcopy
from datetime import datetime

import cobra
Expand Down Expand Up @@ -64,15 +63,16 @@ def save_output(riptide_obj='NULL', path='NULL', file_type='JSON', silent=False)
if riptide_obj == 'NULL':
raise ValueError('ERROR: Did not provide a RIPTiDe object')

curr_wd = str(os.getcwd())
if path == 'NULL':
if silent == False:
print('WARNING: Did not provide an output directory. Using default riptide_files in working directory')
path = riptide_obj.model.id + '_' + riptide_obj.run_start
path = curr_wd + '/' + riptide_obj.model.id + '_' + riptide_obj.run_start
else:
path = path + '_' + riptide_obj.run_start
try:
# Recursively build dirs if possible
os.mkdirs(path)
os.mkdir(path)
if silent == False: print('Saving results to', path)
except:
if silent == False:
Expand Down Expand Up @@ -636,7 +636,6 @@ def _screen_tasks(model, tasks, silent):

return screened_tasks


# Create context-specific model based on transcript distribution
def contextualize(model, transcriptome = 'none', samples = 1000, silent = False, prune = True,
fraction = 0.8, minimum = False, conservative = False, objective = True, additive = False, direct = False,
Expand Down Expand Up @@ -810,7 +809,7 @@ def contextualize(model, transcriptome = 'none', samples = 1000, silent = False,
riptide_model = _prune_model(model, inactive_rxns, conservative)
riptide_object.pruned = _record_pruned_elements(model, riptide_model)
else:
riptide_model = deepcopy(model)
riptide_model = model.copy()

# Find optimal solution space based on transcription and final constraints
if silent == False:
Expand Down Expand Up @@ -1007,7 +1006,7 @@ def _weighted_expression(model, coefficients={}):
# Determine those reactions that carry flux in a pFBA objective set to a threshold of maximum
def _constrain_for_pruning(model, min_coefficients, objective, obj_fraction, tasks, task_fraction, minimum_flux, silent):

constrained_model = deepcopy(model)
constrained_model = model.copy()

# Add objective/task constraints
min_coefficient = min(list(min_coefficients.values()))
Expand All @@ -1034,7 +1033,7 @@ def _constrain_for_pruning(model, min_coefficients, objective, obj_fraction, tas
# Determine those reactions that carry flux in a pFBA objective set to a threshold of maximum
def _constrain_for_sampling(model, max_coefficients, sampling_depth, objective, obj_frac, tasks, task_frac, min_flux, silent):

constrained_model = deepcopy(model)
constrained_model = model.copy()

# Apply weigths to new expression, allow deviation
if isinstance(objective, str) == True:
Expand All @@ -1048,17 +1047,21 @@ def _constrain_for_sampling(model, max_coefficients, sampling_depth, objective,
constrained_model.solver.update()

flux_sum = constrained_model.slim_optimize(error_value=0.)
flux_sum_constraint = constrained_model.problem.Constraint(sampling_expr, lb=min_flux, ub=flux_sum)
model.add_cons_vars(flux_sum_constraint)
constrained_model.solver.update()

# Analyze flux ranges and calculate concordance
try:
flux_samples = _gapsplit(constrained_model, depth=sampling_depth)
concordance = _calc_concordance(flux_samples, max_coefficients)
except:
if flux_sum <= min_flux:
flux_samples = 'Not performed'
concordance = 'Not performed'
else:
flux_sum_constraint = constrained_model.problem.Constraint(sampling_expr, lb=min_flux, ub=flux_sum)
constrained_model.add_cons_vars(flux_sum_constraint)
constrained_model.solver.update()

# Analyze flux ranges and calculate concordance
try:
flux_samples = _gapsplit(constrained_model, depth=sampling_depth)
concordance = _calc_concordance(flux_samples, max_coefficients)
except:
flux_samples = 'Not performed'
concordance = 'Not performed'

return flux_samples, concordance

Expand Down Expand Up @@ -1087,7 +1090,7 @@ def _calc_concordance(flux_samples, coefficient_dict):
# Prune model based on blocked reactions from minimization as well as user-defined reactions
def _prune_model(model, rm_rxns, conserve):

new_model = deepcopy(model)
new_model = model.copy()

if str(new_model.id).strip() == '':
new_model.id = 'model_riptide'
Expand Down