-
Notifications
You must be signed in to change notification settings - Fork 6
/
Functions.py
1269 lines (1007 loc) · 48.5 KB
/
Functions.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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Created on Oct 3, 2011
@author: jcg
'''
from Data import *
from math import log, exp, sqrt
from random import randint, choice, random
import string
from os import system, devnull, chdir, path
from subprocess import call, Popen, PIPE, check_output
import sys
######################
##
# General Functions
##
######################
# validate a CDS region (i.e. begins with start codon, ends with stop codon, and does not have an in-frame stop codon in the middle of the sequence.
def validateCDS(cds=""):
#normalize CDS
cds = cds.lower().replace('u','t')
#cds length is multiple of 3
if len(cds) % 3 != 0:
return False
#starts with a start codon (ATG, GTG, TTG)
if cds[0:3] not in ('atg','gtg','ttg'):
return False
#stop with a stop codon
if cds[-3:] not in ('taa','tag','tga'):
return False
#stop codon in the middle
if len(set([cds[i:i+3] for i in range(3,len(cds)-3,3)]).intersection(['taa','tag','tga'])) != 0:
return False
return True
# translate codons to aa
def translateCDS(sequence):
aa_seq = ""
for i in range(0,len(sequence),3):
aa_seq += codon2aa_table[sequence[i:(i+3)]]+" "
return aa_seq
# return the indices of the array 'array' sorted
def argsort(array, reverse=False):
return sorted(range(len(array)), key=array.__getitem__, reverse=reverse)
#return sequence complementary to seq
def complementary(seq):
return str(seq).translate(string.maketrans("atcg", "tagc"))
#return random nucleotide (different than original)
def randomMutation(nucleotide):
possible_mut = list(set(bases) - set(nucleotide))
return choice(possible_mut)
#returns a random sequence with defined size
def randomSequence(size):
sequence = ""
for i in xrange(size):
sequence += choice(bases)
return sequence
# count the number of different characters between str1 and str2 (hamming distance)
def diff(str1, str2):
nbr=0
for i in range(len(str1)):
if str1[i] != str2[i]:
nbr+=1
return nbr
def lin(x, y):
"""
Summary
Linear regression of y = ax + b
Usage
real, real, real, real, real = lin(list, list)
Returns coefficients to the regression line "y=ax+b" from x[] and y[],
R^2 Value, sum of squares and mean error
"""
if len(x) != len(y):
raise ValueError, 'unequal length'
n = len(x)
sx = sy = sxx = syy = sxy = 0.0
for i, j in zip(x, y):
sx = sx + i
sy = sy + j
sxx = sxx + i*i
syy = syy + j*j
sxy = sxy + i*j
det = sxx * n - sx * sx
a, b = (sxy * n - sy * sx)/det, (sxx * sy - sx * sxy)/det
mean_y = sy/n
mean_error = residual = 0.0
for i, j in zip(x, y):
mean_error = mean_error + (j - mean_y)**2
residual = residual + (j - a * i - b)**2
r2 = 1 - residual/mean_error
ssq = residual / (n-2)
#var_a, var_b = ss * n / det, ss * Sxx / det
return a, b, r2, ssq, mean_error
def appendLabelToDict(somedict,label):
return dict(map(lambda (key, value): (label+str(key), value), somedict.items()))
def average(array):
return sum(array)*1.0/len(array)
def stddev(array):
avg = average(array)
return sqrt(average(map(lambda x: (x - avg)**2, array)))
#select randomly based on probability, prob_list = [('1',0.1),('2',0.5),('3',0.4)]
def pick_random_tuple(prob_list):
r, s = random(), 0
for num in prob_list:
s += num[1]
if s >= r:
return num[0]
#select randomly based on probability, prob_list = [0.1,0.5,0.4]
def pick_random(prob_list):
r, s, i = random(), 0, 0
for num in prob_list:
s += num
if s >= r:
return i
i += 1
######################
##
# Scoring Functions
##
######################
def structureAnalysis(structure_file, propertyOfInterest="ss"):
'''
given a structure file it returns the position that are either single stranded "ss" or double stranded "ds"
'''
if path.exists("tmp/structures/"+structure_file+".ct"):
if propertyOfInterest == "ds":
output = Popen("perl 3rdParty/unafold/ss-count.pl tmp/structures/" + structure_file + ".ct | awk /[[:digit:]][[:blank:]]0/'{print $1}'", stdout=PIPE, shell=True).stdout.read()
return [eval(k) for k in output.split()]
elif propertyOfInterest == "ss":
output = Popen("perl 3rdParty/unafold/ss-count.pl tmp/structures/" + structure_file + ".ct | awk /[[:digit:]][[:blank:]]1/'{print $1}'", stdout=PIPE, shell=True).stdout.read()
return [eval(k) for k in output.split()]
else:
return []
def analyzeCodons(seq, positions = None, data_table = cai_table):
'''
given a sequence it returns a list with two elements: [ list_of_codons, list_of_codons_cai]
'''
if positions == None:
positions = range(0,len(seq),3)
seq = seq.lower();
codons = []
codons_cai = []
for i in positions:
codon = seq[i:i+3]
codons.append(codon)
if data_table.has_key(codon):
codons_cai.append(data_table[codon])
else:
codons_cai.append("NA")
return [codons, codons_cai]
def get_alternate_codons(codon, data=tai_tuller, dist=0):
"""
returns a alternate codon to codon
data: dictionary with a map between codons and tAI
dist: 0 --> only synonymous codon
1-3 --> only codon with 1-3 nt difference from original
"""
if dist == 0:
# return only syn codon
return [(syn_cod, data[syn_cod]) for syn_cod in aa2codon_table[codon2aa_table[codon]] if syn_cod != codon]
else:
# return syn codon and codon 1 nt away
return [(alt_cod, data[alt_cod]) for alt_cod in codons_list if (alt_cod != codon and diff(codon, alt_cod) <= dist)]
def get_tai(seq, data=tai_tuller):
seq = seq.lower()
# seq = seq.replace("t","u")
return [(seq[i:i+3], data[seq[i:i+3]]) for i in range(0,len(seq),3)]
def analyze_tai(seq, window=21, data=tai_tuller, method="harmonic"):
seq = seq.lower()
# seq = seq.replace("t","u")
scores = [data[seq[i:i+3]] for i in range(0,len(seq),3)]
smoothie = []
if window > 1:
if method == "geometric":
for i in range(len(scores)-window+1):
smoothie.append(1)
for j in range(i,i+window):
smoothie[-1] *= scores[j]
smoothie[-1] = smoothie[-1]**(1/float(window))
if method == "harmonic":
for i in range(len(scores)-window+1):
smoothie.append(sum([1/v for v in scores[i:i+window]])/window)
return scores, smoothie
def analyze_hydropathy(seq):
seq = seq.lower();
score = 0
len_sq = 0
for i in range(0,len(seq),3):
if hydropathy_index_table.has_key(seq[i:i+3]):
score += (hydropathy_index_table[seq[i:i+3]])
len_sq += 1
score /= len_sq
return score
def analyze_cai(seq):
seq = seq.lower();
score = 0
len_sq = 0
for i in range(0,len(seq),3):
if cai_table.has_key(seq[i:i+3]):
score += log(cai_table[seq[i:i+3]])
len_sq += 1
score /= len_sq
return exp(score)
def analyze_bottleneck(sequence, window=20, data=tai_tuller, method="harmonic"):
score, smooth = analyze_tai(sequence, window, data=data, method=method)
return [score, smooth]
def analyze_bottleneck_pos(sequence, score, smooth):
return smooth.index(max(smooth))+1
def analyze_bottleneck_abs_strength(sequence, score, smooth):
return max(smooth)
def analyze_bottleneck_rel_strength(sequence, score, smooth):
return (len(score)*max(smooth)/sum([1/v for v in score]))
def analyze_ntcontent(seq):
seq = seq.replace('u', 't')
nuc_freq = { 'NucleotideContentAT': 0,
'NucleotideContentGC': 0,
'NucleotideContentA' : 0,
'NucleotideContentT' : 0,
'NucleotideContentG' : 0,
'NucleotideContentC' : 0}
for i in range (len(seq)):
nuc_freq['NucleotideContent'+seq[i].upper()] += 1
nuc_freq['NucleotideContentA'] /= float(seq.__len__())
nuc_freq['NucleotideContentT'] /= float(seq.__len__())
nuc_freq['NucleotideContentG'] /= float(seq.__len__())
nuc_freq['NucleotideContentC'] /= float(seq.__len__())
nuc_freq['NucleotideContentAT'] = (nuc_freq['NucleotideContentA']+nuc_freq['NucleotideContentT'])
nuc_freq['NucleotideContentGC'] = (nuc_freq['NucleotideContentG']+nuc_freq['NucleotideContentC'])
return nuc_freq
def analyze_pwm_score(seq, pwm):
#print seq
max_score = -99
max_pos = 0
pwm_length = len(pwm[pwm.keys()[0]])
for pos in range(0,len(seq)-pwm_length+1):
tmp_score = pwm_score(seq[pos:(pos+pwm_length)], pwm)
if tmp_score > max_score:
max_score = tmp_score
max_pos = pos
return { 'MotifScore' : max_score , 'MotifPosition' : max_pos }
def pwm_score(seq,pwm):
score=0
for i in range(0,len(seq)):
score += pwm[seq[i]][i]
return (score)
def analyze_terminator(seq):
#use transtermHP
#create input files
file1 = 'test' + str(randint(0000,1000)) + '.fa';
file2 = 'test' + str(randint(0000,1000)) + '-fake.coords';
system("echo '>seq1\n" + str(seq) + "' > tmp/transterm_files/"+file1)
system("echo 'fakegene1 1 2 seq1\nfakegene1 " + str(len(seq)-1) + " " + str(len(seq)) +" seq1' > tmp/transterm_files/"+file2)
#run transtermhp
output = Popen("./3rdParty/transterm/transterm -p 3rdParty/transterm/expterm.dat tmp/transterm_files/"+file1+" tmp/transterm_files/"+file2+" 2> tmp/transterm_files/err.txt | awk /TERM/'{print $8}'", stdout=PIPE, stderr=None, shell=True).stdout.read()
if output == '':
return 'No'
else:
#print "Terminator output: " + output
all_term_scores = [float(score) for score in output.split()]
if max(all_term_scores) > 90:
return 'Yes'
elif max(all_term_scores) > 70:
return 'Maybe'
else:
return 'No'
def analyze_duplex_structure(seq1,seq2,filename):
chdir(project_dir)
structure_pairs = {}
data = {}
r = randint(4,1000)
s1_file = str(r) + "s1.seq"
s2_file = str(r) + "s2.seq"
system("echo '" + str(seq1) + "' > " + s1_file)
system("echo '" + str(seq2) + "' > " + s2_file)
fnull = open(devnull, 'w') #this line is necessary to omit output generated by UNAFOLD
call("./3rdParty/unafold/hybrid-min -n RNA -o "+filename+" "+ s1_file + " " + s2_file + " -t 37 -T 37", shell = True, stdout = fnull, stderr = fnull) #code is necessary to omit output generated by UNAFOLD
if path.exists(project_dir+"/"+filename+".ct"):
system("mv %s*.ct tmp/structures/" % filename)
system("mv %s*.asc tmp/structures/" % filename)
output_ds = Popen("cat tmp/structures/" + filename + ".ct | awk '{print $1 , $5}'", stdout=PIPE, shell=True).stdout.read()
list_of_pairs = output_ds.split('\n')[1:-1]
for pair_str in list_of_pairs:
pair = pair_str.split()
structure_pairs[pair[0]] = pair[1]
data['RNADuplexPairs'] = structure_pairs
system("rm %s*" % filename)
system("rm %s*.*" % r)
fnull.close()
return data
def analyze_duplex_mfe(filename,region = None):
data = {}
chdir(project_dir)
if path.exists(project_dir+"/tmp/structures/"+filename+".ct"):
try:
res = check_output(["./3rdParty/unafold/ct-energy" , "tmp/structures/" + filename + ".ct"])
if res != "":
data['RNADuplexMFE'] = float(str(res).rstrip())
else:
data['RNADuplexMFE'] = 'NA'
except NameError:
data['RNADuplexMFE'] = 'NA'
#system("rm tmp/structures/%s*" % filename)
else:
data['RNADuplexMFE'] = 'NA'
return data
def analyze_duplex_ds(filename,seq1 = "", region = None):
data = {}
chdir(project_dir)
if region == None:
if path.exists(project_dir+"/tmp/structures/"+filename+".ct"):
output_ds = Popen("perl 3rdParty/unafold/ss-count.pl tmp/structures/" + filename + ".ct | awk /[[:digit:]][[:blank:]]0/'{print $1}'", stdout=PIPE, shell=True).stdout.read()
output = output_ds.split()
data['RNADuplexDoubleStrandedBasesList_Mol1'] = [eval(k) for k in output if eval(k) <= len(seq1)]
data['RNADuplexDoubleStrandedBases_Mol1'] = len(data['RNADuplexDoubleStrandedBasesList_Mol1'])
data['RNADuplexDoubleStrandedBasesList_Mol2'] = [eval(k)-len(seq1) for k in output if eval(k) > len(seq1)]
data['RNADuplexDoubleStrandedBases_Mol2'] = len(data['RNADuplexDoubleStrandedBasesList_Mol2'])
else:
data['RNADuplexDoubleStrandedBasesList_Mol1'] = 'NA'
data['RNADuplexDoubleStrandedBases_Mol1'] = 'NA'
data['RNADuplexDoubleStrandedBasesList_Mol2'] = 'NA'
data['RNADuplexDoubleStrandedBases_Mol2'] = 'NA'
#else:
# TODO: get just DS bases from one region of the rna structure
return data
def analyze_duplex_ss(filename,seq1 = "",region = None):
data = {}
chdir(project_dir)
if region == None:
if path.exists(project_dir+"/tmp/structures/"+filename+".ct"):
output_ss = Popen("perl 3rdParty/unafold/ss-count.pl tmp/structures/" + filename + ".ct | awk /[[:digit:]][[:blank:]]1/'{print $1}'", stdout=PIPE, shell=True).stdout.read()
output = output_ss.split()
data['RNADuplexSingleStrandedBasesList_Mol1'] = [eval(k) for k in output if eval(k) <= len(seq1)]
data['RNADuplexSingleStrandedBases_Mol1'] = len(data['RNADuplexDoubleStrandedBasesList_Mol1'])
data['RNADuplexSingleStrandedBasesList_Mol2'] = [eval(k)-len(seq1) for k in output if eval(k) > len(seq1)]
data['RNADuplexSingleStrandedBases_Mol2'] = len(data['RNADuplexDoubleStrandedBasesList_Mol2'])
else:
data['RNADuplexSingleStrandedBasesList_Mol1'] = 'NA'
data['RNADuplexSingleStrandedBases_Mol1'] = 'NA'
data['RNADuplexSingleStrandedBasesList_Mol2'] = 'NA'
data['RNADuplexSingleStrandedBases_Mol2'] = 'NA' #else:
# TODO: get just DS bases from one region of the rna structure
return data
def analyze_structure_prob(seq,filename,window=50,region=[]):
chdir(project_dir)
structure_pairs = {}
data = {}
system("echo '>" + filename + "\n" + str(seq) + "' > " + filename + ".fa")
fnull = open(devnull, 'w') #this line is necessary to omit output generated by UNAFOLD
call("3rdParty/vienna/RNAplfold -d2 -noLP -W "+str(window)+" -u 1 < " + filename + ".fa", shell = True, stdout = fnull, stderr = fnull) #code is necessary to omit output generated by RNAplfold
output_ss = Popen("cat " + filename + "_lunp | awk '{print $1 \"\t\" $2}'", stdout=PIPE, shell=True).stdout.read()
l = output_ss.rstrip().split('\n')[2:]
for p in l:
pair = p.split()
if pair[1] != 'NA':
structure_pairs[pair[0]]=float(pair[1])
else:
structure_pairs[pair[0]]='NA'
if region != []:
reg_avg = 0
for pos in region:
reg_avg += structure_pairs[str(pos)]
reg_avg = reg_avg/len(region)
data['StructureProb'] = reg_avg
else:
data['StructureProb'] = sum(structure_pairs.values())/len(structure_pairs.keys())
data['StructureProbList'] = structure_pairs
# remove tmp files
system("rm %s*" % filename)
#system("mv " + filename + "* tmp/unafold_files/")
fnull.close()
return data
def analyze_ensemble(seq,filename,sample_size=100):
chdir(project_dir)
system("echo '>" + filename + "\n" + str(seq) + "' > " + filename + ".fa")
output_ss = Popen("./3rdParty/vienna/RNAsubopt -d2 -noLP -s -p "+str(sample_size)+" < " + filename + ".fa | tail -n "+str(sample_size), stdout=PIPE, shell=True).stdout.read()
l = output_ss.rstrip().split('\n')
ens_st = []
string_aux = ""
for st in l:
string_aux += str(seq) + "\n" + str(st) + "\n"
system("echo '" + str(string_aux) + "' > " + filename + ".st")
output_ss = Popen("./3rdParty/vienna/RNAeval -d2 < " + filename + ".st | perl -lne 'm/.* \((.*)\)$// print $1'", stdout=PIPE, shell=True).stdout.read().rstrip()
ens_st = map(lambda x: float(x), output_ss.rstrip().split('\n')[2:])
data = {}
data['StructureEnsembleSample'] = ens_st
data['StructureEnsembleSampleMean'] = average(ens_st)
data['StructureEnsembleSampleSD'] = stddev(ens_st)
system("rm %s*" % filename)
# remove tmp files
#system("rm %s*" % filename)
#system("mv " + filename + "* tmp/unafold_files/")
return data
def analyze_structure(seq,filename,ensemble=False):
chdir(project_dir)
system("echo '" + str(seq) + "' > " + filename + ".seq")
fnull = open(devnull, 'w') #this line is necessary to omit output generated by UNAFOLD
if ensemble:
call("./3rdParty/unafold/UNAFold.pl -n RNA " + filename + ".seq", shell = True, stdout = fnull, stderr = fnull) #code is necessary to omit output generated by UNAFOLD
else:
call("./3rdParty/unafold/hybrid-ss-min -n RNA " + filename + ".seq", shell = True, stdout = fnull, stderr = fnull) #code is necessary to omit output generated by UNAFOLD
if os.path.isfile(filename+".ct"):
system("mv %s*.ct tmp/structures/" % filename)
# remove tmp files
system("rm %s*" % filename)
#system("mv " + filename + "* tmp/unafold_files/")
fnull.close()
return 1
def analyze_structure_mfe(filename):
data = {}
chdir(project_dir)
if path.exists(project_dir+"/tmp/structures/"+filename+".ct"):
output = check_output(["./3rdParty/unafold/ct-energy" , "tmp/structures/" + filename + ".ct"]).rstrip()
mfe_list = [float(a) for a in output.split('\n')]
data['StructureMFE'] = mfe_list[0]
else:
data['StructureMFE'] = 0
return data
def analyze_structure_ds(filename,region = None):
data = {}
chdir(project_dir)
if region == None:
if path.exists(project_dir+"/tmp/structures/"+filename+".ct"):
output_ds = Popen("perl 3rdParty/unafold/ss-count.pl tmp/structures/" + filename + ".ct | awk /[[:digit:]][[:blank:]]0/'{print $1}'", stdout=PIPE, shell=True).stdout.read()
data['StructureDoubleStrandedList'] = [eval(k) for k in output_ds.split()]
data['StructureDoubleStranded'] = len(data['StructureDoubleStrandedList'])
else:
data['StructureDoubleStrandedList'] = 'NA'
data['StructureDoubleStranded'] = 'NA'
#else:
# TODO: get just DS bases from one region of the rna structure
return data
def analyze_structure_ss(filename,region = None):
data = {}
chdir(project_dir)
if region == None:
if path.exists(project_dir+"/tmp/structures/"+filename+".ct"):
output_ss = Popen("perl 3rdParty/unafold/ss-count.pl tmp/structures/" + filename + ".ct | awk /[[:digit:]][[:blank:]]1/'{print $1}'", stdout=PIPE, shell=True).stdout.read()
data['StructureSingleStrandedList'] = [eval(k) for k in output_ss.split()]
data['StructureSingleStranded'] = len(data['StructureSingleStrandedList'])
else:
data['StructureSingleStrandedList'] = 'NA'
data['StructureSingleStranded'] = 'NA'
#else:
# TODO: get just DS bases from one region of the rna structure
return data
def analyze_structure_accessibility(filename):
data = {}
chdir(project_dir)
if path.exists(project_dir+"/tmp/structures/"+filename+".ct"):
#output = Popen("perl 3rdParty/unafold/ss-count.pl -w tmp/structures/" + filename + ".ct | awk '{sum += $2; count += 1} END{print sum/count}'", stdout=PIPE, shell=True).stdout.read()
#data['StructureAccessibility'] = eval(output)
output = Popen("perl 3rdParty/unafold/ss-count.pl -w tmp/structures/" + filename + ".ct | awk '{print $2}'", stdout=PIPE, shell=True).stdout.read()
data['StructureEnsembleAccessibilityBasesList'] = [eval(e) for e in output.split('\n') if e != '']
data['StructureEnsembleAccessibility'] = sum(data['StructureEnsembleAccessibilityBasesList'])/len(data['StructureEnsembleAccessibilityBasesList'])
else:
data['StructureEnsembleAccessibility'] = 'NA'
return data
######################
##
# Operators Functions
##
######################
def SimpleStructureOperator(sequence, structurefile, structure_range, mutable_region, cds_region, direction, keep_aa = True, ss_bases=None, ds_bases=None):
'''
Operator that given a sequence and structure, mutates the sequence to change structure
seq: sequence in structure
structure_file: a file containing the structure correspondent to sequence
structure_range - start and end position to calculate structure - a tuple in the form (start, end)
mutable_region - a list with all bases that can be mutated
cds_regions - a list of pairs with begin and end of CDSs - example: [(0,100), (150,300)]
direction: either increase ('+') or decrease ('-') single bases
'''
if not mutable_region: #it's not possible to mutate
return None
if ss_bases == None:
ss_bases = structureAnalysis(structurefile, "ss")
if ds_bases == None:
ds_bases = structureAnalysis(structurefile, "ds")
if direction == '+':
#get double stranded bases
baseToMutate = [(b+structure_range[0]-1) for b in ds_bases if (b+structure_range[0]-1) in mutable_region]
elif direction == '-':
#get single stranded bases
baseToMutate = [(b+structure_range[0]-1) for b in ss_bases if (b+structure_range[0]-1) in mutable_region]
else:
sys.stderr.write("Direction Unknown")
mutated = False
iteration = 0
while not mutated and iteration <= 100:
#position to mutate
index_to_mutate = baseToMutate.pop(randint(0,len(baseToMutate)-1)) if len(baseToMutate) != 0 else mutable_region.pop(randint(0,len(mutable_region)-1))
#mutate base
if keep_aa == True and index_to_mutate >= cds_region[0] and index_to_mutate <= cds_region[1]:
codon_p = (index_to_mutate-cds_region[0])/3
initial_codon = sequence[(cds_region[0]+codon_p*3):(cds_region[0]+codon_p*3+3)]
#codon position
p1 = (cds_region[0]+codon_p*3)
p2 = (cds_region[0]+codon_p*3+1)
p3 = (cds_region[0]+codon_p*3+2)
alt_codons = ([c for c in aa2codon_table[codon2aa_table[initial_codon]] if c!= initial_codon])
while len(alt_codons) != 0:
rnd_alt_codon = alt_codons.pop(randint(0,len(alt_codons)-1))
valid = True
if initial_codon[0] != rnd_alt_codon[0] and not(p1 in mutable_region):
valid = False
if initial_codon[1] != rnd_alt_codon[1] and not(p2 in mutable_region):
valid = False
if initial_codon[2] != rnd_alt_codon[2] and not(p3 in mutable_region):
valid = False
if valid == True:
mutated = True
new_seq = sequence[:(cds_region[0]+codon_p*3)] + rnd_alt_codon + sequence[(cds_region[0]+codon_p*3+3):]
#print "------"
#print initial_codon
#print rnd_alt_codon
#print sequence
#print new_seq
else:
mutated = True
new_seq = list(sequence)
if direction == '+':
comp = complementary(sequence[index_to_mutate])
else:
comp = randomMutation(sequence[index_to_mutate])
new_seq[index_to_mutate] = comp
#print sequence
#print "".join(new_seq)
iteration+=1
return "".join(new_seq)
def SimpleBneckOperator(seq, direction="", distance=0):
"""
Modify bottleneck in seq
direction: + --> increase bottleneck
- --> decrase bottleneck
One codon is chosen at random among the corresponding tertile based on tai value
This codon is randomly replaced by possible alternative codon in the appropriate tertile
Distance control the codon similarity.
distance: 0 --> only synonymous codon
1-3 --> only codon with 1-3 nt difference from original
"""
#print "dist: " , str(distance)
if direction == "":
return None
iteration = 0
tai = get_tai(seq)
alt_codons= []
while len(alt_codons)==0 and iteration != 100:
iteration += 1
codonPosition2replace = randint(0,len(tai)-1)
alt_codons = get_alternate_codons(tai[codonPosition2replace][0], dist=distance)
#print "attempt to replace: " + str(codonPosition2replace) + "-" + str(tai[codonPosition2replace])
if direction == "+":
# randomly pick lower tai for the alternate solution given allowable distance
alt_codons = [alt_cod for alt_cod in alt_codons if alt_cod[1] < tai[codonPosition2replace][1]]
elif direction == "-":
# randomly pick higher tai for the alternate solution given allowable distance
alt_codons = [alt_cod for alt_cod in alt_codons if alt_cod[1] > tai[codonPosition2replace][1]]
if len(alt_codons) != 0:
randomAltCodon = randint(0,len(alt_codons)-1)
#print "Bot operator: old codon -> " + str(tai[codonPosition2replace][0])
#print "Bot operator: new codon -> " + str(alt_codons[randomAltCodon][0])
if iteration == 100:
return None
return seq[:3*codonPosition2replace]+alt_codons[randomAltCodon][0]+seq[3*(codonPosition2replace+1):]
def SimpleNtContentOperator(seq, direction=0, nucleotide = [], mutable_region = None, cds_region = (0,9), keep_aa=True):
if direction == 0:
return seq
mutated = False
seq = seq.lower()
#check direction to decide possible mutations
if direction == '-':
#select mutable position based on presence of nucleotide(s)
mutable_positions = [pos for pos in mutable_region if seq[pos] in set(nucleotide)]
elif direction == '+':
#select mutable position based on absence of nucleotide(s)
mutable_positions = [pos for pos in mutable_region if not (seq[pos] in set(nucleotide))]
while not mutated and mutable_positions.__len__() != 0 :
#print mutable_positions
rnd_pos = mutable_positions.pop(randint(0,mutable_positions.__len__()-1))
if direction == '-':
possible_mutations = list(set(bases) - set(nucleotide))
elif direction == '+':
possible_mutations = list(nucleotide)
while not mutated and possible_mutations.__len__() != 0:
new_seq = seq[:rnd_pos]+possible_mutations.pop(randint(0,possible_mutations.__len__()-1))+seq[rnd_pos+1:]
if keep_aa == True and rnd_pos >= cds_region[0] and rnd_pos <= cds_region[1]:
#check if AA remains the same
codon_p = (rnd_pos-cds_region[0])/3
initial_codon = seq[(cds_region[0]+codon_p*3):(cds_region[0]+codon_p*3+3)]
final_codon = new_seq[(cds_region[0]+codon_p*3):(cds_region[0]+codon_p*3+3)]
#print "initial codon: " + str(initial_codon) + " AA: " + codon2aa_table[initial_codon]
#print "final codon: " + str(final_codon) + " AA: " + codon2aa_table[final_codon]
if codon2aa_table[initial_codon] == codon2aa_table[final_codon]:
mutated = True
else:
mutated = True
if mutated == False:
return None
return new_seq
def SimplePWMScoreOperator(seq, pwmnt, direction=0, mutable_region = None, keep_aa=False, max_iter=100):
if direction == 0:
return seq
elif direction == '+':
direction = 1;
elif direction == '-':
direction = -1
iteration = 0
mutated = False
new_seq = seq
res = analyze_pwm_score(seq,pwmnt)
motif_position = res['MotifPosition']
motifsize = len(pwmnt[pwmnt.keys()[1]])
while not mutated:
iteration += 1
if iteration == max_iter:
return None
#draw position at random and check if better can be found
pos = choice(list(set(mutable_region) & set(range(motif_position,motif_position+motifsize))))
rel_pos = pos - motif_position
current_value = pwmnt[new_seq[pos].replace('u','t')][rel_pos]
base = randomMutation(seq[pos])
if direction * (pwmnt[base.replace('u','t')][rel_pos] - current_value) > 0: #check whether the change goes in the right direction
new_seq = seq[:pos]+base+seq[pos+1:]
mutated = True
if keep_aa:
#get codon assuming the sequence is in frame
cod_offset = (pos)%3
cod = seq[pos-cod_offset:(pos-cod_offset+3)]
new_cod = new_seq[pos-cod_offset:(pos-cod_offset+3)]
if codon2aa_table[cod] != codon2aa_table[new_cod]:
mutated = False
new_seq = seq
return new_seq
def SimplePWMPositionOperator(seq, pwmnt, position=None, mutable_region = None, keep_aa=False, max_iter=100):
if position == None:
return seq
iteration = 0
mutated = False
new_seq = seq
res = analyze_pwm_score(seq,pwmnt)
motif_position = res['MotifPosition']
motifsize = len(pwmnt[pwmnt.keys()[1]])
while not mutated:
iteration += 1
if iteration == max_iter:
return None
if choice([True,False]): #remove unwanted motif
#print "remove current motif"
direction = -1
pos = choice(list(set(mutable_region) & set(range(motif_position,motif_position+motifsize))))
tmp_motif_position=motif_position
else: #create desired motif
#print "improve new motif"
direction = 1
pos = choice(list(set(mutable_region) & set(range(position,position+motifsize))))
tmp_motif_position = position
#print "tmp_motif_position: ",tmp_motif_position
#print "pos: ", pos
rel_pos = pos - tmp_motif_position
current_value = pwmnt[new_seq[pos].replace('u','t')][rel_pos]
base = randomMutation(seq[pos])
if direction * (pwmnt[base.replace('u','t')][rel_pos] - current_value) > 0:
# check whether the change goes in the right direction
new_seq = seq[:pos]+base+seq[pos+1:]
mutated = True
if keep_aa:
#get codon assuming the sequence is in frame
cod_offset = (pos)%3
cod = seq[pos-cod_offset:(pos-cod_offset+3)]
new_cod = new_seq[pos-cod_offset:(pos-cod_offset+3)]
if codon2aa_table[cod] != codon2aa_table[new_cod]:
mutated = False
new_seq = seq
#print "--------"
#print analyze_pwm_score(seq,pwmnt) , seq
#print analyze_pwm_score(new_seq,pwmnt) , new_seq
#print "--------"
return new_seq
def randomMutationOperator(sequence, keep_aa, mutable_region, cds_region, pos=None, n_mut = [1,2]):
'''
Operator that given a sequence, mutates the sequence randomly
sequnce: sequence
mutable_region - a list with all bases that can be mutated
cds_regions - a list of pairs with begin and end of CDSs - example: [(0,100), (150,300)]
'''
mutableCodonsPosition = [c for c in range(cds_region[0],cds_region[1],3) if set([c,c+1,c+2]).issubset(mutable_region)]
mutableUTRPosition = list(set(mutable_region) - set(range(cds_region[0],cds_region[1])))
if mutableCodonsPosition == [] and mutableUTRPosition == []:
sys.stderr.write("randomMutationOperator: No codons available for mutation\n")
return None
else:
if keep_aa == True:
if (mutableUTRPosition == []) or (mutableCodonsPosition != [] and choice([True,False])):
return mutateCDS(sequence, keep_aa, mutableCodonsPosition, cds_region, pos, n_mut)
else:
return mutateAll(sequence, keep_aa, mutableUTRPosition, cds_region, pos, n_mut)
else:
return mutateAll(sequence, keep_aa, mutable_region, cds_region, pos, n_mut)
def mutateCDS(sequence, keep_aa, mutableCodonsPosition, cds_region, pos=None, n_mut = [1,2]):
if keep_aa == True:
result = analyzeCodons(sequence,mutableCodonsPosition)
n_mutations = choice(n_mut)
codons = (result[0])
codons_ind = range(0,codons.__len__())
mutated = False
while codons_ind.__len__() != 0 and n_mutations > 0:
rnd_ind = codons_ind.pop(randint(0,codons_ind.__len__()-1))
rnd_codon = codons[rnd_ind]
alt_codons = [c for c in aa2codon_table[codon2aa_table[rnd_codon]] if c!= rnd_codon and codon2aa_table[c]!='stop']
if alt_codons.__len__() != 0:
mutated = True
n_mutations -= 1
new_codon = choice(alt_codons)
real_codon_pos = mutableCodonsPosition[rnd_ind]
codon_position = (real_codon_pos-cds_region[0])/3
all_codons = analyzeCodons(sequence,range(cds_region[0],cds_region[1]+1,3))[0]
all_codons[codon_position]=new_codon
new_seq = sequence[:cds_region[0]] + ''.join(c for c in all_codons) + sequence[cds_region[1]+1:]
sequence = new_seq
if mutated == False:
sys.stderr.write("RandomMutator: Not able to mutate sequence keeping AA\n")
return None
else:
return new_seq
def mutateAll(sequence, keep_aa, mutable_region, cds_region, pos=None, n_mut = [1,2]):
#####
# Not necessary to keep AA
#
n_mutations = choice(n_mut)
if mutable_region == []:
return None
while n_mutations!=0:
n_mutations -= 1
if pos != None:
intersect_mut = list(set(mutable_region) & set(pos))
if intersect_mut != []:
position_to_mutate = choice(intersect_mut)
else:
position_to_mutate = choice(mutable_region)
else:
position_to_mutate = choice(mutable_region)
mutation = randomMutation(sequence[position_to_mutate])
new_seq = sequence[:position_to_mutate] + mutation + sequence[position_to_mutate+1:]
sequence = new_seq
return new_seq
def SimpleCAIOperator(sequence, cai_range, keep_aa, mutable_region, cds_regions, direction = '+'):
'''
Operator that given a sequence, mutates the sequence to change CAI
sequnce: sequence
cai_range - start and end position to calculate cai - a tuple in the form (start, end)
mutable_region - a list with all bases that can be mutated
cds_regions - a list of pairs with begin and end of CDSs - example: [(0,100), (150,300)]
direction: either increase ('+') or decrease ('-') CAI
'''
mutated = False
mutableCodonsPosition = [c for c in range(cai_range[0],cai_range[1],3) if set([c,c+1,c+2]).issubset(mutable_region)]
if len(mutableCodonsPosition) == 0:
sys.stderr.write("SimpleCAIOperator: No codons available for mutation\n")
return None
result = analyzeCodons(sequence,mutableCodonsPosition)
codons = (result[0])
codons_cai = (result[1])
codons_ind = range(0,codons.__len__())
while not mutated and codons_ind.__len__() != 0:
rnd_ind = codons_ind.pop(randint(0,codons_ind.__len__()-1))
rnd_codon = codons[rnd_ind]
rnd_codon_cai = codons_cai[rnd_ind]
#select alternative codons
if keep_aa == True and direction == '+':
alt_codons = [c for c in aa2codon_table[codon2aa_table[rnd_codon]] if c!= rnd_codon and cai_table[c] > rnd_codon_cai and codon2aa_table[c]!='stop']
elif keep_aa == True and direction == '-':
alt_codons = [c for c in aa2codon_table[codon2aa_table[rnd_codon]] if c!= rnd_codon and cai_table[c] < rnd_codon_cai and codon2aa_table[c]!='stop']
elif keep_aa == False and direction == '+':
alt_codons = list(k for k,v in cai_table.iteritems() if v>rnd_codon_cai and codon2aa_table[k]!='stop')
elif keep_aa == False and direction == '-':
alt_codons = list(k for k,v in cai_table.iteritems() if v<rnd_codon_cai and codon2aa_table[k]!='stop')
if alt_codons.__len__() != 0:
mutated = True
new_codon = choice(alt_codons)
#print "new: " + str(new_codon)