forked from enormandeau/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastq_rrbs_convert.py
executable file
·53 lines (41 loc) · 1.17 KB
/
fastq_rrbs_convert.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
#!/usr/bin/env python
"""Convert fastq to fasta
Usage:
python fastq_to_fastq.py inputfile outputfile proportion
Where:
inputfile is a fastq or fastq.gz file
outputfile is a fastq or fastq.gz file to output to
proportion is the proportion of converted Ts
"""
# Modules
import sys
import gzip
import random
from Bio import SeqIO
try:
infile = sys.argv[1]
outfile = sys.argv[2]
proportion = float(sys.argv[3])
except:
print __doc__
sys.exit(1)
# Functions
def myopen(_file, mode="r"):
if _file.endswith(".gz"):
return gzip.open(_file, mode=mode)
else:
return open(_file, mode=mode)
# Main
sequences = (s for s in (SeqIO.parse(myopen(infile), "fastq")))
with myopen(outfile, "w") as outf:
for s in sequences:
fastq = s.format("fastq").strip().split("\n")
converted = list(fastq[1])
end = len(converted)
if "".join(converted[-3:]) == "CCG":
end = len(converted) - 3
for i in range(3, end):
if converted[i] == "T" and proportion > random.random():
converted[i] = "C"
fastq[1] = "".join(converted)
outf.write("\n".join(fastq) + "\n")