26.1 KB
Newer Older
#+TITLE: Vidjil -- Developer Documentation
#+AUTHOR: The Vidjil team (Mathieu, Mikaël, Aurélien, Florian, Marc, Ryan and Tatiana)
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="org-mode.css" />
4 5 6 7 8 9

# This manual can be browsed online:
#               (last stable release)
#    (development version)

# Vidjil -- High-throughput Analysis of V(D)J Immune Repertoire -- [[]]
# Copyright (C) 2011-2017 by Bonsai bioinformatics
11 12 13
# at CRIStAL (UMR CNRS 9189, Université Lille) and Inria Lille

Mathieu Giraud's avatar
Mathieu Giraud committed
14 15
Here are aggregated notes forming the developer documentation on the Vidjil components, algorithm,
web application client and server.
This documentation is a work-in-progress, it is far from being as polished as the user documentation
Mathieu Giraud's avatar
Mathieu Giraud committed
17 18
Help can also be found in the source code and in the commit messages.


20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
* Algorithm
** Code organisation
   The algorithm follows roughly those steps:
   1. The germlines are read. germlines are in the fasta format and are read
      by the Fasta class (=core/fasta.h=). Germlines are built using the
      Germline (or MultiGermline) class (=core/germline.h=)
   2. The input sequence file (.fasta, .fastq, .gz) is read by an OnlineFasta
      (=core/fasta.h=). The difference with the Fasta class being that all the
      data is not stored in memory but the file is read online, storing only
      the current entry.
   3. Windows must be extracted from the read, which is done by the
      WindowExtractor class (=core/windowExtractor.h=). This class has an
      =extract= method which returns a WindowsStorage object
      (=core/windows.h=) in which windows are stored.
   4. To save space consumption, all the reads linked to a given window are
      not stored. Only the longer ones are kept. The BinReadStorage class is
      used for that purpose (=core/read_storage.h=).
   5. In the WindowStorage, we now have the information on the clusters and on
      the abundance of each cluster. However we lack a sequence representative
      of the cluster. For that purpose the class provides a
      =getRepresentativeComputer= method that provides a
      KmerRepresentativeComputer (=core/representative.h=). This class can
      compute a representative sequence using the (long) reads that were
      stored for a given window.
   6. The representative can then be segmented to determine what V, D and J
      genes are at play. This is done by the FineSegmenter (=core/segment.h=).
46 47 48 49 50 51 52 53 54
** The xxx germline
   - All germlines are inserted in one index using =build_with_one_index()= and
     the segmentation method is set to =SEG_METHOD_MAX12= to tell that the
     segmentation must somehow differ.
   - So that the FineSegmenter correctly segments the sequence, the =rep_5= and
     =rep_3= members (class =Fasta=) of the xxx germline are modified by the
     FineSegmenter. The =override_rep5_rep3_from_labels()= method from the
     Germline is the one that overwrites those members with the Fasta
     corresponding to the affectation found by the KmerSegmenter.
55 56 57 58
* Browser

** Installation

59 60
Run a =make= into =browser/= to get the necessary files.
This will in particular get the germline files as well as the icon files.

62 63
Opening the =browser/index.html= file is then enough to get a functionning browser,
able to open =.vidjil= files with the =import/export= menu.
64 65 66

To work with actual data, the easiest way is to copy =js/conf.js.sample= to =js/conf.js=.
This will unlock the =patients= menu and allow your local browser
Mathieu Giraud's avatar
Mathieu Giraud committed
to access the public server at
68 69 70 71 72 73 74 75

** Browser API

The browser can be opened on a data file specified from a =data= attribute,
and optionally on an analysis file specified from a =analysis= attribute,
as in the following URLs on our test server:

Mathieu Giraud's avatar
Mathieu Giraud committed
76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

Both GET and POST requests are accepted.
Note that the =browser/index.html= file and the =.vidjil/.analysis= files should be hosted on the same server.
Otherwise, the server hosting the =.vidjil/.analysis= files must accept cross-domain queries.

** Architecture

