def METHOD_NAME(options):
    """
    use pysam to iterate over sam/bam input
    """
    in_file = pysam.AlignmentFile(options.input, 'r' if options.type == 'sam' else 'rb')
    out_file = pysam.AlignmentFile('-', 'w' if options.type == 'sam' else 'wb', template=in_file)
    prev_name = None
    first_record = None
    num_written = 0
    num_read = 0
    for record in in_file:
        num_read += 1
        if record.is_secondary:
            continue
        if record.is_read1:
            first_record = record
        elif record.is_read2 and first_record and record.query_name == first_record.query_name:
            out_file.write(first_record)
            out_file.write(record)
            num_written += 2
        # samtools sorts names differently then python 
        #if prev_name and record.query_name < prev_name:
        #    raise RuntimeError('Records not sorted by name: {} {}'.format(prev_name, record.query_name))
        prev_name = record.query_name
    in_file.close()
    out_file.close()
    sys.stderr.write('Filtered {} / {} lines\n'.format(num_read - num_written, num_read))