Esempio n. 1
0
 def test_fetch_model_file(self):
     fba_client = fba_tools(self.callback_url)
     model_files = fba_client.model_to_tsv_file({
         'model_name': 'iMR1_799',
         'workspace_name': 'lqw5322:narrative_1515702128212',
         'fulldb': 1
     })
     pprint(model_files)
Esempio n. 2
0
def test_write_fba_model(pathways, db, model_files, workspace_name, res_dir):
    # rxn_df = pd.read_table(data_dir+'/iMR1_799-reactions.tsv')
    # met_df = pd.read_table(data_dir+'/iMR1_799-compounds.tsv')
    rxn_df = pd.read_table(model_files['reactions_file']['path'])
    met_df = pd.read_table(model_files['compounds_file']['path'])

    Sji = db.Sji

    all_rxns_df = pd.DataFrame()
    allReactions = []
    for ind, res in sorted(pathways.iteritems()):
        reaction_ids = res['reaction_id']
        allReactions.extend(reaction_ids)
        rxn_pathway = rxn_df[rxn_df['id'].isin(reaction_ids)]
        rxn_pathway['id'] = rxn_pathway['id'].astype(str) + '_' + str(ind)
        rxn_pathway['reference'] = ind
        all_rxns_df = all_rxns_df.append(rxn_pathway)

    rxn_file = os.path.join(res_dir, "rxn_file.tsv")
    # rxn_file = data_dir+ "/rxn_file.tsv"
    # with open(rxn_file, 'w') as rf:
    #         rf.write(all_rxns_df)
    all_rxns_df.to_csv(rxn_file, index=False, sep='\t')

    all_mets_list = []
    # for r in list(set(all_rxns_df['id'].tolist())):
    for r in list(set(allReactions)):
        if 'EX' in r: continue
        all_mets_list.extend(Sji[r])
    all_mets_list = list(set(all_mets_list))
    met_pathway_df = met_df[met_df['id'].isin(all_mets_list)]

    cpd_file = os.path.join(res_dir, "cpd_file.tsv")
    # cpd_file = data_dir+"/cpd_file.tsv"
    # with open(cpd_file, 'w') as cf:
    #     cf.write(met_pathway_df)
    met_pathway_df.to_csv(cpd_file, index=False, sep='\t')

    # upload those files as a model and get the reference back.
    # see here for details:
    # https://github.com/kbaseapps/fba_tools/blob/master/fba_tools.spec#L524

    callback_url = os.environ['SDK_CALLBACK_URL']
    fba_client = fba_tools(callback_url)
    model_upa = fba_client.tsv_file_to_model({
        'model_file': {
            'path': rxn_file
        },
        'compounds_file': {
            'path': cpd_file
        },
        'workspace_name': workspace_name,  #self.getWsName(),
        'model_name': 'pathways_from_optStoic',
        'biomass': []
    })

    print('UPLOAD A MODEL - GOT UPA')
    print(model_upa['ref'])
Esempio n. 3
0
    def test_write_fba_model(self):
        cpd_tsv = """id\tname\tformula\tcharge\taliases
cf00001\tcf00001\tnone\t0\tnone
cf00002\tcf00002\tnone\t0\tnone
cf00003\tcf00003\tnone\t0\tnone
cpd00001\tH2O\tH2O\t0\tnone
cpd00002\tATP\tC10H13N5O13P3\t-3\tnone
cpd00003\tNAD\tC21H26N7O14P2\t-1\tnone
cpd00004\tNADH\tC21H27N7O14P2\t-2\tnone
cpd00005\tNADPH\tC21H27N7O17P3\t-3\tnone
cpd00006\tNADP\tC21H26N7O17P3\t-2\tnone
cpd00007\tO2\tO2\t0\tnone
cpd00008\tADP\tC10H13N5O10P2\t-2\tnone
cpd00009\tPhosphate\tHO4P\t-2\tnone"""

        rxn_tsv = """id\tdirection\tcompartment\tgpr\tname\tenzyme\tpathway\treference\tequation
pkr0000001\t>\tc0\tnone\tpkr0000001\tnone\tnone\tnone\t(1) cpd00002[c0] => (1) cpd00008[c0] + (1) cpd00009[c0]"""
        # write out to scratch file system
        cpd_file = os.path.join(self.scratch, "cpd_file.tsv")
        with open(cpd_file, 'w') as cf:
            cf.write(cpd_tsv)

        rxn_file = os.path.join(self.scratch, "rxn_file.tsv")
        with open(rxn_file, 'w') as rf:
            rf.write(rxn_tsv)

        # upload those files as a model and get the reference back.
        # see here for details:
        # https://github.com/kbaseapps/fba_tools/blob/master/fba_tools.spec#L524
        fba_client = fba_tools(self.callback_url)
        model_upa = fba_client.tsv_file_to_model({
            'model_file': {
                'path': rxn_file
            },
            'compounds_file': {
                'path': cpd_file
            },
            'workspace_name':
            self.getWsName(),
            'model_name':
            'my_test_fbamodel',
            'biomass': []
        })

        print('UPLOAD A MODEL - GOT UPA')
        print(model_upa['ref'])

        pprint(self.getWsClient().get_objects2(
            {'objects': [{
                'ref': model_upa['ref']
            }]}))
