class AnnealingSettingsTab(object):
    '''This class describes the tab in the GUI where the user
       can change setting that govern the monte carlo / annleaing
       procedure. This also includes which information from the ccpn
       analysis project is used and which information is
       ignored. This includes:
           * present sequential assignments
           * tentative assignments
           * amino acid type information
           * whether to include untyped spin systems
           * assignments to peak dimensions
       ALso the chain can be selected here.
       Furthermore the user can set the temperature
       regime of the annealing, the amount of times the procedure
       is repeated to obtain statistics. The fraction of peaks
       that is left out in each run to diversify the results,
       the treshhold score for amino acid typing and the treshhold
       collabelling for a peak to be expected.
    '''

    def __init__(self, parent, frame):
        '''Init. args: parent: the guiElement that this
                               tab is part of.
                       frame:  the frame this part of the
                               GUI lives in.
        '''

        self.guiParent = parent
        self.frame = frame
        self.project = parent.project
        self.nmrProject = parent.nmrProject

        self.minIsoFrac = 0.1
        self.leavePeaksOutFraction = 0.0
        self.minTypeScore = 1.0
        self.chain = None
        self.amountOfRepeats = 10
        self.amountOfSteps = 10000
        self.acceptanceConstantList = [0.0, 0.01, 0.015, 0.022,
                                       0.033, 0.050, 0.075, 0.113,
                                       0.170, 0.256, 0.384, 0.576,
                                       0.864, 1.297, 1.946, 2.919,
                                       4.378, 6.568, 9.852, 14.77,
                                       22.16, 33.25]
        self.energyDataSets = [[]]
        self.residues = []
        self.body()

    def body(self):
        '''describes the body of this tab. It bascically consists
           of some field to fill out for the user at the top and
           a ScrolledGraph that shows the progess of the annealing
           procedure a the bottom.
        '''

        frame = self.frame

        # frame.expandGrid(13,0)
        frame.expandGrid(15, 1)
        row = 0

        text = 'Calculate Assignment Suggestions'
        command = self.runCalculations
        self.startButton = Button(frame, command=command, text=text)
        self.startButton.grid(row=row, column=0, sticky='nsew', columnspan=2)

        row += 1

        Label(frame, text='Amount of runs: ', grid=(row, 0))
        tipText = 'The amount of times the whole optimization procedure is performed, each result is safed'
        self.repeatEntry = IntEntry(frame, grid=(row, 1), width=7, text=10,
                                    returnCallback=self.updateRepeatEntry,
                                    tipText=tipText, sticky='nsew')
        self.repeatEntry.bind('<Leave>', self.updateRepeatEntry, '+')

        row += 1

        Label(frame, text='Temperature regime: ', grid=(row, 0))
        tipText = 'This list of numbers govern the temperature steps during the annealing, every number represents 1/(kb*t), where kb is the Boltzmann constant and t the temperature of one step.'
        self.tempEntry = Entry(frame, text=map(str, self.acceptanceConstantList), width=64,
                               grid=(row, 1), isArray=True, returnCallback=self.updateAcceptanceConstantList,
                               tipText=tipText, sticky='nsew')

        row += 1

        Label(frame, text='Amount of attempts per temperature:', grid=(row, 0))
        tipText = 'The amount of attempts to switch the position of two spinsystems in the sequence are performed for each temperature point'
        self.NAStepEntry = IntEntry(frame, grid=(row, 1), width=7, text=10000,
                                    returnCallback=self.updateStepEntry,
                                    tipText=tipText, sticky='nsew')
        self.NAStepEntry.bind('<Leave>', self.updateStepEntry, '+')

        row += 1

        Label(frame, text='Fraction of peaks to leave out:', grid=(row, 0))
        tipText = 'In each run a fraction of the peaks can be left out of the optimization, thereby increasing the variability in the outcome and reducing false negatives. In each run this will be different randomly chosen sub-set of all peaks. 0.1 (10%) can be a good value.'
        self.leaveOutPeaksEntry = FloatEntry(frame, grid=(row, 1), width=7, text=0.0,
                                             returnCallback=self.updateLeavePeaksOutEntry,
                                             tipText=tipText, sticky='nsew')
        self.leaveOutPeaksEntry.bind(
            '<Leave>', self.updateLeavePeaksOutEntry, '+')

        row += 1

        Label(frame, text='Minmal amino acid typing score:', grid=(row, 0))
        tipText = 'If automatic amino acid typing is selected, a cut-off value has to set. Every amino acid type that scores higher than the cut-off is taken as a possible type. This is the same score as can be found under resonance --> spin systems --> predict type. Value should be between 0 and 100'
        self.minTypeScoreEntry = FloatEntry(frame, grid=(row, 1), width=7, text=1.0,
                                            returnCallback=self.updateMinTypeScoreEntry,
                                            tipText=tipText, sticky='nsew')
        self.minTypeScoreEntry.bind(
            '<Leave>', self.updateMinTypeScoreEntry, '+')

        row += 1

        Label(frame, text='Minimal colabelling fraction:', grid=(row, 0))
        tipText = 'The minimal amount of colabelling the different nuclei should have in order to still give rise to a peak.'
        self.minLabelEntry = FloatEntry(frame, grid=(row, 1), width=7, text=0.1,
                                        returnCallback=self.updateMinLabelEntry,
                                        tipText=tipText, sticky='nsew')
        self.minLabelEntry.bind('<Leave>', self.updateMinLabelEntry, '+')

        row += 1

        Label(frame, text='Use sequential assignments:', grid=(row, 0))
        tipText = 'When this option is select the present sequential assignments will be kept in place'
        self.useAssignmentsCheck = CheckButton(
            frame, selected=True, tipText=tipText, grid=(row, 1))

        row += 1

        Label(frame, text='Use tentative assignments:', grid=(row, 0))
        tipText = 'If a spin system has tentative assignments this can be used to narrow down the amount of possible sequential assignments.'
        self.useTentativeCheck = CheckButton(
            frame, selected=True, tipText=tipText, grid=(row, 1))

        row += 1

        Label(frame, text='Use amino acid types:', grid=(row, 0))
        tipText = 'Use amino acid types of the spin systems. If this option is not checked the spin systems are re-typed, only resonance names and frequencies are used'
        self.useTypeCheck = CheckButton(
            frame, selected=True, tipText=tipText, grid=(row, 1))

        row += 1

        Label(frame, text='Include untyped spin systems:', grid=(row, 0))
        tipText = 'Also include spin system that have no type information. Amino acid typing will be done on the fly.'
        self.useAlsoUntypedSpinSystemsCheck = CheckButton(
            frame, selected=True, tipText=tipText, grid=(row, 1))

        row += 1

        Label(frame, text='Use dimensional assignments:', grid=(row, 0))
        tipText = 'If one or more dimensions of a peak is already assigned, assume that this assignment is the only option. If not the check the program will consider all possibilities for the assignment of the dimension.'
        self.useDimensionalAssignmentsCheck = CheckButton(
            frame, selected=True, tipText=tipText, grid=(row, 1))

        row += 1

        Label(frame, text='Chain:', grid=(row, 0))
        self.molPulldown = PulldownList(
            frame, callback=self.changeMolecule, grid=(row, 1))
        self.updateChains()

        row += 1

        Label(frame, text='Residue ranges: ', grid=(row, 0))
        tipText = 'Which residues should be included. Example: "10-35, 62-100, 130".'
        self.residueRangeEntry = Entry(frame, text=None, width=64,
                                       grid=(row, 1), isArray=True, returnCallback=self.updateResidueRanges,
                                       tipText=tipText, sticky='nsew')
        self.updateResidueRanges(fromChain=True)

        row += 1

        self.energyPlot = ScrolledGraph(frame, symbolSize=2, width=600,
                                        height=200, title='Annealing',
                                        xLabel='temperature step', yLabel='energy')
        self.energyPlot.grid(row=row, column=0, columnspan=2, sticky='nsew')

    def runCalculations(self):
        '''Run all calculations. Also triggers the disabling of
           some buttons and fields.
        '''

        self.startButton.disable()
        self.disableIllegalButtonsAfterPrecalculations()
        self.guiParent.connector.runAllCalculations()
        self.startButton.configure(text='More runs')
        self.startButton.enable()

    def disableIllegalButtonsAfterPrecalculations(self):
        '''Disable buttons and field the user can not alter
           any longer after the model is set up and the
           'pre-calculations' have finished.
           This is done because this part of the calculation
           should only be run once. All settings that would
           be changed after this point will not have any influence.
        '''

        illegalButtons = [self.minTypeScoreEntry, self.minLabelEntry,
                          self.useAlsoUntypedSpinSystemsCheck, self.useAssignmentsCheck,
                          self.useTypeCheck, self.useDimensionalAssignmentsCheck,
                          self.useTentativeCheck]

        for illegalButton in illegalButtons:
            illegalButton.configure(state='disabled')

        self.molPulldown.disable()

    def getChainName(self, chain):
        '''Get the name for a chain.
               args: chain: ccpn analysis
                            chain object
               returns: chain name
        '''

        return '%s:%s (%s)' % (chain.molSystem.code, chain.code, chain.molecule.molType)

    def getChains(self):
        '''Get all chains present in the project.
               returns: list of ccpn analysis chain objects
        '''
        chains = []
        if self.project:
            for molSystem in self.project.sortedMolSystems():
                for chain in molSystem.sortedChains():
                    if chain.residues:
                        chains.append(chain)

        return chains

    def updateChains(self, *opt):
        '''Updates the list of chains if a new one is added
           to or deleted from the project. Updates the
           pull down list where a chain can be selected.
        '''

        index = 0
        texts = []
        chains = self.getChains()
        chain = self.chain

        if chains:
            if chain not in chains:
                chain = chains[0]

            texts = [self.getChainName(c) for c in chains]
            index = chains.index(chain)

        else:
            chain = None

        self.molPulldown.setup(texts, chains, index)

        if chain is not self.chain:
            self.chain = chain

    def changeMolecule(self, chain):
        '''Select a molecular chain.'''

        if chain is not self.chain:
            self.chain = chain
            self.updateResidueRanges(fromChain=True)

    def updateStepEntry(self, event=None):
        '''Update the value and entry that sets the amount of
           steps per temperature point.
        '''

        value = self.NAStepEntry.get()
        if value == self.amountOfSteps:
            return
        if value < 1:
            self.NAStepEntry.set(1)
            self.amountOfSteps = 1
        else:
            self.amountOfSteps = value
            self.NAStepEntry.set(value)

    def updateRepeatEntry(self, event=None):
        '''Update the value and entry of that sets
           the amount of times the whole annealing
           procedure is repeated in order
           to obtain statistics.
        '''

        value = self.repeatEntry.get()

        if value == self.amountOfRepeats:
            return
        if value < 1:
            self.repeatEntry.set(1)
            self.amountOfRepeats = 1
        else:
            self.amountOfRepeats = value
            self.repeatEntry.set(value)

    def updateMinTypeScoreEntry(self, event=None):
        '''Updates the value and the entry for the
           treshhold value for amino acid typing.
        '''

        value = self.minTypeScoreEntry.get()

        if value == self.minTypeScore:
            return
        if value < 0:
            self.minTypeScoreEntry.set(0.0)
            self.minTypeScore = 0.0
        elif value > 100:
            self.minTypeScoreEntry.set(100.0)
            self.minTypeScore = 100.0
        else:
            self.minTypeScoreEntry.set(value)
            self.minTypeScore = value

    def updateMinLabelEntry(self, event=None):
        '''Updates the minimum colabelling fraction
           for which a peak is expected to be present
           in the spectra.
        '''

        value = self.minLabelEntry.get()

        if value == self.minIsoFrac:
            return
        if value < 0:
            self.minIsoFrac = 0.0
            self.minLabelEntry.set(0.0)
        elif value > 1:
            self.minIsoFrac = 1.0
            self.minLabelEntry.set(1.0)
        else:
            self.minIsoFrac = value
            self.minLabelEntry.set(value)

    def updateLeavePeaksOutEntry(self, event=None):
        '''Updates the value and entry of the fraction
           of peaks that should be left out in each
           run in order to diversify the results.
        '''

        value = self.leaveOutPeaksEntry.get()

        if value == self.leavePeaksOutFraction:
            return
        if value < 0:
            self.leavePeaksOutFraction = 0.0
            self.leaveOutPeaksEntry.set(0.0)
        elif value > 1:
            self.leavePeaksOutFraction = 1.0
            self.leaveOutPeaksEntry.set(1.0)
        else:
            self.leavePeaksOutFraction = value
            self.leaveOutPeaksEntry.set(value)

    def updateAcceptanceConstantList(self, event=None):
        '''Updates the list with constants that are used
           during the monte carlo procedure to decide whether
           a changed is accepted or not.
        '''

        acList = self.tempEntry.get()
        newList = []

        for constant in acList:

            try:

                number = float(constant)
                newList.append(number)

            except ValueError:

                string = constant + \
                    ' in temperature constants is not a number.'

                showWarning('Not A Number', string, parent=self.guiParent)

                return False

        self.acceptanceConstantList = newList

        return True

    def updateResidueRanges(self, event=None, fromChain=False):

        self.residues = set()

        subRanges = self.residueRangeEntry.get()
        if not subRanges or fromChain:
            self.residues = set(self.chain.residues)
            residues = self.chain.sortedResidues()
            text = '{}-{}'.format(residues[0].seqCode, residues[-1].seqCode)
            self.residueRangeEntry.set(text=text)
            return

        for subRange in subRanges:
            indeces = subRange.split('-')
            start = int(indeces[0])
            stop = int(indeces[-1]) + 1
            for seqCode in range(start, stop):
                residue = self.chain.findFirstResidue(seqCode=seqCode)
                if not residue:
                    showWarning('Residue out of range.',
                                'There is no residue at position {}'.format(seqCode),
                                parent=self.guiParent)
                    self.residues = set()
                    return
                self.residues.add(residue)

    def addEnergyPoint(self, energy, time):
        '''Adds a point to the graph that shows the progress
           of the annealling procedure.
               args: energy: the y-value
                     time:   the x-value
        '''

        point = (time, energy * -1)

        # This means one run has finished
        if len(self.energyDataSets[-1]) / (len(self.acceptanceConstantList) + 1):

            self.energyDataSets.append([point])

        else:

            self.energyDataSets[-1].append(point)

        colors = colorSeries
        Ncolors = len(colors)
        NdataSets = len(self.energyDataSets)
        colorList = (NdataSets / Ncolors) * colors + \
            colors[:NdataSets % Ncolors]
        self.energyPlot.update(dataSets=self.energyDataSets,
                               dataColors=colorList)

        # Forcing the graph to draw, eventhough calculations
        # are still running. Only do this with high numbers of
        # steps, otherwise drawing takes longer than annealling.
        if self.amountOfSteps >= 100000:

            self.energyPlot.draw()
