#!python
# coding=utf-8

#  PINTS: Peak Identifier for Nascent Transcript Starts
#  Copyright (c) 2019-2025 Yu Lab.
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
import argparse
import logging
from pints.qc_engine import calculate_gbody_tss_ratio

logging.basicConfig(format="%(name)s - %(asctime)s - %(levelname)s: %(message)s",
                    datefmt="%d-%b-%y %H:%M:%S",
                    level=logging.INFO,
                    handlers=[
                        logging.StreamHandler()
                    ])
logger = logging.getLogger("PINTS - Sample QC")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument(
        "--bw-pl", action="store", dest="bw_pl",
        type=str, required=True,
        help="Bigwig for the plus strand.")
    parser.add_argument(
        "--bw-mn", action="store", dest="bw_mn",
        type=str, required=True,
        help="Bigwig for the minus strand.")
    parser.add_argument(
        "--annotation-gtf", action="store", dest="annotation_gtf", type=str, required=True,
        help="Gene annotation file (gtf) format for evaluating TSS enrichment.")

    args = parser.parse_args()

    logger.info("Evaluating the effect of cap selection/TSS enrichment...")
    ratio = calculate_gbody_tss_ratio(args.bw_pl, args.bw_mn, args.annotation_gtf)
    if ratio > 0.15:
        logger.critical(f"- PINTS detected high proportion of reads from gene body regions. ({ratio:.2%})")
        logger.critical("- This usually indicates the cap selection is not working as expected.")
    elif ratio > 0.1:
        logger.warning(f"- PINTS observed higher than expected proportion of reads in gene body regions. ({ratio:.2%})")
        logger.warning("- Please proceed with caution.")
    else:
        logger.info("- PINTS doesn't find any significant deviation...")
