This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
team_3_prettyPrintTSPAlignments.py
79 lines (62 loc) · 2.58 KB
/
team_3_prettyPrintTSPAlignments.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
"""
A program to take the alignments generated by the max. TSP algorithm and print them
in a user-readable way.
Relies on you first having run team_3_tsp.py in the current working directory.
Tyler Young and Bob Barnhart
Written for Python 3
"""
from team_3_scoreAlignments import overlap, getFragments
def getSimplifiedFragments(filename):
f = open(filename, 'rU')
resultList = []
# initialize the sequence accumulator
for line in f:
line = line.rstrip()
# ignore blank lines
if line != '':
resultList.append(line)
return resultList
def printAlignments( listOfFragments, alignmentList ):
offset = 0
for pair in alignmentList:
if pair[0] == pair[1] == 0:
break
print( (" "*offset), listOfFragments[pair[0]], sep="" )
theOverlap, addToOffset = overlap(listOfFragments[pair[0]], listOfFragments[pair[1]])
#print("Overlap between",listOfFragments[pair[0]], "and", listOfFragments[pair[1]], " was", theOverlap)
offset += addToOffset
# Count the base pairs:
count = 0
for fragment in listOfFragments:
count += len(fragment)
print("\n\nNumber of base pairs is",count)
print("Expected length of the final sequence is",count,"/ (4*5) = ",count/(4*5))
print("Actual length of the sequence is a bit more than",offset)
def writeAlignmentCSV( fileToWriteTo, fragments, alignments ):
offset = 0
csvFile = open(fileToWriteTo, "w")
for pair in alignments:
if pair[0] == pair[1] == 0:
break
csvFile.write( (" ,"*offset) )
for letter in fragments[pair[0]]:
csvFile.write( letter + "," )
csvFile.write("\n")
theOverlap, addToOffset = overlap(fragments[pair[0]], fragments[pair[1]])
#print("Overlap between",fragments[pair[0]], "and", fragments[pair[1]], " was", theOverlap)
offset += addToOffset
def main():
fragments = getSimplifiedFragments('fragments.txt')
alignmentFile = open("alignmentOrder.txt", "r")
# Strip the comments
fileContents = ( alignmentFile.read().split("#") )[0] #ignore everything after the hash
alignmentSequence = fileContents.split() # split the sequence on spaces
print("Alignment seq is", alignmentSequence)
alignments = []
for i in range( len(alignmentSequence)-1 ):
alignments.append( ( int(alignmentSequence[i]), int(alignmentSequence[i+1])) )
# Print the alignments normally
printAlignments( fragments, alignments )
# Write the alignments to a CSV file
writeAlignmentCSV( "alignments.csv", fragments, alignments )
main()