Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: RNApolys
Path: tmp / main.sage
Views: 491
1
import pickle
2
import glob
3
from os.path import basename, splitext
4
from csv import writer
5
from datetime import datetime
6
import numpy as np
7
from sage.geometry.polyhedron.plot import cyclic_sort_vertices_2d
8
9
load('rna_poly.py')
10
#load('RNApoly2ggb.sage')
11
12
cone2poly = lambda c: Polyhedron(vertices = [[0,0,0,0]],rays=c.rays().matrix())
13
14
15
def get_pairs(s):
16
stack=[]
17
pair = {}
18
for i,c in enumerate(s):
19
if c=='(':
20
stack.append(i)
21
elif c==')':
22
j=stack.pop()
23
pair[i] = j
24
pair[j] = i
25
else:
26
pair[i] = -1
27
return pair
28
29
def get_arc_descriptions(pairs):
30
arcs = {}
31
for i in pairs:
32
j = pairs[i]
33
if j == -1:
34
continue
35
pair = tuple(sorted([i,j]))
36
if pair in arcs:
37
continue
38
else:
39
radius = pair[1]-pair[0]
40
x_center = pair[1]+pair[0]
41
arcs[pair] = (radius,(x_center,0))
42
return arcs
43
44
45
def plot_arcs(arcs,annotate=False,name='name',signature=(1,1,2,3),box=True):
46
P = Graphics()
47
if annotate:
48
max_rad = max([r for r,c in arcs.values()])
49
last_pt = max([r+c[0] for r,c in arcs.values()])
50
for i in range(-1,last_pt/20):
51
P+=line([((i+1)*20,0),((i+1)*20,max_rad)],color='black',alpha=0.15)
52
P+=text("{0}".format((i+1)*10),((i+1)*20,max_rad),horizontal_alignment='center',vertical_alignment='bottom',rgbcolor=(0,0,0),alpha=0.5)
53
P+=text("{0}: {1}".format(name, signature),(last_pt/2,-5),horizontal_alignment='center',vertical_alignment='top',rgbcolor=(0,0,0))
54
for pair in arcs:
55
radius, center = arcs[pair]
56
P += arc( center, radius,radius, sector=(0,pi) )
57
x,y,X,Y = [P.xmin(),P.ymin(),P.xmax(),P.ymax()]
58
width = X-x
59
height = Y-y
60
x -= 0.05*width
61
X += 0.05*width
62
y -= 0.07*height
63
Y += 0.07*height
64
65
P += line([(x,y),(x,Y),(X,Y),(X,y),(x,y)],color='black')
66
return P
67
68
def plot_2s(ss,name,signature):
69
p=get_pairs(ss)
70
a=get_arc_descriptions(p)
71
D=plot_arcs(a,True,name,signature)
72
return D
73
74
def plot_array(structs, name):
75
plot_list = []
76
for signature, reg in structs:
77
structure = reg.original_structure
78
P = plot_2s( structure, name, signature )
79
plot_list.append(P)
80
81
ncols = 1
82
nrows = len(plot_list)
83
G = graphics_array(plot_list, nrows, ncols )
84
return G
85
86
87
def get_bounded_cones_stripes_polygons( P, xbds=(-200,200), ybds=(-200,200) ):
88
x,X=xbds
89
y,Y=ybds
90
# This takes the (pre-computed) d1 slices from the polytope
91
# intersects them with the hyperplane y=b and drops the y coordinate.
92
# In other words, intersect each slice with y=b and the project to xz-plane.
93
slices = sliced_cones_b(P, 0)
94
95
# Returns each of the slices truncated to the region bounded by
96
# xbds \times ybds
97
bounded_regs = bounded_regions(slices, (), (x,X),(y,Y))
98
99
# Containers for output
100
cones = []
101
stripes = []
102
polygons = []
103
all_regions = []
104
for br in bounded_regs:
105
# s will be the slice from which br was constructed
106
s = br.original_region
107
108
# discriminant will be the number of rays defining the slice
109
# cones are bounded by 2 rays, stripes by 1 and polygons by 0.
110
discr = len(s.rays())
111
112
# We take a point in the corresponding polytope to be the signature
113
signature = tuple(br.original_vertex)#cone2poly(s.original_cone).representative_point()
114
115
if discr == 2:
116
cones.append((signature,br))
117
elif discr==1:
118
stripes.append((signature,br))
119
elif discr==0:
120
polygons.append((signature,br))
121
all_regions.append((signature,br))
122
123
# Finally we sort the regions by signature.
124
cones.sort()
125
stripes.sort()
126
polygons.sort()
127
all_regions.sort()
128
129
return (all_regions,cones,stripes,polygons)
130
131
def get_ray_directions(P,b=0):
132
# This takes the (pre-computed) d1 slices from the polytope
133
# intersects them with the hyperplane y=b and drops the y coordinate.
134
# In other words, intersect each slice with y=b and the project to xz-plane.
135
slices = sliced_cones_b(P, b)
136
137
# Containers for output
138
cone_dir = {}
139
stripe_dir = {}
140
all_dir = {}
141
142
for s in slices:
143
144
# discriminant will be the number of rays defining the slice
145
# cones are bounded by 2 rays, stripes by 1 and polygons by 0.
146
discr = len(s.rays())
147
148
if discr == 2:
149
cone_dir[s.rays()[0]]=True
150
cone_dir[s.rays()[1]]=True
151
all_dir[s.rays()[0]] = True
152
all_dir[s.rays()[1]] = True
153
elif discr==1:
154
stripe_dir[s.rays()[0]]=True
155
all_dir[s.rays()[0]] = True
156
157
158
159
return (all_dir.keys(),cone_dir.keys(),stripe_dir.keys())
160
161
def get_all_region_data(P,b=0):
162
163
# This takes the (pre-computed) d1 slices from the polytope
164
# intersects them with the hyperplane y=b and drops the y coordinate.
165
# In other words, intersect each slice with y=b and the project to xz-plane.
166
slices = sliced_cones_b(P, b)
167
168
# Containers for output
169
region_data = []
170
171
for s in slices:
172
173
# discriminant will be the number of rays defining the slice
174
# cones are bounded by 2 rays, stripes by 1 and polygons by 0.
175
discr = len(s.rays())
176
177
# We take a point in the corresponding polytope to be the signature
178
x,y,z,w = tuple(s.original_vertex)#cone2poly(s.original_cone).representative_point()
179
180
reg_type = np.nan
181
direction = np.nan
182
ray2 = np.nan
183
ray1 = np.nan
184
185
if discr == 0:
186
bounded = True
187
reg_type = 'p'
188
else:
189
bounded = False
190
if discr == 1:
191
reg_type = 's'
192
ray1 = tuple(s.rays()[0])
193
194
x_coord = ray1[0]
195
if x_coord < 0:
196
direction = 'l'
197
elif x_coord > 0:
198
direction = 'r'
199
else:
200
direction = 'a'
201
if discr == 2:
202
reg_type = 'w'
203
ray1 = tuple(s.rays()[0])
204
ray2 = tuple(s.rays()[1])
205
x_coord1 = ray1[0]
206
x_coord2 = ray2[0]
207
if x_coord1 <0 and x_coord2 < 0:
208
direction = 'l'
209
elif x_coord1 > 0 and x_coord2>0:
210
direction = 'r'
211
elif x_coord1*x_coord2 <0:
212
direction = 'b'
213
else:
214
direction = 'a'
215
region_data.append((b,x,y,z,w,reg_type,bounded,direction,ray1,ray2))
216
region_data.sort()
217
return region_data
218
219
def get_cones_stripes_polygons( P, b=0, signatures_only=False):
220
221
# This takes the (pre-computed) d1 slices from the polytope
222
# intersects them with the hyperplane y=b and drops the y coordinate.
223
# In other words, intersect each slice with y=b and the project to xz-plane.
224
slices = sliced_cones_b(P, b)
225
226
# Containers for output
227
cones = []
228
stripes = []
229
polygons = []
230
all_regions = []
231
232
for s in slices:
233
234
# discriminant will be the number of rays defining the slice
235
# cones are bounded by 2 rays, stripes by 1 and polygons by 0.
236
discr = len(s.rays())
237
238
# We take a point in the corresponding polytope to be the signature
239
signature = tuple(s.original_vertex)#cone2poly(s.original_cone).representative_point()
240
241
if signatures_only:
242
if discr == 2:
243
cones.append(signature)
244
elif discr==1:
245
stripes.append(signature)
246
elif discr==0:
247
polygons.append(signature)
248
all_regions.append(signature)
249
else:
250
if discr == 2:
251
cones.append((signature,s))
252
elif discr==1:
253
stripes.append((signature,s))
254
elif discr==0:
255
polygons.append((signature,s))
256
all_regions.append((signature,s))
257
258
259
# Finally we sort the regions by signature.
260
cones.sort()
261
stripes.sort()
262
polygons.sort()
263
all_regions.sort()
264
265
return (all_regions,cones,stripes,polygons)
266
267
def get_polygons(P, min_b=0, max_b=None):
268
269
if max_b is None:
270
max_b=min_b
271
max_b += 1
272
273
for b in range(min_b,max_b):
274
275
# This takes the (pre-computed) d1 slices from the polytope
276
# intersects them with the hyperplane y=b and drops the y coordinate.
277
# In other words, intersect each slice with y=b and the project to xz-plane.
278
slices = sliced_cones_b(P, b)
279
280
# Containers for output
281
polygons = []
282
283
for s in slices:
284
# discriminant will be the number of rays defining the slice
285
# polygons have 0 rays.
286
discr = len(s.rays())
287
if discr==0:
288
polygons.append(s)
289
290
polygons.sort()
291
return polygons
292
293
def slope(p,q):
294
x,y = p
295
a,b = q
296
if x == a:
297
# Warning, this actually doesn't make any sense.
298
return -infinity
299
return (y-b)/(x-a)
300
301
302
303
def find_pos_slope(P, min_b=0,max_b=None):
304
305
if max_b is None:
306
max_b=min_b
307
max_b += 1
308
output = []
309
for b in range(min_b,max_b):
310
polys = get_polygons(P,b)
311
312
for p in polys:
313
hull=cyclic_sort_vertices_2d(p.vertices())
314
for i,v in enumerate(hull):
315
u = hull[i-1]
316
if slope(u,v)>0:
317
output.append((b,tuple(p.original_vertex)))
318
return output
319
320
def rectangle_pl(a,b,color='blue'):
321
return polygon2d(((a,b),(a+1,b),(a+1,b+1),(a,b+1)),color=color)
322
323
def plot_positive_intervals(P,b_min,b_max):
324
output = find_pos_slope(P,b_min,b_max)
325
xy_pairs = sorted(list(set([(o[1][0],o[1][2]) for o in output])))
326
xy_dict = {xy:[] for xy in xy_pairs}
327
for o in output:
328
b=o[0]
329
sig=o[1]
330
x,y = sig[0],sig[2]
331
xy_dict[(x,y)].append(b)
332
for k in xy_pairs:
333
xy_dict[k].sort()
334
335
G = Graphics()
336
n = len(xy_pairs)
337
for i,k in enumerate(xy_pairs):
338
G += text('{0}'.format(k),(b_min-0.5,i+0.5),horizontal_alignment='right')
339
G += polygon2d(((b_min,i),(b_max+1,i),(b_max+1,i+1),(b_min,i+1)),fill=False,color='black',thickness=1)
340
for b in xy_dict[k]:
341
G += rectangle_pl(b,i,'red' if i%2 else 'blue')
342
return G
343
344
def iterate_sorted(dirname,pattern='*',order='N',reverse=False):
345
"""
346
Returns a list of filenames under `dirname` satisfying `pattern`. The output
347
is increasingly sorted by name (`N`) or size (`S`). You may choose
348
to reverse the order by using the `reverse` flag.
349
"""
350
fnames = glob.glob('{0}/{1}'.format(dirname,pattern))
351
352
if order=='S':
353
354
fname_size = sorted([(os.path.getsize(f),f) for f in fnames],reverse=reverse)
355
356
return [e[1] for e in fname_size]
357
358
elif order=='N':
359
return sorted(fnames,reverse=reverse)
360
return fnames
361
362
363
def max_z(regions,x0=-100):
364
"""
365
Sorts the regions by d values.
366
"""
367
maxi = -infinity
368
mini = infinity
369
unbounded_above = []
370
unbounded_below = []
371
maxi_bounded_region = None
372
mini_bounded_region = None
373
for br in regions:
374
c = br.original_region.original_cone
375
p = cone2poly(c)
376
lp,x=p.to_linear_program(return_variable=True,base_ring=QQ)
377
lp.add_constraint(x[0]==x0)
378
379
380
lp.set_objective(x[2])
381
try:
382
max_z = lp.solve()
383
except sage.numerical.mip.MIPSolverException as e:
384
if e.args[0].find('unbounded'):
385
max_z=infinity
386
pass
387
else:
388
raise e
389
390
if max_z == infinity:
391
unbounded_above.append(br)
392
elif maxi < max_z:
393
maxi = max_z
394
maxi_bounded_region = br
395
396
397
lp.set_objective(-x[2])
398
try:
399
min_z = lp.solve()
400
except sage.numerical.mip.MIPSolverException as e:
401
if e.args[0].find('unbounded'):
402
min_z = -infinity
403
pass
404
else:
405
raise e
406
407
if min_z == -infinity:
408
unbounded_below.append(br)
409
elif min_z < mini:
410
mini = min_z
411
mini_bounded_region = br
412
413
return ((maxi,maxi_bounded_region),(mini,mini_bounded_region),
414
unbounded_above, unbounded_below)
415
416
get_code = lambda x: x[x.find('/')+1:x.find('.')]
417
418
get_filename_with_code = lambda fnames, c: [x for x in fnames if x.find(c)>-1][0]
419
420
def gen_data(filename,xbds=(-200,200), ybds=(-200,200)):
421
422
P=RNAPolytope.construct_from_file(filename)
423
base_name = splitext(basename(filename))[0]
424
ar, c, s, p = get_cones_stripes_polygons( P )
425
426
# outfile_name = 'my_sample/out_data/{0}.csv'.format(base_name)
427
# with open(outfile_name,'wb') as outfile:
428
# outcsv = writer( outfile )
429
# for sign, reg in ar:
430
# outcsv.writerow(sign)
431
432
# outfile_name = 'my_sample/out_data/{0}--cones.csv'.format(base_name)
433
# with open(outfile_name,'wb') as outfile:
434
# outcsv = writer( outfile )
435
# for sign, reg in c:
436
# outcsv.writerow(sign)
437
438
# outfile_name = 'my_sample/out_data/{0}--stripes.csv'.format(base_name)
439
# with open(outfile_name,'wb') as outfile:
440
# outcsv = writer( outfile )
441
# for sign, reg in s:
442
# outcsv.writerow(sign)
443
444
# outfile_name = 'my_sample/out_data/{0}--poly.csv'.format(base_name)
445
# with open(outfile_name,'wb') as outfile:
446
# outcsv = writer( outfile )
447
# for sign, reg in p:
448
# outcsv.writerow(sign)
449
# print "[{0}] Finished generating data for {1}.".format(datetime.now(),base_name)
450
451
# P = sliced_cone_plots_b(P, abounds=xbds, cbounds=ybds)
452
# for sign,br in c:
453
# P += text('{0}'.format(sign), br.center() )
454
# for sign,br in s:
455
# P += text('{0}'.format(sign), br.center() )
456
# P.save('my_sample/out_data/pngs/{0}.png'.format(base_name),figsize=12)
457
458
signatures = [sign for sign,reg in ar]
459
xvals = list(set([s[0] for s in signatures]))
460
xvals.sort()
461
sign_dict={}
462
for x in xvals:
463
sign_dict[x]=[s for s in signatures if s[0]==x]
464
465
outfile_name = 'my_sample/out_data/{0}--z.csv'.format(base_name)
466
with open(outfile_name,'wb') as outfile:
467
outcsv = writer( outfile )
468
for x in xvals:
469
zs = [s[2] for s in sign_dict[x]]
470
minz = min(zs)
471
maxz = max(zs)
472
outcsv.writerow((x,minz,maxz))
473
474
print "[{0}] Finished processsing {1}.".format(datetime.now(),base_name)
475
print "*"*30
476
477
478
479
def generate_data():#xbds=(-200,200), ybds=(-200,200)):
480
with open('acc_fname.pkl', 'rb') as pk_file:
481
acc_dens_fnames_pairs=pickle.load(pk_file)
482
483
filenames = iterate_sorted('sobjs/','*.sobj')
484
485
for prefix, fname in acc_dens_fnames_pairs:
486
487
code = get_code(fname)
488
try:
489
filename = get_filename_with_code(filenames,code)
490
except IndexError:
491
print "[{0}] No sobj file found for {1}.".format(datetime.now(),code)
492
print "*"*30
493
494
continue
495
496
base_name = splitext(basename(filename))[0]
497
498
print "[{0}] Sarting process for {1}.".format(datetime.now(),base_name )
499
500
P = RNAPolytope.construct_from_file(filename)
501
502
try:
503
rnapoly2ggb(P,'{0}--{1}'.format(prefix,base_name))
504
except:
505
e = sys.exc_info()[0]
506
print "ERROR: {0} ... moving on.".format(e)
507
508
print "[{0}] Done {1}.".format(datetime.now(),base_name )
509
continue
510
511
512
ar, c, s, p = get_cones_stripes_polygons( P )
513
print "[{0}] Finished generating regions for {1}.".format(datetime.now(),base_name)
514
515
outfile_name = 'my_sample/161012-out_data/{0}--{1}.csv'.format(prefix,base_name)
516
with open(outfile_name,'wb') as outfile:
517
outcsv = writer( outfile )
518
for sign, reg in ar:
519
outcsv.writerow(sign)
520
521
outfile_name = 'my_sample/161012-out_data/{0}--{1}--cones.csv'.format(prefix,base_name)
522
with open(outfile_name,'wb') as outfile:
523
outcsv = writer( outfile )
524
for sign, reg in c:
525
outcsv.writerow(sign)
526
527
outfile_name = 'my_sample/161012-out_data/{0}--{1}--stripes.csv'.format(prefix,base_name)
528
with open(outfile_name,'wb') as outfile:
529
outcsv = writer( outfile )
530
for sign, reg in s:
531
outcsv.writerow(sign)
532
533
outfile_name = 'my_sample/161012-out_data/{0}--{1}--poly.csv'.format(prefix,base_name)
534
with open(outfile_name,'wb') as outfile:
535
outcsv = writer( outfile )
536
for sign, reg in p:
537
outcsv.writerow(sign)
538
print "[{0}] Finished generating data for {1}.".format(datetime.now(),base_name)
539
540
# P = sliced_cone_plots_b(P, abounds=xbds, cbounds=ybds)
541
# for sign,br in c:
542
# P += text('{0}'.format(sign), br.center() )
543
# for sign,br in s:
544
# P += text('{0}'.format(sign), br.center() )
545
# P.save('my_sample/161012-out_data/pngs/{0}--{1}.png'.format(prefix,base_name),figsize=12)
546
547
548
signatures = [sign for sign,reg in ar]
549
xvals = list(set([s[0] for s in signatures]))
550
xvals.sort()
551
sign_dict={}
552
for x in xvals:
553
sign_dict[x]=[s for s in signatures if s[0]==x]
554
555
outfile_name = 'my_sample/161012-out_data/{0}--{1}--z.csv'.format(prefix,base_name)
556
with open(outfile_name,'wb') as outfile:
557
outcsv = writer( outfile )
558
for x in xvals:
559
zs = [s[2] for s in sign_dict[x]]
560
minz = min(zs)
561
maxz = max(zs)
562
missing_zs=[z for z in range(minz,maxz) if z not in zs]
563
564
outcsv.writerow((x,minz,maxz,missing_zs))
565
566
if minz != 3*x:
567
print "[{0}] COUNTEREXAMPLE! : See {1} for the x_max != 3 min_z.".format(datetime.now(),base_name)
568
569
print "[{0}] Finished processsing {1}.".format(datetime.now(),base_name)
570
print "*"*30
571
572
573
def gen_max_norms():
574
# Function that should compute the maximum norm of a point in a polygonal
575
# region from the projection of the branching polytope.
576
filenames = iterate_sorted('my_sample/sobjs/','*.sobj')
577
578
with open('my_sample/max_norms.csv','w') as outfile:
579
580
Maxi = 0
581
for filename in filenames:
582
583
#filename = get_filename_with_code(filenames,code)
584
585
base_name = splitext(basename(filename))[0]
586
587
print "[{0}] Sarting process for {1}.".format(datetime.now(),base_name )
588
589
P = RNAPolytope.construct_from_file(filename)
590
591
ar, c, s, p = get_cones_stripes_polygons(P)
592
593
maxi = 0
594
for v in p.vertices_list():
595
V = vector(v)
596
if maxi <= V.norm():
597
maxi = V.norm()
598
if maxi >= Maxi:
599
Maxi = maxi
600
outfile.write('{0}\n'.format(n(maxi)))
601
print "Max norm: {0} ({1})".format(Maxi,n(Maxi))
602
603
def generate_cdata(xbds=(-200,200), ybds=(-200,200)):
604
filenames = iterate_sorted('my_sample/sobjs/','*.sobj')
605
606
for filename in filenames:
607
608
#filename = get_filename_with_code(filenames,code)
609
610
base_name = splitext(basename(filename))[0]
611
612
print "[{0}] Sarting process for {1}.".format(datetime.now(),base_name )
613
614
P = RNAPolytope.construct_from_file(filename)
615
616
ar, c, s, p = get_bounded_cones_stripes_polygons(P,xbds,ybds)
617
print "[{0}] Finished generating regions for {1}.".format(datetime.now(),base_name)
618
619
outfile_name = 'my_sample/161012-out_data/{0}.csv'.format(base_name)
620
with open(outfile_name,'wb') as outfile:
621
outcsv = writer( outfile )
622
for sign, reg in ar:
623
outcsv.writerow(sign)
624
625
outfile_name = 'my_sample/161012-out_data/{0}--cones.csv'.format(base_name)
626
with open(outfile_name,'wb') as outfile:
627
outcsv = writer( outfile )
628
for sign, reg in c:
629
outcsv.writerow(sign)
630
631
outfile_name = 'my_sample/161012-out_data/{0}--stripes.csv'.format(base_name)
632
with open(outfile_name,'wb') as outfile:
633
outcsv = writer( outfile )
634
for sign, reg in s:
635
outcsv.writerow(sign)
636
637
outfile_name = 'my_sample/161012-out_data/{0}--poly.csv'.format(base_name)
638
with open(outfile_name,'wb') as outfile:
639
outcsv = writer( outfile )
640
for sign, reg in p:
641
outcsv.writerow(sign)
642
print "[{0}] Finished generating data for {1}.".format(datetime.now(),base_name)
643
644
P = sliced_cone_plots_b(P, abounds=xbds, cbounds=ybds)
645
for sign,br in c:
646
P += text('{0}'.format(sign), br.center() )
647
for sign,br in s:
648
P += text('{0}'.format(sign), br.center() )
649
P.save('my_sample/161012-out_data/pngs/{0}.png'.format(base_name),figsize=12)
650
651
652
signatures = [sign for sign,reg in ar]
653
xvals = list(set([s[0] for s in signatures]))
654
xvals.sort()
655
sign_dict={}
656
for x in xvals:
657
sign_dict[x]=[s for s in signatures if s[0]==x]
658
659
outfile_name = 'my_sample/161012-out_data/{0}--z.csv'.format(base_name)
660
with open(outfile_name,'wb') as outfile:
661
outcsv = writer( outfile )
662
for x in xvals:
663
zs = [s[2] for s in sign_dict[x]]
664
minz = min(zs)
665
maxz = max(zs)
666
missing_zs=[z for z in range(minz,maxz) if z not in zs]
667
668
outcsv.writerow((x,minz,maxz,missing_zs))
669
670
if minz != 3*x:
671
print "[{0}] COUNTEREXAMPLE! : See {1} for the x_max != 3 min_z.".format(datetime.now(),base_name)
672
673
print "[{0}] Finished processsing {1}.".format(datetime.now(),base_name)
674
print "*"*30
675
676
677
def filter_bottom_left(L):
678
679
# in_Q4 tests whether a 2D vector is in quadrant IV
680
in_Q4 = lambda v: v[0]>=0 and v[1]<=0
681
SE_wedges = []
682
for s,R in L:
683
insert=False
684
for r in R.rays():
685
v = r.vector()
686
if in_Q4(v):
687
insert = True
688
break
689
if insert:
690
SE_wedges.append((s,R))
691
return SE_wedges
692
693
def get_xmax(L):
694
xmax = max([x[0][0] for x in L])
695
return [x for x in L if x[0][0]==xmax]
696
697
def get_relevant_regions(L):
698
bl = filter_bottom_left(L)
699
xm = get_xmax(L)
700
for x in xm:
701
if x not in bl:
702
bl.append(x)
703
bl.sort()
704
return bl
705
706
def generate_arc_diagrams():#xbds=(-200,200), ybds=(-200,200)):
707
with open('my_sample/acc_fname.pkl', 'rb') as pk_file:
708
acc_dens_fnames_pairs=pickle.load(pk_file)
709
710
filenames = iterate_sorted('my_sample/sobjs/','*.sobj')
711
712
for prefix, fname in acc_dens_fnames_pairs:
713
714
code = get_code(fname)
715
try:
716
filename = get_filename_with_code(filenames,code)
717
except IndexError:
718
print "[{0}] No sobj file found for {1}.".format(datetime.now(),code)
719
print "*"*30
720
721
continue
722
723
base_name = splitext(basename(filename))[0]
724
725
print "[{0}] Sarting process for {1}.".format(datetime.now(),base_name )
726
727
P = RNAPolytope.construct_from_file(filename)
728
729
ar, c, s, p = get_cones_stripes_polygons( P )
730
print "[{0}] Finished generating regions for {1}.".format(datetime.now(),base_name)
731
732
rr = get_relevant_regions( ar )
733
734
G=plot_array(rr,base_name)
735
736
outfile_name = 'my_sample/161020-out_data/{0}--{1}.svg'.format(prefix,base_name)
737
738
G.save(outfile_name,axes=False,figsize=24)
739
740
print "[{0}] Finished processsing {1}.".format(datetime.now(),base_name)
741
print "*"*30
742
def generate_d1_data():#xbds=(-200,200), ybds=(-200,200)):
743
with open('my_sample/acc_fname.pkl', 'rb') as pk_file:
744
acc_dens_fnames_pairs=pickle.load(pk_file)
745
746
filenames = iterate_sorted('my_sample/sobjs/','*.sobj')
747
748
for prefix, fname in acc_dens_fnames_pairs:
749
750
code = get_code(fname)
751
try:
752
filename = get_filename_with_code(filenames,code)
753
except IndexError:
754
print "[{0}] No sobj file found for {1}.".format(datetime.now(),code)
755
print "*"*30
756
757
continue
758
759
base_name = splitext(basename(filename))[0]
760
761
print "[{0}] Sarting process for {1}.".format(datetime.now(),base_name )
762
763
P = RNAPolytope.construct_from_file(filename)
764
765
766
ar = [(tuple(s.original_vertex),s) for s in P.d1_slices()]
767
768
769
signatures = [sign for sign,reg in ar]
770
xvals = list(set([s[0] for s in signatures]))
771
xvals.sort()
772
sign_dict={}
773
for x in xvals:
774
sign_dict[x]=[s for s in signatures if s[0]==x]
775
776
outfile_name = 'my_sample/161021-out_data/{0}--{1}--d1--z.csv'.format(prefix,base_name)
777
with open(outfile_name,'wb') as outfile:
778
outcsv = writer( outfile )
779
for x in xvals:
780
zs = [s[2] for s in sign_dict[x]]
781
minz = min(zs)
782
maxz = max(zs)
783
missing_zs=[z for z in range(minz,maxz) if z not in zs]
784
785
outcsv.writerow((x,minz,maxz,missing_zs))
786
787
if minz != 3*x:
788
print "[{0}] COUNTEREXAMPLE! : See {1} for the x_max != 3 min_z.".format(datetime.now(),base_name)
789
790
print "[{0}] Finished processsing {1}.".format(datetime.now(),base_name)
791
print "*"*30
792
793
794
795
796