Source

orange-bioinformatics / orangecontrib / bio / widgets / OWSelectGenes.py

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
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
import sys
import os
import re
import unicodedata
import operator
from collections import defaultdict, namedtuple
from operator import itemgetter
from xml.sax.saxutils import escape

from contextlib import contextmanager

from PyQt4.QtGui import (
    QFrame, QHBoxLayout, QPlainTextEdit, QSyntaxHighlighter, QTextCharFormat,
    QTextCursor, QCompleter, QStandardItemModel, QSortFilterProxyModel,
    QStandardItem, QListView, QTreeView, QStyle, QStyledItemDelegate,
    QStyleOptionViewItemV4, QPalette, QColor, QApplication, QAction,
    QToolButton, QItemSelectionModel, QPlainTextDocumentLayout, QTextDocument,
    QRadioButton, QButtonGroup, QStyleOptionButton, QMenu, QDialog,
    QStackedWidget, QComboBox, QFileDialog
)

from PyQt4.QtCore import Qt, QEvent, QVariant, QThread
from PyQt4.QtCore import pyqtSignal as Signal

import Orange

from Orange.OrangeWidgets.OWWidget import (
    OWWidget, DomainContextHandler, Default
)

from Orange.OrangeWidgets.OWConcurrent import (
    ThreadExecutor, Task, methodinvoke
)

from Orange.OrangeWidgets.OWItemModels import VariableListModel
from Orange.OrangeWidgets import OWGUI
from Orange.orng.orngDataCaching import data_hints
from Orange.bio import obiGene as geneinfo
from Orange.bio import obiTaxonomy as taxonomy


NAME = "Select Genes"
DESCRIPTION = "Select a specified subset of the input genes."
ICON = "icons/SelectGenes.svg"

INPUTS = [("Data", Orange.data.Table, "setData", Default),
          ("Gene Subset", Orange.data.Table, "setGeneSubset")]

OUTPUTS = [("Selected Data", Orange.data.Table)]


def toString(variant):
    if isinstance(variant, QVariant):
        return unicode(variant.toString())
    else:
        return unicode(variant)


def toBool(variant):
    if isinstance(variant, QVariant):
        return bool(variant.toPyObject())
    else:
        return bool(variant)


def toPyObject(variant):
    if isinstance(variant, QVariant):
        return variant.toPyObject()
    else:
        return variant


class SaveSlot(QStandardItem):
    ModifiedRole = next(OWGUI.OrangeUserRole)

    def __init__(self, name, savedata=None, modified=False):
        super(SaveSlot, self).__init__(name)

        self.savedata = savedata
        self.modified = modified
        self.document = None

    @property
    def name(self):
        return unicode(self.text())

    @property
    def modified(self):
        return toBool(self.data(SaveSlot.ModifiedRole))

    @modified.setter
    def modified(self, state):
        self.setData(bool(state), SaveSlot.ModifiedRole)


class SavedSlotDelegate(QStyledItemDelegate):

    def paint(self, painter, option, index):
        option = QStyleOptionViewItemV4(option)
        self.initStyleOption(option, index)

        modified = toBool(index.data(SaveSlot.ModifiedRole))
        if modified:
            option.palette.setColor(QPalette.Text, QColor(Qt.red))
            option.palette.setColor(QPalette.Highlight, QColor(Qt.darkRed))
            option.text = "*" + option.text

        if option.widget:
            widget = option.widget
            style = widget.style()
        else:
            widget = None
            style = QApplication.style()

        style.drawControl(QStyle.CE_ItemViewItem, option, painter, widget)


def radio_indicator_width(button):
    button.ensurePolished()
    style = button.style()
    option = QStyleOptionButton()
    button.initStyleOption(option)

    w = style.pixelMetric(QStyle.PM_ExclusiveIndicatorWidth, option, button)
    return w


