Bio::D...ignment(3pm)
π Bio::D...ignment(3pm) π
User Contributed Perl Documentation β aka the manual page for Bio::DB::Bam::Alignment, bestie. Yes the man name got cropped, we don't talk about it, the module is the whole point. π
β¨ NAME β¨
Bio::DB::Bam::Alignment β it's the SAM/BAM alignment object, and it's lowkey a whole vibe π
π¦ SYNOPSIS π¦
use Bio::DB::Sam;
my $sam = Bio::DB::Sam->new(-fasta=>"data/ex1.fa",
-bam =>"data/ex1.bam");
my @alignments = $sam->get_features_by_location(-seq_id => 'seq2',
-start => 500,
-end => 800);
for my $a (@alignments) {
my $seqid = $a->seq_id;
my $start = $a->start;
my $end = $a->end;
my $strand = $a->strand;
my $ref_dna= $a->dna;
my $query_start = $a->query->start;
my $query_end = $a->query->end;
my $query_strand = $a->query->strand;
my $query_dna = $a->query->dna;
my $cigar = $a->cigar_str;
my @scores = $a->qscore; # per-base quality scores
my $score = $a->qstring; # TAM-style quality string
my $match_qual= $a->qual; # quality of the match
my $paired = $a->get_tag_values('PAIRED');
}
π DESCRIPTION π
okay so listen. Bio::DB::Bam::Alignment and Bio::DB::Bam::AlignWrapper are a duo β together they represent an alignment between a sequence read (the "query") and a reference sequence (the "target"). It's giving collaborative, it's giving two halves of one brain cell but in a good way. β¨
Here's the tea on who does what:
Bio::DB::Bam::Alignmentadheres strictly to the C-level BAM library's definition of abam1_t*, and it's the one used in theBio::DB::Samlow-level API.- The latter (that's
AlignWrapper, bestie, keep up π ) adds convenience methods that make it similar to a BioPerlBio::SeqFeatureIobject.
This manual page describes both. Two icons, one manpage. Periodt. π
π High-level Bio::DB::Bam::Alignment methods π
These methods are provided by Bio::DB::Bam::Alignment, and they're designed to be compatible with the Bio::SeqFeatureI interfaces. π«
β οΈ Lowkey important: these objects are not compatible with Bio::Align::AlignI. Why? Because the BAM API is fundamentally incompatible with the BioPerl API for alignments. The first one deals with the alignment of a single read against the reference sequence, while the second deals with a multiple alignment. Different species of beast entirely, fr fr. π§¬
Also bestie, heads up: the high-level API returns Bio::DB::Bam::AlignWrapper objects... except in the case of the callback to the fast_pileup() method. In that one specific case, the object you get back from calling $pileup->b() is a Bio::DB::Bam::Alignment object, and it's that way for performance reasons. No cap. ποΈπ¨
$seq_id = $align->seq_idβ gives you theseq_idof the reference (target) sequence. π― This one is only available in theBio::DB::Bam::AlignWrapperextension.$start = $align->startβ the start of the alignment, in 1-based reference sequence coordinates.$end = $align->endβ the end of the alignment, in 1-based reference sequence coordinates.$len = $align->lengthβ the length of the alignment on the reference sequence.$mseqid = $align->mate_seq_idβ theseq_idof the mate's reference (target) sequence. π This one is only available in theBio::DB::AlignWrapperextension.$mstart = $align->mate_startβ for paired reads, this gives you the start of the mate's alignment in 1-based reference sequence coordinates.$mend = $align->mate_endβ for paired reads, this gives you the end position of the mate's alignment in 1-based reference sequence coordinates.$mlen = $align->mate_lenβ for mate-pairs, grab the length of the mate's alignment on the reference sequence. π$strand = $align->strandβ the strand of the alignment:-1for reversed,+1for forward. π§
π NOTE: In versions1.00-1.06, this method always returned+1. As of version1.07, this behavior is fixed. So if you're on an old version, you were being lied to, bestie, and I'm so sorry.
$mstrand = $align->mstrandβ if the read has a mate pair, this returns the strand of the mate, in the format-1or+1.$ref_dna = $align->dnaβ returns the reference sequence's DNA across the aligned region. 𧬠Here's the vibe check: if anMDtag is present in the alignment, it will be used preferentially to reconstruct the reference sequence. Otherwise, the reference DNA access object that you passed toBio::DB::Sam->new()gets used instead.$ref_dna = $align->seqβ the reference sequence's DNA, but as aBio::PrimarySeqIobject. β¨ Super useful for passing to BioPerl functions, and for calculating subsequences and reverse complements. It's giving versatile.$query = $align->queryβ this returns aBio::DB::Alignment::Queryobject that you can use to pull info about the query sequence. The next few entries show you exactly how to use this object, so keep scrolling bestie. π$read_name = $align->query->nameβ the name of the read. π·οΈ$q_start = $align->query->startβ the start position of the query (read) sequence, in 1-based coordinates. It works via a transientBio::DB::Bam::Queryobject that exists forBio::Graphicscompatibility (seeBio::Graphics). It's giving temporary but iconic. π¦$q_end = $align->query->endβ the end position of the query sequence, in 1-based coordinates.$q_len = $align->query->lengthβ the length of the alignment on the read.$scores = $align->query->scoreβ returns an array reference holding the unpacked quality scores for each base of the query sequence. π The length of that array reference will be equal to the length of the read. Math is mathing, periodt.$read_dna = $align->query->dnaβ the read's DNA string. π§¬$read_seq = $align->query->seqβ the read's DNA, as aBio::PrimarySeqIobject.$target = $align->target;β thetarget()method is similar toquery(), except that it followsBio::AlignIOconventions for how to represent minus strand alignments. π The object returned hasstart(),end(),qscore(),dna()andseq()methods, but for minus strand alignments the sequence will be represented as it appears on the reverse strand, rather than on the forward strand. Why is that a slay? Because it gives you the read as it came off the machine, before being reverse complemented for use in the SAM file. History preserved, we love to see it. π$query = $align->hitβ thehit()method is identical totarget(), and it returns information about the read. It exists for compatibility with some of theBio::Graphicsglyphs, which usehit()to represent the non-reference sequence in aligned sequences. Same energy, different name. π«Ά$primary_id = $align->primary_idβ this method synthesizes a unique ID for the alignment, which you can pass to$sam->get_feature_by_id()to retrieve the alignment at a later date. Save it for later, bestie, it's the alignment's phone number. π±@tags = $align->get_all_tagsβ returns all the tag names known to this alignment. This includes SAM flags likeM_UNMAPPED, plus auxiliary flags likeH0. π The behavior here depends on the value of-expand_flagswhen the SAM object was created:- If it's false (which is the default), the standard SAM flags get concatenated together into a single string and stored in a tag named
'FLAGS'. The format of that tag value is the list of one or more flag constants separated by the"|"character, like so:"PAIRED|MAP_PAIR|REVERSED|SECOND_MATE". - If
-expand_flagswas true, then each flag becomes its own named tag, such as"MAP_PAIR".
- If it's false (which is the default), the standard SAM flags get concatenated together into a single string and stored in a tag named
@values = $align->get_tag_values($tag)β give it a tag name, like'PAIRED'or'H0', and it returns the value(s). π Heads up:-expand_flagsmust be true in order to use the standard SAM flag constants as tags. Otherwise, you can fetch them by asking for the"FLAGS"tag, or by using the low-level methods described below.$is_true = $align->has_tag($tag)β returns true if the alignment has the indicated tag. β$string = $align->cigar_strβ returns the CIGAR string for this alignment in conventional human readable format (e.g."M34D1M1"). π¬ Cute, legible, we stan.$arrayref = $align->cigar_arrayβ returns a reference to an array representing the CIGAR string. It's an array of arrays, where each subarray consists of a CIGAR operation and a count. Like this:
[ ['M',34], ['D',1], ['M1',1] ]
Nested but make it fashion. π
($ref,$matches,$query) = $align->padded_alignmentβ returns three strings showing the alignment between the reference sequence (the target) and the query. It looks like this:
$ref AGTGCCTTTGTTCA-----ACCCCCTTGCAACAACC
$matches |||||||||||||| |||||||||||||||||
$query AGTGCCTTTGTTCACATAGACCCCCTTGCAACAACC
The gaps are the whole point, bestie. π
$str = $align->auxβ returns the text version of the SAM tags, e.g."XT:A:M NM:i:2 SM:i:37 AM:i:37 XM:i:1 XO:i:1 XG:i:1 MD:Z:6^C0A47".$str = $align->tam_lineβ returns the TAM (text) representation of the alignment. πΈ This one is available in the high-level"AlignWrapper"interface only.$tag = $align->primary_tagβ provided forBio::SeqFeatureIcompatibility. It returns the string"match". π―$tag = $align->source_tagβ also provided forBio::SeqFeatureIcompatibility. It returns the string"sam/bam". π§¬@parts = $align->get_SeqFeaturesβ returns subfeatures of this alignment. π Here's the tea:- If you fetched a
"read_pair"feature, this will be the two mate pair objects (both of typeBio::DB::Bam::AlignWrapper). - If you have
-split_splicesset to true in theBio::DB::Samdatabase, callingget_SeqFeatures()will return the components of split alignments. - For an example of how to use this, go peek at "Bio::DB::Sam Constructor and basic accessors" in
Bio::DB::Sam. π
- If you fetched a
β¨ to be continued, bestie, part 2 is coming β¨
lowkey the Low-Level Bio::DB::Bam::Alignment methods π¬
Okay bestie, these are the down-and-dirty ones. They live on objects of type Bio::DB::Bam::Alignment and on Bio::DB::Bam::AlignWrapper too, and they basically mirror the native C API almost one-for-one. So if you know the C side, you already know the vibe. π
$align = Bio::DB::Bam::Alignment->newβ makes you a brand-new, totally-empty alignment object. Ngl you rarely do this yourself; it's mostly for when you're iterating through aTAMfile withBio::DB::Tam->read1().$tid = $align->tid( [$new_tid] )β hands you the target ID of the alignment. You can pass in a newtidto change it, but bestie listen: currently this is the only field you can change, and the whole feature was implemented as a proof of principle. That's it. That's the whole cheat code. π$read_name = $align->qnameβ returns the name of the read. Perks of being the main character. π$pos = $align->posβ the 0-based leftmost coordinate of the aligned sequence, sitting on the reference sequence.$end = $align->calendβ the 0-based rightmost coordinate of the aligned sequence on the reference sequence, after we've taken alignment gaps into account. Gaps counted, no cap. π§’$len = $align->cigar2qlenβ the length of the query sequence, calculated from the CIGAR string.$quality = $align->qualβ the quality score for the alignment as a whole, the full picture.$flag = $align->flagβ the bitwise flag field (go read the SAM documentation, it's giving cryptic-on-purpose).$mtid = $align->mtidβ for paired reads, the target ID of the mate's alignment. Your read's plus-one. π«Ά$mpos = $align->mposβ for paired reads, the 0-based leftmost coordinate of the mate's alignment on the reference sequence.$n_cigar = $align->n_cigarβ how many CIGAR operations are in this alignment.$length = $align->l_qseqβ the length of the query sequence (aka the read).$dna = $align->qseqβ the actual DNA sequence of the query. And heads up, exactly like in a SAM file: reads aligned to the minus strand of the reference come back reverse complemented. Not a glitch, that's just the format's whole deal. β¨$score_str = $align->_qscoreβ a packed binary string holding the quality scores for every base of the read. Same length as the DNA, always. You can crack it open withunpack('C*',$score_str), or just use the high-levelqscore()method like a normal person.$score_arry = $align->qscore@score_arry = $align->qscoreβ in scalar context you get an array reference with the unpacked quality scores for every base of the query sequence; in list context you get an actual list of the scores. And this array is in the same orientation as the reference sequence. Obsessed with that consistency, fr fr.$score_str = $align->qstringβ returns the quality string in the exact same format used in the SAM (TAM) file. Same drip, different function. π$length = $align->isizeβ the calculated insert size for mapped paired reads.$length = $align->l_auxβ the length of the align "auxiliary" data.$value = $align->aux_get("tag")β give it an auxiliary tag, like"H0", and it gives you back that tag's value. Menu ordering but for metadata.@keys = $align->aux_keysβ returns the list of auxiliary tags this alignment knows about. The whole tag crew. π$data = $align->dataβ returns a packed string with the alignment data (sequence, quality scores and cigar string) all bundled up.$length = $align->data_lenβ the current length of the alignment data.$length = $align->m_dataβ the maximum length of the alignment data. Yes bestie, current vs. maximum, they are different, pay attention. π$is_paired = $align->pairedβ true if the aligned read is part of a mate/read pair, and it does not care one bit whether the mate actually mapped. Situationship status: paired, mate unknown.$is_proper = $align->proper_pairβ true if the aligned read is part of a mate/read pair and both partners mapped to the reference sequence. That's the healthy relationship. π$is_unmapped = $align->unmappedβ true if the read failed to align at all. It tried, it really did. π$mate_is_unmapped = $align->munmappedβ true if the read's mate failed to align.$reversed = $align->reversedβ true if the aligned read got reverse complemented before aligning.$mate_reversed = $align->mreversedβ true if the aligned read's mate got reverse complemented before aligning.$isize = $align->isizeβ for mate-pairs, returns the computed insert size. (Yep,isizeshows up twice on this page, that's not me being chaotic, that's the actual API. π)$arrayref = $align->cigarβ this one hands you the CIGAR data in its native BAM format. You get an arrayref where each operation and its count are packed together into an 8-bit structure. To decode each element you have to do these exact operations:
use Bio::DB::Sam::Constants;
my $c = $align->cigar;
my $op = $c->[0] & BAM_CIGAR_MASK;
my $len = $c->[0] >> BAM_CIGAR_SHIFT;
π SEE ALSO π
Bio::PerlBio::DB::SamBio::DB::Bam::Constants
π AUTHOR π
Lincoln Stein <lincoln.stein@oicr.on.ca>. <lincoln.stein@bmail.com>
Copyright (c) 2009-2015 Ontario Institute for Cancer Research.
This package and its accompanying libraries are free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0, the Apache 2.0 License, or the GNU General Public License (version 1 or higher). Refer to LICENSE for the full license text.
okay that's the whole page bestie, you're officially fluent in Bio::DB::Bam::Alignment π¦ no cap, go align some reads, you ate that π
β¨
And for the record, this was perl v5.42.3, dated 2026-08-19 β that's the Bio::D...ignment(3pm) page, wrapped with so much love. periodt. π
π¦ where this page comes from
- ubuntu stonking: libbio-samtools-perl 1.43-5build2 Β· GPL-1.0-or-later OR Artistic-2.0 OR Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms