示例#1
0
 def test_admin_query_invalid_auth(self):
     client2 = REClient(_API_URL, 'xyz')
     query = f"FOR vert IN {_VERT_COLL} FILTER vert._key == @id RETURN vert"
     with self.assertRaises(RERequestError) as ctx:
         client2.admin_query(query, {'id': 'xyz'})
     self.assertEqual(ctx.exception.resp.status_code, 403)
     # Mostly make sure that the __str__ method does not throw any errs
     self.assertTrue('Unauthorized' in str(ctx.exception))
示例#2
0
 def test_save_docs_invalid_auth(self):
     client2 = REClient(_API_URL, 'xyz')
     docs = [{'_key': 'xyz'}]
     with self.assertRaises(RERequestError) as ctx:
         client2.save_docs(coll=_VERT_COLL, docs=docs)
     self.assertEqual(ctx.exception.resp.status_code, 403)
     # Mostly make sure that the __str__ method does not throw any errs
     self.assertTrue('Unauthorized' in str(ctx.exception))
示例#3
0
#!/usr/bin/env python
## To install the KBase python client run:
## pip install --extra-index-url https://pypi.anaconda.org/kbase/simple releng-client
## The specification (DB schema) for the compounds and reactions are found here:
## https://github.com/kbase/relation_engine_spec/blob/master/stored_queries/
from relation_engine_client import REClient
re_client = REClient("https://kbase.us/services/relation_engine_api")

# Retrieving compounds with 'water' in their synonyms
response = re_client.stored_query("search_compounds",{"search_text":"water"})
for document in response['results']:
  print(document['name'],document['id'],document['formula'],document['charge'],document['aliases'])

# Retrieving reactions with 'water' in their synonyms
response = re_client.stored_query("search_reactions",{"search_text":"water"})
for document in response['results']:
  print(document['name'],document['id'],document['definition'])
示例#4
0
 def setUpClass(cls):
     cls.client = REClient(_API_URL, _TOK_ADMIN)
示例#5
0
def set_taxon_data(tax_id, re_api_url, genome_dict):
    """
    Fetch and set taxonomy data for a genome using an NCBI taxonomy ID.

    We mutate the genome_dict with the following fields:
    {
      "taxonomy": "x;y;z",    # NCBI taxonomy lineage string for human readability
      "domain": "x"           # String name of the domain
      "genetic_code": 11      # NCBI categorization of the lineage
                               (https://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi)
      "taxon_assignments": {  # Mapping of taxonomy namespace to taxonomy ID
        "ncbi": 1234
      }
    }
    """
    # We use the Relation Engine to do a lookup on taxonomy data from ID
    re_client = REClient(re_api_url)
    # FIXME this timestamp should come from the client
    now = int(time.time() *
              1000)  # unix epoch for right now, for use in the RE API
    tax_id = str(tax_id)
    genome_dict.setdefault('warnings', [])
    assignments = genome_dict.get('taxon_assignments')
    # Check to make sure that tax_id == genome.taxon_assignments.ncbi
    if assignments and assignments.get(
            _NCBI_TAX) and assignments[_NCBI_TAX] != tax_id:
        raise RuntimeError(
            f"The provided taxon ID ({tax_id}) differs from the "
            f"taxon ID in the genome's `taxon_assignments` field: {assignments[_NCBI_TAX]}."
        )
    genome_dict['taxon_assignments'] = {'ncbi': tax_id}
    # Fetch the taxon from Relation Engine by taxon ID
    try:
        resp_json = re_client.stored_query('ncbi_fetch_taxon', {
            'id': str(tax_id),
            'ts': now
        },
                                           raise_not_found=True)
    except RENotFound as err:
        # Taxon not found; log and raise
        logging.error(str(err))
        raise err
    re_result = resp_json['results'][0]
    # Refer to the following schema for returned fields in `re_result`:
    # https://github.com/kbase/relation_engine_spec/blob/develop/schemas/ncbi/ncbi_taxon.yaml
    gencode = int(re_result['gencode'])
    # If there is a mismatch on some of these fields from NCBI, save a warning and continue
    if genome_dict.get(
            'genetic_code') and genome_dict['genetic_code'] != gencode:
        genome_dict['warnings'].append(
            f"The genetic code provided by NCBI ({gencode}) "
            f"does not match the one given by the user ({genome_dict['genetic_code']})"
        )
    genome_dict['genetic_code'] = gencode
    # Fetch the lineage on RE using the taxon ID to fill the "taxonomy" and "domain" fields.
    lineage_resp = re_client.stored_query('ncbi_taxon_get_lineage', {
        'id': str(tax_id),
        'ts': now,
        'select': ['scientific_name', 'rank']
    },
                                          raise_not_found=True)
    # The results will be an array of taxon docs with "scientific_name" and "rank" fields
    lineage = [
        r['scientific_name'] for r in lineage_resp['results']
        if r['scientific_name'] != 'root'
    ]
    # Format and normalize the lineage string
    taxonomy = '; '.join(lineage).replace('\n', '')
    # Fetch the domain in the lineage. The `domain` var should be a singleton list.
    # In NCBI taxonomy, 'domain' is known as 'superkingdom'
    domain = [
        r['scientific_name'] for r in lineage_resp['results']
        if r['rank'] == 'superkingdom'
    ]
    if genome_dict.get('domain') and genome_dict['domain'] != domain:
        genome_dict['warnings'].append(
            f"The domain provided by NCBI ({domain}) "
            f"does not match the one given by the user ({genome_dict['domain']})"
        )
    # Set the domain from NCBI, if possible. Otherwise, fall back to anything
    # the user supplied, or 'Unknown'.
    if domain:
        genome_dict['domain'] = domain[0]
    elif not genome_dict.get('domain'):
        genome_dict['domain'] = 'Unknown'
    genome_dict['taxonomy'] = taxonomy
    sciname = re_result['scientific_name']
    # The FastaGFFToGenome labyrinth of code sets the below default, which we want to override
    if genome_dict.get(
            'scientific_name') and genome_dict['scientific_name'] != sciname:
        genome_dict['warnings'].append(
            f"The scientific name provided by NCBI ('{sciname}') "
            f"does not match the one given by the user ('{genome_dict['scientific_name']}')"
        )
    # Assign the scientific name to the most specific (right-most) taxon in the lineage
    genome_dict['scientific_name'] = sciname