#!/usr/bin/env python

"""
Call peaks that are both strong and consistent across multiple bedGraph files
in a way that works with very sharp peaks such as those from Term-seq.

See https://github.com/nichd-bspc/termseq-peaks for more details.
"""


from peaklib.peaklib import idr_peak_calls

if __name__ == "__main__":

    usage = __doc__

    import argparse

    ap = argparse.ArgumentParser(usage=usage)
    ap.add_argument(
        "bedgraphs",
        nargs="+",
        help="""bedGraphs to call peaks on. Multiple
        (space-separated) files can be specified. This algorithm works
        independently of normalization, so raw or normalized files are
        fine. See https://github.com/nichd-bscp/termseq-peaks for details on
        creating this bedGraph file.""",
    )

    ap.add_argument(
        "--peaks",
        required=True,
        help="""Filename for narrowPeak file containing final peaks.""",
    )

    ap.add_argument(
        "--idr-threshold",
        "-t",
        type=float,
        default=0.05,
        help="""IDR threshold to use for final peaks. Default is
        %(default)s.""",
    )
    ap.add_argument(
        "--strand",
        "-s",
        default=".",
        help="""It is assumed all provided bedGraphs are on the same strand;
        use this to specify which strand that is. This strand will be put in
        the output narrowPeak file's "strand" field.  %(default)s.""",
    )
    ap.add_argument(
        "--oracle-output",
        help="""Filename for narrowPeak file of oracle peaks (as called from
        merged bedGraphs). Default is to append the suffix ".oracle.narrowPeak"
        to the peaks output.""",
    )
    args = ap.parse_args()

    if args.oracle_output is None:
        args.oracle_output = args.peaks + ".oracle.narrowPeak"

    idr_peak_calls(
        bedgraphs=args.bedgraphs,
        thresh=args.idr_threshold,
        strand=args.strand,
        oracle_fn=args.oracle_output,
        final_fn=args.peaks,
    )
