-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_LSTINFO.py
57 lines (48 loc) · 1.93 KB
/
process_LSTINFO.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
import csv
import sys
import os
def process_files(input_txt, input_tsv, output_tsv):
"""
Processes two input files and creates a filtered TSV file.
Args:
input_txt (str): Path to the input TXT file.
input_tsv (str): Path to the input TSV file.
output_tsv (str): Path to the output TSV file.
"""
def trim_identifier(identifier):
"""
Trims the identifier to retain only the part before the 3rd dot.
Args:
identifier (str): The full identifier string.
Returns:
str: The trimmed identifier.
"""
return ".".join(identifier.split(".")[:3])
# Extract trimmed "gembase_name" values from the 2nd row of the TXT file
with open(input_txt, "r") as txt_file:
lines = txt_file.readlines()
if len(lines) < 2:
raise ValueError("The TXT file must have at least two rows.")
first_row = lines[0].strip().split()
gembase_names = {trim_identifier(item) for item in first_row}
# Read the TSV file and filter rows
with open(input_tsv, "r") as tsv_file:
reader = csv.DictReader(tsv_file, delimiter="\t")
filtered_rows = [
row for row in reader if row["gembase_name"] in gembase_names
]
fieldnames = reader.fieldnames # Save header
# Write the filtered rows to a new TSV file
with open(output_tsv, "w", newline="") as out_file:
writer = csv.DictWriter(out_file, fieldnames=fieldnames, delimiter="\t")
writer.writeheader()
writer.writerows(filtered_rows)
print(f"Filtered file created: {output_tsv}")
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: %run process_LSTINFO.py <input_txt_file> <input_tsv_file> <output_tsv_file>")
else:
input_txt = sys.argv[1]
input_tsv = sys.argv[2]
output_tsv = sys.argv[3]
process_files(input_txt, input_tsv, output_tsv)