Processes SAM file to get the genomic coordinates in BED format
    step#2

    :param cfg: configuration dict

def queriessam2dalignbed(cfg):
    """
    Processes SAM file to get the genomic coordinates in BED format
    step#2

    :param cfg: configuration dict
    """
    datatmpd=cfg['datatmpd']
    alignmentbedp=cfg['alignmentbedp']
    dalignbedp=cfg['dalignbedp']
    logging.info(basename(dalignbedp))
    if not exists(alignmentbedp) or cfg['force']:
        #input/s
        queriessamps=glob(f'{datatmpd}/01_queries_queryl*.sam')
        for queriessamp in queriessamps:
            if stat(queriessamp).st_size != 0:
                samfile=pysam.AlignmentFile(queriessamp, "rb")
                dalignbed=pd.DataFrame(columns=bed_colns)
                for read in samfile.fetch():
                    algnids=[]
                    
#                     read_position=read.positions[0]
#                     tag_NM=read.get_tag('NM')                    
                    if len(read.positions)!=0:
                        read_position=read.positions[0]
                    else:
                        logging.error('no alignments found')
                        print(read)
                        continue
#                         read_position=[None]
                    try: 
                        tag_NM=read.get_tag('NM')
                    except: 
                        logging.error('no NM tag found')
                        print(read)
                        continue
#                         tag_NM=None
                    algnids.append(f"{read.reference_name}|{'-' if read.is_reverse else '+'}{read_position}|{read.cigarstring}|{tag_NM}")
                    if read.has_tag('XA'):
                        algnids+=['|'.join(s.split(',')) for s in read.get_tag('XA').split(';') if len(s.split(','))>1]
#                     print(len(algnids))
                    chroms=[]
                    starts=[]
                    ends=[]
                    algnids=algnids[:20]
                    NMs=[]
                    strands=[]
                    for a in algnids:
                        strand=a.split('|')[1][0]
                        chroms.append(a.split('|')[0])
                        if strand=='+':
                            offset=0
                        elif strand=='-':
                            offset=0                    
                        starts.append(int(a.split('|')[1][1:])+offset)
                        ends.append(int(a.split('|')[1][1:])+str2num(a.split('|')[2])+offset)
                        NMs.append(a.split('|')[3])
                        strands.append(strand)
                        del strand,offset
                    col2dalignbed={'chromosome':chroms,
                                   'start':starts,
                                   'end':ends,
                                   'id':algnids,
                                   'NM':NMs,
                                   'strand':strands}
                #     col2dalignbed=dict(zip(cols,[a.split('|')[0],a.split('|')[1],a.split('|')[2],a,a.split('|')[3],a.split('|')[4] for a in algnids]))
                    dalignbed_=pd.DataFrame(col2dalignbed)
                    dalignbed_['query id']=read.qname.replace('_',' ')
                    dalignbed = dalignbed.append(dalignbed_,ignore_index=True,sort=True)
                #     break
                samfile.close()
            else:
                logging.warning(f"file is empty: {queriessamp}")
        dalignbed.to_csv(dalignbedp,sep='\t')
        from rohan.dandage.io_nums import str2numorstr
        dalignbed['chromosome']=dalignbed.apply(lambda x : str2numorstr(x['chromosome']),axis=1)
        dalignbed=dalignbed.sort_values(['chromosome','start','end'], ascending=[True, True, True])
        dalignbed.loc[:,bed_colns].to_csv(alignmentbedp,sep='\t',
                        header=False,index=False,
                        chunksize=5000)
    return cfg