29 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 168 169 170 171 172 173
** 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/builder.js][builder.js]] file. By
     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.
279 280
*** 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
283 284 285 286 287 288 289 290 291 292
    [[../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 phantomjs). It
    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
293 294
*** Functional

295 296 297
**** Architecture
    The browser functional testing is done in the directory
    =browser/tests=, with Watir.
298 299 300 301 302 303 304 305 306
    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)

    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
308 309
    all the files matching the previous pattern. It also backs up the test
    reports as =ci_reporter= removes them before each file is run.
310 311 312 313

**** Installation

Mathieu Giraud's avatar
Mathieu Giraud committed
314 315 316
The following instructions are for Ubuntu.
For OS X, see

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
***** 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

351 352 353
This may install a =selenium-webdriver= gem whose version is ≥ 3. We highly
recommend that you force an install to a version < 3 (/e.g./ 2.53.4). However
the webdriver may not work with recent versions of Firefox (> 45).
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 379 380 381 382 383 384

**** Launch browser tests

make functional

**** 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

   Then the browser tests can be launched in headless mode with:
   #+BEGIN_SRC sh
   make headless

385 386 387 388
   It is possible to view the framebuffer content of =Xvfb= using =vnc=. To do so,
   1. =x11vnc -display :99 -localhost=
   2. =vncviewer :0=
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
**** Interactive mode
     For debugging purposes, it may be useful to launch Watir in interactive
     mode. In that case, you should launch =irb= in the =browser/tests= directory.

     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=

406 407
* Packaging

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
** 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

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
** 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/
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484

   $ mkdir -p debian/usr/bin

   And copy the vidjil binary

   $ cp vidjil debian/usr/bin

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

   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

   And place it in the correct folder.

   $ mkdir -p debian/DEBIAN
   $ cp control debian/DEBIAN/

   Now build the package and rename it.

   $ dpkg-deb --build debian
   $ mv debian.deb vidjil_<version>_all.deb

   It can be installed but running

   $ sudo dpkg -i vidjil_<version>_all.deb

   # 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`
485 486 487 488 489 490 491 492 493 494

*** 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
497 498 499 500 501 502 503
    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
    $ dh_make -n

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

506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
    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:

    vidjil usr/bin/

    Vidjil currently depends on some unpackaged files that need to be
    downloaded before compiling.
    $ mkdir browser
    $ make germline
    $ make data

    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
524 525
    that can be obtained here: (Thanks
    to Bas Wijnen <> for this script)
526 527 528 529 530

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

    You're done! You can now install the debian package with:
    $ sudo dpkg -i path/to/package
531 532

* Docker
RyanHerb's avatar
RyanHerb committed
533 534 535 536 537 538 539 540 541 542 543 544
 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
546 547
   - reporter     A monitoring utility that can be configured to send
     monitoring information to a remote server
548 549

** Configuring the Vidjil container
RyanHerb's avatar
RyanHerb committed
550 551 552 553 554 555 556 557 558 559
   If you are using this environment for use on localhost, everything should
   work out of the box, simply skip ahead to the section about building the
   image and running the services.

   However you may need to further configure the setup in order to make it
   available to a whole network.
   Here is a list of the configuration files found in the vidjil directory:
     conf/conf.js                             contains various variables for the vidjil browser
     conf/                             contains various variables for the vidjil server
     conf/gzip.conf                           configuration for gzip in nginx
     conf/gzip_static.conf                    same as the previous but for static resources
RyanHerb's avatar
RyanHerb committed
561 562 563 564 565 566 567 568 569 570 571 572
     conf/uwsgi.ini                           configuration required to run vidjil with uwsgi
     sites/nginx                              configuration required when running vidjil with nginx
     scripts/              entrypoint for the nginx
     service (not currently in use)
     scripts/              entrypoint for the uwsgi
     service. Ensures the owner of some relevant volumes are correct within
     the container and starts uwsgi

  Here are some notable configuration changes you should consider:
    - Change the mysql user/password in docker-compose.yml. You will also
      need to change the DB_ADDRESS in conf/ to match it.