The Vidjil browser is a set of /views/ linked to a same /model/.
The model keeps the views in sync on some global properties,
most notably dealing with the selection of clones, with the clone filtering,
as well with the locus selection.

- The model (=js/model.js=) is the main object of the Vidjil browser.
  It loads and saves =.vidjil= json data (either directly from data, or from a local file, or from some url).
  It provides function to access and edit information on the clones and on the global parameters
  It keeps all the views in sync.

- Each of the views (=Graph=, =ScatterPlot=, =List=, =Segment=) is rendered inside one or several =<div>= elements,
  and kept sync with the model. All the views are optional, and several views of the same type can be added.
  See =js/main.js= for the invocation
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

- The link with the patient database/server is done with the =Database= object (=js/database.js=)

- Other objects: =Report=, =Shortcut=
  Extends functionalities but requires elements from the full =index.html=.

** Integrating the browser

*** HTML and CSS
  - The =index.html= contains the =<div>= for all views and the menus
  - The CSS (=css/light.css=) is generated by =less= from =css/vidjil.less=

  - The =small_example.html= is a minimal example embedding basic HTML, CSS, as well as some data.
    As the menus are not embedded in this file, functionalities should be provided by direct calls to the models and the views.

*** Javascript
  - The wonderful library =require.js= is used, so there is only one file to include
    <script data-main="js/app.js" src="js/lib/require.js"></script>

  - =js/main.js= creates the different views and binds them to the model.
    Another option is to directly define a function named =main()=, as in =small_example.html=.

*** JSON .vidjil data

Clone lists can be passed to the model through several ways:
  - directly by the user (import/export)
  - from a patient database (needs a database)
  - trough the API (see below)
  - or by directly providing data through Javascript (as in =small_example.html=)

The first three solutions need some further elements from the full =index.html=.
133 134

135 136 137 138
** Notifications
*** Priority
    The priority determines how the notification are shown and what action the
    user should do. The priorities can be between 0 and 3.
140 141 142 143 144 145 146
    - 0 :: The notification is not shown
    - 1 :: The notification is shown (usually on green background) and
         automatically disappears
    - 2 :: The notification is shown (usually on yellow background) and
         automatically disappears
    - 3 :: The notification is shown (usually on red background) and doesn't
         disappear until the user clicks on it.

    In the =console.log=, the field =priority= takes one of those priorities.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
** Plots
*** How to add something to be plotted
    You want to add a dimension in the scatterplot or as a color? Read the
**** Scatterplot
     In [[file:../browser/js/scatterPlot.js][scatterPlot.js]], the =available_axis= object defines the dimensions that
     can be displayed. It suffices to add an entry so that it will be proposed
     in the X and Y axis. This kind of way of doing should be generalized to
     the other components.

     The presets are defined in the =preset= object.
**** Color
     Adding a color needs slightly more work than adding a dimension in the

     The function =updateColor= in file [[file:../browser/js/clone.js][clone.js]] must be modified to add our color method.
     The variable =this.color= must contain a color (either in HTML or RGB, or…).

     Then a legend must be displayed to understand what the color represents.
     For this sake, modify the =build_info_color= method in [[file:../browser/js/info.js][info.js]] file. By
169 170 171 172 173
     default four spans are defined (that can be used) to display the legend:
     =span0=, ..., =span3=.

     Finally modify the [[file:../browser/index.html][index.html]] file to add the new color method in the
     select box (which is under the =color_menu= ID).
174 175 176 177 178 179 180
** Classes
*** Clone
**** Info box
     In the info box all the fields starting with a _ are put. Also all the
     fields under the =seg= field are displayed as soon as they have a =start= and
     =stop=. Some of them can be explicitly not displayed by filling the
     =exclude_seg_info= array in =getHtmlInfo=.

182 183
* Server
** Notifications
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

The news system is a means of propagating messages to the users of a vidjil server installation.
Messages are propagated in near-realtime for users interacting directly with the server and at a slightly slower rate for users simply using the browser but for which the server is configured.

*** Message Retrieval
The browser by default periodically queries the server to retrieve any new messages and are displayed on a per user basis. This means that any message having already been viewed by the user is not displayed in the browser.
Older messages can be viewed from the index of news items.