Esempio n. 4
0
    def setUpClass(cls):
        cls.token = environ.get('KB_AUTH_TOKEN', None)
        config_file = environ.get('KB_DEPLOYMENT_CONFIG', None)
        cls.cfg = {}
        config = ConfigParser()
        config.read(config_file)
        for nameval in config.items('kb_uploadmethods'):
            cls.cfg[nameval[0]] = nameval[1]
        authServiceUrl = cls.cfg.get(
            'auth-service-url',
            "https://kbase.us/services/authorization/Sessions/Login")
        auth_client = _KBaseAuth(authServiceUrl)
        cls.user_id = auth_client.get_user(cls.token)
        # WARNING: don't call any logging methods on the context object,
        # it'll result in a NoneType error
        cls.ctx = MethodContext(None)
        cls.ctx.update({
            'token':
            cls.token,
            'user_id':
            cls.user_id,
            'provenance': [{
                'service': 'kb_uploadmethods',
                'method': 'please_never_use_it_in_production',
                'method_params': []
            }],
            'authenticated':
            1
        })
        cls.wsURL = cls.cfg['workspace-url']
        cls.wsClient = workspaceService(cls.wsURL, token=cls.token)
        cls.serviceImpl = kb_uploadmethods(cls.cfg)
        cls.dfu = DataFileUtil(os.environ['SDK_CALLBACK_URL'], token=cls.token)
        cls.gfu = GenomeFileUtil(os.environ['SDK_CALLBACK_URL'],
                                 token=cls.token)
        cls.fba_tools = fba_tools(os.environ['SDK_CALLBACK_URL'],
                                  token=cls.token)
        cls.scratch = cls.cfg['scratch']
        cls.shockURL = cls.cfg['shock-url']

        suffix = int(time.time() * 1000)
        cls.wsName = "test_kb_uploadmethods_phenotype_set" + str(suffix)
        cls.wsClient.create_workspace({'workspace': cls.wsName})

        cls.prepare_data()