573 574
    - Change the hostname in the nginx configuration vidjil/sites/nginx_conf.
      If you are using vidjil on a network, then this might be required.
RyanHerb's avatar
RyanHerb committed
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591

    - Change the default admin password. Login as password 1234
      and go to the following URL: https://<your

    - Change the ssl certificates. When building the image vidjil-server
      which creates a self-signed certificate for the sake of convenience to
      ensure the HTTPS queries work from the start, but this may not be
      acceptable for a production environment.
      In order to replace certificates the current method is to mount the
      certificates to /etc/nginx/ssl with docker volumes in

    - Change the FROM_EMAIL and ADMIN_EMAILS variables in conf/ These
      represent the sender email address and the destination email addresses,
      used in reporting patient milestones and server errors.

592 593 594 595 596 597
    - Change the database password. In the mysql directory you will find an
      entrypoint script which creates the database, the user and set that
      user's password.
      This is the password  you need to match in the file in the
      vidjil configuration.

RyanHerb's avatar
RyanHerb committed
598 599 600 601 602
    - Change the volumes in docker-compose.yml. By default all files that
      require saving outside of the containers (the database, uploads, vidjil
      results and log files) are stored in /opt/vidjil , but  you can change
      this by editing the paths in the volumes.

    - Configure the reporter. Ideally this container should be positioned
RyanHerb's avatar
RyanHerb committed
604 605 606 607
      on a remote server in order to be able to report on a down server, but we have packed it here for convenience.

** 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.
RyanHerb's avatar
RyanHerb committed
609 610 611 612 613 614 615 616 617 618 619 620
   The docker image is not on the DockerHub and therefore needs to be built
   before it can be used.

   $ docker-compose build

   Running the services:

   $ docker-compose up

   Shorthand for the two previous commands:

   $ docker-compose up --build
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654

* Migrating Data
** Database
   The easiest way to perform a database migration is to first extract the
   data with the following command:

   $ mysqldump -u <user> -p <db> -c --no-create-info > <file>

   An important element to note here is the --no-create-info we add this
   parameter because web2py needs to be allowed to create tables itself
   because it keeps track of database migrations and errors will occur if
   tables exist which it considers it needs to create.

   In order to import the data into an installation you first need to ensure
   the tables have been created by Web2py this can be achieved by simply
   accessing a non-static page.

   /!\ If the database has been initialised from the interface you will
   likely encounter primary key collisions or duplicated data, so it is best
   to skip the initialisation altogether.

   Once the tables have been created, the data can be imported as follows:

   $ mysql -u <user> -p <db> < <file>

   Please note that with this method you should have at least one admin user
   that is accessible in the imported data. Since the initialisation is being
   skipped, you will not have the usual admin account present.
   It is also possible to create a user directly from the database although
   this is not the recommended course of action.

** Files
   Files can simply be copied over to the new installation, their filenames
   are stored in the database and should therefore be accessible as long as
   they are in the correct directories.
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679

** Filtering data
   When extracting data for a given user, the whole database should not be
   copied over.
   There are two courses of action:
     - create a copy of the existing database and remove the users that are
       irrelevant. The cascading delete should remove any unwanted data
       barring a few exceptions (notably fused_file, groups and sample_set_membership)

     - export the relevant data directly from the database. This method
       requires multiple queries which will not be detailed here.

  Once the database has been correctly extracted, a list of files can be
  obtained from sequence_file, fused_file, results_file and analysis_file
  with the following query:

  $ SELECT <filename field>
    FROM <table name>
    INTO OUTFILE 'filepath'

  Note: We are managing filenames here which should not contain any
  character such as quotes or commas so we can afford to refrain from
681 682 683 684 685 686
  enclosing the data with quotes.

  This query will output a csv file containing a filename on each line.
  Copying the files is now just a matter of running the following script:

  $ sh copy_files <file source> <file destination> <input file>