示例#1
0
    def _curation(self):
        # read models
        model1 = ModelReader.reader_from_path(self._args.model)
        model1 = model1.create_model()
        model2 = ModelReader.reader_from_path(self._args.dest_model)
        model2 = model2.create_model()

        # initiate curator
        curator = curation.Curator(self._args.compound_map,
                                   self._args.reaction_map,
                                   self._args.curated_compound_map,
                                   self._args.curated_reaction_map)

        # read commpartment map
        compartment_map = {}
        if self._args.compartment_map is not None:
            with open(self._args.compartment_map) as f:
                for row in f:
                    old, new = row.strip().split()
                    compartment_map[old] = new

        if self._args.compound_only:
            self._curate_compound(curator, model1, model2)
        else:
            self._curate_reaction(curator, model1, model2, compartment_map)
示例#2
0
 def setUp(self):
     self._model_dir = tempfile.mkdtemp()
     with open(os.path.join(self._model_dir, 'model.yaml'), 'w') as f:
         f.write('\n'.join([
             '---',
             'reactions:',
             '  - id: rxn_1',
             '    equation: A[e] => B[c]',
             '  - id: rxn_2',
             '    equation: B[c] => C[e]',
             '  - id: rxn_3',
             '    equation: A[e] + (6) B[c] <=> (6) C[e] + (6) D[c]',
             'compounds:',
             '  - id: A',
             '    formula: C6H12O6',
             '    charge: 1',
             '  - id: B',
             '    formula: O2',
             '    charge: 1',
             '  - id: C',
             '    formula: CO2',
             '    charge: -1',
             '  - id: D',
             '    formula: H2O',
         ]))
     self._model = ModelReader.reader_from_path(
         self._model_dir).create_model()
示例#3
0
 def setUp(self):
     self._model_dir = tempfile.mkdtemp()
     with open(os.path.join(self._model_dir, 'model.yaml'), 'w') as f:
         f.write('\n'.join([
             '---',
             'reactions:',
             '  - id: rxn_1',
             '    equation: A[e] => B[c]',
             '  - id: rxn_2',
             '    equation: B[c] => C[e]',
             '  - id: rxn_3',
             '    equation: A[e] + (6) B[c] <=> (6) C[e] + (6) D[c]',
             'compounds:',
             '  - id: A',
             '    formula: C6H12O6',
             '    charge: 1',
             '  - id: B',
             '    formula: O2',
             '    charge: 1',
             '  - id: C',
             '    formula: CO2',
             '    charge: -1',
             '  - id: D',
             '    formula: H2O',
         ]))
     self._model = ModelReader.reader_from_path(
         self._model_dir).create_model()
示例#4
0
    def setUp(self):
        self._model_dir = tempfile.mkdtemp()
        with open(os.path.join(self._model_dir, 'model.yaml'), 'w') as f:
            f.write('\n'.join([
                '---', 'reactions:', '  - id: rxn_1',
                '    equation: A[e] => B[c]', '    genes: gene_1',
                '  - id: rxn_2', '    equation: B[c] => C[c]',
                '    genes: gene_2 and gene_3', '  - id: rxn_3',
                '    equation: C[c] <=> D[c]', '    genes: gene_3 and gene_4',
                '  - id: rxn_4', '    equation: D[c] => E[e]',
                '    genes: gene_3 or gene_4', '  - id: rxn_5',
                '    equation: C[c] => D[e]',
                '    genes: (gene_3 or gene_4) and (gene_1 or gene_5)',
                '  - id: rxn_6', '    equation: D[c] => A[e]',
                '    genes: Biomass', '  - id: rxn_7',
                '    equation: E[e] => A[e]', '  - id: rxn_8',
                '    equation: E[e] =>'
            ]))
        self._model = ModelReader.reader_from_path(
            self._model_dir).create_model()
        self.rxn_entry_dict = {}
        for rx in self._model.reactions:
            self.rxn_entry_dict[rx.id] = rx

        with open(os.path.join(self._model_dir, 'app.tsv'), 'w') as f:
            f.write('\n'.join([
                'gene_1\tgene_a', 'gene_2\tgene_b', 'gene_3\tgene_c',
                'gene_4\t-', 'gene_5\tgene_e, gene_f', '-\tgene_g'
            ]))
