-
Notifications
You must be signed in to change notification settings - Fork 0
/
rnahamming3_17_15_2o.py
791 lines (603 loc) · 30.2 KB
/
rnahamming3_17_15_2o.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
#seq_file =
#"GGGGCCGTAGCTCAGCTGGGAGAGCACCTGCTTTGCAAGCAGGGGGTCGTCGGTTCGATCCCGTCCGGCTCCACCA"
#This one has 3bp long complement to find. Program will only fold
#4bp+ stretches now
#http://trna.bioinf.uni-leipzig.de/DataOutput/Result?ID=tdbD00003770
#alanine from Brucella melitensis
#seq_file = "GGGGGCUCUGUUGGUUCUCCCGCAACGCUACUCUGUUUACCAGGUCAGGUCCGGAAGGAAGCAGCCAAGGCAGAUGACGCGUGUGCCGGGAUGUAGCUGGCAGGGCCCCCACC"
#seq_file = 'GCCGGGCGCGGTGGCGCGTGCCTGTAGTCCCAGCTACTCGGGAGGCTGAGGCTGGAGGATCGCTTGAGTCCAGGAGTTCTGGGCTGTAGTGCGCTATGCCGATCGGGTGTCCGCACTAAGTTCGGCATCAATATGGTGACCTCCCGGGAGCGGGGGACCACCAGGTTGCCTAAGGAGGGGTGAACCGGCCCAGGTCGGAAACGGAGCAGGTCAAAACTCCCGTGCTGATCAGTAGTGGGATCGCGCCTGTGAATAGCCACTGCACTCCAGCCTGGGCAACATAGCGAG'
#http://www.ncbi.nlm.nih.gov/nuccore/527047183?report=fasta
#seq_file = "AAATTGAAGAGTTTGATCATGGCTCAGATTGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAACGGTAACAGGAAGAAGCTTGCTCTTTGCTGACGAGTGGCGGACGGGTGAGTAATGTCTGGGAAACTGCCTGATGGAGGGGGATAACTACTGGAAACGGTAGCTAATACCGCATAACGTCGCAAGACCAAAGAGGGGGACCTTCGGGCCTCTTGCCATCGGATGTGCCCAGATGGGATTAGCTAGTAGGTGGGGTAACGGCTCACCTAGGCGACGATCCCTAGCTGGTCTGAGAGGATGACCAGCCACACTGGAACTGAGACACGGTCCAGACTCCTACGGGAGGCAGCAGTGGGGAATATTGCACAATGGGCGCAAGCCTGATGCAGCCATGCCGCGTGTATGAAGAAGGCCTTCGGGTTGTAAAGTACTTTCAGCGGGGAGGAAGGGAGTAAAGTTAATACCTTTGCTCATTGACGTTACCCGCAGAAGAAGCACCGGCTAACTCCGTGCCAGCAGCCGCGGTAATACGGAGGGTGCAAGCGTTAATCGGAATTACTGGGCGTAAAGCGCACGCAGGCGGTTTGTTAAGTCAGATGTGAAATCCCCGGGCTCAACCTGGGAACTGCATCTGATACTGGCAAGCTTGAGTCTCGTAGAGGGGGGTAGAATTCCAGGTGTAGCGGTGAAATGCGTAGAGATCTGGAGGAATACCGGTGGCGAAGGCGGCCCCCTGGACGAAGACTGACGCTCAGGTGCGAAAGCGTGGGGAGCAAACAGGATTAGATACCCTGGTAGTCCACGCCGTAAACGATGTCGACTTGGAGGTTGTGCCCTTGAGGCGTGGCTTCCGGAGCTAACGCGTTAAGTCGACCGCCTGGGGAGTACGGCCGCAAGGTTAAAACTCAAATGAATTGACGGGGGCCCGCACAAGCGGTGGAGCATGTGGTTTAATTCGATGCAACGCGAAGAACCTTACCTGGTCTTGACATCCACGGAAGTTTTCAGAGATGAGAATGTGCCTTCGGGAACCGTGAGACAGGTGCTGCATGGCTGTCGTCAGCTCGTGTTGTGAAATGTTGGGTTAAGTCCCGCAACGAGCGCAACCCTTATCCTTTGTTGCCAGCGGTCCGGCCGGGAACTCAAAGGAGACTGCCAGTGATAAACTGGAGGAAGGTGGGGATGACGTCAAGTCATCATGGCCCTTACGACCAGGGCTACACACGTGCTACAATGGCGCATACAAAGAGAAGCGACCTCGCGAGAGCAAGCGGACCTCATAAAGTGCGTCGTAGTCCGGATTGGAGTCTGCAACTCGACTCCATGAAGTCGGAATCGCTAGTAATCGTGGATCAGAATGCCACGGTGAATACGTTCCCGGGCCTTGTACACACCGCCCGTCACACCATGGGAGTGGGTTGCAAAAGAAGTAGGTAGCTTAACCTTCGGGAGGGCGCTTACCACTTTGTGATTCATGACTGGGGTGAAGTCGTAACAAGGTAACCGTAGGGGAACCTGCGGTTGGATCACCTCCTTA"
#http://rna.ucsc.edu/rnacenter/xrna/xrna_gallery.html
#http://www.ebi.ac.uk/ena/data/view/A14565&display=fasta
#http://www.arb-silva.de/browser/ssu-121/silva/CP001368
#currently reliably(?) finding hairpin turn ends
seq_file = "AAATTGAAGAGTTTGATCATGGCTCAGATTGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAACGGTAACAGGAAGAAGCTTGCTTCTTTGCTGACGAGTGGCGGACGGGTGAGTAATGTCTGGGAAACTGCCTGATGGAGGGGGATAACTACTGGAAACGGTAGCTAATACCGCATAACGTCGCAAGACCAAAGAGGGGGACCTTCGGGCCTCTTGCCATCGGATGTGCCCAGATGGGATTAGCTAGTAGGTGGGGTAACGGCTCACCTAGGCGACGATCCCTAGCTGGTCTGAGAGGATGACCAGCCACACTGGAACTGAGACACGGTCCAGACTCCTACGGGAGGCAGCAGTGGGGAATATTGCACAATGGGCGCAAGCCTGATGCAGCCATGCCGCGTGTATGAAGAAGGCCTTCGGGTTGTAAAGTACTTTCAGCGGGGAGGAAGGGAGTAAAGTTAATACCTTTGCTCATTGACGTTACCCGCAGAAGAAGCACCGGCTAACTCCGTGCCAGCAGCCGCGGTAATACGGAGGGTGCAAGCGTTAATCGGAATTACTGGGCGTAAAGCGCACGCAGGCGGTTTGTTAAGTCAGATGTGAAATCCCCGGGCTCAACCTGGGAACTGCATCTGATACTGGCAAGCTTGAGTCTCGTAGAGGGGGGTAGAATTCCAGGTGTAGCGGTGAAATGCGTAGAGATCTGGAGGAATACCGGTGGCGAAGGCGGCCCCCTGGACGAAGACTGACGCTCAGGTGCGAAAGCGTGGGGAGCAAACAGGATTAGATACCCTGGTAGTCCACGCCGTAAACGATGTCGACTTGGAGGTTGTGCCCTTGAGGCGTGGCTTCCGGAGCTAACGCGTTAAGTCGACCGCCTGGGGAGTACGGCCGCAAGGTTAAAACTCAAATGAATTGACGGGGGCCCGCACAAGCGGTGGAGCATGTGGTTTAATTCGATGCAACGCGAAGAACCTTACCTGGTCTTGACATCCACGGAAGTTTTCAGAGATGAGAATGTGCCTTCGGGAACCGTGAGACAGGTGCTGCATGGCTGTCGTCAGCTCGTGTTGTGAAATGTTGGGTTAAGTCCCGCAACGAGCGCAACCCTTATCCTTTGTTGCCAGCGGTCCGGCCGGGAACTCAAAGGAGACTGCCAGTGATAAACTGGAGGAAGGTGGGGATGACGTCAAGTCATCATGGCCCTTACGACCAGGGCTACACACGTGCTACAATGGCGCATACAAAGAGAAGCGACCTCGCGAGAGCAAGCGGACCTCATAAAGTGCGTCGTAGTCCGGATTGGAGTCTGCAACTCGACTCCATGAAGTCGGAATCGCTAGTAATCGTGGATCAGAATGCCACGGTGAATACGTTCCCGGGCCTTGTACACACCGCCCGTCACACCATGGGAGTGGGTTGCAAAAGAAGTAGGTAGCTTAACCTTCGGGAGGGCGCTTACCACTTTGTGATTCATGACTGGGGTGAAGTCGTAACAAGGTAACCGTAGGGGAACCTGCGGTTGGATCACCTCCTTA" #
#seq_file = "GAAGTCGTAACAAGGTAACCGTAGGGGAACCTGCGGTTGGATCACCTCCT"
#seq_file = seq_file[0:500]
from Bio.Seq import Seq
from collections import defaultdict
# new strategy: get nuclei
# built out with Ns for shorter segments
# of the secondary folds, select the one closest to the ends of the primary fold
#true fold
#[['GAGTAAAG', 24, 32, 'CTTTGCTC', 39, 47], ['GCGGG', 11, 16, 'CCCGC', 57, 62], ['TTTC', 6, 10,
##def reduceloop(target, s): #functional
##
## #consider a formed helix as a single nt long position
## #also reduces full length of s so that only regions local to the existing fold are considered
## #s_reduced = s[0:target[1]+1] + "-" + s[target[5]:len(s) + 1]
## new_s = s[target[1]-12:target[1]] + "-" + s[target[5]:target[5]+12]
##
## s_reduced = new_s
## #print s[target[1]-25:target[5]+25]
## #return s_reduced
def piece_inhelix(target, s, size, extend): #make pieces in local regions surrounding a previously formed helix.
#it's not making pieces in the right places
lefthalf = []
righthalf=[]
pieces =[]
maxlen = len(s)
begin = target[1]-extend+1
leftmiddle = target[1]
rightmiddle = target[5]
end = target[5]+extend +2
if begin == leftmiddle:
return pieces
if rightmiddle + size >= end:
return pieces
if maxlen > end:
maxlen = end
length = leftmiddle-begin
j = begin
if j < 0 :
j = 0
while j <(leftmiddle-3):
piece = s[j:j+size]
if len(piece)<3:
break
else:
lefthalf.append([piece, j, j+size])
j = j + 1
j = rightmiddle
while j<(end-size):
piece = s[j:j+size]
if len(piece)<3:
break
else:
righthalf.append([piece, j, j+size])
j = j + 1
#print target
pieces = [lefthalf] + [righthalf]
#print pieces
return pieces
def target_extend_outwards2(target, s): # not used ever... get rid of?
#extend unbroken hairpin seeds that fall outside of the initial 32nt long segments(2 10nt matches+ 11nt max loop)
#add on outer end until hamming threshold is reached
#this will be a rare event
if target[1] == 0:
return target
if target[5] ==len(s):
return target
targetA = target[0:3]
targetB = target[3:6]
leftbound = targetA[2]
rightbound = targetB[1]
hamm = 0
while hamm == 0:
tmptargetA = [s[targetA[1]-1:leftbound], targetA[1]-1, leftbound]
tmptargetB = [s[rightbound:targetB[2]+1], rightbound, targetB[2]+1]
hamm = rna_hamming(tmptargetA[0], tmptargetB[0])
if hamm!= 0:
return (targetA + targetB)
else:
targetA=tmptargetA
targetB = tmptargetB
if targetA[1] == 0 or targetB[2] == len(s):
#print "end targetA {0}, and targetB {1}".format(targetA, targetB)
return (targetA + targetB)
#print "end targeta {0}, and targetB {1}".format(targetA, targetB)
return (targetA + targetB)
def exclude_match(targets):
mindistance = 100
target = []
for candidate in targets:
current_distance = candidate[4]-candidate[2]
if current_distance < mindistance:
mindistance = current_distance
target = [candidate]
return target
def piece_match2(pieces):
targets =[]
print pieces
if pieces:
lefthalf = pieces[0]
righthalf = pieces [1]
for left in lefthalf:
for right in righthalf:
if len(left[0]) ==len(right[0]):
if rna_hamming(left[0], right[0]) == 0:
target = left + right
print target
print "extend out"
target = target_extend_outwards(target, s) #FIX HERE#it's not extending far enough?
#pair matching is off. Need to redo architecture to add on one base at a time,...
print target
targets.append(target)
target = exclude_match(targets)
return target
def rna_hamming(s1,s2): #sequence 1, sequence 2
#matches G's to T's/U's, A's to T's/U's, and G's to C's
#A hamming distance function that returns the hamming distance of the match between two ssRNA sequences.
#A hamming distance of 0 means a perfect match
s1rev = str(Seq(s1).reverse_complement())
the_zip = zip(s1rev, s2, s1[::-1])
hamming = 0
for charA, charB, charC in the_zip:
if charA == charB:
hamming = hamming + 0
elif charA == "C" and charB == "T" and charC == "G":
hamming = hamming + 0
elif charA == "A" and charB == "G" and charC == "T":
hamming = hamming + 0
elif charA == "C" and charB == "U" and charC == "G":
hamming = hamming + 0
elif charA == "A" and charB == "G" and charC == "U":
hamming = hamming + 0
elif charA == "N" or charB =="N" or charC == "N":
hamming = hamming + 0
else:
hamming = hamming + 1
return hamming
def seed_make(pieces, s): # working completely
targets =[]
#print str(pieces)
pieces_copy = pieces
for piece in pieces:
for piece_copy in pieces_copy:
if piece[2] < piece_copy[1] and 3<(piece_copy[1]-piece[2])<17:
#if piece[1] > seqlength:
# piece[1] = piece[1]-seqlength
if len(piece[0]) ==len(piece_copy[0]):
if rna_hamming(piece[0], piece_copy[0]) == 0:
range1=set(range(piece[1],piece[2]))
range2=set(range(piece_copy[1], piece_copy[2]))
if not range1.intersection(range2):
target = piece + piece_copy #the zero is the gap penalty
#print target
#print "target: {0}".format(target)
#print "before extend " + str(target)
target = target_extend_inwards(target, s)
#print "after inwards " + str(target)
target = target_extend_outwards(target, s)
#print "after outwards " + str(target)
if len(target[0])==2:
if (target[4]-target[2])>5: #2bp seeds can't bound large hairpin loops
continue
if (target[4]-target[2])>9:
if len(target[0])<6:
continue
#print "after outwards " + str(target)
if not target in targets:
target.append(target[0])
target.append(target[3])
targets.append(target)
#
return targets
def piece_maker(s, size): #used to fragment sequence
pieces =[]
s = s# + s[0:biggest]
length = len(s)
j = 0 #beginning of piece
end = length-1 #end of piece
k=j+size
while j<length-size:
while k > j+size-2:
piece = s[j:k+1]
if piece.isalpha(): #for later when sequences with dashes are run through
pieces.append([piece, j, k+1]) # all the pieces with positional information #the positional tracking is off)
k = k - 1
#print k
j = j + 1
if j==length-size and j!=length:
size = size -1
k=j+size
#print pieces
return pieces
def target_extend_outwards(target, s): # used just for the initial seed
#add on outer end until hamming threshold is reached
if target[1] == 0:
return target
if target[5] ==len(s):
return target
targetA = target[0:3]
targetB = target[3:6]
leftbound = targetA[2]
rightbound = targetB[1]
hamm = 0
while hamm == 0:
tmptargetA = [s[targetA[1]-1:leftbound], targetA[1]-1, leftbound]
#print "TMP A" + str(tmptargetA)
tmptargetB = [s[rightbound:targetB[2]+1], rightbound, targetB[2]+1]
#print "TMP B" + str(tmptargetB)
hamm = rna_hamming(tmptargetA[0], tmptargetB[0])
if hamm!= 0:
return (targetA + targetB)
else:
targetA=tmptargetA
targetB = tmptargetB
if targetA[1] == 0 or targetB[2] == len(s):
#print "end targetA {0}, and targetB {1}".format(targetA, targetB)
return (targetA + targetB)
#print "end targeta {0}, and targetB {1}".format(targetA, targetB)
return (targetA + targetB)
def target_extend_inwards(target, s):
#add on internally until hamming threshold is reached
if target[4]-target[2] < 4: # this is never happening
print "target already less than 4"
return target
targetA = target[0:3]
targetB = target[3:6]
leftbound = targetA[1]
rightbound = targetB[2]
hamm = 0
while hamm==0 and (targetB[1] - targetA[2]) > 4:
tmptargetA = [s[leftbound:targetA[2]+1], leftbound, targetA[2]+1]
tmptargetB = [s[targetB[1]-1:rightbound], targetB[1]-1, rightbound]
hamm = rna_hamming(tmptargetA[0], tmptargetB[0])
if hamm!=0 or (tmptargetB[1] - tmptargetA[2]) < 4:
return (targetA + targetB)
else:
targetA=tmptargetA
targetB = tmptargetB
#print "end targeta {0}, and targetB {1}".format(targetA, targetB)
return (targetA + targetB )
def closerange_target_extend(target,s, order): #index tracking is off on both sides, but offsets are working.
#print "enter closerange"
#print "target: {0}".format(target)
if target[1] <= 4:
return target
if target[5] >=len(s)-2:
return target
#gap = target[6]
tmptargetA = target[0:3]
tmptargetB = target[3:6]
leftbound = tmptargetA[2]
rightbound = tmptargetB[1]
hamm = 0
count = 1
testcycles = 0
#extension_order = [[0, 1], [1, 0], [1, 1], [1, 2], [2, 1], [0,2], [2, 0],[2, 2]] #02, 20 come after 12, 21
if order == "long":
extension_order = ['00', '10', '01', '11', '02', '20', '22', '03', '30', '04', '40', '05', '50', '06', '60', '21', '12','31', '13', '23', '32', '33', '42', '24', '43', '34', '44', '52', '25', '53', '35', '54', '45', '55', '63', '36', '64', '46', '65', '56', '66', '74', '47', '75', '57', '76', '67', '77', '87', '78', '88', '89', '98', '99']
setthatneeds4 = ['03', '30']
setthatneeds5 = ['04', '40', '05', '50', '06', '60']
if order == "short":
extension_order = ['00', '10', '01', '11', '21', '12', '02', '20', '22', '03', '30', '31', '13', '23', '32', '33', '42', '24','04', '40', '42', '24', '43', '34', '44', '52', '25', '53', '35', '05', '50', '06', '60', '54', '45', '55', '63', '36', '64', '46', '65', '56', '66', '74', '47', '75', '57', '76', '67', '77', '87', '78', '88', '89', '98', '99']
setthatneeds4 = ['03', '30']
setthatneeds5 = ['04', '40', '05', '50', '06', '60']
if order == "jump":
extension_order = ['00', '52', '25', '53', '35', '54', '45', '55', '63', '36', '65', '56', '64', '46', '66', '74', '47', '75', '57', '76', '67', '77', '87', '78', '88', '89', '98', '99', '10', '01', '11', '21', '12', '02', '20', '22', '03', '30', '31', '13', '23', '32', '33', '42', '24','04', '40', '42', '24', '43', '34', '44', '05', '50', '06', '60', ]
setthatneeds4 = ['03', '30']
setthatneeds5 = ['04', '40', '05', '50', '06', '60']
while testcycles <len(extension_order):
#print "testcycles: {0}".format(testcycles)
hamm = 0
extensionA= "placeholder"
extensionB= "placeholder"
extend = 1
extendAseed = int(extension_order[testcycles][:1])
extendBseed = int(extension_order[testcycles][-1])
count = 0
##print "enterhamming loop"
#print extension_order[testcycles]
while hamm == 0 and extensionA:
#current_gap = gap + max(extension_order[testcycles][0], extension_order[testcycles][1])
newtarget = [s[tmptargetA[1]-len(extensionA): leftbound], tmptargetA[1]-len(extensionA), leftbound, s[rightbound: tmptargetB[2]+ len(extensionB)], rightbound,tmptargetB[2]+len(extensionB)]
#print "running..." + str(count)
extensionA = s[tmptargetA[1]-extendAseed-extend:tmptargetA[1]]
extensionB = s[tmptargetB[2]:tmptargetB[2]+extendBseed+extend]
hamm = hamm + rna_hamming(extensionA[0:1], extensionB[-1])
#print "this went to hamm, extensionAbegin : {0}, extensionBend: {1}".format(extensionA[0:1], extensionB[-1])
#print "targetA, target B, extensionA, extentionB: {0}, {1}, {2}, {3}".format(tmptargetA, tmptargetB, extensionA, extensionB)
extend = extend + 1
count = count + 1
#print count
#print testcycles
if count<3:
testcycles = testcycles + 1
elif extension_order[testcycles] in setthatneeds4:
if count>4:
#print "found one in set that needs 4"
#print "about to be returned : {0}".format(newtarget)
dashedA = str(newtarget[0][0:count-1])+(extendAseed)*'-'+ str(target[6])
dashedB = str(target[7])+extendBseed*'-'+ str(newtarget[3][-(count-1):])
newtarget.append(dashedA)
newtarget.append(dashedB)
#print dashedA
#print dashedB
#print newtarget
#print '\n'
return newtarget
else:
testcycles = testcycles + 1
elif extension_order[testcycles] in setthatneeds5:
if count>5:
#print "found one in set that needs 5"
#print "about to be returned : {0}".format(newtarget)
dashedA = str(newtarget[0][0:count-1])+(extendAseed)*'-'+ str(target[6])
dashedB = str(target[7])+extendBseed*'-'+ str(newtarget[3][-(count-1):])
newtarget.append(dashedA)
newtarget.append(dashedB)
#print dashedA
#print dashedB
#print newtarget
#print '\n'
return newtarget
else:
testcycles = testcycles + 1
elif not extension_order[testcycles] in setthatneeds4:
if count>2:
#newtarget = [s[tmptargetA[1]-extendAseed -len(extensionA): leftbound], tmptargetA[1]-extendAseed -len(extensionA), leftbound, s[rightbound: tmptargetB[2]+extendBseed+ len(extensionB)], rightbound,tmptargetB[2]+extendBseed+ len(extensionB) ]
#print extension_order[testcycles]
#print "targetA, target B, extensionA, extentionB: {0}, {1}, {2}, {3}, {4}".format(tmptargetA, tmptargetB, extensionA, extensionB, testcycles)
#print "about to be returned : {0}, not in setthatneeds4".format(newtarget)
dashedA = str(newtarget[0][0:count-1])+(extendAseed)*'-'+ str(target[6])
dashedB = str(target[7])+extendBseed*'-'+ str(newtarget[3][-(count-1):])
newtarget.append(dashedA)
newtarget.append(dashedB)
#print dashedA
#print dashedB
#print newtarget
#print '\n'
return newtarget
return target
#return (tmptargetA + tmptargetB)
def end_arm_extension(target, s):
#print "enter closerange"
#print "target: {0}".format(target)
if target[1] <= 1:
return target
if target[5] >=len(s)-2:
return target
#gap = target[6]
tmptargetA = target[0:3]
tmptargetB = target[3:6]
leftbound = tmptargetA[2]
rightbound = tmptargetB[1]
hamm = 0
count = 1
testcycles = 0
extension_order = ['11']
while testcycles <1:
#print "testcycles: {0}".format(testcycles)
hamm = 0
extensionA= "placeholder"
extensionB= "placeholder"
extend = 1
extendAseed = int(extension_order[testcycles][:1])
extendBseed = int(extension_order[testcycles][-1])
count = 0
#print "enterhamming loop"
#print extension_order[testcycles]
while hamm == 0 and extensionA:
#current_gap = gap + max(extension_order[testcycles][0], extension_order[testcycles][1])
newtarget = [s[tmptargetA[1]-len(extensionA): leftbound], tmptargetA[1]-len(extensionA), leftbound, s[rightbound: tmptargetB[2]+ len(extensionB)], rightbound,tmptargetB[2]+len(extensionB)]
#print "running..." + str(count)
extensionA = s[tmptargetA[1]-extendAseed-extend:tmptargetA[1]]
extensionB = s[tmptargetB[2]:tmptargetB[2]+extendBseed+extend]
hamm = hamm + rna_hamming(extensionA[0:1], extensionB[-1])
#print "this went to hamm, extensionAbegin : {0}, extensionBend: {1}".format(extensionA[0:1], extensionB[-1])
#print "targetA, target B, extensionA, extentionB: {0}, {1}, {2}, {3}".format(tmptargetA, tmptargetB, extensionA, extensionB)
extend = extend + 1
count = count + 1
#print count
#print testcycles
if count>1:
#print "found one in set that needs 4"
#print "about to be returned : {0}".format(newtarget)
dashedA = str(newtarget[0][0:count-1])+(extendAseed)*'-'+ str(target[6])
dashedB = str(target[7])+extendBseed*'-'+ str(newtarget[3][-(count-1):])
newtarget.append(dashedA)
newtarget.append(dashedB)
#print dashedA
#print dashedB
#print newtarget
#print '\n'
return newtarget
else:
testcycles = testcycles + 1
return target
def small_hairpin_extension(target, s):
#print "enter closerange"
#print "target: {0}".format(target)
if target[1] <= 4:
return target
if target[5] >=len(s)-2:
return target
#gap = target[6]
tmptargetA = target[0:3]
tmptargetB = target[3:6]
leftbound = tmptargetA[2]
rightbound = tmptargetB[1]
hamm = 0
count = 1
testcycles = 0
extension_order = [ '00', '20', '02','01', '11', '11','12', '21'] #rearranged. May have to just break it up and run several times
while testcycles <len(extension_order):
#print "testcycles: {0}".format(testcycles)
hamm = 0
extensionA= "placeholder"
extensionB= "placeholder"
extend = 1
extendAseed = int(extension_order[testcycles][:1])
extendBseed = int(extension_order[testcycles][-1])
count = 0
##print "enterhamming loop"
#print extension_order[testcycles]
while hamm == 0 and extensionA:
#current_gap = gap + max(extension_order[testcycles][0], extension_order[testcycles][1])
newtarget = [s[tmptargetA[1]-len(extensionA): leftbound], tmptargetA[1]-len(extensionA), leftbound, s[rightbound: tmptargetB[2]+ len(extensionB)], rightbound,tmptargetB[2]+len(extensionB)]
#print "running..." + str(count)
extensionA = s[tmptargetA[1]-extendAseed-extend:tmptargetA[1]]
extensionB = s[tmptargetB[2]:tmptargetB[2]+extendBseed+extend]
hamm = hamm + rna_hamming(extensionA[0:1], extensionB[-1])
#print "this went to hamm, extensionAbegin : {0}, extensionBend: {1}".format(extensionA[0:1], extensionB[-1])
#print "targetA, target B, extensionA, extentionB: {0}, {1}, {2}, {3}".format(tmptargetA, tmptargetB, extensionA, extensionB)
extend = extend + 1
count = count + 1
#print count
#print testcycles
if count>1: #wrong here?
#print "found one in set that needs 4"
#print "about to be returned : {0}".format(newtarget)
dashedA = str(newtarget[0][0:count-1])+(extendAseed)*'-'+ str(target[6])
dashedB = str(target[7])+extendBseed*'-'+ str(newtarget[3][-(count-1):])
newtarget.append(dashedA)
newtarget.append(dashedB)
## print dashedA
## print dashedB
## print newtarget
## print '\n'
return newtarget
else:
testcycles = testcycles + 1
return target
def remove_duplicates(targets):
s = []
for i in targets:
if i not in s:
s.append(i)
return s
#s1 = "AAATAAAGAAA"
#s2 = "TTTTTTTATTT"
s=seq_file
#print len(s)
folds = defaultdict(list)
def controlpanel(s):
#print rna_hamming(s1,s2)
pieces = piece_maker(s,2)
arms = []
#altarms = []
#folddict1 = {}
#folddict2 ={}
#folddict3={}
groupkey = 0
seckey = 0
trikey = 0
groupdict= {}
secdict={}
tridict = {}
#inhelix_pieces =[]
targets = []
seeds = seed_make(pieces, s)
#print "reduced seed list len: {0}".format(len(seeds))
for seed in seeds:
targets.append(seed)
if len(seed[0])==2:
#print "seed {0}".format(seed)
extra = small_hairpin_extension(seed, s)
#print "extra : " +str(extra)
#print "extra {0}".format(extra)
#print "\n"
if extra == seed:
continue #if there's no close extension on top of 2 bp, the go to next seed
##if not (len(extra[0])==4 and len(extra[3])==4): #forgot what this is for...
if len(extra[0]) < 6:#to make sure arm extend enough in most cases
continue
else:
seed = extra
targets.append(extra)
#print "extended seed: {0}".format(seed)
close_target = closerange_target_extend(seed, s, "short")
extra = end_arm_extension(close_target, s)
if not extra in targets:
targets.append(extra)
count = 0
#print "round short"
while count < 4:
close_target = closerange_target_extend(close_target, s, "short")
if not close_target in targets:
targets.append(close_target)
extra = end_arm_extension(close_target, s)
if not extra in targets:
targets.append(extra)
count = count + 1
close_target = closerange_target_extend(seed, s, "long")
extra = end_arm_extension(close_target, s)
count = 0
#print "round long" #made for finding 502ish-546 arm
while count < 4:
close_target = closerange_target_extend(close_target, s, "long")
if not close_target in targets:
targets.append(close_target)
extra = end_arm_extension(close_target, s)
if not extra in targets:
targets.append(extra)
count = count + 1
close_target = closerange_target_extend(seed, s, "jump")
extra = end_arm_extension(close_target, s)
count = 0
#print "round jump" #made for finding 502ish-546 arm
while count < 4:
close_target = closerange_target_extend(close_target, s, "jump")
if not close_target in targets:
targets.append(close_target)
extra = end_arm_extension(close_target, s)
if not extra in targets:
targets.append(extra)
count = count + 1
for target in targets:
arms.append(target)
targets = []
arms = remove_duplicates(arms)
count = 1
#print "all possible arms: "
print count
print len(arms)
#print arms
#print '\n'
#print "all possible arms listed: "
#for arm in arms:
# print str(count) + ". " + str(arm)
# count = count +1
#print '\n\n'
#print len(arms)
#print seeds
#missing the left arm
#problem is in seed maker
#print arm
# print len(arms)
##
#print len(redtargets) #only 82 target seed for the 16s 28 arms
#need to get an accurate structure and sequence of a 16s
#print len(redtargets)
#s1 = "AAATAAAGAAA"
#s2 = "TTTTTTTATTT"
s=seq_file
#s=s[403:554]
#print "full sequence: " + str(s) + "\n"
#s = s[142:220]
#print "Partial sequence s[142:220]: {0}".format(s)
#print "\n"
#s=s[495:550]
#print s[25:37]
#print len(s)
controlpanel(s)
#print "s[1490: 1540]" + str(s[1490:1540])
#print "s[79:90]" + str(s[79:90])
#print "s[149:160]" + str(s[149:160])
#print "s[249:260]" + str(s[249:260])
#print "s[1439:1460]" + str(s[1439:1460])
#next: build dictionary FF:SF:TF
#STRUCTURE INDEX = 1, PROGRAM INDEX = 0
#right half matches structure numbering
#left half matches structure numbering -1
#Program architecture rewrite:
#extend in a pattern. 10 01 11 21 12 02 20 22 03 30 31 13 23 32 33 42 24 43 34 44 52 25 53 35 54 45 55 63 36 64 46 65 56 66 74 47 75 57 76 67 77 87 78 88 89 98 99
#as you get higher , you need a longer bridge at the end
# for 1 1 just need a 1 brdige
# for up to 2, need 2 bridge, not AT TA
#for three just need 2 bridge
# for 5 even, just need a 2 bridge
#determine rules for necessaary bridges.