Skip to content

Commit

Permalink
Reduce allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
EvoArt committed Jan 8, 2025
1 parent 8267ba1 commit 17c0947
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "AssignTaxonomy"
uuid = "941572e2-79a6-4ec3-8380-7ad5c8caa571"
authors = ["Arthur Newbury"]
version = "0.1.2"
version = "0.1.3"

[deps]
BioSequences = "7e6ae17a-c86d-528c-b3b9-7f778a29fe59"
Expand All @@ -14,10 +14,10 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
[compat]
BioSequences = "3"
FASTX = "2"
Kmers = "1"
Polyester = "0.7"
PrettyTables = "2"
Tables = "1"
Polyester = "0.7"
Kmers = "1"
julia = "1.6"

[extras]
Expand Down
32 changes: 24 additions & 8 deletions src/classifier.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Tables.columnnames(m::ClassificationResult) = names(m)

#### Underlying algorithm
function naieve_bayes(seqs::Vector,refs::Vector,k, n_bootstrap,lp,genera)

t = time()
N = length(refs)
n = length(seqs)
assignments = Vector{Int64}(undef,n)
Expand All @@ -65,19 +65,18 @@ function naieve_bayes(seqs::Vector,refs::Vector,k, n_bootstrap,lp,genera)
priors, a =count_mers(refs)
word_priors!(priors,N)
@batch for i in 1:N
log_probs[i] .= log.(conditional_prob.(a[i],priors,1))
@fastmath log_probs[i] .= log.(conditional_prob.(a[i],priors,1))
end
else
log_probs = lp
end
@batch for i in 1:n
kmer_array = count_seq_mers(seqs[i])
assignment = assign(kmer_array,log_probs)
assignment = assign(eachindex(kmer_array)[kmer_array],log_probs)
assignments[i] =assignment
sample_size = sum(kmer_array) ÷ k
confs[i] = bootstrap(vec(kmer_array),log_probs,genera[assignment],sample_size,n_bootstrap,genera)
end

return assignments, confs, log_probs
end

Expand All @@ -87,16 +86,33 @@ function naieve_bayes(seqs::Vector,refs::Vector,taxa ::Array,k, n_bootstrap,lp=f
return hcat(t,c),l
end

function assign(seq_mask,log_probs)
cond_probs =[sum(log_prob[seq_mask]) for log_prob in log_probs]
return findmax(cond_probs)[2]
function assign(seq_mask,log_probs)

cp_max = Float32(-Inf)
ind = 1
for i in eachindex(log_probs)
cp = Float32(0)
@simd for j in seq_mask
@fastmath cp += log_probs[i][j]
end
if cp > cp_max
cp_max = cp
ind = i
end
end
return ind
end


function bootstrap(kmer_vec,log_probs,assignment, sample_size,n_bootstrap,genera)

hits = 0
seq_inds = eachindex(kmer_vec)[kmer_vec]
inds = seq_inds[1:sample_size]
for i in 1:n_bootstrap
inds = rand(seq_inds,sample_size)
for j in 1:sample_size
inds[j] = rand(seq_inds)
end
if genera[assign(inds,log_probs)] == assignment
hits +=1
end
Expand Down
6 changes: 3 additions & 3 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ function get_targets(seq_fasta)
end


word_prior(n,N) = (n+0.5)/(N+1)
word_prior(n,N) = @fastmath (n+0.5)/(N+1)

function word_priors!(priors ::Array,N)
for i in eachindex(priors)
priors[i] = word_prior(priors[i],N)
end
end

conditional_prob(m,P,M) = (m+P)/(M+1)
conditional_prob(m,P,M) = @fastmath (m+P)/(M+1)

#### Count kmers

Expand All @@ -76,7 +76,7 @@ end

function count_ref_mers!(ref_seq,kmer_array,tot_kmer_array,k = 8)
kmer_array .= count_kmers(ref_seq,k)
tot_kmer_array .+= kmer_array
@fastmath tot_kmer_array .+= kmer_array
end

function count_seq_mers(seq,k = 8)
Expand Down

2 comments on commit 17c0947

@EvoArt
Copy link
Owner Author

@EvoArt EvoArt commented on 17c0947 Jan 8, 2025

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/122607

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.3 -m "<description of version>" 17c0947ceb0a59a345021c3ac547be4842145efa
git push origin v0.1.3

Also, note the warning: Version 0.1.3 skips over 0.1.2
This can be safely ignored. However, if you want to fix this you can do so. Call register() again after making the fix. This will update the Pull request.

Please sign in to comment.