# N=1

# config={"dir":"simulated","startingTree":"data/sampledTree.tree","fileName":"simulated","lastSampleDate":2019.03}
# config={"dir":"EBOV2018","outputDir": "results/run1","startingTree":"data/147_phylogeog.CA_MCC.tree","fileName":"EBOV2018-29-05","lastSampleDate":2019.03}
config={"dir":"EBOV2018","outputDir": "results/run2","startingTree":"data/147_phylogeog.CA_MCC.tree","fileName":"EBOV2018-29-05","lastSampleDate":2019.03}

rule all:
    input:       
        "%s/%s/%s.json" %(config["dir"],config["outputDir"],config["fileName"]),
         "%s/%s/%s.trees" %(config["dir"],config["outputDir"],config["fileName"]),
        "%s/%s/%s_links.csv" %(config["dir"],config["outputDir"],config["fileName"]),
        "%s/%s/%s_lineList.csv" %(config["dir"],config["outputDir"],config["fileName"]),

# rule subsample:
#     message: 
#         "Sampling 1 tree from {input.trees}."
#     input:
#           trees = "simulated/data/sampledTree.tree"
#     params:
#         burninTrees=0
#     output:
#           # sampledTree="results/run4/sampledTrees/sampledTree.{i}.tree"
#           sampledTree="results/run4/sampledTrees/sampledTree.tree"

#     shell:
#         """
#         python3 scripts/sampleNexus.py -t {input.trees} -o {output.sampledTree} -n 1 -b {params.burninTrees}
#         """

rule transphylo:
    message:
      "Estimating transmission chain with transphylo on {input.tree}"
    input:
      # tree=rules.subsample.output.sampledTree
      tree="%s/%s" %(config["dir"],config["startingTree"])
    output:
      treeLog = "%s/%s/%s.trees" %(config["dir"],config["outputDir"],config["fileName"]),
      treeAnnotation = "%s/%s/%s.json" %(config["dir"],config["outputDir"],config["fileName"]),
      links = "%s/%s/%s_links.csv" %(config["dir"],config["outputDir"],config["fileName"]),
      lineList = "%s/%s/%s_lineList.csv" %(config["dir"],config["outputDir"],config["fileName"]),

    log: "%s/%s/%s.log" %(config["dir"],config["outputDir"],config["fileName"])
    params:
      dateLastSample=config["lastSampleDate"],
      shape=8.7,
      scale=0.0046,
      MCMC= 10000000,
      thinning=1000,
      samples = 9000,
      file = "%s/%s/%s" %(config["dir"],config["outputDir"],config["fileName"])
    shell:
      """
        scripts/transphylo.R \
          --tree {input.tree} \
          --dateLastSample {params.dateLastSample} \
          --shape={params.shape} \
          --scale={params.scale} \
          --MCMC={params.MCMC} \
          --thinning={params.thinning} \
          --samples={params.samples}\
          --file={params.file}
        """
    
# rule logCombiner:
#   message:
#     "Combining logs from {N} transphylo runs"
#   input:
#     expand("results/run4/Transphylo/sampledTree.{i}.log",i=range(1,N))
#   output:
#     combinedLog="results/run4/combinedLog.log"
#   params:
#     burnin=10000000,

#   shell:
#     """
#       logcombiner -burnin {params.burnin} -renumber {input} {output.combinedLog}
#     """

# rule treeCombiner:
#   message:
#     "Combining trees from {N} transphylo runs"
#   input:
#     expand("results/run4/Transphylo/sampledTree.{i}.log",i=range(1,N))
#   output:
#     combinedLog="results/run4/combinedTrees.trees"
#   params:
#     burnin=10000000,
#     resample=1000000, # N times 

#   shell:
#     """
#       logcombiner -trees -burnin {params.burnin}  -renumber {input} {output.combinedLog}
#     """