*** Caching
News items are kept in cache in order to relieve the database from a potentially large amount of queries.
The cache is stored for each user and is updated only when a change occurs (message read, message created or message edited).

*** Formatting
   Messages can be formatted by using the Markdown syntax. Syntax details are
   available here:

200 201 202
*** Priority
    The priority determines how the notification is shown (see [[browser:priority][here for more
    details]]). From the server we have two ways of modifiying the priority.
    Either by defining the =success= field to ='true'= or to ='false'=, or
204 205 206
    by explicitly specifying the priority in the field =priority=.

    For more details see 35054e4
207 208 209 210 211 212 213 214 215 216 217
** Getting data and analysis
   How the data files (.vidjil) and analysis files are retrieved from the server?
*** Retrieving the data file
    This is done in the controller under the =get_data= function.
    However the .vidjil file is not provided as its exact copy on the
    server. Several informations coming from the DB are fed to the file
    (original filename, time stamps, information on each point, …)
*** Retrieving the analysis file
    This is done in the controller under the =get_analysis= function.
    Actually the real work is done in the model, in the
    =get_analysis_data= function.
Mikaël Salson's avatar
Mikaël Salson committed
218 219 220 221
** Permissions
   Permissions are handled by Web2py's authentication mechanism which is
   specialised to Vidjil's characteristics through the =VidjilAuth= class.

222 223
** Database
*** Export
    mysqldump -u <user> -p <database> -c --no-create-info > <file>
*** Import
    In order to import the data from another server, you need to ensure
227 228 229 230 231 232 233 234 235 236 237
    there will be no key collision, or the import will fail.
    If the database contains data, the easiest is to drop the database and
    create a new empty database.
    This will require you to delete the .table file in web2py/applications/vidjil/databases
    In order to create the tables you should then load a page from the
    webapp, but DO NOT init the database, because this will raise the problem
    of colliding primary keys again.

    Then run:
    mysql -u <user> -p <database> < file

Mikaël Salson's avatar
Mikaël Salson committed
238 239 240 241 242 243 244 245 246 247
*** VidjilAuth
   One VidjilAuth is launched for a given user when a controller is called.
   During that call, we cache as much as possible the calls to the DB.  For
   doing so the =get_permission= method is defined (overriding the native
   =has_permission=). It calls the native =has_permission= only when that call
   hasn't already been done (this is particularly useful for DB intensive
   queries, such as the compare patients).

   Also some user characteristics are preloaded (groups and whether the person
   is an admin), which also prevents may DB calls.
* Tests
249 250 251 252 253 254 255
** Algorithm
*** Unit
    Unit tests are managed using an internal lightweight poorly-designed
    library that outputs a TAP file. They are organised in the directory

    All the tests are defined in the [[../algo/tests/tests.cpp][tests.cpp]] file. But, for the sake of
256 257
    clarity, this file includes other =cpp= files that incorporate all the
    tests. A call to =make= compiles and launches the =tests.cpp= file, which
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    outputs a TAP file (in case of total success) and creates a =tests.cpp.tap=
    file (in every case).
**** Tap test library
     The library is defined in the [[../algo/tests/testing.h][testing.h]] file.

     Tests must be declared in the [[../algo/tests/tests.h][tests.h]] file:
     1. Define a new macro (in the enum) corresponding to the test name
     2. In =declare_tests()= use =RECORD_TAP_TEST= to associate the macro with a
        description (that will be displayed in the TAP output file).

     Then testing can be done using the =TAP_TEST= macro. The macro takes three
     arguments. The first one is a boolean that is supposed to be true, the
     second is the test name (using the macro defined in =tests.h=) and the
     third one (which can be an empty string) is something which is displayed
     when the test fails.

** Browser
276 277 278
*** Code Quality
    Quality of code is checked using [[][JSHint]], by
    running =make quality= from the =browser= directory.
Mathieu Giraud's avatar
Mathieu Giraud committed
279 280 281

    Install with =npm install -g jshint=

