becas.py module documentation

This page contains the API documentation for becas.py, the single module packaged by becas-python that allows you to use the becas API programmatically from your Python modules.

For instructions on how to use the becas API from a command-line tool, read the Command-line interface documentation.

Comprehensive documentation about the becas API is available at its dedicated page.

Required configuration

You are required to authenticate in order to use the becas API. This can be done through the email and tool parameters, which you should set before invoking any of the annotation functions:

import becas
becas.email = 'you@example.com'  # required
becas.tool = 'your-tool-name'    # optional, defaults to 'becas-python'

Configuration parameters

You can set these parameters from your modules to configure becas-python behaviour.

becas.timeout = 120

Seconds to wait before timing out a request

becas.secure = False

Whether to use HTTPS or plain HTTP

Constants

These constants are provided for your reference.

becas.SEMANTIC_GROUPS = ('SPEC', 'ANAT', 'DISO', 'PATH', 'CHED', 'ENZY', 'MRNA', 'PRGE', 'COMP', 'FUNC', 'PROC')

Semantic groups usable as keys of a groups dict

becas.EXPORT_FORMATS = ('json', 'xml', 'a1', 'conll')

Output formats available for the export_text() function

Functions

The following API client functions are available. Read more about the available API methods in the becas API calls reference.

Text annotation

becas.annotate_text(text, groups=None, echo=False)

Annotate text with biomedical concepts.

Parameters:
  • text – text to annotate (str or unicode).
  • groupsoptional dict of concept groups to identity.
  • echooptional flag to return text in the response.
Returns:

dict with annotation results.

Usage:

>>> import becas
>>> becas.email = 'you@example.com'
>>> results = becas.annotate_text('BRCA1 is a human caretaker gene.')
becas.export_text(text, format, groups=None)

Export text annotated with biomedical concepts in JSON, XML, A1 or CONLL.

Parameters:
  • text – text to annotate (str or unicode).
  • format – output format (one of ‘json’, ‘xml’, ‘a1’ or ‘conll’).
  • groupsoptional dict of concept groups to identity.
Returns:

unicode string with annotation results.

Usage:

>>> import becas
>>> becas.email = 'you@example.com'
>>> text = 'BRCA1 is a human caretaker gene.'
>>> json_results = becas.export_text(text, 'json')
>>> iexml_results = becas.export_text(text, 'xml')
>>> a1_results = becas.export_text(text, 'a1')
>>> conll_results = becas.export_text(text, 'conll')

Abstract annotation

becas.annotate_publication(pmid, groups=None)

Annotate PubMed publication with biomedical concepts.

Parameters:
  • pmid – PMID of publication to annotate.
  • groupsoptional dict of concept groups to identity.
Returns:

dict with annotation results.

Usage:

>>> import becas
>>> becas.email = 'you@example.com'
>>> results = becas.annotate_publication(23225384)
becas.export_publication(pmid, groups=None)

Export PubMed publication as MEDLINE IeXML annotated with biomedical concepts.

Parameters:
  • pmid – PMID of publication to annotate.
  • groupsoptional dict of concept groups to identity.
Returns:

unicode string with IeXML annotation results.

Usage:

>>> import becas
>>> becas.email = 'you@example.com'
>>> results = becas.export_publication(23225384)

Exceptions

If something goes wrong, one of these exceptions will be raised.

exception becas.BecasException

Bases: exceptions.RuntimeError

There was an ambiguous exception that occurred while handling your request.

exception becas.AuthenticationRequired

Bases: becas.BecasException

You need to authenticate your requests with your email and optionally a tool name.

As an example, if your email address is you@example.com, you can specify it as follows:

>>> import becas
>>> becas.email = 'you@example.com'

In case of excessive usage of the becas API, UA.PT Bioinformatics will attempt to contact a user at the email address provided before blocking access to the services.

exception becas.InvalidGroups

Bases: becas.BecasException, exceptions.ValueError

You provided an invalid groups dictionary.

exception becas.InvalidFormat

Bases: becas.BecasException, exceptions.ValueError

You provided an invalid export format.

exception becas.TooMuchText

Bases: becas.BecasException, exceptions.ValueError

You provided too much text to annotate. Try again with less text.

exception becas.TooManyRequests(wait)

Bases: becas.BecasException

You performed too many requests in a short amount of time. Wait wait seconds before trying again.

exception becas.PublicationNotFound

Bases: becas.BecasException, exceptions.ValueError

The publication you requested was not found in PubMed.

exception becas.ServiceUnavailable

Bases: becas.BecasException

The annotation service is currently unavailable.

exception becas.ConnectionError

Bases: becas.BecasException

A Connection error occurred.

exception becas.SSLError

Bases: becas.ConnectionError

An SSL error occurred.

exception becas.Timeout

Bases: becas.BecasException

The request timed out.


This documentation was automatically generated from the source code using sphinx.

becas.py is a rather small module. You can read the annotated source at GitHub.

becas-python

becas-python is a command-line tool and Python module for programmatic access to the becas API.

Table Of Contents

Related Topics

Useful Links

Feedback

Feedback is greatly appreciated. If you have any questions, comments, or need help using becas-python, send me an email.

Fork me on GitHub