-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_xf_tlt_files_to_warp.py
133 lines (105 loc) · 4.64 KB
/
copy_xf_tlt_files_to_warp.py
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
# Script to copy xf and tlt files from existing IMOD alignment to the warp_tiltseries/tiltstack
# Read the align.com file for excluded view and filter that out from the xf and tlt before copying
# Usage:
# copy_xf_tlt_files_to_warp.py align.com xf_file tlt_file output_dir
# Written by ChatGPT, edited by HB and Avrin Ghanaeian
import os
import shutil
import argparse
def parse_exclude_list(exclude_string):
"""Parse the ExcludeList string into a list of integers."""
result = []
parts = exclude_string.split(',')
for part in parts:
if '-' in part:
start, end = map(int, part.split('-'))
result.extend(range(start, end + 1))
else:
result.append(int(part))
return result
def get_exclude_list(file_path):
"""Read the file and return the ExcludeList as a list of integers."""
try:
with open(file_path, 'r') as file:
lines = file.readlines()
for line in lines:
if line.strip().startswith("ExcludeList"):
exclude_numbers = line.strip().split(maxsplit=1)[1]
return parse_exclude_list(exclude_numbers)
return []
except FileNotFoundError:
print(f"Error: The file at {file_path} does not exist.")
return []
except Exception as e:
print(f"An error occurred: {e}")
return []
def filter_and_invert_tlt_file(input_path, output_path, exclude_indices):
"""Filter a .tlt file to exclude specific 1-based lines and invert the sign of remaining values."""
try:
with open(input_path, 'r') as infile:
lines = infile.readlines()
filtered_lines = []
excluded_lines = []
for idx, line in enumerate(lines, start=1):
if idx in exclude_indices:
excluded_lines.append(idx)
else:
try:
value = float(line.strip())
inverted_value = -value
filtered_lines.append(f"{inverted_value}\n")
except ValueError:
print(f"Warning: Non-numeric value on line {idx}: {line.strip()}")
with open(output_path, 'w') as outfile:
outfile.writelines(filtered_lines)
print(f"Filtered and inverted .tlt file written to {output_path}")
print(f"Excluded lines: {excluded_lines}")
except FileNotFoundError:
print(f"Error: The file at {input_path} does not exist.")
except Exception as e:
print(f"An error occurred: {e}")
def filter_xf_file(input_path, output_path, exclude_indices):
"""Filter a text file (e.g., .xf file) to exclude specific 1-based lines."""
try:
with open(input_path, 'r') as infile:
lines = infile.readlines()
if not lines:
print(f"Warning: The input file {input_path} is empty.")
return
filtered_lines = []
excluded_lines = []
for idx, line in enumerate(lines, start=1):
if idx in exclude_indices:
excluded_lines.append(idx)
else:
# Ensure the line content is retained correctly
filtered_lines.append(line.strip() + "\n")
if not filtered_lines:
print(f"Warning: No valid lines remain in {input_path} after filtering.")
with open(output_path, 'w') as outfile:
outfile.writelines(filtered_lines)
print(f"Filtered text file written to {output_path}")
print(f"Excluded lines: {excluded_lines}")
except FileNotFoundError:
print(f"Error: The file at {input_path} does not exist.")
except Exception as e:
print(f"An error occurred: {e}")
def main():
parser = argparse.ArgumentParser(description="Filter and copy .xf and .tlt files.")
parser.add_argument("align_com_file", help="Path to the align.com file.")
parser.add_argument("input_xf_file", help="Path to the input .xf file.")
parser.add_argument("input_tlt_file", help="Path to the input .tlt file.")
parser.add_argument("output_dir", help="Path to the output file")
args = parser.parse_args()
align_com_file = args.align_com_file
input_xf_file = args.input_xf_file
input_tlt_file = args.input_tlt_file
output_directory = args.output_dir
# Define output paths
output_xf_file = os.path.join(output_directory, os.path.basename(input_xf_file))
output_tlt_file = os.path.join(output_directory, os.path.basename(input_tlt_file))
exclude_list = get_exclude_list(align_com_file)
filter_xf_file(input_xf_file, output_xf_file, exclude_list)
filter_and_invert_tlt_file(input_tlt_file, output_tlt_file, exclude_list)
if __name__ == "__main__":
main()