This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How To Edit A Bam File With Bio::Db::Bam

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();
$fix->header_write($header);

while (my $align = $bam->read1 ) {
  # EDIT BAM FIELDS HERE
  $fix->write1($align);
}

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?

bioperl samtools perl

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)?

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

1 answer

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.

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

Log in to answer this question.