Skip to content

Refget python package tutorial

Record some versions:

from platform import python_version 
import refget

Computing digests locally

from refget import trunc512_digest

Show some results for sequence digests:

trunc512_digest('ACGT', 26)

Connecting to a remote API

The refget package provides a simple python wrapper around a remote hosted refget RESTful API. Provide the base url when construction a RefGetClient object and you can retrieve sequences from the remote server.

rgc = refget.RefGetClient("")
rgc.refget("6681ac2f62509cfc220d78751b8dc524", start=0, end=10)
rgc.refget("6681ac2f62509cfc220d78751b8dc524", start=0, end=50)

You can also hit the {digest}/metadata and service_info API endpoints described in the refget API specification:

{'metadata': {'aliases': [{'alias': 'ga4gh:SQ.lZyxiD_ByprhOUzrR1o1bq0ezO_1gkrn',
    'naming_authority': 'ga4gh'},
   {'alias': 'I', 'naming_authority': 'unknown'}],
  'length': 230218,
  'md5': '6681ac2f62509cfc220d78751b8dc524',
  'trunc512': '959cb1883fc1ca9ae1394ceb475a356ead1ecceff5824ae7'}}
{'service': {'algorithms': ['ga4gh', 'md5', 'trunc512'],
  'circular_supported': True,
  'subsequence_limit': None,
  'supported_api_versions': ['1.0.0']}}

When requesting a sequence that is not found, the service responds appropriately:

'Not Found'

Use a local database for caching

By default, any full-sequences retrieved from an API are cached locally in memory (in a Python Dict). This data will not persist past a current session, but is useful if you have an application that requires repeated requests. here, we re-request the sequence requested above. It is much faster this time because it uses a local cache:

rgc.refget("6681ac2f62509cfc220d78751b8dc524", start=0, end=10)

We can also add new sequences into the database:

rgc.refget(refget.md5('TCGATCGA'))  # This sequence is not found in our database yet
'Not Found'
checksum = rgc.load_seq("TCGATCGA")  # So, let's add it into database
rgc.refget(checksum)  # This time it returns

Keep in mind that sequences added in this way are added to your local database, not to the remote API, so when we restart, they will be gone:

del rgc
rgc = refget.RefGetClient("")
'Not Found'

Making data persist

If you want to retain your local cache, you can use a Dict that is backed by some persistent storage, such as a database on disk or another running process. There are many ways to do this, for example, you can use an sqlite database, a Redis database, or a MongoDB database. Here we'll show you how to use the sqlitedict package to back your local database.

To start, you need to create a dict object and pass that to the RefGetClient constructor.

import refget
from sqlitedict import SqliteDict
mydict = SqliteDict('./my_db.sqlite', autocommit=True)
rgc = refget.RefGetClient("", mydict)

Now when we retrieve a sequence it will be added to the local sqlite database automatically.

rgc.refget("6681ac2f62509cfc220d78751b8dc524", start=0, end=50)

Look, we can see that this object has been added to our sqlite database:


So now if we kill this object and start it up again without the API connection, but with the mydict local backend, we can still retrieve it:

del rgc
rgc = refget.RefGetClient(database=mydict)
rgc.refget("6681ac2f62509cfc220d78751b8dc524", start=0, end=50)

Loading a fasta file

The package also comes with a helper function for computing checksums for an entire fasta file.

fa_file = "../demo_fasta/demo.fa"
content = rgc.load_fasta(fa_file)
[{'name': 'chr1',
  'length': 4,
  'sequence_digest': 'f1f8f4bf413b16ad135722aa4591043e'},
 {'name': 'chr2',
  'length': 4,
  'sequence_digest': '45d0ff9f1a9504cf2039f89c1ffb4c32'}]
No remote URL connected
rgc.api_url_base = ""
'Not Found'
# You can show the complete contents of the database like this: