From 58de3e91c9d1f0da2f27283d374095678d42216d Mon Sep 17 00:00:00 2001 From: Rodrigo V Honorato Date: Mon, 20 Dec 2021 16:07:42 +0100 Subject: [PATCH 1/2] implement rank column in `caprieval` also some code linting --- .../modules/analysis/caprieval/__init__.py | 61 +++++++++++-------- .../modules/analysis/caprieval/defaults.cfg | 5 +- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/src/haddock/modules/analysis/caprieval/__init__.py b/src/haddock/modules/analysis/caprieval/__init__.py index 1f5d7d72a..a74b9005d 100644 --- a/src/haddock/modules/analysis/caprieval/__init__.py +++ b/src/haddock/modules/analysis/caprieval/__init__.py @@ -234,11 +234,11 @@ def irmsd(self, cutoff=5.0): if P.shape != Q.shape: log.warning( - '[{RECIPE_PATH}] Cannot align these models,' - ' the number of atoms is in the interface' - ' is different.' + f"[{RECIPE_PATH}] Cannot align these models," + " the number of atoms is in the interface" + " is different." ) - i_rmsd = float('nan') + i_rmsd = float("nan") else: P = P - centroid(P) @@ -404,13 +404,17 @@ def fnat(self, cutoff=5.0): self.fnat_dic[model] = fnat return self.fnat_dic - def output(self, output_f, sortby_key, ascending): + def output(self, output_f, sortby_key, sort_ascending, rankby_key, + rank_ascending): """Output the CAPRI results to a .tsv file.""" output_l = [] for model in self.model_list: data = {} # keep always 'model' the first key data["model"] = Path(model.parent.name, model.name) + # create the empty rank here so that it will appear + # as the second column + data["rank"] = None data["score"] = self.score_dic[model] if model in self.irmsd_dic: data["irmsd"] = self.irmsd_dic[model] @@ -423,14 +427,21 @@ def output(self, output_f, sortby_key, ascending): # list of dictionaries output_l.append(data) + # Get the ranking of each model + rankkey_values = [(i, k[rankby_key]) for i, k in enumerate(output_l)] + rankkey_values.sort(key=lambda x: x[1], reverse=not rank_ascending) + for i, k in enumerate(rankkey_values, start=1): + idx, _ = k + output_l[idx]["rank"] = i + + # Sort the column key_values = [(i, k[sortby_key]) for i, k in enumerate(output_l)] - key_values.sort(key=lambda x: x[1], reverse=not ascending) + key_values.sort(key=lambda x: x[1], reverse=not sort_ascending) - max_model_space = max(len(str(_d['model'])) for _d in output_l) + 2 - hmodel = 'model'.center(max_model_space, ' ') - header = hmodel + ''.join( - _.rjust(10, " ") - for _ in list(output_l[0].keys())[1:] + max_model_space = max(len(str(_d["model"])) for _d in output_l) + 2 + hmodel = "model".center(max_model_space, " ") + header = hmodel + "".join( + _.rjust(10, " ") for _ in list(output_l[0].keys())[1:] ) with open(output_f, "w") as out_fh: @@ -440,12 +451,11 @@ def output(self, output_f, sortby_key, ascending): for value in output_l[idx].values(): if isinstance(value, Path): row_l.append(str(value).ljust(max_model_space, " ")) - # elif isinstance(value, (int, float)): + elif isinstance(value, int): + row_l.append(f"{value}".rjust(10, " ")) else: - # better to have the else: statment so errors are - # spotted. Only int and floats should go here row_l.append(f"{value:.3f}".rjust(10, " ")) - out_fh.write(''.join(row_l) + os.linesep) + out_fh.write("".join(row_l) + os.linesep) class HaddockModule(BaseHaddockModule): @@ -453,9 +463,8 @@ class HaddockModule(BaseHaddockModule): name = RECIPE_PATH.name - def __init__( - self, order, path, *ignore, init_params=DEFAULT_CONFIG, **everything - ): + def __init__(self, order, path, *ignore, init_params=DEFAULT_CONFIG, + **everything): super().__init__(order, path, init_params) @classmethod @@ -500,13 +509,13 @@ def _run(self): if self.params["fnat"]: self.log("Calculating FNAT") fnat_cutoff = self.params["fnat_cutoff"] - self.log(f' cutoff: {fnat_cutoff}A') + self.log(f" cutoff: {fnat_cutoff}A") capri.fnat(cutoff=fnat_cutoff) if self.params["irmsd"]: self.log("Calculating I-RMSD") irmsd_cutoff = self.params["irmsd_cutoff"] - self.log(f' cutoff: {irmsd_cutoff}A') + self.log(f" cutoff: {irmsd_cutoff}A") capri.irmsd(cutoff=irmsd_cutoff) if self.params["lrmsd"]: @@ -514,8 +523,8 @@ def _run(self): lrmsd_receptor_chain = self.params["receptor_chain"] lrmsd_ligand_chain = self.params["ligand_chain"] - self.log(f' Receptor chain: {lrmsd_receptor_chain}') - self.log(f' Ligand chain: {lrmsd_ligand_chain}') + self.log(f" Receptor chain: {lrmsd_receptor_chain}") + self.log(f" Ligand chain: {lrmsd_ligand_chain}") capri.lrmsd( receptor_chain=lrmsd_receptor_chain, ligand_chain=lrmsd_ligand_chain, @@ -526,8 +535,8 @@ def _run(self): ilrmsd_ligand_chain = self.params["ligand_chain"] ilrmsd_cutoff = self.params["irmsd_cutoff"] - self.log(f' Ligand chain: {ilrmsd_ligand_chain}') - self.log(f' cutoff: {ilrmsd_cutoff}A') + self.log(f" Ligand chain: {ilrmsd_ligand_chain}") + self.log(f" cutoff: {ilrmsd_cutoff}A") capri.ilrmsd( ligand_chain=ilrmsd_ligand_chain, @@ -539,7 +548,9 @@ def _run(self): capri.output( output_fname, sortby_key=self.params["sortby"], - ascending=self.params["ascending"], + sort_ascending=self.params["sort_ascending"], + rankby_key=self.params["rankby"], + rank_ascending=self.params["sort_ascending"], ) selected_models = models_to_calc diff --git a/src/haddock/modules/analysis/caprieval/defaults.cfg b/src/haddock/modules/analysis/caprieval/defaults.cfg index ab03f4eeb..628715b7a 100644 --- a/src/haddock/modules/analysis/caprieval/defaults.cfg +++ b/src/haddock/modules/analysis/caprieval/defaults.cfg @@ -15,4 +15,7 @@ atoms = ['C','N','CA','O'] # this CA will NOT be used for the alignment. ignore_missing = true sortby = 'score' -ascending = true +sort_ascending = true +# which field should be used to create the ranking +rankby = 'score' +rank_ascending = true \ No newline at end of file From d50363ded4a960d14c7bac9b7a2093fa3de0a9b8 Mon Sep 17 00:00:00 2001 From: Rodrigo V Honorato Date: Mon, 20 Dec 2021 16:08:42 +0100 Subject: [PATCH 2/2] typo --- src/haddock/modules/analysis/caprieval/defaults.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/haddock/modules/analysis/caprieval/defaults.cfg b/src/haddock/modules/analysis/caprieval/defaults.cfg index 628715b7a..e1f553048 100644 --- a/src/haddock/modules/analysis/caprieval/defaults.cfg +++ b/src/haddock/modules/analysis/caprieval/defaults.cfg @@ -18,4 +18,4 @@ sortby = 'score' sort_ascending = true # which field should be used to create the ranking rankby = 'score' -rank_ascending = true \ No newline at end of file +rank_ascending = true