class OWSelectGenes(OWWidget):

    contextHandlers = {
        "": DomainContextHandler(
            "", ["geneIndex", "taxid"]
        ),
        "subset": DomainContextHandler(
            "subset", ["subsetGeneIndex"]
        )
    }

    settingsList = ["autoCommit", "preserveOrder", "savedSelections",
                    "selectedSelectionIndex", "selectedSource",
                    "completeOnSymbols"]

    SelectInput, SelectCustom = 0, 1
    CompletionRole = Qt.UserRole + 1

    def __init__(self, parent=None, signalManager=None, title=NAME):
        OWWidget.__init__(self, parent, signalManager, title,
                          wantMainArea=False)

        self.geneIndex = None
        self.taxid = None
        self.autoCommit = False
        self.preserveOrder = True
        self.savedSelections = [
            ("Example", ["MRE11A", "RAD51", "MLH1", "MSH2", "DMC1"])
        ]

        self.selectedSelectionIndex = -1
        self.selectedSource = OWSelectGenes.SelectCustom
        self.completeOnSymbols = True

        self.loadSettings()

        # Input variables that could contain names
        self.variables = VariableListModel()
        # All gene names and their symbols
        self.geneNames = []
        # A list of (name, info) where name is from the input
        # (geneVar column) and info is the NCBIGeneInfo object if available
        # or None
        self.genes = []
        # Output changed flag
        self._changedFlag = False
        # Current gene names
        self.selection = []
        # Input data
        self.data = None
        self.subsetData = None
        # Input variables that could contain gene names from "Gene Subset"
        self.subsetVariables = VariableListModel()
        # Selected subset variable index
        self.subsetGeneIndex = -1
        self.organisms = []
        self.taxidindex = {}
        self.geneinfo = (None, None)
        self._executor = ThreadExecutor()

        self._infotask = None

        box = OWGUI.widgetBox(self.controlArea, "Gene Attribute")
        box.setToolTip("Column with gene names")
        self.attrsCombo = OWGUI.comboBox(
            box, self, "geneIndex",
            callback=self._onGeneIndexChanged,
        )
        self.attrsCombo.setModel(self.variables)

        box = OWGUI.widgetBox(self.controlArea, "Gene Selection")

        button1 = QRadioButton("Select genes from 'Gene Subset' input")
        button2 = QRadioButton("Select specified genes")

        box.layout().addWidget(button1)

        # Subset gene variable selection
        self.subsetbox = OWGUI.widgetBox(box, None)
        offset = radio_indicator_width(button1)
        self.subsetbox.layout().setContentsMargins(offset, 0, 0, 0)
        self.subsetbox.setEnabled(
            self.selectedSource == OWSelectGenes.SelectInput)

        box1 = OWGUI.widgetBox(self.subsetbox, "Gene Attribute", flat=True)
        self.subsetVarCombo = OWGUI.comboBox(
            box1, self, "subsetGeneIndex",
            callback=self._onSubsetGeneIndexChanged
        )
        self.subsetVarCombo.setModel(self.subsetVariables)
        self.subsetVarCombo.setToolTip(
            "Column with gene names in the 'Gene Subset' input"
        )
        OWGUI.button(box1, self, "Copy genes to saved subsets",
                     callback=self.copyToSaved)

        OWGUI.button(box1, self, "Append genes to current saved selection",
                     callback=self.appendToSelected)

        box.layout().addWidget(button2)

        self.selectedSourceButtons = group = QButtonGroup(box)
        group.addButton(button1, OWSelectGenes.SelectInput)
        group.addButton(button2, OWSelectGenes.SelectCustom)
        group.buttonClicked[int].connect(self._selectionSourceChanged)

        if self.selectedSource == OWSelectGenes.SelectInput:
            button1.setChecked(True)
        else:
            button2.setChecked(True)

        self.entrybox = OWGUI.widgetBox(box, None)
        offset = radio_indicator_width(button2)
        self.entrybox.layout().setContentsMargins(offset, 0, 0, 0)

        self.entrybox.setEnabled(
            self.selectedSource == OWSelectGenes.SelectCustom)

        box = OWGUI.widgetBox(self.entrybox, "Select Genes", flat=True)
        box.setToolTip("Enter gene names to select")
        box.layout().setSpacing(1)

        self.entryField = ListTextEdit(box)
        self.entryField.setTabChangesFocus(True)
        self.entryField.setDocument(self._createDocument())
        self.entryField.itemsChanged.connect(self._onItemsChanged)

        box.layout().addWidget(self.entryField)

        completer = ListCompleter()
        completer.setCompletionMode(QCompleter.PopupCompletion)
        completer.setCompletionRole(
            self.CompletionRole if self.completeOnSymbols else Qt.DisplayRole
        )
        completer.setCaseSensitivity(Qt.CaseInsensitive)
        completer.setMaxVisibleItems(10)

        popup = QTreeView()
        popup.setSelectionMode(QTreeView.ExtendedSelection)
        popup.setEditTriggers(QTreeView.NoEditTriggers)
        popup.setRootIsDecorated(False)
        popup.setAlternatingRowColors(True)
        popup.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        popup.setMaximumWidth(500)
        popup.header().setStretchLastSection(False)
        popup.header().hide()

        completer.setPopup(popup)
        completer.setModel(SetFilterProxyModel(self))

        self.entryField.setCompleter(completer)

        toolbar = QFrame()
        toolbar.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed)
        layout = QHBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(1)

        addaction = QAction("+", self)
        addmenu = QMenu()

        action = addmenu.addAction("Import names from gene sets...")
        action.triggered.connect(self.importGeneSet)

        action = addmenu.addAction("Import names from text file...")
        action.triggered.connect(self.importFromFile)

        addaction.setMenu(addmenu)

        def button(action, popupMode=None):
            b = QToolButton()
            b.setDefaultAction(action)
            if popupMode is not None:
                b.setPopupMode(QToolButton.InstantPopup)
            return b

        b = button(addaction, popupMode=QToolButton.InstantPopup)
        layout.addWidget(b)

        moreaction = QAction("More", self)

        moremenu = QMenu()
        self.completeOnSymbolsAction = QAction(
            "Complete on gene symbol names", self,
            toolTip="Use symbol names for auto completion.",
            checkable=True,
            checked=self.completeOnSymbols
        )

        self.completeOnSymbolsAction.toggled[bool].connect(
            self._onToggleSymbolCompletion
        )

        moremenu.addAction(self.completeOnSymbolsAction)

        self.translateAction = QAction(
            "Translate all names to official symbol names", self,
            enabled=False
        )
        self.translateAction.triggered.connect(self._onTranslate)

        moremenu.addAction(self.translateAction)
        moreaction.setMenu(moremenu)

        b = button(moreaction, popupMode=QToolButton.InstantPopup)
        layout.addWidget(b)

        self.organismsCombo = ComboBox(focusPolicy=Qt.StrongFocus)
        self.organismsCombo.addItem("...")
        self.organismsCombo.model().item(0).setEnabled(False)
        self.organismsCombo.setMinimumWidth(200)
        self.organismsCombo.activated[int].connect(self._onOrganismActivated)

        layout.addSpacing(10)
        layout.addWidget(self.organismsCombo)
        self.pb = QProgressBar()
        self.pb.hide()
        self.pbstack = QStackedWidget()
        self.pbstack.addWidget(self.pb)

        layout.addSpacing(10)
        layout.addWidget(self.pbstack, 0, Qt.AlignRight | Qt.AlignVCenter)
        toolbar.setLayout(layout)

        box.layout().addWidget(toolbar)

        box = OWGUI.widgetBox(self.entrybox, "Saved Selections", flat=True)
        box.setToolTip("Save/Select/Update saved gene selections")
        box.layout().setSpacing(1)

        self.selectionsModel = QStandardItemModel()
        self.selectionsView = QListView()
        self.selectionsView.setAlternatingRowColors(True)
        self.selectionsView.setModel(self.selectionsModel)
        self.selectionsView.setItemDelegate(SavedSlotDelegate(self))
        self.selectionsView.selectionModel().selectionChanged.connect(
            self._onSelectedSaveSlotChanged
        )

        box.layout().addWidget(self.selectionsView)

        self.actionSave = QAction(
            "Save", self,
            toolTip="Save/Update the current selection")

        self.actionAdd = QAction(
            "+", self,
            toolTip="Create a new saved selection")

        self.actionRemove = QAction(
            u"\u2212", self,
            toolTip="Delete the current saved selection")

        toolbar = QFrame()
        layout = QHBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(1)

        b = button(self.actionAdd)
        layout.addWidget(b)

        b = button(self.actionRemove)
        layout.addWidget(b)

        b = button(self.actionSave)
        layout.addWidget(b)

        layout.addStretch(100)
        toolbar.setLayout(layout)

        box.layout().addWidget(toolbar)

        self.actionSave.triggered.connect(self.saveSelection)
        self.actionAdd.triggered.connect(self.addSelection)
        self.actionRemove.triggered.connect(self.removeSelection)

        box = OWGUI.widgetBox(self.controlArea, "Output")
        OWGUI.checkBox(box, self, "preserveOrder", "Preserve input order",
                       tooltip="Preserve the order of the input data "
                               "instances.",
                       callback=self.invalidateOutput)
        cb = OWGUI.checkBox(box, self, "autoCommit", "Auto commit")
        button = OWGUI.button(box, self, "Commit", callback=self.commit)

        OWGUI.setStopper(self, button, cb, "_changedFlag", self.commit)

        # Gene set import dialog (initialized when required)
        self._genesetDialog = None

        # restore saved selections model.
        for name, names in self.savedSelections:
            item = SaveSlot(name, names)
            self.selectionsModel.appendRow([item])

        if self.selectedSelectionIndex != -1:
            self.selectionsView.selectionModel().select(
                self.selectionsModel.index(self.selectedSelectionIndex, 0),
                QItemSelectionModel.Select
            )

        self._updateActions()

        def list_available_organisms():
            return [(taxid, taxonomy.name(taxid))
                    for taxid in taxonomy.common_taxids()]

        self._inittask = Task(function=list_available_organisms)

        self._inittask.finished.connect(self._onInit)
        self._executor.submit(self._inittask)
        self.pb.show()
        self.pb.setRange(0, 0)

    def _onInit(self):
        self.organisms = self._inittask.result()
        self.organismsCombo.clear()
        self.organismsCombo.addItems([name for _, name in self.organisms])

        self.taxidindex = \
            {taxid: i for i, (taxid, _) in enumerate(self.organisms)}

        self.organismsCombo.setCurrentIndex(
            self.taxidindex.get(self.taxid, -1))

        self.pb.hide()

        if self.taxid in self.taxidindex:
            self._updateGeneInfo()

    def _updateGeneInfo(self):
        self.geneinfo = (self.taxid, None)
        self.translateAction.setEnabled(False)

        if self._infotask:
            # Try to cancel existing pending task
            self._infotask.future().cancel()
            self._infotask.resultReady.disconnect(self._onGeneInfoReady)
            self._infotask.exceptionReady.disconnect(self._onGeneInfoError)

        def ncbi_gene_info(taxid=self.taxid):
            try:
                return (taxid, geneinfo.NCBIGeneInfo(taxid))
            except BaseException:
                sys.excepthook(*sys.exc_info())
                raise

        task = Task(function=ncbi_gene_info)
        task.resultReady.connect(self._onGeneInfoReady)
        task.exceptionReady.connect(self._onGeneInfoError)
        self._infotask = task
        self._executor.submit(task)
        self.pb.show()

    def _onOrganismActivated(self, index):
        try:
            taxid, _ = self.organisms[index]
        except IndexError:
            pass
        else:
            if taxid != self.taxid:
                self.taxid = taxid
                self._updateGeneInfo()

    def setData(self, data):
        """
        Set the input data.
        """
        self.closeContext("")
        self.warning(0)
        self.data = data
        if data is not None:
            attrs = gene_candidates(data)
            self.variables[:] = attrs
            self.attrsCombo.setCurrentIndex(0)
            if attrs:
                self.geneIndex = 0
            else:
                self.geneIndex = -1
                self.warning(0, "No suitable columns for gene names.")
        else:
            self.variables[:] = []
            self.geneIndex = -1

        self._changedFlag = True

        oldtaxid = self.taxid
        if data is not None:
            self.taxid = data_hints.get_hint(data, "taxid", None)
        else:
            self.taxid = None

        self.openContext("", data)

        if self.taxid is None:
            self.geneinfo = (None, None)

        if oldtaxid != self.taxid:
            self.organismsCombo.setCurrentIndex(
                self.taxidindex.get(self.taxid, -1))

            if self.taxid in self.taxidindex:
                self._updateGeneInfo()

        self._updateCompletionModel()

        self.commit()

    def setGeneSubset(self, data):
        """
        Set the gene subset input.
        """
        self.closeContext("subset")
        self.warning(1)
        self.subsetData = data
        if data is not None:
            variables = gene_candidates(data)
            self.subsetVariables[:] = variables
            self.subsetVarCombo.setCurrentIndex(0)
            if variables:
                self.subsetGeneIndex = 0
            else:
                self.subsetGeneIndex = -1
                self.warning(1, "No suitable column for subset gene names.")
        else:
            self.subsetVariables[:] = []
            self.subsetGeneIndex = -1

        self.openContext("subset", data)

        if self.selectedSource == OWSelectGenes.SelectInput:
            self.commit()

    @property
    def geneVar(self):
        """
        Current gene attribute or None if none available.
        """
        index = self.attrsCombo.currentIndex()
        if self.data is not None and index >= 0:
            return self.variables[index]
        else:
            return None

    @property
    def subsetGeneVar(self):
        """
        Current subset gene attribute or None if not available.
        """
        index = self.subsetVarCombo.currentIndex()
        if self.subsetData is not None and index >= 0:
            return self.subsetVariables[index]
        else:
            return None

    def invalidateOutput(self):
        if self.autoCommit:
            self.commit()
        else:
            self._changedFlag = True

    def selectedGenes(self):
        """
        Return the names of the current selected genes.
        """
        selection = []
        if self.selectedSource == OWSelectGenes.SelectInput:
            var = self.subsetGeneVar
            if var is not None:
                values = [inst[var] for inst in self.subsetData]
                selection = [str(val) for val in values
                             if not val.is_special()]
        else:
            selection = self.selection
        return selection

    def commit(self):
        """
        Send the selected data subset to the output.
        """
        selection = self.selectedGenes()

        if self.geneinfo[1] is not None:
            backmap = dict((info.symbol, name) for name, info in self.genes
                           if info is not None)
            names = set([name for name, _ in self.genes])
            selection = [backmap.get(name, name) for name in selection]
    
        if self.geneVar is not None:
            data = select_by_genes(self.data, self.geneVar,
                                   gene_list=selection,
                                   preserve_order=self.preserveOrder)
        else:
            data = None

        self.send("Selected Data", data)
        self._changedFlag = False

    def setSelectionSource(self, source):
        if self.selectedSource != source:
            self.selectedSource = source
            self.subsetbox.setEnabled(source == OWSelectGenes.SelectInput)
            self.entrybox.setEnabled(source == OWSelectGenes.SelectCustom)
            b = self.selectedSourceButtons.button(source)
            b.setChecked(True)

    def _selectionSourceChanged(self, source):
        if self.selectedSource != source:
            self.selectedSource = source
            self.subsetbox.setEnabled(source == OWSelectGenes.SelectInput)
            self.entrybox.setEnabled(source == OWSelectGenes.SelectCustom)
            self.invalidateOutput()

    def _updateCompletionModel(self):
        var = self.geneVar
        if var is not None:
            names = [str(inst[var]) for inst in self.data
                     if not inst[var].is_special()]
        else:
            names = []

        infodict = {}

        if self.geneinfo[1] is not None:
            info = [(name, self.geneinfo[1].get_info(name, None))
                    for name in names]
            info = filter(itemgetter(1), info)
            infodict = dict(info)

        names = sorted(set(names))
        genes = zip(names, map(infodict.get, names))

        symbols = [info.symbol for _, info in genes if info is not None]

        model = QStandardItemModel()

        def make_row(name, info):
            if info is not None:
                col1 = QStandardItem(name)
                col1.setData(info.symbol, OWSelectGenes.CompletionRole)
                return [col1,
                        QStandardItem(info.symbol),
                        QStandardItem(info.description)]
            else:
                col1 = QStandardItem(name)
                col1.setData(name, OWSelectGenes.CompletionRole)
                return [col1]

        for name, info in genes:
            model.appendRow(make_row(name, info))

        self.geneNames = sorted(set(names) | set(symbols))
        self.genes = genes
        self.entryField.completer().model().setSourceModel(model)
        self.entryField.document().highlighter.setNames(names + symbols)

        self._updatePopupSections()

    def _onGeneIndexChanged(self):
        self._updateCompletionModel()
        self.invalidateOutput()

    def _onSubsetGeneIndexChanged(self):
        if self.selectedSource == OWSelectGenes.SelectInput:
            self.invalidateOutput()

    def _onItemsChanged(self, names):
        selection = set(names).intersection(self.geneNames)
        curr_selection = set(self.selection).intersection(self.geneNames)

        self.selection = names

        if selection != curr_selection:
            self.invalidateOutput()
            to_complete = sorted(set(self.geneNames) - set(names))
            self.entryField.completer().model().setFilterFixedSet(to_complete)

        item = self._selectedSaveSlot()
        if item:
            item.modified = item.savedata != names

    def _onToggleSymbolCompletion(self, state):
        completer = self.entryField.completer()
        completer.setCompletionRole(
            self.CompletionRole if state else Qt.DisplayRole
        )
        self.completeOnSymbols = state
        self._updatePopupSections()

    def _onTranslate(self):
        if self.geneinfo[1] is not None:
            items = self.entryField.items()
            entries = map(self.geneinfo[1].get_info, items)
            items = [info.symbol if info is not None else item
                     for item, info in zip(items, entries)]
            self.entryField.selectAll()
            self.entryField.insertPlainText(" ".join(items))

    def _onGeneInfoReady(self, geneinfo):
        assert QThread.currentThread() is self.thread()
        # Check if the gene info is for the correct (current requested)
        # organism (we might receive a late response from a previous
        # request)
        if self.geneinfo[0] == geneinfo[0]:
            self.geneinfo = geneinfo
            self.translateAction.setEnabled(True)
            self.pb.hide()
            self._updateCompletionModel()

    def _onGeneInfoError(self, exc):
        self.error(0, str(exc))

    def _updatePopupSections(self):
        completer = self.entryField.completer()
        popup = completer.popup()
        assert isinstance(popup, QTreeView)
        header = popup.header()
        # The column in which the symbols should be
        symbol_col = 0 if self.completeOnSymbols else 1
        if symbol_col != header.sectionPosition(1):
            header.moveSection(0, 1)

    def _selectedSaveSlot(self):
        """
        Return the current selected saved selection slot.
        """
        indexes = self.selectionsView.selectedIndexes()
        if indexes:
            return self.selectionsModel.item(indexes[0].row())
        else:
            return None

    def saveSelection(self):
        """
        Save (update) the items in the current selected selection.
        """
        item = self._selectedSaveSlot()
        if item:
            item.savedata = self.entryField.items()
            item.modified = False

    def copyToSaved(self):
        """
        Copy the current 'Gene Subset' names to saved selections.
        """
        if self.subsetGeneVar and \
                self.selectedSource == OWSelectGenes.SelectInput:
            names = self.selectedGenes()
            item = SaveSlot("New selection")
            item.savedata = names
            self.selectionsModel.appendRow([item])
            self.setSelectionSource(OWSelectGenes.SelectCustom)
            self.selectionsView.setCurrentIndex(item.index())
            self.selectionsView.edit(item.index())

    def appendToSelected(self):
        """
        Append the current 'Gene Subset' names to 'Select Genes' entry field.
        """
        if self.subsetGeneVar and \
                self.selectedSource == OWSelectGenes.SelectInput:
            names = self.selectedGenes()
            text = " ".join(names)
            self.entryField.appendPlainText(text)
            self.setSelectionSource(OWSelectGenes.SelectCustom)
            self.entryField.setFocus()
            self.entryField.moveCursor(QTextCursor.End)

    def addSelection(self, name=None):
        """
        Add a new saved selection entry initialized by the current items.

        The new slot will be selected.

        """
        item = SaveSlot(name or "New selection")
        item.savedata = self.entryField.items()
        self.selectionsModel.appendRow([item])
        self.selectionsView.setCurrentIndex(item.index())

        if not name:
            self.selectionsView.edit(item.index())

    def removeSelection(self):
        """
        Remove the current selected save slot.
        """
        item = self._selectedSaveSlot()
        if item:
            self.selectionsModel.removeRow(item.row())

    def importGeneSet(self):
        if self._genesetDialog is None:
            self._genesetDialog = GeneSetDialog(
                self, windowTitle="Import Gene Set Names")

        dialog = self._genesetDialog

        if self.taxid is not None:
            dialog.setCurrentOrganism(self.taxid)

        result = dialog.exec_()
        if result == QDialog.Accepted:
            gsets = dialog.selectedGeneSets()
            genes = reduce(operator.ior, (gs.genes for gs in gsets), set())
            text = " ".join(genes)
            self.entryField.appendPlainText(text)
            self.entryField.setFocus()
            self.entryField.moveCursor(QTextCursor.End)

    def importFromFile(self):
        filename = QFileDialog.getOpenFileName(
            self, "Open File", os.path.expanduser("~/"))

        if filename:
            filename = unicode(filename)
            with open(filename, "rU") as f:
                text = f.read()
            self.entryField.appendPlainText(text)
            self.entryField.setFocus()
            self.entryField.moveCursor(QTextCursor.End)

    def _onSelectedSaveSlotChanged(self):
        item = self._selectedSaveSlot()
        if item:
            if not item.document:
                item.document = self._createDocument()
                if item.savedata:
                    item.document.setPlainText(" ".join(item.savedata))

            item.document.highlighter.setNames(self.geneNames)

            self.entryField.setDocument(item.document)

        self._updateActions()

    def _createDocument(self):
        """
        Create and new QTextDocument instance for editing gene names.
        """
        doc = QTextDocument(self)
        doc.setDocumentLayout(QPlainTextDocumentLayout(doc))
        doc.highlighter = NameHighlight(doc)
        return doc

    def _updateActions(self):
        """
        Update the Save/remove action enabled state.
        """
        selected = bool(self._selectedSaveSlot())
        self.actionRemove.setEnabled(selected)
        self.actionSave.setEnabled(selected)

    def getSettings(self, *args, **kwargs):
        # copy the saved selections model back to widget settings.
        selections = []
        for i in range(self.selectionsModel.rowCount()):
            item = self.selectionsModel.item(i)
            selections.append((item.name, item.savedata))
        self.savedSelections = selections

        item = self._selectedSaveSlot()
        if item is None:
            self.selectedSelectionIndex = -1
        else:
            self.selectedSelectionIndex = item.row()

        return OWWidget.getSettings(self, *args, **kwargs)

    def sendReport(self):
        report = []
        if self.data is not None:
            report.append("%i instances on input." % len(self.data))
        else:
            report.append("No data on input.")

        if self.geneVar is not None:
            report.append("Gene names taken from %r attribute." %
                          escape(self.geneVar.name))

        self.reportSection("Input")
        self.startReportList()
        for item in report:
            self.addToReportList(item)
        self.finishReportList()
        report = []
        selection = self.selectedGenes()
        if self.selectedSource == OWSelectGenes.SelectInput:
            self.reportRaw(
                "<p>Gene Selection (from 'Gene Subset' input): %s</p>" %
                escape(" ".join(selection))
            )
        else:
            self.reportRaw(
                "<p>Gene Selection: %s</p>" %
                escape(" ".join(selection))
            )
        self.reportSettings(
            "Settings",
            [("Preserve order", self.preserveOrder)]
        )

    def onDeleteWidget(self):
        self._inittask.future().cancel()

        if self._infotask:
            self._infotask.future().cancel()

        self._executor.shutdown(wait=True)
        OWWidget.onDeleteWidget(self)


