-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPrimerClipper.py
199 lines (159 loc) · 5.8 KB
/
PrimerClipper.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#################################################
## path
#################################################
import sys, tempfile, shlex, glob, os, stat, hashlib, time, datetime, re
from subprocess import *
from optparse import OptionParser
from collections import defaultdict
_author="Sebastian Szpakowski"
_date="Apr 8, 2013"
_version="V1.00"
#################################################
## Classes
##
#################################################
### Iterator over input fasta file.
### Only reading when requested
### Useful for very large FASTA files
### with many sequences
class FastaParser:
def __init__ (self, x, quals=False):
self.filename = x
self.fp = open(x, "r")
self.currline = ""
self.currentFastaName = ""
self.currentFastaSequence = ""
self.lastitem=False
if quals:
self.linesep=" "
else:
self.linesep=""
def __iter__(self):
return(self)
#####
def next(self):
for self.currline in self.fp:
if self.currline.startswith(">"):
self.currline = self.currline[1:]
if self.currentFastaName == "":
self.currentFastaName = self.currline
else:
otpt = (self.currentFastaName.strip(), self.currentFastaSequence.strip())
self.currentFastaName = self.currline
self.currentFastaSequence = ""
self.previoustell = self.fp.tell()
return (otpt)
else:
self.addSequence(self.currline)
if not self.lastitem:
self.lastitem=True
return (self.currentFastaName.strip(), self.currentFastaSequence.strip())
else:
raise StopIteration
def addSequence(self, x):
self.currentFastaSequence = "%s%s%s" % (self.currentFastaSequence,self.linesep, x.strip())
def __str__():
return ("reading file: %s" %self.filename)
class fuzznuc:
def __init__(self, templates, pattern, mm):
self.templates = templates
self.pattern = pattern
self.trim = defaultdict(list)
self.mm = mm
self.runFuzznuc()
def runFuzznuc(self):
command="fuzznuc -sequence %s -pattern %s -rformat tagseq -outf stdout -pmismatch %s" % (self.templates, self.pattern, self.mm)
#print (command)
p = Popen(shlex.split(command), stdout=PIPE, stderr=PIPE, close_fds=True )
out,err = p.communicate()
self.parse(out)
def parse(self, lines):
inside = True
b1 = ""
b2 = ""
b3 = ""
counter = 0
for line in lines.split("\n"):
line = line.strip("\n")
inside = not line.startswith("#")
if inside :
if line == "":
counter = -1
else:
counter+=1
if counter % 3 == 0:
b1 = b1 + line[len("nucleotide_motif")+1:]
elif counter % 3 == 1:
b2 = b2 + line[len("nucleotide_motif")+1:]
elif counter % 3 == 2:
b3 = b3 + line[len("nucleotide_motif")+1:]
else:
print "???", line
else:
if len(b2)>0:
#print len(b2), b2
#print len(b3), b3
self.trim[b2] = [b3.find("+"), b3.rfind("+")]
b1 = ""
b2 = ""
b3 = ""
#################################################
## Functions
##
def revComp(string):
global transtab
string=string.upper()
#reverse
string = string [::-1]
return string.translate(transtab)
#################################################
## Arguments
##
parser = OptionParser()
parser.add_option("-i", "--infile", dest="fn_in",
help="input fastA file", metavar="FILE")
parser.add_option("-f", "--forward", dest="pri_f",
help="input primer F", metavar="forward")
parser.add_option("-r", "--reverse", dest="pri_r",
help="input primer R", metavar="reverse")
parser.add_option("-m", "--mm", dest="mismatches", type = "int", default = 1,
help="input primer R", metavar="reverse")
(options, args) = parser.parse_args()
#################################################
## Begin
##
from string import maketrans
inttab= "ACGTN"
outtab = "TGCAN"
transtab = maketrans(inttab, outtab)
forward = options.pri_f
reverse = options.pri_r
fA = fuzznuc(options.fn_in, forward, options.mismatches)
#fB = fuzznuc(options.fn_in, revComp(forward), options.mismatches)
#fC = fuzznuc(options.fn_in, reverse, options.mismatches)
fD = fuzznuc(options.fn_in, revComp(reverse), options.mismatches)
a= set()
#for key, val in fA.trim.items():
# a.add( key[val[0] : val[1]+1])
#print a
newfilename = "%s.noprimers.fasta" % (".".join(options.fn_in.split(".")[:-1]))
otpt = open(newfilename, "w")
for head, seq in FastaParser(options.fn_in):
A = fA.trim[seq]
#B = fB.trim[seq]
#C = fC.trim[seq]
D = fD.trim[seq]
if len(A)>0:
start = A[1]+1
else:
start = 0
if len(D)>0:
end = D[0]
else:
end = len(seq)
newseq = seq[start:end]
otpt.write(">%s\n%s\n" % (head, newseq))
otpt.close()
#################################################
## Finish
#################################################