forked from jrjhealey/bioinfo-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastafetcher.py
executable file
·146 lines (128 loc) · 4.68 KB
/
fastafetcher.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
#!/usr/bin/env python
# Extract fasta files by their descriptors stored in a separate file.
# Requires biopython
# TODO:
# - Create more sophisticated logic for matching IDs/Descriptions/Partial matches etc.
# - Create a mode variable to encapsulate invert/partial/description/id etc?
from Bio import SeqIO
import sys
import argparse
def get_keys(args):
"""Turns the input key file into a list. May be memory intensive."""
with open(args.keyfile, "r") as kfh:
keys = [line.rstrip("\n").lstrip(">") for line in kfh]
return keys
def get_args():
try:
parser = argparse.ArgumentParser(
description="Retrieve one or more fastas from a given multifasta."
)
parser.add_argument(
"-f",
"--fasta",
action="store",
required=True,
help="The multifasta to search.",
)
parser.add_argument(
"-k",
"--keyfile",
action="store",
help="A file of header strings to search the multifasta for. Must be one per line.",
)
parser.add_argument(
"-s",
"--string",
action="store",
help="Provide a string to look for directly, instead of a file (can accept a comma separated list of strings).",
)
parser.add_argument(
"-o",
"--outfile",
action="store",
default=None,
help="Output file to store the new fasta sequences in. Just prints to screen by default.",
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Set whether to print the key list out before the fasta sequences. Useful for debugging.",
)
parser.add_argument(
"-i",
"--invert",
action="store_true",
help="Invert the search, and retrieve all sequences NOT specified in the keyfile.",
)
parser.add_argument(
"-m",
"--method",
action="store",
choices=["exact", "partial"],
default="exact",
help="Search the headers as exact matches, or as partial substring matches. "
"The latter is dangerous, as headers may be matched twice, so be sure "
"your headers/keys are unique to their respective sequences."
)
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
except NameError:
sys.stderr.write(
"An exception occured with argument parsing. Check your provided options."
)
sys.exit(1)
return parser.parse_args()
def main():
"""Takes a string or list of strings in a text file (one per line) and retreives them and their sequences from a provided multifasta."""
args = get_args()
# Call getKeys() to create the list of keys from the provided file:
if not (args.keyfile or args.string):
sys.stderr.write("No key source provided. Exiting.")
sys.exit(1)
if args.keyfile:
keys = get_keys(args)
else:
keys = args.string.split(",")
if args.verbose:
if args.invert is False:
sys.stderr.write("Fetching the following keys:\n")
for key in keys:
sys.stderr.write(key + "\n")
elif args.invert is True:
sys.stderr.write(
"Ignoring the following keys, and retreiving everything else from: {}\n".format(
args.fasta
)
)
for key in keys:
sys.stderr.write(key + "\n")
sys.stderr.write(
"-" * 80 + "\n"
)
# Parse in the multifasta and assign an iterable variable:
to_write = []
for rec in SeqIO.parse(args.fasta, "fasta"):
if args.invert is False:
if args.method == "exact":
if rec.id in keys:
print(rec.format("fasta"))
to_write.append(rec)
elif args.method == "partial":
if any(key in rec.description for key in keys):
print(rec.format("fasta"))
to_write.append(rec)
elif args.invert is True:
if args.method == "exact":
if rec.id not in keys:
print(rec.format("fasta"))
to_write.append(rec)
elif args.method == "partial":
if all(key not in rec.description for key in keys):
print(rec.format("fasta"))
to_write.append(rec)
if args.outfile:
SeqIO.write(to_write, args.outfile, "fasta")
if __name__ == "__main__":
main()