Esempio n. 5
0
    def test_input_metDB_from_tsv(self):
        fba_client = fba_tools(self.callback_url)
        model_files = fba_client.model_to_tsv_file({
            'model_name': 'iMR1_799',
            'workspace_name':
            'lqw5322:narrative_1515706033382',  #'lqw5322:narrative_1515702128212',
            'fulldb': 1
        })
        # pprint(model_files)

        mets_tsv = model_files['compounds_file']['path']
        import pandas as pd
        model_df = pd.read_table(mets_tsv, index_col='id')
        # print model_df.head()

        # met_info = {}

        # get elements
        glucose_formula = model_df.loc['cpd00027_c0']['formula']

        # glucose_formula = 'C6H12O'
        import re
        elements = re.findall(r'([A-Z][a-z]*)(\d*)', glucose_formula)
        # for element in elements:
        #     for ele,value in element.iteritems():
        #         met_info[ele] = float(value)
        met_info = dict(elements)
        for key, value in met_info.iteritems():
            if value == '':
                met_info[key] = 1
        print met_info
        # get charge and dG
        met_info['charge'] = model_df.loc['cpd00027_c0']['charge']
        met_info['dGf'] = model_df.loc['cpd00027_c0']['deltag']

        print met_info
    def _run_fba(self, workspace_name, media_id_list, fbamodel_id, myuuid,
                 base_media):
        print('running fba')
        fba_tool_obj = fba_tools(self.callback_url, service_ver='dev')
        new_media_list = []
        for media in media_id_list:
            if media == base_media:
                new_media_list.append(workspace_name + "/" + media)
            else:
                new_media_list.append(workspace_name + "/" + myuuid + "-" +
                                      media)

        fba_tool_obj.run_flux_balance_analysis({
            "max_c_uptake":
            60,  #"max_c_uptake": 6, // previously default is 6 later set to 60
            "workspace": workspace_name,
            "fbamodel_id": fbamodel_id,
            "fba_output_id": fbamodel_id + ".mifba",
            "fbamodel_workspace": workspace_name,
            "media_id_list": new_media_list,
            "target_reaction": "bio1",
            "minimize_flux": 1
        })
        output = self.ws.get_objects2({
            'objects': [{
                'ref': workspace_name + "/" + fbamodel_id + '.mifba'
            }]
        })

        #json.dump(output, open(self.scratch+'/fba.json', 'w'))

        fba = output['data'][0]['data']
        biomass_data = "FBAs,Biomass\n"
        secretion_file = "," + ','.join(media_id_list) + "\n"
        full_secretion_file = "," + ','.join(media_id_list) + "\n"
        full_flux_file = "," + ','.join(media_id_list) + "\n"
        flux_file = "," + ','.join(media_id_list) + "\n"
        objectives = fba['other_objectives']
        for i in range(0, len(objectives)):
            biomass_data = biomass_data + media_id_list[i] + "," + str(
                objectives[i]) + "\n"

        flux_vars = fba['FBAReactionVariables']
        for var in flux_vars:
            id = var['modelreaction_ref'].split("/").pop()
            flux_file = flux_file + id
            full_flux_file = full_flux_file + id
            fluxes = var['other_values']
            for i in range(0, len(objectives)):
                if objectives[i] == 0:
                    full_flux_file = full_flux_file + ",0"
                    flux_file = flux_file + ",0"
                else:
                    full_flux_file = full_flux_file + "," + str(fluxes[i])
                    if abs(fluxes[i]) < 1e-7:
                        flux_file = flux_file + ",0"
                    else:
                        flux_file = flux_file + ",1"
            flux_file = flux_file + "\n"
            full_flux_file = full_flux_file + "\n"

        secretion_vars = fba['FBACompoundVariables']
        for var in secretion_vars:
            id = var['modelcompound_ref'].split("/").pop()
            secretion_file = secretion_file + id
            full_secretion_file = full_secretion_file + id
            fluxes = var['other_values']
            for i in range(0, len(objectives)):
                if objectives[i] == 0:
                    full_secretion_file = full_secretion_file + ",0"
                    secretion_file = secretion_file + ",0"
                else:
                    full_secretion_file = full_secretion_file + "," + str(
                        fluxes[i])
                    if abs(fluxes[i]) < 1e-7:
                        secretion_file = secretion_file + ",0"
                    elif fluxes[i] < 0:
                        secretion_file = secretion_file + ",-1"
                    else:
                        secretion_file = secretion_file + ",1"
            secretion_file = secretion_file + "\n"
            full_secretion_file = full_secretion_file + "\n"

        output_directory = os.path.join(self.scratch, str(uuid.uuid4()))
        self._mkdir_p(output_directory)
        biomass_path = os.path.join(output_directory, 'biomass.csv')
        secretion_path = os.path.join(output_directory, 'secretion.csv')
        flux_path = os.path.join(output_directory, 'flux.csv')
        full_secretion_path = os.path.join(output_directory,
                                           'full_secretion.csv')
        full_flux_path = os.path.join(output_directory, 'full_flux.csv')

        with open(biomass_path, 'w') as biomass_f:
            biomass_f.write(biomass_data)

        with open(secretion_path, 'w') as secretion_f:
            secretion_f.write(secretion_file)

        with open(flux_path, 'w') as flux_f:
            flux_f.write(flux_file)

        with open(full_secretion_path, 'w') as full_secretion_f:
            full_secretion_f.write(full_secretion_file)

        with open(full_flux_path, 'w') as full_flux_f:
            full_flux_f.write(full_flux_file)

        return [
            biomass_path, secretion_path, flux_path, full_secretion_path,
            full_flux_path
        ]
