Ejemplo n.º 1
0
    def get_probanno(self, ctx, input):
        # ctx is the context object
        # return variables are: output
        #BEGIN get_probanno
        ''' Convert a probabilistic annotation object into a human-readbable table.

            @param ctx Current context object
            @param input Dictionary with input parameters for function
            @return Dictionary keyed by gene to a list of tuples with roleset and likelihood
            @raise WrongVersionError when ProbAnno object version number is invalid
        '''

        input = self._checkInputArguments(ctx, input,
                                          ['probanno', 'probanno_workspace'],
                                          { 'probanno_version': None }
                                          )

        wsClient = Workspace(self.config["workspace_url"], token=ctx['token'])
        probAnnoObjectId = make_object_identity(input["probanno_workspace"], input["probanno"], input['probanno_version'])
        objectList = wsClient.get_objects( [ probAnnoObjectId ] )
        probAnnoObject = objectList[0]
        if probAnnoObject['info'][2] != ProbAnnoType:
            message = 'ProbAnno object type %s is not %s for object %s' %(probAnnoObject['info'][2], ProbAnnoType, probAnnoObject['info'][1])
            ctx.log_err(message)
            raise WrongVersionError(message)
        output = probAnnoObject["data"]["roleset_probabilities"]

        #END get_probanno

        # At some point might do deeper type checking...
        if not isinstance(output, dict):
            raise ValueError('Method get_probanno return value ' +
                             'output is not type dict as required.')
        # return the results
        return [output]
Ejemplo n.º 2
0
def get_genome(genome_id=None,
               workspace_id=None,
               token=None,
               workspace_url=None):
    #download genome object from workspace
    if workspace_url is None:
        workspace_url = 'https://kbase.us/services/ws/'
    if token is None:
        with open('/kb/dev_container/modules/genome_util/mytoken.txt'
                  ) as token_file:
            token = token_file.read()
        token = token.rstrip()

    #print token
    #print genome_id
    #print workspace_id

    workspace_client = Workspace(url=workspace_url, token=token)
    #genome=workspace_client.get_object({'id':genome_id, 'workspace':workspace_id, 'type':'KBaseGenomes.Genome'})
    genome = workspace_client.get_object({
        'id': 'Bifidobacterium_animalis_subsp._lactis_AD011',
        'type': 'KBaseGenomes.Genome',
        'workspace': 'plane83:1436884411390'
    })

    return genome
    def BuildFastaFromSequenceSet(self, ctx, params):
        """
        :param params: instance of type "BuildSeqIn" -> structure: parameter
           "workspace_name" of String, parameter "SequenceSetRef" of String,
           parameter "fasta_outpath" of String
        :returns: instance of type "BuildSeqOut" -> structure: parameter
           "fasta_outpath" of String
        """
        # ctx is the context object
        # return variables are: output
        #BEGIN BuildFastaFromSequenceSet
        #END BuildFastaFromSequenceSet

        # At some point might do deeper type checking...
        dfu = DataFileUtil(self.callback_url)

        bu = BackgroundUtils()
        TU = TestUtils()
        if params['TESTFLAG'] and params['background']:
            targetpath = '/kb/module/work/tmp/testgenome.fa'
            TU.GetGenome(targetpath)
            bu.BuildBackground(targetpath)
        elif params['background']:

            ws = Workspace('https://appdev.kbase.us/services/ws')
            subset = ws.get_object_subset([{
                                         'included':['/features/[*]/location', '/features/[*]/id','/assembly_ref'],
    'ref':params['genome_ref']}])
            aref = subset[0]['data']['assembly_ref']
            assembly_ref = {'ref': aref}
            print('Downloading Assembly data as a Fasta file.')
            assemblyUtil = AssemblyUtil(self.callback_url)
            fasta_file = assemblyUtil.get_assembly_as_fasta(assembly_ref)['path']
            bu.BuildBackground(fasta_file)


        get_objects_params = {'object_refs' : [params['SequenceSetRef']]}

        SeqSet = dfu.get_objects(get_objects_params)['data'][0]['data']
        outFile = open(params['fasta_outpath'],'w')
        for s in SeqSet['sequences']:
            sname = '>' + s['sequence_id'] + '\n'
            outFile.write(sname)
            sseq = s['sequence'] + '\n'
            outFile.write(sseq)
        outFile.close()

        fu=FastaUtils()
        if params['mask_repeats']:
            fu.RemoveRepeats(params['fasta_outpath'],params['fasta_outpath'])

        output = {'fasta_outpath' : params['fasta_outpath']}
        #END BuildFastaFromSequenceSet

        # At some point might do deeper type checking...
        if not isinstance(output, dict):
            raise ValueError('Method BuildFastaFromSequenceSet return value ' +
                             'output is not type dict as required.')
        # return the results
        return [output]
def download_workspace_data(ws_url, source_ws, source_obj, working_dir,
                            logger):
    ws = Workspace(ws_url, token=TOKEN)
    objdata = ws.get_objects([{'ref': source_ws + '/' + source_obj}])[0]
    info = objdata['info']
    if info[2].split('-')[0] != 'KBaseFile.AssemblyFile':
        raise ValueError(
            'This method only works on the KBaseFile.AssemblyFile type')
    shock_url = objdata['data']['assembly_file']['file']['url']
    shock_id = objdata['data']['assembly_file']['file']['id']
    ref = str(info[6]) + '/' + str(info[0]) + '/' + str(info[4])
    source = objdata['data'].get('source')

    outfile = os.path.join(working_dir, source_obj)
    shock_node = shock_url + '/node/' + shock_id + '/?download'
    headers = {'Authorization': 'OAuth ' + TOKEN}
    with open(outfile, 'w') as f:
        response = requests.get(shock_node, stream=True, headers=headers)
        if not response.ok:
            try:
                err = json.loads(response.content)['error'][0]
            except:
                logger.error("Couldn't parse response error content: " +
                             response.content)
                response.raise_for_status()
            raise Exception(str(err))
        for block in response.iter_content(1024):
            if not block:
                break
            f.write(block)

    return shock_url, shock_id, ref, source
Ejemplo n.º 5
0
def get_object_from_ref(ref):
    objid = int(ref.split("/")[1])
    WS_URL = "https://ci.kbase.us/services/ws/"
    from biokbase.workspace.client import Workspace

    ws = Workspace(WS_URL)
    return ws.get_objects([dict(workspace=os.environ["KB_WORKSPACE_ID"], objid=objid)])[0]["data"]
Ejemplo n.º 6
0
    def __init__(self,
                 url=None,
                 token=None,
                 user_id=None,
                 password=None,
                 wsid=None,
                 create=False,
                 **create_kw):
        """Constructor.

        :param url: URL of remote WS service
        :type url: str
        :param token: Authorization token, overrides 'user_id'/'password'
        :type token: str
        :param user_id: User ID for authentication (overridden by 'token')
        :type user_id: str
        :param user_id: Password for authentication (overridden by 'token')
        :type user_id: str
        :param wsid: Workspace ID or name
        :type wsid: str
        :param create_kw: Any extra keywords to create a new workspace
        :type create_kw: None or dict
        :raise: ValueError if workspace id is empty, KeyError if there is no workspace by that name,
                WorkspaceException if creation of the workspace fails.
        """
        WS2.__init__(self,
                     url=url,
                     user_id=user_id,
                     password=password,
                     token=token)
        self.has_wsid = False
        if wsid is not None:
            self._init_ws(wsid, create_kw)
            self.has_wsid = True
Ejemplo n.º 7
0
def get_object_uid(name):
    WS_URL = "https://ci.kbase.us/services/ws/"
    from biokbase.workspace.client import Workspace

    ws = Workspace(WS_URL)
    info = ws.get_objects([dict(workspace=os.environ["KB_WORKSPACE_ID"], name=name)])[0]["info"]
    return "%s/%s/%s" % (info[6], info[0], info[4])
