-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_.py
221 lines (208 loc) · 11.7 KB
/
sim_.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
#Created by Alaa Abdel Latif on 25/08/2016.
#Copyright 2016 Alaa Abdel Latif. All rights reserved.
import sys
import gc
from Aptamers import Aptamers
from Selection import Selection
from Amplification import Amplification
from Mutation import Mutation
from postprocess import dataAnalysis
import utils
'''
aptamerType = str(sys.argv[1])
aptamerNum = int(sys.argv[2])
aptamerSeq = str(sys.argv[3])
seqLength = int(sys.argv[4])
selectionThreshold = long(sys.argv[5])
distanceMeasure = str(sys.argv[6])
roundNum = int(sys.argv[7])
pcrCycleNum = int(sys.argv[8])
pcrYield = float(sys.argv[9])
pcrErrorRate = float(sys.argv[10])
stringency = float(sys.argv[11])
samplingSize = int(sys.argv[12])
outputFileNames = str(sys.argv[13])
post_process = bool(sys.argv[14])
'''
#Fetch experiment parameters from the settings file
import ConfigParser
settings = ConfigParser.ConfigParser()
settings.read('settings.init')
aptamerType = settings.get('general','selex_type')
aptamerNum = settings.getint('general','aptamer_mode')
aptamerSeq = settings.get('general','reference_aptamer')
seqLength = settings.getint('general','sequence_length')
roundNum = settings.getint('general','number_of_rounds')
outputFileNames = settings.get('general','experiment_name')
samplingSize = settings.getint('general','sampling_size')
post_process = settings.get('general','post_process')
selectionThreshold = settings.getint('selectionparams','scale')
distanceMeasure = settings.get('selectionparams','distance')
stringency = settings.getint('selectionparams','stringency')
pcrCycleNum = settings.getint('amplificationparams','number_of_pcr')
pcrYield = settings.getfloat('amplificationparams','pcr_efficiency')
pcrerrorRate = settings.getfloat('amplificationparams','pcr_error_rate')
# Instantiating the appropriate classes
Apt = Aptamers()
S = Selection()
Amplify = Amplification()
Mut = Mutation()
if(distanceMeasure == "hamming"):
# SELEX simulation based on random aptamer assignment, hamming-based stochastic selection, and
# non-ideal stochastic amplfication with pyrimidine-based bias.
for r in range(roundNum):
if(r==0):
if(aptamerType == 'DNA'):
alphabetSet = 'ACGT'
aptamerSeqs, initialSeqNum = Apt.optimumAptamerGenerator(aptamerNum, alphabetSet, seqLength)
elif(aptamerType == 'RNA'):
alphabetSet = 'ACGU'
aptamerSeqs, initialSeqNum = Apt.optimumAptamerGenerator(aptamerNum, alphabetSet, seqLength)
else:
print("Error: Simulation of %.s aptamers not supported" %aptamerType)
break
if(aptamerNum == 0):
aptamerSeqs = aptamerSeq
print("optimum sequences have been chosen")
print("SELEX Round 1 has started")
print("total number of sequences in initial library = "+str(initialSeqNum))
slctdSeqs = S.stochasticHammingSelection_initial(alphabetSet, seqLength, aptamerSeqs, selectionThreshold, initialSeqNum, samplingSize, outputFileNames, r, stringency)
print("selection carried out for R0")
amplfdSeqs = Amplify.randomPCR_with_ErrorsAndBias_FASTv2(slctdSeqs, seqLength, pcrCycleNum, pcrYield, pcrErrorRate, aptamerSeqs, alphabetSet, distanceMeasure)
del(slctdSeqs)
gc.collect()
print("Amplification carried out for R1")
outFile = outputFileNames + "_R" + str(r+1)
nxtRnd = open(outFile, 'w')
print("writing R1 seqs to file")
for seqIdx in amplfdSeqs:
seq = Apt.pseudoAptamerGenerator(seqIdx, alphabetSet, seqLength)
nxtRnd.write(str(seq)+'\t'+str(int(amplfdSeqs[seqIdx][0]))+'\t'+str(int(amplfdSeqs[seqIdx][1]))+'\t'+'\n') #write seqIdx, count, distance, and bias...for now
nxtRnd.close()
else:
print("SELEX Round "+str(r+1)+" has started")
totalSeqNum, uniqSeqNum = utils.seqNumberCounter(amplfdSeqs)
print("total number of sequences in initial pool = "+str(totalSeqNum))
print("total number of unique sequences in initial pool = "+str(int(uniqSeqNum)))
slctdSeqs = S.stochasticHammingSelection(alphabetSet, seqLength, amplfdSeqs, selectionThreshold, uniqSeqNum, totalSeqNum, samplingSize, outputFileNames, r, stringency)
del(amplfdSeqs)
gc.collect()
print("Selection carried for R"+str(r+1))
amplfdSeqs = Amplify.randomPCR_with_ErrorsAndBias_FASTv2(slctdSeqs, seqLength, pcrCycleNum, pcrYield, pcrErrorRate, aptamerSeqs, alphabetSet, distanceMeasure)
del(slctdSeqs)
gc.collect()
print("Amplification carried for R"+str(r+1))
print("writing R"+str(r+1)+" seqs to file")
outFile = outputFileNames + "_R" + str(r+1)
with open(outFile, 'w') as f:
for seqIdx in amplfdSeqs:
seq = Apt.pseudoAptamerGenerator(seqIdx, alphabetSet, seqLength)
f.write(str(seq)+'\t'+str(int(amplfdSeqs[seqIdx][0]))+'\t'+str(int(amplfdSeqs[seqIdx][1]))+'\n')
print("SELEX completed")
elif(distanceMeasure == "basepair"):
# SELEX simulation based on random aptamer assignment, basepair-based stochastic selection, and
# non-ideal stochastic amplfication with pyrimidine-based bias.
for r in range(roundNum):
if(r==0):
if(aptamerType == 'DNA'):
alphabetSet = 'ACGT'
aptamerSeqs, initialSeqNum = Apt.optimumAptamerGenerator(aptamerNum, alphabetSet, seqLength)
elif(aptamerType == 'RNA'):
alphabetSet = 'ACGU'
aptamerSeqs, initialSeqNum = Apt.optimumAptamerGenerator(aptamerNum, alphabetSet, seqLength)
else:
print("Error: Simulation of %.s aptamers not supported" %aptamerType)
break
if(aptamerNum == 0):
aptamerSeqs = aptamerSeq
print("optimum sequences have been chosen")
print("SELEX Round 1 has started")
print("total number of sequences in initial library = "+str(initialSeqNum))
slctdSeqs = S.stochasticBasePairSelection_initial(alphabetSet, seqLength, aptamerSeqs, selectionThreshold, initialSeqNum, samplingSize, outputFileNames, r, stringency)
print("selection carried out for R0")
amplfdSeqs = Amplify.randomPCR_with_ErrorsAndBias_FASTv2(slctdSeqs, seqLength, pcrCycleNum, pcrYield, pcrErrorRate, aptamerSeqs, alphabetSet, distanceMeasure)
print("Amplification carried out for R1")
outFile = outputFileNames + "_R" + str(r+1)
nxtRnd = open(outFile, 'w')
print("writing R1 seqs to file")
for seqIdx in amplfdSeqs:
seq = Apt.pseudoAptamerGenerator(seqIdx, alphabetSet, seqLength)
nxtRnd.write(str(seq)+'\t'+str(int(amplfdSeqs[seqIdx][0]))+'\t'+str(int(amplfdSeqs[seqIdx][1]))+'\t'+'\n') #write seqIdx, count, distance, and bias...for now
nxtRnd.close()
else:
del(slctdSeqs)
print("SELEX Round "+str(r+1)+" has started")
totalSeqNum, uniqSeqNum = utils.seqNumberCounter(amplfdSeqs)
print("total number of sequences in initial pool = "+str(totalSeqNum))
print("total number of unique sequences in initial pool = "+str(int(uniqSeqNum)))
slctdSeqs = S.stochasticBasePairSelection(alphabetSet, seqLength, amplfdSeqs, selectionThreshold, uniqSeqNum, totalSeqNum, samplingSize, outputFileNames, r, stringency)
print("Selection carried for R"+str(r+1))
del(amplfdSeqs)
amplfdSeqs = Amplify.randomPCR_with_ErrorsAndBias_FASTv2(slctdSeqs, seqLength, pcrCycleNum, pcrYield, pcrErrorRate, aptamerSeqs, alphabetSet, distanceMeasure)
print("Amplification carried for R"+str(r+1))
outFile = outputFileNames + "_R" + str(r+1)
nxtRnd = open(outFile, 'w')
print("writing R"+str(r+1)+" seqs to file")
for seqIdx in amplfdSeqs:
seq = Apt.pseudoAptamerGenerator(seqIdx, alphabetSet, seqLength)
nxtRnd.write(str(seq)+'\t'+str(int(amplfdSeqs[seqIdx][0]))+'\t'+str(int(amplfdSeqs[seqIdx][1]))+'\n') #write idx and count for now
nxtRnd.close()
print("SELEX completed")
elif(distanceMeasure == "loop"):
# SELEX simulation based on random aptamer assignment, motif-based stochastic selection, and
# non-ideal stochastic amplfication with pyrimidine-based bias.
for r in range(roundNum):
if(r==0):
if(aptamerType == 'DNA'):
alphabetSet = 'ACGT'
aptamerSeqs, initialSeqNum = Apt.optimumAptamerGenerator(aptamerNum, alphabetSet, seqLength)
elif(aptamerType == 'RNA'):
alphabetSet = 'ACGU'
aptamerSeqs, initialSeqNum = Apt.optimumAptamerGenerator(aptamerNum, alphabetSet, seqLength)
else:
print("Error: Simulation of %.s aptamers not supported" %aptamerType)
break
if(aptamerNum == 0):
aptamerSeqs = aptamerSeq
print("optimum sequences have been chosen")
print("SELEX Round 1 has started")
print("total number of sequences in initial library = "+str(initialSeqNum))
slctdSeqs = S.stochasticLoopSelection_initial(alphabetSet, seqLength, aptamerSeqs, selectionThreshold, initialSeqNum, samplingSize, outputFileNames, r, stringency)
print("selection carried out for R0")
amplfdSeqs = Amplify.randomPCR_with_ErrorsAndBias_FASTv2(slctdSeqs, seqLength, pcrCycleNum, pcrYield, pcrErrorRate, aptamerSeqs, alphabetSet, distanceMeasure)
print("Amplification carried out for R1")
outFile = outputFileNames + "_R" + str(r+1)
nxtRnd = open(outFile, 'w')
print("writing R1 seqs to file")
for seqIdx in amplfdSeqs:
seq = Apt.pseudoAptamerGenerator(seqIdx, alphabetSet, seqLength)
nxtRnd.write(str(seq)+'\t'+str(int(amplfdSeqs[seqIdx][0]))+'\t'+str(int(amplfdSeqs[seqIdx][1]))+'\t'+'\n') #write seqIdx, count, distance, and bias...for now
nxtRnd.close()
else:
del(slctdSeqs)
print("SELEX Round "+str(r+1)+" has started")
totalSeqNum, uniqSeqNum = utils.seqNumberCounter(amplfdSeqs)
print("total number of sequences in initial pool = "+str(totalSeqNum))
print("total number of unique sequences in initial pool = "+str(int(uniqSeqNum)))
slctdSeqs = S.stochasticLoopSelection(alphabetSet, seqLength, amplfdSeqs, selectionThreshold, uniqSeqNum, totalSeqNum, samplingSize, outputFileNames, r, stringency)
print("Selection carried for R"+str(r+1))
del(amplfdSeqs)
amplfdSeqs = Amplify.randomPCR_with_ErrorsAndBias_FASTv2(slctdSeqs, seqLength, pcrCycleNum, pcrYield, pcrErrorRate, aptamerSeqs, alphabetSet, distanceMeasure)
print("Amplification carried for R"+str(r+1))
outFile = outputFileNames + "_R" + str(r+1)
nxtRnd = open(outFile, 'w')
print("writing R"+str(r+1)+" seqs to file")
for seqIdx in amplfdSeqs:
seq = Apt.pseudoAptamerGenerator(seqIdx, alphabetSet, seqLength)
nxtRnd.write(str(seq)+'\t'+str(int(amplfdSeqs[seqIdx][0]))+'\t'+str(int(amplfdSeqs[seqIdx][1]))+'\n') #write idx and count for now
nxtRnd.close()
print("SELEX completed")
else:
print("Invalid argument for distance measure")
if(post_process==True):
print("Data post-processing has started...")
dataAnalysis(seqLength, roundNum, outputFileNames, post_process, distanceMeasure)
print("Data post-processing is complete")
print("The Simulation has ended")
else:
print("The Simulation has ended without post-processing")