forked from collaborativebioinformatics/TRACE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTRACE.py
More file actions
executable file
·342 lines (300 loc) · 9.9 KB
/
TRACE.py
File metadata and controls
executable file
·342 lines (300 loc) · 9.9 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env python3
"""
TRACE.py - Transposon Repeat Annotation and Characterization Engine
Orchestrates the entire pipeline for identifying and annotating transposon repeats
from a Sniffles structural variant VCF file.
"""
import argparse
import subprocess
import os
import sys
import logging
from pathlib import Path
from datetime import datetime
import shutil
__author__ = "Michal Izydorczyk, Nicola Wong, John Adedeji, Julian Chiu, and Thomas X. Garcia"
def setup_logging(debug: bool, log_file: str) -> None:
"""Set up logging configuration."""
level = logging.DEBUG if debug else logging.INFO
logging.basicConfig(
level=level,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
handlers=[
logging.FileHandler(log_file),
logging.StreamHandler()
]
)
def check_dependencies() -> None:
"""Check if required external tools are available."""
required_tools = ['RepeatMasker', 'bedtools']
missing_tools = []
for tool in required_tools:
if shutil.which(tool) is None:
missing_tools.append(tool)
if missing_tools:
logging.error(f"Missing required tools: {', '.join(missing_tools)}")
logging.error("Please install these tools and ensure they are in your PATH")
sys.exit(1)
def validate_file_exists(file_path: str, description: str) -> None:
"""Validate that a file exists."""
if not Path(file_path).exists():
logging.error(f"{description} not found: {file_path}")
sys.exit(1)
def parse_arguments() -> argparse.Namespace:
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(
description='Run the TRACE pipeline for transposon repeat annotation.',
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
'input_vcf',
help='Input VCF file from Sniffles'
)
parser.add_argument(
'--keep-intermediates',
action='store_true',
help='Keep all intermediate files generated during the pipeline'
)
parser.add_argument(
'--remove-log',
action='store_true',
help='Remove the pipeline log file after completion (default: keep)'
)
parser.add_argument(
'--intersect',
required=True,
help='Path to the file to intersect with (e.g., hg38.fa.bed)'
)
parser.add_argument(
'--lib',
required=True,
help='Path to the RepeatMasker library (e.g., Dfam-RepeatMasker.lib)'
)
parser.add_argument(
'--threads',
type=int,
default=1,
help='Number of threads to use for RepeatMasker'
)
parser.add_argument(
'--log-file',
help='Path to log file (default: TRACE_pipeline_TIMESTAMP.log)'
)
parser.add_argument(
'--debug',
action='store_true',
help='Enable debug logging'
)
return parser.parse_args()
def run_command(command: list, log_file: str):
"""Run a command and log its output."""
logging.info(f"Running command: {' '.join(command)}")
with open(log_file, 'a') as f:
f.write(f"Running command: {' '.join(command)}\n")
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True
)
for line in process.stdout:
f.write(line)
sys.stdout.write(line)
process.wait()
if process.returncode != 0:
logging.error(f"Command failed with exit code {process.returncode}")
logging.error(f"Check log file for details: {log_file}")
sys.exit(1)
def main():
"""Main function to orchestrate the TRACE pipeline."""
args = parse_arguments()
# Set up log file with timestamp if not specified
if args.log_file:
log_file = args.log_file
else:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
log_file = f"TRACE_pipeline_{timestamp}.log"
setup_logging(args.debug, log_file)
# Check dependencies
logging.info("Checking for required dependencies...")
check_dependencies()
# Validate input files
logging.info("Validating input files...")
validate_file_exists(args.input_vcf, "Input VCF file")
validate_file_exists(args.intersect, "Intersect file")
validate_file_exists(args.lib, "RepeatMasker library")
# Get the directory of the TRACE.py script
script_dir = Path(__file__).parent.resolve()
# Create output directory for RepeatMasker
repeatmasker_dir = Path("repeatmasker_output")
repeatmasker_dir.mkdir(exist_ok=True)
# --- INS Pipeline ---
logging.info("--- Starting INS Pipeline ---")
# 1. vcf2fasta.py
ins_fasta = Path(args.input_vcf).stem + "_INS.fasta"
cmd = [
sys.executable,
str(script_dir / "vcf2fasta.py"),
args.input_vcf,
"-o",
ins_fasta
]
run_command(cmd, log_file)
# Validate output
validate_file_exists(ins_fasta, "INS FASTA output")
# 2. RepeatMasker
cmd = [
"RepeatMasker",
"-no_is",
"-lib",
args.lib,
"-pa",
str(args.threads),
"-dir",
str(repeatmasker_dir),
ins_fasta
]
run_command(cmd, log_file)
# 3. out2bed.py
rm_out = repeatmasker_dir / f"{ins_fasta}.out"
ins_bed = repeatmasker_dir / f"{ins_fasta}.bed"
# Validate RepeatMasker output
validate_file_exists(str(rm_out), "RepeatMasker output")
cmd = [
sys.executable,
str(script_dir / "out2bed.py"),
str(rm_out),
str(ins_bed)
]
run_command(cmd, log_file)
# Validate output
validate_file_exists(str(ins_bed), "out2bed output")
# 4. squash_repeat_masker.py
ins_squashed = str(ins_bed)[:-4] + "_squashed.tsv"
cmd = [
sys.executable,
str(script_dir / "squash_repeat_masker.py"),
str(ins_bed),
"-o",
ins_squashed
]
run_command(cmd, log_file)
# Validate output
validate_file_exists(ins_squashed, "squash_repeat_masker output")
# --- DEL, DUP, BND Pipeline ---
logging.info("--- Starting DEL, DUP, BND Pipeline ---")
# 5. vcf2bed.py
sv_bed = Path(args.input_vcf).stem + ".bed"
cmd = [
sys.executable,
str(script_dir / "vcf2bed.py"),
args.input_vcf,
"-o",
sv_bed
]
run_command(cmd, log_file)
# Validate output
validate_file_exists(sv_bed, "vcf2bed output")
# 6. bed_processor.py
processed_bed = Path(sv_bed).stem + "_processed.bed"
cmd = [
sys.executable,
str(script_dir / "bed_processor.py"),
sv_bed,
"-o",
processed_bed
]
run_command(cmd, log_file)
# Validate output
validate_file_exists(processed_bed, "bed_processor output")
# 7. bedtools intersect
intersected_bed = Path(processed_bed).stem + "_intersected.bed"
cmd = [
"bedtools",
"intersect",
"-wa",
"-wb",
"-header",
"-b",
args.intersect,
"-a",
processed_bed
]
# Redirect output to file
logging.info(f"Running command: {' '.join(cmd)} > {intersected_bed}")
with open(intersected_bed, 'w') as f_out, open(log_file, 'a') as f_log:
f_log.write(f"Running command: {' '.join(cmd)} > {intersected_bed}\n")
process = subprocess.Popen(cmd, stdout=f_out, stderr=subprocess.PIPE, text=True)
_, stderr = process.communicate()
if stderr:
f_log.write(stderr)
if process.returncode != 0:
sys.stderr.write(stderr)
if process.returncode != 0:
logging.error(f"bedtools intersect failed with exit code {process.returncode}")
sys.exit(1)
# Validate output
validate_file_exists(intersected_bed, "bedtools intersect output")
# 8. squash_intersect.py
intersect_squashed = Path(intersected_bed).stem + "_squashed.tsv"
cmd = [
sys.executable,
str(script_dir / "squash_intersect.py"),
intersected_bed,
"-o",
str(intersect_squashed)
]
run_command(cmd, log_file)
# Validate output
validate_file_exists(str(intersect_squashed), "squash_intersect output")
# --- Annotation ---
logging.info("--- Annotating VCF ---")
# 9. annotate_vcf.py
annotated_vcf = Path(args.input_vcf).stem + "_annotated.vcf"
cmd = [
sys.executable,
str(script_dir / "annotate_vcf.py"),
args.input_vcf,
"--data",
ins_squashed,
str(intersect_squashed),
"-o",
annotated_vcf
]
run_command(cmd, log_file)
# Validate final output
validate_file_exists(annotated_vcf, "Final annotated VCF")
# --- Cleanup ---
if not args.keep_intermediates:
logging.info("--- Cleaning up intermediate files ---")
files_to_remove = [
ins_fasta,
str(rm_out),
str(ins_bed),
ins_squashed,
sv_bed,
processed_bed,
intersected_bed,
str(intersect_squashed)
]
# Remove log file only if explicitly requested
if args.remove_log:
files_to_remove.append(log_file)
for f in files_to_remove:
try:
Path(f).unlink()
logging.info(f"Removed {f}")
except OSError as e:
logging.warning(f"Could not remove {f}: {e}")
# Remove the repeatmasker directory
try:
shutil.rmtree(repeatmasker_dir)
logging.info(f"Removed directory {repeatmasker_dir}")
except OSError as e:
logging.warning(f"Could not remove directory {repeatmasker_dir}: {e}")
logging.info(f"--- Pipeline complete! Final output: {annotated_vcf} ---")
if not args.remove_log:
logging.info(f"Log file saved as: {log_file}")
if __name__ == "__main__":
main()