def is_string(feature):
    return isinstance(feature, Orange.feature.String)


def domain_variables(domain):
    """
    Return all feature descriptors from the domain.
    """
    vars = (domain.features +
            domain.class_vars +
            domain.getmetas().values())
    return vars


def gene_candidates(data):
    """
    Return features that could contain gene names.
    """
    vars = domain_variables(data.domain)
    vars = filter(is_string, vars)
    return vars


def select_by_genes(data, gene_feature, gene_list, preserve_order=True):
    if preserve_order:
        selection = set(gene_list)
        sel = [inst for inst in data
               if str(inst[gene_feature]) in selection]
    else:
        by_genes = defaultdict(list)
        for inst in data:
            by_genes[str(inst[gene_feature])].append(inst)

        sel = []
        for name in gene_list:
            sel.extend(by_genes.get(name, []))

    if sel:
        data = Orange.data.Table(data.domain, sel)
    else:
        data = Orange.data.Table(data.domain)

    return data


_CompletionState = namedtuple(
    "_CompletionState",
    ["start",  # completion prefix start position
     "pos",  # cursor position
     "anchor"]  # anchor position (inline completion end)
)


class ListTextEdit(QPlainTextEdit):
    """
    A text editor specialized for editing a list of items.
    """
    #: Emitted when the list items change.
    itemsChanged = Signal(list)

    def __init__(self, parent=None, **kwargs):
        QPlainTextEdit.__init__(self, parent, **kwargs)

        self._items = None
        self._completer = None
        self._completionState = _CompletionState(-1, -1, -1)

        self.cursorPositionChanged.connect(self._cursorPositionChanged)
        self.textChanged.connect(self._textChanged)

    def setCompleter(self, completer):
        """
        Set a completer for list items.
        """
        if self._completer is not None:
            self._completer.setWidget(None)
            self._completer.activated.disconnect(self._insertCompletion)

        self._completer = completer

        if self._completer:
            self._completer.setWidget(self)
            self._completer.activated.connect(self._insertCompletion)

    def completer(self):
        """
        Return the completer.
        """
        return self._completer

    def setItems(self, items):
        text = " ".join(items)
        self.setPlainText(text)

    def items(self):
        if self._items is None:
            self._items = self._getItems()
        return self._items

    def keyPressEvent(self, event):
        # TODO: in Qt 4.8 QPlainTextEdit uses inputMethodEvent for
        # non-ascii input

        if self._completer.popup().isVisible():
            if event.key() in [Qt.Key_Enter, Qt.Key_Return, Qt.Key_Escape,
                               Qt.Key_Tab, Qt.Key_Backtab]:
                # These need to propagate to the completer.
                event.ignore()
                return

        QPlainTextEdit.keyPressEvent(self, event)

        if not len(event.text()) or not is_printable(unicode(event.text())[0]):
            return

        text = unicode(self.toPlainText())
        cursor = self.textCursor()
        pos = cursor.position()

        if pos == len(text) or not text[pos].strip():
            # cursor is at end of text or whitespace
            # find the beginning of the current word
            whitespace = " \t\n\r\f\v"
            start = max([text.rfind(c, 0, pos) for c in whitespace]) + 1

            prefix = text[start:pos]

            if prefix:
                if self._completer.completionPrefix() != prefix:
                    self._completer.setCompletionPrefix(text[start:pos])

                rect = self.cursorRect()
                popup = self._completer.popup()
                if popup.isVisible():
                    rect.setWidth(popup.width())
                else:
                    view_adjust_size_to_contents(popup)
                    rect.setWidth(popup.width())

                # Popup the completer list
                self._completer.complete(rect)

                # Inline completion of a common prefix
                inline = self._commonCompletionPrefix()
                inline = inline[len(prefix):]

                self._completionState = \
                    _CompletionState(start, pos, pos + len(inline))

                cursor.insertText(inline)
                cursor.setPosition(pos, QTextCursor.KeepAnchor)
                self.setTextCursor(cursor)

            elif self._completer.popup().isVisible():
                self._stopCompletion()

    def _cursorPositionChanged(self):
        cursor = self.textCursor()
        pos = cursor.position()
        start, _, _ = self._completionState

        if start == -1:
            # completion not in progress
            return

        if pos <= start:
            # cursor moved before the start of the prefix
            self._stopCompletion()
            return

        text = unicode(self.toPlainText())
        # Find the end of the word started by completion prefix
        word_end = len(text)
        for i in range(start, len(text)):
            if text[i] in " \t\n\r\f\v":
                word_end = i
                break

        if pos > word_end:
            # cursor moved past the word boundary
            self._stopCompletion()

        # TODO: Update the prefix when moving the cursor
        # inside the word

    def _insertCompletion(self, item):
        if isinstance(item, list):
            completion = " ".join(item)
        else:
            completion = unicode(item)

        start, _, end = self._completionState

        self._stopCompletion()

        cursor = self.textCursor()
        # Replace the prefix+inline with the full completion
        # (correcting for the case-insensitive search).
        cursor.setPosition(min(end, self.document().characterCount()))
        cursor.setPosition(start, QTextCursor.KeepAnchor)

        cursor.insertText(completion + " ")

    def _commonCompletionPrefix(self):
        """
        Return the common prefix of items in the current completion model.
        """
        model = self._completer.completionModel()
        column = self._completer.completionColumn()
        role = self._completer.completionRole()
        items = [toString(model.index(i, column).data(role))
                 for i in range(model.rowCount())]

        if not items:
            return ""

        first = min(items)
        last = max(items)
        for i, c in enumerate(first):
            if c != last[i]:
                return first[:i]

        return first

    def _stopCompletion(self):
        self._completionState = _CompletionState(-1, -1, -1)
        if self._completer.popup().isVisible():
            self._completer.popup().hide()

    def _textChanged(self):
        items = self._getItems()
        if self._items != items:
            self._items = items
            self.itemsChanged.emit(items)

    def _getItems(self):
        """
        Return the current items (a list of strings).

        .. note:: The inline completion text is not included.

        """
        text = unicode(self.toPlainText())
        if self._completionState[0] != -1:
            # Remove the inline completion text
            _, pos, end = self._completionState
            text = text[:pos] + text[end:]
        return [item for item in text.split() if item.strip()]


