Ejemplo n.º 1
0
def run_menescope(draft_sbml, seeds_sbml):
    print('Reading draft network from ', draft_sbml, '...', end='')
    sys.stdout.flush()
    draftnet = sbml.readSBMLnetwork(draft_sbml, 'draft')
    #print(draftnet)
    print('done.')

    print('Reading seeds from ', seeds_sbml, '...', end='')
    sys.stdout.flush()
    seeds = sbml.readSBMLspecies(seeds_sbml, 'seed')
    #print(seeds)
    print('done.')
    #seeds.to_file("seeds.lp")

    print('\nChecking draft network scope ...', end='')
    sys.stdout.flush()
    model = query.get_scope(draftnet, seeds)
    print('done.')
    print(' ', len(model), 'compounds on scope:')
    utils.print_met(model.to_list())
    utils.clean_up()

    return model
Ejemplo n.º 2
0
def run_menecheck(draft_sbml, seeds_sbml, targets_sbml):
    print('Reading draft network from ', draft_sbml, '...', end=' ')
    sys.stdout.flush()
    draftnet = sbml.readSBMLnetwork(draft_sbml, 'draft')
    print('done.')

    print('Reading seeds from ', seeds_sbml, '...', end=' ')
    sys.stdout.flush()
    seeds = sbml.readSBMLspecies(seeds_sbml, 'seed')
    print('done.')

    print('Reading targets from ', targets_sbml, '...', end=' ')
    sys.stdout.flush()
    targets = sbml.readSBMLspecies(targets_sbml, 'target')
    print('done.')

    print('\nChecking draftnet for unproducible targets ...', end=' ')
    sys.stdout.flush()
    model = query.get_unproducible(draftnet, targets, seeds)
    # print(model)
    print('done.')
    #utils.print_met(model.to_list())
    unprod = []
    prod = []
    for a in model:
        if a.pred() == 'unproducible_target':
            unprod.append(a.arg(0))
        elif a.pred() == 'producible_target':
            prod.append(a.arg(0))
    print(str(len(prod)), 'producible targets:')
    print(*prod, sep='\n')
    print('\n')
    print(str(len(unprod)), 'unproducible targets:')
    print(*unprod, sep='\n')

    utils.clean_up()
    return model, unprod, prod
Ejemplo n.º 3
0
                        help="seeds in SBML format",
                        required=True)

    args = parser.parse_args()

    draft_sbml = args.draftnet
    #repair_sbml = args.repairnetwork
    seeds_sbml = args.seeds

    print('Reading draft network from ', draft_sbml, '...', end='')
    sys.stdout.flush()
    draftnet = sbml.readSBMLnetwork(draft_sbml, 'draft')
    #print(draftnet)
    print('done.')

    print('Reading seeds from ', seeds_sbml, '...', end='')
    sys.stdout.flush()
    seeds = sbml.readSBMLspecies(seeds_sbml, 'seed')
    #print(seeds)
    print('done.')
    #seeds.to_file("seeds.lp")

    print('\nChecking draft network scope ...', end='')
    sys.stdout.flush()
    model = query.get_scope(draftnet, seeds)
    print('done.')
    print(' ', len(model), 'compounds on scope:')
    utils.print_met(model.to_list())
    utils.clean_up()
    quit()
