|
1 | 1 | """
|
2 |
| - write_pdb(filename::String, atoms::AbstractVector{<:Atom}, selection; header=:auto, footer=:auto) |
| 2 | + write_pdb(filename::String, atoms::AbstractVector{<:Atom}, [selection]; header=:auto, footer=:auto, append=false) |
3 | 3 |
|
4 | 4 | Write a PDB file with the atoms in `atoms` to `filename`. The `selection` argument is a string
|
5 | 5 | that can be used to select a subset of the atoms in `atoms`. For example, `write_pdb("test.pdb", atoms, "name CA")`.
|
6 | 6 |
|
7 |
| -The `header` and `footer` arguments can be used to add a header and footer to the PDB file. If `header` is `:auto`, |
8 |
| -then a header will be added with the number of atoms in `atoms`. If `footer` is `:auto`, then a footer will be added |
9 |
| -with the "END" keyword. Either can be set to `nothing` if no header or footer is desired. |
| 7 | +# Arguments |
| 8 | +
|
| 9 | +- `filename::String`: The name of the file to write. |
| 10 | +- `atoms::AbstractVector{<:Atom}`: The atoms to write to the file. |
| 11 | +
|
| 12 | +# Optional positional argument |
| 13 | +
|
| 14 | +- `selection::String`: A selection string to select a subset of the atoms in `atoms`. |
| 15 | +
|
| 16 | +# Keyword arguments |
| 17 | +
|
| 18 | +- `header::Union{String, Nothing}=:auto`: The header to add to the PDB file. If `:auto`, a header will be added with the number of atoms in `atoms`. |
| 19 | +- `footer::Union{String, Nothing}=:auto`: The footer to add to the PDB file. If `:auto`, a footer will be added with the "END" keyword. |
| 20 | +- `append::Bool=false`: If `true`, the atoms will be appended to the file instead of overwriting it. |
| 21 | +
|
| 22 | +!!! compat |
| 23 | + The `append` keyword argument is available in PDBTools.jl v2.7.0 and later. |
10 | 24 |
|
11 | 25 | """
|
12 |
| -function write_pdb(filename::String, atoms::AbstractVector{<:Atom}, selection::String; header=:auto, footer=:auto) |
| 26 | +function write_pdb( |
| 27 | + filename::String, |
| 28 | + atoms::AbstractVector{<:Atom}, |
| 29 | + selection::String; |
| 30 | + header=:auto, |
| 31 | + footer=:auto, |
| 32 | + append=false, |
| 33 | +) |
13 | 34 | query = parse_query(selection)
|
14 |
| - write_pdb(filename, atoms, only=atom -> apply_query(query, atom); header, footer) |
| 35 | + write_pdb(filename, atoms, only=atom -> apply_query(query, atom); header, footer, append) |
15 | 36 | end
|
16 | 37 |
|
17 |
| -function write_pdb(filename::String, atoms::AbstractVector{<:Atom}; only::Function=all, header=:auto, footer=:auto) |
18 |
| - file = open(expanduser(filename), "w") |
19 |
| - if header == :auto |
20 |
| - curr_date = Dates.format(Dates.today(), "dd-u-yy") |
21 |
| - header = "PDBTools.jl - $(length(atoms)) atoms" |
22 |
| - println(file, @sprintf "%-10s%-40s%9s" "HEADER" header curr_date) |
23 |
| - elseif header !== nothing |
24 |
| - println(file, header) |
25 |
| - end |
26 |
| - for atom in atoms |
27 |
| - if only(atom) |
28 |
| - println(file, write_pdb_atom(atom)) |
| 38 | +function write_pdb( |
| 39 | + filename::String, atoms::AbstractVector{<:Atom}; |
| 40 | + only::Function=all, |
| 41 | + append=false, |
| 42 | + header=:auto, |
| 43 | + footer=:auto, |
| 44 | +) |
| 45 | + open(expanduser(filename), append ? "a" : "w") do io |
| 46 | + if header == :auto |
| 47 | + curr_date = Dates.format(Dates.today(), "dd-u-yy") |
| 48 | + header = "PDBTools.jl - $(length(atoms)) atoms" |
| 49 | + println(io, @sprintf "%-10s%-40s%9s" "HEADER" header curr_date) |
| 50 | + elseif header !== nothing |
| 51 | + println(io, header) |
| 52 | + end |
| 53 | + for atom in atoms |
| 54 | + if only(atom) |
| 55 | + println(io, write_pdb_atom(atom)) |
| 56 | + end |
| 57 | + end |
| 58 | + if footer == :auto |
| 59 | + println(io, "END") |
| 60 | + elseif footer !== nothing |
| 61 | + println(io, footer) |
29 | 62 | end
|
30 | 63 | end
|
31 |
| - if footer == :auto |
32 |
| - println(file, "END") |
33 |
| - elseif footer !== nothing |
34 |
| - println(file, footer) |
35 |
| - end |
36 |
| - close(file) |
37 | 64 | end
|
38 | 65 |
|
39 | 66 | @testitem "write_pdb" begin
|
|
46 | 73 | f1 = readdlm(PDBTools.SMALLPDB, '\n', header=true)
|
47 | 74 | f2 = readdlm(tmpfile, '\n', header=true)
|
48 | 75 | @test f1[1] == f2[1]
|
| 76 | + # test selection |
| 77 | + write_pdb(tmpfile, pdb, "name CA") |
| 78 | + f3 = read_pdb(tmpfile) |
| 79 | + @test length(f3) == 3 |
| 80 | + # test header and footer |
| 81 | + write_pdb(tmpfile, pdb, "name CA"; header="HEADER test", footer="END test") |
| 82 | + s = split(String(read(tmpfile))) |
| 83 | + @test (s[begin],s[begin+1]) == ("HEADER", "test") |
| 84 | + @test (s[end-1],s[end]) == ("END", "test") |
| 85 | + # test append |
| 86 | + append_pdb = tempname() * ".pdb" |
| 87 | + write_pdb(append_pdb, pdb; append=true) |
| 88 | + pdb1 = read_pdb(append_pdb) |
| 89 | + @test length(eachmodel(pdb1)) == 1 |
| 90 | + @test length(pdb1) == 35 |
| 91 | + write_pdb(append_pdb, pdb; append=true) |
| 92 | + pdb2 = read_pdb(append_pdb) |
| 93 | + @test length(eachmodel(pdb2)) == 2 |
| 94 | + @test length(pdb2) == 70 |
49 | 95 | end
|
50 | 96 |
|
51 | 97 | #
|
|
0 commit comments