def view_adjust_column_sizes(view, maxWidth=None):
    """
    Adjust view's column sizes to to contents.
    """
    if maxWidth is None:
        maxWidth = sys.maxint

    for col in range(view.model().columnCount()):
        width = min(view.sizeHintForColumn(col), maxWidth)
        view.setColumnWidth(col, width)


def view_adjust_size_to_contents(view):
    """
    Adjust the view to a reasonable size based in it's contents.
    """
    view_adjust_column_sizes(view)
    w = sum([view.columnWidth(col)
             for col in range(view.model().columnCount())])
    w += view.verticalScrollBar().sizeHint().width()

    h = view.sizeHintForRow(0) * 7
    h += view.horizontalScrollBar().sizeHint().height()
    view.resize(w, h)


class NameHighlight(QSyntaxHighlighter):
    def __init__(self, parent=None, **kwargs):
        super(NameHighlight, self).__init__(parent, **kwargs)

        self._names = set()

        self._format = QTextCharFormat()
        self._format.setForeground(Qt.blue)

        self._unrecognized_format = QTextCharFormat()
#         self._unrecognized_format.setFontStrikeOut(True)

    def setNames(self, names):
        self._names = set(names)
        self.rehighlight()

    def names(self):
        return set(self._names)

    def highlightBlock(self, text):
        text = unicode(text)
        pattern = re.compile(r"\S+")
        for match in pattern.finditer(text):
            name = text[match.start(): match.end()]
            match_len = match.end() - match.start()

            if not name.strip():
                continue

            if name in self._names:
                format = self._format
            else:
                format = self._unrecognized_format

            self.setFormat(match.start(), match_len, format)


