def write_random_records(fqa, fqb, outfa, outfb, N):

    records = sum(1 for _ in gzip.open(fqa)) / 4
    rand_records = sorted([random.randint(0, records - 1) for _ in xrange(N)])

    suba, subb = gzip.open(outfa, "w"), gzip.open(outfb, "w")
    fha, fhb = gzip.open(fqa),  gzip.open(fqb)
    rec_no = - 1
    
    for rr in rand_records:

        while rec_no < rr:
            rec_no += 1       
            for i in range(4): fha.readline()
            for i in range(4): fhb.readline()
        for i in range(4):
            suba.write(fha.readline())
            subb.write(fhb.readline())
        rec_no += 1 # (thanks @anderwo)

    print >>sys.stderr, "wrote to %s, %s" % (suba.name, subb.name)
get N random headers from a fastq file without reading the
dataset/ETHPy150Open CGATOxford/cgat/obsolete/subset_fastq.py/write_random_records