Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_mmtf_consistency(format, start, stop, step, chunk_size):
if format == "netcdf" and stop is not None and step is not None:
# Currently, there is an inconsistency in in MDTraj's
# NetCDFTrajectoryFile class:
# In this class the number of frames in the output arrays
# is dependent on the 'stride' parameter
return
# MMTF is used as reference for consistency check
# due to higher performance
ref_traj = strucio.load_structure(join(data_dir("structure"), "1l2y.mmtf"))
ref_traj = ref_traj[slice(start, stop, step)]
# Template is first model of the reference
template = ref_traj[0]
if format == "trr":
traj_file_cls = trr.TRRFile
if format == "xtc":
traj_file_cls = xtc.XTCFile
if format == "tng":
traj_file_cls = tng.TNGFile
if format == "dcd":
traj_file_cls = dcd.DCDFile
if format == "netcdf":
traj_file_cls = netcdf.NetCDFFile
traj_file = traj_file_cls.read(
join(data_dir("structure"), f"1l2y.{format}"),
def test_saving(suffix):
array = strucio.load_structure(join(data_dir("structure"), "1l2y.mmtf"))
temp = NamedTemporaryFile("w+", suffix=f".{suffix}")
strucio.save_structure(temp.name, array)
temp.close()
def test_pdbx_consistency(path):
cif_path = splitext(path)[0] + ".cif"
array1 = strucio.load_structure(path)
array2 = strucio.load_structure(cif_path)
if array2.box is not None:
assert np.allclose(array1.box, array2.box)
assert array1.bonds == array2.bonds
for category in array1.get_annotation_categories():
assert array1.get_annotation(category).tolist() == \
array2.get_annotation(category).tolist()
assert array1.coord.tolist() == array2.coord.tolist()
def test_invalid_structure():
array = strucio.load_structure(join(data_dir("structure"), "5ugo.mmtf"))
# Get DNA chain -> Invalid for DSSP
chain = array[array.chain_id == "T"]
with pytest.raises(SubprocessError):
DsspApp.annotate_sse(chain)
def test_index_distance_periodic_orthogonal(shift):
"""
The PBC aware computation, should give the same results,
irrespective of which atoms are centered in the box
"""
array = strucio.load_structure(join(data_dir("structure"), "3o5r.mmtf"))
# Use a box based on the boundaries of the structure
# '+1' to add a margin
array.box = np.diag(
np.max(array.coord, axis=0) - np.min(array.coord, axis=0) + 1
)
length = array.array_length()
dist_indices = np.stack([
np.repeat(np.arange(length), length),
np.tile(np.arange(length), length)
], axis=1)
ref_dist = struc.index_distance(array, dist_indices, periodic=True)
array.coord += shift
array.coord = struc.move_inside_box(array.coord, array.box)
dist = struc.index_distance(array, dist_indices, periodic=True)
# Iterate over each residue
ids, names = struc.get_residues(array)
enantiomers = np.zeros(len(ids), dtype=int)
for i, id in enumerate(ids):
coord = array.coord[array.res_id == id]
if len(coord) != 4:
# Glyine -> no chirality
enantiomers[i] = 0
else:
enantiomers[i] = get_enantiomer(coord[0], coord[1],
coord[2], coord[3])
return enantiomers
# Fetch and parse structure file
file = rcsb.fetch("1l2y", "mmtf", biotite.temp_dir())
stack = strucio.load_structure(file)
# Get first model
array = stack[0]
# Get enantiomers
print("1l2y ", analyze_chirality(array))
# Reflected structures have opposite enantiomers
# Test via reflection at x-y-plane, z -> -z
array_reflect = array.copy()
array_reflect.coord[:,2] *= -1
print("1l2y (reflected)", analyze_chirality(array_reflect))
protein_chain,
# The last array element will be the length of the atom array,
# i.e. no valid index
add_exclusive_stop=True
)
for i in range(len(residue_starts) -1):
res_start = residue_starts[i]
res_stop = residue_starts[i+1]
oscillation[:, res_start:res_stop, :] \
= protein_chain.coord[res_start:res_stop, :] + deviation[:, i:i+1, :]
# An atom array stack containing all frames
oscillating_structure = struc.from_template(protein_chain, oscillation)
# Save as PDB for rendering a video with PyMOL
temp = NamedTemporaryFile(suffix=".pdb")
strucio.save_structure(temp.name, oscillating_structure)
# biotite_static_image = glycosylase_oscillation.gif
temp.close()
# Code source: Patrick Kunzmann
# License: BSD 3 clause
from tempfile import gettempdir
import biotite
import biotite.structure as struc
import biotite.structure.io as strucio
import biotite.database.rcsb as rcsb
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
file_name = rcsb.fetch("1aki", "mmtf", gettempdir())
array = strucio.load_structure(file_name)
# We only consider CA atoms
ca = array[array.atom_name == "CA"]
# 7 Angstrom adjacency threshold
threshold = 7
# Create cell list of the CA atom array
# for efficient measurement of adjacency
cell_list = struc.CellList(ca, cell_size=threshold)
adjacency_matrix = cell_list.create_adjacency_matrix(threshold)
figure = plt.figure()
ax = figure.add_subplot(111)
cmap = ListedColormap(["white", biotite.colors["dimgreen"]])
#ax.matshow(adjacency_matrix, cmap=cmap, origin="lower")
ax.pcolormesh(ca.res_id, ca.res_id, adjacency_matrix, cmap=cmap)
ax.set_aspect("equal")
ax.set_xlabel("Residue number")
import biotite
import biotite.structure as struc
import biotite.structure.io as strucio
import biotite.structure.io.xtc as xtc
import numpy as np
import matplotlib.pyplot as plt
# Put here the path of the downloaded files
templ_file_path = "../../download/lysozyme_md.pdb"
traj_file_path = "../../download/lysozyme_md.xtc"
# Gromacs does not set the element symbol in its PDB files,
# but Biotite guesses the element names from the atom names,
# emitting a warning
template = strucio.load_structure(templ_file_path)
# The structure still has water and ions, that are not needed for our
# calculations, we are only interested in the protein itself
# These are removed for the sake of computational speed using a boolean
# mask
protein_mask = struc.filter_amino_acids(template)
template = template[protein_mask]
# We could have loaded the trajectory also with
# 'strucio.load_structure()', but in this case we only want to load
# those coordinates that belong to the already selected atoms of the
# template structure.
# Hence, we use the 'XTCFile' class directly to load the trajectory
# This gives us the additional option that allows us to select the
# coordinates belonging to the amino acids.
xtc_file = xtc.XTCFile.read(traj_file_path, atom_i=np.where(protein_mask)[0])
trajectory = xtc_file.get_structure(template)
# Get simulation time for plotting purposes
chain_id="A", res_id=last_id, res_name=peptide[index_c].res_name,
atom_name="HXT", element="H"
)
peptide = peptide + struc.array([atom_oxt, atom_hxt])
peptide.bonds.add_bond(index_c, -2, struc.BondType.SINGLE) # C-OXT
peptide.bonds.add_bond(-2, -1, struc.BondType.SINGLE) # OXT-HXT
return peptide
sequence = seq.ProteinSequence("TITANITE")
atom_array = assemble_peptide(sequence)
out_file = NamedTemporaryFile(suffix=".mmtf", delete=False)
strucio.save_structure(out_file.name, atom_array)
# Visualization with PyMOL...
out_file.close()