How To Read Bam File Line By Line Using Ruby
1
1
Entering edit mode
12.2 years ago
Gangcai ▴ 230

Dear all,

I am trying to read bam file line by line by using ruby scripts. Although there is ruby gem bio-samtools(http://rubygems.org/gems/bio-samtools), I don't find a way to read the whole bam file line by line(can get the alignments for given genomic regions). Any suggestion will be appreciated.

BTW, I have used IO.open("samtools view sample1.bam").each do {|x| ...}, however it will put the whole bam file into the memory first.

bam ruby • 3.0k views
ADD COMMENT
1
Entering edit mode

Really? At least in C, the whole idea of popen() is NOT to read the whole file into memory.

ADD REPLY
0
Entering edit mode
2.3 years ago
kojix2 ▴ 250

I know it's a 9 year old question, but I think you could make Ruby bindings for htslib or noodles. No one would do such a thing? I'm trying to make ruby-htslib. It's still buggy, though.

require 'htslib'

bam = HTS::Bam.open("foo.bam")

bam.each do |r|
  pp name: r.qname,
     flag: r.flag,
     chrm: r.chrom,
     strt: r.pos + 1,
     mapq: r.mapq,
     cigr: r.cigar.to_s,
     mchr: r.mate_chrom,
     mpos: r.mpos + 1,
     isiz: r.isize,
     seqs: r.seq,
     qual: r.qual.map { |i| (i + 33).chr }.join,
     MC:   r.aux("MC")
end

bam.close
ADD COMMENT

Login before adding your answer.

Traffic: 1945 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