Skip to content

Commit

Permalink
blacken
Browse files Browse the repository at this point in the history
  • Loading branch information
clami66 committed Mar 25, 2024
1 parent a084434 commit 40d392e
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 49 deletions.
133 changes: 84 additions & 49 deletions src/DockQ/DockQ.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,6 @@ def run_on_all_native_interfaces(
chain_map[chain_pair[1]],
)
results_dic[chain_pair] = info


return results_dic

Expand Down Expand Up @@ -669,28 +668,34 @@ def product_without_dupl(*args, repeat=1):
for prod in result:
yield tuple(prod)


def count_chain_combinations(chain_clusters):
counts = {}
for chain in chain_clusters:
chains = tuple(chain_clusters[chain])
if chains not in counts:
counts[chains]=0
counts[chains]+=1
number_of_combinations=np.prod([math.factorial(a) for a in counts.values()])
counts[chains] = 0
counts[chains] += 1
number_of_combinations = np.prod([math.factorial(a) for a in counts.values()])
return number_of_combinations
#combos=itertools.product(*[itertools.permutations(chains) for chains in set([tuple(ch) for ch in chain_clusters.values()])])

#return(number_of_combinations,counts)
#set(chain_clusters.values())
# combos=itertools.product(*[itertools.permutations(chains) for chains in set([tuple(ch) for ch in chain_clusters.values()])])

# return(number_of_combinations,counts)
# set(chain_clusters.values())


