#!/usr/bin/env python

# Copyright (C) 2011-2012 CRS4.
#
# This file is part of Seal.
#
# Seal 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.
#
# Seal 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.
#
# You should have received a copy of the GNU General Public License along
# with Seal.  If not, see <http://www.gnu.org/licenses/>.

#########################################################
# You need a working Hadoop cluster to use this.
#########################################################

import os
import re
import site
import sys

IntegrationTestDir = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
site.addsitedir(IntegrationTestDir)

from seal_integration_test import SealIntegrationTest

class TestDropFailedFilter(SealIntegrationTest):
	def __init__(self):
		SealIntegrationTest.__init__(self, os.path.realpath(os.path.dirname(__file__)) )

	def run_program(self, hdfs_input, hdfs_output):
		cmd = ["%s/scripts/seal" % self.seal_dir, "prq",
		  "--traditional-ids",
		  "-D", "seal.prq.min-bases-per-read=0",
		  "-D", "seal.prq.drop-failed-filter=true",
		  "-D", "hbam.qseq-input.base-quality-encoding=sanger",
		  "--num-reducers", "1",
		  hdfs_input, hdfs_output]
		self.logger.debug("Command: %s", " ".join(cmd))

		stdout = self.run_cmd_and_output_if_failure(cmd)

		n_failed=int(re.search("FailedFilter=(\d+)$", stdout, re.MULTILINE).group(1))
		if n_failed != 4:
			self.logger.error("Unexpected counted reads without enough known bases (expected 4 but got %d)", n_failed)
			raise StandardError(self.test_name)

		n_dropped = int(re.search("Dropped=(\d+)$", stdout, re.MULTILINE).group(1))
		if n_dropped != 2:
			self.logger("Unexpected number of reads dropped (expected 2 but got %d)", n_dropped)
			raise StandardError(self.test_name)

if __name__ == '__main__':
	success = TestDropFailedFilter().test_method()
	sys.exit( 0 if success else 1 )