示例#5
0
 def test_no_compartment_change(self):
     model = ModelReader.reader_from_path(
         os.path.join(self._model_dir, 'model.yaml')).create_model()
     translated = tr_id.TranslatedModel(model,
                                        self._translated.cpd_mapping_id,
                                        self._translated.rxn_mapping_id)
     self.assertEqual(translated.reactions['rxn_1a'].equation,
                      parse_reaction('A1[e] => B2[c]'))
     self.assertEqual(
         translated.reactions['rxn_3c'].equation,
         parse_reaction('A1[e] + (6) B2[c] <=> (6) A1[e] + (6) D4[c]'))
     self.assertIn('c', translated.compartments)
     self.assertNotIn('s', translated.compartments)
     self.assertIn('e', translated.compartments)
     self.assertSetEqual(translated._compartment_boundaries,
                         set({('c', 'e')}))
     self.assertEqual(translated.extracellular_compartment, 'e')
示例#6
0
 def setUp(self):
     self._model_dir = tempfile.mkdtemp()
     with open(os.path.join(self._model_dir, 'model.yaml'), 'w') as f:
         f.write('\n'.join([
             '---',
             'reactions:',
             '  - id: rxn_1',
             '    equation: A[e] <=> B[c]',
             '    genes: gene_1 and gene_2',
             '  - id: rxn_2',
             '    equation: A[e] => C[c]',
             '    genes: gene_3',
             '  - id: rxn_3',
             '    equation: A[e] => D[e]',
             '  - id: rxn_4',
             '    equation: C[c] => D[e]',
             '    genes: [gene_4, gene_5, gene_6]',
             'compounds:',
             '  - id: A',
             '  - id: B',
             '  - id: C',
             '  - id: D',
             'exchange:',
             '  - compartment: e',
             '    compounds:',
             '      - id: A',
             '        reaction: rxn_5',
             '      - id: D',
             '        reaction: rxn_6',
             'model:',
             '  - reactions:',
             '     - rxn_3',
             '     - rxn_4',
         ]))
     self._model = ModelReader.reader_from_path(
         self._model_dir).create_model()
示例#7
0
 def setUp(self):
     self._model_dir = tempfile.mkdtemp()
     with open(os.path.join(self._model_dir, 'model.yaml'), 'w') as f:
         f.write('\n'.join([
             '---',
             'reactions:',
             '  - id: rxn_1',
             '    equation: A[e] <=> B[c]',
             '    genes: gene_1 and gene_2',
             '  - id: rxn_2',
             '    equation: A[e] => C[c]',
             '    genes: gene_3',
             '  - id: rxn_3',
             '    equation: A[e] => D[e]',
             '  - id: rxn_4',
             '    equation: C[c] => D[e]',
             '    genes: [gene_4, gene_5, gene_6]',
             'compounds:',
             '  - id: A',
             '  - id: B',
             '  - id: C',
             '  - id: D',
             'exchange:',
             '  - compartment: e',
             '    compounds:',
             '      - id: A',
             '        reaction: rxn_5',
             '      - id: D',
             '        reaction: rxn_6',
             'model:',
             '  - reactions:',
             '     - rxn_3',
             '     - rxn_4',
         ]))
     self._model = ModelReader.reader_from_path(
         self._model_dir).create_model()
示例#8
0
                    help='Min flux for the varying reaction.')
parser.add_argument('--max',
                    type=float,
                    help='Max flux for the varying reaction.')
parser.add_argument('-s',
                    '--steps',
                    type=int,
                    help=('How many steps are between min '
                          'and max flux of the varying'
                          'reaction (default = 10).'),
                    default=10)
parser.add_argument('--objective', help='Reaction to use as objective.')

args = parser.parse_args()

model = ModelReader.reader_from_path(args.model)
nm = model.create_model()

if args.objective is None:
    args.objective = nm.biomass_reaction

fvs = list()
if args.fixed_reaction is not None:
    for i in args.fixed_reaction:
        r, lower, upper = i.split(',')
        if r not in nm.reactions:
            sys.exit('Error: %s is not in model' % (r))
        if lower == '':
            lower = -1 * nm.default_flux_limit
        if upper == '':
            upper = nm.default_flux_limit
