Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Take a structure and rotate and translate a copy of it, so that they
are not superimposed anymore.
Then superimpose these structure onto each other and expect an
almost perfect match.
"""
fixed = strucio.load_structure(path, model=1)
mobile = fixed.copy()
mobile = struc.rotate(mobile, (1,2,3))
mobile = struc.translate(mobile, (1,2,3))
fitted, transformation = struc.superimpose(
fixed, mobile
)
assert struc.rmsd(fixed, fitted) == pytest.approx(0, abs=6e-4)
fitted = struc.superimpose_apply(mobile, transformation)
assert struc.rmsd(fixed, fitted) == pytest.approx(0, abs=6e-4)
def test_base_pairs_reverse_no_hydrogen(nuc_sample_array, basepairs):
"""
Remove the hydrogens from the sample structure. Then reverse the
order of residues in the atom_array and then test the function
base_pairs.
"""
nuc_sample_array = nuc_sample_array[nuc_sample_array.element != "H"]
# Reverse sequence of residues in nuc_sample_array
reversed_nuc_sample_array = struc.AtomArray(0)
for residue in reversed_iterator(struc.residue_iter(nuc_sample_array)):
reversed_nuc_sample_array = reversed_nuc_sample_array + residue
computed_basepairs = base_pairs(reversed_nuc_sample_array)
check_output(
reversed_nuc_sample_array[computed_basepairs].res_id, basepairs
)
def test_apply_residue_wise(array):
data = struc.apply_residue_wise(array, np.ones(len(array)), np.sum)
assert data.tolist() == [len(array[array.res_id == i])
for i in range(1, 21)]
"""
if ndim > len(input_atoms.shape):
# Cannot run tests if translation vector has more dimensions
# as input coordinates/atoms
return
np.random.seed(random_seed)
vectors = np.random.rand(*struc.coord(input_atoms).shape[-ndim:])
vectors *= 10
neg_vectors = -vectors
if as_list:
vectors = vectors.tolist()
neg_vectors = neg_vectors.tolist()
translated = struc.translate(input_atoms, vectors)
restored = struc.translate(translated, neg_vectors)
assert type(restored) == type(input_atoms)
assert struc.coord(restored).shape == struc.coord(input_atoms).shape
assert np.allclose(
struc.coord(restored), struc.coord(input_atoms), atol=1e-5
)
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_remove_pbc_unsegmented():
"""
`remove_pbc()` should not alter unsegmented structures,
when the structure is entirely in the box.
Exclude the solvent, due to high distances between each atom.
"""
ref_array = load_structure(join(data_dir("structure"), "3o5r.mmtf"))
# Center structure in box
centroid = struc.centroid(ref_array)
box_center = np.diag(ref_array.box) / 2
ref_array = struc.translate(ref_array, box_center-centroid)
# Remove solvent
ref_array = ref_array[~struc.filter_solvent(ref_array)]
array = struc.remove_pbc(ref_array)
assert ref_array.equal_annotation_categories(array)
assert np.allclose(ref_array.coord, array.coord)
def test_nucleotide_filter():
nuc_sample_array = strucio.load_structure(
join(data_dir("structure"), "5ugo.pdb")
)
assert len(nuc_sample_array[struc.filter_nucleotides(nuc_sample_array)
]) == 651
def test_rdf_periodic():
""" Test if the periodic argument gives the correct results"""
test_file = TEST_FILE
stack = load_structure(test_file)
# calculate oxygen RDF for water
oxygen = stack[:, stack.atom_name == 'OW']
interval = np.array([0, 10])
n_bins = 100
bins, g_r = rdf(oxygen[:, 0].coord, oxygen[:, 1:], interval=interval,
bins=n_bins, periodic=True)
# Compare with MDTraj
import mdtraj
traj = mdtraj.load(TEST_FILE)
ow = [a.index for a in traj.topology.atoms if a.name == 'O']
pairs = itertools.product([ow[0]], ow[1:])
mdt_bins, mdt_g_r = mdtraj.compute_rdf(traj, list(pairs),
r_range=interval/10, n_bins=n_bins,
periodic=True)
def test_rdf_atom_argument():
""" Test if the first argument allows to use AtomArrayStack """
stack = load_structure(TEST_FILE)
# calculate oxygen RDF for water with and without a selection
oxygen = stack[:, stack.atom_name == 'OW']
interval = np.array([0, 10])
n_bins = 100
bins, g_r = rdf(oxygen[:, 0], stack, interval=interval,
bins=n_bins, periodic=False)
atom_bins, atoms_g_r = rdf(oxygen[:, 0].coord, stack, interval=interval,
bins=n_bins, periodic=False)
assert np.allclose(g_r, atoms_g_r)
fasta.set_sequences(file2, seq_dict)
seq_dict2 = fasta.get_sequences(file2)
assert seq_dict == seq_dict2
file3 = fasta.FastaFile()
fasta.set_sequence(file3, seq.NucleotideSequence("AACCTTGG"))
assert file3["sequence"] == "AACCTTGG"
path = os.path.join(data_dir("sequence"), "prot.fasta")
file4 = fasta.FastaFile.read(path)
assert seq.ProteinSequence("YAHGFRTGS") == fasta.get_sequence(file4)
path = os.path.join(data_dir("sequence"), "invalid.fasta")
file5 = fasta.FastaFile.read(path)
with pytest.raises(ValueError):
seq.NucleotideSequence(fasta.get_sequence(file5))