Esempio n. 7
0
 def __init__(self, config):
     self.callback_url = config['SDK_CALLBACK_URL']
     self.token = config['KB_AUTH_TOKEN']
     self.dfu = DataFileUtil(self.callback_url)
     self.fba = fba_tools(self.callback_url)
     self.uploader_utils = UploaderUtil(config)
Esempio n. 8
0
def construct_steadycom(param,mu,config,callback_url):

    model_inputs = param['model_inputs']
    media = param['medium_upa']
    # print media
    # print param.keys()
    workspace_name = param['workspace_name']
    # print "--------------------"
    # print workspace_name
    # print "--------------------"
    ws = workspaceService(config['workspace-url'])
    # dataUtil = DataFileUtil(url=config['workspace-url'])
    # ws_id = dataUtil.ws_name_to_id(workspace_name)

    meida_object = ws.get_objects2({'objects': [{'name': media, 
                                                'workspace': workspace_name}]})
    
    print meida_object
    media_metabolites = meida_object['data'][0]['data']['mediacompounds']
    print media_metabolites
    media_dict = {}
    for met_info in media_metabolites:
        compound_ref = met_info["compound_ref"]
        compound_id = compound_ref.split('/')[-1]
        # print compound_id
        media_dict[compound_id] = met_info["maxFlux"]


    # fetch information of S matrix for each organism k
    # k: index of organism
    # i: index of metabolites
    # j: index of reactions

    fba_client = fba_tools(callback_url)  # or however your callback url is set
                                               # then when you need the files


    S = {} # build S matrix for each FBA model k

    # define sets for steadycom
    reactions = {} # get reaction info for each FBA model k
    reactions_EX = {}
    reactions_biomass = {}

    metabolites = {} # get metaboite info for each FBA model k
    metabolites_EX = {}
    organisms = []

    for model_input in model_inputs:
        model_upa_ref = model_input['model_upa']
        model_id = model_upa_ref.split('/')[1]
        model_upa = ws.get_objects2({'objects': [{'objid': model_id, 
                            'workspace': workspace_name}]})['data'][0]['data']
        files = fba_client.model_to_tsv_file({
            'workspace_name': workspace_name,  # from params
            'model_name': model_upa['name']                     # also params
        })

        # files will have two "File" objects. you should be able to get to them as:
        # files['compounds_file']['path']
        # and
        # files['reactions_file']['path']
        
        # model_file = os.path.join(os.sep, "Users", "wjriehl", "Desktop", "iMR1_799.TSV", "iMR1_799-reactions.tsv")
        
        model_file = files['reactions_file']['path']
        model_df = pd.read_table(model_file)

        Sij,rxns,mets_EX = build_s_matrix(model_df)

        k = model_upa['name']
        organisms.append(k)
        S[k] = Sij
        metabolites[k] = Sij.keys()
        reactions[k] = rxns
        reactions_biomass[k] = 'BIOMASS-Ecoli-core-w-GAM_c0'#model_upa['biomasses'][0].id
        metabolites_EX[k] = list(mets_EX)

    metabolites_com = []
    for k,met_list in metabolites_EX.iteritems():
        metabolites_com = list(set(metabolites_com + met_list))
    # metabolites_com = metabolites_com
    
    reactions_all = []
    for k,rxn_list in reactions.iteritems():
        reactions_all = list(set(reactions_all + rxn_list))
    # reactions_all = set(reactions_all)

    #------- define variables
    X = pulp.LpVariable.dicts("X", organisms,
                              lowBound=0, upBound=1, cat='Continuous')

    # for k in organisms:
    M = 1000
    print "***************"
    for r in reactions_all:
        if "BIOMASS" in r:
            print "found:", r
    print "***************"

    v = pulp.LpVariable.dicts("v", (organisms,reactions_all),
                          lowBound=-M, upBound=M, cat='Continuous')
    vex = pulp.LpVariable.dicts("vex", (organisms,metabolites_com),
                          lowBound=-M, upBound=M, cat='Continuous')
    e = pulp.LpVariable.dicts("e", metabolites_com,
                              lowBound=0, upBound=M, cat='Continuous')

    #------- define LP problem
    lp_prob = pulp.LpProblem("SteadyCom", pulp.LpMaximize)

    #------- define objective function
    lp_prob += pulp.lpSum([X[k] for k in organisms])

    biomass_labels = []
    # define flux balance constraints
    UB = 1000
    LB = -1000
    for k in organisms:
        for i in S[k].keys():
            dot_S_v = pulp.lpSum([S[k][i][j] * v[k][j]
                                  for j in S[k][i].keys()])
            if i in metabolites_EX[k]:
                condition = dot_S_v == vex[k][i]
            else:
                condition = dot_S_v == 0
            lp_prob += condition#, label  

            for j in reactions[k]:
                if j == 'ATPM_c0':
                    lp_prob += v[k][j] <= UB * X[k] #UB[k][j] * X[k]
                    lp_prob += v[k][j] >= 10 * X[k] #LB[k][j] * X[k]
                    continue
                lp_prob += v[k][j] <= UB * X[k] #UB[k][j] * X[k]
                lp_prob += v[k][j] >= LB * X[k] #LB[k][j] * X[k]

    # constraints for medium (joshua: please add it here)
    for i in metabolites_com:
        # some exchange does not actually exist
        for k in organisms:
            if i not in metabolites_EX[k]:
                lp_prob += vex[k][i] == 0

        community_constraint = pulp.lpSum([vex[k][i] for k in organisms])
        if i in media_dict.keys():
            condition_comm = community_constraint - e[i] == -media_dict[i]
        else:
            condition_comm = community_constraint - e[i] == 0
        lp_prob += condition_comm
    return lp_prob,v,X,k,reactions_biomass
