#!/usr/bin/env python ## ## usage: validateSBML.py [-h] [-o output-format] [-d opt1[,opt2,...]] filename.xml ## usage: validateSBML.py [-h] [-o output-format] [-d opt1[,opt2,...]] http://... ## ## Validates the SBML document given by filename.xml or located at the ## http:// URL. Output-format is optional and may be one of: xml, ## xhtml, json, text (default: xml). Also, one or more consistency checks ## can be disabled. ## # # @file validateSBML.py # @brief Validates an SBML document using the SBML.org Online Validator # @author Ben Bornstein # @author Akiya Jouraku # # $Id$ # $Source$ # # Copyright 2006 California Institute of Technology and # Japan Science and Technology Corporation. # # This library is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation; either version 2.1 of the License, or # any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. The software and # documentation provided hereunder is on an "as is" basis, and the # California Institute of Technology and Japan Science and Technology # Corporation have no obligations to provide maintenance, support, # updates, enhancements or modifications. In no event shall the # California Institute of Technology or the Japan Science and Technology # Corporation be liable to any party for direct, indirect, special, # incidental or consequential damages, including lost profits, arising # out of the use of this software and its documentation, even if the # California Institute of Technology and/or Japan Science and Technology # Corporation have been advised of the possibility of such damage. See # the GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this library; if not, write to the Free Software Foundation, # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. # import httplib, random, string, sys from optparse import OptionParser HOST = 'sbml.org' URL = '/validator/' def encode (parameters, filename=None): """encode(parameters, filename=None) -> (boundary, data) Encodes the contents of validator parameters (a Python dictionary) and filename (assumed to XML) into a format suitable for uploading to the SBML.org validator via an HTTP POST. Adapted from the Python Cookbook: - http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306 """ boundary = '<<' + ''.join([random.choice(string.digits) for n in range(30)]) lines = [ ] if filename is not None: format = 'Content-Disposition: form-data; name="file"; filename="%s"' lines.append('--' + boundary) lines.append(format % filename) lines.append('Content-Type: text/xml') lines.append('') stream = open(filename) lines.extend( map(string.rstrip, stream.readlines()) ) stream.close() if parameters is not None: for (name, value) in parameters.items(): lines.append('--' + boundary) lines.append('Content-Disposition: form-data; name="%s"' % name) lines.append('') lines.append( str(value) ) lines.append('--' + boundary + '--') lines.append('') return (boundary, string.join(lines, '\r\n')) def validateSBML (filename, parameters=None): """validateSBML(filename, parameters=None) -> string Validates the given SBML filename (or http:// URL) by calling the SBML.org online validator. The results are returned as a string whose format may be controlled by setting parameters['output'] to one of: 'xml', 'xhtml', 'json', 'text' (default: xml). """ if parameters is None: parameters = { } if filename.startswith('http://'): parameters['url'] = filename filename = None parameters.setdefault('output', 'xml') (boundary, body) = encode(parameters, filename) headers = { 'Content-Type': 'multipart/form-data; boundary=%s' % boundary } http = httplib.HTTPConnection(HOST) http.request('POST', URL, body, headers) return http.getresponse().read() usage = """ validateSBML.py [-h] [-o output-format] [-d opt1[,opt2,...]] filename.xml validateSBML.py [-h] [-o output-format] [-d opt1[,opt2,...]] http://... """ # Parse the command-line arguments parser = OptionParser(usage) parser.add_option("-o", dest="output", default='xml', metavar='output-format', help="Specify an output format: xml, xthml, or text [default=%default]") parser.add_option("-d", dest="offcheck", metavar='opt1[,opt2,...]', help="Disable the given consistency check options. " "The options are given as comma-separated characters. " "Each character is one of the followings: u (unit consistency), " "g (overall SBML consistency), i (identifier consistency), " "m (MathML consistency), s (SBO consistency), " "o (overdetermined model), p (modeling practice)") (options, args) = parser.parse_args() if len(args) == 0 : parser.print_help() sys.exit(2) filename = args[0] parameters = { } if options.output : parameters['output'] = options.output if options.offcheck : parameters['offcheck'] = options.offcheck print validateSBML(filename, parameters)