@contextmanager
def signals_blocked(obj):
    blocked = obj.signalsBlocked()
    obj.blockSignals(True)
    try:
        yield
    finally:
        obj.blockSignals(blocked)


class ListCompleter(QCompleter):
    """
    A completer supporting selection of multiple list items.
    """
    activated = Signal(list)

    def __init__(self, *args, **kwargs):
        QCompleter.__init__(self, *args, **kwargs)

        popup = QListView()
        popup.setEditTriggers(QListView.NoEditTriggers)
        popup.setSelectionMode(QListView.ExtendedSelection)

        self.setPopup(popup)

    def setPopup(self, popup):
        QCompleter.setPopup(self, popup)

        popup.viewport().installEventFilter(self)
        popup.doubleClicked.connect(self._complete)

    def eventFilter(self, receiver, event):
        if event.type() == QEvent.KeyPress and receiver is self.popup():
            if event.key() in [Qt.Key_Enter, Qt.Key_Return, Qt.Key_Tab]:
                self._complete()
                return True

        elif event.type() == QEvent.MouseButtonRelease and \
                receiver is self.popup().viewport():
            # Process the event without emitting 'clicked', ... signal to
            # override the default QCompleter behavior
            with signals_blocked(self.popup()):
                QApplication.sendEvent(self.popup(), event)
                return True

        return QCompleter.eventFilter(self, receiver, event)

    def _complete(self):
        column = self.completionColumn()
        role = self.completionRole()
        indexes = self.popup().selectionModel().selectedRows(column)

        items = [toString(index.data(role)) for index in indexes]

        if self.popup().isVisible():
            self.popup().hide()

        if items:
            self.activated.emit(items)


