a52594f56d06a043a07ae9043ca09d052402f47f
[qpalma.git] / python / Plif.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import QPalmaDP
5
6 class Plf:
7 """
8 means piecewise linear function
9 """
10
11 def __init__(self,num=None):
12 if num == None:
13 self.len = 0
14 self.limits = []
15 self.penalties = []
16 self.transform = ''
17 self.name = ''
18 self.max_len = 0
19 self.min_len = 0
20 else:
21 self.len = num
22 self.limits = linspace(0,40,num)
23 self.penalties = [0]*num
24 self.transform = ''
25 self.name = ''
26 self.max_len = 0
27 self.min_len = 0
28
29
30 def convert2SWIG(self):
31 ps = QPalmaDP.penalty_struct()
32
33 ps.len = len(self.limits)
34
35 ps.limits = QPalmaDP.createDoubleArrayFromList(self.limits)
36 ps.penalties = QPalmaDP.createDoubleArrayFromList(self.penalties)
37
38 ps.max_len = self.max_len
39 ps.min_len = self.min_len
40 ps.transform = 0
41 ps.name = self.name
42
43 return ps
44
45
46 def base_coord(b1,b2):
47 b1 = b1.lower()
48 b2 = b2.lower()
49 assert b1 in ['a','c','g','t','n']
50 assert b2 in ['a','c','g','t','n']
51 cb = {'a':0, 'c':1, 'g':2, 't':3, 'n':4}
52
53 b1 = cb[b1]
54 b2 = cb[b2]
55
56 return b1*5+b2
57
58 def linspace(a,b,n):
59 intervalLength = b-a
60 stepSize = 1.0*intervalLength / (n-1)
61
62 interval = [0]*n
63 interval[0] = a
64 interval[-1] = b
65 for i in range(1,n-1):
66 interval[i] = a+(i*stepSize)
67
68 return interval
69
70 def logspace(a,b,n):
71 interval = [0]*n
72 begin = 10.0**a
73 end = 10.0**b
74 intervalSize = 1.0*(b-a)/(n-1)
75 interval[0] = begin
76 interval[-1] = end
77
78 for i in range(1,n-1):
79 interval[i] = 10**(a+i*intervalSize)
80
81 return interval
82
83 def log10(x):
84 return math.log(x)/math.log(10)