-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremove_fastas.py
More file actions
93 lines (75 loc) · 2.93 KB
/
Copy pathremove_fastas.py
File metadata and controls
93 lines (75 loc) · 2.93 KB
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
#!/usr/bin/env python
###############################################
# Author: B. Anderson
# Date: 28 Jun 2019
# Updated: Oct 2021 (can now take a text file with strings for string matching, one per line; also cleaned up and added parser)
# Updated: Jun 2022; Mar 2026 (added quiet argument; adjusted for improved speed with many files)
# Description: a script to simply remove entries from multifastas based on string matches (arg1)
###############################################
import sys
import os
import argparse
from Bio import SeqIO
# instantiate the parser
parser = argparse.ArgumentParser(description = 'A script to remove fasta entries based on string matching. ' +
'The new fasta files are output in the current directory with prefix \"mod_\".')
# add arguments to parse
parser.add_argument('strings', type = str, help = 'The comma delimited strings to search for ' +
'when removing fastas from the multifastas (or nothing if file specified), ' +
'and then the space-delimited multifastas', nargs = '*')
parser.add_argument('-f', type = str, dest = 'string_file', help = 'A text file with a list of strings, ' +
'one per line (optional). If not specified, then the first space-delimited command line argument ' +
'will be interpreted as the comma-delimited search strings.')
parser.add_argument('--quiet', '-q', action = 'store_true', help = 'Whether to silence screen reporting [default do not]')
# parse the command line
if len(sys.argv[1:]) == 0: # if there are no arguments
parser.print_help(sys.stderr)
sys.exit(1)
args = parser.parse_args()
strings = args.strings
string_file = args.string_file
if not strings:
parser.print_help(sys.stderr)
sys.exit(1)
string_list = []
file_list = []
if not string_file: # if there is no file specified with '-f'
search_strings = strings[0]
for part in search_strings.split(','):
string_list.append(part)
for fasta in strings[1:]:
file_list.append(fasta)
else:
with open(string_file, 'r') as str_file:
for line in str_file:
string_list.append(line.rstrip())
for fasta in strings:
file_list.append(fasta)
# read in the each multifasta file and remove entries
# Now updated to instead selectively store rather than remove
master_list = []
for multifasta in file_list:
filename = os.path.basename(multifasta)
fasta_list = []
removed = 0
fastas = SeqIO.parse(open(multifasta, 'r'), 'fasta')
for fasta in fastas:
store = True
for string in string_list:
if string in fasta.description:
store = False
removed = removed + 1
break
if store:
fasta_list.append(fasta)
if len(fasta_list) > 0:
master_list.append([filename, fasta_list])
if not args.quiet:
print('Removed ' + str(removed) + ' entries from ' + filename)
# output the files
for entry in master_list:
with open('mod_' + entry[0], 'w') as outfile:
if not args.quiet:
print('Writing ' + str(len(entry[1])) + ' entries to new file mod_' + entry[0])
for fasta in entry[1]:
SeqIO.write(fasta, outfile, 'fasta')