File size: 7,714 Bytes
ef28eb2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
"Runs model comparison"
from benchmark import visualization
from benchmark import get_cath
from pathlib import Path
import click
import os
import sys
def check_sets(dataset: Path, training_set: Path):
"""Compares training and testing sets, warns if they overlap.
Parameters
----------
dataset:Path
Path to .txt file with the dataset.
training_set:Path
Path to a file with the training set, can be PISCES, pdb code or pdb+chain.
"""
with open(training_set) as file:
training_chains = [x.split()[0][:4].upper() for x in file.readlines()]
# check for pisces
if len(training_chains[0]) != 5:
training_chains = training_chains[1:]
# check only pdb codes, not chains
with open(dataset) as file:
testing_chains = [x.split()[0][:4].upper() for x in file.readlines()]
repeated_chains = set(testing_chains).intersection(set(training_chains))
if len(repeated_chains) > 0:
print(f"{len(repeated_chains)} chains are in both sets:")
for chain in repeated_chains:
print(chain)
print("\n")
print("Suggested benchmarking set:")
for chain in testing_chains:
if chain not in repeated_chains:
print(chain)
if click.confirm(
"Model evaluation might not be valid. Do you want to continue?"
):
click.echo("Continuing!")
else:
exit()
else:
print("There is no overlap between sets.")
@click.command("compare")
@click.option(
"--dataset",
help="Path to .txt file with dataset list (PDB+chain, e.g., 1a2bA).",
type=click.Path(exists=True),
required=True,
)
@click.option(
"--training_set",
default=False,
help="Path to .txt file with the training set.",
)
@click.option(
"--path_to_pdb",
help="Path to the directory with PDB files.",
type=click.Path(exists=True),
required=True,
)
@click.option(
"--path_to_models",
help="Path to the directory with .csv prediction files.",
type=click.Path(exists=True),
required=True,
)
@click.option(
"--include",
help="Path to .txt file with a list of models to be included in comparison. If not provided, 8 models with the best accuracy are compared.",
type=click.Path(exists=True),
)
@click.option(
"--torsions",
is_flag=True,
help="Produces predicted and true Ramachandran plots for each model.",
)
def compare_models(
dataset: str,
path_to_pdb: str,
path_to_models: str,
training_set: str,
include: str = False,
torsions: bool = False,
) -> None:
"""Generates model summary and comparison plots.
\f
Parameters
---------
dataset: str
Path to .txt file with dataset list (PDB+chain, e.g., 1a2bA).
path_to_pdb: str
Path to the directory with PDB files.
path_to_models: str.
Path to the directory with .csv prediction files.
include: str = False
Path to .txt file with a list of models to be included in comparison. If not provided, 8 models with the best accuracy are compared.
torsions: bool = False
Produces predicted and true Ramachandran plots for each model.
training_set:Path
Path to a file with the training set, can be PISCES, pdb code or pdb+chain.
"""
# check training and testing sets
if training_set:
check_sets(Path(dataset), Path(training_set))
else:
# Warn and ask for confirmation to continue.
if click.confirm(
"Cannot compare training and testing sets. YOUR COMPARISON MIGHT NOT BE STATISTICALLY MEANINGFUL. Do you want to continue?"
):
click.echo("Continuing!")
else:
exit
# get model labels to include in comparison
if include:
with open(include) as file:
models_to_include = [x.strip("\n") for x in file.readlines()]
df = get_cath.read_data(f"{Path(os.path.dirname(sys.argv[0]))/'cath-domain-description-file.txt'}")
filtered_df = get_cath.filter_with_user_list(df, dataset)
df_with_sequence = get_cath.append_sequence(
filtered_df, Path(path_to_pdb)
)
accuracy = []
# load predictions
list_of_models = {}
for name in os.listdir(path_to_models):
if name.split(".")[-1] == "csv":
path_to_file=Path(path_to_models)/name
with open(path_to_file.with_suffix('.txt')) as datasetmap:
model= get_cath.load_prediction_matrix(df_with_sequence, path_to_file.with_suffix('.txt'), path_to_file)
ignore_uncommon=eval(datasetmap.readline().split()[1])
pdbs=datasetmap.readline().split()
if len(pdbs)>1:
#visualize accuracy and entropy on pdb files
for protein in pdbs[1:]:
visualization.show_accuracy(
df_with_sequence,
protein[:4],
model,
Path(path_to_models) / f"{name.strip('.csv')}_{protein}.pdb",
Path(path_to_pdb),
ignore_uncommon,
)
list_of_models[name]=(model,ignore_uncommon)
for model in list_of_models:
# make model summary
visualization.make_model_summary(
df_with_sequence,
list_of_models[model][0],
str(Path(path_to_models) / model),
Path(path_to_pdb),
list_of_models[model][1],
)
# get overall accuracy
accuracy.append(
[
get_cath.score(
df_with_sequence,
list_of_models[model][0],
ignore_uncommon=list_of_models[model][1],
)[0][0],
model,
]
)
# make Ramachandran plots
if torsions:
sequence, prediction, _, angle = get_cath.format_angle_sequence(
df_with_sequence,
list_of_models[model][0],
Path(path_to_pdb),
ignore_uncommon=list_of_models[model][1],
)
visualization.ramachandran_plot(
sequence,
list(get_cath.most_likely_sequence(prediction)),
angle,
str(Path(path_to_models) / model),
)
accuracy = sorted(accuracy)
# pick 8 best models
filtered_models = [list_of_models[model[1]][0] for model in accuracy[-8:]]
ignore_list= [list_of_models[model[1]][1] for model in accuracy[-8:]]
filtered_labels = [model[1] for model in accuracy[-8:]]
# include specified models
if include:
if len(models_to_include) <= 8:
for index, model_name in enumerate(models_to_include):
if model_name not in filtered_labels:
filtered_models[index] = list_of_models[model_name][0]
ignore_list[index]=list_of_models[model_name][1]
filtered_labels[index] = model_name
else:
raise ValueError(
"Too many models are give to plot, select no more than 8 models."
)
visualization.compare_model_accuracy(
df_with_sequence,
filtered_models,
filtered_labels,
Path(path_to_models),
ignore_list,
)
if __name__=="__main__":
compare_models()
|