###############################################################################
#                         The Workflow example
###############################################################################
# This is the workflow example. It provides an example of one of the many
# different workflows that can be used for experiments with Aevol.
#
# The main idea underlying this workflow is to parallel wet lab experiments,
# which are conducted on evolved organisms.
# To use already evolved organisms for Aevol experiments, one can either use
# an evolved genome provided by the community or evolve his own.
# This example does the latter (more complete) case.
#
#
###################
# RECOMMENDATIONS #
###################
#
# It is strongly recommended you read the corresponding section in the user
# manual while following this example since it contains an explanation for
# each step of the example.
#
# The following set of commands should work as is if you have installed aevol
# with make install, which is recommended.
#
# If you haven't installed aevol, you will nedd to either add the binaries'
# locations in your PATH or to specify where to find them at each step.
# They should be in <aevol_dir>/src for the 4 main executables
# and in <aevol_dir>/src/post_treatments for the rest, <aevol_dir> being the
# main aevol directory you have downloaded.
# E.g. if your <aevol_dir> is /home/login/aevol, then the command
# aevol_run -n 5000 will become /home/login/aevol/src/aevol_run -n 5000
# and ae_misc_lineage -e 10000 will become
# /home/login/aevol/src/post_treatments/ae_misc_lineage -e 10000
#
###############################################################################


# ========== Wild-Type generation ==========
#
#mkdir wild_type
cd wild_type
aevol_create
aevol_run -n 5000 -p -1
# "-p -1" means you ask aevol to be run on n threads, with n
# being determined by your system's settings


# ========== Experimental setup ==========
#
# Propagate the experiment, meaning prepare directories for different
# runs starting from the wild type
# By default, the random generator states are modified so that you
# don't end up with the exact same simulation repeated several times
# but rather different repetitions with the same set of parameters
#
outdirsA="line01 line02 line03 line04 line05"
outdirsB="line06 line07 line08 line09 line10"
outdirsC="line11 line12 line13 line14 line15"
for mydir in $outdirsA $outdirsB $outdirsC
do
  aevol_propagate -o ../$mydir
done

# Go back to the workflow example directory
cd ..

# For each experiment, create a file with the parameters to change
echo "# New environment
ENV_GAUSSIAN  0.5   0.2   0.05
ENV_GAUSSIAN  0.5   0.4   0.05
ENV_GAUSSIAN  0.5   0.8   0.05
ENV_VARIATION none
# New rearrangement rates
DUPLICATION_RATE    1e-4
DELETION_RATE       1e-4
TRANSLOCATION_RATE  1e-4
INVERSION_RATE      1e-4" > newparam-groupA.in

echo "# New environment
ENV_GAUSSIAN  0.5   0.2   0.05
ENV_GAUSSIAN  0.5   0.4   0.05
ENV_GAUSSIAN  0.5   0.8   0.05
ENV_VARIATION none
# New rearrangement rates
DUPLICATION_RATE    1e-6
DELETION_RATE       1e-6
TRANSLOCATION_RATE  1e-6
INVERSION_RATE      1e-6" > newparam-groupB.in

echo "# New environment
ENV_GAUSSIAN  0.5   0.2   0.05
ENV_GAUSSIAN  0.5   0.4   0.05
ENV_GAUSSIAN  0.5   0.8   0.05
ENV_VARIATION none" > newparam-groupC.in


# Apply these modifications
#
for mydir in $outdirsA
do
   cd $mydir
   aevol_modify -f ../newparam-groupA.in
   cd ..
done

for mydir in $outdirsB
do
  cd $mydir
  aevol_modify -f ../newparam-groupB.in
  cd ..
done

for mydir in $outdirsC
do
  cd $mydir
  aevol_modify -f ../newparam-groupC.in
  cd ..
done




# ========== Run the simulations ==========
#
for mydir in $outdirsA $outdirsB $outdirsC
do
  cd $mydir
  aevol_run -e 20000 -p -1
  cd ..
done

# You can stop any simulation at any time and resume it from where you've
# stopped it with the exact same command (from within the same directory)
# e.g.
# cd line01
# aevol_run -e 20000 -p -1



# ========== Analyse the outcome ==========
#
# A set of post-treatment tools is available to help analyse the outcome.
# 

# ---------- aevol_misc_view ----------
#
# The simplest miscellaneous tool is view. It allows one to
# visualize a simulation at a given timestep using the exact
# same graphical outputs used in aevol_run.
# However, since it relies on graphics, it is only available when aevol is
# compiled with x enabled (which is the default).
#
#cd line01
#aevol_misc_view
#cd ..


# ---------- aevol_misc_create_eps ----------
#
# Similarly, one can obtain eps outputs for a given generation with the 
# create_eps tool. Files will be outputted in eps_files_xxxxxx (with 
# xxxxxx the generation number)
#
for mydir in $outdirsA $outdirsB $outdirsC
do
  cd $mydir
  aevol_misc_create_eps &
  cd ..
done


# ---------- aevol_misc_robustness ----------
#
# The robustness tool computes the replication statistics of all the 
# individuals of a given generation, like the proportion of neutral, beneficial, 
# deleterious offsprings. This is done by simulating nbchildren replications 
# for each individual (1000 replications by default), with its mutation, 
# rearrangement and transfer rates. Depending on those rates and genome 
# size, there can be several mutations per replication. Those global statistics 
# are written in stat/robustness_numgener.out, with one line per individual
# in the specified generation.
# The program also outputs detailed statistics for one of the individuals (the 
# best one by default). The detailed statistics for this individual are written in
# stats/replication_numgener.out, with one line per simulated child of this 
# particular individual.
#
for mydir in $outdirsA $outdirsB $outdirsC
do
   cd $mydir
   aevol_misc_robustness &
   cd ..
done


# ---------- aevol_misc_lineage ----------
#
# One can reconstruct the lineage of an evolved individual.
# This will generate a lineage file whose name will look like
# lineage-b000000-e010000-i999-r1000.ae containing the complete mutational
# history of a given individual of a given generation.
# This file can then be used as the input for subsequent post-treatments.
#
for mydir in $outdirsA $outdirsB $outdirsC
do
   cd $mydir
   aevol_misc_lineage
   cd ..
done


# ---------- aevol_misc_ancstats ----------
#
# Statistics of a lineage can be obtained with this tool.
# The generated output is in stats/ancstats/
#
# The option -M adds the list of mutational events that occurred
# on the lineage as an additional output (in stats/).
#
for mydir in $outdirsA $outdirsB $outdirsC
do
   cd $mydir
   aevol_misc_ancestor_stats lineage-*.ae -M &
   cd ..
done
