import sys
import random
from optparse import OptionParser, OptionGroup
import collections
import pysam
 
 
parser = OptionParser()
parser.add_option("--input",dest="input",help="A bam file as input")
parser.add_option("--output",dest="output",help="A bam file as output")
(options, args) = parser.parse_args()
 
#file="/Volumes/Volume_4/analysis/pcr-non-pcr/bams/Dsim-PCRfree-rep1.filter.sort.bam"
 
samfile = pysam.Samfile(options.input, "rb" )
ofs=pysam.Samfile(options.output,"wb",template=samfile)
for ar in samfile:
    if ar.tid == ar.rnext:
        continue # discard those with equal id
    ofs.write(ar)
ofs.close()