-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathfasta_extract_name.py
More file actions
executable file
·36 lines (27 loc) · 920 Bytes
/
fasta_extract_name.py
File metadata and controls
executable file
·36 lines (27 loc) · 920 Bytes
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Extract sequences from a fasta file if their name is in a 'wanted' file.
Wanted file contains one sequence name per line.
Usage:
%program <input_file> <wanted_file> <output_file>"""
import sys
import re
try:
from Bio import SeqIO
except:
print("This program requires the Biopython library")
sys.exit(0)
try:
fasta_file = sys.argv[1] # Input fasta file
wanted_word = sys.argv[2] # Word to be found in the sequence name
result_file = sys.argv[3] # Output fasta file
except:
print(__doc__)
sys.exit(0)
fasta_sequences = SeqIO.parse(open(fasta_file),'fasta')
with open(result_file, "w") as f:
for seq in fasta_sequences:
name = seq.name
if wanted_word in name and len(str(seq.seq)) > 0:
wanted.remove(name) # Output only the first appearance for a name
SeqIO.write([seq], f, "fasta")