import wx import wx.lib.ogl as ogl import wx.lib.dialogs _IDGETINFO=1000 _IDIS_A=1001 _IDPART_OF=1002 class OGLcanvas(ogl.ShapeCanvas): def __init__(self, parent, frame,agent): ogl.ShapeCanvas.__init__(self, parent) self.go_child = {} maxWidth = 2000 maxHeight = 2000 self.SetScrollbars(100,100, maxWidth/20, maxHeight/20) self.frame = frame self.SetBackgroundColour("LIGHT BLUE") #wx.WHITE) self.diagram = ogl.Diagram() self.SetDiagram(self.diagram) self.diagram.SetCanvas(self) self.shapes = [] self.save_gdi = [] self.sizeX = 78 self.sizeY = 45 self.is_a = 'is_a' self.part_of = 'part_of' rRectBrush = wx.Brush("MEDIUM TURQUOISE", wx.SOLID) dsBrush = wx.Brush("WHEAT", wx.SOLID) self.setMenu() self.agent = agent # self.Bind(wx.EVT_RIGHT_DOWN, self.showMenu) # self.Bind(wx.EVT_MENU, self.checkVal) def AddNode(self,val,x,y,color,type=None): # print "FROM OGL",val if type!=None: if val.find('GO:')<0: name = 'GO:'+val+'\n '+type else: name = val else: if val.find('GO:')<0: name = 'GO:'+val else: name = val self.MyAddShape(ogl.CircleShape(self.sizeX),x, y, wx.BLACK_PEN, color,name) def AddParent(self,val,x,y,colour = wx.LIGHT_GREY_BRUSH,np=''): self.MyAddShape(ogl.RectangleShape(self.sizeX,self.sizeY),x,y, wx.BLACK_PEN,colour,self.checkName(val,np)) # def AddGranParent(self,val,x,y): # color = wx.Brush("AQUAMARINE", wx.SOLID) # self.MyAddShape(ogl.EllipseShape(self.sizeX,self.sizeY),x,y, wx.BLACK_PEN,color,self.checkName(val)) def AddAncestor(self,val,x,y): color = wx.Brush("WHEAT", wx.SOLID) color = wx.Brush("AQUAMARINE", wx.SOLID) self.MyAddShape(ogl.EllipseShape(self.sizeX,self.sizeY),x,y, wx.BLACK_PEN,color,self.checkName(val)) def AddTextData(self,val,x,y): self.MyAddShape(ogl.TextShape(self.sizeX,self.sizeY),x,y, wx.BLACK_PEN,wx.LIGHT_GREY_BRUSH,val) def checkName(self,name,np=""): if name.find('GO:') < 0: return 'GO:'+name + '\n'+np else: return name + '\n'+np def createTree(self,i,j,text): index = i # for x in range(i,j):#len(self.shapes)): fromShape = self.shapes[i] # if x+1 == len(self.shapes): # toShape = self.shapes[0] # else: toShape = self.shapes[j] line = ogl.LineShape() line.SetCanvas(self) line.SetPen(wx.BLACK_PEN) line.SetBrush(wx.BLACK_BRUSH) line.AddText(text) line.GetShape().GetRegions()[0].GetFormattedText()[0].GetText() line.GetShape().GetRegions()[0].SetSize(40,20) line.GetShape().GetRegions()[0].SetFormatMode(0) line.MakeLineControlPoints(2) line.AddArrow(ogl.ARROW_ARROW,ogl.ARROW_POSITION_START) fromShape.AddLine(line, toShape) self.diagram.AddShape(line) line.Show(True) def MyAddShape(self, shape, x, y, pen, brush, text): # Composites have to be moved for all children to get in place if isinstance(shape, ogl.CompositeShape): dc = wx.ClientDC(self) self.PrepareDC(dc) shape.Move(dc, x, y) else: shape.SetDraggable(True, True) shape.SetCanvas(self) shape.SetX(x) shape.SetY(y) if pen: shape.SetPen(pen) if brush: shape.SetBrush(brush) if text: for line in text.split('\n'): shape.SetFont(wx.Font(8,wx.SWISS, wx.NORMAL, wx.NORMAL)) shape.AddText(line) #shape.SetShadowMode(ogl.SHADOW_RIGHT) self.diagram.AddShape(shape) shape.Show(True) evthandler = MyEvtHandler(self.frame,self) evthandler.SetShape(shape) evthandler.SetPreviousHandler(shape.GetEventHandler()) shape.SetEventHandler(evthandler) self.shapes.append(shape) return shape # def OnRightClick(self, *dontcare): # print ("%s\n" % self.GetShape()) # def OnBeginDragLeft(self, x, y, keys): # print ("OnBeginDragLeft: %s, %s, %s\n" % (x, y, keys)) # def OnEndDragLeft(self, x, y, keys): # print ("OnEndDragLeft: %s, %s, %s\n" % (x, y, keys)) def setGO_child(self,go_child): self.go_child = go_child def getGO_child(self,key): return self.go_child[key] def createLastGraph(self,data,go_child,val_child,name): x = 300 y = 70 # dataVal = dict([(x.split("_")[1],e.index(x)) for e in val for x in e if x.startswith('Child')]) # print val_child closeList = [] countList = {} count = 0 name.reverse() for e in data: key = e.keys()[0] nodeData = e[key] if key[1].find("GO:")<0: newKey = "GO:"+key[1] else: newKey = key[1] if not newKey in closeList: self.AddAncestor(newKey,x,y) closeList.append(newKey) countList[newKey.split("GO:")[1]]=count count = count+1 y=y+80 x = 300 # print "NAME ---->",name for n in nodeData: if not "GO:"+n in closeList: if val_child[n] == 1: color = wx.Brush("MEDIUM TURQUOISE", wx.SOLID) self.AddParent(n,x,y,color,name[0]) else: color = wx.Brush("MEDIUM GOLDENROD", wx.SOLID) self.AddParent(n,x,y,color,name[1]) closeList.append("GO:"+n) countList[n]=count count += 1 if len(nodeData)>1: x=x+100 else: pass pair = [] for e in data: key = e.keys()[0][1] if key.find("GO:")!=-1: newK = key.split("GO:")[1] else: newK = key val = [(countList[newK],countList[w]) for w in e.values()[0]] pair.extend(val) # print pair for e in pair: self.createTree(e[0],e[1],self.is_a) return [] def createGraph(self,data,go_child,name): self.setGO_child(go_child) # print "oglLib",data # print go_child data = [(e.split('_')[0],e.split('_')[1]) for e in data] # print data done = False while not done: # print data[0][1] if data[0][1]=='all': done = True else: if data[0][1].find('GO:')<0: # print "here ---> No GO:",data[0][1] val = go_child['GO:'+data[0][1]] else: # print 'WITH GO',data[0][1] val = go_child[data[0][1]] [data.insert(0,('Ancestor',e)) for e in val] name.reverse() # print "NAME",name self.Graph(data,name) return data def Graph(self,data,name): self.sizeX = 78 self.sizeY = 45 x = 300 y = 60 Y = [] Y.append(y) tmp = 0 for e in range(1,len(data)): tmp = y+y*0.5 try: val = Y[e-1]+tmp Y.append(val) except: # print "FAIL" Y.append(tmp) color1 = wx.Brush("MEDIUM TURQUOISE", wx.SOLID) color2 = wx.Brush("MEDIUM GOLDENROD", wx.SOLID) c = [color1,color2] index,parentIndex,childCount = 0,0,1 x1 = [0,0] name = name#['Human','Yeast'] # print "NAEM",name for i,e in zip(range(0,len(data)),data): if e[0] == "Ancestor": self.AddAncestor(e[1],x,Y[i]) elif e[0]== "Parent": self.AddParent(e[1],x,Y[i]) parentIndex = i elif e[0]== "Child": if childCount==1: x1.insert(0,x + (x*0.2) +(x1[0]*0.5)) childCount = 0 else: x1.insert(1,x-(x*0.2)-(x1[1]*0.5)) childCount = 1 if index == 0: index = i else: i = index self.AddNode(e[1],x1[childCount],Y[i],c[childCount],name[childCount]) boolParent = True for e in range(0,len(data)): if e+1 < len(data): if e==parentIndex: boolParent = False if boolParent: self.createTree(e,e+1,self.is_a) else: self.createTree(parentIndex,e+1,self.is_a) else: pass def setMenu(self): self._menu = wx.Menu() _getInfo = _IDGETINFO self._menu.Append(_getInfo, "GetInfo") # _getIs_a = _IDIS_A # self._menu.Append(_getIs_a, "Expand Graph child") # _getPart_Of = _IDPART_OF # self._menu.Append(_getPart_Of, "Expand Graph Part_Of") def showMenu(self,*goAnnotation): self.goAnnotation = goAnnotation[0] self.x,self.y = goAnnotation[1],goAnnotation[2] self.w,self.h = goAnnotation[3],goAnnotation[4] self.PopupMenu(self._menu) def setID(self,id): self.E_ID = id self.executeOperations() def executeOperations(self): shapeIndex = [e for e in range(0,len(self.shapes))\ if self.shapes[e].GetRegions()[0].GetFormattedText()[0].GetText() == self.goAnnotation] if self.E_ID == _IDGETINFO: data = self.agent.successor.getInfo(self.goAnnotation) definitions = ['Name:\n','Definition:\n','Comments:\n'] msg = [x+'\t'+e[0]+'\n' for x,e in zip(definitions,data) if e] newMsg ='GO:\n\t'+str(self.goAnnotation)+'\n' for e in msg: newMsg += e dlg = wx.lib.dialogs.ScrolledMessageDialog(self, newMsg, "Gene Ontology") dlg.Show() elif self.E_ID == _IDIS_A: # newNode = self.agent.successor.getIS_a(self.goAnnotation) # print self.goAnnotation # print self.go_child newNode = self.getGO_child(self.goAnnotation) # print newNode if newNode: for e in newNode: val = e if val!='all': val = val.split('GO:')[1] c = wx.Brush("MEDIUM VIOLET RED", wx.SOLID) self.AddNode(val,self.x+100,self.y+100,c) self.createTree(len(self.shapes)-1,shapeIndex[0],self.is_a) else: dlg1 = wx.lib.dialogs.messageDialog(self,'No Relation Is_a found with: GO:'+self.goAnnotation,\ "No Found",wx.OK | wx.ICON_INFORMATION) # dlg1.Show() else: newNode = self.agent.successor.getPart_of(self.goAnnotation) # print newNode if newNode: for e in newNode: val = e.split('#')[1] if val!='all': val = val.split('GO:')[1] c = wx.Brush("LIGHT STEEL BLUE", wx.SOLID) self.AddNode(val,self.x+100,self.y+100,c) self.createTree(len(self.shapes)-1,shapeIndex[0],self.part_of) else: dlg1 = wx.lib.dialogs.messageDialog(self,'No Relation Part_of found with: GO:'+self.goAnnotation,\ "No Found",wx.OK | wx.ICON_INFORMATION) class MyEvtHandler(ogl.ShapeEvtHandler): def __init__(self,frame,parent): ogl.ShapeEvtHandler.__init__(self) self.statbarFrame = frame self.parent = parent self.goAnnotation = None self.ID = 0 self.parent.Bind(wx.EVT_MENU, self.checkVal) def setGO(self,val): self.goAnnotation = val def getGO(self): return self.goAnnotation def UpdateStatusBar(self, shape): x, y = shape.GetX(), shape.GetY() width, height = shape.GetBoundingBoxMax() self.statbarFrame.SetStatusText("Pos: (%d, %d) Size: (%d, %d)" % (x, y, width, height)) def OnRightClick(self,*evt): goAnnotation = self.GetShape().GetRegions()[0].GetFormattedText()[0].GetText() shape = self.GetShape() self.x,self.y = shape.GetX(), shape.GetY() self.width, self.height = shape.GetBoundingBoxMax() self.setGO(goAnnotation) self.startMenu() def startMenu(self): self.parent.showMenu(self.goAnnotation,self.x,self.y,self.width,self.height) def checkVal(self,evt): self.ID = evt.GetId() self.parent.setID(self.ID)