class SetFilterProxyModel(QSortFilterProxyModel):
    def __init__(self, *args, **kwargs):
        super(SetFilterProxyModel, self).__init__(*args, **kwargs)
        self._filterFixedSet = None

    def setFilterFixedSet(self, items):
        if items is None:
            self._filterFixedSet = None
        else:
            self._filterFixedSet = set(items)
        self.invalidateFilter()

    def filterAcceptsRow(self, row, parent):
        if self._filterFixedSet is None:
            return True

        model = self.sourceModel()
        col = self.filterKeyColumn()
        var = model.data(model.index(row, col, parent),
                         self.filterRole())
        var = toString(var)
        return var in self._filterFixedSet


class ComboBox(QComboBox):
    """
    A combo box ignoring mouse wheel events.
    """
    def wheelEvent(self, event):
        event.ignore()
        return

# All control character categories.
_control = set(["Cc", "Cf", "Cs", "Co", "Cn"])


def is_printable(unichar):
    """
    Return True if the unicode character `unichar` is a printable character.
    """
    return unicodedata.category(unichar) not in _control


import sys
import itertools

from PyQt4.QtGui import (
    QVBoxLayout, QLineEdit, QDialogButtonBox,
    QProgressBar, QSizePolicy
)

from PyQt4.QtCore import QSize
from Orange.bio import obiGeneSets as genesets
from Orange.utils import serverfiles


