##################################
#                                #
# Last modified 2017/06/22       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import os
import string
def run():

    if len(sys.argv) < 2:
        print 'usage: python %s file everyN' % sys.argv[0]
        print '\tNote: the script will read .gz and .bzip2 files as well as stdin (-)'
        print '\tNote: the script will print standard output'
        sys.exit(1)

    inputfilename = sys.argv[1]
    N = int(sys.argv[2])

    doStdIn = False
    if inputfilename != '-':
        if inputfilename.endswith('.bz2'):
            cmd = 'bzip2 -cd ' + inputfilename
        elif inputfilename.endswith('.gz'):
            cmd = 'zcat ' + inputfilename
        else:
            cmd = 'cat ' + inputfilename
        p = os.popen(cmd, "r")
    else:
        doStdIn = True

    line = 'line'
    i = 1
    k = 1 
    while line != '':
        if doStdIn:
            line = sys.stdin.readline()
        else:
            line = p.readline()
        if line == '':
            break
        if i == 1:
            if line.startswith('@'):
                pass
            else:
                print 'fastq file broken, exiting'
            if k % N == 0:
                print line.strip()
            i = 2
            continue
        if i == 2:
            if k % N == 0:
                print line.strip()
            i = 3
            continue
        if i == 3:
            if line.startswith('+'):
                pass
            else:
                print 'fastq file broken, exiting'
            if k % N == 0:
                print line.strip()
            i = 4
            continue
        if i == 4:
            if k % N == 0:
                print line.strip()
            i = 1
            k += 1
            continue

run()
