#!/usr/bin/env python
#
# Copyright John Reid 2012, 2013, 2014
#

"""
Parses a motif file and creates a reStructuredText and an html file containing
the information and logos.
"""

import logging
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger()

import os
import stempy
from stempy.meme_parse import do_parse_and_extract
from optparse import OptionParser


def print_heading(out, heading, underline):
    print >>out, heading
    print >>out, underline * len(heading.strip())
    print >>out

usage = 'USAGE: %prog [options] <motifs filename>'
parser = OptionParser(usage=usage)
parser.add_option(
    "-o",
    "--output-dir",
    default='.',
    help="Output results to directory: DIR",
    metavar="DIR"
)
parser.add_option(
    "--eps",
    action="store_true",
    help="Create EPS logo as well as PNG",
)
options, args = parser.parse_args()
logo_dir = os.path.join(options.output_dir, 'logos')
stempy.ensure_dir_exists(logo_dir)


#
# Parse the motifs
#
motifs_filename = args[0]
extracted = do_parse_and_extract(open(motifs_filename).read())


#
# Write the reStructuredText
#
out_filename = os.path.join(options.output_dir, 'motifs.rst')
filenames = dict()
with open(out_filename, 'w') as out:
    logger.info('File: %s', motifs_filename)
    print_heading(out, 'Motifs', '-')
    print >>out, 'Filename = %s\n' % motifs_filename
    print >>out, 'Alphabet = %s\n' % extracted.alphabet
    print >>out, 'Background frequencies = %s\n' % extracted.back_freqs
    print >>out, 'Strands = %s\n' % ' '.join(extracted.strands)
    for i, motif in enumerate(extracted.motifs):
        logger.info('Motif %2d: %s', i, motif.name)
        print_heading(
            out,
            '%d: %s%s' % (
                i, motif.name,
                motif.altname and ' (%s)' % motif.altname or ''),
            '-')

        if motif.letter_probs:
            # print_heading(out, 'Letter probabilities', '~')
            print >>out, 'w = %d; ' % motif.letter_probs.w
            print >>out, '# sites = %d; ' % motif.letter_probs.nsites
            if None != motif.letter_probs.E:
                print >>out, 'E = %f; ' % motif.letter_probs.E
            logo_tag = 'motif-%s' % motif.name
            stempy.logo(
                motif.letter_probs.values,
                logo_tag,
                d=logo_dir,
                make_png=True,
                make_eps=options.eps,
                write_title=False,
                show_fineprint=False)
            filenames[motif.name] = os.path.join(logo_dir, 'logo-' + logo_tag + '.png')
            print >>out, '\n.. image:: %s' % filenames[motif.name]
            print >>out


#
# Make HTML page
#
try:
    from jinja2 import Environment, PackageLoader
    env = Environment(loader=PackageLoader('stempy', 'templates'))
    template = env.get_template('motifs.html')

    # Copy the static info
    static_dir = os.path.join(options.output_dir, 'static')
    stempy.html_copy_static(static_dir)

    # write the HTML
    filename = os.path.join(options.output_dir, 'motifs.html')
    logger.info('Writing motifs as HTML to %s', filename)
    with open(filename, 'w') as f:
        variables = {
            'motifs_filename': motifs_filename,
            'extracted': extracted,
            'filenames': filenames,
        }
        f.write(template.render(**variables))

except ImportError, e:
    logger.warning('Import failed, could not write HTML:\n%s', str(e))

