Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

#$Id: selectMolecules.py 281 2016-07-12 00:50:13Z sarkiss $ 

"""LigandPage.py contains the following class. 

""" 

 

import wx, os 

from wx.lib.buttons import ThemedGenBitmapTextButton 

from wx.lib import eventwatcher 

#edit this list to add other atom types 

AD_atom_types = set(['H', 'HD', 'HS', 'C', 'A', 'N', 'NA', 'NS', 'OA', 'OS', 'F', 'Mg', 'MG', 'P', 'SA', 'S', 

                 'Cl', 'CL', 'Ca', 'CA', 'Mn', 'MN', 'Fe', 'FE', 'Zn', 'ZN', 'Br', 'BR', 'I', 'Z']) 

 

class SelectMoleculesPage(wx.Panel): 

    def __init__(self, parent, checkAtomTypes=True): 

        wx.Panel.__init__(self, parent, -1) 

        self.checkAtomTypes = checkAtomTypes 

        mainSizer = wx.BoxSizer(wx.VERTICAL) 

 

        vSizer = wx.BoxSizer(wx.VERTICAL) 

        vSizer.Add(wx.StaticText(self, -1, """Select Ligand(s) and Macromolecule(s) from Navigator -> AutoDock panel. 

\nUse Control and Shift buttons to select multiple Ligands.\n"""), 0, wx.ALL, 10 ) 

        self.label = wx.StaticText(self, -1, "") 

        vSizer.Add(self.label, 0,  wx.ALL, 10) 

        mainSizer.Add(vSizer, 1, wx.EXPAND) 

 

        self.forwardButton = wx.Button(self, wx.ID_FORWARD, "") 

        self.backButton = wx.Button(self, wx.ID_BACKWARD, "") 

        bitmap = wx.ArtProvider_GetBitmap(wx.ART_ADD_BOOKMARK, wx.ART_BUTTON) 

        ligandButton = ThemedGenBitmapTextButton(self, -1, bitmap, "Add Ligand(s)") 

        macromoleculeButton = ThemedGenBitmapTextButton(self, -1, bitmap, "Add Macromolecule(s)") 

 

        buttonSizer = wx.BoxSizer(wx.HORIZONTAL) 

 

        lin = wx.StaticLine(self) 

        buttonSizer.Add(ligandButton) 

        buttonSizer.Add(macromoleculeButton) 

        buttonSizer.Add((150, -1), 1, flag=wx.EXPAND | wx.ALIGN_RIGHT) 

        buttonSizer.Add(self.backButton, 0, wx.ALIGN_RIGHT) 

        buttonSizer.Add(self.forwardButton, 0, wx.ALIGN_RIGHT) 

 

        mainSizer.Add(lin,0,wx.EXPAND) 

        mainSizer.Add(buttonSizer, 0, wx.EXPAND|wx.ALIGN_BOTTOM) 

        self.SetSizer(mainSizer) 

 

        self.Bind(wx.EVT_BUTTON, self.Next, self.forwardButton) 

        self.Bind(wx.EVT_BUTTON, self.Back, self.backButton) 

        self.Bind(wx.EVT_BUTTON, self.AddLigand, ligandButton) 

        self.Bind(wx.EVT_BUTTON, self.AddMacromolecule, macromoleculeButton) 

        #self.Bind(wx.EVT_SHOW, self.SetActive) 

        self.frame = self.TopLevelParent 

        self.ligandsTree = self.frame.autodockNav.autodockTree.ligandTree.tree 

        self.ligandsTree.Bind(wx.EVT_TREE_DELETE_ITEM, self.CheckLigands) 

        self.ligandsTree.Bind(wx.EVT_TREE_SEL_CHANGED, self.CheckLigands) 

 

        self.macromoleculeTree = self.frame.autodockNav.autodockTree.macromoleculeTree.tree 

        self.macromoleculeTree.Bind(wx.EVT_TREE_DELETE_ITEM, self.MacromoleculeEvent) 

        self.macromoleculeTree.Bind(wx.EVT_TREE_SEL_CHANGED, self.MacromoleculeEvent) 

 

        self.ligandTxt = "  |  " 

        self.macromoleculeTxt = "" 

        self.ligandPass = False 

        self.macromoleculePass = False 

        self.macromoleculePaths = [] 

 

    def CheckLigands(self, event): 

        if not self: 

            return 

        selectionCount = len(self.ligandsTree.Selections) 

        if self.ligandsTree.RootItem in self.ligandsTree.Selections: 

            selectionCount -= 1 

        if selectionCount == 0: 

            self.forwardButton.Enable(False) 

            self.ligandPass = False 

            self.ligandTxt  = "No ligand selected.  |   " 

            self.label.SetLabel(self.ligandTxt + self.macromoleculeTxt) 

        else: 

            self.ligandTxt  =  str(selectionCount) +" ligand(s) selected.   |   " 

            self.label.SetLabel(self.ligandTxt + self.macromoleculeTxt) 

            self.ligandPass = True 

            if self.macromoleculePass: 

                self.forwardButton.Enable(True) 

        if event: # Skip is needed continue even chain since this widgets used in Vina and AutoDock  

            event.Skip() 

 

    def MacromoleculeEvent(self, event): 

        if event: 

            event.Skip() 

        wx.CallAfter(self.CheckMacromolecule) 

 

    def CheckMacromolecule(self): 

        if not self: #otherwise this is called on exit and print traceback 

            return 

        selectionCount = len(self.macromoleculeTree.Selections) 

        self.macromoleculePaths = [] 

        if self.macromoleculeTree.RootItem in self.macromoleculeTree.Selections: 

            selectionCount -= 1 

        if selectionCount == 0: 

            self.forwardButton.Enable(False) 

            self.macromoleculePass = False 

            self.macromoleculeTxt  = "No macromolecule selected." 

            self.label.SetLabel(self.ligandTxt + self.macromoleculeTxt) 

        else: 

            for selection in self.macromoleculeTree.Selections: 

                if selection and selection.IsOk() and selection != self.macromoleculeTree.RootItem: 

                    parent = self.macromoleculeTree.GetItemParent(selection) 

                    path = self.macromoleculeTree.GetPyData(selection) 

                    macromoleculePath = None 

                    if os.path.isdir(path) and parent == self.macromoleculeTree.RootItem: 

                        text = self.macromoleculeTree.GetItemText(selection) 

                        macromoleculePath = os.path.join(path,text+'.pdbqt') 

                    if path[-6:].lower() == '.pdbqt' and self.macromoleculeTree.GetItemParent(parent) == self.macromoleculeTree.RootItem and path.find("_out") == -1: 

                        macromoleculePath = path 

                    if macromoleculePath and macromoleculePath.find('_flex.pdbqt') != -1: 

                        macromoleculePath = macromoleculePath.replace('_flex.pdbqt', '_rigid.pdbqt') 

                        if not os.path.exists(macromoleculePath): 

                            self.frame.log.warning("Can't find "+macromoleculePath + " required for flex docking.") 

                    if macromoleculePath and os.path.exists(macromoleculePath): 

                        self.macromoleculePaths.append(macromoleculePath) 

 

            if self.macromoleculePaths: 

                if len(self.macromoleculePaths) == 1: 

                    self.macromoleculeTxt = self.macromoleculePaths[0]+" selected." 

                else: 

                    self.macromoleculePaths = set(self.macromoleculePaths) 

                    self.macromoleculePaths = list(self.macromoleculePaths) 

                    self.macromoleculeTxt = str(len(self.macromoleculePaths))+" macromolecules selected." 

                self.label.SetLabel(self.ligandTxt + self.macromoleculeTxt) 

                if self.ligandPass: 

                    self.forwardButton.Enable(True) 

                self.macromoleculePass = True 

            else: 

                self.forwardButton.Enable(False) 

                self.macromoleculePass = False 

                self.macromoleculeTxt = "No macromolecule selected." 

                self.label.SetLabel(self.ligandTxt + self.macromoleculeTxt) 

 

    def SetActive(self, event): 

        "This method is bound to wx.EVT_SHOW, i.e., invoked when this page is shown" 

        self.frame.navigator.SetSelection(1) 

        self.ligandsTree.Expand(self.ligandsTree.RootItem) 

        self.CheckLigands(None) 

        self.macromoleculeTree.Expand(self.macromoleculeTree.RootItem) 

        self.CheckMacromolecule() 

 

    def AddLigand(self, event): 

        mols = self.frame.OnFileOpenMenu(None) 

        if mols: 

            for mol in mols: 

                self.frame.molNav.item = mol.allAtoms[0].assembly.treeID 

                self.frame.molNav.OnMakeLigand(None) 

            self.ligandsTree.Expand(self.ligandsTree.RootItem) 

            self.frame.navigator.SetSelection(1) 

            self.forwardButton.Enable(True) 

            self.SetActive(None) 

 

    def AddMacromolecule(self, event): 

        mols = self.frame.OnFileOpenMenu(None) 

        if mols: 

            for mol in mols: 

                self.frame.molNav.item = mol.allAtoms[0].assembly.treeID 

                self.frame.molNav.OnMakeMacromolecule(None) 

            self.macromoleculeTree.Expand(self.macromoleculeTree.RootItem) 

            self.frame.navigator.SetSelection(1) 

            self.SetActive(None) 

 

    def Next(self, event): 

        "Goto next page" 

        ligands = [] 

        selections = self.ligandsTree.Selections 

        self.frame.SetAllCursors(wx.StockCursor(wx.CURSOR_WAIT)) 

        for selection in selections: 

            if selection != self.ligandsTree.RootItem: 

                path = self.ligandsTree.GetPyData(selection) 

                ligands.append(path) 

        self.frame.vsModel.ligands = ligands 

        if self.checkAtomTypes: 

            self.frame.vsModel.CreateMolDict() 

            self.CheckAtomTypes() 

        self.frame.SetAllCursors(wx.NullCursor) 

        if self.macromoleculePaths: 

            self.frame.vsModel.macromoleculePaths = self.macromoleculePaths 

            self.macromoleculePass = True 

        else: 

            return 

        if not event == None: 

            self.Parent.SetSelection(2) 

 

    def Back(self, event): 

        "Goto previous page" 

        self.Parent.SetSelection(0) 

 

    def CheckAtomTypes(self): 

        msgChoices = [] 

        for item in self.frame.vsModel.molDict: 

            aElements = self.frame.vsModel.molDict[item]['autodock_element'] 

            diffSet = aElements.difference(AD_atom_types) 

            if diffSet: 

                msg = item + " - Atoms - " + str(list(diffSet)) 

                msgChoices.append(msg) 

                path = os.path.join(self.frame.vsModel.ligandsFolder, item+'.pdbqt') 

                self.tree.SelectItem(self.frame.autodockNav.autodockTree.ligandTree.treeDict[path], False) 

                self.frame.vsModel.ligands.remove(path) 

                self.frame.vsModel.ligand_types.difference_update(diffSet) 

        if msgChoices: 

            dlg = wx.SingleChoiceDialog(self.frame, 

                                        "No AutoDock parameter is available for the following molecule(s)\n"+ 

                                        "(won't dock them without force):", 

                                        'Molecule(s) with Unknown AutoDock Elements', 

                                        msgChoices, 

                                        wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER | wx.OK |wx.CENTRE) 

 

            dlg.ShowModal() 

            dlg.Destroy()