示例#9
0
# from psamm.fluxanalysis import flux_variability
import sys
import timeout_decorator

# In[4]:

# rxn_list = []
# with open(sys.argv[2]) as f:
#     for row in f:
#         if '#' in row:
#             continue
#         rxn_list.append(row.strip())

# In[5]:

model = ModelReader.reader_from_path(sys.argv[1])
nm = model.create_model()
if len(sys.argv) == 4:
    nm.biomass_reaction = sys.argv[3]
mm = nm.create_metabolic_model()

p = FluxBalanceProblem(mm, Solver())
p.maximize(nm.biomass_reaction)
threshold = p.get_flux(nm.biomass_reaction)
p.add_thermodynamic()
p.prob.add_linear_constraints(p.get_flux_var(nm.biomass_reaction) >= threshold)

rxn_list = [rxn for rxn in mm.reactions]
# In[ ]:

示例#10
0
    def setUp(self):
        self._model_dir = tempfile.mkdtemp()
        with open(os.path.join(self._model_dir, 'model1.yaml'), 'w') as f:
            f.write('\n'.join([
                '---',
                'reactions:',
                '  - id: rxn_1',
                '    name: rxn_1',
                '    equation: A[e] => B[c]',
                '    genes: gene1 and gene2 or gene3',
                '  - id: rxn_2',
                '    name: rxn_2',
                '    equation: B[c] => C[e]',
                '    genes: gene5 or gene6',
                '  - id: rxn_3',
                '    name: rxn_3',
                '    equation: A[e] + (6) B[c] <=> (6) C[e] + (6) D[c]',
                '    genes: gene7',
                'compounds:',
                '  - id: A',
                '    name: A',
                '    formula: C6H12O6',
                '    charge: 1',
                '  - id: B',
                '    name: B',
                '    formula: O2',
                '    charge: 1',
                '    kegg: C00010',
                '  - id: C',
                '    name: C',
                '    formula: CO2',
                '    charge: -1',
                '  - id: D',
                '    name: D',
                '    formula: H2O',
            ]))
        self._model1 = ModelReader.reader_from_path(
            os.path.join(self._model_dir, 'model1.yaml')).create_model()
        self._model1 = bayesian.MappingModel(self._model1)

        with open(os.path.join(self._model_dir, 'model2.yaml'), 'w') as f:
            f.write('\n'.join([
                '---',
                'reactions:',
                '  - id: rxn_1',
                '    name: rxn_1',
                '    equation: A[e] => B2[c]',
                '    genes: gene1 and gene2 or gene3',
                '  - id: rnx_2',
                '    equation: B2[c] => C[e]',
                '    genes: gene5 and gene6',
                '  - id: rxn_3',
                '    name: rxn_3',
                '    equation: A[e] <=> (6) C[e] + (6) D4[c]',
                '  - id: rxn_4',
                '    equation: E[c] => F[c]',
                'compounds:',
                '  - id: A',
                '    name: a',
                '    formula: C6H11O6',
                '    charge: 0',
                '  - id: B2',
                '    name: -B ()',
                '    formula: O2',
                '    charge: 1',
                '    kegg: C00010',
                '  - id: C',
                '    name: C',
                '    charge: -1',
                '  - id: D4',
                '    name: D',
                '    formula: H2O',
                '    charge: -1',
            ]))
        self._model2 = ModelReader.reader_from_path(
            os.path.join(self._model_dir, 'model2.yaml')).create_model()
        self._model2 = bayesian.MappingModel(self._model2)
