def setFiles(self, fileNames): if not self.fileList.multiSelect: self.setFile(fileNames[0]) return dirA = None files = [] for fileName in fileNames: dirB, file = splitPath(fileName) if dirA is None: dirA = dirB elif dirA != dirB: msg = 'Cannot select files from different directories' showError('Set files failure', msg, parent=self) return if file in self.fileList.objectList: files.append(file) if dirA: self.setDirectory(dirA) self.fileList.selectObjects(files)
def initialize(self, parHelp): self.parHelp = parHelp (self.fileDir, void) = splitPath(self.name) self.scriptName = 'processingParsIO.py' self.parFiles = {} self.allPars = { 'mainPars': [('numDim', 'ndim', 2, 0), ('fileName', 'file', 2, ''), ('', 'head', 2, 0), ('', 'integer', 1, True), ('', 'swap', 1, False), ('BYTORDP', 'big_endian', 1, None), ('', 'varian', -1, None)], 'dimPar': [('', 'dim', 2, -1)], 'dimPars': [ ('SI', 'npts', 2, None), ('XDIM', 'block', 2, None), ('SW_p', 'sw', 2, 1000.0), ('SF', 'sf', 2, 1000.0), ('OFFSET', 'refppm', 2, 0.0), ('refPoint', 'refpt', 2, 1.0), ('AXNUC', 'nuc', 2, '1H'), ('FT_mod', 'dimType', 2, 0), ] } self.initGeneric()
def initialize(self, numDim, parHelp): self.numDim = numDim self.parHelp = parHelp (self.fileDir, void) = splitPath(self.name) # TODO need to separate out pulse program!! self.parFiles = {} self.acqMode = None self.acqModeValues = {} self.pulseProgramInfo = None self.fnModes = ['undef'] * (self.numDim - 1) self.fnModeDict = { 0: ('undef', 2.0), 1: ('qf', 1.0), 2: ('qseq', 1.0), 3: ('tppi', 1.0), # Correct? 4: ('states', 2.0), 5: ('states-tppi', 2.0), 6: ('echo-antiecho', 2.0) } self.stdIncrements = ('0', '10', '11') self.dateString = None
def initialize(self, numDim, parHelp): self.numDim = numDim self.parHelp = parHelp (self.fileDir, void) = splitPath(self.name) self.parFiles = {}
def chooseFile(self, file): fullPath =self.file_select.getFile() if not os.path.isdir(fullPath) and file: (self.path, module) = splitPath(fullPath) module = re.sub('\..+?$','',module) self.module = module self.moduleLabel2.set(module) self.updateFunctions(file)
def composeMultipleFileText(self,files): fileNames = [] for file in files: (path,fileName) = splitPath(file) fileNames.append(fileName) text = "%s\n%s" % (path,', '.join(fileNames)) return text
def setFile(self, file): if self.show_file: (dir, file) = splitPath(file) else: dir = file self.setDirectory(dir) if self.show_file: self.file_entry.setEntry(file)
def getLowerLimitFile(self, messageType='read'): addKeywds = {} if self.constraintType in ['distance', 'hBond']: (path, fileName) = splitPath(self.fileName) fileName = string.replace(fileName, 'upl', 'lol') fileName = joinPath(path, fileName) # # TODO: could here in principle check if file exists and read automatically? # getLowerDist = False if not self.lowerLimitFile and not self.minimalPrompts: getLowerDist = self.messageReporter.showYesNo( "%s lower limit constraint file" % string.capitalize(messageType), "Do you want to %s the accompanying lower limit constraint file?" % messageType) if getLowerDist: component = '%sConstraints' % self.constraintType interaction = self.multiDialog.FileName( self.guiParent, file=fileName, component=component, selectionText='Select file', format=self.format) self.lowerLimitFile = interaction.file if hasattr(self, 'lowerLimitFile'): addKeywds['lowerDistanceFile'] = self.lowerLimitFile return addKeywds
def initialize(self,numDim,parHelp): self.numDim = numDim self.parHelp = parHelp (self.fileDir,void) = splitPath(self.name) # TODO need to separate out pulse program!! self.parFiles = {} self.acqMode = None self.acqModeValues = {} self.pulseProgramInfo = None self.pulProgName = None self.fnModes = ['undef'] * (self.numDim - 1) self.fnModeDict = { 0: ('undef', 2.0), 1: ('qf', 1.0), 2: ('qseq', 1.0), 3: ('tppi', 1.0), # Correct? 4: ('states', 2.0), 5: ('states-tppi', 2.0), 6: ('echo-antiecho',2.0) } self.stdIncrements = ('0','10','11') self.software = None self.version = None self.dateString = None # initialise per-dim attributes for tag in dimAttrTags: setattr(self, tag, numDim*[None])
def addFile(self): if self.server: fileSelectPopup = FileSelectPopup(self, title='Select Source File', dismiss_text='Cancel', file_types=self.fileTypes, multiSelect=True, selected_file_must_exist=True) filePaths = fileSelectPopup.file_select.getFiles() n = len(self.installRoot) for filePath in filePaths: if self.installRoot != filePath[:n]: showWarning( 'Warning', 'Install root %s not found in file path %s' % (self.installRoot, filePath)) continue filePath = filePath[n + 1:] if filePath: dirName, fileName = splitPath(filePath) if fileName[-3:] == '.py': language = 'python' else: language = 'None' fileUpdate = FileUpdate(self.server, fileName, dirName, language, isNew=True) self.update()
def setupProject(self, fileName, projectName): self.projectFileName = fileName try: self.ccpnProject = XmlIO.loadProjectFile(fileName) print "Read existing CCPN project..." except: print "Creating new CCPN project..." projectName = returnMemopsWord(projectName) self.ccpnMemopsRoot = Implementation.MemopsRoot(name=projectName) self.nmrProject = Nmr.NmrProject(self.ccpnProject, name=self.ccpnProject.name) # # Set the location... # (path, file) = splitPath(fileName) self.ccpnProject.path = file self.ccpnProject.url.path = path outputPath = os.path.join(path, projectName) try: os.stat(outputPath) except: os.mkdir(outputPath) for url in self.ccpnProject.urls: if url != self.ccpnProject.url: url.path = outputPath # TODO: check name of project: if not same CRASH OUT! return self.ccpnProject
def moveDirToRelative(self): dataStores = self.dataStoreTable.currentObjects for dataStore in dataStores: absPath = dataStore.dataUrl.url.dataLocation relPath = dataStore.path absPaths = splitPath(absPath) if absPaths[1]: absPathNew = absPaths[0] relPathNew = joinPath(absPaths[1], relPath) if relPathNew != relPath: # No notifiers for temporarily invalid path ###dataUrl = dataStore.dataUrl self.project.__dict__['override'] = True # 22 Aug 2014: the below completely messes up the other dataStores # everything is handled in changeDataStoreUrl so only need to change dataStore ###for dataUrlStore in dataUrl.dataStores: ### dataUrlStore.path = joinPath(absPaths[1], dataUrlStore.path) dataStore.path = relPathNew self.project.__dict__['override'] = False changeDataStoreUrl(dataStore, absPathNew)
def read(self, verbose=0): dimTranslate = [] if verbose == 1: print "Reading %s peak list %s" % (self.format, self.name) fin = open(self.name, 'rU') # Read, look for lines starting with DATA, FORMAT and VARS line = fin.readline() while line: cols = line.split() if len(cols) == 0 or self.patt['hash'].search(line): pass elif cols[0] == 'DATA': if cols[1] == 'DIMCOUNT': ndim = returnInt(cols[2]) (path, specName) = splitPath(self.name) self.setSpectrumInfo(specName, ndim) elif self.patt[self.format + '?_AXIS'].search(cols[1]): dimCode = self.patt[self.format + '?_AXIS'].search( cols[1]).group(1) dimTranslate.append(dimCode) self.setSpectrumData(cols[2:]) elif cols[0] == 'FORMAT': # Info here not really necessary as it is a space-delimited format pass elif cols[0] == 'VARS': defCols = {} assignKeywords = [] dimChemElement = [] addNDims = 0 # Get column headers for i in range(1, len(cols)): colId = cols[i] if cols[i][:6] == 'Assign': if not assignKeywords: # Special case - hardcode for assignments for dim in range(ndim): colId = 'Assign%d' % (dim + 1) assignKeywords.append(colId) defCols[colId] = i - 1 + dim addNDims = ndim - 1 else: # Means that more than one column defined, so 'none' assign will show up... addNDims -= 1 else: if len(cols[i]) <= 2: axisType = cols[i][-1].upper() chemElement = None #TODO: what does pipp use for 4Ds??!? if axisType in ['X', 'Y', 'Z']: colId = axisType if len(cols[i]) == 2: chemElement = cols[i][0].upper() dimChemElement.append(chemElement) defCols[colId] = i - 1 defColsLen = len(defCols) # Start reading data line = fin.readline() while line: cols = line.split() localAddNDims = addNDims if len(cols) == 0 or self.patt['hash'].search(line): pass colsCopy = cols[:] colIndex = 0 for i in range(len(colsCopy)): col = colsCopy[i] if col.count(self.assignInternalBreak): (astring1, astring2) = col.split(self.assignInternalBreak) (residue, atomName) = astring1.split(self.assignSep) cols[colIndex] = astring1 cols.insert(colIndex + 1, residue + self.assignSep + astring2) colIndex += 2 elif col == self.notAssigned: for i in range(localAddNDims): cols.append(self.notAssigned) colIndex += (localAddNDims + 1) localAddNDims = 0 else: colIndex += 1 if len(cols) != defColsLen: print " Warning: ignoring following line in %s peak file (columns don't match):\n%s" % ( self.format, line) pass else: for peakIdName in ('PkID', 'Pk-ID'): if defCols.has_key(peakIdName): Id = cols[defCols[peakIdName]] break intensity = cols[defCols['Intensity']] # Get the assignment strings assign = [] for defColKwd in assignKeywords: astring = cols[defCols[defColKwd]].strip() if astring[-1] == self.assignBreak: astring = astring[:-1] if astring == self.notAssigned: assign.append(None) else: assign.append(astring) for i in range(len(assign), len(dimTranslate)): assign.append(None) ppm = [] dimSlice = [] for dimCode in dimTranslate: ppm.append(returnFloat(cols[defCols[dimCode]])) if defCols.has_key('Sl.' + dimCode): dimSlice.append(cols[defCols['Sl.' + dimCode]]) else: dimSlice.append(None) # Have to do some (rough) checking to determine the mapping between # the assignment and the columns in the file... self.determineMapping(assign, ppm) self.peaks.append( PippPeak(Id, intensity, assign, ppm, dimSlice=dimSlice)) line = fin.readline() line = fin.readline() fin.close() # # Determine best mappings... gives the mapping between the ASSIGNMENT dim (column # position) to the PPM dim in the peak list (column value). # for aInd in range(0, self.numDim): if self.finalMapping == None: continue prefPpmInd = (-1, 0) for ppmInd in range(0, self.numDim): mappingIndex = (aInd, ppmInd) if self.mapping.has_key(mappingIndex) and self.mapping[ mappingIndex] > prefPpmInd[1]: prefPpmInd = (ppmInd, self.mapping[mappingIndex]) if prefPpmInd[0] not in self.finalMapping: self.finalMapping.append(prefPpmInd[0]) else: print " Error: mapping for assignment could not be determined!" self.finalMapping = None # # Rearrange assignments for all peaks... # if self.finalMapping: for peak in self.peaks: assign = peak.assign[:] for aInd in range(0, len(self.finalMapping)): ppmInd = self.finalMapping[aInd] peak.assign[ppmInd] = assign[aInd]
def body(self, master): master.grid_columnconfigure(0, weight=1) master.grid_rowconfigure(0, weight=1) master.grid_rowconfigure(1, weight=1) self.geometry('500x500') fileNames = [] # All files have to be in same directory! if self.files and self.files[0].find(dirsep) > -1: (path, fileName) = splitPath(self.files[0]) directory = path fileNames.append(fileName) for file in self.files[1:]: (path, fileName) = splitPath(file) fileNames.append(fileName) else: path = None directory = normalisePath(os.path.abspath(os.curdir)) fileNames = [] #print directory #print fileName file_types = [] if fileTypeDict.has_key(self.component): if self.format and fileTypeDict[self.component].has_key( self.format): file_types.extend([ FileType(self.format, fileTypeDict[self.component][self.format]) ]) if fileTypeDict[self.component].has_key('generic'): file_types.extend([ FileType('generic', fileTypeDict[self.component]['generic']) ]) file_types.extend([FileType('All', ['*'])]) self.file_select = FileSelect(master, file_types=file_types, directory=directory, double_callback=self.ok, multiSelect=self.multiSelect) if self.multiSelect: self.file_select.setFiles(fileNames) else: if fileNames: fileName = fileNames[0] else: fileName = '' self.file_select.setFile(fileName) self.file_select.grid(row=0, column=0, sticky=Tkinter.NSEW) texts = [self.selectionText] commands = [self.ok] buttons = createDismissHelpButtonList(master, texts=texts, commands=commands) buttons.grid(row=1, column=0, sticky=Tkinter.EW)
def openUrl(self, url, forceLoad=False, allowModifyPath=True): #print 'openUrl "%s" "%s" "%s" "%s" "%s"' % (url, self.protocol, self.location, self.path, self.dir) if (self.startUrlCallback): self.startUrlCallback(self.yview()) self.setState(Tkinter.NORMAL) (protocol, location, path, query, fragment) = urlparse.urlsplit(url) #print 'openUrl1 "%s" "%s" "%s" "%s" "%s"' % (protocol, location, path, query, fragment) if (not protocol): protocol = self.protocol if (protocol == self.protocol): if (not location): location = self.location if (location == self.location): if (allowModifyPath and not path): allowModifyPath = False path = self.path #print 'openUrl2 "%s" "%s" "%s"' % (protocol, location, path) if (allowModifyPath and self.dir and path and (path[0] != '/')): path = self.dir + '/' + path #print 'openUrl3 "%s" "%s" "%s"' % (protocol, location, path) if (forceLoad or (protocol != self.protocol) or \ (location != self.location) or (path != self.path)): (dir, rest) = splitPath(path) if (rest and (rest[0] == '~')): dir = dir + rest elif (dir == '/'): dir = '' self.protocol = protocol self.location = location self.path = path self.dir = dir self.fragmentIndices = {} self.clearTags() self.clear() self.images = { } # need to keep reference to images otherwise they get gc'ed try: if (self.path[-4:].lower() == '.gif'): self.loadImage(self.path) else: if (protocol == 'file'): u = open(path) else: nonFragmentUrl = urlparse.urlunsplit( (protocol, location, path, query, '')) u = urllib.urlopen(nonFragmentUrl) t = u.read() u.close() self.feed(t) self.close() except: showError('Opening url', 'Could not open URL "%s"' % url) self.moveToFragment(fragment) self.setDefaultCursor() self.setState(Tkinter.DISABLED) if (self.endUrlCallback): fullUrl = urlparse.urlunsplit( (protocol, location, path, query, fragment)) self.endUrlCallback(fullUrl)
def read(self, verbose=0): dataColCodes = [] dataColLengths = [] dataColNum = [] ndim = 0 if verbose == 1: print "Reading sparky peak list %s" % self.name fin = open(self.name, 'rU') # # Start reading, look for header line # line = fin.readline() while line: cols = line.split() if len(cols) == 0 or self.patt['hash'].search(line): pass else: # # First line should have column header names # numHeaderCols = 0 numDataCols = 0 dataColLen = 2 # Space and newline at end of line for refDatum in self.refData: (sparkyCode, numHeaderCol, numDataCol, colLen, refInfo, regExpPatt) = refDatum allMatches = self.patt[self.format + sparkyCode].findall(line) if allMatches != []: numMatches = len(allMatches) # # Number of total columns in header and for data # numHeaderCols += numHeaderCol * numMatches numDataCols += numDataCol * numMatches # # Keep track of column info # for i in range(0, numMatches): dataColCodes.append(sparkyCode) dataColNum.append(numDataCol) dataColLengths.append(colLen) # # This one to check data lines later # dataColLen += colLen * numMatches # # Set ndim (number of dimensions) # if refInfo == 'ndim': ndim = numMatches # # Exit if there's column names that are not recognized: no use trying # to read these for Sparky (very loose format) # if numHeaderCols != len(cols): print " Unrecognized column headers: can't read sparky file... " return # # Initialize peak file # (path, specName) = splitPath(self.name) self.setSpectrumInfo(specName, ndim) # # Start reading data # line = fin.readline() while line: cols = line.split() if len(cols) == 0 or self.patt['hash'].search(line): pass else: # # Two methods to read file: # - default one based on plain column split # - second one assuming valid data line, using expected column length to parse (only works in some cases) # obligColumnFound = 0 if len(cols) == numDataCols or len(line) == dataColLen: dataColCodesLen = len(dataColCodes) else: # # If problems, still try to read in essential bits (all until 'ndim' column) # print " Warning: Bad column match for following Sparky line. Trying to read...\n%s" % line, for i in range(0, len(dataColCodes)): sparkyCode = dataColCodes[i] for (refSparkyCode, numHeaderCol, numDataCol, colLen, refInfo, regExpPatt) in self.refData: if sparkyCode == refSparkyCode: if refInfo == 'ndim' and obligColumnFound == 0: obligColumnFound = 1 break elif refInfo == None and obligColumnFound == 1: obligColumnFound = -1 break # # Obligatory columns found, other ones starting: quit # if obligColumnFound == -1: dataColCodesLen = i break if obligColumnFound != -1: print " Can't read... " line = fin.readline() continue else: print " Reading first %d columns only." % dataColCodesLen print values = {} colNum = 0 for i in range(0, len(dataColCodes)): sparkyCode = dataColCodes[i] if not values.has_key(sparkyCode): values[sparkyCode] = [] if len(cols ) == numDataCols or obligColumnFound == -1: values[sparkyCode].append(" ".join( cols[colNum:colNum + dataColNum[i]])) if dataColNum[i] > 1: colNum += dataColNum[i] - 1 else: values[sparkyCode].append( line[0:dataColLengths[i]]) line = line[dataColLengths[i]:] colNum += 1 self.peaks.append(SparkyPeak(self, values)) line = fin.readline() line = fin.readline() fin.close()
def read(self, fileType='project', saveFileKey=None, readSaveFiles=True, verbose=0): if verbose == 1: print "Reading sparky %s file %s" % (fileType, self.name) fin = open(self.name, 'rU') # # Start reading, look for header line # location = [] level = -1 locationSubs = self.setLocationSubs(level, location) line = fin.readline() if not line.count("<sparky %s file>" % fileType): print " File %s is not a %s file - ignored." % (self.name, fileType) return line = fin.readline() while line: cols = line.split() if len(cols) == 0 or self.patt['hash'].search(line): line = fin.readline() continue bracketSearch = self.patt[self.format + 'SharpBracketBetween'].search(line) if bracketSearch: bracketName = bracketSearch.group(1) bracketSplit = bracketName.split() if bracketSplit[0] == 'version': pass elif bracketSplit[0] == 'end': bracketName = ' '.join(bracketSplit[1:]) # # Resets if no data was found... # if location == [ 'molecule', 'attached data' ] and not self.sequenceFile.sequences[0].elements: self.sequenceFile = None del (self.sequenceLocation[self.molNames[-1]]) # # Make sure navigation is correct # if level == -1: pass elif bracketName != location[level]: print " Bracketing mismatch for %s in sparky project. Exiting." % bracketName return else: level -= 1 location.pop(-1) locationSubs = self.setLocationSubs(level, location) else: if bracketName not in locationSubs: print " Unrecognized subbracket name %s in %s!" % ( bracketName, str(location)) if level != -1: location.append(bracketName) level += 1 else: location.append(bracketName) level += 1 locationSubs = self.setLocationSubs(level, location) if location == ['spectrum']: peakFile = SparkyPeakFile(self.name) self.peaksLocation[saveFileKey] = [] self.peaksLocation[saveFileKey].append(peakFile) tempMolName = None elif location == ['molecule', 'condition']: self.moleculeConditions[self.molNames[-1]] = [] elif location == ['molecule', 'attached data']: if not self.sequenceFile: self.sequenceFile = SparkySequenceFile( self.name) self.sequenceFile.sequences.append( SparkySequence()) self.sequenceLocation[self.molNames[ -1]] = self.sequenceFile.sequences[-1] elif location == [ 'molecule', 'condition', 'resonances' ]: chemShiftFile = SparkyChemShiftFile(self.name) self.chemShiftFiles.append(chemShiftFile) self.chemShiftLocation[self.molNames[-1]][ self.moleculeConditions[ self.molNames[-1]][-1]] = chemShiftFile else: if location == ['savefiles']: self.saveFiles.append(cols[0]) elif location == ['molecule']: if cols[0] == 'name': molName = ' '.join(cols[1:]) self.molNames.append(molName) self.chemShiftLocation[self.molNames[-1]] = {} elif location == ['molecule', 'attached data']: if cols[0] == 'sequence': code1LetterSequence = cols[1] for i in range(0, len(code1LetterSequence)): code1Letter = code1LetterSequence[i] self.sequenceLocation[ self.molNames[-1]].elements.append( SparkySequenceElement( i + 1, code1Letter=code1Letter)) elif cols[0] == 'first_residue_number': firstSeqNum = returnInt(cols[1]) for i in range( 0, len(self.sequenceLocation[ self.molNames[-1]].elements)): seqEl = self.sequenceLocation[ self.molNames[-1]].elements[i] seqEl.seqCode = firstSeqNum + i elif location == ['molecule', 'condition']: if cols[0] == 'name': self.moleculeConditions[self.molNames[-1]].append( ' '.join(cols[1:])) elif location == ['molecule', 'condition', 'resonances']: atomInfo = cols[0].split(self.resonanceSep) # Sometimes split up badly... if len(atomInfo) != 4 and (cols[1][0] == self.resonanceSep or cols[0][-1] == self.resonanceSep): atomInfo = (cols[0] + cols[1]).split(self.resonanceSep) if len(atomInfo) != 4: atomInfo = [] if len(atomInfo) != 4: print " Error: bad '%s' atom information for chemical shift. Ignored." % cols[ 0] else: (void1, residue, atom, void2) = atomInfo values = {} values['residue'] = [residue] values['atom'] = [atom] values['chemShift'] = [cols[1]] values['atomType'] = [cols[2]] curShift = SparkyChemShift(chemShiftFile, values) chemShiftFile.chemShifts.append(curShift) elif location == ['spectrum']: if cols[0] == 'name': peakFile.specNames = [cols[1] ] # TODO: or can have space? elif cols[0] == 'pathname': peakFile.specFiles = [cols[1]] elif cols[0] == 'dimension': peakFile.numDims = [returnInt(cols[1])] elif cols[0] == 'points': peakFile.specNumPoints = [returnInts(cols[1:])] elif cols[0] == 'molecule': tempMolName = ' '.join(cols[1:]) if tempMolName not in self.molNames: self.molNames.append(tempMolName) elif cols[0] == 'condition': tempCondName = ' '.join(cols[1:]) if tempMolName == None: tempMolName = '' self.molNames.append(tempMolName) if tempCondName not in self.moleculeConditions[ tempMolName]: self.moleculeConditions[tempMolName].append( tempMolName) self.peaksCondition[peakFile] = [ tempMolName, tempCondName ] elif location == ['spectrum', 'ornament']: if cols[0] == 'type': curType = cols[1] if curType == 'peak': curPeak = SparkyPeak(peakFile, {}) peakFile.peaks.append(curPeak) elif curType == 'peak': if cols[0] == 'id': curPeak.setValue('num', [cols[1]]) elif cols[0] == 'pos': curPeak.setValue('freqDimsPpm', cols[1:]) elif cols[0] == 'height': curPeak.setValue('intensity', [cols[1]]) curPeak.setValue('intensityFit', [cols[2]]) elif cols[0] == 'linewidth': curPeak.setValue('freqDimsWidth', cols[1:]) elif cols[0] == 'integral': curPeak.setValue('volume', [' '.join(cols[1:3])]) elif cols[0] == 'rs': assignment = '-'.join(cols[1:]) assignment = assignment.replace( self.resonanceSep, '') curPeak.setValue('assignment', [assignment]) elif cols[0] == 'fr': pass # TODO: what does this mean? line = fin.readline() if fileType == 'project' and readSaveFiles: (projectDir, fileName) = splitPath(self.name) for saveFile in self.saveFiles: self.name = joinPath(projectDir, saveFile) self.read(fileType='save', saveFileKey=saveFile, verbose=verbose)
def read(self,verbose = 0, cyanaFormat = False): if verbose == 1: print "Reading xeasy peak list %s" % self.name if cyanaFormat: self.cyanaFormat = cyanaFormat fin = open(self.name, 'rU') # Read number of dimensions on first line line = fin.readline() cols = line.split() n = returnInt(cols[-1]) (path,specName) = splitPath(self.name) self.setSpectrumInfo(specName,n) assign = n*[''] ppm = n*[0] line = fin.readline() # Read rest file while line: if not self.patt['hash'].search(line) and not self.patt['emptyline'].search(line): searchObj = self.patt[self.format + 'PeakInfo'].search(line) line = searchObj.group(1) peak_el = line.split() if len(peak_el) > n and peak_el[0] != '0': peaknum = peak_el.pop(0) for i in range(0,n): ppm[i] = peak_el.pop(0) (colour,userCode,volume,volumeError,intCode) = peak_el[0:5] del peak_el[0:6] # deleting extra void code for i in range(0,n): assign[i] = peak_el.pop(0) # # Create the peak and check if valid # peak = XEasyPeak(peaknum,ppm,colour,userCode,volume,volumeError,intCode,assign,verbose=False) if not None in peak.ppm + peak.assign + [peak.num,peak.volume,peak.volumeError,peak.colour]: self.peaks.append(peak) else: if len(peak_el) == n: start = 0 end = n else: start = len(peak_el) - 3 end = len(peak_el) for i in range(start,end): assign[i] = peak_el[i] self.peaks[-1].addAssignment(assign) elif self.patt[self.format + 'IName'].search(line): searchObj = self.patt[self.format + 'IName'].search(line) dim = returnInt(searchObj.group(1)) dimCode = searchObj.group(2) self.dimCodes[dim-1] = dimCode elif self.patt[self.format + 'CyanaFormat'].search(line): self.cyanaFormat = True line = fin.readline() fin.close()
def parseFields(self, fields): # # This is taken almost directly from Wayne's AzaraParams.py file # key = fields[0] assert key in self.allParKeys, 'key = %s' % key parType = None numFields = None for tempParType in self.allPars.keys(): if key in self.parKeys[tempParType]: for parInfo in self.allPars[tempParType]: if key == parInfo[0]: (origName,analysisName,numFields,default) = parInfo parType = tempParType break if parType: break if not numFields: return if numFields == -1: # Basically any number of parameters! valueList = [] for field in fields[1:]: try: x = int(field) except: x = float(field) valueList.append(x) if key == 'params': dim = self.aPars.get('dim') if dim is None: raise Exception("'params' set before 'dim'") npts = self.aPars.get('npts') if not npts or len(npts) <= dim: raise Exception("'params' set before 'npts'") npts = npts[dim] if len(valueList) != npts: raise Exception("'params' set before 'npts'") if parType == 'mainPars': self.setValue(parInfo, tuple(valueList)) elif parType == 'dimPars': assert self.aPars['dim'] >= 0, '"%s" must be after "dim"' % key dim = self.aPars['dim'] self.setListValue(parInfo,dim,tuple(valueList)) # TBD: should this be done with a setValue somehow?? self.aPars['dimType'][dim] = self.dimTypeSampled return assert len(fields) == numFields, 'len(fields) = %d, should be %d' % (len(fields),numFields) if numFields == 1: if (key == 'little_endian'): # Int is now taken care of by dictionary! self.setValue(parInfo,False) else: self.setValue(parInfo, True) elif numFields == 2: value = fields[1] if parType == 'mainPars': if (key == 'ndim'): assert self.aPars['ndim'] == 0, 'repeated "ndim"' self.setValue(parInfo, returnInt(value)) assert self.aPars['ndim'] > 0, 'ndim <= 0' self.initDims() elif (key == 'file'): assert not self.aPars['file'], 'repeated "file"' path = normalisePath(value) if (path and (not os.path.isabs(path)) and os.path.isabs(self.name)): path = joinPath(splitPath(self.name)[0], path) self.setValue(parInfo,path) elif (key == 'head'): assert self.aPars['head'] == 0, 'repeated "head"' self.setValue(parInfo, 4*int(value)) assert self.aPars['head'] >= 0, 'head < 0' elif parType == 'dimPar': if (key == 'dim'): assert self.aPars['ndim'] > 0, '"dim" must be after "ndim"' dim = self.aPars['dim'] self.setValue(parInfo, int(value) - 1) assert self.aPars['dim'] != dim, 'repeated dim %d' % (dim+1) assert self.aPars['dim'] >= 0, 'dim = %d <= 0' % (self.aPars['dim']+1) assert self.aPars['dim'] < self.aPars['ndim'], 'dim = %d > ndim = %d' % (self.aPars['dim']+1, self.aPars['dim']) elif parType == 'dimPars': assert self.aPars['dim'] >= 0, '"%s" must be after "dim"' % key dim = self.aPars['dim'] # can no longer do below because initDims() sets up default values #assert getattr(self, key)[dim] is None, '%s repeated for dim %d' % (key, dim+1) if (key in ('npts','block')): v = int(value) assert v > 0, '%s <= 0 for dim %d' % (key, dim+1) self.setListValue(parInfo,dim,v) elif (key == 'nuc'): self.setListValue(parInfo,dim,standardNucleusName(value)) else: self.setListValue(parInfo,dim,float(value))
def read(self, verbose=0, cyanaFormat=False, cyanaTextAssignments=False): # # Initialisation to deal with CYANA flavours of this format # addText = "" self.cyanaFormat = cyanaFormat if cyanaFormat: addText += ' as CYANA format' self.cyanaTextAssignments = cyanaTextAssignments if cyanaTextAssignments: addText += ' with assignments as text strings' if verbose == 1: print "Reading %s peak list %s%s." % (self.format, self.name, addText) # # Now read file.. # fin = open(self.name, 'rU') # Try to read number of dimensions on first line (default XEASY format) line = fin.readline() cols = line.split() n = returnInt(cols[-1]) # If this didn't work, try something else, could be of #NnH form if not n: if line[0] == '#': dimCodes = line[1:].strip() n = len(dimCodes) print " Warning: irregular format, extracting dimensions from %s string on first first line." % dimCodes if not n: print " Error: number of dimensions could not be determined, aborting file read." return False (path, specName) = splitPath(self.name) self.setSpectrumInfo(specName, n) assign = n * [''] ppm = n * [0] peaknum = None # CYANA 3 files with text assignments have a column less at the end. if cyanaTextAssignments: numFinalColumns = 0 else: numFinalColumns = 1 line = fin.readline() # Read rest file while line: if not self.patt['hash'].search( line) and not self.patt['emptyline'].search(line): searchObj = self.patt[self.format + 'PeakInfo'].search(line) peakLine = searchObj.group(1) peak_el = peakLine.split() if len(peak_el) > n and (peak_el[0] != '0' or peak_el[1] != '0.000') and ( not peaknum or peaknum != peak_el[0]): peaknum = peak_el.pop(0) for i in range(0, n): ppm[i] = peak_el.pop(0) # This is a minimal format definition that came up, is handled now (Wim 18/03/2010) (colour, userCode, volume) = peak_el[0:3] del peak_el[0:3] if peak_el: (volumeError, intCode) = peak_el[0:2] del peak_el[0:3] # deleting extra void code else: volumeError = '0.0' intCode = '-' for i in range(n): if peak_el: assign[i] = peak_el.pop(0) else: assign[i] = '0' # # Create the peak and check if valid # comment = line.replace(peakLine, '').strip() peak = XEasyPeak(peaknum, ppm, colour, userCode, volume, volumeError, intCode, assign, verbose=False, comment=comment, textAssign=self.cyanaTextAssignments) if not None in peak.ppm + peak.assign + [ peak.num, peak.volume, peak.volumeError, peak.colour ]: self.peaks.append(peak) else: if len(peak_el) == n: start = 0 else: start = len(peak_el) - n - numFinalColumns for i in range(n): assign[i] = peak_el[i + start] self.peaks[-1].addAssignment(assign) elif self.patt[self.format + 'IName'].search(line): searchObj = self.patt[self.format + 'IName'].search(line) dim = returnInt(searchObj.group(1)) dimCode = searchObj.group(2) self.dimCodes[dim - 1] = dimCode elif self.patt[self.format + 'CyanaFormat'].search(line): self.cyanaFormat = True line = fin.readline() fin.close()
def read(self, verbose=0): if verbose == 1: print "Reading %s peak list %s" % (self.format, self.name) fin = open(self.name, 'rU') # Read the atom specs on second line line = fin.readline() line = fin.readline() atomlist = line.split() # Initialize # ---------- n = len(atomlist) (path, specName) = splitPath(self.name) self.setSpectrumInfo(specName, n) assign = n * [''] ppm = n * [0] width = n * [0] box = n * [0] o1 = n * [0] shape = n * [''] # TODO: check if dimCodes, sw match experiment when using data from testProject! # Do this in makeTestProject??! for i in range(0, n): self.dimCodes[i] = string.strip(atomlist[i]) line = fin.readline() # Read rest file while line: # Line with peak info if self.patt[self.format + 'DigitSpace'].search(line): # Decompose line cols = line.split() peaknum = cols.pop(0) # This is format for v3 if self.patt[self.format + 'CurlyBrace'].search(line): num_void = 2 # TODO: one of these is peak comment! # Assignments between {} can be separated by columns: rejoin i = 0 while i < len(cols): col = cols[i] # This is an assignment bit if self.patt[self.format + 'CurlyBraceStart'].search(col): # Join with next column if no closing bracket if not self.patt[self.format + 'CurlyBrace'].search(col): cols[i] = string.join( col + ' ' + cols.pop(i + 1), '') continue # If full assignment, get rid of brackets else: cols[i] = self.patt[self.format + 'CurlyBrace'].search( col).group(1) i = i + 1 # This is format for v2 else: num_void = 1 # Get info for each dimension assignItems = -1 for i in range(0, n): (assign[i], ppm[i], width[i], box[i], shape[i]) = cols[:5] # Check whether assignments are all same length... if assignItems == -1: assignItems = len(assign[i].split(self.assignTagSep)) else: if len(assign[i].split( self.assignTagSep)) != assignItems: self.hasAssignItems = False del cols[0:(5 + num_void)] # Get remaining info (volume, intensity, status) = cols[0:3] # Only collect non-deleted peaks if status != -1: peak = NmrViewPeak(peaknum, assign, ppm, width, box, shape, volume, intensity, status) if not None in (peak.num, peak.status): self.peaks.append(peak) # Line with spectrum info elif self.patt[self.format + 'CurlyBrace'].search( line) or self.patt[self.format + 'NumbersNoBrace'].search(line): # Keeps reported sw in memory for validation purposes self.sws = self.getValues(line) line = fin.readline() # Keeps o1 of spectrum in MHz in memory.for ppm-Hz conversion # Also for validation purposes self.sfs = self.getValues(line) # TODO: check this O1 against value from API! line = fin.readline()
def read(self, verbose=0): if verbose == 1: print "Reading %s peak list %s" % (self.format, self.name) # Initialize # ---------- (path, specName) = splitPath(self.name) self.setSpectrumInfo(specName, 3) fin = open(self.name) reader = Marvin_reader() reader.read(fin) handler = Print_assignments(returnValue=True) # TODO: This is not correct - should be able to import the whole peak list if required! for peak in reader.records: if 'peakAssign' in peak: for assignment in peak['peakAssign']: likelihood = assignment['likelihood'][0].as_float() # hack indeting problems and unneeded if 1: #if likelihood > probability: #0 1 2 3 4 5 #peak_id,from_proton_shift,from_heavy_shift,to_proton_shift,from_assignment.shift_id,from_assignment.residue_id, #6 7 8 9 #from_assignment.residue_type,from_assignment.atom_name,to_assignment.shift_id,to_assignment.residue_id, #10 11 12 13 14 #to_assignment.residue_type,to_assignment.atom_name,probability,from_assignment.residue_type, to_assignment.residue_type values = handler.handle_assignment(peak, assignment) peakId = returnInt(values[0]) # Dangerous, works for most things but really needs a table driven approach heavy_name = None #convertAtomNameByType(self,values[13],values[7]) light_name = values[7] # if light_name== 'HN': # heavy_name='N' # elif light_name[-1].isdigit() or light_name[-1] == '#': # heavy_name = light_name[:-1] # heavy_name=heavy_name.replace('H','C') # else: # heavy_name=light_name.replace('H','C') if values[2] != None: assign = [ self.getAssignString( values[5], 'LIGHT:' + values[13] + ':' + values[7]), self.getAssignString( values[5], 'HEAVY:' + values[13] + ':' + values[7]), self.getAssignString( values[9], 'LIGHT:' + values[14] + ':' + values[11]) ] ppm = values[1:4] label = [values[6], values[6], values[10]] else: assign = [ self.getAssignString( values[5], 'LIGHT:' + values[13] + ':' + values[7]), self.getAssignString( values[9], 'LIGHT:' + values[14] + ':' + values[11]) ] ppm = [values[1], values[3]] label = [values[6], values[10]] #print values[0],assign, values[5],values[7],values[9],values[11],heavy_name if peakId not in self.peakById: newPeak = MarvinPeak(peakId, assign, ppm, label, likelihood) print(peakId, assign, ppm, label, likelihood) self.peaks.append(newPeak) self.peakById[peakId] = self.peaks[-1] else: self.peakById[peakId].addAssignment( assign, label, likelihood) #print assign fin.close()