diff convert_detect_formats/rings_detect_format/glycan_sniff.py @ 0:89592faa2875 draft

Uploaded
author chrisb
date Wed, 23 Mar 2016 14:35:56 -0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/convert_detect_formats/rings_detect_format/glycan_sniff.py	Wed Mar 23 14:35:56 2016 -0400
@@ -0,0 +1,47 @@
+__author__ = "Chris Barnett"
+__version__ = "0.3"
+__license__ = "MIT"
+
+
+def sniff_glycan(inputstream, is_a=None):
+    """
+    Passes input glycan format to RINGS and returns format.
+    If is_a is specified then return boolean
+    :rtype : text, or Boolean if is_a is specified or None if broken (maybe should be False)
+    :param inputstream: inputstream with glycan struct. (not sure for multiple...  in any of LinearCode, KCF etc. (can be mixed))
+    :param is_a: If not None then test for the file type specified.
+    """
+    if inputstream is None or inputstream == [] or inputstream == "":
+        return ""
+    try:
+        from suds.client import Client
+
+        url = 'http://rings.t.soka.ac.jp/axis2/services/Utilities?wsdl'
+        client = Client(url)
+        response = client.service.DeterminingForm(inputstream.read())
+        if response is None:
+            return ""
+        fmt = response.array[0]  # response is an object,just access the attributes
+        if is_a is not None and "" != is_a:
+            if is_a.upper() == fmt.upper():  # compare fmt names and avoid case issues
+                return True
+            else:
+                return False
+        else:
+            return fmt
+    except Exception as e:
+        raise IOError(e)
+
+
+if __name__ == "__main__":
+    from optparse import OptionParser
+
+    usage = "usage: python %prog [options]\n"
+    parser = OptionParser(usage=usage)
+    parser.add_option("-i", action="store", type="string", dest="i", default="input",
+                      help="input any glycan file (input)")
+    parser.add_option("-f", action="store", type="string", dest="f", default=None,
+                      help="return true if is the specified format KCF)")
+    (options, args) = parser.parse_args()
+    instream = file(options.i, 'r')
+    print sniff_glycan(instream, options.f)