Skip to content

Commit

Permalink
Docstrings, tweaks, formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonOresten committed Aug 17, 2024
1 parent f5b4371 commit 5cb1e1b
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 22 deletions.
12 changes: 10 additions & 2 deletions src/ProteinChains.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module ProteinChains

using Backboner

using BioStructures: BioStructures, PDBFormat, MMCIFFormat

include("properties.jl")

include("chain.jl")
Expand All @@ -19,10 +17,20 @@ export BackboneGeometry
include("secondary-structure.jl")

include("chain-properties.jl")
export assign_bond_lengths!
export assign_bond_angles!
export assign_torsion_angles!
export assign_secondary_structure!
export assign_standard_residue!
export assign_residue_rotations!
export assign_residue_rotations_quat!
export assign_residue_translations!
export assign_is_knotted!

include("io.jl")
export readrecord
export readpdb
export readcif
export pdbentry, @pdb_str

end
2 changes: 2 additions & 0 deletions src/chain-properties.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# TODO: store non-backbone atoms

function assign_bond_lengths!(chain::ProteinChain)
chain.bond_lengths = Backboner.get_bond_lengths(Backboner.Backbone(chain.backbone))
end
Expand Down
26 changes: 23 additions & 3 deletions src/chain.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
"""
ProteinChain
## Examples
```jldoctest
julia> structure = pdb"1EYE"
[ Info: Downloading file from PDB: 1EYE
1-chain ProteinStructure "1EYE.cif" with 0 properties:
256-residue ProteinChain "A" with 0 properties
julia> structure[1]
256-residue ProteinChain "A" with 0 properties:
fields:
id::String = "A"
aminoacids::String = "PVQVMGVLNVTDDSFSDGGCYLDLDDAVKHGLAMAAAGAGIVDVGGETSRVIPVVKELAAQGITVSIDTMRADVARAALQNGAQMVNDVSGGRADPAM…
backbone::Array{Float64, 3} = [45.592 44.171 43.719; -10.864 -10.936 -9.688; 30.192 30.504 31.278;;; 42.568 42.02 40.707; -9.163 …
numbers::Vector{Int64} = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14 … 265, 266, 267, 268, 269, 270, 271, 272, 273, 274]
properties: (none)
```
"""
mutable struct ProteinChain
id::String
aminoacids::String
Expand Down Expand Up @@ -30,13 +50,13 @@ end
Base.summary(chain::ProteinChain) = "$(countresidues(chain))-residue ProteinChain \"$(chain.id)\" with $(length(chain.properties)) properties"

function Base.show(io::IO, ::MIME"text/plain", chain::ProteinChain)
print(io, summary(chain), ":")
printstyled(io, "\n fields:", color=:yellow)
context = IOContext(io, :compact => true, :limit => true)
print(context, summary(chain), ":")
printstyled(context, "\n fields:", color=:yellow)
for fieldname in fieldnames(ProteinChain)[1:end-1]
printfield(context, chain, fieldname)
end
printstyled(io, "\n properties:", color=:yellow)
printstyled(context, "\n properties:", color=:yellow)
isempty(chain.properties) && print(io, " (none)")
for property in keys(chain.properties)
printfield(context, chain, property)
Expand Down
31 changes: 17 additions & 14 deletions src/io.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using BioStructures: BioStructures, PDBFormat, MMCIFFormat