Ejemplo n.º 8
0
 def associateReads(self, ctx, params):
     # ctx is the context object
     # return variables are: returnVal
     # BEGIN associateReads
     user_token = ctx["token"]
     ws_client = Workspace(url=self.__WS_URL, token=user_token)
     out = dict()
     out["metadata"] = {
         k: v
         for k, v in params.iteritems()
         if not k in ("ws_id", "analysis_id", "genome_id", "singleend_sample", "pairedend_sample") and v is not None
     }
     self.__LOGGER.info("Uploading RNASeqSample {0}".format(out["metadata"]["sample_id"]))
     if "genome_id" in params and params["genome_id"] is not None:
         out["metadata"]["genome_id"] = script_util.get_obj_info(
             self.__LOGGER, self.__WS_URL, [params["genome_id"]], params["ws_id"], user_token
         )[0]
     if "analysis_id" in params and params["analysis_id"] is not None:
         g_ref = script_util.get_obj_info(
             self.__LOGGER, self.__WS_URL, [params["analysis_id"]], params["ws_id"], user_token
         )[0]
         out["analysis_id"] = g_ref
     if "singleend_sample" in params and params["singleend_sample"] is not None:
         try:
             s_res = ws_client.get_objects([{"name": params["singleend_sample"], "workspace": params["ws_id"]}])
             out["singleend_sample"] = s_res[0]["data"]
             print out["singleend_sample"]
         except Exception, e:
             raise KBaseRNASeqException(
                 "Error Downloading SingleEndlibrary object from the workspace {0},{1}".format(
                     params["singleend_sample"], e
                 )
             )
def delete_narrative(ws_id, auth_token, url=ci_ws):
    """
    Deletes a workspace with the given id. Throws a ServerError if the user given
    by auth_token isn't allowed to do so.
    """
    ws_client = Workspace(url=url, token=auth_token.token)
    ws_client.delete_workspace({'id': ws_id})
Ejemplo n.º 10
0
def get_object_uid(name):
    WS_URL = 'https://ci.kbase.us/services/ws/'
    from biokbase.workspace.client import Workspace
    ws = Workspace(WS_URL)
    info = ws.get_objects(
        [dict(workspace=os.environ['KB_WORKSPACE_ID'], name=name)])[0]['info']
    return '%s/%s/%s' % (info[6], info[0], info[4])
Ejemplo n.º 11
0
def delete_narrative(ws_id, auth_token, url=ci_ws):
    """
    Deletes a workspace with the given id. Throws a ServerError if the user given
    by auth_token isn't allowed to do so.
    """
    ws_client = Workspace(url=url, token=auth_token)
    ws_client.delete_workspace({"id": ws_id})
Ejemplo n.º 12
0
    def TophatCall(self, ctx, params):
        # ctx is the context object
        # return variables are: returnVal
        # BEGIN TophatCall
        ws_client = Workspace(url=self.__WS_URL, token=user_token)
        hs = HandleService(url=self.__HS_URL, token=user_token)
        try:
            ### Make a function to download the workspace object  and prepare dict of genome ,lib_type

            self.__LOGGER.info("Downloading RNASeq Sample file")
            try:
                ret = ws_client.get_objects(
                    [
                        {"name": params["sample_id"], "workspace": params["ws_id"]},
                        {"name": params["reference"], "workspace": params["ws_id"]},
                        {"name": params["bowtie_index"], "workspace": params["ws_id"]},
                        {"name": params["annotation_gtf"], "workspace": params["ws_id"]},
                    ]
                )
            except Exception, e:
                raise KBaseRNASeqException("Error Downloading objects from the workspace ")

                # Download reads from the JSON object
            genome = params["reference"]
            if "data" in reads:
                # if 'metadata' in reads['data']:
                # genome = reads['data']['metadata']['ref_genome']
                if "singleend_sample" in reads["data"]:
                    lib_type = "SingleEnd"
                    # cmdstring =
                elif "pairedend_sample" in reads["data"]:
                    lib_type = "PairedEnd"
Ejemplo n.º 13
0
def download_workspace_data(ws_url, source_ws, source_obj, working_dir,
                            logger):
    ws = Workspace(ws_url, token=TOKEN)
    objdata = ws.get_objects([{'ref': source_ws + '/' + source_obj}])[0]
    info = objdata['info']
    if info[2].split('-')[0] != 'KBaseFile.AssemblyFile':
        raise ValueError(
            'This method only works on the KBaseFile.AssemblyFile type')
    shock_url = objdata['data']['assembly_file']['file']['url']
    shock_id = objdata['data']['assembly_file']['file']['id']
    ref = str(info[6]) + '/' + str(info[0]) + '/' + str(info[4])
    source = objdata['data'].get('source')

    outfile = os.path.join(working_dir, source_obj)
    shock_node = shock_url + '/node/' + shock_id + '/?download'
    headers = {'Authorization': 'OAuth ' + TOKEN}
    with open(outfile, 'w') as f:
        response = requests.get(shock_node, stream=True, headers=headers)
        if not response.ok:
            try:
                err = json.loads(response.content)['error'][0]
            except:
                logger.error("Couldn't parse response error content: " +
                             response.content)
                response.raise_for_status()
            raise Exception(str(err))
        for block in response.iter_content(1024):
            if not block:
                break
            f.write(block)

    return shock_url, shock_id, ref, source
Ejemplo n.º 14
0
def get_object_from_ref(ref):
    objid = int(ref.split('/')[1])
    WS_URL = 'https://ci.kbase.us/services/ws/'
    from biokbase.workspace.client import Workspace
    ws = Workspace(WS_URL)
    return ws.get_objects(
        [dict(workspace=os.environ['KB_WORKSPACE_ID'],
              objid=objid)])[0]['data']
Ejemplo n.º 15
0
    def create(self, ctx, params):
        # ctx is the context object
        # return variables are: info
        #BEGIN create

        print('Creating KBase Report.')

        # check that the basic parameters are set
        if 'report' not in params:
            raise ValueError('Field "report" must be defined to save a report')
        if 'workspace_name' not in params:
            raise ValueError(
                'Field "workspace_name" must be defined to save a report')

        # setup proper provenance for the report
        provenance = [{}]
        if 'provenance' in ctx:
            provenance = ctx['provenance']

        # generate a random report name
        reportName = 'report_' + str(uuid.uuid4())
        if 'prefix' in params:
            reportName = params['prefix'] + reportName

        print('Report Name' + reportName)

        # let any workspace errors just percolate up for now
        ws = Workspace(self.workspaceURL, token=ctx['token'])
        report_info = ws.save_objects({
            'workspace':
            params['workspace_name'],
            'objects': [{
                'type': 'KBaseReport.Report',
                'data': params['report'],
                'name': reportName,
                'meta': {},
                'hidden': 1,
                'provenance': provenance
            }]
        })[0]

        info = {
            'ref':
            str(report_info[6]) + '/' + str(report_info[0]) + '/' +
            str(report_info[4]),
            'name':
            report_info[1]
        }

        #END create

        # At some point might do deeper type checking...
        if not isinstance(info, dict):
            raise ValueError('Method create return value ' +
                             'info is not type dict as required.')
        # return the results
        return [info]
Ejemplo n.º 16
0
def main():
    admin_token = create_user(mini_ws_admin)
    admin_ws = Workspace(url=mini_ws_url, token=admin_token)
    versions = load_narrative_type(admin_ws)
    versions = {"old_ver": "1.0", "new_ver": "2.0"}

    user_token = create_user(test_user)
    user_ws = Workspace(url=mini_ws_url, token=user_token)
    loaded_info = load_narrative_test_data(user_ws, versions)
    pprint(loaded_info)
Ejemplo n.º 17
0
    def create(self, ctx, params):
        # ctx is the context object
        # return variables are: info
        #BEGIN create

        print('Creating KBase Report.')

        # check that the basic parameters are set
        if 'report' not in params:
            raise ValueError('Field "report" must be defined to save a report')
        if 'workspace_name' not in params:
            raise ValueError('Field "workspace_name" must be defined to save a report')

        # setup proper provenance for the report
        provenance = [{}]
        if 'provenance' in ctx:
            provenance = ctx['provenance']

        # generate a random report name
        reportName = 'report_'+str(uuid.uuid4())
        if 'prefix' in params:
            reportName = params['prefix'] + reportName


        print('Report Name' + reportName)

        # let any workspace errors just percolate up for now
        ws = Workspace(self.workspaceURL, token=ctx['token'])
        report_info = ws.save_objects({
                'workspace':params['workspace_name'],
                'objects':[
                    {
                        'type':'KBaseReport.Report',
                        'data':params['report'],
                        'name':reportName,
                        'meta':{},
                        'hidden':1,
                        'provenance':provenance
                    }
                ]
            })[0]

        info = {
            'ref'  : str(report_info[6]) + '/' + str(report_info[0]) + '/' + str(report_info[4]),
            'name' : report_info[1]
        }

        #END create

        # At some point might do deeper type checking...
        if not isinstance(info, dict):
            raise ValueError('Method create return value ' +
                             'info is not type dict as required.')
        # return the results
        return [info]
