"Fossies" - the Fresh Open Source Software Archive

Member "relax-5.0.0/gui/export_bmrb.py" (2 Dec 2019, 16635 Bytes) of package /linux/privat/relax-5.0.0.src.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "export_bmrb.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 4.1.3_vs_5.0.0.

    1 ###############################################################################
    2 #                                                                             #
    3 # Copyright (C) 2012 Edward d'Auvergne                                        #
    4 #                                                                             #
    5 # This file is part of the program relax (http://www.nmr-relax.com).          #
    6 #                                                                             #
    7 # This program is free software: you can redistribute it and/or modify        #
    8 # it under the terms of the GNU General Public License as published by        #
    9 # the Free Software Foundation, either version 3 of the License, or           #
   10 # (at your option) any later version.                                         #
   11 #                                                                             #
   12 # This program is distributed in the hope that it will be useful,             #
   13 # but WITHOUT ANY WARRANTY; without even the implied warranty of              #
   14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
   15 # GNU General Public License for more details.                                #
   16 #                                                                             #
   17 # You should have received a copy of the GNU General Public License           #
   18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
   19 #                                                                             #
   20 ###############################################################################
   21 
   22 # Module docstring.
   23 """The BMRB export wizard."""
   24 
   25 # Python module imports.
   26 import wx
   27 
   28 # relax module imports.
   29 from graphics import IMAGE_PATH, fetch_icon
   30 from gui.components.citations import Citations
   31 from gui.components.molecule import Molecule
   32 from gui.components.relax_data_meta import Relax_data_meta_list
   33 from gui.components.scripts import Scripts
   34 from gui.components.software import Software
   35 from gui.message import Missing_data
   36 from gui.fonts import font
   37 from gui.icons import Relax_icons
   38 from gui.misc import add_border, bitmap_setup
   39 from gui.string_conv import gui_to_str, str_to_gui
   40 from gui.uf_objects import Uf_storage; uf_store = Uf_storage()
   41 from pipe_control.mol_res_spin import molecule_loop
   42 from pipe_control.pipes import cdp_name, pipe_names, switch
   43 from status import Status; status = Status()
   44 
   45 
   46 class Export_bmrb_window(wx.Frame):
   47     """The BMRB export window."""
   48 
   49     def __init__(self, gui):
   50         """Set up the export window.
   51 
   52         @param gui:     The GUI object.
   53         @type gui:      wx.Frame instance
   54         """
   55 
   56         # The window style.
   57         style = wx.DEFAULT_FRAME_STYLE
   58         if not status.debug and status.wx_info["os"] != 'darwin':
   59             style = style | wx.MAXIMIZE
   60 
   61         # Initialise the base class, setting the main GUI window as the parent.
   62         super(Export_bmrb_window, self).__init__(gui, -1, style=style)
   63 
   64         # Some default values.
   65         self.size = (1200, 900)
   66         self.size_min = (900, 700)
   67         self.border = 5
   68         self.spacer = 10
   69         self.button_size = (200, 40)
   70         self.button_spacing = 10
   71         self.main_spacing = 20
   72 
   73         # Set up the frame.
   74         sizer = self.setup_frame()
   75 
   76         # Add the header.
   77         self.add_header(sizer)
   78 
   79         # Top spacing.
   80         sizer.AddSpacer(10)
   81 
   82         # Add the data pipe selection element.
   83         self.add_pipe(sizer)
   84 
   85         # Spacing.
   86         sizer.AddSpacer(self.main_spacing)
   87 
   88         # Add the relaxation data metadata list GUI element.
   89         self.relax_data = Relax_data_meta_list(parent=self.main_panel, box=sizer, id='BMRB export', proportion=2)
   90 
   91         # Spacing.
   92         sizer.AddSpacer(self.main_spacing)
   93 
   94         # Add the molecule GUI element.
   95         self.molecule = Molecule(parent=self.main_panel, box=sizer, id='BMRB export', proportion=1)
   96 
   97         # Spacing.
   98         sizer.AddSpacer(self.main_spacing)
   99 
  100         # Create a horizontal layout for the software, script and citations GUI elements.
  101         sub_sizer = wx.BoxSizer(wx.HORIZONTAL)
  102 
  103         # Add the software GUI element.
  104         self.software = Software(parent=self.main_panel, box=sub_sizer, id='BMRB export', proportion=1)
  105 
  106         # Vertical spacing.
  107         sub_sizer.AddSpacer(self.main_spacing)
  108 
  109         # Add the scripts GUI element.
  110         self.scripts = Scripts(parent=self.main_panel, box=sub_sizer, id='BMRB export', proportion=1)
  111 
  112         # Vertical spacing.
  113         sub_sizer.AddSpacer(self.main_spacing)
  114 
  115         # Add the citation GUI element.
  116         self.citation = Citations(parent=self.main_panel, box=sub_sizer, id='BMRB export', proportion=1)
  117 
  118         # Add the sizer.
  119         sizer.Add(sub_sizer, 2, wx.ALL|wx.EXPAND, 0)
  120 
  121         # Bottom spacing.
  122         sizer.AddSpacer(10)
  123 
  124         # Add the buttons.
  125         self.add_buttons(sizer)
  126 
  127         # Open the window.
  128         if status.show_gui:
  129             self.Show()
  130 
  131 
  132     def action_cancel(self, event=None):
  133         """Cancel the export.
  134 
  135         @keyword event: The wx event.
  136         @type event:    wx event
  137         """
  138 
  139         # Close the window.
  140         self.Close()
  141 
  142 
  143     def action_export(self, event=None):
  144         """Write out the NMR-STAR formatted data.
  145 
  146         @keyword event: The wx event.
  147         @type event:    wx event
  148         """
  149 
  150         # Checks.
  151         missing = self.is_complete()
  152 
  153         # Missing data.
  154         if len(missing):
  155             Missing_data(missing, parent=self)
  156             return
  157 
  158         # Execute the user function.
  159         uf_store['bmrb.write'](wx_parent=self, wx_wizard_sync=True, wx_wizard_modal=True)
  160 
  161         # Close the window.
  162         self.Close()
  163 
  164 
  165     def action_preview(self, event=None):
  166         """Preview the NMR-STAR formatted data.
  167 
  168         @keyword event: The wx event.
  169         @type event:    wx event
  170         """
  171 
  172         # Execute the user function.
  173         uf_store['bmrb.display'](wx_parent=self)
  174 
  175 
  176     def add_buttons(self, sizer):
  177         """Build and add the bottom buttons.
  178 
  179         @param sizer:   The sizer element to pack the buttons into.
  180         @type sizer:    wx.Sizer instance
  181         """
  182 
  183         # Button sizer.
  184         button_sizer = wx.BoxSizer(wx.HORIZONTAL)
  185         sizer.Add(button_sizer, 0, wx.ALIGN_RIGHT, 0)
  186 
  187         # Preview button.
  188         button = wx.lib.buttons.ThemedGenBitmapTextButton(self.main_panel, -1, None, " Preview")
  189         button.SetBitmapLabel(wx.Bitmap(fetch_icon('oxygen.actions.document-preview', "32x32"), wx.BITMAP_TYPE_ANY))
  190         button.SetFont(font.normal)
  191         button.SetMinSize(self.button_size)
  192         button_sizer.Add(button, 0, 0, 0)
  193         self.Bind(wx.EVT_BUTTON, self.action_preview, button)
  194         button.SetToolTip(wx.ToolTip("Preview the NMR-STAR formatted data."))
  195 
  196         # Spacing.
  197         button_sizer.AddSpacer(self.button_spacing)
  198 
  199         # Export button.
  200         button = wx.lib.buttons.ThemedGenBitmapTextButton(self.main_panel, -1, None, " Export")
  201         button.SetBitmapLabel(wx.Bitmap(fetch_icon('relax.bmrb', "32x32"), wx.BITMAP_TYPE_ANY))
  202         button.SetFont(font.normal)
  203         button.SetMinSize(self.button_size)
  204         button_sizer.Add(button, 0, 0, 0)
  205         self.Bind(wx.EVT_BUTTON, self.action_export, button)
  206         button.SetToolTip(wx.ToolTip("Create the NMR-STAR formatted file for BMRB deposition."))
  207 
  208         # Spacing.
  209         button_sizer.AddSpacer(self.button_spacing)
  210 
  211         # Cancel button.
  212         button = wx.lib.buttons.ThemedGenBitmapTextButton(self.main_panel, -1, None, " Cancel")
  213         button.SetBitmapLabel(wx.Bitmap(fetch_icon('oxygen.actions.dialog-cancel', "32x32"), wx.BITMAP_TYPE_ANY))
  214         button.SetFont(font.normal)
  215         button.SetMinSize(self.button_size)
  216         button_sizer.Add(button, 0, 0, 0)
  217         self.Bind(wx.EVT_BUTTON, self.action_cancel, button)
  218         button.SetToolTip(wx.ToolTip("Cancel the BMRB export."))
  219 
  220 
  221     def add_header(self, sizer):
  222         """Build and add the header to the sizer.
  223 
  224         @param sizer:   The sizer element to pack the header into.
  225         @type sizer:    wx.Sizer instance
  226         """
  227 
  228         # Create a horizontal layout.
  229         sub_sizer = wx.BoxSizer(wx.HORIZONTAL)
  230 
  231         # Left spacing.
  232         sub_sizer.AddStretchSpacer(3)
  233 
  234         # Add the BMRB logo (left side).
  235         logo = wx.StaticBitmap(self.main_panel, -1, bitmap_setup(IMAGE_PATH+"bmrb_100x100.png"))
  236         sub_sizer.Add(logo, 0, wx.TOP|wx.ALIGN_CENTER_HORIZONTAL, 0)
  237 
  238         # Spacing.
  239         sub_sizer.AddStretchSpacer()
  240 
  241         # The text sizer.
  242         text_sizer = wx.BoxSizer(wx.VERTICAL)
  243 
  244         # The title.
  245         text = wx.StaticText(self.main_panel, -1, 'Data export for BMRB deposition', style=wx.ALIGN_LEFT)
  246         text.SetFont(font.title)
  247         text_sizer.Add(text, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
  248 
  249         # Spacing.
  250         text_sizer.AddSpacer(15)
  251 
  252         # The text.
  253         main_text = 'This wizard will help in executing all of the relevant user functions required to convert the contents of the selected data pipe to the NMR-STAR format for deposition within the BioMagResBank.  Note that this is currently only for the deposition of model-free analysis results or simple NMR relaxation data.'
  254         text = wx.StaticText(self.main_panel, -1, main_text, style=wx.ALIGN_LEFT)
  255         text.Wrap(600)
  256         text.SetFont(font.normal)
  257         text_sizer.Add(text, 0, 0, 0)
  258 
  259         # Add the text sizer.
  260         sub_sizer.Add(text_sizer, 0, 0, 0)
  261 
  262         # Spacing.
  263         sub_sizer.AddStretchSpacer()
  264 
  265         # Add the BMRB logo (right side).
  266         logo = wx.StaticBitmap(self.main_panel, -1, bitmap_setup(IMAGE_PATH+"bmrb_100x100.png"))
  267         sub_sizer.Add(logo, 0, wx.TOP|wx.ALIGN_CENTER_HORIZONTAL, 0)
  268 
  269         # Right spacing.
  270         sub_sizer.AddStretchSpacer(3)
  271 
  272         # Add the sizer.
  273         sizer.Add(sub_sizer, 0, wx.ALL|wx.EXPAND, 0)
  274 
  275         # A line with spacing.
  276         sizer.AddSpacer(10)
  277         sizer.Add(wx.StaticLine(self.main_panel, -1), 0, wx.EXPAND|wx.ALL, 0)
  278         sizer.AddSpacer(10)
  279 
  280 
  281     def add_pipe(self, sizer):
  282         """Build and add the data pipe selection element.
  283 
  284         @param sizer:   The sizer element to pack the buttons into.
  285         @type sizer:    wx.Sizer instance
  286         """
  287 
  288         # A sizer for the element.
  289         pipe_sizer = wx.BoxSizer(wx.HORIZONTAL)
  290         sizer.Add(pipe_sizer, 0, wx.ALIGN_LEFT, 0)
  291 
  292         # The pipe text.
  293         text = wx.StaticText(self.main_panel, -1, ' The data pipe to export:  ', style=wx.ALIGN_LEFT)
  294         tooltip = "The name of the data pipe to export to NMR-STAR format for BMRB export."
  295         text.SetFont(font.normal)
  296         text.SetToolTip(wx.ToolTip(tooltip))
  297         pipe_sizer.Add(text, 0, wx.ALIGN_CENTER_VERTICAL, 0)
  298 
  299         # Spacing.
  300         pipe_sizer.AddSpacer(20)
  301 
  302         # The pipe selection.
  303         self.pipe_name = wx.ComboBox(self.main_panel, -1, "", style=wx.CB_DROPDOWN|wx.CB_READONLY, choices=[])
  304         self.pipe_name.SetToolTip(wx.ToolTip(tooltip))
  305         self.Bind(wx.EVT_COMBOBOX, self.update_pipes, self.pipe_name)
  306         pipe_sizer.Add(self.pipe_name, 0, wx.ALIGN_CENTER_VERTICAL, 0)
  307 
  308         # Update the pipe selection.
  309         self.update_pipes()
  310 
  311 
  312     def handler_close(self, event):
  313         """Event handler for the close window action.
  314 
  315         @param event:   The wx event.
  316         @type event:    wx event
  317         """
  318 
  319         # Unregister the observers.
  320         self.observer_register(remove=True)
  321 
  322         # Close the window.
  323         event.Skip()
  324 
  325 
  326     def is_complete(self):
  327         """Determine if the data input is complete.
  328 
  329         @return:    A list of all the missing components.
  330         @rtype:     list of str
  331         """
  332 
  333         # Initialise.
  334         missing = []
  335 
  336         # Relaxation data metadata.
  337         if hasattr(cdp, 'ri_ids'):
  338             # Loop over the data.
  339             for i in range(len(cdp.ri_ids)):
  340                 # Check the peak intensity types.
  341                 if not hasattr(cdp, 'exp_info') or not hasattr(cdp.exp_info, 'peak_intensity_type') or not cdp.ri_ids[i] in cdp.exp_info.peak_intensity_type:
  342                     missing.append("The peak intensity type for the relaxation data ID '%s'." % cdp.ri_ids[i])
  343 
  344                 # Check the temperature calibration methods.
  345                 if not hasattr(cdp, 'exp_info') or not hasattr(cdp.exp_info, 'temp_calibration') or not cdp.ri_ids[i] in cdp.exp_info.temp_calibration:
  346                     missing.append("The temperature calibration method for the relaxation data ID '%s'." % cdp.ri_ids[i])
  347 
  348                 # Check the temperature control methods.
  349                 if not hasattr(cdp, 'exp_info') or not hasattr(cdp.exp_info, 'temp_control') or not cdp.ri_ids[i] in cdp.exp_info.temp_control:
  350                     missing.append("The temperature control method for the relaxation data ID '%s'." % cdp.ri_ids[i])
  351 
  352 
  353         # Loop over the molecules.
  354         for mol, mol_id in molecule_loop(return_id=True):
  355             # No name.
  356             if mol.name == None:
  357                 missing.append("The name of the molecule for %s." % mol_id)
  358                 continue
  359 
  360             # No molecule type.
  361             if not hasattr(mol, 'type') or mol.type == None:
  362                 missing.append("The type of the molecule %s." % mol_id)
  363 
  364             # No thiol state.
  365             if not hasattr(cdp, 'exp_info') or not hasattr(cdp.exp_info, 'thiol_state'):
  366                 missing.append("The thiol state of the molecule %s." % mol_id)
  367 
  368         # Return the list of missing data.
  369         return missing
  370 
  371 
  372     def observer_register(self, remove=False):
  373         """Register and unregister methods with the observer objects.
  374 
  375         @keyword remove:    If set to True, then the methods will be unregistered.
  376         @type remove:       False
  377         """
  378 
  379         # Register.
  380         if not remove:
  381             status.observers.pipe_alteration.register('BMRB export', self.update_pipes, method_name='update_pipes')
  382 
  383         # Unregister.
  384         else:
  385             # The class methods.
  386             status.observers.pipe_alteration.unregister('BMRB export')
  387 
  388             # The embedded objects methods.
  389             self.relax_data.observer_register(remove=True)
  390             self.molecule.observer_register(remove=True)
  391             self.software.observer_register(remove=True)
  392             self.scripts.observer_register(remove=True)
  393             self.citation.observer_register(remove=True)
  394 
  395 
  396     def setup_frame(self):
  397         """Set up the relax controller frame.
  398         @return:    The sizer object.
  399         @rtype:     wx.Sizer instance
  400         """
  401 
  402         # Set the frame title.
  403         self.SetTitle("BMRB export window")
  404 
  405         # Set up the window icon.
  406         self.SetIcons(Relax_icons())
  407 
  408         # Place all elements within a panel (to remove the dark grey in MS Windows).
  409         self.main_panel = wx.Panel(self, -1)
  410 
  411         # Use a grid sizer for packing the main elements.
  412         main_sizer = wx.BoxSizer(wx.VERTICAL)
  413         self.main_panel.SetSizer(main_sizer)
  414 
  415         # Build the central sizer, with borders.
  416         sizer = add_border(main_sizer, border=self.border, packing=wx.VERTICAL)
  417 
  418         # Close the window cleanly (unregistering observers).
  419         self.Bind(wx.EVT_CLOSE, self.handler_close)
  420 
  421         # Set the default size of the controller.
  422         self.SetSize(self.size)
  423         self.SetMinSize(self.size_min)
  424 
  425         # Centre the frame.
  426         self.Centre()
  427 
  428         # Return the central sizer.
  429         return sizer
  430 
  431 
  432     def update_pipes(self, event=None):
  433         """Update the spin view data pipe selector.
  434 
  435         @keyword event: The wx event.
  436         @type event:    wx event
  437         """
  438 
  439         # Change the cursor to busy.
  440         wx.BeginBusyCursor()
  441 
  442         # Init.
  443         pipe_switch = False
  444 
  445         # The selected pipe.
  446         if event:
  447             # The name of the selected pipe.
  448             pipe = gui_to_str(self.pipe_name.GetString(event.GetSelection()))
  449 
  450             # A pipe change.
  451             if pipe != cdp_name():
  452                 pipe_switch = True
  453         else:
  454             pipe = cdp_name()
  455         if not pipe:
  456             pipe = ''
  457 
  458         # Clear the previous data.
  459         self.pipe_name.Clear()
  460 
  461         # The list of pipe names.
  462         for name in pipe_names():
  463             self.pipe_name.Append(str_to_gui(name))
  464 
  465         # Switch data pipes.
  466         if pipe_switch:
  467             switch(pipe)
  468 
  469         # Set the pipe name to the cdp.
  470         self.pipe_name.SetValue(str_to_gui(pipe))
  471 
  472         # Reset the cursor.
  473         if wx.IsBusy():
  474             wx.EndBusyCursor()