Ejemplo n.º 4
0
def run_menecof(draft_sbml,seeds_sbml,targets_sbml,cofactors_txt=None,weights=None,suffix=None,enumeration=None):
    print('Reading draft network from ', draft_sbml, '...', end='')
    sys.stdout.flush()
    draftnet = sbml.readSBMLnetwork(draft_sbml, 'draft')
    #print(draftnet)
    print('done.')

    print('Reading seeds from ', seeds_sbml, '...', end='')
    sys.stdout.flush()
    seeds = sbml.readSBMLspecies(seeds_sbml, 'seed')
    #print(seeds)
    print('done.')
    #seeds.to_file("seeds.lp")

    print('Reading targets from ', targets_sbml, '...', end='')
    sys.stdout.flush()
    targets = sbml.readSBMLspecies(targets_sbml, 'target')
    #print(targets)
    print('done.')
    #targets.to_file("targets.lp")

    if weights and cofactors_txt:
        print('Reading cofactors with weights from ', cofactors_txt, '...', end='')
        sys.stdout.flush()
        with open(cofactors_txt,'r') as f:
            cofactors_list = f.read().splitlines()
        cofactors = TermSet()
        for elem in cofactors_list:
            try:
                data = elem.split('\t')
                if suffix != None:
                    cofactors.add(Term('cofactor', ["\""+convert_to_coded_id(data[0]) + suffix + "\"", data[1]]))
                else:
                    cofactors.add(Term('cofactor', ["\""+convert_to_coded_id(data[0]) + "\"", data[1]]))
            except:
                print('Input cofactor file is not tabulated (at least not on every line)\
                \n Please check the file, maybe you did not mean to use --weight option?\
                \nUnsuitable input file... Quitting program')
                quit()
        #print(cofactors)
        print('done.')
        #cofactors.to_file("cofactors.lp")

    elif cofactors_txt:
        print('Reading cofactors from ', cofactors_txt)
        with open(cofactors_txt,'r') as f:
            cofactors_list = f.read().splitlines()
        cofactors = TermSet()
        for elem in cofactors_list:
            if '\t' in elem:
                print('A tabulated file was given as input cofactors. \
                \nAre you sure you did not mean to use the weight option? \
                \nUnsuitable input file... Quitting program')
                quit()
            if suffix != None:
                cofactors.add(Term('cofactor', ["\""+convert_to_coded_id(elem) + suffix + "\""]))
            else:
                cofactors.add(Term('cofactor', ["\""+convert_to_coded_id(elem) + "\""]))
        #print(cofactors)
        print('done.')
        #cofactors.to_file("cofactors.lp")

    else:
        print('No cofactor file is given as input.')
        print('Research of cofactors will be done in the network itself')
        species_and_weights = sbml.make_weighted_list_of_species(draft_sbml)
        cofactors = TermSet()
        for elem in species_and_weights:
            cofactors.add(Term('cofactor', ["\""+elem+"\"", species_and_weights[elem]]))
        weights = True

    print('\nChecking draft network for unproducible targets before cofactors selection ...', end='')
    sys.stdout.flush()
    model = query.get_unproducible(draftnet, targets, seeds)
    unprod = [p.arg(0) for p in model if p.pred() == 'unproducible_target']
    print('done.')
    print(' ',len(unprod),'unproducible targets:')
    utils.print_met(model.to_list())

    print('\nChecking minimal sets of cofactors to produce all targets ...', end='')
    sys.stdout.flush()
    if weights:
        model = query.get_cofs_weighted(draftnet, targets, seeds, cofactors)
        optimum = model.score
        if len(optimum) == 2:
            # it means that all targets can be produced with the selected cofactors
            optimum = [0] + optimum
        optimum = ','.join(map(str, optimum))
        #print(optimum)

    else:
        model = query.get_cofs(draftnet, targets, seeds, cofactors)
        optimum = model.score
        if len(optimum) == 1:
            # it means that all targets can be produced with the selected cofactors
            optimum = [0] + optimum
        optimum = ','.join(map(str, optimum))
    #print('done.')
    print('Optimum score {}'.format(optimum))
    solumodel = model.to_list()
    unproduced_targets = []
    chosen_cofactors = []
    newly_producible_targets = []
    for p in solumodel:
        if p.pred() == "needed_cof":
            cof = p.arg(0)
            try:
                weight = p.arg(1)
            except:
                weight = None
            chosen_cofactors.append((cof,weight))
        elif p.pred() == "still_unprod":
            tgt = p.arg(0)
            unproduced_targets.append(tgt)
        else:
            newly_producible_targets.append(p.arg(0))
    print('Still '+ str(len(unproduced_targets)) + ' unproducible targets:')
    print(*unproduced_targets, sep='\n')
    print('\nSelected cofactors:')
    for cofactor in chosen_cofactors:
        if cofactor[1] == None:
            print(cofactor[0])
        else:
            print(cofactor[0] + ' (' + cofactor[1] + ')')
    print('\n' + str(len(newly_producible_targets))+' newly producible targets:')
    print(str(newly_producible_targets))


    print('\nIntersection of solutions') # with size', optimum, '
    intersection_model = query.get_intersection_of_optimal_solutions_cof(draftnet, seeds, targets, cofactors, optimum, weights)
    solumodel = intersection_model.to_list()
    intersection_icofactors = []
    for p in solumodel:
        if p.pred() == "needed_cof":
            cof = p.arg(0)
            try:
                weight = p.arg(1)
            except:
                weight = None
            intersection_icofactors.append((cof,weight))
        #print('\nSelected cofactors:')
    for cofactor in intersection_icofactors:
        if cofactor[1] == None:
            print(cofactor[0])
        else:
            print(cofactor[0] + ' (' + cofactor[1] + ')')

    print('\nUnion of solutions') # with size', optimum, '
    union_model = query.get_union_of_optimal_solutions_cof(draftnet, seeds, targets, cofactors, optimum, weights)
    solumodel = union_model.to_list()
    union_icofactors = []
    for p in solumodel:
        if p.pred() == "needed_cof":
            cof = p.arg(0)
            try:
                weight = p.arg(1)
            except:
                weight = None
            union_icofactors.append((cof,weight))
        #print('\nSelected cofactors:')
    for cofactor in union_icofactors:
        if cofactor[1] == None:
            print(cofactor[0])
        else:
            print(cofactor[0] + ' (' + cofactor[1] + ')')

    if enumeration:
        print('\nComputing all completions with size ',optimum)
        models =  query.get_optimal_solutions_cof(draftnet, seeds, targets, cofactors, optimum, weights)
        count = 1
        for model in models:
            print('\nSolution '+str(count)+':')
            count+=1
            ccofactors = []
            solumodel = model.to_list()
            for p in solumodel:
                if p.pred() == "needed_cof":
                    cof = p.arg(0)
                    try:
                        weight = p.arg(1)
                    except:
                        weight = None
                    ccofactors.append((cof,weight))
            #print('\nSelected cofactors:')
            for cofactor in ccofactors:
                if cofactor[1] == None:
                    print(cofactor[0])
                else:
                    print(cofactor[0] + ' (' + cofactor[1] + ')')
        utils.clean_up()
        return models, optimum, union_icofactors, intersection_icofactors, chosen_cofactors, unprod, newly_producible_targets

    utils.clean_up()
    return model, optimum, union_icofactors, intersection_icofactors, chosen_cofactors, unprod, newly_producible_targets