Ejemplo n.º 18
0
def upload_narrative(nar_file, auth_token, user_id, url=ci_ws, set_public=False):
    """
    Uploads a Narrative from a downloaded object file.
    This file needs to be in JSON format, and it expects all
    data and info that is usually returned by the Workspace.get_objects
    method.

    Returns a dict of three elements:
        ws: the id of the workspace that was created
        obj: the id of the narrative object
        ref: the above two joined together into an object ref (for convenience)
    """

    # read the file
    f = open(nar_file, 'r')
    nar = json.loads(f.read())
    f.close()

    # do some setup.
    current_nar_metadata = ws_metadata
    current_nar_metadata['narrative_nice_name'] = nar['data']['metadata']['name']
    ws_client = Workspace(url=url, token=auth_token)

    # create the new workspace for the narrative
    ws_info = ws_client.create_workspace({
        'workspace': '{}:{}'.format(user_id, str(time.time()).replace('.', '')),
        'meta': current_nar_metadata,
        'globalread': 'r' if set_public else 'n'
    })
    ws_id = ws_info[0]

    # setup and save the narrative object
    metadata = nar['info'][10]
    ws_save_obj = {
        'type': 'KBaseNarrative.Narrative',
        'data': nar['data'],
        'name': nar['info'][1],
        'meta': nar['info'][10],
        'provenance': [{
            'script': 'upload_narrative_test.py',
            'description': 'Temporary Narrative uploaded for automated testing'
        }]
    }
    obj_info = ws_client.save_objects({'id': ws_id,
                                       'objects': [ws_save_obj]})

    # tweak the workspace's metadata to properly present its narrative
    ws_client.alter_workspace_metadata({'wsi': {'id': ws_id}, 'new': {'narrative': obj_info[0][0]}})
    return {
        'ws': ws_info[0],
        'obj': obj_info[0][0],
        'refstr': '{}/{}'.format(ws_info[0], obj_info[0][0]),
        'ref': NarrativeRef({'wsid': ws_info[0], 'objid': obj_info[0][0]})
    }
    def test_annotate(self):
        ''' Run pa-annotate on a valid Genome object and verify that the job runs and returns a valid ProbAnno object in the expected time.'''

        # Run the annotate() function to generate a ProbAnno object.
        paClient = ProbabilisticAnnotation(self._config["probanno_url"],
                                           token=self._token)
        jobid = paClient.annotate({
            "genome": self._config["genomeid"],
            "genome_workspace": self._config["test_ws"],
            "probanno": self._config["probannoid"],
            "probanno_workspace": self._config["test_ws"]
        })

        # Allow time for the command to run.
        time.sleep(float(self._config["runtime"]))

        # Make sure the job has completed.
        ujsClient = UserAndJobState(self._config['ujs_url'], token=self._token)
        jobList = ujsClient.list_jobs([self._config['test_user']], 'CE')
        jobCompleted = False
        for job in jobList:
            if jobid == job[0]:
                jobCompleted = True
                jobInfo = job
        self.assertTrue(
            jobCompleted, 'Job did not complete before timeout of %s seconds' %
            (self._config['runtime']))

        # See if the job ended in error.
        details = ''
        if jobInfo[11] == 1:
            details = ujsClient.get_detailed_error(jobInfo[0])
        self.assertEqual(jobInfo[11], 0, 'Job ended in error: %s' % (details))

        # Look for the ProbAnno object in the test workspace.
        wsClient = Workspace(self._config["workspace_url"], token=self._token)
        try:
            probannoObjectId = {
                'workspace': self._config['test_ws'],
                'name': self._config['probannoid']
            }
            objectList = wsClient.get_objects([probannoObjectId])
            probannoObject = objectList[0]
            self.assertEqual(
                probannoObject['info'][1], self._config['probannoid'],
                'ProbAnno object id %s is not %s' %
                (probannoObject['info'][1], self._config['probannoid']))
        except WorkspaceServerError as e:
            traceback.print_exc(file=sys.stderr)
            self.fail(
                msg=
                "The expected object %s did not get created in the workspace %s!\n"
                % (self._config["probannoid"], self._config["test_ws"]))
Ejemplo n.º 20
0
def get_obj_info(logger,ws_url,objects,ws_id,token):
    """
    function to get the workspace object id from a object name
    """
    ret = []
    ws_client=Workspace(url=ws_url, token=token)
    for obj in  objects:
    	try:
            obj_infos = ws_client.get_object_info_new({"objects": [{'name': obj, 'workspace': ws_id}]})
            ret.append("{0}/{1}/{2}".format(obj_infos[0][6],obj_infos[0][0],obj_infos[0][4]))
        except Exception, e:
                     logger.error("Couldn't retrieve %s:%s from the workspace , %s " %(ws_id,obj,e))
Ejemplo n.º 21
0
def handler (args) :
    ###
    # download ws object and convert them to csv
    wsd = Workspace(url=args.ws_url, token=os.environ.get('KB_AUTH_TOKEN'))
    indata = wsd.get_object({'id' : args.inobj_id,
                  #'type' : 'KBaseExpression.ExpressionSeries', 
                  'workspace' : args.ws_id})['data']

    if indata is None:
        raise Exception("Object {} not found in workspace {}".format(args.inobj_id, args.ws_id))


    ###
    # execute filtering
    flt_cmd_lst = ['mys_example', "-i", "{}-{}".format(os.getpid(),args.exp_fn) ]
    if (args.method     is not None): 
        flt_cmd_lst.append('-m')
        flt_cmd_lst.append(args.method)
    if (args.p_value    is not None): 
        flt_cmd_lst.append('-p')
        flt_cmd_lst.append(args.p_value)
    if (args.num_genes  is not None): 
        flt_cmd_lst.append('-n')
        flt_cmd_lst.append(args.num_genes)
    if (args.flt_out_fn is not None): 
        flt_cmd_lst.append('-o')
        flt_cmd_lst.append("{}-{}".format(os.getpid(),args.flt_out_fn))

    p1 = Popen(flt_cmd_lst, stdout=PIPE)
    out_str = p1.communicate()
    # print output message for error tracking
    if out_str[0] is not None : print out_str[0]
    if out_str[1] is not None : print >> sys.stderr, out_str[1]
    flt_cmd = " ".join(flt_cmd_lst)
   
    ###
    # put it back to workspace
    #fif = open("{}-{}".format(os.getpid(),args.flt_out_fn), 'r')
    #fif.readline(); # skip header
    
    # assume only one genome id
    outdata = {}
    outdata['key'] = indata['key']
    outdata['value'] = "{}{}".format(indata['value'], indata['value'])
    data_list = []
    data_list.append({'type' : 'MyService.PairString', 'data' : outdata, 'name' : args.outobj_id, 'meta' : {'org.series' : args.inobj_id}})
    wsd.save_objects({'workspace' : args.ws_id, 'objects' : data_list})

    if(args.del_tmps is "true") :
        os.remove("{}-{}".format(os.getpid(), args.exp_fn))
        os.remove("{}-{}".format(os.getpid(), args.flt_out_fn))
Ejemplo n.º 22
0
    def __init__(self, ws_url, auth_url, token):
        """
        The data fetcher needs a workspace client and auth client.
        It needs Auth to get the current user id out of the token, so we know what workspaces
        are actually shared as opposed to just visible.

        Args:
            ws_url (str): Workspace service URL
            auth_url (str): Auth service URL
            token (str): auth token
        """
        self._ws = Workspace(url=ws_url, token=token)
        auth = KBaseAuth(auth_url=auth_url)
        self._user = auth.get_user(token)