282 283
*** Unit
    The unit tests in the browser are managed by QUnit and launched using
    [[][nightmare]], by launching =make unit= from the =browser/test= directory.
    The tests are organised in the directory
286 287 288
    [[../browser/test/QUnit/testFiles][browser/test/QUnit/testFiles]]. The file [[../browser/test/QUnit/testFiles/data_test.js][data_test.js]] contains a toy
    dataset that is used in the tests.

    Unit tests can be launched using a real browser (instead of nightmare). It
290 291 292 293 294 295
    suffices to open the file [[../browser/test/QUnit/test_Qunit.html][test_Qunit.html]]. In this HTML webpage it is
    possible to see the coverage. It is important that all possible functions
    are covered by unit tests. Having the coverage displayed under Firefox
    needs to display the webpage using a web server for security
    reasons. Under Chromium/Chrome this should work fine by just opening the
296 297 298 299 300 301

**** Installation
     Nightmare is distributed withing =node= and can therefore be installed with it.

     #+BEGIN_SRC sh
     apt-get install nodejs-legacy npm
302 303
     npm install nightmare -g # make -C browser/test unit will automatically
     link to global nightmare installation
304 305 306 307

     Note that using =nightmare= for our unit testing
     requires the installation of =xvfb=.
308 309 310 311 312 313 314 315 316 317 318

**** Debugging
     If there is a problem with the nightmare or electron (nightmare
     dependency), you may encounter a lack of output or error messages.
     To address this issue, run:

     #+BEGIN_SRC sh
     cd browser/test/QUnit
     DEBUG=nightmare*,electron:* node nightmare.js

319 320
*** Functional

321 322
**** Architecture
    The browser functional testing is done in the directory
    =browser/tests/functional=, with Watir.
324 325 326 327 328 329 330 331
    The functional tests are built using two base files:
    - vidjil_browser.rb :: abstracts the vidjil browser (avoid using IDs or
         class names that could change in the test). The tests must rely as
         much as possible on vidjil_browser. If access to some
         data/input/menus are missing they must be addded there.
    - browser_test.rb :: prepares the environment for the tests. Each test
         file will extend this class (as can be seen in test_multilocus.rb)

332 333 334
    The file =segmenter_test.rb= extends the class in =browser_test.rb= to adapt
    it to the purpose of testing the analyze autonomous app.

    The tests are in the files whose name matches the pattern =test*.rb=. The
    tests are launched by the script in =../launch_functional_tests= which launches
337 338
    all the files matching the previous pattern. It also backs up the test
    reports as =ci_reporter= removes them before each file is run.
339 340 341

**** Installation

Mathieu Giraud's avatar
Mathieu Giraud committed
342 343 344
The following instructions are for Ubuntu.
For OS X, see

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
***** Install rvm

  #+BEGIN_SRC sh
 \curl -sSL | bash

  Afterwards you may need to launch:
  #+BEGIN_SRC sh
  source /etc/profile.d/

***** Install ruby 2.1.1

rvm install 2.1.1

***** Switch to ruby 2.1.1

rvm use 2.1.1

***** Install necessary gems

gem install minitest
gem install minitest-ci
gem install watir-webdriver
gem install test-unit

This may install a =selenium-webdriver= gem whose version is ≥ 3. We highly
380 381 382 383 384 385 386 387 388
recommend that you force an install to a version < 3 (/e.g./ 2.53.4).

# Removing a new version of selenium-webdriver
gem uninstall selenium-webdriver
# Installing an older version
gem install selenium-webdriver -v 2.53.4

Mikaël Salson's avatar
Mikaël Salson committed
389 390 391 392
However the webdriver may not work with recent versions of Firefox (> 45).  In
such a case you would like to downgrade your Firefox version or use a former
version. The Firefox version used can be set with an environment variable (see
below). All Firefox releases are [[ ][available here]].
393 394 395 396 397 398 399

**** Launch browser tests

make functional

