equilibrator_iJO1366.py 3.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# The MIT License (MIT)
#
# Copyright (c) 2013 Weizmann Institute of Science
# Copyright (c) 2018 Institute for Molecular Systems Biology,
# ETH Zurich
# Copyright (c) 2018 Novo Nordisk Foundation Center for Biosustainability,
# Technical University of Denmark
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
26 27 28 29


import argparse
import csv
Elad Noor's avatar
Elad Noor committed
30
import logging
31 32
import sys

Elad Noor's avatar
Elad Noor committed
33 34 35 36 37
from numpy import nan, sqrt

from equilibrator_api import ComponentContribution, Reaction


38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
if __name__ == '__main__':

    parser = argparse.ArgumentParser(
        description='Calculate potentials for a number of reactions.')
    parser.add_argument(
        'outfile', type=argparse.FileType('w'),
        help='path to output file')
    parser.add_argument('--i', type=float,
                        help='ionic strength in M',
                        default=0.1)
    parser.add_argument('--ph', type=float, help='pH level', default=7.0)
    logging.getLogger().setLevel(logging.WARNING)

    args = parser.parse_args()

    sys.stderr.write('pH = %.1f\n' % args.ph)
    sys.stderr.write('I = %.1f M\n' % args.i)

    ids = []
    reactions = []
    with open('data/iJO1366_reactions.csv', 'r') as f:
        for row in csv.DictReader(f):
            ids.append(row['bigg.reaction'])
            try:
                reactions.append(Reaction.parse_formula(row['formula']))
            except ValueError as e:
                print('warning: cannot parse reaction %s because of %s' %
                      (row['bigg.reaction'], str(e)))
                reactions.append(Reaction({}))
                continue
68

eladnoor's avatar
eladnoor committed
69
    cc = ComponentContribution(pH=args.ph, ionic_strength=args.i)
70

eladnoor's avatar
eladnoor committed
71
    dG0_prime, U = cc.standard_dg_prime_multi(reactions)
72 73 74 75 76 77 78 79 80 81 82

    writer = csv.writer(args.outfile)
    header = ['reaction', 'pH', 'ionic strength [M]', 'dG\'0 [kJ/mol]',
              'uncertainty [kJ/mol]', 'ln(Reversibility Index)', 'comment']
    writer.writerow(header)
    for s, r, dg0, u in zip(ids, reactions,
                            dG0_prime.flat, U.diagonal().flat):
        row = [s, args.ph, args.i]
        if r.is_empty():
            row += [nan, nan, nan, 'reaction is empty']
        elif r.check_full_reaction_balancing():
eladnoor's avatar
eladnoor committed
83
            ln_RI = cc.reversibility_index(r)
84 85 86 87 88 89
            row += ['%.2f' % dg0, '%.2f' % sqrt(u), '%.2f' % ln_RI, '']
        else:
            row += [nan, nan, nan, 'reaction is not chemically balanced']
        writer.writerow(row)

    args.outfile.flush()