#!/usr/bin/env python 

################################################################################
### COPYRIGHT ##################################################################

# New York Genome Center

# SOFTWARE COPYRIGHT NOTICE AGREEMENT
# This software and its documentation are copyright (2014) by the New York
# Genome Center. All rights are reserved. This software is supplied without
# any warranty or guaranteed support whatsoever. The New York Genome Center
# cannot be responsible for its use, misuse, or functionality.

# Version: 0.2
# Author: Andre Corvelo

################################################################# /COPYRIGHT ###
################################################################################



################################################################################
### MODULES ####################################################################

from optparse import OptionParser
import sys
from random import uniform
from itertools import islice


################################################################### /MODULES ###
################################################################################



################################################################################
### FUNCTIONS ##################################################################

################################################################# /FUNCTIONS ###
################################################################################



################################################################################
### ARGUMENTS,OPTIONS ##########################################################

parser = OptionParser(usage="\n%prog [options]", version="%prog v0.2")

parser.add_option(
	"-i",
	metavar = "FILE",
	type = "string",
	dest = "input_file",
	default = 'stdin',
	help = "Input FASTQ file (default = 'stdin')"
	)

parser.add_option(
	"-s",
	metavar = "FLOAT",
	type = "float",
	dest = "prob",
	default = 1,
	help = "Sampling probability (default = 1)"
	)

parser.add_option(
	"-p",
	metavar = "FLAG",
	dest = "paired",
	action = 'store_true',
	default = False,
	help = "Paired sampling (only for interleaved input; default = False)"
	)

(opt, args) = parser.parse_args()

######################################################### /ARGUMENTS,OPTIONS ###
################################################################################



################################################################################
### CONSTANTS ##################################################################

################################################################# /CONSTANTS ###
################################################################################



################################################################################
### MAIN #######################################################################

if __name__ == "__main__":
	nl = 4
	if opt.paired:
		nl = 8

	if opt.input_file != 'stdin':
		fq = open(opt.input_file, 'r')
	else:
		fq = sys.stdin
	while True:
		u = [line for line in islice(fq, nl)]
		if not u:
			break
		if uniform(0,1) < opt.prob:
			sys.stdout.write(''.join(u))

	if opt.input_file != 'stdin':
		fq.close()
	
###################################################################### /MAIN ###
################################################################################
