def save(self, toPath=None): """ Save validation toPath using json encoder return True on error """ if toPath is None: toPath = self.project.path() / cdefs.directories.validation else: toPath = disk.Path(toPath) toPath.makedirs() # sort by key: keyedResults = {} for container in self.data.itervalues(): for key, result in container.iteritems(): # only add result items with data if result is not None: theList = keyedResults.setdefault(key, []) theList.append(result) #end if #end for #end for for key, theList in keyedResults.iteritems(): io.debug('ValidationData.save: key {0}, list of {1} items, saving to "{3}"\n', key, len(theList), theList, toPath / key + '.json' ) jsonTools.obj2json(theList, toPath / key + '.json') #end for del keyedResults # allow garbage collection?? return False
def restore(self, fromPath=None, restoreLinkages=True): """ restore validation data fromPath (set to default if None); establish linkage to project if restoreLinkages == True return True on error """ if fromPath is None: fromPath = self.project.path() / cdefs.directories.validation else: fromPath = disk.Path(fromPath) if not fromPath.exists(): io.error('ValidationData.restore: path "{0}" does not exist\n', fromPath) error = False # used to track if decoding of any of the json files generated an error for vfile in fromPath.glob('*.json'): io.debug('ValidationData.restore: restoring {0}, restoreLinkages = \n', vfile, restoreLinkages) if restoreLinkages: if self._restoreWithLinkages(vfile): error = True else: if self._restoreWithoutLinkages(vfile): error = True #end if #end for return error
def test_openCcpnProjects(): os.chdir(cdefs.cingDefinitions.tmpdir) dataPath = cdefs.cingDefinitions.rootPath / 'data' / 'Tests' / 'ccpn' # for target in '1a4d 1a24 1afp 1ai0 1b4y 1brv 1bus 1cjg 1d3z 1hkt 1hue 1ieh 1iv6 1jwe 1kr8 2hgh 2k0e'.split(): # for target in '1brv'.split(): for target in targets.keys(): path = dataPath / target+'.tgz' io.debug('==> Now trying {0}\n', path) if path.exists(): openCcpn(target, path)
def upgrade075(project, restore): """ Upgrade project from 075 or earlier conventions return upgraded project or None on error """ io.message('upgrade075: converting from CING version {0}\n', project.version) # 0.75 version had moleculeNames stored in molecules attribute # >=0.76 version molecules is a ProjectList instance project.moleculeNames = project.molecules # store the project file and reopen to have correct settings project._save2json() pr = classes.Project._restoreFromJson(project.path(cing.cingPaths.project)) if pr == None: io.error('upgrade075: conversion from version %s failed on read\n', project.version) return None for molName in pr.moleculeNames: pathName = pr.path(cing.directories.molecules, molName) # old reference, versions 0.48-0.75 if pr.version <= 0.48: pathName = pr.path('Molecules', molName) # old reference # end if io.debug('upgrade075: trying molecule conversion from {0}\n', pathName) if not pathName.exists(): io.error('upgrade075: old molecule pathName "{0}" does not exist\n', pathName) return None mol = openMol_075(pathName) if not mol: io.error('upgrade075: conversion from version {0} failed on molecule {1}\n', project.version, molName) return None pathName.removedir() # Save molecule to new format mol.save(pr.molecules.path(molName)) #end for # restore pr.restore() # Save to consolidate pr.save() return cing.Legacy.Legacy100.upgrade100.upgrade100(pr, restore)
def openMol_075( path ) : """Static method to restore molecule from directory path implements the <=0.75 storage model returns Molecule instance or None on error """ # old format content = xmlTools.xML2obj( path=os.path.join( path, NTmolParameters.contentFile ) ) if not content: io.error('openMol_075: error reading xml file "{0}"\n', os.path.join( path, NTmolParameters.contentFile ) ) return None #end if content.keysformat() io.debug('openMol_075: content from xml-file: %s', content.format()) mol = molecule.Molecule( name = content.name ) if not mol: io.error('openMol_075: initializing molecule\n') return None #end if mol.content = content if content.has_key('sequenceFile') and \ restoreSequence(mol, os.path.join(path, content.sequenceFile)) is None: return None if content.has_key('resonanceFile') and \ restoreResonances(mol, os.path.join(path, content.resonanceFile), append=False) < 0: return None if content.has_key('stereoFile') and \ restoreStereoAssignments(mol, os.path.join(path, content.stereoFile)) < 0: return None if content.has_key('coordinateFile') and \ restoreCoordinates(mol, os.path.join(path, content.coordinateFile), append=False) is None: return None mol._check() mol.updateAll() return mol
def openCcpn(target, path): io.debug('openCcpn: doing {0} from {1}\n', target, path) project = classes.Project.open(target, constants.PROJECT_NEWFROMCCPN) assert project is not None # p = project.initCcpn(path) # assert p is not None io.debug('{0}\n',project.format()) chains, residues, atoms, models = targets[target].molecule assert chains == len(project.molecule.allChains()) assert residues == len(project.molecule.allResidues()) assert atoms == len(project.molecule.allAtoms()) assert models == project.molecule.modelCount for restraintList, n in targets[target].restraintLists: assert restraintList in project assert len(project[restraintList]) == n project.nosave = True p = project.path() io.debug('openCcpn: closing {0} and removing {1}\n', target, p) project.close() p.rmdir()
def upgradeProject2Json( name, restore ): """Upgrade the project from project.xml to project.json file Convert several parameters to new Adict types """ io.debug('upgradeToJson: restoring {0}\n', name) root, newName, ext = Project.rootPath(name) if not root: io.error('upgradeToJson: unable to open Project "{0}" because root is [{1}]\n', name, root) return None if not root.exists(): io.error('upgradeToJson: unable to open Project "{0}" because root [{1}] was not found\n', name, root) return None #end if pfile = root / 'project.xml' # Name in all versions <= 1.0 # Check if we find the xml file if pfile.exists(): io.message('==> Upgrading cing project; please stand by\n') #GWV: 20140203: cannot do an update method() -> errors in partioning restraints. Do not understand pr = xmlTools.xML2obj(pfile) if pr is None: io.error('upgradeToJson: parsing from project file "{0}" failed\n', pfile) return None try: # <= 0.75 version have string pr.version = float(pr.version.strip('abcdefghijklmnopqrtsuvw ()!@#$%^&*').split()[0]) except: pass # change to Time object pr.created = io.Time.fromString(pr.created) pr.lastSaved = io.Time(disk.modtime(pfile)) # change types of names list for listName in 'moleculeNames peakListNames distanceListNames dihedralListNames rdcListNames'.split(): #print 'listName>>',listName, (listName in pr) if listName in pr: pr[listName] = [n for n in pr[listName]] # update from old status type status = Adict() for key in constants.VALIDATION_KEYS: sdict = StatusDict(key) status[key] = sdict # if not isinstance(sdict, StatusDict): # sdict = StatusDict(key) if key in pr.status: # print 'upgrade2json> key=', key # not certain if we get dict or NTdict; this circumvent unwanted keys of NTdict for k,v in pr.status[key].items(): #print 'upgrade2json>> k,v', k,v sdict[k] = v #print '>>>\n', sdict.formatItems() #end if #end for #end for # update the status from old definitions; for (key, statusName) in [ (constants.SHIFTX_KEY, 'shiftxStatus'), (constants.PROCHECK_KEY,'procheckStatus'), (constants.DSSP_KEY, 'dsspStatus'), (constants.WHATIF_KEY, 'whatifStatus'), (constants.WATTOS_KEY, 'wattosStatus'), (constants.VASCO_KEY, 'vascoStatus'), (constants.X3DNA_KEY, 'x3dnaStatus') ]: sdict = status[key] if statusName in pr: #print 'upgrade2json> statusName=', statusName # not certain if we get dict or NTdict; this circumvent unwanted keys of NTdict for k,v in pr[statusName].items(): #print 'upgrade2json>> k,v=', k,v sdict[k] = v #print '>>>\n', sdict.formatItems() #end if #LEGACY names pr[statusName] = sdict #end for # update some fields if present for sdict in status.values(): if 'molecule' in sdict and isinstance(sdict['molecule'], tuple): sdict['molecule'] = pid.Pid.new('Molecule:' + sdict['molecule'][0]) if 'moleculeName' in sdict and isinstance(sdict['molecule'], tuple): sdict['molecule'] = pid.Pid.new('Molecule:' + sdict['moleculeName'][0]) del sdict['moleculeName'] if 'runVersion' in sdict: sdict['version'] = sdict['runVersion'] del sdict['runVersion'] if 'smlFile' in sdict: del sdict['smlFile'] # sdict['date'] = io.Time(1360158182.7) # Wed Feb 6 13:43:02 2013 sdict['date'] = io.Time(pr.lastSaved) # make a copy because otherwise the json handler breaks sdict['version'] = 0.95 # old version #end for # make this the new status dict pr.status = status #print '>>>>>\n', pr.status.queeny.formatItems() pr._save2json() # have to make the directory because we have not yet fully initialised all directories at this stage pr._updateProjectPaths() disk.rename(pfile, pr.path() / cdefs.directories.version1 / 'project.xml' ) #now we should be able to open it again return Project.open( name, status = constants.PROJECT_OLD, restore = restore ) else: io.error('upgradeToJson: missing Project file "{0}"\n', pfile) return None