400 401 402 403 404 405 406 407
    By default the tests are launched on the Firefox installed on the system.
    This can be modified by providing the =FUNCTIONAL_CLIENT_BROWSER_PATH=
    environment variable (which can contain several pathes, separated with
    spaces) to the =launch_functional_tests= script.  Or, if one wants to launch
    individual test scripts, to set the =WATIR_BROWSER_PATH= environment

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
**** Headless mode

   On servers without a X server the browser tests can be launched in headless
   For this sake one needs to install a few more dependencies:

   #+BEGIN_SRC sh
   gem install headless

   The virtual framebuffer X server (=xvfb=) must also be installed. Depending
   on the operating system the command will be different:
   #+BEGIN_SRC sh
   # On Debian/Ubuntu
   apt-get install xvfb
   # On Fedora/CentOS
   yum install xvfb

427 428 429 430 431 432 433 434
   If you have set a configuration file (browser/js/conf.js), you should remove it during the headless testing. The easiest way to do it is to launch these commands before and after the test
   #+BEGIN_SRC sh
   # before tests
   mv browser/js/conf.js     browser/js/conf.js.bak 
   # after tests
   mv browser/js/conf.js.bak browser/js/conf.js    
435 436 437 438 439
   Then the browser tests can be launched in headless mode with:
   #+BEGIN_SRC sh
   make headless

440 441 442 443
   It is possible to view the framebuffer content of =Xvfb= using =vnc=. To do so,
   1. =x11vnc -display :99 -localhost=
   2. =vncviewer :0=
444 445
**** Interactive mode
     For debugging purposes, it may be useful to launch Watir in interactive
446 447
     mode. In that case, you should launch =irb= in the =browser/tests/functional=
448 449 450 451 452 453 454 455 456 457 458 459 460

     Then load the file =browser_test.rb= and create a =BrowserTest=:
     #+BEGIN_SRC ruby
       load 'browser_test.rb'
       bt = "toto"

       # Load the Vidjil browser with the given .vidjil file

     Finally you can directly interact with the =VidjilBrowser= using the =$b=

461 462 463 464 465 466 467 468 469 470 471
     Another way of debugging interactively is by using (and installing) the
     =ripl= gem. Then you should add, in the =.rb= file to debug:
     #+BEGIN_SRC ruby
     require 'ripl'
     Then if you want to stop launch an =irb= arrived at a given point in the
     code, the following command must be inserted in the code:
     #+BEGIN_SRC ruby
     Ripl.start :binding => binding


473 474
* Packaging

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
** Script driven building
   In order to make packaging Vidjil simple and facilitate releases scripts
   have been made and all meta data files required for the Debian packages
   can be found in the packaging directory in each package's subdirectory.

   In the packaging directory can be found the scripts for building each of
   the vidjil packages: germline, algo (named vidjil) and server.
   Note: is a helper script that is used by the other
   build-* scripts to build a package.

   Executing one of the scripts will copy the necessary files to the
   corresponding packaging subdirectory (germline, vidjil and server)
   And build the package in the /tmp folder along with all the files needed
   to add the package to a repository

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
** Packaging Vidjil into a Debian Binary Package
  In this section we will explain how to package a pre-compiled version of
  Vidjil that will allow easy installation although it will not meet all the
  requirements for a full Debian package and therefore cannot be added to the
  default Debian repositories.

  In this document we will not go over the fine details of debian packaging
  and the use of each file. For more information you can refer to this page
  from which this document was inspired:

  Being a binary package it will simply contain the vidjil binary which will
  be copied to the chosen location on installation.

*** Let's Get Started
   You will first and foremost need to compile vidjil. Refer to #TODO for
   more information.

   Create a base directory for the package and the folders to which the binary
   will be installed. Let's call our folder debian and copy the binary to /usr/bin/

511 512 513
   #+BEGIN_SRC sh
     mkdir -p debian/usr/bin
514 515 516

   And copy the vidjil binary

517 518 519
   #+BEGIN_SRC sh
     cp vidjil debian/usr/bin
520 521 522

   Now create the necessary control file. It should look something like this:

523 524 525 526 527 528 529 530 531 532 533 534
     Package: vidjil
     Version: <version> (ie. 2016.03-1)
     Section: misc
     Priority: optional
     Architecture: all
     Depends: bash (>= 2.05a-11)
     Maintainer: Vidjil Team <>
     Description: Count lymphocyte clones
     vidjil parses a fasta or fastq file and produces an output with a list
     of clones and meta-data concerning these clones
