Skip to content

Commit

Permalink
formatted code
Browse files Browse the repository at this point in the history
  • Loading branch information
lmiq committed Jun 16, 2023
1 parent df0c628 commit e29d913
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 107 deletions.
3 changes: 1 addition & 2 deletions docs/liveserver.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

import Pkg
Pkg.activate("LiveServer"; shared=true)
Pkg.activate("LiveServer"; shared = true)
Pkg.add("LiveServer")

import LiveServer

Pkg.activate(@__DIR__)
LiveServer.servedocs()

12 changes: 6 additions & 6 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ Pkg.add("Documenter")
using ComplexMixtures
using Documenter
using ComplexMixtures
push!(LOAD_PATH,"../src/")
push!(LOAD_PATH, "../src/")
makedocs(
modules=[ComplexMixtures],
sitename="ComplexMixtures.jl",
modules = [ComplexMixtures],
sitename = "ComplexMixtures.jl",
pages = [
"Introduction" => "index.md",
"Installation" => "installation.md",
Expand All @@ -24,12 +24,12 @@ makedocs(
"Tools" => "tools.md",
"From Python" => "python.md",
"Help entries" => "help.md",
"References" => "references.md"
]
"References" => "references.md",
],
)
deploydocs(
repo = "github.com/m3g/ComplexMixtures.jl.git",
target = "build",
branch = "gh-pages",
versions = ["stable" => "v^", "v#.#" ],
versions = ["stable" => "v^", "v#.#"],
)
2 changes: 1 addition & 1 deletion src/Selection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ end
@testitem "Selection Vector{PDBTools.Atom}" begin
import PDBTools
pdbfile = ComplexMixtures.Testing.pdbfile
atoms = PDBTools.readPDB(pdbfile, "protein and residue 2")
atoms = PDBTools.readPDB(pdbfile, "protein and residue 2")
s = Selection(atoms, nmols = 1, natomspermol = 11)
@test s.imol == ones(Int, 11)
@test s.index == [12 + i for i = 1:11]
Expand Down
3 changes: 1 addition & 2 deletions src/legacy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ const contrib = contributions
using ComplexMixtures, PDBTools
import ComplexMixtures.Testing: test_dir

pdb = readPDB("$test_dir/data/NAMD/structure.pdb")
pdb = readPDB("$test_dir/data/NAMD/structure.pdb")
R = load("$test_dir/data/NAMD/protein_tmao.json")

@test R.sum_md_count == R.coordination_number
@test R.sum_md_count_random == R.coordination_number_random

@test ComplexMixtures.contrib == ComplexMixtures.contributions
end