示例#11
0
    def _model_mapping(self):
        """Run model mapping"""
        # Parse models
        model1 = ModelReader.reader_from_path(self._args.model).create_model()
        model2 = ModelReader.reader_from_path(
            self._args.dest_model).create_model()

        # Read model into internal format
        model1 = bayesian.MappingModel(model1)
        model2 = bayesian.MappingModel(model2)

        # Model summaries
        model1.print_summary()
        print('\n')
        model2.print_summary()
        print('\n')

        # Load actual model mappings
        actual_compound_mapping = None
        if self._args.compound_map is not None:
            with open(self._args.compound_map, 'r') as f:
                actual_compound_mapping = dict(read_mapping_file(f))

        actual_reaction_mapping = None
        if self._args.reaction_map is not None:
            with open(self._args.reaction_map, 'r') as f:
                actual_reaction_mapping = dict(read_mapping_file(f))

        # Load compartment mapping
        compartment_map = {}
        if self._args.compartment_map_file is not None:
            with open(self._args.compartment_map_file, 'r') as f:
                for row in f:
                    source, target = row.strip().split()
                    compartment_map[source] = target

        # Check models
        if (not (model1.check_reaction_compounds()
                 and model2.check_reaction_compounds())
                and self._args.consistency_check):
            quit(('\nError: '
                  'equations have something not listed in compounds.yaml, '
                  'please check it!'))

        mkdir_p(self._args.outpath)

        if (actual_compound_mapping is not None):
            mkdir_p(self._args.outpath + '/roc')

        # Bayesian classifier
        print('Using %i processes...' % (self._args.nproc))
        t = time.time()
        cpd_bayes_pred = bayesian.BayesianCompoundPredictor(
            model1,
            model2,
            self._args.nproc,
            self._args.outpath,
            log=self._args.log,
            kegg=self._args.map_compound_kegg)
        print('It took %s seconds to calculate compound mapping...' %
              (time.time() - t))

        print('Writing output...')
        sys.stdout.flush()
        t = time.time()
        # Write out ROC curve results
        if (actual_compound_mapping is not None):
            with open(self._args.outpath + '/roc/compound_bayes.tsv',
                      'w') as f:
                write_roc_curve(f, cpd_bayes_pred.model1.compounds,
                                cpd_bayes_pred.model2.compounds,
                                cpd_bayes_pred, actual_compound_mapping)

        # Parse and output raw mapping
        if self._args.raw:
            cpd_bayes_pred.get_raw_map().to_csv(self._args.outpath +
                                                '/bayes_compounds.tsv',
                                                sep='\t')

        # Output best mapping
        compound_best = cpd_bayes_pred.get_best_map(
            self._args.threshold_compound)
        compound_best.to_csv(self._args.outpath + '/bayes_compounds_best.tsv',
                             sep='\t')
        print('It took %s seconds to write output...' % (time.time() - t))
        sys.stdout.flush()

        # Bayesian classifier
        t = time.time()
        rxn_bayes_pred = bayesian.BayesianReactionPredictor(
            model1,
            model2,
            compound_best.loc[:, 'p'],
            self._args.nproc,
            self._args.outpath,
            log=self._args.log,
            gene=self._args.map_reaction_gene,
            compartment_map=compartment_map)
        print('It took %s seconds to calculate reaction mapping...' %
              (time.time() - t))

        print('Writing output...')
        sys.stdout.flush()
        t = time.time()
        # Write out ROC curve results
        if (actual_reaction_mapping is not None):
            with open(self._args.outpath + '/roc/reaction_bayes.tsv',
                      'w') as f:
                write_roc_curve(f, rxn_bayes_pred.model1.reactions,
                                rxn_bayes_pred.model2.reactions,
                                rxn_bayes_pred, actual_reaction_mapping)

        # Parse and output raw mapping
        if self._args.raw:
            rxn_bayes_pred.get_raw_map().to_csv(self._args.outpath +
                                                '/bayes_reactions.tsv',
                                                sep='\t')

        # Output best mapping
        reaction_best = rxn_bayes_pred.get_best_map(
            self._args.threshold_reaction)
        reaction_best.to_csv(self._args.outpath + '/bayes_reactions_best.tsv',
                             sep='\t')
        print('It took %s seconds to write output...' % (time.time() - t))
        sys.stdout.flush()
