-
Notifications
You must be signed in to change notification settings - Fork 2
/
fastq_reverse_comp.py
60 lines (44 loc) · 1.38 KB
/
fastq_reverse_comp.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
#!/usr/bin/env python3
# Author: Jeffrey Grover
# Purpose: Reverse complement a fastq or fasta file
# per-base coverage output
# Created: 2019-07-29
from argparse import ArgumentParser
import gzip
def magic_opener(input_file):
if input_file.endswith('gz'):
return gzip.open(input_file, 'rt')
else:
return open(input_file, 'r')
def fq_iter(input_file):
n = 0
fq_record = []
with magic_opener(input_file) as input_handle:
for line in input_handle:
n += 1
fq_record.append(line.strip())
if n == 4:
yield fq_record
n = 0
fq_record = []
def rev_comp(fq_iter):
comp_table = str.maketrans('ATCG', 'TAGC')
for record in fq_iter:
seq_id = record[0]
rev_comp_seq = record[1].translate(comp_table)[::-1]
separator = record[2]
qual = record[3][::-1]
print(seq_id, rev_comp_seq, separator, qual, sep='\n')
# ArgumentParser
def get_args():
parser = ArgumentParser(
description='Returns the reverse complement of a fastq file to stdout.')
parser.add_argument('fastq',
help='Input .fastq, may be gzipped',
metavar='FILE.fastq(.gz)')
return parser.parse_args()
# Entry point
def main(args):
rev_comp(fq_iter(args.fastq))
if __name__ == '__main__':
main(get_args())