.. Track Analyzer - Quantification and visualization of tracking data
    Authors: Arthur Michaut                                                
    Copyright 2016-2019 Harvard Medical School and Brigham and             
                             Women's Hospital                              
    Copyright 2019-2021 Institut Pasteur and CNRS–UMR3738                  
    See the COPYRIGHT file for details                                     
                                                                           
    This file is part of Track Analyzer package.                           
                                                                           
    Track Analyzer is free software: you can redistribute it and/or modify 
    it under the terms of the GNU General Public License as published by   
    the Free Software Foundation, either version 3 of the License, or      
    (at your option) any later version.                                    
                                                                           
    Track Analyzer is distributed in the hope that it will be useful,      
    but WITHOUT ANY WARRANTY; without even the implied warranty of         
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the           
    GNU General Public License for more details .                          
                                                                           
    You should have received a copy of the GNU General Public License      
    along with Track Analyzer (COPYING).                                   
    If not, see <https://www.gnu.org/licenses/>.   


**********
Quickstart
**********


=================
Data requirements
=================

**Track Analyzer** needs as input a text file (csv or txt file) of tracked data containing the position coordinates (in 2D or 3D) along time and the tracks identifiers. 
Optionally, data can be plotted on the original image provided as a 3D or 4D tiff stack (ie. 2D+time or 3D+time). If the format of your movie is 
different (list of images), please convert it to tiff stack using `Fiji <https://fiji.sc/>`_ for instance. 

The position file must contain columns with the x, y, (z) positions, a frame column and track id column. The positions coordinates can be in 
pixels or in scaled data. The information about the scaling and other metadata such as time and length scales will be provided by the user through the graphical interface.

If **Track Analyzer** is run in command line (see below), the data directory must contain: 

- a comma-separated csv file named positions.csv which column names are: x, y, (z), frame, track
- a text file named info.txt containing the metadata (see example)
- (optional) a tiff file named stack.tif

..
  add a section descibing config and info files


====================
Running the pipeline
====================

There are two ways of running **Track Analyzer**: 

- using a Jupyter notebook based graphical interface (highly recommended)
- using terminal command lines

Using a notebook
================

Documentation about Jupyter notebooks can be found `here <https://jupyter.org/>`_. Briefly, a notebook comprises a series of 'cells' which are blocks 
of Python code to be executed. Each cell can be run by pressing Shift+Enter.
Each cell will execute a piece of code generating the pipeline graphical interface. They all depend on each other, therefore, they MUST be run in order.
By default, the code of each cell is hidden but it can be shown by pressing the button at the top of the notebook: 'Click here to toggle on/off the raw code'.
Once the code is hidden, you might miss a cell. This is a common explanation if you get an error. If this happens, start the pipeline again a couple of cells above.

To launch a notebook: 

- the notebook is at the root of the git repository, or you can just download it here: 
  :download:`run_TA.ipynb <../_static/run_TA.ipynb>` .
- go to the project folder, or where you downloaded the notebook by running :code:`cd <path_to_the_project_folder>` in a terminal
- activate the environment: run :code:`conda activate <env_name>` (see :ref:`installation<installation>` for more details)
- launch a Jupyter notebook, run :code:`jupyter notebook`
- a web browser opens, click on :code:`analyze_traj_gui.ipynb`
- to shut down the notebook, press CTRL+C in the terminal. 

Using command lines
====================

If you need to run **Track Analyzer** from a terminal without any graphical interface, it is possible, but you won't beneficiate from the interactive 
modules. Data filtering and analysis parameters will need to be passed through config files (see examples). **Track Analyzer** comes with two commands:

- :code:`traj_analysis` which runs the trajectory analysis section (see below). 
  It takes as arguments: path to data directory (optional: use the flag -r or --refresh to refresh the database)
- :code:`map_analysis` which runs the map analysis section (see below).
  It takes as arguments: path to data directory (optional: use the flag -r or --refresh to refresh the database)

..
  add third way with galaxy


==================
Analysis procedure
==================
 
**Track Analyzer** contains a data filtering section and three main analysis sections.

Load data
=========

Just follow the notebook instructions, to load your data files.

  .. image:: ../_static/screenshots/loading_data.png
    :align: center

If you run **Track Analyzer** for the first time, enter the metadata.

  .. image:: ../_static/screenshots/setting_metadata.png
    :align: center

You can also set some plotting parameters such as image file format, colors to be used, image resolution, etc. 

  .. image:: ../_static/screenshots/ploting_parameters.png
    :align: center

Data filtering section
======================

Subsets of the datasets can be filtered on spatiotemporal criteria: x, y, z positions, time subset and track duration.
A drawing tool also offers the possibility to hand-draw regions of interest. 

  .. image:: ../_static/screenshots/filter_subsets-1.png
    :align: center
  .. image:: ../_static/screenshots/filter_subsets-2.png
    :align: center

Additionally, specific trajectories can be selected by using their position in a region of interest at a specific time. This feature can be
useful to inspect either their past (back-tracking) or their future (fate-mapping). Trajectories can also be selected just using their ids.

These subsets can then be analyzed separately. The analysis will be run independently on each on them.
Alternatively, they can be analyzed together. Trajectories and computed quantities will then be plotted together using color-coding. 

  .. image:: ../_static/screenshots/subset_naming.png
    :align: center


