-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compute Limb Lengths in a Tree.py
49 lines (38 loc) · 1.5 KB
/
Compute Limb Lengths in a Tree.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
import os
import sys
from Bio import SeqIO
def computelimblengths(n, j, D):
return int(
min([D[i][j] + D[j][k] - D[i][k] for i in range(n) for k in range(n) if j != k and k != i and i != j]) / 2)
def read_matrix(problem=os.path.basename(sys.argv[0]).split('.')[0],
path=os.path.join(os.path.expanduser('~'), 'Downloads'),
ext=None,
conv=int,
len_params=1,
name=None):
params = []
D = []
for line in create_strings(problem=problem, path=path, ext=ext, name=name):
if len(params) < len_params:
params.append(int(line))
else:
D.append([conv(s) for s in line.split()])
return (params, D)
def create_strings(problem=os.path.basename(sys.argv[0]).split('.')[0],
path=os.path.join(os.path.expanduser('~'), 'Downloads'),
ext=None,
fasta=False,
name=None):
product = []
with open("C:/Users/iMan/Desktop/rosalind_ba7b.txt", 'r') as f:
if fasta:
for record in SeqIO.parse(f, 'C:/Users/iMan/Desktop/rosalind_ba7b.txt'):
product.append(str(record.seq))
else:
for line in f:
product.append(line.strip())
return product
if __name__ == '__main__':
from helpers import read_matrix
params, D = read_matrix(len_params=2)
print(computelimblengths(params[0], params[1], D))