-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFASTA_exclude_seqs_by_name.py
executable file
·176 lines (142 loc) · 6.37 KB
/
FASTA_exclude_seqs_by_name.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
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
#!/usr/bin/env python3
"""
Purpose: Exclude a given set of sequences from a FASTA file
Author : Chase W. Nelson <[email protected]>
Cite : https://github.com/chasewnelson/
Date : 2022-01-20
Details: Exclude a given set of sequences from a FASTA file
Returns:
- STDOUT: documentation of retained and excluded sequence names
- FILE: --out_file, a FASTA file with excluded sequences removed
"""
import argparse
import os
import re
import sys
from Bio import SeqIO
from typing import List, NamedTuple, TextIO
usage = """# -----------------------------------------------------------------------------
FASTA_exclude_seqs_by_name.py - Exclude a given set of sequences from a FASTA file
# -----------------------------------------------------------------------------
For DOCUMENTATION, run:
$ FASTA_exclude_seqs_by_name.py --help
$ pydoc ./FASTA_exclude_seqs_by_name.py
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
EXAMPLE:
$ FASTA_exclude_seqs_by_name.py --seq_file=seqs.fasta --seq_names=seq_names.txt --out_file=seqs_exclude.fasta
# -----------------------------------------------------------------------------
"""
class Args(NamedTuple):
""" Command-line arguments """
seq_file: TextIO
seq_names: str
out_file: str
# -----------------------------------------------------------------------------
def get_args() -> Args:
""" Get command-line arguments """
parser = argparse.ArgumentParser(
description='Exclude a given set of sequences from a FASTA file. HELP: FASTA_exclude_seqs_by_name.py --help',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# Rename "optional" arguments
parser._optionals.title = 'Named arguments'
# -------------------------------------------------------------------------
# REQUIRED
parser.add_argument('-i',
'--seq_file',
metavar='FILE',
help='FASTA file(s) [REQUIRED]',
required=True,
type=argparse.FileType('rt'))
parser.add_argument('-I',
'--seq_names',
metavar='str/FILE',
help='A comma-separated list of sequence IDs (names; beginning of FASTA headers) [REQUIRED]',
required=True,
type=str)
# -------------------------------------------------------------------------
# OPTIONAL
parser.add_argument('-o',
'--out_file',
metavar='str',
help='Output file prefix for printing sequences; otherwise STDOUT [OPTIONAL]',
required=False,
type=str,
default='')
args = parser.parse_args()
# -------------------------------------------------------------------------
# VALIDATE arguments
# if seq_names is file, convert to string
if os.path.isfile(args.seq_names):
args.seq_names = open(args.seq_names).read().rstrip()
# die if seq_names contains anything besides word, dash, whitespace, and commas
re_NOT_seqID = re.compile(r'[^\w\-\s\n,]')
if re_NOT_seqID.search(args.seq_names):
parser.error(f'\n### ERROR: custom_sites="{args.seq_names[:20]}..." contains invalid characters')
# die if out_file exists
if args.out_file != '' and os.path.isfile(args.out_file):
parser.error(f'\n### ERROR: out_file="{args.out_file}" already exists')
return Args(seq_file=args.seq_file,
seq_names=args.seq_names,
out_file=args.out_file)
# -----------------------------------------------------------------------------
def main() -> None:
""" Tell them they are walking around shining like the sun """
# -------------------------------------------------------------------------
# GATHER arguments
args = get_args()
seq_fh = args.seq_file
seq_names = args.seq_names
out_file = args.out_file
# -------------------------------------------------------------------------
# INITIALIZE OUTPUT AND LOG
print(usage)
print('# -----------------------------------------------------------------------------')
print(f'LOG:command="{" ".join(sys.argv)}"')
print(f'LOG:cwd="{os.getcwd()}"')
print(f'LOG:seq_file="{seq_fh.name}"')
print(f'LOG:seq_names="{seq_names}"')
print(f'LOG:out_file="{out_file}"')
# -------------------------------------------------------------------------
# INITIALIZE set of seq_names
seq_name_list = list(seq_names.split(','))
print(f'LOG:seq_names_count={len(seq_name_list)}')
seq_name_set = set(seq_name_list)
unique_seq_names_count = len(seq_name_set)
print(f'LOG:unique_seq_names_count={unique_seq_names_count}')
# -------------------------------------------------------------------------
# PRINT the wanted recs
# OPEN out_file for writing, to which SeqIO.write() will append
out_fh = sys.stdout
if out_file != '':
out_fh = open(out_file, 'wt')
# initialize counters
nseqs = 0
nseqs_included = 0
seq_names_included: List[str] = []
nseqs_excluded = 0
seq_names_excluded: List[str] = []
for rec in SeqIO.parse(seq_fh, 'fasta'):
nseqs += 1
if rec.id in seq_name_set: # EXCLUDE
nseqs_excluded += 1
seq_names_excluded.append(rec.id)
else: # INCLUDE
nseqs_included += 1
seq_names_included.append(rec.id)
SeqIO.write(rec, out_fh, 'fasta') # APPENDS to an open filehandle
print(f'LOG:nseqs={nseqs}')
print(f'LOG:nseqs_included={nseqs_included}')
print(f'LOG:seq_names_included={",".join(seq_names_included)}')
print(f'LOG:nseqs_excluded={nseqs_excluded}')
print(f'LOG:seq_names_excluded={",".join(seq_names_excluded)}')
# -------------------------------------------------------------------------
# DONE message
print('\n# -----------------------------------------------------------------------------')
print('DONE')
# -----------------------------------------------------------------------------
# CALL MAIN
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
if __name__ == '__main__':
main()