#!/usr/bin/env python

import argparse
import pysam
    
parser = argparse.ArgumentParser(description="Extract all the reads from a bam file that do not have CB or GE tags")
parser.add_argument('--in-bam-file', dest='inbam', help='input bam file')
parser.add_argument('--out-bam-file', dest='outbam', help='output bam file')

args = parser.parse_args()


def getUntaggedReads(in_bam_file,out_bam_file,verbose=True):
    inbam=pysam.AlignmentFile(in_bam_file,'rb')
    outbam=pysam.AlignmentFile(out_bam_file,'wb',template=inbam)
    counter=0
    for read in inbam:
        counter+=1
        try:
            GE_tag=read.get_tag('GE');
            CB_tag=read.get_tag('CB');
        except KeyError:
            outbam.write(read)
            pass
        if(counter %  1e5 == 0):
             print('Processed {} reads'.format(counter));
    outbam.close()
    inbam.close()
    
getUntaggedReads(args.inbam,args.outbam)
