#!/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.


import os
import sys
import pickle

import IlluminaUtils.lib.fastqlib as u
from IlluminaUtils.utils.helperfunctions import populate_tiles_qual_dict_from_input
from IlluminaUtils.utils.helperfunctions import compute_plot_dict_from_tiles_dict 


def main(input_1_path, input_2_path, output_prefix):
   
    ########################################################################################################################
    # qual dicts
    ########################################################################################################################
    
    tiles_dict  = {'1': {}, '2': {}}

    input_1 = u.FastQSource(input_1_path, compressed = input_1_path.endswith('.gz'))
    input_2 = u.FastQSource(input_2_path, compressed = input_2_path.endswith('.gz'))
    
    tiles_dict = populate_tiles_qual_dict_from_input(input_1, input_2, tiles_dict)

    sys.stderr.write('[qual dicts] computing plot dict ... ')
    plot_dict = compute_plot_dict_from_tiles_dict(tiles_dict)
    sys.stderr.write('done.\n')

    sys.stderr.write('[qual dicts] serializing plot dicts ... ')
    pickle.dump(plot_dict, open(os.path.join(output_prefix), 'w'))
    sys.stderr.write('done.\n')
 
    input_1.close()
    input_2.close()

if __name__ == '__main__':
    sys.exit(main(sys.argv[1], sys.argv[2], sys.argv[3]))
