Created
April 1, 2021 11:05
-
-
Save matteoferla/b33585f3aeab58b8424581279e032550 to your computer and use it in GitHub Desktop.
Making a table of Pyrosetta scorefunctions
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 was run in a Jupyter notebook. | |
import pyrosetta | |
import os | |
import pandas as pd | |
from typing import * | |
pyrosetta.distributed.maybe_init(extra_options='-mute all') | |
# ------------------------------------------------------------------------------------ | |
# not all these method were used. | |
def get_weights(scorefxn:Union[str, pyrosetta.ScoreFunction]) -> Dict[str, float]: | |
""" | |
Gets weights for scorefxn | |
""" | |
if isinstance(scorefxn, str): | |
#scorefxn = pyrosetta.create_score_function(scorefxn) | |
# mod: | |
scorefxn = get_scorefxn(scorefxn) | |
get_weight = lambda scorefxn, score_type: scorefxn.get_weight(getattr(pyrosetta.rosetta.core.scoring.ScoreType, score_type.name)) | |
return {'name': scorefxn.get_name(), | |
**{score_type.name: get_weight(scorefxn, score_type) for score_type in scorefxn.get_nonzero_weighted_scoretypes()}, | |
**get_ref_values_badly(scorefxn, prefix=True)} | |
def get_ref_values_badly(scorefxn: pyrosetta.ScoreFunction, | |
prefix: Optional[bool]=True) -> Dict[str, float]: | |
""" | |
I could not find a direct way to get_method_weights. | |
Here this horrid way. | |
""" | |
# similarity order 'IVLFCMAGTSWYPHNDEQKR' | |
# name1 alphabetical order 'ACDEFGHIKLMNPQRSTVWY' | |
# name3 alphabetical order 'ARNDCQEGHILKMFPSTWYV' | |
pose = pyrosetta.pose_from_sequence('ARNDCQEGHILKMFPSTWYV') | |
scorefxn(pose) | |
aa_ref = {} | |
for res in range(1, pose.total_residue() + 1): | |
name = pose.residue(res).name3() | |
value = pose.energies().residue_total_energies(res)[pyrosetta.rosetta.core.scoring.ScoreType.ref] | |
if prefix: | |
aa_ref[f'ref_{name}'] = value | |
else: | |
aa_ref[name] = value | |
return aa_ref | |
def get_scorefxn(scorefxn_name:str) -> pyrosetta.ScoreFunction: | |
""" | |
Gets the scorefxn with appropriate corrections. | |
""" | |
corrections = {'beta_july15': False, | |
'beta_nov16': False, | |
'gen_potential': False, | |
'restore_talaris_behavior': False | |
} | |
if 'beta_july15' in scorefxn_name or 'beta_nov15' in scorefxn_name: | |
# beta_july15 is ref2015 | |
corrections['beta_july15'] = True | |
elif 'beta_nov16' in scorefxn_name: | |
corrections['beta_nov16'] = True | |
elif 'genpot' in scorefxn_name: | |
corrections['gen_potential'] = True | |
pyrosetta.rosetta.basic.options.set_boolean_option('corrections:beta_july15', True) | |
elif 'talaris' in scorefxn_name: #2013 and 2014 | |
corrections['restore_talaris_behavior'] = True | |
else: | |
pass | |
for corr, value in corrections.items(): | |
pyrosetta.rosetta.basic.options.set_boolean_option(f'corrections:{corr}', value) | |
return pyrosetta.create_score_function(scorefxn_name) | |
def get_possible_scorefxn() -> List[str]: | |
""" | |
Returns the scorefxn names | |
""" | |
folder = os.path.join(os.path.split(pyrosetta.__file__)[0], 'database', 'scoring', 'weights') | |
return sorted([fn.replace('.wts', '') for fn in os.listdir(folder) if '.wts' == os.path.splitext(fn)[1]]) | |
def get_scorefxn_block(scorefxn_name) -> str: | |
""" | |
Read the file given a scorefxn name | |
""" | |
filename = os.path.join(os.path.split(pyrosetta.__file__)[0], 'database', 'scoring', 'weights', f'{scorefxn_name}.wts') | |
with open(filename, 'r') as fh: | |
return fh.read() | |
def find_metion(word:str) -> List[str]: | |
""" | |
Find all the scorefxn names whose files contain the string word (case insensitive). | |
>>> find_metion('spades') | |
returns ``['hydrate_score12']`` | |
""" | |
mentionants = [] | |
scorefxn_names = get_possible_scorefxn() | |
for scorefxn_name in scorefxn_names: | |
block = get_scorefxn_block(scorefxn_name) | |
if word.lower() in block.lower(): | |
mentionants.append(scorefxn_name) | |
return mentionants | |
# ------- main --------------------------------------------------------------------- | |
scorefxn_names = ['ref2015', 'ref2015_cart', 'beta_nov16', 'beta_nov16_cart'] | |
# 'talaris2013' and 'talaris2014' ommitted due to clutter | |
# 'franklin2019', 'beta_genpot' will not work due to the ref method weights business. | |
terms = pd.DataFrame(map(get_weights, scorefxn_names)).fillna(0).transpose() | |
print(terms.to_markdown()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: