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
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
|
# Copyright (C) University of Tennessee Health Science Center, Memphis, TN.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# This program is available from Source Forge: at GeneNetwork Project
# (sourceforge.net/projects/genenetwork/).
#
# Contact Drs. Robert W. Williams and Xiaodong Zhou (2010)
# at rwilliams@uthsc.edu and xzhou15@uthsc.edu
#
#
#
# This module is used by GeneNetwork project (www.genenetwork.org)
#
# Created by GeneNetwork Core Team 2010/08/10
#
# Last updated by Zach 12/14/2010
import datetime
import string
from math import *
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from PIL import ImageColor
import os
import json
import htmlgen as HT
from gn2.base import webqtlConfig
from gn2.base.GeneralObject import GeneralObject
from gn2.utility import webqtlUtil
from gn2.utility import Plot
from gn2.utility.tools import get_setting
from gn2.wqflask.interval_analyst import GeneUtil
from gn2.base.webqtlConfig import GENERATED_IMAGE_DIR
from gn2.utility.pillow_utils import draw_rotated_text, draw_open_polygon
from gn2.wqflask.database import database_connection
try: # Only import this for Python3
from functools import reduce
except:
pass
RED = ImageColor.getrgb("red")
BLUE = ImageColor.getrgb("blue")
GRAY = ImageColor.getrgb("gray")
GOLD = ImageColor.getrgb("gold")
BLACK = ImageColor.getrgb("black")
GREEN = ImageColor.getrgb("green")
PURPLE = ImageColor.getrgb("purple")
ORANGE = ImageColor.getrgb("orange")
YELLOW = ImageColor.getrgb("yellow")
DARKRED = ImageColor.getrgb("darkred")
DARKBLUE = ImageColor.getrgb("darkblue")
DARKGRAY = ImageColor.getrgb("darkgray")
DEEPPINK = ImageColor.getrgb("deeppink")
DARKGREEN = ImageColor.getrgb("darkgreen")
GAINSBORO = ImageColor.getrgb("gainsboro")
LIGHTBLUE = ImageColor.getrgb("lightblue")
DARKORANGE = ImageColor.getrgb("darkorange")
DARKVIOLET = ImageColor.getrgb("darkviolet")
MEDIUMPURPLE = ImageColor.getrgb("mediumpurple")
# ---- END: Define common colours ---- #
# ZS: List of distinct colors for manhattan plot if user selects "varied"
COLOR_CODES = ["#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#FF00FF", "#00FFFF",
"#000000", "#800000", "#008000", "#000080", "#808000", "#800080",
"#008080", "#808080", "#C00000", "#00C000", "#0000C0", "#C0C000",
"#C000C0", "#00C0C0", "#C0C0C0", "#400000", "#004000", "#000040"]
DISTINCT_COLOR_LIST = [ImageColor.getrgb(color) for color in COLOR_CODES]
# ---- FONT FILES ---- #
VERDANA_FILE = "./gn2/wqflask/static/fonts/verdana.ttf"
VERDANA_BOLD_FILE = "./gn2/wqflask/static/fonts/verdanab.ttf"
TREBUC_FILE = "./gn2/wqflask/static/fonts/trebucbd.ttf"
FNT_BS_FILE = "./gn2/wqflask/static/fonts/fnt_bs.ttf"
ARIAL_FILE = "./gn2/wqflask/static/fonts/arial.ttf"
assert(os.path.isfile(VERDANA_FILE))
class HtmlGenWrapper:
"""Wrapper Methods for HTML gen"""
@staticmethod
def create_image_tag(**kwargs):
image = HT.Image("", "")
for key, value in list(kwargs.items()):
image.set_attribute(key, value)
return image
@staticmethod
def create_form_tag(**kwargs):
form = HT.Form("POST", "") # Default method is POST
for key, value in list(kwargs.items()):
if key == "submit":
form.append(value)
continue
form.set_attribute(key.replace("cgi", "action"), str(value))
return form
@staticmethod
def create_p_tag(**kwargs):
paragraph = HT.Paragraph()
for key, value in list(kwargs.items()):
paragraph.set_attribute(key, value)
return paragraph
@staticmethod
def create_br_tag():
return HT.VoidElement("br")
@staticmethod
def create_input_tag(**kwargs):
input_ = HT.Input()
for key, value in list(kwargs.items()):
input_.set_attribute(key.lower().replace("_", ""), value)
return input_
@staticmethod
def create_area_tag(**kwargs):
area = HT.VoidElement("area")
for key, value in list(kwargs.items()):
area.set_attribute(key, value)
return area
@staticmethod
def create_link_tag(href, content, **kwargs):
link = HT.Link(href, content)
for key, value in list(kwargs.items()):
link.set_attribute(key, value)
return link
@staticmethod
def create_map_tag(**kwargs):
map_ = HT.Element("map")
for key, value in list(kwargs.items()):
map_.set_attribute(key, value)
return map_
class DisplayMappingResults:
"""Inteval Mapping Plot Page"""
cMGraphInterval = 5
GRAPH_MIN_WIDTH = 900
GRAPH_MAX_WIDTH = 10000 # Don't set this too high
GRAPH_DEFAULT_WIDTH = 1280
MULT_GRAPH_DEFAULT_WIDTH = 2000
MULT_GRAPH_MIN_WIDTH = 1400
MULT_GRAPH_DEFAULT_WIDTH = 1600
GRAPH_DEFAULT_HEIGHT = 600
# Display order:
# UCSC BAND =========
# ENSEMBL BAND -=-=-=
# ** GENES **********
BAND_SPACING = 4
BAND_HEIGHT = 10
BAND_HEIGHT = 10
BAND_HEIGHT = 10
NUM_GENE_ROWS = 10
EACH_GENE_HEIGHT = 6 # number of pixels tall, for each gene to display
EACH_GENE_ARROW_WIDTH = 5
EACH_GENE_ARROW_SPACING = 14
DRAW_DETAIL_MB = 4
DRAW_UTR_LABELS_MB = 4
qmarkImg = HtmlGenWrapper.create_image_tag(
src='/images/qmarkBoxBlue.gif',
width="10", height="13", border="0", alt='Glossary'
)
# Note that "qmark.gif" is a similar, smaller, rounded-edges
# question mark. It doesn't look like the ones on the image,
# though, which is why we don't use it here.
HELP_WINDOW_NAME = 'helpWind'
# BEGIN HaplotypeAnalyst
NR_INDIVIDUALS = 0
# END HaplotypeAnalyst
ALEX_DEBUG_BOOL_PRINT_GENE_LIST = 1
kONE_MILLION = 1000000
LODFACTOR = 4.61
SNP_COLOR = ORANGE # Color for the SNP "seismograph"
TRANSCRIPT_LOCATION_COLOR = MEDIUMPURPLE
BOOTSTRAP_BOX_COLOR = YELLOW
LRS_COLOR = ImageColor.getrgb("#0000FF")
SIGNIFICANT_COLOR = ImageColor.getrgb("#EBC7C7")
SUGGESTIVE_COLOR = GAINSBORO
SIGNIFICANT_WIDTH = 5
SUGGESTIVE_WIDTH = 5
ADDITIVE_COLOR_POSITIVE = GREEN
ADDITIVE_COLOR_NEGATIVE = ORANGE
DOMINANCE_COLOR_POSITIVE = DARKVIOLET
DOMINANCE_COLOR_NEGATIVE = RED
# BEGIN HaplotypeAnalyst
HAPLOTYPE_POSITIVE = GREEN
HAPLOTYPE_NEGATIVE = RED
HAPLOTYPE_HETEROZYGOUS = BLUE
HAPLOTYPE_RECOMBINATION = DARKGRAY
# END HaplotypeAnalyst
TOP_RIGHT_INFO_COLOR = BLACK
CLICKABLE_WEBQTL_REGION_COLOR = ImageColor.getrgb("#F5D3D3")
CLICKABLE_WEBQTL_REGION_OUTLINE_COLOR = ImageColor.getrgb("#FCE9E9")
CLICKABLE_WEBQTL_TEXT_COLOR = ImageColor.getrgb("#912828")
CLICKABLE_PHENOGEN_REGION_COLOR = ImageColor.getrgb("#A2FB94")
CLICKABLE_PHENOGEN_REGION_OUTLINE_COLOR = ImageColor.getrgb("#CEFEC7")
CLICKABLE_PHENOGEN_TEXT_COLOR = ImageColor.getrgb("#1FD504")
CLICKABLE_UCSC_REGION_COLOR = ImageColor.getrgb("#DDDDEE")
CLICKABLE_UCSC_REGION_OUTLINE_COLOR = ImageColor.getrgb("#EDEDFF")
CLICKABLE_UCSC_TEXT_COLOR = ImageColor.getrgb("#333366")
CLICKABLE_ENSEMBL_REGION_COLOR = ImageColor.getrgb("#EEEEDD")
CLICKABLE_ENSEMBL_REGION_OUTLINE_COLOR = ImageColor.getrgb("#FEFEEE")
CLICKABLE_ENSEMBL_TEXT_COLOR = ImageColor.getrgb("#555500")
GRAPH_BACK_LIGHT_COLOR = ImageColor.getrgb("#FBFBFF")
GRAPH_BACK_DARK_COLOR = ImageColor.getrgb("#F1F1F9")
HELP_PAGE_REF = '/glossary.html'
def __init__(self, start_vars):
self.temp_uuid = start_vars['temp_uuid']
self.hash_of_inputs = start_vars['hash_of_inputs']
self.dataid = start_vars['dataid']
self.dataset = start_vars['dataset']
self.this_trait = start_vars['this_trait']
self.n_samples = start_vars['n_samples']
self.species = start_vars['species']
self.genofile_string = ""
if 'genofile_string' in start_vars:
self.genofile_string = start_vars['genofile_string']
self.geno_db_exists = start_vars['geno_db_exists']
self.first_run = True
if 'first_run' in start_vars:
self.first_run = start_vars['first_run']
if 'temp_trait' in start_vars and start_vars['temp_trait'] != "False":
self.temp_trait = "True"
self.group = start_vars['group']
# Needing for form submission when doing single chr
# mapping or remapping after changing options
self.sample_vals = start_vars['sample_vals']
self.vals_hash= start_vars['vals_hash']
self.sample_vals_dict = json.loads(self.sample_vals)
self.transform = start_vars['transform']
self.mapping_method = start_vars['mapping_method']
self.mapping_results_path = start_vars['mapping_results_path']
if self.mapping_method == "rqtl_geno":
self.mapmethod_rqtl = start_vars['method']
self.mapmodel_rqtl = start_vars['model']
self.pair_scan = start_vars['pair_scan']
self.js_data = start_vars['js_data']
# Top markers to display in table
self.trimmed_markers = start_vars['trimmed_markers']
if self.dataset.group.species == "rat":
self._ucscDb = "rn6"
elif self.dataset.group.species == "mouse":
self._ucscDb = "mm10"
else:
self._ucscDb = ""
#####################################
# Options
#####################################
# Mapping options
if start_vars['mapping_scale'] != "":
self.plotScale = start_vars['mapping_scale']
else:
self.plotScale = "physic"
self.manhattan_plot = start_vars['manhattan_plot']
if self.manhattan_plot:
self.color_scheme = "alternating"
if 'color_scheme' in start_vars:
self.color_scheme = start_vars['color_scheme']
if self.color_scheme == "single":
self.manhattan_single_color = ImageColor.getrgb(
"#" + start_vars['manhattan_single_color'])
if 'permCheck' in list(start_vars.keys()):
self.permChecked = start_vars['permCheck']
else:
self.permChecked = False
if start_vars['num_perm'] > 0:
self.nperm = int(start_vars['num_perm'])
if self.permChecked:
self.perm_output = start_vars['perm_output']
self.suggestive = start_vars['suggestive']
self.significant = start_vars['significant']
else:
self.nperm = 0
if 'bootCheck' in list(start_vars.keys()):
self.bootChecked = start_vars['bootCheck']
else:
self.bootChecked = False
if 'num_bootstrap' in list(start_vars.keys()):
self.nboot = int(start_vars['num_bootstrap'])
else:
self.nboot = 0
if 'bootstrap_results' in list(start_vars.keys()):
self.bootResult = start_vars['bootstrap_results']
else:
self.bootResult = []
if 'do_control' in list(start_vars.keys()):
self.doControl = start_vars['do_control']
else:
self.doControl = "false"
if 'control_marker' in list(start_vars.keys()):
self.controlLocus = start_vars['control_marker']
else:
self.controlLocus = ""
if 'covariates' in list(start_vars.keys()):
self.covariates = start_vars['covariates']
if 'maf' in list(start_vars.keys()):
self.maf = start_vars['maf']
else:
self.maf = ""
if 'output_files' in list(start_vars.keys()):
self.output_files = start_vars['output_files']
if 'use_loco' in list(start_vars.keys()) and self.mapping_method == "gemma":
self.use_loco = start_vars['use_loco']
if self.mapping_method == "reaper":
if 'output_files' in start_vars:
self.output_files = ",".join(
[(the_file if the_file is not None else "") for the_file in start_vars['output_files']])
self.categorical_vars = ""
self.perm_strata = ""
if 'perm_strata' in list(start_vars.keys()) and 'categorical_vars' in list(start_vars.keys()):
self.categorical_vars = start_vars['categorical_vars']
self.perm_strata = start_vars['perm_strata']
self.selectedChr = int(start_vars['selected_chr'])
self.strainlist = start_vars['samples']
self.traitList = []
thisTrait = start_vars['this_trait']
self.traitList.append(thisTrait)
################################################################
# Calculations QTL goes here
################################################################
self.multipleInterval = len(self.traitList) > 1
self.qtlresults = start_vars['qtl_results']
if self.multipleInterval:
self.colorCollection = Plot.colorSpectrum(len(self.qtlresults))
else:
self.colorCollection = [self.LRS_COLOR]
self.dataset.group.genofile = self.genofile_string.split(":")[0]
if self.mapping_method == "reaper" and self.manhattan_plot != True:
self.genotype = self.dataset.group.read_genotype_file(
use_reaper=True)
else:
self.genotype = self.dataset.group.read_genotype_file()
# Darwing Options
try:
if self.selectedChr > -1:
self.graphWidth = min(self.GRAPH_MAX_WIDTH, max(
self.GRAPH_MIN_WIDTH, int(start_vars['graphWidth'])))
else:
self.graphWidth = min(self.GRAPH_MAX_WIDTH, max(
self.MULT_GRAPH_MIN_WIDTH, int(start_vars['graphWidth'])))
except:
if self.selectedChr > -1:
self.graphWidth = self.GRAPH_DEFAULT_WIDTH
else:
self.graphWidth = self.MULT_GRAPH_DEFAULT_WIDTH
# BEGIN HaplotypeAnalyst
if 'haplotypeAnalystCheck' in list(start_vars.keys()):
self.haplotypeAnalystChecked = start_vars['haplotypeAnalystCheck']
else:
self.haplotypeAnalystChecked = False
# END HaplotypeAnalyst
self.graphHeight = self.GRAPH_DEFAULT_HEIGHT
self.dominanceChecked = False
if 'LRSCheck' in list(start_vars.keys()):
self.LRS_LOD = start_vars['LRSCheck']
else:
self.LRS_LOD = start_vars['score_type']
self.intervalAnalystChecked = True
self.draw2X = False
if 'additiveCheck' in list(start_vars.keys()):
self.additiveChecked = start_vars['additiveCheck']
else:
self.additiveChecked = False
if 'viewLegend' in list(start_vars.keys()):
self.legendChecked = start_vars['viewLegend']
else:
self.legendChecked = False
if 'showSNP' in list(start_vars.keys()):
self.SNPChecked = start_vars['showSNP']
else:
self.SNPChecked = False
if 'showHomology' in list(start_vars.keys()):
self.homologyChecked = start_vars['showHomology']
else:
self.homologyChecked = "ON"
if 'showGenes' in list(start_vars.keys()):
self.geneChecked = start_vars['showGenes']
else:
self.geneChecked = False
try:
self.startMb = float(start_vars['startMb'])
except:
self.startMb = -1
try:
self.endMb = float(start_vars['endMb'])
except:
self.endMb = -1
try:
self.lrsMax = float(start_vars['lrsMax'])
except:
self.lrsMax = 0
# Trait Infos
self.identification = ""
################################################################
# Generate Chr list and Retrieve Length Information
################################################################
self.ChrList = [("All", -1)]
for i, indChr in enumerate(self.genotype):
if self.dataset.group.species == "mouse" and indChr.name == "20":
self.ChrList.append(("X", i))
elif self.dataset.group.species == "rat" and indChr.name == "21":
self.ChrList.append(("X", i))
self.ChrList.append((indChr.name, i))
with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor:
cursor.execute("SELECT Length FROM Chr_Length, InbredSet "
"WHERE Chr_Length.SpeciesId = InbredSet.SpeciesId "
"AND InbredSet.Name = %s AND Chr_Length.Name IN "
f"({', '.join(['%s' for x in self.ChrList[1:]])}) "
"ORDER BY Chr_Length.OrderId",
(self.dataset.group.name,
*[x[0] for x in self.ChrList[1:]],))
self.ChrLengthMbList = cursor.fetchall()
self.ChrLengthMbList = [x[0] / 1000000.0 for x in self.ChrLengthMbList]
self.ChrLengthMbSum = reduce(
lambda x, y: x + y, self.ChrLengthMbList, 0.0)
if self.ChrLengthMbList:
self.MbGraphInterval = self.ChrLengthMbSum / \
(len(self.ChrLengthMbList) * 12) # Empirical Mb interval
else:
self.MbGraphInterval = 1
self.ChrLengthCMList = []
for i, _chr in enumerate(self.genotype):
self.ChrLengthCMList.append(_chr[-1].cM - _chr[0].cM)
self.ChrLengthCMSum = reduce(
lambda x, y: x + y, self.ChrLengthCMList, 0.0)
if self.plotScale == 'physic':
self.GraphInterval = self.MbGraphInterval # Mb
else:
self.GraphInterval = self.cMGraphInterval # cM
#########################
# Get the sorting column
#########################
RISet = self.dataset.group.name
if RISet in ('AXB', 'BXA', 'AXBXA'):
self.diffCol = ['B6J', 'A/J']
elif RISet in ('BXD', 'BXD300', 'B6D2F2', 'BDF2-2005', 'BDF2-1999', 'BHHBF2', 'BXD-Harvested', 'BXD-Longevity', 'BXD-Micturition', 'BXD-AE', 'BXD-NIA-AD', 'B6D2RI', 'BXD-Bone', 'DOD-BXD-GWI', 'BXD-Heart-Metals', 'UTHSC-Cannabinoid'):
self.diffCol = ['B6J', 'D2J']
elif RISet in ('CXB'):
self.diffCol = ['CBY', 'B6J']
elif RISet in ('BXH', 'BHF2'):
self.diffCol = ['B6J', 'C3H']
elif RISet in ('B6BTBRF2'):
self.diffCol = ['B6J', 'BTB']
elif RISet in ('LXS'):
self.diffCol = ['ILS', 'ISS']
else:
self.diffCol = []
for i, strain in enumerate(self.diffCol):
with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor:
cursor.execute("SELECT Id FROM Strain WHERE Symbol = %s",
(strain,))
if result := cursor.fetchone():
self.diffCol[i] = result[0]
################################################################
# GeneCollection goes here
################################################################
if self.plotScale == 'physic' and self.selectedChr != -1:
#StartMb or EndMb
if self.startMb < 0 or self.endMb < 0:
self.startMb = 0
self.endMb = self.ChrLengthMbList[self.selectedChr - 1]
geneTable = ""
self.geneCol = None
self.homology = None
if self.plotScale == 'physic' and self.selectedChr > -1 and (self.intervalAnalystChecked or self.geneChecked or self.homologyChecked):
# Draw the genes for this chromosome / region of this chromosome
webqtldatabase = self.dataset.name
if self.dataset.group.species == "mouse":
if self.selectedChr == 20:
chrName = "X"
else:
chrName = self.selectedChr
self.geneCol = GeneUtil.loadGenes(
str(chrName), self.diffCol, self.startMb, self.endMb, "mouse")
self.homology = GeneUtil.load_homology(str(chrName), self.startMb, self.endMb, "mouse_to_human.csv")
elif self.dataset.group.species == "rat":
if self.selectedChr == 21:
chrName = "X"
else:
chrName = self.selectedChr
self.geneCol = GeneUtil.loadGenes(
str(chrName), self.diffCol, self.startMb, self.endMb, "rat")
if self.geneCol and self.intervalAnalystChecked:
#######################################################################
#Nick use GENEID as RefGene to get Literature Correlation Informations#
#For Interval Mapping, Literature Correlation isn't useful, so skip it#
#through set GENEID is None #
#######################################################################
GENEID = None
self.geneTable(self.geneCol, GENEID)
# BEGIN HaplotypeAnalyst
# count the amount of individuals to be plotted, and increase self.graphHeight
if self.haplotypeAnalystChecked and self.selectedChr > -1:
thisTrait = self.this_trait
smd = []
for sample in self.sample_vals_dict.keys():
if self.sample_vals_dict[sample] != "x":
temp = GeneralObject(name=sample, value=float(
self.sample_vals_dict[sample]))
smd.append(temp)
else:
continue
samplelist = list(self.genotype.prgy)
for j, _geno in enumerate(self.genotype[0][1].genotype):
for item in smd:
if item.name == samplelist[j]:
self.NR_INDIVIDUALS = self.NR_INDIVIDUALS + 1
# default:
self.graphHeight = self.graphHeight + 2 * \
(self.NR_INDIVIDUALS + 10) * self.EACH_GENE_HEIGHT
# END HaplotypeAnalyst
if self.homologyChecked and self.homology and self.geneChecked and self.geneCol:
self.graphHeight = self.graphHeight + (self.NUM_GENE_ROWS) * self.EACH_GENE_HEIGHT
if self.geneChecked and self.geneCol:
self.graphHeight = self.graphHeight + (self.NUM_GENE_ROWS) * self.EACH_GENE_HEIGHT
################################################################
# Plots goes here
################################################################
showLocusForm = ""
intCanvas = Image.new("RGBA", size=(self.graphWidth, self.graphHeight))
gifmap = self.plotIntMapping(
intCanvas, startMb=self.startMb, endMb=self.endMb, showLocusForm=showLocusForm)
self.gifmap = gifmap.__str__()
self.filename = webqtlUtil.genRandStr("Itvl_")
intCanvas.save(
"{}.png".format(
os.path.join(webqtlConfig.GENERATED_IMAGE_DIR, self.filename)),
format='png')
intImg = HtmlGenWrapper.create_image_tag(
src="/image/{}.png".format(self.filename),
border="0", usemap='#WebQTLImageMap'
)
# Scales plot differently for high resolution
if self.draw2X:
intCanvasX2 = Image.new("RGBA", size=(
self.graphWidth * 2, self.graphHeight * 2))
gifmapX2 = self.plotIntMapping(
intCanvasX2, startMb=self.startMb, endMb=self.endMb, showLocusForm=showLocusForm, zoom=2)
intCanvasX2.save(
"{}.png".format(
os.path.join(webqtlConfig.GENERATED_IMAGE_DIR,
self.filename + "X2")),
format='png')
################################################################
# Outputs goes here
################################################################
# this form is used for opening Locus page or trait page, only available for genetic mapping
if showLocusForm:
showLocusForm = HtmlGenWrapper.create_form_tag(
cgi=os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE),
enctype='multipart/form-data',
name=showLocusForm,
submit=HtmlGenWrapper.create_input_tag(type_='hidden'))
hddn = {'FormID': 'showDatabase', 'ProbeSetID': '_', 'database': fd.RISet + \
"Geno", 'CellID': '_', 'RISet': fd.RISet, 'incparentsf1': 'ON'}
for key in hddn.keys():
showLocusForm.append(HtmlGenWrapper.create_input_tag(
name=key, value=hddn[key], type_='hidden'))
showLocusForm.append(intImg)
else:
showLocusForm = intImg
if (self.permChecked and self.nperm > 0) and not (self.multipleInterval and 0 < self.nperm):
self.perm_filename = self.drawPermutationHistogram()
################################################################
# footnote goes here
################################################################
# Small('More information about this graph is available here.')
btminfo = HtmlGenWrapper.create_p_tag(id="smallsize")
if self.traitList and self.traitList[0].dataset and self.traitList[0].dataset.type == 'Geno':
btminfo.append(HtmlGenWrapper.create_br_tag())
btminfo.append(
'Mapping using genotype data as a trait will result in infinity LRS at one locus. In order to display the result properly, all LRSs higher than 100 are capped at 100.')
def plotIntMapping(self, canvas, offset=(80, 120, 110, 100), zoom=1, startMb=None, endMb=None, showLocusForm=""):
im_drawer = ImageDraw.Draw(canvas)
# calculating margins
xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset
if self.multipleInterval:
yTopOffset = max(90, yTopOffset)
else:
if self.legendChecked:
yTopOffset += 10
if self.covariates != "" and self.controlLocus and self.doControl != "false":
yTopOffset += 25
if len(self.transform) > 0:
yTopOffset += 5
else:
pass
if self.plotScale != 'physic':
yBottomOffset = max(120, yBottomOffset)
fontZoom = zoom
if zoom == 2:
xLeftOffset += 20
fontZoom = 1.5
xLeftOffset = int(xLeftOffset * fontZoom)
xRightOffset = int(xRightOffset * fontZoom)
yBottomOffset = int(yBottomOffset * fontZoom)
cWidth = canvas.size[0]
cHeight = canvas.size[1]
plotWidth = cWidth - xLeftOffset - xRightOffset
plotHeight = cHeight - yTopOffset - yBottomOffset
# Drawing Area Height
drawAreaHeight = plotHeight
if self.plotScale == 'physic' and self.selectedChr > -1:
if self.dataset.group.species == "mouse" or self.dataset.group.species == "rat":
drawAreaHeight -= 4 * self.BAND_HEIGHT + 4 * self.BAND_SPACING + 10 * zoom
else:
drawAreaHeight -= 3 * self.BAND_HEIGHT + 3 * self.BAND_SPACING + 10 * zoom
if self.homologyChecked:
drawAreaHeight -= self.NUM_GENE_ROWS * \
self.EACH_GENE_HEIGHT + 3 * self.BAND_SPACING
if self.geneChecked:
drawAreaHeight -= self.NUM_GENE_ROWS * \
self.EACH_GENE_HEIGHT + 3 * self.BAND_SPACING
else:
if self.selectedChr > -1:
drawAreaHeight -= 20
else:
drawAreaHeight -= 30
# BEGIN HaplotypeAnalyst
if self.haplotypeAnalystChecked and self.selectedChr > -1:
drawAreaHeight -= self.EACH_GENE_HEIGHT * \
(self.NR_INDIVIDUALS + 10) * 2 * zoom
# END HaplotypeAnalyst
if zoom == 2:
drawAreaHeight -= 60
# Image map
gifmap = HtmlGenWrapper.create_map_tag(name="WebQTLImageMap")
newoffset = (xLeftOffset, xRightOffset, yTopOffset, yBottomOffset)
# Draw the alternating-color background first and get plotXScale
plotXScale = self.drawGraphBackground(
canvas, gifmap, offset=newoffset, zoom=zoom, startMb=startMb, endMb=endMb)
# draw bootstap
if self.bootChecked and not self.multipleInterval:
self.drawBootStrapResult(canvas, self.nboot, drawAreaHeight, plotXScale,
offset=newoffset, zoom=zoom, startMb=startMb, endMb=endMb)
# Draw clickable region and gene band if selected
if self.plotScale == 'physic' and self.selectedChr > -1:
self.drawClickBand(canvas, gifmap, plotXScale, offset=newoffset,
zoom=zoom, startMb=startMb, endMb=endMb)
if self.geneChecked and self.geneCol:
self.drawGeneBand(canvas, gifmap, plotXScale, offset=newoffset,
zoom=zoom, startMb=startMb, endMb=endMb)
if self.homologyChecked and self.homology:
if self.geneChecked and self.geneCol:
yTopOffset = newoffset[3] + self.NUM_GENE_ROWS * \
self.EACH_GENE_HEIGHT + 3 * self.BAND_SPACING + 10
self.drawHomologyBand(canvas, gifmap, plotXScale, offset=(xLeftOffset, xRightOffset, yTopOffset, yBottomOffset),
zoom=zoom, startMb=startMb, endMb=endMb)
if self.SNPChecked:
self.drawSNPTrackNew(
canvas, offset=newoffset, zoom=2 * zoom, startMb=startMb, endMb=endMb)
# BEGIN HaplotypeAnalyst
if self.haplotypeAnalystChecked:
self.drawHaplotypeBand(
canvas, gifmap, plotXScale, offset=newoffset, zoom=zoom, startMb=startMb, endMb=endMb)
# END HaplotypeAnalyst
# Draw X axis
self.drawXAxis(canvas, drawAreaHeight, gifmap, plotXScale, showLocusForm,
offset=newoffset, zoom=zoom, startMb=startMb, endMb=endMb)
# Draw QTL curve
self.drawQTL(canvas, drawAreaHeight, gifmap, plotXScale,
offset=newoffset, zoom=zoom, startMb=startMb, endMb=endMb)
# draw legend
if self.multipleInterval:
self.drawMultiTraitName(
fd, canvas, gifmap, showLocusForm, offset=newoffset)
elif self.legendChecked:
self.drawLegendPanel(canvas, offset=newoffset, zoom=zoom)
else:
pass
# draw position, no need to use a separate function
self.drawProbeSetPosition(
canvas, plotXScale, offset=newoffset, zoom=zoom)
return gifmap
def drawBootStrapResult(self, canvas, nboot, drawAreaHeight, plotXScale, offset=(40, 120, 80, 10), zoom=1, startMb=None, endMb=None):
im_drawer = ImageDraw.Draw(canvas)
xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset
plotWidth = canvas.size[0] - xLeftOffset - xRightOffset
plotHeight = canvas.size[1] - yTopOffset - yBottomOffset
yZero = canvas.size[1] - yBottomOffset
fontZoom = zoom
if zoom == 2:
fontZoom = 1.5
bootHeightThresh = drawAreaHeight * 3 / 4
# break bootstrap result into groups
BootCoord = []
i = 0
previous_chr = None
previous_chr_as_int = 0
startX = xLeftOffset
BootChrCoord = []
if self.selectedChr == -1: # ZS: If viewing full genome/all chromosomes
for i, result in enumerate(self.qtlresults):
if result['chr'] != previous_chr:
previous_chr = result['chr']
previous_chr_as_int += 1
if previous_chr_as_int != 1:
BootCoord.append(BootChrCoord)
BootChrCoord = []
startX += (
self.ChrLengthDistList[previous_chr_as_int - 2] + self.GraphInterval) * plotXScale
if self.plotScale == 'physic':
Xc = startX + (result['Mb'] - self.startMb) * plotXScale
else:
Xc = startX + \
(result['cM'] - self.qtlresults[0]['cM']) * plotXScale
BootChrCoord.append([Xc, self.bootResult[i]])
else:
for i, result in enumerate(self.qtlresults):
if str(result['chr']) == str(self.ChrList[self.selectedChr][0]):
if self.plotScale == 'physic':
Xc = startX + (result['Mb'] - \
self.startMb) * plotXScale
else:
Xc = startX + \
(result['cM'] - self.qtlresults[0]
['cM']) * plotXScale
BootChrCoord.append([Xc, self.bootResult[i]])
BootCoord = [BootChrCoord]
# reduce bootResult
if self.selectedChr > -1:
maxBootBar = 80.0
else:
maxBootBar = 200.0
stepBootStrap = plotWidth / maxBootBar
reducedBootCoord = []
maxBootCount = 0
for BootChrCoord in BootCoord:
nBoot = len(BootChrCoord)
bootStartPixX = BootChrCoord[0][0]
bootCount = BootChrCoord[0][1]
for i in range(1, nBoot):
if BootChrCoord[i][0] - bootStartPixX < stepBootStrap:
bootCount += BootChrCoord[i][1]
continue
else:
if maxBootCount < bootCount:
maxBootCount = bootCount
# end if
reducedBootCoord.append(
[bootStartPixX, BootChrCoord[i][0], bootCount])
bootStartPixX = BootChrCoord[i][0]
bootCount = BootChrCoord[i][1]
# end else
# end for
# add last piece
if BootChrCoord[-1][0] - bootStartPixX > stepBootStrap / 2.0:
reducedBootCoord.append(
[bootStartPixX, BootChrCoord[-1][0], bootCount])
else:
reducedBootCoord[-1][2] += bootCount
reducedBootCoord[-1][1] = BootChrCoord[-1][0]
# end else
if maxBootCount < reducedBootCoord[-1][2]:
maxBootCount = reducedBootCoord[-1][2]
# end if
for item in reducedBootCoord:
if item[2] > 0:
if item[0] < xLeftOffset:
item[0] = xLeftOffset
if item[0] > xLeftOffset + plotWidth:
item[0] = xLeftOffset + plotWidth
if item[1] < xLeftOffset:
item[1] = xLeftOffset
if item[1] > xLeftOffset + plotWidth:
item[1] = xLeftOffset + plotWidth
if item[0] != item[1]:
im_drawer.rectangle(
xy=((item[0], yZero),
(item[1], yZero - item[2] * bootHeightThresh / maxBootCount)),
fill=self.BOOTSTRAP_BOX_COLOR, outline=BLACK)
if maxBootCount == 0:
return
# draw boot scale
highestPercent = (maxBootCount * 100.0) / nboot
bootScale = Plot.detScale(0, highestPercent)
bootScale = Plot.frange(
bootScale[0], bootScale[1], bootScale[1] / bootScale[2])
bootScale = bootScale[:-1] + [highestPercent]
bootOffset = 50 * fontZoom
bootScaleFont = ImageFont.truetype(
font=VERDANA_FILE, size=13 * fontZoom)
im_drawer.rectangle(
xy=((canvas.size[0] - bootOffset, yZero - bootHeightThresh),
(canvas.size[0] - bootOffset - 15 * zoom, yZero)),
fill=YELLOW, outline=BLACK)
im_drawer.line(
xy=((canvas.size[0] - bootOffset + 4, yZero),
(canvas.size[0] - bootOffset, yZero)),
fill=BLACK)
TEXT_Y_DISPLACEMENT = -8
im_drawer.text(xy=(canvas.size[0] - bootOffset + 10, yZero + TEXT_Y_DISPLACEMENT), text='0%',
font=bootScaleFont, fill=BLACK)
for item in bootScale:
if item == 0:
continue
bootY = yZero - bootHeightThresh * item / highestPercent
im_drawer.line(
xy=((canvas.size[0] - bootOffset + 4, bootY),
(canvas.size[0] - bootOffset, bootY)),
fill=BLACK)
im_drawer.text(xy=(canvas.size[0] - bootOffset + 10, bootY + TEXT_Y_DISPLACEMENT),
text='%2.1f' % item, font=bootScaleFont, fill=BLACK)
if self.legendChecked:
if hasattr(self.traitList[0], 'chr') and hasattr(self.traitList[0], 'mb'):
startPosY = 30
else:
startPosY = 15
smallLabelFont = ImageFont.truetype(
font=TREBUC_FILE, size=12 * fontZoom)
leftOffset = canvas.size[0] - xRightOffset - 190
im_drawer.rectangle(
xy=((leftOffset, startPosY - 6),
(leftOffset + 12, startPosY + 6)),
fill=YELLOW, outline=BLACK)
im_drawer.text(xy=(canvas.size[0] - xRightOffset - 170, startPosY + TEXT_Y_DISPLACEMENT),
text='Frequency of the Peak LRS',
font=smallLabelFont, fill=BLACK)
def drawProbeSetPosition(self, canvas, plotXScale, offset=(40, 120, 80, 10), zoom=1, startMb=None, endMb=None):
im_drawer = ImageDraw.Draw(canvas)
if len(self.traitList) != 1:
return
xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset
plotWidth = canvas.size[0] - xLeftOffset - xRightOffset
plotHeight = canvas.size[1] - yTopOffset - yBottomOffset
yZero = canvas.size[1] - yBottomOffset
fontZoom = zoom
if zoom == 2:
fontZoom = 1.5
try:
Chr = self.traitList[0].chr
Mb = self.traitList[0].mb
except:
return
previous_chr = 1
previous_chr_as_int = 0
if self.plotScale == "physic":
this_chr = str(self.ChrList[self.selectedChr][0])
else:
this_chr = str(self.ChrList[self.selectedChr][1] + 1)
if self.plotScale == 'physic':
if self.selectedChr > -1:
if this_chr != Chr or Mb < self.startMb or Mb > self.endMb:
return
else:
locPixel = xLeftOffset + (Mb - self.startMb) * plotXScale
else:
locPixel = xLeftOffset
for i, _chr in enumerate(self.ChrList[1:]):
if _chr[0] != Chr:
locPixel += (self.ChrLengthDistList[i] + \
self.GraphInterval) * plotXScale
else:
locPixel += Mb * plotXScale
break
else:
if self.selectedChr > -1:
for i, qtlresult in enumerate(self.qtlresults):
if qtlresult['chr'] != self.selectedChr:
continue
if i == 0 and qtlresult['Mb'] >= Mb:
locPixel = -1
break
# the trait's position is between two traits
if i > 0 and self.qtlresults[i - 1]['Mb'] < Mb and qtlresult['Mb'] >= Mb:
locPixel = xLeftOffset + plotXScale * (self.qtlresults[i - 1]['Mb'] + (qtlresult['Mb'] - self.qtlresults[i - 1]['Mb']) * (
Mb - self.qtlresults[i - 1]['Mb']) / (qtlresult['Mb'] - self.qtlresults[i - 1]['Mb']))
break
# the trait's position is on the right of the last genotype
if i == len(self.qtlresults) and Mb >= qtlresult['Mb']:
locPixel = -1
else:
locPixel = xLeftOffset
for i, _chr in enumerate(self.ChrList):
if i < (len(self.ChrList) - 1):
if _chr != Chr:
locPixel += (self.ChrLengthDistList[i] + \
self.GraphInterval) * plotXScale
else:
locPixel += (Mb * (_chr[-1].cM - _chr[0].cM) / \
self.ChrLengthCMList[i]) * plotXScale
break
if locPixel >= 0 and self.plotScale == 'physic':
traitPixel = ((locPixel, yZero), (locPixel - 7,
yZero + 14), (locPixel + 7, yZero + 14))
draw_open_polygon(canvas, xy=traitPixel, outline=BLACK,
fill=self.TRANSCRIPT_LOCATION_COLOR)
def drawSNPTrackNew(self, canvas, offset=(40, 120, 80, 10), zoom=1, startMb=None, endMb=None):
im_drawer = ImageDraw.Draw(canvas)
if self.plotScale != 'physic' or self.selectedChr == -1 or not self.diffCol:
return
SNP_HEIGHT_MODIFIER = 18.0
xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset
plotWidth = canvas.size[0] - xLeftOffset - xRightOffset
plotHeight = canvas.size[1] - yTopOffset - yBottomOffset
yZero = canvas.size[1] - yBottomOffset
fontZoom = zoom
if zoom == 2:
fontZoom = 1.5
drawSNPLocationY = yTopOffset + plotHeight
#chrName = self.genotype[0].name
chrName = self.ChrList[self.selectedChr][0]
stepMb = (endMb - startMb) / plotWidth
strainId1, strainId2 = self.diffCol
SNPCounts = []
while startMb < endMb:
with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor:
# snp count
cursor.execute("SELECT COUNT(*) FROM BXDSnpPosition "
"WHERE Chr = %s AND Mb >= %s AND Mb < %s AND "
"StrainId1 = %s AND StrainId2 = %s",
(chrName, f"{startMb:2.6f}",
f"{startMb + stepMb:2.6f}", strainId1, strainId2,))
SNPCounts.append(cursor.fetchone()[0])
startMb += stepMb
if (len(SNPCounts) > 0):
maxCount = max(SNPCounts)
if maxCount > 0:
for i in range(xLeftOffset, xLeftOffset + plotWidth):
snpDensity = float(
SNPCounts[i - xLeftOffset] * SNP_HEIGHT_MODIFIER / maxCount)
im_drawer.line(
xy=((i, drawSNPLocationY + (snpDensity) * zoom),
(i, drawSNPLocationY - (snpDensity) * zoom)),
fill=self.SNP_COLOR, width=1)
def drawMultiTraitName(self, fd, canvas, gifmap, showLocusForm, offset=(40, 120, 80, 10), zoom=1):
nameWidths = []
yPaddingTop = 10
colorFont = ImageFont.truetype(font=TREBUC_FILE, size=12)
if len(self.qtlresults) > 20 and self.selectedChr > -1:
rightShift = 20
rightShiftStep = 60
rectWidth = 10
else:
rightShift = 40
rightShiftStep = 80
rectWidth = 15
for k, thisTrait in enumerate(self.traitList):
thisLRSColor = self.colorCollection[k]
kstep = k % 4
if k != 0 and kstep == 0:
if nameWidths:
rightShiftStep = max(nameWidths[-4:]) + rectWidth + 20
rightShift += rightShiftStep
name = thisTrait.displayName()
nameWidth, nameHeight = im_drawer.textsize(name, font=colorFont)
nameWidths.append(nameWidth)
im_drawer.rectangle(
xy=((rightShift, yPaddingTop + kstep * 15),
(rectWidth + rightShift, yPaddingTop + 10 + kstep * 15)),
fill=thisLRSColor, outline=BLACK)
im_drawer.text(
text=name, xy=(rectWidth + 2 + rightShift,
yPaddingTop + 10 + kstep * 15),
font=colorFont, fill=BLACK)
if thisTrait.db:
COORDS = "%d,%d,%d,%d" % (rectWidth + 2 + rightShift, yPaddingTop + kstep * \
15, rectWidth + 2 + rightShift + nameWidth, yPaddingTop + 10 + kstep * 15,)
HREF = "javascript:showDatabase3('%s','%s','%s','');" % (
showLocusForm, thisTrait.db.name, thisTrait.name)
Areas = HtmlGenWrapper.create_area_tag(
shape='rect', coords=COORDS, href=HREF)
gifmap.append(Areas) # TODO
def drawLegendPanel(self, canvas, offset=(40, 120, 80, 10), zoom=1):
im_drawer = ImageDraw.Draw(canvas)
xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset
plotWidth = canvas.size[0] - xLeftOffset - xRightOffset
plotHeight = canvas.size[1] - yTopOffset - yBottomOffset
yZero = canvas.size[1] - yBottomOffset
TEXT_Y_DISPLACEMENT = -8
fontZoom = zoom
if zoom == 2:
fontZoom = 1.5
labelFont = ImageFont.truetype(font=TREBUC_FILE, size=12 * fontZoom)
startPosY = 15
stepPosY = 12 * fontZoom
startPosX = canvas.size[0] - xRightOffset - 415
if hasattr(self.traitList[0], 'chr') and hasattr(self.traitList[0], 'mb'):
startPosY = 15
nCol = 2
smallLabelFont = ImageFont.truetype(
font=TREBUC_FILE, size=12 * fontZoom)
leftOffset = canvas.size[0] - xRightOffset - 190
draw_open_polygon(
canvas,
xy=(
(leftOffset + 6, startPosY - 7),
(leftOffset - 1, startPosY + 7),
(leftOffset + 13, startPosY + 7)),
outline=BLACK, fill=self.TRANSCRIPT_LOCATION_COLOR
)
TEXT_Y_DISPLACEMENT = -8
im_drawer.text(
text="Sequence Site",
xy=(leftOffset + 20, startPosY + TEXT_Y_DISPLACEMENT), font=smallLabelFont,
fill=self.TOP_RIGHT_INFO_COLOR)
if self.manhattan_plot != True:
im_drawer.line(
xy=((startPosX, startPosY), (startPosX + 32, startPosY)),
fill=self.LRS_COLOR, width=2)
im_drawer.text(
text=self.LRS_LOD, xy=(
startPosX + 40, startPosY + TEXT_Y_DISPLACEMENT),
font=labelFont, fill=BLACK)
startPosY += stepPosY
if self.additiveChecked:
im_drawer.line(
xy=((startPosX, startPosY), (startPosX + 17, startPosY)),
fill=self.ADDITIVE_COLOR_POSITIVE, width=2)
im_drawer.line(
xy=((startPosX + 18, startPosY), (startPosX + 32, startPosY)),
fill=self.ADDITIVE_COLOR_NEGATIVE, width=2)
im_drawer.text(
text='Additive Effect', xy=(startPosX + 40, startPosY + TEXT_Y_DISPLACEMENT),
font=labelFont, fill=BLACK)
startPosY += stepPosY
if self.genotype.type == 'intercross' and self.dominanceChecked:
im_drawer.line(
xy=((startPosX, startPosY), (startPosX + 17, startPosY)),
fill=self.DOMINANCE_COLOR_POSITIVE, width=4)
im_drawer.line(
xy=((startPosX + 18, startPosY), (startPosX + 35, startPosY)),
fill=self.DOMINANCE_COLOR_NEGATIVE, width=4)
im_drawer.text(
text='Dominance Effect', xy=(startPosX + 42, startPosY + 5),
font=labelFont, fill=BLACK)
startPosY += stepPosY
if self.haplotypeAnalystChecked:
im_drawer.line(
xy=((startPosX - 34, startPosY), (startPosX - 17, startPosY)),
fill=self.HAPLOTYPE_POSITIVE, width=4)
im_drawer.line(
xy=((startPosX - 17, startPosY), (startPosX, startPosY)),
fill=self.HAPLOTYPE_NEGATIVE, width=4)
im_drawer.line(
xy=((startPosX, startPosY), (startPosX + 17, startPosY)),
fill=self.HAPLOTYPE_HETEROZYGOUS, width=4)
im_drawer.line(
xy=((startPosX + 17, startPosY), (startPosX + 34, startPosY)),
fill=self.HAPLOTYPE_RECOMBINATION, width=4)
im_drawer.text(
text='Haplotypes (Pat, Mat, Het, Unk)',
xy=(startPosX + 41, startPosY + TEXT_Y_DISPLACEMENT), font=labelFont, fill=BLACK)
startPosY += stepPosY
if self.permChecked and self.nperm > 0:
thisStartX = startPosX
if self.multipleInterval and not self.bootChecked:
thisStartX = canvas.size[0] - xRightOffset - 205
im_drawer.line(
xy=((thisStartX, startPosY), (startPosX + 32, startPosY)),
fill=self.SIGNIFICANT_COLOR, width=self.SIGNIFICANT_WIDTH)
im_drawer.line(
xy=((thisStartX, startPosY + stepPosY),
(startPosX + 32, startPosY + stepPosY)),
fill=self.SUGGESTIVE_COLOR, width=self.SUGGESTIVE_WIDTH)
im_drawer.text(
text='Significant %s = %2.2f' % (
self.LRS_LOD, self.significant),
xy=(thisStartX + 40, startPosY + TEXT_Y_DISPLACEMENT), font=labelFont, fill=BLACK)
im_drawer.text(
text='Suggestive %s = %2.2f' % (self.LRS_LOD, self.suggestive),
xy=(thisStartX + 40, startPosY + TEXT_Y_DISPLACEMENT + stepPosY), font=labelFont,
fill=BLACK)
labelFont = ImageFont.truetype(font=VERDANA_FILE, size=12 * fontZoom)
labelColor = BLACK
if self.dataset.type == "Publish" or self.dataset.type == "Geno":
dataset_label = self.dataset.fullname
else:
dataset_label = "%s - %s" % (self.dataset.group.name,
self.dataset.fullname)
self.current_datetime = datetime.datetime.now().strftime("%b %d %Y %H:%M:%S")
string1 = 'UTC Timestamp: %s' % (self.current_datetime)
string2 = 'Dataset: %s' % (dataset_label)
string3 = 'Trait Hash: %s' % (self.vals_hash)
if self.genofile_string == "":
string4 = 'Genotype File: %s.geno' % self.dataset.group.name
else:
string4 = 'Genotype File: %s' % self.genofile_string.split(":")[1]
string6 = ''
if self.mapping_method == "gemma" or self.mapping_method == "gemma_bimbam":
if self.use_loco == "True":
string5 = 'Using GEMMA mapping method with LOCO and '
else:
string5 = 'Using GEMMA mapping method with '
if self.covariates != "":
string5 += 'the cofactors below:'
cofactor_names = ", ".join(
[covar.split(":")[0] for covar in self.covariates.split(",")])
string6 = cofactor_names
else:
string5 += 'no cofactors'
elif self.mapping_method == "rqtl_plink" or self.mapping_method == "rqtl_geno":
string5 = 'Using R/qtl mapping method with '
if self.covariates != "":
string5 += 'the cofactors below:'
cofactor_names = ", ".join(
[covar.split(":")[0] for covar in self.covariates.split(",")])
string6 = cofactor_names
elif self.controlLocus and self.doControl != "false":
string5 += '%s as control' % self.controlLocus
else:
string5 += 'no cofactors'
else:
string5 = 'Using Haldane mapping function with '
if self.controlLocus and self.doControl != "false":
string5 += '%s as control' % self.controlLocus
else:
string5 += 'no control for other QTLs'
y_constant = 10
if self.this_trait.name:
if self.selectedChr == -1:
identification = "Mapping on All Chromosomes for "
else:
identification = "Mapping on Chromosome %s for " % (
self.ChrList[self.selectedChr][0])
if self.this_trait.symbol:
identification += "Trait: %s - %s" % (
self.this_trait.display_name, self.this_trait.symbol)
elif self.dataset.type == "Publish":
if self.this_trait.post_publication_abbreviation:
identification += "Trait: %s - %s" % (
self.this_trait.display_name, self.this_trait.post_publication_abbreviation)
elif self.this_trait.pre_publication_abbreviation:
identification += "Trait: %s - %s" % (
self.this_trait.display_name, self.this_trait.pre_publication_abbreviation)
else:
identification += "Trait: %s" % (self.this_trait.display_name)
else:
identification += "Trait: %s" % (self.this_trait.display_name)
identification += " with %s samples" % (self.n_samples)
d = 4 + max(
im_drawer.textsize(identification, font=labelFont)[0],
im_drawer.textsize(string1, font=labelFont)[0],
im_drawer.textsize(string2, font=labelFont)[0],
im_drawer.textsize(string3, font=labelFont)[0],
im_drawer.textsize(string4, font=labelFont)[0])
im_drawer.text(
text=identification,
xy=(xLeftOffset, y_constant * fontZoom), font=labelFont,
fill=labelColor)
y_constant += 15
else:
d = 4 + max(
im_drawer.textsize(string1, font=labelFont)[0],
im_drawer.textsize(string2, font=labelFont)[0],
im_drawer.textsize(string3, font=labelFont)[0],
im_drawer.textsize(string4, font=labelFont)[0])
if len(self.transform) > 0:
transform_text = "Transform - "
if self.transform == "qnorm":
transform_text += "Quantile Normalized"
elif self.transform == "log2" or self.transform == "log10":
transform_text += self.transform.capitalize()
elif self.transform == "sqrt":
transform_text += "Square Root"
elif self.transform == "zscore":
transform_text += "Z-Score"
elif self.transform == "invert":
transform_text += "Invert +/-"
im_drawer.text(
text=transform_text, xy=(xLeftOffset, y_constant * fontZoom),
font=labelFont, fill=labelColor)
y_constant += 15
im_drawer.text(
text=string1, xy=(xLeftOffset, y_constant * fontZoom),
font=labelFont, fill=labelColor)
y_constant += 15
im_drawer.text(
text=string2, xy=(xLeftOffset, y_constant * fontZoom),
font=labelFont, fill=labelColor)
y_constant += 15
im_drawer.text(
text=string3, xy=(xLeftOffset, y_constant * fontZoom),
font=labelFont, fill=labelColor)
y_constant += 15
im_drawer.text(
text=string4, xy=(xLeftOffset, y_constant * fontZoom),
font=labelFont, fill=labelColor)
y_constant += 15
if string4 != '':
im_drawer.text(
text=string5, xy=(xLeftOffset, y_constant * fontZoom),
font=labelFont, fill=labelColor)
y_constant += 15
if string5 != '':
im_drawer.text(
text=string6, xy=(xLeftOffset, y_constant * fontZoom),
font=labelFont, fill=labelColor)
def drawHomologyBand(self, canvas, gifmap, plotXScale, offset=(40, 120, 80, 10), zoom=1, startMb=None, endMb=None):
im_drawer = ImageDraw.Draw(canvas)
if self.plotScale != 'physic' or self.selectedChr == -1 or not self.homology:
return
xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset
plotWidth = canvas.size[0] - xLeftOffset - xRightOffset
plotHeight = canvas.size[1] - yTopOffset - yBottomOffset
yZero = canvas.size[1] - yBottomOffset
fontZoom = zoom
if zoom == 2:
fontZoom = 1.5
yPaddingTop = yTopOffset
for index, homology_dict in enumerate(self.homology):
ref_strand = homology_dict["ref_strand"]
ref_start = homology_dict["ref_start"]
ref_end = homology_dict["ref_end"]
query_chr = homology_dict["query_chr"]
query_strand = homology_dict["query_strand"]
query_start = homology_dict["query_start"]
query_end = homology_dict["query_end"]
geneLength = (ref_end - ref_start) * 1000.0
tenPercentLength = geneLength * 0.0001
geneStartPix = xLeftOffset + \
plotXScale * (float(ref_start) - startMb)
geneEndPix = xLeftOffset + plotXScale * \
(float(ref_end) - startMb) # at least one pixel
if (geneEndPix < xLeftOffset):
return # this gene is not on the screen
if (geneEndPix > xLeftOffset + plotWidth):
geneEndPix = xLeftOffset + plotWidth # clip the last in-range gene
if (geneStartPix > xLeftOffset + plotWidth):
return # we are outside the valid on-screen range, so stop drawing genes
elif (geneStartPix < xLeftOffset):
geneStartPix = xLeftOffset # clip the first in-range gene
myColor = BLACK
outlineColor = myColor
fillColor = myColor
TITLE = f"hg38: Chr {query_chr} from {query_start:.3f} to {query_end:.3f} Mb"
HREF = f"http://genome.ucsc.edu/cgi-bin/hgTracks?db=hg38&position=chr{query_chr}:{int(query_start * 1000000)}-{int(query_end * 1000000)}"
# Draw Genes
geneYLocation = yPaddingTop + \
(index % self.NUM_GENE_ROWS) * self.EACH_GENE_HEIGHT * zoom
if self.dataset.group.species == "mouse" or self.dataset.group.species == "rat":
geneYLocation += 4 * self.BAND_HEIGHT + 4 * self.BAND_SPACING
else:
geneYLocation += 3 * self.BAND_HEIGHT + 3 * self.BAND_SPACING
# draw the detail view
utrColor = ImageColor.getrgb("rgb(66%, 66%, 66%)")
arrowColor = ImageColor.getrgb("rgb(70%, 70%, 70%)")
# draw the line that runs the entire length of the gene
im_drawer.line(
xy=(
(geneStartPix, geneYLocation + \
self.EACH_GENE_HEIGHT / 2 * zoom),
(geneEndPix, geneYLocation + self.EACH_GENE_HEIGHT / 2 * zoom)),
fill=outlineColor, width=1)
# draw the arrows
if geneEndPix - geneStartPix < 1:
genePixRange = 1
else:
genePixRange = int(geneEndPix - geneStartPix)
for xCoord in range(0, genePixRange):
if (xCoord % self.EACH_GENE_ARROW_SPACING == 0 and xCoord + self.EACH_GENE_ARROW_SPACING < geneEndPix - geneStartPix) or xCoord == 0:
if query_strand == "+":
im_drawer.line(
xy=((geneStartPix + xCoord, geneYLocation),
(geneStartPix + xCoord + self.EACH_GENE_ARROW_WIDTH,
geneYLocation + (self.EACH_GENE_HEIGHT / 2) * zoom)),
fill=arrowColor, width=1)
im_drawer.line(
xy=((geneStartPix + xCoord,
geneYLocation + self.EACH_GENE_HEIGHT * zoom),
(geneStartPix + xCoord + self.EACH_GENE_ARROW_WIDTH,
geneYLocation + (self.EACH_GENE_HEIGHT / 2) * zoom)),
fill=arrowColor, width=1)
else:
im_drawer.line(
xy=((geneStartPix + xCoord + self.EACH_GENE_ARROW_WIDTH,
geneYLocation),
(geneStartPix + xCoord,
geneYLocation + (self.EACH_GENE_HEIGHT / 2) * zoom)),
fill=arrowColor, width=1)
im_drawer.line(
xy=((geneStartPix + xCoord + self.EACH_GENE_ARROW_WIDTH,
geneYLocation + self.EACH_GENE_HEIGHT * zoom),
(geneStartPix + xCoord,
geneYLocation + (self.EACH_GENE_HEIGHT / 2) * zoom)),
fill=arrowColor, width=1)
COORDS = "%d, %d, %d, %d" % (
geneStartPix, geneYLocation, geneEndPix, (geneYLocation + self.EACH_GENE_HEIGHT))
gifmap.append(
HtmlGenWrapper.create_area_tag(
shape='rect',
coords=COORDS,
href=HREF,
title=TITLE,
target="_blank"))
def drawGeneBand(self, canvas, gifmap, plotXScale, offset=(40, 120, 80, 10), zoom=1, startMb=None, endMb=None):
im_drawer = ImageDraw.Draw(canvas)
if self.plotScale != 'physic' or self.selectedChr == -1 or not self.geneCol:
return
xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset
plotWidth = canvas.size[0] - xLeftOffset - xRightOffset
plotHeight = canvas.size[1] - yTopOffset - yBottomOffset
yZero = canvas.size[1] - yBottomOffset
fontZoom = zoom
if zoom == 2:
fontZoom = 1.5
yPaddingTop = yTopOffset
for gIndex, theGO in enumerate(self.geneCol):
geneNCBILink = 'http://www.ncbi.nlm.nih.gov/gene?term=%s'
if self.dataset.group.species == "mouse":
exonStarts = []
exonEnds = []
txStart = theGO["TxStart"]
txEnd = theGO["TxEnd"]
geneLength = (txEnd - txStart) * 1000.0
tenPercentLength = geneLength * 0.0001
SNPdensity = theGO["snpCount"] / geneLength
if theGO['exonStarts']:
exonStarts = list(
map(float, theGO['exonStarts'].split(",")[:-1]))
exonEnds = list(map(float, theGO['exonEnds'].split(",")[:-1]))
cdsStart = theGO['cdsStart']
cdsEnd = theGO['cdsEnd']
accession = theGO['NM_ID']
geneSymbol = theGO["GeneSymbol"]
strand = theGO["Strand"]
exonCount = theGO["exonCount"]
geneStartPix = xLeftOffset + \
plotXScale * (float(txStart) - startMb)
geneEndPix = xLeftOffset + plotXScale * \
(float(txEnd) - startMb) # at least one pixel
if (geneEndPix < xLeftOffset):
return # this gene is not on the screen
elif (geneEndPix > xLeftOffset + plotWidth):
geneEndPix = xLeftOffset + plotWidth # clip the last in-range gene
if (geneStartPix > xLeftOffset + plotWidth):
return # we are outside the valid on-screen range, so stop drawing genes
elif (geneStartPix < xLeftOffset):
geneStartPix = xLeftOffset # clip the first in-range gene
# color the gene based on SNP density
# found earlier, needs to be recomputed as snps are added
# always apply colors now, even if SNP Track not checked - Zach 11/24/2010
densities = [1.0000000000000001e-05, 0.094094033555233408,
0.3306166377816987, 0.88246026851027781, 2.6690084029581951, 4.1, 61.0]
if SNPdensity < densities[0]:
myColor = BLACK
elif SNPdensity < densities[1]:
myColor = PURPLE
elif SNPdensity < densities[2]:
myColor = DARKBLUE
elif SNPdensity < densities[3]:
myColor = DARKGREEN
elif SNPdensity < densities[4]:
myColor = GOLD
elif SNPdensity < densities[5]:
myColor = DARKORANGE
else:
myColor = DARKRED
outlineColor = myColor
fillColor = myColor
TITLE = "Gene: %s (%s)\nFrom %2.3f to %2.3f Mb (%s)\nNum. exons: %d." % (
geneSymbol, accession, float(txStart), float(txEnd), strand, exonCount)
# NL: 06-02-2011 Rob required to change this link for gene related
HREF = geneNCBILink % geneSymbol
elif self.dataset.group.species == "rat":
exonStarts = []
exonEnds = []
txStart = theGO["TxStart"]
txEnd = theGO["TxEnd"]
cdsStart = theGO["TxStart"]
cdsEnd = theGO["TxEnd"]
geneSymbol = theGO["GeneSymbol"]
strand = theGO["Strand"]
exonCount = 0
geneStartPix = xLeftOffset + \
plotXScale * (float(txStart) - startMb)
geneEndPix = xLeftOffset + plotXScale * \
(float(txEnd) - startMb) # at least one pixel
if (geneEndPix < xLeftOffset):
return # this gene is not on the screen
elif (geneEndPix > xLeftOffset + plotWidth):
geneEndPix = xLeftOffset + plotWidth # clip the last in-range gene
if (geneStartPix > xLeftOffset + plotWidth):
return # we are outside the valid on-screen range, so stop drawing genes
elif (geneStartPix < xLeftOffset):
geneStartPix = xLeftOffset # clip the first in-range gene
outlineColor = DARKBLUE
fillColor = DARKBLUE
TITLE = "Gene: %s\nFrom %2.3f to %2.3f Mb (%s)" % (
geneSymbol, float(txStart), float(txEnd), strand)
# NL: 06-02-2011 Rob required to change this link for gene related
HREF = geneNCBILink % geneSymbol
else:
outlineColor = ORANGE
fillColor = ORANGE
TITLE = "Gene: %s" % geneSymbol
# Draw Genes
geneYLocation = yPaddingTop + \
(gIndex % self.NUM_GENE_ROWS) * self.EACH_GENE_HEIGHT * zoom
if self.dataset.group.species == "mouse" or self.dataset.group.species == "rat":
geneYLocation += 4 * self.BAND_HEIGHT + 4 * self.BAND_SPACING
else:
geneYLocation += 3 * self.BAND_HEIGHT + 3 * self.BAND_SPACING
# draw the detail view
if self.endMb - self.startMb <= self.DRAW_DETAIL_MB and geneEndPix - geneStartPix > self.EACH_GENE_ARROW_SPACING * 3:
utrColor = ImageColor.getrgb("rgb(66%, 66%, 66%)")
arrowColor = ImageColor.getrgb("rgb(70%, 70%, 70%)")
# draw the line that runs the entire length of the gene
im_drawer.line(
xy=(
(geneStartPix, geneYLocation + \
self.EACH_GENE_HEIGHT / 2 * zoom),
(geneEndPix, geneYLocation + self.EACH_GENE_HEIGHT / 2 * zoom)),
fill=outlineColor, width=1)
# draw the arrows
if geneEndPix - geneStartPix < 1:
genePixRange = 1
else:
genePixRange = int(geneEndPix - geneStartPix)
for xCoord in range(0, genePixRange):
if (xCoord % self.EACH_GENE_ARROW_SPACING == 0 and xCoord + self.EACH_GENE_ARROW_SPACING < geneEndPix - geneStartPix) or xCoord == 0:
if strand == "+":
im_drawer.line(
xy=((geneStartPix + xCoord, geneYLocation),
(geneStartPix + xCoord + self.EACH_GENE_ARROW_WIDTH,
geneYLocation + (self.EACH_GENE_HEIGHT / 2) * zoom)),
fill=arrowColor, width=1)
im_drawer.line(
xy=((geneStartPix + xCoord,
geneYLocation + self.EACH_GENE_HEIGHT * zoom),
(geneStartPix + xCoord + self.EACH_GENE_ARROW_WIDTH,
geneYLocation + (self.EACH_GENE_HEIGHT / 2) * zoom)),
fill=arrowColor, width=1)
else:
im_drawer.line(
xy=((geneStartPix + xCoord + self.EACH_GENE_ARROW_WIDTH,
geneYLocation),
(geneStartPix + xCoord,
geneYLocation + (self.EACH_GENE_HEIGHT / 2) * zoom)),
fill=arrowColor, width=1)
im_drawer.line(
xy=((geneStartPix + xCoord + self.EACH_GENE_ARROW_WIDTH,
geneYLocation + self.EACH_GENE_HEIGHT * zoom),
(geneStartPix + xCoord,
geneYLocation + (self.EACH_GENE_HEIGHT / 2) * zoom)),
fill=arrowColor, width=1)
# draw the blocks for the exon regions
for i in range(0, len(exonStarts)):
exonStartPix = (
exonStarts[i] - startMb) * plotXScale + xLeftOffset
exonEndPix = (exonEnds[i] - startMb) * \
plotXScale + xLeftOffset
if (exonStartPix < xLeftOffset):
exonStartPix = xLeftOffset
if (exonEndPix < xLeftOffset):
exonEndPix = xLeftOffset
if (exonEndPix > xLeftOffset + plotWidth):
exonEndPix = xLeftOffset + plotWidth
if (exonStartPix > xLeftOffset + plotWidth):
exonStartPix = xLeftOffset + plotWidth
im_drawer.rectangle(
xy=((exonStartPix, geneYLocation),
(exonEndPix, (geneYLocation + self.EACH_GENE_HEIGHT * zoom))),
outline=outlineColor, fill=fillColor)
# draw gray blocks for 3' and 5' UTR blocks
if cdsStart and cdsEnd:
utrStartPix = (txStart - startMb) * \
plotXScale + xLeftOffset
utrEndPix = (cdsStart - startMb) * plotXScale + xLeftOffset
if (utrStartPix < xLeftOffset):
utrStartPix = xLeftOffset
if (utrEndPix < xLeftOffset):
utrEndPix = xLeftOffset
if (utrEndPix > xLeftOffset + plotWidth):
utrEndPix = xLeftOffset + plotWidth
if (utrStartPix > xLeftOffset + plotWidth):
utrStartPix = xLeftOffset + plotWidth
if self.endMb - self.startMb <= self.DRAW_UTR_LABELS_MB:
if strand == "-":
labelText = "3'"
else:
labelText = "5'"
im_drawer.text(
text=labelText,
xy=(utrStartPix - 9, geneYLocation + \
self.EACH_GENE_HEIGHT),
font=ImageFont.truetype(font=ARIAL_FILE, size=2))
# the second UTR region
utrStartPix = (cdsEnd - startMb) * plotXScale + xLeftOffset
utrEndPix = (txEnd - startMb) * plotXScale + xLeftOffset
if (utrStartPix < xLeftOffset):
utrStartPix = xLeftOffset
if (utrEndPix < xLeftOffset):
utrEndPix = xLeftOffset
if (utrEndPix > xLeftOffset + plotWidth):
utrEndPix = xLeftOffset + plotWidth
if (utrStartPix > xLeftOffset + plotWidth):
utrStartPix = xLeftOffset + plotWidth
if self.endMb - self.startMb <= self.DRAW_UTR_LABELS_MB:
if strand == "-":
labelText = "5'"
else:
labelText = "3'"
im_drawer.text(
text=labelText,
xy=(utrEndPix + 2, geneYLocation + \
self.EACH_GENE_HEIGHT),
font=ImageFont.truetype(font=ARIAL_FILE, size=2))
# draw the genes as rectangles
else:
im_drawer.rectangle(
xy=((geneStartPix, geneYLocation),
(geneEndPix, (geneYLocation + self.EACH_GENE_HEIGHT * zoom))),
outline=outlineColor, fill=fillColor)
COORDS = "%d, %d, %d, %d" % (
geneStartPix, geneYLocation, geneEndPix, (geneYLocation + self.EACH_GENE_HEIGHT))
# NL: 06-02-2011 Rob required to display NCBI info in a new window
gifmap.append(
HtmlGenWrapper.create_area_tag(
shape='rect',
coords=COORDS,
href=HREF,
title=TITLE,
target="_blank"))
# BEGIN HaplotypeAnalyst
def drawHaplotypeBand(self, canvas, gifmap, plotXScale, offset=(40, 120, 80, 10), zoom=1, startMb=None, endMb=None):
if self.plotScale != 'physic' or self.selectedChr == -1 or not self.geneCol:
return
xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset
plotWidth = canvas.size[0] - xLeftOffset - xRightOffset
yPaddingTop = yTopOffset
thisTrait = self.this_trait
samplelist = list(self.genotype.prgy)
smd = []
for sample in self.sample_vals_dict.keys():
if self.sample_vals_dict[sample] != "x" and sample in samplelist:
temp = GeneralObject(name=sample, value=float(
self.sample_vals_dict[sample]))
smd.append(temp)
else:
continue
smd.sort(key=lambda A: A.value)
smd.reverse()
oldgeneEndPix = -1
# Initializing plotRight, error before
plotRight = xRightOffset
im_drawer = ImageDraw.Draw(canvas)
# find out PlotRight
for _chr in self.genotype:
if _chr.name == self.ChrList[self.selectedChr][0]:
for i, _locus in enumerate(_chr):
txStart = _chr[i].Mb
txEnd = _chr[i].Mb
geneStartPix = xLeftOffset + plotXScale * \
(float(txStart) - startMb) - 0
geneEndPix = xLeftOffset + plotXScale * \
(float(txEnd) - startMb) - 0
drawit = 1
if (geneStartPix < xLeftOffset):
drawit = 0
if (geneStartPix > xLeftOffset + plotWidth):
drawit = 0
if drawit == 1:
if _chr[i].name != " - ":
plotRight = geneEndPix + 4
# end find out PlotRight
firstGene = 1
lastGene = 0
# Sets the length to the length of the strain list. Beforehand, "oldgeno = self.genotype[0][i].genotype"
# was the only place it was initialized, which worked as long as the very start (startMb = None/0) wasn't being mapped.
# Now there should always be some value set for "oldgeno" - Zach 12/14/2010
oldgeno = [None] * len(self.strainlist)
for i, _chr in enumerate(self.genotype):
if _chr.name == self.ChrList[self.selectedChr][0]:
for j, _locus in enumerate(_chr):
txStart = _chr[j].Mb
txEnd = _chr[j].Mb
geneStartPix = xLeftOffset + plotXScale * \
(float(txStart) - startMb) - 0
geneEndPix = xLeftOffset + plotXScale * \
(float(txEnd) - startMb) + 0
if oldgeneEndPix >= xLeftOffset:
drawStart = oldgeneEndPix + 4
else:
drawStart = xLeftOffset + 3
drawEnd = plotRight - 9
drawit = 1
if (geneStartPix < xLeftOffset):
if firstGene == 1:
drawit = 1
else:
drawit = 0
elif (geneStartPix > (xLeftOffset + plotWidth - 3)):
if lastGene == 0:
drawit = 1
drawEnd = xLeftOffset + plotWidth - 6
lastGene = 1
else:
break
else:
firstGene = 0
drawit = 1
if drawit == 1:
myColor = DARKBLUE
outlineColor = myColor
fillColor = myColor
maxind = 0
# Draw Genes
geneYLocation = yPaddingTop + self.NUM_GENE_ROWS * \
(self.EACH_GENE_HEIGHT) * zoom
if self.dataset.group.species == "mouse" or self.dataset.group.species == "rat":
geneYLocation += 4 * self.BAND_HEIGHT + 4 * self.BAND_SPACING
else:
geneYLocation += 3 * self.BAND_HEIGHT + 3 * self.BAND_SPACING
if _chr[j].name != " - ":
if (firstGene == 1) and (lastGene != 1):
oldgeneEndPix = drawStart = xLeftOffset
oldgeno = _chr[j].genotype
continue
for k, _geno in enumerate(_chr[j].genotype):
plotbxd = 0
if samplelist[k] in [item.name for item in smd]:
plotbxd = 1
if (plotbxd == 1):
ind = 0
if samplelist[k] in [item.name for item in smd]:
ind = [item.name for item in smd].index(
samplelist[k])
maxind = max(ind, maxind)
# lines
if (oldgeno[k] == -1 and _geno == -1):
mylineColor = self.HAPLOTYPE_NEGATIVE
elif (oldgeno[k] == 1 and _geno == 1):
mylineColor = self.HAPLOTYPE_POSITIVE
elif (oldgeno[k] == 0 and _geno == 0):
mylineColor = self.HAPLOTYPE_HETEROZYGOUS
else:
mylineColor = self.HAPLOTYPE_RECOMBINATION # XZ: Unknown
im_drawer.line(
xy=((drawStart,
geneYLocation + 7 + 2 * ind * self.EACH_GENE_HEIGHT * zoom),
(drawEnd,
geneYLocation + 7 + 2 * ind * self.EACH_GENE_HEIGHT * zoom)),
fill=mylineColor, width=zoom * (self.EACH_GENE_HEIGHT + 2))
fillColor = BLACK
outlineColor = BLACK
if lastGene == 0:
im_drawer.rectangle(
xy=((geneStartPix,
geneYLocation + 2 * ind * self.EACH_GENE_HEIGHT * zoom),
(geneEndPix,
geneYLocation + 2 * ind * self.EACH_GENE_HEIGHT + 2 * self.EACH_GENE_HEIGHT * zoom)),
outline=outlineColor, fill=fillColor)
COORDS = "%d, %d, %d, %d" % (
geneStartPix, geneYLocation + ind * self.EACH_GENE_HEIGHT, geneEndPix + 1, (geneYLocation + ind * self.EACH_GENE_HEIGHT))
TITLE = "Strain: %s, marker (%s) \n Position %2.3f Mb." % (
samplelist[k], _chr[j].name, float(txStart))
HREF = ''
gifmap.append(
HtmlGenWrapper.create_area_tag(
shape='rect',
coords=COORDS,
href=HREF,
title=TITLE))
# if there are no more markers in a chromosome, the plotRight value calculated above will be before the plotWidth
# resulting in some empty space on the right side of the plot area. This draws an "unknown" bar from plotRight to the edge.
if (plotRight < (xLeftOffset + plotWidth - 3)) and (lastGene == 0):
drawEnd = xLeftOffset + plotWidth - 6
mylineColor = self.HAPLOTYPE_RECOMBINATION
im_drawer.line(
xy=((plotRight,
geneYLocation + 7 + 2 * ind * self.EACH_GENE_HEIGHT * zoom),
(drawEnd,
geneYLocation + 7 + 2 * ind * self.EACH_GENE_HEIGHT * zoom)),
fill=mylineColor, width=zoom * (self.EACH_GENE_HEIGHT + 2))
if lastGene == 0:
draw_rotated_text(
canvas, text="%s" % (_chr[j].name),
font=ImageFont.truetype(font=VERDANA_FILE,
size=12),
xy=(geneStartPix,
geneYLocation + 17 + 2 * maxind * self.EACH_GENE_HEIGHT * zoom),
fill=BLACK, angle=-90)
oldgeneEndPix = geneEndPix
oldgeno = _chr[j].genotype
firstGene = 0
else:
lastGene = 0
for i, _chr in enumerate(self.genotype):
if _chr.name == self.ChrList[self.selectedChr][0]:
for j, _geno in enumerate(_chr[1].genotype):
plotbxd = 0
if samplelist[j] in [item.name for item in smd]:
plotbxd = 1
if (plotbxd == 1):
ind = [item.name for item in smd].index(
samplelist[j]) - 1
expr = smd[ind+1].value
# Place where font is hardcoded
im_drawer.text(
text="%s" % (samplelist[j]),
xy=((xLeftOffset + plotWidth + 10),
geneYLocation + 11 + 2 * ind * self.EACH_GENE_HEIGHT * zoom),
font=ImageFont.truetype(
font=VERDANA_FILE, size=12),
fill=BLACK)
im_drawer.text(
text="%2.2f" % (expr),
xy=((xLeftOffset + plotWidth + 60),
geneYLocation + 11 + 2 * ind * self.EACH_GENE_HEIGHT * zoom),
font=ImageFont.truetype(
font=VERDANA_FILE, size=12),
fill=BLACK)
# END HaplotypeAnalyst
def drawClickBand(self, canvas, gifmap, plotXScale, offset=(40, 120, 80, 10), zoom=1, startMb=None, endMb=None):
im_drawer = ImageDraw.Draw(canvas)
if self.plotScale != 'physic' or self.selectedChr == -1:
return
xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset
plotWidth = canvas.size[0] - xLeftOffset - xRightOffset
plotHeight = canvas.size[1] - yTopOffset - yBottomOffset
yZero = canvas.size[1] - yBottomOffset
fontZoom = zoom
if zoom == 2:
fontZoom = 1.5
# only draw this many clickable regions (if you set it higher, you get more precision in clicking,
# but it makes the HTML huge, and takes forever to render the page in the first place)
# Draw the bands that you can click on to go to UCSC / Ensembl
MAX_CLICKABLE_REGION_DIVISIONS = 100
clickableRegionLabelFont = ImageFont.truetype(
font=VERDANA_FILE, size=9)
pixelStep = max(
5, int(float(plotWidth) / MAX_CLICKABLE_REGION_DIVISIONS))
# pixelStep: every N pixels, we make a new clickable area for the user to go to that area of the genome.
numBasesCurrentlyOnScreen = self.kONE_MILLION * \
abs(startMb - endMb) # Number of bases on screen now
flankingWidthInBases = int(
min((float(numBasesCurrentlyOnScreen) / 2.0), (5 * self.kONE_MILLION)))
webqtlZoomWidth = numBasesCurrentlyOnScreen / 16.0
# Flanking width should be such that we either zoom in to a 10 million base region, or we show the clicked region at the same scale as we are currently seeing.
currentChromosome = self.genotype[0].name
i = 0
paddingTop = yTopOffset
phenogenPaddingTop = paddingTop + \
(self.BAND_HEIGHT + self.BAND_SPACING)
if self.dataset.group.species == "mouse" or self.dataset.group.species == "rat":
ucscPaddingTop = paddingTop + 2 * \
(self.BAND_HEIGHT + self.BAND_SPACING)
ensemblPaddingTop = paddingTop + 3 * \
(self.BAND_HEIGHT + self.BAND_SPACING)
else:
ucscPaddingTop = paddingTop + \
(self.BAND_HEIGHT + self.BAND_SPACING)
ensemblPaddingTop = paddingTop + 2 * \
(self.BAND_HEIGHT + self.BAND_SPACING)
if zoom == 1:
for pixel in range(xLeftOffset, xLeftOffset + plotWidth, pixelStep):
calBase = self.kONE_MILLION * \
(startMb + (endMb - startMb) * \
(pixel - xLeftOffset - 0.0) / plotWidth)
xBrowse1 = pixel
xBrowse2 = min(xLeftOffset + plotWidth,
(pixel + pixelStep - 1))
WEBQTL_COORDS = "%d, %d, %d, %d" % (
xBrowse1, paddingTop, xBrowse2, (paddingTop + self.BAND_HEIGHT))
WEBQTL_HREF = "javascript:rangeView('%s', %f, %f)" % (self.selectedChr - 1, max(
0, (calBase - webqtlZoomWidth)) / 1000000.0, (calBase + webqtlZoomWidth) / 1000000.0)
WEBQTL_TITLE = "Click to view this section of the genome in WebQTL"
gifmap.append(
HtmlGenWrapper.create_area_tag(
shape='rect',
coords=WEBQTL_COORDS,
href=WEBQTL_HREF,
title=WEBQTL_TITLE))
im_drawer.rectangle(
xy=((xBrowse1, paddingTop),
(xBrowse2, (paddingTop + self.BAND_HEIGHT))),
outline=self.CLICKABLE_WEBQTL_REGION_COLOR,
fill=self.CLICKABLE_WEBQTL_REGION_COLOR)
im_drawer.line(
xy=((xBrowse1, paddingTop), (xBrowse1,
(paddingTop + self.BAND_HEIGHT))),
fill=self.CLICKABLE_WEBQTL_REGION_OUTLINE_COLOR)
if self.dataset.group.species == "mouse" or self.dataset.group.species == "rat":
PHENOGEN_COORDS = "%d, %d, %d, %d" % (
xBrowse1, phenogenPaddingTop, xBrowse2, (phenogenPaddingTop + self.BAND_HEIGHT))
if self.dataset.group.species == "mouse":
PHENOGEN_HREF = "https://phenogen.org/gene.jsp?speciesCB=Mm&auto=Y&geneTxt=chr%s:%d-%d&genomeVer=mm10" % (
self.selectedChr, max(0, calBase - flankingWidthInBases), calBase + flankingWidthInBases)
else:
PHENOGEN_HREF = "https://phenogen.org/gene.jsp?speciesCB=Mm&auto=Y&geneTxt=chr%s:%d-%d&genomeVer=mm10" % (
self.selectedChr, max(0, calBase - flankingWidthInBases), calBase + flankingWidthInBases)
PHENOGEN_TITLE = "Click to view this section of the genome in PhenoGen"
gifmap.append(
HtmlGenWrapper.create_area_tag(
shape='rect',
coords=PHENOGEN_COORDS,
href=PHENOGEN_HREF,
title=PHENOGEN_TITLE))
im_drawer.rectangle(
xy=((xBrowse1, phenogenPaddingTop),
(xBrowse2, (phenogenPaddingTop + self.BAND_HEIGHT))),
outline=self.CLICKABLE_PHENOGEN_REGION_COLOR,
fill=self.CLICKABLE_PHENOGEN_REGION_COLOR)
im_drawer.line(
xy=((xBrowse1, phenogenPaddingTop), (xBrowse1,
(phenogenPaddingTop + self.BAND_HEIGHT))),
fill=self.CLICKABLE_PHENOGEN_REGION_OUTLINE_COLOR)
UCSC_COORDS = "%d, %d, %d, %d" % (
xBrowse1, ucscPaddingTop, xBrowse2, (ucscPaddingTop + self.BAND_HEIGHT))
if self.dataset.group.species == "mouse":
UCSC_HREF = "http://genome.ucsc.edu/cgi-bin/hgTracks?db=%s&position=chr%s:%d-%d&hgt.customText=%s/snp/chr%s" % (
self._ucscDb, self.selectedChr, max(0, calBase - flankingWidthInBases), calBase + flankingWidthInBases, webqtlConfig.PORTADDR, self.selectedChr)
else:
UCSC_HREF = "http://genome.ucsc.edu/cgi-bin/hgTracks?db=%s&position=chr%s:%d-%d" % (
self._ucscDb, self.selectedChr, max(0, calBase - flankingWidthInBases), calBase + flankingWidthInBases)
UCSC_TITLE = "Click to view this section of the genome in the UCSC Genome Browser"
gifmap.append(
HtmlGenWrapper.create_area_tag(
shape='rect',
coords=UCSC_COORDS,
href=UCSC_HREF,
title=UCSC_TITLE))
im_drawer.rectangle(
xy=((xBrowse1, ucscPaddingTop),
(xBrowse2, (ucscPaddingTop + self.BAND_HEIGHT))),
outline=self.CLICKABLE_UCSC_REGION_COLOR,
fill=self.CLICKABLE_UCSC_REGION_COLOR)
im_drawer.line(
xy=((xBrowse1, ucscPaddingTop),
(xBrowse1, (ucscPaddingTop + self.BAND_HEIGHT))),
fill=self.CLICKABLE_UCSC_REGION_OUTLINE_COLOR)
ENSEMBL_COORDS = "%d, %d, %d, %d" % (
xBrowse1, ensemblPaddingTop, xBrowse2, (ensemblPaddingTop + self.BAND_HEIGHT))
if self.dataset.group.species == "mouse":
ENSEMBL_HREF = "http://www.ensembl.org/Mus_musculus/contigview?highlight=&chr=%s&vc_start=%d&vc_end=%d&x=35&y=12" % (
self.selectedChr, max(0, calBase - flankingWidthInBases), calBase + flankingWidthInBases)
else:
ENSEMBL_HREF = "http://www.ensembl.org/Rattus_norvegicus/contigview?chr=%s&start=%d&end=%d" % (
self.selectedChr, max(0, calBase - flankingWidthInBases), calBase + flankingWidthInBases)
ENSEMBL_TITLE = "Click to view this section of the genome in the Ensembl Genome Browser"
gifmap.append(HtmlGenWrapper.create_area_tag(
shape='rect',
coords=ENSEMBL_COORDS,
href=ENSEMBL_HREF,
title=ENSEMBL_TITLE))
im_drawer.rectangle(
xy=((xBrowse1, ensemblPaddingTop),
(xBrowse2, (ensemblPaddingTop + self.BAND_HEIGHT))),
outline=self.CLICKABLE_ENSEMBL_REGION_COLOR,
fill=self.CLICKABLE_ENSEMBL_REGION_COLOR)
im_drawer.line(
xy=((xBrowse1, ensemblPaddingTop),
(xBrowse1, (ensemblPaddingTop + self.BAND_HEIGHT))),
fill=self.CLICKABLE_ENSEMBL_REGION_OUTLINE_COLOR)
# end for
im_drawer.text(
text="Click to view the corresponding section of the genome in an 8x expanded WebQTL map",
xy=((xLeftOffset + 10), paddingTop), # + self.BAND_HEIGHT/2),
font=clickableRegionLabelFont,
fill=self.CLICKABLE_WEBQTL_TEXT_COLOR)
if self.dataset.group.species == "mouse" or self.dataset.group.species == "rat":
im_drawer.text(
text="Click to view the corresponding section of the genome in PhenoGen",
# + self.BAND_HEIGHT/2),
xy=((xLeftOffset + 10), phenogenPaddingTop),
font=clickableRegionLabelFont, fill=self.CLICKABLE_PHENOGEN_TEXT_COLOR)
im_drawer.text(
text="Click to view the corresponding section of the genome in the UCSC Genome Browser",
# + self.BAND_HEIGHT/2),
xy=((xLeftOffset + 10), ucscPaddingTop),
font=clickableRegionLabelFont, fill=self.CLICKABLE_UCSC_TEXT_COLOR)
im_drawer.text(
text="Click to view the corresponding section of the genome in the Ensembl Genome Browser",
# + self.BAND_HEIGHT/2),
xy=((xLeftOffset + 10), ensemblPaddingTop),
font=clickableRegionLabelFont, fill=self.CLICKABLE_ENSEMBL_TEXT_COLOR)
# draw the gray text
chrFont = ImageFont.truetype(
font=VERDANA_BOLD_FILE, size=26 * zoom)
chrX = xLeftOffset + plotWidth - 2 - im_drawer.textsize(
"Chr %s" % self.ChrList[self.selectedChr][0], font=chrFont)[0]
im_drawer.text(
text="Chr %s" % self.ChrList[self.selectedChr][0],
xy=(chrX, paddingTop), font=chrFont, fill=GRAY)
# end of drawBrowserClickableRegions
else:
# draw the gray text
chrFont = ImageFont.truetype(font=VERDANA_FILE, size=26 * zoom)
chrX = xLeftOffset + (plotWidth - im_drawer.textsize(
"Chr %s" % currentChromosome, font=chrFont)[0]) / 2
im_drawer.text(
text="Chr %s" % currentChromosome, xy=(chrX, 32), font=chrFont,
fill=GRAY)
# end of drawBrowserClickableRegions
pass
def drawXAxis(self, canvas, drawAreaHeight, gifmap, plotXScale, showLocusForm, offset=(40, 120, 80, 10), zoom=1, startMb=None, endMb=None):
im_drawer = ImageDraw.Draw(canvas)
xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset
plotWidth = canvas.size[0] - xLeftOffset - xRightOffset
plotHeight = canvas.size[1] - yTopOffset - yBottomOffset
yZero = canvas.size[1] - yBottomOffset
fontZoom = zoom
if zoom == 2:
fontZoom = 1.5
# Parameters
NUM_MINOR_TICKS = 5 # Number of minor ticks between major ticks
X_MAJOR_TICK_THICKNESS = 3
X_MINOR_TICK_THICKNESS = 1
X_AXIS_THICKNESS = 1 * zoom
# ======= Alex: Draw the X-axis labels (megabase location)
MBLabelFont = ImageFont.truetype(font=VERDANA_FILE, size=15 * zoom)
xMajorTickHeight = 10 * zoom # How high the tick extends below the axis
xMinorTickHeight = 5 * zoom
xAxisTickMarkColor = BLACK
xAxisLabelColor = BLACK
fontHeight = 12 * fontZoom # How tall the font that we're using is
spacingFromLabelToAxis = 10
if self.plotScale == 'physic':
strYLoc = yZero + MBLabelFont.font.height / 2
# Physical single chromosome view
if self.selectedChr > -1:
XScale = Plot.detScale(startMb, endMb)
XStart, XEnd, XStep = XScale
if XStep < 8:
XStep *= 2
spacingAmtX = spacingAmt = (XEnd - XStart) / XStep
j = 0
while abs(spacingAmtX - int(spacingAmtX)) >= spacingAmtX / 100.0 and j < 6:
j += 1
spacingAmtX *= 10
formatStr = '%%2.%df' % j
for counter, _Mb in enumerate(Plot.frange(XStart, XEnd, spacingAmt / NUM_MINOR_TICKS)):
if _Mb < startMb or _Mb > endMb:
continue
Xc = xLeftOffset + plotXScale * (_Mb - startMb)
if counter % NUM_MINOR_TICKS == 0: # Draw a MAJOR mark, not just a minor tick mark
im_drawer.line(xy=((Xc, yZero),
(Xc, yZero + xMajorTickHeight)),
fill=xAxisTickMarkColor,
width=X_MAJOR_TICK_THICKNESS) # Draw the MAJOR tick mark
# What Mbase location to put on the label
labelStr = str(formatStr % _Mb)
strWidth, strHeight = im_drawer.textsize(
labelStr, font=MBLabelFont)
drawStringXc = (Xc - (strWidth / 2.0))
im_drawer.text(xy=(drawStringXc, strYLoc),
text=labelStr, font=MBLabelFont,
fill=xAxisLabelColor)
else:
im_drawer.line(xy=((Xc, yZero),
(Xc, yZero + xMinorTickHeight)),
fill=xAxisTickMarkColor,
width=X_MINOR_TICK_THICKNESS) # Draw the MINOR tick mark
# Physical genome wide view
else:
distScale = 0
startPosX = xLeftOffset
for i, distLen in enumerate(self.ChrLengthDistList):
if distScale == 0: # universal scale in whole genome mapping
if distLen > 75:
distScale = 25
elif distLen > 30:
distScale = 10
else:
distScale = 5
for j, tickdists in enumerate(range(distScale, int(ceil(distLen)), distScale)):
im_drawer.line(
xy=((startPosX + tickdists * plotXScale, yZero),
(startPosX + tickdists * plotXScale, yZero + 7)),
fill=BLACK, width=1 * zoom)
if j % 2 == 0:
draw_rotated_text(
canvas, text=str(tickdists), font=MBLabelFont,
xy=(startPosX + tickdists * plotXScale,
yZero + 10 * zoom), fill=BLACK, angle=270)
startPosX += (self.ChrLengthDistList[i] + \
self.GraphInterval) * plotXScale
megabaseLabelFont = ImageFont.truetype(
font=VERDANA_FILE, size=int(18 * zoom * 1.5))
im_drawer.text(
text="Megabases",
xy=(
xLeftOffset + (plotWidth - im_drawer.textsize(
"Megabases", font=megabaseLabelFont)[0]) / 2,
strYLoc + MBLabelFont.font.height + 10 * (zoom % 2)),
font=megabaseLabelFont, fill=BLACK)
pass
else:
strYLoc = yZero + spacingFromLabelToAxis + MBLabelFont.font.height / 2
ChrAInfo = []
preLpos = -1
distinctCount = 0.0
if self.selectedChr == -1: # ZS: If viewing full genome/all chromosomes
for i, _chr in enumerate(self.genotype):
thisChr = []
Locus0CM = _chr[0].cM
nLoci = len(_chr)
if nLoci <= 8:
for _locus in _chr:
if _locus.name != ' - ':
if _locus.cM != preLpos:
distinctCount += 1
preLpos = _locus.cM
thisChr.append(
[_locus.name, _locus.cM - Locus0CM])
else:
for j in (0, round(nLoci / 4), round(nLoci / 2), round(nLoci * 3 / 4), -1):
while _chr[j].name == ' - ':
j += 1
if _chr[j].cM != preLpos:
distinctCount += 1
preLpos = _chr[j].cM
thisChr.append(
[_chr[j].name, _chr[j].cM - Locus0CM])
ChrAInfo.append(thisChr)
else:
for i, _chr in enumerate(self.genotype):
if _chr.name == self.ChrList[self.selectedChr][0]:
thisChr = []
Locus0CM = _chr[0].cM
for _locus in _chr:
if _locus.name != ' - ':
if _locus.cM != preLpos:
distinctCount += 1
preLpos = _locus.cM
thisChr.append(
[_locus.name, _locus.cM - Locus0CM])
ChrAInfo.append(thisChr)
stepA = (plotWidth + 0.0) / distinctCount
LRectWidth = 10
LRectHeight = 3
offsetA = -stepA
lineColor = LIGHTBLUE
startPosX = xLeftOffset
for j, ChrInfo in enumerate(ChrAInfo):
preLpos = -1
for i, item in enumerate(ChrInfo):
Lname, Lpos = item
if Lpos != preLpos:
offsetA += stepA
differ = 1
else:
differ = 0
preLpos = Lpos
Lpos *= plotXScale
if self.selectedChr > -1:
Zorder = i % 5
else:
Zorder = 0
if differ:
im_drawer.line(
xy=((startPosX + Lpos, yZero), (xLeftOffset + offsetA,\
yZero + 25)),
fill=lineColor)
im_drawer.line(
xy=((xLeftOffset + offsetA, yZero + 25), (xLeftOffset + offsetA,\
yZero + 40 + Zorder * (LRectWidth + 3))),
fill=lineColor)
rectColor = ORANGE
else:
im_drawer.line(
xy=((xLeftOffset + offsetA, yZero + 40 + Zorder * (LRectWidth + 3) - 3), (\
xLeftOffset + offsetA, yZero + 40 + Zorder * (LRectWidth + 3))),
fill=lineColor)
rectColor = DEEPPINK
im_drawer.rectangle(
xy=((xLeftOffset + offsetA, yZero + 40 + Zorder * (LRectWidth + 3)),
(xLeftOffset + offsetA - LRectHeight,
yZero + 40 + Zorder * (LRectWidth + 3) + LRectWidth)),
outline=rectColor, fill=rectColor, width=0)
COORDS = "%d,%d,%d,%d" % (xLeftOffset + offsetA - LRectHeight, yZero + 40 + Zorder * (LRectWidth + 3),\
xLeftOffset + offsetA, yZero + 40 + Zorder * (LRectWidth + 3) + LRectWidth)
HREF = "/show_trait?trait_id=%s&dataset=%s" % (
Lname, self.dataset.group.name + "Geno")
#HREF="javascript:showDatabase3('%s','%s','%s','');" % (showLocusForm,fd.RISet+"Geno", Lname)
Areas = HtmlGenWrapper.create_area_tag(
shape='rect',
coords=COORDS,
href=HREF,
target="_blank",
title="Locus : {}".format(Lname))
gifmap.append(Areas)
# piddle bug
if j == 0:
im_drawer.line(
xy=((startPosX, yZero), (startPosX, yZero + 40)),
fill=lineColor)
startPosX += (self.ChrLengthDistList[j] + \
self.GraphInterval) * plotXScale
centimorganLabelFont = ImageFont.truetype(
font=VERDANA_FILE, size=int(18 * zoom * 1.5))
im_drawer.text(
text="Centimorgans",
xy=(xLeftOffset + (plotWidth - im_drawer.textsize(
"Centimorgans", font=centimorganLabelFont)[0]) / 2,
strYLoc + MBLabelFont.font.height + 10 * (zoom % 2)),
font=centimorganLabelFont, fill=BLACK)
im_drawer.line(xy=((xLeftOffset, yZero), (xLeftOffset + plotWidth, yZero)),
fill=BLACK, width=X_AXIS_THICKNESS) # Draw the X axis itself
def drawQTL(self, canvas, drawAreaHeight, gifmap, plotXScale, offset=(40, 120, 80, 10), zoom=1, startMb=None, endMb=None):
im_drawer = ImageDraw.Draw(canvas)
xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset
plotWidth = canvas.size[0] - xLeftOffset - xRightOffset
plotHeight = canvas.size[1] - yTopOffset - yBottomOffset
fontZoom = zoom
if zoom == 2:
fontZoom = 1.5
INTERCROSS = (self.genotype.type == "intercross")
# draw the LRS scale
# We first determine whether or not we are using a sliding scale.
# If so, we need to compute the maximum LRS value to determine where the max y-value should be, and call this LRS_LOD_Max.
# LRSTop is then defined to be above the LRS_LOD_Max by enough to add one additional LRSScale increment.
# if we are using a set-scale, then we set LRSTop to be the user's value, and LRS_LOD_Max doesn't matter.
# ZS: This is a mess, but I don't know a better way to account for different mapping methods returning results in different formats + the option to change between LRS and LOD
if self.lrsMax <= 0: # sliding scale
if "lrs_value" in self.qtlresults[0]:
LRS_LOD_Max = max([result['lrs_value']
for result in self.qtlresults])
if self.LRS_LOD == "LOD" or self.LRS_LOD == "-logP":
LRS_LOD_Max = LRS_LOD_Max / self.LODFACTOR
if self.permChecked and self.nperm > 0 and not self.multipleInterval:
self.significant = min(
self.significant / self.LODFACTOR, webqtlConfig.MAXLRS)
self.suggestive = min(
self.suggestive / self.LODFACTOR, webqtlConfig.MAXLRS)
else:
if self.permChecked and self.nperm > 0 and not self.multipleInterval:
self.significant = min(
self.significant, webqtlConfig.MAXLRS)
self.suggestive = min(
self.suggestive, webqtlConfig.MAXLRS)
else:
pass
else:
LRS_LOD_Max = max([result['lod_score']
for result in self.qtlresults]) + 1
if self.LRS_LOD == "LRS":
LRS_LOD_Max = LRS_LOD_Max * self.LODFACTOR
if self.permChecked and self.nperm > 0 and not self.multipleInterval:
self.significant = min(
self.significant * self.LODFACTOR, webqtlConfig.MAXLRS)
self.suggestive = min(
self.suggestive * self.LODFACTOR, webqtlConfig.MAXLRS)
else:
if self.permChecked and self.nperm > 0 and not self.multipleInterval:
self.significant = min(
self.significant, webqtlConfig.MAXLRS)
self.suggestive = min(
self.suggestive, webqtlConfig.MAXLRS)
else:
pass
if self.permChecked and self.nperm > 0 and not self.multipleInterval:
LRS_LOD_Max = max(self.significant, LRS_LOD_Max)
# genotype trait will give infinite LRS
LRS_LOD_Max = min(LRS_LOD_Max, webqtlConfig.MAXLRS)
else:
LRS_LOD_Max = self.lrsMax
# ZS: Needed to pass to genome browser
js_data = json.loads(self.js_data)
if self.LRS_LOD == "LRS":
js_data['max_score'] = LRS_LOD_Max / 4.61
else:
js_data['max_score'] = LRS_LOD_Max
self.js_data = json.dumps(js_data)
LRSScaleFont = ImageFont.truetype(font=VERDANA_FILE, size=16 * zoom)
LRSLODFont = ImageFont.truetype(
font=VERDANA_FILE, size=int(18 * zoom * 1.5))
yZero = yTopOffset + plotHeight
LRSHeightThresh = drawAreaHeight
AdditiveHeightThresh = drawAreaHeight / 2
DominanceHeightThresh = drawAreaHeight / 2
if LRS_LOD_Max > 100:
LRSScale = 20.0
elif LRS_LOD_Max > 20:
LRSScale = 5.0
elif LRS_LOD_Max > 7.5:
LRSScale = 2.5
else:
LRSScale = 1.0
LRSAxisList = Plot.frange(LRSScale, LRS_LOD_Max, LRSScale)
# ZS: Convert to int if all axis values are whole numbers
all_int = True
for item in LRSAxisList:
if isinstance(item, int):
continue
else:
all_int = False
break
# TODO(PIL): Convert from PIL
# if all_int:
# max_lrs_width = canvas.stringWidth("%d" % LRS_LOD_Max, font=LRSScaleFont) + 40
# else:
# max_lrs_width = canvas.stringWidth("%2.1f" % LRS_LOD_Max, font=LRSScaleFont) + 30
# draw the "LRS" or "LOD" string to the left of the axis
LRSScaleFont = ImageFont.truetype(font=VERDANA_FILE, size=16 * zoom)
LRSLODFont = ImageFont.truetype(
font=VERDANA_FILE, size=int(18 * zoom * 1.5))
yZero = yTopOffset + plotHeight
# TEXT_X_DISPLACEMENT = -20
#TEXT_Y_DISPLACEMENT = -215
if all_int:
TEXT_X_DISPLACEMENT = -12
else:
TEXT_X_DISPLACEMENT = -30
if self.LRS_LOD == "-logP":
TEXT_Y_DISPLACEMENT = -242
else:
TEXT_Y_DISPLACEMENT = -210
draw_rotated_text(
canvas, text=self.LRS_LOD, font=LRSLODFont,
xy=(xLeftOffset - im_drawer.textsize(
"999.99", font=LRSScaleFont)[0] - 15 * (zoom - 1) + TEXT_X_DISPLACEMENT,
yZero + TEXT_Y_DISPLACEMENT - 300 * (zoom - 1)),
fill=BLACK, angle=90)
for item in LRSAxisList:
if LRS_LOD_Max == 0.0:
LRS_LOD_Max = 0.000001
yTopOffset + 30 * (zoom - 1)
yLRS = yZero - (item / LRS_LOD_Max) * LRSHeightThresh
im_drawer.line(xy=((xLeftOffset, yLRS), (xLeftOffset - 4, yLRS)),
fill=self.LRS_COLOR, width=1 * zoom)
if all_int:
scaleStr = "%d" % item
else:
scaleStr = "%2.1f" % item
# Draw the LRS/LOD Y axis label
TEXT_Y_DISPLACEMENT = -10
im_drawer.text(
text=scaleStr,
xy=(xLeftOffset - 4 - im_drawer.textsize(scaleStr, font=LRSScaleFont)[0] - 5,
yLRS + TEXT_Y_DISPLACEMENT),
font=LRSScaleFont, fill=self.LRS_COLOR)
if self.permChecked and self.nperm > 0 and not self.multipleInterval:
significantY = yZero - self.significant * LRSHeightThresh / LRS_LOD_Max
suggestiveY = yZero - self.suggestive * LRSHeightThresh / LRS_LOD_Max
# significantY = yZero - self.significant*LRSHeightThresh/LRSAxisList[-1]
# suggestiveY = yZero - self.suggestive*LRSHeightThresh/LRSAxisList[-1]
startPosX = xLeftOffset
# "Significant" and "Suggestive" Drawing Routine
# ======= Draw the thick lines for "Significant" and "Suggestive" ===== (crowell: I tried to make the SNPs draw over these lines, but piddle wouldn't have it...)
# ZS: I don't know if what I did here with this inner function is clever or overly complicated, but it's the only way I could think of to avoid duplicating the code inside this function
def add_suggestive_significant_lines_and_legend(start_pos_x, chr_length_dist):
rightEdge = xLeftOffset + plotWidth
im_drawer.line(
xy=((start_pos_x + self.SUGGESTIVE_WIDTH / 1.5, suggestiveY),
(rightEdge, suggestiveY)),
fill=self.SUGGESTIVE_COLOR, width=self.SUGGESTIVE_WIDTH * zoom
# ,clipX=(xLeftOffset, xLeftOffset + plotWidth-2)
)
im_drawer.line(
xy=((start_pos_x + self.SUGGESTIVE_WIDTH / 1.5, significantY),
(rightEdge, significantY)),
fill=self.SIGNIFICANT_COLOR,
width=self.SIGNIFICANT_WIDTH * zoom
# , clipX=(xLeftOffset, xLeftOffset + plotWidth-2)
)
sugg_coords = "%d, %d, %d, %d" % (
start_pos_x, suggestiveY - 2, rightEdge + 2 * zoom, suggestiveY + 2)
sig_coords = "%d, %d, %d, %d" % (
start_pos_x, significantY - 2, rightEdge + 2 * zoom, significantY + 2)
if self.LRS_LOD == 'LRS':
sugg_title = "Suggestive LRS = %0.2f" % self.suggestive
sig_title = "Significant LRS = %0.2f" % self.significant
else:
sugg_title = "Suggestive LOD = %0.2f" % (
self.suggestive / 4.61)
sig_title = "Significant LOD = %0.2f" % (
self.significant / 4.61)
Areas1 = HtmlGenWrapper.create_area_tag(
shape='rect',
coords=sugg_coords,
title=sugg_title)
Areas2 = HtmlGenWrapper.create_area_tag(
shape='rect',
coords=sig_coords,
title=sig_title)
gifmap.append(Areas1)
gifmap.append(Areas2)
start_pos_x += (chr_length_dist + \
self.GraphInterval) * plotXScale
return start_pos_x
for i, _chr in enumerate(self.genotype):
if self.selectedChr != -1:
if _chr.name == self.ChrList[self.selectedChr][0]:
startPosX = add_suggestive_significant_lines_and_legend(
startPosX, self.ChrLengthDistList[0])
break
else:
continue
else:
startPosX = add_suggestive_significant_lines_and_legend(
startPosX, self.ChrLengthDistList[i])
if self.multipleInterval:
lrsEdgeWidth = 1
else:
if self.additiveChecked:
additiveMax = max([abs(X['additive'])
for X in self.qtlresults])
lrsEdgeWidth = 3
if zoom == 2:
lrsEdgeWidth = 2 * lrsEdgeWidth
LRSCoordXY = []
AdditiveCoordXY = []
DominanceCoordXY = []
symbolFont = ImageFont.truetype(
font=FNT_BS_FILE, size=5) # ZS: For Manhattan Plot
previous_chr = 1
previous_chr_as_int = 0
lineWidth = 1
oldStartPosX = 0
startPosX = xLeftOffset
for i, qtlresult in enumerate(self.qtlresults):
m = 0
thisLRSColor = self.colorCollection[0]
if qtlresult['chr'] != previous_chr and self.selectedChr == -1:
if self.manhattan_plot != True and len(LRSCoordXY) > 1:
draw_open_polygon(canvas, xy=LRSCoordXY,
outline=thisLRSColor, width=lrsEdgeWidth)
if not self.multipleInterval and self.additiveChecked:
plusColor = self.ADDITIVE_COLOR_POSITIVE
minusColor = self.ADDITIVE_COLOR_NEGATIVE
for k, aPoint in enumerate(AdditiveCoordXY):
if k > 0:
Xc0, Yc0 = AdditiveCoordXY[k - 1]
Xc, Yc = aPoint
if (Yc0 - yZero) * (Yc - yZero) < 0:
if Xc == Xc0: # genotype , locus distance is 0
Xcm = Xc
else:
Xcm = (yZero - Yc0) / \
((Yc - Yc0) / (Xc - Xc0)) + Xc0
if Yc0 < yZero:
im_drawer.line(
xy=((Xc0, Yc0), (Xcm, yZero)),
fill=plusColor, width=lineWidth
)
im_drawer.line(
xy=((Xcm, yZero),
(Xc, yZero - (Yc - yZero))),
fill=minusColor, width=lineWidth
)
else:
im_drawer.line(
xy=((Xc0, yZero - (Yc0 - yZero)),
(Xcm, yZero)),
fill=minusColor, width=lineWidth
)
im_drawer.line(
xy=((Xcm, yZero), (Xc, Yc)),
fill=plusColor, width=lineWidth
)
elif (Yc0 - yZero) * (Yc - yZero) > 0:
if Yc < yZero:
im_drawer.line(
xy=((Xc0, Yc0), (Xc, Yc)),
fill=plusColor,
width=lineWidth
)
else:
im_drawer.line(
xy=((Xc0, yZero - (Yc0 - yZero)),
(Xc, yZero - (Yc - yZero))),
fill=minusColor, width=lineWidth
)
else:
minYc = min(Yc - yZero, Yc0 - yZero)
if minYc < 0:
im_drawer.line(
xy=((Xc0, Yc0), (Xc, Yc)),
fill=plusColor, width=lineWidth
)
else:
im_drawer.line(
xy=((Xc0, yZero - (Yc0 - yZero)),
(Xc, yZero - (Yc - yZero))),
fill=minusColor, width=lineWidth
)
LRSCoordXY = []
AdditiveCoordXY = []
previous_chr = qtlresult['chr']
previous_chr_as_int += 1
newStartPosX = (
self.ChrLengthDistList[previous_chr_as_int - 1] + self.GraphInterval) * plotXScale
if newStartPosX != oldStartPosX:
startPosX += newStartPosX
oldStartPosX = newStartPosX
# This is because the chromosome value stored in qtlresult['chr'] can be (for example) either X or 20 depending upon the mapping method/scale used
this_chr = str(self.ChrList[self.selectedChr][0])
if self.plotScale != "physic":
this_chr = str(self.ChrList[self.selectedChr][1] + 1)
if self.selectedChr == -1 or str(qtlresult['chr']) == this_chr:
if self.plotScale != "physic" and self.mapping_method == "reaper" and not self.manhattan_plot:
start_cm = self.genotype[self.selectedChr - 1][0].cM
Xc = startPosX + (qtlresult['cM'] - start_cm) * plotXScale
if hasattr(self.genotype, "filler"):
if self.genotype.filler:
if self.selectedChr != -1:
Xc = startPosX + \
(qtlresult['Mb'] - start_cm) * plotXScale
else:
Xc = startPosX + ((qtlresult['Mb'] - start_cm - startMb) * plotXScale) * (
((qtlresult['Mb'] - start_cm - startMb) * plotXScale) / ((qtlresult['Mb'] - start_cm - startMb + self.GraphInterval) * plotXScale))
else:
if self.selectedChr != -1 and qtlresult['Mb'] > endMb:
Xc = startPosX + endMb * plotXScale
else:
if qtlresult['Mb'] - startMb < 0:
continue
Xc = startPosX + (qtlresult['Mb'] - startMb) * plotXScale
# updated by NL 06-18-2011:
# fix the over limit LRS graph issue since genotype trait may give infinite LRS;
# for any lrs is over than 460(LRS max in this system), it will be reset to 460
yLRS = yZero - (item / LRS_LOD_Max) * LRSHeightThresh
if 'lrs_value' in qtlresult:
if self.LRS_LOD == "LOD" or self.LRS_LOD == "-logP":
if qtlresult['lrs_value'] > 460 or qtlresult['lrs_value'] == 'inf':
Yc = yZero - webqtlConfig.MAXLRS * \
LRSHeightThresh / \
(LRS_LOD_Max * self.LODFACTOR)
else:
Yc = yZero - \
qtlresult['lrs_value'] * LRSHeightThresh / \
(LRS_LOD_Max * self.LODFACTOR)
else:
if qtlresult['lrs_value'] > 460 or qtlresult['lrs_value'] == 'inf':
Yc = yZero - webqtlConfig.MAXLRS * LRSHeightThresh / LRS_LOD_Max
else:
Yc = yZero - \
qtlresult['lrs_value'] * \
LRSHeightThresh / LRS_LOD_Max
else:
if qtlresult['lod_score'] > 100 or qtlresult['lod_score'] == 'inf':
Yc = yZero - webqtlConfig.MAXLRS * LRSHeightThresh / LRS_LOD_Max
else:
if self.LRS_LOD == "LRS":
Yc = yZero - \
qtlresult['lod_score'] * self.LODFACTOR * \
LRSHeightThresh / LRS_LOD_Max
else:
Yc = yZero - \
qtlresult['lod_score'] * \
LRSHeightThresh / LRS_LOD_Max
if self.manhattan_plot == True:
if self.color_scheme == "single":
point_color = self.manhattan_single_color
elif self.color_scheme == "varied":
point_color = DISTINCT_COLOR_LIST[previous_chr_as_int]
else:
if self.selectedChr == -1 and (previous_chr_as_int % 2 == 1):
point_color = RED
else:
point_color = BLUE
im_drawer.text(
text="5",
xy=(
Xc - im_drawer.textsize("5",
font=symbolFont)[0] / 2 + 1,
Yc - 4),
fill=point_color, font=symbolFont)
else:
LRSCoordXY.append((Xc, Yc))
if not self.multipleInterval and self.additiveChecked:
if additiveMax == 0.0:
additiveMax = 0.000001
Yc = yZero - qtlresult['additive'] * \
AdditiveHeightThresh / additiveMax
AdditiveCoordXY.append((Xc, Yc))
if self.selectedChr != -1 and qtlresult['Mb'] > endMb and endMb != -1:
break
m += 1
if self.manhattan_plot != True and len(LRSCoordXY) > 1:
draw_open_polygon(canvas, xy=LRSCoordXY, outline=thisLRSColor,
width=lrsEdgeWidth)
if not self.multipleInterval and self.additiveChecked:
plusColor = self.ADDITIVE_COLOR_POSITIVE
minusColor = self.ADDITIVE_COLOR_NEGATIVE
for k, aPoint in enumerate(AdditiveCoordXY):
if k > 0:
Xc0, Yc0 = AdditiveCoordXY[k - 1]
Xc, Yc = aPoint
if (Yc0 - yZero) * (Yc - yZero) < 0:
if Xc == Xc0: # genotype , locus distance is 0
Xcm = Xc
else:
Xcm = (yZero - Yc0) / \
((Yc - Yc0) / (Xc - Xc0)) + Xc0
if Yc0 < yZero:
im_drawer.line(
xy=((Xc0, Yc0), (Xcm, yZero)),
fill=plusColor, width=lineWidth
# , clipX=(xLeftOffset, xLeftOffset + plotWidth)
)
im_drawer.line(
xy=((Xcm, yZero), (Xc, yZero - (Yc - yZero))),
fill=minusColor, width=lineWidth
# , clipX=(xLeftOffset, xLeftOffset + plotWidth)
)
else:
im_drawer.line(
xy=((Xc0, yZero - (Yc0 - yZero)),
(Xcm, yZero)),
fill=minusColor, width=lineWidth
# , clipX=(xLeftOffset, xLeftOffset + plotWidth)
)
im_drawer.line(
xy=((Xcm, yZero), (Xc, Yc)),
fill=plusColor, width=lineWidth
# , clipX=(xLeftOffset, xLeftOffset + plotWidth)
)
elif (Yc0 - yZero) * (Yc - yZero) > 0:
if Yc < yZero:
im_drawer.line(
xy=((Xc0, Yc0), (Xc, Yc)), fill=plusColor,
width=lineWidth
# , clipX=(xLeftOffset, xLeftOffset + plotWidth)
)
else:
im_drawer.line(
xy=((Xc0, yZero - (Yc0 - yZero)),
(Xc, yZero - (Yc - yZero))),
fill=minusColor, width=lineWidth
# , clipX=(xLeftOffset, xLeftOffset + plotWidth)
)
else:
minYc = min(Yc - yZero, Yc0 - yZero)
if minYc < 0:
im_drawer.line(
xy=((Xc0, Yc0), (Xc, Yc)),
fill=plusColor, width=lineWidth
# , clipX=(xLeftOffset, xLeftOffset + plotWidth)
)
else:
im_drawer.line(
xy=((Xc0, yZero - (Yc0 - yZero)),
(Xc, yZero - (Yc - yZero))),
fill=minusColor, width=lineWidth
# , clipX=(xLeftOffset, xLeftOffset + plotWidth)
)
if not self.multipleInterval and INTERCROSS and self.dominanceChecked:
plusColor = self.DOMINANCE_COLOR_POSITIVE
minusColor = self.DOMINANCE_COLOR_NEGATIVE
for k, aPoint in enumerate(DominanceCoordXY):
if k > 0:
Xc0, Yc0 = DominanceCoordXY[k - 1]
Xc, Yc = aPoint
if (Yc0 - yZero) * (Yc - yZero) < 0:
if Xc == Xc0: # genotype , locus distance is 0
Xcm = Xc
else:
Xcm = (yZero - Yc0) / \
((Yc - Yc0) / (Xc - Xc0)) + Xc0
if Yc0 < yZero:
im_drawer.line(
xy=((Xc0, Yc0), (Xcm, yZero)),
fill=plusColor, width=lineWidth
# , clipX=(xLeftOffset, xLeftOffset + plotWidth)
)
im_drawer.line(
xy=((Xcm, yZero), (Xc, yZero - (Yc - yZero))),
fill=minusColor, width=lineWidth
# , clipX=(xLeftOffset, xLeftOffset + plotWidth)
)
else:
im_drawer.line(
xy=((Xc0, yZero - (Yc0 - yZero)), (Xcm, yZero)),
fill=minusColor, width=lineWidth
# , clipX=(xLeftOffset, xLeftOffset + plotWidth)
)
im_drawer.line(
xy=((Xcm, yZero), (Xc, Yc)),
fill=plusColor, width=lineWidth
# , clipX=(xLeftOffset, xLeftOffset + plotWidth)
)
elif (Yc0 - yZero) * (Yc - yZero) > 0:
if Yc < yZero:
im_drawer.line(
xy=((Xc0, Yc0), (Xc, Yc)),
fill=plusColor, width=lineWidth
# , clipX=(xLeftOffset, xLeftOffset + plotWidth)
)
else:
im_drawer.line(
xy=((Xc0, yZero - (Yc0 - yZero)),
(Xc, yZero - (Yc - yZero))),
fill=minusColor, width=lineWidth
# , clipX=(xLeftOffset, xLeftOffset + plotWidth)
)
else:
minYc = min(Yc - yZero, Yc0 - yZero)
if minYc < 0:
im_drawer.line(
xy=((Xc0, Yc0), (Xc, Yc)),
fill=plusColor, width=lineWidth
# , clipX=(xLeftOffset, xLeftOffset + plotWidth)
)
else:
im_drawer.line(
xy=((Xc0, yZero - (Yc0 - yZero)),
(Xc, yZero - (Yc - yZero))), fill=minusColor,
width=lineWidth
# , clipX=(xLeftOffset, xLeftOffset + plotWidth)
)
# draw additive scale
if not self.multipleInterval and self.additiveChecked:
additiveScaleFont = ImageFont.truetype(
font=VERDANA_FILE, size=16 * zoom)
additiveScale = Plot.detScaleOld(0, additiveMax)
additiveStep = (additiveScale[1] - \
additiveScale[0]) / additiveScale[2]
additiveAxisList = Plot.frange(0, additiveScale[1], additiveStep)
addPlotScale = AdditiveHeightThresh / additiveMax
TEXT_Y_DISPLACEMENT = -8
additiveAxisList.append(additiveScale[1])
for item in additiveAxisList:
additiveY = yZero - item * addPlotScale
im_drawer.line(
xy=((xLeftOffset + plotWidth, additiveY),
(xLeftOffset + 4 + plotWidth, additiveY)),
fill=self.ADDITIVE_COLOR_POSITIVE, width=1 * zoom)
scaleStr = "%2.3f" % item
im_drawer.text(
text=scaleStr,
xy=(xLeftOffset + plotWidth + 6,
additiveY + TEXT_Y_DISPLACEMENT),
font=additiveScaleFont, fill=self.ADDITIVE_COLOR_POSITIVE)
im_drawer.line(
xy=((xLeftOffset + plotWidth, additiveY),
(xLeftOffset + plotWidth, yZero)),
fill=self.ADDITIVE_COLOR_POSITIVE, width=1 * zoom)
im_drawer.line(
xy=((xLeftOffset, yZero), (xLeftOffset, yTopOffset + 30 * (zoom - 1))),
fill=self.LRS_COLOR, width=1 * zoom) # the blue line running up the y axis
def drawGraphBackground(self, canvas, gifmap, offset=(80, 120, 80, 50), zoom=1, startMb=None, endMb=None):
# conditions
# multiple Chromosome view
# single Chromosome Physical
# single Chromosome Genetic
im_drawer = ImageDraw.Draw(canvas)
xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset
plotWidth = canvas.size[0] - xLeftOffset - xRightOffset
plotHeight = canvas.size[1] - yTopOffset - yBottomOffset
yBottom = yTopOffset + plotHeight
fontZoom = zoom
if zoom == 2:
fontZoom = 1.5
yTopOffset += 30
# calculate plot scale
if self.plotScale != 'physic':
self.ChrLengthDistList = self.ChrLengthCMList
drawRegionDistance = self.ChrLengthCMSum
else:
self.ChrLengthDistList = self.ChrLengthMbList
drawRegionDistance = self.ChrLengthMbSum
if self.selectedChr > -1: # single chromosome view
spacingAmt = plotWidth / 13.5
i = 0
for startPix in Plot.frange(xLeftOffset, xLeftOffset + plotWidth, spacingAmt):
if (i % 2 == 0):
theBackColor = self.GRAPH_BACK_DARK_COLOR
else:
theBackColor = self.GRAPH_BACK_LIGHT_COLOR
i += 1
im_drawer.rectangle(
[(startPix, yTopOffset),
(min(startPix + spacingAmt, xLeftOffset + plotWidth), yBottom)],
outline=theBackColor, fill=theBackColor)
drawRegionDistance = self.ChrLengthDistList[self.ChrList[self.selectedChr][1]]
self.ChrLengthDistList = [drawRegionDistance]
if self.plotScale == 'physic':
plotXScale = plotWidth / (endMb - startMb)
else:
plotXScale = plotWidth / drawRegionDistance
else: # multiple chromosome view
plotXScale = plotWidth / \
((len(self.genotype) - 1) * self.GraphInterval + drawRegionDistance)
startPosX = xLeftOffset
if fontZoom == 1.5:
chrFontZoom = 2
else:
chrFontZoom = 1
chrLabelFont = ImageFont.truetype(
font=VERDANA_FILE, size=24 * chrFontZoom)
for i, _chr in enumerate(self.genotype):
if (i % 2 == 0):
theBackColor = self.GRAPH_BACK_DARK_COLOR
else:
theBackColor = self.GRAPH_BACK_LIGHT_COLOR
# draw the shaded boxes and the sig/sug thick lines
im_drawer.rectangle(
((startPosX, yTopOffset),
(startPosX + self.ChrLengthDistList[i] * plotXScale, yBottom)),
outline=GAINSBORO,
fill=theBackColor)
chrNameWidth, chrNameHeight = im_drawer.textsize(
_chr.name, font=chrLabelFont)
chrStartPix = startPosX + \
(self.ChrLengthDistList[i] * plotXScale - chrNameWidth) / 2
chrEndPix = startPosX + \
(self.ChrLengthDistList[i] * plotXScale + chrNameWidth) / 2
TEXT_Y_DISPLACEMENT = 0
im_drawer.text(xy=(chrStartPix, yTopOffset + TEXT_Y_DISPLACEMENT),
text=_chr.name, font=chrLabelFont, fill=BLACK)
COORDS = "%d,%d,%d,%d" % (
chrStartPix, yTopOffset, chrEndPix, yTopOffset + 20)
# add by NL 09-03-2010
HREF = "javascript:chrView(%d,%s);" % (i, self.ChrLengthMbList)
Areas = HtmlGenWrapper.create_area_tag(
shape='rect',
coords=COORDS,
href=HREF)
gifmap.append(Areas)
startPosX += (self.ChrLengthDistList[i] + \
self.GraphInterval) * plotXScale
return plotXScale
def drawPermutationHistogram(self):
#########################################
# Permutation Graph
#########################################
myCanvas = Image.new("RGBA", size=(500, 300))
if 'lod_score' in self.qtlresults[0] and self.LRS_LOD == "LRS":
perm_output = [value * 4.61 for value in self.perm_output]
elif 'lod_score' not in self.qtlresults[0] and self.LRS_LOD == "LOD":
perm_output = [value / 4.61 for value in self.perm_output]
else:
perm_output = self.perm_output
filename = webqtlUtil.genRandStr("Reg_")
Plot.plotBar(myCanvas, perm_output, XLabel=self.LRS_LOD,
YLabel='Frequency', title=' Histogram of Permutation Test')
myCanvas.save("{}.gif".format(GENERATED_IMAGE_DIR + filename),
format='gif')
return filename
def geneTable(self, geneCol, refGene=None):
if self.dataset.group.species == 'mouse' or self.dataset.group.species == 'rat':
self.gene_table_header = self.getGeneTableHeaderList(refGene=None)
self.gene_table_body = self.getGeneTableBody(geneCol, refGene=None)
else:
self.gene_table_header = None
self.gene_table_body = None
def getGeneTableHeaderList(self, refGene=None):
gene_table_header_list = []
if self.dataset.group.species == "mouse":
if refGene:
gene_table_header_list = ["Index",
"Symbol",
"Mb Start",
"Length (Kb)",
"SNP Count",
"SNP Density",
"Avg Expr",
"Human Chr",
"Mb Start (hg19)",
"Literature Correlation",
"Gene Description"]
else:
gene_table_header_list = ["",
"Index",
"Symbol",
"Mb Start",
"Length (Kb)",
"SNP Count",
"SNP Density",
"Avg Expr",
"Human Chr",
"Mb Start (hg19)",
"Gene Description"]
elif self.dataset.group.species == "rat":
gene_table_header_list = ["",
"Index",
"Symbol",
"Mb Start",
"Length (Kb)",
"Avg Expr",
"Mouse Chr",
"Mb Start (mm9)",
"Human Chr",
"Mb Start (hg19)",
"Gene Description"]
return gene_table_header_list
def getGeneTableBody(self, geneCol, refGene=None):
gene_table_body = []
tableIterationsCnt = 0
if self.dataset.group.species == "mouse":
for gIndex, theGO in enumerate(geneCol):
tableIterationsCnt = tableIterationsCnt + 1
this_row = [] # container for the cells of each row
selectCheck = HtmlGenWrapper.create_input_tag(
type_="checkbox",
name="selectCheck",
value=theGO["GeneSymbol"],
Class="checkbox trait_checkbox") # checkbox for each row
geneLength = (theGO["TxEnd"] - theGO["TxStart"]) * 1000.0
tenPercentLength = geneLength * 0.0001
txStart = theGO["TxStart"]
txEnd = theGO["TxEnd"]
theGO["snpDensity"] = theGO["snpCount"] / geneLength
if self.ALEX_DEBUG_BOOL_PRINT_GENE_LIST:
geneIdString = 'http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=gene&cmd=Retrieve&dopt=Graphics&list_uids=%s' % theGO[
"GeneID"]
if theGO["snpCount"]:
snpString = HT.Link(
(f"http://genenetwork.org/webqtl/main.py?FormID=snpBrowser&"
f"chr={theGO['Chr']}&"
f"start={theGO['TxStart']}&"
f"end={theGO['TxEnd']}&"
f"geneName={theGO['GeneSymbol']}&"
f"s1={self.diffCol[0]}&s2=%d"),
str(theGO["snpCount"]) # The text to display
)
snpString.set_blank_target()
snpString.set_attribute("class", "normalsize")
else:
snpString = 0
mouseStartString = "http://genome.ucsc.edu/cgi-bin/hgTracks?clade=vertebrate&org=Mouse&db=mm10&position=chr" + \
theGO["Chr"] + "%3A" + str(int(theGO["TxStart"] * 1000000.0)) + "-" + str(
int(theGO["TxEnd"] * 1000000.0)) + "&pix=620&Submit=submit"
# the chromosomes for human 1 are 1qXX.XX
if 'humanGene' in theGO:
if theGO['humanGene']["TxStart"] == '':
humanStartDisplay = ""
else:
humanStartDisplay = "%0.6f" % theGO['humanGene']["TxStart"]
humanChr = theGO['humanGene']["Chr"]
humanTxStart = theGO['humanGene']["TxStart"]
humanStartString = "http://genome.ucsc.edu/cgi-bin/hgTracks?clade=vertebrate&org=Human&db=hg17&position=chr%s:%d-%d" % (
humanChr, int(1000000 * theGO['humanGene']["TxStart"]), int(1000000 * theGO['humanGene']["TxEnd"]))
else:
humanStartString = humanChr = humanStartDisplay = "--"
geneDescription = theGO["GeneDescription"]
if len(geneDescription) > 70:
geneDescription = geneDescription[:70] + "..."
if theGO["snpDensity"] < 0.000001:
snpDensityStr = "0"
else:
snpDensityStr = "%0.6f" % theGO["snpDensity"]
avgExpr = [] # theGO["avgExprVal"]
if avgExpr in ([], None):
avgExpr = "--"
else:
avgExpr = "%0.6f" % avgExpr
# If we have a referenceGene then we will show the Literature Correlation
if theGO["Chr"] == "X":
chr_as_int = 19
else:
chr_as_int = int(theGO["Chr"]) - 1
if refGene:
literatureCorrelationString = str(self.getLiteratureCorrelation(
self.cursor, refGene, theGO['GeneID']) or "N/A")
this_row = [selectCheck.__str__(),
str(tableIterationsCnt),
str(HtmlGenWrapper.create_link_tag(
geneIdString,
theGO["GeneSymbol"],
target="_blank")
),
str(HtmlGenWrapper.create_link_tag(
mouseStartString,
"{:.6f}".format(txStart),
target="_blank")
),
str(HtmlGenWrapper.create_link_tag(
"javascript:rangeView('{}', {:f}, {:f})".format(
str(chr_as_int),
txStart - tenPercentLength,
txEnd + tenPercentLength),
"{:.3f}".format(geneLength))),
snpString,
snpDensityStr,
avgExpr,
humanChr,
str(HtmlGenWrapper.create_link_tag(
humanStartString,
humanStartDisplay,
target="_blank")),
literatureCorrelationString,
geneDescription]
else:
this_row = [selectCheck.__str__(),
str(tableIterationsCnt),
str(HtmlGenWrapper.create_link_tag(
geneIdString, theGO["GeneSymbol"],
target="_blank")),
str(HtmlGenWrapper.create_link_tag(
mouseStartString,
"{:.6f}".format(txStart),
target="_blank")),
str(HtmlGenWrapper.create_link_tag(
"javascript:rangeView('{}', {:f}, {:f})".format(
str(chr_as_int),
txStart - tenPercentLength,
txEnd + tenPercentLength),
"{:.3f}".format(geneLength))),
snpString,
snpDensityStr,
avgExpr,
humanChr,
str(HtmlGenWrapper.create_link_tag(
humanStartString,
humanStartDisplay,
target="_blank")),
geneDescription]
gene_table_body.append(this_row)
elif self.dataset.group.species == 'rat':
for gIndex, theGO in enumerate(geneCol):
this_row = [] # container for the cells of each row
selectCheck = str(HtmlGenWrapper.create_input_tag(
type_="checkbox",
name="selectCheck",
Class="checkbox trait_checkbox")) # checkbox for each row
if theGO["GeneID"] != "":
geneSymbolNCBI = str(HtmlGenWrapper.create_link_tag(
"http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=gene&cmd=Retrieve&dopt=Graphics&list_uids={}".format(
theGO["GeneID"]),
theGO["GeneSymbol"],
Class="normalsize",
target="_blank"))
else:
geneSymbolNCBI = theGO["GeneSymbol"]
if theGO["Chr"] == "X":
chr_as_int = 20
else:
chr_as_int = int(theGO["Chr"]) - 1
geneLength = (float(theGO["TxEnd"]) - float(theGO["TxStart"]))
geneLengthURL = "javascript:rangeView('%s', %f, %f)" % (theGO["Chr"], float(
theGO["TxStart"]) - (geneLength * 0.1), float(theGO["TxEnd"]) + (geneLength * 0.1))
avgExprVal = []
if avgExprVal != "" and avgExprVal:
avgExprVal = "%0.5f" % float(avgExprVal)
else:
avgExprVal = ""
# Mouse Gene
if theGO['mouseGene']:
mouseChr = theGO['mouseGene']["Chr"]
mouseTxStart = "%0.6f" % theGO['mouseGene']["TxStart"]
else:
mouseChr = mouseTxStart = ""
# the chromosomes for human 1 are 1qXX.XX
if 'humanGene' in theGO:
humanChr = theGO['humanGene']["Chr"]
humanTxStart = "%0.6f" % theGO['humanGene']["TxStart"]
else:
humanChr = humanTxStart = ""
geneDesc = theGO["GeneDescription"]
if geneDesc == "---":
geneDesc = ""
this_row = [selectCheck.__str__(),
str(gIndex + 1),
geneSymbolNCBI,
"%0.6f" % theGO["TxStart"],
str(HtmlGenWrapper.create_link_tag(
geneLengthURL,
"{:.3f}".format(geneLength * 1000.0))),
avgExprVal,
mouseChr,
mouseTxStart,
humanChr,
humanTxStart,
geneDesc]
gene_table_body.append(this_row)
return gene_table_body
def getLiteratureCorrelation(cursor, geneId1=None, geneId2=None):
if not geneId1 or not geneId2:
return None
if geneId1 == geneId2:
return 1.0
geneId1 = str(geneId1)
geneId2 = str(geneId2)
lCorr = None
try:
query = 'SELECT Value FROM LCorrRamin3 WHERE GeneId1 = %s and GeneId2 = %s'
for x, y in [(geneId1, geneId2), (geneId2, geneId1)]:
cursor.execute(query, (x, y))
lCorr = cursor.fetchone()
if lCorr:
lCorr = lCorr[0]
break
except:
raise # lCorr = None
return lCorr
|