34 changes: 19 additions & 15 deletions src/mddf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
end
function Buffer(traj::Trajectory, R::Result)
return Buffer(
solute_read = similar(traj.x_solute),
solute_read = similar(traj.x_solute),
solvent_read = similar(traj.x_solvent),
ref_solutes = zeros(Int, R.options.n_random_samples),
list = fill(zero(MinimumDistance), R.solvent.nmols),
Expand All @@ -33,7 +33,7 @@ end
traj = Trajectory("$(Testing.data_dir)/NAMD/trajectory.dcd", protein, tmao)
R = Result(traj, options)
b0 = ComplexMixtures.Buffer(
solute_read = similar(traj.x_solute),
solute_read = similar(traj.x_solute),
solvent_read = similar(traj.x_solvent),
ref_solutes = zeros(Int, R.options.n_random_samples),
list = fill(zero(ComplexMixtures.MinimumDistance), R.solvent.nmols),
Expand Down Expand Up @@ -70,13 +70,13 @@ function randomize_solvent!(
)
for isolvent = 1:R.solvent.nmols
# Choose randomly one molecule from the bulk, if there are bulk molecules
if n_solvent_in_bulk > 0
if n_solvent_in_bulk > 0
jmol = buff.indexes_in_bulk[rand(RNG, 1:n_solvent_in_bulk)]
else
jmol = rand(RNG, 1:R.solvent.nmols)
end
# Pick coordinates of the molecule to be randomly moved
y_new = viewmol(isolvent, system.ypositions, R.solvent)
y_new = viewmol(isolvent, system.ypositions, R.solvent)
# Copy the coordinates of the random solvent molecule chosen
y_new .= viewmol(jmol, buff.solvent_read, R.solvent)
# Randomize rotations and translation for this molecule
Expand Down Expand Up @@ -143,19 +143,20 @@ function mddf(trajectory::Trajectory, options::Options = Options())
end

# Print some information about this run
if !options.silent
if !options.silent
title(R, trajectory.solute, trajectory.solvent, nchunks)
progress = Progress(R.nframes_read, 1)
end

# Loop over the trajectory
read_lock = ReentrantLock()
Threads.@threads for (frame_range, ichunk) in ChunkSplitters.chunks(1:R.nframes_read, nchunks)
Threads.@threads for (frame_range, ichunk) in
ChunkSplitters.chunks(1:R.nframes_read, nchunks)
# Reset the number of frames read by each chunk
R_chunk[ichunk].nframes_read = 0
for _ in frame_range
# Read frame coordinates
lock(read_lock) do
lock(read_lock) do
# skip frames if stride > 1
while (iframe + 1) % options.stride != 0
nextframe!(trajectory)
Expand All @@ -171,8 +172,9 @@ function mddf(trajectory::Trajectory, options::Options = Options())
@. buff[ichunk].solvent_read = trajectory.x_solvent
update_unitcell!(system[ichunk], getsides(trajectory, iframe))
# Run GC if memory is getting full: this are issues with Chemfiles reading scheme
if options.GC && (Sys.free_memory() / Sys.total_memory() < options.GC_threshold)
GC.gc()
if options.GC &&
(Sys.free_memory() / Sys.total_memory() < options.GC_threshold)
GC.gc()
end
options.silent || next!(progress)
end # release reading lock
Expand Down Expand Up @@ -286,7 +288,8 @@ end
atoms = readPDB("$(Testing.data_dir)/toy/cross.pdb")
protein = Selection(select(atoms, "protein and model 1"), nmols = 1)
water = Selection(select(atoms, "resname WAT and model 1"), natomspermol = 3)
traj = Trajectory("$(Testing.data_dir)/toy/cross.pdb", protein, water, format = "PDBTraj")
traj =
Trajectory("$(Testing.data_dir)/toy/cross.pdb", protein, water, format = "PDBTraj")

for lastframe in [1, 2]
options = Options(
Expand All @@ -309,7 +312,8 @@ end
# Self correlation
atoms = readPDB("$(Testing.data_dir)/toy/self_monoatomic.pdb")
atom = Selection(select(atoms, "resname WAT and model 1"), natomspermol = 1)
traj = Trajectory("$(Testing.data_dir)/toy/self_monoatomic.pdb", atom, format="PDBTraj")
traj =
Trajectory("$(Testing.data_dir)/toy/self_monoatomic.pdb", atom, format = "PDBTraj")

# without atoms in the bulk
options = Options(
Expand Down Expand Up @@ -377,15 +381,15 @@ end
R = mddf(traj, options)
# Deterministic
@test R.volume.total 603078.4438609097
@test sum(R.md_count) 25.250000000000007
@test sum(R.md_count) 25.250000000000007
@test sum(R.rdf_count) 19.550000000000004
@test R.density.solute 1.6581590839128614e-6
@test R.density.solvent 0.00030012679418822794
# Dependent on the random number seed
@test R.volume.domain 75368.14585709268 rtol = 0.1
@test R.volume.bulk 527710.298003817 rtol = 0.1
@test R.density.solvent_bulk 0.000305944380109164 rtol = 0.1
@test sum(R.mddf) 582.8371304452286 rtol = 0.1
@test sum(R.mddf) 582.8371304452286 rtol = 0.1
@test sum(R.rdf) 491.4450029864516 rtol = 0.1
@test R.kb[end] -6019.863896959123 rtol = 0.5
@test R.kb_rdf[end] -6905.975623304156 rtol = 0.5
Expand All @@ -401,10 +405,10 @@ end
@test R.density.solvent 0.00030012679418822794
# Dependent on the random number seed
@test R.volume.domain 6801.384672431371 rtol = 0.1
@test R.volume.bulk 596277.0591884783 rtol = 0.1
@test R.volume.bulk 596277.0591884783 rtol = 0.1
@test R.density.solvent_bulk 0.00029875568324470034 rtol = 0.1
@test sum(R.mddf) 275.5648734200309 rtol = 0.1
@test sum(R.rdf) 168.77009506954508 rtol = 0.1
@test sum(R.rdf) 168.77009506954508 rtol = 0.1
@test R.kb[end] -386.8513153147712 rtol = 0.5
@test R.kb_rdf[end] -326.32083509753284 rtol = 0.5
end
18 changes: 15 additions & 3 deletions src/minimum_distances.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,15 @@ Function that updates a list of minimum distances given the indexes of the atoms
for autocorrelations (such that the identity of `isolute` is needed)
"""
function update_list!(i, j, d2, jref_atom, j_natoms_per_molecule, isolute, list::Vector{MinimumDistance})
function update_list!(
i,
j,
d2,
jref_atom,
j_natoms_per_molecule,
isolute,
list::Vector{MinimumDistance},
)
jmol = mol_index(j, j_natoms_per_molecule)
if jmol != isolute
d = sqrt(d2)
Expand Down Expand Up @@ -150,7 +158,8 @@ function minimum_distances!(
jnatomspermol = R.solvent.natomspermol
if R.autocorrelation
map_pairwise!(
(x, y, i, j, d2, list) -> update_list!(i, j, d2, jref_atom, jnatomspermol, isolute, list),
(x, y, i, j, d2, list) ->
update_list!(i, j, d2, jref_atom, jnatomspermol, isolute, list),
system;
update_lists = update_lists,
)
Expand Down Expand Up @@ -183,7 +192,10 @@ function setup_PeriodicSystem(trajectory::Trajectory, options::Options)
closetraj!(trajectory)
system = PeriodicSystem(
xpositions = zeros(SVector{3,Float64}, trajectory.solute.natomspermol),
ypositions = zeros(SVector{3,Float64}, trajectory.solvent.nmols * trajectory.solvent.natomspermol),
ypositions = zeros(
SVector{3,Float64},
trajectory.solvent.nmols * trajectory.solvent.natomspermol,
),
unitcell = unitcell,
cutoff = options.usecutoff ? options.cutoff : options.dbulk,
output = fill(zero(MinimumDistance), trajectory.solvent.nmols),
Expand Down
11 changes: 7 additions & 4 deletions src/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
# Putting some things in `@setup_workload` instead of `@compile_workload` can reduce the size of the
# precompile file and potentially make loading faster.
dir = "$(Testing.data_dir)/NAMD"
atoms = readPDB("$dir/structure.pdb","resname TMAO")
atoms = readPDB("$dir/structure.pdb", "resname TMAO")
tmao1 = Selection(select(atoms, "resname TMAO and resnum 1"), natomspermol = 14)
@compile_workload begin
options = Options(lastframe=-1, silent = true)
tmao2 = Selection(select(atoms, "resname TMAO and resnum 2 or resname TMAO and resnum 3"), natomspermol = 14)
options = Options(lastframe = -1, silent = true)
tmao2 = Selection(
select(atoms, "resname TMAO and resnum 2 or resname TMAO and resnum 3"),
natomspermol = 14,
)
traj = Trajectory("$dir/trajectory.dcd", tmao1, tmao2)
R = mddf(traj, options)
traj = Trajectory("$dir/trajectory.dcd", tmao2)
R = mddf(traj, options)
end
end
end
18 changes: 15 additions & 3 deletions src/rigid_body.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ function random_move!(

# Generate random coordinates for the center of mass
cmin, cmax = PeriodicSystems.get_computing_box(system)
newcm = SVector{3}(scale * (cmin[i] + rand(RNG, Float64) * (cmax[i] - cmin[i])) for i in 1:3)
newcm = SVector{3}(
scale * (cmin[i] + rand(RNG, Float64) * (cmax[i] - cmin[i])) for i = 1:3
)

# Generate random rotation angles
beta = 2π * rand(RNG, Float64)
Expand Down Expand Up @@ -164,7 +166,12 @@ end
RNG = ComplexMixtures.init_random(Options())
# Orthorhombic cell
x = [-1.0 .+ 2 * rand(SVector{3,Float64}) for _ = 1:5]
system = PeriodicSystem(positions=x, cutoff=0.1, unitcell=SVector(10.0, 10.0, 10.0), output=0.0)
system = PeriodicSystem(
positions = x,
cutoff = 0.1,
unitcell = SVector(10.0, 10.0, 10.0),
output = 0.0,
)
@test check_internal_distances(x, ComplexMixtures.random_move!(copy(x), 1, system, RNG))
system.xpositions .= [-9.0 .+ 2 * rand(SVector{3,Float64}) for _ = 1:5]
@test check_internal_distances(x, ComplexMixtures.random_move!(copy(x), 1, system, RNG))
Expand All @@ -173,7 +180,12 @@ end

# Triclinic cell
x = [-1.0 .+ 2 * rand(SVector{3,Float64}) for _ = 1:5]
system = PeriodicSystem(positions=x, cutoff=0.1, unitcell=@SMatrix[10.0 5.0 0.0; 0.0 10.0 0.0; 0.0 0.0 10.0], output=0.0)
system = PeriodicSystem(
positions = x,
cutoff = 0.1,
unitcell = @SMatrix[10.0 5.0 0.0; 0.0 10.0 0.0; 0.0 0.0 10.0],
output = 0.0,
)
@test check_internal_distances(x, ComplexMixtures.random_move!(copy(x), 1, system, RNG))
system.xpositions .= [-9.0 .+ 2 * rand(SVector{3,Float64}) for _ = 1:5]
@test check_internal_distances(x, ComplexMixtures.random_move!(copy(x), 1, system, RNG))
Expand Down
23 changes: 14 additions & 9 deletions src/tools/contributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ with indexes `[901, 902, 903]`.
"""
function contributions(
s::Selection,
atom_contributions::Matrix{Float64},
indexes::Vector{Int};
first_atom_is_ref = false
s::Selection,
atom_contributions::Matrix{Float64},
indexes::Vector{Int};
first_atom_is_ref = false,
)
nbins = size(atom_contributions, 1)
c = zeros(nbins)
Expand All @@ -44,7 +44,9 @@ function contributions(
else
for it in indexes
if it > s.natomspermol
error("The index list contains atoms with indexes greater than the number of atoms of one molecule.")
error(
"The index list contains atoms with indexes greater than the number of atoms of one molecule.",
)
end
c += @view(atom_contributions[:, it])
end
Expand All @@ -56,9 +58,9 @@ end
# If a list of atom names is provided
#
function contributions(
s::Selection,
atom_contributions::Matrix{Float64},
names::Vector{String}
s::Selection,
atom_contributions::Matrix{Float64},
names::Vector{String},
)
indexes = Vector{Int}(undef, 0)
for name in names
Expand Down Expand Up @@ -119,7 +121,10 @@ end
atoms = readPDB("$dir/trajectory.pdb", "model 1")

solute = Selection(select(atoms, "resname TMAO and resnum 1"), nmols = 1)
solvent = Selection(select(atoms, "resname TMAO and resnum 2 or resname TMAO and resnum 3"), nmols = 2)
solvent = Selection(
select(atoms, "resname TMAO and resnum 2 or resname TMAO and resnum 3"),
nmols = 2,
)

traj = Trajectory("$dir/trajectory.pdb", solute, solvent, format = "PDBTraj")
results = mddf(traj)
Expand Down
24 changes: 16 additions & 8 deletions src/tools/coordination_number.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,22 @@ residue50_coordination[findlast(<(5), R.d)]
```
"""
function coordination_number(s::Selection, atom_contributions::Matrix{Float64}, R::Result, group)
function coordination_number(
s::Selection,
atom_contributions::Matrix{Float64},
R::Result,
group,
)
# Extract the group contributions to the MDDF
group_contributions = contributions(s, atom_contributions, group)
# Compute the coordination number
return coordination_number(R, group_contributions)
end
function coordination_number(R::Result, group_contributions::Vector{Float64})
cn = cumsum(group_contributions[i] * R.md_count_random[i] for i in eachindex(group_contributions))
cn = cumsum(
group_contributions[i] * R.md_count_random[i] for
i in eachindex(group_contributions)
)
return cn
end

Expand All @@ -73,16 +81,16 @@ end
using ComplexMixtures, PDBTools
import ComplexMixtures.Testing: test_dir

pdb = readPDB("$test_dir/data/NAMD/structure.pdb")
pdb = readPDB("$test_dir/data/NAMD/structure.pdb")
R = load("$test_dir/data/NAMD/protein_tmao.json")
solute = Selection(PDBTools.select(pdb, "protein"), nmols=1)
solute = Selection(PDBTools.select(pdb, "protein"), nmols = 1)
cn = coordination_number(solute, R.solute_atom, R, PDBTools.select(pdb, "residue 50"))
@test cn[findlast(<(5), R.d)] 0.24999999999999997 atol=1e-10
@test cn[findlast(<(5), R.d)] 0.24999999999999997 atol = 1e-10

pdb = readPDB("$test_dir/data/NAMD/Protein_in_Glycerol/system.pdb")
R = load("$test_dir/data/NAMD/Protein_in_Glycerol/protein_water.json")
R = load("$test_dir/data/NAMD/Protein_in_Glycerol/protein_water.json")
group = PDBTools.select(pdb, "protein")
solute = Selection(group, nmols=1)
solute = Selection(group, nmols = 1)
cn = coordination_number(solute, R.solute_atom, R, group)
@test maximum(R.coordination_number .- cn) < 1e-10
end
end
Loading

2 comments on commit e29d913

@lmiq
Copy link
Member Author

@lmiq lmiq commented on e29d913 Jun 16, 2023

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/85700

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 v1.0.0 -m "<description of version>" e29d9137ed0e60c64952ae7ffdae5f7b8b7ea40d
git push origin v1.0.0

Please sign in to comment.