Ejemplo n.º 23
0
 def test_handles(self):
     wsName = self.generatePesudoRandomWorkspaceName()
     self.ws.set_permissions({
         'workspace': wsName,
         'new_permission': 'w',
         'users': [self.ctx2['user_id']]
     })
     temp_shock_file = "/kb/module/work/tmp/shock1.txt"
     with open(temp_shock_file, "w") as f1:
         f1.write("Test Shock Handle")
     token1 = self.ctx['token']
     dfu = DataFileUtil(os.environ['SDK_CALLBACK_URL'], token=token1)
     handle1 = dfu.file_to_shock({
         'file_path': temp_shock_file,
         'make_handle': 1
     })['handle']
     hid1 = handle1['hid']
     genome_name = "Genome.1"
     ws2 = Workspace(self.cfg['workspace-url'], token=token1)
     ws2.save_objects({
         'workspace':
         wsName,
         'objects': [{
             'name': genome_name,
             'type': 'KBaseGenomes.Genome',
             'data': {
                 'id': "qwerty",
                 'scientific_name': "Qwerty",
                 'domain': "Bacteria",
                 'genetic_code': 11,
                 'genbank_handle_ref': hid1
             }
         }]
     })
     genome = self.impl.get_genome_v1(
         self.ctx2, {'genomes': [{
             'ref': wsName + '/' + genome_name
         }]})[0]['genomes'][0]['data']
     self.impl.save_one_genome_v1(self.ctx2, {
         'workspace': wsName,
         'name': genome_name,
         'data': genome
     })[0]
     genome = self.impl.get_genome_v1(
         self.ctx2, {'genomes': [{
             'ref': wsName + '/' + genome_name
         }]})[0]['genomes'][0]['data']
     self.assertTrue('genbank_handle_ref' in genome)
     hid2 = genome['genbank_handle_ref']
     self.assertNotEqual(hid1, hid2)
    def export_genome_annotation_as_genbank(self, ctx, params):
        """
        A method designed especially for download, this calls 'genome_annotation_to_genbank' to do
        the work, but then packages the output with WS provenance and object info into
        a zip file and saves to shock.
        :param params: instance of type "ExportParams" -> structure:
           parameter "input_ref" of String
        :returns: instance of type "ExportOutput" -> structure: parameter
           "shock_id" of String
        """
        # ctx is the context object
        # return variables are: output
        #BEGIN export_genome_annotation_as_genbank

         # validate parameters
        if 'input_ref' not in params:
            raise ValueError('Cannot export GenomeAnnotation- not input_ref field defined.')

        # get WS metadata to get ws_name and obj_name
        ws = Workspace(url=self.workspaceURL)
        info = ws.get_object_info_new({'objects':[{'ref': params['input_ref'] }],'includeMetadata':0, 'ignoreErrors':0})[0]

        # export to a file
        file = self.genome_annotation_to_genbank(ctx, { 
                            'genome_ref': params['input_ref'], 
                            'new_genbank_file_name': info[1]+'.gbk' })[0]

        # create the output directory and move the file there
        export_package_dir = os.path.join(self.sharedFolder, info[1])
        os.makedirs(export_package_dir)
        shutil.move(file['path'], os.path.join(export_package_dir, os.path.basename(file['path'])))

        # package it up and be done
        dfUtil = DataFileUtil(self.callback_url)
        package_details = dfUtil.package_for_download({
                                    'file_path': export_package_dir,
                                    'ws_refs': [ params['input_ref'] ]
                                })

        output = { 'shock_id': package_details['shock_id'] }

        #END export_genome_annotation_as_genbank

        # At some point might do deeper type checking...
        if not isinstance(output, dict):
            raise ValueError('Method export_genome_annotation_as_genbank return value ' +
                             'output is not type dict as required.')
        # return the results
        return [output]
 def test_cleanup(self):
     ''' Cleanup objects created by tests. '''
     
     # Delete all of the objects in the test workspace.
     wsClient = Workspace(self._config["workspace_url"], token=self._token)
     listObjectsParams = dict()
     listObjectsParams['workspaces'] = [ self._config['test_ws'] ]
     objectList = wsClient.list_objects(listObjectsParams)
     deleteList = list()
     for object in objectList:
         deleteList.append( { 'wsid': object[6], 'objid': object[0], 'ver': object[4] })
     wsClient.delete_objects(deleteList)
     objectList = wsClient.list_objects(listObjectsParams)
     for object in objectList:
         print 'After delete Object %s %s' %(object[1], object[4])
Ejemplo n.º 26
0
    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_cufflinks'):
            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_cufflinks',
                             'method': 'please_never_use_it_in_production',
                             'method_params': []
                             }],
                        'authenticated': 1})
        cls.wsURL = cls.cfg['workspace-url']
        cls.wsClient = Workspace(url=cls.wsURL, token=token)
        cls.serviceImpl = kb_cufflinks(cls.cfg)
        cls.scratch = cls.cfg['scratch']
        cls.callback_url = environ.get('SDK_CALLBACK_URL')
        cls.srv_wiz_url = cls.cfg['srv-wiz-url']

        # cls.wsName = 'cufflinks_test_' + user_id  # reuse existing workspace
        suffix = int(time.time() * 1000)
        cls.wsName = "test_kb_cufflinks_" + str(suffix)
        print('workspace_name: ' + cls.wsName)

        try:
            # reuse existing (previously torn down) workspace
            cls.wsClient.undelete_workspace({'workspace': cls.wsName})
            print('reusing old workspace...')
        except BaseException:
            try:
                # create if workspace does not exist
                cls.wsClient.create_workspace({'workspace': cls.wsName})
            except BaseException:
                # get workspace if it exists and was not previously deleted (previously
                # not torn down)
                ws_info = cls.wsClient.get_workspace_info({'workspace': cls.wsName})
                print("creating new workspace: " + str(ws_info))

        cls.dfu = DataFileUtil(cls.callback_url)

        cls.gfu = GenomeFileUtil(cls.callback_url)
        cls.ru = ReadsUtils(cls.callback_url)
        cls.rau = ReadsAlignmentUtils(cls.callback_url)
        cls.set_api = SetAPI(cls.srv_wiz_url, service_ver='dev')

        cls.cufflinks_runner = CufflinksUtils(cls.cfg)

        cls.prepare_data()
Ejemplo n.º 27
0
def get_genome(genome_id=None,workspace_id=None,token=None,workspace_url=None):
	#download genome object from workspace
	if workspace_url is None:
		workspace_url='https://ci.kbase.us/services/ws'

	
	#print token
	#print genome_id
	#print workspace_id
	
	workspace_client=Workspace(url=workspace_url, token=token)
	genome=workspace_client.get_object({'id':genome_id, 'workspace':workspace_id, 'type':'KBaseGenomes.Genome'})
	#genome = workspace_client.get_object({'id' : 'Bifidobacterium_animalis_subsp._lactis_AD011', 'type' : 'KBaseGenomes.Genome',  'workspace' : 'plane83:1436884411390'})
	

	return genome
Ejemplo n.º 28
0
def main():

    endpoint = 'prod'

    ws = Workspace(WS_URL[endpoint])
    target_dir = os.path.abspath('../specs')
    repo = Repo(os.path.abspath('../'))

    # create the manifest
    manifest = Manifest(os.path.abspath('../manifest.json'))
    manifest.set_ws_url(WS_URL[endpoint])

    modules = [
        'DataPalette', 'GenomeComparison', 'KBaseAssembly', 'KBaseBiochem',
        'KBaseCommon', 'KBaseFBA', 'KBaseFeatureValues', 'KBaseFile',
        'KBaseGeneFamilies', 'KBaseGenomeAnnotations', 'KBaseGenomes',
        'KBaseNarrative', 'KBaseOntology', 'KBasePhenotypes', 'KBaseRNASeq',
        'KBaseSets', 'KBaseTrees', 'ProbabilisticAnnotation'
    ]

    for m in modules:
        try:
            mu = ModuleUpdater(ws, m, target_dir, repo)
            mu.update(manifest)
        except Exception as e:
            print(str(e))
Ejemplo n.º 29
0
    def setUpClass(cls):
        token = os.environ.get('KB_AUTH_TOKEN', None)
        # 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,
            'provenance': [{
                'service': 'GenomeAnnotationAPI',
                'method': 'please_never_use_it_in_production',
                'method_params': []
            }],
            'authenticated':
            1
        })

        config_file = os.environ.get('KB_DEPLOYMENT_CONFIG', None)
        config = ConfigParser.ConfigParser()
        config.read(config_file)
        cls.cfg = {n[0]: n[1] for n in config.items('GenomeAnnotationAPI')}
        cls.ws = Workspace(cls.cfg['workspace-url'], token=token)
        cls.impl = GenomeAnnotationAPI(cls.cfg)

        cls.ga_ref = "8020/81/1"
        cls.genome_ref = "8020/83/1"
def upload_workspace_data(cs, ws_url, source_ref, target_ws, obj_name):
    ws = Workspace(ws_url, token=TOKEN)
    type_ = ws.translate_from_MD5_types([CS_MD5_TYPE])[CS_MD5_TYPE][0]
    ws.save_objects(
        {'workspace': target_ws,
         'objects': [{'name': obj_name,
                      'type': type_,
                      'data': cs,
                      'provenance': [{'script': SCRIPT_NAME,
                                      'script_ver': __VERSION__,
                                      'input_ws_objects': [source_ref],
                                      }]
                      }
                     ]
         }
    )
Ejemplo n.º 31
0
def _init_clients():
    ws_c = Workspace(ws_url)
    # The KBase web-client instantiated below is being deprecated and no longer works for all functions.
    # A local work-around has been added using kb_sdk and fba_tools
    fba_c = fbaModelServices(fba_url)
    gap_c = LocalFbaModelServices
    return ws_c, fba_c, gap_c