class GeneSetView(QFrame):
    selectedOrganismChanged = Signal(str)
    selectionChanged = Signal()
    geneSetsLoaded = Signal()

    def __init__(self, *args, **kwargs):
        super(GeneSetView, self).__init__(*args, **kwargs)

        self._taxid = None

        layout = QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)

        self._stack = QStackedWidget()
        self._stack.setContentsMargins(0, 0, 0, 0)
        self._stack.setSizePolicy(QSizePolicy.MinimumExpanding,
                                  QSizePolicy.Fixed)
        self.orgcombo = ComboBox(minimumWidth=150, focusPolicy=Qt.StrongFocus)
        self.orgcombo.activated[int].connect(self._on_organismSelected)
        self._stack.addWidget(self.orgcombo)

        self.progressbar = QProgressBar()
        self._stack.addWidget(self.progressbar)

        layout.addWidget(self._stack)

        self.searchline = QLineEdit()
        self.searchline.setPlaceholderText("Filter...")
        completer = QCompleter()
        self.searchline.setCompleter(completer)
        layout.addWidget(self.searchline)

        self.gsview = QTreeView()
        self.gsview.setAlternatingRowColors(True)
        self.gsview.setRootIsDecorated(False)
        self.gsview.setSelectionMode(QTreeView.ExtendedSelection)
        self.proxymodel = QSortFilterProxyModel(
            filterKeyColumn=1, sortCaseSensitivity=Qt.CaseInsensitive
        )

        self.gsview.setModel(self.proxymodel)
        self.gsview.selectionModel().selectionChanged.connect(
            self._on_selectionChanged)

        self.searchline.textChanged.connect(
            self.proxymodel.setFilterFixedString)

        layout.addWidget(self.gsview)
        self.setLayout(layout)

        self._executor = ThreadExecutor(self)
        self.initialize()

    def initialize(self):
        self.gs_hierarchy = gs = genesets.list_all()
        taxids = set(taxid for _, taxid, _ in gs)
        self.organisms = [(taxid, taxonomy.name(taxid)) for taxid in taxids]
        for taxid, name in self.organisms:
            self.orgcombo.addItem(name, taxid)

        self.orgcombo.setCurrentIndex(-1)

    def sizeHint(self):
        return QSize(500, 550)

    def setCurrentOrganism(self, taxid):
        taxids = [tid for tid, _ in self.organisms]
        if taxid is not None and taxid not in taxids:
            taxid = None

        if taxid != self._taxid:
            self._taxid = taxid
            if taxid is None:
                self.orgcombo.setCurrentIndex(-1)
            else:
                index = taxids.index(taxid)
                self.orgcombo.setCurrentIndex(index)
            self._updateGeneSetsModel()
            self.selectedOrganismChanged.emit(taxid)

    def currentOrganism(self):
        return self._taxid

    def selectedGeneSets(self):
        selmod = self.gsview.selectionModel()
        model = self.proxymodel.sourceModel()
        rows = [self.proxymodel.mapToSource(row)
                for row in selmod.selectedRows(1)]
        gsets = [model.data(row, Qt.UserRole) for row in rows]
        return map(toPyObject, gsets)

    def _updateGeneSetsModel(self):
        taxid = self._taxid
        if taxid is None:
            self.proxymodel.setSourceModel(None)
        else:
            currentsets = [(hier, tid)
                           for hier, tid, _ in self.gs_hierarchy
                           if tid == taxid]

            gsmissing = [(hier, tid, local)
                         for hier, tid, local in self.gs_hierarchy
                         if tid == taxid and not local]

            self._stack.setCurrentWidget(self.progressbar)

            if gsmissing:
                self.progressbar.setRange(0, 100)
                progress_info = methodinvoke(
                    self.progressbar, "setValue", (int,))
            else:
                self.progressbar.setRange(0, 0)
                progress_info = None

            def load():
                gs_ensure_downloaded(
                    gsmissing,
                    progress_info=progress_info)

                return [((hier, tid), genesets.load(hier, tid))
                        for hier, tid in currentsets]

            self._task = Task(function=load)
            self._task.finished.connect(self._on_loadFinished)
            self._executor.submit(self._task)

    def _on_loadFinished(self):
        assert QThread.currentThread() is self.thread()
        self._stack.setCurrentWidget(self.orgcombo)

        try:
            sets = self._task.result()
        except Exception:
            # Should do something better here.
            sys.excepthook(*sys.exc_info())
            sets = []

        model = sets_to_model(sets)
        self.proxymodel.setSourceModel(model)
        self.gsview.resizeColumnToContents(0)
        self.geneSetsLoaded.emit()

    def _on_organismSelected(self, index):
        if index != -1:
            item = self.orgcombo.model().item(index)
            taxid = toString(item.data(Qt.UserRole))
            self.setCurrentOrganism(taxid)

    def _on_selectionChanged(self, *args):
        self.selectionChanged.emit()


