-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[3] Prepare support for multiple backmapping protocols #340
Open
jan-stevens
wants to merge
4
commits into
marrink-lab:master
Choose a base branch
from
jan-stevens:choose_backmapping_procedure
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import numpy as np | ||
import scipy.optimize | ||
import networkx as nx | ||
from .linalg_functions import rotate_xyz | ||
from .graph_utils import find_connecting_edges | ||
from .linalg_functions import norm_matrix | ||
|
||
def orient_by_bonds(meta_molecule, current_node, template, built_nodes): | ||
""" | ||
Given a `template` and a `node` of a `meta_molecule` at lower resolution | ||
find the bonded interactions connecting the higher resolution template | ||
to its neighbours and orient a template such that the atoms point torwards | ||
the neighbours. In case some nodes of meta_molecule have already been built | ||
at the lower resolution they can be provided as `built_nodes`. In case the | ||
lower resolution is already built the atoms will be oriented torward the lower | ||
resolution atom participating in the bonded interaction. | ||
|
||
Parameters: | ||
----------- | ||
meta_molecule: :class:`polyply.src.meta_molecule` | ||
current_node: | ||
node key of the node in meta_molecule to which template referes to | ||
template: dict[collections.abc.Hashable, np.ndarray] | ||
dict of positions referring to the lower resolution atoms of node | ||
built_nodes: list | ||
list of meta_molecule node keys of residues that are already built | ||
|
||
Returns: | ||
-------- | ||
dict | ||
the oriented template | ||
""" | ||
# 1. find neighbours at meta_mol level | ||
neighbours = nx.all_neighbors(meta_molecule, current_node) | ||
current_resid = meta_molecule.nodes[current_node]["resid"] | ||
|
||
# 2. find connecting atoms at low-res level | ||
edges = [] | ||
ref_nodes = [] | ||
for node in neighbours: | ||
resid = meta_molecule.nodes[node]["resid"] | ||
edge = find_connecting_edges(meta_molecule, | ||
meta_molecule.molecule, | ||
(node, current_node)) | ||
edges += edge | ||
ref_nodes.extend([node]*len(edge)) | ||
|
||
# 3. build coordinate system | ||
ref_coords = np.zeros((3, len(edges))) | ||
opt_coords = np.zeros((3, len(edges))) | ||
|
||
for ndx, edge in enumerate(edges): | ||
for atom in edge: | ||
resid = meta_molecule.molecule.nodes[atom]["resid"] | ||
if resid == current_resid: | ||
current_atom = atom | ||
else: | ||
ref_atom = atom | ||
ref_resid = resid | ||
|
||
# the reference residue has already been build so we take the lower | ||
# resolution coordinates as reference | ||
if ref_resid in built_nodes: | ||
atom_name = meta_molecule.molecule.nodes[current_atom]["atomname"] | ||
|
||
# record the coordinates of the atom that is rotated | ||
opt_coords[:, ndx] = template[atom_name] | ||
|
||
# given the reference atom that already exits translate it to the origin | ||
# of the rotation, this will be the reference point for rotation | ||
ref_coords[:, ndx] = meta_molecule.molecule.nodes[ref_atom]["position"] -\ | ||
meta_molecule.nodes[current_node]["position"] | ||
|
||
# the reference residue has not been build the CG center is taken as | ||
# reference | ||
else: | ||
atom_name = meta_molecule.molecule.nodes[current_atom]["atomname"] | ||
cg_node = ref_nodes[ndx] #find_atoms(meta_molecule, "resid", ref_resid)[0] | ||
|
||
# record the coordinates of the atom that is rotated | ||
opt_coords[:, ndx] = template[atom_name] | ||
|
||
# as the reference atom is not built take the cg node as reference point | ||
# for rotation; translate it to origin | ||
ref_coords[:, ndx] = meta_molecule.nodes[cg_node]["position"] -\ | ||
meta_molecule.nodes[current_node]["position"] | ||
|
||
|
||
# 4. optimize the distance between reference nodes and nodes to be placed | ||
# only using rotation of the complete template | ||
#@profile | ||
def target_function(angles): | ||
rotated = rotate_xyz(opt_coords, angles[0], angles[1], angles[2]) | ||
diff = rotated - ref_coords | ||
score = norm_matrix(diff) | ||
return score | ||
|
||
# choose random starting angles | ||
angles = np.random.uniform(low=0, high=2*np.pi, size=(3)) | ||
opt_results = scipy.optimize.minimize(target_function, angles, method='L-BFGS-B', | ||
options={'ftol':0.01, 'maxiter': 400}) | ||
|
||
# 5. write the template as array and rotate it corrsponding to the result above | ||
template_arr = np.zeros((3, len(template))) | ||
key_to_ndx = {} | ||
for ndx, key in enumerate(template.keys()): | ||
template_arr[:, ndx] = template[key] | ||
key_to_ndx[key] = ndx | ||
|
||
angles = opt_results['x'] | ||
template_rotated_arr = rotate_xyz(template_arr, angles[0], angles[1], angles[2]) | ||
|
||
# 6. write the template back as dictionary | ||
template_rotated = {} | ||
for key, ndx in key_to_ndx.items(): | ||
template_rotated[key] = template_rotated_arr[:, ndx] | ||
|
||
return template_rotated |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add docstring for the two variables