Skip to content

Add getter methods (and forwarded getter methods) for alignment types #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 1, 2022
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added
- Getter functions for `AlignedSequence`, `PairwiseAlignment`, and
`PairwiseAlignmentResult`

## [2.2.0]

### Added
Expand Down
21 changes: 21 additions & 0 deletions docs/src/alignments.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,24 @@ julia> AlignedSequence(seq, ref)
ACGT--AAT--

```

You can get the underlying alignment and sequence back out of an
`AlignedSequence` by using the `alignment` and `sequence` functions.

```jldoctest
julia> aln = AlignedSequence(dna"ACGT--AAT--", dna"ACGTTTAT-GG")
Β·Β·Β·Β·Β·Β·Β·Β·-Β·Β·
ACGT--AAT--

julia> alignment(aln)
Alignment:
aligned range:
seq: 0-7
ref: 0-10
CIGAR string: 4=2D1=1X1I2D

julia> sequence(aln)
7nt DNA Sequence:
ACGTAAT

```
3 changes: 3 additions & 0 deletions docs/src/pairalign.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ PairwiseAlignment{BioSequences.LongSequence{BioSequences.DNAAlphabet{4}}, BioSeq
||| || || |
ref: 1 ACCT-GGTATGATAGCG 16

julia> sequence(res)
10nt DNA Sequence:
CCTAGGAGGG

julia> count_matches(aln)
8
Expand Down
3 changes: 2 additions & 1 deletion src/BioAlignments.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export
score,
distance,
alignment,
hasalignment
hasalignment,
sequence

using BioGenerics
import BioGenerics: distance
Expand Down
15 changes: 15 additions & 0 deletions src/alignedseq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ function AlignedSequence(seq::BioSequences.BioSequence, seqpos::Integer,
return AlignedSequence(newseq, anchors)
end

# Getter functions
"""
alignment(aligned_sequence)

Gets the [`Alignment`](@ref) of `aligned_sequence`.
"""
alignment(alnseq::AlignedSequence) = alnseq.aln

"""
sequence(aligned_sequence)

Return the sequence of `aligned_sequence`.
"""
sequence(alnseq::AlignedSequence) = alnseq.seq

# First position in the reference sequence.
function IntervalTrees.first(alnseq::AlignedSequence)
return alnseq.aln.firstref
Expand Down
15 changes: 15 additions & 0 deletions src/pairwise/alignment.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ mutable struct PairwiseAlignment{S1,S2}
b::S2
end

# Getter functions
"""
alignment(pairwise_alignment)

Gets the underlying [`Alignment`](@ref) from `pairwise_alignment`.
"""
alignment(aln::PairwiseAlignment) = alignment(aln.a)

"""
sequence(pairwise_alignment)

Gets the query sequence of `pairwise_alignment`.
"""
sequence(aln::PairwiseAlignment) = sequence(aln.a)

function Base.iterate(aln::PairwiseAlignment, ij=(2,1))
i, j = ij
if i > lastindex(aln.a.aln.anchors)
Expand Down
12 changes: 11 additions & 1 deletion src/pairwise/result.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ hasalignment(aln::PairwiseAlignmentResult) = aln.aln !== nothing
"""
alignment(alignment_result)

Return alignment if any.
Return the alignment if any as a [`PairwiseAlignment`](@ref). To get the
[`Alignment`](@ref), nest the function, e.g. `alignment(alignment(alignment_result))`.
This function returns a `PairwiseAlignment` instead of an `Alignment` for
backwards-compatibility reasons.

See also: `hasalignment`
"""
Expand All @@ -64,6 +67,13 @@ function alignment(aln::PairwiseAlignmentResult)
return aln.aln
end

"""
sequence(alignment_result)

Return the query sequence of `alignment_result`, if it exists.
"""
sequence(aln::PairwiseAlignmentResult) = sequence(alignment(aln))


# Printer
# -------
Expand Down