#!/usr/bin/env python
"""VCF-Toolbox.

Usage:
  tb.py listvars <vcf>          
  tb.py plot <vcf> <x> [<y>]      [options]
  tb.py QC-Report <vcf>           [options]
  tb.py compare <vcf>             [options]
  tb.py -h | --help
  tb.py --version

Options:
  -h --help                   Show this screen.
  --version                   Show version.
  --title=<title>             Set Custom plot titles.
  --region=<region>           Restrict analysis to a particular region.
  --include=<filter-expr>     Use a custom filtering string with bcftools.
  --facet=<facet-var>         Facet analysis on a categorical variable.
  --split-format              When plotting genotype FORMAT fields, facet by sample.

"""
from docopt import docopt
from subprocess import call
from vcf import vcf
from utils import *
from plots import *


class opts:
  """ Defines options that can be overridden """
  functions = ""
  binwidth = ""
  add = ""


if __name__ == '__main__':
    args = docopt(__doc__, version='Naval Fate 2.0')
    print(args)

    v = vcf(args["<vcf>"])

    if args["listvars"] == True:
      v.list_vars()
    elif args["plot"] == True:
      if args["<y>"] is None:

        #======================#
        # Single Variable Plot #
        #======================#

        filename, r = v.query(args["<x>"])
        if args["<x>"] == "POS":
          # Facet by Chromosome Automatically
          print("")
          print(bc("Plotting Position; Automatically facetting by Chromosome","BOLD"))
          print("")
          # Setup Plot for chromosome.
          opts.add += " + \n  facet_grid(.~CHROM, scales='free_x')"
          opts.add += " + \n scale_x_continuous(labels = genetic_scale) "
          opts.functions += genetic_scale
        print r
        if r["number"] == 1 and r["type"] in ["Integer","Float"]:
          print(bc("Creating Histogram of %s" % r["df"],"BOLD"))
          var1 = r["df"]
          Rcode = histogram.format(**locals())
          with open(filename + ".R","w") as R:
            R.write(Rcode)
          call(["Rscript",filename + ".R"])

          
      else:

        #======================#
        # Two Variable Plot    #
        #======================#

        r = v.query(args["<x>"], args["<y>"])

    elif args["QC"] == True:
      print("List Variables")

    elif args["compare"] == True:
      print("List Variables")

    elif args["report"] == True:
      print("Generate summary report")
      
  # Run R script to generate plots

@@ -627,22 +627,22 @@
 --split-
-format
+sample
         
@@ -1587,17 +1587,16 @@
  %22 + %5Cn 
- 
 facet_gr
@@ -1972,117 +1972,82 @@
     
-  with open(filename + %22.R%22,%22w%22) as R:%0A            R.write(Rcode)%0A          call(%5B%22Rscript%22,filename + %22.R%22%5D)
+elif r%5B%22number%22%5D == 1 and r%5B%22type%22%5D in %5B%22Integer%22,%22Float%22%5D:%0A          pass
 %0A%0A  
@@ -2160,32 +2160,32 @@
 =============#%0A%0A
-
         r = v.qu
@@ -2209,24 +2209,137 @@
 args%5B%22%3Cy%3E%22%5D)
+%0A      %0A      with open(filename + %22.R%22,%22w%22) as R:%0A        R.write(Rcode)%0A      call(%5B%22Rscript%22,filename + %22.R%22%5D)
 %0A%0A    elif a

split samples option.