def getFormatData(type): from org.yaml.snakeyaml import Yaml from java.io import ByteArrayInputStream formatData = loadResource('template.yaml') input = java.io.ByteArrayInputStream(formatData) yaml = Yaml() data = yaml.load(input) return data[type]
def readYaml(file): from java.io import FileInputStream from org.yaml.snakeyaml import Yaml input = FileInputStream(file) yaml = Yaml() data = yaml.load(input) return data
def genNEFYaml(fileName): input = 'nef : ' + fileName + '\n' input += yamlStr print input yaml = Yaml() data = yaml.load(input) return data
def genYaml(): input = ''' molecule : entities : - sequence : GPGAST type : nv ptype : protein anneal: steps : 1000 highTemp : 1000.0 ''' yaml = Yaml() data = yaml.load(input) return data
import de.embl.cba.metadata.MetaData as MetaData; import de.embl.cba.metadata.MetadataCreator as MetadataCreator; import ij.IJ as IJ; import org.yaml.snakeyaml.DumperOptions as DumperOptions; import org.yaml.snakeyaml.Yaml as Yaml; import java.io.FileInputStream as FileInputStream; outputPath = "/Volumes/cba/exchange/OeyvindOedegaard/yaml_project/test.yaml"; yaml = Yaml(); md = yaml.load( FileInputStream( "/Volumes/cba/exchange/OeyvindOedegaard/yaml_project/test.yaml" ) ); metaDict = md.metadata print( metaDict[ MetadataCreator.IMAGE_DIMENSIONS ] );
def readYamlString(yamlString): from org.yaml.snakeyaml import Yaml yaml = Yaml() data = yaml.load(yamlString) return data
def isRNA(mol): polymers = mol.getPolymers() rna = False for polymer in polymers: for residue in polymer.getResidues(): resName = residue.getName() if resName in ["A","C","G","U"]: rna = True break return rna if argFile.endswith('.yaml'): input = FileInputStream(argFile) yaml = Yaml() data = yaml.load(input) refiner=refine() osfiles.setOutFiles(refiner,dataDir, 0) refiner.rootName = "temp" refiner.loadFromYaml(data,0) mol = refiner.molecule vienna = data['rna']['vienna'] mol.setDotBracket(vienna) #rnapred.predictFromSequence(mol,vienna) rnapred.predictFromSequence() rnapred.dumpPredictions(mol) elif argFile.endswith('.pdb') or argFile.endswith('.cif'): if argFile.endswith('.pdb'): mol = molio.readPDB(argFile) elif argFile.endswith('.cif'):
class nomjyc (HttpServlet): """ This is the sandbox of the nomjyc web interface. Please refer to <a href="http://nomjyc.ath.cx:8080">nomjyc.ath.cx:8080</a> for the main game. Called without parameters, it will most probably just display the nomjyc rules, read from the main nomjyc game. Send a GET or POST parameter called "test" to add an arbitrary rule to be executed at the end of the rule list. """ def __init__(self): self.yaml = Yaml() self.yamlLexer = YamlLexer() self.pythonLexer = PythonLexer() self.pythonTBLexer = PythonTracebackLexer() self.htmlFormatter = HtmlFormatter() def dumpData(self,c): rules = c.data["rules"] c.data["rules"] = "omitted" code = highlight(self.yaml.dump(c.data), self.yamlLexer, self.htmlFormatter) c.data["rules"] = rules return code def dumpYaml(self, anything): return highlight(self.yaml.dump(anything), self.yamlLexer, self.htmlFormatter) def dumpPython(self, code): return highlight(code, self.pythonLexer, self.htmlFormatter) def dumpPythonTB(self, code): return highlight(code, self.pythonTBLexer, self.htmlFormatter) def explainException(self, title): nruter = "<div class=\"erroroutput\"><span class=\"gu\">%s</span>" % title exc_type, exc_value, exc_tb = sys.exc_info() for line in traceback.format_exception(exc_type, exc_value, exc_tb): nruter += "\n%s" % self.dumpPythonTB(line) return nruter + "</div>" def doGet(self, request, response): # Treat GET and POST equally self.doPost(request, response) DIV_HIDEOUS_BOX = "<div class=\"%s\"><div class=\"gh\">%s - <a href=\"javascript:ReverseDisplay('%s')\">show/hide %s</a></div><div id=\"%s\" style=\"display:%s\">%s</div>%s" def divHideCode(self, claz, title, id, data, visible=False, content="", openend=False): return self.DIV_HIDEOUS_BOX % (claz, title, id, content, id, visible and "show" or "none", data, openend and " " or "</div>") def doPost(self, request, response): c = NomjycContainer() c.parameters = request.getParameterMap() log = open("/var/log/nomjyc/sandbox.log","a") safelog = dict(c.parameters) if "pass" in safelog: safelog["pass"]=len(safelog["pass"]) log.write(("[[%s]] %s %s\n" % (request.getRemoteAddr(), datetime.utcnow(), self.yaml.dump(safelog))).encode("utf-8")) log.close() output = "<div class=\"infobox\"><span class=\"gh\">nomjyc 0.1</span>\n" c.session = {} if len(c.parameters)==0: output += "<pre>%s</pre>" % self.__doc__ output += "</div>" c.salt="dckx" try: c.data = self.yaml.load(FileInputStream("/var/lib/tomcat6/webapps/nomjyc/data/nomjyc.yaml")) except: output += self.explainException("Error while initiating game state") # Print some debug information - for now. output += self.divHideCode("infobox", "Request", "reqi", self.dumpYaml(c.parameters), visible=True) output += self.divHideCode("infobox", "Data read", "dri", self.dumpYaml(c.data)) # Add a final rule, if the test parameter is set if "test" in c.parameters: for code in c.parameters["test"]: c.data["rules"].add({"author":"impromptu", "code":code, "creation":datetime.utcnow(), "title":"test rule"}) cycles = 1 if "cycles" in c.parameters: try: cycles = int(c.parameters["cycles"][0]) except: pass if cycles<0 or cycles>3: cycles = 1 # Execute all rules against the user input brain = PythonInterpreter() c.sandbox = True # a flag that gives away that we are in a sandbox for i in range(cycles): c.cycle = i # a counter for the cycle we are in for rule in c.data["rules"][:]: # we are going to modify the rules. a lot. this prevents concurrent modification. try: output += self.divHideCode("rulebox", "Executing rule '%s'" % rule["title"], "id"+str(random()), self.dumpPython(rule["code"]), openend=True) err = StringWriter() out = StringWriter() checksum = hashlib.md5(self.yaml.dump(c.data)).hexdigest() brain.set("self", c) brain.setErr(err) brain.setOut(out) before = time.time() timeout (brain.exec,(rule["code"],),timeout_duration=30) runtime = int((time.time()-before) * 1000) changes = (checksum != hashlib.md5(self.yaml.dump(c.data)).hexdigest()) output += "<div class=\"ruleoutput\">" if changes: output += "<div class=\"erroroutput\">This rule changed the game data.</div>" if (err.getBuffer().length()): output += "<div class=\"erroroutput\">Err:<br />%s</div>" % self.dumpPythonTB(err.toString().strip()) if (out.getBuffer().length()): output += "<div class=\"gu\">Out:</div>"+out.toString().strip() output += "<div>(runtime: %sms)</div></div></div>" % runtime except Exception, ex: output += self.explainException("Execution failed") + "</div>" # Dump the data back to file - but not in the sandbox #try: # self.yaml.dump(c.data, FileWriter("/var/lib/tomcat6/webapps/sandbox/data/nomjyc.yaml")) #except: # output += self.explainException("Error while storing game state") # Print some debug information - for now. output += self.divHideCode("infobox", "Session data", "sess", self.dumpYaml(c.session), visible=True) output += self.divHideCode("infobox", "Data stored", "dsf", self.dumpYaml(c.data)) # Send all output to the user (xml or html?) toClient = response.getWriter() response.setContentType("text/html") toClient.println(""" <html> <head> <title>Nomjyc</title> <link rel="stylesheet" type="text/css" href="pygments.css" /> <script type="text/javascript" src="nomjyc.js"></script> </head> <body>%s</body> </html> """ % output)
def _readTemplate(self, file2read): ''' evaluate if the template has a yaml format ''' if not self.isYamlTemplate: initialFile = file2read logger.debug('[%s._readTemplate] Looking for file: "%s"' % (self.__class__.__name__, initialFile)) if not os.path.exists(file2read): # check both .yaml & .yml extension file2read = '%s.yaml' % (file2read) logger.debug('[%s._readTemplate] Looking for file: "%s"' % (self.__class__.__name__, file2read)) if os.path.exists(file2read): self.isYamlTemplate = True else: file2read = '%s.yml' % (file2read) logger.debug('[%s._readTemplate] Looking for file: "%s"' % (self.__class__.__name__, file2read)) if os.path.exists(file2read): self.isYamlTemplate = True else: logger.error( '[%s._readTemplate] Template File "%s" doesn\'t exist (even with yaml extension)' % (self.__class__.__name__, initialFile)) raise MySyntaxError( '[%s._readTemplate] Template File "%s" doesn\'t exist (even with yaml extension)' % (self.__class__.__name__, initialFile)) # So the template file exists try: logger.debug('[%s._readTemplate] Reading template file "%s"' % (self.__class__.__name__, file2read)) lines = open(file2read, 'r').readlines() except: logger.error( '[%s._readTemplate] failure opening template file "%s"' % (self.__class__.__name__, file2read)) raise MySyntaxError( '[%s._readTemplate] failure opening template file "%s"' % (self.__class__.__name__, file2read)) # Shebang testing for Yaml format if not self.isYamlTemplate and (lines[0].find('#!yaml') >= 0 or lines[0].find('#!yml') >= 0): self.isYamlTemplate = True if not self.isYamlTemplate: logger.error( '[%s._readTemplate] compatibility issue ! template must be YAML data', (self.__class__.__name__)) raise SyntaxError( '[%s._readTemplate] compatibility issue ! template must be YAML data', (self.__class__.__name__)) # Yaml format: load the string to Yaml if we don't have already if not self.yamlTemplate: yaml = Yaml(Constructor(), Representer(), DumperOptions(), CustomResolver()) try: self.yamlTemplate = yaml.load(''.join(lines).strip()) except (MarkedYAMLException, YAMLException, ParserException, ReaderException, ScannerException), e: logger.error('Error while parsing YAML-file "%s":\n%s' % (file2read, e)) raise MySyntaxError('Error while parsing YAML-file "%s":\n%s' % (file2read, e)) logger.trace("_readTemplate - Loaded Yaml : '''%s'''" % (self.yamlTemplate))
def loadYamlWin(yamlFile, createNewStage=True): with open(yamlFile) as fIn: inputData = fIn.read() yaml = Yaml() if createNewStage > 0: nw.new() pathComps = os.path.split(yamlFile) title = pathComps[1] if title.endswith('_fav.yaml'): title = title[0:-9] elif title.endswith('.yaml'): title = title[0:-5] nw.setTitle(title) data = yaml.load(inputData) if 'geometry' in data: (x, y, w, h) = data['geometry'] nw.geometry(x, y, w, h) if 'grid' in data: (rows, cols) = data['grid'] nw.grid(rows, cols) if 'sconfig' in data: sconfig = data['sconfig'] nw.sconfig(sconfig) spectra = data['spectra'] for v in spectra: print v datasets = v['datasets'] if 'grid' in v: (iRow, iCol) = v['grid'] else: iRow = 0 iCol = 0 activeWin = iRow * cols + iCol print 'g', iRow, iCol, activeWin nw.active(activeWin) if 'cconfig' in v: cconfig = v['cconfig'] nw.cconfig(cconfig) datasetValues = [] for dataset in datasets: print dataset name = dataset['name'] datasetValues.append(name) print 'dv', datasetValues nw.cmd.datasets(datasetValues) if 'lim' in v: lim = v['lim'] nw.lim(lim) for dataset in datasets: name = dataset['name'] if 'config' in dataset: cfg = dataset['config'] nw.config(datasets=[name], pars=cfg) if 'dims' in dataset: dims = dataset['dims'] nw.setDims(dataset=name, dims=dims) if 'peaklists' in v: peakLists = v['peaklists'] peakListValues = [] for peakList in peakLists: print peakList name = peakList['name'] peakListValues.append(name) print 'dv', peakListValues nw.cmd.peakLists(peakListValues) for peakList in peakLists: name = peakList['name'] if 'config' in peakList: cfg = peakList['config'] nw.pconfig(peakLists=[name], pars=cfg) nw.drawAll()
def parseArgs(): parser = OptionParser() parser.add_option("-p", "--pdbs", dest="pdbPath", default="") parser.add_option("-o", "--outDir", dest="outDir", default="analysis") parser.add_option("-y", "--yaml", dest="yamlFile", default=None) #Will now be used to add addition files to parse that are not included in the yaml file parser.add_option("-c", "--convert", action='store_true', dest="modifyFileType", default=False) # not used! parser.add_option("-s", "--shifts", dest="shiftFile", default=None) parser.add_option("-d", "--distances", dest="disFile", default=None) parser.add_option("-r", "--range", dest="resRange", default="") (options, args) = parser.parse_args() outDir = os.path.join(homeDir, options.outDir) if not os.path.exists(outDir): os.mkdir(outDir) pdbFilePath = options.pdbPath if pdbFilePath == "": pdbFiles = args else: pdbFiles = getFiles(pdbFilePath) changeRange = (options.resRange != '') range = options.resRange if (options.yamlFile != None): input = FileInputStream(options.yamlFile) yaml = Yaml() data = yaml.load(input) else: data = {'ribose': True} if options.shiftFile != None: shift = {} if changeRange: shift['range'] = range arr = options.shiftFile.split(' ') if len(arr) == 1: shift['type'] = 'nv' shift['file'] = arr[0] else: shift['file'] = arr[0] if arr[1] == 's': shift['type'] = 'str3' elif arr[1] == 'n': shift['type'] = 'nv' data['shifts'] = shift else: if changeRange: data['shifts']['range'] = range if options.disFile != None: dis = {} if changeRange: dis['range'] = range arr = options.disFile.split(' ') if len(arr) == 1: dis['type'] = 'nv' dis['file'] = arr[0] else: dis['file'] = arr[0] if arr[1] == 'a': dis['type'] = 'amber' elif arr[1] == 'n': dis['type'] = 'nv' if 'distances' in data: included = checkIncluded(data['distances'], dis) if not included: data['distances'].append(dis) else: print dis[ 'file'] + ' is already included in yaml file. Ignoring Duplicate.' else: data['distances'] = [dis] else: if changeRange: for dict in data['distances']: dict['range'] = range outFiles = loadPDBModels(pdbFiles, data, outDir) summary(outFiles)
class nomjyc (HttpServlet): """ This is the nomjyc web interface. Nomic is a game where the players change the rules. Nomjyc is the same game, except that the rules are written in jython. Called without parameters, it will most probably just display its own rules. Call it with GET or POST parameters to execute and change them. Read the code to understand how. Just kidding. We have a wiki at <a href="http://nomjyc.ath.cx:8080/cgi/fossil">http://nomjyc.ath.cx:8080/cgi/fossil</a> """ def __init__(self): self.yaml = Yaml() self.yamlLexer = YamlLexer() self.pythonLexer = PythonLexer() self.pythonTBLexer = PythonTracebackLexer() self.htmlFormatter = HtmlFormatter() def dumpData(self,c): rules = c.data["rules"] c.data["rules"] = "omitted" code = highlight(self.yaml.dump(c.data), self.yamlLexer, self.htmlFormatter) c.data["rules"] = rules return code def dumpYaml(self, anything): #return highlight(self.yaml.dump(anything), self.yamlLexer, self.htmlFormatter) return "<pre>%s</pre>" % self.yaml.dump(anything) def dumpPython(self, code): return highlight(code, self.pythonLexer, self.htmlFormatter) def dumpPythonTB(self, code): return highlight(code, self.pythonTBLexer, self.htmlFormatter) def explainException(self, title): nruter = "<div class=\"erroroutput\"><span class=\"gu\">%s</span>" % title exc_type, exc_value, exc_tb = sys.exc_info() for line in traceback.format_exception(exc_type, exc_value, exc_tb): nruter += "\n%s" % self.dumpPythonTB(line) return nruter + "</div>" def doGet(self, request, response): # Treat GET and POST equally self.doPost(request, response) DIV_HIDEOUS_BOX = "<div class=\"%s\"><div class=\"gh\">%s - <a href=\"javascript:ReverseDisplay('%s')\">show/hide %s</a></div><div id=\"%s\" style=\"display:%s\">%s</div>%s" def divHideCode(self, claz, title, id, data, visible=False, content="", openend=False): return self.DIV_HIDEOUS_BOX % (claz, title, id, content, id, visible and "show" or "none", data, openend and " " or "</div>") def doPost(self, request, response): c = NomjycContainer() # re-initialized container upon every request c.parameters = request.getParameterMap() log = open("/var/log/nomjyc/nomjyc.log","a") safelog = dict(c.parameters) if "pass" in safelog: safelog["pass"]=len(safelog["pass"]) log.write(("[[%s]] %s %s\n" % (request.getRemoteAddr(), datetime.utcnow(), self.yaml.dump(safelog))).encode("utf-8")) log.close() output = "<div class=\"infobox\"><span class=\"gh\">nomjyc 0.1</span>\n" c.session = {} if len(c.parameters)==0: output += "<pre>%s</pre>" % self.__doc__ output += "</div>" c.salt="dckx" try: c.data = self.yaml.load(FileInputStream("/var/lib/tomcat6/webapps/nomjyc/data/nomjyc.yaml")) except: output += self.explainException("Error while initiating game state - trying to load backup.") c.data = self.yaml.load(FileInputStream("/var/lib/tomcat6/webapps/nomjyc/data/nomjyc.bak")) # Print some debug information - for now. output += self.divHideCode("infobox", "Request", "reqi", self.dumpYaml(c.parameters), visible=True) output += self.divHideCode("infobox", "Data read", "dri", self.dumpYaml(c.data)) # If we have come so far, we assume that it is safe to write a backup. try: self.yaml.dump(c.data, FileWriter("/var/lib/tomcat6/webapps/nomjyc/data/nomjyc.bak")) except: output += self.explainException("Error while storing backup game state") # Execute all rules against the user input brain = PythonInterpreter() c.sandbox = False # a flag that tells that we are not in a sandbox checksum = hashlib.md5(self.yaml.dump(c.data)).hexdigest() for rule in c.data["rules"][:]: # we are going to modify the rules. a lot. this prevents concurrent modification. try: output += self.divHideCode("rulebox", "Executing rule '%s'" % rule["title"], "id"+str(random()), self.dumpPython(rule["code"]), openend=True) err = StringWriter() out = StringWriter() # Compile the rule into a jython/java class brain.set("self", c) # expose the container to the rules brain.setErr(err) brain.setOut(out) before = time.time() timeout (brain.exec,(rule["code"],),timeout_duration=30) runtime = int((time.time()-before) * 1000) newsum = hashlib.md5(self.yaml.dump(c.data)).hexdigest() changes = (checksum != newsum) checksum = newsum output += "<div class=\"ruleoutput\">" if changes: output += "<div class=\"erroroutput\">This rule changed the game data.</div>" if (err.getBuffer().length()): output += "<div class=\"erroroutput\">Err:<br />%s</div>" % self.dumpPythonTB(err.toString()) if (out.getBuffer().length()): output += "<div class=\"gu\">Out:</div>"+out.toString() output += "<div>(runtime: %sms)</div></div></div>" % runtime except Exception, ex: output += self.explainException("Execution failed") + "</div>" # Dump the data back to file try: self.yaml.dump(c.data, FileWriter("/var/lib/tomcat6/webapps/nomjyc/data/nomjyc.yaml")) except: output += self.explainException("Error while storing game state") # Print some debug information - for now. output += self.divHideCode("infobox", "Session data", "sess", self.dumpYaml(c.session), visible=True) output += self.divHideCode("infobox", "Data stored", "dsf", self.dumpYaml(c.data)) # Send all output to the user (xml or html?) toClient = response.getWriter() response.setContentType("text/html") toClient.println(""" <html> <head> <title>Nomjyc</title> <link rel="stylesheet" type="text/css" href="pygments.css" /> <script type="text/javascript" src="nomjyc.js"></script> </head> <body>%s</body> </html> """ % output)