-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepmasc_core.py
More file actions
180 lines (153 loc) · 6.85 KB
/
deepmasc_core.py
File metadata and controls
180 lines (153 loc) · 6.85 KB
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
"""
Core processing logic for DeepMASC map analysis.
Shared by both CLI (main.py) and Relion (gtf_relion4_run_select_class3d.py) modes.
"""
import os
import sys
import shutil
import tempfile
import subprocess
import select
from pathlib import Path
from loguru import logger
from map_utils import calc_map_ccc, calculate_fsc, is_map_empty
def process_map_files(
mrc_files,
output_path,
gpu_ids,
batch_size,
reso_input,
debug_mode=False,
class_ids=None,
):
"""
Core processing logic for DeepMASC map analysis.
Args:
mrc_files: List of MRC file paths to process
output_path: Output directory path
gpu_ids: GPU IDs to use for CryoREAD prediction
batch_size: Batch size for CryoREAD prediction
reso_input: Resolution parameter for CryoREAD (8.0 for Low, 2.0 for High)
debug_mode: If True, copy all intermediate files to output
class_ids: Optional list of class IDs corresponding to mrc_files.
If None, uses indices (0, 1, 2, ...)
Returns:
list: [(class_id, mrc_file, real_space_cc, cutoff_05), ...]
Sorted by real_space_cc in descending order
"""
# Create temp directory
temp_path = os.path.join(output_path, "temp")
os.makedirs(temp_path, exist_ok=True)
temp_dir = tempfile.TemporaryDirectory(dir=temp_path)
temp_dir_path = os.path.abspath(temp_dir.name)
# Get script path and CryoREAD path
CURR_SCRIPT_PATH = Path(__file__).absolute().parent
CRYOREAD_PATH = CURR_SCRIPT_PATH / "CryoREAD" / "main.py"
result_list = []
try:
for idx, mrc_file in enumerate(mrc_files):
# Get class_id for this map
class_id = class_ids[idx] if class_ids is not None else idx
# Check if map is empty
if is_map_empty(mrc_file):
logger.warning(f"Empty map found, skipping {mrc_file}")
result_list.append([class_id, mrc_file, 0.0, 0.0])
continue
# Prepare paths
map_name = Path(mrc_file).stem.split(".")[0].strip()
curr_out_dir = os.path.join(temp_dir_path, map_name)
os.makedirs(curr_out_dir, exist_ok=True)
seg_map_path = os.path.join(curr_out_dir, "input_segment.mrc")
prot_prob_path = os.path.join(curr_out_dir, "mask_protein.mrc")
# Run CryoREAD if outputs don't exist
if not os.path.exists(seg_map_path) or not os.path.exists(prot_prob_path):
logger.info(f"Running CryoREAD prediction on {mrc_file}")
cmd = [
sys.executable,
str(CRYOREAD_PATH),
"--mode=0",
f"-F={mrc_file}",
"--contour=0",
f"--gpu={gpu_ids}",
f"--batch_size={batch_size}",
"--prediction_only",
f"--resolution={reso_input}",
f"--output={curr_out_dir}",
]
logger.info(f"Running CryoREAD command: {' '.join(cmd)}")
# Run subprocess with real-time output
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
bufsize=1,
universal_newlines=True,
env=dict(os.environ, PYTHONUNBUFFERED="1"),
)
# Read and print output
outputs = [process.stdout, process.stderr]
while outputs:
readable, _, _ = select.select(outputs, [], [])
for output in readable:
line = output.readline()
if not line:
outputs.remove(output)
continue
if output == process.stdout:
logger.info(line.strip())
else:
logger.error(line.strip())
# Wait for process to complete
process.wait()
if process.returncode != 0:
raise RuntimeError(f"CryoREAD failed with exit code {process.returncode}")
# Calculate metrics
try:
real_space_cc = calc_map_ccc(seg_map_path, prot_prob_path)[0]
except Exception as e:
logger.warning(f"Failed to calculate real space CC: {e}")
real_space_cc = 0.0
try:
fsc_output_path = os.path.join(curr_out_dir, "fsc_data.txt")
x, fsc, cutoff_05, cutoff_0143 = calculate_fsc(seg_map_path, prot_prob_path, fsc_output_path)
except Exception as e:
logger.warning(f"Failed to calculate FSC: {e}")
cutoff_05 = 0.0
# Add to results
result_list.append([class_id, mrc_file, real_space_cc, cutoff_05])
# Copy files to final output directory
if debug_mode:
# Copy everything
final_out_path = os.path.join(output_path, map_name)
shutil.copytree(curr_out_dir, final_out_path)
else:
# Copy specific files including FSC plots
files_to_copy = [
("2nd_stage_detection/chain_base_prob.mrc", f"{map_name}_chain_base_prob.mrc"),
("2nd_stage_detection/chain_phosphate_prob.mrc", f"{map_name}_chain_phosphate_prob.mrc"),
("2nd_stage_detection/chain_sugar_prob.mrc", f"{map_name}_chain_sugar_prob.mrc"),
("2nd_stage_detection/chain_protein_prob.mrc", f"{map_name}_chain_protein_prob.mrc"),
("input_segment.mrc", f"{map_name}_segment.mrc"),
("mask_protein.mrc", f"{map_name}_mask_protein.mrc"),
("CCC_FSC05.txt", f"{map_name}_CCC_FSC05.txt"),
("fsc_data.txt", f"{map_name}_fsc_data.txt"),
("fsc_data_plot.png", f"{map_name}_fsc_data_plot.png"),
]
for src_rel, dst_name in files_to_copy:
src_path = os.path.join(curr_out_dir, src_rel)
dst_path = os.path.join(output_path, dst_name)
if os.path.exists(src_path):
shutil.copy(src_path, dst_path)
else:
logger.warning(f"File not found, skipping: {src_path}")
except Exception as e:
logger.error(f"Error during processing: {str(e)}")
logger.error("Stack trace:", exc_info=True)
raise
finally:
# Cleanup temp directory after all files are copied
temp_dir.cleanup()
logger.info("Temporary directory cleaned up")
# Sort by real_space_cc (descending)
result_list.sort(key=lambda x: x[2], reverse=True)
return result_list