Esempio n. 9
0
 def __init__(self, fba_url, ws_url, ctx):
     self.ws_client = Workspace(ws_url, token=ctx['token'])
     self.fba_client = fba_tools(fba_url)
Esempio n. 10
0
def construct_metS(list_of_mets,params,config):
    ws = workspaceService(config['workspace-url'])
    callback_url = os.environ['SDK_CALLBACK_URL']
    fba_client = fba_tools(callback_url, service_ver="beta")

    model_upa_ref = params['model_upa']
    workspace_name = params['workspace_name']

    print model_upa_ref
    
    model_name = model_upa_ref
    if '/'  in model_upa_ref:
        model_name = model_upa_ref.split('/')[1]
    # model_upa = ws.get_objects2({'objects': [{'objid': model_id, 
    #                     'workspace': workspace_name}]})['data'][0]['data']
    # model_name = model_upa['name']

    # model_name = params['model_upa']
    # print model_name

    use_fulldb = 0 # default use just a GSM
    if params['use_heterologous_steps'] == True:
        use_fulldb = 1

    model_files = fba_client.model_to_tsv_file({
    'model_name': model_name, #'iMR1_799',
    'workspace_name': workspace_name, #'lqw5322:narrative_1515706033382'
    'fulldb': use_fulldb
    })
    # pprint(model_files)

    mets_tsv = model_files['compounds_file']['path']
    model_df = pd.read_table(mets_tsv,index_col='id')

    met_S = {}


    for met_id in list_of_mets:
        met_info = {'C': 0,'H': 0,'N': 0,'O': 0,'P': 0,'S': 0,'F': 0,'Cl': 0,\
            'Mg': 0,'Fe': 0,'Se': 0,'Co': 0,'As': 0,'Br': 0,'I': 0,'R': 0,\
            'charge':None,'dGf':None}
        # get elements
        formula = model_df.loc[met_id]['formula']

        print formula
        elements = re.findall(r'([A-Z][a-z]*)(\d*)', formula)
        # for element in elements:
        #     for ele,value in element.iteritems():
        #         met_info[ele] = float(value)
        elements = dict(elements)
        for key, value in elements.iteritems():
            if value == '': 
                met_info[key] = 1
            else:
                met_info[key] = float(value)

        # get charge and dG
        met_info['charge'] = model_df.loc[met_id]['charge']
        met_info['dGf'] = model_df.loc[met_id]['deltag']
        met_S[met_id] = met_info
    return met_S, model_files