"""

"""
import json, os, inspect, sys

# Set PYTHONPATH before running Snakemake :
# export PYTHONPATH=$PYTHONPATH:/home/haars001/scratch/projects/VLPB/
# Install snakemake like this :
# virtualenv --python=python3 venv && source venv/bin/activate && pip3 install snakemake biopython
# Get lib configuration
from SnakeMakeVlpb import VLPB_LIB_PATH
from SnakeMakeVlpb.src.utils.CreateSymlinkReads import createSymlinks

ruleDir = VLPB_LIB_PATH + "rules/"

"""
Recursive function to do a string format on each of the string values in the config.
    Full replace on a JSON does not work: it uses '{}' for formatting, which results
    in errors.
"""
def replace_paths_in_json(conf, paths):
    for item in conf:
        if isinstance(conf[item], str): #is it a string value?
            conf[item] = conf[item].format(**paths) #use json to replace values
        elif isinstance(conf[item], dict): # is this a json object (dict) ?
            replace_paths_in_json(conf[item], paths) # parse object

# Get the config file:
with open("config.json") as conf:
    with open("paths.json") as paths:
        PATHS = json.load(paths)
        CONFIG = json.load(conf)
        replace_paths_in_json(CONFIG, PATHS)

## Create a symlink so the data is perfectly fit for all rules. All reads contain the name of their library.
## All forward reads are {library name}_1.fastq and all reversed reads are {library name}_2.fastq
createSymlinks(CONFIG)

# List of samples
samples, = glob_wildcards("reads/{S}.fastq.gz")


#Preprocessing
include: ruleDir + "fastqProcessors/Trimmomatic.rules"                  # Trimmomatic performs a variety of useful trimming tasks for illumina paired-end and single ended data.
include: ruleDir + "fastqProcessors/bfc.rules"                          # Clean reads
include: ruleDir + "fastqProcessors/FastqMcf.rules"                     # Deduplicate and filter reads
include: ruleDir + "mappers/Bowtie.rules"                               # Needed for next step

# include: ruleDir + "fastqProcessors/ContaminationFiltering.rules"

#Assembly
#include: ruleDir + "assemblers/Wgs.rules"


rule all:
    input:
        sorted(expand("trimmed/{S}.fastq.gz", S=samples))
        ,sorted(expand("cleaned/{S}.fastq.gz", S=samples))
        # ,sorted(expand("dedup/{S}.fastq.gz", S=samples))