示例#2
0
class DataFileImportFrame(Frame):
    """
  TODO: should I also ask for sample conditions info with chemical shifts? Or do in ECI - possible? Check!
  """

    # TODO should go where?
    email = '*****@*****.**'

    defaultSelectText = "Select file."
    fontOkColor = 'green3'
    fontCheckColor = 'orange3'
    fontBadColor = 'red3'
    fontDefaultColor = 'black'

    def __init__(self, parent, basePopup, *args, **kw):

        #
        # Variable initialisation
        #

        self.fcWrapper = basePopup.fcWrapper
        self.project = basePopup.project
        self.basePopup = basePopup

        # TODO necessary?
        if self.project:
            self.nmrProject = self.project.currentNmrProject
            self.entry = self.project.currentNmrEntryStore.findFirstEntry()
            if not self.entry:
                self.entry = self.project.currentNmrEntryStore.newEntry(
                    name=self.project.name)
        else:
            self.nmrProject = None

        self.sequenceCoordinatesLoaded = False
        self.shiftsLoaded = False
        self.linkResDone = False

        self.currentShiftList = None
        self.shiftListChainPairs = []

        self.moleculeList = []
        self.moleculeDict = {}

        #
        # Frame setup
        #

        Frame.__init__(self, parent, **kw)

        self.grid_columnconfigure(1, weight=1)
        self.grid_rowconfigure(1, weight=1)

        options = ['Import', 'Deposition']

        tabbedFrame = TabbedFrame(self,
                                  options=options,
                                  callback=self.selectTab)
        tabbedFrame.grid(row=1, column=0, columnspan=2, sticky='nsew')

        self.tabbedFrame = tabbedFrame
        frameA, frameD = tabbedFrame.frames

        #
        # Main
        #

        frameA.grid_columnconfigure(0, weight=1)
        #frameA.grid_columnconfigure(1, weight=1) # Change to 2 if want 2 columns
        frameA.grid_rowconfigure(12, weight=1)
        #frameA.grid_rowconfigure(12, weight=1)

        row = 0

        div = LabelDivider(
            frameA,
            text=
            'Select the full coordinate file or a sequence file (with info for a single molecule).',
            justify='center',
            grid=(row, 0),
            gridSpan=(1, 1))

        row += 1

        self.sequenceCoordinatesImport = Button(
            frameA,
            text=self.defaultSelectText,
            command=self.importSequenceOrCoords,
            foreground=self.fontDefaultColor)
        self.sequenceCoordinatesImport.grid(row=row, column=0, sticky='ew')

        row += 1

        label = Label(frameA, text="")
        label.grid(row=row, column=0, sticky='ew')

        row += 1

        div = LabelDivider(
            frameA,
            text='Select the molecule relevant for your chemical shift file.',
            justify='center',
            grid=(row, 0),
            gridSpan=(1, 1))

        row += 1

        self.moleculeSelect = Label(
            frameA,
            text="None available yet - import valid file first",
            foreground=self.fontBadColor)
        self.moleculeSelect.grid(row=row, column=0, sticky='ew')
        self.moleculeSelectRow = row

        row += 1

        label = Label(frameA, text="")
        label.grid(row=row, column=0, sticky='ew')

        row += 1

        div = LabelDivider(
            frameA,
            text=
            'Select a chemical shift file with values only for the above molecule.',
            justify='center',
            grid=(row, 0),
            gridSpan=(1, 1))

        row += 1

        self.shiftImport = Button(frameA,
                                  text=self.defaultSelectText,
                                  command=self.importShifts,
                                  foreground=self.fontDefaultColor)
        self.shiftImport.grid(row=row, column=0, sticky='ew')

        row += 1

        label = Label(frameA, text="")
        label.grid(row=row, column=0, sticky='ew')

        row += 1

        div = LabelDivider(
            frameA,
            text='Consistency check between molecule and shift information.',
            justify='center',
            grid=(row, 0),
            gridSpan=(2, 1))

        row += 1

        self.linkResCheckInfo = Label(frameA, text='')
        self.linkResCheckInfo.grid(row=row, column=0, sticky='ew')

        row += 1

        div = Separator(frameA, grid=(row, 0), gridSpan=(1, 1))

        row += 1

        texts = ['Import new sequence', 'Import new set of shifts']
        commands = [self.resetSequenceImport, self.resetShiftImport]

        self.mainButtons = ButtonList(frameA, texts=texts, commands=commands)
        self.mainButtons.grid(row=row, column=0, columnspan=2, sticky='ew')
        self.mainButtons.buttons[0].config(foreground=self.fontDefaultColor)

        #print row

        self.frameA = frameA

        #
        # Not in use...
        #

        #frameX.grid_columnconfigure(0, weight=1)
        #frameX.grid_columnconfigure(1, weight=1)
        #frameX.grid_rowconfigure(1, weight=1)
        #frameX.grid_rowconfigure(3, weight=1)

        #
        # Deposition, is updated after each successful import run.
        #

        frameD.grid_columnconfigure(0, weight=1)
        frameD.grid_rowconfigure(5, weight=1)

        self.frameD = frameD

        row = 0

        div = LabelDivider(frameD,
                           text='Imported data.',
                           justify='center',
                           grid=(row, 0),
                           gridSpan=(1, 2))

        row += 1

        self.depositionImportText = "\nImported %d shift list(s) for a total of %d shifts.\n\nImported %d molecule(s) and %d chain(s).\n\nImported %d model(s) for a total of %d atom coordinates.\n\nLinked %.2f%% of imported NMR information to %d chain(s)."
        self.depositionImportLoc = (row, 0)

        # These used for setting text above...
        self.depositionImportNums = [0, 0, 0, 0, 0, 0, 0.0, 0]
        self.importedShiftLists = []
        self.connectedChains = []

        self.depositionImportLabel = Label(frameD,
                                           text=self.depositionImportText %
                                           tuple(self.depositionImportNums),
                                           foreground=self.fontBadColor)
        self.depositionImportLabel.grid(row=row, column=0, sticky='ew')

        row += 1

        label = Label(frameD, text="")
        label.grid(row=row, column=0, sticky='ew')

        #
        # Finalize the import part, proceed to ECI.
        #

        row += 1

        div = LabelDivider(
            frameD,
            text=
            'Import completed, save project and start Entry Completion Interface.',
            justify='center',
            grid=(row, 0),
            gridSpan=(1, 1))

        row += 1

        self.eciStart = Button(frameD,
                               text="Finalise import",
                               command=self.finaliseImport,
                               foreground=self.fontBadColor)
        self.eciStart.grid(row=row, column=0, sticky='ew')

    def finaliseImport(self):

        if not self.depositionImportNums[6]:
            showWarning('Failure',
                        'Need connected molecule and shift information first')
            return

        if showYesNo(
                "Save project and continue annotation",
                "Are you sure you want to save this project and continue?",
                parent=self):

            if self.depositionImportNums[5] or showYesNo(
                    "No coordinates",
                    "No coordinates are available - are you sure you want to continue?",
                    parent=self):

                showInfo("Project name",
                         "Your project will be saved in the %s directory." %
                         self.project.name,
                         parent=self)

                self.project.saveModified()

                #userData = self.project.findFirstRepository(name='userData')
                #currentPath = userData.url.path
                #currentProjectName = self.project.name

                #projectDir = os.path.join(currentPath,currentProjectName)

                eci = EntryCompletionGui(self.basePopup.root)
                eci.initProject(self.project)

    #
    # File type determination and import
    #

    def createFileTypes(self, dataTypes):

        fileTypes = [FileType('all', ['*'])]

        for dataType in dataTypes:
            formatNames = fileTypeDict[dataType].keys()
            formatNames.sort()
            for formatName in formatNames:
                if formatName in self.fcWrapper.formatNameLists[dataType]:
                    fileTypes.append(
                        FileType(formatName,
                                 fileTypeDict[dataType][formatName]))

        return fileTypes

    def getFileName(self, title, fileTypes):

        fileSelectPopup = FileSelectPopup(
            self,
            file_types=fileTypes,
            title=title,
            dismiss_text='Cancel',
            selected_file_must_exist=True,
            multiSelect=False,
        )

        self.fileName = fileSelectPopup.getFile()

        if not self.fileName:
            showWarning('Failure',
                        'Please select an existing file.',
                        parent=self)
            return False

        return True

    def importSequenceOrCoords(self):

        dataTypes = ['sequence', 'coordinates']
        fileTypes = self.createFileTypes(dataTypes)

        if self.getFileName('Import sequence or coordinate file', fileTypes):

            formatNameSuggestions = {}

            for dataType in dataTypes:

                tmpList = self.fcWrapper.determineFormatNamesForFile(
                    dataType, self.fileName)

                if tmpList:
                    formatNameSuggestions[dataType] = tmpList

            if not formatNameSuggestions:
                showWarning(
                    'Failure',
                    'This file cannot be read by this software.\nPlease send the file with an explanation to %s.'
                    % self.email,
                    parent=self)
                return False

            #
            # Let user select if multiple options, otherwise take default
            #

            if len(formatNameSuggestions) == 1 and len(formatNameSuggestions[
                    formatNameSuggestions.keys()[0]]) == 1:

                dataType = formatNameSuggestions.keys()[0]
                formatName = formatNameSuggestions[dataType][0]

                if not showYesNo(
                        'File type detected',
                        'Reading as %s file in %s format. Is this correct?' %
                    (dataType, formatName),
                        parent=self):
                    showWarning(
                        'Failure',
                        'This file cannot be read by this software.\nPlease send the file with an explanation to %s.'
                        % self.email,
                        parent=self)
                    return False

            else:

                #
                # Create a selection (hopefully user-understandable)
                #

                selectionList = []
                selectionDict = {}

                for dataType in dataTypes:

                    dataTypeString = dataType

                    if formatNameSuggestions.has_key(dataType):
                        formatNames = formatNameSuggestions[dataType]
                        formatNames.sort()

                        for formatName in formatNames:
                            selectionString = "%s file in %s format." % (
                                dataTypeString, formatName)
                            selectionList.append(selectionString)
                            selectionDict[selectionString] = (dataType,
                                                              formatName)

                interaction = SelectionListPopup(self,
                                                 selectionList,
                                                 title='File format selection',
                                                 text='This is a:',
                                                 selectionDict=selectionDict,
                                                 dismissButton=True,
                                                 modal=True)

                #
                # Check if anything was selected...
                #

                dataType = formatName = None

                if interaction.isSelected:
                    (dataType, formatName) = interaction.selection
                else:
                    showWarning(
                        'Failure',
                        'This file cannot by read without a format selection.\nIf the correct format is not available, please send the file with an explanation to %s'
                        % self.email,
                        parent=self)
                    return False

            #
            # Now read the file, need to do some field updates!
            #

            (fileRead, fileInformation) = self.fcWrapper.readFile(
                dataType, formatName, self.fileName)

            if not fileRead:
                showWarning(
                    'Failure',
                    'This file cannot be read by this software:%s\nPlease send the file with an explanation to %s.'
                    % (fileInformation, self.email),
                    parent=self)
                return False

            (conversionInfo, conversionSuccess) = (
                self.fcWrapper.formatConversion.conversionInfo,
                self.fcWrapper.formatConversion.conversionSuccess)

            if not conversionSuccess:
                showWarning(
                    'Failure',
                    'This file was read by the software but contains invalid information.\nPlease send the file with an explanation to %s.'
                    % self.email,
                    parent=self)
                return False

            #
            # Set info if import worked OK
            #

            conversionLines = conversionInfo.split(": ")

            showInfo("Import coordinate and/or sequence information",
                     ":\n".join(conversionLines),
                     parent=self)

            if dataType == 'sequence':
                chains = self.fcWrapper.importReturns[dataType]
                models = []
            elif dataType == 'coordinates':
                models = self.fcWrapper.importReturns[dataType]
                chains = [
                    cChain.chain
                    for cChain in models[0].structureEnsemble.coordChains
                ]

            self.sequenceCoordinatesImport.setText(
                self.fileName)  # TODO change color or something?
            self.sequenceCoordinatesImport.configure(
                foreground=self.fontOkColor)

            self.sequenceCoordinatesLoaded = True

            #
            # Reset to list selector for further use
            #

            moleculeName = None

            for chain in chains:

                if chain in self.moleculeDict.values():
                    continue

                numResidues = len(chain.residues)
                if numResidues == 1:
                    residueText = "%s residue" % chain.findFirstResidue(
                    ).ccpCode
                else:
                    residueText = "%d residues" % numResidues

                moleculeName = "%s (chain '%s', %s)" % (
                    chain.molecule.name, chain.code, residueText)

                self.moleculeList.append(moleculeName)
                self.moleculeDict[moleculeName] = chain

            self.moleculeList.sort()

            self.moleculeSelect.destroy()

            if len(chains) == 1 and moleculeName:
                selectedIndex = self.moleculeList.index(moleculeName)
            else:
                selectedIndex = 0

            self.moleculeSelect = PulldownList(self.frameA,
                                               texts=self.moleculeList,
                                               index=selectedIndex,
                                               sticky='ew')
            self.moleculeSelect.grid(row=self.moleculeSelectRow, column=0)

            #
            # Now update Deposition tab
            #

            molecules = []
            for chain in chains:
                if not chain.molecule in molecules:
                    molecules.append(chain.molecule)

            numCoords = 0
            for model in models:
                numCoords += len(model.coords)

            self.updateDepositionImportLabel(molecules=len(molecules),
                                             chains=len(chains),
                                             models=len(models),
                                             coordinates=numCoords)

            # Add the molSystem to the entry!
            if chains and not chains[0].molSystem == self.entry.molSystem:
                self.entry.molSystem = chains[0].molSystem

        self.updateAll()

    def importShifts(self):

        currentChain = self.getCurrentChain()

        if not currentChain:
            showWarning(
                'Failure',
                'Please first read in a sequence or coordinate file and select the molecule relevant for this shift list.',
                parent=self)
            return

        elif self.currentShiftList:
            shiftListChainPair = (self.currentShiftList, currentChain)

            if shiftListChainPair in self.shiftListChainPairs:
                showWarning(
                    'Failure',
                    "You already read in chemical shifts for this chain.\nPlease read in related shifts for the other chain(s), if present, or press the 'Import new set of shifts' button to read in a new set of shifts.",
                    parent=self)
                return

        dataType = 'shifts'

        fileTypes = self.createFileTypes([dataType])

        if self.getFileName('Import chemical shift file', fileTypes):

            formatNameSuggestions = self.fcWrapper.determineFormatNamesForFile(
                dataType, self.fileName)

            if not formatNameSuggestions:
                showWarning(
                    'Failure',
                    'This file cannot be read by this software.\nPlease send the file with an explanation to %s.'
                    % self.email,
                    parent=self)
                return False

            #
            # Let user select if multiple options, otherwise take default
            #

            if len(formatNameSuggestions) == 1:

                formatName = formatNameSuggestions[0]

                if not showYesNo(
                        'File type detected',
                        'Reading as a chemical shift file in %s format. Is this correct?'
                        % formatName,
                        parent=self):
                    showWarning(
                        'Failure',
                        'This file cannot be read by this software.\nPlease send the file with an explanation to %s.'
                        % self.email,
                        parent=self)
                    return False

            else:

                #
                # Create a selection (hopefully user-understandable)
                #

                selectionList = []
                selectionDict = {}

                formatNameSuggestions.sort()

                for formatName in formatNameSuggestions:
                    selectionString = "chemical shift file in %s format." % (
                        formatName)
                    selectionList.append(selectionString)
                    selectionDict[selectionString] = formatName

                interaction = SelectionListPopup(self,
                                                 selectionList,
                                                 title='File format selection',
                                                 text='This is a:',
                                                 selectionDict=selectionDict,
                                                 dismissButton=True,
                                                 modal=True)

                #
                # Check if anything was selected...
                #

                formatName = None

                if interaction.isSelected:
                    formatName = interaction.selection
                else:
                    showWarning(
                        'Failure',
                        'This file cannot by read without a format selection.\nIf the correct format is not available, please send the file with an explanation to %s'
                        % self.email,
                        parent=self)
                    return False

            #
            # Now read the file, need to do some field updates! Also make sure to re-use shift list for other molecules...
            #

            (fileRead, fileInformation) = self.fcWrapper.readFile(
                dataType,
                formatName,
                self.fileName,
                addKeywords={'measurementList': self.currentShiftList})

            if not fileRead:
                showWarning(
                    'Failure',
                    'This file cannot be read by this software:%s\nPlease send the file with an explanation to %s.'
                    % (fileInformation, self.email),
                    parent=self)
                return False

            (conversionInfo, conversionSuccess) = (
                self.fcWrapper.formatConversion.conversionInfo,
                self.fcWrapper.formatConversion.conversionSuccess)

            if not conversionSuccess:
                showWarning(
                    'Failure',
                    'This file was read by the software but contains invalid information.\nPlease send the file with an explanation to %s.'
                    % self.email,
                    parent=self)
                return False

            #
            # Set info if import worked OK
            #

            conversionLines = conversionInfo.split(": ")

            showInfo("Import chemical shift information",
                     ":\n".join(conversionLines),
                     parent=self)

            self.shiftImport.setText(
                self.fileName)  # TODO change color or something?
            self.shiftImport.configure(foreground=self.fontOkColor)
            self.shiftsLoaded = True
            self.shiftsFormatName = formatName

            shiftList = self.fcWrapper.importReturns[dataType]

            if not self.currentShiftList:
                self.currentShiftList = shiftList

            self.shiftListChainPairs.append(
                (self.currentShiftList, currentChain))

            self.updateDepositionImportLabel(shiftList=shiftList)

            if not shiftList in self.entry.measurementLists:
                print shiftList
                self.entry.addMeasurementList(shiftList)
                print self.entry.measurementLists

        self.updateAll()

    #
    # Updaters
    #

    def selectTab(self, index):

        funcsDict = {
            0: (self.updateMain, ),
            1: (self.updateCoordinates, ),
            2: (self.updateDeposition, )
        }

        for func in funcsDict[index]:
            func()

    def updateMain(self):

        pass

    def updateCoordinates(self):

        pass

    def updateDeposition(self):

        pass

    def updateAll(self):

        self.selectTab(self.tabbedFrame.selected)

        if self.sequenceCoordinatesLoaded and self.shiftsLoaded:
            if showYesNo(
                    'Connect shifts to sequence',
                    'You have to check whether the chemical shift information matches the sequence. Do you want to this now?',
                    parent=self):
                self.connectShiftsSequence()

        self.waiting = False

    def getCurrentChain(self):

        if len(self.moleculeList) == 1:
            chain = self.moleculeDict.values()[0]
        else:
            try:
                moleculeSelected = self.moleculeSelect.getText()
                chain = self.moleculeDict[moleculeSelected]
            except:
                chain = None

        return chain

    def connectShiftsSequence(self):

        if not self.linkResDone:

            changeResetColor = False

            #
            # Get chain mapping and run linkResonances
            #

            chain = self.getCurrentChain()

            forceChainMappings = self.fcWrapper.linkResonancesToSequence(
                chain=chain)
            self.fcWrapper.formatConversion.linkResonances(
                forceChainMappings=forceChainMappings, guiParent=self)

            #
            # Get information about the linking process
            #
            # TODO Should only have an nmrProject (no restraint import, should be included?)
            # In any case, is always the LAST info in numResonancesLinked info

            numResonancesLinked = self.fcWrapper.formatConversion.numResonancesLinked
            (origLinked, origUnlinked, linked,
             unlinked) = (numResonancesLinked['origLinked'][-1],
                          numResonancesLinked['origUnlinked'][-1],
                          numResonancesLinked['linked'][-1],
                          numResonancesLinked['unlinked'][-1])

            #
            # Track number of new resonances, and reset for new import
            #

            if self.fcWrapper.formatConversion.allResonancesLinked:
                status = 'All information matches (for all imports).'
                foreground = self.fontOkColor
                changeResetColor = True
            else:
                if origUnlinked - unlinked == self.fcWrapper.numNewResonances:
                    status = 'All information matches (for this import)'
                    foreground = self.fontOkColor
                    changeResetColor = True
                else:
                    if origUnlinked != unlinked:
                        status = 'Not all information matches (for this and/or another import).'
                        foreground = self.fontCheckColor
                        changeResetColor = True
                    else:
                        status = 'No information matches (for this import).'
                        foreground = self.fontBadColor

                    otherUnlinked = (origUnlinked -
                                     self.fcWrapper.numNewResonances)
                    notLinked = unlinked - otherUnlinked

                    status += "\nUnable to link %d out of %d imported shifts (%.2f%%)." % (
                        notLinked, self.fcWrapper.numNewResonances,
                        (notLinked * 100.0) / self.fcWrapper.numNewResonances)

            self.linkResCheckInfo.set("Status: %s" % status)
            self.linkResCheckInfo.config(foreground=foreground)

            self.linkResDone = True

            #
            # Change the color of the reset button to indicate OK to do next one
            #

            if changeResetColor:

                self.mainButtons.buttons[0].config(foreground=self.fontOkColor)

                self.updateDepositionImportLabel(
                    shiftList=None,
                    percentageLinked=(linked * 100.0 / (unlinked + linked)),
                    connectedChain=chain)

    def updateDepositionImportLabel(self,
                                    shiftList=None,
                                    molecules=0,
                                    chains=0,
                                    models=0,
                                    coordinates=0,
                                    percentageLinked=None,
                                    connectedChain=None):

        if shiftList and shiftList not in self.importedShiftLists:
            self.importedShiftLists.append(shiftList)
            self.depositionImportNums[0] += 1

        shifts = 0
        for shiftList in self.importedShiftLists:
            shifts += len(shiftList.measurements)

        self.depositionImportNums[1] += shifts
        self.depositionImportNums[2] += molecules
        self.depositionImportNums[3] += chains
        self.depositionImportNums[4] += models
        self.depositionImportNums[5] += coordinates

        if percentageLinked != None:
            self.depositionImportNums[6] = percentageLinked

        if connectedChain and connectedChain not in self.connectedChains:
            self.depositionImportNums[7] += 1
            self.connectedChains.append(connectedChain)

        self.depositionImportLabel.destroy()

        finalForeground = self.fontBadColor
        if self.depositionImportNums[0] == 0 and self.depositionImportNums[
                2] == 0:
            # Nothing imported
            foreground = self.fontBadColor

        elif self.depositionImportNums[6]:
            # Linked shifts available - TODO base this on % of shifts linked?
            foreground = self.fontOkColor
            if self.depositionImportNums[5]:
                finalForeground = self.fontOkColor
            else:
                finalForeground = self.fontCheckColor

        else:
            # Intermediate state
            foreground = self.fontCheckColor

        self.depositionImportLabel = Label(self.frameD,
                                           text=self.depositionImportText %
                                           tuple(self.depositionImportNums),
                                           foreground=foreground)
        self.depositionImportLabel.grid(row=self.depositionImportLoc[0],
                                        column=self.depositionImportLoc[1],
                                        sticky='ew')

        self.eciStart.configure(foreground=finalForeground)

    def resetSequenceImport(self):

        doReset = True

        if not self.linkResDone and self.sequenceCoordinatesLoaded and self.shiftsLoaded:
            if showYesNo(
                    'Shifts not connected to sequence',
                    'You have not checked whether the imported chemical shift information matches the imported sequence. Do you want to this first? If not, the last imported data will be invalid.',
                    parent=self):
                self.connectShiftsSequence()
                doReset = False

        if doReset:

            self.mainButtons.buttons[0].config(
                foreground=self.fontDefaultColor)

            self.sequenceCoordinatesLoaded = self.shiftsLoaded = self.linkResDone = False

            self.sequenceCoordinatesImport.setText(self.defaultSelectText)
            self.sequenceCoordinatesImport.configure(
                foreground=self.fontDefaultColor)

            self.moleculeSelect.destroy()
            self.moleculeSelect = Label(
                self.frameA,
                text="None available yet - import valid file first",
                foreground=self.fontBadColor)
            self.moleculeSelect.grid(row=self.moleculeSelectRow,
                                     column=0,
                                     sticky='ew')

            self.shiftImport.setText(self.defaultSelectText)
            self.shiftImport.configure(foreground=self.fontDefaultColor)

            self.linkResCheckInfo.set("")

    def resetShiftImport(self):

        doReset = True

        if not self.linkResDone and self.sequenceCoordinatesLoaded and self.shiftsLoaded:
            if showYesNo(
                    'Shifts not connected to sequence',
                    'You have not checked whether the imported chemical shift information matches the imported sequence. Do you want to this first? If not, the last imported data will be invalid.',
                    parent=self):
                self.connectShiftsSequence()
                doReset = False

        if doReset:

            self.mainButtons.buttons[1].config(
                foreground=self.fontDefaultColor)

            self.shiftsLoaded = self.linkResDone = False

            self.currentShiftList = None

            self.shiftImport.setText(self.defaultSelectText)
            self.shiftImport.configure(foreground=self.fontDefaultColor)

            self.linkResCheckInfo.set("")

    def destroy(self):

        Frame.destroy(self)

    #
    # Instructions
    #

    def showMainInstructions(self):

        popup = getPopup(self)

        message = """Use this tab to import the chemical shifts and the coordinate and/or sequence information for your molecular chains.
    
The imported chemical shift file should contain information for only *one* molecular chain to make it easier to connect the molecule information to the chemical shift information. You therefore have to select a single chain for each shift list from the dropdown menu that will appear after you imported a coordinate or sequence file.
    
For example, when using sequence files for a dimer, import the information for the chemical shifts for each chain separately:
    
  1. Import the sequence for the first molecular chain.
  2. Import a chemical shift file with shifts only for this molecular chain.  
  3. Reset using the 'Import new sequence' button
  4. Import the sequence for the second molecular chain
  5. Import a chemical shift file with shifts only for this second chain
  
Alternatively, it is possible to read in the molecule information from a full coordinate file:

  1. Import a coordinate file with all molecular information, including coordinates.
  2. Select a molecular chain.
  3. Import a chemical shift file with shifts only for the selected molecular chain.
  4. Go back to step 2. if necessary.
  
You can also import multiple sets of chemical shifts (e.g. for the sample in different conditions). In this case, you have to import all chemical shift information that belongs together for all molecular chains, then press the 'Import new set of shifts' button.

Notes:

1. This application always creates a new CCPN project. It is not possible to import files into existing projects.
2. If your chemical shift file contains information for multiple chains, you have to edit it manually to split up the information per chain.
    """

        showHelpText(self, message, popup=popup)

    def showFormats(self):

        popup = getPopup(self)

        message = """For chemical shifts, the following formats are recognised:

*** Auremol ***

section_sequenzdefinition
_Residue_seq_code
_Atom_num_code
_Residue_label
_Atom_name
_Atom_type
_Atom_alias
_Atom_equivalent
_Atom_CSA
  1   1 MET   HN H   -      -            8.95
  1   2 MET    N N   -      -          157.00
  1   3 MET   CA C   -      -           40.00


*** Autoassign ***

AA        HN    N15    CO-1   CA-1   CB-1   HA-1          CO     CA     CB     HA

A31      8.14  121.4                                            51.3   19.6                 (GS178 115.HSQC)
D32      8.88  122.9          51.3   19.5                       55.4   39.6                 (GS271 22.HSQC)


*** CNS ***

do ( store1 = 53.13218 ) ( resid 78 and name CA )
do ( store1 = 0.7356673 ) ( resid 15 and name HD1# )
do ( store1 = 120.5381 ) ( resid 8 and name N )
do ( store1 = 121.1414 ) ( resid 78 and name N )


*** Cosmos ***

CS_VALUES 3
C_ALA 176.6
CA_ALA 51.66
CB_ALA 17.26
END


*** CSI ***

#     A       HA       CA       CO       CB       Consensus
#
1     MET       0 C      0 C      NA       0 C         0 C 
2     GLY       0 C      0 C      NA       0 C         0 C 


*** MARS ***

            H         N         CO-1      CA        CA-1   
PR_2        8.900   123.220   170.540    55.080    54.450  
PR_4        8.320   115.340   175.920      -       55.080  


*** MONTE ***

         1            102.544      8.211     45.853     54.925      0.000     18.069    180.112 
         2            103.276      8.580     45.334     54.154      0.000     35.650    175.087 
         3            103.997      7.407     45.165      0.000      0.000      0.000      0.000 


*** NMR-STAR ***

data_test

save_shifts1
   _Saveframe_category               assigned_chemical_shifts

   loop_
      _Atom_shift_assign_ID
      _Residue_seq_code
      _Residue_label
      _Atom_name
      _Atom_type
      _Chem_shift_value
      _Chem_shift_value_error
      _Chem_shift_ambiguity_code

          1     1   ASP  CA    C   52.000  0.02  1  
          2     1   ASP  HA    H    4.220  0.02  1  

   stop_

save_


*** NMRVIEW ***

  2.CG1     18.549 0
  2.CG2     18.844 0
  2.HG1#     0.800 0
  2.HG2#     0.723 0
  3.HG2      2.298 0
  3.HG1      2.298 0


*** PIPP ***

RES_ID          1
RES_TYPE        MET
SPIN_SYSTEM_ID  1
    CA        55.9920
    CB        33.1470
    HA         4.1141
    HB#        2.0492
    HG#        2.4250
END_RES_DEF


*** PISTACHIO ***

   1    1  GLY    C     C  172.621  1.000  0 
   2    1  GLY   CA     C   44.308  1.000  0 
   3    2  SER    N     N  122.241  1.000  0 


*** PRONTO ***

Spin system   HN          HA      Other:

1: Val-1                  3.76    HB: 1.945, HG1: 0.770, HG2: 0.608
2: Ile-2      8.80        4.26    HB: 1.526, HG1: 1.278, HG2: 0.728, HD: 0.918


*** SHIFTX ***

  NUM RES   HA     H       N        CA      CB       C
--- --- ------ ------ -------- ------- ------- --------
 2     T  4.4161 8.1749 111.0443 61.8324 70.3867 172.5362
 3     Y  4.9022 9.0239 120.2106 56.0493 41.4218 173.0761

 NUM RES  H    HA   HB   HB2  HB3  HD1  HD2  HD21 HD22 HD3  HE   HE1 HE2  HE21 HE22 HE3  HG   HG1  HG12 HG13 HG2  HG3  HZ
 2     T  8.17 4.42 4.24 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.21 0.00 0.00
 3     Y  9.02 4.90 0.00 2.22 2.20 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00


*** SPARKY ***

 Group   Atom  Nuc    Shift   SDev  Assignments

     R2     CA  13C   56.539  0.003      3
     R2     CB  13C   30.808  0.009      3


*** TALOS ***

VARS   RESID RESNAME PHI PSI DPHI DPSI DIST COUNT CLASS
FORMAT %4d %s %8.3f %8.3f %8.3f %8.3f %8.3f %2d %s

   1 Q 9999.000 9999.000    0.000    0.000    0.000  0 None
   2 N  -85.000  124.000   23.000   28.000   85.920 10 Good


***XEASY/CYANA:

   1 117.803 0.000 N       1
   2   8.208 0.002 HN      1
   3  56.508 0.055 CA      1
   4 999.000 0.000 HA      1
   5  29.451 0.004 CB      1
    
    """

        showHelpText(self, message, popup=popup)