-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor of universe creation and applying transformations #93
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
base: rmsd_refactor_analysisbase
Are you sure you want to change the base?
Changes from 10 commits
3e7f6d8
5e65b34
8a8b20a
8c08fa6
b7ba29f
113dc6d
a73f096
2f7a441
e4e778f
edca537
f75a38a
48fcdce
ede6a4a
2a82654
c661984
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| from typing import Optional | ||
|
|
||
| import MDAnalysis as mda | ||
| from MDAnalysis.transformations import unwrap | ||
|
|
||
| from ..transformations import Aligner, ClosestImageShift, NoJump | ||
|
|
||
|
|
||
| def apply_transformations( | ||
|
hannahbaumann marked this conversation as resolved.
Outdated
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very small nit: is
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, renamed this and also renamed the other functions (for protein and ligand) since those are not actually applying the transformations, but just getting them. |
||
| u: mda.Universe, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I often make this mistake, so I'm nit-picking but
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
| protein: Optional[mda.AtomGroup] = None, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given we're post python 3.9, it might be good to go for mda.AtomGroup | None instead of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
| ligand: Optional[mda.AtomGroup] = None, | ||
| ): | ||
| """ | ||
| Apply a collection of transformations to a Universe. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since MDAnalysis transformations are meant to be quite flexible and additive, it might be good to be more descriptive about this sentence. Maybe be even just "collection of transformations usually required for X, Y, Z analyses" would be fine.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added that they are typically used in RMSD analyses and other structural analyses. |
||
|
|
||
| Parameters | ||
| ---------- | ||
| u: Universe | ||
| The Universe the transformations are applied to | ||
| protein: Optional[AtomGroup] | ||
| The AtomGroup of the protein | ||
| ligand: Optional[AtomGroup] | ||
| The AtomGroup of the ligand | ||
|
|
||
| Notes | ||
| ----- | ||
| Depending on whether a protein is present, a sequence of trajectory | ||
| transformations is applied: | ||
|
|
||
| If a protein is present: | ||
|
|
||
| - Unwraps protein and ligand atom to be made whole | ||
| - Shifts protein chains and the ligand to the image closest to the first | ||
| protein chain (:class:`ClosestImageShift`) | ||
| - Aligns the entire system to minimise the protein RMSD (:class:`Aligner`) | ||
|
|
||
| If only a ligand is present: | ||
|
|
||
| - Prevents the ligand from jumping between periodic images | ||
| - Aligns the ligand to minimize its RMSD | ||
| """ | ||
| has_protein = protein is not None and protein.n_atoms > 0 | ||
| has_ligand = ligand is not None and ligand.n_atoms > 0 | ||
|
|
||
| if has_protein: | ||
| lig = ligand if has_ligand else None | ||
| transforms = _apply_transformations_complex(protein, lig) | ||
| elif has_ligand: | ||
| transforms = _apply_transformations_ligand_only(ligand) | ||
| else: | ||
| return | ||
|
|
||
| u.trajectory.add_transformations(*transforms) | ||
|
|
||
|
|
||
| def _apply_transformations_complex(protein, ligand=None): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do add type hints everywhere.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
| """ | ||
| Build transformations for systems containing a protein | ||
| and optionally a ligand. | ||
| """ | ||
| transforms = [] | ||
| # 1. Make molecules whole (protein + optional ligand) | ||
| group = protein if ligand is None else protein + ligand | ||
| transforms.append(unwrap(group)) | ||
|
|
||
| # 2. Closest image shift for protein chains + ligand (if present) | ||
| chains = [seg.atoms for seg in protein.segments] | ||
| shift_targets = chains[1:] | ||
| if ligand is not None: | ||
| shift_targets.append(ligand) | ||
| transforms.append(ClosestImageShift(chains[0], shift_targets)) | ||
|
|
||
| # 3. Align on protein backbone/atoms | ||
| transforms.append(Aligner(protein)) | ||
|
|
||
| return transforms | ||
|
|
||
|
|
||
| def _apply_transformations_ligand_only(ligand): | ||
| """ | ||
| Build transformations for ligand-only systems. | ||
| - make the ligand not jump periodic images between frames | ||
| - align the ligand to minimize its RMSD | ||
| """ | ||
| return [ | ||
| NoJump(ligand), | ||
| Aligner(ligand), | ||
| ] | ||
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.
I think this should go into
utils. The reader file should really concentrate on reader things rather than universe creation.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.
I moved it into it's own
universe_utils.pyfile.