00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 import PIL.Image
00035 import logging, os
00036 import wx
00037
00038 from PyPedal import pyp_demog
00039 from PyPedal import pyp_graphics
00040 from PyPedal import pyp_io
00041 from PyPedal import pyp_newclasses
00042 from PyPedal import pyp_nrm
00043 from PyPedal import pyp_metrics
00044 from PyPedal import pyp_utils
00045 from PyPedal import pyp_db
00046 from PyPedal import pyp_reports
00047 from PyPedal import pyp_gui_graphs
00048 from PyPedal import pyp_gui_metrics
00049 from PyPedal import pyp_gui_utils
00050
00051
00052 ID_FILE_OPEN = 101
00053 ID_FILE_SAVE = 102
00054 ID_FILE_SAVEAS = 103
00055 ID_FILE_CLOSE = 104
00056 ID_FILE_EXIT = 110
00057
00058 ID_PED_META = 201
00059 ID_PED_LIST = 202
00060 ID_PED_VIEW = 203
00061
00062 ID_METRIC_INBREEDING = 301
00063 ID_METRIC_CALC_FA = 302
00064
00065 ID_GRAPH_INBREEDING = 401
00066
00067 ID_SETTING_OPTIONS = 501
00068 ID_SETTING_LOG = 502
00069
00070 ID_HELP_HELP = 601
00071 ID_HELP_ABOUT = 602
00072
00073 ID_MAIN_TB = 901
00074
00075
00076 MAIN_WINDOW_X = 640
00077 MAIN_WINDOW_Y = 480
00078
00079 FIXED_FONT = ('Courier New', 10)
00080
00081 options = {}
00082 options['pedformat'] = 'asdxb'
00083 options['renumber'] = 1
00084 options['messages'] = 'quiet'
00085
00086
00087
00088 class MainWindow(wx.Frame):
00089 def __init__(self,parent,id,title):
00090 wx.Frame.__init__(self,parent,wx.ID_ANY, title,
00091 size = ( MAIN_WINDOW_X, MAIN_WINDOW_Y ),
00092 style = wx.DEFAULT_FRAME_STYLE |
00093 wx.NO_FULL_REPAINT_ON_RESIZE )
00094 self.textbox = wx.TextCtrl(self, 1,
00095 style = wx.TE_MULTILINE)
00096 self.CreateStatusBar()
00097
00098
00099 filemenu = self.filemenu = wx.Menu()
00100 filemenu.Append(ID_FILE_OPEN, "&Open", " Open a pedigree file")
00101 filemenu.Append(ID_FILE_SAVE, "&Save", " Save a pedigree file")
00102 filemenu.Append(ID_FILE_SAVEAS, "Save &As", " Save a pedigree file as...")
00103 filemenu.Append(ID_FILE_CLOSE, "&Close", " Close a pedigree file")
00104 filemenu.AppendSeparator()
00105 filemenu.Append(ID_FILE_EXIT,"E&xit", " Exit PyPedal")
00106
00107
00108 pedmenu= wx.Menu()
00109 pedmenu.Append(ID_PED_META, "&Metadata", " View pedigree metadata")
00110 pedmenu.Append(ID_PED_LIST, "&List Animals", " View a list of animal records")
00111 pedmenu.Append(ID_PED_VIEW, "&View", " View a diagram of the pedigree")
00112
00113
00114 metricmenu= wx.Menu()
00115 metricmenu.Append(ID_METRIC_INBREEDING, "&Inbreeding", " Calculate coefficients of inbreeding")
00116 metricmenu.Append(ID_METRIC_CALC_FA, "Effective &Founders", " Calculate the effective founder number")
00117
00118
00119 graphmenu= wx.Menu()
00120 graphmenu.Append(ID_GRAPH_INBREEDING, "&Inbreeding", " View inbreeding by birth year")
00121
00122
00123 settingmenu= wx.Menu()
00124 settingmenu.Append(ID_SETTING_OPTIONS, "Pedigree &Options", " Pedigree options")
00125 settingmenu.AppendSeparator()
00126 settingmenu.Append(ID_SETTING_LOG, "View &Log", " View logfile")
00127
00128
00129 helpmenu= wx.Menu()
00130 helpmenu.Append(ID_HELP_HELP, "&Help", " Help with PyPedal")
00131 helpmenu.AppendSeparator()
00132 helpmenu.Append(ID_HELP_ABOUT,"&About", " About PyPedal")
00133
00134
00135
00136
00137 self.pedigreedirty = False
00138 self.filetag='_test_gui_'
00139 self.sepchar=' '
00140 self.debug=0
00141 self.io='no'
00142 self.renum=1
00143 self.outformat='0'
00144 self.name='GUI Test Pedigree'
00145 self.alleles=0
00146
00147
00148
00149
00150 menuBar = self.menuBar = wx.MenuBar()
00151 menuBar.Append(filemenu,"&File")
00152 menuBar.Append(pedmenu,"&Pedigree")
00153 menuBar.Append(metricmenu,"&Metrics")
00154 menuBar.Append(graphmenu,"&Graphics")
00155 menuBar.Append(settingmenu,"&Settings")
00156 menuBar.Append(helpmenu,"&Help")
00157 self.SetMenuBar(menuBar)
00158
00159
00160 wx.EVT_MENU(self, ID_FILE_OPEN, self.OnFileOpenDialog)
00161 wx.EVT_MENU(self, ID_FILE_SAVE, self.ToDo)
00162 wx.EVT_MENU(self, ID_FILE_SAVEAS, self.ToDo)
00163 wx.EVT_MENU(self, ID_FILE_CLOSE, self.ToDo)
00164 wx.EVT_MENU(self, ID_FILE_EXIT, self.OnAppExit)
00165
00166 wx.EVT_MENU(self, ID_PED_META, self.OnPedMeta)
00167 wx.EVT_MENU(self, ID_PED_LIST, self.OnPedList)
00168 wx.EVT_MENU(self, ID_PED_VIEW, self.OnPedView)
00169
00170 wx.EVT_MENU(self, ID_METRIC_INBREEDING, self.OnMetricsInbreeding)
00171 wx.EVT_MENU(self, ID_METRIC_CALC_FA, self.OnMetricsEffectiveFounders)
00172
00173 wx.EVT_MENU(self, ID_GRAPH_INBREEDING, self.OnGraphInbreed)
00174
00175 wx.EVT_MENU(self, ID_SETTING_OPTIONS, self.OnSettingsOptions)
00176 wx.EVT_MENU(self, ID_SETTING_LOG, self.OnSettingsViewLog)
00177
00178 wx.EVT_MENU(self, ID_HELP_HELP, self.ToDo)
00179 wx.EVT_MENU(self, ID_HELP_ABOUT, self.OnAbout)
00180
00181 self.Show(True)
00182
00183
00184
00185
00186 self.filehistory = wx.FileHistory()
00187 self.filehistory.UseMenu(filemenu)
00188
00189
00190
00191
00192 self.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
00193 self.Bind(wx.EVT_MENU, self.OnFileOpenDialog, id=ID_FILE_OPEN)
00194 self.Bind( wx.EVT_MENU_RANGE,
00195 self.OnFileHistory,
00196 id = wx.ID_FILE1,
00197 id2 = wx.ID_FILE9 )
00198 self.Bind(wx.EVT_WINDOW_DESTROY, self.Cleanup)
00199
00200 def Cleanup(self, *args):
00201
00202 del self.filehistory
00203
00204
00205 def OnRightClick(self, evt):
00206 self.PopupMenu(self.menu, evt.GetPosition())
00207
00208
00209
00210
00211
00212
00213 def OnFileOpenDialog(self, event):
00214 dlg = wx.FileDialog(self,
00215 'PyPedal - Select a pedigree to open',
00216 '.',
00217 '',
00218 '*.ped',
00219 wx.OPEN)
00220 try:
00221 if dlg.ShowModal() == wx.ID_OK:
00222 path = dlg.GetPath()
00223 self._OpenFile(dlg.GetPath())
00224 logging.info('OnFileOpenDialog(): Pedigree file %s selected.\n', path)
00225 self.filehistory.AddFileToHistory(path)
00226 except:
00227 dlg.Destroy()
00228
00229
00230 def OnFileHistory(self, evt):
00231
00232 fileNum = evt.GetId() - wx.ID_FILE1
00233 path = self.filehistory.GetHistoryFile(fileNum)
00234 logging.info('OnFileHistory(): Pedigree file %s selected.\n', path)
00235
00236 self.filehistory.AddFileToHistory(path)
00237
00238
00239
00240
00241
00242
00243
00244 def _OpenFile(self, filename):
00245 self.SetFilename(filename)
00246 f = open(filename, 'r')
00247 data = f.read()
00248 f.close()
00249 self.textbox.Clear()
00250
00251 try:
00252 options['pedfile'] = filename
00253 self._pedigree = pyp_newclasses.NewPedigree(options)
00254 self._pedigree.load()
00255 self.textbox.AppendText(self._pedigree.metadata.stringme())
00256 except:
00257 pass
00258
00259
00260
00261
00262
00263
00264 def SetFilename(self, filename):
00265 self.filename = filename
00266 self.SetStatusText(self.filename)
00267
00268
00269
00270
00271
00272
00273
00274 def Save(self, event):
00275 pass
00276
00277
00278
00279
00280
00281
00282 def OnPedMeta(self, event):
00283 if hasattr(self,'_pedigree'):
00284 self.textbox.Clear()
00285 self.textbox.AppendText(self._pedigree.metadata.stringme())
00286 else:
00287 pyp_gui_utils.PyPedalShowErrorDialog(self,sedTitle='Display pedigree metadata', sedMessage='These data cannot be displayed because you have not yet loaded a pedigree!')
00288
00289
00290
00291
00292
00293
00294 def OnPedList(self, event):
00295 pass
00296
00297
00298
00299
00300
00301
00302
00303 def OnPedView(self, event):
00304 pass
00305
00306
00307
00308
00309
00310
00311 def OnAbout(self, event):
00312 dlg = wx.MessageDialog(self, ' PyPedal 2.0.0a19\n'
00313 ' A software package for pedigree analysis.\n'
00314 ' (c) 2002-2005 John B. Cole\n'
00315 ' http://pypedal.sourceforge.net/\n'
00316 ' jcole@aipl.arsusda.gov',
00317 'About PyPedal',
00318 wx.OK |
00319 wx.ICON_INFORMATION)
00320
00321 dlg.ShowModal()
00322 dlg.Destroy()
00323
00324
00325
00326
00327
00328
00329 def OnAppExit(self, event):
00330 changed = 0
00331 dlg = wx.MessageDialog(self,
00332 'Are you sure you want to exit PyPedal?',
00333 'Exit PyPedal?',
00334 wx.YES_NO |
00335 wx.ICON_QUESTION)
00336 if dlg.ShowModal() == wx.ID_YES:
00337 changed = 1
00338 dlg.Destroy()
00339 if changed:
00340 self.Destroy()
00341
00342
00343
00344
00345
00346
00347 def OnSettingsViewLog(self, event):
00348 pyp_gui_utils.UtilsViewLog(self)
00349
00350
00351
00352
00353
00354
00355 def ToDo(self,event):
00356 dlg = wx.MessageDialog(self,
00357 'This feature has not yet been implemented!',
00358 'PyPedal - Unknown Feature',
00359 wx.OK |
00360 wx.ICON_INFORMATION)
00361 dlg.ShowModal()
00362 dlg.Destroy()
00363
00364
00365
00366
00367
00368
00369 def OnGraphInbreed(self,event):
00370
00371 if hasattr(self,'_pedigree'):
00372 if not self._pedigree.kw['f_computed']:
00373 _ped_inbr = pyp_nrm.inbreeding(self._pedigree)
00374 pyp_db.loadPedigreeTable(self._pedigree)
00375 _coi_by_year = pyp_reports.meanMetricBy(self._pedigree,metric='fa',byvar='by')
00376 del(_coi_by_year[1900])
00377 _graph = pyp_graphics.plot_line_xy(_coi_by_year,gfilename='_coi_by_year',gtitle='Average coefficients of inbreeding',gxlabel='Birth year',gylabel='f')
00378 if _graph:
00379
00380
00381
00382
00383 dlg = pyp_gui_graphs.PyPedalGraphDialogInbreeding(self,ID_MAIN_TB,'PyPedal - View inbreeding by birth year')
00384 try:
00385 dlg.ShowModal()
00386 finally:
00387 dlg.Destroy()
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398 def OnMetricsInbreeding(self,event):
00399 pyp_gui_metrics.MetricsInbreeding(self)
00400
00401
00402
00403
00404
00405
00406
00407 def OnMetricsEffectiveFounders(self,event):
00408 pyp_gui_metrics.MetricsEffectiveFounders(self)
00409
00410 def OnSettingsOptions(self,event):
00411 pass
00412
00413 class MyApp(wx.App):
00414 def OnInit(self):
00415 frame = MainWindow(None, -1, "PyPedal")
00416 frame.Show(True)
00417 self.SetTopWindow(frame)
00418 return True
00419
00420 if __name__ == '__main__':
00421 app = MyApp(0)
00422 app.MainLoop()