def find_fastq_pairs(fq1, fq2, out1, out2, tmpdir=None, quiet=False):
    tmp1 = tempfile.NamedTemporaryFile(delete=False, prefix='.tmp', suffix='.gz', dir=tmpdir if tmpdir else os.path.dirname(fq1.fname))
    tmp1_fname = tmp1.name
    tmp1_out = gzip.GzipFile(fileobj=tmp1)

    ngsutils.fastq.sort.fastq_sort(fq1, out=tmp1_out, tmpdir=tmpdir if tmpdir else os.path.dirname(fq1.fname))
    tmp1_out.close()
    tmp1.close()

    tmp2 = tempfile.NamedTemporaryFile(delete=False, prefix='.tmp', suffix='.gz', dir=tmpdir if tmpdir else os.path.dirname(fq2.fname))
    tmp2_fname = tmp2.name
    tmp2_out = gzip.GzipFile(fileobj=tmp2)

    ngsutils.fastq.sort.fastq_sort(fq2, out=tmp2_out, tmpdir=tmpdir if tmpdir else os.path.dirname(fq2.fname))
    tmp2_out.close()
    tmp2.close()

    sys.stderr.write('Finding properly paired FASTQ reads...\n')

    fq_tmp1 = FASTQ(tmp1_fname)
    fq_tmp2 = FASTQ(tmp2_fname)

    reader1 = fq_tmp1.fetch(quiet=quiet)
    reader2 = fq_tmp2.fetch(quiet=True)

    read1 = reader1.next()
    read2 = reader2.next()

    pairs = 0
    discarded_1 = 0
    discarded_2 = 0

    while read1 and read2:
        if read1.name == read2.name:
            read1.write(out1)
            read2.write(out2)

            try:
                read1 = reader1.next()
                read2 = reader2.next()
            except __HOLE__:
                break

            pairs += 1
        elif read1.name < read2.name:
            discarded_1 += 1
            try:
                read1 = reader1.next()
            except StopIteration:
                break
        else:
            discarded_2 += 1
            try:
                read2 = reader2.next()
            except StopIteration:
                break

    fq_tmp1.close()
    fq_tmp2.close()

    os.unlink(tmp1_fname)
    os.unlink(tmp2_fname)

    return pairs, discarded_1, discarded_2
dataset/ETHPy150Open ngsutils/ngsutils/ngsutils/fastq/properpairs.py/find_fastq_pairs