#!/usr/bin/env python
# -*- coding: UTF-8 -*-

"""
Subroutines to aid MERACULOUS assembly.
"""

import os.path as op
import sys

from jcvi.formats.fastq import readlen
from jcvi.formats.base import write_file
from jcvi.assembly.base import FastqNamings, get_libs
from jcvi.utils.table import comment_banner, load_csv
from jcvi.apps.base import OptionParser, ActionDispatcher


header = """
lib_seq [ wildcard ][ prefix ][ insAvg ][ insSdev ][ avgReadLen ][
hasInnieArtifact ][ isRevComped ][ useForContigging ][ scaffRound ][
useForGapClosing ][ 5pWiggleRoom ][3pWiggleRoom]
""".translate(None, "[]\n")


def main():

    actions = (
        ('prepare', 'prepare meraculous input'),
            )
    p = ActionDispatcher(actions)
    p.dispatch(globals())


def prepare(args):
    """
    %prog prepare genomesize *.fastq

    Prepare MERACULOUS configuation file. Genome size should be entered in Mb.
    """
    p = OptionParser(prepare.__doc__ + FastqNamings)
    p.add_option("-K", default=51, type="int", help="K-mer size")
    p.set_cpus()
    opts, args = p.parse_args(args)

    if len(args) < 2:
        sys.exit(not p.print_help())

    genomesize = float(args[0]) / 1000
    fnames = args[1:]
    for x in fnames:
        assert op.exists(x), "File `{0}` not found.".format(x)

    s = comment_banner("Meraculous params file") + "\n"
    s += comment_banner("Basic parameters") + "\n"
    s += "# Describe the libraries ( one line per library )\n"
    s += "# " + " ".join(header.split()) + "\n"

    libs = get_libs(fnames)
    lib_seqs = []
    rank = 0
    for lib, fs in libs:
        size = lib.size
        if size == 0:
            continue
        rank += 1
        wildcard = fs[0].replace(".1.", ".?.")
        rl = max(readlen([x]) for x in fs)
        name = lib.library_name.replace("-", "")
        lib_seq = lib.get_lib_seq(wildcard, name, rl, rank)
        lib_seqs.append(lib_seq)

    s += "\n" + "\n".join(load_csv(None, lib_seqs, sep=" ")) + "\n"
    params = [("genome_size", genomesize),
              ("is_diploid", 1),
              ("mer_size", opts.K),
              ("num_prefix_blocks", 1),
              ("no_read_validation", 1),
              ("local_num_procs", opts.cpus)]
    s += "\n" + "\n".join(load_csv(None, params, sep=" ")) + "\n"

    cfgfile = "meraculous.config"
    write_file(cfgfile, s, tee=True)

    s = "~/export/meraculous/meraculous/bin/run_meraculous.sh -c {0}"\
                .format(cfgfile)
    runsh = "run.sh"
    write_file(runsh, s)


if __name__ == '__main__':
    main()

@@ -1038,16 +1038,23 @@
 et_cpus(
+cpus=48
 )%0A    op
@@ -1690,137 +1690,186 @@
     
-wildcard = fs%5B0%5D.replace(%22.1.%22, %22.?.%22)%0A        rl = max(readlen(%5Bx%5D) for x in fs)%0A        name = lib.library_name.replace(%22-%22, %22%22
+library_name = lib.library_name%0A        name = library_name.replace(%22-%22, %22%22)%0A        wildcard = %22%7B0%7D*.1.*,%7B0%7D*.2.*%22.format(library_name)%0A        rl = max(readlen(%5Bx%5D) for x in fs
 )%0A  
@@ -2100,17 +2100,17 @@
 ploid%22, 
-1
+0
 ),%0A     
@@ -2217,17 +2217,17 @@
 ation%22, 
-1
+0
 ),%0A     

change assembly.meraculous.prepare() defaults