forked from pimbongaerts/radseq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfasta_include.py
More file actions
executable file
·46 lines (39 loc) · 1.45 KB
/
fasta_include.py
File metadata and controls
executable file
·46 lines (39 loc) · 1.45 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
#!/usr/bin/env python
"""
Reduces FASTA file to only those loci listed in supplied text file.
"""
import sys
import argparse
__author__ = 'Pim Bongaerts'
__copyright__ = 'Copyright (C) 2016 Pim Bongaerts'
__license__ = 'GPL'
def main(fasta_filename, list_filename):
# Read list from file and convert to set (unique values only)
with open(list_filename) as file:
lines = [line.strip() for line in file]
loci = set(lines)
# Parse FASTA and only output those in set
fasta_file = open(fasta_filename, 'r')
output_sequence = False
locuscount = 0
for line in fasta_file:
if line[0] == '>':
# Evaluate if sequence in list of loci
locusname = line[1:].strip()
if locusname in loci:
output_sequence = True
print(line.strip())
else:
output_sequence = False
elif output_sequence:
# Output sequence if in list of loci
print(line.strip())
fasta_file.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('fasta_filename', metavar='fasta_file',
help='FASTA input file (`.fasta`/ `.fa`)')
parser.add_argument('inclusion_filename', metavar='inclusion_file',
help='text file with names of loci to be included')
args = parser.parse_args()
main(args.fasta_filename, args.inclusion_filename)