#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2011, 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_r1, input_r2, output_file_path):
    input_r1 = u.FastQSource(input_r1)
    input_r2 = u.FastQSource(input_r2)

    interleaved_output = u.FastQOutput(output_file_path)

    while input_r1.next(raw=True) and input_r2.next(raw=True):
        if input_r1.pos % 5000 == 0:
            sys.stderr.write('\r[fastqlib:interleaving] Processed: %s'  % (u.big_number_pretty_print(input_r1.pos)))
            sys.stderr.flush()

        interleaved_output.store_entry(input_r1.entry)
        interleaved_output.store_entry(input_r2.entry)

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

    print('Total number of reads processed: %s' % u.big_number_pretty_print(input_r1.pos))
    print('Interleaved FASTQ file output  : %s' % output_file_path)
    print()

if __name__ == '__main__':
    import argparse
    
    parser = argparse.ArgumentParser(description='Remove reads from FASTQ File')
    parser.add_argument('-1', '--input-r1', required=True, metavar = 'R1 FASTQ',
                        help = 'Read 1')
    parser.add_argument('-2', '--input-r2', required=True, metavar = 'R2 FASTQ',
                        help = 'Read 1')
    parser.add_argument('-o', '--output-file-path', required=True, metavar = 'OUTPUT_FILE_PATH',
                        help = 'Interleaved FASTQ file path (give it a good name).')

    args = parser.parse_args()

    sys.exit(main(args.input_r1, args.input_r2, args.output_file_path))
