Skip to content
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

Update ispalindromic #304

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/biosequence/predicates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,36 @@ end


"""
ispalindromic(seq::BioSequence)
ispalindromic(seq::NucSeq) -> Bool

Check if `seq` is palindromic. A palindromic sequence is identical to its
reverse-complement, so this should be equivalent to checking if
`seq == reverse_complement(seq)`.

# Examples
```jldoctest
julia> ispalindromic(dna"TGCA")
true

julia> ispalindromic(dna"TCCT")
false

julia> ispalindromic(rna"ACGGU")
false
```

Return `true` if `seq` is a palindromic sequence; otherwise return `false`.
"""
function ispalindromic(seq::BioSequence{<:NucleicAcidAlphabet})
for i in 1:cld(length(seq), 2)
if seq[i] != complement(seq[end - i + 1])
return false
end
end
_ispalindromic(seq)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to insert 4-spaces instead of a TAB-character here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops yes!

end

return true
# For two-bit alphabets, all odd-length sequences are not palindromic.
function ispalindromic(seq::BioSequence{<:Union{DNAAlphabet{2}, RNAAlphabet{2}}})
isodd(length(seq)) ? false : _ispalindromic(seq)
end

@inline _ispalindromic(seq) = all(seq[i] == complement(seq[end - i + 1]) for i in 1:cld(length(seq), 2))

"""
hasambiguity(seq::BioSequence)
Expand Down
Loading