-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentify-v4-16S-region.py
executable file
·185 lines (141 loc) · 4.58 KB
/
identify-v4-16S-region.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#! /usr/bin/env python3
from Bio import SeqIO
import argparse
__author__ = "Sami Pietila"
__copyright__ = "Copyright 2016"
__credits__ = ["Sami Pietila"]
__license__ = "GPL"
__version__ = "0.1"
__maintainer__ = "Sami Pietila"
__email__ = "[email protected]"
#V3+V4:
#Forward CCTACGGGNGGCWGCAG
#Reverse GACTACHVGGGTATCTAATCC
#V4:
#forward: GTGCCAGCMGCCGCGGTAA
#reverse: GGACTACHVGGGTWTCTAAT
#V4+V5
#forward: GTGCCAGCMGCCGCGGTAA
#reverse: CCCGTCAATTCMTTTRAGT
primers = {#("v3v4","forward"): "CCTACGGGNGGCWGCAG",
("v4","forward"): "GTGCCAGCMGCCGCGGTAA",
#("v4v5","forward"): "GTGCCAGCMGCCGCGGTAA",
#("v3v4","revcomp"):"GACTACHVGGGTATCTAATCC",
("v4","revcomp"):"GGACTACHVGGGTWTCTAAT",
#("v4v5","revcomp"):"CCCGTCAATTCMTTTRAGT"
}
nt = {}
nt['A']=["A"]
nt['C']=["C"]
nt['G']=["G"]
nt['T']=["T"]
nt['W']=["A","T"]
nt['S']=["C","G"]
nt['M']=["A","C"]
nt['K']=["G","T"]
nt['R']=["A","G"]
nt['Y']=["C","T"]
nt['B']=["C","G","T"]
nt['D']=["A","G","T"]
nt['H']=["A","C","T"]
nt['V']=["A","C","G"]
nt['N']=["A","C","G","T"]
ntc = {}
ntc['A']="T"
ntc['C']="G"
ntc['G']="C"
ntc['T']="A"
ntc['W']="W"
ntc['S']="S"
ntc['M']="K"
ntc['K']="M"
ntc['R']="Y"
ntc['Y']="R"
ntc['B']="V"
ntc['D']="H"
ntc['H']="D"
ntc['V']="B"
ntc['N']="N"
def revcompPrimer(primer):
revcomp = ""
for i in range(len(primer)):
revcomp+=ntc[primer[-1-i]]
return revcomp
def matchPrimer(primer, seq):
assert len(primer) <= len(seq)
n = 0
for i in range(len(primer)):
if seq[i] in nt[primer[i]]:
n = n +1
return n
def find_primer(primer, seq, max_misbases):
pl = len(primer)
matchL = []
matchI = []
for i in range(len(seq)-len(primer)):
n = matchPrimer(primer, seq[i:])
matchL.append(n)
matchI.append(i)
m = max(matchL)
if pl - m <= max_misbases:
ml = [i for i, j in enumerate(matchL) if j == m]
inds = []
for i in ml:
inds.append(matchI[i])
return m, inds
return None, []
def find_primer_from_fasta(input_fasta, max_missbases):
records = []
counts = {}
for region, ptype in primers:
counts[region, ptype] = {"found":0, "missed":0}
with open(input_fasta, "r") as input_fh:
for record in SeqIO.parse(input_fh, "fasta"):
seq = str(record.seq)
forward_primer_found = False
reverse_primer_found = False
for region, ptype in primers:
if ptype == "revcomp":
primer = revcompPrimer(primers[region, ptype])
elif ptype == "forward":
primer = primers[region, ptype]
else:
raise Exception("Unknown primer treatment")
_,inds = find_primer(primer, record.seq, int(max_missbases))
offset = 0
for ind in inds:
from_ind = None
to_ind = None
from_ind = ind+offset
to_ind = ind+offset + len(primer)
if from_ind != None or to_ind != None:
if ptype == "forward":
forward_primer_found = True
seq = seq[:from_ind] + "[" + seq[from_ind:]
offset+=1
if ptype == "revcomp":
reverse_primer_found = True
seq = seq[:to_ind] + "]" + seq[to_ind:]
offset+=1
if forward_primer_found and reverse_primer_found:
print (">" + record.id)
print (seq)
# if records:
# SeqIO.write(records, output_fasta, "fasta")
return
if __name__=="__main__":
parser = argparse.ArgumentParser(description='This is a script to find primer sequences from fasta')
parser.add_argument('--input-fasta',
action='store',
dest='input_fasta',
required=True,
default=None,
help='Input fasta file')
parser.add_argument('--max_missbases',
action='store',
dest='max_missbases',
required=False,
default="1",
help='Maxum amount of wrong bases [default 1]')
args = parser.parse_args()
find_primer_from_fasta(args.input_fasta, args.max_missbases)