Trajectory analysis section
===========================

Trajectories can be plotted over the original image, frame by frame, with some custom color-coding (z color-coded, t color-coded, subset, or random). 
The total trajectories can also be plotted together with the option to center their origin. This can be useful to detect some patterns in trajectories. 

Several quantities can be computed and plotted: velocities and acceleration (spatial components and its modulus). 
The local cell density can be estimated by performing a Voronoi tesselation. The Voronoi diagram can be plotted and the area of each Voronoi cell can 
be calculated and plotted. Currently, only the Voronoi tesselation in 2D (even if the data are 3D) is available.

All these quantities can also be averaged over the whole trajectory and plotted. 

Trajectories can also be quantified using the Mean Squared Displacement (MSD) analysis. The MSD can be plotted and fitted with some diffusion models 
to compute the diffusion coefficient.

  .. image:: ../_static/screenshots/trajectory_section.png
    :align: center


Map analysis section
====================

Data can be averaged on a regular grid to produce maps of such quantities. Two kinds of maps can be plotted: vector fields and scalar fields.

Vector fields
-------------

Velocity and acceleration vectors can be plotted on 2D maps. If 3D data, the z dimension can be color-coded. 
Such maps can be superimposed on a scalar field. 

Scalar fields
-------------

The velocity and acceleration components and moduli can be plotted as color-coded maps. The vector average moduli can also be computed. 
The difference between the velocity mean and the vector average modulus is that the velocity mean is the mean over all velocities in
the grid unit, while the vector average modulus is the modulus of the vector averaged in the grid unit. 
Divergence (contraction and expansion) maps, and curl (rotation) maps can also be plotted.

  .. image:: ../_static/screenshots/map_section.png
    :align: center

Comparator section
==================

Previously generated data by the trajectory analysis section can be compared by plotting parameters together on the same plots. 


======
Output
======

**Track Analyzer** generates several files, plots, data points, and configuration files. 

Database and configuration files
================================

Some files are necessary to the pipeline processing: 

- *data_base.p* is a binary collection of python objects generated when the initial tracking file is loaded. It allows the initial loading to be skipped if the pipeline is run several times on the same tracking data. It can be refreshed if necessary.
- *info.txt* is a text file containing important metadata: 'lengthscale', 'timescale', 'z_step', 'image_width', 'image_height', 'length_unit', 'time_unit', 'table_unit', 'separator'. It can be interactively generated using the notebook
- if the original image stack is 4D (3D+t), a `stack_maxproj.tif` is generated by performing a maximum projection over the z dimension, so a 2D image can be used for 2D based plotting
- if run using command lines, the parameters are passed using several configuration files stored in the *config* folder in data directory output

Data output
===========

The trajectory analysis and the map analysis are generated respectively in a traj_analysis and map_analysis directory. Each subset's analysis is saved in a new folder. 

In each subset's directory:  

- a *config* folder is generated with the configuration parameters used for this specific analysis
- *all_data.csv* stores the subset's table of positions
- *track_prop.csv* stores the averaged quantities along trajectories
- each plot is saved using an image format, size and resolution that can be chosen when the plotting parameters are set in notebook. Additionally, the default colors and color maps can be customized in the plotting parameters sections.
- the data points of each plot is saved in a csv file with the same name as the image file, so you can replot the data using your favorite plotting software


========
Examples
========

Real data
=========

You can get familiar with **Track Analyzer** by running it on example data. For instance, you can analyze data of a C. elegans developing embryo provided by `the cell tracking challenge <http://celltrackingchallenge.net/3d-datasets/>`_. Download the data directory containing trajectories and metadata (these positions were extracted following napari's tutorial):  

:download:`cell tracking challenge <../_static/example/Fluo-N3DH-CE.tar.gz>`

Additionally, you can download the original timelapse for optimal visualization. Download the `dataset <http://data.celltrackingchallenge.net/training-datasets/Fluo-N3DH-CE.zip>`_. And run the following :download:`python script <../_static/example/load_tracking.py>` to extract image and generate a single tiff file that you can use during the analysis.  
To run the script, open a terminal and run: ::

    pip install imagecodecs
    cd <path_to_script_folder>
    python load_tracking.py <path_to_dataset_folder>

You can also generate the positions by adding the flag :code:`-p`, it will generate the *positions.csv* file present in the archive. 

Warning: if you try to open the generated tiff file with `Fiji <https://fiji.sc/>`_, you will see that the t and z dimensions are not separated. You will have to run "stack to hyperstack" with z=35 and t=195. But this is only if you want to see the file in Fiji, you don't need to do this for **Track Analyzer**!


Synthetic data
==============

You can also analyze synthetic data that were generated to ensure that the analysis performed by **Track Analyzer** is correct. You can download several datasets :download:`here <../_static/example/synthetic_data.tar.gz>`. They all have a *param.csv* with the input values for each trajectory.


===============
Troubleshooting
===============

The 3D visualization and the drawing selection tool depend on the `napari <https://napari.org/>`_ package. 
The installation of this package can lead to issues depending on your system.
If you are not able to solve this installation, you will not be able to have access to 3D rendering. However, you will still be able to 
use **Track Analyzer** without the drawing tool, by using coordinates sliders in the graphical interface.

