forked from weka511/bioinformatics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BA7F.py
80 lines (68 loc) · 2.94 KB
/
BA7F.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
# Copyright (C) 2017-2020 Greenweaves Software Limited
#
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
# BA7F Implement SmallParsimony
import argparse
import os
import time
from helpers import read_strings
import operator,random
from rosalind import LabelledTree,hamm
from phylogeny import SmallParsimony
def print_assignments(assignments):
assignments.nodes.sort()
for node in assignments.nodes:
if node in assignments.edges:
for edge in assignments.edges[node]:
end,weight=edge
if node in assignments.labels and end in assignments.labels:
print ('{0}->{1}:{2}'.format(assignments.labels[node],
assignments.labels[end],
hamm(assignments.labels[node],assignments.labels[end])))
if __name__=='__main__':
start = time.time()
parser = argparse.ArgumentParser('BA7F Implement SmallParsimony ')
parser.add_argument('--sample', default=False, action='store_true', help='process sample dataset')
parser.add_argument('--rosalind', default=False, action='store_true', help='process Rosalind dataset')
args = parser.parse_args()
if args.sample:
N = 4
T = LabelledTree.parse(4,
['4->CAAATCCC',
'4->ATTGCGAC',
'5->CTGCGCTG',
'5->ATGGACGA',
'6->4',
'6->5'],
bidirectional=False
)
score,assignments=SmallParsimony(T)
print (score)
print_assignments(assignments)
if args.rosalind:
Input = read_strings(f'data/rosalind_{os.path.basename(__file__).split(".")[0]}.txt')
T = LabelledTree.parse(int(Input[0]),Input[1],bidirectional=False)
score,assignments=SmallParsimony(T)
print (score)
print_assignments(assignments)
Result = None
print (Result)
with open(f'{os.path.basename(__file__).split(".")[0]}.txt','w') as f:
for line in Result:
f.write(f'{line}\n')
elapsed = time.time() - start
minutes = int(elapsed/60)
seconds = elapsed - 60*minutes
print (f'Elapsed Time {minutes} m {seconds:.2f} s')