forked from adiamb/FASTQ_to_FASTA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FASTQ_to_FASTA.py
54 lines (49 loc) · 1.29 KB
/
FASTQ_to_FASTA.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
import sys, subprocess, gzip
infile = sys.argv[1]
fasta = sys.argv[2]
gz = sys.argv[3]
## if gz is present open it with the gzip library else normal
if '.gz' in infile:
print 'detected a .gz file '
fastq = gzip.open(infile, 'rb')
else:
fastq = open(infile, 'r')
## main function, skip line id 34 and rest counter to write out the fasta
def main(fastq, fasta, gz):
line_n =0
line_buffer = 0
line_id = 1
if gz == '1': ### if gz argument is provided it will write out a gz file else normal
outfile = gzip.open(fasta+'.gz', 'w')
else:
outfile = open(fasta, 'w')
fastas = 1
fasta_length =0
for line in fastq:
line_n += 1
#line_id += 1
if line_n == 10000:
line_buffer += 10000
line_n =0
print 'processed lines ', line_buffer
if line_id == 4:
line_id = 1
elif line_id == 3:
line_id += 1
elif line_id == 2:
line_id += 1
fasta_line = line
fasta_length += len(fasta_line.strip())
outfile.write(fasta_line)
fastas += 1
else:
if '@' not in line:
print 'are you sure this is a fastq ??'
else:
fasta_header = line.replace('@', '>')
line_id += 1
outfile.write(fasta_header)
outfile.close()
print 'FASTA records written', fastas, 'average length of fasta sequences ', float(fasta_length//fastas)
if __name__ == '__main__':
main(fastq, fasta, gz)