summaryrefslogtreecommitdiff
path: root/cad/src/protein/commands/CompareProteins/CompareProteins_PropertyManager.py
blob: b9f9f6bf6fadc26ffaef19117fea1999fdd260e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# Copyright 2008 Nanorex, Inc.  See LICENSE file for details.
"""
CompareProteins_PropertyManager.py

The CompareProteins_PropertyManager class provides a Property Manager
for the B{Compare Proteins} command on the flyout toolbar in the
Build > Protein mode.

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

TODO:
- Switch display styles of selected proteins to diPROTEIN (instead of the GDS).
- PM color comboboxes to choose the "difference colors".

BUGS:
- Previously compared proteins still show differences after comparing a new set of proteins.
- "Hide differences" button should be disabled (or hidden) when there are no differences.

"""

import foundation.env as env

from command_support.Command_PropertyManager import Command_PropertyManager
from utilities.prefs_constants import workingDirectory_prefs_key
from utilities.constants import yellow

from PyQt4.Qt import SIGNAL
from PM.PM_PushButton   import PM_PushButton
from PM.PM_GroupBox import PM_GroupBox
from PM.PM_LineEdit import PM_LineEdit
from PM.PM_DoubleSpinBox import PM_DoubleSpinBox
from PM.PM_Constants import PM_DONE_BUTTON
from PM.PM_Constants import PM_WHATS_THIS_BUTTON
from protein.model.Protein import getAllProteinChunksInPart
from utilities.constants import diPROTEIN
from utilities.Log import redmsg

