-
Notifications
You must be signed in to change notification settings - Fork 23
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
Moved hbp calculator into class SCI-1804 #73
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
# 2017-08-10: Created by Andy Maloney, the Cambridge Crystallographic Data Centre | ||
# 2020-08-21: made available by the Cambridge Crystallographic Data Centre | ||
# 2022-12-21: Updated by Joanna S. Stevens, the Cambridge Crystallographic Data Centre | ||
# 2025-01-08: Updated by Pablo Martinez-Bulit, the Cambridge Crystallographic Data Centre | ||
# | ||
|
||
""" | ||
|
@@ -53,6 +54,75 @@ | |
|
||
|
||
############################################################################### | ||
class PropensityCalc: | ||
"HBP Calculator" | ||
def __init__(self, crystal, work_directory, min_donor_coordination, min_acceptor_coordination, fg_count): | ||
self.crystal = crystal | ||
self.directory = work_directory | ||
self.min_donor_coordination = min_donor_coordination | ||
self.min_acceptor_coordination = min_acceptor_coordination | ||
self.fg_count = fg_count | ||
self.settings = self._hbp_settings() | ||
self.hbp = CrystalDescriptors.HBondPropensities() | ||
|
||
def _hbp_settings(self): | ||
settings = CrystalDescriptors.HBondPropensities.Settings() | ||
settings.hbond_criterion.require_hydrogens = True | ||
settings.hbond_criterion.path_length_range = (3, 999) | ||
settings.working_directory = self.directory | ||
|
||
return settings | ||
|
||
def calculate(self): | ||
self.hbp.settings = self.settings | ||
|
||
# Set up the target structure for the calculation | ||
self.hbp.set_target(self.crystal) | ||
print(self.hbp.functional_groups) | ||
|
||
# Generate Training Dataset | ||
self.hbp.match_fitting_data(count=self.fg_count) # set to >300, preferably 500 for better representation of functional groups | ||
|
||
self.hbp.analyse_fitting_data() | ||
|
||
for d in self.hbp.donors: | ||
print(d.identifier, d.npositive, d.nnegative) | ||
for a in self.hbp.acceptors: | ||
print(a.identifier, a.npositive, a.nnegative) | ||
|
||
# Perform regression | ||
model = self.hbp.perform_regression() | ||
|
||
print(model.equation) | ||
print('Area under ROC curve: {} -- {}'.format(round(model.area_under_roc_curve, 3), model.advice_comment)) | ||
|
||
propensities = self.hbp.calculate_propensities() | ||
|
||
intra_flag = True if len(self.hbp.intra_propensities) > 0 else False | ||
intra_count = len([p for p in propensities if not p.is_intermolecular and p.is_observed]) | ||
intra_obs = True if intra_count > 0 else False | ||
|
||
# Use default coordination cutoffs unless user overrides | ||
groups = self.hbp.generate_hbond_groupings(min_donor_prob=self.min_donor_coordination, | ||
min_acceptor_prob=self.min_acceptor_coordination) | ||
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. 🚫 [flake8] <128> reported by reviewdog 🐶 |
||
|
||
observed_group = self.hbp.target_hbond_grouping() | ||
|
||
return (self.hbp.functional_groups, | ||
self.hbp.fitting_data, | ||
self.hbp.donors, | ||
self.hbp.acceptors, | ||
model, | ||
propensities, | ||
intra_flag, | ||
groups, | ||
observed_group, | ||
self.min_donor_coordination, | ||
self.min_acceptor_coordination, | ||
intra_count, | ||
intra_obs) | ||
|
||
|
||
def make_diagram(mol, directory): | ||
# Generates a diagram from a given structure | ||
molecule_diagram_generator = DiagramGenerator() | ||
|
@@ -143,59 +213,6 @@ def launch_word_processor(output_file): | |
subprocess.Popen(['open', output_file]) | ||
|
||
|
||
def propensity_calc(crystal, directory, min_donor_coordination, min_acceptor_coordination, fg_count): | ||
# Perform a Hydrogen Bond Propensity calculation | ||
|
||
# Provide settings for the calculation | ||
settings = CrystalDescriptors.HBondPropensities.Settings() | ||
settings.working_directory = directory | ||
settings.hbond_criterion.require_hydrogens = True | ||
settings.hbond_criterion.path_length_range = (3, 999) | ||
|
||
# Set up the HBP calculator | ||
hbp = CrystalDescriptors.HBondPropensities(settings) | ||
|
||
# Set up the target structure for the calculation | ||
hbp.set_target(crystal) | ||
|
||
print(hbp.functional_groups) | ||
|
||
# Generate Training Dataset | ||
hbp.match_fitting_data(count=fg_count) # set to >300, preferably 500 for better representation of functional groups | ||
|
||
hbp.analyse_fitting_data() | ||
|
||
for d in hbp.donors: | ||
print(d.identifier, d.npositive, d.nnegative) | ||
for a in hbp.acceptors: | ||
print(a.identifier, a.npositive, a.nnegative) | ||
|
||
# Perform regression | ||
model = hbp.perform_regression() | ||
|
||
print(model.equation) | ||
print('Area under ROC curve: {} -- {}'.format(round(model.area_under_roc_curve, 3), model.advice_comment)) | ||
|
||
propensities = hbp.calculate_propensities() | ||
|
||
intra_flag = False | ||
intra_obs = False | ||
intra_count = 0 | ||
if len(hbp.intra_propensities) > 0: | ||
intra_flag = True | ||
intra_count = len([p for p in propensities if not p.is_intermolecular and p.is_observed]) | ||
if intra_count > 0: | ||
intra_obs = True | ||
|
||
# Use default coordination cutoffs unless user overrides | ||
groups = hbp.generate_hbond_groupings(min_donor_prob=min_donor_coordination, min_acceptor_prob=min_acceptor_coordination) | ||
|
||
observed_group = hbp.target_hbond_grouping() | ||
|
||
return hbp.functional_groups, hbp.fitting_data, hbp.donors, hbp.acceptors, model, propensities, \ | ||
intra_flag, groups, observed_group, min_donor_coordination, min_acceptor_coordination, intra_count, intra_obs | ||
|
||
|
||
def coordination_scores_calc(crystal, directory): | ||
# Calculate coordination scores for the target structure | ||
|
||
|
@@ -220,6 +237,8 @@ def format_scores(scores, das, d_type): | |
|
||
def normalize_molecule(molecule): | ||
# Normalise bond types for the input structure (important for cifs) | ||
if any(bond.bond_type == 'Unknown' for bond in molecule.bonds): | ||
print('Unknown type bonds in molecule will be auto assigned') | ||
molecule.assign_bond_types(which='unknown') | ||
molecule.standardise_aromatic_bonds() | ||
molecule.standardise_delocalised_bonds() | ||
|
@@ -356,9 +375,12 @@ def main(structure, directory, csdrefcode, min_donor_coordination, min_acceptor_ | |
# Set up a work directory for the HBP files | ||
work_directory = os.path.join(directory, str(structure).split('.')[0]) | ||
|
||
hbp_calculator = PropensityCalc(crystal, work_directory, min_donor_coordination, | ||
min_acceptor_coordination, fg_count) | ||
|
||
# Get all the necessary data from a HBP calculation | ||
functional_groups, fitting_data, donors, acceptors, model, propensities, intra_flag, \ | ||
groups, observed_groups, min_donor_coordination, min_acceptor_coordination, intra_count, intra_obs = propensity_calc(crystal, work_directory, min_donor_coordination, min_acceptor_coordination, fg_count) | ||
(functional_groups, fitting_data, donors, acceptors, model, propensities, intra_flag, groups, observed_groups, | ||
min_donor_coordination, min_acceptor_coordination, intra_count, intra_obs) = hbp_calculator.calculate() | ||
|
||
# Calculate the coordination scores separately | ||
coordination_scores = coordination_scores_calc(crystal, work_directory) | ||
|
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
Oops, something went wrong.
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.
🚫 [flake8] <128> reported by reviewdog 🐶
continuation line under-indented for visual indent