-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastq2fasta.py
executable file
·149 lines (128 loc) · 4.22 KB
/
fastq2fasta.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
#
# This file is part of the FastQTool distribution (https://gitlab.com/bio-pnpi/fastqtools or http://xxx.github.io).
# Copyright (c) 2023 Alexey Shvetsov.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-FileCopyrightText: 2023 Alexey Shvetsov <alexxyum@gmail.com>
# SPDX-License-Identifier: GPL-3.0-or-later
import argparse
from FastQTools.ExpandPrimer import ExpandPrimer
from FastQTools.ReadFastqGzToFasta import ReadFasqGzToFasta
from FastQTools.StripPrimer import StripPrimer
parser = argparse.ArgumentParser(
prog="fastq2fasta",
description="Read and convert fastq to fasta"
)
parser.add_argument(
'--fastq',
type=str,
nargs='+',
required=True,
help='Input file(s) in fastq format'
)
parser.add_argument(
'--merge',
type=str,
help='Merged file with all seq in fasta format'
)
parser.add_argument(
'--primers',
dest='primersFnm',
type=str,
help='File with primers'
)
parser.add_argument(
'--strip-primers',
dest='stripPrimers',
action='store_true'
)
args = parser.parse_args()
def readPrimers(primersFnm):
fd = open(primersFnm)
data = fd.readlines()
fd.close()
Primers = []
for line in data:
primer = line.strip()
if checkPrimer(primer):
Primers.append(line.strip())
else:
print("Error: Input primers has malformed lines {}".format(primer))
exit(1)
return Primers
def checkPrimer(Primer):
pAlphabet = frozenset('ATGCMRWSYKVHDBN')
return pAlphabet.issuperset(Primer)
def checkFastqGz(fnm):
if fnm.endswith(".fastq.gz"):
return True
else:
return False
def writeFasta(fnm, hdr, seq):
fo = open(fnm, 'w+')
num = len(hdr)
for i in range(num):
print(">{h}\n{s}".format(h=hdr[i], s=seq[i]), file=fo)
fo.close()
def main():
Primers = None
fPrimers = []
rPrimers = []
hdrall = []
seqall = []
# deal with primers
if args.stripPrimers:
if args.primersFnm is not None:
Primers = readPrimers(args.primersFnm)
print("Primers is: {}".format(Primers))
else:
print("Error: Strip Primers option is set, while no Primers Given")
exit(1)
if Primers is not None:
# Expand all primers
for primer in Primers:
ePrimer = ExpandPrimer(primer)
ePrimer.Expand()
for fp in ePrimer.GetForward():
fPrimers.append(fp)
for rp in ePrimer.GetReverse():
rPrimers.append(rp)
print("Expanded forward primers: {}".format(fPrimers))
print("Expanded reverse primers: {}".format(rPrimers))
# Now read and convers fastq files
for fastq in args.fastq:
if checkFastqGz(fastq):
print("Processing {}".format(fastq))
fqgz = ReadFasqGzToFasta(fastq)
fqgz.Process()
hdr = fqgz.GetHdrs()
seq = fqgz.GetSeqs()
print("Read {} records from {} writing result".format(fqgz.GetNumRecords(), fastq))
writeFasta(fastq.replace(".fastq.gz", ".fasta"), hdr, seq)
if args.stripPrimers:
sp = StripPrimer(seq=seq, quality=None, fprimers=fPrimers, rprimers=rPrimers)
sp.Strip()
seq = sp.GetStripped()
if args.merge is not None:
for i in range(fqgz.GetNumRecords()):
hdrall.append(hdr[i])
seqall.append(seq[i])
else:
print("Error: input files should be .fastq.gz")
exit(1)
if args.merge is not None:
writeFasta(args.merge, hdrall, seqall)
if __name__ == '__main__':
main()