-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart1.py
executable file
·319 lines (233 loc) · 14.2 KB
/
part1.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import re
import random as rd
import itertools
import myfunctions as myf
def generatePermutations(theListOfLists): # Used to make all permutations
allpermutations = []
for i in theListOfLists:
for subset in itertools.permutations(i):
if subset not in allpermutations:
allpermutations.append(subset)
return allpermutations
def generateCombinations(theList): # Used to make all combinations
combinationsStore = []
upTo = len(theList) + 1
for i in xrange(1, upTo):
for acomb in itertools.combinations(theList, i):
combinationsStore.append(acomb)
return combinationsStore
def equaliseSize(theListofLists, lengthdesired): # Adds empty strings instead of motifs, if less than 4 motifs are used
outerList = []
for i in theListofLists:
innerList = []
for j in i:
innerList.append(j)
while len(innerList) < lengthdesired:
innerList.append("")
outerList.append(innerList)
return outerList
def gc_cont(bar_code): # GC/AT ratio taken into account, for thermodynamics and their effects at the experiment
bar_code = bar_code.upper()
total_count = bar_code.count("G") + bar_code.count("C")
return total_count/float(len(bar_code))
# function makes the barcodes
def make_barcode(length):
barcode = []
l1=['A','G','C','T']
for i in range(length):
barcode.append(rd.choice(l1))
return ''.join(barcode)
def barcode_generator(number, mingc, maxgc,barCodeLength, diffs=2): # generates barcodes that differ from existing barcodes by a minimum number of bases, each sequence has a unique barcode
barcode_storage = []
countlist = []
l1 = ['A','G','C','T']
for i in range(number):
short_term = make_barcode(barCodeLength)
if short_term not in barcode_storage and gc_cont(short_term) * 100 < maxgc and gc_cont(
short_term) * 100 > mingc:
for bc in barcode_storage:
count = 0;
for pos in range(barCodeLength):
if short_term[pos] == bc[pos]:
count += 1
else:
pass
countlist += [count]
if len(countlist) != 0:
if max(countlist) < diffs:
barcode_storage += [short_term]
else:
while short_term in barcode_storage or gc_cont(short_term) * 100 > maxgc == True or gc_cont(
short_term) * 100 < mingc == True:
position = rd.choice(range(barCodeLength))
short_term = short_term.replace(short_term[position], rd.choice(l1))
else:
barcode_storage += [short_term]
else:
barcode_storage += [short_term]
else:
while short_term in barcode_storage or gc_cont(short_term) * 100 > maxgc == True or gc_cont(
short_term) * 100 < mingc == True:
position = rd.choice(range(barCodeLength))
short_term = short_term.replace(short_term[position], rd.choice(l1))
else:
barcode_storage += [short_term]
return barcode_storage
###########################################################################################################################################################################
def oligosynthesizer(BackgroundS, minSpacing, maxSpacing, permutationsL, ReverseOrNot, windowSize=70):
middle_background = len(BackgroundS)/2
Identifier = []
Sequences = []
if BackgroundS != '' and not BackgroundS.startswith('>'):
maxSpacingForRange = int(maxSpacing)+1
for permutationL in permutationsL:
for distanceBetween1_2 in range(maxSpacingForRange):
for distanceBetween2_3 in range(maxSpacingForRange):
for distanceBetween3_4 in range(maxSpacingForRange):
motiflen = distanceBetween1_2 + distanceBetween2_3 + distanceBetween3_4
windowSize = min(windowSize, middle_background)
first_position = windowSize - motiflen/2
first_position_last = first_position + len(permutationL[0]) - 1
second_position = first_position_last + 1 + distanceBetween1_2
second_position_last = second_position + len(permutationL[1]) - 1
third_position = second_position_last + 1 + distanceBetween2_3
third_position_last = third_position + len(permutationL[2]) - 1
fourth_position = third_position_last + 1 + distanceBetween3_4
fourth_position_last = fourth_position + len(permutationL[3]) - 1
startingAndLastPositionsL = [(first_position, first_position_last), (second_position, second_position_last),
(third_position, third_position_last), (fourth_position, fourth_position_last)]
#for i in startingAndLastPositionsL: print "%s - %s" %(i[0], i[1])
# This code block is checking whether the motif distances are within the parameters
# minSpacing and maxSpacing. The next code takes care of this.
make_sequence_or_no = True
for index, item in enumerate(startingAndLastPositionsL[:-1]):
distanceBetweenMotifs = startingAndLastPositionsL[index+1][0] - item[1] - 1
if distanceBetweenMotifs < minSpacing or distanceBetweenMotifs > maxSpacing:
make_sequence_or_no = False
break
if make_sequence_or_no:
# Sequence = ''.join([BackgroundS[:startingPositionsL[0]], permutationL[0],
# BackgroundS[(startingPositionsL[0] + len(permutationL[0])):startingPositionsL[1]], permutationL[1],
# BackgroundS[(startingPositionsL[1] + len(permutationL[1])):startingPositionsL[2]], permutationL[2],
# BackgroundS[(startingPositionsL[2] + len(permutationL[2])):startingPositionsL[3]], permutationL[3],
# BackgroundS[startingPositionsL[3]:]])
BackgroundCopy = list(BackgroundS)
for index, (startingPosition, lastPosition) in enumerate(startingAndLastPositionsL):
if lastPosition < startingPosition:
pass
else:
BackgroundCopy[startingPosition: lastPosition] = permutationL[index]
Sequence = "".join(BackgroundCopy)
#pdb.set_trace()
if Sequence not in Sequences:
Sequences += [Sequence]
Identifier += [permutationL, distanceBetween1_2, distanceBetween2_3, distanceBetween3_4, ]
if ReverseOrNot == 'Yes': #ReverseComplement if YES to generate these sequences too
print "YESSSS THIS IS REERSE OR NOT YES"
ReverseComplementBackground = myf.revcompl(BackgroundS)
# the insertion begins.. WE EED TO FIX THIS BULSHIT
Sequence_ = ''.join([ReverseComplementBackground[:startingAndLastPositionsL[0][0]], permutationL[0],
ReverseComplementBackground[
(startingPositionsL[0][0] + len(permutationL[0])):startingPositionsL[1][0]], permutationL[1],
ReverseComplementBackground[
(startingPositionsL[1] + len(permutationL[1])):startingPositionsL[2]], permutationL[2],
ReverseComplementBackground[
(startingPositionsL[2] + len(permutationL[2])):startingPositionsL[3]], permutationL[3],
ReverseComplementBackground[startingPositionsL[3]:]])
Sequences += [Sequence_]
Identifier += [[permutationL, distanceBetween1_2, distanceBetween2_3, distanceBetween3_4, ]]
else:
print "The if statement is false that means the motif distances are not within the constraints."
storeID = []
Step = len(Identifier) / len(Sequences)
for distanceBetween1_2 in range(0, len(Identifier), Step):
storeID += [[Identifier[distanceBetween1_2 + distanceBetween3_4] for distanceBetween3_4 in range(Step)]]
return {"storeID": storeID, "Sequences": Sequences}
##############################################################################################################################################
#
def TotalSeq(Adaptors1, Restriction1, Restriction2, Sequences, barcode_storage, Adaptors2):
Synthetics = [Adaptors1 + Restriction1 + Sequences[i] + barcode_storage[i]+Restriction2 + Adaptors2]
return Synthetics
def Invalid_data(Sequences, Restriction):
for i in Sequences:
for j in Restriction:
match = myf.findMatch(i, j)
if match != {}:
print "for sequence", i, " and for Restriction Seq", Restriction[j], "and Position", match
def createMPRAResultOutput(finalOutput, numOfBarCodesPerSequence, barCodes, restriction1,
restriction2, adapter1, adapter2, ordering):
response = ""
sequenceHTMLL = []
for index, item in enumerate(finalOutput):
for numTimes in range(numOfBarCodesPerSequence):
outputHeader = item['header']
if barCodes:
outputHeader += "|BARCODE - " + str(numTimes+1)
if restriction1:
outputHeader += "|RESTRICTION - 1"
if restriction2:
outputHeader += "|RESTRICTION - 2"
if adapter1:
outputHeader += "|ADAPTER - 1"
if adapter2:
outputHeader += "|ADAPTER - 2"
outputSequence = ""
outputSequenceHTML = ""
for orderItem in ordering:
if adapter1 and orderItem == "adapter site 1":
outputSequence += adapter1
outputSequenceHTML += adapter1
if restriction1 and orderItem == "restriction site 1":
outputSequence += restriction1
outputSequenceHTML += restriction1
elif orderItem == "Background":
outputSequence += item['sequence']
if not item.get('sequenceHTML'):
outputSequenceHTML += item.get('sequence')
else:
outputSequenceHTML += item.get('sequenceHTML')
elif restriction2 and orderItem == "restriction site 2":
outputSequence += restriction2
outputSequenceHTML += restriction2
elif adapter2 and orderItem == "adapter site 2":
outputSequence += adapter2
outputSequenceHTML += adapter2
elif barCodes and orderItem == "Barcode":
outputSequence += barCodes[index * numOfBarCodesPerSequence + numTimes]
outputSequenceHTML += barCodes[index * numOfBarCodesPerSequence + numTimes]
outputSequence += "\n"
if restriction1:
#if outputSequence.lower().count(restriction1.lower()) > 1:
if len(myf.findMatch(sequenceS=outputSequence.lower(), motifS=restriction1.lower()) ) > 1:
outputHeader += "|DUPLICATE_RESTRICTION_SITES - RESTRICTION1"
if restriction2:
# if outputSequence.lower().count(restriction2.lower()) > 1:
if len(myf.findMatch(sequenceS=outputSequence.lower(), motifS=restriction2.lower()) ) > 1:
outputHeader += "|DUPLICATE_RESTRICTION_SITES - RESTRICTION2"
outputHeader += "\n"
response += outputHeader
response += outputSequence
sequenceHTMLL.append([outputHeader, outputSequenceHTML])
return response, sequenceHTMLL
def getBarCodes(barCodeLength, minimumGCContent, maximumGCContent, numOfBarCodesPerSequence, barCodeDistance, finalOutput):
"""
This returns the barcodes ( as a list of strings or just 'None').
:param barCodeLength:
:param minimumGCContent:
:param maximumGCContent:
:param numOfBarCodesPerSequence:
:param barCodeDistance:
:param finalOutput:
:return: a tuple of 2 elements. First element - barCodes. Second element - number of barcodes per sequence
"""
barCodes = None
# checking if the user wants to input barcodes.
if barCodeLength and minimumGCContent and maximumGCContent and numOfBarCodesPerSequence:
editdistance = 2
if barCodeDistance: editdistance = barCodeDistance
barCodes = barcode_generator(len(finalOutput) * numOfBarCodesPerSequence, minimumGCContent,
maximumGCContent, barCodeLength=barCodeLength, diffs=editdistance)
else:
numOfBarCodesPerSequence = 1 # NOTE THIS DOES NOT ACTUALLY MEAN 1 barcode per sequence. this
# else statement means 0 numOfBarCodesPerSequence.
return barCodes, numOfBarCodesPerSequence