535 536 537

   And place it in the correct folder.

538 539 540 541
   #+BEGIN_SRC sh
     mkdir -p debian/DEBIAN
     cp control debian/DEBIAN/
542 543 544

   Now build the package and rename it.

545 546 547 548
   #+BEGIN_SRC sh
     dpkg-deb --build debian
     mv debian.deb vidjil_<version>_all.deb
549 550 551

   It can be installed but running

552 553 554
   #+BEGIN_SRC sh
     sudo dpkg -i vidjil_<version>_all.deb
555 556 557 558 559 560 561 562 563

   # TODO Add Changelog, copyright, etc.

** Packaging Vidjil into a Debian Source Package

  Note: This document is currently incomplete. This process will not produce a
  working debian package. The package build will fail when attempting to
  emulate `make install`
564 565 566 567 568 569 570 571 572 573

*** Requirements
   - The release version of Vidjil you wish to package
   - Knowledge of Debian packaging
   In this documentation we will not go over all the specifics of creating a
   debian package. You can find the required information here:

*** Creating the orig archive
    In order to build a debian package, it is required to have a folder named
    debian with several files required for the package which contain meta
576 577 578 579 580
    data and permit users to have information on packages and updates for

    In order to generate this folder run the following from the source base
581 582 583
    #+BEGIN_SRC sh
      dh_make -n

585 586
    You can remove all files from the debian folder that match the patterns *.ex, *.EX and README*

587 588 589 590 591 592
    Update debian/changelog, debian/control and debian/copyright to contain the correct
    information to reflect the most recent changes and metadata of Vidjil.

    Vidjil has no install rule so we need to use a debian packaging feature.
    Create a file named debian/install with the following line:

593 594 595
      vidjil usr/bin/
596 597 598

    Vidjil currently depends on some unpackaged files that need to be
    downloaded before compiling.
599 600 601 602 603 604

    #+BEGIN_SRC sh
      mkdir browser
      make germline
      make data
605 606 607 608 609

    Debian packaging also requires archives of the original source. This is
    to manage people packaging software they haven't developed with changes
    they have made. To make things simpler, we simply package the current
    source as the reference archive and build the package with the script
610 611
    that can be obtained here: (Thanks
    to Bas Wijnen <> for this script)
612 613 614 615

    From the source directory, run that script to create the package.

    You're done! You can now install the debian package with:
616 617 618 619

    #+BEGIN_SRC sh
      sudo dpkg -i path/to/package
620 621

* Docker
RyanHerb's avatar
RyanHerb committed
622 623 624 625 626 627 628 629 630 631 632 633
 The vidjil Docker environment is managed by Docker Compose since it is
 composed of several different services this allows us to easily start and
 stop individual services.
 The services are as follows:
   - mysql        The database
   - postfix      The mailing server
   - uwsgi        The Web2py backend server
   - fuse         The XmlRPCServer that handles custom fuses (for comparing
   - nginx        The web server
   - workers      The Web2py Scheduler workers in charge of executing vidjil
     users' samples
   - backup       Starts a cron job to schedule regular backups
RyanHerb's avatar
RyanHerb committed
635 636
   - reporter     A monitoring utility that can be configured to send
     monitoring information to a remote server

RyanHerb's avatar
RyanHerb committed
638 639 640

** Building and starting the environment
   Building the image is simple and can be done prior to editing the
   configuration files if you wish, although it is not necessary.
642 643
   The backup and reporter images are built based on the latest vidjil
   docker image from Dockerhub.
RyanHerb's avatar
RyanHerb committed

645 646 647
   #+BEGIN_SRC sh
     docker-compose build
RyanHerb's avatar
RyanHerb committed
648 649 650

   Running the services:

651 652 653
   #+BEGIN_SRC sh
     docker-compose up
RyanHerb's avatar
RyanHerb committed
654 655 656

   Shorthand for the two previous commands:

657 658 659
   #+BEGIN_SRC sh
     docker-compose up --build