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

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

History:
2008-07-24: Created

"""

from utilities.Log  import greenmsg
from command_support.EditCommand import EditCommand
from protein.commands.InsertPeptide.PeptideGenerator import PeptideGenerator
from utilities.constants import gensym
from commands.SelectChunks.SelectChunks_GraphicsMode import SelectChunks_GraphicsMode
from protein.temporary_commands.PeptideLine_GraphicsMode import PeptideLine_GraphicsMode
from utilities.debug import print_compact_stack, print_compact_traceback

import foundation.env as env
from utilities.prefs_constants import cursorTextColor_prefs_key

from protein.commands.BuildProtein.BuildProtein_Command import BuildProtein_Command
from protein.commands.InsertPeptide.InsertPeptide_PropertyManager import InsertPeptide_PropertyManager

_superclass = EditCommand
class InsertPeptide_EditCommand(EditCommand):

    PM_class = InsertPeptide_PropertyManager

    cmd              =  greenmsg("Insert Peptide: ")
    prefix           =  'Peptide'   # used for gensym
    cmdname          = 'Insert Peptide'

    commandName      = 'INSERT_PEPTIDE'
    featurename      = "Insert Peptide"
    from utilities.constants import CL_SUBCOMMAND
    command_level = CL_SUBCOMMAND
    command_parent = 'BUILD_PROTEIN'

    create_name_from_prefix  =  True

    GraphicsMode_class = PeptideLine_GraphicsMode
    #required by PeptideLine_GraphicsMode
    mouseClickPoints = []

    structGenerator = PeptideGenerator()

    command_should_resume_prevMode = True
    command_has_its_own_PM = True

    def __init__(self, commandSequencer):
        """
        Constructor for InsertPeptide_EditCommand
        """
        _superclass.__init__(self, commandSequencer)
        #Maintain a list of peptides created while this command is running.
        self._peptideList = []
        return

    def command_entered(self):
        """
        Extends superclass method.
        @see: basecommand.command_entered() for documentation
        """
        _superclass.command_entered(self)
        #NOTE: Following code was copied from self.init_gui() that existed
        #in old command API -- Ninad 2008-09-18
        if isinstance(self.graphicsMode, PeptideLine_GraphicsMode):
            self._setParamsForPeptideLineGraphicsMode()
            self.mouseClickPoints = []
        #Clear the peptideList as it may still be maintaining a list of peptides
        #from the previous run of the command.
        self._peptideList = []
        ss_idx, self.phi, self.psi, aa_type = self._gatherParameters()
        return

    def command_will_exit(self):
        """
        Extends superclass method.
        @see: basecommand.command_will_exit() for documentation
        """
        if isinstance(self.graphicsMode, PeptideLine_GraphicsMode):
                self.mouseClickPoints = []
        self.graphicsMode.resetVariables()
        self._peptideList = []

        _superclass.command_will_exit(self)
        return

    def _getFlyoutToolBarActionAndParentCommand(self):
        """
        See superclass for documentation.
        @see: self.command_update_flyout()
        """
        flyoutActionToCheck = 'buildPeptideAction'
        parentCommandName = 'BUILD_PROTEIN'
        return flyoutActionToCheck, parentCommandName

    def keep_empty_group(self, group):
        """
        Returns True if the empty group should not be automatically deleted.
        otherwise returns False. The default implementation always returns
        False. Subclasses should override this method if it needs to keep the
        empty group for some reasons. Note that this method will only get called
        when a group has a class constant autdelete_when_empty set to True.
        (and as of 2008-03-06, it is proposed that cnt_updater calls this method
        when needed.
        @see: Command.keep_empty_group() which is overridden here.
        """

        bool_keep = _superclass.keep_empty_group(self, group)
        return bool_keep

    def _gatherParameters(self):
        """
        Return the parameters from the property manager.
        """
        return self.propMgr.getParameters()

    def runCommand(self):
        """
        Overrides EditCommand.runCommand
        """
        self.struct = None
        return

    def _createStructure(self):
        """
        Build a peptide from the parameters in the Property Manager.
        """

        # self.name needed for done message
        if self.create_name_from_prefix:
            # create a new name
            name = self.name = gensym(self.prefix, self.win.assy) # (in _build_struct)
            self._gensym_data_for_reusing_name = (self.prefix, name)
        else:
            # use externally created name
            self._gensym_data_for_reusing_name = None
                # (can't reuse name in this case -- not sure what prefix it was
                #  made with)
            name = self.name

        self.secondary, self.phi, self.psi, aa_type = self._gatherParameters()

        #
        self.win.assy.part.ensure_toplevel_group()
        """
        struct = self.structGenerator.make(self.win.assy,
                                           name,
                                           params,
                                           -self.win.glpane.pov)
        """
        from geometry.VQT import V
        pos1 = V(self.mouseClickPoints[0][0], \
                 self.mouseClickPoints[0][1], \
                 self.mouseClickPoints[0][2])
        pos2 = V(self.mouseClickPoints[1][0], \
                 self.mouseClickPoints[1][1], \
                 self.mouseClickPoints[1][2])
        struct = self.structGenerator.make_aligned(self.win.assy,
                                                   name,
                                                   aa_type,
                                                   self.phi,
                                                   self.psi,
                                                   pos1,
                                                   pos2,
                                                   fake_chain = False,
                                                   secondary = self.secondary)

        self.win.assy.part.topnode.addmember(struct)
        self.win.win_update()
        return struct

    def _modifyStructure(self, params):
        """
        Modify the structure based on the parameters specified.
        Overrides EditCommand._modifystructure. This method removes the old
        structure and creates a new one using self._createStructure.
        See more comments in this method.
        """

        #@NOTE: Unlike editcommands such as Plane_EditCommand or
        #DnaSegment_EditCommand this  actually removes the structure and
        #creates a new one when its modified.
        #TODO: Change this implementation to make it similar to whats done
        #iin DnaSegment resize. (see DnaSegment_EditCommand)
        self._removeStructure()

        self.previousParams = params

        self.struct = self._createStructure()
        return

    def cancelStructure(self):
        """
        Overrides Editcommand.cancelStructure. Calls _removePeptides which
        deletes all the peptides created while this command was running.
        @see: B{EditCommand.cancelStructure}
        """
        _superclass.cancelStructure(self)
        self._removePeptides()
        return

    def _removePeptides(self):
        """
        Deletes all the peptides created while this command was running
        @see: L{self.cancelStructure}
        """
        peptideList = self._peptideList

        #for peptide in peptideList:
            #can peptide be None?  Lets add this condition to be on the safer
            #side.
        #    if peptide is not None:
        #        peptide.kill_with_contents()
        #    self._revertNumber()

        self._peptideList = []
        self.win.win_update()
        return

    def createStructure(self):
        """
        Overrides superclass method. Creates the structure

        """
        assert self.propMgr is not None

        if self.struct is not None:
            self.struct = None

        self.win.assy.part.ensure_toplevel_group()
        self.propMgr.endPoint1 = self.mouseClickPoints[0]
        self.propMgr.endPoint2 = self.mouseClickPoints[1]

        #ntLength = vlen(self.mouseClickPoints[0] - self.mouseClickPoints[1])

        self.preview_or_finalize_structure(previewing = True)

        #Now append this peptide to self._peptideList
        self._peptideList.append(self.struct)

        #clear the mouseClickPoints list
        self.mouseClickPoints = []
        self.graphicsMode.resetVariables()
        return

    def _finalizeStructure(self):
        """
        Finalize the structure. This is a step just before calling Done method.
        to exit out of this command. Subclasses may overide this method
        @see: EditCommand_PM.ok_btn_clicked
        """

        if len(self.mouseClickPoints) == 1:
            return
        else:
            _superclass._finalizeStructure(self)
        return


    def _getStructureType(self):
        """
        Subclasses override this method to define their own structure type.
        Returns the type of the structure this editCommand supports.
        This is used in isinstance test.
        @see: EditCommand._getStructureType() (overridden here)
        """

        return self.win.assy.Chunk

    def _setParamsForPeptideLineGraphicsMode(self):
        #"""
        #Needed for PeptideLine_GraphicsMode (NanotubeLine_GM). The method names need to
        #be revised (e.g. callback_xxx. The prefix callback_ was for the earlier
        #implementation of CntLine mode where it just used to supply some
        #parameters to the previous mode using the callbacks from the
        #previous mode.
        #"""
        self.mouseClickLimit = None
        self.jigList = self.win.assy.getSelectedJigs()
        self.callbackMethodForCursorTextString = self.getCursorText
        self.callbackForSnapEnabled = self.isRubberbandLineSnapEnabled
        self.callback_rubberbandLineDisplay = self.getDisplayStyleForNtRubberbandLine
        return

    def getCursorText(self, endPoint1, endPoint2):
        """
        This is used as a callback method in PeptideLineLine mode
        @see: PeptideLine_GraphicsMode.setParams, PeptideLine_GraphicsMode.Draw
        """

        text = ''
        textColor = env.prefs[cursorTextColor_prefs_key]

        if endPoint1 is None or endPoint2 is None:
            return text, textColor

        vec = endPoint2 - endPoint1
        from geometry.VQT import vlen
        peptideLength = vlen(vec)
        ss_idx, phi, psi, aa_type = self._gatherParameters()
        peptideLength = self.structGenerator.get_number_of_res(endPoint1, endPoint2, phi, psi)
        lengthString = self._getCursorText_length(peptideLength)

        thetaString = ''
        #Urmi 20080804: not sure if angle will be required later
        theta = self.glpane.get_angle_made_with_screen_right(vec)
        thetaString = '%5.2f deg'%theta

        commaString = ", "

        text = lengthString

        if text and thetaString:
            text += commaString

        text += thetaString

        return text, textColor


    def _getCursorText_length(self, peptideLength):
        """
        Returns a string that gives the length of the Peptide for the cursor
        text.

        @param peptideLength: length of the peptide (number of amino acids)
        @type peptideLength: int
        """

        # This should be moved to more appropriate place. piotr 081308
        self.secondary, self.phi, self.psi, aa_type = self._gatherParameters()

        peptideLengthString = ''

        lengthUnitString = 'AA'
        #change the unit of length to nanometers if the length is > 10A
        #fixes part of bug 2856

        peptideLengthString = "%d%s"%(peptideLength, lengthUnitString)

        return peptideLengthString

    def getDisplayStyleForNtRubberbandLine(self):
        """
        This is used as a callback method in peptideLine mode.
        @return: The current display style for the rubberband line.
        @rtype: string
        @note: Placeholder for now
        """
        return 'Ladder'

    def isRubberbandLineSnapEnabled(self):
        """
        This returns True or False based on the checkbox state in the PM.

        This is used as a callback method in CntLine mode (NanotubeLine_GM)
        @see: NanotubeLine_GM.snapLineEndPoint where the boolean value returned from
              this method is used
        @return: Checked state of the linesnapCheckBox in the PM
        @rtype: boolean
        @note: placeholder for now
        """
        return True