Print header of bam file using htsjdk library
2
1
Entering edit mode
7.4 years ago

Hi, I am using htsjdk library and want to print the header of bam file. I tried but the following code just prints the first header line again and again. I want all header lines. Any suggestions?? Thanks for your time and consideration.

File bamFile =new File("SampleFile.bam");

SamReader sr = 
SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT).open(bamFile);     

SAMRecordIterator r = sr.iterator();

while(r.hasNext()) {

    SAMRecord rec=r.next();    

    System.out.println(rec.getHeader());

}

r.close();

sr.close();
NGS samformat • 1.6k views
ADD COMMENT
1
Entering edit mode
7.4 years ago

I think you should replace rec.getHeader() with:

rec.getHeader().getTextHeader()
ADD COMMENT
0
Entering edit mode

Thanks... it worked

ADD REPLY
1
Entering edit mode
7.4 years ago

https://github.com/samtools/htsjdk/blob/master/src/main/java/htsjdk/samtools/SAMTextHeaderCodec.java

 StringWriter str = new StringWriter();
 new SAMTextHeaderCodec().encode(str, sr.getFileHeader());
System.out.println(str);
ADD COMMENT

Login before adding your answer.

Traffic: 2274 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6