def get_all_chain_maps(chain_clusters,initial_mapping,reverse_map,model_chains_to_combo,native_chains_to_combo):
def get_all_chain_maps(
chain_clusters,
initial_mapping,
reverse_map,
model_chains_to_combo,
native_chains_to_combo,
):
all_mappings = product_without_dupl(
*[cluster for cluster in chain_clusters.values() if cluster]
)
for mapping in all_mappings:
chain_map = {key:value for key, value in initial_mapping.items()}
for mapping in all_mappings:
chain_map = {key: value for key, value in initial_mapping.items()}
if reverse_map:
chain_map.update(
{
Expand All @@ -699,11 +704,13 @@ def get_all_chain_maps(chain_clusters,initial_mapping,reverse_map,model_chains_t
}
)
else:
chain_map.update({
native_chain: mapping[i] for i, native_chain in enumerate(native_chains_to_combo)
})
yield(chain_map)

chain_map.update(
{
native_chain: mapping[i]
for i, native_chain in enumerate(native_chains_to_combo)
}
)
yield (chain_map)


# @profile
Expand All @@ -728,8 +735,12 @@ def main():
best_result = None
best_mapping = None

model_chains_to_combo = [mc for mc in model_chains if mc not in initial_mapping.values()]
native_chains_to_combo = [nc for nc in native_chains if nc not in initial_mapping.keys()]
model_chains_to_combo = [
mc for mc in model_chains if mc not in initial_mapping.values()
]
native_chains_to_combo = [
nc for nc in native_chains if nc not in initial_mapping.keys()
]

chain_clusters, reverse_map = group_chains(
model_structure,
Expand All @@ -739,51 +750,75 @@ def main():
args.allowed_mismatches,
)

num_chain_combinations=count_chain_combinations(chain_clusters)
chain_maps=get_all_chain_maps(chain_clusters,initial_mapping,reverse_map,model_chains_to_combo,native_chains_to_combo)

low_memory=num_chain_combinations>100
run_chain_map=partial(run_on_all_native_interfaces,
model_structure,
native_structure,
no_align=args.no_align,
capri_peptide=args.capri_peptide,
low_memory=low_memory) ##args: chain_map

if num_chain_combinations>1:
#chunk_size=max(1,num_chain_combinations // args.n_cpu)
#I suspect large chunk_size will result in large input arguments to the workers.
chunk_size=512
#for large num_chain_combinations it should be possible to divide the chain_maps in chunks
result_this_mappings=progress_map(run_chain_map,chain_maps, total=num_chain_combinations,n_cpu=args.n_cpu, chunk_size=chunk_size)
#get a fresh iterator
chain_maps=get_all_chain_maps(chain_clusters,initial_mapping,reverse_map,model_chains_to_combo,native_chains_to_combo)
for chain_map,result_this_mapping in zip(chain_maps,result_this_mappings):
num_chain_combinations = count_chain_combinations(chain_clusters)
chain_maps = get_all_chain_maps(
chain_clusters,
initial_mapping,
reverse_map,
model_chains_to_combo,
native_chains_to_combo,
)

low_memory = num_chain_combinations > 100
run_chain_map = partial(
run_on_all_native_interfaces,
model_structure,
native_structure,
no_align=args.no_align,
capri_peptide=args.capri_peptide,
low_memory=low_memory,
) ##args: chain_map

if num_chain_combinations > 1:
# chunk_size=max(1,num_chain_combinations // args.n_cpu)
# I suspect large chunk_size will result in large input arguments to the workers.
chunk_size = 512
# for large num_chain_combinations it should be possible to divide the chain_maps in chunks
result_this_mappings = progress_map(
run_chain_map,
chain_maps,
total=num_chain_combinations,
n_cpu=args.n_cpu,
chunk_size=chunk_size,
)
# get a fresh iterator
chain_maps = get_all_chain_maps(
chain_clusters,
initial_mapping,
reverse_map,
model_chains_to_combo,
native_chains_to_combo,
)
for chain_map, result_this_mapping in zip(chain_maps, result_this_mappings):
total_dockq = sum(
[result["DockQ_F1" if args.optDockQF1 else "DockQ"] for result in result_this_mapping.values()]
[
result["DockQ_F1" if args.optDockQF1 else "DockQ"]
for result in result_this_mapping.values()
]
)

if total_dockq > best_dockq:
best_dockq = total_dockq
best_result = result_this_mapping
best_mapping = chain_map

else: #skip multi-threading for single jobs (skip the bar basically)
# result_this_mappings=[run_chain_map(chain_map) for chain_map in chain_maps]
else: # skip multi-threading for single jobs (skip the bar basically)
# result_this_mappings=[run_chain_map(chain_map) for chain_map in chain_maps]
for chain_map in chain_maps:
result_this_mapping=run_chain_map(chain_map)
result_this_mapping = run_chain_map(chain_map)
total_dockq = sum(
[result["DockQ_F1" if args.optDockQF1 else "DockQ"] for result in result_this_mapping.values()]
[
result["DockQ_F1" if args.optDockQF1 else "DockQ"]
for result in result_this_mapping.values()
]
)
if total_dockq > best_dockq:
best_dockq = total_dockq
best_result = result_this_mapping
best_mapping = chain_map



if low_memory: #retrieve the full output by reruning the best chain mapping
best_result=run_on_all_native_interfaces(

if low_memory: # retrieve the full output by reruning the best chain mapping
best_result = run_on_all_native_interfaces(
model_structure,
native_structure,
best_mapping,
Expand Down
21 changes: 21 additions & 0 deletions testdata/6q2n_peptide.dockq
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
****************************************************************
* DockQ-CAPRI peptide *
* Do not trust any thing you read.... *
* OBS THE DEFINITION OF Fnat and iRMS are different for *
* peptides in CAPRI *
* *
* Ref: S. Basu and B. Wallner, DockQ: A quality measure for *
* protein-protein docking models *
* doi:10.1371/journal.pone.0161879 *
* For comments, please email: [email protected] *
****************************************************************
Model : examples/6qwn-assembly1.cif.gz
Native : examples/6qwn-assembly2.cif.gz
Total DockQ over 1 native interfaces: 0.872
Native chains: B, G
Model chains: A, F
DockQ_F1: 0.885
DockQ: 0.872
irms: 0.588
Lrms: 0.982
fnat: 0.762

0 comments on commit 40d392e

Please sign in to comment.