Skip to content

Commit

Permalink
Merge pull request #19 from cdanielmachado/py3
Browse files Browse the repository at this point in the history
merge py3 with master
  • Loading branch information
cdanielmachado committed May 28, 2018
2 parents 7404718 + e9fbcfa commit 43ed829
Show file tree
Hide file tree
Showing 18 changed files with 121 additions and 79 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ carveme.egg-info/*

*.pyc

.eggs/*

docs/_build/*
Dockerfile
4 changes: 3 additions & 1 deletion carveme/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from future import standard_library
standard_library.install_aliases()
import os
from ConfigParser import ConfigParser
from configparser import ConfigParser
from framed import set_default_solver, set_default_parameter, Parameter

__version__ = '1.1.0'
Expand Down
13 changes: 8 additions & 5 deletions carveme/reconstruction/carving.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import print_function
from builtins import zip
from builtins import range
import numpy as np
import warnings
import pandas as pd
Expand Down Expand Up @@ -66,7 +69,7 @@ def minmax_reduction(model, scores, min_growth=0.1, min_atpm=0.1, eps=1e-3, bigM
objective = {}

scores = scores.copy()
reactions = scores.keys()
reactions = list(scores.keys())

if not soft_constraints:
soft_constraints = {}
Expand Down Expand Up @@ -94,11 +97,11 @@ def minmax_reduction(model, scores, min_growth=0.1, min_atpm=0.1, eps=1e-3, bigM
solver.pos_vars = []

for r_id in reactions:
if model.reactions[r_id].lb < 0 or model.reactions[r_id].lb is None:
if model.reactions[r_id].lb is None or model.reactions[r_id].lb < 0:
y_r = 'yr_' + r_id
solver.add_variable(y_r, 0, 1, vartype=VarType.BINARY, update_problem=False)
solver.neg_vars.append(y_r)
if model.reactions[r_id].ub > 0 or model.reactions[r_id].ub is None:
if model.reactions[r_id].ub is None or model.reactions[r_id].ub > 0:
y_f = 'yf_' + r_id
solver.add_variable(y_f, 0, 1, vartype=VarType.BINARY, update_problem=False)
solver.pos_vars.append(y_f)
Expand Down Expand Up @@ -226,7 +229,7 @@ def carve_model(model, reaction_scores, outputfile=None, flavor=None, inplace=Tr
if sol.status == Status.OPTIMAL:
inactive = inactive_reactions(model, sol)
else:
print "MILP solver failed: {}".format(sol.message)
print("MILP solver failed: {}".format(sol.message))
return

if debug_output:
Expand All @@ -245,7 +248,7 @@ def carve_model(model, reaction_scores, outputfile=None, flavor=None, inplace=Tr
gpr = parse_gpr_rule(row['GPR'], prefix='G_')
model.set_gpr_association(r_id, gpr, add_genes=True)
except:
print 'Failed to parse:', row['GPR']
print('Failed to parse:', row['GPR'])

cleanup_metadata(model)

Expand Down
3 changes: 2 additions & 1 deletion carveme/reconstruction/diamond.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import pandas as pd
from subprocess import call
import os
Expand Down Expand Up @@ -63,7 +64,7 @@ def run_blast(inputfile, input_type, outputfile, database, args=None, verbose=Tr
cmd += args.split()

if verbose:
print ' '.join(cmd)
print(' '.join(cmd))

with open(os.devnull, 'w') as devnull:
try:
Expand Down
7 changes: 5 additions & 2 deletions carveme/reconstruction/gapfilling.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import division
from __future__ import print_function
from past.utils import old_div
from carveme.reconstruction.utils import medium_to_constraints
from framed.model.transformation import disconnected_metabolites
from framed.solvers import solver_instance
Expand Down Expand Up @@ -60,7 +63,7 @@ def gapFill(model, universe, constraints=None, min_growth=0.1, scores=None, inpl

solver.update()

objective = {'y_'+r_id: 1.0 / (1.0 + scores.get(r_id, 0.0)) for r_id in new_reactions}
objective = {'y_'+r_id: old_div(1.0, (1.0 + scores.get(r_id, 0.0))) for r_id in new_reactions}

solution = solver.solve(linear=objective, minimize=True, constraints=constraints)

Expand Down Expand Up @@ -123,7 +126,7 @@ def multiGapFill(model, universe, media, media_db, min_growth=0.1, max_uptake=10
gapFill(model, universe, constraints=constraints, min_growth=min_growth,
scores=scores, inplace=True, bigM=bigM, solver=solver, tag=medium_name)
else:
print 'Medium {} not in database, ignored.'.format(medium_name)
print('Medium {} not in database, ignored.'.format(medium_name))

return model

Expand Down
15 changes: 9 additions & 6 deletions carveme/reconstruction/ncbi_download.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from __future__ import print_function
from future import standard_library
standard_library.install_aliases()
import pandas as pd
import urllib
import urllib.request, urllib.parse, urllib.error
import os


Expand All @@ -11,7 +14,7 @@ def load_ncbi_table(inputfile):
def download_ncbi_genome(accession, refseq_table, prefer_protein=True):

if accession not in refseq_table.index:
print 'Invalid accession code'
print('Invalid accession code')
return

entry = refseq_table.loc[accession, :]
Expand All @@ -24,9 +27,9 @@ def download_ncbi_genome(accession, refseq_table, prefer_protein=True):

outputfile = '{}.faa.gz'.format(accession)

_, result = urllib.urlretrieve(url, outputfile)
_, result = urllib.request.urlretrieve(url, outputfile)

if result.gettype() != 'application/x-gzip':
if result.get_content_type() != 'application/x-gzip':
os.remove(outputfile)
else:
downloaded = True
Expand All @@ -37,9 +40,9 @@ def download_ncbi_genome(accession, refseq_table, prefer_protein=True):

outputfile = '{}.fna.gz'.format(accession)

_, result = urllib.urlretrieve(url, outputfile)
_, result = urllib.request.urlretrieve(url, outputfile)

if result.gettype() != 'application/x-gzip':
if result.get_content_type() != 'application/x-gzip':
os.remove(outputfile)
else:
downloaded = True
Expand Down
4 changes: 3 additions & 1 deletion carveme/reconstruction/scoring.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import division
from past.utils import old_div
import pandas as pd


Expand Down Expand Up @@ -118,7 +120,7 @@ def reaction_scoring(annotation, gprs, spontaneous_score=0.0, debug_output=None)
reaction_scores.to_csv(debug_output + '_reaction_scores.tsv', sep='\t', index=False)

if avg_score != 0:
reaction_scores['normalized_score'] = reaction_scores['score'] / avg_score
reaction_scores['normalized_score'] = old_div(reaction_scores['score'], avg_score)
return reaction_scores
else:
return None
2 changes: 2 additions & 0 deletions carveme/reconstruction/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from builtins import str
from builtins import zip
from collections import OrderedDict
from warnings import warn

Expand Down
20 changes: 13 additions & 7 deletions carveme/universe/bigg_download.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from __future__ import division
from __future__ import print_function
from builtins import str
from builtins import map
from builtins import range
from past.utils import old_div
import requests
import pandas as pd
import sys
Expand All @@ -15,7 +21,7 @@


def progress(i, n):
p = int(((i+1)*100.0)/n)
p = int(old_div(((i+1)*100.0),n))
sys.stdout.write("\r{}%".format(p))
sys.stdout.flush()

Expand Down Expand Up @@ -43,9 +49,9 @@ def get_request(url, max_tries=10):
break

if data is None:
print 'max number of attempts exceeded:', max_tries
print('max number of attempts exceeded:', max_tries)
elif i > 0:
print 'failed attempts', i
print('failed attempts', i)

return data

Expand Down Expand Up @@ -188,7 +194,7 @@ def build_bigg_universe_model(outputfile=None):
CBModel: universe model
"""

print 'Downloading universal data from BiGG...'
print('Downloading universal data from BiGG...')
model = CBModel('bigg_universe')
bigg_rxns = get_request(reactions_url)

Expand All @@ -197,7 +203,7 @@ def build_bigg_universe_model(outputfile=None):
build_reaction(model, entry['bigg_id'])
progress(i, n)

print '\n'
print('\n')

if outputfile:
save_cbmodel(model, outputfile)
Expand All @@ -221,7 +227,7 @@ def download_model_specific_data(outputfile=None):
"""

print 'Downloading model-specific data from BiGG...'
print('Downloading model-specific data from BiGG...')

data = get_request(models_url)
rows = []
Expand All @@ -247,7 +253,7 @@ def download_model_specific_data(outputfile=None):
rows.append((r_id, model_id, lb, ub, subsystem, gpr))

progress(i, n)
print '\n'
print('\n')

columns = ['reaction', 'model', 'lower_bound', 'upper_bound', 'subsystem', 'GPR']
df = pd.DataFrame(rows, columns=columns)
Expand Down
31 changes: 16 additions & 15 deletions carveme/universe/curation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import numpy as np
from framed.model.transformation import disconnected_metabolites
from carveme.reconstruction.utils import create_exchange_reactions, add_biomass_equation, create_sink_reactions, \
Expand Down Expand Up @@ -312,15 +313,15 @@ def curate_universe(model, model_specific_data, bigg_models, biomass_eq, taxa=No
essencial reactions/metabolites that would otherwise be structurally blocked).
"""

print 'Starting universe curation...'
print '(initial size: {} x {})\n'.format(len(model.metabolites), len(model.reactions))
print('Starting universe curation...')
print('(initial size: {} x {})\n'.format(len(model.metabolites), len(model.reactions)))

trusted_models = bigg_models.query('trusted == True').index.tolist()

add_bounds_from_extracted_data(model, model_specific_data, trusted_models)

if taxa:
print 'Filtering by taxa:', taxa
print('Filtering by taxa:', taxa)
kingdom_map = bigg_models['domain'].to_dict()

if taxa in {'cyanobacteria', 'bacteria'}:
Expand All @@ -340,10 +341,10 @@ def curate_universe(model, model_specific_data, bigg_models, biomass_eq, taxa=No
other_compartments = set(model.compartments.keys()) - valid_compartments
model.remove_compartments(other_compartments, delete_metabolites=True, delete_reactions=True)

print '(size: {} x {})\n'.format(len(model.metabolites), len(model.reactions))
print('(size: {} x {})\n'.format(len(model.metabolites), len(model.reactions)))

if thermodynamics_data is not None:
print 'Computing thermodynamics...',
print('Computing thermodynamics...', end=' ')

dG0 = thermodynamics_data['dG0'].to_dict()
sdG0 = thermodynamics_data['sdG0'].to_dict()
Expand All @@ -354,10 +355,10 @@ def curate_universe(model, model_specific_data, bigg_models, biomass_eq, taxa=No
x0 = None

compute_flux_bounds(model, dG0, sdG0, x0, method=thermodynamics_method, inplace=True, override_trusted=False)
print 'done\n'
print('done\n')


print 'Applying manual curation rules...',
print('Applying manual curation rules...', end=' ')

if use_heuristics:
reversibility_heuristics(model, no_reverse_atp=True, no_proton_pumps=False, override_trusted=False)
Expand All @@ -368,7 +369,7 @@ def curate_universe(model, model_specific_data, bigg_models, biomass_eq, taxa=No
if r_id in model.reactions:
model.set_flux_bounds(r_id, lb, ub)

print 'done\n'
print('done\n')

if remove_unbalanced:

Expand All @@ -377,11 +378,11 @@ def curate_universe(model, model_specific_data, bigg_models, biomass_eq, taxa=No
for m_id in ['M_photon_e', 'M_photon_p', 'M_photon_c']:
model.metabolites[m_id].metadata['FORMULA'] = ''

print 'Removing unbalanced reactions...'
print('Removing unbalanced reactions...')
remove_unbalanced_reactions(model)
print '(size: {} x {})\n'.format(len(model.metabolites), len(model.reactions))
print('(size: {} x {})\n'.format(len(model.metabolites), len(model.reactions)))

print 'Creating pseudo-reactions...'
print('Creating pseudo-reactions...')

create_exchange_reactions(model, default_lb=-1000, default_ub=1000)

Expand All @@ -392,14 +393,14 @@ def curate_universe(model, model_specific_data, bigg_models, biomass_eq, taxa=No

add_maintenance_atp(model)

print '(size: {} x {})\n'.format(len(model.metabolites), len(model.reactions))
print('(size: {} x {})\n'.format(len(model.metabolites), len(model.reactions)))

if remove_blocked:
print 'Removing blocked reactions and dead-end metabolites...'
print('Removing blocked reactions and dead-end metabolites...')
simplify(model)
print '(size: {} x {})\n'.format(len(model.metabolites), len(model.reactions))
print('(size: {} x {})\n'.format(len(model.metabolites), len(model.reactions)))

if outputfile:
save_cbmodel(model, outputfile)

print 'Done.'
print('Done.')
11 changes: 7 additions & 4 deletions carveme/universe/thermodynamics.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import division
from builtins import zip
from past.utils import old_div
import pandas as pd
import numpy as np
from framed import load_cbmodel
Expand Down Expand Up @@ -100,7 +103,7 @@ def calculate_deltaG0s(model, kegg_compounds, pH=default_pH, I=default_I, T=defa
"""
kegg_rxns = build_kegg_reactions(model, kegg_compounds)
kmodel = KeggModel.from_formulas(kegg_rxns.values(), raise_exception=True)
kmodel = KeggModel.from_formulas(list(kegg_rxns.values()), raise_exception=True)
kmodel.add_thermo(CC.init())
dG0, sdG0, _ = kmodel.get_transformed_dG0(pH, I, T)

Expand Down Expand Up @@ -170,7 +173,7 @@ def dG_bounds(model, r_id, dG0, sdG0=None, x0=None, excluded=None, T=default_T):
if m_id in excluded:
continue
elif m_id in x0:
x_min = x0[m_id] / sqrt(measured_fold_change)
x_min = old_div(x0[m_id], sqrt(measured_fold_change))
x_max = x0[m_id] * sqrt(measured_fold_change)
else:
x_min = concentration_min
Expand All @@ -183,8 +186,8 @@ def dG_bounds(model, r_id, dG0, sdG0=None, x0=None, excluded=None, T=default_T):
prod_min.append(x_min ** coeff)
prod_max.append(x_max ** coeff)

dG_min = dG0[r_id] - sdG0[r_id] + R * T * np.log(np.prod(prod_min) / np.prod(reac_max))
dG_max = dG0[r_id] + sdG0[r_id] + R * T * np.log(np.prod(prod_max) / np.prod(reac_min))
dG_min = dG0[r_id] - sdG0[r_id] + R * T * np.log(old_div(np.prod(prod_min), np.prod(reac_max)))
dG_max = dG0[r_id] + sdG0[r_id] + R * T * np.log(old_div(np.prod(prod_max), np.prod(reac_min)))

return dG_min, dG_max

Expand Down
1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
framed==0.3.5
pandas>=0.20.0
requests>=2.18



Expand Down
3 changes: 2 additions & 1 deletion scripts/build_universe
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python

from __future__ import print_function
from carveme import config, project_dir
import argparse
import os
Expand Down Expand Up @@ -113,7 +114,7 @@ def main(mode, noheuristics=False, nothermo=False, allow_unbalanced=False, allow
remove_blocked=(not allow_blocked))

else:
print 'Unrecognized option:', mode
print('Unrecognized option:', mode)


if __name__ == '__main__':
Expand Down

0 comments on commit 43ed829

Please sign in to comment.