-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.record.from.multifasta.py
executable file
·57 lines (49 loc) · 1.74 KB
/
extract.record.from.multifasta.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
#!/usr/bin/env python
import gzip
import os
import sys
from argparse import ArgumentParser
from Bio import SeqIO
def parseArgs():
parser = ArgumentParser(
description='extract record(s) from a multi-record FastA file that '
'have a query string match to defline(s)', add_help=False)
req = parser.add_argument_group('Required')
req.add_argument('-i', '--infile', required=True, metavar='FILE',
help='input multi-FastA file, optionally gunzip compressed')
req.add_argument('-q', '--query', required=True, type=str, metavar='STR',
help='string to search deflines')
opt = parser.add_argument_group('Optional')
opt.add_argument('-h', '--help', action='help',
help='show this help message and exit')
opt.add_argument('-o', '--outfile', default=None, metavar='FILE',
help='FastA output [stdout]')
opt.add_argument('-y', '--search-type', default='contains',
choices=['contains', 'full_exact'], help='type of query match '
'[default: contains]')
return parser.parse_args()
def main():
opt = parseArgs()
infile = os.path.abspath(os.path.expanduser(opt.infile))
query = opt.query
if infile.endswith('.gz'):
infile = gzip.open(infile)
mfasta = SeqIO.parse(infile, 'fasta')
query_match = []
for record in mfasta:
if opt.search_type == 'contains':
if query in str(record.description):
query_match.append(record)
elif opt.search_type == 'full_exact':
if query == str(record.description):
query_match.append(record)
if len(query_match) == 0:
sys.stderr.write('ERROR: {} absent from deflines\n'.format(query))
sys.exit(1)
if opt.outfile:
outfile = os.path.abspath(os.path.expanduser(opt.outfile))
SeqIO.write(query_match, outfile, 'fasta')
else:
SeqIO.write(query_match, sys.stdout, 'fasta')
if __name__ == '__main__':
main()