"""Processes related to DNA/protein sequences"""
from pyppl import Proc, Box
#from .utils import runcmd, helpers, genenorm, write
from . import params
from . import delefactory, procfactory
from modkit import Modkit
Modkit().delegate(delefactory())

@procfactory
def _pConsvPerm():
	"""
	@name:
		pConsvPerm
	@description:
		Generate a null distribution of conservation scores.
	@input:
		`seed`: The seed to generate the random regions. Default: None
	@output:
		`outfile:file`: A file with mean conservation scores sorted descendingly.
	@args:
		`len`: The length of a random region. Default: 50
		`nperm`: Number of permutations. Default: 1000
		`gsize`: The chrom size file.
		`bedtools`: The path of bedtools.
		`bwtool`: The path of bwtool.
		`consvdir`: The directory containing bigwig files of conservation scores
			- The bigwig file should start with chr name: chrN.*
	@requires:
		[bwtool](https://github.com/CRG-Barcelona/bwtool)
		[bedtools](http://bedtools.readthedocs.io/en/latest/content/bedtools-suite.html)
	"""
	pConsvPerm               = Proc(desc = 'Generate a null distribution of conservation scores.')
	pConsvPerm.input         = 'seed'
	pConsvPerm.output        = 'outfile:file:consv-len{{args.len}}-nperm{{args.nperm}}-{{i.seed}}.txt'
	pConsvPerm.args.len      = 50
	pConsvPerm.args.nperm    = 1000
	pConsvPerm.args.consvdir = params.consvdir.value
	pConsvPerm.args.gsize    = params.gsize.value
	pConsvPerm.args.bedtools = params.bedtools.value
	pConsvPerm.args.bwtool   = params.bwtool.value
	pConsvPerm.args.seed     = None
	pConsvPerm.lang          = params.python.value
	pConsvPerm.script        = "file:scripts/seq/pConsvPerm.py"
	return pConsvPerm


@procfactory
def _pConsv():
	"""
	@name:
		pConsv
	@description:
		Get the conservation scores of regions.
		It uses wigFix to find the conservation scores.
		But first you have to convert those wigFix.gz files to bigWig files using ucsc-wigToBigWig
	@input:
		`bedfile:file`: The bedfile with regions in the same chromosome
		`permfile:file`:The permutaiton file generated by `pConsvPerm`, used to calculate p-values
	@output:
		`outfile:file`: The output file
	@args:
		`consvdir`:   The bigwig directory, the bigwig files must be named as "chrN.*.bw"
			- For example: `chr1.phyloP30way.bw`
		`bwtool`:   The path of bwtool executable. Default: `bwtool`
		`bedtools`: The path of bedtools executable. Default: `bedtools`
		`pval`:     Whether calculate pvalue of each region. Default: False
			- In this case, the `i.permfile` can be ignored.
	@requires:
		[bwtool](https://github.com/CRG-Barcelona/bwtool)
		[bedtools](http://bedtools.readthedocs.io/en/latest/content/bedtools-suite.html)
	"""
	pConsv               = Proc(desc = 'Get the conservation scores of regions.')
	pConsv.input         = "bedfile:file, permfile:file"
	pConsv.output        = "outfile:file:{{i.bedfile | fn}}-consv.bed"
	pConsv.args.bwtool   = params.bwtool.value
	pConsv.args.consvdir = params.consvdir.value
	pConsv.args.pval     = False
	pConsv.lang          = params.python.value
	pConsv.script        = "file:scripts/seq/pConsv.py"
	return pConsv


@procfactory
def _pPromoters():
	"""
	@name:
		pPromoters
	@description:
		Get the promoter regions in bed format of a gene list give in infile.
		Gene names are supposed to be normalized by `gene.pGeneNameNorm`
	@input:
		`infile:file`: the gene list file
	@output:
		`outfile:file`: the bed file containing the promoter region
	@args:
		`region`: The region to output. Default: `Box(up = 2000, down = None, withbody = False)`
			- `up`: The upstream distance to TSS.
			- `down`: The downstream distance to TSS. Defaults to `args.region.up` if `None`
			- `withbody`: Include gene body in the output region? Default: `False`
		`notfound`: How to deal with records can't be found. Default: `skip`
			- `skip` : Skip the record
			- `error`: Report error and exit
		`refgene`: The ref gene file. Default: `@params.refgene`
		`inopts` : The options for input file. Default: `Box(cnames = False, genecol = 0, delimit = "\t")`
			- `cnames`: Whether the input file has header
			- `genecol`: The 0-based index or the colname of gene column.
			- `delimit`: The delimit of the input file.
		`outopts`:  The options for output file. Default: `Box(cnames = False)`
	"""
	pPromoters               = Proc(desc = 'Get the promoter regions in bed format of a gene list give in infile.')
	pPromoters.input         = "infile:file"
	pPromoters.output        = "outfile:file:{{i.infile | fn}}-promoters.bed"
	pPromoters.args.region   = Box(up = 2000, down = None, withbody = False)
	pPromoters.args.notfound = 'skip' # error
	pPromoters.args.inopts   = Box(cnames = False, delimit = "\t")
	pPromoters.args.genecol  = 0
	pPromoters.args.refgene  = params.refgene.value
	pPromoters.lang          = params.python.value
	pPromoters.script        = "file:scripts/seq/pPromoters.py"
	return pPromoters

