Commits

Brent Pedersen committed 1150ff4

add commandline interface for the socket server.

  • Participants
  • Parent commits 372bba0

Comments (0)

Files changed (3)

nwalign/nwalign/__init__.py

+"""
+------------------------------------------------------------------------------
+nwalign: fast `cython`_  - `Needleman-Wunsch`_ alignment
+------------------------------------------------------------------------------
+
+.. _`Needleman-Wunsch`: http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm 
+.. _`scoring matrix`: http://en.wikipedia.org/wiki/Substitution_matrix
+.. _`cython`: http://cython.org
+
+This module provides a python module and a command-line interface to do global-
+sequence alignment using the `Needleman-Wunsch` algorithm. It uses `cython`_ 
+and numpy for speed.
+
+Example Command-Line Usage 
+==========================
+
+
+the nwalign executable is installed to the PATH by setuptools
+::
+
+    $ nwalign alphabet alpet
+    alphabet
+    alp---et
+
+specify an alignment `scoring matrix`_ 
+::
+
+    $ nwalign --matrix /usr/share/ncbi/data/BLOSUM62 EEAEE EEEEG
+    EEAEE-
+    EE-EEG
+
+with specified penalties
+::
+
+    $ nwalign --gap_open -10 --gap_extend -4 --match 12 ASDFF ASFF
+    ASDFF
+    AS-FF
+
+
+Usage as a python module
+========================
+::
+
+    >>> import nwalign as nw
+    >>> nw.global_align("CEELECANTH", "PELICAN", matrix='PAM250')
+    ('CEELECANTH', '-PELICA--N')
+
+
+the matrix is specified as the full path to an `scoring matrix`_ as
+is distributed with the NCBI toolset.
+"""
 from cnwalign import global_align, global_align_no_matrix, score_alignment
-import cnwalign
-__doc__ = cnwalign.__doc__
 
 
 def main():
     parser = optparse.OptionParser(usage="""
     %prog [options] seq1 seq2 
     """)
-    parser.add_option("--gap", dest="gap", help="gap extend penalty (must be integer <= 0)", type="int", default=-1)
-    parser.add_option("--gap_init", dest="gap_init", help="gap start penalty (must be integer <= 0)", type="int", default=-1)
+    parser.add_option("--gap_extend", dest="gap_extend", help="gap extend penalty (must be integer <= 0)", type="int", default=-1)
+    parser.add_option("--gap_open", dest="gap_open", help="gap open penalty (must be integer <= 0)", type="int", default=-1)
     parser.add_option("--match", dest="match", help="match score (must be integer > 0)", type="int", default=1)
-    parser.add_option("--mismatch", dest="mismatch", help="gap penalty (must be integer < 0)", type="int", default=-1)
     parser.add_option("--matrix", dest="matrix", help="scoring matrix in ncbi/data/ format,\
                                       if not specificied, match/mismatch are used", default=None)
+    parser.add_option("--server", dest="server", default=0, type='int',
+                      help="if non-zero integer, a server is started")
 
     try:
         options, args = parser.parse_args()
     except:
         sys.exit(parser.print_help())
-    if len(args) != 2:
+    if options.server != 0:
+        import nwserver
+        nwserver.main(sys.argv[2])
+        
+    elif len(args) != 2:
         sys.exit(parser.print_help())
-    print "\n".join(global_align(args[0], args[1], options.gap, options.match,
-                                 options.mismatch, int(options.gap_init), options.matrix))
+    else:
+        print "\n".join(global_align(args[0], args[1], options.match,
+                                 options.gap_open, options.gap_extend, options.matrix))
 
 if __name__ == "__main__":
     main()

nwalign/nwalign/cnwalign.pyx

