-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcsm.py
41 lines (31 loc) · 1.12 KB
/
lcsm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @File : lcsm.py
# @Date : 2019-02-18
# @Author : luyang([email protected])
import numpy as np
from Bio import SeqIO
def matrix(seq1, seq2):
matrix_record = np.zeros([len(seq1) + 1, len(seq2) + 1], dtype=np.int64)
for i in range(len(seq1)):
for j in range(len(seq2)):
if seq1[i] == seq2[j]:
matrix_record[i + 1, j + 1] = matrix_record[i, j] + 1
else:
matrix_record[i + 1, j + 1] = 0
return matrix_record
def traceback(matrix_record, seq1):
location = np.argwhere(matrix_record == np.max(matrix_record))
length = matrix_record[location[0, 0], location[0, 1]]
longest = seq1[location[0, 0] - length: location[0, 0]]
return longest
def main():
file = 'input/rosalind_lcsm.txt'
records = list(SeqIO.parse(file, "fasta"))
shared_motif = str(records[0].seq)
for i in range(1, len(records)):
matrix_record = matrix(shared_motif, str(records[i].seq))
shared_motif = traceback(matrix_record, shared_motif)
print(shared_motif)
if __name__ == "__main__":
main()