def get_annotation_ontology_events(self, ctx, params): """ Retrieves annotation ontology events in a standardized form cleaning up inconsistencies in underlying data :param params: instance of type "GetAnnotationOntologyEventsParams" -> structure: parameter "input_ref" of String, parameter "input_workspace" of String, parameter "query_events" of list of String, parameter "query_genes" of list of String :returns: instance of type "GetAnnotationOntologyEventsOutput" -> structure: parameter "events" of list of type "AnnotationOntologyEvent" -> structure: parameter "event_id" of String, parameter "ontology_id" of String, parameter "method" of String, parameter "method_version" of String, parameter "timestamp" of String, parameter "ontology_terms" of mapping from String to list of type "AnnotationOntologyTerm" (Insert your typespec information here.) -> structure: parameter "term" of String, parameter "modelseed_id" of String, parameter "evidence" of String """ # ctx is the context object # return variables are: output #BEGIN get_annotation_ontology_events self.config['ctx'] = ctx #print(("Input parameters: " + pformat(params))) anno_api = None if self.ws_client == None: if "workspace-url" in params: anno_api = AnnotationOntologyAPI( self.config, workspaceService(params['workspace-url'], token=ctx['token']), self.dfu_client) else: anno_api = AnnotationOntologyAPI( self.config, workspaceService(self.config['workspace-url'], token=ctx['token']), self.dfu_client) else: anno_api = AnnotationOntologyAPI(self.config, self.ws_client, self.dfu_client) output = anno_api.get_annotation_ontology_events(params) #END get_annotation_ontology_events # At some point might do deeper type checking... if not isinstance(output, dict): raise ValueError( 'Method get_annotation_ontology_events return value ' + 'output is not type dict as required.') # return the results return [output]
def setUpClass(cls): token = environ.get('KB_AUTH_TOKEN', None) user_id = requests.post( 'https://kbase.us/services/authorization/Sessions/Login', data='token={}&fields=user_id'.format(token)).json()['user_id'] # 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': token, 'user_id': user_id, 'provenance': [ {'service': 'kb_virsorter', 'method': 'please_never_use_it_in_production', 'method_params': [] }], 'authenticated': 1}) config_file = environ.get('KB_DEPLOYMENT_CONFIG', None) cls.cfg = {} config = ConfigParser() config.read(config_file) for nameval in config.items('kb_virsorter'): cls.cfg[nameval[0]] = nameval[1] cls.wsURL = cls.cfg['workspace-url'] cls.wsClient = workspaceService(cls.wsURL, token=token) cls.serviceImpl = kb_virsorter(cls.cfg) cls.testobjref = [] #cls.testobjdata = [] cls.testwsname = []
def setUpClass(cls): config_file = environ.get("KB_DEPLOYMENT_CONFIG", None) cls.cfg = {} config = ConfigParser() config.read(config_file) for nameval in config.items("kb_prokka"): cls.cfg[nameval[0]] = nameval[1] # Token validation token = environ.get("KB_AUTH_TOKEN", None) authServiceUrl = cls.cfg.get( "auth-service-url", "https://kbase.us/services/authorization/Sessions/Login") auth_client = _KBaseAuth(authServiceUrl) user_id = auth_client.get_user(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": token, "user_id": user_id, "provenance": [{ "service": "ProkkaAnnotation", "method": "please_never_use_it_in_production", "method_params": [] }], "authenticated": 1 }) cls.wsURL = cls.cfg["workspace-url"] cls.wsClient = workspaceService(cls.wsURL, token=token) cls.serviceImpl = kb_prokka(cls.cfg)
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('NarrativeIndexer'): cls.cfg[nameval[0]] = nameval[1] # Getting username from Auth profile for token # authServiceUrl = cls.cfg['auth-service-url'] # auth_client = _KBaseAuth(authServiceUrl) # 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.wsURL = cls.cfg['workspace-url'] cls.wsClient = workspaceService(cls.wsURL) cls.scratch = cls.cfg['scratch'] cls.callback_url = os.environ['SDK_CALLBACK_URL'] cls.cfg['token'] = cls.token cls.wsid = 16962 cls.upa = '16962/3' cls.test_dir = os.path.dirname(os.path.abspath(__file__)) cls.mock_dir = os.path.join(cls.test_dir, 'mock_data') cls.wsinfo = cls.read_mock('get_workspace_info.json') # [16962, u'scanon:narrative_1485560571814', u'scanon', # u'2018-10-18T00:12:42+0000', 25, u'a', u'n', # u'unlocked', # {u'is_temporary': u'false', u'narrative': u'23', # u'narrative_nice_name': u'RNASeq Analysis - Jose', # u'data_palette_id': u'22'}] cls.narobj = cls.read_mock('narrative_object.json')
def setUpClass(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_mash'): cls.cfg[nameval[0]] = nameval[1] # Getting username from Auth profile for token # authServiceUrl = cls.cfg['auth-service-url'] # auth_client = _KBaseAuth(authServiceUrl) # user_id = auth_client.get_user(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': token, # 'user_id': user_id, 'provenance': [{ 'service': 'kb_mash', 'method': 'please_never_use_it_in_production', 'method_params': [] }], 'authenticated': 1 }) cls.wsURL = cls.cfg['workspace-url'] cls.wsClient = workspaceService(cls.wsURL) # suffix = int(time.time() * 1000) # wsName = "test_kb_mash_" + str(suffix) # cls.ws_info = cls.wsClient.create_workspace({'workspace': wsName}) cls.serviceImpl = kb_mash(cls.cfg) cls.scratch = cls.cfg['scratch'] cls.callback_url = os.environ['SDK_CALLBACK_URL']
def setUpClass(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_assembly_compare'): cls.cfg[nameval[0]] = nameval[1] # Getting username from Auth profile for token authServiceUrl = cls.cfg['auth-service-url'] auth_client = _KBaseAuth(authServiceUrl) user_id = auth_client.get_user(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': token, 'user_id': user_id, 'provenance': [{ 'service': 'kb_assembly_compare', 'method': 'please_never_use_it_in_production', 'method_params': [] }], 'authenticated': 1 }) cls.wsURL = cls.cfg['workspace-url'] cls.wsClient = workspaceService(cls.wsURL) cls.serviceImpl = kb_assembly_compare(cls.cfg) cls.callback_url = os.environ['SDK_CALLBACK_URL'] cls.scratch = cls.cfg['scratch'] if not os.path.exists(cls.scratch): os.makedirs(cls.scratch)
def setUpClass(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_phylogenomics'): cls.cfg[nameval[0]] = nameval[1] authServiceUrl = cls.cfg.get( 'auth-service-url', "https://kbase.us/services/authorization/Sessions/Login") auth_client = _KBaseAuth(authServiceUrl) user_id = auth_client.get_user(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': token, 'user_id': user_id, 'provenance': [{ 'service': 'kb_phylogenomics', 'method': 'please_never_use_it_in_production', 'method_params': [] }], 'authenticated': 1 }) cls.wsURL = cls.cfg['workspace-url'] cls.wsClient = workspaceService(cls.wsURL, token=token) cls.serviceImpl = kb_phylogenomics(cls.cfg)
def __init__(self, config): #BEGIN_CONSTRUCTOR self.config = config self.config['SDK_CALLBACK_URL'] = os.environ['SDK_CALLBACK_URL'] self.config['KB_AUTH_TOKEN'] = os.environ['KB_AUTH_TOKEN'] self.ws_client = workspaceService(config["workspace-url"]) #END_CONSTRUCTOR pass
def setUpClass(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('SetAPI'): cls.cfg[nameval[0]] = nameval[1] authServiceUrl = cls.cfg.get( "auth-service-url", "https://kbase.us/services/authorization/Sessions/Login") auth_client = _KBaseAuth(authServiceUrl) user_id = auth_client.get_user(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': token, 'user_id': user_id, 'provenance': [{ 'service': 'SetAPI', 'method': 'please_never_use_it_in_production', 'method_params': [] }], 'authenticated': 1 }) cls.wsURL = cls.cfg['workspace-url'] cls.wsClient = workspaceService(cls.wsURL, token=token) cls.serviceImpl = SetAPI(cls.cfg) # setup data at the class level for now (so that the code is run # once for all tests, not before each test case. Not sure how to # do that outside this function..) suffix = int(time.time() * 1000) wsName = "test_SetAPI_" + str(suffix) cls.wsClient.create_workspace({'workspace': wsName}) cls.wsName = wsName foft = FakeObjectsForTests(os.environ['SDK_CALLBACK_URL']) # Make fake genomes [fake_genome, fake_genome2] = foft.create_fake_genomes({ "ws_name": wsName, "obj_names": ["fake_genome", "fake_genome2"] }) cls.genome_refs = [info_to_ref(fake_genome), info_to_ref(fake_genome2)] # Make some fake feature sets cls.featureset_refs = [ make_fake_feature_set("feature_set_{}".format(i), cls.genome_refs[0], wsName, cls.wsClient) for i in range(3) ]
def setUpClass(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_Msuite'): cls.cfg[nameval[0]] = nameval[1] # Getting username from Auth profile for token authServiceUrl = cls.cfg['auth-service-url'] auth_client = _KBaseAuth(authServiceUrl) user_id = auth_client.get_user(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': token, 'user_id': user_id, 'provenance': [{ 'service': 'kb_Msuite', 'method': 'please_never_use_it_in_production', 'method_params': [] }], 'authenticated': 1 }) cls.wsURL = cls.cfg['workspace-url'] cls.wsClient = workspaceService(cls.wsURL) cls.serviceImpl = kb_Msuite(cls.cfg) cls.scratch = cls.cfg['scratch'] cls.callback_url = os.environ['SDK_CALLBACK_URL'] cls.checkm_runner = CheckMUtil(cls.cfg, cls.ctx) suffix = int(time.time() * 1000) cls.wsName = "test_kb_Msuite_" + str(suffix) cls.ws_info = cls.wsClient.create_workspace({'workspace': cls.wsName}) cls.au = AssemblyUtil(os.environ['SDK_CALLBACK_URL']) cls.setAPI = SetAPI(url=cls.cfg['srv-wiz-url'], token=cls.ctx['token']) cls.gfu = GenomeFileUtil(os.environ['SDK_CALLBACK_URL'], service_ver='dev') cls.mu = MetagenomeUtils(os.environ['SDK_CALLBACK_URL']) # stage an input and output directory """ cls.input_dir = os.path.join(cls.scratch, 'input_1') cls.output_dir = os.path.join(cls.scratch, 'output_1') cls.all_seq_fasta = os.path.join(cls.scratch, 'all_seq.fna') shutil.copytree(os.path.join('data', 'example_out', 'input'), cls.input_dir) shutil.copytree(os.path.join('data', 'example_out', 'output'), cls.output_dir) shutil.copy(os.path.join('data', 'example_out', 'all_seq.fna'), cls.all_seq_fasta) """ # prepare WS data cls.prepare_data()
def __init__(self, config): self.ws_url = config["workspace-url"] self.callback_url = config['SDK_CALLBACK_URL'] self.token = config['KB_AUTH_TOKEN'] self.shock_url = config['shock-url'] self.srv_wiz_url = config['srv-wiz-url'] self.scratch = config['scratch'] self.dfu = DataFileUtil(self.callback_url) self.wsClient = workspaceService(self.ws_url, token=self.token) self.cu = ConditionUtils(self.callback_url, service_ver="dev")
def setUpClass(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('SetAPI'): cls.cfg[nameval[0]] = nameval[1] authServiceUrl = cls.cfg.get( 'auth-service-url', "https://kbase.us/services/authorization/Sessions/Login") auth_client = _KBaseAuth(authServiceUrl) user_id = auth_client.get_user(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': token, 'user_id': user_id, 'provenance': [{ 'service': 'SetAPI', 'method': 'please_never_use_it_in_production', 'method_params': [] }], 'authenticated': 1 }) cls.wsURL = cls.cfg['workspace-url'] cls.wsClient = workspaceService(cls.wsURL, token=token) cls.serviceImpl = SetAPI(cls.cfg) cls.serviceWizardURL = cls.cfg['service-wizard'] cls.dataPaletteServiceVersion = cls.cfg['datapaletteservice-version'] # setup data at the class level for now (so that the code is run # once for all tests, not before each test case. Not sure how to # do that outside this function..) suffix = int(time.time() * 1000) wsName = "test_SetAPI_" + str(suffix) ret = cls.wsClient.create_workspace({'workspace': wsName}) cls.wsName = wsName foft = FakeObjectsForTests(os.environ['SDK_CALLBACK_URL']) [info1, info2] = foft.create_fake_reads({ 'ws_name': wsName, 'obj_names': ['reads1', 'reads2'] }) cls.read1ref = str(info1[6]) + '/' + str(info1[0]) + '/' + str( info1[4]) cls.read2ref = str(info2[6]) + '/' + str(info2[0]) + '/' + str( info2[4])
def __init__(self, config): #BEGIN_CONSTRUCTOR self.callback_url = os.environ['SDK_CALLBACK_URL'] self.shared_folder = config['scratch'] logging.basicConfig(format='%(created)s %(levelname)s: %(message)s', level=logging.INFO) self.config = config self.config['SDK_CALLBACK_URL'] = os.environ['SDK_CALLBACK_URL'] self.config['KB_AUTH_TOKEN'] = os.environ['KB_AUTH_TOKEN'] self.ws_client = workspaceService(config["workspace-url"]) #END_CONSTRUCTOR pass
def __init__(self, config): #BEGIN_CONSTRUCTOR self.config = config self.ws_client = None self.dfu_client = None if 'SDK_CALLBACK_URL' in os.environ: self.config['SDK_CALLBACK_URL'] = os.environ['SDK_CALLBACK_URL'] self.dfu_client = DataFileUtil(self.config['SDK_CALLBACK_URL']) self.config['KB_AUTH_TOKEN'] = os.environ['KB_AUTH_TOKEN'] self.ws_client = workspaceService(config["workspace-url"]) #END_CONSTRUCTOR pass
def run_virsorter(self, ctx, params): """ Identify viral sequences in microbial reads :param params: instance of type "VirsorterParams" -> structure: parameter "assembly_ref" of String, parameter "genome_ref" of String :returns: instance of type "VirsorterResults" -> structure: parameter "report_name" of String, parameter "report_ref" of String """ # ctx is the context object # return variables are: returnVal #BEGIN run_virsorter if 'assembly_ref' not in params and 'genome_ref' not in params: raise ValueError('Parameter assembly_ref or genome_ref is not set in input arguments') elif 'assembly_ref' in params: assemblyRef = params['assembly_ref'] #elif 'genome_ref' in params: # genomeRef = params['genome_ref'] if 'ws_name' not in params: raise ValueError('Parameter ws_name is not set in input arguments') else: wsName = params['ws_name'] token = ctx['token'] wsClient = workspaceService(self.workspaceURL, token=token) #headers = {'Authorization': 'OAuth ' + token} uuid_string = str(uuid.uuid4()) file_path = self.scratch + "/" + uuid_string os.mkdir(file_path) if assemblyRef: report_name, report_ref = self.do_assembly(assemblyRef, wsName) #elif genomeRef: # report_name, report_ref = self.do_genome(genomeRef, file_path, wsName, wsClient) output = {'report_name': report_name, 'report_ref': report_ref } # END run_kb_virsorter # At some point might do deeper type checking... if not isinstance(output, dict): raise ValueError('Method run_SPAdes return value ' + 'output is not type dict as required.') # return the results return [output]
def run_virsorter(self, ctx, params): """ Identify viral sequences in microbial reads :param params: instance of type "VirsorterParams" -> structure: parameter "assembly_ref" of String, parameter "genome_ref" of String :returns: instance of type "VirsorterResults" -> structure: parameter "report_name" of String, parameter "report_ref" of String """ # ctx is the context object # return variables are: returnVal #BEGIN run_virsorter if 'assembly_ref' not in params and 'genome_ref' not in params: raise ValueError( 'Parameter assembly_ref or genome_ref is not set in input arguments' ) elif 'assembly_ref' in params: assemblyRef = params['assembly_ref'] #elif 'genome_ref' in params: # genomeRef = params['genome_ref'] if 'ws_name' not in params: raise ValueError('Parameter ws_name is not set in input arguments') else: wsName = params['ws_name'] token = ctx['token'] wsClient = workspaceService(self.workspaceURL, token=token) #headers = {'Authorization': 'OAuth ' + token} uuid_string = str(uuid.uuid4()) file_path = self.scratch + "/" + uuid_string os.mkdir(file_path) if assemblyRef: report_name, report_ref = self.do_assembly(assemblyRef, wsName) #elif genomeRef: # report_name, report_ref = self.do_genome(genomeRef, file_path, wsName, wsClient) output = {'report_name': report_name, 'report_ref': report_ref} # END run_kb_virsorter # At some point might do deeper type checking... if not isinstance(output, dict): raise ValueError('Method run_SPAdes return value ' + 'output is not type dict as required.') # return the results return [output]
def __init__(self, config): self.scratch = config["scratch"] self.ctx = config['ctx']; self.callback_url = config["SDK_CALLBACK_URL"] self.ws_client = workspaceService(config["workspace-url"]) self.gfu = GenomeFileUtil(self.callback_url) self.au = AssemblyUtil(self.callback_url) self.kbr = KBaseReport(self.callback_url) self.dfu = DataFileUtil(self.callback_url) self.genome_api = GenomeAnnotationAPI(self.callback_url) self.sso_ref = None self.sso_event = None self.ec_to_sso = {} self.output_workspace = None
def setUpClass(cls): token = environ.get('KB_AUTH_TOKEN', None) cls.ctx = {'token': token} cls.token = token config_file = environ.get('KB_DEPLOYMENT_CONFIG', None) cls.cfg = {} config = ConfigParser() config.read(config_file) for nameval in config.items('MEGAHIT'): print(nameval[0] + '=' + nameval[1]) cls.cfg[nameval[0]] = nameval[1] cls.wsURL = cls.cfg['workspace-url'] cls.ws = workspaceService(cls.wsURL, token=token) cls.serviceImpl = MEGAHIT(cls.cfg) cls.shockURL = cls.cfg['shock-url'] cls.handleURL = cls.cfg['handle-service-url']
def setUpClass(cls): token = environ.get('KB_AUTH_TOKEN', None) cls.ctx = {'token': token, 'provenance': [{'service': 'AssemblyRAST', 'method': 'please_never_use_it_in_production', 'method_params': []}], 'authenticated': 1} config_file = environ.get('KB_DEPLOYMENT_CONFIG', None) cls.cfg = {} config = ConfigParser() config.read(config_file) for nameval in config.items('AssemblyRAST'): cls.cfg[nameval[0]] = nameval[1] cls.wsURL = cls.cfg['workspace-url'] cls.ws = workspaceService(cls.wsURL, token=token) cls.serviceImpl = AssemblyRAST(cls.cfg) cls.shockURL = cls.cfg['shock-url'] cls.handleURL = cls.cfg['handle-service-url'] logger.info('shockURL='+cls.shockURL)
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('NarrativeIndexer'): cls.cfg[nameval[0]] = nameval[1] # Getting username from Auth profile for token # authServiceUrl = cls.cfg['auth-service-url'] # auth_client = _KBaseAuth(authServiceUrl) # 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.wsURL = cls.cfg['workspace-url'] cls.wsClient = workspaceService(cls.wsURL) cls.scratch = cls.cfg['scratch'] cls.callback_url = os.environ['SDK_CALLBACK_URL'] cls.cfg['token'] = cls.token cls.wsid = 16962
def setUpClass(cls): token = environ.get('KB_AUTH_TOKEN', None) cls.token = token cls.ctx = {'token': token, 'provenance': [{'service': 'kb_ObjectUtilities', 'method': 'please_never_use_it_in_production', 'method_params': []}], 'authenticated': 1} config_file = environ.get('KB_DEPLOYMENT_CONFIG', None) cls.cfg = {} config = ConfigParser() config.read(config_file) for nameval in config.items('kb_ObjectUtilities'): print(nameval[0] + '=' + nameval[1]) cls.cfg[nameval[0]] = nameval[1] cls.wsURL = cls.cfg['workspace-url'] cls.shockURL = cls.cfg['shock-url'] cls.handleURL = cls.cfg['handle-service-url'] cls.serviceWizardURL = cls.cfg['service-wizard-url'] cls.callbackURL = os.environ['SDK_CALLBACK_URL'] cls.wsClient = workspaceService(cls.wsURL, token=token) cls.serviceImpl = kb_ObjectUtilities(cls.cfg) cls.scratch = os.path.abspath(cls.cfg['scratch']) if not os.path.exists(cls.scratch): os.makedirs(cls.scratch)
def setUpClass(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('FeatureSetUtils'): cls.cfg[nameval[0]] = nameval[1] # Getting username from Auth profile for token authServiceUrl = cls.cfg['auth-service-url'] auth_client = _KBaseAuth(authServiceUrl) user_id = auth_client.get_user(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': token, 'user_id': user_id, 'provenance': [ {'service': 'FeatureSetUtils', 'method': 'please_never_use_it_in_production', 'method_params': [] }], 'authenticated': 1}) cls.wsURL = cls.cfg['workspace-url'] cls.wsClient = workspaceService(cls.wsURL) cls.serviceImpl = FeatureSetUtils(cls.cfg) cls.scratch = cls.cfg['scratch'] cls.callback_url = os.environ['SDK_CALLBACK_URL'] suffix = int(time.time() * 1000) cls.wsName = "test_kb_featureset_util_" + str(suffix) cls.wsClient.create_workspace({'workspace': cls.wsName}) cls.gfu = GenomeFileUtil(cls.callback_url) cls.dfu = DataFileUtil(cls.callback_url) cls.prepare_data()
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
def setUpClass(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('SetAPI'): cls.cfg[nameval[0]] = nameval[1] authServiceUrl = cls.cfg.get( "auth-service-url", "https://kbase.us/services/authorization/Sessions/Login") auth_client = _KBaseAuth(authServiceUrl) user_id = auth_client.get_user(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': token, 'user_id': user_id, 'provenance': [{ 'service': 'SetAPI', 'method': 'please_never_use_it_in_production', 'method_params': [] }], 'authenticated': 1 }) cls.wsURL = cls.cfg['workspace-url'] cls.wsClient = workspaceService(cls.wsURL, token=token) cls.serviceImpl = SetAPI(cls.cfg) # setup data at the class level for now (so that the code is run # once for all tests, not before each test case. Not sure how to # do that outside this function..) suffix = int(time.time() * 1000) wsName = "test_SetAPI_" + str(suffix) cls.wsClient.create_workspace({'workspace': wsName}) cls.wsName = wsName foft = FakeObjectsForTests(os.environ['SDK_CALLBACK_URL']) # Make a fake genome [fake_genome, fake_genome2] = foft.create_fake_genomes({ "ws_name": wsName, "obj_names": ["fake_genome", "fake_genome2"] }) cls.genome_refs = [info_to_ref(fake_genome), info_to_ref(fake_genome2)] # Make some fake reads objects fake_reads_list = foft.create_fake_reads({ 'ws_name': wsName, "obj_names": ["reads1", "reads2", "reads3"] }) cls.alignment_refs = list() cls.reads_refs = list() # Make some fake alignments referencing those reads and genome dummy_filename = "dummy.txt" cls.dummy_path = os.path.join(cls.cfg['scratch'], dummy_filename) shutil.copy(os.path.join("data", dummy_filename), cls.dummy_path) for idx, reads_info in enumerate(fake_reads_list): reads_ref = info_to_ref(reads_info) cls.reads_refs.append(reads_ref) cls.alignment_refs.append( make_fake_alignment(os.environ['SDK_CALLBACK_URL'], cls.dummy_path, "fake_alignment_{}".format(idx), reads_ref, cls.genome_refs[0], wsName, cls.wsClient)) # Need a fake annotation to get the expression objects cls.annotation_ref = make_fake_annotation( os.environ['SDK_CALLBACK_URL'], cls.dummy_path, "fake_annotation", wsName, cls.wsClient) # Now we can phony up some expression objects to build sets out of. # name, genome_ref, annotation_ref, alignment_ref, ws_name, ws_client cls.expression_refs = list() for idx, alignment_ref in enumerate(cls.alignment_refs): cls.expression_refs.append( make_fake_expression(os.environ['SDK_CALLBACK_URL'], cls.dummy_path, "fake_expression_{}".format(idx), cls.genome_refs[0], cls.annotation_ref, alignment_ref, wsName, cls.wsClient)) # Make a fake RNASeq Alignment Set object # Make a fake RNASeqSampleSet cls.sampleset_ref = make_fake_sampleset("fake_sampleset", [], [], wsName, cls.wsClient) # Finally, make a couple fake RNASeqAlignmentSts objects from those alignments cls.fake_rnaseq_alignment_set = make_fake_old_alignment_set( "fake_rnaseq_alignment_set", cls.reads_refs, cls.genome_refs[0], cls.sampleset_ref, cls.alignment_refs, wsName, cls.wsClient) # Make a fake RNASeq Expression Set object cls.fake_rnaseq_expression_set = make_fake_old_expression_set( "fake_rnaseq_expression_set", cls.genome_refs[0], cls.sampleset_ref, cls.alignment_refs, cls.fake_rnaseq_alignment_set, cls.expression_refs, wsName, cls.wsClient, True)
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
def setUpClass(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('SetAPI'): cls.cfg[nameval[0]] = nameval[1] authServiceUrl = cls.cfg.get( 'auth-service-url', "https://kbase.us/services/authorization/Sessions/Login") auth_client = _KBaseAuth(authServiceUrl) user_id = auth_client.get_user(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': token, 'user_id': user_id, 'provenance': [{ 'service': 'SetAPI', 'method': 'please_never_use_it_in_production', 'method_params': [] }], 'authenticated': 1 }) cls.wsURL = cls.cfg['workspace-url'] cls.wsClient = workspaceService(cls.wsURL, token=token) cls.serviceImpl = SetAPI(cls.cfg) # setup data at the class level for now (so that the code is run # once for all tests, not before each test case. Not sure how to # do that outside this function..) suffix = int(time.time() * 1000) wsName = "test_SetAPI_" + str(suffix) ret = cls.wsClient.create_workspace({'workspace': wsName}) # wsName = 'pranjan77:1477441032423' cls.wsName = wsName # copy test file to scratch area fna_filename = "seq.fna" fna_path = os.path.join(cls.cfg['scratch'], fna_filename) shutil.copy(os.path.join("data", fna_filename), fna_path) ru = AssemblyUtil(os.environ['SDK_CALLBACK_URL']) ws_obj_name = 'MyNewAssembly' cls.assembly1ref = ru.save_assembly_from_fasta({ 'file': { 'path': fna_path }, 'workspace_name': wsName, 'assembly_name': 'assembly_obj_1' }) cls.assembly2ref = ru.save_assembly_from_fasta({ 'file': { 'path': fna_path }, 'workspace_name': wsName, 'assembly_name': 'assembly_obj_2' })
def KButil_Concat_MSAs(self, ctx, params): """ :param params: instance of type "KButil_Concat_MSAs_Params" (KButil_Concat_MSAs() ** ** Method for Concatenating MSAs into a combined MSA) -> structure: parameter "workspace_name" of type "workspace_name" (** The workspace object refs are of form: ** ** objects = ws.get_objects([{'ref': params['workspace_id']+'/'+params['obj_name']}]) ** ** "ref" means the entire name combining the workspace id and the object name ** "id" is a numerical identifier of the workspace or object, and should just be used for workspace ** "name" is a string identifier of a workspace or object. This is received from Narrative.), parameter "input_refs" of type "data_obj_ref", parameter "output_name" of type "data_obj_name", parameter "desc" of String, parameter "blanks_flag" of type "bool" :returns: instance of type "KButil_Concat_MSAs_Output" -> structure: parameter "report_name" of type "data_obj_name", parameter "report_ref" of type "data_obj_ref" """ # ctx is the context object # return variables are: returnVal #BEGIN KButil_Concat_MSAs console = [] invalid_msgs = [] self.log(console, 'Running KButil_Concat_MSAs with params=') self.log(console, "\n" + pformat(params)) report = '' # report = 'Running KButil_Concat_MSAs with params=' # report += "\n"+pformat(params) #### do some basic checks # if 'workspace_name' not in params: raise ValueError('workspace_name parameter is required') if 'desc' not in params: raise ValueError('desc parameter is required') if 'input_refs' not in params: raise ValueError('input_refs parameter is required') if 'output_name' not in params: raise ValueError('output_name parameter is required') # clean input_refs clean_input_refs = [] for ref in params['input_refs']: if ref != None and ref != '': clean_input_refs.append(ref) params['input_refs'] = clean_input_refs if len(params['input_refs']) < 2: self.log(console, "Must provide at least two MSAs") self.log(invalid_msgs, "Must provide at least two MSAs") # Build FeatureSet # row_order = [] alignment = {} curr_pos = 0 MSA_seen = {} discard_set = {} sequence_type = None for MSA_i, MSA_ref in enumerate(params['input_refs']): if len(params['input_refs']) < 2: # too lazy to reindent the block continue if not MSA_ref in MSA_seen.keys(): MSA_seen[MSA_ref] = True else: self.log(console, "repeat MSA_ref: '" + MSA_ref + "'") self.log(invalid_msgs, "repeat MSA_ref: '" + MSA_ref + "'") continue try: ws = workspaceService(self.workspaceURL, token=ctx['token']) #objects = ws.get_objects([{'ref': MSA_ref}]) objects = ws.get_objects2({'objects': [{ 'ref': MSA_ref }]})['data'] data = objects[0]['data'] info = objects[0]['info'] # Object Info Contents # absolute ref = info[6] + '/' + info[0] + '/' + info[4] # 0 - obj_id objid # 1 - obj_name name # 2 - type_string type # 3 - timestamp save_date # 4 - int version # 5 - username saved_by # 6 - ws_id wsid # 7 - ws_name workspace # 8 - string chsum # 9 - int size # 10 - usermeta meta type_name = info[2].split('.')[1].split('-')[0] except Exception as e: raise ValueError( 'Unable to fetch input_ref object from workspace: ' + str(e)) #to get the full stack trace: traceback.format_exc() if type_name != 'MSA': raise ValueError("Bad Type: Should be MSA instead of '" + type_name + "'") this_MSA = data this_genomes_seen = {} # set sequence_type try: this_sequence_type = this_MSA['sequence_type'] if sequence_type == None: sequence_type = this_sequence_type elif this_sequence_type != sequence_type: self.log( invalid_msgs, "inconsistent sequence type for MSA " + MSA_ref + " '" + this_sequence_type + "' doesn't match '" + sequence_type + "'") continue except: pass # build row_order this_row_order = [] if 'row_order' in this_MSA.keys(): #self.log(console,"IN row_order A") # DEBUG this_row_order = this_MSA['row_order'] else: #self.log(console,"IN row_order B") # DEBUG this_row_order = sorted(this_MSA['alignment'].keys()) # DEBUG #for row_id in this_row_order: # self.log(console,"ROW_ORDER_ID: '"+row_id+"'") #for row_id in sorted(this_MSA['alignment']): # self.log(console,"ALIGNMENT_ID: '"+row_id+"'") # concat alignments using base genome_id to unify (input rows are probably gene ids) this_aln_len = len(this_MSA['alignment'][this_row_order[0]]) genome_row_ids_updated = {} for row_id in this_row_order: id_pieces = re.split('\.', row_id) if len(id_pieces) >= 2: genome_id = ".".join(id_pieces[0:2]) # just want genome_id else: genome_id = row_id # can't have repeat genome_ids (i.e. no paralogs allowed) try: genome_id_seen = this_genomes_seen[genome_id] self.log( console, "only one feature per genome permitted in a given MSA. MSA: " + MSA_ref + " genome_id: " + genome_id + " row_id: " + row_id) self.log( invalid_msgs, "only one feature per genome permitted in a given MSA. MSA: " + MSA_ref + " genome_id: " + genome_id + " row_id: " + row_id) continue except: this_genomes_seen[genome_id] = True this_row_len = len(this_MSA['alignment'][row_id]) if this_row_len != this_aln_len: self.log( invalid_msgs, "inconsistent alignment len in " + MSA_ref + ": first_row_len=" + str(this_aln_len) + " != " + str(this_row_len) + " (" + row_id + ")") continue # create new rows if genome_id not in alignment.keys(): row_order.append(genome_id) alignment[genome_id] = '' if MSA_i > 0: #self.log(console,"NOT IN OLD MSA: "+genome_id) # DEBUG discard_set[genome_id] = True alignment[genome_id] += ''.join( ['-' for s in range(curr_pos)]) #else: # DEBUG #self.log(console,"SEEN IN MSA: "+genome_id) # DEBUG # add new genome_row_ids_updated[genome_id] = True alignment[genome_id] += this_MSA['alignment'][row_id] # append blanks for rows that weren't in new MSA if MSA_i > 0: for genome_id in alignment.keys(): try: updated = genome_row_ids_updated[genome_id] except: #self.log(console,"NOT IN NEW MSA: "+genome_id) # DEBUG discard_set[genome_id] = True alignment[genome_id] += ''.join( ['-' for s in range(this_aln_len)]) # update curr_pos curr_pos += this_aln_len # report if len(invalid_msgs) == 0: report += 'num rows in input set ' + MSA_ref + ': ' + str( len(this_row_order)) + " " + str(this_row_order) + "\n" self.log( console, 'num rows in input set ' + MSA_ref + ': ' + str(len(this_row_order))) self.log( console, 'row_ids in input set ' + MSA_ref + ': ' + str(this_row_order)) # report which are incomplete rows (regardless of whether discarding) if len(invalid_msgs) == 0: for genome_id in row_order: try: discard = discard_set[genome_id] self.log(console, 'incomplete row: ' + genome_id) report += 'incomplete row: ' + genome_id + "\n" except: self.log(console, 'complete row: ' + genome_id) report += 'complete row: ' + genome_id + "\n" # remove incomplete rows if not adding blanks if 'blanks_flag' in params and params[ 'blanks_flag'] != None and params['blanks_flag'] == 0: new_row_order = [] new_alignment = {} for genome_id in row_order: try: discard = discard_set[genome_id] self.log(console, 'discarding row: ' + genome_id + "\n") report += 'discarding row: ' + genome_id except: new_row_order.append(genome_id) new_alignment[genome_id] = alignment[genome_id] row_order = new_row_order alignment = new_alignment # report which rows are retained for genome_id in row_order: self.log(console, 'output MSA contains row: ' + genome_id) report += 'output MSA contains row: ' + genome_id + "\n" # load the method provenance from the context object # self.log(console, "SETTING PROVENANCE") # DEBUG provenance = [{}] if 'provenance' in ctx: provenance = ctx['provenance'] # add additional info to provenance here, in this case the input data object reference provenance[0]['input_ws_objects'] = [] for MSA_ref in params['input_refs']: provenance[0]['input_ws_objects'].append(MSA_ref) provenance[0]['service'] = 'kb_ObjectUtilities' provenance[0]['method'] = 'KButil_Concat_MSAs' # DEBUG: check alignment and row_order #for genome_id in row_order: # self.log(console,"AFTER ROW_ORDER: "+genome_id) #for genome_id in alignment.keys(): # self.log(console,"AFTER ALIGNMENT: "+genome_id+",\t"+alignment[genome_id]) # Store output object # if len(invalid_msgs) == 0: self.log(console, "SAVING OUTPUT MSA") # DEBUG output_MSA = { 'name': params['output_name'], 'description': params['desc'], 'row_order': row_order, 'alignment': alignment, 'alignment_length': len(alignment[row_order[0]]) } if sequence_type != None: output_MSA['sequence_type'] = sequence_type new_obj_info = ws.save_objects({ 'workspace': params['workspace_name'], 'objects': [{ 'type': 'KBaseTrees.MSA', 'data': output_MSA, 'name': params['output_name'], 'meta': {}, 'provenance': provenance }] }) # build output report object # self.log(console, "BUILDING REPORT") # DEBUG if len(invalid_msgs) == 0: self.log( console, "rows in output MSA " + params['output_name'] + ": " + str(len(row_order))) report += 'rows in output MSA ' + params[ 'output_name'] + ': ' + str(len(row_order)) + "\n" reportObj = { 'objects_created': [{ 'ref': params['workspace_name'] + '/' + params['output_name'], 'description': 'KButil_Concat_MSAs' }], 'text_message': report } else: report += "FAILURE:\n\n" + "\n".join(invalid_msgs) + "\n" reportObj = {'objects_created': [], 'text_message': report} reportName = 'kb_ObjectUtilities_concat_msas_report_' + str( uuid.uuid4()) ws = workspaceService(self.workspaceURL, token=ctx['token']) report_obj_info = ws.save_objects({ # 'id':info[6], 'workspace': params['workspace_name'], 'objects': [{ 'type': 'KBaseReport.Report', 'data': reportObj, 'name': reportName, 'meta': {}, 'hidden': 1, 'provenance': provenance }] })[0] # Build report and return # self.log(console, "BUILDING RETURN OBJECT") returnVal = { 'report_name': reportName, 'report_ref': str(report_obj_info[6]) + '/' + str(report_obj_info[0]) + '/' + str(report_obj_info[4]), } self.log(console, "KButil_Concat_MSAs DONE") #END KButil_Concat_MSAs # At some point might do deeper type checking... if not isinstance(returnVal, dict): raise ValueError('Method KButil_Concat_MSAs return value ' + 'returnVal is not type dict as required.') # return the results return [returnVal]