def sets_to_model(gsets):
    model = QStandardItemModel()
    model.setHorizontalHeaderLabels(["Category", "Name"])

    for (hier, tid), sets in gsets:
        for gset in sets:
            ngenes = len(gset.genes)
            names = [escape(name) for name in list(gset.genes)[:30]]
            names = ", ".join(names)
            tooltip = "<p>{0}</p>{1}".format(escape(gset.name), names)
            if ngenes > 30:
                tooltip += ", ... ({0} names not shown)".format(ngenes - 30)

            category = QStandardItem(" ".join(hier))
            category.setData((hier, tid), Qt.UserRole)
            category.setEditable(False)
            category.setToolTip(tooltip)
            name = QStandardItem(gset.name)
            name.setData(gset, Qt.UserRole)
            name.setEditable(False)
            name.setToolTip(tooltip)
            model.appendRow([category, name])

    return model


def gs_ensure_downloaded(gslist, progress_info=None):
    hierlist = [(hier, taxid) for hier, taxid, local in gslist
                if not local]

    files = [(genesets.sfdomain, genesets.filename(hier, taxid))
             for hier, taxid in hierlist]

    download_list(files, progress_info)


def download_list(files_list, progress_callback=None):
    nfiles = len(files_list)
    count = 100 * nfiles
    counter = itertools.count()

    def advance():
        progress_callback(100.0 * next(counter) / count)

    for domain, filename in files_list:
        serverfiles.download(domain, filename,
                             callback=advance if progress_callback else None)


class GeneSetDialog(QDialog):
    selectionChanged = Signal()

    def __init__(self, *args, **kwargs):
        super(GeneSetDialog, self).__init__(*args, **kwargs)
        layout = QVBoxLayout()
        layout.setContentsMargins(4, 4, 4, 4)
        self.gsview = GeneSetView()
        self.gsview.selectionChanged.connect(self.selectionChanged)

        layout.addWidget(self.gsview)

        buttonbox = QDialogButtonBox(
            QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
            Qt.Horizontal
        )
        buttonbox.accepted.connect(self.accept)
        buttonbox.rejected.connect(self.reject)

        layout.addWidget(buttonbox)

        self.setLayout(layout)

    def selectedGeneSets(self):
        return self.gsview.selectedGeneSets()

    def setCurrentOrganism(self, taxid):
        self.gsview.setCurrentOrganism(taxid)

    def currentOrganism(self):
        return self.gsview.currentOrganism()


def test1():
    app = QApplication([])
    dlg = GeneSetDialog()
    dlg.exec_()
    del dlg
    app.processEvents()


def test():
    app = QApplication([])
    w = OWSelectGenes()
    data = Orange.data.Table("brown-selected")
    w.setData(data)
    w.setGeneSubset(Orange.data.Table(data[:10]))
    w.show()
    app.exec_()
    w.saveSettings()
    w.deleteLater()
    del w
    app.processEvents()

if __name__ == "__main__":
    test()