-
Notifications
You must be signed in to change notification settings - Fork 11
/
fastq_trim.py
executable file
·54 lines (42 loc) · 1.45 KB
/
fastq_trim.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
#!/usr/bin/env python
from optparse import OptionParser
import bz2
import gzip
'''
fastq_trim.py
Filter a FASTQ file for various properties, like read length.
'''
################################################################################
# main
################################################################################
def main():
usage = 'usage: %prog [options] <trim_length> <fastq_file>'
parser = OptionParser(usage)
(options,args) = parser.parse_args()
if len(args) != 2:
parser.error('Must provide trim length and FASTQ file')
else:
trim_length = int(args[0])
fastq_file = args[1]
if fastq_file[-3:] == '.gz':
fastq_in = gzip.open(fastq_file, 'rt')
elif fastq_file[-4:] == '.bz2':
fastq_in = bz2.open(fastq_file, 'rt')
else:
fastq_in = open(fastq_file)
header = fastq_in.readline().rstrip()
while header:
seq = fastq_in.readline().rstrip()
mid = fastq_in.readline().rstrip()
qual = fastq_in.readline().rstrip()
# trim
seq = seq[:trim_length]
qual = qual[:trim_length]
print('%s\n%s\n%s\n%s' % (header,seq,mid,qual))
header = fastq_in.readline().rstrip()
fastq_in.close()
################################################################################
# __main__
################################################################################
if __name__ == '__main__':
main()