# Interact with Cytomine

All data available from the Cytomine graphical interface can be manipulated programmatically from your computer. This page introduces the key concepts on how to interact with Cytomine without this graphical interface.

# A RESTful application

Cytomine is a RESTful (opens new window) application. It means that the data stored and managed by Cytomine can be obtained through specific URLs. Contrary to the graphical interface, these URLs only provide relevant information data.

You can test it yourself.

  1. Log to your Cytomine instance with your usual browser. In the example, we assume a fictive https://CYTOMINE_URL Cytomine server.
  2. List the projects you have access to. The URL in your browser is https://CYTOMINE_URL/#/projects. It is the URL to list the projects from the graphical interface.
  3. Now, open a new tab in your browser. Go to https://CYTOMINE_URL/api/project.json. You have a lot of textual information: this is the list of projects you have access to without any graphical information, only data.

This https://CYTOMINE_URL/api/project.json URL is one of these specific URLs which provide only data. It is called an API endpoint.

These API endpoints let Cytomine users, Cytomine apps and external tools interact with Cytomine for various purposes:

  • Data mining
  • Scripting
  • Interact with AI algorithms
  • Create new graphical interfaces for dedicated purposes
  • Etc.

The Cytomine HTTP API

The Cytomine HTTP API structures and specifies all the endpoints for the whole application. All the endpoints are secured. It means that you need to be authenticated (with some HTTP mechanisms) so that the Cytomine server accept to treat your request. You keep the rights you have in the graphical interface: if you don't have the rights to perform some actions in the graphical interface, you don't have them by using the API neither. The graphical interface actually relies on the HTTP API.

When dealing with an HTTP API, you have to manage yourself the HTTP layer: create the request, authenticate the user using appropriate HTTP header mechanism, send the request to the server, wait for the response, analyse and parse the received response. In the previous example with your first API test, your browser has managed all of these for you.

When you are running a program outside your browser, you have to manage these yourself, which is not very convenient. To ease interaction with Cytomine, we developed API client libraries for multiple programming languages that do the job for you and let you focus on your task.

Whether you are a data scientist, a machine or deep learning developer or a system administrator, you might be interested to learn how to use these libraries to help you in your tasks.

# API client libraries

To ease interaction with Cytomine, the API client libraries encapsulate all the technical details relative to the HTTP API so that you can manipulate Cytomine resources without complexity. We have API client libraries for 3 languages:

Choose the API client library in the language you prefer. The libraries tend to follow the same structure so that you can switch from a language to another without learning a whole new library. Some libraries embed extra functionalities, such as the API client library for Python which provides helpers for machine and deep learning.

What if my language is missing?

You can interact with Cytomine in other languages that currently do not have a dedicated client library, but you have to manage yourself the usually encapsulated tasks such as HTTP handling, authentication and response parsing.

However, implement an API client library in another language in a good way to contribute to the Cytomine Open Source project.

# Client library structure

We present here the general structure of a Cytomine API client library, but refer to the documentation relative to each library for more details.

The Cytomine API client libraries follows the Object Oriented paradigm (opens new window). A Cytomine resource (such as a project STUDY-01 or an image cell01.tiff) is an instance of a domain (here, the project or the image domain).

In terms of object-oriented programming, a resource is an object which is an instance of a model class describing its domain. Each object (thus an instance of a model):

  • has a set of attributes corresponding to resource attributes
  • is managed itself through fetch(), save(), update() and delete() methods that communicate with the Cytomine server
  • has some utilities to manage HTTP and JSON technical details

Examples of models in Cytomine are Project, ImageInstance, Annotation, Term, User, CurrentUser, etc.

A Collection is a representation of a collection of models. It has methods to fetch multiples instances of a model with filters and/or pagination.

# A first script example

  1. Choose the API client library in the language you prefer.
  2. Install the library following the instructions on dedicated pages (in a new development project or environment, depending on the selected language)
  3. When the library is installed, you can use it as any other library into your code.

In this first script, we would like to count the number of annotations that are associated to a given ontology term in all projects we have access to and that have P as first letter in their name.

# In Python

from cytomine import Cytomine
from cytomine.models import *
# Replace with actual values or -better- use args.
host = 'https://mycytomine.com'
public_key = 'AAA'
private_key = 'ZZZ'
id_term = 0
# Connect to Cytomine
with Cytomine(host, public_key, private_key) as cytomine:
    # Get all the available projects you have access to
    projects = ProjectCollection().fetch()
    # Instantiate a new list
    projects_with_p = ProjectCollection()
    # Filter the projects
    for project in projects:
        if project.name.startswith("P"):
            projects_with_p.append(project)
    # Get the term
    term = Term().fetch(id_term)
    # Count annotations having that term in filtered projects
    count = 0
    for project in projects_with_p:
        annotations = AnnotationCollection(project=project.id, term=term.id).fetch()
        count += len(annotations)
    print("There are {} annotations with term {}".format(count, term.name))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

# In Java

import be.cytomine.client.*;
import be.cytomine.client.collections.*;
import be.cytomine.client.models.*;
public class AnnotationCounter {
  public static void main(String[] args){
    // Replace with actual values or -better- use args.
    String publicKey = "AAA";
    String privateLey = "ZZZ";
    String cytomineCoreUrl = "https://mycytomine.com/"; //Fictive: use your Cytomine server
    Long termId = 0L;
    // Connection to Cytomine
    Cytomine.connection(cytomineCoreUrl, publicKey, privateKey);
    // Get all the available projects you have access to
    Collection<Project> projects = Collection.fetch(Project.class);
    // Instantiate a new list
    List projectsStartingWithP = new ArrayList<>();
    // Filter the projects
    for (int i= 0; i<projects.size();i++){
      Project project = projects.get(i);
      if(project.get("name").startsWith("P") {
        projectsStartingWithP.add(project);
      }
    }
    // Get the term
    Term term = new Term().fetch(termId);
    // Count annotations having that term in filtered projects
    AnnotationCollection ac;
    int count = 0;
    for (Project p : projectsStartingWithP){
      ac = AnnotationCollection.fetchByTermAndProject(term, p);
      count += ac.size();
    }
    System.out.println("There are " + new String(count) + " annotations with term " + term.getStr("name"))
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

# What can I do next ?

Learn how to master the API client library for your language with the dedicated guides and examples (for Python, Java or Javascript).

Read the Cytomine apps guide go one step further. It lets you create complete programs manipulating Cytomine data, run them from your computer and save results to Cytomine. Every execution is reported to Cytomine, with the parameters applied to the program. The results of each program execution can then be compared from the graphical interface. Last but not least, these apps can be plugged to Cytomine server and directly launched from the graphical interface on dedicated hardware.

Last Updated: 5/11/2021, 1:45:45 PM