#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2018, A. Murat Eren
#
# 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 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.


# when a list of ids and a fastq file provided, this script removes reads from fastq file
# that have a matching id in the given ids list.
#
# if something seems to be wrong with it, please send an e-mail to meren@mbl.edu


import os
import sys

import IlluminaUtils.lib.fastqlib as u

def main(input_file_path, output_r1_path, output_r2_path):
    input_fastq = u.FastQSource(input_file_path)

    output_r1 = u.FastQOutput(output_r1_path)
    output_r2 = u.FastQOutput(output_r2_path)

    num_processed_all = 0
    num_processed_r1 = 0
    num_processed_r2 = 0

    while 1:
        if input_fastq.next(raw=True):
            output_r1.store_entry(input_fastq.entry)
            num_processed_all += 1
            num_processed_r1 += 1
        else:
            break

        if input_fastq.next(raw=True):
            output_r2.store_entry(input_fastq.entry)
            num_processed_all += 1
            num_processed_r2 += 1
        else:
            break

        if num_processed_all % 5000 == 0:
            sys.stderr.write('\r[fastqlib:de-interleaving] Processed: %s'  % (u.big_number_pretty_print(input_fastq.pos)))
            sys.stderr.flush()

    sys.stderr.write('\n\n')

    print('Total number of reads processed: %s' % u.big_number_pretty_print(input_fastq.pos))
    print('De-interleaved FASTQ for R1    : %s (%d entries)' % (output_r1_path, num_processed_r1))
    print('De-interleaved FASTQ for R2    : %s (%d entries)' % (output_r2_path, num_processed_r2))
    print()

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description='De-interleave an interleaved FASTQ file')
    parser.add_argument('input', metavar = 'INPUT',
                        help = 'FASTQ file to be de-interleaved')
    parser.add_argument('-1', '--output-r1', required=True, metavar = 'R1 FASTQ',
                        help = 'Read 1s')
    parser.add_argument('-2', '--output-r2', required=True, metavar = 'R2 FASTQ',
                        help = 'Read 2s')

    args = parser.parse_args()

    sys.exit(main(args.input, args.output_r1, args.output_r2))
