-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathfasta_to_csv.py
More file actions
29 lines (23 loc) · 873 Bytes
/
fasta_to_csv.py
File metadata and controls
29 lines (23 loc) · 873 Bytes
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
from tqdm import tqdm
from argparse import ArgumentParser
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("-i", "--input", help="path to the input fasta file")
parser.add_argument("-o", "--output", help="path to the output csv file")
args = parser.parse_args()
reader = open(args.input, "r")
writer = open(args.output, "w")
writer.write("name,sequence\n")
name, seq = str(), str()
for row in tqdm(reader, unit="rows", unit_scale=True):
if ">" in row:
if len(name) > 0 and len(seq) > 0:
writer.write(f"{name},{seq}\n")
name, seq = str(), str()
name = row.strip().replace(",", "|")
else:
seq += row.strip()
if len(name) > 0 and len(seq) > 0:
writer.write(f"{name},{seq}\n")
reader.close()
writer.close()