Ejemplo n.º 32
0
class Validator(TransformBase):
    def __init__(self, args):
        TransformBase.__init__(self, args)
        self.ws_url = args.ws_url
        self.cfg_name = args.cfg_name
        self.sws_id = args.sws_id
        self.etype = args.etype
        self.opt_args = args.opt_args

        # download ws object and find where the validation script is located
        self.wsd = Workspace(url=self.ws_url, token=self.token)
        self.config = self.wsd.get_object({
            'id': self.cfg_name,
            'workspace': self.sws_id
        })['data']['config_map']

        if self.config is None:
            raise Exception("Object {} not found in workspace {}".format(
                self.cfg_name, self.sws_id))

    def validation_handler(self):
        ###
        # execute validation
        ## TODO: Add logging

        if self.etype not in self.config['validator']:
            raise Exception(
                "No validation script was registered for {}".format(
                    self.etype))

        fd_list = []
        if os.path.exists("{}/{}".format(self.sdir, self.itmp)):
            fd_list.append("{}/{}".format(self.sdir, self.itmp))
        else:
            fd_list = glob.glob("{}/{}_*".format(self.sdir, self.itmp))

        for fd in fd_list:
            vcmd_lst = [
                self.config['validator'][self.etype]['cmd_name'],
                self.config['validator'][self.etype]['cmd_args']['input'], fd
            ]

            if 'validator' in self.opt_args:
                opt_args = self.opt_args['validator']
                for k in opt_args:
                    if k in self.config['validator'][etype][
                            'opt_args'] and opt_args[k] is not None:
                        vcmd_lst.append(self.config['validator'][self.etype]
                                        ['opt_args'][k])
                        vcmd_lst.append(opt_args[k])

            p1 = Popen(vcmd_lst, stdout=PIPE)
            out_str = p1.communicate()
            # print output message for error tracking
            if out_str[0] is not None: print out_str[0]
            if out_str[1] is not None: print >> sys.stderr, out_str[1]

            if p1.returncode != 0:
                raise Exception(out_str[1])
Ejemplo n.º 33
0
 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.dfu = DataFileUtil(self.callback_url)
     self.ws = Workspace(self.ws_url, token=self.token)
     self.scratch = config['scratch']
    def test_loadGenome(self):
        ''' Load a test Genome object into the test workspace. '''
        
        # Create the test workspace.
        wsClient = Workspace(self._config["workspace_url"], token=self._token)
        try:
            # See if the workspace exists.
            wsInfo = wsClient.get_workspace_info( { "workspace": self._config["test_ws"] } )
        except WorkspaceServerError as e:
            # Hopefully this means the workspace does not exist. (It could also mean someone messed up setting up the URLs)
            traceback.print_exc(file=sys.stderr)
            wsInfo = wsClient.create_workspace( { "workspace": self._config["test_ws"] } )

        # We also need to put in a mapping and a biochemistry object somewhere.
        # To do this, I just create a "dependency workspace" and pull them from there.
        try:
            # See if the workspace exists.
            wsInfo = wsClient.get_workspace_info( { "workspace": self._config["dependency_ws"] } )
        except WorkspaceServerError as e:
            # Hopefully this means the workspace does not exist. (It could also mean someone messed up setting up the URLs)
#            traceback.print_exc(file=sys.stderr)
            depWsInfo = wsClient.create_workspace( { "workspace": self._config["dependency_ws"] } )

        # Load the mapping and biochemistry objects
        testContigSet = json.load(open(self._config['contigset_file'], 'r'))
        contigSetSaveData = dict()
        contigSetSaveData['type'] = 'KBaseGenomes.ContigSet'
        contigSetSaveData['name'] = self._config['contigsetid']
        contigSetSaveData['data'] = testContigSet        
        testGenome = json.load(open(self._config["genome_file"], "r"))
        genomeSaveData = dict()
        genomeSaveData['type'] = 'KBaseGenomes.Genome'
        genomeSaveData['name'] = self._config['genomeid']
        genomeSaveData['data'] = testGenome
        wsClient.save_objects( { 'workspace': self._config['test_ws'], 'objects': [ genomeSaveData, contigSetSaveData ] } )
Ejemplo n.º 35
0
def handler (args) :
    ###
    # download ws object and find where the validation script is located
    wsd = Workspace(url=args.ws_url, token=os.environ.get('KB_AUTH_TOKEN'))
    indata = wsd.get_object({'id' : args.etype,
                  'workspace' : args.sws_id})['data']

    if indata is None:
        raise Exception("Object {} not found in workspace {}".format(args.inobj_id, args.sws_id))

    try:
        os.mkdir(args.sdir)
    except:
        raise Exception("Could not create directory {}".format(args.sdir))

    if indata['validation_script']['id'] is None: raise Exception("Script Shock node id information is not provided")

    surl = args.shock_url
    if indata['validation_script']['shock_url'] is not None: surl = indata['validation_script']['shock_url']
    
    meta = urllib.urlopen("{}/node/{}".format(surl, indata['validation_script']['id']))
    script = urllib.urlopen("{}/node/{}?Download".format(surl, indata['id']))
    data = urllib.urlopen("{}/node/{}?Download".format(surl, args.inobj_id))
        
    # TODO: add compressed file handling using meta (tar.gz, tgz, etc).
    sif = open("{}/validator".format(args.sdir),'w')
    sif.write(script.read())
    sif.close()
    
    dif = open("{}/in_file".format(args.sdir),'w')
    dif.write(data.read())
    dif.close()

    script.close()
    data.close()
    
    ###
    # execute validation
    vcmd_lst = ["{}/validator".format(args.sdir), "-i", "{}/in_file".format(args.sdir) ]

    p1 = Popen(vcmd_lst, stdout=PIPE)
    out_str = p1.communicate()
    # print output message for error tracking
    if out_str[0] is not None : print out_str[0]
    if out_str[1] is not None : print >> sys.stderr, out_str[1]
Ejemplo n.º 36
0
    def manyHellos_runEach(self, ctx, task):
        """
        :param task: instance of type "ManyHellos_task" -> structure:
           parameter "msg" of String, parameter "job_number" of Long,
           parameter "workspace" of String
        :returns: instance of type "ManyHellos_runEachResult" (runEach()) ->
           structure: parameter "message" of String
        """
        # ctx is the context object
        # return variables are: returnVal
        #BEGIN manyHellos_runEach
        print("this is manyHellos_runEach...")
        pprint(["task is ", task])

        res = "{0}: {1}".format(task['job_number'], task['msg'])

        ws_client = Workspace(url=self.config['workspace-url'],
                              token=ctx['token'])
        res_obj = ws_client.save_objects({
            "workspace":
            task['workspace'],
            "objects": [{
                'type':
                'KBaseReport.Report',
                "data": {
                    'objects_created': [],
                    'text_message': res
                },
                "name":
                "{0}_{1}.rpt".format(task['msg'], task['job_number']),
                "meta": {}
            }]
        })
        res = json.dumps(res_obj)

        print("exiting manyHellos_runEach(), res is", res)
        returnVal = {'message': res}
        #END manyHellos_runEach

        # At some point might do deeper type checking...
        if not isinstance(returnVal, dict):
            raise ValueError('Method manyHellos_runEach return value ' +
                             'returnVal is not type dict as required.')
        # return the results
        return [returnVal]
Ejemplo n.º 37
0
    def __init__(self, args):
        TransformBase.__init__(self, args)
        self.ws_url = args.ws_url
        self.cfg_name = args.cfg_name
        self.sws_id = args.sws_id
        self.etype = args.etype
        self.opt_args = args.opt_args

        # download ws object and find where the validation script is located
        self.wsd = Workspace(url=self.ws_url, token=self.token)
        self.config = self.wsd.get_object({
            'id': self.cfg_name,
            'workspace': self.sws_id
        })['data']['config_map']

        if self.config is None:
            raise Exception("Object {} not found in workspace {}".format(
                self.cfg_name, self.sws_id))
    def setUp(self):
        # Set configuration variables.
        self._config = get_config(os.environ["KB_TEST_CONFIG"])

        # Get an authorization token for the test user.
        wsClient = Workspace(self._config["workspace_url"],
                             user_id=self._config["test_user"],
                             password=self._config["test_pwd"])
        self._token = wsClient._headers['AUTHORIZATION']
Ejemplo n.º 39
0
def fetch_narrative(nar_id, auth_token, url=ci_ws, file_name=None):
    """
    Fetches a Narrative object with the given reference id (of the form ##/##).
    If a file_name is given, then it is printed to that file.
    If the narrative is found, the jsonized string of it is returned.

    If nothing is found, an empty Dict is returned.
    """
    ws_client = Workspace(url=url, token=auth_token)
    nar_data = ws_client.get_objects([{"ref": nar_id}])
    if len(nar_data) > 0:
        nar_json = json.dumps(nar_data[0])
        if file_name is not None:
            f = open(file_name, "w")
            f.write(nar_json)
            f.close()
        return nar_json
    return {}
