annotate fasta_affixer.py @ 30:cab41d23e2a3 draft

planemo upload commit 20bdf879b52796d3fb251a20807191ff02084d3c-dirty
author petr-novak
date Thu, 03 Aug 2023 08:40:38 +0000
parents a4cd8608ef6b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
1 #!/usr/bin/env python3
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
2 ''' fasta affixer - adding prefixes and suffixes to fasta sequence names'''
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
3 import argparse
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
4
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
5 parser = argparse.ArgumentParser()
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
6 parser.add_argument("-f", "--fasta", type=str, help="fasta file")
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
7 parser.add_argument("-o", "--output", type=str, help="output fasta file")
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
8 parser.add_argument(
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
9 "-p", "--prefix",
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
10 type=str, help="prefix to be added to names")
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
11 parser.add_argument(
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
12 "-s", "--suffix",
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
13 type=str, help="suffix to be added",
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
14 default='')
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
15 parser.add_argument("-n",
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
16 "--nspace",
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
17 type=int,
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
18 help="number of spaces to ignore",
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
19 default='0')
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
20
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
21 args = parser.parse_args()
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
22
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
23 with open(args.fasta, "r") as f, open(args.output, "w") as out:
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
24 for oneline in f:
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
25 if oneline == "":
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
26 continue
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
27 if not oneline:
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
28 break
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
29 if oneline[0] == ">":
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
30 header = " ".join(oneline.split()[:1 + args.nspace])
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
31 header_out = header[0] + args.prefix + header[1:] + args.suffix + "\n"
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
32 out.write(header_out)
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
33 else:
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
34 out.write(oneline)
a4cd8608ef6b Uploaded
petr-novak
parents:
diff changeset
35