/
main.py
240 lines (199 loc) · 10.7 KB
/
main.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import re
from Bio.Blast import NCBIWWW
from Bio.Seq import reverse_complement, translate
from load import *
from featuresortdic import *
from Bio import SeqIO
from Bio import SeqUtils
from Bio.SeqRecord import SeqRecord
from Bio.Alphabet import generic_dna
from Bio.SeqFeature import *
import pickle
import random
pickle_file_Name = "featureObjects"
def generate():
print("Loading vectors")
features_Container = getFeature("vectors.gb")
featureDictionary = FeatureSortDic(features_Container)
featureDictionary.appendSpecialTransFeature("tags_epitopes.mfasta", "fasta")
featureDictionary.appendPrimer("common_primer.mfasta", "fasta")
return featureDictionary.featureDictionary
def createPickle(featureStatistic_container):
# open the file for writing
fileObject = open(pickle_file_Name,'wb')
pickle.dump(featureStatistic_container,fileObject)
fileObject.close()
def addFeatureSTF():
if m.end() <= seqLength:
newFeature = SeqFeature(FeatureLocation(m.start(),m.end(), strand=1), type=str(feature))
newFeature.qualifiers['note'] = featureName
newRecord.features.append(newFeature)
else:
newFeature = SeqFeature(CompoundLocation([FeatureLocation(m.start(),seqLength, strand=1), FeatureLocation(1,(seqLength - m.end()),strand=1)]), type=str(feature))
newFeature.qualifiers['note'] = featureName
newRecord.features.append(newFeature)
def addFeatureComplSTF():
if m.end() <= seqLength:
newFeature = SeqFeature(FeatureLocation(m.start(),m.end(), strand=-1), type=str(feature))
newFeature.qualifiers['note'] = featureName
newRecord.features.append(newFeature)
else:
newFeature = SeqFeature(CompoundLocation([FeatureLocation(m.start(),seqLength, strand=-1), FeatureLocation(1, (seqLength - m.end()), strand=-1)]), type=str(feature))
newFeature.qualifiers['note'] = featureName
newRecord.features.append(newFeature)
def writeSTF():
global difference, seqRecordToCheck, seqRecordToCheckComplement, variation, featureName, featureSeq, seqLength, m
difference = len(record.seq) % 3
seqRecordToCheck = str(record.seq)
if difference != 0:
seqRecordToCheck = str(record.seq)[:-difference]
else:
seqRecordToCheck = str(record.seq)
seqRecordToCheckComplement = str(reverse_complement(seqRecordToCheck))
# Reading Frames
firstReadingFrame = translate(seqRecordToCheck)
secondReadingFrame = translate(seqRecordToCheck[1::] + seqRecordToCheck[0])
thirdReadingFrame = translate(seqRecordToCheck[2::] + seqRecordToCheck[0:2])
# Reading Frames (reverseComplement)
firstReadingFrameComplement = translate(seqRecordToCheckComplement)
secondReadingFrameComplement = translate(seqRecordToCheckComplement[1::] + seqRecordToCheckComplement[0])
thirdReadingFrameComplement = translate(seqRecordToCheckComplement[2::] + seqRecordToCheckComplement[0:2])
for variation in featureStatistic_container[feature]:
featureName = variation.note
featureSeq = str(variation.seq)
featureLength = len(variation.seq)
seqLength = len(seqRecordToCheck)
firstReadingFrameCircular = firstReadingFrame + firstReadingFrame[0:featureLength - 1]
secondReadingFrameCircular = secondReadingFrame + secondReadingFrame[0:featureLength - 1]
thirdReadingFrameCircular = thirdReadingFrame + thirdReadingFrame[0:featureLength - 1]
firstReadingFrameComplementCircular = firstReadingFrameComplement + firstReadingFrameComplement[
0:featureLength - 1]
secondReadingFrameComplementCircular = secondReadingFrameComplement + secondReadingFrameComplement[
0:featureLength - 1]
thirdReadingFrameComplementCircular = thirdReadingFrameComplement + thirdReadingFrameComplement[
0:featureLength - 1]
# Find Matches
firstFrameMatchesCircular = re.finditer(featureSeq, firstReadingFrameCircular)
secondFrameMatchesCircular = re.finditer(featureSeq, secondReadingFrameCircular)
thirdFrameMatchesCircular = re.finditer(featureSeq, thirdReadingFrameCircular)
firstFrameComplementMatchesCircular = re.finditer(featureSeq, firstReadingFrameComplementCircular)
secondFrameComplementMatchesCircular = re.finditer(featureSeq, secondReadingFrameComplementCircular)
thirdFrameComplementMatchesCircular = re.finditer(featureSeq, thirdReadingFrameComplementCircular)
for m in firstFrameMatchesCircular:
addFeatureSTF()
for m in secondFrameMatchesCircular:
addFeatureSTF()
for m in thirdFrameMatchesCircular:
addFeatureSTF()
for m in firstFrameComplementMatchesCircular:
addFeatureComplSTF()
for m in secondFrameComplementMatchesCircular:
addFeatureComplSTF()
for m in thirdFrameComplementMatchesCircular:
addFeatureComplSTF()
def writePBS():
global variation, seqRecordToCheck, seqRecordToCheckComplement, difference, newFeature
for variation in featureStatistic_container[feature]:
primerSeq = str(variation.seq)
primerName = variation.note
partialPrimerSeq = primerSeq[len(primerSeq) - 15::]
seqRecordToCheck = str(record.seq)
seqRecordToCheckComplement = str(reverse_complement(record.seq))
matchingPrimerPositions = SeqUtils.nt_search(seqRecordToCheck, partialPrimerSeq)
if (len(matchingPrimerPositions) > 1):
difference = len(primerSeq) - len(partialPrimerSeq)
length = len(matchingPrimerPositions)
for j in range(1, length):
if primerSeq == seqRecordToCheck[matchingPrimerPositions[j] -
difference: matchingPrimerPositions[j] - difference + len(primerSeq)]:
newFeature = SeqFeature(FeatureLocation(matchingPrimerPositions[j],
matchingPrimerPositions[j] + len(primerSeq),
strand=1), type=str(feature))
newFeature.qualifiers['note'] = primerName
newRecord.features.append(newFeature)
else:
newFeature = SeqFeature(FeatureLocation(matchingPrimerPositions[j], AfterPosition(
matchingPrimerPositions[j] + len(primerSeq)), strand=1), type=str(feature))
newFeature.qualifiers['note'] = primerName
newRecord.features.append(newFeature)
matchingPrimerPositions = SeqUtils.nt_search(seqRecordToCheckComplement, partialPrimerSeq)
if (len(matchingPrimerPositions) > 1):
difference = len(primerSeq) - len(partialPrimerSeq)
length = len(matchingPrimerPositions)
for j in range(1, length):
if primerSeq == seqRecordToCheckComplement[matchingPrimerPositions[j] -
difference: matchingPrimerPositions[j] - difference + len(primerSeq)]:
newFeature = SeqFeature(FeatureLocation(matchingPrimerPositions[j],
matchingPrimerPositions[j] + len(primerSeq),
strand=-1), type=str(feature))
newFeature.qualifiers['note'] = primerName
newRecord.features.append(newFeature)
else:
newFeature = SeqFeature(FeatureLocation(matchingPrimerPositions[j], AfterPosition(
matchingPrimerPositions[j] + len(primerSeq)), strand=-1), type=str(feature))
newFeature.qualifiers['note'] = primerName
newRecord.features.append(newFeature)
def writeFeature(strand):
global newFeature
if (len(occurrence) > 1):
for i in range(1, len(occurrence)):
newFeature = SeqFeature(FeatureLocation(occurrence[i], occurrence[i] + len(featureSeq), strand=strand),
type=str(feature))
if variation.product is not None:
newFeature.qualifiers['product'] = variation.product
if variation.gene is not None:
newFeature.qualifiers['gene'] = variation.gene
if variation.bound_moiety is not None:
newFeature.qualifiers['bound_moiety'] = variation.bound_moiety
if variation.mobile is not None:
newFeature.qualifiers['mobile'] = variation.mobile
if variation.note is not None:
newFeature.qualifiers['note'] = variation.note
newRecord.features.append(newFeature)
if __name__ == "__main__":
try:
print("Trying to load pickle object feature dictionary")
featureStatistic_container = pickle.load(open(pickle_file_Name,'r'))
print("successfully loaded... ")
except IOError:
print("loading pickle faild")
featureStatistic_container = generate()
print("create pickle object")
createPickle(featureStatistic_container)
doBlast = False
output_handle = open("outbput.gb", "w")
records = list(SeqIO.parse("vectors.gb", "genbank"))
print("Annotate vectors")
for randomRec in range(1,2):
record = records[random.randint(1, len(records))]
newRecord = SeqRecord(record.seq)
#writing Header
newRecord.seq.alphabet = generic_dna
newRecord.id = record.id
newRecord.name = record.name
newRecord.description = record.description
recordSeq = str(record.seq)
for feature in featureStatistic_container:
if feature not in ["PBS", "STF"]:
for variation in featureStatistic_container[feature]:
featureSeq = str(variation.seq)
occurrence = SeqUtils.nt_search(recordSeq, featureSeq)
writeFeature(strand=1)
featureSeqComplement = str(variation.seq.complement())
occurrence = SeqUtils.nt_search(recordSeq, featureSeqComplement)
writeFeature(strand=-1)
else:
if(feature == "STF"):
writeSTF()
if(feature == "PBS"):
writePBS()
SeqIO.write(newRecord, output_handle, "genbank")
## Do BLAST
if doBlast:
result_handle = NCBIWWW.qblast("blastx", "refseq_protein", record.seq)
blast_result = open("blst" +str(randomRec)+ ".xml", "w")
blast_result.write(result_handle.read())
blast_result.close()
result_handle.close()
output_handle.close()
print("done")