_superclass = Command_PropertyManager
class CompareProteins_PropertyManager(Command_PropertyManager):
    """
    The CompareProteins_PropertyManager class provides a Property Manager
    for the B{Compare Proteins} command on the Build Protein flyout toolbar.

    @ivar title: The title that appears in the property manager header.
    @type title: str

    @ivar pmName: The name of this property manager. This is used to set
                  the name of the PM_Dialog object via setObjectName().
    @type name: str

    @ivar iconPath: The relative path to the PNG file that contains a
                    22 x 22 icon image that appears in the PM header.
    @type iconPath: str

    @ivar proteinChunk1: The first currently selected protein to be compared.
    @type proteinChunk1: protein chunk

    @ivar proteinChunk2: The second currently selected protein to be compared.
    @type proteinChunk2: protein chunk
    """

    title          =  "Compare Proteins"
    pmName         =  title
    iconPath       =  "ui/actions/Command Toolbar/BuildProtein/Compare.png"
    proteinChunk1  =  None
    proteinChunk2  =  None

    def __init__( self, command ):
        """
        Constructor for the property manager.
        """
        self.threshold = 10.0

        _superclass.__init__(self, command)

        self.showTopRowButtons( PM_DONE_BUTTON | \
                                PM_WHATS_THIS_BUTTON)
        return

    def connect_or_disconnect_signals(self, isConnect = True):

        if isConnect:
            change_connect = self.win.connect
        else:
            change_connect = self.win.disconnect

        change_connect(self.comparePushButton,
                       SIGNAL("clicked()"),
                       self._compareProteins)

        change_connect(self.thresholdDoubleSpinBox,
                       SIGNAL("valueChanged(double)"),
                       self._thresholdChanged)

        change_connect(self.hidePushButton,
                       SIGNAL("clicked()"),
                       self._hideDifferences)
        return

    def close(self):
        """
        Closes the Property Manager. Overrides EditCommand_PM.close()
        """

        env.history.statusbar_msg("")
        self._resetAminoAcids()
        _superclass.close(self)

        # Restore the original global display style.
        self.o.setGlobalDisplayStyle(self.originalDisplayStyle)
        return

    def show(self):
        """
        Show the PM. Extends superclass method.

        @note: _update_UI_do_updates() gets called immediately after this and
               updates PM widgets with their correct values/settings.
        """
        _superclass.show(self)
        env.history.statusbar_msg("")

        # Force the Global Display Style to "Protein" since this is the only way
        # to see comparisons. The global display style will be restored when leaving
        # this command (via self.close()).
        self.originalDisplayStyle = self.o.displayMode
        self.o.setGlobalDisplayStyle(diPROTEIN)
        return

    def _addGroupBoxes( self ):
        """
        Add the Property Manager group boxes.
        """
        self._pmGroupBox1 = PM_GroupBox( self,
                                         title = "Compare")
        self._loadGroupBox1( self._pmGroupBox1 )
        return

    def _addWhatsThisText( self ):
        """
        What's This text for widgets in this Property Manager.
        """
        pass

    def _addToolTipText(self):
        """
        Tool Tip text for widgets in this Property Manager.
        """
        pass

    def _loadGroupBox1(self, pmGroupBox):
        """
        Load widgets in group box.
        """

        self.structure1LineEdit = \
            PM_LineEdit( pmGroupBox,
                         label         =  "First structure:",
                         setAsDefault  =  False)
        self.structure1LineEdit.setEnabled(False)

        self.structure2LineEdit = \
            PM_LineEdit( pmGroupBox,
                         label         =  "Second structure:",
                         setAsDefault  =  False)
        self.structure2LineEdit.setEnabled(False)

        self.thresholdDoubleSpinBox  =  \
            PM_DoubleSpinBox( pmGroupBox,
                              label         =  "Threshold:",
                              value         =  self.threshold,
                              setAsDefault  =  True,
                              minimum       =  0.0,
                              maximum       =  360.0,
                              decimals      =  1,
                              singleStep    =  30.0,
                              suffix        = " deg",
                              spanWidth = False)

        self.comparePushButton  = \
            PM_PushButton( pmGroupBox,
                         text         =  "Compare",
                         setAsDefault  =  True)

        self.hidePushButton  = \
            PM_PushButton( pmGroupBox,
                         text         =  "Hide differences",
                         setAsDefault  =  True)
        return

    def _compareProteins(self):
        """
        Slot for Compare button.

        Compares two selected proteins of the same length.
        Amino acids that differ greater than the "threshold"
        value are displayed in two colors (red for the first protein
        and yellow for the second protein) and are only visible when
        the two proteins are displayed in the
        reduced display style.
        """
        from utilities.constants import red, orange, green, cyan

        if not self.proteinChunk1 or \
           not self.proteinChunk2:
            return

        protein_1 = self.proteinChunk1.protein
        protein_2 = self.proteinChunk2.protein

        if protein_1 and \
           protein_2:
            aa_list_1 = protein_1.get_amino_acids()
            aa_list_2 = protein_2.get_amino_acids()
            protein_1.collapse_all_rotamers()
            protein_2.collapse_all_rotamers()
            if len(aa_list_1) == len(aa_list_2):
                for aa1, aa2 in zip (aa_list_1, aa_list_2):
                    aa1.color = None
                    aa2.color = None
                    #aa1.collapse()
                    #aa2.collapse()
                    if aa1.get_one_letter_code() != aa2.get_one_letter_code():
                        aa1.set_color(red)
                        aa1.expand()
                        aa2.set_color(yellow)
                        aa2.expand()
                    else:
                        max = 0.0
                        for chi in range(0, 3):
                            angle1 = aa1.get_chi_angle(chi)
                            angle2 = aa2.get_chi_angle(chi)
                            if angle1 and \
                               angle2:
                                if angle1 < 0.0:
                                    angle1 += 360.0
                                if angle2 < 0.0:
                                    angle2 += 360.0
                                diff = abs(angle1 - angle2)
                                if diff > max:
                                    max = diff
                        if max >= self.threshold:
                            # This be a parameter.
                            aa1.set_color(green)
                            aa1.expand()
                            aa2.set_color(cyan)
                            aa2.expand()

                self.win.glpane.gl_update()
            else:
                msg = "The lengths of compared proteins are not equal."
                self.updateMessage(msg)
                env.history.redmsg(msg)
        return

    def _hideDifferences(self):
        """
        Slot for the "Hide differences" button.

        Hides amino acids that differ greater than the "threshold" value.

        @warning: Untested. Code looks suspicious.
        """
        if not self.proteinChunk1 or \
           not self.proteinChunk2:
            return

        protein_1 = self.proteinChunk1.protein
        protein_2 = self.proteinChunk2.protein

        if protein_1 and \
           protein_2:
            aa_list_1 = protein_1.get_amino_acids()
            aa_list_2 = protein_2.get_amino_acids()
            if len(aa_list_1) == len(aa_list_2):
                protein_1.collapse_all_rotamers() #@@@
                protein_2.collapse_all_rotamers() #@@@
                for aa1, aa2 in zip (aa_list_1, aa_list_2):
                    aa1.color = None
                    aa2.color = None
                    aa1.collapse()
                    aa2.collapse()
            self.win.glpane.gl_update()
        return

    def _thresholdChanged(self, value):
        """
        Slot for Threshold spinbox.
        """
        self.threshold = value
        self._compareProteins()
        return

    def _resetAminoAcids(self):
        """
        Resets the color and collapse all amino acids of all proteins.
        """

        proteinChunkList = getAllProteinChunksInPart(self.win.assy)

        for proteinChunk in proteinChunkList:
            proteinChunk.protein.collapse_all_rotamers()
            aa_list = proteinChunk.protein.get_amino_acids()
            for aa in aa_list:
                aa.color = None
                aa.collapse()

        self.win.glpane.gl_update()
        return

    def _update_UI_do_updates(self):
        """
        Overrides superclass method.

        @see: Command_PropertyManager._update_UI_do_updates()
        """

        self.proteinChunk1 = None
        self.proteinChunk2 = None
        self.comparePushButton.setEnabled(False)
        self.hidePushButton.setEnabled(False)

        selectedProteinList = self.win.assy.getSelectedProteinChunks()

        if len(selectedProteinList) == 0:
            self.structure1LineEdit.setText("")
            self.structure2LineEdit.setText("")
            msg = "Select two structures of the same length in the graphics area, "\
                "then click the <b>Compare</b> button to compare them."

        elif len(selectedProteinList) == 1:
            self.proteinChunk1 = selectedProteinList[0]
            aa1_count = " (%d)" % self.proteinChunk1.protein.count_amino_acids()
            self.structure1LineEdit.setText(self.proteinChunk1.name + aa1_count)
            self.structure2LineEdit.setText("")
            msg = "Select one more structure in the graphics area that is the same "\
                "length as <b>" + self.proteinChunk1.name + "</b>. "\
                "Then click the <b>Compare</b> button to compare them."

        elif len(selectedProteinList) == 2:
            self.proteinChunk1 = selectedProteinList[0]
            aa1_count = " (%d)" % self.proteinChunk1.protein.count_amino_acids()
            self.structure1LineEdit.setText(self.proteinChunk1.name + aa1_count)

            self.proteinChunk2 = selectedProteinList[1]
            aa2_count = " (%d)" % self.proteinChunk2.protein.count_amino_acids()
            self.structure2LineEdit.setText(self.proteinChunk2.name + aa2_count)

            if aa1_count == aa2_count:
                self.comparePushButton.setEnabled(True)
                self.hidePushButton.setEnabled(True)
                msg = "Click the <b>Compare</b> button to compare the two selected structures."
            else:
                msg = "<b>%s</b> and <b>%s</b> are not the same length." % \
                    (self.proteinChunk1.name, self.proteinChunk2.name)
                msg = redmsg(msg)

        else:
            self.structure1LineEdit.setText("")
            self.structure2LineEdit.setText("")
            msg = redmsg("Too many proteins selected.")

        self.updateMessage(msg)
        env.history.redmsg(msg)
        return