示例#12
0
    def setUp(self):
        self._model_dir = tempfile.mkdtemp()
        with open(os.path.join(self._model_dir, 'model.yaml'), 'w') as f:
            f.write('\n'.join([
                '---',
                'biomass: rxn_1',
                'compartments:',
                '  - id: c',
                '    adjacent_to: e',
                '  - id: e',
                'reactions:',
                '  - id: rxn_1',
                '    name: rxn_1',
                '    equation: A[e] => B[c]',
                '    genes: gene1 and gene2 or gene3',
                '  - id: rxn_2',
                '    name: rxn_2',
                '    equation: B[c] => C[e]',
                '    genes: gene5 or gene6',
                '  - id: rxn_3',
                '    name: rxn_3',
                '    equation: A[e] + (6) B[c] <=> (6) C[e] + (6) D[c]',
                '    genes: gene7',
                'compounds:',
                '  - id: A',
                '    name: compound_A',
                '    formula: C6H12O6',
                '    charge: 1',
                '  - id: B',
                '    name: compound_B',
                '    formula: O2',
                '    charge: 1',
                '    kegg: C00010',
                '  - id: C',
                '    name: compound_C',
                '    formula: CO2',
                '    charge: -1',
                '  - id: D',
                '    name: compound_D',
                '    formula: H2O',
                'exchange:',
                '  - compartment: e',
                '  - compounds:',
                '    - id: A',
                '      upper: 100',
                '    - id: C',
                'limits:',
                '  - reaction: rxn_1',
                '    lower: -50',
                '  - reaction: rxn_2',
                '    upper: 100',
            ]))
        self._model = ModelReader.reader_from_path(
            os.path.join(self._model_dir, 'model.yaml')).create_model()

        with open(os.path.join(self._model_dir, 'newmodel.yaml'), 'w') as f:
            f.write('\n'.join([
                '---',
                'biomass: rxn_1a',
                'extracellular: s',
                'compartments:',
                '  - id: c',
                '    adjacent_to: s',
                '  - id: s',
                'reactions:',
                '  - id: rxn_1a',
                '    name: rxn_1',
                '    equation: A1[s] => B2[c]',
                '    genes: gene1 and gene2 or gene3',
                '  - id: rxn_2b',
                '    name: rxn_2',
                '    equation: B2[c] => A1[s]',
                '    genes: gene5 or gene6',
                '  - id: rxn_3c',
                '    name: rxn_3',
                '    equation: A1[s] + (6) B2[c] <=> (6) A1[s] + (6) D4[c]',
                '    genes: gene7',
                'compounds:',
                '  - id: A1',
                '    name: compound_A',
                '    formula: C6H12O6',
                '    charge: 1',
                '  - id: B2',
                '    name: compound_B',
                '    formula: O2',
                '    charge: 1',
                '    kegg: C00010',
                '  - id: C3',
                '    name: compound_C',
                '    formula: CO2',
                '    charge: -1',
                '  - id: D4',
                '    name: compound_D',
                '    formula: H2O',
                'exchange:',
                '  - compartment: s',
                '  - compounds:',
                '    - id: A1',
                '      upper: 100',
                '      reaction: EX_A1(s)',
                'limits:',
                '  - reaction: rxn_1a',
                '    lower: -50',
            ]))
        self._newmodel = ModelReader.reader_from_path(
            os.path.join(self._model_dir, 'newmodel.yaml')).create_model()

        with open(os.path.join(self._model_dir, 'compount_map.tsv'), 'w') as f:
            f.write('\n'.join([
                'id1\tid2\tp',
                'A\tA1\t1.0',
                'B\tB2\t0.9',
                'C\tA1\t0.001',
                'D\tD4\t0.32',
                'F\t\t',
            ]))

        with open(os.path.join(self._model_dir, 'reaction_map.tsv'), 'w') as f:
            f.write('\n'.join([
                'id1\tid2\tp',
                'rxn_1\trxn_1a\t1.0',
                'rxn_2\trxn_1a\t0.9',
                'rxn_4\t\t',
                'rxn_3\trxn_3c\t0.32',
            ]))

        with open(os.path.join(self._model_dir, 'compartment_map.tsv'),
                  'w') as f:
            f.write('\n'.join([
                'e\ts',
            ]))

        self._compound_map = tr_id.read_mapping(
            os.path.join(self._model_dir, 'compount_map.tsv'))
        self._reaction_map = tr_id.read_mapping(
            os.path.join(self._model_dir, 'reaction_map.tsv'))
        self._compartment_map = tr_id.read_mapping(
            os.path.join(self._model_dir, 'compartment_map.tsv'))
        self._translated = tr_id.TranslatedModel(self._model,
                                                 self._compound_map,
                                                 self._reaction_map,
                                                 self._compartment_map)