This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Analogy of exifread library in pysam for bam and vcf header files

Hi Biostars community,

Is there an analogous way in Pysam to the code below to extract the header information in .bam and .vcf (or .vcf.gz) files? Help with a code snippet will great. The code snippet below uses exifread library to get the meta-info (in key-value pair) in jpeg and tiff files.

import exifread

def acPostProcForPut(rule_args, callback, rei): sv = session_vars.get_map(rei) phypath = sv['data_object']['file_path'] objpath = sv['data_object']['object_path'] exiflist = [] with open(phypath, 'rb') as f: tags = exifread.process_file(f, details=False) for (k, v) in tags.iteritems(): if k not in ('JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'EXIF MakerNote'): exifpair = '{0}={1}'.format(k, v) exiflist.append(exifpair) exifstring = '%'.join(exiflist) <post>

bam vcf pysam bcftools htslib

Thanks for posting the link. I read it, but I didn't get how to remove the "@" in .bam and "##" in vcf file. I was hoping that someone who knows better can help. Here is my approach:

import pysam

pysamlist = []

with pysam.AlignmentFile("ex1.bam", "rb") as f:
    tags = pysam.view("-h", f)
    for (k, v) in tags.iteritems():
        pysampair = '{0}={1}'.format(k, v)
        pysamlist.append(pysampair)
    pysamstring = '%'.join(pysamlist)

Could you please give an example how your output should look like?

0 answers

No answers yet.

Log in to answer this question.