#######################################################################
# Imports
#######################################################################

import optparse
from auto_process_ngs import stats

#######################################################################
# Main script
#######################################################################

if __name__ == '__main__':

    # Process command line
    p = optparse.OptionParser(
        usage="%prog [OPTIONS] STATS_INFO_FILE",
        version="%prog " + __version__,
        description="Summarise the per sample stats for each lane "
        "for an Illumina sequencing run, using the data from "
        "STATS_INFO_FILE (typically called 'statistics.info' and "
        "found in the top-level directory of sequencing runs "
        "processed using auto_process.py.")
    options, args = p.parse_args()
    if len(args) == 0:
        stats_file = "statistics.info"
    elif len(args) > 1:
        p.error("expects a single argument (statistics file)")
    else:
        stats_file = args[0]
    stats.report_per_lane_stats(stats_file)
#######################################################################
# Imports
#######################################################################

import optparse
from auto_process_ngs import stats

#######################################################################
# Main script
#######################################################################

if __name__ == '__main__':

    # Process command line
    p = optparse.OptionParser(usage="%prog [OPTIONS] STATS_INFO_FILE",
                              version="%prog "+__version__,
                              description="Summarise the per sample stats for each lane "
                              "for an Illumina sequencing run, using the data from "
                              "STATS_INFO_FILE (typically called 'statistics.info' and "
                              "found in the top-level directory of sequencing runs "
                              "processed using auto_process.py.")
    options,args = p.parse_args()
    if len(args) == 0:
        stats_file = "statistics.info"
    elif len(args) > 1:
        p.error("expects a single argument (statistics file)")
    else:
        stats_file = args[0]
    stats.report_per_lane_stats(stats_file)
        if not options.force:
            stats = TabFile.TabFile(filen=filein,first_line_is_header=True)
        else:
            logging.warning("--force specified, statistics will be regenerated")
    if stats is None:
        # (Re)create statistics from raw files
        try:
            illumina_data = IlluminaData.IlluminaData(args[0],
                                                      unaligned_dir=options.unaligned_dir)
        except IlluminaData.IlluminaDataError,ex:
            logging.error("Failed to get data from %s: %s" % (args[0],ex))
            sys.exit(1)
        # Generate statistics for fastq files
        stats = fastq_statistics(illumina_data,n_processors=options.n)
        stats.write(options.stats_file,include_header=True)
    print "Statistics written to %s" % options.stats_file
    # Per-lane sequencer statistics
    if illumina_data.lanes == [None]:
        print "No lane information: per-lane statistics not generated"
    else:
        auto_process_stats.report_per_lane_stats(options.stats_file,
                                                 options.per_lane_stats_file)
        print "Per-lane sequencer stats written to %s" % \
            options.per_lane_stats_file
        # Summary of per-lane sequencer stats
        per_lane_stats_summary
        seqstats = sequencer_stats(stats)
        seqstats.write(seq_stats_file,include_header=True)
        print "Summary of per-lane sequencer stats written to %s" % \
            seq_stats_file