7
7
8
8
import sys
9
9
import pickle
10
- import copy
10
+ import copy # !! Not used
11
11
import json
12
12
13
+ ## !! DEFINE MODULE LEVEL CONSTANTS AT THE TOP.
14
+
13
15
def load_json (file_name , period = 32 ):
14
16
"""
15
17
Loads a .json file in the correct way, so it can still be read by cadnano2.
@@ -20,7 +22,7 @@ def load_json(file_name, period=32):
20
22
The name and location of the *.json file
21
23
period : int
22
24
The period of repeating segments of the staples, in the square lattice this is by default 32.
23
-
25
+
24
26
Returns
25
27
-------
26
28
data : list
@@ -34,15 +36,15 @@ def load_json(file_name, period=32):
34
36
idx : dict
35
37
polarity
36
38
period
37
-
39
+
38
40
Example
39
41
-------
40
42
data, vstrands, num_helices, num_bases, idx, polarity, per = load_json('ruler_design_12nov_1353.json')
41
43
"""
42
44
with open (file_name ) as f :
43
45
data = json .load (f )
44
-
45
- vstrands = data ['vstrands' ]
46
+
47
+ vstrands = data ['vstrands' ] # !! vstrands also in outer script.
46
48
num_helices = len (vstrands )
47
49
num_bases = len (vstrands [0 ]['scaf' ])
48
50
idx = {} #Generate dictionary for translating helix_num to vstrand_num
@@ -61,7 +63,7 @@ def save_json(file_name):
61
63
file_name : str
62
64
The name and location of the *.json file
63
65
"""
64
- data ['vstrands' ] = vstrands
66
+ data ['vstrands' ] = vstrands # !! Accessing global variables
65
67
with open (file_name , 'wb' ) as outfile :
66
68
json .dump (data , outfile )
67
69
@@ -79,17 +81,18 @@ def comp_seq_FN(raw_sequence):
79
81
return antisense_seq
80
82
81
83
def stap_color_string_FN (stap_color_int ):
82
- return color_dc [stap_color_int ]
84
+ return color_dc [stap_color_int ] # !! Accessing global variables
83
85
84
86
def initVars ():
85
87
"""
86
88
Initialization of variables, gives the colors a name and initiates the null_bp
87
-
89
+
88
90
Example
89
91
-------
90
92
stap_color_dc, null_bp = initVars()
91
93
"""
92
- null_bp = [- 1 , - 1 ]
94
+ null_bp = [- 1 , - 1 ] # !! Redefining global variables
95
+ # Do this in a single statement:
93
96
stap_color_dc = {}
94
97
stap_color_dc [13369344 ] = 'red'
95
98
stap_color_dc [16204552 ] = 'red orange'
@@ -107,14 +110,14 @@ def initVars():
107
110
108
111
def openPickledFile (f ):
109
112
""" Loads a pickled file """
110
- input_file = file (f , 'r' )
113
+ input_file = file (f , 'r' ) # DONT USE file() !!
111
114
loaded_txt = pickle .load (input_file )
112
115
input_file .close ()
113
116
return loaded_txt
114
117
115
118
def openFile (f ):
116
119
"""" Opens a txt file in standard format. """
117
- input_file = file (f , 'r' )
120
+ input_file = file (f , 'r' ) # DONT USE file() !!
118
121
loaded_txt = input_file .read ()
119
122
input_file .close ()
120
123
return loaded_txt
@@ -240,7 +243,7 @@ def give_sequences(cadnano_file):
240
243
return scaf_output_ra , sorted_stap_output_ra , vstrands
241
244
242
245
def print_sequences ():
243
- print
246
+ print
244
247
print
245
248
#Print sorted staple strand sequences with annotations
246
249
for sub_ra in sorted_stap_output_ra :
@@ -256,7 +259,7 @@ def print_sequences():
256
259
note = ' short scaf strand\t ' + str (sub_ra [0 ]) + 'mer\t ' + str (sub_ra [1 ]) + '\t ' + 'start\t ' + str (sub_ra [2 ]) + '\t ' + 'end'
257
260
seq_note = seq + '\t ' + note
258
261
print seq_note
259
-
262
+
260
263
#Print additional miscellaneous information
261
264
num_stap_bases = 0
262
265
for sub_ra in sorted_stap_output_ra :
@@ -303,13 +306,12 @@ def print_sequences():
303
306
eight_mers = openPickledFile ('141110_1628_ortho_8mers_2303.txt' )
304
307
handle_color = {'cyan' : [0 , 5 ], 'blue' : [1 , 6 ], 'red orange' : [2 , 7 ], 'light gray' : [3 , 8 ], 'magenta' : [4 , 9 ]}
305
308
306
- for color in handle_color :
307
- for i in range (len (sorted_stap_output_ra )):
308
- if sorted_stap_output_ra [i ][0 ] == color :
309
- if sorted_stap_output_ra [i ][3 ][0 ] == 0 :
310
- sorted_stap_output_ra [i ][- 1 ] += 'TT' + eight_mers [handle_color [color ][0 ]]
311
- if sorted_stap_output_ra [i ][3 ][0 ] == 1 :
312
- sorted_stap_output_ra [i ][- 1 ] += 'TT' + eight_mers [handle_color [color ][1 ]]
309
+ for color , seqidx in handle_color .items ():
310
+ for row in sorted_stap_output_ra :
311
+ if row [0 ] == color and row [3 ][0 ] in (0 , 1 ):
312
+ # Add 'TT' to staple seq:
313
+ row [- 1 ] += 'TT' + eight_mers [seqidx [row [3 ][0 ]]]
314
+
313
315
print_sequences ()
314
316
315
317
@@ -323,15 +325,12 @@ def print_sequences():
323
325
324
326
print "before adding handles"
325
327
# print_sequences()
328
+ helix_to_handle_seq_idx = {16 : 0 , 18 : 1 }
329
+ for end_num , seqidx in handle_color .items ():
330
+ for row in sorted_stap_output_ra :
331
+ # row[3] is cadnano_start coordinate (helix, basenum), e.g. [2, 200]
332
+ if row [3 ][1 ] == end_num and row [3 ][0 ] in (16 , 18 ):
333
+ row [- 1 ] += 'TTTT' + comp_seq_FN (eight_mers [seqidx [0 if row [3 ][0 ] == 16 else 1 ]])
326
334
327
- for end_num in handle_color :
328
- for i in range (len (sorted_stap_output_ra )):
329
- if sorted_stap_output_ra [i ][3 ][1 ] == end_num :
330
- if sorted_stap_output_ra [i ][3 ][0 ] == 16 :
331
- sorted_stap_output_ra [i ][- 1 ] += 'TTTT' + comp_seq_FN (eight_mers [handle_color [end_num ][0 ]])
332
-
333
- if sorted_stap_output_ra [i ][3 ][0 ] == 18 :
334
- sorted_stap_output_ra [i ][- 1 ] += 'TTTT' + comp_seq_FN (eight_mers [handle_color [end_num ][1 ]])
335
335
print "after adding handles"
336
336
print_sequences ()
337
-
0 commit comments