-
Notifications
You must be signed in to change notification settings - Fork 42
/
fasta_extract_max_length.py
executable file
·65 lines (54 loc) · 1.64 KB
/
fasta_extract_max_length.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Extract fasta sequences that are below a maximal length.
Usage:
%program <input_file> <max_length> <output_file>"""
import gzip
import sys
import re
class Fasta(object):
"""Fasta object with name and sequence
"""
def __init__(self, name, sequence):
self.name = name
self.sequence = sequence
def write_to_file(self, handle):
handle.write(">" + self.name + "\n")
handle.write(self.sequence + "\n")
# Functions
def myopen(infile, mode="r"):
if infile.endswith(".gz"):
return gzip.open(infile, mode=mode)
else:
return open(infile, mode=mode)
def fasta_iterator(input_file):
"""Takes a fasta file input_file and returns a fasta iterator
"""
with myopen(input_file) as f:
sequence = ""
name = ""
begun = False
for line in f:
line = line.strip()
if line.startswith(">"):
if begun:
yield Fasta(name, sequence)
name = line.replace(">", "")
sequence = ""
begun = True
else:
sequence += line
if name != "":
yield Fasta(name, sequence)
try:
fasta_file = sys.argv[1] # Input fasta file
max_length = int(sys.argv[2]) # Maximum length of sequence
result_file = sys.argv[3] # Output fasta file
except:
print(__doc__)
sys.exit(0)
fasta_sequences = fasta_iterator(fasta_file)
with myopen(result_file, "w") as outfile:
for seq in fasta_sequences:
if len(seq.sequence) <= max_length:
seq.write_to_file(outfile)