-"""
-------------------------------------------------------------------------------
-nwalign: fast `cython`_  - `Needleman-Wunsch`_ alignment
-------------------------------------------------------------------------------
-
-.. _`Needleman-Wunsch`: http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm 
-.. _`scoring matrix`: http://en.wikipedia.org/wiki/Substitution_matrix
-.. _`cython`: http://cython.org
-
-This module provides a python module and a command-line interface to do global-
-sequence alignment using the `Needleman-Wunsch` algorithm. It uses `cython`_ 
-and numpy for speed.
-
-Example Command-Line Usage 
-==========================
-
-
-the nwalign executable is installed to the PATH by setuptools::
-
-    $ nwalign alphabet alpet
-    alphabet
-    alp---et
-
-specify an alignment `scoring matrix`_ ::
-
-    $ nwalign --matrix /usr/share/ncbi/data/BLOSUM62 EEAEE EEEEG
-    EEAEE-
-    EE-EEG
-
-
-Usage as a python module
-========================
-::
-
-    >>> import nwalign as nw
-    >>> nw.global_align("CEELECANTH", "PELICAN", matrix='PAM250')
-    ('CEELECANTH', '-PELICA--N')
-
-
-the matrix is specified as the full path to an `scoring matrix`_ as
-is distributed with the NCBI toolset.
-"""
 import numpy as np
 cimport numpy as np
 

nwalign/nwalign/nwserver.py

-import socket
-from nwalign import global_align
 import sys
-import atexit
 
-PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 1233
-CHUNK = 32768 * 8
-HOST = 'localhost'
+def main(port=1233):
+    import socket
+    from __init__ import global_align
+    import atexit
 
-server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-server.bind((HOST, PORT))
-server.listen(4)
-print "\nstarted server on %s:%i\n" % (HOST, PORT)
+    PORT = int(port)
+    CHUNK = 32768 * 8
+    HOST = 'localhost' 
 
-def get_args(astr):
-    kwargsab = astr.split(" ")
-    a, b = kwargsab[-2:]
-    kwargs = [v.lstrip("-") for v in kwargsab[:-2]]
-    kw = {}
-    for i, k in enumerate(kwargs[::2]):
-        if k == 'matrix':
-            kw[k] = kwargs[2 * i + 1]
-        else:
-            kw[k] = int(kwargs[2 * i + 1])
-    return a, b, kw
+    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    server.bind((HOST, PORT))
+    server.listen(4)
+    print "\nstarted server on %s:%i\n" % (HOST, PORT)
 
+    def get_args(astr):
+        kwargsab = astr.split(" ")
+        a, b = kwargsab[-2:]
+        kwargs = kwargsab[:-2]
+        kw = {}
+        for i, k in enumerate(kwargs[::2]):
+            k = k.lstrip('-')
+            if k == 'matrix':
+                kw[k] = kwargs[2 * i + 1]
+            else:
+                kw[k] = int(kwargs[2 * i + 1])
+        return a, b, kw
 
+    while True:
+        client, address = server.accept()
+        data = True 
+        while data:
+            try: 
+                data = client.recv(CHUNK).strip()
+                if data == "EXIT":
+                    client.close()
+                    server.close()
+                    print "EXITING service"
+                    sys.exit(0)
 
-
-while True:
-    client, address = server.accept()
-    data = True 
-    while data:
-        try: 
-            data = client.recv(CHUNK).strip()
-            if data == "EXIT":
-                client.close()
-                server.close()
-                print "EXITING service"
-                sys.exit(0)
-
-            a,b, kwargs = get_args(data)
-            r = global_align(a, b, **kwargs)
-            client.send(" ".join(r))
-        except Exception, e:
-            try:
-                client.send("ERROR:" + str(e))
-            except socket.error:
-                # they already closed...
-                client.close()
-                break
+                a,b, kwargs = get_args(data)
+                r = global_align(a, b, **kwargs)
+                client.send(" ".join(r))
+            except Exception, e:
+                try:
+                    client.send("ERROR:" + str(e))
+                except socket.error:
+                    # they already closed...
+                    client.close()
+                    break
 
 
     client.close()
 
-atexit.register(server.close)
+    atexit.register(server.close)