-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRNAfoldsimulation.py
More file actions
362 lines (305 loc) · 11.6 KB
/
RNAfoldsimulation.py
File metadata and controls
362 lines (305 loc) · 11.6 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import sys
import subprocess
from Bio import SeqIO
import random
import argparse
def parseRNAfold(structure):
#This function isn't actually used. It's been replaced by parseRNAfold2.
RNAlength = len(structure)
leftfacing = [] #indexes of left facing parentheses
rightfacing = [] #indexes of right facing parentheses
pairing = {} # {upstream base : downstream base it's paired to}
#Get positions of parentheses
for i, x in enumerate(structure):
if x == '(':
rightfacing.append(i + 1) #There is no base 0
elif x == ')':
leftfacing.append(i + 1)
#Pair the left and right facing parentheses
currentrights = []
currentlefts = []
currentdirection = 'right'
for i in range(RNAlength):
if i + 1 in rightfacing and currentdirection == 'right':
currentrights.append(i + 1)
if i + 1 in leftfacing:
currentdirection = 'left'
currentlefts.append(i + 1)
if (i + 1 in rightfacing and currentdirection == 'left') or i + 1 == RNAlength:
#Pair the current parenthesis
if len(currentrights) != len(currentlefts):
print 'ERROR!!! Number of left base pairs does not equal number of right base pairs!'
print currentrights, currentlefts
for j in range(len(currentrights)):
leftbase = currentrights[j]
rightbase = currentlefts[-1 - j]
pairing[leftbase] = rightbase
pairing[rightbase] = leftbase
currentdirection = 'right' #Reset direction
currentrights = [i + 1]
currentlefts = []
i = i - 1 #We will need to redo this in in the next round of the for loop
#Any base that is not paired is now paired to 0
for i in range(RNAlength):
if i + 1 not in pairing:
pairing[i + 1] = 0
print pairing
return pairing
def parseRNAfold2(structure):
#Switchpoints are when you switch from being a leftstem ('(') to a right stem (')')
#This function can handle a structure with multiple stems, which parseRNAfold cannot.
switchpoints = [0] #always have a switchpoint at the very beginning
stemleft = []
stemright = []
pairing = {}
for i, x in enumerate(structure):
if x == '(':
stemleft.append(i + 1) #There is no base 0
elif x == ')':
stemright.append(i + 1)
stemside = 'left'
for i, x in enumerate(structure):
if x == ')' and stemside == 'left':
switchpoints.append(i) #This is the (1-based) base just upstream of the first ')' (i starts at 0)
stemside = 'right'
if x == '(':
stemside = 'left'
switchpoints.append(len(structure) + 1) #always have a switchpoint be after the last base
for ind, switchpoint in enumerate(switchpoints):
if switchpoint == 0:
continue
if switchpoint > len(structure):
break
leftstemsbeforeswitchpoint = []
rightstemsafterswitchpoint = []
#Get right stems after switchpoint but before next switchpoint
for i in range(switchpoint, switchpoints[ind + 1]):
if i in stemright:
rightstemsafterswitchpoint.append(i)
#Get left stems before switchpoint, including any leftovers
for j in range(1, switchpoint):
if j in stemleft:
leftstemsbeforeswitchpoint.append(j)
if len(leftstemsbeforeswitchpoint) > len(rightstemsafterswitchpoint):
for k in range(len(rightstemsafterswitchpoint)):
rightbase = rightstemsafterswitchpoint[k]
leftbase = leftstemsbeforeswitchpoint[-1 - k]
pairing[rightbase] = leftbase
pairing[leftbase] = rightbase
stemleft.remove(leftbase)
stemright.remove(rightbase)
elif len(leftstemsbeforeswitchpoint) < len(rightstemsafterswitchpoint):
for n in range(len(leftstemsbeforeswitchpoint)):
rightbase = rightstemsafterswitchpoint[n]
leftbase = leftstemsbeforeswitchpoint[-1 - n]
pairing[rightbase] = leftbase
pairing[leftbase] = rightbase
stemleft.remove(leftbase)
stemright.remove(rightbase)
elif len(leftstemsbeforeswitchpoint) == len(rightstemsafterswitchpoint):
for p in range(len(leftstemsbeforeswitchpoint)):
rightbase = rightstemsafterswitchpoint[p]
leftbase = leftstemsbeforeswitchpoint[-1 - p]
pairing[rightbase] = leftbase
pairing[leftbase] = rightbase
stemleft.remove(leftbase)
stemright.remove(rightbase)
#Any base that is not paired is now paired to 0
for i in range(len(structure)):
if i + 1 not in pairing:
pairing[i + 1] = 0
return pairing
def comparepairing(structureA, structureB):
#Compare to "pairing" dicts to see how many base pairs are different
#Different means they are paired to some other base, or are paired/unpaired if they were unpaired/paired before
diff = {} # {base : 1 if different, 0 if not}
for base in structureA:
partnerA = structureA[base]
partnerB = structureB[base]
if partnerA == partnerB:
diff[base] = 0
elif partnerA != partnerB:
diff[base] = 1
return diff
def comparestructuretoprobs(structureA, probsB):
#Given a pairing dict from a MFE structure (i.e. the MFE of the original sequence)
diff = {} #{base : 1 if different, 0 if not}
for base in structureA:
partnerA = structureA[base]
partnerB = probsB[base]
if partnerA == 0:
if not partnerB:
diff[base] = 0
elif partnerB:
diff[base] = 1
elif partnerA != 0:
if partnerA in partnerB:
diff[base] = 0
elif partnerA not in partnerB:
diff[base] = 1
return diff
def updatediff(currentdiff, difftoadd):
#Update the diff dictionary with new values
updateddiff = {}
for base in currentdiff:
currentvalue = currentdiff[base]
valuetoadd = difftoadd[base]
updateddiff[base] = currentvalue + valuetoadd
return updateddiff
def getstructure(seq):
#Given a sequence, return it's structure in parentheses format
command = 'RNAfold'
job = subprocess.Popen(command, shell=True, stdout = subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
job.stdin.write(seq)
output = job.communicate()
return str(output[0].split('\n')[1].split(' ')[0])
def getbpprobs(seq):
#Given a sequence, make bp probabilities and return every pair that has a probability of at least <filter>
pairs = {} # {base : [list of bp it is paired to with at least a minimum probability]}
probfilter = 0.5
#Populate dictionary
for i in range(len(seq)):
pairs[i + 1] = []
command = 'RNAfold -p'
job = subprocess.Popen(command, shell=True, stdout = subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
job.stdin.write(seq)
output = job.communicate()
structure = str(output[0].split('\n')[1].split(' ')[0])
print structure
bpfh = open('dot.ps', 'r')
#lines containing bp probs have 'ubox' in the 4th field
for line in bpfh:
line = line.strip().split(' ')
if len(line) != 4:
continue
if line[3] == 'ubox':
leftbase = int(line[0])
rightbase = int(line[1])
bpprob = float(line[2])
if bpprob >= probfilter:
pairs[leftbase].append(rightbase)
pairs[rightbase].append(leftbase)
bpfh.close()
return pairs
def makeoriginalstructure(originalfasta):
#Make the diff dict of the sequence you want to compare everything to
for record in SeqIO.parse(originalfasta, 'fasta'):
seq = str(record.seq)
structure = getstructure(seq)
originalpairs = parseRNAfold2(structure)
return originalpairs
def compareseqs(fasta, seqlengths, originalpairs, outputfile):
#Fasta = fasta of all mutagenized sequences
#seqlengths = length of all fasta sequences (must all be the same)
#originalpairs = structure of your standard seq to compare to (from makeoriginalstructure)
#First make the starting diff dictionary
startingdiff = {}
for i in range(seqlengths):
startingdiff[i + 1] = 0
counter = 0
#Now go through each sequence in the fasta
for record in SeqIO.parse(fasta, 'fasta'):
print 'Folding {0}...'.format(record.id)
counter += 1
if counter % 1000 == 0:
print 'Folding sequence {0}...'.format(counter)
seq = str(record.seq.transcribe())
structure = getstructure(seq)
print record.id, structure
pairs = parseRNAfold2(structure)
difftoadd = comparepairing(originalpairs, pairs)
if counter == 1:
updateddiff = updatediff(startingdiff, difftoadd)
elif counter >1:
updateddiff = updatediff(updateddiff, difftoadd)
outfh = open(outputfile, 'w')
outfh.write('base' + '\t' + 'structure_changes' + '\t' + 'original' + '\n')
for base in sorted(updateddiff):
if originalpairs[base] == 0: #unpaired in original
original = '.'
elif originalpairs[base] != 0: #paired in original
original = '|'
outfh.write(str(base) + '\t' + str(updateddiff[base]) + '\t' + original + '\n')
outfh.close()
def compareseqs_probs(fasta, seqlengths, originalpairs, outputfile):
#Fasta = fasta of all mutagenized sequences
#seqlengths = length of all fasta sequences (must all be the same)
#originalpairs = structure of your standard seq to compare to (from makeoriginalstructure)
#First make the starting diff dictionary
startingdiff = {}
for i in range(seqlengths):
startingdiff[i + 1] = 0
counter = 0
#Not go through each sequence in the fasta
for record in SeqIO.parse(fasta, 'fasta'):
print 'Folding {0}...'.format(record.id)
counter += 1
if counter % 1000 == 0:
print 'Folding sequence {0}...'.format(counter)
seq = str(record.seq.transcribe())
probs = getbpprobs(seq)
difftoadd = comparestructuretoprobs(originalpairs, probs)
if counter == 1:
updateddiff = updatediff(startingdiff, difftoadd)
elif counter > 1:
updateddiff = updatediff(updateddiff, difftoadd)
outfh = open(outputfile, 'w')
outfh.write('base' + '\t' + 'structure_changes' + '\t' + 'original' + '\n')
for base in sorted(updateddiff):
if originalpairs[base] == 0: #unpaired in original
original = '.'
elif originalpairs[base] != 0: #paired in original
original = '|'
outfh.write(str(base) + '\t' + str(updateddiff[base]) + '\t' + original + '\n')
outfh.close()
def makemutatedfasta(inputfasta, iterations):
mutdict = {'A' : ['C','U','G'], 'G' : ['A','U','C'], 'C' : ['A','U','G'], 'U' : ['A','C','G'], 'T' : ['A','C','G']}
lottery = []
outfh = open('MutatedSequences.fasta', 'w')
for i in range(1000):
lottery.append(i + 1)
#Get the original sequence
for record in SeqIO.parse(inputfasta, 'fasta'):
seq = str(record.seq)
for i in range(int(iterations)):
if (i + 1) % 10000 == 0:
print 'Creating mutated sequence {0} of {1}...'.format(i + 1, iterations)
seqtitle = 'Seq' + str(i + 1)
currentseq = ''
for nt in seq:
ticket = random.choice(lottery)
if ticket <= 949:
currentseq += nt
elif ticket > 949 and ticket <= 966:
mutation = mutdict[nt][0]
currentseq += mutation
elif ticket > 966 and ticket <= 983:
mutation = mutdict[nt][1]
currentseq += mutation
elif ticket > 983 and ticket <= 1000:
mutation = mutdict[nt][2]
currentseq += mutation
outfh.write('>' + seqtitle + '\n' + currentseq + '\n')
print 'Done making mutated fasta!!'
outfh.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--originalsequence', type = str, help = 'Original sequence in fasta format.')
parser.add_argument('--iterations', type = int, help = 'Number of mutated sequences to make.')
parser.add_argument('--mode', choices = ['MFE','prob'], type = str, help = 'Compare MFE to MFE or MFE of original sequence to probs in mutated sequences?')
parser.add_argument('--output', type = str, help = 'Output file of structure changes.')
args = parser.parse_args()
#Get length of original sequence
for record in SeqIO.parse(args.originalsequence, 'fasta'):
seqlength = len(str(record.seq))
#Make mutated fasta
print 'Making mutated sequences...'
makemutatedfasta(args.originalsequence, args.iterations)
#Define basepairs in original fasta
print 'Defining basepairs in original sequence...'
originalpairs = makeoriginalstructure(args.originalsequence)
if args.mode == 'MFE':
#Fold and compare mutated sequences
compareseqs('MutatedSequences.fasta', int(seqlength), originalpairs, args.output)
elif args.mode == 'prob':
compareseqs_probs('MutatedSequences.fasta', int(seqlength), originalpairs, args.output)