def findTopObjectPath(repositoryPath, topObject): """Get topObject absolute file path given the repositoryPath, where topObject can be of class MemopsRoot or TopObject. Will find an existing file fitting the TopObject ID. If none is found returns default file name """ suffix = fileSuffix lenSuffix = lenFileSuffix sep = separatorFileNameChar if topObject.root is topObject: # MemopsRoot objId = topObject.name else: # other TopObject objId = topObject.guid # get default file name topObjectDir = uniIo.joinPath(repositoryPath, *topObject.packageName.split('.')) result = uniIo.joinPath(topObjectDir, getTopObjectFile(topObject)) if not os.path.isfile(result): # default file name is not there. Look for alternative file that fits ID if os.path.isdir(topObjectDir): for filename in os.listdir(topObjectDir): if filename.endswith(suffix): if filename.split(sep)[-1][:-lenSuffix] == objId: result = os.path.join(topObjectDir, filename) break # return whatever result we have return result
def getCcpForgeUrls(molType, ccpCode, sourceName=None): """ Creates the URL info for ChemComp(Coord) downloads from CcpForge """ ccpForgeUrl = "http://ccpforge.cse.rl.ac.uk/gf/project/ccpn-chemcomp/scmcvs/?action=browse&root=ccpn-chemcomp&pathrev=MAIN&path=/" checkOutDir = "~checkout~" archiveDir = "ccpn-chemcomp/data/pdbe/chemComp/archive/" #%2Acheckout%2A%2Fccpn-chemcomp%2Fdata%2Fpdbe%2FchemComp%2Farchive%2FChemComp%2Fprotein%2Fprotein%252B004%252Bpdbe_ccpnRef_2010-09-23-14-41-20-237_00001.xml&revision=1.1 if not sourceName: fileType = 'ChemComp' subPath = getChemCompArchiveXmlFilePath("", molType, ccpCode) else: fileType = 'ChemCompCoord' subPath = getChemCompCoordArchiveXmlFilePath("", sourceName, molType, ccpCode) # wb104: 15 Dec 2010: need to use joinPath because otherwise does not # work on Windows, but cannot stick ccpForgeUrl in joinPath() because # that would convert http:// to http:/ ccpForgeDirUrl = ccpForgeUrl + uniIo.joinPath(archiveDir, fileType, subPath) ccpForgeDownloadUrl = ccpForgeUrl + uniIo.joinPath(checkOutDir, archiveDir, fileType, subPath) return (fileType, ccpForgeDirUrl, ccpForgeDownloadUrl)
def testNameParsing(dirPath): """ Frame code to run name testing """ # set of imput info stdInfoFile = uniIo.joinPath(uniIo.getTopDirectory(), 'data', 'ccp', 'bruker', 'BrukerPulseProgMap.xml') fileNames = os.listdir(dirPath) fileNames.sort() parseInfo = ccpGenUtil.loadAllParseInfo(stdInfoFile) useInfo = ccpGenUtil.convertParseInfo(parseInfo) # set up mapId:list dictionary matchByMap = {} notmatched = [] skipped = [] for expNameMap in parseInfo['expNameMaps']: matchByMap[id(expNameMap)] = [] # match filenames and store by matchDict for fname in fileNames: dd = matchName(fname, useInfo, specialCases) dd['fname'] = fname expNameMap = dd.get('expNameMap') if (dd.get('status') == 'skip' or not os.path.isfile(uniIo.joinPath(dirPath,fname))): skipped.append(dd) elif expNameMap is None: notmatched.append(dd) else: matchByMap[id(expNameMap)].append(dd) #print fname, dd['matches'], dd['names'], dd['unused'] # print result print '\nSKIPPED:' for dd in skipped: print ' - ', dd['fname'], dd.get('usenames'), dd.get('prefixes'), dd.get('unused') print '\nNO MATCH:' for dd in notmatched: print ' - ', dd['fname'], dd.get('usenames'), dd.get('prefixes'), dd.get('unused') unused = [] for xmap in parseInfo['expNameMaps']: ll = matchByMap[id(xmap)] prototypes = [(x.get('name'), x.get('isReversed')) for x in xmap['expPrototypes']] if ll: print '\nMATCH:', xmap.get('namesStartWith'), xmap.get('hasnames'), xmap.get('hasprefixes') print ' ', prototypes for dd in ll: if dd.get('unused'): ss = ' NB - ' else: ss = ' - ' print ss, dd['fname'], dd.get('usenames'), dd.get('prefixes'), dd.get('unused') for xmap in unused: print '\nUNUSED', xmap.get('namesStartWith'), xmap.get('hasnames'), xmap.get('hasprefixes') print ' ', [(x.get('name'), x.get('isReversed')) for x in xmap['expPrototypes']]
def getChemCompArchiveXmlFilePath(chemCompPath, molType, ccpCode): chemCompXmlFilePath = uniIo.joinPath(chemCompPath, molType) if molType == 'other': chemCompXmlFilePath = uniIo.joinPath(chemCompXmlFilePath, ccpCode[0]) return chemCompXmlFilePath
def prepareLocalExecution(nmrCalcRun, targetDir): """ return [procArgs, targetDir, logFile list for later, local execution And carry out any preliminary commands """ # Set up parameters for program call shellParObj = nmrCalcRun.findFirstRunParameter(name='programCall') if shellParObj is None: raise Exception("Parameter name=programCall not found") else: shellCall = shellParObj.textValue executeScript = os.path.join(os.path.expandvars(programBin), shellCall) executeScript2 = os.path.join(os.path.expandvars(programBin), 'cyanatable') fileNameObj = nmrCalcRun.findFirstRunParameter(name='fileNameSetup') if fileNameObj is None: raise Exception("no 'fileNameSetup' parameter found") filePath = uniIo.joinPath(targetDir, fileNameObj.textValue) procargs = [executeScript, filePath] procargs2 = [executeScript2] # return (procargs, targetDir, procargs2)
def getDataPath(*args): """ Gives location of data path. Extra args are added on as extra directories Result ends with the last arg (which might be either a file or a dictionary) """ dataPath = uniIo.joinPath(uniIo.getTopDirectory(), 'data', *args) return dataPath
def parsePulProgName(name, fileLocation=None): """ Input: pulse program name to be parsed location of pulse program map file - if not given use standard file Output: List of (expName,isReversed) tuples expName can be either an NmrExpPrototype name or a RefExperiment name. isReversed can be True, False, or None (meaning not set). Caches pulse program parsing map """ global pulProgNameMap # get pulse program name translation map if fileLocation is None: if pulProgNameMap is None: fileLocation = uniIo.joinPath(uniIo.getTopDirectory(), 'data', 'ccp', 'bruker', 'BrukerPulseProgMap.xml') parseInfo = ccpGenUtil.loadAllParseInfo(fileLocation) pulProgNameMap = ccpGenUtil.convertParseInfo(parseInfo) else: parseInfo = ccpGenUtil.loadAllParseInfo(fileLocation) pulProgNameMap = ccpGenUtil.convertParseInfo(parseInfo) # get match result matchMap = matchName(name, pulProgNameMap, specialCases) dd = matchMap return matchMap['matches']
def prepareLocalExecution(nmrCalcRun, targetDir): """ return [procArgs, targetDir, logFile list for later, local execution And carry out any preliminary commands """ # Set up parameters for program call shellParObj = nmrCalcRun.findFirstRunParameter(name='programCall') if shellParObj is None: raise Exception("Parameter name=programCall not found") else: shellCall = shellParObj.textValue executeScript = os.path.join(os.path.expandvars(programBin), shellCall) fileNameObj = nmrCalcRun.findFirstRunParameter(name='fileNameSetup') if fileNameObj is None: raise Exception("no 'fileNameSetup' parameter found") filePath = uniIo.joinPath(targetDir, fileNameObj.textValue) procargs = [executeScript, '-f', filePath] algObj = nmrCalcRun.findFirstRunParameter(name='MDAlgorithm') if 'XPLOR' in algObj.textValue.upper(): # copy xplor template files for fname in ('sa.inp', 'generate.inp', 'generate_template.inp'): src = os.path.join(os.path.expandvars(unioXplor), fname) shutil.copy2(src, targetDir) # return (procargs, targetDir)
def read(nmrCalcRun, dataDir): """ Read Output files for Unio run Input: nmrCalcRun: NmrCalc.Run dataDir: directory directly containing program output. """ fileNames = stdFileNames.copy() # make summary text element summary = "" for tag in ('overview', ): path = uniIo.joinPath(dataDir, fileNames[tag]) if os.path.isfile(path): txt = open(path).read() else: txt = 'WARNING, file % not found' % fileNames[tag] summary += ('-' * 80 + "\nFROM FILE : %s\n----------------\n%s\n\n\n" % (fileNames[tag], txt)) nmrCalcRun.newRunParameter(name='summary', ioRole='output', textValue=summary) # Get MolSystem molResidueData = ( nmrCalcRun.findFirstData(className='MolResidueData', ioRole='input') or nmrCalcRun.findFirstData(className='MolSystemData', ioRole='input')) molSystem = molResidueData.molSystem # read pdb structures path = uniIo.joinPath(dataDir, fileNames['pdbBundle']) if os.path.isfile(path): ensemble = StructureIo.getStructureFromFile(molSystem, path) # make EnsembleData nmrCalcRun.newStructureEnsembleData(name='result', ioRole='output', structureEnsemble=ensemble, details="Unio calculated structures") # Make Nmr.StructureCalculation nmrProject = nmrCalcRun.nmrCalcStore.nmrProject nmrCalcRun.structureGeneration = nmrProject.newStructureGeneration( generationType='denovo', name='UNIO', structureEnsemble=ensemble)
def getChemCompOrCoordXmlFile(repository, fileSearchString, fileSearchPath, className, identifier, showError, copyFile): result = None if showError is None: showError = uniIo.printError # Try to find file... import glob fileNameMatches = glob.glob( uniIo.joinPath(fileSearchPath, fileSearchString)) if fileNameMatches: if len(fileNameMatches) > 1: errorText = "Error: multiple matches found for %s %s - taking last one." % ( className, identifier) showError("Multiple %s matches" % className, errorText) filePath = fileNameMatches[-1] (fileDir, fileName) = os.path.split(filePath) # # Copy file if found... # if os.path.exists(filePath): if copyFile: savePath = repository.getFileLocation('ccp.molecule.%s' % className) if not os.path.exists(savePath): os.makedirs(savePath) import shutil saveFilePath = uniIo.joinPath(savePath, fileName) shutil.copy(filePath, saveFilePath) result = saveFilePath print " %s file %s copied to %s..." % (className, fileName, savePath) else: result = filePath return result
def saveProject(project, name='test', subDir='data'): # Find the backup repository and set the right path for it. backupRepos = project.findFirstRepository(name='backup') backupPath = uniIo.joinPath(os.getcwd(), subDir, name + '_backup') # Create a URL for the backup repository. backupRepos.url = Url(path=backupPath) # Do the same for the repository containing the data in the project. projectRepos = project.findFirstRepository(name='userData') newPath = uniIo.joinPath(os.getcwd(), subDir, name) # And also for the URL of this data. projectRepos.url = Url(path=newPath) # This saves the project to the path specified by the URL for the # 'userData' repository. project.saveModified()
def oldwrite(nmrCalcRun, topDir=None): """ Write input files for Program run Input: nmrCalcRun: NmrCalc.Run topDir: optional destination directory. """ targetDir = intIo.createTargetDir(nmrCalcRun, topDir=topDir) # write Properties file propDict, dummy = intIo.makeParameterDict(nmrCalcRun) propFile = uniIo.joinPath(targetDir, intIo.propFileName) open(propFile,'w').write(json.dumps(propDict, sort_keys=True, indent=intIo.propIndent)) # Write data file # get file inpFile = propDict.get('INPUT_FILE') if inpFile: talosInputFile = uniIo.joinPath(targetDir, inpFile) else: raise Exception("No code=INPUT_FILE RunParameter in nmrCalcRun") # Get shiftList shiftList = intUtil.getMeasurementList(nmrCalcRun) if shiftList is None: raise Exception("Run must have exactly one shift list, %s found" % len(ll)) # Get residues ll = list(nmrCalcRun.findAllData(className='MolResidueData')) if len(ll) == 1: obj = ll.pop() residues = list(obj.residues) residues.sort(key=operator.attrgetter('seqId')) if len(set(obj.chainCodes)) != 1: raise Exception("Run MolResidueData did not have a (single) chain" ) else: raise Exception("Run must have excactly one MolResidueData, %s found" % len(ll)) # talosIo.writeShiftFile(open(talosInputFile,'w'), residues, shiftList, propDict.get('minShiftQuality'))
def getProjectFile(repositoryPath, projectName=None): """Get project file given the repositoryPath and optionally the projectName (if none given then determined from repositoryPath) """ if not projectName: projectName = os.path.basename(repositoryPath) implDirectory = getImplementationDirectory(repositoryPath) return uniIo.joinPath(implDirectory, projectName + fileSuffix)
def write(nmrCalcRun, targetDir): """ Write input files for Program run Input: nmrCalcRun: NmrCalc.Run targetDir: destination directory. """ intIo.writeDataFiles(nmrCalcRun, targetDir) jsonDict = intIo.makeJsonDict(nmrCalcRun) # write properties file propFile = uniIo.joinPath(targetDir, intIo.propFileName) open(propFile, 'w').write( json.dumps(jsonDict, sort_keys=True, indent=intIo.propIndent)) # Write program setup file fileNameObj = nmrCalcRun.findFirstRunParameter(name='fileNameSetup') if fileNameObj is not None: filePath = uniIo.joinPath(targetDir, fileNameObj.textValue) writeSetupFile(filePath, jsonDict)
def write(nmrCalcRun, targetDir): """ Write input files for Program run Input: nmrCalcRun: NmrCalc.Run targetDir: destination directory. """ intIo.writeDataFiles(nmrCalcRun, targetDir) jsonDict = intIo.makeJsonDict(nmrCalcRun) # write properties file propFile = uniIo.joinPath(targetDir, intIo.propFileName) print 'About to write', propFile open(propFile, 'w').write( json.dumps(jsonDict, sort_keys=True, indent=intIo.propIndent)) # Write program setup file fileNameObj = nmrCalcRun.findFirstRunParameter(name='fileNameSetup') importJsonName = 'cyana2ccpn.json' if fileNameObj is not None: # write init.cya initFile = uniIo.joinPath(targetDir, initFileName) rmsdrange = jsonDict['RunParameter'].pop('rmsdrange') seqFile = jsonDict['RunParameter'].pop('fileNameSequence') text = """# Cyana init file - Generated by CcpNmr Integrator rmsdrange:=%s cyanalib read seq %s """ % (rmsdrange, seqFile) open(initFile, 'w').write(text) # write main command file filePath = uniIo.joinPath(targetDir, fileNameObj.textValue) importFilePath = uniIo.joinPath(targetDir, importJsonName) writeSetupFile(filePath, jsonDict) writeImportJson(importFilePath, jsonDict)
def getDetails(self, fullfile): details = '' if os.path.isfile(fullfile): format = self.formatPulldown.getText() detailsDir = os.path.dirname(fullfile) detailsFile = uniIo.joinPath(detailsDir, details_file_dict[format]) if os.path.exists(detailsFile): fp = open(detailsFile) details = fp.read().strip().replace('\n', ' ').replace('\r', ' ') fp.close() return (details, )
def getPossibleProjectFiles(repositoryPath): """Get the possible project files given the repositoryPath """ if os.path.isdir(repositoryPath): implDirectory = getImplementationDirectory(repositoryPath) if os.path.isdir(implDirectory): files = os.listdir(implDirectory) files = [ uniIo.joinPath(implDirectory, file) for file in files if file.endswith(fileSuffix) ] return files return []
def write(nmrCalcRun, targetDir): """ Write input files for Program run Input: nmrCalcRun: NmrCalc.Run targetDir: destination directory. """ #intIo.writeDataFiles(nmrCalcRun, targetDir) jsonDict = intIo.makeJsonDict(nmrCalcRun) # write properties file propFile = uniIo.joinPath(targetDir, intIo.propFileName) print 'About to write', propFile open(propFile, 'w').write( json.dumps(jsonDict, sort_keys=True, indent=intIo.propIndent))
def getObjFileName(self, metaObj, absoluteName=True, addSuffix=False): """ Get filename for metaObj If absoluteName is True the file name is absolute otherwise it is relative to the relevant baseDir """ # absoulte or relative path if absoluteName: pathList = [self.rootDirName, self.baseDirName] else: pathList = [] if metaObj.container is None: # Root package only pathList.append(ImpConstants.modellingPackageName) if self.codeDirName: pathList.append(self.codeDirName) pathList.append(self.rootFileName) else: # any other object ll = metaObj.qualifiedName().split('.') if self.codeDirName: ll[1:1] = [self.codeDirName] pathList.extend(ll) # special handling for ModelElements that may correspond to directories: if (isinstance(metaObj, MetaModel.MetaPackage) and (metaObj.containedPackages or not self.classesInPackage)): pathList.append(self.packageFile) elif not isinstance(metaObj.container, MetaModel.MetaPackage): raise MemopsError( " file names not implemented for objects of type %s" % (metaObj.__class__.__name__, )) # add suffix if addSuffix and self.fileSuffix: pathList[-1] = '%s.%s' % (pathList[-1], self.fileSuffix) # return uniIo.joinPath(*pathList)
def fullLoadValidationStore(topObj): """hard load ValidationStore from old location """ from memops.format.xml import Util as xmlUtil from memops.universal import Io as uniIo from memops.format.xml import XmlIO root = topObj.memopsRoot locator = ( root.findFirstPackageLocator(targetName='ccp.molecule.Validation') or root.findFirstPackageLocator(targetName='any')) repository = locator.findFirstRepository() #repository = topObj.activeRepositories[0] fileLocation = repository.getFileLocation( 'ccp.molecule.StructureValidation') filePath = uniIo.joinPath(fileLocation, xmlUtil.getTopObjectFile(topObj)) XmlIO.loadFromStream(open(filePath), topObject=topObj, topObjId=topObj.guid, partialLoad=False)
def areAllTopObjectsPresent(project): """ Input: project Output: Boolean - True if all loaded TopObjects exist in storage """ # set up findLocator = project.findFirstPackageLocator anyLocator = findLocator(targetName='any') allLocations = {} # check for topObject presence result = True for topObject in project.topObjects: if topObject is not project and not topObject.isLoaded: # get locations locator = findLocator( targetName=topObject.packageName) or anyLocator locations = allLocations.get(locator) if locations is None: locations = [ x.url.getDataLocation() for x in locator.repositories ] allLocations[locator] = locations # check for file presence ll = topObject.packageName.split('.') ll.append(getTopObjectFile(topObject)) for location in locations: if os.path.isfile(uniIo.joinPath(location, *ll)): # file found break else: # no file found result = False break # return result
# # Make sure it saves the information in the projectDir # To do this on the Python level you have to reset the path # for the urls that indicate the directory locations for the # saved files... # # Alternatively create the project in the right directory to # start with - see convertCns2Pdb # for repository in ccpnProject.repositories: if repository.name in ('userData','backup'): (oldUrlPath,baseName) = uniIo.splitPath((repository.url.path)) newUrlPath = uniIo.joinPath(projectDir,baseName) repository.url = Implementation.Url(path = newUrlPath) # # Create main Tk window for popups # gui = Tkinter.Tk() # # Create an NmrView class and read in the files... create an experiment # for the peak list beforehand and pass in the parameters. # nmrViewFormat = NmrViewFormat(ccpnProject,gui)
# # Because the original project was saved in a different directory structure, # the paths have to be reset first... here I am resetting everything 'hardcoded' # based on the examples and/or data directory. # localDataPaths = {} localDataPaths['examples'] = os.path.abspath('.') localDataPaths['data'] = joinPath(getTopDirectory(),'data') for repository in ccpnProject.repositories: for findName in ('examples','data'): findNameIndex = repository.url.path.find(findName) if findNameIndex>= 0: repository.url = Implementation.Url(path = uniIo.joinPath(localDataPaths[findName],repository.url.path[findNameIndex + len(findName) + 1:])) break # # Print out some objects from this project # print "Project object:", ccpnProject print "Project name:", ccpnProject.name # # Note that there is a pause after the above is printed - this is because of # 'lazy loading' the API will now have to read the MolSystem.xml file before it # can print the information in there. # for mol in ccpnProject.molecules:
def openSpectra(self): noVerify = self.verifySelect.getSelected() # tracks if 'add to existing experiment' has already ben OK'ed self.okExpSet = set() directory = self.fileSelect.getDirectory() spectra = [] specIndex = 0 for obj in self.scrolledMatrix.objectList: fileName = uniIo.joinPath(directory, obj.fileName) spectrum = self.openSpectrum(obj.exptName, obj.specName, fileName, obj.window, obj.shiftListName) specIndex += 1 if (spectrum): # check endianness if we are not verifying spectra.append(spectrum) if noVerify: isBigEndian = isSpectrumBigEndian( spectrum) # according to data in file if isBigEndian is not None: isBigEndianCurr = getIsSpectrumBigEndian( spectrum) # according to data model setIsSpectrumBigEndian(spectrum, isBigEndian) if isBigEndian != isBigEndianCurr: if isBigEndian: s = 'big' else: s = 'little' print 'WARNING: swapped endianess of spectrum to %s endian' % s # del self.okExpSet if noVerify and len(spectra) > 1 and self.sharedExpSelect.getSelected( ): # if we are using a shared experiment and not verifying, # set referencing to match first spectrum for all # get reference spectrum and set up data structure # use most recent pre-existing spectrum, otherwise first new one refSpec = spectra[0] for spec in spectra[0].experiment.sortedDataSources(): if spec in spectra: break else: refSpec = spec ddrLists = {} refDdrs = [] for dataDim in refSpec.sortedDataDims(): for ddr in dataDim.dataDimRefs: ddrLists[ddr.expDimRef] = [] refDdrs.append(ddr) # get dataDimRefs, store by ExpDimRef, # checking that all spectra have data dim refs for same set of xdr nTotal = len(ddrLists) for spec in spectra: nFound = 0 for dataDim in spec.sortedDataDims(): for ddr in dataDim.dataDimRefs: xdr = ddr.expDimRef ll = ddrLists.get(xdr) if ll is None: # something did not match - do nothing break else: ll.append(ddr) nFound += 1 else: if nFound == nTotal: # we are OK. Do next spectrum continue # something did not match - do nothing break else: # all spectra matched. Now reset O1 references to match reference if refSpec is spectra[0]: startAt = 1 else: startAt = 0 for refDdr in refDdrs: dataDim = refDdr.dataDim centrePoint = dataDim.numPointsOrig / 2 - dataDim.pointOffset + 1 refValue = refDdr.pointToValue(centrePoint) xdr = refDdr.expDimRef for ddr in ddrLists[xdr][startAt:]: dataDim = ddr.dataDim centrePoint = dataDim.numPointsOrig / 2 - dataDim.pointOffset + 1 ddr.refPoint = centrePoint ddr.refValue = refValue # set refExperiment if there is only one possibility experiments = [] ignoreSet = set() showPopup = False for spectrum in spectra: experiment = spectrum.experiment if experiment not in ignoreSet: ignoreSet.add(experiment) if not experiment.refExperiment: experiments.append(spectrum.experiment) if noVerify: resetCategory = False if not hasattr(experiment, 'category'): if (hasattr(experiment, 'pulProgName') and hasattr(experiment, 'pulProgType')): # this is first time we get here, and we have external name and source # use external source to set fullType experiment.category = 'use external' resetCategory = True refExperiments = getRefExperiments(experiment) if resetCategory and not refExperiments: # no refExperiments match external source. # unset 'use external' category del experiment.category if len(refExperiments) == 1: # only one possibility, just set it setRefExperiment(experiment, refExperiments[0]) # wb104: 20 Oct 2014: do not popup Experiment types dialog if noVerify #else: # showPopup = True # Pop up refExperiment verification if experiments and (showPopup or not noVerify): self.parent.initRefExperiments(experiments) # set up internal Analysis data for spectrum in spectra: self.parent.finishInitSpectrum(spectrum) print 'finished opening spectrum', spectrum.experiment.name, spectrum.name
def findDataStoringFromFilepath(project, fullFilePath, preferDataUrls=None, dataLocationStore=None, keepDirectories=1): """ Get DataUrl and relative filePath from normalised absolute filePath Uses heuristics to select compatible DataUrl from existing ones. sisterObjects is a collection of objects with a dataStore link - DataUrls in use for sisterObjects are given preference in the heuristics. uses dataLocationStore or current dataLocationStore If no compatible DataUrl is found the routine returns dataUrl None and the file name plus the lowest keepDirectories directories as the filePath """ # NB fullFilePath *must* be absolute her for code to work properly # if not os.path.isabs(fullFilePath): raise ApiError( "findDataStoringFromFilepath called with non-absolute file name %s" % fullFilePath) # get DataLocationStore if not dataLocationStore: setCurrentStore(project, 'DataLocationStore') dataLocationStore = project.currentDataLocationStore # get DataUrl that match fullFilePath dataUrls = [] for dataUrl in dataLocationStore.dataUrls: dirPath = uniIo.normalisePath(dataUrl.url.path) if fullFilePath.startswith(dirPath): lenPath = len(dirPath) ss = fullFilePath while len(ss) > lenPath: ss, junk = uniIo.splitPath(ss) if ss == dirPath: # DataUrl path matches file path dataUrls.append(dataUrl) # process result if dataUrls: if preferDataUrls: # look for DataUrls that are in use with related objects ll = [x for x in dataUrls if x in preferDataUrls] if ll: dataUrls = ll if len(dataUrls) == 1: # only one DataUrl - use it dataUrl = dataUrls[0] else: # use DataUrl with longest path ll = [(len(dataUrl.url.path), dataUrl) for dataUrl in dataUrls] ll.sort() dataUrl = ll[-1][1] # get filePath ss = uniIo.joinPath(dataUrl.url.path, '') # removes file separator from end filePath = fullFilePath[len(ss) + 1:] else: dataUrl = None ll = [] ss = fullFilePath for dummy in range(keepDirectories + 1): ss, name = os.path.split(ss) ll.append(name) ll.reverse() filePath = uniIo.joinPath(*ll) # return (dataUrl, filePath)
def getImplementationDirectory(repositoryPath): """Get implementation directory from the repositoryPath """ return uniIo.joinPath(repositoryPath, ImpConstants.modellingPackageName, ImpConstants.implementationPackageName)
def chooseFiles(self, forceUpdate=False, *file): directory = self.fileSelect.getDirectory() fileNames = self.fileSelect.fileList.currentObjects fullFileNames1 = [uniIo.joinPath(directory, x) for x in fileNames] fullFileNames2 = [x.fileName for x in self.scrolledMatrix.objectList] fullFileNames2 = [uniIo.joinPath(directory, x) for x in fullFileNames2] if fullFileNames1 == fullFileNames2 and not forceUpdate: return objectList = [] textMatrix = [] format = self.formatPulldown.getText() shiftListName = self.getShiftLists()[0][0] windowOpt = WINDOW_OPTS[1] oneUp = os.path.dirname if format == 'Bruker': if self.sharedExpSelect.getSelected(): nameTemplate = 'Bruker_%d' next = self.getNextExpNum(nfiles=len(fileNames), nameTemplate=nameTemplate) exptName = nameTemplate % (next) for i, fileName in enumerate(fileNames): fullFileName = fullFileNames1[i] specName = os.path.basename( oneUp(oneUp(oneUp(fullFileName)))) datum = (exptName, specName, fileName, windowOpt, shiftListName) dataObj = RowObject(*datum) textMatrix.append(datum) objectList.append(dataObj) else: for i, fileName in enumerate(fileNames): fullFileName = fullFileNames1[i] try: # below should not fail ss1 = oneUp(fullFileName) specName = os.path.basename(ss1) ss2 = os.path.basename(oneUp(oneUp(ss1))) exptName = 'Bruker_' + ss2 except: # just put in something ss = os.path.basename(fullFileName) exptName = 'Bruker_' + ss specName = ss datum = (exptName, specName, fileName, windowOpt, shiftListName) dataObj = RowObject(*datum) textMatrix.append(datum) objectList.append(dataObj) else: next = self.getNextExpNum(nfiles=len(fileNames)) if self.sharedExpSelect.getSelected(): exptName = 'Expt_%d' % (next) for i, fileName in enumerate(fileNames): specName = re.sub('\.\w+$', '', fileName) datum = (exptName, specName, fileName, windowOpt, shiftListName) dataObj = RowObject(*datum) textMatrix.append(datum) objectList.append(dataObj) else: for i, fileName in enumerate(fileNames): exptName = 'Expt_%d' % (next + i) specName = re.sub('\.\w+$', '', fileName) datum = (exptName, specName, fileName, windowOpt, shiftListName) dataObj = RowObject(*datum) textMatrix.append(datum) objectList.append(dataObj) if len(fileNames) > 1: self.openButton.config(text='Open Spectra') else: self.openButton.config(text='Open Spectrum') self.scrolledMatrix.update(objectList=objectList, textMatrix=textMatrix)
################### # Main of program # ################### if __name__ == "__main__": # # Variables... # dataDir = 'data/' sequenceFileName = 'fasta.seq' currentDir = os.path.abspath('.') projectDir = uniIo.joinPath((currentDir, 'local')) projectName = 'testImportSequence' # # Make sure the projectDir exists and delete existing data # if not os.path.exists(projectDir): os.mkdir(projectDir) projectPath = os.path.join(projectDir, projectName) if os.path.exists(projectPath): shutil.rmtree(projectPath) # # Create a CCPN project. This sets default save locations in the
# # Make sure it saves the information in the projectDir # To do this on the Python level you have to reset the path # for the urls that indicate the directory locations for the # saved files... # # Alternatively create the project in the right directory to # start with - see convertCns2Pdb # for repository in ccpnProject.repositories: if repository.name in ('userData', 'backup'): (oldUrlPath, baseName) = uniIo.splitPath(repository.url.path) newUrlPath = uniIo.joinPath(projectDir, baseName) repository.url = Implementation.Url(path=newUrlPath) # # Open a Tk window for handling the popups... # root = Tkinter.Tk() # # Create the FormatConverter CnsFormat object # cnsFormat = CnsFormat(ccpnProject, root)
return True if __name__ == '__main__': from memops.format.xml import Util as xmlUtil from memops.format.xml import XmlIO from memops.universal import Io as uniIo from memops.api import Implementation import Tkinter # standard project name projectName = 'refIsotopeEditor' # get or make project repDir = uniIo.joinPath(uniIo.normalisePath(uniIo.os.getcwd()), projectName) projectFile = xmlUtil.getProjectFile(repDir, projectName=projectName) if os.path.isfile(projectFile): project = XmlIO.loadProjectFile(projectFile) else: project = Implementation.MemopsRoot(name=projectName) # reorder to make sure data are saved in refData labelingSchemeLocator = project.findFirstPackageLocator( targetName='ccp.molecule.ChemCompLabel') repositories = list(labelingSchemeLocator.repositories) for rr in repositories: if rr.name == 'refData': refRepository = rr break else:
################### # Main of program # ################### if __name__ == "__main__": # # Variables... # dataDir = 'data/' sequenceFileName = 'fasta.seq' currentDir = os.path.abspath('.') projectDir = uniIo.joinPath((currentDir,'local')) projectName = 'testImportSequence' # # Make sure the projectDir exists and delete existing data # if not os.path.exists(projectDir): os.mkdir(projectDir) projectPath = os.path.join(projectDir,projectName) if os.path.exists(projectPath): shutil.rmtree(projectPath) # # Create a CCPN project. This sets default save locations in the
def read(nmrCalcRun, dataDir): """ Read Output files for Rosetta run Input: nmrCalcRun: NmrCalc.Run dataDir: directory directly containing program output. """ # set up files = {} data = {} fileNames = stdFileNames.copy() #fileNames['properties'] = intIo.propFileName # check presence of files: if not os.path.isdir(dataDir): raise IOError("Data directory %s not found" % dataDir) for tag in ( 'scores', 'rms', 'rawRms', ): ss = uniIo.joinPath(dataDir, fileNames[tag]) files[tag] = ss if not os.path.isfile(ss): raise IOError("%s file %s not found in directory %s" % (tag, ss, dataDir)) # check NmrCalcRun: if not intUtil.runIsWritable(nmrCalcRun): raise Exception("Cannot read into %s - has already been used" % nmrCalcRun) # get simple data records #ss = fileNames.get('input') #if ss: # path = uniIo.joinPath(dataDir, ss) # if os.path.isfile(path): # data = open(path).read() # nmrCalcRun.newRunParameter(name='inputUsed', code=stdFileNames['input'], # ioRole='output', textValue=data, # ) #details='Input data, as used by Rosetta') #NBNB TBD remove. Not there. Properties is an input thing. #ss = fileNames.get('properties') #if ss: # path = uniIo.joinPath(dataDir, ss) # if os.path.isfile(path): # data = open(ss).read() # nmrCalcRun.newRunParameter(name='propertiesUsed', # code=stdFileNames['properties'], # ioRole='output', textValue=data, # details='Properties file, as used by Rosetta') # read data arrays and sort by Rosetta score scoreFile = open(files['scores']) rmsFile = open(files['rms']) rawRmsFile = open(files['rawRms']) data = [] for ss in scoreFile: rms = float(rmsFile.next().split()[1]) rawRms = float(rawRmsFile.next().split(None, 1)[0]) tt = ss.split() # Order is: score, rawscore shiftChi2, rms, rawrms, name data.append( (float(tt[3]), float(tt[1]), float(tt[2]), rms, rawRms, tt[0])) data.sort() scores, rawScores, shiftChi2, rms, rawRms, names = zip(*data) # Set scores data in NmrCalc matrices docTemplate = ( "Rosetta %s for all calculated structures, sorted by corrected score") ss = 'corrected scores' nmrCalcRun.newFloatMatrixData(code='scores', name=ss, ioRole='output', shape=(0, ), data=scores, details=docTemplate % ss) ss = 'raw scores' nmrCalcRun.newFloatMatrixData(code='rawScores', name=ss, ioRole='output', shape=(0, ), data=rawScores, details=docTemplate % ss) ss = 'shift chi2 scores' nmrCalcRun.newFloatMatrixData(code='shiftChi2', name=ss, ioRole='output', shape=(0, ), data=shiftChi2, details=docTemplate % 'Chemical' + ss) ss = 'RMS to best struct' nmrCalcRun.newFloatMatrixData(code='rms', name=ss, ioRole='output', shape=(0, ), data=rms, details=docTemplate % 'RMS to structure with best corrected score') ss = 'RMS to best raw struct' nmrCalcRun.newFloatMatrixData(code='rawRms', name=ss, ioRole='output', shape=(0, ), data=rawRms, details=docTemplate % 'RMS to structure with best raw score') # Get MolSystem molResidueData = nmrCalcRun.findFirstData(className='MolResidueData', ioRole='input') molSystem = molResidueData.molSystem # read structures in order of lowest corrected score paths = [] for modelNum, fileName in enumerate(names): path = uniIo.joinPath(dataDir, fileName + '.pdb') if os.path.isfile(path): paths.append(path) else: break ensemble = StructureIo.getStructureFromFiles(molSystem, paths, fileType='rough') ss = "Rosetta calculated structures, sorted by corrected score" nmrCalcRun.newStructureEnsembleData(name='result', ioRole='output', structureEnsemble=ensemble, details=docTemplate % ss) # Make Nmr.StructureCalculation nmrProject = nmrCalcRun.nmrCalcStore.nmrProject nmrCalcRun.structureGeneration = nmrProject.newStructureGeneration( generationType='denovo', name='CSRosetta', structureEnsemble=ensemble)