Ejemplo n.º 40
0
def fetch_narrative(nar_id, auth_token, url=ci_ws, file_name=None):
    """
    Fetches a Narrative object with the given reference id (of the form ##/##).
    If a file_name is given, then it is printed to that file.
    If the narrative is found, the jsonized string of it is returned.

    If nothing is found, an empty Dict is returned.
    """
    ws_client = Workspace(url=url, token=auth_token)
    nar_data = ws_client.get_objects([{'ref':nar_id}])
    if len(nar_data) > 0:
        nar_json = json.dumps(nar_data[0])
        if file_name is not None:
            f = open(file_name, 'w')
            f.write(nar_json)
            f.close()
        return nar_json
    return {}
Ejemplo n.º 41
0
 def __init__(self):
     c = Config()
     c.HTMLExporter.preprocessors = [NarrativePreprocessor]
     c.TemplateExporter.template_path = [
         '.', self._narrative_template_path()
     ]
     c.CSSHTMLHeaderPreprocessor.enabled = True
     self.html_exporter = HTMLExporter(config=c)
     self.html_exporter.template_file = 'narrative'
     self.ws_client = Workspace(URLS.workspace)
     self.narr_fetcher = NarrativeIO()
Ejemplo n.º 42
0
def upload_workspace_data(cs, ws_url, source_ref, target_ws, obj_name):
    ws = Workspace(ws_url, token=TOKEN)
    type_ = ws.translate_from_MD5_types([CS_MD5_TYPE])[CS_MD5_TYPE][0]
    ws.save_objects({
        'workspace':
        target_ws,
        'objects': [{
            'name':
            obj_name,
            'type':
            type_,
            'data':
            cs,
            'provenance': [{
                'script': SCRIPT_NAME,
                'script_ver': __VERSION__,
                'input_ws_objects': [source_ref],
            }]
        }]
    })
Ejemplo n.º 43
0
def get_genome(genome_id=None,
               workspace_id=None,
               token=None,
               workspace_url=None):
    #download genome object from workspace
    if workspace_url is None:
        workspace_url = 'https://ci.kbase.us/services/ws'

    #print token
    #print genome_id
    #print workspace_id

    workspace_client = Workspace(url=workspace_url, token=token)
    genome = workspace_client.get_object({
        'id': genome_id,
        'workspace': workspace_id,
        'type': 'KBaseGenomes.Genome'
    })
    #genome = workspace_client.get_object({'id' : 'Bifidobacterium_animalis_subsp._lactis_AD011', 'type' : 'KBaseGenomes.Genome',  'workspace' : 'plane83:1436884411390'})

    return genome
Ejemplo n.º 44
0
def get_genome(genome_id=None,workspace_id=None,token=None,workspace_url=None):
	#download genome object from workspace
	if workspace_url is None:
		workspace_url='https://kbase.us/services/ws/'
	if token is None:
		with open('/kb/dev_container/modules/genome_util/mytoken.txt') as token_file:
			token=token_file.read()
		token=token.rstrip()


	
	#print token
	#print genome_id
	#print workspace_id
	
	workspace_client=Workspace(url=workspace_url, token=token)
	#genome=workspace_client.get_object({'id':genome_id, 'workspace':workspace_id, 'type':'KBaseGenomes.Genome'})
	genome = workspace_client.get_object({'id' : 'Bifidobacterium_animalis_subsp._lactis_AD011', 'type' : 'KBaseGenomes.Genome',  'workspace' : 'plane83:1436884411390'})
	

	return genome
    def test_annotate(self):
        ''' Run pa-annotate on a valid Genome object and verify that the job runs and returns a valid ProbAnno object in the expected time.'''

        # Run the annotate() function to generate a ProbAnno object.
        paClient = ProbabilisticAnnotation(self._config["probanno_url"], token=self._token)
        jobid = paClient.annotate( {
            "genome": self._config["genomeid"],
            "genome_workspace": self._config["test_ws"],
            "probanno": self._config["probannoid"],
            "probanno_workspace": self._config["test_ws"] } )
        
        # Allow time for the command to run.
        time.sleep(float(self._config["runtime"]))
        
        # Make sure the job has completed.
        ujsClient = UserAndJobState(self._config['ujs_url'], token=self._token)
        jobList = ujsClient.list_jobs([ self._config['test_user'] ], 'CE')
        jobCompleted = False
        for job in jobList:
            if jobid == job[0]:
                jobCompleted = True
                jobInfo = job
        self.assertTrue(jobCompleted, 'Job did not complete before timeout of %s seconds' %(self._config['runtime']))
        
        # See if the job ended in error.
        details = ''
        if jobInfo[11] == 1:
            details = ujsClient.get_detailed_error(jobInfo[0])
        self.assertEqual(jobInfo[11], 0, 'Job ended in error: %s' %(details))

        # Look for the ProbAnno object in the test workspace.
        wsClient = Workspace(self._config["workspace_url"], token=self._token)
        try:
            probannoObjectId = { 'workspace': self._config['test_ws'], 'name': self._config['probannoid'] }
            objectList = wsClient.get_objects( [ probannoObjectId ] )
            probannoObject = objectList[0]
            self.assertEqual(probannoObject['info'][1], self._config['probannoid'], 'ProbAnno object id %s is not %s' %(probannoObject['info'][1], self._config['probannoid']))
        except WorkspaceServerError as e:
            traceback.print_exc(file=sys.stderr)
            self.fail(msg = "The expected object %s did not get created in the workspace %s!\n" %(self._config["probannoid"], self._config["test_ws"]))
Ejemplo n.º 46
0
    def get_rxnprobs(self, ctx, input):
        # ctx is the context object
        # return variables are: output
        #BEGIN get_rxnprobs
        ''' Convert a reaction probability object into a human-readable table.

            @param ctx Current context object
            @param input Dictionary with input parameters for function
            @return List of reaction_probability tuples
            @raise WrongVersionError when RxnProbs object version number is invalid
        '''

        # Sanity check on input arguments
        input = self._checkInputArguments(ctx, input, 
                                          [ "rxnprobs", "rxnprobs_workspace" ], 
                                          { 'rxnprobs_version': None, 'sort_field': 'rxnid' }
                                          )

        wsClient = Workspace(self.config["workspace_url"], token=ctx['token'])
        rxnProbsObjectId = make_object_identity(input["rxnprobs_workspace"], input["rxnprobs"], input['rxnprobs_version'])
        objectList = wsClient.get_objects( [ rxnProbsObjectId ] )
        rxnProbsObject = objectList[0]
        if rxnProbsObject['info'][2] != RxnProbsType:
            message = 'RxnProbs object type %s is not %s for object %s' %(rxnProbsObject['info'][2], RxnProbsType, rxnProbsObject['info'][1])
            ctx.log_err(message)
            raise WrongVersionError(message)
        output = rxnProbsObject["data"]["reaction_probabilities"]
        if input['sort_field'] == 'rxnid':
            output.sort(key=lambda tup: tup[0])
        elif input['sort_field'] == 'probability':
            output.sort(key=lambda tup: tup[1], reverse=True)
        #END get_rxnprobs

        # At some point might do deeper type checking...
        if not isinstance(output, list):
            raise ValueError('Method get_rxnprobs return value ' +
                             'output is not type list as required.')
        # return the results
        return [output]
Ejemplo n.º 47
0
def save_ws_object(obj):
    """Save an object to the workspace

    Parameters
    ----------
    obj : dict
        Object with the fields: type, data, and name.
        The type must be the full typespec
        (e.g. 'MetaboliteAtlas.Compound-0.3')

    Returns
    -------
    id : str
        Object workspace id
    """
    from biokbase.workspace.client import Workspace
    ws = Workspace(WS_URL)
    obj.setdefault('hidden', 0)
    wks = ws.list_workspaces({'excludeGlobal': 1})
    ws_id = [wk[-1] for wk in wks if wk[0] == os.environ['KB_WORKSPACE_ID']][0]
    save_objects_params = {'id': ws_id, 'objects': [obj]}
    return ws.save_objects(save_objects_params)[0][-2]