Ejemplo n.º 5
0
def run_menepath(draft_sbml,
                 seeds_sbml,
                 targets_sbml,
                 min_size=None,
                 enumeration=None):
    print('Reading draft network from ', draft_sbml, '...', end='')
    sys.stdout.flush()
    draftnet = sbml.readSBMLnetwork(draft_sbml, 'draft')
    #print(draftnet)
    print('done.')

    print('Reading seeds from ', seeds_sbml, '...', end='')
    sys.stdout.flush()
    seeds = sbml.readSBMLspecies(seeds_sbml, 'seed')
    #print(seeds)
    print('done.')
    #seeds.to_file("seeds.lp")

    print('Reading targets from ', targets_sbml, '...', end='')
    sys.stdout.flush()
    targets = sbml.readSBMLspecies(targets_sbml, 'target')
    #print(targets)
    print('done.')
    #seeds.to_file("targets.lp")

    print('\nChecking network for unproducible targets ...', end=' ')
    sys.stdout.flush()
    model = query.get_unproducible(draftnet, targets, seeds)
    print('done.')
    unproducible_targets = TermSet()
    producible_targets = TermSet()

    #print(model)
    for p in model:
        if p.pred() == "unproducible_target":
            tgt = p.arg(0)
            #print(tgt)
            unproducible_targets.add(Term('unproducible_targets', [tgt]))
        elif p.pred() == "producible_target":
            tgt = p.arg(0)
            producible_targets.add(Term('target', [tgt]))
            #producible_targets = TermSet(producible_targets.union(t))

    print(' ', len(unproducible_targets), 'unproducible targets:')
    utils.print_met(model.to_list())

    #print(producible_targets)
    for t in producible_targets:
        print('\n')
        print(t)
        single_target = TermSet()
        single_target.add(t)

        draftfact = String2TermSet('draft("draft")')
        lp_instance = TermSet(
            draftnet.union(draftfact).union(single_target).union(seeds))

        # one solution, minimal or not, depending on option
        if min_size:
            print(
                '\nComputing one solution of cardinality-minimal production paths for ',
                t)
        else:
            print(
                '\nComputing one solution of production paths for',
                t,
            )
        one_model = query.get_paths(lp_instance, min_size)
        optimum = one_model.score
        print('Solution size ', len(one_model), ' reactions')
        utils.print_met(one_model.to_list())

        # union of solutions
        if min_size:
            print(
                '\nComputing union of cardinality-minimal production paths for ',
                t)
        else:
            print(
                '\nComputing union of production paths for',
                t,
            )
        union = query.get_union_of_paths(lp_instance, optimum, min_size)
        print('Union size ', len(union), ' reactions')
        utils.print_met(union.to_list())

        # intersection of solutions
        if min_size:
            print(
                '\nComputing intersection of cardinality-minimal production paths for ',
                t)
        else:
            print(
                '\nComputing intersection of production paths for',
                t,
            )
        intersection = query.get_intersection_of_paths(lp_instance, optimum,
                                                       min_size)
        print('Intersection size (essential reactions) ', len(intersection),
              ' reactions')
        utils.print_met(intersection.to_list())

        # if wanted, get union of all solutions
        if enumeration:
            if min_size:
                print(
                    '\nComputing all cardinality-minimal production paths for ',
                    t, ' - ', optimum)
            else:
                print('\nComputing all production paths for ', t, ' - ',
                      optimum)

            all_models = query.get_all_paths(lp_instance, optimum, min_size)
            count = 1
            for model in all_models:
                print('\nSolution ' + str(count) + ' of size :' +
                      str(len(model)) + ' reactions:')
                count += 1
                utils.print_met(model.to_list())
            utils.clean_up()
            return all_models, unproducible_targets, one_model, union, intersection

    utils.clean_up()
    return model, unproducible_targets, one_model, union, intersection