summaryrefslogtreecommitdiff
path: root/cad/src/graphics/display_styles/DnaCylinderChunks.py
blob: 42bea277af373c399749b8f8cf426b1fce18c7ba (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
# Copyright 2008-2009 Nanorex, Inc.  See LICENSE file for details. 
"""
DnaCylinderChunks.py -- defines I{DNA Cylinder} display mode, which draws 
axis chunks as a cylinder in the chunk's color.

@author: Mark, Piotr
@version: $Id$
@copyright: 2008-2009 Nanorex, Inc.  See LICENSE file for details. 

History:

Mark 2008-02-12: Created by making a copy of CylinderChunks.py

piotr 080310: I have rewritten most of the code here from scratch. Most of 
the code computed within this function has to be moved to "getmemo" 
for a speed optimization. There are several issues with this rendering code,
including lack of highlighting and selection support, and some
weird behavior when modeltree is used to select individual strands.

The DNA display style requires DNA updater to be enabled.

There are four independent structures within the DNA display style:
axis, strands, struts and bases. Properties of these parts can be set
in the User Preferences dialog.

piotr 080317: Added POV-Ray rendering routines.

piotr 080318: Moved most of the data generation code to "getmemo"
Highlighting and selection now works. Thank you, Ninad!!!

("rainbow" strands still can't be selected nor highlighted, this will 
be fixed later)

piotr 080328: Added several "interactive" features: labels, base orientation
indicators, "2D" style.

piotr 080509: Code refactoring.

piotr 080520: Further code cleanup..

"""

import sys
import foundation.env as env
from graphics.drawing.CS_draw_primitives import drawcylinder
from graphics.drawing.CS_draw_primitives import drawpolycone
from graphics.drawing.CS_draw_primitives import drawpolycone_multicolor
from graphics.drawing.CS_draw_primitives import drawsphere
from graphics.drawing.drawers import drawCircle
from graphics.drawing.drawers import drawFilledCircle
from graphics.drawing.drawers import drawtext

from math import sin, cos, pi
from Numeric import dot, argmax, argmin, sqrt

from graphics.display_styles.displaymodes import ChunkDisplayMode

from geometry.VQT import V, Q, norm, cross, angleBetween

from utilities.debug import print_compact_traceback
from utilities.debug_prefs import debug_pref, Choice, Choice_boolean_True, Choice_boolean_False

from utilities.prefs_constants import hoverHighlightingColor_prefs_key
from utilities.prefs_constants import selectionColor_prefs_key
from utilities.constants import ave_colors, black, red, blue, white

from utilities.prefs_constants import atomHighlightColor_prefs_key

# 3D and 2D rendition display styles. Mark 2008-05-15
from utilities.prefs_constants import dnaRendition_prefs_key

# piotr 080309: user pereferences for DNA style
from utilities.prefs_constants import dnaStyleStrandsColor_prefs_key
from utilities.prefs_constants import dnaStyleAxisColor_prefs_key
from utilities.prefs_constants import dnaStyleStrutsColor_prefs_key
from utilities.prefs_constants import dnaStyleBasesColor_prefs_key
from utilities.prefs_constants import dnaStyleStrandsShape_prefs_key
from utilities.prefs_constants import dnaStyleBasesShape_prefs_key
from utilities.prefs_constants import dnaStyleStrandsArrows_prefs_key
from utilities.prefs_constants import dnaStyleAxisShape_prefs_key
from utilities.prefs_constants import dnaStyleStrutsShape_prefs_key
from utilities.prefs_constants import dnaStyleStrandsScale_prefs_key
from utilities.prefs_constants import dnaStyleAxisScale_prefs_key
from utilities.prefs_constants import dnaStyleBasesScale_prefs_key
from utilities.prefs_constants import dnaStyleAxisEndingStyle_prefs_key
from utilities.prefs_constants import dnaStyleStrutsScale_prefs_key

# piotr 080325 added more user preferences
from utilities.prefs_constants import dnaStrandLabelsEnabled_prefs_key
from utilities.prefs_constants import dnaStrandLabelsColor_prefs_key
from utilities.prefs_constants import dnaStrandLabelsColorMode_prefs_key
from utilities.prefs_constants import dnaBaseIndicatorsEnabled_prefs_key
from utilities.prefs_constants import dnaBaseIndicatorsColor_prefs_key
from utilities.prefs_constants import dnaBaseInvIndicatorsEnabled_prefs_key
from utilities.prefs_constants import dnaBaseInvIndicatorsColor_prefs_key
from utilities.prefs_constants import dnaBaseIndicatorsPlaneNormal_prefs_key
from utilities.prefs_constants import dnaStyleBasesDisplayLetters_prefs_key

try:
    from OpenGL.GLE import glePolyCone
    from OpenGL.GLE import gleGetNumSides 
    from OpenGL.GLE import gleSetNumSides 
    from OpenGL.GLE import gleExtrusion
    from OpenGL.GLE import gleTwistExtrusion
    from OpenGL.GLE import glePolyCylinder 
    from OpenGL.GLE import gleSetJoinStyle
    from OpenGL.GLE import TUBE_NORM_EDGE 
    from OpenGL.GLE import TUBE_NORM_PATH_EDGE 
    from OpenGL.GLE import TUBE_JN_ROUND
    from OpenGL.GLE import TUBE_JN_ANGLE
    from OpenGL.GLE import TUBE_CONTOUR_CLOSED 
    from OpenGL.GLE import TUBE_JN_CAP 
except:
    print "DNA Cylinder: GLE module can't be imported. Now trying _GLE"
    from OpenGL._GLE import glePolyCone 
    from OpenGL._GLE import gleGetNumSides 
    from OpenGL._GLE import gleSetNumSides 
    from OpenGL._GLE import gleExtrusion
    from OpenGL._GLE import gleTwistExtrusion    
    from OpenGL._GLE import glePolyCylinder 
    from OpenGL._GLE import gleSetJoinStyle
    from OpenGL._GLE import TUBE_NORM_EDGE 
    from OpenGL._GLE import TUBE_NORM_PATH_EDGE 
    from OpenGL._GLE import TUBE_JN_ROUND
    from OpenGL._GLE import TUBE_JN_ANGLE
    from OpenGL._GLE import TUBE_CONTOUR_CLOSED 
    from OpenGL._GLE import TUBE_JN_CAP 

# OpenGL functions are called by "realtime" draw methods.
from OpenGL.GL import glBegin
from OpenGL.GL import GL_BLEND
from OpenGL.GL import glEnd
from OpenGL.GL import glVertex3f
from OpenGL.GL import glVertex3fv
from OpenGL.GL import glColor3f
from OpenGL.GL import glColor3fv
from OpenGL.GL import glTranslatef
from OpenGL.GL import GL_LINE_STRIP        
from OpenGL.GL import GL_LINES
from OpenGL.GL import GL_POLYGON
from OpenGL.GL import glEnable
from OpenGL.GL import glDisable
from OpenGL.GL import GL_LIGHTING
from OpenGL.GL import GL_SMOOTH
from OpenGL.GL import glShadeModel
from OpenGL.GL import GL_COLOR_MATERIAL
from OpenGL.GL import GL_TRIANGLES
from OpenGL.GL import GL_FRONT_AND_BACK
from OpenGL.GL import GL_AMBIENT_AND_DIFFUSE
from OpenGL.GL import glMaterialfv
from OpenGL.GL import glPushMatrix
from OpenGL.GL import glPopMatrix
from OpenGL.GL import glRotatef
from OpenGL.GL import glScalef
from OpenGL.GL import glLineWidth
from OpenGL.GL import GL_QUADS
from OpenGL.GL import GL_LINE_SMOOTH

import colorsys

from OpenGL.GLU import gluUnProject

from dna.model.DnaLadderRailChunk import DnaStrandChunk

from model.chunk import Chunk

chunkHighlightColor_prefs_key = atomHighlightColor_prefs_key # initial kluge

# piotr 080519: made this method a global function
# it needs to be moved to a more appropriate location
def get_dna_base_orientation_indicators(chunk, normal):
    """
    Returns two lists for DNA bases perpendicular and anti-perpendicular
    to a plane specified by the plane normal vector.
    
    @param normal: normal vector defining a plane
    @type normal: float[3]
    """
    
    from utilities.prefs_constants import dnaBaseIndicatorsAngle_prefs_key
    from utilities.prefs_constants import dnaBaseIndicatorsDistance_prefs_key

    indicators_angle = env.prefs[dnaBaseIndicatorsAngle_prefs_key]
    indicators_distance = env.prefs[dnaBaseIndicatorsDistance_prefs_key]

    indicators = []
    inv_indicators = []

    if chunk.isStrandChunk(): 
        if chunk.ladder.axis_rail:
            n_bases = chunk.ladder.baselength()
            if chunk == chunk.ladder.strand_rails[0].baseatoms[0].molecule:
                chunk_strand = 0
            else:
                chunk_strand = 1
            for pos in range(0, n_bases):
                atom1 = chunk.ladder.strand_rails[chunk_strand].baseatoms[pos]
                atom2 = chunk.ladder.axis_rail.baseatoms[pos]
                vz = normal
                v2 = norm(atom1.posn()-atom2.posn())
                # calculate the angle between this vector 
                # and the vector towards the viewer
                a = angleBetween(vz, v2)
                if abs(a) < indicators_angle:
                    indicators.append(atom1)
                if abs(a) > (180.0 - indicators_angle):
                    inv_indicators.append(atom1)
    
    return (indicators, inv_indicators)


def get_all_available_dna_base_orientation_indicators(chunk, 
                                                      normal,
                                                      reference_indicator_dict = {},                                           
                                                      skip_isStrandChunk_check = False):
    """
    
    """
    # by Ninad
    #@TODO: Move this and other methods out of this file, into a general
    #helper pkg and module -- Ninad 2008-06-01
    from utilities.prefs_constants import dnaBaseIndicatorsAngle_prefs_key
    from utilities.prefs_constants import dnaBaseIndicatorsDistance_prefs_key

    indicators_angle = env.prefs[dnaBaseIndicatorsAngle_prefs_key]
    indicators_distance = env.prefs[dnaBaseIndicatorsDistance_prefs_key]

    all_indicators_dict = {}
        
    if skip_isStrandChunk_check:
        pass
        #caller has already done this check and is explicitely asking to
        #skip isStrandChunk test (for optimization) 
    else:        
        if chunk.isStrandChunk():
            return {}, {}
     
    if chunk.ladder.axis_rail:
        n_bases = chunk.ladder.baselength()
        if chunk == chunk.ladder.strand_rails[0].baseatoms[0].molecule:
            chunk_strand = 0
        else:
            chunk_strand = 1
        for pos in range(0, n_bases):
            atom1 = chunk.ladder.strand_rails[chunk_strand].baseatoms[pos]
            atom2 = chunk.ladder.axis_rail.baseatoms[pos]
            vz = normal
            v2 = norm(atom1.posn()-atom2.posn())
            # calculate the angle between this vector 
            # and the vector towards the viewer
            a = angleBetween(vz, v2)
            if abs(a) < indicators_angle:
                if not reference_indicator_dict.has_key(id(atom1)):
                    all_indicators_dict[id(atom1)] = atom1
            if abs(a) > (180.0 - indicators_angle):
                if not reference_indicator_dict.has_key(id(atom1)):
                    all_indicators_dict[id(atom1)] = atom1
    
    return all_indicators_dict
    


def get_dna_base_orientation_indicator_dict(chunk, 
                                            normal,
                                            reference_indicator_dict = {},
                                            reference_inv_indicator_dict = {},
                                            skip_isStrandChunk_check = False):
    """
    Returns two  dictionaries for DNA bases perpendicular and anti-perpendicular
    to a plane specified by the plane normal vector.
    """
    # by Ninad
    #@TODO: Move this and other methods out of this file, into a general
    #helper pkg and module -- Ninad 2008-06-01
    
    from utilities.prefs_constants import dnaBaseIndicatorsAngle_prefs_key
    from utilities.prefs_constants import dnaBaseIndicatorsDistance_prefs_key

    indicators_angle = env.prefs[dnaBaseIndicatorsAngle_prefs_key]
    indicators_distance = env.prefs[dnaBaseIndicatorsDistance_prefs_key]

    indicators_dict = {}
    inv_indicators_dict = {}
        
    if skip_isStrandChunk_check:
        pass
        #caller has already done this check and is explicitely asking to
        #skip isStrandChunk test (for optimization) 
    else:        
        if chunk.isStrandChunk():
            return {}, {}
     
    if chunk.ladder.axis_rail:
        n_bases = chunk.ladder.baselength()
        if chunk == chunk.ladder.strand_rails[0].baseatoms[0].molecule:
            chunk_strand = 0
        else:
            chunk_strand = 1
        for pos in range(0, n_bases):
            atom1 = chunk.ladder.strand_rails[chunk_strand].baseatoms[pos]
            atom2 = chunk.ladder.axis_rail.baseatoms[pos]
            vz = normal
            v2 = norm(atom1.posn()-atom2.posn())
            # calculate the angle between this vector 
            # and the vector towards the viewer
            a = angleBetween(vz, v2)
            if abs(a) < indicators_angle:
                if not reference_indicator_dict.has_key(id(atom1)) and \
                   not reference_inv_indicator_dict.has_key(id(atom1)):
                    indicators_dict[id(atom1)] = atom1
            if abs(a) > (180.0 - indicators_angle):
                if not reference_indicator_dict.has_key(id(atom1)) and \
                   not reference_inv_indicator_dict.has_key(id(atom1)):
                    inv_indicators_dict[id(atom1)] = atom1
    
    return (indicators_dict, inv_indicators_dict)

   
class DnaCylinderChunks(ChunkDisplayMode):
    """
    Implements DNA Cylinder display mode, which draws PAM-model DNA objects
    using simplified represenations for individual components.
    
    There are four components treated independently: "axis", "strands", "struts"
    and "nucleotides". Each of these components has its own set of settings.

    @note: Nothing else is rendered (no atoms, sugar atoms, etc) when 
        set to this display mode. 
        piotr 080316: Some of these features can be displayed as "nucleotides"

    @attention: This is still considered experimental.
    """
    # OLD limitations/known bugs, mostly fixed
    #
    # - Cylinders are always straight. DNA axis chunks with atoms that are not 
    # aligned in a straight line are not displayed correctly (i.e. they don't
    # follow a curved axis path. -- fixed 080310 piotr
    # - Hover highlighting does not work. fixed 080318 piotr: thank you, Ninad! 
    # - Selected chunks are not colored in the selection color. fix 080318 piotr 
    # - Cannot drag/move a selected cylinder interactively. fix 080318 piotr
    # - DNA Cylinders are not written to POV-Ray file. piotr: fixed 080317
    # - DNA Cylinders are not written to PDB file and displayed in QuteMolX.
    # --- this is a more general problem related to limitation of QuteMolX

    # mmp_code must be a unique 3-letter code, distinct from the values in 
    # constants.dispNames or in other display modes
    mmp_code = 'dna'  
    disp_label = 'DNA Cylinder' # label for statusbar fields, menu text, etc.
    featurename = "Set Display DNA Cylinder"
    
    icon_name = "modeltree/DnaCylinder.png"
    hide_icon_name = "modeltree/DnaCylinder-hide.png"
    ### also should define icon as an icon object or filename, 
    ### either in class or in each instance
    ### also should define a featurename for wiki help
    
    # Several of the methods below should be split into their own files.
    # piotr 082708
    
    def _compute_spline(self, data, idx, t):
        """
        Implements a Catmull-Rom spline. Interpolates between data[idx] 
        and data[idx+1]. 0.0 <= t <= 1.0.
        
        @param data: array with at least four values to be used for 
        interpolation. The following values are used: data[idx-1], data[idx],
        data[idx+1], data[idx+2]
        
        @param t: position (0 <= t <= 1)
        @type t: float
        
        @return: spline value at t
        """
        t2 = t * t
        t3 = t2 * t
        x0 = data[idx-1]
        x1 = data[idx]
        x2 = data[idx+1]
        x3 = data[idx+2]
        res = 0.5 * ((2.0 * x1) +
                     t * (-x0 + x2) +
                     t2 * (2.0 * x0 - 5.0 * x1 + 4.0 * x2 - x3) +
                     t3 * (-x0 + 3.0 * x1 - 3.0 * x2 + x3))
        return res

    def _get_rainbow_color(self, hue, saturation, value):
        """
        Gets a color of a hue range limited to 0 - 0.667 (red - blue color range).
        
        @param hue: color hue (0..1)
        @type hue: float
        
        @param saturation: color saturation (0..1)
        @type saturation: float
        
        @param value: color value (0..1)
        @type value: float
        
        @return: color for given (h,s,v) 
        """

        hue = 0.666 * (1.0 - hue)
        if hue < 0.0: 
            hue = 0.0
        if hue > 0.666: 
            hue = 0.666
        return colorsys.hsv_to_rgb(hue, saturation, value)

    def _get_full_rainbow_color(self, hue, saturation, value):
        """
        Gets a color from a full hue range (red - red). Can be used for
        color wrapping (color at hue == 0 is the same as color at hue == 1).
        
        @param hue: color hue (0..1)
        @type hue: float
        
        @param saturation: color saturation (0..1)
        @type saturation: float
        
        @param value: color value (0..1)
        @type value: float
        
        @return: color for given (h,s,v)         
        """
        if hue < 0.0: 
            hue = 0.0
        if hue > 1.0: 
            hue = 1.0
        return colorsys.hsv_to_rgb(hue, saturation, value)

    def _get_nice_rainbow_color(self, hue, saturation, value):
        """
        Gets a color of a hue limited to red-magenta range.

        @param hue: color hue (0..1)
        @type hue: float
        
        @param saturation: color saturation (0..1)
        @type saturation: float
        
        @param value: color value (0..1)
        @type value: float
        
        @return: color for given (h,s,v)                 
        """
        hue *= 0.8
        if hue < 0.0: 
            hue = 0.0
        if hue > 1.0: 
            hue = 1.0
        return colorsys.hsv_to_rgb(hue, saturation, value)

    def _get_base_color(self, base):
        """
        Returns a color according to DNA base type.
        Two-ring bases (G and A) have darker colors.
        
        G = red
        C = orange
        A = blue
        T = cyan
        
        @note: there should be a user pref setting for these
        
        @param base: DNA base symbol
        @type base: 1-char string
        
        @return: color corresponding to a given base
        """
        
        if base == "G":
            color = [1.0, 0.0, 0.0]
        elif base == "C":
            color = [1.0, 0.5, 0.0]
        elif base == "A":
            color = [0.0, 0.3, 0.9]
        elif base == "T":
            color = [0.0, 0.7, 0.8]
        else:
            color = [0.5, 0.5, 0.5]
        return color
    
    def _get_rainbow_color_in_range(self, pos, count, saturation, value):
        """
        Gets a color of a hue range limited to 0 - 0.667 (red - blue color range)
        correspoding to a "pos" value from (0..count) range.
        
        @param pos: position in (0..count range) 
        @type pos: integer
        
        @param count: limits the range of allowable values
        @type count: integer
        
        @param saturation: color saturation (0..1)
        @type saturation: float
        
        @param value: color value (0..1)
        @type value: float
        
        @return: color for given (pos, s, v) 
        """
        if count > 1: 
            count -= 1
        hue = float(pos)/float(count)        
        if hue < 0.0: 
            hue = 0.0
        if hue > 1.0: 
            hue = 1.0
        return self._get_rainbow_color(hue, saturation, value)

    def _get_full_rainbow_color_in_range(self, pos, count, saturation, value):
        """
        Gets a color from a full hue range (red to red). Can be used for
        color wrapping (color at hue == 0 is the same as color at hue == 1).
        The color corresponds to a "pos" value from (0..count) range.
        
        @param pos: position in (0..count range) 
        @type pos: integer
        
        @param count: limits the range of allowable values
        @type count: integer
        
        @param saturation: color saturation (0..1)
        @type saturation: float
        
        @param value: color value (0..1)
        @type value: float
        
        @return: color for given (pos, s, v) 
        """
        if count > 1: 
            count -= 1
        hue = float(pos)/float(count)        
        if hue < 0.0: 
            hue = 0.0
        if hue > 1.0: 
            hue = 1.0
        return self._get_full_rainbow_color(hue, saturation, value)

    def _get_nice_rainbow_color_in_range(self, pos, count, saturation, value):
        """
        Gets a color of a hue range limited to red-magenta range.
        The color corresponds to a "pos" value from (0..count) range.
        
        @param pos: position in (0..count) range 
        @type pos: integer
        
        @param count: limits the range of allowable values
        @type count: integer
        
        @param saturation: color saturation (0..1)
        @type saturation: float
        
        @param value: color value (0..1)
        @type value: float
        
        @return: color for given (pos, s, v) 
        """
        if count > 1: 
            count -= 1
        hue = float(pos)/float(count)        
        if hue < 0.0: 
            hue = 0.0
        if hue > 1.0: 
            hue = 1.0
        return self._get_nice_rainbow_color(hue, saturation, value)
    
    def _make_curved_strand(self, points, colors, radii):
        """
        Converts a polycylinder tube to a smooth, curved tube
        by spline interpolating of points, colors and radii.
        
        Assumes that len(points) == len(colors) == len(radii)
        
        @param points: consecutive points to be interpolated
        @type points: list of V or list of float[3]
        
        @param colors: colors corresponding to the points
        @type colors: list of colors
        
        @param radii: radii correspoding to individual points
        @type radii: list of radii
        
        @return: tuple of interpolated (points, colors, radii)
        """
        n = len(points)
        if n > 3:
            # Create lists for the interpolated positions, colors, and radii.
            new_points = [ None ] * (4*(n-2)-1)
            new_colors = [ None ] * (4*(n-2)-1)
            new_radii = [ 0.0 ] * (4*(n-2)-1)
            for p in range(0, (4*(n-2)-1)):
                new_points[p] = [ 0.0 ] * 3
                new_colors[p] = [ 0.0 ] * 3
            o = 1
            # Fill-in the lists by computing spline values at consecutive points.
            # Assume that the spline resolution equals 4.
            for p in range (1, n-2):
                for m in range (0, 4):
                    t = 0.25 * m
                    new_points[o] = self._compute_spline(points, p, t)
                    new_colors[o] = self._compute_spline(colors, p, t)
                    new_radii[o] = self._compute_spline(radii, p, t)
                    o += 1        
                    
            # Fill-in terminal positions.
            new_points[o] = self._compute_spline(points, p, 1.0)
            new_colors[o] = self._compute_spline(colors, p, 1.0)
            new_radii[o] = self._compute_spline(radii, p, 1.0)
            o += 1
            new_points[0] = 3.0 * new_points[1] \
                      - 3.0 * new_points[2] \
                      + new_points[3] 
            new_points[o] = 3.0 * new_points[o-1] \
                      - 3.0 * new_points[o-2] \
                      + new_points[o-3] 
            new_colors[0] = new_colors[1]
            new_colors[o] = new_colors[o - 1]
            new_radii[0] = new_radii[1]
            new_radii[o] = new_radii[o - 1]
            return (new_points, new_colors, new_radii)
        else:
            # if not enough points, just return the initial lists
            return (points, colors, radii)

    def _get_axis_positions(self, chunk, atom_list, color_style):
        """
        From an atom list create a list of positions extended by two dummy 
        positions at both ends. The list looks different depending on the
        used color style.

        Uses the chunk to convert atom coordinates to molecule-relative coords.
        
        @param chunk: chunk
        @type chunk: Chunk
        
        @param atom_list: list of axis chunk atoms
        @type atom_list: list of Atoms
        
        @param color_style: color style to be used to render the axis
        @type color_style: int
        
        @return: positions of the DNA axis cylinder 
        """
        n_atoms = len(atom_list)
        if color_style == 2 or color_style == 3: 
            # Use discrete colors. Below is an explanation of how the "discrete"
            # colors work. piotr 080827
            # The GLE "polycylinder" (and related methods, e.g. glePolyCone) 
            # interpolates colors between consecutive links. To create a sharp,
            # "discrete" color transition between two links, a new virtual link
            # of length 0 has to be introduced. The virtual link will be not
            # drawn, its purpose is only to make the color transitions sharp.
            # The virtual links are introduced in-between existing links,
            # for example:
            # Original sequence: (P0,C0) - (P1,C1) - (P2,C2)
            # will be replaced by:
            # New sequence: (P0,C0) - (P1,C0) - (P1,C1) - (P2,C1) - (P2,C2)
            # where (Px,Cx) is a (position,color) pair of an individual link.
            positions = [None] * (2 * n_atoms + 2)
            pos = 2
            for i in range (1, n_atoms):
                pos1 = chunk.abs_to_base(atom_list[i-1].posn())
                pos2 = chunk.abs_to_base(atom_list[i].posn())                
                positions[pos] = 0.5*(pos1 + pos2)
                positions[pos + 1] = 0.5*(pos1 + pos2)
                pos += 2
            positions[1] = chunk.abs_to_base(atom_list[0].posn())
            positions[0] = 2 * positions[1] - positions[2]
            positions[pos] = chunk.abs_to_base(atom_list[n_atoms - 1].posn())
            positions[pos + 1] = 2 * positions[pos] - positions[pos - 1]
        else:    
            positions = [None] * (n_atoms + 2)
            for i in range(1, n_atoms + 1):
                positions[i] = chunk.abs_to_base(atom_list[i-1].posn())
            if n_atoms > 1:
                positions[0] = 2 * positions[1] - positions[2]
            else:
                positions[0] = positions[1]
            if n_atoms > 1:    
                positions[n_atoms + 1] = 2 * positions[n_atoms] - positions[n_atoms - 1]
            else:
                positions[n_atoms + 1] = positions[n_atoms]                    
        return positions

    def _get_axis_atom_color_per_base(self, pos, strand_atoms_lists):
        """
        Gets a color of an axis atom depending on a base type.
        
        If the axis cylinder is colored 'per base', it uses two distinct
        colors: orange for G-C pairs, and teal for A-T pairs.
        Unknown bases are colored gray. 
        
        @param pos: atom position in axis chunk
        @type pos: integer
        
        @param strand_atoms_lists: lists of strand atoms (used to find out
        the name of DNA base).
        
        @return: axis atom color
        """
        color = [0.5, 0.5, 0.5]
        if len(strand_atoms_lists) > 1:
            if strand_atoms_lists[0] and strand_atoms_lists[1]:
                len1 = len(strand_atoms_lists[0])
                len2 = len(strand_atoms_lists[1])
                if pos>=0 and pos < len1 and pos < len2:
                    base_name = strand_atoms_lists[0][pos].getDnaBaseName()
                    if base_name == 'A' or base_name == 'T':
                        color = [0.0, 1.0, 0.5]
                    elif base_name == 'G' or base_name == 'C':
                        color = [1.0, 0.5, 0.0]
        return color                    

    def _get_axis_colors(self, atom_list, strand_atoms_lists, \
                         color_style, chunk_color, group_color):
        """
        Create a list of colors from an axis atom list, depending on the
        used color style.
        
        @param atom_list: list of axis atoms
        
        @param strand_atoms_list: lists of strand atoms
        
        @param color_style: color style used to draw the axis chunk
        
        @param chunk_color: color of the chunk
        
        @param group_color: 
        """
        n_atoms = len(atom_list)
        if color_style == 2 or \
           color_style == 3: 
            # Discrete colors.
            # For discrete color scheme, the number of internal nodes has to be
            # duplicated in order to deal with (defult) color interpolation
            # in glePolyCylinder routine (colors are interpolated on links
            # with zero length, so the interpolation effects are not visible.)
            # Also, look at comments in _get_axis_positions.
            colors = [None] * (2 * n_atoms + 2)
            pos = 2
            for i in range (1, n_atoms):
                if color_style == 2: 
                    color1 = self._get_rainbow_color_in_range(
                        i-1, n_atoms, 0.75, 1.0)
                    color2 = self._get_rainbow_color_in_range(
                        i, n_atoms, 0.75, 1.0)
                elif color_style == 3:
                    color1 = self._get_axis_atom_color_per_base(
                        i-1, strand_atoms_lists)
                    color2 = self._get_axis_atom_color_per_base(
                        i, strand_atoms_lists)
                colors[pos] = color1
                colors[pos + 1] = color2
                pos += 2
            colors[1] = colors[2] 
            colors[0] = colors[1] 
            colors[pos] = colors[pos - 1] 
            colors[pos + 1] = colors[pos] 
        else:
            colors = [None] * (n_atoms + 2)
            if color_style == 1:                    
                for i in range(1, n_atoms + 1):
                    colors[i] = self._get_rainbow_color_in_range(
                        i-1, n_atoms, 0.75, 1.0)
            elif color_style == 4:
                for i in range(1, n_atoms + 1):
                    colors[i] = group_color
            else:
                for i in range(1, n_atoms + 1):
                    colors[i] = chunk_color
            colors[0] = colors[1]
            colors[n_atoms + 1] = colors[n_atoms]

        return colors

    def _get_axis_radii(self, atom_list, color_style, shape, scale, ending_style):
        """
        Create a list of radii from the axis atom list.
        
        @param atom_list: list of axis atoms
        
        @param strand_atoms_list: lists of strand atoms
        
        @param color_style: color style used to draw the axis chunk
        
        @param shape: shape of the axis component
        
        @param scale: scale of the axis component
        
        @param ending_style: ending style of the axis component (blunt/tapered)
        """
        if shape == 1:
            rad = 7.0 * scale
        else:    
            rad = 2.0 * scale
        n_atoms = len(atom_list)
        if color_style == 2 or color_style == 3: 
            # Discrete colors.
            # For discrete colors duplicate a number of nodes.
            length = 2 * n_atoms + 2
            radii = [rad] * (length)
            # Append radii for the virtual ends.
            if ending_style == 2 or ending_style == 3:
                radii[1] = 0.0
                radii[2] = 0.5 * rad
                radii[3] = 0.5 * rad
            if ending_style == 1 or ending_style == 3:
                radii[length-4] = 0.5 * rad
                radii[length-3] = 0.5 * rad
                radii[length-2] = 0.0
        else:
            length = n_atoms + 2
            radii = [rad] * (length)
            if ending_style == 2 or ending_style == 3:
                radii[1] = 0.0
                radii[2] = 0.66 * rad
            if ending_style == 1 or ending_style == 3:
                radii[length-3] = 0.66 * rad
                radii[length-2] = 0.0        

        return radii

    def _get_strand_positions(self, chunk, atom_list):
        """
        From an strand atom list create a list of positions
        extended by two dummy positions at both ends.
        
        @param chunk: current chunk
        
        @param atom_list: list of strand atom positions 
        """
        n_atoms = len(atom_list)
        positions = [None] * (n_atoms + 2)
        for i in range(1, n_atoms + 1):
            positions[i] = chunk.abs_to_base(atom_list[i-1].posn())
        if n_atoms < 3:
            positions[0] = positions[1]
            positions[n_atoms + 1] = positions[n_atoms]
        else:
            positions[0] = 3.0 * positions[1] \
                     - 3.0 * positions[2] \
                     + positions[3]
            positions[n_atoms + 1] = 3.0 * positions[n_atoms] \
                     - 3.0 * positions[n_atoms - 1] \
                     + positions[n_atoms - 2] 

        return positions

    def _get_atom_rainbow_color(self, idx, n_atoms, start, end, length):
        """ 
        Calculates a "rainbow" color for a single atom.
        This code is partially duplicated in get_strand_colors.
        
        @param idx: atom index relative to the chunk length
        @type idx: integer
        
        @param n_atoms: number of atoms in the chunk
        @type n_atoms: integer
        
        @param start: index of the first chunk atom relative to the total strand length
        @type start: integer

        @param end: index of the last chunk atom relative to the total strand length
        @type end: integer
        
        @param length: total length of the strand
        @type length: integer
        """
        if n_atoms > 1:
            step = float(end - start)/float(n_atoms - 1)
        else:
            step = 0.0
        if length > 1:
            ilength = 1.0 / float(length - 1)
        else:
            ilength = 1.0
        q = ilength * (start + step * idx)
        ### if strand_direction == -1:
        ###    q = 1.0 - q
        return self._get_rainbow_color(q, 0.75, 1.0)


    def _get_strand_colors(self, atom_list, color_style, start, end, \
                           length, chunk_color, group_color):
        """
        From the strand atom list create a list of colors extended by two dummy 
        positions at both ends.
        
        @param atom_list: list of the strand atoms
        
        @param color_style: color style used to draw the axis chunk
        
        @param start: index of the first chunk atom relative to the total strand length
        @type start: integer

        @param end: index of the last chunk atom relative to the total strand length
        @type end: integer
        
        @param length: total length of the strand
        @type length: integer

        @param chunk_color: color of the chunk
        
        @param group_color: color of the group        
        """
        n_atoms = len(atom_list)
        colors = [None] * (n_atoms + 2)
        pos = 0
        if n_atoms > 1:
            step = float(end - start)/float(n_atoms - 1)
        else:
            step = 0.0
        if length > 1:
            ilength = 1.0 / float(length - 1)
        else:
            ilength = 1.0
        r = start
        for i in range(0, n_atoms):
            if color_style == 0:
                col = chunk_color
            elif color_style == 1:
                q =  r * ilength
                #if strand_direction == -1:
                #    q = 1.0 - q
                col = self._get_rainbow_color(q, 0.75, 1.0)
                r += step
            else:
                col = group_color
            colors[i + 1] = V(col[0], col[1], col[2])
            # Has to convert to array, otherwise spline interpolation
            # doesn't work (why - I suppose because tuples are immutable ?)
        colors[0] = colors[1]
        colors[n_atoms + 1] = colors[n_atoms]
        return colors

    def _get_strand_radii(self, atom_list, radius):
        """
        From the atom list create a list of radii extended by two dummy positions 
        at both ends.
        
        @param atom_list: list of strand atoms
        
        @param radius: scale factor of the strands
        """
        n_atoms = len(atom_list)
        radii = [None] * (n_atoms + 2)
        for i in range(1, n_atoms + 1):
            radii[i] = radius
        radii[0] = radii[1]
        radii[n_atoms + 1] = radii[n_atoms]
        return radii


    def _make_discrete_polycone(self, positions, colors, radii):
        """
        Converts a polycone_multicolor colors from smoothly interpolated 
        gradient to discrete (sharp edged) color scheme. The number of nodes 
        will be duplicated.
        
        @param positions: list of positions
        
        @param colors: list of colors
        
        @param radii: list of radii
        
        @return: (positions, colors, radii) tuple 
        
        @note: The method is written so it can be called in a following way:        
        pos, col, rad = _make_discrete_polycone(pos, col, rad)
        """
        # See a comment in "_get_axis_positions"
        n = len(positions)
        new_positions = []
        new_colors = []
        new_radii = []
        for i in range(0, n - 1):
            new_positions.append(positions[i])
            new_positions.append(positions[i])
            new_colors.append(colors[i])
            new_colors.append(colors[i+1])
            new_radii.append(radii[i])
            new_radii.append(radii[i])
        return (new_positions, new_colors, new_radii)


    def drawchunk(self, glpane, chunk, memo, highlighted):
        """
        Draw chunk in glpane in the whole-chunk display mode represented by 
        this ChunkDisplayMode subclass.

        Assume we're already in chunk's local coordinate system (i.e. do all
        drawing using atom coordinates in chunk.basepos, not chunk.atpos).

        If highlighted is true, draw it in hover-highlighted form (but note 
        that it may have already been drawn in unhighlighted form in the same
        frame, so normally the highlighted form should augment or obscure the
        unhighlighted form).

        Draw it as unselected, whether or not chunk.picked is true. See also
        self.drawchunk_selection_frame. (The reason that's a separate method
        is to permit future drawing optimizations when a chunk is selected
        or deselected but does not otherwise change in appearance or position.)

        If this drawing requires info about chunk which it is useful to 
        precompute (as an optimization), that info should be computed by our
        compute_memo method and will be passed as the memo argument
        (whose format and content is whatever self.compute_memo returns). 
        That info must not depend on the highlighted variable or on whether
        the chunk is selected.        
        """
        # ---------------------------------------------------------------------

        if not memo: 
            # nothing to render
            return

        if self.dnaExperimentalMode > 0: 
            # experimental models is drawn in drawchunk_realtime
            return

        positions, colors, radii, \
        arrows, struts_cylinders, base_cartoons = memo

        # render the axis cylinder        
        if chunk.isAxisChunk() and \
           positions: 
            # fixed bug 2877 (exception when "positions" 
            # is set to None) - piotr 080516
            n_points = len(positions)            
            if self.dnaStyleAxisShape > 0:
                # spherical ends    
                if self.dnaStyleAxisEndingStyle == 4:
                    drawsphere(colors[1], 
                                      positions[1], 
                                      radii[1], 2)
                    drawsphere(colors[n_points - 2], 
                                      positions[n_points - 2], 
                                      radii[n_points - 2], 2)                    

                # set polycone parameters
                gleSetJoinStyle(TUBE_JN_ANGLE | TUBE_NORM_PATH_EDGE 
                                | TUBE_JN_CAP | TUBE_CONTOUR_CLOSED) 
                
                # draw the polycone                
                if self.dnaStyleAxisColor == 1 \
                   or self.dnaStyleAxisColor == 2 \
                   or self.dnaStyleAxisColor == 3: 
                    # render discrete colors                
                    drawpolycone_multicolor([0, 0, 0, -2], 
                                                   positions, 
                                                   colors, 
                                                   radii)
                else:   
                    drawpolycone(colors[1], 
                                        positions, 
                                        radii)

        elif chunk.isStrandChunk(): # strands, struts and bases 
            gleSetJoinStyle(TUBE_JN_ANGLE | TUBE_NORM_PATH_EDGE 
                            | TUBE_JN_CAP | TUBE_CONTOUR_CLOSED) 

            if positions:                    
                if self.dnaStyleStrandsColor == 1:
                    # opacity value == -2 is a flag enabling 
                    # the "GL_COLOR_MATERIAL" mode, the
                    # color argument is ignored and colors array
                    # is used instead
                    ### positions, colors, radii = self._make_discrete_polycone(positions, colors, radii)
                    drawpolycone_multicolor([0, 0, 0, -2], 
                                                   positions,
                                                   colors,
                                                   radii)
                else:
                    drawpolycone(colors[1], 
                                        positions,
                                        radii)
    
                n_points = len(positions)
                
                # draw the ending spheres
                drawsphere(
                    colors[1], 
                    positions[1], 
                    radii[1], 2) 
                
                drawsphere(
                    colors[n_points - 2], 
                    positions[n_points - 2], 
                    radii[n_points - 2], 2) 
                
                # draw the arrows
                for color, pos, rad in arrows:
                    drawpolycone(color, pos, rad)
            # render struts
            for color, pos1, pos2, rad in struts_cylinders:
                drawcylinder(color, pos1, pos2, rad, True)
                
            # render nucleotides            
            if self.dnaStyleBasesShape > 0:
                for color, a1pos, a2pos, a3pos, normal, bname in base_cartoons:
                    if a1pos:
                        if self.dnaStyleBasesShape == 1: # sugar spheres
                            drawsphere(color, a1pos, self.dnaStyleBasesScale, 2)
                        elif self.dnaStyleBasesShape == 2: 
                            if a2pos:
                                # draw a schematic 'cartoon' shape
                                aposn = a1pos + 0.50 * (a2pos - a1pos)
                                bposn = a1pos + 0.66 * (a2pos - a1pos)
                                cposn = a1pos + 0.75 * (a2pos - a1pos)
                                
                                drawcylinder(color, 
                                    a1pos, 
                                    bposn, 
                                    0.20 * self.dnaStyleBasesScale, True) 
                                
                                if bname == 'G' or \
                                   bname == 'A': 
                                    # draw two purine rings                                
                                    drawcylinder(color, 
                                        aposn - 0.25 * self.dnaStyleBasesScale * normal,
                                        aposn + 0.25 * self.dnaStyleBasesScale * normal,
                                        0.7 * self.dnaStyleBasesScale, True)                            
                                    drawcylinder(color, 
                                        cposn - 0.25 * self.dnaStyleBasesScale * normal,
                                        cposn + 0.25 * self.dnaStyleBasesScale * normal,
                                        0.9 * self.dnaStyleBasesScale, True)
                                else:
                                    drawcylinder(color, 
                                        bposn - 0.25 * self.dnaStyleBasesScale * normal,
                                        bposn + 0.25 * self.dnaStyleBasesScale * normal,
                                        0.9 * self.dnaStyleBasesScale, True)                            
                            
    def drawchunk_selection_frame(self, glpane, chunk, selection_frame_color, memo, highlighted):
        """
        Given the same arguments as drawchunk, plus selection_frame_color, 
        draw the chunk's selection frame.

        (Drawing the chunk itself as well would not cause drawing errors
        but would presumably be a highly undesirable slowdown, especially if
        redrawing after selection and deselection is optimized to not have to
        redraw the chunk at all.)

        @note: in the initial implementation of the code that calls this method,
        the highlighted argument might be false whether or not we're actually
        hover-highlighted. And if that's fixed, then just as for drawchunk, 
        we might be called twice when we're highlighted, once with 
        highlighted = False and then later with highlighted = True.
        """
        drawchunk(self, glpane, chunk, selection_frame_color, memo, highlighted)
        return

    def drawchunk_realtime(self, glpane, chunk, highlighted=False):
        """
        Draws the chunk style that may depend on a current view.
        These are experimental features, work in progress as of 080319.
        For the DNA style, draws base orientation indicators and strand labels.
        080321 piotr: added better label positioning
        and 
        """

        def _realTextSize(text, fm):
            """
            Returns a pair of vectors corresponding to a width
            and a height vector of a rendered text. 
            Beware, this call is quite expensive.

            @params: text - text to be measured
                     fm - font metrics created for the text font:
                     fm = QFontMetrics(font)

            @returns: (v1,v2) - pair of vector covering the text rectangle
                                expressed in world coordinates
            """
            textwidth = fm.width(text)
            textheight = fm.ascent()
            x0, y0, z0 = gluUnProject(0, 0, 0)
            x1, y1, z1 = gluUnProject(textwidth, 0, 0)
            x2, y2, z2 = gluUnProject(0, textheight, 0)
            return (V(x1-x0, y1-y0, z1-z0),V(x2-x0, y2-y0, z2-z0))

        def _get_screen_position_of_strand_atom(strand_atom):
            """
            For a given strand atom, find its on-screen position.
            """

            axis_atom = strand_atom.axis_neighbor()
            
            if axis_atom:
                mol = axis_atom.molecule
                axis_atoms = mol.ladder.axis_rail.baseatoms
                if axis_atoms is None:
                    return None
                n_bases = len(axis_atoms)
                pos = axis_atoms.index(axis_atom)
                atom0 = axis_atom
                if pos < n_bases - 1:
                    atom1 = axis_atoms[pos + 1]
                    dpos = atom1.posn() - atom0.posn()
                else:
                    atom1 = axis_atoms[pos - 1]
                    atom2 = axis_atoms[pos]
                    dpos = atom2.posn() - atom1.posn()
                last_dpos = dpos
                out = mol.quat.unrot(glpane.out)
                if flipped:
                    dvec = norm(cross(dpos, -out))
                else:
                    dvec = norm(cross(dpos, out))
                pos0 = axis_atom.posn()
                if not highlighted:
                    pos0 = chunk.abs_to_base(pos0)
                pos1 = pos0 + ssep * dvec
                pos2 = pos0 - ssep * dvec
                if mol.ladder.strand_rails[0].baseatoms[pos] == strand_atom:
                    return (pos1, dpos)
                elif mol.ladder.strand_rails[1].baseatoms[pos] == strand_atom:
                    return (pos2, -dpos)

            return (None, None)

        
        def _draw_arrow(atom):
            """
            Draws a 2-D arrow ending of a DNA strand at 3' atom.
            
            @return: True if arrow was drawn, False if this is not a 3' atom.
            """
            if atom:
                strand = atom.molecule.parent_node_of_class(
                    atom.molecule.assy.DnaStrand)
                if atom == strand.get_three_prime_end_base_atom():
                    ax_atom = atom.axis_neighbor()
                    a_neighbors = ax_atom.axis_neighbors()
                    pos0, dvec = _get_screen_position_of_strand_atom(atom)
                    ovec = norm(cross(dvec, chunk.quat.unrot(glpane.out)))
                    pos1 = pos0 + 0.5 * dvec
                    if mode == 1:
                        pos1 += 1.0 * dvec
                    glVertex3fv(pos0)
                    glVertex3fv(pos1)
                    dvec = norm(dvec)
                    avec2 = pos1 - 0.5 * (dvec - ovec) - dvec
                    glVertex3fv(pos1)
                    glVertex3fv(avec2)
                    avec3 = pos1 - 0.5 * (dvec + ovec) - dvec
                    glVertex3fv(pos1)
                    glVertex3fv(avec3)
                    return True
            return False
        
        def _draw_external_bonds():
            """
            Draws external bonds between different chunks of the same group.
            """
            # note: this is a local function inside 'def drawchunk_realtime'.
            # it has no direct relation to ChunkDrawer._draw_external_bonds.
            for bond in chunk.externs:
                if bond.atom1.molecule.dad == bond.atom2.molecule.dad: # same group
                    if bond.atom1.molecule != bond.atom2.molecule: # but different chunks
                        pos0, dvec = _get_screen_position_of_strand_atom(bond.atom1)
                        pos1, dvec = _get_screen_position_of_strand_atom(bond.atom2)
                        if pos0 and pos1:
                            glVertex3f(pos0[0], pos0[1], pos0[2])
                            glVertex3f(pos1[0], pos1[1], pos1[2])
        
        def _light_color(color):
            """
            Make a lighter color.
            """
            lcolor = [0.0, 0.0, 0.0]
            lcolor[0] = 0.5 * (1.0 + color[0])
            lcolor[1] = 0.5 * (1.0 + color[1])
            lcolor[2] = 0.5 * (1.0 + color[2])
            return lcolor

        # note: this starts the body of def drawchunk_realtime,
        # after it defines several local functions.
        
        from utilities.constants import lightgreen
        from PyQt4.Qt import QFont, QString, QColor, QFontMetrics
        from widgets.widget_helpers import RGBf_to_QColor
        from dna.model.DnaLadderRailChunk import DnaStrandChunk

        labels_enabled = env.prefs[dnaStrandLabelsEnabled_prefs_key]
        indicators_enabled = env.prefs[dnaBaseIndicatorsEnabled_prefs_key]

        if chunk.color: # sometimes the chunk.color is not defined
            chunk_color = chunk.color
        else:
            chunk_color = white

        hhColor = env.prefs[hoverHighlightingColor_prefs_key]
        selColor = env.prefs[selectionColor_prefs_key]

        if self.dnaExperimentalMode == 0:

            if indicators_enabled: # draw the orientation indicators
                self.dnaStyleStrandsShape = env.prefs[dnaStyleStrandsShape_prefs_key]
                self.dnaStyleStrutsShape = env.prefs[dnaStyleStrutsShape_prefs_key]
                self.dnaStyleBasesShape = env.prefs[dnaStyleBasesShape_prefs_key]
                indicators_color = env.prefs[dnaBaseIndicatorsColor_prefs_key]
                inv_indicators_color = env.prefs[dnaBaseInvIndicatorsColor_prefs_key]                
                inv_indicators_enabled = env.prefs[dnaBaseInvIndicatorsEnabled_prefs_key]

                plane_normal_idx = env.prefs[dnaBaseIndicatorsPlaneNormal_prefs_key]
                
                plane_normal = glpane.up
                
                if plane_normal_idx == 1:
                    plane_normal = glpane.out
                elif plane_normal_idx == 2:
                    plane_normal = glpane.right
                    
                indicators, inv_indicators = get_dna_base_orientation_indicators(chunk, plane_normal)
                 
                if highlighted:
                    for atom in indicators:
                        drawsphere(
                            indicators_color, 
                            atom.posn(), 1.5, 2)
                    if inv_indicators_enabled:
                        for atom in inv_indicators:
                            drawsphere(
                                inv_indicators_color, 
                                atom.posn(), 1.5, 2)
                else:
                    for atom in indicators:
                        drawsphere(
                            indicators_color, 
                            chunk.abs_to_base(atom.posn()), 1.5, 2)                
                    if inv_indicators_enabled:
                        for atom in inv_indicators:
                            drawsphere(
                                inv_indicators_color, 
                                chunk.abs_to_base(atom.posn()), 1.5, 2)
    
            if chunk.isStrandChunk():       
                if hasattr(chunk, "_dnaStyleExternalBonds"):
                    for exbond in chunk._dnaStyleExternalBonds:
                        atom1, atom2, color = exbond
                        pos1 = atom1.posn()
                        pos2 = atom2.posn()
                        if chunk.picked:
                            color = selColor
                        if highlighted:
                            color = hhColor
                        else:
                            pos1 = chunk.abs_to_base(pos1)
                            pos2 = chunk.abs_to_base(pos2)
                        drawsphere(color, pos1, self.dnaStyleStrandsScale, 2)
                        drawsphere(color, pos2, self.dnaStyleStrandsScale, 2)
                        drawcylinder(color, pos1, pos2, self.dnaStyleStrandsScale, True)              

                if self.dnaStyleBasesDisplayLetters: 
                    # calculate text size
                    font_scale = int(500.0 / glpane.scale)
                    if sys.platform == "darwin":
                        font_scale *= 2                        
                    if font_scale < 9:
                        font_scale = 9
                    if font_scale > 50:
                        font_scale = 50

                    # create the label font
                    labelFont = QFont( QString("Lucida Grande"), font_scale)
                    # get font metrics for the current font
                    fm = QFontMetrics(labelFont)
                    glpane.qglColor(RGBf_to_QColor(black))
                    # get text size in world coordinates
                    label_text = QString("X")
                    dx, dy = _realTextSize(label_text, fm)
                    # disable lighting
                    glDisable(GL_LIGHTING)            
                    for atom in chunk.atoms.itervalues():
                        # pre-compute atom position
                        if not highlighted:
                            textpos = chunk.abs_to_base(atom.posn()) + 3.0 * glpane.out
                        else:
                            textpos = atom.posn() + 3.0 * glpane.out
                        # get atom base name
                        label_text = QString(atom.getDnaBaseName())
                        # move the text center to the atom position
                        textpos -= 0.5*(dx + dy)   
                        # render the text
                        glpane.renderText(textpos[0], textpos[1], textpos[2], 
                                          label_text, labelFont)                    

                    # done, enable lighting
                    glEnable(GL_LIGHTING)

                if labels_enabled: 
                    # draw the strand labels

                    self.dnaStyleStrandsShape = env.prefs[dnaStyleStrandsShape_prefs_key]
                    self.dnaStyleStrutsShape = env.prefs[dnaStyleStrutsShape_prefs_key]
                    self.dnaStyleBasesShape = env.prefs[dnaStyleBasesShape_prefs_key]

                    labels_color_mode = env.prefs[dnaStrandLabelsColorMode_prefs_key]

                    if labels_color_mode == 1:
                        labels_color = black
                    elif labels_color_mode == 2:
                        labels_color = white
                    else: 
                        labels_color = env.prefs[dnaStrandLabelsColor_prefs_key]

                    # calculate the text size
                    font_scale = int(500.0 / glpane.scale)
                    if sys.platform == "darwin":
                        font_scale *= 2                        
                    if font_scale < 9:
                        font_scale = 9
                    if font_scale > 50:
                        font_scale = 50

                    if chunk.isStrandChunk():                
                        if self.dnaStyleStrandsShape > 0 or \
                           self.dnaStyleBasesShape > 0 or \
                           self.dnaStyleStrutsShape > 0:                   

                            # REVIEW: Is the following comment still valid?
                            #
                            # Q. the following is copied from DnaStrand.py
                            # I need to find a 5' sugar atom of the strand
                            # is there any more efficient way of doing that?
                            # this is terribly slow... I need something like
                            # "get_strand_chunks_in_bond_direction"...
                            #
                            # A [bruce 081001]: yes, there is an efficient way.
                            # The strand rail atoms are in order, and it's possible
                            # to determine which end is 5'. I forget the details.

                            strandGroup = chunk.parent_node_of_class(chunk.assy.DnaStrand)
                            if strandGroup is None:
                                strand = chunk
                            else:
                                    #dna_updater case which uses DnaStrand object for 
                                    #internal DnaStrandChunks
                                strand = strandGroup                  

                            # the label is positioned at 5' end of the strand
                            # get the 5' strand atom
                            atom = strand.get_five_prime_end_base_atom()
                            if atom:

                                # and the next atom for extra positioning
                                next_atom = atom.strand_next_baseatom(1)

                                if atom.molecule is chunk and atom and next_atom: 
                                    # draw labels only for the first chunk

                                    # vector to move the label slightly away from the atom center
                                    halfbond = 0.5*(atom.posn()-next_atom.posn())

                                    # create the label font
                                    labelFont = QFont( QString("Helvetica"), font_scale)

                                    # define a color of the label
                                    if highlighted:
                                        glpane.qglColor(RGBf_to_QColor(hhColor))
                                    elif chunk.picked:
                                        glpane.qglColor(RGBf_to_QColor(selColor))
                                    else:
                                        if labels_color_mode == 0:
                                            glpane.qglColor(RGBf_to_QColor(chunk_color))
                                        else:
                                            glpane.qglColor(RGBf_to_QColor(labels_color))
    
                                        
                                    # get font metrics to calculate text extents
                                    fm = QFontMetrics(labelFont)
                                    label_text = QString(strand.name)+QString(" ")
                                    textsize = fm.width(label_text)

                                    # calculate the text position
                                    # move a bit into viewers direction
                                    if not highlighted:
                                        textpos = chunk.abs_to_base(atom.posn()) + halfbond + 5.0 * glpane.out 
                                    else:
                                        textpos = atom.posn() + halfbond + 5.0 * glpane.out 
                                        
                                    # calculate shift for right aligned text
                                    dx, dy = _realTextSize(label_text, fm)

                                    # check if the right alignment is necessary
                                    if dot(glpane.right,halfbond)<0.0:
                                        textpos -= dx

                                    # center the label vertically
                                    textpos -= 0.5 * dy

                                    # draw the label
                                    glDisable(GL_LIGHTING)
                                    glpane.renderText(textpos[0], textpos[1], textpos[2], 
                                                      label_text, labelFont)
                                    glEnable(GL_LIGHTING)


        if self.dnaExperimentalMode > 0:
            # Very exprimental, buggy and undocumented 2D DNA display mode.
            # The helices are flattened, so sequence ond overall topology
            # can be visualized in a convenient way. Particularly
            # useful for short structural motifs and origami structures.
            # As of 080415, this work is still considered very preliminary
            # and experimental.
            
            # As of 080520, this code is less buggy, but still quite slow.
            # REVIEW: Suggestions for speedup?
            # suggestion added by piotr 080910:
            # The 2D representation is drawn in intermediate mode. 
            # This code uses multiple individual OpenGL calls (glVertex, 
            # glColor). These calls should be replaced by single OpenGL
            # array call. 
            
            # The structure will follow a 2D projection of the central axis.
            # Note: this mode doesn't work well for PAM5 models.
            
            axis = chunk.ladder.axis_rail

            flipped = False
            
            mode = self.dnaExperimentalMode - 1

            no_axis = False
            
            if axis is None:
                axis = chunk.ladder.strand_rails[0]
                no_axis = True
                
            if axis:
                # Calculate the font scale.
                if mode == 0:
                    font_scale = int(500.0 / glpane.scale)
                else:
                    font_scale = int(300.0 / glpane.scale)
                    
                # Rescale font scale for OSX.
                if sys.platform == "darwin":
                    font_scale *= 2         
    
                # Limit the font scale.            
                if font_scale < 9:
                    font_scale = 9
                if font_scale > 100:
                    font_scale = 100
    
                # Number of bases
                n_bases = len(axis)
                
                
                # Disable lighting, we are drawing only text and lines.                
                glDisable(GL_LIGHTING)

                # Calculate the line width.
                if mode == 0 \
                   or mode == 2:
                    lw = 1.0 + 100.0 / glpane.scale

                if mode == 1:
                    lw = 2.0 + 200.0 / glpane.scale
 
                labelFont = QFont( QString("Lucida Grande"), font_scale)
                fm = QFontMetrics(labelFont)
                
                # Calculate the font extents
                dx, dy = _realTextSize("X", fm)

                # Get the axis atoms
                axis_atoms = axis.baseatoms 
                
                # Get the strand atoms
                strand_atoms = [None, None]
                for i in range(0, len(chunk.ladder.strand_rails)):
                    strand_atoms[i] = chunk.ladder.strand_rails[i].baseatoms
                            
                base_list = []
                    
                if mode == 0:
                    ssep = 7.0
                elif mode == 1 \
                     or mode == 2:
                    ssep = 7.0
                
                # Prepare a list of bases to render and their positions.
                for pos in range(0, n_bases):
                    atom0 = axis_atoms[pos]
                    if pos < n_bases - 1:
                        atom1 = axis_atoms[pos + 1]
                        dpos = atom1.posn() - atom0.posn()
                    else:
                        atom1 = axis_atoms[pos - 1]
                        atom2 = axis_atoms[pos]
                        dpos = atom2.posn() - atom1.posn()                           
                    last_dpos = dpos
                    # Project the axis atom position onto a current view plane
                    out = chunk.quat.unrot(glpane.out)
                    if flipped:
                        dvec = norm(cross(dpos, -out))
                    else:
                        dvec = norm(cross(dpos, out))                    
                    pos0 = atom0.posn()
                    if not highlighted:
                        pos0 = chunk.abs_to_base(pos0)
                    s_atom0 = s_atom1 = None
                    if strand_atoms[0]:
                        if pos < len(strand_atoms[0]):
                            s_atom0 = strand_atoms[0][pos]
                    if strand_atoms[1]:
                        if pos < len(strand_atoms[1]):
                            s_atom1 = strand_atoms[1][pos]
                    s_atom0_pos = pos0 + ssep * dvec
                    s_atom1_pos = pos0 - ssep * dvec
                    str_atoms = [s_atom0, s_atom1]
                    str_pos = [s_atom0_pos, s_atom1_pos]
                    base_list.append((atom0, pos0, 
                                      str_atoms, str_pos)) 
                
                if chunk.isStrandChunk():
                    
                    glLineWidth(lw)

                    for str in [0, 1]:
                        # Draw the strand
                        atom = None
                        for base in base_list:
                            ax_atom, ax_atom_pos, str_atoms, str_atoms_pos = base
                            if str_atoms[str] != None:
                                atom = str_atoms[str]
                                break
        
                        if atom and \
                           chunk == atom.molecule:
                            if atom.molecule.color:
                                strand_color = atom.molecule.color
                            else:
                                strand_color = lightgreen
                            
                            if chunk.picked:
                                strand_color = selColor
                             
                            if highlighted:
                                strand_color = hhColor
                                
                            glColor3fv(strand_color)
                            
                            glBegin(GL_LINES)
                            last_str_atom_pos = None
                            for base in base_list:
                                ax_atom, ax_atom_pos, str_atoms, str_atoms_pos = base
                                if str_atoms[str]:
                                    if last_str_atom_pos:
                                        glVertex3fv(last_str_atom_pos)
                                        glVertex3fv(str_atoms_pos[str])
                                    last_str_atom_pos = str_atoms_pos[str]
                                else:
                                    last_str_atom_pos = None
                            glEnd()
                            
                            if mode == 0 \
                               or mode == 2:
                                glLineWidth(lw)
                            elif mode == 1:
                                glLineWidth(0.5 * lw)
                            
                            glBegin(GL_LINES)
                            # Draw an arrow on 3' atom    
                            if not _draw_arrow(atom):
                                # Check out the other end
                                atom = None
                                for base in base_list:
                                    ax_atom, ax_atom_pos, str_atoms, str_atoms_pos = base
                                    if str_atoms[str] != None:
                                        atom = str_atoms[str]                        
                                # Draw an arrow on 3' atom    
                                _draw_arrow(atom)                                
                            glEnd()
                            
                            glLineWidth(lw)

                            glBegin(GL_LINES)
                            
                            # draw the external bonds
                            _draw_external_bonds()
                                # note: this calls a local function defined
                                # earlier (not a method, thus the lack of self),
                                # which has no direct relation to
                                # ChunkDrawer._draw_external_bonds.
            
                            # Line drawing done
                            glEnd()
                            
                            # Draw the base letters
                            if mode == 0:
                                for base in base_list:
                                    ax_atom, ax_atom_pos, str_atoms, str_atoms_pos = base
                                    if str_atoms[str]:
                                        textpos = ax_atom_pos + 0.5 * (str_atoms_pos[str] - ax_atom_pos)
                                        base_name = str_atoms[str].getDnaBaseName()
                                        label_text = QString(base_name)
                                        textpos -= 0.5 * (dx + dy)  
                                        color = black
                                        
                                        if chunk.picked:
                                            glColor3fv(selColor)                                        
                                            color = selColor
                                        else:
                                            base_color = self._get_base_color(base_name)
                                            glColor3fv(base_color)
                                            color = base_color
                                            
                                        if highlighted:
                                            glColor3fv(hhColor)
                                            color = hhColor
                                            
                                        ### drawtext(label_text, color, textpos, font_scale, glpane)
                                        glpane.renderText(textpos[0], 
                                                          textpos[1], 
                                                          textpos[2], 
                                                          label_text, labelFont)                    
                            elif mode == 1 \
                                 or mode == 2:
                                if no_axis == False:
                                    glLineWidth(lw)
                                    glBegin(GL_LINES)
                                    if chunk.picked:
                                        glColor3fv(selColor)
                                    elif highlighted:
                                        glColor3fv(hhColor)
                                    for base in base_list:
                                        ax_atom, ax_atom_pos, str_atoms, str_atoms_pos = base
                                        if str_atoms[str]:
                                            if not chunk.picked and \
                                               not highlighted:
                                                base_color = self._get_base_color(
                                                    str_atoms[str].getDnaBaseName())
                                                glColor3fv(base_color)   
                                            glVertex3fv(str_atoms_pos[str])
                                            glVertex3fv(ax_atom_pos)                                
                                    glEnd()
                                
                                if mode == 1:
                                    # draw circles interior
                                    for base in base_list:
                                        if chunk.picked:
                                            lcolor = _light_color(selColor)
                                        elif highlighted:
                                            lcolor = hhColor
                                        else:
                                            lcolor = _light_color(strand_color)
                                        ax_atom, ax_atom_pos, str_atoms, str_atoms_pos = base
                                        if str_atoms[str]:
                                            drawFilledCircle(
                                                lcolor, 
                                                str_atoms_pos[str] + 3.0 * chunk.quat.unrot(glpane.out), 
                                                1.5, chunk.quat.unrot(glpane.out))                                     
                                
                                    # draw circles border
                                    glLineWidth(3.0)
                                    #glColor3fv(strand_color)
                                    for base in base_list:
                                        ax_atom, ax_atom_pos, str_atoms, str_atoms_pos = base
                                        if str_atoms[str]:
                                            drawCircle(
                                                strand_color, 
                                                str_atoms_pos[str] + 3.1 * chunk.quat.unrot(glpane.out), 
                                                1.5, chunk.quat.unrot(glpane.out)) 
                                    glLineWidth(1.0)
                    
                if chunk.isAxisChunk():
                    if mode == 0:
                        # Draw filled circles in the center of the axis rail.
                        if chunk.picked:
                            color = selColor
                        elif highlighted:
                            color = hhColor
                        else:
                            color = black
                        for base in base_list:
                            ax_atom, ax_atom_pos, str_atoms, str_atoms_pos = base
                            if str_atoms[0] and str_atoms[1]:
                                drawFilledCircle(color, ax_atom_pos, 0.5, chunk.quat.unrot(glpane.out))
                            else:
                                drawFilledCircle(color, ax_atom_pos, 0.15, chunk.quat.unrot(glpane.out))

                glEnable(GL_LIGHTING)              
                
                glLineWidth(1.0)             
                # line width should be restored to initial value
                # but I think 1.0 is maintained within the program

    def writepov(self, chunk, memo, file):
        """
        Renders the chunk to a POV-Ray file.

        This is an experimental feature as of 080319.

        
        @param chunk: chunk to be rendered
        @type chunk: Chunk
        
        @param memo: a tuple describing the reduced representation
        @type memo: a tuple
        """

        from graphics.rendering.povray.povheader import povpoint
        from geometry.VQT import vlen

        def writetube(points, colors, radii, rainbow, smooth):
            """ 
            Writes a smooth tube in a POV-Ray format.
            
            @param points: list of tube points
            
            @param colors: list of tube colors
            
            @param radii: list of tube radii
            
            @param rainbow: use rainbow gradient to color the tube
            
            @param smooth: if True, use smooth tube, otherwise draw it
            as connected cylinders
            @type smooth: boolean
            """
            file.write("sphere_sweep {\n")
            if smooth == True:
                file.write("  linear_spline\n")
            else:
                # REVIEW: What should the non-smooth version be?
                # The non-smooth version should just draw straight
                # cylinders with spherical joints. This is not implemented.
                file.write("  linear_spline\n")
            file.write("  %d,\n" % (len(points)))
            n = len(points)
            for i in range(0,n):
                file.write("  " + povpoint(chunk.base_to_abs(points[i])) +", %g\n" % radii[i]);
            
            file.write("  pigment {\n")
            vec = points[n-1]-points[0]
            nvec = radii[0] * norm(vec)
            vec += 2.0 * nvec
            file.write("    gradient <%g,%g,%g> scale %g translate " % 
                       (vec[0], vec[1], vec[2], vlen(vec)))
            file.write(povpoint(chunk.base_to_abs(points[0] - nvec)) + "\n")
            file.write("    color_map { RainbowMap }\n")
            file.write("  }\n")
            
            file.write("}\n")

        def writecylinder(start, end, rad, color):
            """
            Write a POV-Ray cylinder starting at start and ending at end,
            of radius rad, using given color.
            """
            file.write("cylinder {\n")
            file.write("  " + povpoint(chunk.base_to_abs(start)) + ", " + povpoint(chunk.base_to_abs(end)))
            file.write(", %g\n" % (rad))
            file.write("  pigment {color <%g %g %g>}\n" % (color[0], color[1], color[2]))
            file.write("}\n")

        def writesphere(color, pos, rad):
            """
            Write a POV-Ray sphere at position pos of radius rad using given color.
            """
            file.write("sphere {\n")
            file.write("  " + povpoint(chunk.base_to_abs(pos)))
            file.write(", %g\n" % rad)
            file.write("  pigment {color <%g %g %g>}\n" % (color[0], color[1], color[2]))
            file.write("}\n")
        
        def writecone(color, pos1, pos2, rad1, rad2):            
            """
            Write a POV-Ray cone starting at pos1 ending at pos2 using
            radii rad1 and rad2 in given color.
            """
            file.write("cone {\n")
            file.write("  " + povpoint(chunk.base_to_abs(pos1)))
            file.write(", %g\n" % rad1)
            file.write("  " + povpoint(chunk.base_to_abs(pos2)))
            file.write(", %g\n" % rad2)
            file.write("  pigment {color <%g %g %g>}\n" % (color[0], color[1], color[2]))
            file.write("}\n")

        # Write a POV-Ray file.
        
        # Make sure memo is precomputed.
        if memo is None:
            return
        
        positions, colors, radii, \
        arrows, struts_cylinders, base_cartoons = memo

        if positions is None:
            return
        
        # Render the axis cylinder        
        n_points = len(positions)            
        if self.dnaStyleAxisShape > 0:
            # spherical ends    
            if self.dnaStyleAxisEndingStyle == 4:
                writesphere(colors[1], 
                            positions[1], 
                            radii[1])                
                writesphere(colors[n_points - 2], 
                            positions[n_points - 2], 
                            radii[n_points - 2])                    
            
            # draw the polycone
            writetube(positions, colors, radii, False, True)

        elif chunk.isStrandChunk(): # strands, struts and bases 

            writetube(positions, colors, radii, False, True)

            # draw the arrows
            for color, pos, rad in arrows:
                writecone(color, pos[1], pos[2], rad[1], rad[2])

            # render struts
            for color, pos1, pos2, rad in struts_cylinders:
                writecylinder(pos1, pos2, rad, color)
                
            # render nucleotides            
            if self.dnaStyleBasesShape > 0:
                for color, a1pos, a2pos, a3pos, normal, bname in base_cartoons:
                    if a1pos:
                        if self.dnaStyleBasesShape == 1: # sugar spheres
                            writesphere(color, a1pos, self.dnaStyleBasesScale)
                        elif self.dnaStyleBasesShape == 2: 
                            if a2pos:
                                # draw a schematic 'cartoon' shape
                                aposn = a1pos + 0.50 * (a2pos - a1pos)
                                bposn = a1pos + 0.66 * (a2pos - a1pos)
                                cposn = a1pos + 0.75 * (a2pos - a1pos)
                                writecylinder( 
                                    a1pos, 
                                    bposn, 
                                    0.20 * self.dnaStyleBasesScale, color) 
                                if bname == 'G' \
                                   or bname == 'A': # draw two purine rings                                
                                    writecylinder(
                                        aposn - 0.25 * self.dnaStyleBasesScale * normal,
                                        aposn + 0.25 * self.dnaStyleBasesScale * normal,
                                        0.7 * self.dnaStyleBasesScale, color)                            
                                    writecylinder( 
                                        cposn - 0.25 * self.dnaStyleBasesScale * normal,
                                        cposn + 0.25 * self.dnaStyleBasesScale * normal,
                                        0.9 * self.dnaStyleBasesScale, color)
                                else:
                                    writecylinder( 
                                        bposn - 0.25 * self.dnaStyleBasesScale * normal,
                                        bposn + 0.25 * self.dnaStyleBasesScale * normal,
                                        0.9 * self.dnaStyleBasesScale, color)                            
        
    def compute_memo(self, chunk):
        """
        If drawing chunks in this display mode can be optimized by precomputing
        some info from chunk's appearance, compute that info and return it.

        If this computation requires preference values, access them as 
        env.prefs[key], and that will cause the memo to be removed (invalidated)
        when that preference value is changed by the user.

        This computation is assumed to also depend on, and only on, chunk's
        appearance in ordinary display modes (i.e. it's invalidated whenever
        havelist is). There is not yet any way to change that, so bugs will 
        occur if any ordinarily invisible chunk info affects this rendering,
        and potential optimizations will not be done if any ordinarily visible
        info is not visible in this rendering. These can be fixed if necessary
        by having the real work done within class Chunk's _recompute_ rules,
        with this function or drawchunk just accessing the result of that
        (and sometimes causing its recomputation), and with whatever 
        invalidation is needed being added to appropriate setter methods of 
        class Chunk. If the real work can depend on more than chunk's ordinary
        appearance can, the access would need to be in drawchunk;
        otherwise it could be in drawchunk or in this method compute_memo().

        @param chunk: The chunk
        @type  chunk: Chunk
        """

        # REVIEW: Does this take into account curved strand shapes?
        #
        # for this example, we'll turn the chunk axes into a cylinder.
        # Since chunk.axis is not always one of the vectors chunk.evecs 
        # (actually chunk.poly_evals_evecs_axis[2]),
        # it's best to just use the axis and center, then recompute 
        # a bounding cylinder.

        # piotr 080910 comment: yes, it is not a straight cylinder representing
        # the chunk axis anymore. It is a glePolyCone object
        # drawn along a path of DNA axis chunk. Similarly, the strand chunks
        # are represented by curved polycone objects. 
        
        # import the style preferences from User Preferences
        self.dnaStyleStrandsShape = env.prefs[dnaStyleStrandsShape_prefs_key]
        self.dnaStyleStrandsColor = env.prefs[dnaStyleStrandsColor_prefs_key]
        self.dnaStyleStrandsScale = env.prefs[dnaStyleStrandsScale_prefs_key]
        self.dnaStyleStrandsArrows = env.prefs[dnaStyleStrandsArrows_prefs_key]        
        self.dnaStyleAxisShape = env.prefs[dnaStyleAxisShape_prefs_key]
        self.dnaStyleAxisColor = env.prefs[dnaStyleAxisColor_prefs_key]
        self.dnaStyleAxisScale = env.prefs[dnaStyleAxisScale_prefs_key]
        self.dnaStyleAxisEndingStyle = env.prefs[dnaStyleAxisEndingStyle_prefs_key]
        self.dnaStyleStrutsShape = env.prefs[dnaStyleStrutsShape_prefs_key]
        self.dnaStyleStrutsColor = env.prefs[dnaStyleStrutsColor_prefs_key]
        self.dnaStyleStrutsScale = env.prefs[dnaStyleStrutsScale_prefs_key]                
        self.dnaStyleBasesShape = env.prefs[dnaStyleBasesShape_prefs_key]
        self.dnaStyleBasesColor = env.prefs[dnaStyleBasesColor_prefs_key]
        self.dnaStyleBasesScale = env.prefs[dnaStyleBasesScale_prefs_key]
        self.dnaStyleBasesDisplayLetters = env.prefs[dnaStyleBasesDisplayLetters_prefs_key]        
        self.dnaExperimentalMode = env.prefs[dnaRendition_prefs_key]

        # Four components of the reduced DNA style can be created and
        # controlled independently: central axis, strands, structs, and bases.
        
        if not hasattr(chunk, 'ladder'):
            # DNA updater is off? Don't render 
            # (should display a warning message?)
            return None

        if not chunk.atoms or not chunk.ladder: 
            # nothing to display - return
            return None

        n_bases = chunk.ladder.baselength()    
        if n_bases < 1: 
            # no bases - return
            return None

        # make sure there is a chunk color
        if chunk.color:
            chunk_color = chunk.color
        else:
            # sometimes the chunk.color is not defined, use white color
            # in this case. this may happen when a strand or segment
            # are being interactively edited. 
            chunk_color = white

        # atom positions in strand and axis
        strand_positions = axis_positions = None

        # number of strand in the ladder
        num_strands = chunk.ladder.num_strands()

        # current strand 
        current_strand = 0

        # get both lists of strand atoms
        strand_atoms = [None] * num_strands
        for i in range(0, num_strands):
            strand_atoms[i] = chunk.ladder.strand_rails[i].baseatoms
            if chunk.ladder.strand_rails[i].baseatoms[0].molecule is chunk:
                current_strand = i

        # empty list for strand colors
        strand_colors = [None] * num_strands

        # list of axis atoms
        axis_atoms = None
        if chunk.ladder.axis_rail:
            axis_atoms = chunk.ladder.axis_rail.baseatoms

        # group color (white by default)
        group_color = white

        # 5' and 3' end atoms of current strand (used for drawing arrowheads)
        five_prime_atom = three_prime_atom = None
        
        # current strand chunk direction
        strand_direction = 0

        # start and end positions of the current strand chunk (or corresponding
        # axis segment chunk) relative to the length of the entire strand
        start_index = end_index = 0
        total_strand_length = 1

        # positions, colors and radii to be used for model drawing
        positions = None
        colors = None
        radii = None
        
        # pre-calculate polycylinder positions (main drawing primitive
        # for strands and/or central axis)

        arrows = []
        struts_cylinders = []
        base_cartoons = []
        
        if chunk.isAxisChunk() \
           and axis_atoms \
           and num_strands > 1: 

            if self.dnaStyleAxisColor == 4: 
                # color according to position along the longest strand.
                longest_rail = None
                longest_wholechain = None
                longest_length = 0
                
                # Find a longest rail and wholechain
                strand_rails = chunk.ladder.strand_rails      
                for rail in strand_rails:
                    length = len(rail.baseatoms[0].molecule.wholechain)
                    if length > longest_length:
                        longest_length = length
                        longest_rail = rail
                        longest_wholechain = rail.baseatoms[0].molecule.wholechain
                        
                wholechain = longest_wholechain
                
                # Get first and last positions of the wholechain
                pos0, pos1 = wholechain.wholechain_baseindex_range()
                
                # index of the first wholechain base in the longest rail
                idx = wholechain.wholechain_baseindex(longest_rail, 0)
                
                # The "group_color" is a uniform color used for entire chunk
                # Calculate the group color according to relative position
                # of the wholechain using the "nice rainbow" coloring scheme.
                # For circular structures, the exact starting position 
                # is unpredictable, but the whole color range is still
                # properly displayed.
                group_color = self._get_nice_rainbow_color_in_range(
                    idx - pos0, 
                    pos1 - pos0, 
                    0.75,
                    1.0) 
            
            # Make sure there are two strands present in the rail
            # piotr 080430 (fixed post-FNANO Top 20 bugs - exception in
            # DNA cylinder chunks)
            positions = self._get_axis_positions(
                chunk, 
                axis_atoms, 
                self.dnaStyleAxisColor)
            colors = self._get_axis_colors(
                axis_atoms, 
                strand_atoms, 
                self.dnaStyleAxisColor, 
                chunk_color, 
                group_color)
            radii = self._get_axis_radii(axis_atoms, 
                self.dnaStyleAxisColor, 
                self.dnaStyleAxisShape,
                self.dnaStyleAxisScale,
                self.dnaStyleAxisEndingStyle)
            
        elif chunk.isStrandChunk() and \
            (strand_atoms[0] or 
             strand(atoms[1])):

            n_atoms = len(strand_atoms[current_strand])
            
            strand_group = chunk.getDnaGroup()
            if strand_group:
                strands = strand_group.getStrands()
                # find out strand color
                group_color = self._get_rainbow_color_in_range(
                    strands.index(chunk.dad), len(strands), 0.75, 1.0)
                strand = chunk.parent_node_of_class(chunk.assy.DnaStrand)
                if strand:
                    # determine 5' and 3' end atoms of the strand the chunk 
                    # belongs to, and find out the strand direction
                    five_prime_atom = strand.get_five_prime_end_base_atom()
                    three_prime_atom = strand.get_three_prime_end_base_atom()
                    strand_direction = chunk.idealized_strand_direction()
                    
                    wholechain = chunk.wholechain
    
                    # determine strand and end atom indices
                    # within the entire strand.
    
                    all_atoms = strand.get_strand_atoms_in_bond_direction()
                        
                    start_atom = strand_atoms[current_strand][0]
                    end_atom = strand_atoms[current_strand][len(strand_atoms[0])-1]
                    
                    # find out first and last strand chunk atom positions relative
                    # to the length of the entire strand
                    if  start_atom in all_atoms and \
                        end_atom in all_atoms:
                        start_index = all_atoms.index(start_atom) - 1
                        end_index = all_atoms.index(end_atom) - 1
                        total_strand_length = len(all_atoms) - 2           
                    
            if self.dnaStyleStrandsShape > 0: 
            
                # Get positions, colors and radii for current strand chunk
                positions = self._get_strand_positions(
                    chunk, strand_atoms[current_strand])
                
                colors = self._get_strand_colors(
                    strand_atoms[current_strand], 
                    self.dnaStyleStrandsColor,
                    start_index, end_index, total_strand_length,
                    chunk_color, group_color)
                
                radii = self._get_strand_radii(
                    strand_atoms[current_strand], 
                    self.dnaStyleStrandsScale)
    
                if self.dnaStyleStrandsShape == 2:
                    # strand shape is a tube
                    positions, \
                    colors, \
                    radii = self._make_curved_strand( 
                        positions, 
                        colors, 
                        radii )
    
                # Create a list of external bonds.
                # Moved drawing to draw_realtime, otherwise the struts are not
                # updated. piotr 080411
                
                # These bonds need to be drawn in draw_realtime
                # to reflect position changes of the individual chunks.                
    
                chunk._dnaStyleExternalBonds = []
                
                for bond in chunk.externs:
                    if bond.atom1.molecule.dad == bond.atom2.molecule.dad: # same group
                        if bond.atom1.molecule != bond.atom2.molecule: # but different chunks
                            if bond.atom1.molecule is chunk:
                                idx = strand_atoms[current_strand].index(bond.atom1)
                                if self.dnaStyleStrandsColor == 0:
                                    color = chunk.color
                                elif self.dnaStyleStrandsColor == 1:
                                    color = self._get_atom_rainbow_color(
                                        idx, n_atoms, start_index, end_index, 
                                        total_strand_length)
                                else:
                                    color = group_color                                
                                chunk._dnaStyleExternalBonds.append(
                                    (bond.atom1, bond.atom2, color))         
                    
                # Make the strand arrows.
                # Possibly, this code is too complicated... make sure that 
                # the conditions below are not redundant.
                
                arrlen = 5.0                
                n = len(positions)
                
                if strand_direction == 1:                
                    draw_5p = (strand_atoms[current_strand][0] == five_prime_atom)
                    draw_3p = (strand_atoms[current_strand][n_atoms - 1] == three_prime_atom)
                    if draw_5p and (self.dnaStyleStrandsArrows == 1 or 
                                    self.dnaStyleStrandsArrows == 3):
                        arrvec = arrlen * norm(positions[2] - positions[1])                    
                        arrows.append((colors[1],
                                      [positions[1] + arrvec,
                                       positions[1] + arrvec,
                                       positions[1] - arrvec,
                                       positions[1] - arrvec],
                                       [0.0, 0.0, 
                                        radii[1]*2.0, 
                                        radii[1]*2.0]))
                        
                    if draw_3p and (self.dnaStyleStrandsArrows == 2 or 
                                    self.dnaStyleStrandsArrows == 3):
                        arrvec = arrlen * norm(positions[n-3] - positions[n-2])
                        arrows.append((colors[n-2],
                                            [positions[n-2],
                                             positions[n-2],
                                             positions[n-2] - arrvec,
                                             positions[n-2] - arrvec],
                                             [radii[n-2]*2.0, 
                                              radii[n-2]*2.0, 
                                              0.0, 0.0]))
                else:
                    draw_5p = (strand_atoms[current_strand][n_atoms - 1] == five_prime_atom)
                    draw_3p = (strand_atoms[current_strand][0] == three_prime_atom)
                    if draw_3p and (self.dnaStyleStrandsArrows == 2 or 
                                    self.dnaStyleStrandsArrows == 3):
                        arrvec = arrlen * norm(positions[2] - positions[1])
                        arrows.append((colors[1],
                                            [positions[1],
                                             positions[1],
                                             positions[1] - arrvec,
                                             positions[1] - arrvec],
                                             [radii[1]*2.0, 
                                              radii[1]*2.0, 
                                              0.0, 0.0]))
                        
                    if draw_5p and (self.dnaStyleStrandsArrows == 1 or 
                                    self.dnaStyleStrandsArrows == 3):
                        arrvec = arrlen * norm(positions[n-3] - positions[n-2])
                        arrows.append((colors[n-2],
                                            [positions[n-2] + arrvec,
                                             positions[n-2] + arrvec,
                                             positions[n-2] - arrvec,
                                             positions[n-2] - arrvec],
                                             [0.0, 0.0, 
                                              radii[n-2]*2.0, 
                                              radii[n-2]*2.0]))
   
                        
            # Make struts.            
            if self.dnaStyleStrutsShape > 0:
                if num_strands > 1:
                    for pos in range(0, n_atoms):
                        atom1 = strand_atoms[current_strand][pos]
                        atom3 = strand_atoms[1 - current_strand][pos]
                        if self.dnaStyleStrutsShape == 1: 
                            # strand-axis-strand type
                            atom2_pos = chunk.abs_to_base(axis_atoms[pos].posn())   
                        elif self.dnaStyleStrutsShape == 2: 
                            # strand-strand type
                            atom2_pos = chunk.abs_to_base(0.5 * (atom1.posn() + atom3.posn()))
                        if self.dnaStyleStrutsColor == 0:
                            # color by chunk color
                            color = chunk_color
                        elif self.dnaStyleStrutsColor == 1:
                            # color by base order
                            color = self._get_rainbow_color_in_range(
                                pos, n_atoms, 0.75, 1.0)                        
                        else:
                            # color by base type
                            color = self._get_base_color(atom1.getDnaBaseName())

                        struts_cylinders.append(
                            (color,
                             chunk.abs_to_base(atom1.posn()),
                             atom2_pos,
                             0.5 * self.dnaStyleStrutsScale))

            # Make nucleotides.            
            if self.dnaStyleBasesShape > 0:
                atom1_pos = None
                atom2_pos = None
                atom3_pos = None
                normal = None
                for pos in range(0, n_atoms):
                    atom = strand_atoms[current_strand][pos]
                    bname = atom.getDnaBaseName()
                    if self.dnaStyleBasesColor == 0:
                        # color by chunk color
                        color = chunk_color
                    elif self.dnaStyleBasesColor == 1:
                        # color by base order
                        color = self._get_rainbow_color_in_range(pos, n_atoms, 0.75, 1.0)
                    elif self.dnaStyleBasesColor == 2:
                        # color by group color
                        color = group_color
                    else:
                        # color by base type
                        color = self._get_base_color(atom.getDnaBaseName())

                    if self.dnaStyleBasesShape == 1: 
                        # draw spheres
                        atom1_pos = chunk.abs_to_base(atom.posn())
                    elif self.dnaStyleBasesShape == 2 and \
                         num_strands > 1: 
                        # draw a schematic 'cartoon' shape
                        atom1_pos = chunk.abs_to_base(strand_atoms[current_strand][pos].posn())
                        atom3_pos = chunk.abs_to_base(strand_atoms[1 - current_strand][pos].posn())                        
                        atom2_pos = chunk.abs_to_base(axis_atoms[pos].posn())                        
                        # figure out a normal to the bases plane
                        v1 = atom1_pos - atom2_pos
                        v2 = atom1_pos - atom3_pos
                        normal = norm(cross(v1, v2))
                    
                    base_cartoons.append((
                        color, atom1_pos, atom2_pos, atom3_pos, normal, bname))
        
        # For current chunk, returns: list of positions, colors, radii, arrows, 
        # strut cylinders and base cartoons
        return (positions,
                colors,
                radii,
                arrows,
                struts_cylinders,
                base_cartoons)

    pass # end of class DnaCylinderChunks

ChunkDisplayMode.register_display_mode_class(DnaCylinderChunks)

# end