-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathanalyse_seqs.py
executable file
·1498 lines (1225 loc) · 81.5 KB
/
analyse_seqs.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
'''
Python script to analyse DNA single-stranded tile sequences. Run with --help for options. Python2 and python3 compatible.
Shipped with DNA single-stranded tile (SST) sequence designer used in the following publication.
"Diverse and robust molecular algorithms using reprogrammable DNA self-assembly"
Woods*, Doty*, Myhrvold, Hui, Zhou, Yin, Winfree. (*Joint first co-authors)
Nature, 2019
'''
import math,sys,os,time,itertools,pickle
import argparse as argparse
from sst_dsd import pfunc, binding, mfe_binding, RNAduplex_multiple
from atam2ssts import eval_colocated_end_pair
import re
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from multiprocessing.pool import ThreadPool
# from lru_cache import lru_cache # this was used when we had a hand-rolled lru_cache, but it's now in Python 3
try:
from functools import lru_cache
except ImportError:
# in case this is python2
from lru_cache import lru_cache
from datetime import datetime
global_thread_pool = ThreadPool()
MORE_NEGAIVE_ENERGY_IS_MORE_FAVOURABLE = True
NEGATE = not MORE_NEGAIVE_ENERGY_IS_MORE_FAVOURABLE
#############################################################################
# Define constants and inputs
#############################################################################
_default_temp_in_C = 53.0 # if the user does not specify a temperature, we use this value
_threaded = True
_bad_first_algo_conflict_threshold = -8.0
_lower_thres_correct_binding = -10.8
_check_double_mismatches = False # Setting to False speeds up lattice binding analysis
_num_helices=16
_bad_lattice_binding_to_arbitrary_lattice_threshold = -10.0
# Some output filenames
filelist = ['nupack domain pair binding V RNAduplex domain pair (1999) - non-wc.pdf',
'nupack domain pair binding V RNAduplex domain pair (1999) - wc.pdf',
'dG of lattice+tile-lattice-tile V binding(tile,lattice).pdf',
'nupack pair binding V RNAduplex pair (1999).pdf',
'nupack pair binding V RNAduplex pair (2004).pdf',
'RNAduplex pair (1999) V RNAduplex pair (2004).pdf',
'nupack pair mfe V RNAduplex pair.pdf',
'nupack pair binding V nupack pair mfe.pdf']
#############################################################################
# args e.g. --help
#############################################################################
def parse_args():
parser = argparse.ArgumentParser(description=
'''Python3 script that runs a barrage of energetics tests on DNA-tile sequences.
If you are running a non-algorithmic tile set please use option -nl (to turn off
some tests and prevent errors). Input is given as a text file in IDT format, for example:
# Example 1
# Algorithmic SST example. Note that sequences may contain T bases with an ("internal") biotin modification, denoted "/iBiodT/".
# Strand domains (that encode aTAM tile-glues) are separated by a single space. Comments are ignored by the analysis code.
#
U3;10->10;sw,AGTGTGTTTTT AGTTCGATGT AGAGGCTTTT ATCAGAGGGAA,25nm,STD
U3;10->10;se,ACTCGTTCTT TTCCC/iBiodT/CTGAT TCCTCCAATTA AAAAACGCAA,25nm,STD
U3;10->11;nw,AAGAACCACT TTCGTCAAATT ATACATCACCT AACCCACCAA,25nm,STD
# Example 2
# Non-algorithmic example, tile names do not have any ";" symbols.
# This example shows that tiles need not have four domains/glues.
#
tile_name1,AATCCTAGAA ATTGTTATTTC ATGTATACAAA GATAGATCAG,25nm,STD
tile_name2,AACTAGAAAC CTTAGGAATT,25nm,STD
tile_name3,TAATACTTTCA TTTATCATCG CTACATTCTT ATTTGTTTATC,25nm,STD
The script makes use of pfunc and mfe from NUPACK, and RNAduplex from ViennaRNA, and assumes these
three executables are available.
''',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog= '''Example usage:
python analyse_seqs.py input_file.idt
''')
#parser.add_argument('-s', '--seqs', required=True, # action="store_true",
# help="Specify a batch file that contains one or more image filenames to process.", default="")
# parser.add_argument('-o', '--check', required=False, action="store_true", # action="store", dest="query",
# help="Use with --batch. Check orientation of ribbons. Produces ribbon image files, some of which may be incorrectly orientated.", default="")
parser.add_argument('files', nargs='*') # default=os.getcwd()
#_StoreAction(option_strings=[], dest='files', nargs='*', const=None, default='', type=None, choices=None, help=None, metavar=None)
#parser.add_argument('-s', '--strands', required=False, action="store_true", # action="store", dest="query",
# help="Analyse secondary structure energy of individual strands.", default="")
parser.add_argument('-T', '--temperature', required=False, type=float, # action="store", dest="query",
help="Temperature in C to analyse sequences. Default temperature is 53.0 C")
parser.add_argument('-nl', '--no-lattice-binding', required=False, action="store_true", # action="store", dest="query",
help="Do not run analysis of lattice-binding properties (includes analysis of algorithmic errors which is probably only relevant for algorithmic tile sets).", default="")
parser.add_argument('-nd', '--no-domains', required=False, action="store_true", # action="store", dest="query",
help="Do not run domain-level secondary structure analysis.", default="")
parser.add_argument('-ns', '--no-strand-analysis', required=False, action="store_true", # action="store", dest="query",
help="Do not run strand-level/tile-level secondary structure analysis.", default="")
parser.add_argument('-np', '--no-tile-pairs', required=False, action="store_true", # action="store", dest="query",
help="Do not run the (expensive) tile pair test, nor the domain pair test.", default="")
parser.add_argument('-nm', '--no-mfe-analysis', required=False, action="store_true", # action="store", dest="query",
help="Do not run (expensive) mfe analysis.", default="")
parser.add_argument('-p', '--pickle', required=False, action="store_true", # action="store", dest="query",
help="Use python pickle lbrary to save processed data. Useful when doing time-consuming calculations and want the data for further processing.", default="")
parser.add_argument('-b', '--bins', required=False, type=int, # action="store", dest="query",
help="Number of bins in histogram plots.")
parser.add_argument('-nst', '--non-standard-tiles', required=False, action="store_true", # action="store", dest="query",
help="Tiles that do not have exactly 4 domains.", default="")
args = parser.parse_args()
if not (args.files):
parser.error('Error: no input filename specified. Please specify a .idt file with DNA sequences for processing.')
return args
#############################################################################
# Run analysis
#############################################################################
import string
# maketrans is in string if python2 but in str if python3
try:
_wctable = str.maketrans('ACGTacgt_-','TGCAtgca_-')
except AttributeError:
_wctable = string.maketrans('ACGTacgt_-','TGCAtgca_-')
def wc(seq):
'''Return reverse Watson-Crick complement of seq'''
return seq.translate(_wctable)[::-1]
def run_analysis(f,directory,temp_in_C,domain_analysis=1,strand_analysis=1,tile_pair_check=1,
lattice_binding_analysis=1,run_mfe=1,non_standard_tiles=0,pickle_data=0,num_bins=-1):
domains, domains_with_biotin = read_domains_from_idt_order(f,non_standard_tiles)
names, seqs, seqs_bt, seq_ws, seq_ws_bt = read_strand_data_from_idt_order(f)
if not tile_pair_check: num_tile_and_domain_pairs = -1 # -1 means do not do the tile pair, nor domain pair, check
else: num_tile_and_domain_pairs = 0 # 0 means do the tile pair, and domain pair, check (for fast debugging/testing: num_tile_and_domain_pairs>1 means only check the first num_tile_and_domain_pairs pairs)
# strand level analysis
if strand_analysis:
run_strand_analysis(seqs,seqs_bt,domains,domains_with_biotin,temp_in_C,
num_tile_pairs=num_tile_and_domain_pairs, run_mfe=mfe_analysis,
directory=directory,pickle_data=pickle_data,num_bins=num_bins)
if domain_analysis:
# domain level analysis
analyse_domains(names,seqs,seqs_bt,domains,domains_with_biotin,
seq_ws,seq_ws_bt,temp_in_C,num_domain_pairs=num_tile_and_domain_pairs,
directory=directory,non_standard_tiles=non_standard_tiles,pickle_data=pickle_data,num_bins=num_bins) # num_tile_pairs ,num_domain_pairs=num_tile_pairs
if lattice_binding_analysis and not non_standard_tiles:
print('\nStarting lattice binding analysis')
list_of_pairs_of_lists,descriptors = run_lattice_binding_analysis(f,directory,temp_in_C,num_bins=num_bins)
plot_energies2(list_of_pairs_of_lists, labels=descriptors,
xaxis='dG of lattice+tile-lattice-tile',yaxis='binding(tile,lattice)',
title_info='',plot_dir=directory)
#convert_scatter_plots_to_jpg(f, directory)
#print('Done with: run_analysis()')
# analyze sequences at the 'strand' level of abstraction
def run_strand_analysis(seqs,seqs_bt,domains,domains_with_biotin,temp_in_C,
num_tile_pairs=False,run_mfe=False,directory='plots/',pickle_data=0,num_bins=-1):
'''Analyse `strand' energetics: secondary structure, and strand pair interactions'''
print('running analysis on strands')
description = '' # f[1]
if not os.path.exists(directory):
print(directory)
os.makedirs(directory)
pickle_directory = directory+'pickle/'
if pickle_data:
if not os.path.exists(pickle_directory):
os.makedirs(pickle_directory)
print(' run_strand_analysis(): plots will be placed in: '+directory+ pickle_data*(', pickled datastructures will be placed in: ' + pickle_directory))
print(str(len(seqs_bt))+' sequences (where we leave in biotin markers), ' + str(len(seqs))+' sequences (where we do not leave in biotin markers)')
print(str(len([s for s in seqs_bt if '/iBiodT/' in s])) +' sequences with biotin markers')
if all([len(s)==42 for s in seqs] ): print('All strands are of length 42 bases')
else: 'Some strands are not of length 42 bases'
print(str(len(domains))+' unique domains (glue sequences), ' +str(len([d for d in domains if len(d)==10])) + ' of length 10, '+str(len([d for d in domains if len(d)==11])) + ' of length 11')
sys.stdout.flush()
# domain secondary structure
#print 'analysing domain secondary structure';sys.stdout.flush()
#print temp_in_C
#domain_energies = [pfunc(d, temp_in_C, NEGATE) for d in domains]
#histogram([domain_energies],labels=[description],xaxis='energy (kcal/mol)',yaxis='',title='domain secondary structure',plot_dir=directory)
#pickle.dump(domain_energies, open( pickle_directory+"domain_secondary_structure.p", "wb" ) )
# strand secondary structure
sys.stdout.write('analysing tile (strand) secondary structure: '); sys.stdout.flush()
sec_struct_energies = [pfunc(s, temp_in_C, NEGATE) for s in seqs]
sys.stdout.write(str(len(sec_struct_energies))+' tiles\n'); sys.stdout.flush()
histogram([sec_struct_energies],labels=[description],xaxis='energy (kcal/mol)',yaxis='',title='internal tile secondary structure (via Nupack pfunc)',plot_dir=directory,num_bins=num_bins)
if pickle_data:
pickle.dump(sec_struct_energies, open(pickle_directory+"sec_struct_energies.p", "wb" ) )
# tile pair analysis
if num_tile_pairs != -1:
sys.stdout.write('analysing tile pairs: ');sys.stdout.flush()
seq_pairs = get_seq_pairs(seqs)
if num_tile_pairs>0 : seq_pairs = seq_pairs[:num_tile_pairs]
print(str(len(seq_pairs)) + ' tile pairs to process');sys.stdout.flush()
RNAduplex_pair_energies = tile_pair_RNAduplex_energies(seq_pairs,temp_in_C)
RNAduplex_pair_energies_2004 = tile_pair_RNAduplex_energies(seq_pairs,temp_in_C,NA_parameter_set='dna_mathews2004.par')
histogram([RNAduplex_pair_energies],labels=[description],xaxis='energy (kcal/mol)',yaxis='',title='tile pairs - RNAduplex (1999)',plot_dir=directory,num_bins=num_bins)
histogram([RNAduplex_pair_energies_2004],labels=[description],xaxis='energy (kcal/mol)',yaxis='',title='tile pairs - RNAduplex (2004)',plot_dir=directory,num_bins=num_bins)
binding_pair_energies = tile_pair_NUPACK_binding_energies(seq_pairs,temp_in_C)
histogram([binding_pair_energies],labels=[description],xaxis='energy (kcal/mol)',yaxis='',title='tile pairs - nupack binding',plot_dir=directory,num_bins=num_bins)
scatter_plot_energies(binding_pair_energies, RNAduplex_pair_energies, xaxis='nupack pair binding', yaxis='RNAduplex pair (1999)', title_info='',plot_dir=directory) #,xthreshold=9.3,ythreshold=9.3
scatter_plot_energies(binding_pair_energies, RNAduplex_pair_energies_2004, xaxis='nupack pair binding', yaxis='RNAduplex pair (2004)', title_info='',plot_dir=directory) #,xthreshold=9.3,ythreshold=9.3
scatter_plot_energies(RNAduplex_pair_energies, RNAduplex_pair_energies_2004, xaxis='RNAduplex pair (1999)', yaxis='RNAduplex pair (2004)', title_info='',plot_dir=directory)
if run_mfe and num_tile_pairs != -1:
mfe_pair_energies = tile_pair_NUPACK_mfe_energies(seq_pairs,temp_in_C)
histogram([mfe_pair_energies],labels=[description],xaxis='energy (kcal/mol)',yaxis='',title='tile pairs - nupack mfe',plot_dir=directory,num_bins=num_bins)
scatter_plot_energies(mfe_pair_energies, RNAduplex_pair_energies, xaxis='nupack pair mfe', yaxis='RNAduplex pair', title_info='',plot_dir=directory)
scatter_plot_energies(binding_pair_energies, mfe_pair_energies, xaxis='nupack pair binding', yaxis='nupack pair mfe', title_info='',plot_dir=directory)
descriptors = ['RNAduplex (1999)','RNAduplex (2004)','NUPACK binding','NUPACK mfe']
histogram([RNAduplex_pair_energies,RNAduplex_pair_energies_2004,binding_pair_energies,mfe_pair_energies],labels=descriptors,xaxis='energy (kcal/mol)',yaxis='',title='tile pair energies',plot_dir=directory,num_bins=num_bins)
if pickle_data:
pickle.dump(mfe_pair_energies, open( pickle_directory+"mfe_pair_energies.p", "wb" ) )
elif not run_mfe and num_tile_pairs != -1:
descriptors = ['RNAduplex (1999)','RNAduplex (2004)','NUPACK binding']
histogram([RNAduplex_pair_energies,RNAduplex_pair_energies_2004,binding_pair_energies],labels=descriptors,xaxis='energy (kcal/mol)',yaxis='',title='tile pair energies',plot_dir=directory,num_bins=num_bins)
#descriptors = ['binding v RNAduplex (1999)','binding v RNAduplex (2004)','mfe v RNAduplex (1999)','mfe v RNAduplex (2004)']
#pickle.dump(RNAduplex_pair_energies, open( pickle_directory+"RNAduplex_pair_energies.p", "wb" ) )
#pickle.dump(binding_pair_energies, open( pickle_directory+"binding_pair_energies.p", "wb" ) )
#pickle.dump(seqs, open( pickle_directory+"seqs.p", "wb" ) )
#pickle.dump(seq_pairs, open( pickle_directory+"seq_pairs.p", "wb" ) )
#if run_mfe: return seqs, seq_pairs, sec_struct_energies, binding_pair_energies, RNAduplex_pair_energies, binding_pair_energies, mfe_pair_energies
#else: return seqs, seq_pairs, sec_struct_energies, binding_pair_energies, RNAduplex_pair_energies, binding_pair_energies
def lattice_binding_spacer(strand,lattice_end_top,lattice_spacer,lattice_end_bottom,temp_in_C):
'''Strand is string with 4 single-whitespace delimited domains. lattice is of the form domain+lattice_spacer+domain,
where lattice_spacer is typically 'TTTTT'.'''
tile1_domains = strand.split(' ')
dG_sticky_ends_bound = max(binding(tile1_domains[1]+tile1_domains[2],lattice_end_top+ lattice_spacer +lattice_end_bottom,temp_in_C),
binding(tile1_domains[1]+tile1_domains[2],lattice_end_top+wc(lattice_spacer)+lattice_end_bottom,temp_in_C))
dG_tube_ends_after = max(pfunc(tile1_domains[0]+ lattice_spacer +tile1_domains[3],temp_in_C, NEGATE),
pfunc(tile1_domains[0]+wc(lattice_spacer)+tile1_domains[3],temp_in_C, NEGATE))
dG_strand_before = pfunc(''.join(tile1_domains),temp_in_C, NEGATE)
dG_tube_ends_before = max(pfunc(lattice_end_top+ lattice_spacer +lattice_end_bottom,temp_in_C, NEGATE),
pfunc(lattice_end_top+wc(lattice_spacer)+lattice_end_bottom,temp_in_C, NEGATE))
dG = dG_sticky_ends_bound + dG_tube_ends_after - dG_strand_before - dG_tube_ends_before # + dG_adjustment
return dG
_bad_single_mismatch_threshold = 10.0
def run_lattice_binding_analysis(f,directory,temp_in_C,lattice_spacer='TTTTT',num_bins=-1):
# check for "first" algo conflicts (single mismatches that are the first error in a proofreading block)
# first with empty lattice_spacer
_,_,_ = analyse_algo_conflicts(f,directory,temp_in_C=temp_in_C,lattice_spacer='',threaded=True,num_bins=num_bins)
# then with lattice_spacer='TTTTT'
_,_,_ = analyse_algo_conflicts(f,directory,temp_in_C=temp_in_C,lattice_spacer=lattice_spacer,threaded=True,num_bins=num_bins)
_,_,_ = analyse_algo_conflicts(f,directory,temp_in_C=temp_in_C,lattice_spacer=lattice_spacer,threaded=False,num_bins=num_bins)
correct_dG_algo,mismatch_below_algo,mismatch_above_algo = analyse_algo_conflicts(f,directory,temp_in_C=temp_in_C,lattice_spacer=lattice_spacer,threaded=True)
# check for generalised algo conflicts (single mismatches)
analyse_row_conflicts(f,directory, temp_in_C=temp_in_C,lattice_spacer=lattice_spacer,normed=True,check_double_mismatches=_check_double_mismatches,num_bins=num_bins)
analyse_row_conflicts(f,directory,temp_in_C=temp_in_C,lattice_spacer=lattice_spacer,normed=False,check_double_mismatches=_check_double_mismatches,num_bins=num_bins)
_,_,_ = lattice_binding_energies_detailed(f,directory,temp_in_C=temp_in_C,lattice_spacer=lattice_spacer,threaded=True,num_bins=num_bins)
_,_,_ = lattice_binding_energies_detailed(f,directory,temp_in_C=temp_in_C,lattice_spacer=lattice_spacer,threaded=False,num_bins=num_bins)
correct_dG,mismatch_below_dG,mismatch_above_dG = lattice_binding_energies_detailed(f,directory,temp_in_C=temp_in_C,lattice_spacer=lattice_spacer,threaded=True,num_bins=num_bins)
correct_binding,mismatch_below_binding,mismatch_above_binding = lattice_binding_energies_simplified(f,directory,temp_in_C=temp_in_C,lattice_spacer=lattice_spacer,num_bins=num_bins)
list_of_pairs_of_lists = [[correct_dG,correct_binding],
[mismatch_below_dG,mismatch_below_binding],
[mismatch_above_dG,mismatch_above_binding]]
descriptors = ['correct binding',
'1 mismatch (below)',
'1 mismatch (above)']
return list_of_pairs_of_lists,descriptors
def algo_format_tile_name(n):
return len(n.split(";"))==3 and n.split(";")[2] in ['ne','nw','se','sw']
def analyse_row_conflicts(f,directory,temp_in_C,lattice_spacer='TTTTT',check_double_mismatches=False,normed=True,threaded=True,num_bins=-1):
'''Nupack binding() energy of row-conflicting tile (single-mismatch that can arise
during correct and incorrect growth) ... TODO: explain row-conflict'''
names, _, _, seqs, seqs_bt = read_strand_data_from_idt_order(f)
domains, domains_incl_biotin_labels = read_domains_from_idt_order(f)
is_algo_tile_set = True
for n in names:
if not algo_format_tile_name(n):
is_algo_tile_set = False
if not is_algo_tile_set:
print("The input tile set seems to not be an algorithmic tile set (based on it's tile names), hence I'm skipping execution of analyse_row_conflicts().")
return 0
if not os.path.exists(directory): os.makedirs(directory)
print(' analyse_row_conflicts(), lattice binding analysis from file: '+f+'\n plots will be placed in: ' + directory)
names_seqs = list(zip(names,seqs))
#threaded = False # _threaded # threaded code is not yet implemented
num_tiles_processed = 0
correct_growth_energies = []
single_mismatch_below_energies = []
single_mismatch_above_energies = []
double_mismatch_energies = []
double_mismatch_crazy_energies = []
thres = _bad_single_mismatch_threshold
helix_tiles = [[] for i in range(_num_helices+1)]
for helix in range(_num_helices):
for tile_name, tile_seq in names_seqs:
if tile_helix(tile_name) == helix:
helix_tiles[helix].append( (tile_name, tile_seq) )
for tile1_name, tile1_seq in names_seqs:
if num_tiles_processed%10==0 and num_tiles_processed>0: sys.stdout.write(str(num_tiles_processed))
num_tiles_processed += 1
sys.stdout.write('.'); sys.stdout.flush()
correct_growth_energies.append(lattice_binding_spacer(tile1_seq,
wc(tile1_seq.split(' ')[2]),
lattice_spacer,
wc(tile1_seq.split(' ')[1]),
temp_in_C))
# mismatch above:
for tile2_name,tile2_seq in helix_tiles[ (tile_helix(tile1_name)+1) % _num_helices ]:
# i.e. both tile1 and tile2 are "e"ast or both are "w"est within their pf blocks
if tile_dir(tile1_name)[1] == tile_dir(tile2_name)[1] and tile1_seq.split(' ')[2] != wc(tile2_seq.split(' ')[0]): # really I should check tile names intead of seqs here
e = lattice_binding_spacer(tile1_seq,
tile2_seq.split(' ')[0],
lattice_spacer,
wc(tile1_seq.split(' ')[1]),
temp_in_C)
single_mismatch_above_energies.append(e)
if e > thres:
print_lattice_mismatch_above(e, tile2_seq.split(' ')[0], tile2_name, tile1_seq, tile1_name, wc(tile1_seq.split(' ')[1]) )
# mismatch below:
h = (tile_helix(tile1_name)-1) % _num_helices
if h == 0: h = 16
for tile2_name,tile2_seq in helix_tiles[ h ]:
# i.e. one of tile1, tile2 is "e"ast and the other is "w"est within their pf blocks
if tile_dir(tile1_name)[1] != tile_dir(tile2_name)[1] and tile1_seq.split(' ')[1] != wc(tile2_seq.split(' ')[3]): # really I should check tile names intead of seqs here
e = lattice_binding_spacer(tile1_seq,
wc(tile1_seq.split(' ')[2]),
lattice_spacer,
tile2_seq.split(' ')[3],
temp_in_C)
single_mismatch_below_energies.append(e)
if e > thres:
print_lattice_mismatch_below(e, tile1_seq.split(' ')[2], tile1_seq, tile1_name, wc(tile2_seq.split(' ')[3]), tile2_name)
list_of_lists_of_energies= [correct_growth_energies,single_mismatch_below_energies,single_mismatch_above_energies]
labels=['correct growth ({} energies)'.format(len(correct_growth_energies)),
'one mismatch below ({} energies)'.format(len(single_mismatch_below_energies)),
'one mismatch above ({} energies)'.format(len(single_mismatch_above_energies))]
# Mismatch below and above (double mismatch)
if check_double_mismatches:
# double mismatches that do preserve row/direction
for tile_below_name,tile_below_seq in helix_tiles[ h ]: # h was defined above
double_mismatch_res = [global_thread_pool.apply_async(lattice_binding_spacer, args=(tile1_seq,
tile_above_seq.split(' ')[0],
lattice_spacer,
tile_below_seq.split(' ')[3],
temp_in_C)) for tile_above_name,tile_above_seq in helix_tiles[ (tile_helix(tile1_name)+1) % _num_helices ] \
if tile1_seq.split(' ')[1] != wc(tile_below_seq.split(' ')[3]) and \
tile1_seq.split(' ')[2] != wc(tile_above_seq.split(' ')[0]) and \
tile_dir(tile1_name)[1] != tile_dir(tile_below_name)[1] and \
tile_dir(tile1_name)[1] == tile_dir(tile_above_name)[1]]
double_mismatch_energies.extend([result.get() for result in double_mismatch_res])
# double mismatches that do not preserve row/direction
for tile_below_name,tile_below_seq in helix_tiles[ h ]: # h was defined above
double_mismatch_crazy_res = [global_thread_pool.apply_async(lattice_binding_spacer, args=(tile1_seq,
tile_above_seq.split(' ')[0],
lattice_spacer,
tile_below_seq.split(' ')[3],
temp_in_C)) for tile_above_name,tile_above_seq in helix_tiles[ (tile_helix(tile1_name)+1) % _num_helices ] \
if tile1_seq.split(' ')[1] != wc(tile_below_seq.split(' ')[3]) and \
tile1_seq.split(' ')[2] != wc(tile_above_seq.split(' ')[0])]
double_mismatch_crazy_energies.extend([result.get() for result in double_mismatch_crazy_res])
if check_double_mismatches:
list_of_lists_of_energies.extend( [double_mismatch_crazy_energies, double_mismatch_energies] )
labels.extend(['double mismatch, arbitrary ({} energies)'.format(len(double_mismatch_crazy_energies)),
'double mismatch, row/dir preserving ({} energies)'.format(len(double_mismatch_energies))])
histogram(list_of_lists_of_energies,labels=labels,
xaxis='energy (kcal/mol)',yaxis='',
title='growth on right (dG before-after binding, 1'+ ' and 2'*check_double_mismatches +' mismatch'+'es'*check_double_mismatches+', lattice spacer is '+lattice_spacer*(bool(lattice_spacer))+'empty string'*(not(bool(lattice_spacer)))+', not normalised'*(not normed)+')', #; for {} tiles)'.format(len(names_seqs)),
label_size=12,legend_font_size=9,
plot_dir=directory,normed=normed,xmin=-18,num_bins=num_bins) # ,xmin=4,xmax=18
return correct_growth_energies,single_mismatch_below_energies,single_mismatch_above_energies,double_mismatch_energies
def lattice_binding_energies_detailed(f,directory,temp_in_C=53,lattice_spacer='TTTTT',threaded=True,num_bins=-1):
'''Nupack binding() energy of tile binding of right (non-seeded end) of
nanotube with zero or one mistmatches'''
names, _, _, seqs, seqs_bt = read_strand_data_from_idt_order(f)
domains, domains_incl_biotin_labels = read_domains_from_idt_order(f)
if not os.path.exists(directory): os.makedirs(directory)
print('lattice_binding_energies_detailed(), lattice binding analysis from file:\n'+f+' plots will be placed in: ' + directory)
names_seqs = list(zip(names,seqs))
num_tiles_processed = 0
correct_growth_energies = []
single_mismatch_below_energies = []
single_mismatch_above_energies = []
bad_conflicts = ''
for tile1_name, tile1_seq in names_seqs:
tile1_domains = tile1_seq.split(' ')
if threaded:
if num_tiles_processed%10==0 and num_tiles_processed>0: sys.stdout.write(str(num_tiles_processed))
sys.stdout.write('.'); sys.stdout.flush()
num_tiles_processed += 1
correct_growth_energies.append(lattice_binding_spacer(tile1_seq,
wc(tile1_domains[2]),
lattice_spacer,
wc(tile1_domains[1]),
temp_in_C))
mismatch_below_res = [global_thread_pool.apply_async(lattice_binding_spacer, args=(tile1_seq,
#wc(tile1_domains[2])+'TTTTT'+tile2_seq.split(' ')[3],
wc(tile1_domains[2]),
lattice_spacer,
tile2_seq.split(' ')[3],
temp_in_C)) for _,tile2_seq in names_seqs if wc(tile1_seq.split(' ')[1])!=tile2_seq.split(' ')[3]]
single_mismatch_below_energies = single_mismatch_below_energies + [result.get() for result in mismatch_below_res]
mismatch_above_res = [global_thread_pool.apply_async(lattice_binding_spacer, args=(tile1_seq,
#tile2_seq.split(' ')[0]+'TTTTT'+wc(tile1_domains[1]),
tile2_seq.split(' ')[0],
lattice_spacer,
wc(tile1_domains[1]),
temp_in_C)) for _,tile2_seq in names_seqs if wc(tile1_seq.split(' ')[2])!=tile2_seq.split(' ')[0] ]
single_mismatch_above_energies = single_mismatch_above_energies + [result.get() for result in mismatch_above_res]
else: # not threaded
thres = _bad_lattice_binding_to_arbitrary_lattice_threshold
correct_growth_energies.append(lattice_binding_spacer(tile1_seq,
wc(tile1_domains[2]),
lattice_spacer,
wc(tile1_domains[1]),
temp_in_C))
for tile2_name,tile2_seq in names_seqs:
# mismatch below
if wc(tile1_domains[1])!=tile2_seq.split(' ')[3]:
e = lattice_binding_spacer(tile1_seq,
wc(tile1_domains[2]),
lattice_spacer,
tile2_seq.split(' ')[3],
temp_in_C)
single_mismatch_below_energies.append(e)
if e < thres:
bad_conflicts += print_lattice_mismatch_below(e, wc(tile1_seq.split(' ')[2]),
tile1_seq, tile1_name,
tile2_seq.split(' ')[3],
'mismatch: dom 3 of '+tile2_name)
bad_conflicts += str('{0:.2f}'.format(eval_colocated_end_pair(tile1_seq.split(' ')[1],tile2_seq.split(' ')[3],temp_in_C))) +' = eval_colocated_end_pair('+str( tile1_seq.split(' ')[1])+','+str(tile2_seq.split(' ')[3])+','+str(temp_in_C)+')\n'
# mismatch above
if wc(tile1_domains[2])!=tile2_seq.split(' ')[0]:
e = lattice_binding_spacer(tile1_seq,
tile2_seq.split(' ')[0],
lattice_spacer,
wc(tile1_domains[1]),
temp_in_C)
single_mismatch_above_energies.append(e)
if e < thres:
bad_conflicts += print_lattice_mismatch_above(e, tile2_seq.split(' ')[0],
'mismatch: dom 0 of tile '+tile2_name,
tile1_seq, tile1_name,
wc(tile1_seq.split(' ')[1]))
bad_conflicts += str('{0:.2f}'.format(eval_colocated_end_pair(tile2_seq.split(' ')[0], tile1_seq.split(' ')[2], temp_in_C)))+' = eval_colocated_end_pair('+str(tile2_seq.split(' ')[0])+','+str(tile1_seq.split(' ')[2])+','+str(temp_in_C)+')\n'
if not threaded:
with open(directory+'bad_arbitrary_lattice_conflicts - '+lattice_spacer+'.txt', 'w') as f:
f.write('Showing erroneous attachments with energy $\leq ' +str(thres) +'$ kcal/mol for single-match and single-mismatch binding events ' +'to arbitrary (correct or incorrect) lattices. These errors ' +'may or may not respect proofreading block tile-position.')
f.write('\\begin{footnotesize}\\begin{verbatim}\n')
f.write(bad_conflicts)
f.write('\\end{verbatim}\\end{footnotesize}\n')
histogram([correct_growth_energies,single_mismatch_below_energies,single_mismatch_above_energies],
labels=['correct growth ({} energies)'.format(len(correct_growth_energies)),
'one mismatch below ({} energies)'.format(len(single_mismatch_below_energies)),
'one mismatch above ({} energies)'.format(len(single_mismatch_above_energies))
],
xaxis='energy (-kcal/mol)',yaxis='',
label_size=12,legend_font_size=9,
title='growth on right (detailed binding, incorrect and correct lattices, lattice spacer '+lattice_spacer*(bool(lattice_spacer))+'is empty string'*(not(bool(lattice_spacer)))+')', #; for {} tiles)'.format(len(names_seqs)),
plot_dir=directory,xmin=-18,
num_bins=num_bins) # xmin=4,xmax=18
sys.stdout.write('\n')
return correct_growth_energies,single_mismatch_below_energies,single_mismatch_above_energies
def lattice_binding_energies_simplified(f,directory, temp_in_C=53, lattice_spacer='TTTTT',num_bins=-1):
'''Nupack binding() energy of tile binding of right of nanotube with no mistmatches
or one mismatch'''
names, _, _, seqs, seqs_bt = read_strand_data_from_idt_order(f)
domains, domains_incl_biotin_labels = read_domains_from_idt_order(f)
if not os.path.exists(directory): os.makedirs(directory)
print('lattice_binding_energies_simplified(), lattice binding analysis from file:\n'+f+' plots will be placed in: ' + directory)
names_seqs = list(zip(names,seqs))
threaded = _threaded
num_tiles_processed = 0
correct_growth_energies = []
single_mismatch_below_energies = []
single_mismatch_above_energies = []
for tile1_name, tile1_seq in names_seqs:
tile1_domains = tile1_seq.split(' ')
sys.stdout.write('.')
if num_tiles_processed%10==0 and num_tiles_processed>0: sys.stdout.write(str(num_tiles_processed))
#if num_tiles_processed%10==0: sys.stdout.write('.')
sys.stdout.flush()
num_tiles_processed += 1
#correct_growth_energies.append(binding(tile1_domains[1],wc(tile1_domains[1]),temp_in_C)+binding(tile1_domains[2],wc(tile1_domains[2]),temp_in_C))
correct_growth_energies.append(binding(tile1_domains[1]+tile1_domains[2],
wc(tile1_domains[2])+lattice_spacer+wc(tile1_domains[1]),
temp_in_C))
# mismatch below
results1 = [global_thread_pool.apply_async(binding, args=(tile1_domains[1]+tile1_domains[2],
wc(tile1_domains[2])+lattice_spacer+tile2_seq.split(' ')[3],
temp_in_C)) for _,tile2_seq in names_seqs if wc(tile1_seq.split(' ')[1])!=tile2_seq.split(' ')[3]]
single_mismatch_below_energies = single_mismatch_below_energies + [result.get() for result in results1]
# mismatch above
results2 = [global_thread_pool.apply_async(binding, args=(tile1_domains[1]+tile1_domains[2],
tile2_seq.split(' ')[0]+lattice_spacer+wc(tile1_domains[1]),
temp_in_C)) for _,tile2_seq in names_seqs if wc(tile1_seq.split(' ')[2])!=tile2_seq.split(' ')[0] ]
single_mismatch_above_energies = single_mismatch_above_energies + [result.get() for result in results2]
histogram([correct_growth_energies,single_mismatch_below_energies,single_mismatch_above_energies],
labels=['correct growth ({} energies)'.format(len(correct_growth_energies)),
'one mismatch below ({} energies)'.format(len(single_mismatch_below_energies)),
'one mismatch above ({} energies)'.format(len(single_mismatch_above_energies))
],
xaxis='energy (-kcal/mol)',yaxis='',
title='growth on right (simplified binding(), lattice spacer is '+lattice_spacer*(bool(lattice_spacer))+'empty string'*(not(bool(lattice_spacer)))+')',
label_size=12,legend_font_size=9,
plot_dir=directory,xmin=-18,
num_bins=num_bins) # ,xmin=4,xmax=18
return correct_growth_energies,single_mismatch_below_energies,single_mismatch_above_energies
def tile_helix(tile_name):
'''return helix number for tile with name tile_name'''
n = tile_num(tile_name)
d = tile_dir(tile_name)
#if n%2==0:
if d == 'nw' or d == 'se' : return 2*n
elif d == 'sw': return 2*n-1
elif d == 'ne': return (2*n+1) % _num_helices
else: sys.exit('Exiting program: Error in tile name. Was expecting a tile name with two semi-colons followed by one of nw,ne,se,sw. E.g. U6;00->00;ne.')
def tile_dir(name):
return name[10:12]
def tile_num(name):
return int(name[1:2])
def analyse_algo_conflicts(f,directory,temp_in_C=53,lattice_spacer='TTTTT',threaded=True,num_bins=-1):
'''binding() energy of algorithmic conflicting tile (single-mismatches that can arise
as the first error from a locally-correct lattice)'''
names, _, _, seqs, seqs_bt = read_strand_data_from_idt_order(f)
domains, domains_incl_biotin_labels = read_domains_from_idt_order(f)
if not os.path.exists(directory): os.makedirs(directory)
print(' the next few plots will be placed in: ' + directory)
names_seqs = list(zip(names,seqs))
print(' analysing single mismatches that are the first error in a \
locally correct lattice (aka first algorithmic conflicts) using \
the function analyse_algo_conflicts(), from file: ' + f +\
' (iterating through '+str(len(names_seqs))+' strands):')
num_tiles_processed = 0
correct_growth_energies = []
single_mismatch_below_energies = []
single_mismatch_above_energies = []
thres = _bad_first_algo_conflict_threshold
lower_thres_correct_binding = _lower_thres_correct_binding
bad_conflicts = ''
weak_correct_bindings = ''
for tile1_name, tile1_seq in names_seqs:
tile1_domains = tile1_seq.split(' ')
if threaded:
if num_tiles_processed%10==0 and num_tiles_processed>0: sys.stdout.write(str(num_tiles_processed))
sys.stdout.write('.')
sys.stdout.flush()
num_tiles_processed += 1
correct_growth_energies.append(lattice_binding_spacer(tile1_seq,
wc(tile1_seq.split(' ')[2]),
lattice_spacer,
wc(tile1_seq.split(' ')[1]),
temp_in_C))
mismatch_below_res = [global_thread_pool.apply_async(lattice_binding_spacer, args=(tile1_seq,
wc(tile1_seq.split(' ')[2]),
lattice_spacer,
wc(tile2_seq.split(' ')[1]),
temp_in_C)) \
for _,tile2_seq in names_seqs \
if tile1_seq.split(' ')[2]==tile2_seq.split(' ')[2] and \
tile1_seq.split(' ')[1]!=tile2_seq.split(' ')[1]]
single_mismatch_below_energies = single_mismatch_below_energies + [result.get() for result in mismatch_below_res]
mismatch_above_res = [global_thread_pool.apply_async(lattice_binding_spacer, args=(tile1_seq,
wc(tile2_seq.split(' ')[2]),
lattice_spacer,
wc(tile1_seq.split(' ')[1]),
temp_in_C)) \
for _,tile2_seq in names_seqs \
if tile1_seq.split(' ')[1]==tile2_seq.split(' ')[1] and \
tile1_seq.split(' ')[2]!=tile2_seq.split(' ')[2]]
single_mismatch_above_energies = single_mismatch_above_energies + [result.get() for result in mismatch_above_res]
else: # not threaded
e = lattice_binding_spacer(tile1_seq,
wc(tile1_seq.split(' ')[2]),
lattice_spacer,
wc(tile1_seq.split(' ')[1]),
temp_in_C)
correct_growth_energies.append(e)
if e > lower_thres_correct_binding:
weak_correct_bindings += print_lattice_binding(e, tile1_seq, tile1_name)
for tile2_name,tile2_seq in names_seqs:
# mismatch below
if tile1_seq.split(' ')[2]==tile2_seq.split(' ')[2] and tile1_seq.split(' ')[1]!=tile2_seq.split(' ')[1]:
e = lattice_binding_spacer(tile1_seq,
wc(tile1_seq.split(' ')[2]),
lattice_spacer,
wc(tile2_seq.split(' ')[1]), # there was previously an eror here
temp_in_C)
single_mismatch_below_energies.append(e)
if e < thres:
#print_lattice_mismatch_below(energy, top_output, tile_seq, tile_name, bottom_output, bottom_name):
bad_conflicts += print_lattice_mismatch_below(e, wc(tile1_seq.split(' ')[2]), tile1_seq, tile1_name, wc(tile2_seq.split(' ')[1]), 'mismatch: dom 3 of tile WC to dom 1 of '+tile2_name)
bad_conflicts += str('{0:.2f}'.format(eval_colocated_end_pair(tile1_seq.split(' ')[1],wc(tile2_seq.split(' ')[1]),temp_in_C))) +' = eval_colocated_end_pair('+str( tile1_seq.split(' ')[1])+','+str(wc(tile2_seq.split(' ')[1]))+','+str(temp_in_C)+')\n'
# mismatch above
if tile1_seq.split(' ')[1]==tile2_seq.split(' ')[1] and tile1_seq.split(' ')[2]!=tile2_seq.split(' ')[2]:
e = lattice_binding_spacer(tile1_seq,
wc(tile2_seq.split(' ')[2]),
lattice_spacer,
wc(tile1_seq.split(' ')[1]),
temp_in_C)
single_mismatch_above_energies.append(e)
if e < thres:
bad_conflicts += print_lattice_mismatch_above(e, wc(tile2_seq.split(' ')[2]), 'mismatch: dom 0 of tile WC to dom 2 of '+tile2_name, tile1_seq, tile1_name, wc(tile1_seq.split(' ')[1]))
bad_conflicts += str('{0:.2f}'.format(eval_colocated_end_pair(wc(tile2_seq.split(' ')[2]), tile1_seq.split(' ')[2], temp_in_C)))+' = eval_colocated_end_pair('+str(wc(tile2_seq.split(' ')[2]))+','+str(tile1_seq.split(' ')[2])+','+str(temp_in_C)+')\n'
if not threaded:
with open(directory+'bad_first_algo_conflicts - '+lattice_spacer+'.txt', 'w') as f:
f.write('Showing erroneous attachments with energy $\leq ' +str(thres) +'$ kcal/mol for single mismatches (algorithmic conflicts) that occur as ' +'the first error in a proofreading block.')
f.write('\\begin{footnotesize}\\begin{verbatim}\n')
f.write(bad_conflicts)
f.write('\\end{verbatim}\\end{footnotesize}\n')
#f.write('Done writing ' +str(len(bad_conflicts))+ ' characters')
#f.close()
with open(directory+'weak_correct_bindings - '+lattice_spacer+'.txt', 'w') as f:
f.write('Showing correct lattice attachments with energy $\geq '+str(lower_thres_correct_binding)+'$ kcal/mol.')
f.write('\\begin{footnotesize}\\begin{verbatim}\n')
f.write(weak_correct_bindings)
f.write('\\end{verbatim}\\end{footnotesize}\n')
histogram([correct_growth_energies,single_mismatch_below_energies,single_mismatch_above_energies],
labels=['correct growth ({} energies)'.format(len(correct_growth_energies)),
'one mismatch below ({} energies)'.format(len(single_mismatch_below_energies)),
'one mismatch above ({} energies)'.format(len(single_mismatch_above_energies))
],
xaxis='energy (kcal/mol)',yaxis='count',
title='correct and algorithmic error attachments to a valid lattice'+' (lattice spacer is empty)'*(not(bool(lattice_spacer))), #; for {} tiles)'.format(len(names_seqs)),
filename='algorithmic error (first), dG before and after, mismatches above and below, lattice spacer is '+lattice_spacer*(bool(lattice_spacer))+'empty string'*(not(bool(lattice_spacer))), #; for {} tiles)'.format(len(names_seqs)),
normed=0,label_size=12,legend_font_size=9,plot_dir=directory,
xmin=min(-17.1,min(correct_growth_energies+single_mismatch_below_energies+single_mismatch_above_energies)),
xmax=0,
num_bins=num_bins) # was ymax=282
histogram([correct_growth_energies,single_mismatch_below_energies+single_mismatch_above_energies],
labels=['{} correct attachments'.format(len(correct_growth_energies)),
'{} algorithmic errors'.format(len(single_mismatch_below_energies+single_mismatch_above_energies))
],
xaxis='energy (kcal/mol)',yaxis='count',
title= 'correct and algorithmic error attachments to a valid lattice'+' (lattice spacer is empty)'*(not(bool(lattice_spacer))), #; for {} tiles)'.format(len(names_seqs)),
filename='algorithmic error (first), dG before and after, lattice spacer is '+lattice_spacer*(bool(lattice_spacer))+'empty string'*(not(bool(lattice_spacer)))+')', #; for {} tiles)'.format(len(names_seqs)),
normed=0,label_size=12,legend_font_size=9,plot_dir=directory,
xmin=min(-17.1,min(correct_growth_energies+single_mismatch_below_energies+single_mismatch_above_energies)),
xmax=0,
num_bins=num_bins) # was ymax=282
sys.stdout.write('\n')
return correct_growth_energies,single_mismatch_below_energies,single_mismatch_above_energies
def print_lattice_binding(energy, tile_seq, tile_name):
s = '\n'
s += str('{0:.2f}'.format(energy)) + ' binding strength\n'
s += '<-' + wc(tile_seq.split(' ')[2])[::-1]+'\n'
s += ' /' +tile_seq.split(' ')[2]+'-'+tile_seq.split(' ')[3]+'-> '+tile_name+'\n'
s += ' \\' +tile_seq.split(' ')[1][::-1]+'-'+tile_seq.split(' ')[0][::-1]+'\n'
s += ' -' + wc(tile_seq.split(' ')[1]) +'->\n'
return s
def print_lattice_mismatch_below(energy, top_output, tile_seq, tile_name, bottom_output, bottom_name):
s = '\n'
s += str('{0:.2f}'.format(energy)) + ' strength mismatch\n'
s += '<-' + top_output[::-1]+'\n'
s += ' /' +tile_seq.split(' ')[2]+'-'+tile_seq.split(' ')[3]+'-> '+tile_name+'\n'
s += ' \\' +tile_seq.split(' ')[1][::-1]+'-'+tile_seq.split(' ')[0][::-1]+'\n'
s += ' -' + bottom_output +'-> '+bottom_name +'\n'
#print s ; sys.stdout.flush()
return s
def print_lattice_mismatch_above(energy, top_output, top_name, tile_seq, tile_name, bottom_output):
s = '\n'
s += str('{0:.2f}'.format(energy)) + ' strength mismatch\n'
s += '<-' + top_output[::-1]+' '+top_name + '\n'
s += ' /' +tile_seq.split(' ')[2]+'-'+tile_seq.split(' ')[3]+'-> '+tile_name+'\n'
s += ' \\' +tile_seq.split(' ')[1][::-1]+'-'+tile_seq.split(' ')[0][::-1]+'\n'
s += ' -' + bottom_output +'->\n'
#print s; sys.stdout.flush()
return s
def analyse_domains(names,seqs,seqs_bt,domains,domains_incl_biotin_labels,
seq_ws,seq_ws_bt,temp_in_C=53.0,num_domain_pairs=False,
directory='plots_tmp/',non_standard_tiles=0,pickle_data=0,num_bins=-1):
'''Analyse domain sequences'''
print('\ndomain analysis: plots will be placed in: ' + directory)
#print 'Analysing sequences from file:\n' + filename + '\n' +str(len(domains))+' unique domains, ' +str(len(domains_incl_biotin_labels))+' domains (leaving in biotin labels), ' + str(len([d for d in domains if len(d)==10])) + ' of length 10, '+str(len([d for d in domains if len(d)==11])) + ' of length 11' ; sys.stdout.flush()
print(' analysing tile input and output sec structure'); sys.stdout.flush()
if non_standard_tiles:
print(seq_ws)
seq_ws = [s.split()[0]+' '+s.split()[2]+' '+s.split()[3]+' '+s.split()[5] for s in seq_ws if len(s.split())==6]
print(seq_ws)
input_energies = analyse_input_pairs(seq_ws, temp_in_C)
output_energies = analyse_output_pairs(seq_ws, temp_in_C)
lattice_input_energies_correct_binding = analyse_lattice_input_energies_correct_binding(seq_ws, temp_in_C)
histogram([input_energies,lattice_input_energies_correct_binding,output_energies],
labels=['input','lattice (correct)','output'],xaxis='energy (kcal/mol)',yaxis='',
title='tile input, lattice and output secondrary structure (ss)',plot_dir=directory,
num_bins=num_bins)
scatter_plot_energies(input_energies, lattice_input_energies_correct_binding, xaxis='input sec struct', yaxis='lattice sec struct (correct binding)', title_info='',plot_dir=directory) #,xthreshold=9.3,ythreshold=9.3
# domain secondary structure
sys.stdout.write(' analysing domain secondary structure: '); sys.stdout.flush()
domain_energies = [pfunc(d, temp_in_C, NEGATE) for d in domains]
sys.stdout.write(str(len(domain_energies))+ ' domains\n')
strong_doms = [d.replace('/iBiodT/','T') for d in domains_incl_biotin_labels if '/iBiodT/' in d]
# in the following line I should replace d.replace('/iBiodT/','T') with d:
weak_doms = [d for d in domains_incl_biotin_labels if '/iBiodT/' not in d]
strong_doms_energies = [pfunc(d, temp_in_C, NEGATE) for d in strong_doms]
weak_doms_energies = [pfunc(d, temp_in_C, NEGATE) for d in weak_doms]
if len(strong_doms_energies)>0 and len(weak_doms_energies):
histogram([weak_doms_energies,strong_doms_energies],labels=['domains','domains with biotin'],
xaxis='energy (kcal/mol)',yaxis='',title='domain secondary structure energy histogram (via NUPACK pfunc)',
plot_dir=directory,
num_bins=num_bins)
else:
#weak_doms_energies.extend(strong_doms_energies)
histogram([domain_energies],labels=['domains'],xaxis='energy (kcal/mol)',yaxis='',
title='domain secondary structure (pfunc)',plot_dir=directory,
num_bins=num_bins)
if num_domain_pairs != -1:
sys.stdout.write(' analysing domain pairs: ');sys.stdout.flush()
domains_pairs = get_seq_pairs(domains)
print(str(len(domains_pairs)) + ' domain pairs to process'); sys.stdout.flush()
if num_domain_pairs>0:
domains_pairs = domains_pairs[:num_domain_pairs]
print('Warning: I am merely going to analyse a strict subset ('+str(len(domains_pairs))+') of all domain pairs')
#if num_domain_pairs: domains_pairs = domains_pairs[:num_domain_pairs]
#sys.stdout.write(' analysing domain pair secondary structure: '); sys.stdout.flush()
#print(str(len(domains_pairs)) + ' strand pairs to process');sys.stdout.flush()
non_wc_domains_pairs = [p for p in domains_pairs if p[0]!=wc(p[1])]
wc_domains_pairs = [p for p in domains_pairs if p[0]==wc(p[1])]
RNAduplex_non_wc_domains_pairs_energies = tile_pair_RNAduplex_energies(non_wc_domains_pairs,temp_in_C)
# unpaired strands get RNAduplex energy of ~ -100, so set those to -5:
if MORE_NEGAIVE_ENERGY_IS_MORE_FAVOURABLE:
RNAduplex_non_wc_domains_pairs_energies = [e if e > -100 else -5 for e in RNAduplex_non_wc_domains_pairs_energies ]
else:
RNAduplex_non_wc_domains_pairs_energies = [e if e < 100 else 5 for e in RNAduplex_non_wc_domains_pairs_energies ]
threshold = 3.0
print(str(len( [e for e in RNAduplex_non_wc_domains_pairs_energies if e > threshold ]))+' non-wc domain pairs with energy > '+str(threshold))
RNAduplex_wc_domains_pairs_energies = tile_pair_RNAduplex_energies(wc_domains_pairs,temp_in_C)
histogram([RNAduplex_wc_domains_pairs_energies,RNAduplex_non_wc_domains_pairs_energies],
labels=['wc domains','non-wc domains'],xaxis='energy (kcal/mol)',yaxis='',
title='domain pair (RNAduplex)',plot_dir=directory,
num_bins=num_bins)
non_wc_binding_pair_energies = tile_pair_NUPACK_binding_energies(non_wc_domains_pairs,temp_in_C)
scatter_plot_energies(non_wc_binding_pair_energies, RNAduplex_non_wc_domains_pairs_energies, xaxis='nupack domain pair binding', yaxis='RNAduplex domain pair (1999)', title_info='non-wc',plot_dir=directory) #,xthreshold=9.3,ythreshold=9.3
wc_binding_pair_energies = tile_pair_NUPACK_binding_energies(wc_domains_pairs,temp_in_C)
scatter_plot_energies(wc_binding_pair_energies, RNAduplex_wc_domains_pairs_energies, xaxis='nupack domain pair binding', yaxis='RNAduplex domain pair (1999)', title_info='wc',plot_dir=directory) #,xthreshold=9.3,ythreshold=9.3
histogram([wc_binding_pair_energies,non_wc_binding_pair_energies],
labels=[str(len(wc_binding_pair_energies))+ ' WC domain pairs',str(len(non_wc_binding_pair_energies))+' non-WC domain pairs'],
xaxis='energy (kcal/mol)',yaxis='',
normed=1,label_size=12,legend_font_size=9,
title='domain pair (nupack binding())',plot_dir=directory,
num_bins=num_bins)
#############################################################################
# Reading from idt-formatted files (with extension .idt)
#############################################################################
def get_lines_from_file(filename):
'''Returns uncommented and non-empty lines from a text file'''
with open(filename, 'r') as f:
lines = f.readlines()
lines = [line[:line.find('#')].strip() for line in lines]
lines = [line for line in lines if len(line) > 0]
return lines
def is_DNA_sequence(s):
return set(s.replace('/iBiodT/','').replace(' ','')).issubset('ACTG')
def get_DNA_sequences_from_idt_file(f):
return get_testtube_DNA_sequences_from_idt_file(f) + get_plate_DNA_sequences_from_idt_file(f)
def get_testtube_lines_from_idt_file(f):
lines = get_lines_from_file(f)
return [l for l in lines if len(l.split(','))==4 and is_DNA_sequence(l.split(',')[1]) and 'guard' not in l]
def get_plate_lines_from_idt_file(f):
'''Ignores any line containing the word guard'''
lines = get_lines_from_file(f)
return [l for l in lines if len(l.split(','))==4 and is_DNA_sequence(l.split(',')[2]) and 'guard' not in l]
def read_strand_data_from_idt_order(f):
tiles = get_testtube_lines_from_idt_file(f)
tile_names = [l.split(',')[0].replace(' ','').replace('\n','').replace('\r','').lstrip().strip() for l in tiles]
tile_seqs_without_biotin_labels = [tile.split(',')[1].replace('/iBiodT/','T').replace(' ','').replace('\n','').replace('\r','').lstrip().strip() for tile in tiles]
tile_seqs_with_biotin_labels = [tile.split(',')[1].replace(' ','').replace('\n','').replace('\r','').lstrip().strip() for tile in tiles]
tile_seqs_with_whitespace_between_doms = [tile.split(',')[1].replace('/iBiodT/','T').replace('\n','').replace('\r','').lstrip().strip() for tile in tiles]
tile_seqs_biotins_whitespace_between_doms = [tile.split(',')[1].replace('\n','').replace('\r','').lstrip().strip() for tile in tiles]
tiles = get_plate_lines_from_idt_file(f)
tile_names += [l.split(',')[1].replace(' ','').replace('\n','').replace('\r','').lstrip().strip() for l in tiles]
tile_seqs_without_biotin_labels += [tile.split(',')[2].replace('/iBiodT/','T').replace(' ','').replace('\n','').replace('\r','').lstrip().strip() for tile in tiles]
tile_seqs_with_biotin_labels += [tile.split(',')[2].replace(' ','').replace('\n','').replace('\r','').lstrip().strip() for tile in tiles]
tile_seqs_with_whitespace_between_doms += [tile.split(',')[2].replace('/iBiodT/','T').replace('\n','').replace('\r','').lstrip().strip() for tile in tiles]
tile_seqs_biotins_whitespace_between_doms += [tile.split(',')[2].replace('\n','').replace('\r','').lstrip().strip() for tile in tiles]
if len(tile_seqs_without_biotin_labels) == 0:
exit('There are 0 DNA strands. Did you give the correct input? Strands should be specified in idt file format. Run with --help to get an example.')
if len(tile_names) != len(tile_seqs_without_biotin_labels):
exit('Error: there are '+str(len(tile_names))+ ' strand names and '+str(len(tile_seqs_without_biotin_labels))+'strands!')
return tile_names, tile_seqs_without_biotin_labels, tile_seqs_with_biotin_labels, tile_seqs_with_whitespace_between_doms, tile_seqs_biotins_whitespace_between_doms
def read_domains_from_idt_order(filename, non_standard_tiles=0):
_, _, _, tile_seqs_with_whitespace_between_doms, tile_seqs_biotins_whitespace_between_doms = read_strand_data_from_idt_order(filename)
doms = []
doms_with_biotin = []
for l in tile_seqs_with_whitespace_between_doms:
doms.extend(l.replace('\n','').replace('\r','').split(' ') )
for l in tile_seqs_biotins_whitespace_between_doms:
doms_with_biotin.extend(l.replace('\n','').replace('\r','').split(' ') )
return list(set(doms)), list(set(doms_with_biotin))
def flatten(l):
return [item for sublist in l for item in sublist]
#############################################################################
# Plots
#############################################################################
def histogram(data, labels='', xaxis='', yaxis='', title='', filename='', plot_dir='', log_scale=False,
normed=1, xmin=0, xmax=0, ymin=0, ymax=0, label_size=12, legend_font_size=12, show_mean_min_max=1,
legend_location='upper left',num_bins=-1):
'''Plot a histogram. By default data is normalized (area under curve sums to 1 for each dataset (curve colour))'''
min_data = 10000; max_data = -10000
# Fix up function param types/formatting
if filename=='': filename=title
if type(data)!=list: data = [data]
if labels=='':labels=['' for _ in data]
if title!='': filename = title
else: filename = '_'.join(l for l in labels)
pp = PdfPages(plot_dir + filename + '.pdf')
colours = itertools.cycle(['r','b','g','y'])
for i,d in enumerate(data):
if d!= []: #assert len(d)!=0
min_data = min(min_data,min(d))
max_data = max(max_data,max(d))
if num_bins==-1: num_bins = int(min(50, math.ceil(math.sqrt(len(flatten(data))) +1) ))
step_size = (max_data-min_data)/num_bins
bins = [min_data+(i*step_size) for i in range(num_bins)]
for i,d in enumerate(data):
if d != []:
ax = plt.hist(d, bins,
density=normed, #normed=normed, # normed is depreciated, so using density instead
range=[2.0,18.0],
histtype='stepfilled',
facecolor=next(colours), alpha=0.5, edgecolor='black',
label=labels[i] + show_mean_min_max*((labels[i]!='')*', '+'mean='+str('{0:.2f}'.format(sum(d)/float(len(d))))+', min='+str('{0:.2f}'.format(min(d)))+', max='+str('{0:.2f}'.format(max(d)))))
if xmin!=0 or xmax!=0:
plt.xlim(xmin, xmax)
if ymin!=0 or ymax!=0:
plt.ylim(ymin, ymax)
else:
from numpy import isnan
if all(isnan(v) for v in ax[0]):
pp.savefig(); plt.clf(); pp.close()
print("Warning: unable to make plot with the following data\n"+str(data)+