forked from Shivi91/Rosalind-1
-
Notifications
You must be signed in to change notification settings - Fork 4
/
014_LCSM.py
43 lines (35 loc) · 1.32 KB
/
014_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
42
43
#!/usr/bin/env python
'''
A solution to a ROSALIND bioinformatics problem.
Problem Title: Finding a Shared Motif
Rosalind ID: LCSM
Rosalind #: 014
URL: http://rosalind.info/problems/lcsm/
'''
from scripts import ReadFASTA
def LongestSubstring(string_list):
'''Extracts all substrings from the first string in a list, and sends longest substring candidates to be checked.'''
longest = ''
for start_index in xrange(len(string_list[0])):
for end_index in xrange(len(string_list[0]), start_index, -1):
# Break if the length becomes too small, as it will only get smaller.
if end_index - start_index <= len(longest):
break
elif CheckSubstring(string_list[0][start_index:end_index], string_list):
longest = string_list[0][start_index:end_index]
return longest
def CheckSubstring(find_string, string_list):
'Checks if a given substring appears in all members of a given collection of strings and returns True/False.'
for string in string_list:
if (len(string) < len(find_string)) or (find_string not in string):
return False
return True
if __name__ == '__main__':
fasta_list = ReadFASTA('data/rosalind_lcsm.txt')
dna = []
for fasta in fasta_list:
dna.append(fasta[1])
lcsm = LongestSubstring(dna)
print lcsm
with open('output/014_LCSM.txt', 'w') as output_data:
output_data.write(lcsm)