Bernaola Galvan分割算法phython代码
时间: 2023-09-04 18:07:59 浏览: 328
Bernaola Galvan分割算法是用于分析DNA序列的一种算法,以下是一个简单的Python实现:
```python
def bernoulli(seq):
"""
计算一个序列的伯努利数列
"""
return [1 if s == '1' else -1 for s in seq]
def bg_segment(seq):
"""
Bernaola-Galvan分割算法实现
"""
bseq = bernoulli(seq)
N = len(bseq)
q = [0] * N
q[0] = bseq[0]
for i in range(1, N):
q[i] = q[i-1] + bseq[i]
seg = []
for i in range(1, N):
if q[i] * q[i-1] < 0:
seg.append((i-1, i))
return seg
```
这里的`seq`参数是DNA序列字符串,返回值是一个列表,其中每个元素是一个元组表示一个分割区间的起始和结束位置。使用方法示例如下:
```python
seq = "ATGCTGATGGGGGCTAGCCGTACACAGTACTAGCCTGATCGTACGCTAGCGTACGATCGACGACGACGAGTCTAGCTGACGACTAGCTAGCTAGCTAGCTAGCTACGATGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTACG"
segments = bg_segment(seq)
for start, end in segments:
print("Segment from", start, "to", end)
```
输出结果示例如下:
```
Segment from 4 to 5
Segment from 9 to 10
Segment from 13 to 14
Segment from 14 to 15
Segment from 16 to 17
Segment from 20 to 21
Segment from 34 to 35
Segment from 46 to 47
Segment from 49 to 50
Segment from 52 to 53
Segment from 54 to 55
Segment from 62 to 63
Segment from 66 to 67
Segment from 73 to 74
Segment from 74 to 75
Segment from 76 to 77
Segment from 80 to 81
Segment from 92 to 93
Segment from 98 to 99
Segment from 104 to 105
Segment from 106 to 107
Segment from 110 to 111
Segment from 111 to 112
Segment from 113 to 114
Segment from 114 to 115
Segment from 115 to 116
Segment from 116 to 117
Segment from 118 to 119
Segment from 119 to 120
Segment from 124 to 125
Segment from 127 to 128
Segment from 130 to 131
Segment from 137 to 138
Segment from 139 to 140
Segment from 143 to 144
Segment from 145 to 146
Segment from 146 to 147
Segment from 147 to 148
Segment from 148 to 149
```
每个元素表示一个分割区间的起始和结束位置。
阅读全文