Ejemplo n.º 48
0
def upload_narrative(nar_file, auth_token, user_id, url=ci_ws, set_public=False):
    """
    Uploads a Narrative from a downloaded object file.
    This file needs to be in JSON format, and it expects all
    data and info that is usually returned by the Workspace.get_objects
    method.

    Returns a dict of three elements:
        ws: the id of the workspace that was created
        obj: the id of the narrative object
        ref: the above two joined together into an object ref (for convenience)
    """

    # read the file
    f = open(nar_file, "r")
    nar = json.loads(f.read())
    f.close()

    # do some setup.
    current_nar_metadata = ws_metadata
    current_nar_metadata["narrative_nice_name"] = nar["data"]["metadata"]["name"]
    ws_client = Workspace(url=url, token=auth_token)

    # create the new workspace for the narrative
    ws_info = ws_client.create_workspace(
        {
            "workspace": "{}:{}".format(user_id, str(time.time()).replace(".", "")),
            "meta": current_nar_metadata,
            "globalread": "r" if set_public else "n",
        }
    )
    ws_id = ws_info[0]

    # setup and save the narrative object
    nar["info"][10]
    ws_save_obj = {
        "type": "KBaseNarrative.Narrative",
        "data": nar["data"],
        "name": nar["info"][1],
        "meta": nar["info"][10],
        "provenance": [
            {
                "script": "upload_narrative_test.py",
                "description": "Temporary Narrative uploaded for automated testing",
            }
        ],
    }
    obj_info = ws_client.save_objects({"id": ws_id, "objects": [ws_save_obj]})

    # tweak the workspace's metadata to properly present its narrative
    ws_client.alter_workspace_metadata(
        {"wsi": {"id": ws_id}, "new": {"narrative": obj_info[0][0]}}
    )
    return {
        "ws": ws_info[0],
        "obj": obj_info[0][0],
        "refstr": "{}/{}".format(ws_info[0], obj_info[0][0]),
        "ref": NarrativeRef({"wsid": ws_info[0], "objid": obj_info[0][0]}),
    }
 def test_calculate(self):
     ''' Run pa-calculate on a valid ProbAnno object and verify that the job runs and returns a valid RxnProbs object.'''
     
     # Run the calculate() function to generate a RxnProbs object.
     paClient = ProbabilisticAnnotation(self._config["probanno_url"], token=self._token)
     rxnprobsMetadata = paClient.calculate( {
         "probanno":           self._config["probannoid"],
         "probanno_workspace": self._config["test_ws"],
         "rxnprobs":           self._config["rxnprobsid"],
         "rxnprobs_workspace": self._config["test_ws"] 
         } )
      
     # Look for the RxnProbs object in the test workspace.
     wsClient = Workspace(self._config["workspace_url"], token=self._token)
     try:
         rxnprobsObjectId = { 'workspace': self._config['test_ws'], 'name': self._config['rxnprobsid'] }
         objectList = wsClient.get_objects( [ rxnprobsObjectId ] )
         rxnprobsObject = objectList[0]
         self.assertEqual(rxnprobsObject['info'][1], self._config['rxnprobsid'], 'RxnProbs object id %s is not %s' %(rxnprobsObject['info'][1], self._config['rxnprobsid']))
     except WorkspaceServerError as e:
         traceback.print_exc(file=sys.stderr)
         self.fail(msg = "The expected object %s did not get created in the workspace %s!\n" %(self._config["rxnprobsid"], self._config["test_ws"]))
Ejemplo n.º 50
0
def save_ws_object(obj):
    """Save an object to the workspace

    Parameters
    ----------
    obj : dict
        Object with the fields: type, data, and name.
        The type must be the full typespec
        (e.g. 'MetaboliteAtlas.Compound-0.3')

    Returns
    -------
    id : str
        Object workspace id
    """
    from biokbase.workspace.client import Workspace

    ws = Workspace(WS_URL)
    obj.setdefault("hidden", 0)
    wks = ws.list_workspaces({"excludeGlobal": 1})
    ws_id = [wk[-1] for wk in wks if wk[0] == os.environ["KB_WORKSPACE_ID"]][0]
    save_objects_params = {"id": ws_id, "objects": [obj]}
    return ws.save_objects(save_objects_params)[0][-2]
Ejemplo n.º 51
0
    def __init__(self, args):
        TransformBase.__init__(self,args)
        self.ws_url = args.ws_url
        self.cfg_name = args.cfg_name
        self.sws_id = args.sws_id
        self.etype = args.etype
        self.opt_args = args.opt_args

        # download ws object and find where the validation script is located
        self.wsd = Workspace(url=self.ws_url, token=self.token)
        self.config = self.wsd.get_object({'id' : self.cfg_name, 'workspace' : self.sws_id})['data']['config_map']
     
        if self.config is None:
            raise Exception("Object {} not found in workspace {}".format(self.cfg_name, self.sws_id))
Ejemplo n.º 52
0
    def SetupRNASeqAnalysis(self, ctx, params):
        # ctx is the context object
        # return variables are: returnVal
        # BEGIN SetupRNASeqAnalysis
        user_token = ctx["token"]
        ws_client = Workspace(url=self.__WS_URL, token=user_token)
        out_obj = {k: v for k, v in params.iteritems() if not k in ("ws_id", "genome_id", "annotation_id") and v}
        pprint(out_obj)
        if "num_samples" in out_obj:
            out_obj["num_samples"] = int(out_obj["num_samples"])
        if "num_replicates" in out_obj:
            out_obj["num_replicates"] = int(out_obj["num_replicates"])
        if "genome_id" in params and params["genome_id"] is not None:
            out_obj["genome_id"] = script_util.get_obj_info(
                self.__LOGGER, self.__WS_URL, [params["genome_id"]], params["ws_id"], user_token
            )[0]
        if "annotation_id" in params and params["annotation_id"] is not None:
            g_ref = script_util.get_obj_info(
                self.__LOGGER, self.__WS_URL, [params["annotation_id"]], params["ws_id"], user_token
            )[0]
            out_obj["annotation_id"] = g_ref
        self.__LOGGER.info("Uploading RNASeq Analysis object to workspace {0}".format(out_obj["experiment_id"]))
        try:
            res = ws_client.save_objects(
                {
                    "workspace": params["ws_id"],
                    "objects": [
                        {"type": "KBaseRNASeq.RNASeqAnalysis", "data": out_obj, "name": out_obj["experiment_id"]}
                    ],
                }
            )
            returnVal = {"workspace": params["ws_id"], "output": out_obj["experiment_id"]}

        except Exception, e:
            raise KBaseRNASeqException(
                "Error Saving the object to workspace {0},{1}".format(out_obj["experiment_id"], e)
            )
Ejemplo n.º 53
0
class Validator(TransformBase):
    def __init__(self, args):
        TransformBase.__init__(self,args)
        self.ws_url = args.ws_url
        self.cfg_name = args.cfg_name
        self.sws_id = args.sws_id
        self.etype = args.etype
        self.opt_args = args.opt_args

        # download ws object and find where the validation script is located
        self.wsd = Workspace(url=self.ws_url, token=self.token)
        self.config = self.wsd.get_object({'id' : self.cfg_name, 'workspace' : self.sws_id})['data']['config_map']
     
        if self.config is None:
            raise Exception("Object {} not found in workspace {}".format(self.cfg_name, self.sws_id))


    def validation_handler (self) :
        ###
        # execute validation
        ## TODO: Add logging
        
        if self.etype not in self.config['validator']:
          raise Exception("No validation script was registered for {}".format(self.etype))

        fd_list = []
        if os.path.exists("{}/{}".format(self.sdir,self.itmp)):
          fd_list.append( "{}/{}".format(self.sdir,self.itmp))
        else:
          fd_list = glob.glob("{}/{}_*".format(self.sdir,self.itmp))

        for fd in fd_list:
          vcmd_lst = [self.config['validator'][self.etype]['cmd_name'], self.config['validator'][self.etype]['cmd_args']['input'], fd]
         
          if 'validator' in self.opt_args:
            opt_args = self.opt_args['validator']
            for k in opt_args:
              if k in self.config['validator'][etype]['opt_args'] and opt_args[k] is not None:
                vcmd_lst.append(self.config['validator'][self.etype]['opt_args'][k])
                vcmd_lst.append(opt_args[k])
               
          p1 = Popen(vcmd_lst, stdout=PIPE)
          out_str = p1.communicate()
          # print output message for error tracking
          if out_str[0] is not None : print out_str[0]
          if out_str[1] is not None : print >> sys.stderr, out_str[1]
         
          if p1.returncode != 0: 
              raise Exception(out_str[1])