const ProteinFileFormat = Union{PDBFormat, MMCIFFormat}
const AMINOACIDS = Set("ACDEFGHIKLMNPQRSTVWY")
const BACKBONE_ATOM_NAMES = ["N", "CA", "C"]
Expand Down Expand Up @@ -34,13 +36,16 @@ get_backbone(chain::BioStructures.Chain, selector) = get_backbone(BioStructures.
aminoacid_sequence(residues::Vector{BioStructures.AbstractResidue}) = join(oneletter_resname.(residues))
aminoacid_sequence(chain::BioStructures.Chain, selector) = aminoacid_sequence(BioStructures.collectresidues(chain, selector))

function get_residue_atoms(residues::Vector{BioStructures.AbstractResidue})
residue_atoms = Vector{Atom}[]
for res in residues
push!(residue_atoms, Atom.(BioStructures.collectatoms(res, a -> BioStructures.standardselector(a) && !BioStructures.disorderselector(a))))
#=function get_residue_atoms(residues::Vector{BioStructures.AbstractResidue})
atom_names = Vector{Vector{String}}[]
atom_coords = Vector{Vector{Float64}}[]
for residue in residues
residue_atoms = BioStructures.collectatoms(residue, a -> !backbone_atom_selector(a) && BioStructures.standardselector(a) && !BioStructures.disorderselector(a))
push!(atom_names, map(a -> a.name), residue_atoms)
push!(atom_coords, map(a -> a.coords), residue_atoms)
end
return residue_atoms
end
return atom_names, atom_coords
end=#

function ProteinChain(residues::Vector{BioStructures.AbstractResidue})
id = only(unique(BioStructures.chainid.(residues)))
Expand Down Expand Up @@ -89,14 +94,14 @@ Exported formats: `PDBFormat`, `MMCIFFormat`
## Examples
```jldoctest
julia> readrecord("example.pdb"); # detects PDB format from extension
```julia
readrecord("example.pdb"); # detects PDB format from extension
julia> readrecord("example.cif"); # detects mmCIF format from extension
readrecord("example.cif"); # detects mmCIF format from extension
julia> readrecord("example.abc", PDBFormat); # force PDB format
readrecord("example.abc", PDBFormat); # force PDB format
julia> readrecord("example.xyz", MMCIFFormat); # force mmCIF format
readrecord("example.xyz", MMCIFFormat); # force mmCIF format
```
"""
readrecord(path::AbstractString, format::Type{<:ProteinFileFormat}) = ProteinStructure(read(path, format))
Expand Down Expand Up @@ -197,6 +202,4 @@ macro pdb_str(pdbid)
quote
pdbentry($(esc(pdbid)))
end
end

export @pdb_str
end
4 changes: 2 additions & 2 deletions src/properties.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ function printfield(io::IO, x, field::Symbol; indent=4)
printstyled(io, "::"; color=:red)
printstyled(io, string(typeof(value)); color=:blue)
printstyled(io, " = "; color=:red)
printstyled(io, truncate(repr(value; context=io), 120))
printstyled(io, truncate(repr(value; context=io), 100))
end

function printproperty(io::IO, x, property::Symbol; indent=4)
value = getproperty(x, property)
print(io, "\n"*" "^indent)
printstyled(io, ":", string(property); color=:magenta)
printstyled(io, " => "; color=:red)
printstyled(io, truncate(repr(value; context=io), 120))
printstyled(io, truncate(repr(value; context=io), 100))
end

# TODO: `assign_property!(chain, property)` vs `assign_<property>!(chain)`
Expand Down
17 changes: 16 additions & 1 deletion src/structure.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""
ProteinStructure <: AbstractVector{ProteinChain}
"""
mutable struct ProteinStructure <: AbstractVector{ProteinChain}
name::String
chains::Vector{ProteinChain}
Expand All @@ -16,4 +19,16 @@ Base.setproperty!(structure::ProteinStructure, property::Symbol, value) = _setpr
Base.size(structure::ProteinStructure) = (length(structure.chains),)
Base.getindex(structure::ProteinStructure, i) = structure.chains[i]

Base.getindex(structure::ProteinStructure, id::AbstractString) = structure[findfirst(c -> c.id == id, structure.chains)]
Base.getindex(structure::ProteinStructure, id::AbstractString) = structure[findfirst(c -> c.id == id, structure.chains)]

Base.summary(structure::ProteinStructure) = "$(length(structure))-chain ProteinStructure \"$(structure.name)\" with $(length(structure.properties)) properties"

function Base.show(io::IO, ::MIME"text/plain", structure::ProteinStructure)
print(io, summary(structure), ":")
n, i = 10, 0
for chain in structure
(i += 1) <= n || break
print(io, "\n ", summary(chain))
end
i < length(structure) && print("\n")
end

0 comments on commit 5cb1e1b

Please sign in to comment.