Python-api client

Python-api client

The MOLGENIS python client API allows you to retrieve, create, update and delete entities from within python.

You can install the Python REST Client as a package with pip:

pip install molgenis-py-client

Now you can create a python script. To get started, you should import the Python client, connect to a molgenis server and login:

import molgenis.client
session = molgenis.client.Session("https://molgenis.mydomain.example/api/")
session.login("username","password")

Always put the import and molgenis.Session in your script to make the api work.

Overview example

import molgenis.client
session = molgenis.client.Session("https://molgenis.mydomain.example/api/")
session.login("username","password")
my_table = session.get("package_entityName")
print(my_table)

To get a full overview of all functions in the python client, you can look at the examples on GitHub.

Methods

login

session.login(username,password)

Login to the MOLGENIS REST API

logout

session.logout()

Logout from the MOLGENIS REST API and destroy the session.

get_by_id

session.get_by_id("tableId", "rowId")

Retrieves a single entity row from an entity repository.

get

session.get("package_entityName")

Retrieves entities and returns the result in a dataframe.

Supported RSQL/FIQL query operators (see https://github.com/jirutka/rsql-parser)

Argument can be a single value, or multiple values in parenthesis separated by comma. Value that doesn’t contain any reserved character or a white space can be unquoted, other arguments must be enclosed in single or double quotes.

Examples

session.get("celiacsprue")
session.get("celiacsprue", num = 100000, start = 1000)
session.get("celiacsprue", attributes = c("Individual", "celiac_gender"))
session.get("celiacsprue", q = "(celiac_weight>=80 and celiac_height<180) or (celiac_gender==Female)")
session.get("celiacsprue", q = "(celiac_weight>=80;celiac_height<180),(celiac_gender==Female)")

add

session.add('Person', firstName='Jan', lastName='Klaassen')

Creates a new instance of an entity (i.e. a new row of the entity data table) and returns the id.

Examples

session.add('Person', {'firstName': 'Jan', 'lastName':'Klaassen'})
session.add('Plot', files={'image': ('expression.jpg', open('~/first-plot.jpg','rb')),
        'image2': ('expression-large.jpg', open('/Users/me/second-plot.jpg', 'rb'))},
        data={'name':'IBD-plot'})

update_one

session.update_one("entityType", "id", "attribute", "newValue")

Updates a value of a specified attribute in a specified row in a specified entityType.

add_all

session.add_all(entity, entities)

Creates new instances of an entity (i.e. adds new rows to the entity data table) and returns the ids.

Example

update = [{'id': '157', 'type': 'gnome', 'year': '1998'},
          {'id': '158', 'type': 'fairy', 'year': '1998'},
          {'id': '159', 'type': 'unicorn', 'year': '1998'}]


session.add_all("demo_sightings", update)

delete

session.delete(entity, id)

Deletes row based on its id.

delete_list

session.delete_list(entity, entities)

Deletes a number of rows based on a list of id's.

upload_zip

session.upload_zip("pathtozipfile")

This function uploads a zip file based on the EMX format.

get_entity_meta_data

session.get_entity_meta_data(entity)

Retrieves the metadata for an entity repository.

get_attribute_meta_data

session.get_attribute_meta_data(entity, attribute)

Retrieves the metadata for a single attribute of an entity repository.

Last updated