Question: converting SAMRecord to JSON or String format
gravatar for dsvispute91
2.6 years ago by
dsvispute910 wrote:

I want to develop RESTful web service which will provide bam reads in JSON format. I used SamFileReader (htsjdk.samtools.SAMFileReader) for reading bam file and then using SAMRecordIterator to get SAMRecord. But when I try to convert it into String/ JSON it throws following error :

Method not implemented for: class htsjdk.samtools.SAMSequenceRecord (through reference chain: htsjdk.samtools.BAMRecord["header"]->htsjdk.samtools.SAMFileHeader["sequenceDictionary"]->htsjdk.samtools.SAMSequenceDictionary["sequences"]->java.util.UnmodifiableRandomAccessList[0]->htsjdk.samtools.SAMSequenceRecord["id"])

I tried following ways to create JSON for SamRecord :

  1. Used com.fasterxml.jackson.databind.ObjectMapper and ObjectWriter with "writeValueAsString()" API.
  2. Used directly Response Body annotation for request URL method.
  3. Tried to use JSONSerializer from "net.sf.json" and same error occured.

Is there any other way to convert SamRecord to JSON/String ?

samrecord sam json htsjdk • 958 views
ADD COMMENTlink modified 12 months ago by Biostar ♦♦ 20 • written 2.6 years ago by dsvispute910
gravatar for Pierre Lindenbaum
2.6 years ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum117k wrote:

SAMRecord is not treated as a POJO: when jackson tries to serialize a samrecord, it finds that a samrecord is linked to a header. and jacksons tries to serialize everything

which contains a whole sequence dictionary

which contains a list of

which contains a getter "getId" with the following implementation:

  public String getId() {
        throw new UnsupportedOperationException("Method not implemented for: " + this.getClass());

the best way is to create your very own serializer.

FYI, I wrote a SAMTojson :


ADD COMMENTlink written 2.6 years ago by Pierre Lindenbaum117k

Thank you Pierre Lindenbaum :)

ADD REPLYlink written 2.6 years ago by dsvispute910
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1225 users visited in the last hour