comparison swalign.py @ 4:af383638de66 draft

planemo upload commit 022984f323d3da44f70b3bf79c684cfd8dda3f61-dirty
author nick
date Mon, 23 Nov 2015 18:44:23 -0500
parents
children
comparison
equal deleted inserted replaced
3:13bcc2f459b0 4:af383638de66
1 import os
2 import ctypes
3
4 script_dir = os.path.dirname(os.path.realpath(__file__))
5 swalign = ctypes.cdll.LoadLibrary(os.path.join(script_dir, 'swalignc.so'))
6
7
8 # C struct for ctypes
9 class SeqPairC(ctypes.Structure):
10 _fields_ = [
11 ('a', ctypes.c_char_p),
12 ('alen', ctypes.c_uint),
13 ('b', ctypes.c_char_p),
14 ('blen', ctypes.c_uint),
15 ]
16
17
18 # C struct for ctypes
19 class AlignC(ctypes.Structure):
20 _fields_ = [
21 ('seqs', ctypes.POINTER(SeqPairC)),
22 ('start_a', ctypes.c_int),
23 ('start_b', ctypes.c_int),
24 ('end_a', ctypes.c_int),
25 ('end_b', ctypes.c_int),
26 ('matches', ctypes.c_int),
27 ('score', ctypes.c_double),
28 ]
29
30
31 # The Python version
32 class Align(object):
33 def __init__(self, align_c):
34 self.target = align_c.seqs.contents.a
35 self.query = align_c.seqs.contents.b
36 self.start_target = align_c.start_a
37 self.start_query = align_c.start_b
38 self.end_target = align_c.end_a
39 self.end_query = align_c.end_b
40 self.matches = align_c.matches
41 self.score = align_c.score
42
43
44 # Initialize functions (define types).
45 swalign.smith_waterman.restype = ctypes.POINTER(AlignC)
46 swalign.revcomp.restype = ctypes.c_char_p
47
48
49 def smith_waterman(target, query):
50 seq_pair = SeqPairC(target, len(target), query, len(query))
51 align_c = swalign.smith_waterman(ctypes.pointer(seq_pair), 1).contents
52 return Align(align_c)
53
54
55 def revcomp(seq):
56 """WARNING: This will alter the input string in-place!"""
57 swalign.revcomp(seq)