Question: How To Edit A Bam File With Bio::Db::Bam
gravatar for Owen S.
6.4 years ago by
Owen S.350
Oakland CA
Owen S.350 wrote:

I have just begun exploring Lincoln Stein's excellent Bio::DB::Bam perl module and have an elementary question. Is it possible to edit values of an Bio::DB::Bam::Alignment alignment object?

Here is a dead simple script that just copies a bam file.

my $bam = Bio::DB::Bam->open("in.bam") ;
my $fix = Bio::DB::Bam->open("out.bam", "w" ) ;

my $header = $bam->header();

while (my $align = $bam->read1 ) {

What I want to do is edit some of the fields before writing them. For example, for each alignment, I can access the CIGAR field with $align->cigar_str or $align->cigar_array and I can get the sequence of the read with $align->qseq -- but is there a way I can change these values before writing them to the new file?

perl bioperl samtools • 2.4k views
ADD COMMENTlink modified 6.3 years ago by marcosmorgan90 • written 6.4 years ago by Owen S.350

I read through the CPAN documentation. At a first glance it does not look like there are setter methods? Have you tried giving an argument to the method call. $align->cigar_str(your string)?

ADD REPLYlink modified 6.4 years ago • written 6.4 years ago by Zev.Kronenberg11k

Thank you, your hunch was quite right, no setter methods for most of the objects.

ADD REPLYlink written 6.3 years ago by Owen S.350
gravatar for marcosmorgan
6.3 years ago by
MRC centre for regenerative medicine, University of Edinburgh
marcosmorgan90 wrote:

I went through the Bio::DB::Bam::Alignment documentation and found this under “Low-level Bio::DB::Bam::Alignment methods”:

These methods are available to objects of type Bio::DB::Bam::Alignment as well as Bio::DB::Bam::AlignWrapper and closely mirror the native C API.


$tid = $align->tid( [$new_tid] ) Return the target ID of the alignment. Optionally you may change the tid by providing it as an argument (currently this is the only field that you can change; the functionality was implemented as a proof of principle).

Therefore, the only value that you can edit is the target ID.

ADD COMMENTlink modified 6.3 years ago • written 6.3 years ago by marcosmorgan90

Hopefully there is continued development on Bio::DB::BAM~!

ADD REPLYlink written 6.3 years ago by Zev.Kronenberg11k
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: 1259 users visited in the last hour