Ejemplo n.º 54
0
def _find_narrative_py2_code(ws_id: int,
                             ws: Workspace,
                             rt: RefactoringTool,
                             verbose: bool = False) -> NarrativeInfo:
    """
    ws_id - workspace id to scan for Narratives
    ws - an authenticated workspace client

    returns a NarrativeInfo object
    """
    if verbose:
        print(f"WS:{ws_id} Checking workspace {ws_id}")

    ws_info = ws.administer({
        "command": "getWorkspaceInfo",
        "params": {
            "id": ws_id,
        },
    })
    narr_id = ws_info[8].get("narrative")
    if narr_id is None:
        print(f"WS:{ws_id} has no linked Narrative")
        return

    narr_obj = ws.administer({
        "command": "getObjects",
        "params": {
            "objects": [{
                "ref": f"{ws_id}/{narr_id}"
            }]
        },
    })["data"][0]
    if not narr_obj["info"][2].startswith("KBaseNarrative.Narrative"):
        raise TypeError(f"Object {ws_id}/{narr_id} is not a Narrative!")

    return _update_narrative(narr_obj, ws_info, rt)
    def test_calculate(self):
        ''' Run pa-calculate on a valid ProbAnno object and verify that the job runs and returns a valid RxnProbs object.'''

        # Run the calculate() function to generate a RxnProbs object.
        paClient = ProbabilisticAnnotation(self._config["probanno_url"],
                                           token=self._token)
        rxnprobsMetadata = paClient.calculate({
            "probanno":
            self._config["probannoid"],
            "probanno_workspace":
            self._config["test_ws"],
            "rxnprobs":
            self._config["rxnprobsid"],
            "rxnprobs_workspace":
            self._config["test_ws"]
        })

        # Look for the RxnProbs object in the test workspace.
        wsClient = Workspace(self._config["workspace_url"], token=self._token)
        try:
            rxnprobsObjectId = {
                'workspace': self._config['test_ws'],
                'name': self._config['rxnprobsid']
            }
            objectList = wsClient.get_objects([rxnprobsObjectId])
            rxnprobsObject = objectList[0]
            self.assertEqual(
                rxnprobsObject['info'][1], self._config['rxnprobsid'],
                'RxnProbs object id %s is not %s' %
                (rxnprobsObject['info'][1], self._config['rxnprobsid']))
        except WorkspaceServerError as e:
            traceback.print_exc(file=sys.stderr)
            self.fail(
                msg=
                "The expected object %s did not get created in the workspace %s!\n"
                % (self._config["rxnprobsid"], self._config["test_ws"]))
Ejemplo n.º 56
0
    def __init__(self, url=None, token=None, user_id=None, password=None, wsid=None,
                 create=False, **create_kw):
        """Constructor.

        :param url: URL of remote WS service
        :type url: str
        :param token: Authorization token, overrides 'user_id'/'password'
        :type token: str
        :param user_id: User ID for authentication (overridden by 'token')
        :type user_id: str
        :param user_id: Password for authentication (overridden by 'token')
        :type user_id: str
        :param wsid: Workspace ID or name
        :type wsid: str
        :param create_kw: Any extra keywords to create a new workspace
        :type create_kw: None or dict
        :raise: ValueError if workspace id is empty, KeyError if there is no workspace by that name,
                WorkspaceException if creation of the workspace fails.
        """
        WS2.__init__(self, url=url, user_id=user_id, password=password, token=token)
        self.has_wsid = False
        if wsid is not None:
            self._init_ws(wsid, create_kw)
            self.has_wsid = True
    def get_feature_ids(self, ctx, p):
        """
        :param p: instance of type "Params" (Insert your typespec information
           here.) -> structure: parameter "ref" of String
        :returns: instance of type "Output" -> structure:
        """
        # ctx is the context object
        # return variables are: o
        #BEGIN get_feature_ids
        ws = Workspace(self.workspaceURL, token = ctx['token'])

        featureContainers = ws.get_object_subset([{
                                     'included':['feature_container_references'], 
                                     'ref':p['ref']}])[0]['data']['feature_container_references']

        all_features = {}
        for fc in featureContainers:
            fc_ws_id = featureContainers[fc]
            features = ws.get_object_subset([{
                                     'included':['/features/*/feature_id'], 
                                     'ref':fc_ws_id}])[0]['data']['features']
            all_features[fc] = features.keys()
            #feature_list = []
            #for f in features:
            #    feature_list.append(features[f]['feature_id'])
            #all_features[fc] = feature_list

        o = all_features
        #END get_feature_ids

        # At some point might do deeper type checking...
        if not isinstance(o, dict):
            raise ValueError('Method get_feature_ids return value ' +
                             'o is not type dict as required.')
        # return the results
        return [o]
Ejemplo n.º 58
0
    def CreateRNASeqSampleSet(self, ctx, params):
        """
        :param params: instance of type "CreateRNASeqSampleSetParams"
           (FUNCTIONS used in the service) -> structure: parameter "ws_id" of
           String, parameter "sampleset_id" of String, parameter
           "sampleset_desc" of String, parameter "domain" of String,
           parameter "platform" of String, parameter "sample_ids" of list of
           String, parameter "condition" of list of String, parameter
           "source" of String, parameter "Library_type" of String, parameter
           "publication_id" of String, parameter "external_source_date" of
           String
        :returns: instance of type "RNASeqSampleSet" (Object to Describe the
           RNASeq SampleSet @optional platform num_replicates source
           publication_Id external_source_date sample_ids @metadata ws
           sampleset_id @metadata ws platform @metadata ws num_samples
           @metadata ws num_replicates @metadata ws length(condition)) ->
           structure: parameter "sampleset_id" of String, parameter
           "sampleset_desc" of String, parameter "domain" of String,
           parameter "platform" of String, parameter "num_samples" of Long,
           parameter "num_replicates" of Long, parameter "sample_ids" of list
           of String, parameter "condition" of list of String, parameter
           "source" of String, parameter "Library_type" of String, parameter
           "publication_Id" of String, parameter "external_source_date" of
           String
        """
        # ctx is the context object
        # return variables are: returnVal
        #BEGIN CreateRNASeqSampleSet
	
	user_token=ctx['token']
        ws_client=Workspace(url=self.__WS_URL, token=user_token)
	hs = HandleService(url=self.__HS_URL, token=user_token)
	try:
	    ### Create the working dir for the method; change it to a function call
	    out_obj = { k:v for k,v in params.iteritems() if not k in ('ws_id')}  	
	    sample_ids = params["sample_ids"]
	    out_obj['num_samples'] = len(sample_ids)
	    ## Validation to check if the Set contains more than one samples
	    if len(sample_ids) < 2:
		raise ValueError("This methods can only take 2 or more RNASeq Samples. If you have only one read sample, run either 'Align Reads using Tophat/Bowtie2' methods directly for getting alignment")

	    ## Validation to Check if the number of samples is equal to number of condition
	    if len(params["condition"]) != out_obj['num_samples']:
		raise ValueError("Please specify a treatment label for each sample in the RNA-seq SampleSet. Please enter the same label for the replicates in a sample type")
	    ## Validation to Check if the user is loading the same type as specified above
	    if params["Library_type"] == 'PairedEnd' : lib_type = 'KBaseAssembly.PairedEndLibrary'
	    else: lib_type = 'KBaseAssembly.SingleEndLibrary'
	    for i in sample_ids:
	    	s_info = ws_client.get_object_info_new({"objects": [{'name': i, 'workspace': params['ws_id']}]})
                obj_type = s_info[0][2].split('-')[0]
		if obj_type != lib_type:
			raise ValueError("Library_type mentioned : {0}. Please add only {1} typed objects in Reads fields".format(lib_type,lib_type)) 
	
   	    ## Code to Update the Provenance; make it a function later
            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']=[ params['ws_id']+'/'+sample for sample in sample_ids]
	    
	    #Saving RNASeqSampleSet to Workspace
	    self.__LOGGER.info("Saving {0} object to workspace".format(params['sampleset_id']))
	    res= ws_client.save_objects(
                                {"workspace":params['ws_id'],
                                 "objects": [{
                                                "type":"KBaseRNASeq.RNASeqSampleSet",
                                                "data":out_obj,
                                                "name":out_obj['sampleset_id'],
						"provenance": provenance}]
                                })
            returnVal = out_obj
        except Exception,e:
                raise KBaseRNASeqException("Error Saving the object to workspace {0},{1}".format(out_obj['sampleset_id'],"".join(traceback.format_exc())))
Ejemplo n.º 59
0
from biokbase.workspace.client import Workspace
ws_client = Workspace()
ws_next_client = Workspace(url='https://next.kbase.us/services/ws')
a, b = ws_next_client.get_objects([{'objid' : '4', 'wsid' : '68'}, {'objid' : '5', 'wsid' : '68'}])[0:2]
a_params = {'type' : a['info'][2], 'data': a['data']}
b_params = {'type' : b['info'][2], 'data': b['data']}
ws_client.save_objects({'id': '9145', 'objects': [a_params, b_params]})